Skip to content

Commit 021cbba

Browse files
committed
js-sys: Add bindings for WebAssembly.instantiate
Part of rustwasm#275
1 parent 8b5f5a7 commit 021cbba

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

crates/js-sys/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2866,6 +2866,20 @@ pub mod WebAssembly {
28662866
#[wasm_bindgen(js_namespace = WebAssembly)]
28672867
pub fn compile(buffer_source: &JsValue) -> Promise;
28682868

2869+
/// The `WebAssembly.instantiate()` function allows you to compile and
2870+
/// instantiate WebAssembly code.
2871+
///
2872+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiate)
2873+
#[wasm_bindgen(js_namespace = WebAssembly, js_name = instantiate)]
2874+
pub fn instantiate_buffer(buffer: &[u8], imports: &Object) -> Promise;
2875+
2876+
/// The `WebAssembly.instantiate()` function allows you to compile and
2877+
/// instantiate WebAssembly code.
2878+
///
2879+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiate)
2880+
#[wasm_bindgen(js_namespace = WebAssembly, js_name = instantiate)]
2881+
pub fn instantiate_module(module: &Module, imports: &Object) -> Promise;
2882+
28692883
/// The `WebAssembly.validate()` function validates a given typed
28702884
/// array of WebAssembly binary code, returning whether the bytes
28712885
/// form a valid wasm module (`true`) or not (`false`).

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,17 @@ fn webassembly_instance() {
172172
assert!(Reflect::has(exports.as_ref(), &"exported_func".into()));
173173
}
174174

175+
#[wasm_bindgen_test(async)]
176+
fn instantiate_module() -> impl Future<Item = (), Error = JsValue> {
177+
let module = WebAssembly::Module::new(&get_valid_wasm()).unwrap();
178+
let imports = get_imports();
179+
let p = WebAssembly::instantiate_module(&module, &imports);
180+
JsFuture::from(p)
181+
.map(|inst| {
182+
assert!(inst.is_instance_of::<WebAssembly::Instance>());
183+
})
184+
}
185+
175186
#[wasm_bindgen_test]
176187
fn memory_works() {
177188
let obj = Object::new();

0 commit comments

Comments
 (0)