Skip to content

Commit 948e35b

Browse files
authoredNov 15, 2022
fix(bindings): Revert #6436 (#6444)
1 parent 6cc71e9 commit 948e35b

File tree

6 files changed

+41
-87
lines changed

6 files changed

+41
-87
lines changed
 

‎Cargo.lock

+12-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎bindings/Cargo.lock

-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎bindings/binding_core_wasm/Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ swc_core = { version = "0.43.3", features = [
2929
] }
3030
tracing = { version = "0.1.37", features = ["max_level_off"] }
3131
wasm-bindgen = { version = "0.2.82", features = [
32+
"serde-serialize",
3233
"enable-interning",
3334
] }
34-
serde = { version = "1", features = ["derive"] }
35-
serde-wasm-bindgen = "0.4.5"
3635

3736
[package.metadata.wasm-pack.profile.release]
3837
wasm-opt = false

‎bindings/binding_core_wasm/src/lib.rs

+12-36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use anyhow::Error;
2-
use serde::Serialize;
3-
use serde_wasm_bindgen::Serializer;
42
use swc_core::{
53
base::HandlerOpts,
64
binding_macros::wasm::{
@@ -23,12 +21,6 @@ use swc_core::{
2321
use wasm_bindgen::{prelude::*, JsCast};
2422
mod types;
2523

26-
// A serializer with options to provide backward compat for the input / output
27-
// from the bindgen generated swc interfaces.
28-
const COMPAT_SERIALIZER: Serializer = Serializer::new()
29-
.serialize_maps_as_objects(true)
30-
.serialize_missing_as_null(true);
31-
3224
/// Custom interface definitions for the @swc/wasm's public interface instead of
3325
/// auto generated one, which is not reflecting most of types in detail.
3426
#[wasm_bindgen(typescript_custom_section)]
@@ -89,16 +81,12 @@ pub fn minify_sync(s: JsString, opts: JsValue) -> Result<JsValue, JsValue> {
8981
let opts = if opts.is_null() || opts.is_undefined() {
9082
Default::default()
9183
} else {
92-
serde_wasm_bindgen::from_value(opts)
93-
.map_err(|e| anyhow::anyhow!("failed to parse options: {}", e))?
84+
anyhow::Context::context(opts.into_serde(), "failed to parse options")?
9485
};
9586
let fm = c.cm.new_source_file(FileName::Anon, s.into());
9687
let program =
9788
anyhow::Context::context(c.minify(fm, handler, &opts), "failed to minify file")?;
98-
99-
program
100-
.serialize(&COMPAT_SERIALIZER)
101-
.map_err(|e| anyhow::anyhow!("failed to serialize program: {}", e))
89+
anyhow::Context::context(JsValue::from_serde(&program), "failed to serialize json")
10290
})
10391
})
10492
.map_err(|e| convert_err(e, None))
@@ -118,8 +106,7 @@ pub fn parse_sync(s: JsString, opts: JsValue) -> Result<JsValue, JsValue> {
118106
let opts: ParseOptions = if opts.is_null() || opts.is_undefined() {
119107
Default::default()
120108
} else {
121-
serde_wasm_bindgen::from_value(opts)
122-
.map_err(|e| anyhow::anyhow!("failed to parse options: {}", e))?
109+
anyhow::Context::context(opts.into_serde(), "failed to parse options")?
123110
};
124111
let fm = c.cm.new_source_file(FileName::Anon, s.into());
125112
let cmts = c.comments().clone();
@@ -146,9 +133,7 @@ pub fn parse_sync(s: JsString, opts: JsValue) -> Result<JsValue, JsValue> {
146133
opts.syntax.typescript(),
147134
));
148135

149-
program
150-
.serialize(&COMPAT_SERIALIZER)
151-
.map_err(|e| anyhow::anyhow!("failed to serialize program: {}", e))
136+
anyhow::Context::context(JsValue::from_serde(&program), "failed to serialize json")
152137
})
153138
})
154139
})
@@ -175,9 +160,9 @@ pub fn transform_sync(
175160
let opts: Options = if opts.is_null() || opts.is_undefined() {
176161
Default::default()
177162
} else {
178-
serde_wasm_bindgen::from_value(opts)?
163+
anyhow::Context::context(opts.into_serde(), "failed to parse options")
164+
.map_err(|e| convert_err(e, None))?
179165
};
180-
181166
let error_format = opts.experimental.error_format.unwrap_or_default();
182167
try_with_handler(c.cm.clone(), Default::default(), |handler| {
183168
c.run(|| {
@@ -208,13 +193,9 @@ pub fn transform_sync(
208193
"failed to process js file",
209194
)?
210195
}
211-
Err(v) => {
212-
c.process_js(handler, serde_wasm_bindgen::from_value(v).expect("Should able to deserialize into program"), &opts)?
213-
}
196+
Err(v) => unsafe { c.process_js(handler, v.into_serde().expect(""), &opts)? },
214197
};
215-
216-
out.serialize(&COMPAT_SERIALIZER)
217-
.map_err(|e| anyhow::anyhow!("failed to serialize transform result: {}", e))
198+
anyhow::Context::context(JsValue::from_serde(&out), "failed to serialize json")
218199
})
219200
})
220201
.map_err(|e| convert_err(e, Some(error_format)))
@@ -237,13 +218,10 @@ pub fn print_sync(s: JsValue, opts: JsValue) -> Result<JsValue, JsValue> {
237218
let opts: Options = if opts.is_null() || opts.is_undefined() {
238219
Default::default()
239220
} else {
240-
serde_wasm_bindgen::from_value(opts)
241-
.map_err(|e| anyhow::anyhow!("failed to parse options: {}", e))?
221+
anyhow::Context::context(opts.into_serde(), "failed to parse options")?
242222
};
243-
244-
let program: Program = serde_wasm_bindgen::from_value(s)
245-
.map_err(|e| anyhow::anyhow!("failed to deserialize program: {}", e))?;
246-
223+
let program: Program =
224+
anyhow::Context::context(s.into_serde(), "failed to deserialize program")?;
247225
let s = anyhow::Context::context(
248226
c.print(
249227
&program,
@@ -263,9 +241,7 @@ pub fn print_sync(s: JsValue, opts: JsValue) -> Result<JsValue, JsValue> {
263241
),
264242
"failed to print code",
265243
)?;
266-
267-
serde_wasm_bindgen::to_value(&s)
268-
.map_err(|e| anyhow::anyhow!("failed to serialize json: {}", e))
244+
anyhow::Context::context(JsValue::from_serde(&s), "failed to serialize json")
269245
})
270246
})
271247
.map_err(|e| convert_err(e, None))

‎crates/binding_macros/Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ anyhow = { optional = true, version = "1.0.58" }
4040
console_error_panic_hook = { optional = true, version = "0.1.7" }
4141
js-sys = { optional = true, version = "0.3.59" }
4242
once_cell = { optional = true, version = "1.13.0" }
43-
serde = { optional = true, version = "1", features = ["derive"] }
4443
wasm-bindgen = { optional = true, version = "0.2.82", features = [
44+
"serde-serialize",
4545
"enable-interning",
4646
] }
4747
wasm-bindgen-futures = { optional = true, version = "0.4.32" }
48-
serde-wasm-bindgen = { optional = true, version = "0.4.5" }

‎crates/binding_macros/src/wasm.rs

+15-31
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ use anyhow::Error;
66
#[doc(hidden)]
77
pub use js_sys;
88
use once_cell::sync::Lazy;
9-
use serde::Serialize;
10-
use serde_wasm_bindgen::Serializer;
119
use swc::{config::ErrorFormat, Compiler};
1210
#[doc(hidden)]
1311
pub use swc::{
@@ -26,12 +24,6 @@ pub use wasm_bindgen::{JsCast, JsValue};
2624
#[doc(hidden)]
2725
pub use wasm_bindgen_futures::future_to_promise;
2826

29-
// A serializer with options to provide backward compat for the input / output
30-
// from the bindgen generated swc interfaces.
31-
const COMPAT_SERIALIZER: Serializer = Serializer::new()
32-
.serialize_maps_as_objects(true)
33-
.serialize_missing_as_null(true);
34-
3527
/// Get global sourcemap
3628
pub fn compiler() -> Arc<Compiler> {
3729
console_error_panic_hook::set_once();
@@ -74,16 +66,13 @@ macro_rules! build_minify_sync {
7466
let opts = if opts.is_null() || opts.is_undefined() {
7567
Default::default()
7668
} else {
77-
$crate::wasm::serde_wasm_bindgen::from_value(opts)
78-
.map_err(|e| $crate::wasm::anyhow::anyhow!("failed to parse options: {}", e))?
69+
$crate::wasm::anyhow::Context::context(opts.into_serde(), "failed to parse options")?
7970
};
8071

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

84-
program
85-
.serialize(&COMPAT_SERIALIZER)
86-
.map_err(|e| $crate::wasm::anyhow::anyhow!("failed to serialize program: {}", e))
75+
$crate::wasm::anyhow::Context::context($crate::wasm::JsValue::from_serde(&program), "failed to serialize json")
8776
})
8877
},
8978
)
@@ -126,8 +115,7 @@ macro_rules! build_parse_sync {
126115
let opts: $crate::wasm::ParseOptions = if opts.is_null() || opts.is_undefined() {
127116
Default::default()
128117
} else {
129-
$crate::wasm::serde_wasm_bindgen::from_value(opts)
130-
.map_err(|e| $crate::wasm::anyhow::anyhow!("failed to parse options: {}", e))?
118+
$crate::wasm::anyhow::Context::context(opts.into_serde(), "failed to parse options")?
131119
};
132120

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

155-
program
156-
.serialize(&COMPAT_SERIALIZER)
157-
.map_err(|e| $crate::wasm::anyhow::anyhow!("failed to serialize program: {}", e))
143+
$crate::wasm::anyhow::Context::context($crate::wasm::JsValue::from_serde(&program), "failed to serialize json")
158144
})
159145
},
160146
)
@@ -197,12 +183,10 @@ macro_rules! build_print_sync {
197183
let opts: $crate::wasm::Options = if opts.is_null() || opts.is_undefined() {
198184
Default::default()
199185
} else {
200-
$crate::wasm::serde_wasm_bindgen::from_value(opts)
201-
.map_err(|e| $crate::wasm::anyhow::anyhow!("failed to parse options: {}", e))?
186+
$crate::wasm::anyhow::Context::context(opts.into_serde(), "failed to parse options")?
202187
};
203188

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

224-
program
225-
.serialize(&COMPAT_SERIALIZER)
226-
.map_err(|e| $crate::wasm::anyhow::anyhow!("failed to serialize program: {}", e))
208+
$crate::wasm::anyhow::Context::context(JsValue::from_serde(&s), "failed to serialize json")
227209
})
228210
},
229211
)
@@ -299,7 +281,9 @@ macro_rules! build_transform_sync {
299281
buffer
300282
};
301283

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

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

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

354-
out
355-
.serialize(&COMPAT_SERIALIZER)
356-
.map_err(|e| $crate::wasm::anyhow::anyhow!("failed to serialize transform result: {}", e))
339+
$crate::wasm::anyhow::Context::context($crate::wasm::JsValue::from_serde(&out),
340+
"failed to serialize json")
357341
})
358342
},
359343
)

1 commit comments

Comments
 (1)

github-actions[bot] commented on Nov 15, 2022

@github-actions[bot]

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.