Skip to content

Commit

Permalink
Revert "refactor(bindings): Deprecate JsValue::*_serde (swc-project…
Browse files Browse the repository at this point in the history
…#6436)"

This reverts commit 1dd8b3d.
  • Loading branch information
kdy1 committed Nov 15, 2022
1 parent b8bb849 commit 0479ce6
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 88 deletions.
26 changes: 12 additions & 14 deletions Cargo.lock

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

2 changes: 0 additions & 2 deletions bindings/Cargo.lock

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

7 changes: 4 additions & 3 deletions bindings/binding_core_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ plugin = ["swc_core/plugin_transform_host_js"]
anyhow = "1.0.66"
serde = {version = "1", features = ["derive"]}
serde-wasm-bindgen = "0.4.5"
swc_core = { version = "0.43.11", features = [
swc_core = {version = "0.43.11", features = [
"common_perf",
"binding_macro_wasm",
"ecma_transforms",
"ecma_visit",
] }
tracing = { version = "0.1.37", features = ["max_level_off"] }
]}
tracing = {version = "0.1.37", features = ["max_level_off"]}
wasm-bindgen = {version = "0.2.82", features = [
"serde-serialize",
"enable-interning",
]}

Expand Down
48 changes: 12 additions & 36 deletions bindings/binding_core_wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use anyhow::Error;
use serde::Serialize;
use serde_wasm_bindgen::Serializer;
use swc_core::{
base::HandlerOpts,
binding_macros::wasm::{
Expand All @@ -23,12 +21,6 @@ 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 @@ -89,16 +81,12 @@ pub fn minify_sync(s: JsString, opts: JsValue) -> Result<JsValue, JsValue> {
let opts = if opts.is_null() || opts.is_undefined() {
Default::default()
} else {
serde_wasm_bindgen::from_value(opts)
.map_err(|e| anyhow::anyhow!("failed to parse options: {}", e))?
anyhow::Context::context(opts.into_serde(), "failed to parse options")?
};
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")?;

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

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

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 @@ -208,13 +193,9 @@ pub fn transform_sync(
"failed to process js file",
)?
}
Err(v) => {
c.process_js(handler, serde_wasm_bindgen::from_value(v).expect("Should able to deserialize into program"), &opts)?
}
Err(v) => unsafe { c.process_js(handler, v.into_serde().expect(""), &opts)? },
};

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

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

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

serde_wasm_bindgen::to_value(&s)
.map_err(|e| anyhow::anyhow!("failed to serialize json: {}", e))
anyhow::Context::context(JsValue::from_serde(&s), "failed to serialize json")
})
})
.map_err(|e| convert_err(e, None))
Expand Down
3 changes: 1 addition & 2 deletions crates/binding_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ 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"] }
wasm-bindgen = { optional = true, version = "0.2.82", features = [
"serde-serialize",
"enable-interning",
] }
wasm-bindgen-futures = { optional = true, version = "0.4.32" }
serde-wasm-bindgen = { optional = true, version = "0.4.5" }
46 changes: 15 additions & 31 deletions crates/binding_macros/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use anyhow::Error;
#[doc(hidden)]
pub use js_sys;
use once_cell::sync::Lazy;
use serde::Serialize;
use serde_wasm_bindgen::Serializer;
use swc::{config::ErrorFormat, Compiler};
#[doc(hidden)]
pub use swc::{
Expand All @@ -26,12 +24,6 @@ pub use wasm_bindgen::{JsCast, JsValue};
#[doc(hidden)]
pub use wasm_bindgen_futures::future_to_promise;

// 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);

/// Get global sourcemap
pub fn compiler() -> Arc<Compiler> {
console_error_panic_hook::set_once();
Expand Down Expand Up @@ -74,16 +66,13 @@ macro_rules! build_minify_sync {
let opts = if opts.is_null() || opts.is_undefined() {
Default::default()
} else {
$crate::wasm::serde_wasm_bindgen::from_value(opts)
.map_err(|e| $crate::wasm::anyhow::anyhow!("failed to parse options: {}", e))?
$crate::wasm::anyhow::Context::context(opts.into_serde(), "failed to parse options")?
};

let fm = c.cm.new_source_file($crate::wasm::FileName::Anon, s.into());
let program = $crate::wasm::anyhow::Context::context(c.minify(fm, handler, &opts), "failed to minify file")?;

program
.serialize(&COMPAT_SERIALIZER)
.map_err(|e| $crate::wasm::anyhow::anyhow!("failed to serialize program: {}", e))
$crate::wasm::anyhow::Context::context($crate::wasm::JsValue::from_serde(&program), "failed to serialize json")
})
},
)
Expand Down Expand Up @@ -126,8 +115,7 @@ macro_rules! build_parse_sync {
let opts: $crate::wasm::ParseOptions = if opts.is_null() || opts.is_undefined() {
Default::default()
} else {
$crate::wasm::serde_wasm_bindgen::from_value(opts)
.map_err(|e| $crate::wasm::anyhow::anyhow!("failed to parse options: {}", e))?
$crate::wasm::anyhow::Context::context(opts.into_serde(), "failed to parse options")?
};

let fm = c.cm.new_source_file($crate::wasm::FileName::Anon, s.into());
Expand All @@ -152,9 +140,7 @@ macro_rules! build_parse_sync {
"failed to parse code"
)?;

program
.serialize(&COMPAT_SERIALIZER)
.map_err(|e| $crate::wasm::anyhow::anyhow!("failed to serialize program: {}", e))
$crate::wasm::anyhow::Context::context($crate::wasm::JsValue::from_serde(&program), "failed to serialize json")
})
},
)
Expand Down Expand Up @@ -197,12 +183,10 @@ macro_rules! build_print_sync {
let opts: $crate::wasm::Options = if opts.is_null() || opts.is_undefined() {
Default::default()
} else {
$crate::wasm::serde_wasm_bindgen::from_value(opts)
.map_err(|e| $crate::wasm::anyhow::anyhow!("failed to parse options: {}", e))?
$crate::wasm::anyhow::Context::context(opts.into_serde(), "failed to parse options")?
};

let program: $crate::wasm::Program = $crate::wasm::serde_wasm_bindgen::from_value(s)
.map_err(|e| $crate::wasm::anyhow::anyhow!("failed to deserialize program: {}", e))?;
let program: $crate::wasm::Program = $crate::wasm::anyhow::Context::context(s.into_serde(), "failed to deserialize program")?;
let s = $crate::wasm::anyhow::Context::context(c
.print(
&program,
Expand All @@ -221,9 +205,7 @@ macro_rules! build_print_sync {
false,
),"failed to print code")?;

program
.serialize(&COMPAT_SERIALIZER)
.map_err(|e| $crate::wasm::anyhow::anyhow!("failed to serialize program: {}", e))
$crate::wasm::anyhow::Context::context(JsValue::from_serde(&s), "failed to serialize json")
})
},
)
Expand Down Expand Up @@ -299,7 +281,9 @@ macro_rules! build_transform_sync {
buffer
};

let bytes: Vec<u8> = $crate::wasm::serde_wasm_bindgen::from_value(data).expect("Could not read byte from plugin resolver");
let bytes: Vec<u8> = data
.into_serde()
.expect("Could not read byte from plugin resolver");

// In here we 'inject' externally loaded bytes into the cache, so
// remaining plugin_runner execution path works as much as
Expand All @@ -312,7 +296,8 @@ macro_rules! build_transform_sync {
let opts: $crate::wasm::Options = if opts.is_null() || opts.is_undefined() {
Default::default()
} else {
$crate::wasm::serde_wasm_bindgen::from_value(opts)?
$crate::wasm::anyhow::Context::context(opts.into_serde(), "failed to parse options")
.map_err(|e| $crate::wasm::convert_err(e, None))?
};

let error_format = opts.experimental.error_format.unwrap_or_default();
Expand Down Expand Up @@ -348,12 +333,11 @@ macro_rules! build_transform_sync {
), "failed to process js file"
)?
}
Err(v) => unsafe { c.process_js(handler, $crate::wasm::serde_wasm_bindgen::from_value(v).expect(""), &opts)? },
Err(v) => unsafe { c.process_js(handler, v.into_serde().expect(""), &opts)? },
};

out
.serialize(&COMPAT_SERIALIZER)
.map_err(|e| $crate::wasm::anyhow::anyhow!("failed to serialize transform result: {}", e))
$crate::wasm::anyhow::Context::context($crate::wasm::JsValue::from_serde(&out),
"failed to serialize json")
})
},
)
Expand Down

1 comment on commit 0479ce6

@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: 0479ce6 Previous: 948e35b Ratio
es/full/bugs-1 343577 ns/iter (± 17188) 343550 ns/iter (± 20307) 1.00
es/full/minify/libraries/antd 1890584580 ns/iter (± 68346499) 1918613605 ns/iter (± 27788602) 0.99
es/full/minify/libraries/d3 434724079 ns/iter (± 24818563) 411847176 ns/iter (± 9000097) 1.06
es/full/minify/libraries/echarts 1633711386 ns/iter (± 100004227) 1602234114 ns/iter (± 45659768) 1.02
es/full/minify/libraries/jquery 104300146 ns/iter (± 10825094) 99042557 ns/iter (± 5193040) 1.05
es/full/minify/libraries/lodash 121868548 ns/iter (± 8901163) 117907487 ns/iter (± 5810567) 1.03
es/full/minify/libraries/moment 64437037 ns/iter (± 4882009) 59190908 ns/iter (± 2466949) 1.09
es/full/minify/libraries/react 21167701 ns/iter (± 556358) 20728014 ns/iter (± 1778732) 1.02
es/full/minify/libraries/terser 330672171 ns/iter (± 10582408) 297951302 ns/iter (± 7917389) 1.11
es/full/minify/libraries/three 581108629 ns/iter (± 18076965) 587759274 ns/iter (± 13857055) 0.99
es/full/minify/libraries/typescript 3486977107 ns/iter (± 119480369) 3519955127 ns/iter (± 104605921) 0.99
es/full/minify/libraries/victory 857912875 ns/iter (± 22304250) 852374252 ns/iter (± 24390741) 1.01
es/full/minify/libraries/vue 175297851 ns/iter (± 15744328) 160770327 ns/iter (± 7981538) 1.09
es/full/codegen/es3 35603 ns/iter (± 1511) 34925 ns/iter (± 1627) 1.02
es/full/codegen/es5 35309 ns/iter (± 10496) 34319 ns/iter (± 1683) 1.03
es/full/codegen/es2015 34858 ns/iter (± 769) 35211 ns/iter (± 2154) 0.99
es/full/codegen/es2016 34917 ns/iter (± 1177) 34683 ns/iter (± 2157) 1.01
es/full/codegen/es2017 35116 ns/iter (± 1029) 34512 ns/iter (± 815) 1.02
es/full/codegen/es2018 35154 ns/iter (± 1158) 34392 ns/iter (± 905) 1.02
es/full/codegen/es2019 35252 ns/iter (± 4362) 34312 ns/iter (± 818) 1.03
es/full/codegen/es2020 35026 ns/iter (± 798) 34279 ns/iter (± 1151) 1.02
es/full/all/es3 196386653 ns/iter (± 14006781) 194127133 ns/iter (± 11726037) 1.01
es/full/all/es5 190198727 ns/iter (± 13778004) 182127686 ns/iter (± 16083062) 1.04
es/full/all/es2015 150017198 ns/iter (± 8983472) 147043997 ns/iter (± 11211648) 1.02
es/full/all/es2016 149975237 ns/iter (± 11113609) 146351168 ns/iter (± 8371993) 1.02
es/full/all/es2017 149870577 ns/iter (± 15422849) 145674489 ns/iter (± 8588438) 1.03
es/full/all/es2018 148916534 ns/iter (± 14859021) 143979878 ns/iter (± 6720903) 1.03
es/full/all/es2019 157119542 ns/iter (± 16097076) 142589262 ns/iter (± 9629843) 1.10
es/full/all/es2020 154008011 ns/iter (± 16686999) 137907789 ns/iter (± 7413935) 1.12
es/full/parser 769383 ns/iter (± 139096) 715916 ns/iter (± 20625) 1.07
es/full/base/fixer 28136 ns/iter (± 1894) 26126 ns/iter (± 994) 1.08
es/full/base/resolver_and_hygiene 98002 ns/iter (± 13337) 91329 ns/iter (± 2580) 1.07
serialization of ast node 232 ns/iter (± 7) 216 ns/iter (± 4) 1.07
serialization of serde 223 ns/iter (± 38) 222 ns/iter (± 2) 1.00

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

Please sign in to comment.