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:
nyash-codex
2025-11-06 22:59:47 +09:00
parent 8d179e9499
commit edf4513b5a
5 changed files with 44 additions and 87 deletions

View File

@ -25,7 +25,7 @@ pub(super) fn try_handle_array_box(
return Ok(true);
}
"push" => {
if args.len() != 1 { return Err(VMError::InvalidInstruction("push expects 1 arg".into())); }
this.validate_args_exact("push", args, 1)?;
let val = this.reg_load(args[0])?.to_nyash_box();
let _ = ab.push(val);
this.write_void(dst);
@ -43,14 +43,14 @@ pub(super) fn try_handle_array_box(
return Ok(true);
}
"get" => {
if args.len() != 1 { return Err(VMError::InvalidInstruction("get expects 1 arg".into())); }
this.validate_args_exact("get", args, 1)?;
let idx = this.reg_load(args[0])?.to_nyash_box();
let ret = ab.get(idx);
this.write_result(dst, VMValue::from_nyash_box(ret));
return Ok(true);
}
"set" => {
if args.len() != 2 { return Err(VMError::InvalidInstruction("set expects 2 args".into())); }
this.validate_args_exact("set", args, 2)?;
let idx = this.reg_load(args[0])?.to_nyash_box();
let val = this.reg_load(args[1])?.to_nyash_box();
let _ = ab.set(idx, val);