Skip to content

Commit

Permalink
refactor(bindings): Deprecate jsvalue::*_serde (#6462)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Nov 18, 2022
1 parent 44b0790 commit dd4b9e8
Show file tree
Hide file tree
Showing 14 changed files with 388 additions and 58 deletions.
1 change: 1 addition & 0 deletions .github/workflows/CI.yml
Expand Up @@ -641,6 +641,7 @@ jobs:
- name: Run cargo test (core)
if: matrix.settings.crate == 'swc_core'
run: |
rustup target add wasm32-unknown-unknown
cargo test -p swc_core --features ecma_quote --features common --features ecma_utils
- name: Run cargo test (binding_core_wasm)
Expand Down
32 changes: 18 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion bindings/binding_core_wasm/Cargo.toml
Expand Up @@ -31,7 +31,6 @@ swc_core = { version = "0.43.15", features = [
] }
tracing = { version = "0.1.37", features = ["max_level_off"] }
wasm-bindgen = { version = "0.2.82", features = [
"serde-serialize",
"enable-interning",
] }

Expand Down
48 changes: 36 additions & 12 deletions bindings/binding_core_wasm/src/lib.rs
@@ -1,4 +1,6 @@
use anyhow::Error;
use serde::Serialize;
use serde_wasm_bindgen::Serializer;
use swc_core::{
base::HandlerOpts,
binding_macros::wasm::{
Expand All @@ -21,6 +23,12 @@ use swc_core::{
use wasm_bindgen::{prelude::*, JsCast};
mod types;

// A serializer with options to provide backward compat for the input / output
// from the bindgen generated swc interfaces.
const COMPAT_SERIALIZER: Serializer = Serializer::new()
.serialize_maps_as_objects(true)
.serialize_missing_as_null(true);

/// Custom interface definitions for the @swc/wasm's public interface instead of
/// auto generated one, which is not reflecting most of types in detail.
#[wasm_bindgen(typescript_custom_section)]
Expand Down Expand Up @@ -81,12 +89,16 @@ pub fn minify_sync(s: JsString, opts: JsValue) -> Result<JsValue, JsValue> {
let opts = if opts.is_null() || opts.is_undefined() {
Default::default()
} else {
anyhow::Context::context(opts.into_serde(), "failed to parse options")?
serde_wasm_bindgen::from_value(opts)
.map_err(|e| anyhow::anyhow!("failed to parse options: {}", e))?
};
let fm = c.cm.new_source_file(FileName::Anon, s.into());
let program =
anyhow::Context::context(c.minify(fm, handler, &opts), "failed to minify file")?;
anyhow::Context::context(JsValue::from_serde(&program), "failed to serialize json")

program
.serialize(&COMPAT_SERIALIZER)
.map_err(|e| anyhow::anyhow!("failed to serialize program: {}", e))
})
})
.map_err(|e| convert_err(e, None))
Expand All @@ -106,7 +118,8 @@ pub fn parse_sync(s: JsString, opts: JsValue) -> Result<JsValue, JsValue> {
let opts: ParseOptions = if opts.is_null() || opts.is_undefined() {
Default::default()
} else {
anyhow::Context::context(opts.into_serde(), "failed to parse options")?
serde_wasm_bindgen::from_value(opts)
.map_err(|e| anyhow::anyhow!("failed to parse options: {}", e))?
};
let fm = c.cm.new_source_file(FileName::Anon, s.into());
let cmts = c.comments().clone();
Expand All @@ -133,7 +146,9 @@ pub fn parse_sync(s: JsString, opts: JsValue) -> Result<JsValue, JsValue> {
opts.syntax.typescript(),
));

anyhow::Context::context(JsValue::from_serde(&program), "failed to serialize json")
program
.serialize(&COMPAT_SERIALIZER)
.map_err(|e| anyhow::anyhow!("failed to serialize program: {}", e))
})
})
})
Expand All @@ -160,9 +175,9 @@ pub fn transform_sync(
let opts: Options = if opts.is_null() || opts.is_undefined() {
Default::default()
} else {
anyhow::Context::context(opts.into_serde(), "failed to parse options")
.map_err(|e| convert_err(e, None))?
serde_wasm_bindgen::from_value(opts)?
};

let error_format = opts.experimental.error_format.unwrap_or_default();
try_with_handler(c.cm.clone(), Default::default(), |handler| {
c.run(|| {
Expand Down Expand Up @@ -193,9 +208,13 @@ pub fn transform_sync(
"failed to process js file",
)?
}
Err(v) => unsafe { c.process_js(handler, v.into_serde().expect(""), &opts)? },
Err(v) => {
c.process_js(handler, serde_wasm_bindgen::from_value(v).expect("Should able to deserialize into program"), &opts)?
}
};
anyhow::Context::context(JsValue::from_serde(&out), "failed to serialize json")

out.serialize(&COMPAT_SERIALIZER)
.map_err(|e| anyhow::anyhow!("failed to serialize transform result: {}", e))
})
})
.map_err(|e| convert_err(e, Some(error_format)))
Expand All @@ -218,10 +237,13 @@ pub fn print_sync(s: JsValue, opts: JsValue) -> Result<JsValue, JsValue> {
let opts: Options = if opts.is_null() || opts.is_undefined() {
Default::default()
} else {
anyhow::Context::context(opts.into_serde(), "failed to parse options")?
serde_wasm_bindgen::from_value(opts)
.map_err(|e| anyhow::anyhow!("failed to parse options: {}", e))?
};
let program: Program =
anyhow::Context::context(s.into_serde(), "failed to deserialize program")?;

let program: Program = serde_wasm_bindgen::from_value(s)
.map_err(|e| anyhow::anyhow!("failed to deserialize program: {}", e))?;

let s = anyhow::Context::context(
c.print(
&program,
Expand All @@ -241,7 +263,9 @@ pub fn print_sync(s: JsValue, opts: JsValue) -> Result<JsValue, JsValue> {
),
"failed to print code",
)?;
anyhow::Context::context(JsValue::from_serde(&s), "failed to serialize json")

serde_wasm_bindgen::to_value(&s)
.map_err(|e| anyhow::anyhow!("failed to serialize json: {}", e))
})
})
.map_err(|e| convert_err(e, None))
Expand Down
7 changes: 6 additions & 1 deletion crates/binding_macros/Cargo.toml
Expand Up @@ -18,12 +18,15 @@ binding_wasm = [
"swc_common",
"swc_ecma_transforms",
"swc_ecma_ast",
"swc_ecma_visit",

# Optional packages
"once_cell",
"wasm-bindgen",
"wasm-bindgen-futures",
"js-sys",
"serde",
"serde-wasm-bindgen",
"anyhow",
"console_error_panic_hook",
]
Expand All @@ -34,14 +37,16 @@ swc = { optional = true, version = "0.232.99", path = "../swc" }
swc_common = { optional = true, version = "0.29.14", path = "../swc_common" }
swc_ecma_ast = { optional = true, version = "0.94.19", path = "../swc_ecma_ast" }
swc_ecma_transforms = { optional = true, version = "0.198.52", path = "../swc_ecma_transforms" }
swc_ecma_visit = { optional = true, version = "0.80.19", path = "../swc_ecma_visit" }

# Optional deps for the wasm binding macro
anyhow = { optional = true, version = "1.0.58" }
console_error_panic_hook = { optional = true, version = "0.1.7" }
js-sys = { optional = true, version = "0.3.59" }
once_cell = { optional = true, version = "1.13.0" }
serde = { optional = true, version = "1", features = ["derive"] }
serde-wasm-bindgen = { optional = true, version = "0.4.5" }
wasm-bindgen = { optional = true, version = "0.2.82", features = [
"serde-serialize",
"enable-interning",
] }
wasm-bindgen-futures = { optional = true, version = "0.4.32" }

1 comment on commit dd4b9e8

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: dd4b9e8 Previous: 4d7b920 Ratio
es/full/bugs-1 333261 ns/iter (± 24615) 366800 ns/iter (± 31418) 0.91
es/full/minify/libraries/antd 1807198979 ns/iter (± 32721704) 2055494042 ns/iter (± 78757627) 0.88
es/full/minify/libraries/d3 419103916 ns/iter (± 12419303) 477783305 ns/iter (± 18919141) 0.88
es/full/minify/libraries/echarts 1566847566 ns/iter (± 49474633) 1779525769 ns/iter (± 46840644) 0.88
es/full/minify/libraries/jquery 99481084 ns/iter (± 2778702) 118346314 ns/iter (± 7646826) 0.84
es/full/minify/libraries/lodash 127093735 ns/iter (± 10497244) 137623358 ns/iter (± 6206468) 0.92
es/full/minify/libraries/moment 66319902 ns/iter (± 6021505) 69789977 ns/iter (± 1720809) 0.95
es/full/minify/libraries/react 22117838 ns/iter (± 1405033) 23433969 ns/iter (± 1581414) 0.94
es/full/minify/libraries/terser 325721497 ns/iter (± 6132262) 372333562 ns/iter (± 12267596) 0.87
es/full/minify/libraries/three 585693790 ns/iter (± 5794116) 649739339 ns/iter (± 19737265) 0.90
es/full/minify/libraries/typescript 3471689839 ns/iter (± 78001210) 3799607078 ns/iter (± 61154067) 0.91
es/full/minify/libraries/victory 860902587 ns/iter (± 21743934) 934895778 ns/iter (± 30254734) 0.92
es/full/minify/libraries/vue 168050579 ns/iter (± 11482018) 182105144 ns/iter (± 11566172) 0.92
es/full/codegen/es3 33755 ns/iter (± 937) 34522 ns/iter (± 3486) 0.98
es/full/codegen/es5 33929 ns/iter (± 604) 34487 ns/iter (± 5155) 0.98
es/full/codegen/es2015 34413 ns/iter (± 6277) 35078 ns/iter (± 3853) 0.98
es/full/codegen/es2016 33957 ns/iter (± 979) 34826 ns/iter (± 2698) 0.98
es/full/codegen/es2017 34391 ns/iter (± 1993) 34187 ns/iter (± 2043) 1.01
es/full/codegen/es2018 34562 ns/iter (± 13329) 34358 ns/iter (± 2770) 1.01
es/full/codegen/es2019 35699 ns/iter (± 5306) 34475 ns/iter (± 3493) 1.04
es/full/codegen/es2020 34602 ns/iter (± 4503) 35314 ns/iter (± 3217) 0.98
es/full/all/es3 219672312 ns/iter (± 33052956) 223676981 ns/iter (± 15577815) 0.98
es/full/all/es5 183535112 ns/iter (± 14564518) 205150591 ns/iter (± 12701799) 0.89
es/full/all/es2015 146592478 ns/iter (± 12870827) 159631999 ns/iter (± 10881445) 0.92
es/full/all/es2016 146346454 ns/iter (± 12561533) 161724721 ns/iter (± 11560641) 0.90
es/full/all/es2017 147970126 ns/iter (± 15861961) 159447439 ns/iter (± 14104711) 0.93
es/full/all/es2018 146178569 ns/iter (± 11174568) 159974078 ns/iter (± 12840273) 0.91
es/full/all/es2019 144069288 ns/iter (± 12461896) 159551306 ns/iter (± 13664315) 0.90
es/full/all/es2020 139428674 ns/iter (± 8948386) 154461090 ns/iter (± 10076296) 0.90
es/full/parser 725785 ns/iter (± 32380) 772838 ns/iter (± 66860) 0.94
es/full/base/fixer 26742 ns/iter (± 897) 28855 ns/iter (± 3848) 0.93
es/full/base/resolver_and_hygiene 92772 ns/iter (± 2698) 99021 ns/iter (± 16403) 0.94
serialization of ast node 219 ns/iter (± 4) 220 ns/iter (± 26) 1.00
serialization of serde 222 ns/iter (± 7) 251 ns/iter (± 36) 0.88

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.