2025-09-10 20:56:14 +09:00
|
|
|
#![cfg(all(feature = "plugins", not(target_arch = "wasm32")))]
|
|
|
|
|
use nyash_rust::runtime::box_registry::get_global_registry;
|
2025-09-16 23:49:36 +09:00
|
|
|
use nyash_rust::runtime::plugin_loader_v2::{get_global_loader_v2, init_global_loader_v2};
|
2025-09-10 20:56:14 +09:00
|
|
|
use nyash_rust::runtime::PluginConfig;
|
2025-09-16 23:49:36 +09:00
|
|
|
use nyash_rust::{
|
|
|
|
|
mir::{instruction::MirInstruction, passes::method_id_inject::inject_method_ids, MirCompiler},
|
|
|
|
|
parser::NyashParser,
|
|
|
|
|
};
|
2025-09-10 20:56:14 +09:00
|
|
|
|
|
|
|
|
fn try_init_plugins() -> bool {
|
2025-09-16 23:49:36 +09:00
|
|
|
if !std::path::Path::new("nyash.toml").exists() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if let Err(e) = init_global_loader_v2("nyash.toml") {
|
|
|
|
|
eprintln!("init failed: {:?}", e);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-09-10 20:56:14 +09:00
|
|
|
let loader = get_global_loader_v2();
|
|
|
|
|
let loader = loader.read().unwrap();
|
|
|
|
|
if let Some(conf) = &loader.config {
|
|
|
|
|
let mut map = std::collections::HashMap::new();
|
2025-09-16 23:49:36 +09:00
|
|
|
for (lib, def) in &conf.libraries {
|
|
|
|
|
for b in &def.boxes {
|
|
|
|
|
map.insert(b.clone(), lib.clone());
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-10 20:56:14 +09:00
|
|
|
get_global_registry().apply_plugin_config(&PluginConfig { plugins: map });
|
|
|
|
|
true
|
2025-09-16 23:49:36 +09:00
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
2025-09-10 20:56:14 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn injects_method_id_for_filebox_open() {
|
2025-09-16 23:49:36 +09:00
|
|
|
if !try_init_plugins() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-09-10 20:56:14 +09:00
|
|
|
let code = r#"
|
|
|
|
|
local f
|
|
|
|
|
f = new FileBox()
|
|
|
|
|
f.open("/tmp/test.txt", "r")
|
|
|
|
|
"#;
|
|
|
|
|
let ast = NyashParser::parse_from_string(code).expect("parse failed");
|
|
|
|
|
let mut compiler = MirCompiler::new();
|
|
|
|
|
let module = compiler.compile(ast).expect("mir compile failed").module;
|
|
|
|
|
let mut module2 = module.clone();
|
|
|
|
|
let injected = inject_method_ids(&mut module2);
|
|
|
|
|
assert!(injected > 0);
|
|
|
|
|
for func in module2.functions.values() {
|
|
|
|
|
for block in func.blocks.values() {
|
|
|
|
|
for inst in &block.instructions {
|
2025-09-16 23:49:36 +09:00
|
|
|
if let MirInstruction::BoxCall {
|
|
|
|
|
method, method_id, ..
|
|
|
|
|
} = inst
|
|
|
|
|
{
|
|
|
|
|
if method == "open" {
|
|
|
|
|
assert!(method_id.is_some(), "open missing method_id");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-09-10 20:56:14 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
panic!("FileBox.open not found");
|
2025-09-16 23:49:36 +09:00
|
|
|
}
|