This repository has been archived by the owner on Jan 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Sier codec wasm binding. #140
Open
melatron
wants to merge
8
commits into
master
Choose a base branch
from
melatron/sier-wasm-binding
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+183
−1
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ab74fb6
taking into consideration the 8 bytes for struct definition when pars…
melatron 057f01f
Stop adding ID for inner objects serialize.
melatron 0c70ce1
Implemented sier codec bytes to JSON parser
melatron d731d3d
Sier codec wasm binding.
melatron 8825740
test fix
melatron 67df89b
Fix error strings.
melatron 36df62e
Merge branch 'master' into melatron/sier-wasm-binding
melatron 5c0ad6b
review fix
melatron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "sier-codec-js" | ||
version = "0.1.0" | ||
authors = ["Antoni Dikov <[email protected]"] | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
|
||
[dependencies] | ||
wasm-bindgen = { version = "0.2.79", features = ["serde-serialize"] } | ||
|
||
sier-codec = { path="../sier-codec" } | ||
serde_json = "1.0.79" | ||
js-sys = "0.3.56" | ||
|
||
[dev-dependencies] | ||
wasm-bindgen-test = "0.3.29" |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//TODO(melatron): Remove js-test folder | ||
import pkg from '../pkg/sier_codec_js.js'; | ||
|
||
const JSON_STRUCT_DEF = ` | ||
struct Corge { | ||
gz :u64; | ||
op :bool; | ||
} | ||
|
||
struct Foo { | ||
bar :u64; | ||
baz :string; | ||
qux :List<u64>; | ||
corge :Corge; | ||
}`; | ||
|
||
const FOO = | ||
{ | ||
"bar": 42, | ||
"baz": "abc", | ||
"qux": [4, 2], | ||
"corge": { | ||
"gz": 42, | ||
"op": true | ||
} | ||
}; | ||
console.log(FOO); | ||
|
||
let sier = pkg.serialize(FOO, JSON_STRUCT_DEF, "Foo"); | ||
console.log(sier); | ||
let json = pkg.deserialize(sier, JSON_STRUCT_DEF); | ||
console.log(json); |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "sier-codec-js-test", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"author": "Antoni Dikov", | ||
"license": "MIT", | ||
"type": "module" | ||
} |
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use sier_codec::Parser; | ||
use wasm_bindgen::prelude::*; | ||
//TODO(melatron): Create a JS Class that holds the file_defs inside and | ||
// serialize/deserialize are methods of this class. | ||
#[wasm_bindgen] | ||
pub fn serialize(js_object: JsValue, file_defs: &str, struct_def: &str) -> js_sys::Uint8Array { | ||
if !js_object.is_object() { | ||
wasm_bindgen::throw_str("Provided argument is not a JS Object."); | ||
} | ||
|
||
let json = js_sys::JSON::stringify(&js_object).unwrap_or_else(|e| { | ||
wasm_bindgen::throw_val(e); | ||
}); | ||
Comment on lines
+11
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be doable as:
|
||
let json = json | ||
.as_string() | ||
.expect("Parsed as string from JSON::stringify"); | ||
|
||
let mut parser = Parser::default(); | ||
parser.add_file_defs(file_defs).unwrap_or_else(|e| { | ||
wasm_bindgen::throw_str(&e.to_string()); | ||
}); | ||
|
||
let def = parser.struct_def(struct_def).unwrap_or_else(|| { | ||
wasm_bindgen::throw_str( | ||
&format!( | ||
"Could not find struct {0} in provided definitions.", | ||
struct_def | ||
), | ||
); | ||
}); | ||
|
||
let obj = parser.json_str(json.as_str(), def).unwrap_or_else(|e| { | ||
wasm_bindgen::throw_str(&e.to_string()); | ||
}); | ||
|
||
js_sys::Uint8Array::from(&obj.serialize()[..]) | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn deserialize(sier: js_sys::Uint8Array, file_defs: &str) -> JsValue { | ||
let mut parser = Parser::default(); | ||
parser.add_file_defs(file_defs).unwrap_or_else(|e| { | ||
wasm_bindgen::throw_str(&format!("Wrong file definitions provided {0}", e)); | ||
}); | ||
|
||
let obj = parser.parse(&sier.to_vec()).unwrap_or_else(|e| { | ||
wasm_bindgen::throw_str(&format!("Parse failed - {0}", e)); | ||
}); | ||
|
||
let json = sier_codec::json::transform_sier_obj(&obj).unwrap_or_else(|e| { | ||
wasm_bindgen::throw_str(&format!("Transforming sier object failed - {0}", e)); | ||
}); | ||
|
||
JsValue::from_serde(&json).unwrap_or_else(|e| { | ||
wasm_bindgen::throw_str(&format!( | ||
"Serde Value to Wasm JsValue parsing failed - {0}", | ||
e | ||
)); | ||
}) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//! Test suite for the Web and headless browsers. | ||
|
||
#![cfg(target_arch = "wasm32")] | ||
|
||
extern crate wasm_bindgen_test; | ||
use wasm_bindgen_test::*; | ||
|
||
use wasm_bindgen::prelude::*; | ||
|
||
use sier_codec_js; | ||
|
||
const JSON_STRUCT_DEF: &'static str = r#" | ||
struct Corge { | ||
gz :u64; | ||
op :bool; | ||
} | ||
|
||
struct Foo { | ||
bar :u64; | ||
baz :string; | ||
qux :List<u64>; | ||
corge :Corge; | ||
} | ||
"#; | ||
|
||
const JSON: &'static str = r#" | ||
{ | ||
"bar": 42, | ||
"baz": "abc", | ||
"qux": [4, 2], | ||
"corge": { | ||
"gz": 42, | ||
"op": true | ||
} | ||
} | ||
"#; | ||
|
||
#[wasm_bindgen_test] | ||
fn deserialize() { | ||
let json = js_sys::JSON::parse(JSON).unwrap(); | ||
let sier = sier_codec_js::serialize(json, JSON_STRUCT_DEF, "Foo"); | ||
let deserialized_json = sier_codec_js::deserialize(sier, JSON_STRUCT_DEF); | ||
let str_json = js_sys::JSON::stringify(&deserialized_json).unwrap(); | ||
let left: String = str_json.into(); | ||
|
||
assert_eq!( | ||
left, | ||
"{\"bar\":42,\"baz\":\"abc\",\"corge\":{\"gz\":42,\"op\":true},\"qux\":[4,2]}" | ||
); | ||
} | ||
|
||
#[wasm_bindgen_test] | ||
fn serialize() { | ||
let json = js_sys::JSON::parse(JSON).unwrap(); | ||
let sier = sier_codec_js::serialize(json, JSON_STRUCT_DEF, "Foo"); | ||
|
||
assert_eq!( | ||
sier.to_vec(), | ||
vec![ | ||
206, 206, 22, 230, 245, 223, 67, 43, 42, 0, 0, 0, 0, 0, 0, 0, 3, 97, 98, 99, 16, 4, 0, | ||
0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 1 | ||
] | ||
); | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might be able to have these functions return
Result<T, JsValue>
instead of explicitly throwing everywhere. More idiomatic and probably easier to read/write. Might even be able to use the?
operator.