Skip to content

Implement shim_url #3247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 of the generated JS shim.
///
/// 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