feat(phase-9.77): Implement BoxCall instructions and fix wasmtime version

Phase 9.77 WASM Emergency Recovery Progress:
-  Task 1.1: Implement BoxCall instruction for toString(), print(), equals(), clone(), log()
-  Task 1.2: Update wasmtime 18.0 → 35.0.0 and add runtime imports
- 🔄 Task 1.3: Working on UTF-8 encoding error fix

Changes:
- Add generate_box_call() method in codegen.rs with 5 helper methods
- Update wasmtime dependency to 35.0.0 for AOT compatibility
- Add BoxCall runtime imports (box_to_string, box_print, box_equals, box_clone)
- Implement wat_to_wasm() with UTF-8 validation and debug output
- Update CURRENT_TASK.md with Copilot handoff notes

Current issue: 'Generated WASM is not valid UTF-8' error source unknown
Next: Copilot to investigate error origin and complete Task 1.3

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Moe Charm
2025-08-16 21:21:51 +09:00
parent fa1a3ad644
commit cde961defc
9 changed files with 288 additions and 5 deletions

View File

@ -403,6 +403,11 @@ impl WasmCodegen {
Ok(instructions)
},
// Phase 9.77: BoxCall Implementation - Critical Box method calls
MirInstruction::BoxCall { dst, box_val, method, args, effects: _ } => {
self.generate_box_call(*dst, *box_val, method, args)
},
// Unsupported instructions
_ => Err(WasmError::UnsupportedInstruction(
format!("Instruction not yet supported: {:?}", instruction)
@ -570,6 +575,117 @@ impl WasmCodegen {
.copied()
.ok_or_else(|| WasmError::CodegenError(format!("Local variable not found for ValueId: {:?}", value_id)))
}
/// Phase 9.77: Generate BoxCall method invocation
/// Implements critical Box methods: toString, print, equals, clone
fn generate_box_call(&mut self, dst: Option<ValueId>, box_val: ValueId, method: &str, args: &[ValueId]) -> Result<Vec<String>, WasmError> {
match method {
"toString" => self.generate_to_string_call(dst, box_val),
"print" => self.generate_print_call(dst, box_val),
"equals" => self.generate_equals_call(dst, box_val, args),
"clone" => self.generate_clone_call(dst, box_val),
"log" => self.generate_log_call(dst, box_val, args),
_ => Err(WasmError::UnsupportedInstruction(
format!("Unsupported BoxCall method: {}", method)
))
}
}
/// Generate toString() method call - Box → String conversion
fn generate_to_string_call(&mut self, dst: Option<ValueId>, box_val: ValueId) -> Result<Vec<String>, WasmError> {
let Some(dst) = dst else {
return Err(WasmError::CodegenError("toString() requires destination".to_string()));
};
Ok(vec![
format!(";; toString() implementation for ValueId({})", box_val.as_u32()),
format!("local.get ${}", self.get_local_index(box_val)?),
"call $box_to_string".to_string(),
format!("local.set ${}", self.get_local_index(dst)?),
])
}
/// Generate print() method call - Basic output
fn generate_print_call(&mut self, dst: Option<ValueId>, box_val: ValueId) -> Result<Vec<String>, WasmError> {
let mut instructions = vec![
format!(";; print() implementation for ValueId({})", box_val.as_u32()),
format!("local.get ${}", self.get_local_index(box_val)?),
"call $box_print".to_string(),
];
// Store void result if destination is provided
if let Some(dst) = dst {
instructions.extend(vec![
"i32.const 0".to_string(), // Void result
format!("local.set ${}", self.get_local_index(dst)?),
]);
}
Ok(instructions)
}
/// Generate equals() method call - Box comparison
fn generate_equals_call(&mut self, dst: Option<ValueId>, box_val: ValueId, args: &[ValueId]) -> Result<Vec<String>, WasmError> {
let Some(dst) = dst else {
return Err(WasmError::CodegenError("equals() requires destination".to_string()));
};
if args.len() != 1 {
return Err(WasmError::CodegenError(
format!("equals() expects 1 argument, got {}", args.len())
));
}
Ok(vec![
format!(";; equals() implementation for ValueId({}) == ValueId({})", box_val.as_u32(), args[0].as_u32()),
format!("local.get ${}", self.get_local_index(box_val)?),
format!("local.get ${}", self.get_local_index(args[0])?),
"call $box_equals".to_string(),
format!("local.set ${}", self.get_local_index(dst)?),
])
}
/// Generate clone() method call - Box duplication
fn generate_clone_call(&mut self, dst: Option<ValueId>, box_val: ValueId) -> Result<Vec<String>, WasmError> {
let Some(dst) = dst else {
return Err(WasmError::CodegenError("clone() requires destination".to_string()));
};
Ok(vec![
format!(";; clone() implementation for ValueId({})", box_val.as_u32()),
format!("local.get ${}", self.get_local_index(box_val)?),
"call $box_clone".to_string(),
format!("local.set ${}", self.get_local_index(dst)?),
])
}
/// Generate log() method call - Console logging (ConsoleBox.log)
fn generate_log_call(&mut self, dst: Option<ValueId>, box_val: ValueId, args: &[ValueId]) -> Result<Vec<String>, WasmError> {
let mut instructions = vec![
format!(";; log() implementation for ValueId({})", box_val.as_u32()),
];
// Load box_val (ConsoleBox instance)
instructions.push(format!("local.get ${}", self.get_local_index(box_val)?));
// Load all arguments
for arg in args {
instructions.push(format!("local.get ${}", self.get_local_index(*arg)?));
}
// Call console log function
instructions.push("call $console_log".to_string());
// Store void result if destination is provided
if let Some(dst) = dst {
instructions.extend(vec![
"i32.const 0".to_string(), // Void result
format!("local.set ${}", self.get_local_index(dst)?),
]);
}
Ok(instructions)
}
}
#[cfg(test)]