Skip to content

Commit

Permalink
Merge pull request #1431 from alexcrichton/function-table
Browse files Browse the repository at this point in the history
Add an accessor for the function table
  • Loading branch information
alexcrichton authored Apr 8, 2019
2 parents a9a2302 + 7e5e401 commit b670ea4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
8 changes: 8 additions & 0 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,14 @@ impl<'a> Context<'a> {
))
})?;

self.bind("__wbindgen_function_table", &|me| {
me.function_table_needed = true;
Ok(format!(
"function() {{ return {}; }}",
me.add_heap_object("wasm.__wbg_function_table")
))
})?;

self.bind("__wbindgen_rethrow", &|me| {
Ok(format!(
"function(idx) {{ throw {}; }}",
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ externs! {

fn __wbindgen_memory() -> u32;
fn __wbindgen_module() -> u32;
fn __wbindgen_function_table() -> u32;
}
}

Expand Down Expand Up @@ -736,6 +737,12 @@ pub fn memory() -> JsValue {
unsafe { JsValue::_new(__wbindgen_memory()) }
}

/// Returns a handle to this wasm instance's `WebAssembly.Table` which is the
/// indirect function table used by Rust
pub fn function_table() -> JsValue {
unsafe { JsValue::_new(__wbindgen_function_table()) }
}

#[doc(hidden)]
pub mod __rt {
use core::cell::{Cell, UnsafeCell};
Expand Down
6 changes: 6 additions & 0 deletions tests/wasm/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,9 @@ exports.debug_values = () => ([
() => (null),
new Set(),
]);

exports.assert_function_table = (x, i) => {
const rawWasm = require('wasm-bindgen-test_bg.js');
assert.ok(x instanceof WebAssembly.Table);
assert.strictEqual(x.get(i), rawWasm.function_table_lookup);
};
12 changes: 12 additions & 0 deletions tests/wasm/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern "C" {
fn js_eq_works();
fn assert_null(v: JsValue);
fn debug_values() -> JsValue;
fn assert_function_table(a: JsValue, b: usize);
}

#[wasm_bindgen_test]
Expand Down Expand Up @@ -171,3 +172,14 @@ fn debug_output() {
assert_eq!(format!("{:?}", test.unwrap()), expected);
}
}

#[wasm_bindgen_test]
fn function_table_is() {
assert_function_table(
wasm_bindgen::function_table(),
function_table_lookup as usize,
);
}

#[no_mangle]
pub extern "C" fn function_table_lookup() {}

0 comments on commit b670ea4

Please sign in to comment.