Skip to content

Commit e845f51

Browse files
authored
Use object URLs for inlined linked modules (#4067)
1 parent 5d45bd6 commit e845f51

File tree

6 files changed

+40
-31
lines changed

6 files changed

+40
-31
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@
141141
* Updated the WebGPU WebIDL to the current draft as of 2024-08-05.
142142
[#4062](https://github.com/rustwasm/wasm-bindgen/pull/4062)
143143

144+
* Use object URLs for linked modules without `--split-linked-modules`.
145+
[#4067](https://github.com/rustwasm/wasm-bindgen/pull/4067)
146+
144147
### Fixed
145148

146149
* Copy port from headless test server when using `WASM_BINDGEN_TEST_ADDRESS`.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ xxx_debug_only_print_generated_code = [
4343
]
4444

4545
[dependencies]
46+
once_cell = "1.12"
4647
wasm-bindgen-macro = { path = "crates/macro", version = "=0.2.92" }
4748
serde = { version = "1.0", optional = true }
4849
serde_json = { version = "1.0", optional = true }

crates/backend/src/codegen.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,16 +190,18 @@ impl TryToTokens for ast::LinkToModule {
190190
let link_function_name = self.0.link_function_name(0);
191191
let name = Ident::new(&link_function_name, Span::call_site());
192192
let wasm_bindgen = &self.0.wasm_bindgen;
193-
let abi_ret = quote! { #wasm_bindgen::convert::WasmRet<<std::string::String as #wasm_bindgen::convert::FromWasmAbi>::Abi> };
193+
let abi_ret = quote! { #wasm_bindgen::convert::WasmRet<<#wasm_bindgen::__rt::alloc::string::String as #wasm_bindgen::convert::FromWasmAbi>::Abi> };
194194
let extern_fn = extern_fn(&name, &[], &[], &[], abi_ret);
195195
(quote! {
196196
{
197197
#program
198198
#extern_fn
199199

200-
unsafe {
201-
<std::string::String as #wasm_bindgen::convert::FromWasmAbi>::from_abi(#name().join())
202-
}
200+
static __VAL: #wasm_bindgen::__rt::Lazy<String> = #wasm_bindgen::__rt::Lazy::new(|| unsafe {
201+
<#wasm_bindgen::__rt::alloc::string::String as #wasm_bindgen::convert::FromWasmAbi>::from_abi(#name().join())
202+
});
203+
204+
#wasm_bindgen::__rt::alloc::string::String::clone(&__VAL)
203205
}
204206
})
205207
.to_tokens(tokens);

crates/cli-support/src/js/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3262,9 +3262,11 @@ impl<'a> Context<'a> {
32623262
'`' | '\\' | '$' => escaped.extend(['\\', c]),
32633263
_ => escaped.extend([c]),
32643264
});
3265-
Ok(format!(
3266-
"\"data:application/javascript,\" + encodeURIComponent(`{escaped}`)"
3267-
))
3265+
prelude.push_str(&format!("const val = `{escaped}`;\n"));
3266+
Ok("typeof URL.createObjectURL === 'undefined' ? \
3267+
\"data:application/javascript,\" + encodeURIComponent(val) : \
3268+
URL.createObjectURL(new Blob([val], { type: \"text/javascript\" }))"
3269+
.to_owned())
32683270
} else {
32693271
Err(anyhow!("wasm-bindgen needs to be invoked with `--split-linked-modules`, because \"{}\" cannot be embedded.\n\
32703272
See https://rustwasm.github.io/wasm-bindgen/reference/cli.html#--split-linked-modules for details.", path))

crates/cli/tests/reference/raw.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,6 @@ export function __wbg_set_wasm(val) {
66
}
77

88

9-
const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;
10-
11-
let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });
12-
13-
cachedTextDecoder.decode();
14-
15-
let cachedUint8ArrayMemory0 = null;
16-
17-
function getUint8ArrayMemory0() {
18-
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
19-
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
20-
}
21-
return cachedUint8ArrayMemory0;
22-
}
23-
24-
function getStringFromWasm0(ptr, len) {
25-
ptr = ptr >>> 0;
26-
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
27-
}
28-
299
const heap = new Array(128).fill(undefined);
3010

3111
heap.push(undefined, null, true, false);
@@ -45,6 +25,26 @@ function takeObject(idx) {
4525
dropObject(idx);
4626
return ret;
4727
}
28+
29+
const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;
30+
31+
let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });
32+
33+
cachedTextDecoder.decode();
34+
35+
let cachedUint8ArrayMemory0 = null;
36+
37+
function getUint8ArrayMemory0() {
38+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
39+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
40+
}
41+
return cachedUint8ArrayMemory0;
42+
}
43+
44+
function getStringFromWasm0(ptr, len) {
45+
ptr = ptr >>> 0;
46+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
47+
}
4848
/**
4949
* @param {number} test
5050
* @returns {number}
@@ -110,11 +110,11 @@ export function __wbg_test2_39fe629b9aa739cf() {
110110
return addHeapObject(ret);
111111
};
112112

113-
export function __wbindgen_throw(arg0, arg1) {
114-
throw new Error(getStringFromWasm0(arg0, arg1));
115-
};
116-
117113
export function __wbindgen_object_drop_ref(arg0) {
118114
takeObject(arg0);
119115
};
120116

117+
export function __wbindgen_throw(arg0, arg1) {
118+
throw new Error(getStringFromWasm0(arg0, arg1));
119+
};
120+

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,7 @@ pub mod __rt {
15181518
use alloc::alloc::{alloc, dealloc, realloc, Layout};
15191519
use alloc::boxed::Box;
15201520
use alloc::rc::Rc;
1521+
pub use once_cell::sync::Lazy;
15211522

15221523
#[macro_export]
15231524
#[doc(hidden)]

0 commit comments

Comments
 (0)