2025-08-21 16:46:07 +09:00
|
|
|
#![cfg(all(feature = "plugins", not(target_arch = "wasm32")))]
|
|
|
|
|
|
|
|
|
|
use nyash_rust::parser::NyashParser;
|
|
|
|
|
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-08-21 16:46:07 +09:00
|
|
|
use nyash_rust::runtime::PluginConfig;
|
|
|
|
|
|
|
|
|
|
fn try_init_plugins() -> bool {
|
2025-09-16 23:49:36 +09:00
|
|
|
if !std::path::Path::new("nyash.toml").exists() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-08-21 16:46:07 +09:00
|
|
|
if let Err(e) = init_global_loader_v2("nyash.toml") {
|
|
|
|
|
eprintln!("[e2e] init_global_loader_v2 failed: {:?}", e);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// Map all configured boxes to plugin providers for legacy registry
|
|
|
|
|
let loader = get_global_loader_v2();
|
|
|
|
|
let loader = loader.read().unwrap();
|
|
|
|
|
if let Some(conf) = &loader.config {
|
|
|
|
|
let mut map = std::collections::HashMap::new();
|
|
|
|
|
for (lib_name, lib_def) in &conf.libraries {
|
2025-09-16 23:49:36 +09:00
|
|
|
for b in &lib_def.boxes {
|
|
|
|
|
map.insert(b.clone(), lib_name.clone());
|
|
|
|
|
}
|
2025-08-21 16:46:07 +09:00
|
|
|
}
|
|
|
|
|
let reg = get_global_registry();
|
|
|
|
|
reg.apply_plugin_config(&PluginConfig { plugins: map });
|
|
|
|
|
true
|
2025-09-16 23:49:36 +09:00
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
2025-08-21 16:46:07 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2025-09-04 20:16:13 +09:00
|
|
|
#[ignore = "MIR13/plugin loader semantics: counter handle parity pending"]
|
2025-08-21 16:46:07 +09:00
|
|
|
fn e2e_counter_basic_inc_get() {
|
2025-09-16 23:49:36 +09:00
|
|
|
if !try_init_plugins() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-08-21 16:46:07 +09:00
|
|
|
|
|
|
|
|
let code = r#"
|
|
|
|
|
local c, v1, v2
|
|
|
|
|
c = new CounterBox()
|
|
|
|
|
v1 = c.get()
|
|
|
|
|
c.inc()
|
|
|
|
|
v2 = c.get()
|
|
|
|
|
v2
|
|
|
|
|
"#;
|
|
|
|
|
let ast = NyashParser::parse_from_string(code).expect("parse failed");
|
|
|
|
|
let mut interpreter = nyash_rust::interpreter::NyashInterpreter::new();
|
|
|
|
|
|
|
|
|
|
match interpreter.execute(ast) {
|
|
|
|
|
Ok(result) => {
|
|
|
|
|
// After one inc(), count should be 1
|
|
|
|
|
assert_eq!(result.to_string_box().value, "1");
|
|
|
|
|
}
|
|
|
|
|
Err(e) => panic!("Counter basic test failed: {:?}", e),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2025-09-04 20:16:13 +09:00
|
|
|
#[ignore = "MIR13/plugin loader semantics: assignment sharing parity pending"]
|
2025-08-21 17:07:26 +09:00
|
|
|
fn e2e_counter_assignment_shares_handle() {
|
2025-09-16 23:49:36 +09:00
|
|
|
if !try_init_plugins() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-08-21 16:46:07 +09:00
|
|
|
|
|
|
|
|
let code = r#"
|
|
|
|
|
local c, x, v
|
|
|
|
|
c = new CounterBox()
|
|
|
|
|
x = c
|
|
|
|
|
x.inc()
|
|
|
|
|
v = c.get()
|
|
|
|
|
v
|
|
|
|
|
"#;
|
|
|
|
|
let ast = NyashParser::parse_from_string(code).expect("parse failed");
|
|
|
|
|
let mut interpreter = nyash_rust::interpreter::NyashInterpreter::new();
|
|
|
|
|
|
|
|
|
|
match interpreter.execute(ast) {
|
|
|
|
|
Ok(result) => {
|
2025-08-21 17:07:26 +09:00
|
|
|
// New semantics: plugin handle assign shares, so c reflects x.inc()
|
|
|
|
|
assert_eq!(result.to_string_box().value, "1");
|
2025-08-21 16:46:07 +09:00
|
|
|
}
|
|
|
|
|
Err(e) => panic!("Counter assignment test failed: {:?}", e),
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-21 21:35:17 +09:00
|
|
|
|
|
|
|
|
#[test]
|
2025-09-04 20:16:13 +09:00
|
|
|
#[ignore = "MIR13/plugin loader semantics: MapBox shared handle parity pending"]
|
2025-08-21 21:35:17 +09:00
|
|
|
fn e2e_counter_mapbox_shares_handle() {
|
2025-09-16 23:49:36 +09:00
|
|
|
if !try_init_plugins() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-08-21 21:35:17 +09:00
|
|
|
|
|
|
|
|
let code = r#"
|
|
|
|
|
local c, m, v
|
|
|
|
|
c = new CounterBox()
|
|
|
|
|
m = new MapBox()
|
|
|
|
|
m.set("k", c)
|
|
|
|
|
v = m.get("k")
|
|
|
|
|
v.inc()
|
|
|
|
|
// c should reflect the increment if handle is shared
|
|
|
|
|
v = c.get()
|
|
|
|
|
v
|
|
|
|
|
"#;
|
|
|
|
|
let ast = NyashParser::parse_from_string(code).expect("parse failed");
|
|
|
|
|
let mut interpreter = nyash_rust::interpreter::NyashInterpreter::new();
|
|
|
|
|
|
|
|
|
|
match interpreter.execute(ast) {
|
|
|
|
|
Ok(result) => {
|
|
|
|
|
assert_eq!(result.to_string_box().value, "1");
|
|
|
|
|
}
|
|
|
|
|
Err(e) => panic!("Counter MapBox share test failed: {:?}", e),
|
|
|
|
|
}
|
|
|
|
|
}
|