@@ -20,3 +20,12 @@ pub fn write (caller : Caller<'_, u32>, x : Rooted<EqRef>) {
|
||||
Scm::Immediate (x) => write_immediate (caller, x)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn truthy_p (caller : Caller<'_, u32>, x : Rooted<EqRef>,) -> u32 {
|
||||
let r = scm::interpret (&caller, x).unwrap ();
|
||||
if let Scm::Immediate (Immediate::Bool (false)) = r {
|
||||
0
|
||||
} else {
|
||||
1
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,3 +40,31 @@ pub fn interpret (store : impl AsContext, x : Rooted<EqRef>) -> Option<Scm> {
|
||||
todo! ()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn encode_immediate (
|
||||
store : impl AsContext,
|
||||
imm : Immediate
|
||||
) -> scm_bits {
|
||||
use Immediate::*;
|
||||
match imm {
|
||||
SmallFixnum (n) => (n << 1).try_into ().unwrap (),
|
||||
Bool (false) => scm_false,
|
||||
Bool (true) => scm_true,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn encode (store : impl AsContextMut, x : Scm) -> Rooted<EqRef> {
|
||||
match x {
|
||||
Scm::Immediate (imm) => {
|
||||
let i31 = I31::new_u32 (encode_immediate (&store, imm))
|
||||
.unwrap ();
|
||||
EqRef::from_i31 (store, i31)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn encode_bool (store : impl AsContextMut, b : bool) -> Rooted<EqRef> {
|
||||
let x = if b { scm_true } else { scm_false };
|
||||
let i31 = I31::new_u32 (x).unwrap ();
|
||||
EqRef::from_i31 (store, i31)
|
||||
}
|
||||
|
||||
+7
-1
@@ -31,13 +31,19 @@ fn get_config () -> Config {
|
||||
cfg
|
||||
}
|
||||
|
||||
fn link_primitives (linker : &mut Linker<u32>) -> wasmtime::Result<()> {
|
||||
linker.func_wrap ("gyehoek", "write", gyehoek::write)?;
|
||||
linker.func_wrap ("gyehoek", "to-bool", gyehoek::truthy_p)?;
|
||||
Ok (())
|
||||
}
|
||||
|
||||
pub fn main () -> wasmtime::Result<()> {
|
||||
let args = Args::parse ();
|
||||
let wasm_config = get_config ();
|
||||
let engine = Engine::new (&wasm_config)?;
|
||||
let module = Module::new (&engine, read (args.wasm)?)?;
|
||||
let mut linker = Linker::new (&engine);
|
||||
linker.func_wrap ("gyehoek", "write", gyehoek::write)?;
|
||||
link_primitives (&mut linker)?;
|
||||
let mut store : Store<u32> = Store::new (&engine, 4);
|
||||
let instance = linker.instantiate (&mut store, &module)?;
|
||||
let main = instance.get_typed_func::<(),()> (&mut store, "main")?;
|
||||
|
||||
Reference in New Issue
Block a user