From fe25a9a268bb15179b7fc3c35a4bff5331309a7a Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 4 Jul 2018 15:27:01 +0200 Subject: [PATCH] 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")]