Files
hakorune/docs/development/roadmap/phases/phase-7/phase7_async_mir.md
Moe Charm cc2a820af7 feat(plugin): Fix plugin BoxRef return and Box argument support
- Fixed deadlock in FileBox plugin copyFrom implementation (single lock)
- Added TLV Handle (tag=8) parsing in calls.rs for returned BoxRefs
- Improved plugin loader with config path consistency and detailed logging
- Fixed loader routing for proper Handle type_id/fini_method_id resolution
- Added detailed logging for TLV encoding/decoding in plugin_loader_v2

Test docs/examples/plugin_boxref_return.nyash now works correctly:
- cloneSelf() returns FileBox Handle properly
- copyFrom(Box) accepts plugin Box arguments
- Both FileBox instances close and fini correctly

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-21 00:41:26 +09:00

3.2 KiB
Raw Blame History

Phase 7: Async model in MIR (nowait/await)

Summary

  • nowait/await を MIR に薄く導入Future 表現)。スレッドベース実装と整合。
  • 既存のFutureBox実装を活用し、MIR/VMレイヤーで非同期処理を表現。

Background

  • Nyashでは既にFutureBoxが実装済みsrc/boxes/future/mod.rs
  • nowait/awaitはトークン・ASTードとして定義済み
  • 現在のインタープリターではthread::spawnベースの実装

Scope

MIR命令の追加

  • FutureNew { dst, value } - 新しいFuture作成初期値付き
  • FutureSet { future, value } - Futureに値を設定
  • Await { dst, future } - Futureの完了を待って値を取得

Lowering実装

  • ASTNode::Nowait { variable, expression }
    1. expressionを評価
    2. FutureNew命令でFuture作成
    3. 別スレッドでの実行をスケジュール
  • ASTNode::AwaitExpression { expression }
    1. expressionを評価Future値を期待
    2. Await命令で値取得

VM実装

  • FutureNew: 新しいVMValue::Future作成
  • FutureSet: Future値の更新is_readyフラグも設定
  • Await: Future完了まで待機してから値を返す

Tasks

  • Phase 7.1: MIR命令定義
    • src/mir/instruction.rsにFutureNew/FutureSet/Await追加
    • Effect maskの設定FutureNewはPURE、AwaitはREAD
    • printer/verificationサポート
  • Phase 7.2: AST→MIR lowering
    • src/mir/builder.rsにnowait/awaitの処理追加
    • 適切なbasic block分割awaitは制御フローに影響
  • Phase 7.3: VM実装
    • src/backend/vm.rsにVMValue::Future追加
    • 各命令の実行ロジック実装
    • FutureBoxとの統合
  • Phase 7.4: テスト・検証
    • 基本的なnowait/awaitのテストケース
    • 複数のnowait実行順序テスト
    • エラーケースFuture未完了時の扱い等

Test Cases

// 基本的なnowait/await
static box Main {
  main() {
    nowait f1 = compute(10)
    nowait f2 = compute(20)
    local result1 = await f1
    local result2 = await f2
    print(result1 + result2)
  }
}

// ネストしたnowait
static box Main {
  main() {
    nowait outer = {
      nowait inner = compute(5)
      await inner * 2
    }
    print(await outer)
  }
}

Acceptance Criteria

  • 上記テストケースがMIRダンプで正しい命令列を生成
  • VM実行で期待通りの結果並行実行→正しい順序で結果取得
  • 既存のFutureBox実装との整合性維持
  • verifierがFuture関連の不正を検出

Implementation Notes

  • 初期実装ではシンプルにthread::spawnベース継続
  • Futureの型情報は当面VMValue内で管理型システムは後続フェーズ
  • エラー処理は最小限Future未完了時のawaitはブロック

Out of Scope (Phase 7)

  • async/await構文Rustライク
  • Promise chain / then構文
  • 取り消し可能なFuture
  • 複雑なスケジューリング戦略
  • Future型の静的型チェック

References

  • docs/nyash_core_concepts.mdnowait/await + FutureBox
  • src/boxes/future/mod.rs既存FutureBox実装
  • src/interpreter/async_methods.rs現在のnowait/await実装