Skip to content

Add wasm_bindgen::script_url #3032

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 6 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 @@ -229,6 +229,9 @@ intrinsics! {
#[symbol = "__wbindgen_module"]
#[signature = fn() -> Externref]
Module,
#[symbol = "__wbindgen_script_url"]
#[signature = fn() -> String]
ScriptUrl,
#[symbol = "__wbindgen_function_table"]
#[signature = fn() -> Externref]
FunctionTable,
Expand Down
33 changes: 26 additions & 7 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,15 @@ impl<'a> Context<'a> {
// function.
OutputMode::NoModules { global } => {
js.push_str("const __exports = {};\n");
js.push_str("let script_src;\n");
js.push_str(
"\
if (typeof document === 'undefined') {
script_src = location.href;
} else {
script_src = document.currentScript.src;
}\n",
);
js.push_str("let wasm;\n");
init = self.gen_init(needs_manual_start, None)?;
footer.push_str(&format!("{} = Object.assign(init, __exports);\n", global));
Expand Down Expand Up @@ -701,13 +710,7 @@ impl<'a> Context<'a> {
),
OutputMode::NoModules { .. } => "\
if (typeof input === 'undefined') {
let src;
if (typeof document === 'undefined') {
src = location.href;
} else {
src = document.currentScript.src;
}
input = src.replace(/\\.js$/, '_bg.wasm');
input = script_src.replace(/\\.js$/, '_bg.wasm');
}"
.to_string(),
_ => "".to_string(),
Expand Down Expand Up @@ -3426,6 +3429,22 @@ impl<'a> Context<'a> {
format!("wasm.{}", self.export_name_of(memory))
}

Intrinsic::ScriptUrl => {
assert_eq!(args.len(), 0);
match self.config.mode {
OutputMode::Web
| OutputMode::Deno
| OutputMode::Node {
experimental_modules: true,
} => format!("import.meta.url"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to use format! here, you should use .to_owned() instead.

OutputMode::Node {
experimental_modules: false,
} => format!("__filename"),
OutputMode::NoModules { .. } => format!("script_src"),
OutputMode::Bundler { .. } => format!("__webpack_public_path__"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After a skim of the docs, I don't this returns the path of the current file; I think it returns the path at which the root of the webpack project is hosted. So, this won't work.

Webpack does seem to have some support for worker entrypoints, though: https://webpack.js.org/guides/web-workers/.

}
}

Intrinsic::FunctionTable => {
assert_eq!(args.len(), 0);
let name = self.export_function_table()?;
Expand Down
16 changes: 9 additions & 7 deletions crates/cli/tests/wasm-bindgen/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,19 @@ fn default_module_path_target_no_modules() {
cmd.assert().success();
let contents =
fs::read_to_string(out_dir.join("default_module_path_target_no_modules.js")).unwrap();
assert!(contents.contains(
"\
if (typeof document === 'undefined') {
script_src = location.href;
} else {
script_src = document.currentScript.src;
}",
));
assert!(contents.contains(
"\
async function init(input) {
if (typeof input === 'undefined') {
let src;
if (typeof document === 'undefined') {
src = location.href;
} else {
src = document.currentScript.src;
}
input = src.replace(/\\.js$/, '_bg.wasm');
input = script_src.replace(/\\.js$/, '_bg.wasm');
}",
));
}
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::script_url(),
);

Url::create_object_url_with_blob(&Blob::new_with_str_sequence_and_options(
Expand Down
15 changes: 15 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,7 @@ externs! {

fn __wbindgen_memory() -> u32;
fn __wbindgen_module() -> u32;
fn __wbindgen_script_url(ret: *mut [usize; 2]) -> ();
fn __wbindgen_function_table() -> u32;
}
}
Expand Down Expand Up @@ -1251,6 +1252,20 @@ pub fn memory() -> JsValue {
unsafe { JsValue::_new(__wbindgen_memory()) }
}

/// Returns the URL to the script that instantiated the wasm module.
///
/// On some wasm-bindgen targets, this script is a module or a bundle.
/// Also, the returned URL is relative on some wasm-bindgen targets.
#[cfg(feature = "std")]
pub fn script_url() -> String {
unsafe {
let mut ret = [0; 2];
__wbindgen_script_url(&mut ret);
let data = Vec::from_raw_parts(ret[0] as *mut u8, ret[1], ret[1]);
String::from_utf8_unchecked(data)
}
}

/// 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