Skip to content

Commit b670ea4

Browse files
authored
Merge pull request #1431 from alexcrichton/function-table
Add an accessor for the function table
2 parents a9a2302 + 7e5e401 commit b670ea4

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,14 @@ impl<'a> Context<'a> {
783783
))
784784
})?;
785785

786+
self.bind("__wbindgen_function_table", &|me| {
787+
me.function_table_needed = true;
788+
Ok(format!(
789+
"function() {{ return {}; }}",
790+
me.add_heap_object("wasm.__wbg_function_table")
791+
))
792+
})?;
793+
786794
self.bind("__wbindgen_rethrow", &|me| {
787795
Ok(format!(
788796
"function(idx) {{ throw {}; }}",

src/lib.rs

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

518518
fn __wbindgen_memory() -> u32;
519519
fn __wbindgen_module() -> u32;
520+
fn __wbindgen_function_table() -> u32;
520521
}
521522
}
522523

@@ -736,6 +737,12 @@ pub fn memory() -> JsValue {
736737
unsafe { JsValue::_new(__wbindgen_memory()) }
737738
}
738739

740+
/// Returns a handle to this wasm instance's `WebAssembly.Table` which is the
741+
/// indirect function table used by Rust
742+
pub fn function_table() -> JsValue {
743+
unsafe { JsValue::_new(__wbindgen_function_table()) }
744+
}
745+
739746
#[doc(hidden)]
740747
pub mod __rt {
741748
use core::cell::{Cell, UnsafeCell};

tests/wasm/api.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,9 @@ exports.debug_values = () => ([
5555
() => (null),
5656
new Set(),
5757
]);
58+
59+
exports.assert_function_table = (x, i) => {
60+
const rawWasm = require('wasm-bindgen-test_bg.js');
61+
assert.ok(x instanceof WebAssembly.Table);
62+
assert.strictEqual(x.get(i), rawWasm.function_table_lookup);
63+
};

tests/wasm/api.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ extern "C" {
99
fn js_eq_works();
1010
fn assert_null(v: JsValue);
1111
fn debug_values() -> JsValue;
12+
fn assert_function_table(a: JsValue, b: usize);
1213
}
1314

1415
#[wasm_bindgen_test]
@@ -171,3 +172,14 @@ fn debug_output() {
171172
assert_eq!(format!("{:?}", test.unwrap()), expected);
172173
}
173174
}
175+
176+
#[wasm_bindgen_test]
177+
fn function_table_is() {
178+
assert_function_table(
179+
wasm_bindgen::function_table(),
180+
function_table_lookup as usize,
181+
);
182+
}
183+
184+
#[no_mangle]
185+
pub extern "C" fn function_table_lookup() {}

0 commit comments

Comments
 (0)