@@ -0,0 +1 @@
|
||||
target/
|
||||
Generated
+1935
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "gyehoek-wasm-runtime"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
clap = { version = "4.6.1", features = ["derive"] }
|
||||
clio = { version = "0.3.5", features = ["clap-parse"] }
|
||||
memoize = "0.6.0"
|
||||
wasmtime = "46.0.1"
|
||||
@@ -0,0 +1,13 @@
|
||||
{ rustPlatform
|
||||
, lib
|
||||
, crane-lib
|
||||
}:
|
||||
|
||||
crane-lib.buildPackage (lib.fix (finalAttrs: {
|
||||
pname = "gyehoek-wasm-runtime";
|
||||
version = "0.1.0";
|
||||
src = ./.;
|
||||
# cargoLock = ./Cargo.lock;
|
||||
doCheck = true;
|
||||
meta.mainProgram = "gyehoek-wasm-runtime";
|
||||
}))
|
||||
@@ -0,0 +1,38 @@
|
||||
use wasmtime::*;
|
||||
use crate::internal as scm;
|
||||
use crate::internal::{Scm,Immediate,HeapObject};
|
||||
|
||||
// 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 ().unwrap () {
|
||||
Scm::Immediate (x) => write_immediate (caller, x),
|
||||
Scm::HeapObject (x) => write_heap_object (caller, x),
|
||||
}
|
||||
}
|
||||
|
||||
fn write_heap_object (_caller : Caller<'_, u32>, x : HeapObject) {
|
||||
match x {
|
||||
HeapObject::Procedure => print! ("#<procedure>"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn truthy_p (caller : Caller<'_, u32>, x : Rooted<EqRef>,) -> u32 {
|
||||
let r = scm::interpret (&caller, x).unwrap ().unwrap ();
|
||||
if let Scm::Immediate (Immediate::Bool (false)) = r {
|
||||
0
|
||||
} else {
|
||||
1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
use wasmtime::*;
|
||||
use crate::types;
|
||||
|
||||
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 HeapObject {
|
||||
Procedure
|
||||
}
|
||||
|
||||
pub enum Scm {
|
||||
Immediate (Immediate),
|
||||
HeapObject (HeapObject),
|
||||
}
|
||||
|
||||
#[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_heap_object (
|
||||
store : impl AsContext,
|
||||
x : Rooted<EqRef>
|
||||
) -> Result<Option<HeapObject>> {
|
||||
if x.matches_ty (&store, &types::closure (&store)?)? {
|
||||
Ok (Some (HeapObject::Procedure))
|
||||
} else {
|
||||
todo! ()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn interpret (
|
||||
store : impl AsContext,
|
||||
x : Rooted<EqRef>
|
||||
) -> Result<Option<Scm>> {
|
||||
if let Some (imm) = x.as_i31 (&store)? {
|
||||
Ok (
|
||||
interpret_immediate (imm.get_u32 ())
|
||||
.map (Scm::Immediate)
|
||||
)
|
||||
} else {
|
||||
Ok (
|
||||
interpret_heap_object (&store, x)?
|
||||
.map (Scm::HeapObject)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
Scm::HeapObject (ho) => {
|
||||
todo! ()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
mod gyehoek;
|
||||
mod internal;
|
||||
mod types;
|
||||
|
||||
use std::io;
|
||||
use std::io::Read;
|
||||
use clio::*;
|
||||
use clap::Parser;
|
||||
use wasmtime::*;
|
||||
|
||||
/// A Wasm runtime for Gyehoek scheme.
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(name = "gyehoek", version, about, long_about = None)]
|
||||
struct Args {
|
||||
/// Path to Wasm binary or textual source
|
||||
#[clap(value_parser)]
|
||||
wasm: Input,
|
||||
}
|
||||
|
||||
fn read<R : Read> (mut rdr : R) -> io::Result<Vec<u8>> {
|
||||
let mut buf = vec! [];
|
||||
rdr.read_to_end (&mut buf)?;
|
||||
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
|
||||
}
|
||||
|
||||
fn link_primitives (linker : &mut Linker<u32>) -> wasmtime::Result<()> {
|
||||
linker.func_wrap ("gyehoek", "write", gyehoek::write)?;
|
||||
linker.func_wrap ("gyehoek", "truthy?", 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);
|
||||
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")?;
|
||||
main.call (&mut store, ())?;
|
||||
Ok (())
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
use wasmtime::*;
|
||||
use memoize::memoize;
|
||||
|
||||
pub fn heap_object_struct (store : impl AsContext) -> Result<StructType> {
|
||||
let ctx = store.as_context ();
|
||||
let engine = ctx.engine ();
|
||||
Ok (
|
||||
StructType::with_finality_and_supertype (
|
||||
engine,
|
||||
Finality::NonFinal,
|
||||
None,
|
||||
vec![
|
||||
hash_field ()
|
||||
]
|
||||
)?
|
||||
)
|
||||
}
|
||||
|
||||
pub fn heap_object (_store : impl AsContext) -> Result<HeapType> {
|
||||
todo! ()
|
||||
}
|
||||
|
||||
#[memoize]
|
||||
pub fn hash_field () -> FieldType {
|
||||
FieldType::new (
|
||||
Mutability::Var,
|
||||
StorageType::ValType (ValType::I32)
|
||||
)
|
||||
}
|
||||
|
||||
pub fn closure (store : impl AsContext) -> Result<HeapType> {
|
||||
let ctx = store.as_context ();
|
||||
let engine = ctx.engine ();
|
||||
Ok (
|
||||
HeapType::ConcreteStruct (
|
||||
StructType::with_finality_and_supertype (
|
||||
engine,
|
||||
Finality::NonFinal,
|
||||
Some (&heap_object_struct (&store)?),
|
||||
vec![
|
||||
hash_field (),
|
||||
FieldType::new (
|
||||
Mutability::Const,
|
||||
StorageType::ValType (ValType::Ref (
|
||||
RefType::new (
|
||||
false,
|
||||
HeapType::ConcreteFunc (
|
||||
FuncType::new (
|
||||
engine,
|
||||
vec![ValType::I32],
|
||||
vec![],
|
||||
)
|
||||
)
|
||||
)
|
||||
))
|
||||
),
|
||||
]
|
||||
)?
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user