Skip to content

Commit

Permalink
Revert "refactor(bindings): Deprecate JsValue::*_serde (#6436)"
Browse files Browse the repository at this point in the history
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

0 comments on commit 0479ce6

Please sign in to comment.