Skip to content

Commit 250e84f

Browse files
authored
Merge pull request #1620 from Pauan/add-typed-array-into
Add From impl for TypedArrays
2 parents e106ca3 + 497c5ed commit 250e84f

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

crates/js-sys/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4589,6 +4589,14 @@ macro_rules! arrays {
45894589
all_wasm_memory.set(self, offset as u32);
45904590
}
45914591
}
4592+
4593+
impl<'a> From<&'a [$ty]> for $name {
4594+
#[inline]
4595+
fn from(slice: &'a [$ty]) -> $name {
4596+
// This is safe because the `new` function makes a copy if its argument is a TypedArray
4597+
unsafe { $name::new(&$name::view(slice)) }
4598+
}
4599+
}
45924600
)*);
45934601
}
45944602

crates/js-sys/tests/wasm/TypedArray.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ fn view() {
111111
});
112112
}
113113

114+
#[wasm_bindgen_test]
115+
fn from() {
116+
let x: Vec<i32> = vec![1, 2, 3];
117+
let array = Int32Array::from(x.as_slice());
118+
assert_eq!(array.length(), 3);
119+
array.for_each(&mut |x, i, _| {
120+
assert_eq!(x, (i + 1) as i32);
121+
});
122+
}
123+
114124
#[wasm_bindgen_test]
115125
fn copy_to() {
116126
let mut x = [0; 10];

examples/wasm-in-wasm/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@ const WASM: &[u8] = include_bytes!("add.wasm");
1919
pub fn run() -> Result<(), JsValue> {
2020
console_log!("instantiating a new wasm module directly");
2121

22-
// Note that `Uint8Array::view` this is somewhat dangerous (hence the
22+
// Note that `Uint8Array::view` is somewhat dangerous (hence the
2323
// `unsafe`!). This is creating a raw view into our module's
2424
// `WebAssembly.Memory` buffer, but if we allocate more pages for ourself
2525
// (aka do a memory allocation in Rust) it'll cause the buffer to change,
2626
// causing the `Uint8Array` to be invalid.
2727
//
2828
// As a result, after `Uint8Array::view` we have to be very careful not to
29-
// do any memory allocations before it's next used.
29+
// do any memory allocations before it's dropped.
3030
let a = unsafe {
3131
let array = Uint8Array::view(WASM);
3232
WebAssembly::Module::new(array.as_ref())?
3333
};
34+
3435
let b = WebAssembly::Instance::new(&a, &Object::new())?;
3536
let c = b.exports();
3637

examples/webgl/src/lib.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,25 @@ pub fn start() -> Result<(), JsValue> {
3939

4040
let buffer = context.create_buffer().ok_or("failed to create buffer")?;
4141
context.bind_buffer(WebGlRenderingContext::ARRAY_BUFFER, Some(&buffer));
42-
let vert_array = unsafe { js_sys::Float32Array::view(&vertices) };
43-
context.buffer_data_with_array_buffer_view(
44-
WebGlRenderingContext::ARRAY_BUFFER,
45-
&vert_array,
46-
WebGlRenderingContext::STATIC_DRAW,
47-
);
42+
43+
// Note that `Float32Array::view` is somewhat dangerous (hence the
44+
// `unsafe`!). This is creating a raw view into our module's
45+
// `WebAssembly.Memory` buffer, but if we allocate more pages for ourself
46+
// (aka do a memory allocation in Rust) it'll cause the buffer to change,
47+
// causing the `Float32Array` to be invalid.
48+
//
49+
// As a result, after `Float32Array::view` we have to be very careful not to
50+
// do any memory allocations before it's dropped.
51+
unsafe {
52+
let vert_array = js_sys::Float32Array::view(&vertices);
53+
54+
context.buffer_data_with_array_buffer_view(
55+
WebGlRenderingContext::ARRAY_BUFFER,
56+
&vert_array,
57+
WebGlRenderingContext::STATIC_DRAW,
58+
);
59+
}
60+
4861
context.vertex_attrib_pointer_with_i32(0, 3, WebGlRenderingContext::FLOAT, false, 0, 0);
4962
context.enable_vertex_attrib_array(0);
5063

0 commit comments

Comments
 (0)