42 lines
915 B
Rust
42 lines
915 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 bc = lwvm::compile_bytecode (src.to_vec ()).unwrap ();
|
|
let chunk = lwvm::parse_chunk (&bc).unwrap ();
|
|
|
|
let host_lib = [
|
|
("add", Value::RsFunc (host_add)),
|
|
].into_iter ().map (|(k, v)| (k.to_string (), v));
|
|
|
|
let env = [
|
|
("host_lib", Value::from_iter (host_lib.into_iter ())),
|
|
].into_iter ().map (|(k, v)| (k.to_string (), 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)]);
|
|
}
|