diff --git a/src/js.rs b/src/js.rs index 51d40ef5..c70d6d2d 100644 --- a/src/js.rs +++ b/src/js.rs @@ -254,6 +254,45 @@ extern "C" { pub fn unshift(this: &Array, value: JsValue) -> u32; } +// ArrayBuffer +#[wasm_bindgen] +extern "C" { + pub type ArrayBuffer; + + /// The `ArrayBuffer` object is used to represent a generic, + /// fixed-length raw binary data buffer. You cannot directly + /// manipulate the contents of an `ArrayBuffer`; instead, you + /// create one of the typed array objects or a `DataView` object + /// which represents the buffer in a specific format, and use that + /// to read and write the contents of the buffer. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer + #[wasm_bindgen(constructor)] + pub fn new(length: u32) -> ArrayBuffer; + + /// The `slice()` method returns a new `ArrayBuffer` whose contents + /// are a copy of this `ArrayBuffer`'s bytes from begin, inclusive, + /// up to end, exclusive. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/isView + #[wasm_bindgen(static_method_of = ArrayBuffer, js_name = isView)] + pub fn is_view(value: JsValue) -> bool; + + /// The `slice()` method returns a new `ArrayBuffer` whose contents + /// are a copy of this `ArrayBuffer`'s bytes from begin, inclusive, + /// up to end, exclusive. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/slice + #[wasm_bindgen(method)] + pub fn slice(this: &ArrayBuffer, begin: u32) -> ArrayBuffer; + + /// Like `slice()` but with the `end` argument. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/slice + #[wasm_bindgen(method, js_name = slice)] + pub fn slice_with_end(this: &ArrayBuffer, begin: u32, end: u32) -> ArrayBuffer; +} + // Array Iterator #[wasm_bindgen] extern "C" { diff --git a/tests/all/js_globals/ArrayBuffer.rs b/tests/all/js_globals/ArrayBuffer.rs new file mode 100644 index 00000000..79b987b1 --- /dev/null +++ b/tests/all/js_globals/ArrayBuffer.rs @@ -0,0 +1,110 @@ +#![allow(non_snake_case)] + +use super::project; + +#[test] +fn new() { + project() + .file("src/lib.rs", r#" + #![feature(proc_macro, wasm_custom_section)] + + extern crate wasm_bindgen; + use wasm_bindgen::prelude::*; + use wasm_bindgen::js::ArrayBuffer; + + #[wasm_bindgen] + pub fn new_arraybuffer() -> ArrayBuffer { + ArrayBuffer::new(42) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + assert.equal(typeof wasm.new_arraybuffer(), "object"); + } + "#) + .test() +} + +#[test] +fn is_view() { + 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::ArrayBuffer; + + #[wasm_bindgen] + pub fn is_view(value: JsValue) -> bool { + ArrayBuffer::is_view(value) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + assert.equal(wasm.is_view(new Uint8Array(42)), true); + } + "#) + .test() +} + +#[test] +fn slice() { + project() + .file("src/lib.rs", r#" + #![feature(proc_macro, wasm_custom_section)] + + extern crate wasm_bindgen; + use wasm_bindgen::prelude::*; + use wasm_bindgen::js::ArrayBuffer; + + #[wasm_bindgen] + pub fn slice(arraybuffer: &ArrayBuffer, begin: u32) -> ArrayBuffer { + arraybuffer.slice(begin) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + const arraybuffer = new ArrayBuffer(4); + assert.equal(typeof wasm.slice(arraybuffer, 2), "object"); + } + "#) + .test() +} + +#[test] +fn slice_with_end() { + project() + .file("src/lib.rs", r#" + #![feature(proc_macro, wasm_custom_section)] + + extern crate wasm_bindgen; + use wasm_bindgen::prelude::*; + use wasm_bindgen::js::ArrayBuffer; + + #[wasm_bindgen] + pub fn slice_with_end(arraybuffer: &ArrayBuffer, begin: u32, end: u32) -> ArrayBuffer { + arraybuffer.slice_with_end(begin, end) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + const arraybuffer = new ArrayBuffer(4); + assert.equal(typeof wasm.slice_with_end(arraybuffer, 1, 2), "object"); + } + "#) + .test() +} diff --git a/tests/all/js_globals/mod.rs b/tests/all/js_globals/mod.rs index 8c02811c..9d9f7aba 100644 --- a/tests/all/js_globals/mod.rs +++ b/tests/all/js_globals/mod.rs @@ -3,6 +3,7 @@ use super::project; mod Array; +mod ArrayBuffer; mod ArrayIterator; mod Boolean; mod Date;