refactor(vm): StaticBoxRegistry unifies static box management

箱化モジュール化: Phase 173-B の散在した static box 管理を一元化

Before (4箇所に散在):
- static_box_decls: HashMap (AST経由のBox宣言)
- static_boxes: HashMap (実行時シングルトン)
- vm.rs Phase 173-B手動検出コード (~60行)
- method.rs static_box_decls.contains_key() 直接参照

After (StaticBoxRegistry箱に統一):
- declarations: AST経由のBox宣言を登録
- detected_boxes: MIR関数名から自動検出 (using import対応)
- instances: 遅延作成シングルトン
- naming utilities: parse/format関数

Benefits:
- vm.rs: 63行削減 (Phase 173-B手動コード削除)
- 自動検出: using import した static box も対応
- 単一責務: static box lifecycle を1箱に集約
- Fail-Fast: 存在しないBoxへのアクセスは即エラー

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-12-04 18:56:23 +09:00
parent 9d29718f03
commit 397bc77eb3
5 changed files with 316 additions and 119 deletions

View File

@ -496,74 +496,11 @@ impl NyashRunner {
use crate::backend::MirInterpreter;
let mut vm = MirInterpreter::new();
// Register static box declarations for singleton persistence
// Register static box declarations for singleton persistence (AST-based)
for (name, decl) in static_box_decls {
vm.register_static_box_decl(name, decl);
}
// 🎯 Phase 173-B: Register using-imported static boxes from MIR function names
// Extract box names from MIR function patterns like "BoxName.method/arity"
// This enables VM singleton creation for using-imported static boxes
{
use std::collections::HashSet;
let mut imported_boxes: HashSet<String> = HashSet::new();
for fn_name in module_vm.functions.keys() {
// Parse "BoxName.method/arity" pattern
if let Some(dot_pos) = fn_name.find('.') {
let box_name = &fn_name[..dot_pos];
// Skip known non-static boxes (runtime data boxes)
if !matches!(
box_name,
"Main"
| "main"
| "StringBox"
| "IntegerBox"
| "BoolBox"
| "ArrayBox"
| "MapBox"
| "FileBox"
| "NetBox"
) {
imported_boxes.insert(box_name.to_string());
}
}
}
// Register any boxes not already in static_box_decls
for box_name in imported_boxes {
if !vm.has_static_box_decl(&box_name) {
// Create minimal BoxDeclaration for using-imported boxes
// VM only needs the name for singleton creation
let decl = crate::core::model::BoxDeclaration {
name: box_name.clone(),
fields: Vec::new(),
public_fields: Vec::new(),
private_fields: Vec::new(),
methods: std::collections::HashMap::new(),
constructors: std::collections::HashMap::new(),
init_fields: Vec::new(),
weak_fields: Vec::new(),
is_interface: false,
extends: Vec::new(),
implements: Vec::new(),
type_parameters: Vec::new(),
};
if std::env::var("NYASH_USING_STATIC_BOX_TRACE")
.ok()
.as_deref()
== Some("1")
{
eprintln!(
"[phase173-b] Registering using-imported static box: {}",
box_name
);
}
vm.register_static_box_decl(box_name, decl);
}
}
}
// Optional: verify MIR before execution (dev-only)
if crate::config::env::env_bool("NYASH_VM_VERIFY_MIR") {
let ring0 = crate::runtime::ring0::get_global_ring0();