Add an unsafe method view_mut_raw (#1850)

* adding .vscode folder to .gitignore

* Adding view_mut_raw to generated arrays

* test populating rust vector from JS  function

* Uint32Array test, need to make it generic

* Add doc + more test cases

* replacing macro-generated tests with generic test function

it is cleaner, safer and better that way

* improving rustdoc
This commit is contained in:
lshlyapnikov 2019-11-06 22:37:26 +04:00 committed by Alex Crichton
parent e7bfa161e0
commit d51f539d1a
4 changed files with 117 additions and 0 deletions

1
.gitignore vendored
View File

@ -9,3 +9,4 @@ yarn.lock
*.d.ts
/publish
/publish.exe
.vscode

View File

@ -4792,6 +4792,31 @@ macro_rules! arrays {
)
}
/// Creates a JS typed array which is a view into wasm's linear
/// memory at the specified pointer with specified length.
///
/// This function returns a new typed array which is a view into
/// wasm's memory. This view does not copy the underlying data.
///
/// # Unsafety
///
/// Views into WebAssembly memory are only valid so long as the
/// backing buffer isn't resized in JS. Once this function is called
/// any future calls to `Box::new` (or malloc of any form) may cause
/// the returned value here to be invalidated. Use with caution!
///
/// Additionally the returned object can be safely mutated,
/// the changes are guranteed to be reflected in the input array.
pub unsafe fn view_mut_raw(ptr: *mut $ty, length: usize) -> $name {
let buf = wasm_bindgen::memory();
let mem = buf.unchecked_ref::<WebAssembly::Memory>();
$name::new_with_byte_offset_and_length(
&mem.buffer(),
ptr as u32,
length as u32
)
}
fn raw_copy_to(&self, dst: &mut [$ty]) {
let buf = wasm_bindgen::memory();
let mem = buf.unchecked_ref::<WebAssembly::Memory>();

View File

@ -0,0 +1,6 @@
// Used for `Array.rs` tests
exports.populate_array = function(arr, start, len) {
for (i = 0; i < len; i++) {
arr[i] = start + i;
}
};

View File

@ -1,5 +1,6 @@
use js_sys::*;
use std::iter::FromIterator;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use wasm_bindgen::JsValue;
use wasm_bindgen_test::*;
@ -466,3 +467,87 @@ fn array_inheritance() {
assert!(array.is_instance_of::<Object>());
let _: &Object = array.as_ref();
}
#[wasm_bindgen(module = "tests/wasm/Array.js")]
extern "C" {
fn populate_array(arr: JsValue, start: JsValue, len: JsValue) -> JsValue;
}
fn test_array_view_mut_raw<ElemT: std::cmp::PartialEq + std::fmt::Debug, ArrT>(
sut: unsafe fn(*mut ElemT, usize) -> ArrT,
u8ToElem: fn(u8) -> ElemT,
arrToJsValue: fn(ArrT) -> JsValue,
) {
let start: u8 = 10;
let len: usize = 32;
let end: u8 = start + len as u8;
let mut buffer: Vec<ElemT> = Vec::new();
buffer.reserve(len);
unsafe {
let array: ArrT = sut(buffer.as_mut_ptr(), len);
populate_array(
arrToJsValue(array),
JsValue::from(start),
JsValue::from(len as u32),
);
buffer.set_len(len);
}
let expected: Vec<ElemT> = (start..end).map(u8ToElem).collect();
assert_eq!(buffer, expected)
}
#[wasm_bindgen_test]
fn Int8Array_view_mut_raw() {
fn u8Toi8_unsafe(x: u8) -> i8 {
x as i8
}
test_array_view_mut_raw(
js_sys::Int8Array::view_mut_raw,
u8Toi8_unsafe,
JsValue::from,
);
}
#[wasm_bindgen_test]
fn Int16Array_view_mut_raw() {
test_array_view_mut_raw(js_sys::Int16Array::view_mut_raw, i16::from, JsValue::from);
}
#[wasm_bindgen_test]
fn Int32Array_view_mut_raw() {
test_array_view_mut_raw(js_sys::Int32Array::view_mut_raw, i32::from, JsValue::from);
}
#[wasm_bindgen_test]
fn Uint8Array_view_mut_raw() {
test_array_view_mut_raw(js_sys::Uint8Array::view_mut_raw, u8::from, JsValue::from);
}
#[wasm_bindgen_test]
fn Uint8ClampedArray_view_mut_raw() {
test_array_view_mut_raw(
js_sys::Uint8ClampedArray::view_mut_raw,
u8::from,
JsValue::from,
);
}
#[wasm_bindgen_test]
fn Uint16Array_view_mut_raw() {
test_array_view_mut_raw(js_sys::Uint16Array::view_mut_raw, u16::from, JsValue::from);
}
#[wasm_bindgen_test]
fn Uint32Array_view_mut_raw() {
test_array_view_mut_raw(js_sys::Uint32Array::view_mut_raw, u32::from, JsValue::from);
}
#[wasm_bindgen_test]
fn Float32Array_view_mut_raw() {
test_array_view_mut_raw(js_sys::Float32Array::view_mut_raw, f32::from, JsValue::from);
}
#[wasm_bindgen_test]
fn Float64Array_view_mut_raw() {
test_array_view_mut_raw(js_sys::Float64Array::view_mut_raw, f64::from, JsValue::from);
}