Skip to content

Commit

Permalink
Merge pull request #956 from alexcrichton/js-sys-typed-array-slice
Browse files Browse the repository at this point in the history
js-sys: Add bindings for `TypedArray.slice`
  • Loading branch information
alexcrichton authored Oct 10, 2018
2 parents 4993e45 + f9d2dbd commit e72ae12
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
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);
}

0 comments on commit e72ae12

Please sign in to comment.