Skip to content

Commit e1f7641

Browse files
committed
Implement shim_url
1 parent 3a939c4 commit e1f7641

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

crates/cli-support/src/intrinsic.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ intrinsics! {
255255
#[symbol = "__wbindgen_module"]
256256
#[signature = fn() -> Externref]
257257
Module,
258+
#[symbol = "__wbindgen_shim_url"]
259+
#[signature = fn() -> Externref]
260+
ShimUrl,
258261
#[symbol = "__wbindgen_function_table"]
259262
#[signature = fn() -> Externref]
260263
FunctionTable,

crates/cli-support/src/js/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3503,6 +3503,15 @@ impl<'a> Context<'a> {
35033503
format!("wasm.{}", self.export_name_of(memory))
35043504
}
35053505

3506+
Intrinsic::ShimUrl => {
3507+
assert_eq!(args.len(), 0);
3508+
match self.config.mode {
3509+
OutputMode::Web => format!("import.meta.url"),
3510+
OutputMode::NoModules { .. } => format!("script_src"),
3511+
_ => format!("null"),
3512+
}
3513+
}
3514+
35063515
Intrinsic::FunctionTable => {
35073516
assert_eq!(args.len(), 0);
35083517
let name = self.export_function_table()?;

examples/wasm-audio-worklet/src/dependent_module.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,14 @@ use js_sys::{Array, JsString};
22
use wasm_bindgen::prelude::*;
33
use web_sys::{Blob, BlobPropertyBag, Url};
44

5-
// This is a not-so-clean approach to get the current bindgen ES module URL
6-
// in Rust. This will fail at run time on bindgen targets not using ES modules.
7-
#[wasm_bindgen]
8-
extern "C" {
9-
#[wasm_bindgen]
10-
type ImportMeta;
11-
12-
#[wasm_bindgen(method, getter)]
13-
fn url(this: &ImportMeta) -> JsString;
14-
15-
#[wasm_bindgen(js_namespace = import, js_name = meta)]
16-
static IMPORT_META: ImportMeta;
17-
}
18-
195
pub fn on_the_fly(code: &str) -> Result<String, JsValue> {
206
// Generate the import of the bindgen ES module, assuming `--target web`,
217
// preluded by the TextEncoder/TextDecoder polyfill needed inside worklets.
228
let header = format!(
239
"import '{}';\n\
2410
import init, * as bindgen from '{}';\n\n",
2511
wasm_bindgen::link_to!(module = "/src/polyfill.js"),
26-
IMPORT_META.url(),
12+
wasm_bindgen::shim_url().expect("not using `--target web`"),
2713
);
2814

2915
Url::create_object_url_with_blob(&Blob::new_with_str_sequence_and_options(

src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,7 @@ externs! {
10841084

10851085
fn __wbindgen_memory() -> u32;
10861086
fn __wbindgen_module() -> u32;
1087+
fn __wbindgen_shim_url() -> u32;
10871088
fn __wbindgen_function_table() -> u32;
10881089
}
10891090
}
@@ -1363,6 +1364,15 @@ pub fn memory() -> JsValue {
13631364
unsafe { JsValue::_new(__wbindgen_memory()) }
13641365
}
13651366

1367+
/// Returns the URL to the script that instantiated the wasm module.
1368+
///
1369+
/// This will currently return `None` on every target except `web` and `no-modules`.
1370+
/// Additionally it will return `None` when used with the `no-modules` target but
1371+
/// not executed in a document.
1372+
pub fn shim_url() -> Option<String> {
1373+
unsafe { JsValue::_new(__wbindgen_shim_url()) }.as_string()
1374+
}
1375+
13661376
/// Returns a handle to this wasm instance's `WebAssembly.Table` which is the
13671377
/// indirect function table used by Rust
13681378
pub fn function_table() -> JsValue {

0 commit comments

Comments
 (0)