Skip to content

Commit

Permalink
js-sys: Add exports getter to WebAssembly.Instance
Browse files Browse the repository at this point in the history
Part of rustwasm#275
  • Loading branch information
fitzgen committed Sep 6, 2018
1 parent cb2aa99 commit 8b5f5a7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 9 additions & 0 deletions crates/js-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2917,6 +2917,15 @@ pub mod WebAssembly {
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Instance)
#[wasm_bindgen(catch, constructor, js_namespace = WebAssembly)]
pub fn new(module: &Module, imports: &Object) -> Result<Instance, JsValue>;

/// The `exports` readonly property of the `WebAssembly.Instance` object
/// prototype returns an object containing as its members all the
/// functions exported from the WebAssembly module instance, to allow
/// them to be accessed and used by JavaScript.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Instance/exports)
#[wasm_bindgen(getter, method, js_namespace = WebAssembly)]
pub fn exports(this: &Instance) -> Object;
}

// WebAssembly.LinkError
Expand Down
8 changes: 7 additions & 1 deletion crates/js-sys/tests/wasm/WebAssembly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,19 @@ fn runtime_error_inheritance() {
}

#[wasm_bindgen_test]
fn instance_constructor_and_inheritance() {
fn webassembly_instance() {
let module = WebAssembly::Module::new(&get_valid_wasm()).unwrap();
let imports = get_imports();
let instance = WebAssembly::Instance::new(&module, &imports).unwrap();

// Inheritance chain is correct.
assert!(instance.is_instance_of::<WebAssembly::Instance>());
assert!(instance.is_instance_of::<Object>());
let _: &Object = instance.as_ref();

// Has expected exports.
let exports = instance.exports();
assert!(Reflect::has(exports.as_ref(), &"exported_func".into()));
}

#[wasm_bindgen_test]
Expand Down

0 comments on commit 8b5f5a7

Please sign in to comment.