diff --git a/crates/cli-support/src/intrinsic.rs b/crates/cli-support/src/intrinsic.rs index e8874d5e055..e2a3965c27c 100644 --- a/crates/cli-support/src/intrinsic.rs +++ b/crates/cli-support/src/intrinsic.rs @@ -255,6 +255,9 @@ intrinsics! { #[symbol = "__wbindgen_module"] #[signature = fn() -> Externref] Module, + #[symbol = "__wbindgen_shim_url"] + #[signature = fn() -> Externref] + ShimUrl, #[symbol = "__wbindgen_function_table"] #[signature = fn() -> Externref] FunctionTable, diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index b53997c1b1a..3f1a4b9708a 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -3545,6 +3545,15 @@ impl<'a> Context<'a> { format!("wasm.{}", self.export_name_of(memory)) } + Intrinsic::ShimUrl => { + assert_eq!(args.len(), 0); + match self.config.mode { + OutputMode::Web => format!("import.meta.url"), + OutputMode::NoModules { .. } => format!("script_src"), + _ => format!("null"), + } + } + Intrinsic::FunctionTable => { assert_eq!(args.len(), 0); let name = self.export_function_table()?; diff --git a/examples/wasm-audio-worklet/src/dependent_module.rs b/examples/wasm-audio-worklet/src/dependent_module.rs index a1706d46ccf..dfea1625af3 100644 --- a/examples/wasm-audio-worklet/src/dependent_module.rs +++ b/examples/wasm-audio-worklet/src/dependent_module.rs @@ -2,25 +2,11 @@ use js_sys::{Array, JsString}; use wasm_bindgen::prelude::*; use web_sys::{Blob, BlobPropertyBag, Url}; -// This is a not-so-clean approach to get the current bindgen ES module URL -// in Rust. This will fail at run time on bindgen targets not using ES modules. -#[wasm_bindgen] -extern "C" { - #[wasm_bindgen] - type ImportMeta; - - #[wasm_bindgen(method, getter)] - fn url(this: &ImportMeta) -> JsString; - - #[wasm_bindgen(js_namespace = import, js_name = meta)] - static IMPORT_META: ImportMeta; -} - pub fn on_the_fly(code: &str) -> Result { // Generate the import of the bindgen ES module, assuming `--target web`. let header = format!( "import init, * as bindgen from '{}';\n\n", - IMPORT_META.url(), + wasm_bindgen::shim_url().expect("not using `--target web`"), ); Url::create_object_url_with_blob(&Blob::new_with_str_sequence_and_options( diff --git a/src/lib.rs b/src/lib.rs index f8576ed4c3e..4da85700711 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1084,6 +1084,7 @@ externs! { fn __wbindgen_memory() -> u32; fn __wbindgen_module() -> u32; + fn __wbindgen_shim_url() -> u32; fn __wbindgen_function_table() -> u32; } } @@ -1363,6 +1364,15 @@ pub fn memory() -> JsValue { unsafe { JsValue::_new(__wbindgen_memory()) } } +/// Returns the URL to the script that instantiated the wasm module. +/// +/// This will currently return `None` on every target except `web` and `no-modules`. +/// Additionally it will return `None` when used with the `no-modules` target but +/// not executed in a document. +pub fn shim_url() -> Option { + unsafe { JsValue::_new(__wbindgen_shim_url()) }.as_string() +} + /// Returns a handle to this wasm instance's `WebAssembly.Table` which is the /// indirect function table used by Rust pub fn function_table() -> JsValue {