chore: Phase 25.1 完了 - LoopForm v2/Stage1 CLI/環境変数削減 + Phase 26-D からの変更
Phase 25.1 完了成果: - ✅ LoopForm v2 テスト・ドキュメント・コメント完備 - 4ケース(A/B/C/D)完全テストカバレッジ - 最小再現ケース作成(SSAバグ調査用) - SSOT文書作成(loopform_ssot.md) - 全ソースに [LoopForm] コメントタグ追加 - ✅ Stage-1 CLI デバッグ環境構築 - stage1_cli.hako 実装 - stage1_bridge.rs ブリッジ実装 - デバッグツール作成(stage1_debug.sh/stage1_minimal.sh) - アーキテクチャ改善提案文書 - ✅ 環境変数削減計画策定 - 25変数の完全調査・分類 - 6段階削減ロードマップ(25→5、80%削減) - 即時削除可能変数特定(NYASH_CONFIG/NYASH_DEBUG) Phase 26-D からの累積変更: - PHI実装改善(ExitPhiBuilder/HeaderPhiBuilder等) - MIRビルダーリファクタリング - 型伝播・最適化パス改善 - その他約300ファイルの累積変更 🎯 技術的成果: - SSAバグ根本原因特定(条件分岐内loop変数変更) - Region+next_iパターン適用完了(UsingCollectorBox等) - LoopFormパターン文書化・テスト化完了 - セルフホスティング基盤強化 Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: ChatGPT <noreply@openai.com> Co-Authored-By: Task Assistant <task@anthropic.com>
This commit is contained in:
@ -10,8 +10,12 @@ impl MirInterpreter {
|
||||
let key = format!("{}.{}", target, method);
|
||||
for pat in flt.split(',') {
|
||||
let p = pat.trim();
|
||||
if p.is_empty() { continue; }
|
||||
if p == method || p == key { return true; }
|
||||
if p.is_empty() {
|
||||
continue;
|
||||
}
|
||||
if p == method || p == key {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -23,7 +27,9 @@ impl MirInterpreter {
|
||||
if let JsonValue::Object(ref mut m) = v {
|
||||
if !m.contains_key("version") {
|
||||
m.insert("version".to_string(), JsonValue::from(0));
|
||||
if let Ok(out) = serde_json::to_string(&v) { return out; }
|
||||
if let Ok(out) = serde_json::to_string(&v) {
|
||||
return out;
|
||||
}
|
||||
}
|
||||
}
|
||||
s.to_string()
|
||||
@ -64,9 +70,15 @@ impl MirInterpreter {
|
||||
VMValue::Void => println!("null"),
|
||||
VMValue::String(s) => println!("{}", s),
|
||||
VMValue::BoxRef(bx) => {
|
||||
if bx.as_any().downcast_ref::<crate::box_trait::VoidBox>().is_some() {
|
||||
if bx
|
||||
.as_any()
|
||||
.downcast_ref::<crate::box_trait::VoidBox>()
|
||||
.is_some()
|
||||
{
|
||||
println!("null");
|
||||
} else if let Some(sb) = bx.as_any().downcast_ref::<crate::box_trait::StringBox>() {
|
||||
} else if let Some(sb) =
|
||||
bx.as_any().downcast_ref::<crate::box_trait::StringBox>()
|
||||
{
|
||||
println!("{}", sb.value);
|
||||
} else {
|
||||
println!("{}", v.to_string());
|
||||
@ -90,9 +102,15 @@ impl MirInterpreter {
|
||||
VMValue::Void => eprintln!("[warn] null"),
|
||||
VMValue::String(s) => eprintln!("[warn] {}", s),
|
||||
VMValue::BoxRef(bx) => {
|
||||
if bx.as_any().downcast_ref::<crate::box_trait::VoidBox>().is_some() {
|
||||
if bx
|
||||
.as_any()
|
||||
.downcast_ref::<crate::box_trait::VoidBox>()
|
||||
.is_some()
|
||||
{
|
||||
eprintln!("[warn] null");
|
||||
} else if let Some(sb) = bx.as_any().downcast_ref::<crate::box_trait::StringBox>() {
|
||||
} else if let Some(sb) =
|
||||
bx.as_any().downcast_ref::<crate::box_trait::StringBox>()
|
||||
{
|
||||
eprintln!("[warn] {}", sb.value);
|
||||
} else {
|
||||
eprintln!("[warn] {}", v.to_string());
|
||||
@ -116,9 +134,15 @@ impl MirInterpreter {
|
||||
VMValue::Void => eprintln!("[error] null"),
|
||||
VMValue::String(s) => eprintln!("[error] {}", s),
|
||||
VMValue::BoxRef(bx) => {
|
||||
if bx.as_any().downcast_ref::<crate::box_trait::VoidBox>().is_some() {
|
||||
if bx
|
||||
.as_any()
|
||||
.downcast_ref::<crate::box_trait::VoidBox>()
|
||||
.is_some()
|
||||
{
|
||||
eprintln!("[error] null");
|
||||
} else if let Some(sb) = bx.as_any().downcast_ref::<crate::box_trait::StringBox>() {
|
||||
} else if let Some(sb) =
|
||||
bx.as_any().downcast_ref::<crate::box_trait::StringBox>()
|
||||
{
|
||||
eprintln!("[error] {}", sb.value);
|
||||
} else {
|
||||
eprintln!("[error] {}", v.to_string());
|
||||
@ -140,15 +164,29 @@ impl MirInterpreter {
|
||||
if std::env::var("HAKO_V1_EXTERN_PROVIDER").ok().as_deref() == Some("1") {
|
||||
return Some(Ok(VMValue::String(String::new())));
|
||||
}
|
||||
if args.is_empty() { return Some(Err(ErrorBuilder::arg_count_mismatch("env.mirbuilder.emit", 1, args.len()))); }
|
||||
let program_json = match self.reg_load(args[0]) { Ok(v) => v.to_string(), Err(e) => return Some(Err(e)) };
|
||||
if args.is_empty() {
|
||||
return Some(Err(ErrorBuilder::arg_count_mismatch(
|
||||
"env.mirbuilder.emit",
|
||||
1,
|
||||
args.len(),
|
||||
)));
|
||||
}
|
||||
let program_json = match self.reg_load(args[0]) {
|
||||
Ok(v) => v.to_string(),
|
||||
Err(e) => return Some(Err(e)),
|
||||
};
|
||||
|
||||
// Phase 21.8: Read imports from environment variable if present
|
||||
let imports = if let Ok(imports_json) = std::env::var("HAKO_MIRBUILDER_IMPORTS") {
|
||||
match serde_json::from_str::<std::collections::HashMap<String, String>>(&imports_json) {
|
||||
match serde_json::from_str::<std::collections::HashMap<String, String>>(
|
||||
&imports_json,
|
||||
) {
|
||||
Ok(map) => map,
|
||||
Err(e) => {
|
||||
eprintln!("[mirbuilder/imports] Failed to parse HAKO_MIRBUILDER_IMPORTS: {}", e);
|
||||
eprintln!(
|
||||
"[mirbuilder/imports] Failed to parse HAKO_MIRBUILDER_IMPORTS: {}",
|
||||
e
|
||||
);
|
||||
std::collections::HashMap::new()
|
||||
}
|
||||
}
|
||||
@ -156,10 +194,17 @@ impl MirInterpreter {
|
||||
std::collections::HashMap::new()
|
||||
};
|
||||
|
||||
let res = match crate::host_providers::mir_builder::program_json_to_mir_json_with_imports(&program_json, imports) {
|
||||
Ok(s) => Ok(VMValue::String(Self::patch_mir_json_version(&s))),
|
||||
Err(e) => Err(ErrorBuilder::with_context("env.mirbuilder.emit", &e.to_string())),
|
||||
};
|
||||
let res =
|
||||
match crate::host_providers::mir_builder::program_json_to_mir_json_with_imports(
|
||||
&program_json,
|
||||
imports,
|
||||
) {
|
||||
Ok(s) => Ok(VMValue::String(Self::patch_mir_json_version(&s))),
|
||||
Err(e) => Err(ErrorBuilder::with_context(
|
||||
"env.mirbuilder.emit",
|
||||
&e.to_string(),
|
||||
)),
|
||||
};
|
||||
Some(res)
|
||||
}
|
||||
"env.codegen.emit_object" => {
|
||||
@ -167,55 +212,114 @@ impl MirInterpreter {
|
||||
if std::env::var("HAKO_V1_EXTERN_PROVIDER").ok().as_deref() == Some("1") {
|
||||
return Some(Ok(VMValue::String(String::new())));
|
||||
}
|
||||
if args.is_empty() { return Some(Err(ErrorBuilder::arg_count_mismatch("env.codegen.emit_object", 1, args.len()))); }
|
||||
let mir_json_raw = match self.reg_load(args[0]) { Ok(v) => v.to_string(), Err(e) => return Some(Err(e)) };
|
||||
if args.is_empty() {
|
||||
return Some(Err(ErrorBuilder::arg_count_mismatch(
|
||||
"env.codegen.emit_object",
|
||||
1,
|
||||
args.len(),
|
||||
)));
|
||||
}
|
||||
let mir_json_raw = match self.reg_load(args[0]) {
|
||||
Ok(v) => v.to_string(),
|
||||
Err(e) => return Some(Err(e)),
|
||||
};
|
||||
// Normalize to v1 shape if missing/legacy (prevents harness NoneType errors)
|
||||
let mir_json = Self::patch_mir_json_version(&mir_json_raw);
|
||||
let opts = crate::host_providers::llvm_codegen::Opts {
|
||||
out: None,
|
||||
nyrt: std::env::var("NYASH_EMIT_EXE_NYRT").ok().map(std::path::PathBuf::from),
|
||||
opt_level: std::env::var("HAKO_LLVM_OPT_LEVEL").ok().or(Some("0".to_string())),
|
||||
nyrt: std::env::var("NYASH_EMIT_EXE_NYRT")
|
||||
.ok()
|
||||
.map(std::path::PathBuf::from),
|
||||
opt_level: std::env::var("HAKO_LLVM_OPT_LEVEL")
|
||||
.ok()
|
||||
.or(Some("0".to_string())),
|
||||
timeout_ms: None,
|
||||
};
|
||||
let res = match crate::host_providers::llvm_codegen::mir_json_to_object(&mir_json, opts) {
|
||||
let res = match crate::host_providers::llvm_codegen::mir_json_to_object(
|
||||
&mir_json, opts,
|
||||
) {
|
||||
Ok(p) => Ok(VMValue::String(p.to_string_lossy().into_owned())),
|
||||
Err(e) => Err(ErrorBuilder::with_context("env.codegen.emit_object", &e.to_string())),
|
||||
Err(e) => Err(ErrorBuilder::with_context(
|
||||
"env.codegen.emit_object",
|
||||
&e.to_string(),
|
||||
)),
|
||||
};
|
||||
Some(res)
|
||||
}
|
||||
"env.codegen.link_object" => {
|
||||
// Only supported on C-API route; expect 1 or 2 args: obj_path [, exe_out]
|
||||
let obj_path = match args.get(0) {
|
||||
Some(v) => match self.reg_load(*v) { Ok(v) => v.to_string(), Err(e) => return Some(Err(e)) },
|
||||
None => return Some(Err(self.err_invalid("env.codegen.link_object expects 1+ args"))),
|
||||
Some(v) => match self.reg_load(*v) {
|
||||
Ok(v) => v.to_string(),
|
||||
Err(e) => return Some(Err(e)),
|
||||
},
|
||||
None => {
|
||||
return Some(Err(
|
||||
self.err_invalid("env.codegen.link_object expects 1+ args")
|
||||
))
|
||||
}
|
||||
};
|
||||
let exe_out = match args.get(1) {
|
||||
Some(v) => Some(match self.reg_load(*v) { Ok(v) => v.to_string(), Err(e) => return Some(Err(e)) }),
|
||||
Some(v) => Some(match self.reg_load(*v) {
|
||||
Ok(v) => v.to_string(),
|
||||
Err(e) => return Some(Err(e)),
|
||||
}),
|
||||
None => None,
|
||||
};
|
||||
let extra = std::env::var("HAKO_AOT_LDFLAGS").ok();
|
||||
// Require C-API toggles
|
||||
if std::env::var("NYASH_LLVM_USE_CAPI").ok().as_deref() != Some("1") ||
|
||||
std::env::var("HAKO_V1_EXTERN_PROVIDER_C_ABI").ok().as_deref() != Some("1") {
|
||||
return Some(Err(ErrorBuilder::invalid_instruction("env.codegen.link_object: C-API route disabled")));
|
||||
if std::env::var("NYASH_LLVM_USE_CAPI").ok().as_deref() != Some("1")
|
||||
|| std::env::var("HAKO_V1_EXTERN_PROVIDER_C_ABI")
|
||||
.ok()
|
||||
.as_deref()
|
||||
!= Some("1")
|
||||
{
|
||||
return Some(Err(ErrorBuilder::invalid_instruction(
|
||||
"env.codegen.link_object: C-API route disabled",
|
||||
)));
|
||||
}
|
||||
let obj = std::path::PathBuf::from(obj_path);
|
||||
let exe = exe_out.map(std::path::PathBuf::from).unwrap_or_else(|| std::env::temp_dir().join("hako_link_out.exe"));
|
||||
match crate::host_providers::llvm_codegen::link_object_capi(&obj, &exe, extra.as_deref()) {
|
||||
let exe = exe_out
|
||||
.map(std::path::PathBuf::from)
|
||||
.unwrap_or_else(|| std::env::temp_dir().join("hako_link_out.exe"));
|
||||
match crate::host_providers::llvm_codegen::link_object_capi(
|
||||
&obj,
|
||||
&exe,
|
||||
extra.as_deref(),
|
||||
) {
|
||||
Ok(()) => Some(Ok(VMValue::String(exe.to_string_lossy().into_owned()))),
|
||||
Err(e) => Some(Err(ErrorBuilder::with_context("env.codegen.link_object", &e.to_string()))),
|
||||
Err(e) => Some(Err(ErrorBuilder::with_context(
|
||||
"env.codegen.link_object",
|
||||
&e.to_string(),
|
||||
))),
|
||||
}
|
||||
}
|
||||
// Environment
|
||||
"env.get" => {
|
||||
if args.is_empty() { return Some(Err(ErrorBuilder::arg_count_mismatch("env.get", 1, args.len()))); }
|
||||
let key = match self.reg_load(args[0]) { Ok(v) => v.to_string(), Err(e) => return Some(Err(e)) };
|
||||
if args.is_empty() {
|
||||
return Some(Err(ErrorBuilder::arg_count_mismatch(
|
||||
"env.get",
|
||||
1,
|
||||
args.len(),
|
||||
)));
|
||||
}
|
||||
let key = match self.reg_load(args[0]) {
|
||||
Ok(v) => v.to_string(),
|
||||
Err(e) => return Some(Err(e)),
|
||||
};
|
||||
let val = std::env::var(&key).ok();
|
||||
Some(Ok(match val { Some(s) => VMValue::String(s), None => VMValue::Void }))
|
||||
Some(Ok(match val {
|
||||
Some(s) => VMValue::String(s),
|
||||
None => VMValue::Void,
|
||||
}))
|
||||
}
|
||||
"env.set" => {
|
||||
if args.len() < 2 {
|
||||
return Some(Err(ErrorBuilder::arg_count_mismatch("env.set", 2, args.len())));
|
||||
return Some(Err(ErrorBuilder::arg_count_mismatch(
|
||||
"env.set",
|
||||
2,
|
||||
args.len(),
|
||||
)));
|
||||
}
|
||||
let key = match self.reg_load(args[0]) {
|
||||
Ok(v) => v.to_string(),
|
||||
@ -247,24 +351,24 @@ impl MirInterpreter {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Some(Err(self.err_invalid(
|
||||
"env.box_introspect.kind expects 1 arg",
|
||||
)));
|
||||
return Some(Err(
|
||||
self.err_invalid("env.box_introspect.kind expects 1 arg")
|
||||
));
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "plugins", not(target_arch = "wasm32")))]
|
||||
let result = plugin_loader_v2::handle_box_introspect("kind", &collected);
|
||||
#[cfg(any(not(feature = "plugins"), target_arch = "wasm32"))]
|
||||
let result: crate::bid::BidResult<Option<Box<dyn crate::box_trait::NyashBox>>> =
|
||||
Err(crate::bid::BidError::PluginError);
|
||||
let result: crate::bid::BidResult<
|
||||
Option<Box<dyn crate::box_trait::NyashBox>>,
|
||||
> = Err(crate::bid::BidError::PluginError);
|
||||
|
||||
match result {
|
||||
Ok(Some(b)) => Some(Ok(VMValue::BoxRef(Arc::from(b)))),
|
||||
Ok(None) => Some(Ok(VMValue::Void)),
|
||||
Err(e) => Some(Err(self.err_with_context(
|
||||
"env.box_introspect.kind",
|
||||
&format!("{:?}", e),
|
||||
))),
|
||||
Err(e) => Some(Err(
|
||||
self.err_with_context("env.box_introspect.kind", &format!("{:?}", e))
|
||||
)),
|
||||
}
|
||||
}
|
||||
"hostbridge.extern_invoke" => {
|
||||
@ -272,17 +376,30 @@ impl MirInterpreter {
|
||||
eprintln!("[hb:entry:provider] hostbridge.extern_invoke");
|
||||
}
|
||||
if args.len() < 2 {
|
||||
return Some(Err(self.err_invalid("extern_invoke expects at least 2 args")));
|
||||
return Some(Err(
|
||||
self.err_invalid("extern_invoke expects at least 2 args")
|
||||
));
|
||||
}
|
||||
let name = match self.reg_load(args[0]) { Ok(v) => v.to_string(), Err(e) => return Some(Err(e)) };
|
||||
let method = match self.reg_load(args[1]) { Ok(v) => v.to_string(), Err(e) => return Some(Err(e)) };
|
||||
let name = match self.reg_load(args[0]) {
|
||||
Ok(v) => v.to_string(),
|
||||
Err(e) => return Some(Err(e)),
|
||||
};
|
||||
let method = match self.reg_load(args[1]) {
|
||||
Ok(v) => v.to_string(),
|
||||
Err(e) => return Some(Err(e)),
|
||||
};
|
||||
// Extract first payload arg (optional)
|
||||
let mut first_arg_str: Option<String> = None;
|
||||
if let Some(a2) = args.get(2) {
|
||||
let v = match self.reg_load(*a2) { Ok(v) => v, Err(e) => return Some(Err(e)) };
|
||||
let v = match self.reg_load(*a2) {
|
||||
Ok(v) => v,
|
||||
Err(e) => return Some(Err(e)),
|
||||
};
|
||||
match v {
|
||||
VMValue::BoxRef(b) => {
|
||||
if let Some(ab) = b.as_any().downcast_ref::<crate::boxes::array::ArrayBox>() {
|
||||
if let Some(ab) =
|
||||
b.as_any().downcast_ref::<crate::boxes::array::ArrayBox>()
|
||||
{
|
||||
let idx: Box<dyn crate::box_trait::NyashBox> =
|
||||
Box::new(crate::box_trait::IntegerBox::new(0));
|
||||
let elem = ab.get(idx);
|
||||
@ -299,16 +416,27 @@ impl MirInterpreter {
|
||||
eprintln!("[hb:dispatch:provider] {} {}", name, method);
|
||||
}
|
||||
let out = match (name.as_str(), method.as_str()) {
|
||||
("env.codegen", "link_object") if std::env::var("HAKO_CABI_TRACE").ok().as_deref() == Some("1") => {
|
||||
("env.codegen", "link_object")
|
||||
if std::env::var("HAKO_CABI_TRACE").ok().as_deref() == Some("1") =>
|
||||
{
|
||||
// Trace payload shape before actual handling
|
||||
if let Some(a2) = args.get(2) {
|
||||
let v = match self.reg_load(*a2) { Ok(v) => v, Err(_) => VMValue::Void };
|
||||
let v = match self.reg_load(*a2) {
|
||||
Ok(v) => v,
|
||||
Err(_) => VMValue::Void,
|
||||
};
|
||||
match &v {
|
||||
VMValue::BoxRef(b) => {
|
||||
if b.as_any().downcast_ref::<crate::boxes::array::ArrayBox>().is_some() {
|
||||
if b.as_any()
|
||||
.downcast_ref::<crate::boxes::array::ArrayBox>()
|
||||
.is_some()
|
||||
{
|
||||
eprintln!("[hb:provider:args] link_object third=ArrayBox");
|
||||
} else {
|
||||
eprintln!("[hb:provider:args] link_object third=BoxRef({})", b.type_name());
|
||||
eprintln!(
|
||||
"[hb:provider:args] link_object third=BoxRef({})",
|
||||
b.type_name()
|
||||
);
|
||||
}
|
||||
}
|
||||
other => {
|
||||
@ -327,13 +455,19 @@ impl MirInterpreter {
|
||||
};
|
||||
match v {
|
||||
VMValue::BoxRef(b) => {
|
||||
if let Some(ab) = b.as_any().downcast_ref::<crate::boxes::array::ArrayBox>() {
|
||||
let idx0: Box<dyn crate::box_trait::NyashBox> = Box::new(crate::box_trait::IntegerBox::new(0));
|
||||
if let Some(ab) =
|
||||
b.as_any().downcast_ref::<crate::boxes::array::ArrayBox>()
|
||||
{
|
||||
let idx0: Box<dyn crate::box_trait::NyashBox> =
|
||||
Box::new(crate::box_trait::IntegerBox::new(0));
|
||||
let elem0 = ab.get(idx0).to_string_box().value;
|
||||
let mut exe: Option<String> = None;
|
||||
let idx1: Box<dyn crate::box_trait::NyashBox> = Box::new(crate::box_trait::IntegerBox::new(1));
|
||||
let idx1: Box<dyn crate::box_trait::NyashBox> =
|
||||
Box::new(crate::box_trait::IntegerBox::new(1));
|
||||
let e1 = ab.get(idx1).to_string_box().value;
|
||||
if !e1.is_empty() { exe = Some(e1); }
|
||||
if !e1.is_empty() {
|
||||
exe = Some(e1);
|
||||
}
|
||||
(elem0, exe)
|
||||
} else {
|
||||
(b.to_string_box().value, None)
|
||||
@ -342,25 +476,47 @@ impl MirInterpreter {
|
||||
_ => (v.to_string(), None),
|
||||
}
|
||||
} else {
|
||||
return Some(Err(self.err_invalid("extern_invoke env.codegen.link_object expects args array")));
|
||||
return Some(Err(self.err_invalid(
|
||||
"extern_invoke env.codegen.link_object expects args array",
|
||||
)));
|
||||
};
|
||||
if std::env::var("NYASH_LLVM_USE_CAPI").ok().as_deref() != Some("1") ||
|
||||
std::env::var("HAKO_V1_EXTERN_PROVIDER_C_ABI").ok().as_deref() != Some("1") {
|
||||
return Some(Err(ErrorBuilder::invalid_instruction("env.codegen.link_object: C-API route disabled")));
|
||||
if std::env::var("NYASH_LLVM_USE_CAPI").ok().as_deref() != Some("1")
|
||||
|| std::env::var("HAKO_V1_EXTERN_PROVIDER_C_ABI")
|
||||
.ok()
|
||||
.as_deref()
|
||||
!= Some("1")
|
||||
{
|
||||
return Some(Err(ErrorBuilder::invalid_instruction(
|
||||
"env.codegen.link_object: C-API route disabled",
|
||||
)));
|
||||
}
|
||||
let extra = std::env::var("HAKO_AOT_LDFLAGS").ok();
|
||||
let obj = std::path::PathBuf::from(objs);
|
||||
let exe = exe_out.map(std::path::PathBuf::from).unwrap_or_else(|| std::env::temp_dir().join("hako_link_out.exe"));
|
||||
match crate::host_providers::llvm_codegen::link_object_capi(&obj, &exe, extra.as_deref()) {
|
||||
let exe = exe_out
|
||||
.map(std::path::PathBuf::from)
|
||||
.unwrap_or_else(|| std::env::temp_dir().join("hako_link_out.exe"));
|
||||
match crate::host_providers::llvm_codegen::link_object_capi(
|
||||
&obj,
|
||||
&exe,
|
||||
extra.as_deref(),
|
||||
) {
|
||||
Ok(()) => Ok(VMValue::String(exe.to_string_lossy().into_owned())),
|
||||
Err(e) => Err(ErrorBuilder::with_context("env.codegen.link_object", &e.to_string()))
|
||||
Err(e) => Err(ErrorBuilder::with_context(
|
||||
"env.codegen.link_object",
|
||||
&e.to_string(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
("env.mirbuilder", "emit") => {
|
||||
if let Some(s) = first_arg_str {
|
||||
// Phase 21.8: Read imports from environment variable if present
|
||||
let imports = if let Ok(imports_json) = std::env::var("HAKO_MIRBUILDER_IMPORTS") {
|
||||
match serde_json::from_str::<std::collections::HashMap<String, String>>(&imports_json) {
|
||||
let imports = if let Ok(imports_json) =
|
||||
std::env::var("HAKO_MIRBUILDER_IMPORTS")
|
||||
{
|
||||
match serde_json::from_str::<
|
||||
std::collections::HashMap<String, String>,
|
||||
>(&imports_json)
|
||||
{
|
||||
Ok(map) => map,
|
||||
Err(e) => {
|
||||
eprintln!("[mirbuilder/imports] Failed to parse HAKO_MIRBUILDER_IMPORTS: {}", e);
|
||||
@ -383,16 +539,21 @@ impl MirInterpreter {
|
||||
if let Some(s) = first_arg_str {
|
||||
let opts = crate::host_providers::llvm_codegen::Opts {
|
||||
out: None,
|
||||
nyrt: std::env::var("NYASH_EMIT_EXE_NYRT").ok().map(std::path::PathBuf::from),
|
||||
nyrt: std::env::var("NYASH_EMIT_EXE_NYRT")
|
||||
.ok()
|
||||
.map(std::path::PathBuf::from),
|
||||
opt_level: std::env::var("HAKO_LLVM_OPT_LEVEL").ok(),
|
||||
timeout_ms: None,
|
||||
};
|
||||
match crate::host_providers::llvm_codegen::mir_json_to_object(&s, opts) {
|
||||
match crate::host_providers::llvm_codegen::mir_json_to_object(&s, opts)
|
||||
{
|
||||
Ok(p) => Ok(VMValue::String(p.to_string_lossy().into_owned())),
|
||||
Err(e) => Err(self.err_with_context("env.codegen.emit_object", &e.to_string())),
|
||||
Err(e) => Err(self
|
||||
.err_with_context("env.codegen.emit_object", &e.to_string())),
|
||||
}
|
||||
} else {
|
||||
Err(self.err_invalid("extern_invoke env.codegen.emit_object expects 1 arg"))
|
||||
Err(self
|
||||
.err_invalid("extern_invoke env.codegen.emit_object expects 1 arg"))
|
||||
}
|
||||
}
|
||||
("env.codegen", "link_object") => {
|
||||
@ -408,12 +569,18 @@ impl MirInterpreter {
|
||||
};
|
||||
match v {
|
||||
VMValue::BoxRef(b) => {
|
||||
if let Some(ab) = b.as_any().downcast_ref::<crate::boxes::array::ArrayBox>() {
|
||||
let idx0: Box<dyn crate::box_trait::NyashBox> = Box::new(crate::box_trait::IntegerBox::new(0));
|
||||
if let Some(ab) =
|
||||
b.as_any().downcast_ref::<crate::boxes::array::ArrayBox>()
|
||||
{
|
||||
let idx0: Box<dyn crate::box_trait::NyashBox> =
|
||||
Box::new(crate::box_trait::IntegerBox::new(0));
|
||||
obj_s = Some(ab.get(idx0).to_string_box().value);
|
||||
let idx1: Box<dyn crate::box_trait::NyashBox> = Box::new(crate::box_trait::IntegerBox::new(1));
|
||||
let idx1: Box<dyn crate::box_trait::NyashBox> =
|
||||
Box::new(crate::box_trait::IntegerBox::new(1));
|
||||
let s1 = ab.get(idx1).to_string_box().value;
|
||||
if !s1.is_empty() { exe_s = Some(s1); }
|
||||
if !s1.is_empty() {
|
||||
exe_s = Some(s1);
|
||||
}
|
||||
} else {
|
||||
obj_s = Some(b.to_string_box().value);
|
||||
}
|
||||
@ -426,18 +593,37 @@ impl MirInterpreter {
|
||||
}
|
||||
let objs = match obj_s {
|
||||
Some(s) => s,
|
||||
None => return Some(Err(self.err_invalid("extern_invoke env.codegen.link_object expects args"))),
|
||||
None => {
|
||||
return Some(Err(self.err_invalid(
|
||||
"extern_invoke env.codegen.link_object expects args",
|
||||
)))
|
||||
}
|
||||
};
|
||||
if std::env::var("NYASH_LLVM_USE_CAPI").ok().as_deref() != Some("1") ||
|
||||
std::env::var("HAKO_V1_EXTERN_PROVIDER_C_ABI").ok().as_deref() != Some("1") {
|
||||
return Some(Err(ErrorBuilder::invalid_instruction("env.codegen.link_object: C-API route disabled")));
|
||||
if std::env::var("NYASH_LLVM_USE_CAPI").ok().as_deref() != Some("1")
|
||||
|| std::env::var("HAKO_V1_EXTERN_PROVIDER_C_ABI")
|
||||
.ok()
|
||||
.as_deref()
|
||||
!= Some("1")
|
||||
{
|
||||
return Some(Err(ErrorBuilder::invalid_instruction(
|
||||
"env.codegen.link_object: C-API route disabled",
|
||||
)));
|
||||
}
|
||||
let extra = std::env::var("HAKO_AOT_LDFLAGS").ok();
|
||||
let obj = std::path::PathBuf::from(objs);
|
||||
let exe = exe_s.map(std::path::PathBuf::from).unwrap_or_else(|| std::env::temp_dir().join("hako_link_out.exe"));
|
||||
match crate::host_providers::llvm_codegen::link_object_capi(&obj, &exe, extra.as_deref()) {
|
||||
let exe = exe_s
|
||||
.map(std::path::PathBuf::from)
|
||||
.unwrap_or_else(|| std::env::temp_dir().join("hako_link_out.exe"));
|
||||
match crate::host_providers::llvm_codegen::link_object_capi(
|
||||
&obj,
|
||||
&exe,
|
||||
extra.as_deref(),
|
||||
) {
|
||||
Ok(()) => Ok(VMValue::String(exe.to_string_lossy().into_owned())),
|
||||
Err(e) => Err(ErrorBuilder::with_context("env.codegen.link_object", &e.to_string()))
|
||||
Err(e) => Err(ErrorBuilder::with_context(
|
||||
"env.codegen.link_object",
|
||||
&e.to_string(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
("env.box_introspect", "kind") => {
|
||||
@ -487,9 +673,7 @@ impl MirInterpreter {
|
||||
}
|
||||
}
|
||||
other => {
|
||||
if std::env::var("NYASH_BOX_INTROSPECT_TRACE")
|
||||
.ok()
|
||||
.as_deref()
|
||||
if std::env::var("NYASH_BOX_INTROSPECT_TRACE").ok().as_deref()
|
||||
== Some("1")
|
||||
{
|
||||
eprintln!(
|
||||
@ -509,16 +693,15 @@ impl MirInterpreter {
|
||||
#[cfg(all(feature = "plugins", not(target_arch = "wasm32")))]
|
||||
let result = plugin_loader_v2::handle_box_introspect("kind", &collected);
|
||||
#[cfg(any(not(feature = "plugins"), target_arch = "wasm32"))]
|
||||
let result: crate::bid::BidResult<Option<Box<dyn NyashBox>>> =
|
||||
Err(crate::bid::BidError::PluginError);
|
||||
let result: crate::bid::BidResult<
|
||||
Option<Box<dyn NyashBox>>,
|
||||
> = Err(crate::bid::BidError::PluginError);
|
||||
|
||||
match result {
|
||||
Ok(Some(b)) => Ok(VMValue::BoxRef(Arc::from(b))),
|
||||
Ok(None) => Ok(VMValue::Void),
|
||||
Err(e) => Err(self.err_with_context(
|
||||
"env.box_introspect.kind",
|
||||
&format!("{:?}", e),
|
||||
)),
|
||||
Err(e) => Err(self
|
||||
.err_with_context("env.box_introspect.kind", &format!("{:?}", e))),
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
@ -529,7 +712,7 @@ impl MirInterpreter {
|
||||
"hostbridge.extern_invoke unsupported for {}.{} [provider] (check extern_provider_dispatch / env.* handlers)",
|
||||
name, method
|
||||
)))
|
||||
},
|
||||
}
|
||||
};
|
||||
Some(out)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user