Skip to content

Commit

Permalink
Implement shim_url
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Feb 27, 2023
1 parent 3d9fe53 commit 5b53485
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
3 changes: 3 additions & 0 deletions crates/cli-support/src/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand Down
16 changes: 1 addition & 15 deletions examples/wasm-audio-worklet/src/dependent_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, JsValue> {
// 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(
Expand Down
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,7 @@ externs! {

fn __wbindgen_memory() -> u32;
fn __wbindgen_module() -> u32;
fn __wbindgen_shim_url() -> u32;
fn __wbindgen_function_table() -> u32;
}
}
Expand Down Expand Up @@ -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<String> {
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 {
Expand Down

0 comments on commit 5b53485

Please sign in to comment.