"fix stuff lol"
This commit is contained in:
+19
-2
@@ -1,5 +1,22 @@
|
||||
use wasmtime::*;
|
||||
use crate::internal as scm;
|
||||
use crate::internal::{Scm,Immediate};
|
||||
|
||||
pub fn say_hi (_caller : Caller<'_, u32>) {
|
||||
println! ("hiiii~")
|
||||
// pub fn small_fixnum_p (_)
|
||||
|
||||
// pub fn immediate_p (caller : Caller<'_, u32>, x : EqRef) -> EqRef {
|
||||
// x.is_i31 ()
|
||||
// }
|
||||
|
||||
fn write_immediate (_caller : Caller<'_, u32>, imm : Immediate) {
|
||||
match imm {
|
||||
Immediate::SmallFixnum (n) => print! ("{}", n),
|
||||
Immediate::Bool (b) => print! ("{}", if b { "#t" } else { "#f" }),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write (caller : Caller<'_, u32>, x : Rooted<EqRef>) {
|
||||
match scm::interpret (&caller, x).unwrap () {
|
||||
Scm::Immediate (x) => write_immediate (caller, x)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
use wasmtime::*;
|
||||
|
||||
pub fn immediate_p (store : impl AsContext, x : Rooted<EqRef>) -> bool {
|
||||
x.is_i31 (store).unwrap ()
|
||||
}
|
||||
|
||||
pub enum Immediate {
|
||||
SmallFixnum (i32),
|
||||
Bool (bool),
|
||||
}
|
||||
|
||||
pub enum Scm {
|
||||
Immediate (Immediate),
|
||||
}
|
||||
|
||||
#[allow(nonstandard_style)]
|
||||
pub type scm_bits = u32;
|
||||
|
||||
#[allow(nonstandard_style)]
|
||||
pub const scm_false : scm_bits = 0b01;
|
||||
#[allow(nonstandard_style)]
|
||||
pub const scm_true : scm_bits = 0b11;
|
||||
|
||||
pub fn interpret_immediate (x : scm_bits) -> Option<Immediate> {
|
||||
if x & 1 == 0 {
|
||||
Some (Immediate::SmallFixnum ((x >> 1).try_into ().unwrap ()))
|
||||
} else if x == scm_true {
|
||||
Some (Immediate::Bool (true))
|
||||
} else if x == scm_false {
|
||||
Some (Immediate::Bool (false))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn interpret (store : impl AsContext, x : Rooted<EqRef>) -> Option<Scm> {
|
||||
if let Some (imm) = x.as_i31 (store).unwrap () {
|
||||
Some (Scm::Immediate (interpret_immediate (imm.get_u32 ())?))
|
||||
} else {
|
||||
todo! ()
|
||||
}
|
||||
}
|
||||
+13
-2
@@ -1,4 +1,5 @@
|
||||
mod gyehoek;
|
||||
mod internal;
|
||||
|
||||
use std::io;
|
||||
use std::io::Read;
|
||||
@@ -21,12 +22,22 @@ fn read<R : Read> (mut rdr : R) -> io::Result<Vec<u8>> {
|
||||
Ok (buf)
|
||||
}
|
||||
|
||||
fn get_config () -> Config {
|
||||
let mut cfg = Config::new ();
|
||||
cfg.wasm_reference_types (true);
|
||||
cfg.wasm_function_references (true);
|
||||
cfg.wasm_tail_call (true);
|
||||
cfg.wasm_gc (true);
|
||||
cfg
|
||||
}
|
||||
|
||||
pub fn main () -> wasmtime::Result<()> {
|
||||
let args = Args::parse ();
|
||||
let engine = Engine::default ();
|
||||
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", "say-hi", gyehoek::say_hi)?;
|
||||
linker.func_wrap ("gyehoek", "write", gyehoek::write)?;
|
||||
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