From 5df2347a7a02c0ca6d9b9c3ad45322f053b6339a Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Thu, 6 Sep 2018 13:32:31 -0700 Subject: [PATCH 01/10] js-sys: Add `extends` to `JSON` Part of #670 --- crates/js-sys/src/lib.rs | 6 +++++- crates/js-sys/tests/wasm/JSON.rs | 14 +++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 89e728eed32..91eedb55e31 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -3048,8 +3048,12 @@ pub mod WebAssembly { // JSON #[wasm_bindgen] extern "C" { - + /// The `JSON` object contains methods for parsing [JavaScript Object + /// Notation (JSON)](https://json.org/) and converting values to JSON. It + /// can't be called or constructed, and aside from its two method + /// properties, it has no interesting functionality of its own. #[derive(Clone, Debug)] + #[wasm_bindgen(extends = Object)] pub type JSON; /// The `JSON.parse()` method parses a JSON string, constructing the diff --git a/crates/js-sys/tests/wasm/JSON.rs b/crates/js-sys/tests/wasm/JSON.rs index b1d9fd1841b..d38bae3bc59 100644 --- a/crates/js-sys/tests/wasm/JSON.rs +++ b/crates/js-sys/tests/wasm/JSON.rs @@ -1,4 +1,4 @@ -use wasm_bindgen::JsValue; +use wasm_bindgen::prelude::*; use wasm_bindgen_test::*; use wasm_bindgen::JsCast; use js_sys::*; @@ -82,3 +82,15 @@ fn stringify_error() { let err_msg: String = From::from(err.message()); assert!(err_msg.contains("rust really rocks")); } + +#[wasm_bindgen_test] +fn json_extends() { + #[wasm_bindgen] + extern { + #[wasm_bindgen(js_name = JSON)] + static json: JSON; + } + + assert!(json.is_instance_of::()); + let _: &Object = json.as_ref(); +} From bfff8661c19d9d88b9baa7e40616f5fd8cead880 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Thu, 6 Sep 2018 13:39:30 -0700 Subject: [PATCH 02/10] js-sys: Add `extends` to `Math` Part of #670 --- crates/js-sys/src/lib.rs | 1 + crates/js-sys/tests/wasm/Math.rs | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 91eedb55e31..ef3921be3d8 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -1203,6 +1203,7 @@ extern { #[wasm_bindgen] extern "C" { #[derive(Clone, Debug)] + #[wasm_bindgen(extends = Object)] pub type Math; /// The Math.abs() function returns the absolute value of a number, that is diff --git a/crates/js-sys/tests/wasm/Math.rs b/crates/js-sys/tests/wasm/Math.rs index a46ded34fd7..c6047fca6d7 100644 --- a/crates/js-sys/tests/wasm/Math.rs +++ b/crates/js-sys/tests/wasm/Math.rs @@ -1,9 +1,22 @@ use std::f64::consts::PI; use std::f64::{NEG_INFINITY, NAN}; +use wasm_bindgen::{JsCast, prelude::*}; use wasm_bindgen_test::*; use js_sys::*; +#[wasm_bindgen_test] +fn math_extends() { + #[wasm_bindgen] + extern { + #[wasm_bindgen(js_name = Math)] + static math: Math; + } + + assert!(math.is_instance_of::()); + let _: &Object = math.as_ref(); +} + macro_rules! assert_eq { ($a:expr, $b:expr) => ({ let (a, b) = (&$a, &$b); From 66d155d7085222391a3af4337175a88268764c45 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Thu, 6 Sep 2018 13:42:16 -0700 Subject: [PATCH 03/10] js-sys: Add `extends` to `Reflect` Part of #670 --- crates/js-sys/src/lib.rs | 1 + crates/js-sys/tests/wasm/Reflect.rs | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index ef3921be3d8..c2d2bd3aaf7 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -2128,6 +2128,7 @@ extern { #[wasm_bindgen] extern "C" { #[derive(Clone, Debug)] + #[wasm_bindgen(extends = Object)] pub type Reflect; /// The static `Reflect.apply()` method calls a target function with diff --git a/crates/js-sys/tests/wasm/Reflect.rs b/crates/js-sys/tests/wasm/Reflect.rs index 5f6f47f1779..da2c5217a7b 100644 --- a/crates/js-sys/tests/wasm/Reflect.rs +++ b/crates/js-sys/tests/wasm/Reflect.rs @@ -1,4 +1,4 @@ -use wasm_bindgen::prelude::*; +use wasm_bindgen::{JsCast, prelude::*}; use wasm_bindgen_test::*; use js_sys::*; @@ -180,3 +180,15 @@ fn set_prototype_of() { let obj = JsValue::from(obj); assert_eq!(JsValue::from(Reflect::get_prototype_of(&obj)), JsValue::null()); } + +#[wasm_bindgen_test] +fn reflect_extends() { + #[wasm_bindgen] + extern { + #[wasm_bindgen(js_name = Reflect)] + static reflect: Reflect; + } + + assert!(reflect.is_instance_of::()); + let _: &Object = reflect.as_ref(); +} From cb2aa999c0068ced63779c19cad0411e53d1a9a8 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Thu, 6 Sep 2018 14:09:49 -0700 Subject: [PATCH 04/10] js-sys: Define imports for `WebAssembly.Instance` and its constructor Part of #670 and #275 --- crates/js-sys/src/lib.rs | 23 +++++++++++ crates/js-sys/tests/wasm/WebAssembly.js | 11 +++++ crates/js-sys/tests/wasm/WebAssembly.rs | 53 ++++++++++++++++--------- 3 files changed, 68 insertions(+), 19 deletions(-) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index c2d2bd3aaf7..843eb404f24 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -2896,6 +2896,29 @@ pub mod WebAssembly { pub fn new(message: &str) -> CompileError; } + // WebAssembly.Instance + #[wasm_bindgen] + extern "C" { + /// A `WebAssembly.Instance` object is a stateful, executable instance + /// of a `WebAssembly.Module`. Instance objects contain all the exported + /// WebAssembly functions that allow calling into WebAssembly code from + /// JavaScript. + /// + /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Instance) + #[wasm_bindgen(extends = Object, js_namespace = WebAssembly)] + #[derive(Clone, Debug)] + pub type Instance; + + /// The `WebAssembly.Instance()` constructor function can be called to + /// synchronously instantiate a given `WebAssembly.Module` + /// object. However, the primary way to get an `Instance` is through the + /// asynchronous `WebAssembly.instantiateStreaming()` function. + /// + /// [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; + } + // WebAssembly.LinkError #[wasm_bindgen] extern "C" { diff --git a/crates/js-sys/tests/wasm/WebAssembly.js b/crates/js-sys/tests/wasm/WebAssembly.js index 51d0c343a71..a24342f532e 100644 --- a/crates/js-sys/tests/wasm/WebAssembly.js +++ b/crates/js-sys/tests/wasm/WebAssembly.js @@ -21,8 +21,19 @@ function getInvalidTableObject() { return { element: "anyfunc", initial: 1, maximum: 0 } } +function getImports() { + return { + imports: { + imported_func: function () { + return 1; + } + } + }; +} + module.exports = { getInvalidTableObject, getTableObject, getWasmArray, + getImports, }; diff --git a/crates/js-sys/tests/wasm/WebAssembly.rs b/crates/js-sys/tests/wasm/WebAssembly.rs index 35207f968b3..ad363acc72b 100644 --- a/crates/js-sys/tests/wasm/WebAssembly.rs +++ b/crates/js-sys/tests/wasm/WebAssembly.rs @@ -1,11 +1,11 @@ use futures::Future; use js_sys::*; -use wasm_bindgen::{JsCast, prelude::*}; +use wasm_bindgen::{prelude::*, JsCast}; use wasm_bindgen_futures::JsFuture; use wasm_bindgen_test::*; #[wasm_bindgen(module = "tests/wasm/WebAssembly.js")] -extern { +extern "C" { #[wasm_bindgen(js_name = getWasmArray)] fn get_wasm_array() -> Uint8Array; @@ -14,6 +14,9 @@ extern { #[wasm_bindgen(js_name = getInvalidTableObject)] fn get_invalid_table_object() -> Object; + + #[wasm_bindgen(js_name = getImports)] + fn get_imports() -> Object; } fn get_invalid_wasm() -> JsValue { @@ -38,23 +41,19 @@ fn validate() { #[wasm_bindgen_test(async)] fn compile_compile_error() -> impl Future { let p = WebAssembly::compile(&get_invalid_wasm()); - JsFuture::from(p) - .map(|_| unreachable!()) - .or_else(|e| { - assert!(e.is_instance_of::()); - Ok(()) - }) + JsFuture::from(p).map(|_| unreachable!()).or_else(|e| { + assert!(e.is_instance_of::()); + Ok(()) + }) } #[wasm_bindgen_test(async)] fn compile_type_error() -> impl Future { let p = WebAssembly::compile(&get_bad_type_wasm()); - JsFuture::from(p) - .map(|_| unreachable!()) - .or_else(|e| { - assert!(e.is_instance_of::()); - Ok(()) - }) + JsFuture::from(p).map(|_| unreachable!()).or_else(|e| { + assert!(e.is_instance_of::()); + Ok(()) + }) } #[wasm_bindgen_test(async)] @@ -63,8 +62,7 @@ fn compile_valid() -> impl Future { JsFuture::from(p) .map(|module| { assert!(module.is_instance_of::()); - }) - .map_err(|_| unreachable!()) + }).map_err(|_| unreachable!()) } #[wasm_bindgen_test] @@ -81,7 +79,9 @@ fn module_error() { let error = WebAssembly::Module::new(&get_invalid_wasm()).err().unwrap(); assert!(error.is_instance_of::()); - let error = WebAssembly::Module::new(&get_bad_type_wasm()).err().unwrap(); + let error = WebAssembly::Module::new(&get_bad_type_wasm()) + .err() + .unwrap(); assert!(error.is_instance_of::()); } @@ -117,7 +117,9 @@ fn table_inheritance() { #[wasm_bindgen_test] fn table_error() { - let error = WebAssembly::Table::new(&get_invalid_table_object()).err().unwrap(); + let error = WebAssembly::Table::new(&get_invalid_table_object()) + .err() + .unwrap(); assert!(error.is_instance_of::()); } @@ -154,6 +156,16 @@ fn runtime_error_inheritance() { let _: &Error = error.as_ref(); } +#[wasm_bindgen_test] +fn instance_constructor_and_inheritance() { + let module = WebAssembly::Module::new(&get_valid_wasm()).unwrap(); + let imports = get_imports(); + let instance = WebAssembly::Instance::new(&module, &imports).unwrap(); + assert!(instance.is_instance_of::()); + assert!(instance.is_instance_of::()); + let _: &Object = instance.as_ref(); +} + #[wasm_bindgen_test] fn memory_works() { let obj = Object::new(); @@ -166,7 +178,10 @@ fn memory_works() { assert_eq!(mem.grow(2), 2); assert_eq!(mem.grow(3), 4); assert_eq!( - mem.buffer().dyn_into::().unwrap().byte_length(), + mem.buffer() + .dyn_into::() + .unwrap() + .byte_length(), 7 * 64 * 1024, ); } From 8b5f5a75601858ad08065544a7a582e1dd6fa763 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Thu, 6 Sep 2018 14:16:28 -0700 Subject: [PATCH 05/10] js-sys: Add `exports` getter to `WebAssembly.Instance` Part of #275 --- crates/js-sys/src/lib.rs | 9 +++++++++ crates/js-sys/tests/wasm/WebAssembly.rs | 8 +++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 843eb404f24..4b6b4c5d08c 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -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; + + /// 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 diff --git a/crates/js-sys/tests/wasm/WebAssembly.rs b/crates/js-sys/tests/wasm/WebAssembly.rs index ad363acc72b..2d84dc4f924 100644 --- a/crates/js-sys/tests/wasm/WebAssembly.rs +++ b/crates/js-sys/tests/wasm/WebAssembly.rs @@ -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::()); assert!(instance.is_instance_of::()); let _: &Object = instance.as_ref(); + + // Has expected exports. + let exports = instance.exports(); + assert!(Reflect::has(exports.as_ref(), &"exported_func".into())); } #[wasm_bindgen_test] From 021cbbab7113947ea51f9badca28f0dc81565e11 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Thu, 6 Sep 2018 14:35:45 -0700 Subject: [PATCH 06/10] js-sys: Add bindings for `WebAssembly.instantiate` Part of #275 --- crates/js-sys/src/lib.rs | 14 ++++++++++++++ crates/js-sys/tests/wasm/WebAssembly.rs | 11 +++++++++++ 2 files changed, 25 insertions(+) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 4b6b4c5d08c..26b2b300f1e 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -2866,6 +2866,20 @@ pub mod WebAssembly { #[wasm_bindgen(js_namespace = WebAssembly)] pub fn compile(buffer_source: &JsValue) -> Promise; + /// The `WebAssembly.instantiate()` function allows you to compile and + /// instantiate WebAssembly code. + /// + /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiate) + #[wasm_bindgen(js_namespace = WebAssembly, js_name = instantiate)] + pub fn instantiate_buffer(buffer: &[u8], imports: &Object) -> Promise; + + /// The `WebAssembly.instantiate()` function allows you to compile and + /// instantiate WebAssembly code. + /// + /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiate) + #[wasm_bindgen(js_namespace = WebAssembly, js_name = instantiate)] + pub fn instantiate_module(module: &Module, imports: &Object) -> Promise; + /// The `WebAssembly.validate()` function validates a given typed /// array of WebAssembly binary code, returning whether the bytes /// form a valid wasm module (`true`) or not (`false`). diff --git a/crates/js-sys/tests/wasm/WebAssembly.rs b/crates/js-sys/tests/wasm/WebAssembly.rs index 2d84dc4f924..72ec451aa23 100644 --- a/crates/js-sys/tests/wasm/WebAssembly.rs +++ b/crates/js-sys/tests/wasm/WebAssembly.rs @@ -172,6 +172,17 @@ fn webassembly_instance() { assert!(Reflect::has(exports.as_ref(), &"exported_func".into())); } +#[wasm_bindgen_test(async)] +fn instantiate_module() -> impl Future { + let module = WebAssembly::Module::new(&get_valid_wasm()).unwrap(); + let imports = get_imports(); + let p = WebAssembly::instantiate_module(&module, &imports); + JsFuture::from(p) + .map(|inst| { + assert!(inst.is_instance_of::()); + }) +} + #[wasm_bindgen_test] fn memory_works() { let obj = Object::new(); From fb5e6e9c06aa9cd189e46cb1b9beefeac6da8eea Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Thu, 6 Sep 2018 14:47:37 -0700 Subject: [PATCH 07/10] js-sys: Add bindings for `WebAssembly.instantiateStreaming` Part of #275 --- crates/js-sys/src/lib.rs | 9 +++++++++ crates/js-sys/tests/wasm/WebAssembly.js | 6 ++++++ crates/js-sys/tests/wasm/WebAssembly.rs | 14 ++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 26b2b300f1e..07ffd5a05bb 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -2880,6 +2880,15 @@ pub mod WebAssembly { #[wasm_bindgen(js_namespace = WebAssembly, js_name = instantiate)] pub fn instantiate_module(module: &Module, imports: &Object) -> Promise; + /// The `WebAssembly.instantiateStreaming()` function compiles and + /// instantiates a WebAssembly module directly from a streamed + /// underlying source. This is the most efficient, optimized way to load + /// wasm code. + /// + /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiateStreaming) + #[wasm_bindgen(js_namespace = WebAssembly, js_name = instantiateStreaming)] + pub fn instantiate_streaming(response: &Promise, imports: &Object) -> Promise; + /// The `WebAssembly.validate()` function validates a given typed /// array of WebAssembly binary code, returning whether the bytes /// form a valid wasm module (`true`) or not (`false`). diff --git a/crates/js-sys/tests/wasm/WebAssembly.js b/crates/js-sys/tests/wasm/WebAssembly.js index a24342f532e..694046853d4 100644 --- a/crates/js-sys/tests/wasm/WebAssembly.js +++ b/crates/js-sys/tests/wasm/WebAssembly.js @@ -31,6 +31,12 @@ function getImports() { }; } +// Polyfill `WebAssembly.instantiateStreaming` for node. +if (!global.WebAssembly.instantiateStreaming) { + global.WebAssembly.instantiateStreaming = + (response, imports) => response.then(buf => WebAssembly.instantiate(buf, imports)); +} + module.exports = { getInvalidTableObject, getTableObject, diff --git a/crates/js-sys/tests/wasm/WebAssembly.rs b/crates/js-sys/tests/wasm/WebAssembly.rs index 72ec451aa23..e0b10931f82 100644 --- a/crates/js-sys/tests/wasm/WebAssembly.rs +++ b/crates/js-sys/tests/wasm/WebAssembly.rs @@ -183,6 +183,20 @@ fn instantiate_module() -> impl Future { }) } +#[wasm_bindgen_test(async)] +fn instantiate_streaming() -> impl Future { + let response = Promise::resolve(&get_valid_wasm()); + let imports = get_imports(); + let p = WebAssembly::instantiate_streaming(&response, &imports); + JsFuture::from(p) + .map(|obj| { + assert!( + Reflect::get(obj.as_ref(), &"instance".into()) + .is_instance_of::() + ); + }) +} + #[wasm_bindgen_test] fn memory_works() { let obj = Object::new(); From 2d4f36c9da85ec27f1a7127b29c86440ffafb7a5 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Thu, 6 Sep 2018 14:54:49 -0700 Subject: [PATCH 08/10] js-sys: Add bindings to `WebAssembly.Table.prototype.get` Part of #275 --- crates/js-sys/src/lib.rs | 7 +++++++ crates/js-sys/tests/wasm/WebAssembly.rs | 3 +++ 2 files changed, 10 insertions(+) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 07ffd5a05bb..d2d5e83d50f 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -3061,6 +3061,13 @@ pub mod WebAssembly { /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Table/length) #[wasm_bindgen(method, getter, js_namespace = WebAssembly)] pub fn length(this: &Table) -> u32; + + /// The `get()` prototype method of the `WebAssembly.Table()` object + /// retrieves a function reference stored at a given index. + /// + /// [MDN documentation]( + #[wasm_bindgen(method, catch, js_namespace = WebAssembly)] + pub fn get(this: &Table, index: u32) -> Result; } // WebAssembly.Memory diff --git a/crates/js-sys/tests/wasm/WebAssembly.rs b/crates/js-sys/tests/wasm/WebAssembly.rs index e0b10931f82..d2e42a6977c 100644 --- a/crates/js-sys/tests/wasm/WebAssembly.rs +++ b/crates/js-sys/tests/wasm/WebAssembly.rs @@ -127,6 +127,9 @@ fn table_error() { fn table() { let table = WebAssembly::Table::new(&get_table_object().into()).unwrap(); assert_eq!(table.length(), 1); + + assert!(table.get(0).is_ok()); + assert!(table.get(999).is_err()); } #[wasm_bindgen_test] From 8dbb0fc5f24aee42ab865587ce7bb44bfdab799a Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Thu, 6 Sep 2018 14:58:46 -0700 Subject: [PATCH 09/10] js-sys: Expose bindings to `WebAssembly.Table.prototype.grow` Par of #275 --- crates/js-sys/src/lib.rs | 10 +++++++++- crates/js-sys/tests/wasm/WebAssembly.rs | 3 +++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index d2d5e83d50f..d6c746f662a 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -3065,9 +3065,17 @@ pub mod WebAssembly { /// The `get()` prototype method of the `WebAssembly.Table()` object /// retrieves a function reference stored at a given index. /// - /// [MDN documentation]( + /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Table/get) #[wasm_bindgen(method, catch, js_namespace = WebAssembly)] pub fn get(this: &Table, index: u32) -> Result; + + /// The `grow()` prototype method of the `WebAssembly.Table` object + /// increases the size of the `Table` instance by a specified number of + /// elements. + /// + /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Table/grow) + #[wasm_bindgen(method, catch, js_namespace = WebAssembly)] + pub fn grow(this: &Table, additional_capacity: u32) -> Result; } // WebAssembly.Memory diff --git a/crates/js-sys/tests/wasm/WebAssembly.rs b/crates/js-sys/tests/wasm/WebAssembly.rs index d2e42a6977c..230a4bfcc75 100644 --- a/crates/js-sys/tests/wasm/WebAssembly.rs +++ b/crates/js-sys/tests/wasm/WebAssembly.rs @@ -130,6 +130,9 @@ fn table() { assert!(table.get(0).is_ok()); assert!(table.get(999).is_err()); + + table.grow(1).unwrap(); + assert_eq!(table.length(), 2); } #[wasm_bindgen_test] From bfff31fcb90e1c105f3fd6efe8e2b3e50cc8cbec Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Thu, 6 Sep 2018 15:02:01 -0700 Subject: [PATCH 10/10] js-sys: Expose bindings to `WebAssembly.Table.prototype.set` Part of #275 --- crates/js-sys/src/lib.rs | 7 +++++++ crates/js-sys/tests/wasm/WebAssembly.rs | 3 +++ 2 files changed, 10 insertions(+) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index d6c746f662a..6f9bc275bcb 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -3076,6 +3076,13 @@ pub mod WebAssembly { /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Table/grow) #[wasm_bindgen(method, catch, js_namespace = WebAssembly)] pub fn grow(this: &Table, additional_capacity: u32) -> Result; + + /// The `set()` prototype method of the `WebAssembly.Table` object mutates a + /// reference stored at a given index to a different value. + /// + /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Table/set) + #[wasm_bindgen(method, catch, js_namespace = WebAssembly)] + pub fn set(this: &Table, index: u32, function: &Function) -> Result<(), JsValue>; } // WebAssembly.Memory diff --git a/crates/js-sys/tests/wasm/WebAssembly.rs b/crates/js-sys/tests/wasm/WebAssembly.rs index 230a4bfcc75..65385ec5938 100644 --- a/crates/js-sys/tests/wasm/WebAssembly.rs +++ b/crates/js-sys/tests/wasm/WebAssembly.rs @@ -133,6 +133,9 @@ fn table() { table.grow(1).unwrap(); assert_eq!(table.length(), 2); + + let f = table.get(0).unwrap(); + table.set(1, &f).unwrap(); } #[wasm_bindgen_test]