From 575c9a33f7a1b8f359b4621220377e328a1d4c23 Mon Sep 17 00:00:00 2001 From: Richard Dodd Date: Fri, 14 Feb 2020 16:15:50 +0000 Subject: [PATCH 1/3] Add get/set for `TypedArray`s. --- crates/js-sys/src/lib.rs | 8 ++++++++ crates/js-sys/tests/wasm/TypedArray.rs | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 61eca1717a5..b227e2cb680 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -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_(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_(this: &$name, idx: u32, value: $ty); } impl $name { diff --git a/crates/js-sys/tests/wasm/TypedArray.rs b/crates/js-sys/tests/wasm/TypedArray.rs index fc7cc3c9ef1..62c19db7ece 100644 --- a/crates/js-sys/tests/wasm/TypedArray.rs +++ b/crates/js-sys/tests/wasm/TypedArray.rs @@ -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_(0) as f64, 0 as f64); + arr.set_(0, 1 as _); + assert_eq!(arr.get_(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()); From 9661b0e6edf49010177353db9342ef78db534d64 Mon Sep 17 00:00:00 2001 From: Richard Dodd Date: Tue, 18 Feb 2020 14:52:27 +0000 Subject: [PATCH 2/3] Change names. --- crates/js-sys/src/lib.rs | 4 ++-- crates/js-sys/tests/wasm/TypedArray.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index b227e2cb680..6a73befe1fc 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -4887,11 +4887,11 @@ macro_rules! arrays { /// Gets the value at `idx`, equivalent to the javascript `my_var = arr[idx]`. #[wasm_bindgen(method, structural, indexing_getter)] - pub fn get_(this: &$name, idx: u32) -> $ty; + pub fn get_array_like(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_(this: &$name, idx: u32, value: $ty); + pub fn set_array_like(this: &$name, idx: u32, value: $ty); } impl $name { diff --git a/crates/js-sys/tests/wasm/TypedArray.rs b/crates/js-sys/tests/wasm/TypedArray.rs index 62c19db7ece..128ddf4f282 100644 --- a/crates/js-sys/tests/wasm/TypedArray.rs +++ b/crates/js-sys/tests/wasm/TypedArray.rs @@ -92,9 +92,9 @@ fn new_fill() { macro_rules! test_get_set { ($arr:ident) => {{ let arr = $arr::new(&1.into()); - assert_eq!(arr.get_(0) as f64, 0 as f64); - arr.set_(0, 1 as _); - assert_eq!(arr.get_(0) as f64, 1 as f64); + assert_eq!(arr.get_array_like(0) as f64, 0 as f64); + arr.set_array_like(0, 1 as _); + assert_eq!(arr.get_array_like(0) as f64, 1 as f64); }}; } #[wasm_bindgen_test] From 099da8e72ac4d1be12481bf7793286b53b136f73 Mon Sep 17 00:00:00 2001 From: Richard Dodd Date: Tue, 18 Feb 2020 15:55:24 +0000 Subject: [PATCH 3/3] Change name to @pauan suggestion. --- crates/js-sys/src/lib.rs | 4 ++-- crates/js-sys/tests/wasm/TypedArray.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 6a73befe1fc..bc8acc99c3e 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -4887,11 +4887,11 @@ macro_rules! arrays { /// Gets the value at `idx`, equivalent to the javascript `my_var = arr[idx]`. #[wasm_bindgen(method, structural, indexing_getter)] - pub fn get_array_like(this: &$name, idx: u32) -> $ty; + 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_array_like(this: &$name, idx: u32, value: $ty); + pub fn set_index(this: &$name, idx: u32, value: $ty); } impl $name { diff --git a/crates/js-sys/tests/wasm/TypedArray.rs b/crates/js-sys/tests/wasm/TypedArray.rs index 128ddf4f282..a30457e5fe9 100644 --- a/crates/js-sys/tests/wasm/TypedArray.rs +++ b/crates/js-sys/tests/wasm/TypedArray.rs @@ -92,9 +92,9 @@ fn new_fill() { macro_rules! test_get_set { ($arr:ident) => {{ let arr = $arr::new(&1.into()); - assert_eq!(arr.get_array_like(0) as f64, 0 as f64); - arr.set_array_like(0, 1 as _); - assert_eq!(arr.get_array_like(0) as f64, 1 as f64); + 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]