@@ -4657,7 +4657,7 @@ pub fn global() -> Object {
4657
4657
}
4658
4658
4659
4659
macro_rules! arrays {
4660
- ( $( #[ doc = $ctor: literal] #[ doc = $mdn: literal] $name: ident: $ty: ident, ) * ) => ( $(
4660
+ ( $( #[ doc = $ctor: literal] #[ doc = $mdn: literal] $name: ident: $ty: ident => $zero : literal , ) * ) => ( $(
4661
4661
#[ wasm_bindgen]
4662
4662
extern "C" {
4663
4663
#[ wasm_bindgen( extends = Object ) ]
@@ -4792,6 +4792,14 @@ macro_rules! arrays {
4792
4792
)
4793
4793
}
4794
4794
4795
+ fn raw_copy_to( & self , dst: & mut [ $ty] ) {
4796
+ let buf = wasm_bindgen:: memory( ) ;
4797
+ let mem = buf. unchecked_ref:: <WebAssembly :: Memory >( ) ;
4798
+ let all_wasm_memory = $name:: new( & mem. buffer( ) ) ;
4799
+ let offset = dst. as_ptr( ) as usize / mem:: size_of:: <$ty>( ) ;
4800
+ all_wasm_memory. set( self , offset as u32 ) ;
4801
+ }
4802
+
4795
4803
/// Copy the contents of this JS typed array into the destination
4796
4804
/// Rust slice.
4797
4805
///
@@ -4805,11 +4813,14 @@ macro_rules! arrays {
4805
4813
/// different than the length of the provided `dst` array.
4806
4814
pub fn copy_to( & self , dst: & mut [ $ty] ) {
4807
4815
assert_eq!( self . length( ) as usize , dst. len( ) ) ;
4808
- let buf = wasm_bindgen:: memory( ) ;
4809
- let mem = buf. unchecked_ref:: <WebAssembly :: Memory >( ) ;
4810
- let all_wasm_memory = $name:: new( & mem. buffer( ) ) ;
4811
- let offset = dst. as_ptr( ) as usize / mem:: size_of:: <$ty>( ) ;
4812
- all_wasm_memory. set( self , offset as u32 ) ;
4816
+ self . raw_copy_to( dst) ;
4817
+ }
4818
+
4819
+ /// Efficiently copies the contents of this JS typed array into a new Vec.
4820
+ pub fn to_vec( & self ) -> Vec <$ty> {
4821
+ let mut output = vec![ $zero; self . length( ) as usize ] ;
4822
+ self . raw_copy_to( & mut output) ;
4823
+ output
4813
4824
}
4814
4825
}
4815
4826
@@ -4826,37 +4837,37 @@ macro_rules! arrays {
4826
4837
arrays ! {
4827
4838
/// `Int8Array()`
4828
4839
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int8Array
4829
- Int8Array : i8 ,
4840
+ Int8Array : i8 => 0 ,
4830
4841
4831
4842
/// `Int16Array()`
4832
4843
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int16Array
4833
- Int16Array : i16 ,
4844
+ Int16Array : i16 => 0 ,
4834
4845
4835
4846
/// `Int32Array()`
4836
4847
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int32Array
4837
- Int32Array : i32 ,
4848
+ Int32Array : i32 => 0 ,
4838
4849
4839
4850
/// `Uint8Array()`
4840
4851
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array
4841
- Uint8Array : u8 ,
4852
+ Uint8Array : u8 => 0 ,
4842
4853
4843
4854
/// `Uint8ClampedArray()`
4844
4855
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8ClampedArray
4845
- Uint8ClampedArray : u8 ,
4856
+ Uint8ClampedArray : u8 => 0 ,
4846
4857
4847
4858
/// `Uint16Array()`
4848
4859
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint16Array
4849
- Uint16Array : u16 ,
4860
+ Uint16Array : u16 => 0 ,
4850
4861
4851
4862
/// `Uint32Array()`
4852
4863
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array
4853
- Uint32Array : u32 ,
4864
+ Uint32Array : u32 => 0 ,
4854
4865
4855
4866
/// `Float32Array()`
4856
4867
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array
4857
- Float32Array : f32 ,
4868
+ Float32Array : f32 => 0.0 ,
4858
4869
4859
4870
/// `Float64Array()`
4860
4871
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float64Array
4861
- Float64Array : f64 ,
4872
+ Float64Array : f64 => 0.0 ,
4862
4873
}
0 commit comments