Skip to content

Commit b8e3cd5

Browse files
committed
Expose WebAssembly.Instance
1 parent d090e94 commit b8e3cd5

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

crates/cli-support/src/intrinsic.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ intrinsics! {
252252
#[symbol = "__wbindgen_memory"]
253253
#[signature = fn() -> Externref]
254254
Memory,
255+
#[symbol = "__wbindgen_instance"]
256+
#[signature = fn() -> Externref]
257+
Instance,
255258
#[symbol = "__wbindgen_module"]
256259
#[signature = fn() -> Externref]
257260
Module,

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ impl<'a> Context<'a> {
274274
shim.push_str(
275275
"
276276
const wasmModule = new WebAssembly.Module(bytes);
277-
const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
278-
wasm = wasmInstance.exports;
277+
instance = new WebAssembly.Instance(wasmModule, imports);
278+
wasm = instance.exports;
279279
module.exports.__wasm = wasm;
280280
",
281281
);
@@ -345,8 +345,8 @@ impl<'a> Context<'a> {
345345
throw new Error(`Unsupported protocol: ${{wasm_url.protocol}}`);
346346
}}
347347
348-
const wasmInstance = (await WebAssembly.instantiate(wasmCode, imports)).instance;
349-
const wasm = wasmInstance.exports;",
348+
const instance = (await WebAssembly.instantiate(wasmCode, imports)).instance;
349+
const wasm = instance.exports;",
350350
module_name = module_name
351351
)
352352
}
@@ -385,6 +385,7 @@ impl<'a> Context<'a> {
385385
script_src = new URL(document.currentScript.src, location.href).toString();
386386
}\n",
387387
);
388+
js.push_str("let instance;\n");
388389
js.push_str("let wasm;\n");
389390
init = self.gen_init(needs_manual_start, None)?;
390391
footer.push_str(&format!(
@@ -400,6 +401,7 @@ impl<'a> Context<'a> {
400401
} => {
401402
js.push_str(&self.generate_node_imports());
402403

404+
js.push_str("let instance;\n");
403405
js.push_str("let wasm;\n");
404406

405407
for (id, js) in crate::sorted_iter(&self.wasm_import_definitions) {
@@ -464,6 +466,7 @@ impl<'a> Context<'a> {
464466

465467
self.imports_post.push_str(
466468
"\
469+
let instance;
467470
let wasm;
468471
export function __wbg_set_wasm(val) {
469472
wasm = val;
@@ -481,6 +484,7 @@ impl<'a> Context<'a> {
481484
// expose the same initialization function as `--target no-modules`
482485
// as the default export of the module.
483486
OutputMode::Web => {
487+
self.imports_post.push_str("let instance;\n");
484488
self.imports_post.push_str("let wasm;\n");
485489
init = self.gen_init(needs_manual_start, Some(&mut imports))?;
486490
footer.push_str("export { initSync }\n");
@@ -860,7 +864,7 @@ impl<'a> Context<'a> {
860864
module = new WebAssembly.Module(module);
861865
}}
862866
863-
const instance = new WebAssembly.Instance(module, imports);
867+
instance = new WebAssembly.Instance(module, imports);
864868
865869
return finalizeInit(instance, module);
866870
}}
@@ -3498,6 +3502,11 @@ impl<'a> Context<'a> {
34983502
format!("init.__wbindgen_wasm_module")
34993503
}
35003504

3505+
Intrinsic::Instance => {
3506+
assert_eq!(args.len(), 0);
3507+
"wasm".to_string()
3508+
}
3509+
35013510
Intrinsic::Memory => {
35023511
assert_eq!(args.len(), 0);
35033512
let mut memories = self.module.memories.iter();

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,7 @@ externs! {
10821082

10831083
fn __wbindgen_not(idx: u32) -> u32;
10841084

1085+
fn __wbindgen_instance() -> u32;
10851086
fn __wbindgen_memory() -> u32;
10861087
fn __wbindgen_module() -> u32;
10871088
fn __wbindgen_function_table() -> u32;
@@ -1358,6 +1359,11 @@ pub fn module() -> JsValue {
13581359
unsafe { JsValue::_new(__wbindgen_module()) }
13591360
}
13601361

1362+
/// Returns a handle to this wasm instance's `WebAssembly.Instance`
1363+
pub fn instance() -> JsValue {
1364+
unsafe { JsValue::_new(__wbindgen_instance()) }
1365+
}
1366+
13611367
/// Returns a handle to this wasm instance's `WebAssembly.Memory`
13621368
pub fn memory() -> JsValue {
13631369
unsafe { JsValue::_new(__wbindgen_memory()) }

0 commit comments

Comments
 (0)