fix(bridge): implement env.box_introspect.kind lowering + Stage0 build fixes

Phase 25.1b type system groundwork - env.* namespace support in Bridge layer

Changes:
- Bridge layer (JSON v0 → MIR):
  - Add 'env' as well-known variable in MapVars::resolve()
  - Implement env.box_introspect.kind(value) → ExternCall lowering
  - Pattern: Method { recv: Method { recv: Var("env"), method: "box_introspect" }, method: "kind" }

- VM/extern fixes:
  - Add Arc::from() conversion for env.box_introspect.kind result
  - Fix MapBox API usage in extern_functions.rs logging

- Build fixes:
  - Comment out missing llvm_legacy/llvm modules in src/backend/mod.rs
  - Comment out missing gui_visual_node_prototype in Cargo.toml

- New files:
  - lang/src/shared/common/box_type_inspector_box.hako (type introspection API)

Context:
- Enables BoxTypeInspectorBox to query runtime Box types via env.box_introspect.kind
- Required for selfhost MirBuilder type-aware lowering (multi-carrier loops, etc.)
- Part of Phase 25.1b "no fallback" selfhosting strategy

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-11-16 17:19:56 +09:00
parent 5f06d82ee5
commit fbf4687ea1
6 changed files with 391 additions and 34 deletions

View File

@ -1,6 +1,7 @@
use super::*;
use crate::backend::mir_interpreter::utils::error_helpers::ErrorBuilder;
use serde_json::Value as JsonValue;
use std::sync::Arc;
impl MirInterpreter {
#[inline]
@ -212,7 +213,44 @@ impl MirInterpreter {
let val = std::env::var(&key).ok();
Some(Ok(match val { Some(s) => VMValue::String(s), None => VMValue::Void }))
}
// Legacy global-call form: hostbridge.extern_invoke(name, method, args?)
// Direct env.box_introspect.kind extern (ExternCall form)
"env.box_introspect.kind" => {
use crate::box_trait::{NyashBox, StringBox};
use crate::runtime::plugin_loader_v2;
let mut collected: Vec<Box<dyn NyashBox>> = Vec::new();
if let Some(arg_reg) = args.get(0) {
let v = match self.reg_load(*arg_reg) {
Ok(v) => v,
Err(e) => return Some(Err(e)),
};
match v {
VMValue::BoxRef(b) => collected.push(b.clone_box()),
other => {
collected.push(Box::new(StringBox::new(&other.to_string())));
}
}
} else {
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);
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),
))),
}
}
"hostbridge.extern_invoke" => {
if std::env::var("HAKO_CABI_TRACE").ok().as_deref() == Some("1") {
eprintln!("[hb:entry:provider] hostbridge.extern_invoke");
@ -386,12 +424,93 @@ impl MirInterpreter {
Err(e) => Err(ErrorBuilder::with_context("env.codegen.link_object", &e.to_string()))
}
}
("env.box_introspect", "kind") => {
// hostbridge.extern_invoke("env.box_introspect","kind",[value])
// args layout (regs): [name, method, array_box_or_value, ...]
// For BoxTypeInspectorBox we only care about the first element of the ArrayBox.
use crate::box_trait::{NyashBox, StringBox};
use crate::runtime::plugin_loader_v2;
let mut collected: Vec<Box<dyn NyashBox>> = Vec::new();
if let Some(arg_reg) = args.get(2) {
let v = match self.reg_load(*arg_reg) {
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>()
{
let idx0: Box<dyn NyashBox> =
Box::new(crate::box_trait::IntegerBox::new(0));
let elem0 = ab.get(idx0);
if std::env::var("NYASH_BOX_INTROSPECT_TRACE")
.ok()
.as_deref()
== Some("1")
{
eprintln!(
"[box_introspect:extern] using ArrayBox[0] value_type={}",
elem0.type_name()
);
}
collected.push(elem0);
} else {
if std::env::var("NYASH_BOX_INTROSPECT_TRACE")
.ok()
.as_deref()
== Some("1")
{
eprintln!(
"[box_introspect:extern] using BoxRef({}) directly",
b.type_name()
);
}
collected.push(b.clone_box());
}
}
other => {
if std::env::var("NYASH_BOX_INTROSPECT_TRACE")
.ok()
.as_deref()
== Some("1")
{
eprintln!(
"[box_introspect:extern] non-box arg kind={:?}",
other
);
}
collected.push(Box::new(StringBox::new(&other.to_string())));
}
}
} else {
return Some(Err(self.err_invalid(
"extern_invoke env.box_introspect.kind expects args array",
)));
}
#[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);
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),
)),
}
}
_ => {
if std::env::var("HAKO_CABI_TRACE").ok().as_deref() == Some("1") {
eprintln!("[hb:unsupported:provider] {}.{}", name, method);
}
Err(self.err_invalid(format!(
"hostbridge.extern_invoke unsupported for {}.{} [provider]",
"hostbridge.extern_invoke unsupported for {}.{} [provider] (check extern_provider_dispatch / env.* handlers)",
name, method
)))
},