Files
hakorune/src/backend/mir_interpreter/handlers/mod.rs
nyash-codex 22b668927e refactor: complete error message unification (Phase 3.5)
Migrate remaining 45 error generation patterns to unified helpers:
- calls.rs: 13 sites → 0
- extern_provider.rs: 9 sites → 0
- externals.rs: 5 sites → 0
- boxes_plugin.rs: 5 sites → 0
- boxes.rs: 5 sites → 0
- boxes_string.rs: 4 sites → 0
- boxes_instance.rs: 2 sites → 0
- mod.rs + boxes_array.rs: 2 sites → 0

Error patterns now 100% unified:
- All 80 InvalidInstruction sites use helpers (100%)
- Consistent error formatting across entire codebase
- Single source of truth for error messages

Code reduction:
- Phase 3.5: 50-70 lines saved
- Cumulative (Phase 1+2+3+3.5): 200-267 lines removed (6-8% handlers)
- Total patterns unified: 192 (destination 60 + args 52 + errors 80)

Benefits:
- 100% error message consistency achieved
- Easy to modify error formats globally
- Foundation for i18n support ready
- Improved maintainability and testability

Test results: ✓ Build successful, Phase 21.0 smoke tests passing
Related: Phase 21.0 refactoring milestone complete
Risk: Low (error messages only, behavior preserved)
2025-11-06 23:34:46 +09:00

108 lines
3.5 KiB
Rust

use super::*;
// VM dispatch trace macro (used across handlers)
macro_rules! trace_dispatch {
($method:expr, $handler:expr) => {
if $method == "length"
&& std::env::var("NYASH_VM_TRACE").ok().as_deref() == Some("1")
{
eprintln!("[vm-trace] length dispatch handler={}", $handler);
}
};
}
mod arithmetic;
mod boxes;
mod boxes_array;
mod boxes_string;
mod boxes_map;
mod boxes_object_fields;
mod boxes_instance;
mod boxes_plugin;
mod boxes_void_guards;
mod call_resolution;
mod calls;
mod externals;
mod extern_provider;
mod memory;
mod misc;
impl MirInterpreter {
pub(super) fn execute_instruction(&mut self, inst: &MirInstruction) -> Result<(), VMError> {
match inst {
MirInstruction::Const { dst, value } => self.handle_const(*dst, value)?,
MirInstruction::NewBox {
dst,
box_type,
args,
} => self.handle_new_box(*dst, box_type, args)?,
MirInstruction::PluginInvoke {
dst,
box_val,
method,
args,
..
} => self.handle_plugin_invoke(*dst, *box_val, method, args)?,
MirInstruction::BoxCall {
dst,
box_val,
method,
args,
..
} => self.handle_box_call(*dst, *box_val, method, args)?,
MirInstruction::ExternCall {
dst,
iface_name,
method_name,
args,
..
} => self.handle_extern_call(*dst, iface_name, method_name, args)?,
MirInstruction::RefSet {
reference,
field,
value,
} => self.handle_ref_set(*reference, field, *value)?,
MirInstruction::RefGet {
dst,
reference,
field,
} => self.handle_ref_get(*dst, *reference, field)?,
MirInstruction::BinOp { dst, op, lhs, rhs } => {
self.handle_binop(*dst, *op, *lhs, *rhs)?
}
MirInstruction::UnaryOp { dst, op, operand } => {
self.handle_unary_op(*dst, *op, *operand)?
}
MirInstruction::Compare { dst, op, lhs, rhs } => {
self.handle_compare(*dst, *op, *lhs, *rhs)?
}
MirInstruction::Copy { dst, src } => self.handle_copy(*dst, *src)?,
MirInstruction::Load { dst, ptr } => self.handle_load(*dst, *ptr)?,
MirInstruction::Store { ptr, value } => self.handle_store(*ptr, *value)?,
MirInstruction::Call {
dst,
func,
callee,
args,
..
} => self.handle_call(*dst, *func, callee.as_ref(), args)?,
MirInstruction::Debug { message, value } => {
self.handle_debug(message, *value)?;
}
MirInstruction::Print { value, .. } => self.handle_print(*value)?,
MirInstruction::BarrierRead { .. }
| MirInstruction::BarrierWrite { .. }
| MirInstruction::Barrier { .. }
| MirInstruction::Safepoint
| MirInstruction::Nop => {}
other => {
return Err(self.err_invalid(format!(
"MIR interp: unimplemented instruction: {:?}",
other
)))
}
}
Ok(())
}
}