Files
hakorune/src/backend/mir_interpreter/mod.rs
nyash-codex 772149c86d Analyzer安定化完了: NYASH_DISABLE_PLUGINS=1復元 + plugin無効化根治
## 修正内容
1. **hako_check.sh/run_tests.sh**: NYASH_DISABLE_PLUGINS=1 + NYASH_BOX_FACTORY_POLICY=builtin_first追加
2. **src/box_factory/plugin.rs**: NYASH_DISABLE_PLUGINS=1チェック追加
3. **src/box_factory/mod.rs**: plugin shortcut pathでNYASH_DISABLE_PLUGINS尊重
4. **tools/hako_check/render/graphviz.hako**: smart quotes修正(parse error解消)

## 根本原因
- NYASH_USE_PLUGIN_BUILTINS=1が自動設定され、ArrayBox/MapBoxがplugin経由で生成を試行
- bid/registry.rsで"Plugin loading temporarily disabled"の状態でも試行されエラー
- mod.rs:272のshortcut pathがNYASH_DISABLE_PLUGINSを無視していた

## テスト結果
- 10/11 PASS(HC011,13-18,21-22,31)
- HC012: 既存issue(JSON安定化未完)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-08 15:49:25 +09:00

236 lines
9.2 KiB
Rust

/*!
* Minimal MIR Interpreter
*
* Executes a subset of MIR instructions for fast iteration without LLVM/JIT.
* Supported: Const, BinOp(Add/Sub/Mul/Div/Mod), Compare, Load/Store, Branch, Jump, Return,
* Print/Debug (best-effort), Barrier/Safepoint (no-op).
*/
use std::collections::HashMap;
use crate::box_trait::NyashBox;
pub(super) use crate::backend::abi_util::{eq_vm, to_bool_vm};
pub(super) use crate::backend::vm::{VMError, VMValue};
pub(super) use crate::mir::{
BasicBlockId, BinaryOp, Callee, CompareOp, ConstValue, MirFunction, MirInstruction, MirModule,
ValueId,
};
mod exec;
mod handlers;
mod helpers;
mod method_router;
mod utils;
pub struct MirInterpreter {
pub(super) regs: HashMap<ValueId, VMValue>,
pub(super) mem: HashMap<ValueId, VMValue>,
// Object field storage keyed by stable object identity (Arc ptr addr fallback)
pub(super) obj_fields: HashMap<u64, HashMap<String, VMValue>>,
pub(super) functions: HashMap<String, MirFunction>,
pub(super) cur_fn: Option<String>,
// Trace context (dev-only; enabled with NYASH_VM_TRACE=1)
pub(super) last_block: Option<BasicBlockId>,
pub(super) last_inst: Option<MirInstruction>,
// Static box singleton instances (persistent across method calls)
pub(super) static_boxes: HashMap<String, crate::instance_v2::InstanceBox>,
// Static box declarations (metadata for creating instances)
pub(super) static_box_decls: HashMap<String, crate::core::model::BoxDeclaration>,
}
impl MirInterpreter {
pub fn new() -> Self {
Self {
regs: HashMap::new(),
mem: HashMap::new(),
obj_fields: HashMap::new(),
functions: HashMap::new(),
cur_fn: None,
last_block: None,
last_inst: None,
static_boxes: HashMap::new(),
static_box_decls: HashMap::new(),
}
}
/// Register static box declarations (called from vm.rs during setup)
pub fn register_static_box_decl(&mut self, name: String, decl: crate::core::model::BoxDeclaration) {
self.static_box_decls.insert(name, decl);
}
/// Ensure static box singleton instance exists, create if not
/// Returns mutable reference to the singleton instance
fn ensure_static_box_instance(&mut self, box_name: &str) -> Result<&mut crate::instance_v2::InstanceBox, VMError> {
// Check if instance already exists
if !self.static_boxes.contains_key(box_name) {
// Get declaration
let decl = self.static_box_decls.get(box_name)
.ok_or_else(|| VMError::InvalidInstruction(
format!("static box declaration not found: {}", box_name)
))?
.clone();
// Create instance from declaration
let instance = crate::instance_v2::InstanceBox::from_declaration(
box_name.to_string(),
decl.fields.clone(),
decl.methods.clone(),
);
self.static_boxes.insert(box_name.to_string(), instance);
if std::env::var("NYASH_VM_STATIC_TRACE").ok().as_deref() == Some("1") {
eprintln!("[vm-static] created singleton instance for static box: {}", box_name);
}
}
// Return mutable reference
self.static_boxes.get_mut(box_name)
.ok_or_else(|| VMError::InvalidInstruction(
format!("static box instance not found after creation: {}", box_name)
))
}
/// Check if a function name represents a static box method
/// Format: "BoxName.method/Arity"
fn is_static_box_method(&self, func_name: &str) -> Option<String> {
if let Some((box_name, _rest)) = func_name.split_once('.') {
if self.static_box_decls.contains_key(box_name) {
return Some(box_name.to_string());
}
}
None
}
/// Execute module entry (main) and return boxed result
pub fn execute_module(&mut self, module: &MirModule) -> Result<Box<dyn NyashBox>, VMError> {
// Snapshot functions for call resolution
self.functions = module.functions.clone();
// Determine entry function with sensible fallbacks
// Priority:
// 1) NYASH_ENTRY env (exact), then basename before '/' if provided (e.g., "Main.main/0" → "Main.main")
// 2) "Main.main" if present
// 3) "main" (legacy/simple scripts)
let mut candidates: Vec<String> = Vec::new();
if let Ok(e) = std::env::var("NYASH_ENTRY") {
if !e.trim().is_empty() {
candidates.push(e.trim().to_string());
}
}
candidates.push("Main.main".to_string());
candidates.push("main".to_string());
// Try candidates in order
let mut chosen: Option<&nyash_rust::mir::MirFunction> = None;
for c in &candidates {
// exact
if let Some(f) = module.functions.get(c) {
chosen = Some(f);
break;
}
// if contains '/': try name before '/'
if let Some((head, _)) = c.split_once('/') {
if let Some(f) = module.functions.get(head) {
chosen = Some(f);
break;
}
}
// if looks like "Box.method": try plain "main" as last resort only when c endswith .main
if c.ends_with(".main") {
if let Some(f) = module.functions.get("main") {
chosen = Some(f);
break;
}
}
}
let func = match chosen {
Some(f) => f,
None => {
// Build helpful error message
let mut names: Vec<&String> = module.functions.keys().collect();
names.sort();
let avail = names.into_iter().take(12).cloned().collect::<Vec<_>>().join(", ");
let tried = candidates.join(", ");
let msg = format!(
"entry function not found. searched: [{}]. available: [{}]. hint: define 'static box Main {{ method main(args){{ ... }} }}' or set NYASH_ENTRY=Name",
tried, avail
);
return Err(VMError::InvalidInstruction(msg));
}
};
// Prepare arguments if the entry takes parameters (pass script args as ArrayBox)
let ret = if func.signature.params.len() == 0 {
self.execute_function(func)?
} else {
// Build argv from (priority) HEX JSON, normal JSON, or NYASH_ARGV
// 1) NYASH_SCRIPT_ARGS_HEX_JSON: JSON array of hex-encoded UTF-8 strings
// 2) NYASH_SCRIPT_ARGS_JSON: JSON array of strings
// 3) NYASH_ARGV: JSON array (legacy)
let mut argv_list: Vec<String> = Vec::new();
if let Ok(s) = std::env::var("NYASH_SCRIPT_ARGS_HEX_JSON") {
if let Ok(v) = serde_json::from_str::<Vec<String>>(&s) {
let mut out = Vec::with_capacity(v.len());
for hx in v.into_iter() {
match hex_decode_to_string(&hx) {
Ok(ss) => out.push(ss),
Err(_) => out.push(String::new()),
}
}
argv_list = out;
}
} else if let Ok(s) = std::env::var("NYASH_SCRIPT_ARGS_JSON") {
if let Ok(v) = serde_json::from_str::<Vec<String>>(&s) { argv_list = v; }
} else if let Ok(s) = std::env::var("NYASH_ARGV") {
if let Ok(v) = serde_json::from_str::<Vec<String>>(&s) { argv_list = v; }
}
// Construct ArrayBox of StringBox
let array = crate::boxes::array::ArrayBox::new();
for a in argv_list.iter() {
let sb = crate::boxes::basic::StringBox::new(a);
let _ = array.push(Box::new(sb));
}
let boxed: Box<dyn crate::box_trait::NyashBox> = Box::new(array);
let arg0 = super::vm_types::VMValue::from_nyash_box(boxed);
// Fill remaining params with Void
let mut vm_args: Vec<super::vm_types::VMValue> = Vec::new();
vm_args.push(arg0);
for _ in 1..func.signature.params.len() {
vm_args.push(super::vm_types::VMValue::Void);
}
self.exec_function_inner(func, Some(&vm_args))?
};
Ok(ret.to_nyash_box())
}
fn execute_function(&mut self, func: &MirFunction) -> Result<VMValue, VMError> {
self.exec_function_inner(func, None)
}
}
fn hex_decode_to_string(hex: &str) -> Result<String, ()> {
let mut bytes: Vec<u8> = Vec::with_capacity(hex.len() / 2);
let mut it = hex.as_bytes().iter().cloned();
while let (Some(h), Some(l)) = (it.next(), it.next()) {
let hi = from_hex(h).ok_or(())?;
let lo = from_hex(l).ok_or(())?;
bytes.push((hi << 4) | lo);
}
match String::from_utf8(bytes) {
Ok(s) => Ok(s),
Err(e) => Ok(String::from_utf8_lossy(e.as_bytes()).into_owned()),
}
}
fn from_hex(b: u8) -> Option<u8> {
match b {
b'0'..=b'9' => Some(b - b'0'),
b'a'..=b'f' => Some(b - b'a' + 10),
b'A'..=b'F' => Some(b - b'A' + 10),
_ => None,
}
}