Skip to content

Commit

Permalink
fix(bindings): Revert #6436 (#6444)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Nov 15, 2022
1 parent 6cc71e9 commit 948e35b
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 87 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.

3 changes: 1 addition & 2 deletions bindings/binding_core_wasm/Cargo.toml
Expand Up @@ -29,10 +29,9 @@ swc_core = { version = "0.43.3", features = [
] }
tracing = { version = "0.1.37", features = ["max_level_off"] }
wasm-bindgen = { version = "0.2.82", features = [
"serde-serialize",
"enable-interning",
] }
serde = { version = "1", features = ["derive"] }
serde-wasm-bindgen = "0.4.5"

[package.metadata.wasm-pack.profile.release]
wasm-opt = false
48 changes: 12 additions & 36 deletions bindings/binding_core_wasm/src/lib.rs
@@ -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
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
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 948e35b

@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: 948e35b Previous: 99934b0 Ratio
es/full/bugs-1 343550 ns/iter (± 20307) 353937 ns/iter (± 15478) 0.97
es/full/minify/libraries/antd 1918613605 ns/iter (± 27788602) 1868891374 ns/iter (± 34639916) 1.03
es/full/minify/libraries/d3 411847176 ns/iter (± 9000097) 421790779 ns/iter (± 8879696) 0.98
es/full/minify/libraries/echarts 1602234114 ns/iter (± 45659768) 1607486654 ns/iter (± 22612941) 1.00
es/full/minify/libraries/jquery 99042557 ns/iter (± 5193040) 112252600 ns/iter (± 5703459) 0.88
es/full/minify/libraries/lodash 117907487 ns/iter (± 5810567) 121615034 ns/iter (± 3380778) 0.97
es/full/minify/libraries/moment 59190908 ns/iter (± 2466949) 61102830 ns/iter (± 2091024) 0.97
es/full/minify/libraries/react 20728014 ns/iter (± 1778732) 20960742 ns/iter (± 607548) 0.99
es/full/minify/libraries/terser 297951302 ns/iter (± 7917389) 323519854 ns/iter (± 42117752) 0.92
es/full/minify/libraries/three 587759274 ns/iter (± 13857055) 569456979 ns/iter (± 42207651) 1.03
es/full/minify/libraries/typescript 3519955127 ns/iter (± 104605921) 3461879651 ns/iter (± 132493402) 1.02
es/full/minify/libraries/victory 852374252 ns/iter (± 24390741) 885543151 ns/iter (± 20195024) 0.96
es/full/minify/libraries/vue 160770327 ns/iter (± 7981538) 188916760 ns/iter (± 9096546) 0.85
es/full/codegen/es3 34925 ns/iter (± 1627) 34554 ns/iter (± 1230) 1.01
es/full/codegen/es5 34319 ns/iter (± 1683) 34282 ns/iter (± 1448) 1.00
es/full/codegen/es2015 35211 ns/iter (± 2154) 34593 ns/iter (± 1267) 1.02
es/full/codegen/es2016 34683 ns/iter (± 2157) 34499 ns/iter (± 1368) 1.01
es/full/codegen/es2017 34512 ns/iter (± 815) 34845 ns/iter (± 1992) 0.99
es/full/codegen/es2018 34392 ns/iter (± 905) 34526 ns/iter (± 2356) 1.00
es/full/codegen/es2019 34312 ns/iter (± 818) 34348 ns/iter (± 1842) 1.00
es/full/codegen/es2020 34279 ns/iter (± 1151) 34195 ns/iter (± 1428) 1.00
es/full/all/es3 194127133 ns/iter (± 11726037) 229084131 ns/iter (± 14912080) 0.85
es/full/all/es5 182127686 ns/iter (± 16083062) 180403040 ns/iter (± 11310020) 1.01
es/full/all/es2015 147043997 ns/iter (± 11211648) 148853027 ns/iter (± 7310353) 0.99
es/full/all/es2016 146351168 ns/iter (± 8371993) 149398134 ns/iter (± 16523408) 0.98
es/full/all/es2017 145674489 ns/iter (± 8588438) 163861520 ns/iter (± 14451991) 0.89
es/full/all/es2018 143979878 ns/iter (± 6720903) 156390161 ns/iter (± 15223961) 0.92
es/full/all/es2019 142589262 ns/iter (± 9629843) 149809909 ns/iter (± 12657867) 0.95
es/full/all/es2020 137907789 ns/iter (± 7413935) 139469119 ns/iter (± 10011078) 0.99
es/full/parser 715916 ns/iter (± 20625) 728932 ns/iter (± 28397) 0.98
es/full/base/fixer 26126 ns/iter (± 994) 26728 ns/iter (± 1184) 0.98
es/full/base/resolver_and_hygiene 91329 ns/iter (± 2580) 92334 ns/iter (± 4845) 0.99
serialization of ast node 216 ns/iter (± 4) 226 ns/iter (± 9) 0.96
serialization of serde 222 ns/iter (± 2) 233 ns/iter (± 4) 0.95

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

Please sign in to comment.