Skip to content
This repository was archived by the owner on Jan 13, 2023. It is now read-only.

put wasm behind a feature gate #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ serde = "1.0.101"
serde_json = "1.0.40"
serde_derive = "1.0.101"
bson = { git = "https://github.com/lrlna/bson-rs", branch = "wasm-dec128" }
wee_alloc = "0.4.2"
console_error_panic_hook = "0.1.6"
js-sys = "0.3.25"
web-sys = { version = "0.3.16", features = ['console'] }
wasm-bindgen-test = "0.3.8"
wee_alloc = { version = "0.4.2", optional = true }
console_error_panic_hook = { version = "0.1.6", optional = true }
js-sys = { version = "0.3.25", optional = true }
web-sys = { version = "0.3.16", features = ['console'], optional = true }
wasm-bindgen-test = { version = "0.3.8", optional = true }

[dependencies.wasm-bindgen]
version = "^0.2.37"
features = ["serde-serialize"]
optional = true

[features]
wasm = ["wee_alloc", "console_error_panic_hook", "js-sys", "web-sys", "wasm-bindgen-test", "wasm-bindgen"]
default = ["wasm"]
4 changes: 3 additions & 1 deletion src/field_type.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![allow(clippy::option_map_unit_fn)]
use super::{Bson, SchemaParser, ValueType, HashMap, console};
#[cfg(feature = "wasm")]
use super::console;
use super::{Bson, HashMap, SchemaParser, ValueType};

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct FieldType {
Expand Down
10 changes: 9 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,19 @@ extern crate serde_derive;
extern crate serde;
use serde_json::Value;

#[cfg(feature = "wasm")]
use js_sys::{Object, Uint8Array};
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;
// add to use console.log to send debugs to js land
#[cfg(feature = "wasm")]
use web_sys::console;

// using custom allocator which is built specifically for wasm; makes it smaller
// + faster
#[cfg(feature = "wasm")]
use wee_alloc;
#[cfg(feature = "wasm")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

Expand All @@ -86,10 +91,12 @@ mod value_type;
use crate::value_type::ValueType;

// WASM Api of the Schema Parser.
#[cfg(feature = "wasm")]
mod lib_wasm;
#[cfg(feature = "wasm")]
use crate::lib_wasm::*;

#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct SchemaParser {
pub count: usize,
Expand Down Expand Up @@ -154,6 +161,7 @@ impl SchemaParser {
/// let uint8 = Uint8Array::new(&JsValue::from_str(r#"{ "name": "Chashu", "type": "Cat" }"#));
/// schema_parser.write_raw(uint8);
/// ```
#[cfg(feature = "wasm")]
#[inline]
pub fn write_raw(&mut self, uint8: Uint8Array) -> Result<(), failure::Error> {
let mut decoded_vec = vec![0u8; uint8.length() as usize];
Expand Down
11 changes: 8 additions & 3 deletions tests/schema.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use wasm_bindgen_test::*;
use mongodb_schema_parser::SchemaParser;
use web_sys::console;
#[cfg(feature = "wasm")]
use js_sys::Uint8Array;
use mongodb_schema_parser::SchemaParser;
#[cfg(feature = "wasm")]
use wasm_bindgen::JsValue;
#[cfg(feature = "wasm")]
use wasm_bindgen_test::*;
#[cfg(feature = "wasm")]
use web_sys::console;

#[cfg(feature = "wasm")]
#[wasm_bindgen_test]
fn test_binary_sales_supplies() {
// documents that failed with an unreachable error in js land
Expand Down