Adding in to_vec method for typed arrays (#1844)

* Adding in to_vec method for typed arrays

* Fixing type error
This commit is contained in:
Pauan 2019-10-30 00:10:18 +01:00 committed by Alex Crichton
parent 6159d50eb6
commit 1f51831c3d
2 changed files with 33 additions and 15 deletions

View File

@ -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)]
@ -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.
///
@ -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
}
}
@ -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,
}

View File

@ -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]);
}