wander_wasm/
lib.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5//! This project exposes functionality from the Rust implementation of Wander to WASM and JS runtimes thanks to wasm-bindgen and wasm-pack.
6
7mod utils;
8use wander::{WanderValue, HostType, WanderError};
9use wasm_bindgen::prelude::*;
10use serde::Serialize;
11
12#[wasm_bindgen]
13pub fn run(script: String) -> JsValue {
14    let mut bindings = wander::preludes::common::<wander::NoHostType>();
15    let res = wander::run(&script, &mut bindings);
16    let res = RunResult {
17        object: res.clone(),
18        string: res.map(|res| format!("{}", res))
19    };
20    serde_wasm_bindgen::to_value(&res).unwrap()
21}
22
23#[derive(Serialize)]
24pub struct RunResult<T: HostType> {
25    object: Result<WanderValue<T>,WanderError>,
26    string: Result<String, WanderError>
27}
28
29#[wasm_bindgen]
30pub fn introspect(script: String) -> JsValue {
31    let mut bindings = wander::preludes::common::<wander::NoHostType>();
32    serde_wasm_bindgen::to_value(&wander::introspect(&script, &mut bindings)).unwrap()
33}