Files
hakorune/docs/development/current/function_values_and_captures.md
Selfhosting Dev 81211c22ad feat: MIR Call命令統一Phase 3.1-3.2完了!統一Call実装進行中
 Phase 3.1-3.2実装完了
- build_indirect_call_expressionでCallTarget::Value使用
- print関数をcall_global print()として統一
- build_function_callでemit_unified_call使用
- ExternCall(env.console.log)→Callee::Global(print)完全移行

🏗️ MIR統一基盤構築
- src/mir/definitions/call_unified.rs: 統一定義(297行)
- emit_unified_call()と便利メソッド3種実装
- NYASH_MIR_UNIFIED_CALL=1で段階移行制御
- VM実行器でCallee対応実装済み

📊 進捗状況(26%削減見込み)
- Phase 1-2:  基盤構築完了
- Phase 3.1-3.2:  基本関数統一完了
- Phase 3.3: 🔄 BoxCall統一中
- Phase 4: 📅 Python LLVM(最優先・63%削減)
- Phase 5: 📅 PyVM/VM統一

📚 ドキュメント更新
- CLAUDE.md: テストスクリプト参考集追加
- CURRENT_TASK.md: Phase 3進捗更新
- python-llvm-priority-rationale.md: 優先順位戦略文書化
- mir-call-unification-master-plan.md: スケジュール最新化

🎯 6種類→1種類: Call/BoxCall/PluginInvoke/ExternCall/NewBox/NewClosure → MirCall統一へ

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-24 01:05:44 +09:00

2.0 KiB

Function Values, Captures, and Events

Summary of current behavior and guidelines.

  • Function values: created via function(...) { ... } produce a FunctionBox with captured environment (by-ref for locals via RefCellBox, by-value for globals/statics) and optional weak me.
  • Assignment cell reflection: assigning to a variable or field bound to RefCellBox updates the inner value instead of replacing the cell.
  • this → me: inside methods, this is bound as me for field/method access. External code should prefer me.
  • Parent:: syntax: parser groundwork exists for Parent::method references; calling from child uses from-call lowering.
  • ? (propagate): expr ? lowers to isOk/getValue branching and returns early with the Result on error.
  • peek: tokenized and parsed; desugars to if-else chain in VM.

Event APIs

  • P2PBox.on/onOnce/off: handlers now accept both MethodBox and FunctionBox.
    • MethodBox handlers invoke the bound method on receive with arguments (intent, from).
    • FunctionBox handlers execute the function body with params bound from (intent, from) (excess args ignored).

Notes

  • RefCell-backed locals captured by closures will reflect assignments (x = ...) in the outer scope.
  • For plugin-backed boxes, assignment and argument passing uses share semantics to preserve identity.

MIR/VM call unification (Phase 12)

  • MIR Call: accepts either a function name (String) or a FunctionBox value.
    • If the callee is a String, VM performs a named-function dispatch (existing path).
    • If the callee is a FunctionBox (BoxRef), VM runs it via the interpreter helper with captures/me injected and proper return propagation.
  • Lambda immediate calls are still directly lowered inline for P1 compatibility.
  • Lambda→FunctionBox: Lambda expressions now lower to a NewClosure MIR instruction that constructs a FunctionBox value (minimal: captures currently omitted). This enables MIR-only pipelines to construct and call function values.