Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(bindings/wasm): deprecate jsvalue::*_serde #6462

Merged
merged 8 commits into from Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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" }