Skip to content

Commit

Permalink
use wasm memory
Browse files Browse the repository at this point in the history
  • Loading branch information
nbittich committed Feb 6, 2024
1 parent acf6c7e commit a6cca50
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 18 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
- run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
- run: cargo test --verbose -- --nocapture
- run: wasm-pack test --headless --firefox ./adana-script-wasm
- run: wasm-pack test --headless --chrome ./adana-script-wasm
- run: cargo clippy --all-targets --all-features -- -D warnings
- run: cargo release --no-publish --no-tag --allow-branch pr_check --no-push --dependent-version upgrade minor
#- run: cargo publish --dry-run
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ console_error_panic_hook = { version = "0.1.7" }
serde-wasm-bindgen = "0.6.3"
wee_alloc = "0.4.5"
wasm-bindgen-test = "0.3.41"

# workspace specific libs
adana-script-core = { version = "0.16.4", path = "./adana-script-core" }
adana-script = { version = "0.16.4", path = "./adana-script" }
Expand Down
1 change: 1 addition & 0 deletions adana-script-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ serde.workspace = true
serde-wasm-bindgen.workspace = true
console_error_panic_hook = { workspace = true, optional = true }
wee_alloc = { workspace = true, optional = true }
bincode.workspace = true

[dev-dependencies]
wasm-bindgen-test.workspace = true
30 changes: 21 additions & 9 deletions adana-script-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,31 @@ pub struct Out {
}

#[wasm_bindgen]
pub fn compute(script: &str, ctx: JsValue) -> Result<JsValue, JsError> {
pub fn compute(script: &str, mem: &mut [u8]) -> Result<JsValue, JsError> {
utils::set_panic_hook();
// TODO try wasm memory:
// wasm memory:
// https://developer.mozilla.org/fr/docs/WebAssembly/JavaScript_interface/Memory
let mut ctx: BTreeMap<String, RefPrimitive> =
if !ctx.is_undefined() && ctx.is_object() {
serde_wasm_bindgen::from_value(ctx)?
} else {
BTreeMap::new()
};
let mut ctx: BTreeMap<String, RefPrimitive> = if !mem.is_empty() {
bincode::deserialize(mem)?
} else {
BTreeMap::new()
};

let result = adana_script::compute(script, &mut ctx, "N/A")
.map_err(|e| e.to_string())
.map_err(|e| JsError::new(&e))?;

Ok(serde_wasm_bindgen::to_value(&Out { ctx, result })?)
let new_mem = bincode::serialize(&ctx)?;
if mem.len() < new_mem.len() {
Err(JsError::new(&format!(
"out of memory: {} < {}",
mem.len(),
new_mem.len()
)))
} else {
for (i, o) in new_mem.into_iter().enumerate() {
mem[i] = o;
}
Ok(serde_wasm_bindgen::to_value(&result)?)
}
}
21 changes: 13 additions & 8 deletions adana-script-wasm/tests/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@

extern crate wasm_bindgen_test;

use adana_script_core::primitive::Primitive;
use adana_script_wasm::{compute, Out};
use adana_script_core::primitive::{Primitive, RefPrimitive};
use adana_script_wasm::compute;
use std::assert_eq;
use std::collections::BTreeMap;
use wasm_bindgen::JsValue;
use wasm_bindgen_test::*;

wasm_bindgen_test_configure!(run_in_browser);

#[wasm_bindgen_test]
fn compute_test() {
let res = compute("x = 1+1", JsValue::NULL).map_err(JsValue::from).unwrap();
let res: Out = serde_wasm_bindgen::from_value(res).unwrap();
assert_eq!(Primitive::Int(2), res.result);
assert_eq!(1, res.ctx.len());
assert_eq!(Primitive::Int(2), res.ctx["x"].read().unwrap().clone());
let mut memory = vec![0; 64];
let res = compute("x = 1+1", &mut memory).map_err(JsValue::from).unwrap();

//wasm_bindgen_test::console_log!("{memory:?}");
let res: Primitive = serde_wasm_bindgen::from_value(res).unwrap();
assert_eq!(Primitive::Int(2), res);
let ctx: BTreeMap<String, RefPrimitive> =
bincode::deserialize(&memory).unwrap();
assert_eq!(1, ctx.len());
assert_eq!(Primitive::Int(2), ctx["x"].read().unwrap().clone());
}

0 comments on commit a6cca50

Please sign in to comment.