From fe25a9a268bb15179b7fc3c35a4bff5331309a7a Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 4 Jul 2018 15:27:01 +0200 Subject: [PATCH 1/4] feat(js) Implement the `WebAssembly.validate` binding. --- src/js.rs | 14 ++++++++++++++ tests/all/js_globals/WebAssembly.rs | 30 +++++++++++++++++++++++++++++ tests/all/js_globals/mod.rs | 1 + 3 files changed, 45 insertions(+) create mode 100644 tests/all/js_globals/WebAssembly.rs diff --git a/src/js.rs b/src/js.rs index 51d40ef5..37d01ff1 100644 --- a/src/js.rs +++ b/src/js.rs @@ -1138,6 +1138,20 @@ extern "C" { pub fn delete(this: &WeakSet, value: Object) -> bool; } +// WebAssembly +#[wasm_bindgen] +extern "C" { + pub type WebAssembly; + + /// 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`). + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/validate + #[wasm_bindgen(static_method_of = WebAssembly)] + pub fn validate(bufferSource: JsValue) -> bool; +} + // JsString #[wasm_bindgen] extern "C" { diff --git a/tests/all/js_globals/WebAssembly.rs b/tests/all/js_globals/WebAssembly.rs new file mode 100644 index 00000000..dcdea951 --- /dev/null +++ b/tests/all/js_globals/WebAssembly.rs @@ -0,0 +1,30 @@ +#![allow(non_snake_case)] + +use super::project; + +#[test] +fn validate() { + project() + .file("src/lib.rs", r#" + #![feature(proc_macro, wasm_custom_section)] + + extern crate wasm_bindgen; + use JsValue; + use wasm_bindgen::prelude::*; + use wasm_bindgen::js::WebAssembly; + + #[wasm_bindgen] + pub fn validate_wasm(wasm: JsValue) -> bool { + WebAssembly::validate(wasm) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + assert.equal(wasm.validate_wasm(new ArrayBuffer(42)), false); + } + "#) + .test() +} diff --git a/tests/all/js_globals/mod.rs b/tests/all/js_globals/mod.rs index 8c02811c..7b780349 100644 --- a/tests/all/js_globals/mod.rs +++ b/tests/all/js_globals/mod.rs @@ -20,6 +20,7 @@ mod SetIterator; mod TypedArray; mod WeakMap; mod WeakSet; +mod WebAssembly; #[test] #[cfg(feature = "std")] From c450fbf2379363c555aaf53c6c69cbec8958cf30 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 4 Jul 2018 15:51:55 +0200 Subject: [PATCH 2/4] feat(js) Support exception on `WebAssembly::validate`. --- src/js.rs | 4 ++-- tests/all/js_globals/WebAssembly.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/js.rs b/src/js.rs index 37d01ff1..2bce01fe 100644 --- a/src/js.rs +++ b/src/js.rs @@ -1148,8 +1148,8 @@ extern "C" { /// form a valid wasm module (`true`) or not (`false`). /// /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/validate - #[wasm_bindgen(static_method_of = WebAssembly)] - pub fn validate(bufferSource: JsValue) -> bool; + #[wasm_bindgen(static_method_of = WebAssembly, catch)] + pub fn validate(bufferSource: JsValue) -> Result; } // JsString diff --git a/tests/all/js_globals/WebAssembly.rs b/tests/all/js_globals/WebAssembly.rs index dcdea951..790e6338 100644 --- a/tests/all/js_globals/WebAssembly.rs +++ b/tests/all/js_globals/WebAssembly.rs @@ -15,7 +15,7 @@ fn validate() { #[wasm_bindgen] pub fn validate_wasm(wasm: JsValue) -> bool { - WebAssembly::validate(wasm) + WebAssembly::validate(wasm).unwrap_or(false) } "#) .file("test.ts", r#" From d1955c91b76ae97b95f65fe4aa368c74fa68a0b1 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Fri, 6 Jul 2018 10:41:32 +0200 Subject: [PATCH 3/4] test(js) Don't unwrap to `false`. --- tests/all/js_globals/WebAssembly.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/all/js_globals/WebAssembly.rs b/tests/all/js_globals/WebAssembly.rs index 790e6338..c829909e 100644 --- a/tests/all/js_globals/WebAssembly.rs +++ b/tests/all/js_globals/WebAssembly.rs @@ -14,11 +14,14 @@ fn validate() { use wasm_bindgen::js::WebAssembly; #[wasm_bindgen] - pub fn validate_wasm(wasm: JsValue) -> bool { - WebAssembly::validate(wasm).unwrap_or(false) + pub fn validate_wasm(wasm: JsValue) -> JsValue { + match WebAssembly::validate(wasm) { + Ok(value) => value.into(), + Err(err) => err + } } "#) - .file("test.ts", r#" + .file("test.js", r#" import * as assert from "assert"; import * as wasm from "./out"; From b4bf8fbfba44c9f13e01ea62ff63189a0c3e894a Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Fri, 6 Jul 2018 10:41:51 +0200 Subject: [PATCH 4/4] test(js) New `WebAssembly.validate_with_invalid_input` test case. --- tests/all/js_globals/WebAssembly.rs | 35 +++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/all/js_globals/WebAssembly.rs b/tests/all/js_globals/WebAssembly.rs index c829909e..a62c51aa 100644 --- a/tests/all/js_globals/WebAssembly.rs +++ b/tests/all/js_globals/WebAssembly.rs @@ -31,3 +31,38 @@ fn validate() { "#) .test() } + +#[test] +fn validate_with_invalid_input() { + project() + .file("src/lib.rs", r#" + #![feature(proc_macro, wasm_custom_section)] + + extern crate wasm_bindgen; + use JsValue; + use wasm_bindgen::prelude::*; + use wasm_bindgen::js::WebAssembly; + + #[wasm_bindgen] + pub fn validate_wasm(wasm: JsValue) -> JsValue { + match WebAssembly::validate(wasm) { + Ok(value) => value.into(), + Err(err) => err + } + } + "#) + .file("test.js", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + try { + wasm.validate_wasm(42); + assert.ok(false); + } catch (e) { + assert.ok(true); + } + } + "#) + .test() +}