Skip to content

Commit cb2aa99

Browse files
committed
js-sys: Define imports for WebAssembly.Instance and its constructor
Part of rustwasm#670 and rustwasm#275
1 parent 66d155d commit cb2aa99

File tree

3 files changed

+68
-19
lines changed

3 files changed

+68
-19
lines changed

crates/js-sys/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2896,6 +2896,29 @@ pub mod WebAssembly {
28962896
pub fn new(message: &str) -> CompileError;
28972897
}
28982898

2899+
// WebAssembly.Instance
2900+
#[wasm_bindgen]
2901+
extern "C" {
2902+
/// A `WebAssembly.Instance` object is a stateful, executable instance
2903+
/// of a `WebAssembly.Module`. Instance objects contain all the exported
2904+
/// WebAssembly functions that allow calling into WebAssembly code from
2905+
/// JavaScript.
2906+
///
2907+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Instance)
2908+
#[wasm_bindgen(extends = Object, js_namespace = WebAssembly)]
2909+
#[derive(Clone, Debug)]
2910+
pub type Instance;
2911+
2912+
/// The `WebAssembly.Instance()` constructor function can be called to
2913+
/// synchronously instantiate a given `WebAssembly.Module`
2914+
/// object. However, the primary way to get an `Instance` is through the
2915+
/// asynchronous `WebAssembly.instantiateStreaming()` function.
2916+
///
2917+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Instance)
2918+
#[wasm_bindgen(catch, constructor, js_namespace = WebAssembly)]
2919+
pub fn new(module: &Module, imports: &Object) -> Result<Instance, JsValue>;
2920+
}
2921+
28992922
// WebAssembly.LinkError
29002923
#[wasm_bindgen]
29012924
extern "C" {

crates/js-sys/tests/wasm/WebAssembly.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,19 @@ function getInvalidTableObject() {
2121
return { element: "anyfunc", initial: 1, maximum: 0 }
2222
}
2323

24+
function getImports() {
25+
return {
26+
imports: {
27+
imported_func: function () {
28+
return 1;
29+
}
30+
}
31+
};
32+
}
33+
2434
module.exports = {
2535
getInvalidTableObject,
2636
getTableObject,
2737
getWasmArray,
38+
getImports,
2839
};

crates/js-sys/tests/wasm/WebAssembly.rs

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use futures::Future;
22
use js_sys::*;
3-
use wasm_bindgen::{JsCast, prelude::*};
3+
use wasm_bindgen::{prelude::*, JsCast};
44
use wasm_bindgen_futures::JsFuture;
55
use wasm_bindgen_test::*;
66

77
#[wasm_bindgen(module = "tests/wasm/WebAssembly.js")]
8-
extern {
8+
extern "C" {
99
#[wasm_bindgen(js_name = getWasmArray)]
1010
fn get_wasm_array() -> Uint8Array;
1111

@@ -14,6 +14,9 @@ extern {
1414

1515
#[wasm_bindgen(js_name = getInvalidTableObject)]
1616
fn get_invalid_table_object() -> Object;
17+
18+
#[wasm_bindgen(js_name = getImports)]
19+
fn get_imports() -> Object;
1720
}
1821

1922
fn get_invalid_wasm() -> JsValue {
@@ -38,23 +41,19 @@ fn validate() {
3841
#[wasm_bindgen_test(async)]
3942
fn compile_compile_error() -> impl Future<Item = (), Error = JsValue> {
4043
let p = WebAssembly::compile(&get_invalid_wasm());
41-
JsFuture::from(p)
42-
.map(|_| unreachable!())
43-
.or_else(|e| {
44-
assert!(e.is_instance_of::<WebAssembly::CompileError>());
45-
Ok(())
46-
})
44+
JsFuture::from(p).map(|_| unreachable!()).or_else(|e| {
45+
assert!(e.is_instance_of::<WebAssembly::CompileError>());
46+
Ok(())
47+
})
4748
}
4849

4950
#[wasm_bindgen_test(async)]
5051
fn compile_type_error() -> impl Future<Item = (), Error = JsValue> {
5152
let p = WebAssembly::compile(&get_bad_type_wasm());
52-
JsFuture::from(p)
53-
.map(|_| unreachable!())
54-
.or_else(|e| {
55-
assert!(e.is_instance_of::<TypeError>());
56-
Ok(())
57-
})
53+
JsFuture::from(p).map(|_| unreachable!()).or_else(|e| {
54+
assert!(e.is_instance_of::<TypeError>());
55+
Ok(())
56+
})
5857
}
5958

6059
#[wasm_bindgen_test(async)]
@@ -63,8 +62,7 @@ fn compile_valid() -> impl Future<Item = (), Error = JsValue> {
6362
JsFuture::from(p)
6463
.map(|module| {
6564
assert!(module.is_instance_of::<WebAssembly::Module>());
66-
})
67-
.map_err(|_| unreachable!())
65+
}).map_err(|_| unreachable!())
6866
}
6967

7068
#[wasm_bindgen_test]
@@ -81,7 +79,9 @@ fn module_error() {
8179
let error = WebAssembly::Module::new(&get_invalid_wasm()).err().unwrap();
8280
assert!(error.is_instance_of::<WebAssembly::CompileError>());
8381

84-
let error = WebAssembly::Module::new(&get_bad_type_wasm()).err().unwrap();
82+
let error = WebAssembly::Module::new(&get_bad_type_wasm())
83+
.err()
84+
.unwrap();
8585
assert!(error.is_instance_of::<TypeError>());
8686
}
8787

@@ -117,7 +117,9 @@ fn table_inheritance() {
117117

118118
#[wasm_bindgen_test]
119119
fn table_error() {
120-
let error = WebAssembly::Table::new(&get_invalid_table_object()).err().unwrap();
120+
let error = WebAssembly::Table::new(&get_invalid_table_object())
121+
.err()
122+
.unwrap();
121123
assert!(error.is_instance_of::<RangeError>());
122124
}
123125

@@ -154,6 +156,16 @@ fn runtime_error_inheritance() {
154156
let _: &Error = error.as_ref();
155157
}
156158

159+
#[wasm_bindgen_test]
160+
fn instance_constructor_and_inheritance() {
161+
let module = WebAssembly::Module::new(&get_valid_wasm()).unwrap();
162+
let imports = get_imports();
163+
let instance = WebAssembly::Instance::new(&module, &imports).unwrap();
164+
assert!(instance.is_instance_of::<WebAssembly::Instance>());
165+
assert!(instance.is_instance_of::<Object>());
166+
let _: &Object = instance.as_ref();
167+
}
168+
157169
#[wasm_bindgen_test]
158170
fn memory_works() {
159171
let obj = Object::new();
@@ -166,7 +178,10 @@ fn memory_works() {
166178
assert_eq!(mem.grow(2), 2);
167179
assert_eq!(mem.grow(3), 4);
168180
assert_eq!(
169-
mem.buffer().dyn_into::<ArrayBuffer>().unwrap().byte_length(),
181+
mem.buffer()
182+
.dyn_into::<ArrayBuffer>()
183+
.unwrap()
184+
.byte_length(),
170185
7 * 64 * 1024,
171186
);
172187
}

0 commit comments

Comments
 (0)