Skip to content

Commit

Permalink
Add an accessor for the function table
Browse files Browse the repository at this point in the history
This commit adds an intrinsics to the `wasm_bindgen` crate which
accesses the `WebAssembly.Table` which is the function table of the
module. Eventually the thinking is that a module would import its own
function table via native wasm functionality (via `anyref` and such),
but until that's implemented let's add a binding for it ourselves!

Closes rustwasm#1427
  • Loading branch information
alexcrichton committed Apr 8, 2019
1 parent 16745ed commit ff917e7
Show file tree
Hide file tree
Showing 4 changed files with 25 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
4 changes: 4 additions & 0 deletions tests/wasm/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ exports.debug_values = () => ([
() => (null),
new Set(),
]);

exports.assert_function_table = x => {
assert.ok(x instanceof WebAssembly.Table);
};
6 changes: 6 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);
}

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

#[wasm_bindgen_test]
fn function_table_is() {
assert_function_table(wasm_bindgen::function_table());
}

0 comments on commit ff917e7

Please sign in to comment.