2025-09-03 20:03:45 +09:00
|
|
|
#![cfg(feature = "e2e")]
|
2025-08-21 15:55:00 +09:00
|
|
|
//! E2E: Interop between builtin and mock plugin boxes via MapBox storage
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
|
|
use nyash_rust::box_factory::builtin::BuiltinGroups;
|
2025-09-16 23:49:36 +09:00
|
|
|
use nyash_rust::box_factory::BoxFactory;
|
|
|
|
|
use nyash_rust::box_trait::{BoolBox, BoxBase, BoxCore, NyashBox, StringBox};
|
2025-08-21 15:55:00 +09:00
|
|
|
use nyash_rust::interpreter::{NyashInterpreter, RuntimeError};
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2025-09-16 23:49:36 +09:00
|
|
|
struct EchoBox {
|
|
|
|
|
base: BoxBase,
|
|
|
|
|
msg: String,
|
|
|
|
|
}
|
|
|
|
|
impl EchoBox {
|
|
|
|
|
fn new(msg: String) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
base: BoxBase::new(),
|
|
|
|
|
msg,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-21 15:55:00 +09:00
|
|
|
|
|
|
|
|
impl BoxCore for EchoBox {
|
2025-09-16 23:49:36 +09:00
|
|
|
fn box_id(&self) -> u64 {
|
|
|
|
|
self.base.id
|
|
|
|
|
}
|
|
|
|
|
fn parent_type_id(&self) -> Option<std::any::TypeId> {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
write!(f, "EchoBox(\"{}\")", self.msg)
|
|
|
|
|
}
|
|
|
|
|
fn as_any(&self) -> &dyn std::any::Any {
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
|
|
|
|
|
self
|
|
|
|
|
}
|
2025-08-21 15:55:00 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl NyashBox for EchoBox {
|
2025-09-16 23:49:36 +09:00
|
|
|
fn to_string_box(&self) -> StringBox {
|
|
|
|
|
StringBox::new(self.msg.clone())
|
|
|
|
|
}
|
2025-08-21 15:55:00 +09:00
|
|
|
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
2025-09-16 23:49:36 +09:00
|
|
|
if let Some(e) = other.as_any().downcast_ref::<EchoBox>() {
|
|
|
|
|
BoolBox::new(self.msg == e.msg)
|
|
|
|
|
} else {
|
|
|
|
|
BoolBox::new(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fn type_name(&self) -> &'static str {
|
|
|
|
|
"EchoBox"
|
|
|
|
|
}
|
|
|
|
|
fn clone_box(&self) -> Box<dyn NyashBox> {
|
|
|
|
|
Box::new(self.clone())
|
|
|
|
|
}
|
|
|
|
|
fn share_box(&self) -> Box<dyn NyashBox> {
|
|
|
|
|
Box::new(self.clone())
|
2025-08-21 15:55:00 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct TestPluginFactory;
|
2025-09-16 23:49:36 +09:00
|
|
|
impl TestPluginFactory {
|
|
|
|
|
fn new() -> Self {
|
|
|
|
|
Self
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-21 15:55:00 +09:00
|
|
|
impl BoxFactory for TestPluginFactory {
|
2025-09-16 23:49:36 +09:00
|
|
|
fn create_box(
|
|
|
|
|
&self,
|
|
|
|
|
name: &str,
|
|
|
|
|
args: &[Box<dyn NyashBox>],
|
|
|
|
|
) -> Result<Box<dyn NyashBox>, RuntimeError> {
|
2025-08-21 15:55:00 +09:00
|
|
|
match name {
|
|
|
|
|
"EchoBox" => {
|
2025-09-16 23:49:36 +09:00
|
|
|
let msg = args
|
|
|
|
|
.get(0)
|
|
|
|
|
.map(|a| a.to_string_box().value)
|
|
|
|
|
.unwrap_or_else(|| "".to_string());
|
2025-08-21 15:55:00 +09:00
|
|
|
Ok(Box::new(EchoBox::new(msg)))
|
|
|
|
|
}
|
2025-09-16 23:49:36 +09:00
|
|
|
_ => Err(RuntimeError::InvalidOperation {
|
|
|
|
|
message: format!("Unknown Box type: {}", name),
|
|
|
|
|
}),
|
2025-08-21 15:55:00 +09:00
|
|
|
}
|
|
|
|
|
}
|
2025-09-16 23:49:36 +09:00
|
|
|
fn box_types(&self) -> Vec<&str> {
|
|
|
|
|
vec!["EchoBox"]
|
|
|
|
|
}
|
2025-08-21 15:55:00 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn new_interpreter_with_factory() -> NyashInterpreter {
|
|
|
|
|
let mut i = NyashInterpreter::new_with_groups(BuiltinGroups::native_full());
|
|
|
|
|
i.register_box_factory(Arc::new(TestPluginFactory::new()));
|
|
|
|
|
i
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn e2e_interop_mapbox_store_plugin_box() {
|
|
|
|
|
let mut i = new_interpreter_with_factory();
|
|
|
|
|
let code = r#"
|
|
|
|
|
m = new MapBox()
|
|
|
|
|
e = new EchoBox("ok")
|
|
|
|
|
m.set("k", e)
|
|
|
|
|
v = m.get("k")
|
|
|
|
|
v
|
|
|
|
|
"#;
|
|
|
|
|
let ast = nyash_rust::parser::NyashParser::parse_from_string(code).expect("parse ok");
|
|
|
|
|
let result = i.execute(ast).expect("exec ok");
|
|
|
|
|
assert_eq!(result.to_string_box().value, "ok");
|
|
|
|
|
}
|