-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update data path && wasm ctx can now be optional
- Loading branch information
Showing
6 changed files
with
83 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,83 @@ | ||
#![cfg(target_arch = "wasm32")] | ||
|
||
mod utils; | ||
use adana_script_core::primitive::{Primitive, RefPrimitive}; | ||
use std::collections::BTreeMap; | ||
use wasm_bindgen::prelude::*; | ||
|
||
#[global_allocator] | ||
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; | ||
|
||
fn compute( | ||
script: &str, | ||
mem: &mut [u8], | ||
) -> Result<(BTreeMap<String, RefPrimitive>, Primitive), JsError> { | ||
utils::set_panic_hook(); | ||
let mut ctx: BTreeMap<String, RefPrimitive> = if !mem.is_empty() { | ||
bincode::deserialize(mem)? | ||
} else { | ||
BTreeMap::new() | ||
}; | ||
mod internal { | ||
use crate::utils; | ||
use adana_script_core::primitive::{Primitive, RefPrimitive}; | ||
use std::collections::BTreeMap; | ||
use wasm_bindgen::prelude::{JsError, JsValue}; | ||
fn compute( | ||
script: &str, | ||
mem: &mut [u8], | ||
) -> Result<(BTreeMap<String, RefPrimitive>, Primitive), JsError> { | ||
utils::set_panic_hook(); | ||
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))?; | ||
|
||
let result = adana_script::compute(script, &mut ctx, "N/A") | ||
.map_err(|e| e.to_string()) | ||
.map_err(|e| JsError::new(&e))?; | ||
Ok((ctx, result)) | ||
} | ||
pub(super) fn compute_as_js_value( | ||
script: &str, | ||
mem: &mut [u8], | ||
) -> Result<(BTreeMap<String, RefPrimitive>, JsValue), JsError> { | ||
let (ctx, result) = compute(script, mem)?; | ||
let value = serde_wasm_bindgen::to_value(&result)?; | ||
|
||
Ok((ctx, result)) | ||
Ok((ctx, value)) | ||
} | ||
pub(super) fn compute_as_string( | ||
script: &str, | ||
mem: &mut [u8], | ||
) -> Result<(BTreeMap<String, RefPrimitive>, String), JsError> { | ||
let (ctx, result) = compute(script, mem)?; | ||
Ok((ctx, result.to_string())) | ||
} | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn compute_as_js_value( | ||
script: &str, | ||
mem: &mut [u8], | ||
) -> Result<JsValue, JsError> { | ||
let (ctx, result) = compute(script, mem)?; | ||
let (ctx, res) = internal::compute_as_js_value(script, mem)?; | ||
bincode::serialize_into(mem, &ctx)?; | ||
|
||
Ok(serde_wasm_bindgen::to_value(&result)?) | ||
Ok(res) | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn compute_as_string( | ||
script: &str, | ||
mem: &mut [u8], | ||
) -> Result<String, JsError> { | ||
let (ctx, result) = compute(script, mem)?; | ||
|
||
let result = { | ||
if let Some(out) = ctx.get("") { | ||
let mut out_rl = out.write()?; | ||
|
||
match &mut *out_rl { | ||
Primitive::Array(ref mut a) if !a.is_empty() => { | ||
let mut s = String::new(); | ||
for p in a.drain(0..) { | ||
s.push_str(&p.to_string()); | ||
} | ||
if !matches!(result, Primitive::Unit) { | ||
s.push_str(&result.to_string()); | ||
} | ||
|
||
Ok(s) | ||
} | ||
_ => Ok(result.to_string()), | ||
} | ||
} else { | ||
Ok(result.to_string()) | ||
} | ||
}; | ||
let (ctx, res) = internal::compute_as_string(script, mem)?; | ||
bincode::serialize_into(mem, &ctx)?; | ||
Ok(res) | ||
} | ||
|
||
result | ||
#[wasm_bindgen] | ||
pub fn make_ctx_and_compute_as_string( | ||
script: &str, | ||
heap_size_in_mb: Option<usize>, | ||
) -> Result<String, JsError> { | ||
let heap_size = | ||
if let Some(heap_size) = heap_size_in_mb.filter(|h| h <= &32) { | ||
heap_size | ||
} else { | ||
1 | ||
}; | ||
let mut mem = Vec::with_capacity(heap_size * 1024 * 1024); // 1mb by default | ||
let (_, res) = internal::compute_as_string(script, &mut mem)?; | ||
Ok(res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters