diff --git a/Cargo.toml b/Cargo.toml index 3898abf..648b138 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,6 +48,8 @@ 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" +web-sys = "0.3.68" + # workspace specific libs adana-script-core = { version = "0.17.2", path = "./adana-script-core" } adana-script = { version = "0.17.2", path = "./adana-script" } diff --git a/adana-playground/app.js b/adana-playground/app.js index 619682e..a86b07a 100644 --- a/adana-playground/app.js +++ b/adana-playground/app.js @@ -19,12 +19,21 @@ async function run() { const out = document.querySelector("#out"); out.value = ""; + let logs = []; + + console.log = function () { + for (const a of arguments) { + logs.push(a); + } + }; + form.addEventListener("submit", (e) => { e.preventDefault(); + logs = []; const data = new FormData(e.target); let res = compute(data.get("code") || "", ctx); console.log(res); // NORDINE - out.value = res; + out.value = logs.join(""); }); const issueLink = document.querySelector("#issueLink"); issueLink.onclick = (e) => { diff --git a/adana-script-core/src/lib.rs b/adana-script-core/src/lib.rs index d6a59ae..42ee600 100644 --- a/adana-script-core/src/lib.rs +++ b/adana-script-core/src/lib.rs @@ -8,9 +8,6 @@ use constants::{ TO_HEX, TRUE, WHILE, }; -#[cfg(target_arch = "wasm32")] -use constants::WASM_OUT; - use primitive::Primitive; use serde::{Deserialize, Serialize}; use strum::EnumCount; @@ -85,9 +82,6 @@ pub mod constants { pub const IN: &str = "in"; pub const REQUIRE: &str = "require"; pub const NATIVE_LIB: &[u8; 14] = b"__native_lib__"; - - #[cfg(target_arch = "wasm32")] - pub const WASM_OUT: &str = "_WASM_OUT_"; } #[derive(Debug, EnumCount)] @@ -385,8 +379,6 @@ pub const FORBIDDEN_VARIABLE_NAME: &[&str] = &[ REQUIRE, MULTILINE, STRUCT, - #[cfg(target_arch = "wasm32")] - WASM_OUT, Operator::Add.as_str(), Operator::Subtr.as_str(), Operator::Div.as_str(), diff --git a/adana-script-wasm/Cargo.toml b/adana-script-wasm/Cargo.toml index 884749f..95b242b 100644 --- a/adana-script-wasm/Cargo.toml +++ b/adana-script-wasm/Cargo.toml @@ -20,6 +20,7 @@ crate-type = ["cdylib", "rlib"] default = ["wee_alloc", "console_error_panic_hook"] [dependencies] + [target.'cfg(target_arch = "wasm32")'.dependencies] wasm-bindgen = { workspace = true } adana-script.workspace = true diff --git a/adana-script-wasm/src/lib.rs b/adana-script-wasm/src/lib.rs index 714447a..a864141 100644 --- a/adana-script-wasm/src/lib.rs +++ b/adana-script-wasm/src/lib.rs @@ -1,7 +1,6 @@ #![cfg(target_arch = "wasm32")] mod utils; -use adana_script_core::constants::WASM_OUT; use adana_script_core::primitive::{Primitive, RefPrimitive}; use std::collections::BTreeMap; use wasm_bindgen::prelude::*; @@ -45,7 +44,7 @@ pub fn compute_as_string( let (ctx, result) = compute(script, mem)?; let result = { - if let Some(out) = ctx.get(WASM_OUT) { + if let Some(out) = ctx.get("") { let mut out_rl = out.write()?; match &mut *out_rl { diff --git a/adana-script-wasm/tests/web.rs b/adana-script-wasm/tests/web.rs index c198d51..3d71008 100644 --- a/adana-script-wasm/tests/web.rs +++ b/adana-script-wasm/tests/web.rs @@ -4,9 +4,8 @@ extern crate wasm_bindgen_test; -use adana_script_core::constants::WASM_OUT; use adana_script_core::primitive::{Primitive, RefPrimitive}; -use adana_script_wasm::{compute_as_js_value, compute_as_string}; +use adana_script_wasm::compute_as_js_value; use std::assert_eq; use std::collections::BTreeMap; use wasm_bindgen::JsValue; @@ -20,7 +19,6 @@ fn compute_as_js_value_test() { let res = compute_as_js_value("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 = @@ -28,22 +26,3 @@ fn compute_as_js_value_test() { assert_eq!(1, ctx.len()); assert_eq!(Primitive::Int(2), ctx["x"].read().unwrap().clone()); } -#[wasm_bindgen_test] -fn compute_as_string_test() { - let mut memory = vec![0; 64]; - let res = compute_as_string( - r#" - println("hello") - println("world") - "#, - &mut memory, - ) - .map_err(JsValue::from) - .unwrap(); - - assert_eq!("hello\nworld\n".to_string(), res); - let ctx: BTreeMap = - bincode::deserialize(&memory).unwrap(); - assert_eq!(1, ctx.len()); - assert_eq!(Primitive::Array(vec![]), ctx[WASM_OUT].read().unwrap().clone()); -} diff --git a/adana-script/Cargo.toml b/adana-script/Cargo.toml index 5fba23e..680c75a 100644 --- a/adana-script/Cargo.toml +++ b/adana-script/Cargo.toml @@ -17,6 +17,11 @@ adana-script-core.workspace = true anyhow.workspace = true slab_tree.workspace = true + +[target.'cfg(target_arch = "wasm32")'.dependencies] +web-sys = { workspace = true, features = ["console"] } +wasm-bindgen.workspace = true + [dev-dependencies] # some test must run sequentially. # if library is broken, alternative is: cargo test -- --test-threads 1 diff --git a/adana-script/src/compute.rs b/adana-script/src/compute.rs index dbda1dd..08c6009 100644 --- a/adana-script/src/compute.rs +++ b/adana-script/src/compute.rs @@ -11,28 +11,6 @@ use crate::{parser::parse_instructions, prelude::BTreeMap}; use super::{ast::to_ast, require_dynamic_lib::require_dynamic_lib}; -#[cfg(target_arch = "wasm32")] -fn write_to_wasm_out( - ctx: &mut BTreeMap, - out: Primitive, -) -> anyhow::Result { - use adana_script_core::constants::WASM_OUT; - use std::collections::btree_map::Entry; - - let wasm_out = match ctx.entry(WASM_OUT.to_string()) { - Entry::Occupied(o) => o.into_mut(), - Entry::Vacant(v) => v.insert(Primitive::Array(vec![]).ref_prim()), - }; - let new_arr = { - let arr = wasm_out - .read() - .map_err(|e| anyhow::format_err!("could not read rc arr: {e}"))?; - arr.add(&out) - }; - *wasm_out = new_arr.ref_prim(); - Ok(Primitive::Unit) -} - use adana_script_core::{ primitive::{ Abs, Add, And, Array, BitShift, Cos, DisplayBinary, DisplayHex, Div, @@ -381,10 +359,12 @@ fn compute_recur( } #[cfg(target_arch = "wasm32")] { - write_to_wasm_out( - ctx, - Primitive::String(format!("{v}\n")), - ) + web_sys::console::log_1( + &wasm_bindgen::JsValue::from_str(&format!( + "{v}\n" + )), + ); + Ok(Primitive::Unit) } } adana_script_core::BuiltInFunctionType::Print => { @@ -395,10 +375,12 @@ fn compute_recur( } #[cfg(target_arch = "wasm32")] { - write_to_wasm_out( - ctx, - Primitive::String(v.to_string()), - ) + web_sys::console::log_1( + &wasm_bindgen::JsValue::from_str(&format!( + "{v}" + )), + ); + Ok(Primitive::Unit) } } adana_script_core::BuiltInFunctionType::Require => {