Skip to content

Commit 5b53485

Browse files
committed
Implement shim_url
1 parent 3d9fe53 commit 5b53485

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
@@ -3545,6 +3545,15 @@ impl<'a> Context<'a> {
35453545
format!("wasm.{}", self.export_name_of(memory))
35463546
}
35473547

3548+
Intrinsic::ShimUrl => {
3549+
assert_eq!(args.len(), 0);
3550+
match self.config.mode {
3551+
OutputMode::Web => format!("import.meta.url"),
3552+
OutputMode::NoModules { .. } => format!("script_src"),
3553+
_ => format!("null"),
3554+
}
3555+
}
3556+
35483557
Intrinsic::FunctionTable => {
35493558
assert_eq!(args.len(), 0);
35503559
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,25 +2,11 @@ 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
let header = format!(
228
"import init, * as bindgen from '{}';\n\n",
23-
IMPORT_META.url(),
9+
wasm_bindgen::shim_url().expect("not using `--target web`"),
2410
);
2511

2612
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)