Skip to content

Commit

Permalink
Adding in to_vec method for typed arrays (#1844)
Browse files Browse the repository at this point in the history
* Adding in to_vec method for typed arrays

* Fixing type error
  • Loading branch information
Pauan authored and alexcrichton committed Oct 29, 2019
1 parent 6159d50 commit 1f51831
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
41 changes: 26 additions & 15 deletions crates/js-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4657,7 +4657,7 @@ pub fn global() -> Object {
}

macro_rules! arrays {
($(#[doc = $ctor:literal] #[doc = $mdn:literal] $name:ident: $ty:ident,)*) => ($(
($(#[doc = $ctor:literal] #[doc = $mdn:literal] $name:ident: $ty:ident => $zero:literal,)*) => ($(
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = Object)]
Expand Down Expand Up @@ -4792,6 +4792,14 @@ macro_rules! arrays {
)
}

fn raw_copy_to(&self, dst: &mut [$ty]) {
let buf = wasm_bindgen::memory();
let mem = buf.unchecked_ref::<WebAssembly::Memory>();
let all_wasm_memory = $name::new(&mem.buffer());
let offset = dst.as_ptr() as usize / mem::size_of::<$ty>();
all_wasm_memory.set(self, offset as u32);
}

/// Copy the contents of this JS typed array into the destination
/// Rust slice.
///
Expand All @@ -4805,11 +4813,14 @@ macro_rules! arrays {
/// different than the length of the provided `dst` array.
pub fn copy_to(&self, dst: &mut [$ty]) {
assert_eq!(self.length() as usize, dst.len());
let buf = wasm_bindgen::memory();
let mem = buf.unchecked_ref::<WebAssembly::Memory>();
let all_wasm_memory = $name::new(&mem.buffer());
let offset = dst.as_ptr() as usize / mem::size_of::<$ty>();
all_wasm_memory.set(self, offset as u32);
self.raw_copy_to(dst);
}

/// Efficiently copies the contents of this JS typed array into a new Vec.
pub fn to_vec(&self) -> Vec<$ty> {
let mut output = vec![$zero; self.length() as usize];
self.raw_copy_to(&mut output);
output
}
}

Expand All @@ -4826,37 +4837,37 @@ macro_rules! arrays {
arrays! {
/// `Int8Array()`
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int8Array
Int8Array: i8,
Int8Array: i8 => 0,

/// `Int16Array()`
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int16Array
Int16Array: i16,
Int16Array: i16 => 0,

/// `Int32Array()`
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int32Array
Int32Array: i32,
Int32Array: i32 => 0,

/// `Uint8Array()`
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array
Uint8Array: u8,
Uint8Array: u8 => 0,

/// `Uint8ClampedArray()`
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8ClampedArray
Uint8ClampedArray: u8,
Uint8ClampedArray: u8 => 0,

/// `Uint16Array()`
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint16Array
Uint16Array: u16,
Uint16Array: u16 => 0,

/// `Uint32Array()`
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array
Uint32Array: u32,
Uint32Array: u32 => 0,

/// `Float32Array()`
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array
Float32Array: f32,
Float32Array: f32 => 0.0,

/// `Float64Array()`
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float64Array
Float64Array: f64,
Float64Array: f64 => 0.0,
}
7 changes: 7 additions & 0 deletions crates/js-sys/tests/wasm/TypedArray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,10 @@ fn copy_to() {
assert_eq!(*i, 5);
}
}

#[wasm_bindgen_test]
fn to_vec() {
let array = Int32Array::new(&10.into());
array.fill(5, 0, 10);
assert_eq!(array.to_vec(), vec![5, 5, 5, 5, 5, 5, 5, 5, 5, 5]);
}

0 comments on commit 1f51831

Please sign in to comment.