Skip to content

Commit

Permalink
run function in WASM package (#1894)
Browse files Browse the repository at this point in the history
  • Loading branch information
raviqqe authored Dec 27, 2024
1 parent b6096f2 commit 384eab6
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ stak-compiler = { version = "0.1.89", path = "../compiler" }
stak-configuration = { version = "0.1.81", path = "../configuration" }
stak-device = { version = "0.2.85", path = "../device", features = ["std"] }
stak-file = { version = "0.4.43", path = "../file" }
stak-macro = { version = "0.2.4", path = "../macro" }
stak-module = { version = "0.1.5", path = "../module" }
stak-process-context = { version = "0.2.43", path = "../process_context" }
stak-r7rs = { version = "0.8.20", path = "../r7rs" }
stak-time = { version = "0.1.26", path = "../time" }
Expand All @@ -27,5 +29,8 @@ stak-configuration = { path = "../configuration" }
stak-macro = { path = "../macro" }
wasm-bindgen-test = "0.3.47"

[build-dependencies]
stak-build = { version = "0.1.43", path = "../build" }

[lints]
workspace = true
7 changes: 7 additions & 0 deletions wasm/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! A build script.
use stak_build::{build_r7rs, BuildError};

fn main() -> Result<(), BuildError> {
build_r7rs()
}
35 changes: 33 additions & 2 deletions wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
use stak_compiler::compile_r7rs;
use stak_device::ReadWriteDevice;
use stak_file::VoidFileSystem;
use stak_process_context::VoidProcessContext;
use stak_file::{MemoryFileSystem, VoidFileSystem};
use stak_macro::include_module;
use stak_module::{Module, UniversalModule};
use stak_process_context::{MemoryProcessContext, VoidProcessContext};
use stak_r7rs::SmallPrimitiveSet;
use stak_time::VoidClock;
use stak_vm::Vm;
Expand Down Expand Up @@ -39,3 +41,32 @@ pub fn interpret(bytecodes: &[u8], input: &[u8], heap_size: usize) -> Result<Vec

Ok(output)
}

/// Runs a Scheme script with standard input and returns its standard output.
#[wasm_bindgen]
pub fn run(source: &str, input: &[u8], heap_size: usize) -> Result<Vec<u8>, JsError> {
const MAIN_FILE: &str = "main.scm";

let mut heap = vec![Default::default(); heap_size];
let mut output = vec![];
let mut error = vec![];
let files = [(MAIN_FILE.as_bytes(), source.as_bytes())];
let mut file_entries = [Default::default(); 8];

let mut vm = Vm::new(
&mut heap,
SmallPrimitiveSet::new(
ReadWriteDevice::new(input, &mut output, &mut error),
MemoryFileSystem::new(&files, &mut file_entries),
MemoryProcessContext::new(&["scheme", MAIN_FILE], &[]),
VoidClock::new(),
),
)?;

static MODULE: UniversalModule = include_module!("run.scm", stak_module);

vm.initialize(MODULE.bytecode().iter().copied())?;
vm.run()?;

Ok(output)
}
1 change: 1 addition & 0 deletions wasm/src/run.scm
20 changes: 17 additions & 3 deletions wasm/tests/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use stak_configuration::DEFAULT_HEAP_SIZE;
use stak_macro::compile_r7rs;
use stak_wasm::{compile, interpret};
use stak_wasm::{compile, interpret, run};
use wasm_bindgen_test::wasm_bindgen_test;

const SOURCE: &str = r#"
Expand All @@ -14,7 +14,7 @@ const SOURCE: &str = r#"

#[wasm_bindgen_test]
fn compile_source() {
compile(SOURCE).ok().unwrap();
compile(SOURCE).unwrap();
}

#[wasm_bindgen_test]
Expand All @@ -28,7 +28,21 @@ fn run_bytecodes() {
);

assert_eq!(
interpret(BYTECODE, &[], DEFAULT_HEAP_SIZE).ok().unwrap(),
interpret(BYTECODE, &[], DEFAULT_HEAP_SIZE).unwrap(),
b"Hello, world!"
);
}

#[wasm_bindgen_test]
fn run_script() {
const SCRIPT: &str = r#"
(import (scheme write))
(display "Hello, world!")
"#;

assert_eq!(
run(SCRIPT, &[], DEFAULT_HEAP_SIZE).unwrap(),
b"Hello, world!"
);
}

0 comments on commit 384eab6

Please sign in to comment.