mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-11-24 06:33:33 +03:00
Merge pull request #387 from Hywan/webassembly
feat(js) Implement the `WebAssembly.validate` binding.
This commit is contained in:
commit
f7dc819289
14
src/js.rs
14
src/js.rs
@ -1218,6 +1218,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, catch)]
|
||||
pub fn validate(bufferSource: JsValue) -> Result<bool, JsValue>;
|
||||
}
|
||||
|
||||
// JsString
|
||||
#[wasm_bindgen]
|
||||
extern "C" {
|
||||
|
68
tests/all/js_globals/WebAssembly.rs
Normal file
68
tests/all/js_globals/WebAssembly.rs
Normal file
@ -0,0 +1,68 @@
|
||||
#![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) -> 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() {
|
||||
assert.equal(wasm.validate_wasm(new ArrayBuffer(42)), false);
|
||||
}
|
||||
"#)
|
||||
.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()
|
||||
}
|
@ -23,6 +23,7 @@ mod Symbol;
|
||||
mod TypedArray;
|
||||
mod WeakMap;
|
||||
mod WeakSet;
|
||||
mod WebAssembly;
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "std")]
|
||||
|
Loading…
Reference in New Issue
Block a user