Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get/set for TypedArrays. #2001

Merged
merged 3 commits into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions crates/js-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4884,6 +4884,14 @@ macro_rules! arrays {
/// input values from a specified array.
#[wasm_bindgen(method)]
pub fn set(this: &$name, src: &JsValue, offset: u32);

/// Gets the value at `idx`, equivalent to the javascript `my_var = arr[idx]`.
#[wasm_bindgen(method, structural, indexing_getter)]
pub fn get_index(this: &$name, idx: u32) -> $ty;

/// Sets the value at `idx`, equivalent to the javascript `arr[idx] = value`.
#[wasm_bindgen(method, structural, indexing_setter)]
pub fn set_index(this: &$name, idx: u32, value: $ty);
}

impl $name {
Expand Down
13 changes: 13 additions & 0 deletions crates/js-sys/tests/wasm/TypedArray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ fn new_fill() {
each!(test_fill);
}

macro_rules! test_get_set {
($arr:ident) => {{
let arr = $arr::new(&1.into());
assert_eq!(arr.get_index(0) as f64, 0 as f64);
arr.set_index(0, 1 as _);
assert_eq!(arr.get_index(0) as f64, 1 as f64);
}};
}
#[wasm_bindgen_test]
fn new_get_set() {
each!(test_get_set);
}

macro_rules! test_slice {
($arr:ident) => {{
let arr = $arr::new(&4.into());
Expand Down