mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-12-02 21:25:16 +03:00
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:
parent
e7bfa161e0
commit
d51f539d1a
1
.gitignore
vendored
1
.gitignore
vendored
@ -9,3 +9,4 @@ yarn.lock
|
||||
*.d.ts
|
||||
/publish
|
||||
/publish.exe
|
||||
.vscode
|
||||
|
@ -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>();
|
||||
|
6
crates/js-sys/tests/wasm/Array.js
Normal file
6
crates/js-sys/tests/wasm/Array.js
Normal 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;
|
||||
}
|
||||
};
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user