2023-10-02 00:50:50 +00:00
|
|
|
use lunar_wave_vm as lwvm;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn embedding () {
|
|
|
|
use lwvm::{
|
|
|
|
State,
|
|
|
|
Value,
|
|
|
|
};
|
|
|
|
|
|
|
|
let src = br#"
|
|
|
|
return host_lib.add (14, 12)
|
|
|
|
"#;
|
|
|
|
|
|
|
|
fn host_add (l: &mut State, num_args: usize) -> usize {
|
|
|
|
assert_eq! (num_args, 2);
|
|
|
|
let a = l.reg (0).as_int ().unwrap ();
|
|
|
|
let b = l.reg (1).as_int ().unwrap ();
|
|
|
|
*l.reg_mut (0) = Value::from (a + b + 1993);
|
|
|
|
1
|
|
|
|
}
|
2023-10-04 22:29:51 +00:00
|
|
|
/*
|
2023-10-02 21:14:34 +00:00
|
|
|
let mut si = lwvm::Interner::default ();
|
|
|
|
|
2023-10-02 03:50:06 +00:00
|
|
|
let bc = lwvm::compile_bytecode (src.to_vec ()).unwrap ();
|
2023-10-02 21:14:34 +00:00
|
|
|
let chunk = lwvm::parse_chunk (&bc, &mut si).unwrap ();
|
2023-10-02 00:50:50 +00:00
|
|
|
|
|
|
|
let host_lib = [
|
|
|
|
("add", Value::RsFunc (host_add)),
|
2023-10-02 21:14:34 +00:00
|
|
|
].into_iter ().map (|(k, v)| (si.intern (k), v));
|
2023-10-02 00:50:50 +00:00
|
|
|
|
|
|
|
let env = [
|
|
|
|
("host_lib", Value::from_iter (host_lib.into_iter ())),
|
2023-10-02 21:14:34 +00:00
|
|
|
].into_iter ().map (|(k, v)| (si.intern (k), v));
|
2023-10-02 00:50:50 +00:00
|
|
|
|
|
|
|
let upvalues = vec! [
|
|
|
|
Value::from_iter (env.into_iter ()),
|
|
|
|
];
|
|
|
|
|
2023-10-02 01:31:30 +00:00
|
|
|
let mut vm = State::new (chunk, upvalues);
|
2023-10-02 03:50:06 +00:00
|
|
|
let output = vm.execute ().unwrap ();
|
2023-10-02 00:50:50 +00:00
|
|
|
|
|
|
|
assert_eq! (output, vec! [Value::from (2019)]);
|
2023-10-04 22:29:51 +00:00
|
|
|
*/
|
2023-10-02 00:50:50 +00:00
|
|
|
}
|