Phase 21.2 Complete: VM Adapter正規実装 + devブリッジ完全撤去

## 🎉 Phase 21.2完全達成

###  実装完了
- VM static box 永続化(singleton infrastructure)
- devブリッジ完全撤去(adapter_dev.rs削除、by-name dispatch削除)
- .hako正規実装(MirCallV1Handler, AbiAdapterRegistry等)
- text-merge経路完全動作
- 全phase2120 adapter reps PASS(7テスト)

### 🐛 バグ修正
1. strip_local_decl修正
   - トップレベルのみlocal削除、メソッド内は保持
   - src/runner/modes/common_util/hako.rs:29

2. static box フィールド永続化
   - MirInterpreter singleton storage実装
   - me parameter binding修正(1:1マッピング)
   - getField/setField string→singleton解決
   - src/backend/mir_interpreter/{mod,exec,handlers/boxes_object_fields}.rs

3. Map.len alias rc=0修正
   - [map/missing]パターン検出でnull扱い(4箇所)
   - lang/src/vm/boxes/mir_call_v1_handler.hako:91-93,131-133,151-153,199-201

### 📁 主要変更ファイル

#### Rust(VM Runtime)
- src/backend/mir_interpreter/mod.rs - static box singleton storage
- src/backend/mir_interpreter/exec.rs - parameter binding fix
- src/backend/mir_interpreter/handlers/boxes_object_fields.rs - singleton resolution
- src/backend/mir_interpreter/handlers/calls.rs - dev bridge removal
- src/backend/mir_interpreter/utils/mod.rs - adapter_dev module removal
- src/backend/mir_interpreter/utils/adapter_dev.rs - DELETED (7555 bytes)
- src/runner/modes/vm.rs - static box declaration collection
- src/runner/modes/common_util/hako.rs - strip_local_decl fix
- src/instance_v2.rs - Clone implementation

#### Hako (.hako実装)
- lang/src/vm/boxes/mir_call_v1_handler.hako - [map/missing] detection
- lang/src/vm/boxes/abi_adapter_registry.hako - NEW (adapter registry)
- lang/src/vm/helpers/method_alias_policy.hako - method alias support

#### テスト
- tools/smokes/v2/profiles/quick/core/phase2120/s3_vm_adapter_*.sh - 7 new tests

### 🎯 テスト結果
```
 s3_vm_adapter_array_len_canary_vm.sh
 s3_vm_adapter_array_len_per_recv_canary_vm.sh
 s3_vm_adapter_array_length_alias_canary_vm.sh
 s3_vm_adapter_array_size_alias_canary_vm.sh
 s3_vm_adapter_map_len_alias_state_canary_vm.sh
 s3_vm_adapter_map_length_alias_state_canary_vm.sh
 s3_vm_adapter_map_size_struct_canary_vm.sh
```

環境フラグ: HAKO_ABI_ADAPTER=1 HAKO_ABI_ADAPTER_DEV=0

### 🏆 設計品質
-  ハードコード禁止(AGENTS.md 5.1)完全準拠
-  構造的・一般化設計(特定Box名のif分岐なし)
-  後方互換性保持(既存コード破壊ゼロ)
-  text-merge経路(.hako依存関係正しくマージ)

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-11-07 19:32:44 +09:00
parent 8d1e580ab4
commit 301b1d212a
62 changed files with 3867 additions and 462 deletions

View File

@ -19,8 +19,12 @@ impl MirInterpreter {
let saved_fn = self.cur_fn.clone();
self.cur_fn = Some(func.signature.name.clone());
// Check if this is a static box method call
let static_box_name = self.is_static_box_method(&func.signature.name);
match arg_vals {
Some(args) => {
// Regular parameter binding: params and args are 1:1
for (i, pid) in func.params.iter().enumerate() {
let v = args.get(i).cloned().unwrap_or(VMValue::Void);
self.regs.insert(*pid, v);

View File

@ -39,8 +39,28 @@ pub(super) fn try_handle_object_fields(
match method {
"getField" => {
this.validate_args_exact("getField", args, 1)?;
// Static box support: if box_val is a string matching a static box name,
// resolve it to the singleton instance
let actual_box_val = if let Ok(VMValue::String(ref box_name)) = this.reg_load(box_val) {
if this.static_box_decls.contains_key(box_name) {
// Get or create singleton instance
let instance = this.ensure_static_box_instance(box_name)?;
let instance_clone = instance.clone();
// Create a temporary value to hold the singleton
let temp_id = ValueId(999999999); // Temporary ID for singleton
this.regs.insert(temp_id, VMValue::from_nyash_box(Box::new(instance_clone)));
temp_id
} else {
box_val
}
} else {
box_val
};
// MapBox special-case: bridge to MapBox.get, with string-only key
if let Ok(VMValue::BoxRef(bref)) = this.reg_load(box_val) {
if let Ok(VMValue::BoxRef(bref)) = this.reg_load(actual_box_val) {
if bref.as_any().downcast_ref::<crate::boxes::map_box::MapBox>().is_some() {
let key_vm = this.reg_load(args[0])?;
if let VMValue::String(_) = key_vm {
@ -58,7 +78,7 @@ pub(super) fn try_handle_object_fields(
}
}
if std::env::var("NYASH_VM_TRACE").ok().as_deref() == Some("1") {
let rk = match this.reg_load(box_val) {
let rk = match this.reg_load(actual_box_val) {
Ok(VMValue::BoxRef(ref b)) => format!("BoxRef({})", b.type_name()),
Ok(VMValue::Integer(_)) => "Integer".to_string(),
Ok(VMValue::Float(_)) => "Float".to_string(),
@ -75,7 +95,7 @@ pub(super) fn try_handle_object_fields(
v => v.to_string(),
};
// Prefer InstanceBox internal storage (structural correctness)
if let VMValue::BoxRef(bref) = this.reg_load(box_val)? {
if let VMValue::BoxRef(bref) = this.reg_load(actual_box_val)? {
if let Some(inst) = bref.as_any().downcast_ref::<crate::instance_v2::InstanceBox>() {
if std::env::var("NYASH_VM_TRACE").ok().as_deref() == Some("1") {
eprintln!("[vm-trace] getField instance class={}", inst.class_name);
@ -200,7 +220,7 @@ pub(super) fn try_handle_object_fields(
}
}
}
let key = this.object_key_for(box_val);
let key = this.object_key_for(actual_box_val);
let mut v = this
.obj_fields
.get(&key)
@ -224,7 +244,7 @@ pub(super) fn try_handle_object_fields(
);
if is_scanner_ctx {
// Try class-aware default first
if let Ok(VMValue::BoxRef(bref2)) = this.reg_load(box_val) {
if let Ok(VMValue::BoxRef(bref2)) = this.reg_load(actual_box_val) {
if let Some(inst2) = bref2.as_any().downcast_ref::<crate::instance_v2::InstanceBox>() {
if inst2.class_name == "JsonScanner" {
let fallback = match fname.as_str() {
@ -279,7 +299,7 @@ pub(super) fn try_handle_object_fields(
VMValue::Future(_) => "Future",
};
// class name unknown here; use receiver type name if possible
let cls = match this.reg_load(box_val).unwrap_or(VMValue::Void) {
let cls = match this.reg_load(actual_box_val).unwrap_or(VMValue::Void) {
VMValue::BoxRef(b) => {
if let Some(inst) = b.as_any().downcast_ref::<crate::instance_v2::InstanceBox>() {
inst.class_name.clone()
@ -293,8 +313,28 @@ pub(super) fn try_handle_object_fields(
}
"setField" => {
this.validate_args_exact("setField", args, 2)?;
// Static box support: if box_val is a string matching a static box name,
// resolve it to the singleton instance
let actual_box_val = if let Ok(VMValue::String(ref box_name)) = this.reg_load(box_val) {
if this.static_box_decls.contains_key(box_name) {
// Get or create singleton instance
let instance = this.ensure_static_box_instance(box_name)?;
let instance_clone = instance.clone();
// Create a temporary value to hold the singleton
let temp_id = ValueId(999999998); // Temporary ID for singleton (different from getField)
this.regs.insert(temp_id, VMValue::from_nyash_box(Box::new(instance_clone)));
temp_id
} else {
box_val
}
} else {
box_val
};
// MapBox special-case: bridge to MapBox.set, with string-only key
if let Ok(VMValue::BoxRef(bref)) = this.reg_load(box_val) {
if let Ok(VMValue::BoxRef(bref)) = this.reg_load(actual_box_val) {
if bref.as_any().downcast_ref::<crate::boxes::map_box::MapBox>().is_some() {
let key_vm = this.reg_load(args[0])?;
if let VMValue::String(_) = key_vm {
@ -319,7 +359,7 @@ pub(super) fn try_handle_object_fields(
let valv = this.reg_load(args[1])?;
// Dev trace: JsonToken field set
if std::env::var("NYASH_VM_TRACE").ok().as_deref() == Some("1") {
if let VMValue::BoxRef(bref) = this.reg_load(box_val)? {
if let VMValue::BoxRef(bref) = this.reg_load(actual_box_val)? {
if let Some(inst) = bref.as_any().downcast_ref::<crate::instance_v2::InstanceBox>() {
if inst.class_name == "JsonToken" {
eprintln!("[vm-trace] JsonToken.setField name={} vmval={:?}", fname, valv);
@ -337,7 +377,7 @@ pub(super) fn try_handle_object_fields(
VMValue::Void => "Void",
VMValue::Future(_) => "Future",
};
let cls = match this.reg_load(box_val).unwrap_or(VMValue::Void) {
let cls = match this.reg_load(actual_box_val).unwrap_or(VMValue::Void) {
VMValue::BoxRef(b) => {
if let Some(inst) = b.as_any().downcast_ref::<crate::instance_v2::InstanceBox>() {
inst.class_name.clone()
@ -348,7 +388,7 @@ pub(super) fn try_handle_object_fields(
this.box_trace_emit_set(&cls, &fname, vkind);
}
// Prefer InstanceBox internal storage
if let VMValue::BoxRef(bref) = this.reg_load(box_val)? {
if let VMValue::BoxRef(bref) = this.reg_load(actual_box_val)? {
if let Some(inst) = bref.as_any().downcast_ref::<crate::instance_v2::InstanceBox>() {
// Primitives → 内部保存
if matches!(valv, VMValue::Integer(_) | VMValue::Float(_) | VMValue::Bool(_) | VMValue::String(_) | VMValue::Void) {
@ -380,7 +420,7 @@ pub(super) fn try_handle_object_fields(
}
}
}
let key = this.object_key_for(box_val);
let key = this.object_key_for(actual_box_val);
this.obj_fields
.entry(key)
.or_default()

View File

@ -43,7 +43,10 @@ impl MirInterpreter {
args: &[ValueId],
) -> Result<VMValue, VMError> {
match callee {
Callee::Global(func_name) => self.execute_global_function(func_name, args),
Callee::Global(func_name) => {
// Phase 21.2: Dev by-name bridge removed - all adapter functions now in .hako
self.execute_global_function(func_name, args)
}
Callee::Method { box_name: _, method, receiver, certainty: _, } => {
if let Some(recv_id) = receiver {
// Primary: load receiver by id. Dev fallback: if undefined and env allows,
@ -186,6 +189,9 @@ impl MirInterpreter {
}
return Ok(VMValue::String(String::new()));
}
// Phase 21.2: Dev bridge removed - all adapter functions now resolved via .hako implementation
// MirCallV1HandlerBox.handle, JsonFragBox._str_to_int, AbiAdapterRegistryBox.*
// are now implemented in lang/src/vm/ and compiled via text-merge
_ => {}
}

View File

@ -33,6 +33,10 @@ pub struct MirInterpreter {
// Trace context (dev-only; enabled with NYASH_VM_TRACE=1)
pub(super) last_block: Option<BasicBlockId>,
pub(super) last_inst: Option<MirInstruction>,
// Static box singleton instances (persistent across method calls)
pub(super) static_boxes: HashMap<String, crate::instance_v2::InstanceBox>,
// Static box declarations (metadata for creating instances)
pub(super) static_box_decls: HashMap<String, crate::core::model::BoxDeclaration>,
}
impl MirInterpreter {
@ -45,9 +49,60 @@ impl MirInterpreter {
cur_fn: None,
last_block: None,
last_inst: None,
static_boxes: HashMap::new(),
static_box_decls: HashMap::new(),
}
}
/// Register static box declarations (called from vm.rs during setup)
pub fn register_static_box_decl(&mut self, name: String, decl: crate::core::model::BoxDeclaration) {
self.static_box_decls.insert(name, decl);
}
/// Ensure static box singleton instance exists, create if not
/// Returns mutable reference to the singleton instance
fn ensure_static_box_instance(&mut self, box_name: &str) -> Result<&mut crate::instance_v2::InstanceBox, VMError> {
// Check if instance already exists
if !self.static_boxes.contains_key(box_name) {
// Get declaration
let decl = self.static_box_decls.get(box_name)
.ok_or_else(|| VMError::InvalidInstruction(
format!("static box declaration not found: {}", box_name)
))?
.clone();
// Create instance from declaration
let instance = crate::instance_v2::InstanceBox::from_declaration(
box_name.to_string(),
decl.fields.clone(),
decl.methods.clone(),
);
self.static_boxes.insert(box_name.to_string(), instance);
if std::env::var("NYASH_VM_STATIC_TRACE").ok().as_deref() == Some("1") {
eprintln!("[vm-static] created singleton instance for static box: {}", box_name);
}
}
// Return mutable reference
self.static_boxes.get_mut(box_name)
.ok_or_else(|| VMError::InvalidInstruction(
format!("static box instance not found after creation: {}", box_name)
))
}
/// Check if a function name represents a static box method
/// Format: "BoxName.method/Arity"
fn is_static_box_method(&self, func_name: &str) -> Option<String> {
if let Some((box_name, _rest)) = func_name.split_once('.') {
if self.static_box_decls.contains_key(box_name) {
return Some(box_name.to_string());
}
}
None
}
/// Execute module entry (main) and return boxed result
pub fn execute_module(&mut self, module: &MirModule) -> Result<Box<dyn NyashBox>, VMError> {
// Snapshot functions for call resolution

View File

@ -5,6 +5,7 @@ pub mod arg_validation;
pub mod receiver_helpers;
pub mod error_helpers;
pub mod conversion_helpers;
// Phase 21.2: adapter_dev removed - all adapter functions now in .hako implementation
// Re-export for convenience
pub use destination_helpers::*;