Skip to content
This repository has been archived by the owner on Jun 15, 2020. It is now read-only.

Commit

Permalink
feat(wasm) Export Vec<Block> to wasm.
Browse files Browse the repository at this point in the history
So, `wasm-bindgen` does not support `Vec<T>` (see
rustwasm/wasm-bindgen#111), so my quick and
dirty solution right now is to serialize the vector to JSON, and parse
it from the JS land.
  • Loading branch information
Hywan committed Apr 11, 2018
1 parent 420c3a9 commit d3e9738
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
5 changes: 3 additions & 2 deletions src/ast.rs
Original file line number Diff line number Diff line change
@@ -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<json::Value>,
pub inner_blocks: Vec<Block<'a>>
}
7 changes: 3 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
#[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;
#[macro_use] pub mod combinators;
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.
Expand Down
9 changes: 5 additions & 4 deletions src/wasm.rs
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit d3e9738

Please sign in to comment.