Files
hakorune/src/tests/nyash_abi_basic.rs

148 lines
5.0 KiB
Rust
Raw Normal View History

#[cfg(test)]
mod tests {
use crate::runtime::type_registry::{known_methods_for, resolve_slot_by_name};
#[test]
fn type_registry_resolves_core_slots() {
// MapBox
assert_eq!(resolve_slot_by_name("MapBox", "size", 0), Some(200));
assert_eq!(resolve_slot_by_name("MapBox", "len", 0), Some(201));
assert_eq!(resolve_slot_by_name("MapBox", "has", 1), Some(202));
assert_eq!(resolve_slot_by_name("MapBox", "get", 1), Some(203));
assert_eq!(resolve_slot_by_name("MapBox", "set", 2), Some(204));
// ArrayBox
assert_eq!(resolve_slot_by_name("ArrayBox", "get", 1), Some(100));
assert_eq!(resolve_slot_by_name("ArrayBox", "set", 2), Some(101));
assert_eq!(resolve_slot_by_name("ArrayBox", "len", 0), Some(102));
// StringBox
assert_eq!(resolve_slot_by_name("StringBox", "len", 0), Some(300));
// Known methods listing should include representative entries
let mm = known_methods_for("MapBox").expect("map methods");
assert!(mm.contains(&"size"));
assert!(mm.contains(&"get"));
assert!(mm.contains(&"set"));
}
#[test]
#[ignore]
fn vm_vtable_map_set_get_has() {
feat(mir/builder): implement BoxCompilationContext for structural metadata isolation 箱理論の完璧な実装!各static boxコンパイルを独立したコンテキストで実行。 設計: - BoxCompilationContext: variable_map, value_origin_newbox, value_types を箱化 - MirBuilder: compilation_context: Option<BoxCompilationContext> フィールド追加 - context swap: lower_static_method_as_function 開始/終了時に std::mem::swap - 自動クリーンアップ: スコープ終了でコンテキスト破棄 実装: 1. src/mir/builder/context.rs: BoxCompilationContext構造体定義(テスト付き) 2. src/mir/builder.rs: compilation_contextフィールド追加、既存フィールドにコメント追加 3. src/mir/builder/lifecycle.rs: 各static boxでコンテキスト作成・破棄 4. src/mir/builder/builder_calls.rs: lower_static_method_as_functionでcontext swap 5. src/mir/builder/decls.rs, exprs.rs: 古いmanual clear()削除 効果: ✅ グローバル状態汚染を構造的に不可能化 ✅ 各static boxが完全に独立したコンテキストでコンパイル ✅ 既存コード変更なし(swap技法で完全後方互換性) ✅ StageBArgsBox ValueId(21)エラー完全解決 箱理論的評価: 🟢 95点 - 明示的な境界: 各boxのコンテキストが物理的に分離 - 汚染不可能: 前の箱の状態が構造的に残らない - 戻せる: コンテキスト差し替えで簡単ロールバック - 美しい設計: スコープベースのリソース管理 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-17 11:28:18 +09:00
use crate::backend::VM;
use crate::mir::{
BasicBlockId, ConstValue, EffectMask, FunctionSignature, MirFunction, MirInstruction,
MirModule, MirType,
};
// Enable vtable-preferred path
std::env::set_var("NYASH_ABI_VTABLE", "1");
// Program: m = new MapBox(); m.set("k","v"); h = m.has("k"); g = m.get("k"); return g
let mut m = MirModule::new("nyash_abi_map_get".into());
let sig = FunctionSignature {
name: "main".into(),
params: vec![],
return_type: MirType::String,
effects: EffectMask::PURE,
};
let mut f = MirFunction::new(sig, BasicBlockId::new(0));
let bb = f.entry_block;
let mapv = f.next_value_id();
f.get_block_mut(bb)
.unwrap()
.add_instruction(MirInstruction::NewBox {
dst: mapv,
box_type: "MapBox".into(),
args: vec![],
});
let k = f.next_value_id();
f.get_block_mut(bb)
.unwrap()
.add_instruction(MirInstruction::Const {
dst: k,
value: ConstValue::String("k".into()),
});
let v = f.next_value_id();
f.get_block_mut(bb)
.unwrap()
.add_instruction(MirInstruction::Const {
dst: v,
value: ConstValue::String("v".into()),
});
f.get_block_mut(bb)
.unwrap()
.add_instruction(MirInstruction::BoxCall {
dst: None,
box_val: mapv,
method: "set".into(),
args: vec![k, v],
method_id: None,
effects: EffectMask::PURE,
});
let k2 = f.next_value_id();
f.get_block_mut(bb)
.unwrap()
.add_instruction(MirInstruction::Const {
dst: k2,
value: ConstValue::String("k".into()),
});
let hasv = f.next_value_id();
f.get_block_mut(bb)
.unwrap()
.add_instruction(MirInstruction::BoxCall {
dst: Some(hasv),
box_val: mapv,
method: "has".into(),
args: vec![k2],
method_id: None,
effects: EffectMask::PURE,
});
let k3 = f.next_value_id();
f.get_block_mut(bb)
.unwrap()
.add_instruction(MirInstruction::Const {
dst: k3,
value: ConstValue::String("k".into()),
});
let got = f.next_value_id();
f.get_block_mut(bb)
.unwrap()
.add_instruction(MirInstruction::BoxCall {
dst: Some(got),
box_val: mapv,
method: "get".into(),
args: vec![k3],
method_id: None,
effects: EffectMask::PURE,
});
f.get_block_mut(bb)
.unwrap()
.add_instruction(MirInstruction::Return { value: Some(got) });
m.add_function(f);
let mut vm = VM::new();
let out = vm.execute_module(&m).expect("vm exec");
assert_eq!(out.to_string_box().value, "v");
}
#[test]
fn mapbox_keys_values_return_arrays() {
// Direct Box-level test (not via VM): keys()/values() should return ArrayBox
use crate::box_trait::{IntegerBox, StringBox};
use crate::boxes::map_box::MapBox;
let map = MapBox::new();
map.set(Box::new(StringBox::new("a")), Box::new(IntegerBox::new(1)));
map.set(Box::new(StringBox::new("b")), Box::new(IntegerBox::new(2)));
let keys = map.keys();
let values = map.values();
assert_eq!(keys.type_name(), "ArrayBox");
assert_eq!(values.type_name(), "ArrayBox");
}
}