Bindings for TypedArray subclasses (#486)

* Bindings for TypedArray subclasses

* Fill with Rust values rather than JsValues
This commit is contained in:
Craig Disselkoen 2018-07-17 16:24:56 -07:00 committed by Alex Crichton
parent bf64f74cab
commit a05d930a38
2 changed files with 457 additions and 98 deletions

192
src/js.rs
View File

@ -106,25 +106,6 @@ extern "C" {
pub fn parse_float(text: &str) -> f64;
}
// UInt8Array
#[wasm_bindgen]
extern "C" {
pub type Uint8Array;
/// The `Uint8Array()` constructor creates an array of unsigned 8-bit integers.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array
#[wasm_bindgen(constructor)]
pub fn new(constructor_arg: JsValue) -> Uint8Array;
/// The fill() method fills all the elements of an array from a start index
/// to an end index with a static value. The end index is not included.
///
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill
#[wasm_bindgen(method)]
pub fn fill(this: &Uint8Array, value: JsValue, start: u32, end: u32) -> Uint8Array;
}
// Array
#[wasm_bindgen]
extern "C" {
@ -590,6 +571,44 @@ extern "C" {
pub fn to_string(this: &Error) -> JsString;
}
// Float32Array
#[wasm_bindgen]
extern "C" {
pub type Float32Array;
/// The `Float32Array()` constructor creates an array of 32-bit floats.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array
#[wasm_bindgen(constructor)]
pub fn new(constructor_arg: JsValue) -> Float32Array;
/// The fill() method fills all the elements of an array from a start index
/// to an end index with a static value. The end index is not included.
///
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill
#[wasm_bindgen(method)]
pub fn fill(this: &Float32Array, value: f32, start: u32, end: u32) -> Float32Array;
}
// Float64Array
#[wasm_bindgen]
extern "C" {
pub type Float64Array;
/// The `Float64Array()` constructor creates an array of 64-bit floats.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float64Array
#[wasm_bindgen(constructor)]
pub fn new(constructor_arg: JsValue) -> Float64Array;
/// The fill() method fills all the elements of an array from a start index
/// to an end index with a static value. The end index is not included.
///
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill
#[wasm_bindgen(method)]
pub fn fill(this: &Float64Array, value: f64, start: u32, end: u32) -> Float64Array;
}
// Function
#[wasm_bindgen]
extern "C" {
@ -671,6 +690,63 @@ extern {
pub fn throw(this: &Generator, error: &Error) -> Result<JsValue, JsValue>;
}
// Int8Array
#[wasm_bindgen]
extern "C" {
pub type Int8Array;
/// The `Int8Array()` constructor creates an array of signed 8-bit integers.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int8Array
#[wasm_bindgen(constructor)]
pub fn new(constructor_arg: JsValue) -> Int8Array;
/// The fill() method fills all the elements of an array from a start index
/// to an end index with a static value. The end index is not included.
///
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill
#[wasm_bindgen(method)]
pub fn fill(this: &Int8Array, value: i8, start: u32, end: u32) -> Int8Array;
}
// Int16Array
#[wasm_bindgen]
extern "C" {
pub type Int16Array;
/// The `Int16Array()` constructor creates an array of signed 16-bit integers.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int16Array
#[wasm_bindgen(constructor)]
pub fn new(constructor_arg: JsValue) -> Int16Array;
/// The fill() method fills all the elements of an array from a start index
/// to an end index with a static value. The end index is not included.
///
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill
#[wasm_bindgen(method)]
pub fn fill(this: &Int16Array, value: i16, start: u32, end: u32) -> Int16Array;
}
// Int32Array
#[wasm_bindgen]
extern "C" {
pub type Int32Array;
/// The `Int32Array()` constructor creates an array of signed 32-bit integers.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int32Array
#[wasm_bindgen(constructor)]
pub fn new(constructor_arg: JsValue) -> Int32Array;
/// The fill() method fills all the elements of an array from a start index
/// to an end index with a static value. The end index is not included.
///
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill
#[wasm_bindgen(method)]
pub fn fill(this: &Int32Array, value: i32, start: u32, end: u32) -> Int32Array;
}
// Map
#[wasm_bindgen]
extern {
@ -1737,6 +1813,84 @@ extern {
pub fn values(set: &Set) -> SetIterator;
}
// Uint8Array
#[wasm_bindgen]
extern "C" {
pub type Uint8Array;
/// The `Uint8Array()` constructor creates an array of unsigned 8-bit integers.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array
#[wasm_bindgen(constructor)]
pub fn new(constructor_arg: JsValue) -> Uint8Array;
/// The fill() method fills all the elements of an array from a start index
/// to an end index with a static value. The end index is not included.
///
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill
#[wasm_bindgen(method)]
pub fn fill(this: &Uint8Array, value: u8, start: u32, end: u32) -> Uint8Array;
}
// Uint8ClampedArray
#[wasm_bindgen]
extern "C" {
pub type Uint8ClampedArray;
/// The `Uint8ClampedArray()` constructor creates an array of unsigned 8-bit integers clamped
/// to 0-255; if you specified a value that is out of the range of [0,255], 0 or 255 will be
/// set instead.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8ClampedArray
#[wasm_bindgen(constructor)]
pub fn new(constructor_arg: JsValue) -> Uint8ClampedArray;
/// The fill() method fills all the elements of an array from a start index
/// to an end index with a static value. The end index is not included.
///
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill
#[wasm_bindgen(method)]
pub fn fill(this: &Uint8ClampedArray, value: u8, start: u32, end: u32) -> Uint8ClampedArray;
}
// Uint16Array
#[wasm_bindgen]
extern "C" {
pub type Uint16Array;
/// The `Uint16Array()` constructor creates an array of unsigned 16-bit integers.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint16Array
#[wasm_bindgen(constructor)]
pub fn new(constructor_arg: JsValue) -> Uint16Array;
/// The fill() method fills all the elements of an array from a start index
/// to an end index with a static value. The end index is not included.
///
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill
#[wasm_bindgen(method)]
pub fn fill(this: &Uint16Array, value: u16, start: u32, end: u32) -> Uint16Array;
}
// Uint32Array
#[wasm_bindgen]
extern "C" {
pub type Uint32Array;
/// The `Uint32Array()` constructor creates an array of unsigned 32-bit integers.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array
#[wasm_bindgen(constructor)]
pub fn new(constructor_arg: JsValue) -> Uint32Array;
/// The fill() method fills all the elements of an array from a start index
/// to an end index with a static value. The end index is not included.
///
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill
#[wasm_bindgen(method)]
pub fn fill(this: &Uint32Array, value: u32, start: u32, end: u32) -> Uint32Array;
}
// WeakMap
#[wasm_bindgen]
extern "C" {

View File

@ -1,13 +1,10 @@
#![allow(non_snake_case)]
use project;
use std::string::String;
#[test]
fn new_undefined() {
project()
.file(
"src/lib.rs",
r#"
fn new_undefined_lib(array_type: &str) -> String {
format!(r#"
#![feature(use_extern_macros, wasm_custom_section)]
extern crate wasm_bindgen;
@ -15,13 +12,13 @@ fn new_undefined() {
use wasm_bindgen::js;
#[wasm_bindgen]
pub fn new_array() -> js::Uint8Array {
js::Uint8Array::new(JsValue::undefined())
pub fn new_array() -> js::{} {{
js::{}::new(JsValue::undefined())
}}
"#, array_type, array_type)
}
"#,
)
.file(
"test.js",
fn new_undefined_test_js() -> &'static str {
r#"
import * as assert from "assert";
import * as wasm from "./out";
@ -29,17 +26,83 @@ fn new_undefined() {
export function test() {
assert.equal(wasm.new_array().length, 0);
}
"#,
)
"#
}
#[test]
fn new_Uint8Array_undefined() {
project()
.file("src/lib.rs", &new_undefined_lib("Uint8Array"),)
.file("test.js", new_undefined_test_js(),)
.test()
}
#[test]
fn new_length() {
fn new_Uint8ClampedArray_undefined() {
project()
.file(
"src/lib.rs",
r#"
.file("src/lib.rs", &new_undefined_lib("Uint8ClampedArray"),)
.file("test.js", new_undefined_test_js(),)
.test()
}
#[test]
fn new_Uint16Array_undefined() {
project()
.file("src/lib.rs", &new_undefined_lib("Uint16Array"),)
.file("test.js", new_undefined_test_js(),)
.test()
}
#[test]
fn new_Uint32Array_undefined() {
project()
.file("src/lib.rs", &new_undefined_lib("Uint32Array"),)
.file("test.js", new_undefined_test_js(),)
.test()
}
#[test]
fn new_Int8Array_undefined() {
project()
.file("src/lib.rs", &new_undefined_lib("Int8Array"),)
.file("test.js", new_undefined_test_js(),)
.test()
}
#[test]
fn new_Int16Array_undefined() {
project()
.file("src/lib.rs", &new_undefined_lib("Int16Array"),)
.file("test.js", new_undefined_test_js(),)
.test()
}
#[test]
fn new_Int32Array_undefined() {
project()
.file("src/lib.rs", &new_undefined_lib("Int32Array"),)
.file("test.js", new_undefined_test_js(),)
.test()
}
#[test]
fn new_Float32Array_undefined() {
project()
.file("src/lib.rs", &new_undefined_lib("Float32Array"),)
.file("test.js", new_undefined_test_js(),)
.test()
}
#[test]
fn new_Float64Array_undefined() {
project()
.file("src/lib.rs", &new_undefined_lib("Float64Array"),)
.file("test.js", new_undefined_test_js(),)
.test()
}
fn new_length_lib(array_type: &str) -> String {
format!(r#"
#![feature(use_extern_macros, wasm_custom_section)]
extern crate wasm_bindgen;
@ -47,13 +110,13 @@ fn new_length() {
use wasm_bindgen::js;
#[wasm_bindgen]
pub fn new_array() -> js::Uint8Array {
js::Uint8Array::new(JsValue::from_f64(4.0))
pub fn new_array() -> js::{} {{
js::{}::new(JsValue::from_f64(4.0))
}}
"#, array_type, array_type)
}
"#,
)
.file(
"test.js",
fn new_length_test_js() -> &'static str {
r#"
import * as assert from "assert";
import * as wasm from "./out";
@ -61,15 +124,83 @@ fn new_length() {
export function test() {
assert.equal(wasm.new_array().length, 4);
}
"#,
)
"#
}
#[test]
fn new_Uint8Array_length() {
project()
.file("src/lib.rs", &new_length_lib("Uint8Array"),)
.file("test.js", new_length_test_js(),)
.test()
}
#[test]
fn fill() {
fn new_Uint8ClampedArray_length() {
project()
.file("src/lib.rs", r#"
.file("src/lib.rs", &new_length_lib("Uint8ClampedArray"),)
.file("test.js", new_length_test_js(),)
.test()
}
#[test]
fn new_Uint16Array_length() {
project()
.file("src/lib.rs", &new_length_lib("Uint16Array"),)
.file("test.js", new_length_test_js(),)
.test()
}
#[test]
fn new_Uint32Array_length() {
project()
.file("src/lib.rs", &new_length_lib("Uint32Array"),)
.file("test.js", new_length_test_js(),)
.test()
}
#[test]
fn new_Int8Array_length() {
project()
.file("src/lib.rs", &new_length_lib("Int8Array"),)
.file("test.js", new_length_test_js(),)
.test()
}
#[test]
fn new_Int16Array_length() {
project()
.file("src/lib.rs", &new_length_lib("Int16Array"),)
.file("test.js", new_length_test_js(),)
.test()
}
#[test]
fn new_Int32Array_length() {
project()
.file("src/lib.rs", &new_length_lib("Int32Array"),)
.file("test.js", new_length_test_js(),)
.test()
}
#[test]
fn new_Float32Array_length() {
project()
.file("src/lib.rs", &new_length_lib("Float32Array"),)
.file("test.js", new_length_test_js(),)
.test()
}
#[test]
fn new_Float64Array_length() {
project()
.file("src/lib.rs", &new_length_lib("Float64Array"),)
.file("test.js", new_length_test_js(),)
.test()
}
fn fill_lib(array_type: &str, el_type: &str) -> String {
format!(r#"
#![feature(use_extern_macros, wasm_custom_section)]
extern crate wasm_bindgen;
@ -77,21 +208,95 @@ fn fill() {
use wasm_bindgen::js;
#[wasm_bindgen]
pub fn fill_with(this: &js::Uint8Array, value: JsValue, start: u32, end: u32) -> js::Uint8Array {
pub fn fill_with(this: &js::{}, value: {}, start: u32, end: u32) -> js::{} {{
this.fill(value, start, end)
}}
"#, array_type, el_type, array_type)
}
"#)
.file("test.js", r#"
fn fill_test_js(array_type: &str) -> String {
format!(r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
let characters = new Uint8Array([0, 0, 0, 0, 0, 0]);
export function test() {{
let characters = new {}([0, 0, 0, 0, 0, 0]);
let subset = wasm.fill_with(characters, 1, 0, 3);
assert.equal(subset[0], 1);
assert.equal(subset[4], 0);
}}
"#, array_type)
}
"#)
#[test]
fn fill_Uint8Array() {
project()
.file("src/lib.rs", &fill_lib("Uint8Array", "u8"),)
.file("test.js", &fill_test_js("Uint8Array"))
.test()
}
#[test]
fn fill_Uint8ClampedArray() {
project()
.file("src/lib.rs", &fill_lib("Uint8ClampedArray", "u8"),)
.file("test.js", &fill_test_js("Uint8ClampedArray"))
.test()
}
#[test]
fn fill_Uint16Array() {
project()
.file("src/lib.rs", &fill_lib("Uint16Array", "u16"),)
.file("test.js", &fill_test_js("Uint16Array"))
.test()
}
#[test]
fn fill_Uint32Array() {
project()
.file("src/lib.rs", &fill_lib("Uint32Array", "u32"),)
.file("test.js", &fill_test_js("Uint32Array"))
.test()
}
#[test]
fn fill_Int8Array() {
project()
.file("src/lib.rs", &fill_lib("Int8Array", "i8"),)
.file("test.js", &fill_test_js("Int8Array"))
.test()
}
#[test]
fn fill_Int16Array() {
project()
.file("src/lib.rs", &fill_lib("Int16Array", "i16"),)
.file("test.js", &fill_test_js("Int16Array"))
.test()
}
#[test]
fn fill_Int32Array() {
project()
.file("src/lib.rs", &fill_lib("Int32Array", "i32"),)
.file("test.js", &fill_test_js("Int32Array"))
.test()
}
#[test]
fn fill_Float32Array() {
project()
.file("src/lib.rs", &fill_lib("Float32Array", "f32"),)
.file("test.js", &fill_test_js("Float32Array"))
.test()
}
#[test]
fn fill_Float64Array() {
project()
.file("src/lib.rs", &fill_lib("Float64Array", "f64"),)
.file("test.js", &fill_test_js("Float64Array"))
.test()
}