diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 858fedf1..13586bcf 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -627,6 +627,21 @@ extern "C" { /// types here. #[wasm_bindgen(method, js_name = forEach)] pub fn for_each(this: &Float32Array, callback: &mut FnMut(f32, u32, Float32Array)); + + /// The `length` accessor property represents the length (in elements) of a + /// typed array. + #[wasm_bindgen(method, getter)] + pub fn length(this: &Float32Array) -> u32; + + /// The `byteLength` accessor property represents the length (in bytes) of a + /// typed array. + #[wasm_bindgen(method, getter, js_name = byteLength)] + pub fn byte_length(this: &Float32Array) -> u32; + + /// The `byteOffset` accessor property represents the offset (in bytes) of a + /// typed array from the start of its `ArrayBuffer`. + #[wasm_bindgen(method, getter, js_name = byteOffset)] + pub fn byte_offset(this: &Float32Array) -> u32; } // Float64Array @@ -655,14 +670,29 @@ extern "C" { /// The `subarray()` method stores multiple values in the typed array, /// reading input values from a specified array. #[wasm_bindgen(method)] - pub fn subarray(this: &Float64Array, begin: u64, end: u64) -> Float64Array; + pub fn subarray(this: &Float64Array, begin: u32, end: u32) -> Float64Array; /// The `forEach()` method executes a provided function once per array /// element. This method has the same algorithm as /// `Array.prototype.forEach()`. `TypedArray` is one of the typed array /// types here. #[wasm_bindgen(method, js_name = forEach)] - pub fn for_each(this: &Float64Array, callback: &mut FnMut(f64, u64, Float64Array)); + pub fn for_each(this: &Float64Array, callback: &mut FnMut(f64, u32, Float64Array)); + + /// The `length` accessor property represents the length (in elements) of a + /// typed array. + #[wasm_bindgen(method, getter)] + pub fn length(this: &Float64Array) -> u32; + + /// The `byteLength` accessor property represents the length (in bytes) of a + /// typed array. + #[wasm_bindgen(method, getter, js_name = byteLength)] + pub fn byte_length(this: &Float64Array) -> u32; + + /// The `byteOffset` accessor property represents the offset (in bytes) of a + /// typed array from the start of its `ArrayBuffer`. + #[wasm_bindgen(method, getter, js_name = byteOffset)] + pub fn byte_offset(this: &Float64Array) -> u32; } // Function @@ -780,6 +810,21 @@ extern "C" { /// types here. #[wasm_bindgen(method, js_name = forEach)] pub fn for_each(this: &Int8Array, callback: &mut FnMut(i8, u32, Int8Array)); + + /// The `length` accessor property represents the length (in elements) of a + /// typed array. + #[wasm_bindgen(method, getter)] + pub fn length(this: &Int8Array) -> u32; + + /// The `byteLength` accessor property represents the length (in bytes) of a + /// typed array. + #[wasm_bindgen(method, getter, js_name = byteLength)] + pub fn byte_length(this: &Int8Array) -> u32; + + /// The `byteOffset` accessor property represents the offset (in bytes) of a + /// typed array from the start of its `ArrayBuffer`. + #[wasm_bindgen(method, getter, js_name = byteOffset)] + pub fn byte_offset(this: &Int8Array) -> u32; } // Int16Array @@ -816,6 +861,21 @@ extern "C" { /// types here. #[wasm_bindgen(method, js_name = forEach)] pub fn for_each(this: &Int16Array, callback: &mut FnMut(i16, u32, Int16Array)); + + /// The `length` accessor property represents the length (in elements) of a + /// typed array. + #[wasm_bindgen(method, getter)] + pub fn length(this: &Int16Array) -> u32; + + /// The `byteLength` accessor property represents the length (in bytes) of a + /// typed array. + #[wasm_bindgen(method, getter, js_name = byteLength)] + pub fn byte_length(this: &Int16Array) -> u32; + + /// The `byteOffset` accessor property represents the offset (in bytes) of a + /// typed array from the start of its `ArrayBuffer`. + #[wasm_bindgen(method, getter, js_name = byteOffset)] + pub fn byte_offset(this: &Int16Array) -> u32; } // Int32Array @@ -852,6 +912,21 @@ extern "C" { /// types here. #[wasm_bindgen(method, js_name = forEach)] pub fn for_each(this: &Int32Array, callback: &mut FnMut(i32, u32, Int32Array)); + + /// The `length` accessor property represents the length (in elements) of a + /// typed array. + #[wasm_bindgen(method, getter)] + pub fn length(this: &Int32Array) -> u32; + + /// The `byteLength` accessor property represents the length (in bytes) of a + /// typed array. + #[wasm_bindgen(method, getter, js_name = byteLength)] + pub fn byte_length(this: &Int32Array) -> u32; + + /// The `byteOffset` accessor property represents the offset (in bytes) of a + /// typed array from the start of its `ArrayBuffer`. + #[wasm_bindgen(method, getter, js_name = byteOffset)] + pub fn byte_offset(this: &Int32Array) -> u32; } // Map @@ -1988,6 +2063,21 @@ extern "C" { /// types here. #[wasm_bindgen(method, js_name = forEach)] pub fn for_each(this: &Uint8Array, callback: &mut FnMut(u8, u32, Uint8Array)); + + /// The `length` accessor property represents the length (in elements) of a + /// typed array. + #[wasm_bindgen(method, getter)] + pub fn length(this: &Uint8Array) -> u32; + + /// The `byteLength` accessor property represents the length (in bytes) of a + /// typed array. + #[wasm_bindgen(method, getter, js_name = byteLength)] + pub fn byte_length(this: &Uint8Array) -> u32; + + /// The `byteOffset` accessor property represents the offset (in bytes) of a + /// typed array from the start of its `ArrayBuffer`. + #[wasm_bindgen(method, getter, js_name = byteOffset)] + pub fn byte_offset(this: &Uint8Array) -> u32; } // Uint8ClampedArray @@ -2026,6 +2116,21 @@ extern "C" { /// types here. #[wasm_bindgen(method, js_name = forEach)] pub fn for_each(this: &Uint8ClampedArray, callback: &mut FnMut(u8, u32, Uint8ClampedArray)); + + /// The `length` accessor property represents the length (in elements) of a + /// typed array. + #[wasm_bindgen(method, getter)] + pub fn length(this: &Uint8ClampedArray) -> u32; + + /// The `byteLength` accessor property represents the length (in bytes) of a + /// typed array. + #[wasm_bindgen(method, getter, js_name = byteLength)] + pub fn byte_length(this: &Uint8ClampedArray) -> u32; + + /// The `byteOffset` accessor property represents the offset (in bytes) of a + /// typed array from the start of its `ArrayBuffer`. + #[wasm_bindgen(method, getter, js_name = byteOffset)] + pub fn byte_offset(this: &Uint8ClampedArray) -> u32; } // Uint16Array @@ -2062,6 +2167,21 @@ extern "C" { /// types here. #[wasm_bindgen(method, js_name = forEach)] pub fn for_each(this: &Uint16Array, callback: &mut FnMut(u16, u32, Uint16Array)); + + /// The `length` accessor property represents the length (in elements) of a + /// typed array. + #[wasm_bindgen(method, getter)] + pub fn length(this: &Uint16Array) -> u32; + + /// The `byteLength` accessor property represents the length (in bytes) of a + /// typed array. + #[wasm_bindgen(method, getter, js_name = byteLength)] + pub fn byte_length(this: &Uint16Array) -> u32; + + /// The `byteOffset` accessor property represents the offset (in bytes) of a + /// typed array from the start of its `ArrayBuffer`. + #[wasm_bindgen(method, getter, js_name = byteOffset)] + pub fn byte_offset(this: &Uint16Array) -> u32; } // Uint32Array @@ -2098,6 +2218,21 @@ extern "C" { /// types here. #[wasm_bindgen(method, js_name = forEach)] pub fn for_each(this: &Uint32Array, callback: &mut FnMut(u32, u32, Uint32Array)); + + /// The `length` accessor property represents the length (in elements) of a + /// typed array. + #[wasm_bindgen(method, getter)] + pub fn length(this: &Uint32Array) -> u32; + + /// The `byteLength` accessor property represents the length (in bytes) of a + /// typed array. + #[wasm_bindgen(method, getter, js_name = byteLength)] + pub fn byte_length(this: &Uint32Array) -> u32; + + /// The `byteOffset` accessor property represents the offset (in bytes) of a + /// typed array from the start of its `ArrayBuffer`. + #[wasm_bindgen(method, getter, js_name = byteOffset)] + pub fn byte_offset(this: &Uint32Array) -> u32; } // WeakMap diff --git a/crates/js-sys/tests/all/TypedArray.rs b/crates/js-sys/tests/all/TypedArray.rs deleted file mode 100644 index 0042859a..00000000 --- a/crates/js-sys/tests/all/TypedArray.rs +++ /dev/null @@ -1,302 +0,0 @@ -#![allow(non_snake_case)] - -use project; -use std::string::String; - -fn new_undefined_lib(array_type: &str) -> String { - format!(r#" - #![feature(use_extern_macros)] - - extern crate wasm_bindgen; - extern crate js_sys; - use wasm_bindgen::prelude::*; - - #[wasm_bindgen] - pub fn new_array() -> js_sys::{} {{ - js_sys::{}::new(JsValue::undefined()) - }} - "#, array_type, array_type) -} - -fn new_undefined_test_js() -> &'static str { - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - 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_Uint8ClampedArray_undefined() { - project() - .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)] - - extern crate wasm_bindgen; - extern crate js_sys; - use wasm_bindgen::prelude::*; - - #[wasm_bindgen] - pub fn new_array() -> js_sys::{} {{ - js_sys::{}::new(JsValue::from_f64(4.0)) - }} - "#, array_type, array_type) -} - -fn new_length_test_js() -> &'static str { - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - 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 new_Uint8ClampedArray_length() { - project() - .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)] - - extern crate wasm_bindgen; - extern crate js_sys; - use wasm_bindgen::prelude::*; - - #[wasm_bindgen] - pub fn fill_with(this: &js_sys::{}, value: {}, start: u32, end: u32) -> js_sys::{} {{ - this.fill(value, start, end) - }} - "#, array_type, el_type, array_type) -} - -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 {}([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() -} diff --git a/crates/js-sys/tests/all/main.rs b/crates/js-sys/tests/all/main.rs index abb187dc..74d57434 100644 --- a/crates/js-sys/tests/all/main.rs +++ b/crates/js-sys/tests/all/main.rs @@ -12,7 +12,6 @@ fn project() -> project_builder::Project { mod ArrayIterator; mod Reflect; -mod TypedArray; mod WeakMap; mod WeakSet; mod WebAssembly; diff --git a/crates/js-sys/tests/wasm/TypedArray.rs b/crates/js-sys/tests/wasm/TypedArray.rs new file mode 100644 index 00000000..53b01c76 --- /dev/null +++ b/crates/js-sys/tests/wasm/TypedArray.rs @@ -0,0 +1,76 @@ +use wasm_bindgen::prelude::*; +use wasm_bindgen_test::*; +use js_sys::*; + +macro_rules! each { + ($m:ident) => ( + $m!(Uint8Array); + $m!(Uint8ClampedArray); + $m!(Uint16Array); + $m!(Uint32Array); + $m!(Int8Array); + $m!(Int16Array); + $m!(Int32Array); + $m!(Float32Array); + $m!(Float64Array); + ) +} + +macro_rules! test_undefined { + ($arr:ident) => ({ + let arr = $arr::new(JsValue::undefined()); + assert_eq!(arr.length(), 0); + assert_eq!(arr.byte_length(), 0); + assert_eq!(arr.byte_offset(), 0); + assert!(JsValue::from(arr.buffer()).is_object()); + }) +} +#[wasm_bindgen_test] +fn new_undefined() { + each!(test_undefined); +} + +macro_rules! test_length { + ($arr:ident) => ({ + let arr = $arr::new(4.into()); + assert_eq!(arr.length(), 4); + assert!(arr.byte_length() != 0); + assert_eq!(arr.byte_offset(), 0); + assert!(JsValue::from(arr.buffer()).is_object()); + }) +} +#[wasm_bindgen_test] +fn new_length() { + each!(test_length); +} + +macro_rules! test_subarray { + ($arr:ident) => ({ + assert_eq!($arr::new(4.into()).subarray(0, 1).length(), 1); + }) +} +#[wasm_bindgen_test] +fn new_subarray() { + each!(test_subarray); +} + +macro_rules! test_fill { + ($arr:ident) => ({ + let arr = $arr::new(4.into()); + arr.for_each(&mut |x, _, _| { + assert_eq!(x as f64, 0.0); + }); + arr.fill(2 as _, 0, 2); + arr.for_each(&mut |x, i, _| { + if i < 2 { + assert_eq!(x as f64, 2.0); + } else { + assert_eq!(x as f64, 0.0); + } + }); + }) +} +#[wasm_bindgen_test] +fn new_fill() { + each!(test_fill); +} diff --git a/crates/js-sys/tests/wasm/main.rs b/crates/js-sys/tests/wasm/main.rs index d0df75ce..72c0ed7a 100644 --- a/crates/js-sys/tests/wasm/main.rs +++ b/crates/js-sys/tests/wasm/main.rs @@ -26,3 +26,4 @@ pub mod Proxy; pub mod Set; pub mod SetIterator; pub mod Symbol; +pub mod TypedArray;