2025-11-02 15:43:43 +09:00
|
|
|
|
/*!
|
|
|
|
|
|
* CoreExecutor — JSON v0 → Execute (boxed)
|
|
|
|
|
|
*
|
|
|
|
|
|
* Responsibility
|
|
|
|
|
|
* - Single entry to execute a MIR(JSON) payload under Gate‑C/Core policy.
|
|
|
|
|
|
* - Encapsulates: optional canonicalize, v1-bridge try, v0-parse fallback,
|
|
|
|
|
|
* OOB strict observation, and rc mapping via MIR Interpreter.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Notes
|
|
|
|
|
|
* - For now, execution uses the existing MIR Interpreter runner
|
|
|
|
|
|
* (execute_mir_module_quiet_exit). Later we can swap internals to call
|
|
|
|
|
|
* the Core Dispatcher directly without touching callers.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
use super::NyashRunner;
|
2025-11-02 15:46:58 +09:00
|
|
|
|
use std::io::Write;
|
2025-11-02 15:43:43 +09:00
|
|
|
|
|
|
|
|
|
|
pub(crate) fn run_json_v0(runner: &NyashRunner, json: &str) -> i32 {
|
2025-11-02 15:46:58 +09:00
|
|
|
|
// Optional: direct Core Dispatcher via child nyash (boxed)
|
|
|
|
|
|
// Toggle: HAKO_CORE_DIRECT=1 (alias: NYASH_CORE_DIRECT)
|
|
|
|
|
|
let core_direct = std::env::var("HAKO_CORE_DIRECT").ok().as_deref() == Some("1")
|
|
|
|
|
|
|| std::env::var("NYASH_CORE_DIRECT").ok().as_deref() == Some("1");
|
|
|
|
|
|
if core_direct {
|
|
|
|
|
|
if let Some(rc) = try_run_core_direct(json) {
|
|
|
|
|
|
return rc;
|
|
|
|
|
|
}
|
|
|
|
|
|
eprintln!("[core-exec] direct Core failed; falling back to VM interpreter");
|
|
|
|
|
|
}
|
2025-11-02 15:43:43 +09:00
|
|
|
|
let mut payload = json.to_string();
|
|
|
|
|
|
|
|
|
|
|
|
let use_core_wrapper = crate::config::env::nyvm_core_wrapper();
|
|
|
|
|
|
let use_downconvert = crate::config::env::nyvm_v1_downconvert();
|
|
|
|
|
|
|
|
|
|
|
|
if use_core_wrapper || use_downconvert {
|
|
|
|
|
|
// Best-effort canonicalize
|
|
|
|
|
|
if let Ok(j) = crate::runner::modes::common_util::core_bridge::canonicalize_module_json(&payload) {
|
|
|
|
|
|
payload = j;
|
|
|
|
|
|
}
|
|
|
|
|
|
match crate::runner::json_v1_bridge::try_parse_v1_to_module(&payload) {
|
|
|
|
|
|
Ok(Some(module)) => {
|
|
|
|
|
|
super::json_v0_bridge::maybe_dump_mir(&module);
|
|
|
|
|
|
// OOB strict: reset observation flag
|
|
|
|
|
|
crate::runner::child_env::pre_run_reset_oob_if_strict();
|
|
|
|
|
|
let rc = runner.execute_mir_module_quiet_exit(&module);
|
|
|
|
|
|
if crate::config::env::oob_strict_fail() && crate::runtime::observe::oob_seen() {
|
|
|
|
|
|
eprintln!("[gate-c][oob-strict] Out-of-bounds observed → exit(1)");
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
return rc;
|
|
|
|
|
|
}
|
|
|
|
|
|
Ok(None) => { /* fall through to v0 */ }
|
|
|
|
|
|
Err(e) => {
|
|
|
|
|
|
eprintln!("❌ JSON v1 bridge error: {}", e);
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
match super::json_v0_bridge::parse_json_v0_to_module(&payload) {
|
|
|
|
|
|
Ok(module) => {
|
|
|
|
|
|
super::json_v0_bridge::maybe_dump_mir(&module);
|
|
|
|
|
|
crate::runner::child_env::pre_run_reset_oob_if_strict();
|
|
|
|
|
|
let rc = runner.execute_mir_module_quiet_exit(&module);
|
|
|
|
|
|
if crate::config::env::oob_strict_fail() && crate::runtime::observe::oob_seen() {
|
|
|
|
|
|
eprintln!("[gate-c][oob-strict] Out-of-bounds observed → exit(1)");
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
rc
|
|
|
|
|
|
}
|
|
|
|
|
|
Err(e) => {
|
|
|
|
|
|
eprintln!("❌ JSON v0 bridge error: {}", e);
|
|
|
|
|
|
1
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-02 15:46:58 +09:00
|
|
|
|
fn try_run_core_direct(json: &str) -> Option<i32> {
|
|
|
|
|
|
// Generate a temporary Hako program that includes the Core dispatcher
|
|
|
|
|
|
// and calls NyVmDispatcher.run(json), printing the numeric result.
|
|
|
|
|
|
let tmp_dir = std::path::Path::new("tmp");
|
|
|
|
|
|
let _ = std::fs::create_dir_all(tmp_dir);
|
|
|
|
|
|
let script_path = tmp_dir.join("core_exec_direct.hako");
|
|
|
|
|
|
// Escape JSON into Hako string literal (simple backslash+quote escaping)
|
|
|
|
|
|
let mut j = String::new();
|
|
|
|
|
|
for ch in json.chars() {
|
|
|
|
|
|
match ch {
|
|
|
|
|
|
'\\' => j.push_str("\\\\"),
|
|
|
|
|
|
'"' => j.push_str("\\\""),
|
|
|
|
|
|
_ => j.push(ch),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
let code = format!(
|
|
|
|
|
|
"include \"lang/src/vm/core/dispatcher.hako\"\nstatic box Main {{ method main(args) {{ local j=\"{}\"; local r=NyVmDispatcher.run(j); print(r); return 0 }} }}\n",
|
|
|
|
|
|
j
|
|
|
|
|
|
);
|
|
|
|
|
|
if let Ok(mut f) = std::fs::File::create(&script_path) {
|
|
|
|
|
|
let _ = f.write_all(code.as_bytes());
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return None;
|
|
|
|
|
|
}
|
|
|
|
|
|
// Determine nyash binary (current executable)
|
|
|
|
|
|
let exe = std::env::current_exe().ok()?;
|
|
|
|
|
|
let mut cmd = std::process::Command::new(exe);
|
|
|
|
|
|
crate::runner::child_env::apply_core_wrapper_env(&mut cmd);
|
|
|
|
|
|
// Quiet: parse only the last numeric line
|
|
|
|
|
|
let out = cmd
|
|
|
|
|
|
.args(["--backend", "vm", script_path.to_string_lossy().as_ref()])
|
|
|
|
|
|
.output()
|
|
|
|
|
|
.ok()?;
|
|
|
|
|
|
let stdout = String::from_utf8_lossy(&out.stdout);
|
|
|
|
|
|
// Parse last numeric line
|
|
|
|
|
|
let mut rc: Option<i32> = None;
|
|
|
|
|
|
for line in stdout.lines().rev() {
|
|
|
|
|
|
let s = line.trim();
|
|
|
|
|
|
if s.is_empty() { continue; }
|
|
|
|
|
|
if let Ok(v) = s.parse::<i64>() {
|
|
|
|
|
|
let m = ((v % 256) + 256) % 256;
|
|
|
|
|
|
rc = Some(m as i32);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-02 16:24:50 +09:00
|
|
|
|
if let Some(code) = rc { Some(code) } else { Some(1) }
|
2025-11-02 15:46:58 +09:00
|
|
|
|
}
|