Skip to content

Commit 7db01a7

Browse files
authored
Add get/set for TypedArrays. (#2001)
* Add get/set for `TypedArray`s. * Change names. * Change name to @Pauan suggestion.
1 parent b619070 commit 7db01a7

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

crates/js-sys/src/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -4884,6 +4884,14 @@ macro_rules! arrays {
48844884
/// input values from a specified array.
48854885
#[wasm_bindgen(method)]
48864886
pub fn set(this: &$name, src: &JsValue, offset: u32);
4887+
4888+
/// Gets the value at `idx`, equivalent to the javascript `my_var = arr[idx]`.
4889+
#[wasm_bindgen(method, structural, indexing_getter)]
4890+
pub fn get_index(this: &$name, idx: u32) -> $ty;
4891+
4892+
/// Sets the value at `idx`, equivalent to the javascript `arr[idx] = value`.
4893+
#[wasm_bindgen(method, structural, indexing_setter)]
4894+
pub fn set_index(this: &$name, idx: u32, value: $ty);
48874895
}
48884896

48894897
impl $name {

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

+13
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,19 @@ fn new_fill() {
8989
each!(test_fill);
9090
}
9191

92+
macro_rules! test_get_set {
93+
($arr:ident) => {{
94+
let arr = $arr::new(&1.into());
95+
assert_eq!(arr.get_index(0) as f64, 0 as f64);
96+
arr.set_index(0, 1 as _);
97+
assert_eq!(arr.get_index(0) as f64, 1 as f64);
98+
}};
99+
}
100+
#[wasm_bindgen_test]
101+
fn new_get_set() {
102+
each!(test_get_set);
103+
}
104+
92105
macro_rules! test_slice {
93106
($arr:ident) => {{
94107
let arr = $arr::new(&4.into());

0 commit comments

Comments
 (0)