From 97aa7f6e9ae843cb5ab2138f70ebf4fae5b5c6b1 Mon Sep 17 00:00:00 2001 From: dAxpeDDa Date: Sat, 26 Nov 2022 18:11:54 +0100 Subject: [PATCH 1/3] Implement `shim_is_module` --- crates/cli-support/src/intrinsic.rs | 3 +++ crates/cli-support/src/js/mod.rs | 4 ++++ src/lib.rs | 7 +++++++ 3 files changed, 14 insertions(+) diff --git a/crates/cli-support/src/intrinsic.rs b/crates/cli-support/src/intrinsic.rs index e8874d5e055..6058731481a 100644 --- a/crates/cli-support/src/intrinsic.rs +++ b/crates/cli-support/src/intrinsic.rs @@ -276,5 +276,8 @@ intrinsics! { #[symbol = "__wbindgen_init_externref_table"] #[signature = fn() -> Unit] InitExternrefTable, + #[symbol = "__wbindgen_shim_is_module"] + #[signature = fn() -> Boolean] + ShimIsModule, } } diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 6850b91169f..ff9e0592e1f 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -3551,6 +3551,10 @@ impl<'a> Context<'a> { } base } + + Intrinsic::ShimIsModule => { + format!("{}", !self.config.mode.no_modules()) + } }; Ok(expr) } diff --git a/src/lib.rs b/src/lib.rs index 50920321b37..831af75a176 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1083,6 +1083,8 @@ externs! { fn __wbindgen_memory() -> u32; fn __wbindgen_module() -> u32; fn __wbindgen_function_table() -> u32; + + fn __wbindgen_shim_is_module() -> u32; } } @@ -1367,6 +1369,11 @@ pub fn function_table() -> JsValue { unsafe { JsValue::_new(__wbindgen_function_table()) } } +/// Returns if the generated JS shim is a ES module. +pub fn shim_is_module() -> bool { + (unsafe { __wbindgen_shim_is_module() }) != 0 +} + #[doc(hidden)] pub mod __rt { use crate::JsValue; From c09bb3beccc881c0fd92abbc579a82633506deba Mon Sep 17 00:00:00 2001 From: dAxpeDDa Date: Thu, 19 Jan 2023 17:03:34 +0100 Subject: [PATCH 2/3] Rewrite to `ShimFormat` --- crates/cli-support/src/intrinsic.rs | 13 +++++++++--- crates/cli-support/src/js/mod.rs | 13 +++++++++--- src/lib.rs | 32 +++++++++++++++++++++++++---- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/crates/cli-support/src/intrinsic.rs b/crates/cli-support/src/intrinsic.rs index 6058731481a..375cde92673 100644 --- a/crates/cli-support/src/intrinsic.rs +++ b/crates/cli-support/src/intrinsic.rs @@ -84,6 +84,10 @@ fn opt_i64() -> Descriptor { Descriptor::Option(Box::new(Descriptor::I64)) } +fn opt_u32() -> Descriptor { + Descriptor::Option(Box::new(Descriptor::U32)) +} + fn slice(contents: Descriptor) -> Descriptor { Descriptor::Ref(Box::new(Descriptor::Slice(Box::new(contents)))) } @@ -276,8 +280,11 @@ intrinsics! { #[symbol = "__wbindgen_init_externref_table"] #[signature = fn() -> Unit] InitExternrefTable, - #[symbol = "__wbindgen_shim_is_module"] - #[signature = fn() -> Boolean] - ShimIsModule, + #[symbol = "__wbindgen_shim_format_variant"] + #[signature = fn() -> opt_u32()] + ShimFormatVariant, + #[symbol = "__wbindgen_shim_format_string"] + #[signature = fn() -> String] + ShimFormatString, } } diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index ff9e0592e1f..0a32b1c2e3b 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -3552,9 +3552,16 @@ impl<'a> Context<'a> { base } - Intrinsic::ShimIsModule => { - format!("{}", !self.config.mode.no_modules()) - } + Intrinsic::ShimFormatVariant => match &self.config.mode { + OutputMode::Web => 0.to_string(), + OutputMode::NoModules { .. } => 1.to_string(), + _ => String::from("null"), + }, + + Intrinsic::ShimFormatString => match &self.config.mode { + OutputMode::NoModules { global } => format!("'{global}'"), + _ => String::from("undefined"), + }, }; Ok(expr) } diff --git a/src/lib.rs b/src/lib.rs index 831af75a176..65b8adf2a9b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1084,7 +1084,8 @@ externs! { fn __wbindgen_module() -> u32; fn __wbindgen_function_table() -> u32; - fn __wbindgen_shim_is_module() -> u32; + fn __wbindgen_shim_format_variant() -> WasmOption; + fn __wbindgen_shim_format_string() -> WasmSlice; } } @@ -1369,9 +1370,32 @@ pub fn function_table() -> JsValue { unsafe { JsValue::_new(__wbindgen_function_table()) } } -/// Returns if the generated JS shim is a ES module. -pub fn shim_is_module() -> bool { - (unsafe { __wbindgen_shim_is_module() }) != 0 +/// Represents the format of the shim. +#[derive(Clone, Debug)] +#[non_exhaustive] +pub enum ShimFormat { + /// The shim is an ES module. + EsModule, + /// The shim is regular JavaScript. + NoModules { + /// The name of the global variable. + global_name: String, + }, +} + +/// Returns the format of the generated JS shim. +/// +/// This will currently return `None` on every target except `web` and `no-modules`. +pub fn shim_format() -> Option { + match unsafe { __wbindgen_shim_format_variant() } { + WasmOption::Some(0) => Some(ShimFormat::EsModule), + WasmOption::Some(1) => { + let global_name = unsafe { String::from_abi(__wbindgen_shim_format_string()) }; + Some(ShimFormat::NoModules { global_name }) + } + WasmOption::Some(_) => unreachable!("unexpected variant identifier"), + WasmOption::None => None, + } } #[doc(hidden)] From 3ca7308847ab3f1d5bf8c8663bfbdd54f65d5ce9 Mon Sep 17 00:00:00 2001 From: dAxpeDDa Date: Wed, 15 Mar 2023 01:01:34 +0100 Subject: [PATCH 3/3] Address review Co-Authored-By: Liam Murphy --- src/lib.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 65b8adf2a9b..afa5cc1963a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1376,9 +1376,15 @@ pub fn function_table() -> JsValue { pub enum ShimFormat { /// The shim is an ES module. EsModule, - /// The shim is regular JavaScript. + /// The shim doesn't use any module system, and instead exposes a global variable + /// with its API. + /// + /// It has to be imported as a script to work properly, as the global variable is + /// created by simply declaring a top-level variable, which only works in scripts. NoModules { - /// The name of the global variable. + /// The name of the global variable the shim exposes. + /// + /// By default, this is `"wasm_bindgen"`. global_name: String, }, }