2025-09-25 01:09:48 +09:00
|
|
|
use super::*;
|
|
|
|
|
use crate::box_trait::NyashBox;
|
|
|
|
|
|
|
|
|
|
impl MirInterpreter {
|
|
|
|
|
pub(super) fn handle_new_box(
|
|
|
|
|
&mut self,
|
|
|
|
|
dst: ValueId,
|
|
|
|
|
box_type: &str,
|
|
|
|
|
args: &[ValueId],
|
|
|
|
|
) -> Result<(), VMError> {
|
2025-09-26 00:27:02 +09:00
|
|
|
// Provider Lock guard (受け口・既定は挙動不変)
|
|
|
|
|
if let Err(e) = crate::runtime::provider_lock::guard_before_new_box(box_type) {
|
|
|
|
|
return Err(VMError::InvalidInstruction(e));
|
|
|
|
|
}
|
2025-09-25 01:09:48 +09:00
|
|
|
let mut converted: Vec<Box<dyn NyashBox>> = Vec::with_capacity(args.len());
|
|
|
|
|
for vid in args {
|
|
|
|
|
converted.push(self.reg_load(*vid)?.to_nyash_box());
|
|
|
|
|
}
|
|
|
|
|
let reg = crate::runtime::unified_registry::get_global_unified_registry();
|
|
|
|
|
let created = reg
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.create_box(box_type, &converted)
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
VMError::InvalidInstruction(format!("NewBox {} failed: {}", box_type, e))
|
|
|
|
|
})?;
|
2025-09-26 14:34:42 +09:00
|
|
|
// Store created instance first so 'me' can be passed to birth
|
|
|
|
|
let created_vm = VMValue::from_nyash_box(created);
|
|
|
|
|
self.regs.insert(dst, created_vm.clone());
|
|
|
|
|
|
|
|
|
|
// Note: birth の自動呼び出しは削除。
|
|
|
|
|
// 正しい設計は Builder が NewBox 後に明示的に birth 呼び出しを生成すること。
|
2025-09-25 01:09:48 +09:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn handle_plugin_invoke(
|
|
|
|
|
&mut self,
|
|
|
|
|
dst: Option<ValueId>,
|
|
|
|
|
box_val: ValueId,
|
|
|
|
|
method: &str,
|
|
|
|
|
args: &[ValueId],
|
|
|
|
|
) -> Result<(), VMError> {
|
|
|
|
|
let recv = self.reg_load(box_val)?;
|
|
|
|
|
let recv_box: Box<dyn NyashBox> = match recv.clone() {
|
|
|
|
|
VMValue::BoxRef(b) => b.share_box(),
|
|
|
|
|
other => other.to_nyash_box(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if let Some(p) = recv_box
|
|
|
|
|
.as_any()
|
|
|
|
|
.downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>()
|
|
|
|
|
{
|
|
|
|
|
let host = crate::runtime::plugin_loader_unified::get_global_plugin_host();
|
|
|
|
|
let host = host.read().unwrap();
|
|
|
|
|
let mut argv: Vec<Box<dyn NyashBox>> = Vec::with_capacity(args.len());
|
|
|
|
|
for a in args {
|
|
|
|
|
argv.push(self.reg_load(*a)?.to_nyash_box());
|
|
|
|
|
}
|
|
|
|
|
match host.invoke_instance_method(&p.box_type, method, p.inner.instance_id, &argv) {
|
|
|
|
|
Ok(Some(ret)) => {
|
|
|
|
|
if let Some(d) = dst {
|
|
|
|
|
self.regs.insert(d, VMValue::from_nyash_box(ret));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(None) => {
|
|
|
|
|
if let Some(d) = dst {
|
|
|
|
|
self.regs.insert(d, VMValue::Void);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
return Err(VMError::InvalidInstruction(format!(
|
|
|
|
|
"PluginInvoke {}.{} failed: {:?}",
|
|
|
|
|
p.box_type, method, e
|
|
|
|
|
)))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
} else if method == "toString" {
|
|
|
|
|
if let Some(d) = dst {
|
|
|
|
|
self.regs
|
|
|
|
|
.insert(d, VMValue::String(recv_box.to_string_box().value));
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
} else {
|
|
|
|
|
Err(VMError::InvalidInstruction(format!(
|
|
|
|
|
"PluginInvoke unsupported on {} for method {}",
|
|
|
|
|
recv_box.type_name(),
|
|
|
|
|
method
|
|
|
|
|
)))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn handle_box_call(
|
|
|
|
|
&mut self,
|
|
|
|
|
dst: Option<ValueId>,
|
|
|
|
|
box_val: ValueId,
|
|
|
|
|
method: &str,
|
|
|
|
|
args: &[ValueId],
|
|
|
|
|
) -> Result<(), VMError> {
|
2025-09-26 14:34:42 +09:00
|
|
|
// Debug: trace length dispatch receiver type before any handler resolution
|
|
|
|
|
if method == "length" && std::env::var("NYASH_VM_TRACE").ok().as_deref() == Some("1") {
|
|
|
|
|
let recv = self.reg_load(box_val).unwrap_or(VMValue::Void);
|
|
|
|
|
let type_name = match recv.clone() {
|
|
|
|
|
VMValue::BoxRef(b) => b.type_name().to_string(),
|
|
|
|
|
VMValue::Integer(_) => "Integer".to_string(),
|
|
|
|
|
VMValue::Float(_) => "Float".to_string(),
|
|
|
|
|
VMValue::Bool(_) => "Bool".to_string(),
|
|
|
|
|
VMValue::String(_) => "String".to_string(),
|
|
|
|
|
VMValue::Void => "Void".to_string(),
|
|
|
|
|
VMValue::Future(_) => "Future".to_string(),
|
|
|
|
|
};
|
|
|
|
|
eprintln!("[vm-trace] length dispatch recv_type={}", type_name);
|
|
|
|
|
}
|
2025-09-26 03:30:59 +09:00
|
|
|
// Graceful void guard for common short-circuit patterns in user code
|
|
|
|
|
// e.g., `A or not last.is_eof()` should not crash when last is absent.
|
|
|
|
|
match self.reg_load(box_val)? {
|
|
|
|
|
VMValue::Void => {
|
|
|
|
|
match method {
|
|
|
|
|
"is_eof" => { if let Some(d) = dst { self.regs.insert(d, VMValue::Bool(false)); } return Ok(()); }
|
|
|
|
|
"length" => { if let Some(d) = dst { self.regs.insert(d, VMValue::Integer(0)); } return Ok(()); }
|
|
|
|
|
"substring" => { if let Some(d) = dst { self.regs.insert(d, VMValue::String(String::new())); } return Ok(()); }
|
|
|
|
|
"push" => { if let Some(d) = dst { self.regs.insert(d, VMValue::Void); } return Ok(()); }
|
|
|
|
|
"get_position" => { if let Some(d) = dst { self.regs.insert(d, VMValue::Integer(0)); } return Ok(()); }
|
|
|
|
|
"get_line" => { if let Some(d) = dst { self.regs.insert(d, VMValue::Integer(1)); } return Ok(()); }
|
|
|
|
|
"get_column" => { if let Some(d) = dst { self.regs.insert(d, VMValue::Integer(1)); } return Ok(()); }
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
VMValue::BoxRef(ref b) => {
|
|
|
|
|
if b.as_any().downcast_ref::<crate::box_trait::VoidBox>().is_some() {
|
|
|
|
|
match method {
|
|
|
|
|
"is_eof" => { if let Some(d) = dst { self.regs.insert(d, VMValue::Bool(false)); } return Ok(()); }
|
|
|
|
|
"length" => { if let Some(d) = dst { self.regs.insert(d, VMValue::Integer(0)); } return Ok(()); }
|
|
|
|
|
"substring" => { if let Some(d) = dst { self.regs.insert(d, VMValue::String(String::new())); } return Ok(()); }
|
|
|
|
|
"push" => { if let Some(d) = dst { self.regs.insert(d, VMValue::Void); } return Ok(()); }
|
|
|
|
|
"get_position" => { if let Some(d) = dst { self.regs.insert(d, VMValue::Integer(0)); } return Ok(()); }
|
|
|
|
|
"get_line" => { if let Some(d) = dst { self.regs.insert(d, VMValue::Integer(1)); } return Ok(()); }
|
|
|
|
|
"get_column" => { if let Some(d) = dst { self.regs.insert(d, VMValue::Integer(1)); } return Ok(()); }
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
2025-09-25 01:09:48 +09:00
|
|
|
if self.try_handle_object_fields(dst, box_val, method, args)? {
|
2025-09-26 14:34:42 +09:00
|
|
|
if method == "length" && std::env::var("NYASH_VM_TRACE").ok().as_deref() == Some("1") {
|
|
|
|
|
eprintln!("[vm-trace] length dispatch handler=object_fields");
|
|
|
|
|
}
|
2025-09-25 01:09:48 +09:00
|
|
|
return Ok(());
|
|
|
|
|
}
|
2025-09-26 03:30:59 +09:00
|
|
|
if self.try_handle_instance_box(dst, box_val, method, args)? {
|
2025-09-26 14:34:42 +09:00
|
|
|
if method == "length" && std::env::var("NYASH_VM_TRACE").ok().as_deref() == Some("1") {
|
|
|
|
|
eprintln!("[vm-trace] length dispatch handler=instance_box");
|
|
|
|
|
}
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
if super::boxes_string::try_handle_string_box(self, dst, box_val, method, args)? {
|
|
|
|
|
if method == "length" && std::env::var("NYASH_VM_TRACE").ok().as_deref() == Some("1") {
|
|
|
|
|
eprintln!("[vm-trace] length dispatch handler=string_box");
|
|
|
|
|
}
|
2025-09-26 03:30:59 +09:00
|
|
|
return Ok(());
|
|
|
|
|
}
|
2025-09-26 14:34:42 +09:00
|
|
|
if super::boxes_array::try_handle_array_box(self, dst, box_val, method, args)? {
|
|
|
|
|
if method == "length" && std::env::var("NYASH_VM_TRACE").ok().as_deref() == Some("1") {
|
|
|
|
|
eprintln!("[vm-trace] length dispatch handler=array_box");
|
|
|
|
|
}
|
2025-09-25 01:09:48 +09:00
|
|
|
return Ok(());
|
|
|
|
|
}
|
2025-09-26 14:34:42 +09:00
|
|
|
if super::boxes_map::try_handle_map_box(self, dst, box_val, method, args)? {
|
|
|
|
|
if method == "length" && std::env::var("NYASH_VM_TRACE").ok().as_deref() == Some("1") {
|
|
|
|
|
eprintln!("[vm-trace] length dispatch handler=map_box");
|
|
|
|
|
}
|
2025-09-26 03:30:59 +09:00
|
|
|
return Ok(());
|
|
|
|
|
}
|
2025-09-26 14:34:42 +09:00
|
|
|
// Narrow safety valve: if 'length' wasn't handled by any box-specific path,
|
|
|
|
|
// treat it as 0 (avoids Lt on Void in common loops). This is a dev-time
|
|
|
|
|
// robustness measure; precise behavior should be provided by concrete boxes.
|
|
|
|
|
if method == "length" {
|
|
|
|
|
if std::env::var("NYASH_VM_TRACE").ok().as_deref() == Some("1") {
|
|
|
|
|
eprintln!("[vm-trace] length dispatch handler=fallback(length=0)");
|
|
|
|
|
}
|
|
|
|
|
if let Some(d) = dst { self.regs.insert(d, VMValue::Integer(0)); }
|
2025-09-26 03:30:59 +09:00
|
|
|
return Ok(());
|
|
|
|
|
}
|
2025-09-26 14:34:42 +09:00
|
|
|
// Fallback: unique-tail dynamic resolution for user-defined methods
|
|
|
|
|
if let Some(func) = {
|
|
|
|
|
let tail = format!(".{}{}", method, format!("/{}", args.len()));
|
|
|
|
|
let mut cands: Vec<String> = self
|
|
|
|
|
.functions
|
|
|
|
|
.keys()
|
|
|
|
|
.filter(|k| k.ends_with(&tail))
|
|
|
|
|
.cloned()
|
|
|
|
|
.collect();
|
|
|
|
|
if cands.len() == 1 {
|
|
|
|
|
self.functions.get(&cands[0]).cloned()
|
|
|
|
|
} else { None }
|
|
|
|
|
} {
|
|
|
|
|
// Build argv: pass receiver as first arg (me)
|
|
|
|
|
let recv_vm = self.reg_load(box_val)?;
|
|
|
|
|
let mut argv: Vec<VMValue> = Vec::with_capacity(1 + args.len());
|
|
|
|
|
argv.push(recv_vm);
|
|
|
|
|
for a in args { argv.push(self.reg_load(*a)?); }
|
|
|
|
|
let ret = self.exec_function_inner(&func, Some(&argv))?;
|
|
|
|
|
if let Some(d) = dst { self.regs.insert(d, ret); }
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-25 01:09:48 +09:00
|
|
|
self.invoke_plugin_box(dst, box_val, method, args)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn try_handle_object_fields(
|
|
|
|
|
&mut self,
|
|
|
|
|
dst: Option<ValueId>,
|
|
|
|
|
box_val: ValueId,
|
|
|
|
|
method: &str,
|
|
|
|
|
args: &[ValueId],
|
|
|
|
|
) -> Result<bool, VMError> {
|
2025-09-26 14:34:42 +09:00
|
|
|
// Local helpers to bridge NyashValue <-> VMValue for InstanceBox fields
|
|
|
|
|
fn vm_to_nv(v: &VMValue) -> crate::value::NyashValue {
|
|
|
|
|
use crate::value::NyashValue as NV;
|
|
|
|
|
use super::VMValue as VV;
|
|
|
|
|
match v {
|
|
|
|
|
VV::Integer(i) => NV::Integer(*i),
|
|
|
|
|
VV::Float(f) => NV::Float(*f),
|
|
|
|
|
VV::Bool(b) => NV::Bool(*b),
|
|
|
|
|
VV::String(s) => NV::String(s.clone()),
|
|
|
|
|
VV::Void => NV::Void,
|
|
|
|
|
VV::Future(_) => NV::Void, // not expected in fields
|
|
|
|
|
VV::BoxRef(_) => NV::Void, // store minimal; complex object fields are not required here
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fn nv_to_vm(v: &crate::value::NyashValue) -> VMValue {
|
|
|
|
|
use crate::value::NyashValue as NV;
|
|
|
|
|
use super::VMValue as VV;
|
|
|
|
|
match v {
|
|
|
|
|
NV::Integer(i) => VV::Integer(*i),
|
|
|
|
|
NV::Float(f) => VV::Float(*f),
|
|
|
|
|
NV::Bool(b) => VV::Bool(*b),
|
|
|
|
|
NV::String(s) => VV::String(s.clone()),
|
|
|
|
|
NV::Null | NV::Void => VV::Void,
|
|
|
|
|
NV::Array(_) | NV::Map(_) | NV::Box(_) | NV::WeakBox(_) => VV::Void,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-25 01:09:48 +09:00
|
|
|
match method {
|
|
|
|
|
"getField" => {
|
|
|
|
|
if args.len() != 1 {
|
|
|
|
|
return Err(VMError::InvalidInstruction("getField expects 1 arg".into()));
|
|
|
|
|
}
|
2025-09-26 14:34:42 +09:00
|
|
|
if std::env::var("NYASH_VM_TRACE").ok().as_deref() == Some("1") {
|
|
|
|
|
let rk = match self.reg_load(box_val) {
|
|
|
|
|
Ok(VMValue::BoxRef(ref b)) => format!("BoxRef({})", b.type_name()),
|
|
|
|
|
Ok(VMValue::Integer(_)) => "Integer".to_string(),
|
|
|
|
|
Ok(VMValue::Float(_)) => "Float".to_string(),
|
|
|
|
|
Ok(VMValue::Bool(_)) => "Bool".to_string(),
|
|
|
|
|
Ok(VMValue::String(_)) => "String".to_string(),
|
|
|
|
|
Ok(VMValue::Void) => "Void".to_string(),
|
|
|
|
|
Ok(VMValue::Future(_)) => "Future".to_string(),
|
|
|
|
|
Err(_) => "<err>".to_string(),
|
|
|
|
|
};
|
|
|
|
|
eprintln!("[vm-trace] getField recv_kind={}", rk);
|
|
|
|
|
}
|
2025-09-25 01:09:48 +09:00
|
|
|
let fname = match self.reg_load(args[0])? {
|
|
|
|
|
VMValue::String(s) => s,
|
|
|
|
|
v => v.to_string(),
|
|
|
|
|
};
|
2025-09-26 14:34:42 +09:00
|
|
|
// Prefer InstanceBox internal storage (structural correctness)
|
|
|
|
|
if let VMValue::BoxRef(bref) = self.reg_load(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);
|
|
|
|
|
}
|
|
|
|
|
// Special-case bridge: JsonParser.length -> tokens.length()
|
|
|
|
|
if inst.class_name == "JsonParser" && fname == "length" {
|
|
|
|
|
if let Some(tokens_shared) = inst.get_field("tokens") {
|
|
|
|
|
let tokens_box: Box<dyn crate::box_trait::NyashBox> = tokens_shared.share_box();
|
|
|
|
|
if let Some(arr) = tokens_box.as_any().downcast_ref::<crate::boxes::array::ArrayBox>() {
|
|
|
|
|
let len_box = arr.length();
|
|
|
|
|
if let Some(d) = dst { self.regs.insert(d, VMValue::from_nyash_box(len_box)); }
|
|
|
|
|
return Ok(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// First: prefer fields_ng (NyashValue) when present
|
|
|
|
|
if let Some(nv) = inst.get_field_ng(&fname) {
|
|
|
|
|
// Treat complex Box-like values as "missing" for internal storage so that
|
|
|
|
|
// legacy obj_fields (which stores BoxRef) is used instead.
|
|
|
|
|
// This avoids NV::Box/Array/Map being converted to Void by nv_to_vm.
|
|
|
|
|
let is_missing = matches!(
|
|
|
|
|
nv,
|
|
|
|
|
crate::value::NyashValue::Null
|
|
|
|
|
| crate::value::NyashValue::Void
|
|
|
|
|
| crate::value::NyashValue::Array(_)
|
|
|
|
|
| crate::value::NyashValue::Map(_)
|
|
|
|
|
| crate::value::NyashValue::Box(_)
|
|
|
|
|
| crate::value::NyashValue::WeakBox(_)
|
|
|
|
|
);
|
|
|
|
|
if !is_missing {
|
|
|
|
|
if std::env::var("NYASH_VM_TRACE").ok().as_deref() == Some("1") {
|
|
|
|
|
eprintln!("[vm-trace] getField internal {}.{} -> {:?}", inst.class_name, fname, nv);
|
|
|
|
|
}
|
|
|
|
|
if let Some(d) = dst {
|
|
|
|
|
// Special-case: NV::Box should surface as VMValue::BoxRef
|
|
|
|
|
if let crate::value::NyashValue::Box(arc_m) = nv {
|
|
|
|
|
if let Ok(guard) = arc_m.lock() {
|
|
|
|
|
let cloned: Box<dyn crate::box_trait::NyashBox> = guard.clone_box();
|
|
|
|
|
let arc: std::sync::Arc<dyn crate::box_trait::NyashBox> = std::sync::Arc::from(cloned);
|
|
|
|
|
self.regs.insert(d, VMValue::BoxRef(arc));
|
|
|
|
|
} else {
|
|
|
|
|
self.regs.insert(d, VMValue::Void);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
self.regs.insert(d, nv_to_vm(&nv));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Ok(true);
|
|
|
|
|
} else {
|
|
|
|
|
// Provide pragmatic defaults for JsonScanner numeric fields
|
|
|
|
|
if inst.class_name == "JsonScanner" {
|
|
|
|
|
let def = match fname.as_str() {
|
|
|
|
|
"position" | "length" => Some(VMValue::Integer(0)),
|
|
|
|
|
"line" | "column" => Some(VMValue::Integer(1)),
|
|
|
|
|
"text" => Some(VMValue::String(String::new())),
|
|
|
|
|
_ => None,
|
|
|
|
|
};
|
|
|
|
|
if let Some(v) = def {
|
|
|
|
|
if std::env::var("NYASH_VM_TRACE").ok().as_deref() == Some("1") {
|
|
|
|
|
eprintln!("[vm-trace] getField default JsonScanner.{} -> {:?}", fname, v);
|
|
|
|
|
}
|
|
|
|
|
if let Some(d) = dst { self.regs.insert(d, v); }
|
|
|
|
|
return Ok(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// fields_ng missing entirely → try JsonScanner defaults next, otherwise fallback to legacy/opfields
|
|
|
|
|
if inst.class_name == "JsonScanner" {
|
|
|
|
|
let def = match fname.as_str() {
|
|
|
|
|
"position" | "length" => Some(VMValue::Integer(0)),
|
|
|
|
|
"line" | "column" => Some(VMValue::Integer(1)),
|
|
|
|
|
"text" => Some(VMValue::String(String::new())),
|
|
|
|
|
_ => None,
|
|
|
|
|
};
|
|
|
|
|
if let Some(v) = def {
|
|
|
|
|
if std::env::var("NYASH_VM_TRACE").ok().as_deref() == Some("1") {
|
|
|
|
|
eprintln!("[vm-trace] getField default(JsonScanner missing) {} -> {:?}", fname, v);
|
|
|
|
|
}
|
|
|
|
|
if let Some(d) = dst { self.regs.insert(d, v); }
|
|
|
|
|
return Ok(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Finally: legacy fields (SharedNyashBox) for complex values
|
|
|
|
|
if let Some(shared) = inst.get_field(&fname) {
|
|
|
|
|
if let Some(d) = dst { self.regs.insert(d, VMValue::BoxRef(shared)); }
|
|
|
|
|
return Ok(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let key = self.object_key_for(box_val);
|
|
|
|
|
let mut v = self
|
2025-09-25 01:09:48 +09:00
|
|
|
.obj_fields
|
2025-09-26 14:34:42 +09:00
|
|
|
.get(&key)
|
2025-09-25 01:09:48 +09:00
|
|
|
.and_then(|m| m.get(&fname))
|
|
|
|
|
.cloned()
|
|
|
|
|
.unwrap_or(VMValue::Void);
|
2025-09-26 14:34:42 +09:00
|
|
|
// Final safety: for JsonScanner legacy path, coerce missing numeric fields
|
|
|
|
|
if let VMValue::Void = v {
|
|
|
|
|
if let Ok(VMValue::BoxRef(bref2)) = self.reg_load(box_val) {
|
|
|
|
|
if let Some(inst2) = bref2.as_any().downcast_ref::<crate::instance_v2::InstanceBox>() {
|
|
|
|
|
if inst2.class_name == "JsonScanner" {
|
|
|
|
|
if matches!(fname.as_str(), "position" | "length") {
|
|
|
|
|
v = if fname == "position" { VMValue::Integer(0) } else { VMValue::Integer(0) };
|
|
|
|
|
} else if matches!(fname.as_str(), "line" | "column") {
|
|
|
|
|
v = VMValue::Integer(1);
|
|
|
|
|
} else if fname == "text" {
|
|
|
|
|
v = VMValue::String(String::new());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if std::env::var("NYASH_VM_TRACE").ok().as_deref() == Some("1") {
|
|
|
|
|
if let VMValue::BoxRef(b) = &v {
|
|
|
|
|
eprintln!("[vm-trace] getField legacy {} -> BoxRef({})", fname, b.type_name());
|
|
|
|
|
} else {
|
|
|
|
|
eprintln!("[vm-trace] getField legacy {} -> {:?}", fname, v);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-25 01:09:48 +09:00
|
|
|
if let Some(d) = dst {
|
|
|
|
|
self.regs.insert(d, v);
|
|
|
|
|
}
|
|
|
|
|
Ok(true)
|
|
|
|
|
}
|
|
|
|
|
"setField" => {
|
|
|
|
|
if args.len() != 2 {
|
|
|
|
|
return Err(VMError::InvalidInstruction(
|
|
|
|
|
"setField expects 2 args".into(),
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
let fname = match self.reg_load(args[0])? {
|
|
|
|
|
VMValue::String(s) => s,
|
|
|
|
|
v => v.to_string(),
|
|
|
|
|
};
|
|
|
|
|
let valv = self.reg_load(args[1])?;
|
2025-09-26 14:34:42 +09:00
|
|
|
// Prefer InstanceBox internal storage
|
|
|
|
|
if let VMValue::BoxRef(bref) = self.reg_load(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) {
|
|
|
|
|
let _ = inst.set_field_ng(fname.clone(), vm_to_nv(&valv));
|
|
|
|
|
return Ok(true);
|
|
|
|
|
}
|
|
|
|
|
// BoxRef のうち、Integer/Float/Bool/String はプリミティブに剥がして内部保存
|
|
|
|
|
if let VMValue::BoxRef(bx) = &valv {
|
|
|
|
|
if let Some(ib) = bx.as_any().downcast_ref::<crate::box_trait::IntegerBox>() {
|
|
|
|
|
let _ = inst.set_field_ng(fname.clone(), crate::value::NyashValue::Integer(ib.value));
|
|
|
|
|
return Ok(true);
|
|
|
|
|
}
|
|
|
|
|
if let Some(fb) = bx.as_any().downcast_ref::<crate::boxes::FloatBox>() {
|
|
|
|
|
let _ = inst.set_field_ng(fname.clone(), crate::value::NyashValue::Float(fb.value));
|
|
|
|
|
return Ok(true);
|
|
|
|
|
}
|
|
|
|
|
if let Some(bb) = bx.as_any().downcast_ref::<crate::box_trait::BoolBox>() {
|
|
|
|
|
let _ = inst.set_field_ng(fname.clone(), crate::value::NyashValue::Bool(bb.value));
|
|
|
|
|
return Ok(true);
|
|
|
|
|
}
|
|
|
|
|
if let Some(sb) = bx.as_any().downcast_ref::<crate::box_trait::StringBox>() {
|
|
|
|
|
let _ = inst.set_field_ng(fname.clone(), crate::value::NyashValue::String(sb.value.clone()));
|
|
|
|
|
return Ok(true);
|
|
|
|
|
}
|
|
|
|
|
// For complex Box values (InstanceBox/MapBox/ArrayBox...), store into
|
|
|
|
|
// legacy fields to preserve identity across clones/gets.
|
|
|
|
|
let _ = inst.set_field(fname.as_str(), std::sync::Arc::clone(bx));
|
|
|
|
|
return Ok(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let key = self.object_key_for(box_val);
|
2025-09-25 01:09:48 +09:00
|
|
|
self.obj_fields
|
2025-09-26 14:34:42 +09:00
|
|
|
.entry(key)
|
2025-09-25 01:09:48 +09:00
|
|
|
.or_default()
|
|
|
|
|
.insert(fname, valv);
|
|
|
|
|
Ok(true)
|
|
|
|
|
}
|
|
|
|
|
_ => Ok(false),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-26 14:34:42 +09:00
|
|
|
// moved: try_handle_map_box → handlers/boxes_map.rs
|
2025-09-26 03:30:59 +09:00
|
|
|
fn try_handle_map_box(
|
|
|
|
|
&mut self,
|
|
|
|
|
dst: Option<ValueId>,
|
|
|
|
|
box_val: ValueId,
|
|
|
|
|
method: &str,
|
|
|
|
|
args: &[ValueId],
|
|
|
|
|
) -> Result<bool, VMError> {
|
2025-09-26 14:34:42 +09:00
|
|
|
super::boxes_map::try_handle_map_box(self, dst, box_val, method, args)
|
2025-09-26 03:30:59 +09:00
|
|
|
}
|
|
|
|
|
|
2025-09-26 14:34:42 +09:00
|
|
|
// moved: try_handle_string_box → handlers/boxes_string.rs
|
2025-09-25 01:09:48 +09:00
|
|
|
fn try_handle_string_box(
|
|
|
|
|
&mut self,
|
|
|
|
|
dst: Option<ValueId>,
|
|
|
|
|
box_val: ValueId,
|
|
|
|
|
method: &str,
|
|
|
|
|
args: &[ValueId],
|
|
|
|
|
) -> Result<bool, VMError> {
|
2025-09-26 14:34:42 +09:00
|
|
|
super::boxes_string::try_handle_string_box(self, dst, box_val, method, args)
|
2025-09-25 01:09:48 +09:00
|
|
|
}
|
|
|
|
|
|
2025-09-26 03:30:59 +09:00
|
|
|
fn try_handle_instance_box(
|
|
|
|
|
&mut self,
|
|
|
|
|
dst: Option<ValueId>,
|
|
|
|
|
box_val: ValueId,
|
|
|
|
|
method: &str,
|
|
|
|
|
args: &[ValueId],
|
|
|
|
|
) -> Result<bool, VMError> {
|
|
|
|
|
let recv_vm = self.reg_load(box_val)?;
|
|
|
|
|
let recv_box_any: Box<dyn NyashBox> = match recv_vm.clone() {
|
|
|
|
|
VMValue::BoxRef(b) => b.share_box(),
|
|
|
|
|
other => other.to_nyash_box(),
|
|
|
|
|
};
|
2025-09-26 14:34:42 +09:00
|
|
|
if std::env::var("NYASH_VM_TRACE").ok().as_deref() == Some("1") && method == "toString" {
|
|
|
|
|
eprintln!("[vm-trace] instance-check recv_box_any.type={} args_len={}", recv_box_any.type_name(), args.len());
|
|
|
|
|
}
|
2025-09-26 03:30:59 +09:00
|
|
|
if let Some(inst) = recv_box_any.as_any().downcast_ref::<crate::instance_v2::InstanceBox>() {
|
2025-09-26 14:34:42 +09:00
|
|
|
// Development guard: ensure JsonScanner core fields have sensible defaults
|
|
|
|
|
if inst.class_name == "JsonScanner" {
|
|
|
|
|
// populate missing fields to avoid Void in comparisons inside is_eof/advance
|
|
|
|
|
if inst.get_field_ng("position").is_none() {
|
|
|
|
|
let _ = inst.set_field_ng("position".to_string(), crate::value::NyashValue::Integer(0));
|
|
|
|
|
}
|
|
|
|
|
if inst.get_field_ng("length").is_none() {
|
|
|
|
|
let _ = inst.set_field_ng("length".to_string(), crate::value::NyashValue::Integer(0));
|
|
|
|
|
}
|
|
|
|
|
if inst.get_field_ng("line").is_none() {
|
|
|
|
|
let _ = inst.set_field_ng("line".to_string(), crate::value::NyashValue::Integer(1));
|
|
|
|
|
}
|
|
|
|
|
if inst.get_field_ng("column").is_none() {
|
|
|
|
|
let _ = inst.set_field_ng("column".to_string(), crate::value::NyashValue::Integer(1));
|
|
|
|
|
}
|
|
|
|
|
if inst.get_field_ng("text").is_none() {
|
|
|
|
|
let _ = inst.set_field_ng("text".to_string(), crate::value::NyashValue::String(String::new()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// JsonNodeInstance narrow bridges removed: rely on builder rewrite and instance dispatch
|
|
|
|
|
// birth on user-defined InstanceBox: treat as no-op constructor init
|
|
|
|
|
if method == "birth" {
|
|
|
|
|
if let Some(d) = dst { self.regs.insert(d, VMValue::Void); }
|
|
|
|
|
return Ok(true);
|
|
|
|
|
}
|
|
|
|
|
if std::env::var("NYASH_VM_TRACE").ok().as_deref() == Some("1") && method == "toString" {
|
|
|
|
|
eprintln!(
|
|
|
|
|
"[vm-trace] instance-check downcast=ok class={} stringify_present={{class:{}, alt:{}}}",
|
|
|
|
|
inst.class_name,
|
|
|
|
|
self.functions.contains_key(&format!("{}.stringify/0", inst.class_name)),
|
|
|
|
|
self.functions.contains_key(&format!("{}Instance.stringify/0", inst.class_name))
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-09-26 03:30:59 +09:00
|
|
|
// Resolve lowered method function: "Class.method/arity"
|
2025-09-26 14:34:42 +09:00
|
|
|
let primary = format!("{}.{}{}", inst.class_name, method, format!("/{}", args.len()));
|
|
|
|
|
// Alternate naming: "ClassInstance.method/arity"
|
|
|
|
|
let alt = format!("{}Instance.{}{}", inst.class_name, method, format!("/{}", args.len()));
|
|
|
|
|
// Static method variant that takes 'me' explicitly as first arg: "Class.method/(arity+1)"
|
|
|
|
|
let static_variant = format!("{}.{}{}", inst.class_name, method, format!("/{}", args.len() + 1));
|
|
|
|
|
// Special-case: toString() → stringify/0 if present
|
|
|
|
|
// Prefer base class (strip trailing "Instance") stringify when available.
|
|
|
|
|
let (stringify_base, stringify_inst) = if method == "toString" && args.is_empty() {
|
|
|
|
|
let base = inst
|
|
|
|
|
.class_name
|
|
|
|
|
.strip_suffix("Instance")
|
|
|
|
|
.map(|s| s.to_string());
|
|
|
|
|
let base_name = base.unwrap_or_else(|| inst.class_name.clone());
|
|
|
|
|
(
|
|
|
|
|
Some(format!("{}.stringify/0", base_name)),
|
|
|
|
|
Some(format!("{}.stringify/0", inst.class_name)),
|
|
|
|
|
)
|
|
|
|
|
} else { (None, None) };
|
|
|
|
|
|
|
|
|
|
if std::env::var("NYASH_VM_TRACE").ok().as_deref() == Some("1") {
|
|
|
|
|
eprintln!(
|
|
|
|
|
"[vm-trace] instance-dispatch class={} method={} arity={} candidates=[{}, {}, {}]",
|
|
|
|
|
inst.class_name, method, args.len(), primary, alt, static_variant
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prefer stringify for toString() if present (semantic alias). Try instance first, then base.
|
|
|
|
|
let func_opt = if let Some(ref sname) = stringify_inst {
|
|
|
|
|
self.functions.get(sname).cloned()
|
|
|
|
|
} else { None }
|
|
|
|
|
.or_else(|| stringify_base.as_ref().and_then(|n| self.functions.get(n).cloned()))
|
|
|
|
|
.or_else(|| self.functions.get(&primary).cloned())
|
|
|
|
|
.or_else(|| self.functions.get(&alt).cloned())
|
|
|
|
|
.or_else(|| self.functions.get(&static_variant).cloned());
|
|
|
|
|
|
|
|
|
|
if let Some(func) = func_opt {
|
|
|
|
|
if std::env::var("NYASH_VM_TRACE").ok().as_deref() == Some("1") {
|
|
|
|
|
eprintln!("[vm-trace] instance-dispatch hit -> {}", func.signature.name);
|
|
|
|
|
}
|
|
|
|
|
// Build argv: me + args (works for both instance and static(me, ...))
|
2025-09-26 03:30:59 +09:00
|
|
|
let mut argv: Vec<VMValue> = Vec::with_capacity(1 + args.len());
|
|
|
|
|
argv.push(recv_vm.clone());
|
2025-09-26 14:34:42 +09:00
|
|
|
for a in args { argv.push(self.reg_load(*a)?); }
|
2025-09-26 03:30:59 +09:00
|
|
|
let ret = self.exec_function_inner(&func, Some(&argv))?;
|
2025-09-26 14:34:42 +09:00
|
|
|
if let Some(d) = dst { self.regs.insert(d, ret); }
|
2025-09-26 03:30:59 +09:00
|
|
|
return Ok(true);
|
2025-09-26 14:34:42 +09:00
|
|
|
} else {
|
|
|
|
|
// Conservative fallback: search unique function by name tail ".method/arity"
|
|
|
|
|
let tail = format!(".{}{}", method, format!("/{}", args.len()));
|
|
|
|
|
let mut cands: Vec<String> = self
|
|
|
|
|
.functions
|
|
|
|
|
.keys()
|
|
|
|
|
.filter(|k| k.ends_with(&tail))
|
|
|
|
|
.cloned()
|
|
|
|
|
.collect();
|
|
|
|
|
if cands.len() == 1 {
|
|
|
|
|
let fname = cands.remove(0);
|
|
|
|
|
if std::env::var("NYASH_VM_TRACE").ok().as_deref() == Some("1") {
|
|
|
|
|
eprintln!("[vm-trace] instance-dispatch fallback unique tail -> {}", fname);
|
|
|
|
|
}
|
|
|
|
|
if let Some(func) = self.functions.get(&fname).cloned() {
|
|
|
|
|
let mut argv: Vec<VMValue> = Vec::with_capacity(1 + args.len());
|
|
|
|
|
argv.push(recv_vm.clone());
|
|
|
|
|
for a in args { argv.push(self.reg_load(*a)?); }
|
|
|
|
|
let ret = self.exec_function_inner(&func, Some(&argv))?;
|
|
|
|
|
if let Some(d) = dst { self.regs.insert(d, ret); }
|
|
|
|
|
return Ok(true);
|
|
|
|
|
}
|
|
|
|
|
} else if cands.len() > 1 {
|
|
|
|
|
// Narrow by receiver class prefix (and optional "Instance" suffix)
|
|
|
|
|
let recv_cls = inst.class_name.clone();
|
|
|
|
|
let pref1 = format!("{}.", recv_cls);
|
|
|
|
|
let pref2 = format!("{}Instance.", recv_cls);
|
|
|
|
|
let mut filtered: Vec<String> = cands
|
|
|
|
|
.into_iter()
|
|
|
|
|
.filter(|k| k.starts_with(&pref1) || k.starts_with(&pref2))
|
|
|
|
|
.collect();
|
|
|
|
|
if filtered.len() == 1 {
|
|
|
|
|
let fname = filtered.remove(0);
|
|
|
|
|
if std::env::var("NYASH_VM_TRACE").ok().as_deref() == Some("1") {
|
|
|
|
|
eprintln!("[vm-trace] instance-dispatch narrowed by class -> {}", fname);
|
|
|
|
|
}
|
|
|
|
|
if let Some(func) = self.functions.get(&fname).cloned() {
|
|
|
|
|
let mut argv: Vec<VMValue> = Vec::with_capacity(1 + args.len());
|
|
|
|
|
argv.push(recv_vm.clone());
|
|
|
|
|
for a in args { argv.push(self.reg_load(*a)?); }
|
|
|
|
|
let ret = self.exec_function_inner(&func, Some(&argv))?;
|
|
|
|
|
if let Some(d) = dst { self.regs.insert(d, ret); }
|
|
|
|
|
return Ok(true);
|
|
|
|
|
}
|
|
|
|
|
} else if std::env::var("NYASH_VM_TRACE").ok().as_deref() == Some("1") {
|
|
|
|
|
eprintln!("[vm-trace] instance-dispatch multiple candidates remain after narrowing: {:?}", filtered);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-26 03:30:59 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(false)
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-26 14:34:42 +09:00
|
|
|
// moved: try_handle_array_box → handlers/boxes_array.rs
|
2025-09-26 03:30:59 +09:00
|
|
|
fn try_handle_array_box(
|
|
|
|
|
&mut self,
|
|
|
|
|
dst: Option<ValueId>,
|
|
|
|
|
box_val: ValueId,
|
|
|
|
|
method: &str,
|
|
|
|
|
args: &[ValueId],
|
|
|
|
|
) -> Result<bool, VMError> {
|
2025-09-26 14:34:42 +09:00
|
|
|
super::boxes_array::try_handle_array_box(self, dst, box_val, method, args)
|
2025-09-26 03:30:59 +09:00
|
|
|
}
|
|
|
|
|
|
2025-09-25 01:09:48 +09:00
|
|
|
fn invoke_plugin_box(
|
|
|
|
|
&mut self,
|
|
|
|
|
dst: Option<ValueId>,
|
|
|
|
|
box_val: ValueId,
|
|
|
|
|
method: &str,
|
|
|
|
|
args: &[ValueId],
|
|
|
|
|
) -> Result<(), VMError> {
|
|
|
|
|
let recv = self.reg_load(box_val)?;
|
|
|
|
|
let recv_box: Box<dyn NyashBox> = match recv.clone() {
|
|
|
|
|
VMValue::BoxRef(b) => b.share_box(),
|
|
|
|
|
other => other.to_nyash_box(),
|
|
|
|
|
};
|
|
|
|
|
if let Some(p) = recv_box
|
|
|
|
|
.as_any()
|
|
|
|
|
.downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>()
|
|
|
|
|
{
|
|
|
|
|
if p.box_type == "ConsoleBox" && method == "readLine" {
|
|
|
|
|
use std::io::{self, Read};
|
|
|
|
|
let mut s = String::new();
|
|
|
|
|
let mut stdin = io::stdin();
|
|
|
|
|
let mut buf = [0u8; 1];
|
|
|
|
|
while let Ok(n) = stdin.read(&mut buf) {
|
|
|
|
|
if n == 0 {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
let ch = buf[0] as char;
|
|
|
|
|
if ch == '\n' {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
s.push(ch);
|
|
|
|
|
if s.len() > 1_000_000 {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if let Some(d) = dst {
|
|
|
|
|
self.regs.insert(d, VMValue::String(s));
|
|
|
|
|
}
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
let host = crate::runtime::plugin_loader_unified::get_global_plugin_host();
|
|
|
|
|
let host = host.read().unwrap();
|
|
|
|
|
let mut argv: Vec<Box<dyn NyashBox>> = Vec::with_capacity(args.len());
|
|
|
|
|
for a in args {
|
|
|
|
|
argv.push(self.reg_load(*a)?.to_nyash_box());
|
|
|
|
|
}
|
|
|
|
|
match host.invoke_instance_method(&p.box_type, method, p.inner.instance_id, &argv) {
|
|
|
|
|
Ok(Some(ret)) => {
|
|
|
|
|
if let Some(d) = dst {
|
|
|
|
|
self.regs.insert(d, VMValue::from_nyash_box(ret));
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
Ok(None) => {
|
|
|
|
|
if let Some(d) = dst {
|
|
|
|
|
self.regs.insert(d, VMValue::Void);
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
Err(e) => Err(VMError::InvalidInstruction(format!(
|
|
|
|
|
"BoxCall {}.{} failed: {:?}",
|
|
|
|
|
p.box_type, method, e
|
|
|
|
|
))),
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2025-09-26 14:34:42 +09:00
|
|
|
// Generic toString fallback for any non-plugin box
|
|
|
|
|
if method == "toString" {
|
|
|
|
|
if let Some(d) = dst {
|
|
|
|
|
self.regs.insert(d, VMValue::String(recv_box.to_string_box().value));
|
|
|
|
|
}
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
2025-09-26 05:28:20 +09:00
|
|
|
// Dynamic fallback for user-defined InstanceBox: dispatch to lowered function "Class.method/Arity"
|
|
|
|
|
if let Some(inst) = recv_box.as_any().downcast_ref::<crate::instance_v2::InstanceBox>() {
|
|
|
|
|
let class_name = inst.class_name.clone();
|
|
|
|
|
let arity = args.len(); // function name arity excludes 'me'
|
|
|
|
|
let fname = format!("{}.{}{}", class_name, method, format!("/{}", arity));
|
|
|
|
|
if let Some(func) = self.functions.get(&fname).cloned() {
|
|
|
|
|
let mut argv: Vec<VMValue> = Vec::with_capacity(arity + 1);
|
|
|
|
|
// Pass receiver as first arg ('me')
|
|
|
|
|
argv.push(recv.clone());
|
|
|
|
|
for a in args {
|
|
|
|
|
argv.push(self.reg_load(*a)?);
|
|
|
|
|
}
|
|
|
|
|
let ret = self.exec_function_inner(&func, Some(&argv))?;
|
|
|
|
|
if let Some(d) = dst { self.regs.insert(d, ret); }
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-25 01:09:48 +09:00
|
|
|
Err(VMError::InvalidInstruction(format!(
|
|
|
|
|
"BoxCall unsupported on {}.{}",
|
|
|
|
|
recv_box.type_name(),
|
|
|
|
|
method
|
|
|
|
|
)))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|