From d3e9738dd655d840df11d70a3fea30fc8b9f94ed Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 11 Apr 2018 14:18:07 +0200 Subject: [PATCH] feat(wasm) Export `Vec` to wasm. So, `wasm-bindgen` does not support `Vec` (see https://github.com/rustwasm/wasm-bindgen/issues/111), so my quick and dirty solution right now is to serialize the vector to JSON, and parse it from the JS land. --- Cargo.toml | 2 ++ src/ast.rs | 5 +++-- src/lib.rs | 7 +++---- src/wasm.rs | 9 +++++---- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 537d032..ae2c755 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,8 @@ default = [] [dependencies] nom = { version = "4.0.0-beta2" } +serde = "^1.0" +serde_derive = "^1.0" serde_json = "^1.0" wasm-bindgen = { version = "^0.2", optional = true } wee_alloc = { version = "^0.2", optional = true } \ No newline at end of file diff --git a/src/ast.rs b/src/ast.rs index ce747b1..5f0c03d 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -1,8 +1,9 @@ +use super::Input; use serde_json as json; -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Serialize)] pub struct Block<'a> { - pub name: (&'a [u8], &'a [u8]), + pub name: (Input<'a>, Input<'a>), pub attributes: Option, pub inner_blocks: Vec> } diff --git a/src/lib.rs b/src/lib.rs index 9ff002f..c1d7dae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,8 @@ #[macro_use] extern crate nom; #[cfg(feature = "wasm")] extern crate wasm_bindgen; #[cfg(feature = "wasm")] extern crate wee_alloc; +extern crate serde; +#[macro_use] extern crate serde_derive; #[cfg_attr(test, macro_use)] extern crate serde_json; pub mod ast; @@ -10,12 +12,9 @@ pub mod ast; pub mod parser; #[cfg(feature = "wasm")] pub mod wasm; - -#[cfg(feature = "wasm")] use wee_alloc::WeeAlloc; - #[cfg(feature = "wasm")] #[global_allocator] -static ALLOC: WeeAlloc = WeeAlloc::INIT; +static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; /// Represent the type of the input elements. diff --git a/src/wasm.rs b/src/wasm.rs index 1e514ba..fc0e7a6 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -1,10 +1,11 @@ +use serde_json as json; use wasm_bindgen::prelude::*; #[wasm_bindgen] -pub fn root(input: &str) -> bool { - if let Ok(_) = super::root(input.as_bytes()) { - true +pub fn root(input: &str) -> String { + if let Ok((_remaining, blocks)) = super::root(input.as_bytes()) { + json::to_string(&blocks).unwrap() } else { - false + "".to_owned() } }