From 2d496f789c89b89892b285ec66898e5bf45e757c Mon Sep 17 00:00:00 2001 From: daxpedda Date: Mon, 12 Aug 2024 22:54:10 +0200 Subject: [PATCH] Use object URLs for inlined linked modules --- CHANGELOG.md | 3 ++ Cargo.toml | 1 + crates/backend/src/codegen.rs | 10 ++++--- crates/cli-support/src/js/mod.rs | 8 ++++-- crates/cli/tests/reference/raw.js | 48 +++++++++++++++---------------- src/lib.rs | 1 + 6 files changed, 40 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02e23a00a4f..1548e21c8af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -141,6 +141,9 @@ * Updated the WebGPU WebIDL to the current draft as of 2024-08-05. [#4062](https://github.com/rustwasm/wasm-bindgen/pull/4062) +* Use object URLs for linked modules without `--split-linked-modules`. + [#4067](https://github.com/rustwasm/wasm-bindgen/pull/4067) + ### Fixed * Copy port from headless test server when using `WASM_BINDGEN_TEST_ADDRESS`. diff --git a/Cargo.toml b/Cargo.toml index ca425da4c2f..282df895512 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,6 +43,7 @@ xxx_debug_only_print_generated_code = [ ] [dependencies] +once_cell = "1.12" wasm-bindgen-macro = { path = "crates/macro", version = "=0.2.92" } serde = { version = "1.0", optional = true } serde_json = { version = "1.0", optional = true } diff --git a/crates/backend/src/codegen.rs b/crates/backend/src/codegen.rs index d11894518cb..4121ada5fdc 100644 --- a/crates/backend/src/codegen.rs +++ b/crates/backend/src/codegen.rs @@ -190,16 +190,18 @@ impl TryToTokens for ast::LinkToModule { let link_function_name = self.0.link_function_name(0); let name = Ident::new(&link_function_name, Span::call_site()); let wasm_bindgen = &self.0.wasm_bindgen; - let abi_ret = quote! { #wasm_bindgen::convert::WasmRet<::Abi> }; + let abi_ret = quote! { #wasm_bindgen::convert::WasmRet<<#wasm_bindgen::__rt::alloc::string::String as #wasm_bindgen::convert::FromWasmAbi>::Abi> }; let extern_fn = extern_fn(&name, &[], &[], &[], abi_ret); (quote! { { #program #extern_fn - unsafe { - ::from_abi(#name().join()) - } + static __VAL: #wasm_bindgen::__rt::Lazy = #wasm_bindgen::__rt::Lazy::new(|| unsafe { + <#wasm_bindgen::__rt::alloc::string::String as #wasm_bindgen::convert::FromWasmAbi>::from_abi(#name().join()) + }); + + #wasm_bindgen::__rt::alloc::string::String::clone(&__VAL) } }) .to_tokens(tokens); diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index c95a2ae7892..5d655c1f707 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -3262,9 +3262,11 @@ impl<'a> Context<'a> { '`' | '\\' | '$' => escaped.extend(['\\', c]), _ => escaped.extend([c]), }); - Ok(format!( - "\"data:application/javascript,\" + encodeURIComponent(`{escaped}`)" - )) + prelude.push_str(&format!("const val = `{escaped}`;\n")); + Ok("typeof URL.createObjectURL === 'undefined' ? \ + \"data:application/javascript,\" + encodeURIComponent(val) : \ + URL.createObjectURL(new Blob([val], { type: \"text/javascript\" }))" + .to_owned()) } else { Err(anyhow!("wasm-bindgen needs to be invoked with `--split-linked-modules`, because \"{}\" cannot be embedded.\n\ See https://rustwasm.github.io/wasm-bindgen/reference/cli.html#--split-linked-modules for details.", path)) diff --git a/crates/cli/tests/reference/raw.js b/crates/cli/tests/reference/raw.js index 3432d150366..6b831debb89 100644 --- a/crates/cli/tests/reference/raw.js +++ b/crates/cli/tests/reference/raw.js @@ -6,26 +6,6 @@ export function __wbg_set_wasm(val) { } -const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder; - -let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true }); - -cachedTextDecoder.decode(); - -let cachedUint8ArrayMemory0 = null; - -function getUint8ArrayMemory0() { - if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) { - cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer); - } - return cachedUint8ArrayMemory0; -} - -function getStringFromWasm0(ptr, len) { - ptr = ptr >>> 0; - return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len)); -} - const heap = new Array(128).fill(undefined); heap.push(undefined, null, true, false); @@ -45,6 +25,26 @@ function takeObject(idx) { dropObject(idx); return ret; } + +const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder; + +let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true }); + +cachedTextDecoder.decode(); + +let cachedUint8ArrayMemory0 = null; + +function getUint8ArrayMemory0() { + if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) { + cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer); + } + return cachedUint8ArrayMemory0; +} + +function getStringFromWasm0(ptr, len) { + ptr = ptr >>> 0; + return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len)); +} /** * @param {number} test * @returns {number} @@ -110,11 +110,11 @@ export function __wbg_test2_39fe629b9aa739cf() { return addHeapObject(ret); }; -export function __wbindgen_throw(arg0, arg1) { - throw new Error(getStringFromWasm0(arg0, arg1)); -}; - export function __wbindgen_object_drop_ref(arg0) { takeObject(arg0); }; +export function __wbindgen_throw(arg0, arg1) { + throw new Error(getStringFromWasm0(arg0, arg1)); +}; + diff --git a/src/lib.rs b/src/lib.rs index 23cc9db2f0c..8c512d882a1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1518,6 +1518,7 @@ pub mod __rt { use alloc::alloc::{alloc, dealloc, realloc, Layout}; use alloc::boxed::Box; use alloc::rc::Rc; + pub use once_cell::sync::Lazy; #[macro_export] #[doc(hidden)]