Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

js-sys: Add bindings for TypedArray.slice #956

Merged
merged 1 commit into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions crates/js-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,12 @@ extern "C" {
#[wasm_bindgen(method)]
pub fn subarray(this: &Float32Array, begin: u32, end: u32) -> Float32Array;

/// The `slice()` method returns a shallow copy of a portion of a typed
/// array into a new typed array object. This method has the same algorithm
/// as `Array.prototype.slice()`.
#[wasm_bindgen(method)]
pub fn slice(this: &Float32Array, begin: u32, end: u32) -> Uint8ClampedArray;

/// 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
Expand Down Expand Up @@ -772,6 +778,12 @@ extern "C" {
#[wasm_bindgen(method)]
pub fn subarray(this: &Float64Array, begin: u32, end: u32) -> Float64Array;

/// The `slice()` method returns a shallow copy of a portion of a typed
/// array into a new typed array object. This method has the same algorithm
/// as `Array.prototype.slice()`.
#[wasm_bindgen(method)]
pub fn slice(this: &Float64Array, begin: u32, end: u32) -> Uint8ClampedArray;

/// 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
Expand Down Expand Up @@ -960,6 +972,12 @@ extern "C" {
#[wasm_bindgen(method)]
pub fn subarray(this: &Int8Array, begin: u32, end: u32) -> Int8Array;

/// The `slice()` method returns a shallow copy of a portion of a typed
/// array into a new typed array object. This method has the same algorithm
/// as `Array.prototype.slice()`.
#[wasm_bindgen(method)]
pub fn slice(this: &Int8Array, begin: u32, end: u32) -> Uint8ClampedArray;

/// 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
Expand Down Expand Up @@ -1013,6 +1031,12 @@ extern "C" {
#[wasm_bindgen(method)]
pub fn subarray(this: &Int16Array, begin: u32, end: u32) -> Int16Array;

/// The `slice()` method returns a shallow copy of a portion of a typed
/// array into a new typed array object. This method has the same algorithm
/// as `Array.prototype.slice()`.
#[wasm_bindgen(method)]
pub fn slice(this: &Int16Array, begin: u32, end: u32) -> Uint8ClampedArray;

/// 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
Expand Down Expand Up @@ -1066,6 +1090,12 @@ extern "C" {
#[wasm_bindgen(method)]
pub fn subarray(this: &Int32Array, begin: u32, end: u32) -> Int32Array;

/// The `slice()` method returns a shallow copy of a portion of a typed
/// array into a new typed array object. This method has the same algorithm
/// as `Array.prototype.slice()`.
#[wasm_bindgen(method)]
pub fn slice(this: &Int32Array, begin: u32, end: u32) -> Uint8ClampedArray;

/// 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
Expand Down Expand Up @@ -2803,6 +2833,12 @@ extern "C" {
#[wasm_bindgen(method)]
pub fn subarray(this: &Uint8Array, begin: u32, end: u32) -> Uint8Array;

/// The `slice()` method returns a shallow copy of a portion of a typed
/// array into a new typed array object. This method has the same algorithm
/// as `Array.prototype.slice()`.
#[wasm_bindgen(method)]
pub fn slice(this: &Uint8Array, begin: u32, end: u32) -> Uint8ClampedArray;

/// 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
Expand Down Expand Up @@ -2858,6 +2894,12 @@ extern "C" {
#[wasm_bindgen(method)]
pub fn subarray(this: &Uint8ClampedArray, begin: u32, end: u32) -> Uint8ClampedArray;

/// The `slice()` method returns a shallow copy of a portion of a typed
/// array into a new typed array object. This method has the same algorithm
/// as `Array.prototype.slice()`.
#[wasm_bindgen(method)]
pub fn slice(this: &Uint8ClampedArray, begin: u32, end: u32) -> Uint8ClampedArray;

/// 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
Expand Down Expand Up @@ -2911,6 +2953,12 @@ extern "C" {
#[wasm_bindgen(method)]
pub fn subarray(this: &Uint16Array, begin: u32, end: u32) -> Uint16Array;

/// The `slice()` method returns a shallow copy of a portion of a typed
/// array into a new typed array object. This method has the same algorithm
/// as `Array.prototype.slice()`.
#[wasm_bindgen(method)]
pub fn slice(this: &Uint16Array, begin: u32, end: u32) -> Uint8ClampedArray;

/// 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
Expand Down Expand Up @@ -2964,6 +3012,12 @@ extern "C" {
#[wasm_bindgen(method)]
pub fn subarray(this: &Uint32Array, begin: u32, end: u32) -> Uint32Array;

/// The `slice()` method returns a shallow copy of a portion of a typed
/// array into a new typed array object. This method has the same algorithm
/// as `Array.prototype.slice()`.
#[wasm_bindgen(method)]
pub fn slice(this: &Uint32Array, begin: u32, end: u32) -> Uint8ClampedArray;

/// 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
Expand Down
12 changes: 12 additions & 0 deletions crates/js-sys/tests/wasm/TypedArray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,15 @@ macro_rules! test_fill {
fn new_fill() {
each!(test_fill);
}

macro_rules! test_slice {
($arr:ident) => {{
let arr = $arr::new(&4.into());
assert_eq!(arr.length(), 4);
assert_eq!(arr.slice(1, 2).length(), 1);
}};
}
#[wasm_bindgen_test]
fn new_slice() {
each!(test_slice);
}