diff --git a/docs/development/roadmap/phases/phase-9/phase_9_79b_2_vm_vtable_thunks_and_pic.md b/docs/development/roadmap/phases/phase-9/phase_9_79b_2_vm_vtable_thunks_and_pic.md index 10ed4296..45e471a6 100644 --- a/docs/development/roadmap/phases/phase-9/phase_9_79b_2_vm_vtable_thunks_and_pic.md +++ b/docs/development/roadmap/phases/phase-9/phase_9_79b_2_vm_vtable_thunks_and_pic.md @@ -4,6 +4,7 @@ Status: Planned Owner: core-runtime Target: Before Phase 10 (Cranelift JIT) Last Updated: 2025-08-26 +Progress: Fast-path thunks (universal slots) + PIC skeleton committed ## Goals - Implement unified VM BoxCall path via vtable thunks indexed by `MethodId`. @@ -30,6 +31,16 @@ Last Updated: 2025-08-26 - Monomorphic PIC - Test coverage for core scenarios +## Progress Notes (2025-08-26) +- Implemented fast-path thunks for universal slots (0..3: toString/type/equals/clone) in `execute_boxcall` using `method_id`. +- `MirInstruction::BoxCall` now carries optional `method_id` emitted by the builder when resolvable. +- Added monomorphic PIC skeleton: per-(receiver type, method_id/name) hit counters in the VM. +- Minimal tests: verify fast-path behavior for `type()` and `equals()`. + +Next: +- Threshold-based direct dispatch using per-site cache entries. +- Extend beyond universal slots to general `method_id`-resolved methods. + ## Non-Goals - Polymorphic PIC (plan only) - JIT emission (Phase 10) @@ -48,4 +59,3 @@ Last Updated: 2025-08-26 ## Roll-forward - Phase 10: Cranelift JIT lowers BoxCall to the same thunks; add poly-PIC and codegen stubs. - diff --git a/docs/ideas/improvements/2025-08-26-modular-builtin-box-system.md b/docs/ideas/improvements/2025-08-26-modular-builtin-box-system.md new file mode 100644 index 00000000..92d3a841 --- /dev/null +++ b/docs/ideas/improvements/2025-08-26-modular-builtin-box-system.md @@ -0,0 +1,118 @@ +# モジュラービルトインBoxシステム +Status: Pending +Created: 2025-08-26 +Priority: Medium +Related: ビルトインBox管理の柔軟性向上 + +## 現状の問題 +- ビルトインBoxは本体にハードコードされており、不要な機能も常に含まれる +- プラグインBoxはWASMでは動作しない(.so/.dll依存) +- 特殊用途のBox(MidnightBox等)が常にビルドに含まれる + +## 提案:3層アーキテクチャ + +``` +┌─────────────────────────────────────┐ +│ Core Builtin Boxes │ ← 絶対必要(String, Integer等) +├─────────────────────────────────────┤ +│ Optional Builtin Modules │ ← 条件付きコンパイル(MidnightBox等) +├─────────────────────────────────────┤ +│ Plugin Boxes (BID) │ ← 外部プラグイン(FileBox等) +└─────────────────────────────────────┘ +``` + +## 実装案 + +### 1. BuiltinModuleトレイト +```rust +pub trait BuiltinModule: Send + Sync { + fn name(&self) -> &'static str; + fn create_box(&self, args: Vec>) -> Option>; + fn required_features(&self) -> Vec<&'static str> { vec![] } +} + +pub struct BuiltinModuleRegistry { + modules: HashMap>, + enabled: HashSet, +} +``` + +### 2. 設定ファイルベース管理 +```toml +# nyash.toml +[builtin_modules] +core = ["StringBox", "IntegerBox", "BoolBox", "ArrayBox", "MapBox"] + +[builtin_modules.optional] +midnight = { enabled = true, features = ["testnet"] } +egui = { enabled = false } +webcanvas = { enabled = true, target = "wasm32" } +``` + +### 3. Cargo features統合 +```toml +[features] +default = ["core-boxes"] + +# オプショナルモジュール +builtin-midnight = ["web-sys", "js-sys", "wasm-bindgen"] +builtin-egui = ["egui", "eframe"] +builtin-webcanvas = ["web-sys/CanvasRenderingContext2d"] + +# プリセット +full = ["builtin-midnight", "builtin-egui", "builtin-webcanvas", "plugins"] +minimal = ["core-boxes"] +wasm-web = ["core-boxes", "builtin-webcanvas", "builtin-midnight"] +``` + +### 4. 動的登録マクロ +```rust +builtin_module! { + MidnightModule, + condition = cfg!(feature = "builtin-midnight") +} +``` + +## 利点 +1. **バイナリサイズ削減**: 必要な機能のみ含める +2. **ビルド時間短縮**: 不要な依存関係を除外 +3. **WASM対応**: Rust WASM機能をフル活用可能 +4. **設定の柔軟性**: ビルド時/実行時の両方で制御 +5. **後方互換性**: 既存のコードは変更不要 + +## 使用例 + +```nyash +// 条件付き実行 +if hasBox("MidnightBox") { + local voting = new VotingApp() + voting.run() +} else { + print("MidnightBox is not available in this build") +} +``` + +## ビルドコマンド例 +```bash +# 最小構成 +cargo build --release + +# Midnight対応版 +cargo build --release --features builtin-midnight + +# フル機能版 +cargo build --release --features full + +# WASM Web版 +cargo build --target wasm32-unknown-unknown --features wasm-web +``` + +## 実装タイミング +- Phase 10以降の最適化フェーズで検討 +- プラグインシステムの成熟後に実装 +- パフォーマンス問題が顕在化した場合に優先度上げ + +## 関連課題 +- プラグインシステムとの統合 +- 設定ファイル管理の標準化 +- ドキュメント自動生成(有効なBoxの一覧) \ No newline at end of file diff --git a/src/backend/vm.rs b/src/backend/vm.rs index 096229c4..56791969 100644 --- a/src/backend/vm.rs +++ b/src/backend/vm.rs @@ -1018,4 +1018,32 @@ console.log("ok") let result = vm.execute_module(&compile_result.module).expect("vm exec failed"); assert_eq!(result.to_string_box().value, "void"); } + + #[test] + fn test_vm_fastpath_universal_type_and_equals() { + // toString/type/equals/cloneのうち、typeとequalsのfast-pathを検証 + // 1) type: (new ArrayBox()).type() == "ArrayBox" + let code_type = r#" +return (new ArrayBox()).type() +"#; + let ast = NyashParser::parse_from_string(code_type).expect("parse failed"); + let runtime = NyashRuntime::new(); + let mut compiler = crate::mir::MirCompiler::new(); + let compile_result = compiler.compile(ast).expect("mir compile failed"); + let mut vm = VM::with_runtime(runtime); + let result = vm.execute_module(&compile_result.module).expect("vm exec failed"); + assert_eq!(result.to_string_box().value, "ArrayBox"); + + // 2) equals: (new IntegerBox(5)).equals(5) == true + let code_eq = r#" +return (new IntegerBox(5)).equals(5) +"#; + let ast = NyashParser::parse_from_string(code_eq).expect("parse failed"); + let runtime = NyashRuntime::new(); + let mut compiler = crate::mir::MirCompiler::new(); + let compile_result = compiler.compile(ast).expect("mir compile failed"); + let mut vm = VM::with_runtime(runtime); + let result = vm.execute_module(&compile_result.module).expect("vm exec failed"); + assert_eq!(result.to_string_box().value, "true"); + } }