Skip to content

Commit

Permalink
use console.log
Browse files Browse the repository at this point in the history
  • Loading branch information
nbittich committed Feb 10, 2024
1 parent dc0afdc commit 2580900
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 63 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
11 changes: 10 additions & 1 deletion adana-playground/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
8 changes: 0 additions & 8 deletions adana-script-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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(),
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 @@ -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
Expand Down
3 changes: 1 addition & 2 deletions adana-script-wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down Expand Up @@ -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 {
Expand Down
23 changes: 1 addition & 22 deletions adana-script-wasm/tests/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,30 +19,10 @@ 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<String, RefPrimitive> =
bincode::deserialize(&memory).unwrap();
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<String, RefPrimitive> =
bincode::deserialize(&memory).unwrap();
assert_eq!(1, ctx.len());
assert_eq!(Primitive::Array(vec![]), ctx[WASM_OUT].read().unwrap().clone());
}
5 changes: 5 additions & 0 deletions adana-script/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 12 additions & 30 deletions adana-script/src/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, RefPrimitive>,
out: Primitive,
) -> anyhow::Result<Primitive> {
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,
Expand Down Expand Up @@ -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 => {
Expand All @@ -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 => {
Expand Down

0 comments on commit 2580900

Please sign in to comment.