Merge pull request #1620 from Pauan/add-typed-array-into

Add From impl for TypedArrays
This commit is contained in:
Alex Crichton 2019-06-26 08:29:23 +02:00 committed by GitHub
commit 250e84f091
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 8 deletions

View File

@ -4589,6 +4589,14 @@ macro_rules! arrays {
all_wasm_memory.set(self, offset as u32);
}
}
impl<'a> From<&'a [$ty]> for $name {
#[inline]
fn from(slice: &'a [$ty]) -> $name {
// This is safe because the `new` function makes a copy if its argument is a TypedArray
unsafe { $name::new(&$name::view(slice)) }
}
}
)*);
}

View File

@ -111,6 +111,16 @@ fn view() {
});
}
#[wasm_bindgen_test]
fn from() {
let x: Vec<i32> = vec![1, 2, 3];
let array = Int32Array::from(x.as_slice());
assert_eq!(array.length(), 3);
array.for_each(&mut |x, i, _| {
assert_eq!(x, (i + 1) as i32);
});
}
#[wasm_bindgen_test]
fn copy_to() {
let mut x = [0; 10];

View File

@ -19,18 +19,19 @@ const WASM: &[u8] = include_bytes!("add.wasm");
pub fn run() -> Result<(), JsValue> {
console_log!("instantiating a new wasm module directly");
// Note that `Uint8Array::view` this is somewhat dangerous (hence the
// Note that `Uint8Array::view` is somewhat dangerous (hence the
// `unsafe`!). This is creating a raw view into our module's
// `WebAssembly.Memory` buffer, but if we allocate more pages for ourself
// (aka do a memory allocation in Rust) it'll cause the buffer to change,
// causing the `Uint8Array` to be invalid.
//
// As a result, after `Uint8Array::view` we have to be very careful not to
// do any memory allocations before it's next used.
// do any memory allocations before it's dropped.
let a = unsafe {
let array = Uint8Array::view(WASM);
WebAssembly::Module::new(array.as_ref())?
};
let b = WebAssembly::Instance::new(&a, &Object::new())?;
let c = b.exports();

View File

@ -39,12 +39,25 @@ pub fn start() -> Result<(), JsValue> {
let buffer = context.create_buffer().ok_or("failed to create buffer")?;
context.bind_buffer(WebGlRenderingContext::ARRAY_BUFFER, Some(&buffer));
let vert_array = unsafe { js_sys::Float32Array::view(&vertices) };
context.buffer_data_with_array_buffer_view(
WebGlRenderingContext::ARRAY_BUFFER,
&vert_array,
WebGlRenderingContext::STATIC_DRAW,
);
// Note that `Float32Array::view` is somewhat dangerous (hence the
// `unsafe`!). This is creating a raw view into our module's
// `WebAssembly.Memory` buffer, but if we allocate more pages for ourself
// (aka do a memory allocation in Rust) it'll cause the buffer to change,
// causing the `Float32Array` to be invalid.
//
// As a result, after `Float32Array::view` we have to be very careful not to
// do any memory allocations before it's dropped.
unsafe {
let vert_array = js_sys::Float32Array::view(&vertices);
context.buffer_data_with_array_buffer_view(
WebGlRenderingContext::ARRAY_BUFFER,
&vert_array,
WebGlRenderingContext::STATIC_DRAW,
);
}
context.vertex_attrib_pointer_with_i32(0, 3, WebGlRenderingContext::FLOAT, false, 0, 0);
context.enable_vertex_attrib_array(0);