refactor: complete MIR interpreter utility migration (Phase 2)
Migrate remaining argument validation and destination write patterns to utility helpers introduced in Phase 1. Changes: - boxes_map.rs: 135 → 123 lines (-12) * 6 arg validations → validate_args_exact() * 7 destination writes → write_result() - externals.rs: 219 → 206 lines (-13) * 12 destination patterns unified * Simplified env/future/modules handling - boxes_string.rs: 209 → 197 lines (-12) * 4 arg validations + 4 destinations unified * Methods: replace, contains, lastIndexOf, concat, etc. - boxes_array.rs: 64 lines (3 validations migrated) * Methods: push, get, set - boxes_object_fields.rs: 400 → 394 lines (-6) * 2 arg validations (getField, setField) Pattern Elimination: - Argument validation: 15 → 0 (100% eliminated) - Destination writes: 51 → 28 (45% eliminated) Code Reduction: - Phase 2: 43 lines removed (1.3%) - Cumulative: 117-154 lines removed (3.5-4.6% of handlers/) Test Results: - Build: ✅ SUCCESS (no new warnings) - MapBox: ✅ 37/37 passed - StringBox: ✅ 30/30 passed - ArrayBox: ⚠️ 13/14 (1 pre-existing failure) Benefits: - Unified error messages across all handlers - Single source of truth for validation logic - Easier maintenance and future refactoring - Consistent behavior throughout codebase Related: Phase 21.0 refactoring (DUPLICATION_ANALYSIS_REPORT.md) Risk: Low (pure refactoring, behavior preserved)
This commit is contained in:
@ -30,14 +30,13 @@ impl MirInterpreter {
|
||||
if let Some(a0) = args.get(0) {
|
||||
let key = self.reg_load(*a0)?.to_string();
|
||||
let val = std::env::var(&key).ok();
|
||||
if let Some(d) = dst {
|
||||
if let Some(s) = val {
|
||||
self.regs.insert(d, VMValue::String(s));
|
||||
} else {
|
||||
// Represent missing env as null-equivalent (Void)
|
||||
self.regs.insert(d, VMValue::Void);
|
||||
}
|
||||
}
|
||||
let result = if let Some(s) = val {
|
||||
VMValue::String(s)
|
||||
} else {
|
||||
// Represent missing env as null-equivalent (Void)
|
||||
VMValue::Void
|
||||
};
|
||||
self.write_result(dst, result);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -81,9 +80,7 @@ impl MirInterpreter {
|
||||
let v = self.reg_load(*a0)?;
|
||||
fut.set_result(v.to_nyash_box());
|
||||
}
|
||||
if let Some(d) = dst {
|
||||
self.regs.insert(d, VMValue::Future(fut));
|
||||
}
|
||||
self.write_result(dst, VMValue::Future(fut));
|
||||
Ok(())
|
||||
}
|
||||
("env.future", "set") => {
|
||||
@ -96,9 +93,7 @@ impl MirInterpreter {
|
||||
return Err(VMError::TypeError("env.future.set expects Future".into()));
|
||||
}
|
||||
}
|
||||
if let Some(d) = dst {
|
||||
self.regs.insert(d, VMValue::Void);
|
||||
}
|
||||
self.write_void(dst);
|
||||
Ok(())
|
||||
}
|
||||
("env.future", "await") => {
|
||||
@ -107,9 +102,7 @@ impl MirInterpreter {
|
||||
match f {
|
||||
VMValue::Future(fut) => {
|
||||
let v = fut.get();
|
||||
if let Some(d) = dst {
|
||||
self.regs.insert(d, VMValue::from_nyash_box(v));
|
||||
}
|
||||
self.write_result(dst, VMValue::from_nyash_box(v));
|
||||
}
|
||||
_ => {
|
||||
return Err(VMError::TypeError("await expects Future".into()));
|
||||
@ -120,9 +113,7 @@ impl MirInterpreter {
|
||||
}
|
||||
("env.runtime", "checkpoint") => {
|
||||
crate::runtime::global_hooks::safepoint_and_poll();
|
||||
if let Some(d) = dst {
|
||||
self.regs.insert(d, VMValue::Void);
|
||||
}
|
||||
self.write_void(dst);
|
||||
Ok(())
|
||||
}
|
||||
("env.modules", "set") => {
|
||||
@ -131,9 +122,7 @@ impl MirInterpreter {
|
||||
let v = self.reg_load(args[1])?.to_nyash_box();
|
||||
crate::runtime::modules_registry::set(k, v);
|
||||
}
|
||||
if let Some(d) = dst {
|
||||
self.regs.insert(d, VMValue::Void);
|
||||
}
|
||||
self.write_void(dst);
|
||||
Ok(())
|
||||
}
|
||||
("env.modules", "get") => {
|
||||
@ -141,26 +130,24 @@ impl MirInterpreter {
|
||||
let k = self.reg_load(*a0)?.to_string();
|
||||
let vb = crate::runtime::modules_registry::get(&k)
|
||||
.unwrap_or_else(|| Box::new(crate::box_trait::VoidBox::new()));
|
||||
if let Some(d) = dst {
|
||||
self.regs.insert(d, VMValue::from_nyash_box(vb));
|
||||
}
|
||||
self.write_result(dst, VMValue::from_nyash_box(vb));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
("env", "get") => {
|
||||
// Delegate to provider
|
||||
let ret = self.extern_provider_dispatch("env.get", args).unwrap_or(Ok(VMValue::Void))?;
|
||||
if let Some(d) = dst { self.regs.insert(d, ret); }
|
||||
self.write_result(dst, ret);
|
||||
Ok(())
|
||||
}
|
||||
("env.mirbuilder", "emit") => {
|
||||
let ret = self.extern_provider_dispatch("env.mirbuilder.emit", args).unwrap_or(Ok(VMValue::Void))?;
|
||||
if let Some(d) = dst { self.regs.insert(d, ret); }
|
||||
self.write_result(dst, ret);
|
||||
Ok(())
|
||||
}
|
||||
("env.codegen", "emit_object") => {
|
||||
let ret = self.extern_provider_dispatch("env.codegen.emit_object", args).unwrap_or(Ok(VMValue::Void))?;
|
||||
if let Some(d) = dst { self.regs.insert(d, ret); }
|
||||
self.write_result(dst, ret);
|
||||
Ok(())
|
||||
}
|
||||
("env.codegen", "link_object") => {
|
||||
@ -197,13 +184,13 @@ impl MirInterpreter {
|
||||
let exe = exe_out.map(std::path::PathBuf::from).unwrap_or_else(|| std::env::temp_dir().join("hako_link_out.exe"));
|
||||
crate::host_providers::llvm_codegen::link_object_capi(&obj, &exe, extra.as_deref())
|
||||
.map_err(|e| VMError::InvalidInstruction(format!("env.codegen.link_object: {}", e)))?;
|
||||
if let Some(d) = dst { self.regs.insert(d, VMValue::String(exe.to_string_lossy().into_owned())); }
|
||||
self.write_result(dst, VMValue::String(exe.to_string_lossy().into_owned()));
|
||||
Ok(())
|
||||
}
|
||||
("hostbridge", "extern_invoke") => {
|
||||
if let Some(res) = self.extern_provider_dispatch("hostbridge.extern_invoke", args) {
|
||||
match res {
|
||||
Ok(v) => { if let Some(d) = dst { self.regs.insert(d, v); } }
|
||||
Ok(v) => { self.write_result(dst, v); }
|
||||
Err(e) => { return Err(e); }
|
||||
}
|
||||
return Ok(());
|
||||
|
||||
Reference in New Issue
Block a user