44 lines
966 B
Rust
44 lines
966 B
Rust
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
|
|
}
|
|
|
|
let mut si = lwvm::Interner::default ();
|
|
|
|
let bc = lwvm::compile_bytecode (src.to_vec ()).unwrap ();
|
|
let chunk = lwvm::parse_chunk (&bc, &mut si).unwrap ();
|
|
|
|
let host_lib = [
|
|
("add", Value::RsFunc (host_add)),
|
|
].into_iter ().map (|(k, v)| (si.intern (k), v));
|
|
|
|
let env = [
|
|
("host_lib", Value::from_iter (host_lib.into_iter ())),
|
|
].into_iter ().map (|(k, v)| (si.intern (k), v));
|
|
|
|
let upvalues = vec! [
|
|
Value::from_iter (env.into_iter ()),
|
|
];
|
|
|
|
let mut vm = State::new (chunk, upvalues);
|
|
let output = vm.execute ().unwrap ();
|
|
|
|
assert_eq! (output, vec! [Value::from (2019)]);
|
|
}
|