From 8b6e951e3479a80f8433e7eb678ae4f2a5583167 Mon Sep 17 00:00:00 2001 From: Pauan Date: Mon, 4 Feb 2019 01:32:43 +0100 Subject: [PATCH] Adding in Reflect::get_f64, Reflect::get_u32, Reflect::set_f64, and Reflect::set_u32 --- crates/js-sys/src/lib.rs | 16 +++++++++++ crates/js-sys/tests/wasm/Reflect.rs | 44 +++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 556b827f6be..6118b604466 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -2654,6 +2654,14 @@ extern "C" { #[wasm_bindgen(static_method_of = Reflect, catch)] pub fn get(target: &JsValue, key: &JsValue) -> Result; + /// The same as [`Reflect::get`](#method.get) except the key is an `f64`, which is slightly faster. + #[wasm_bindgen(static_method_of = Reflect, js_name = "get", catch)] + pub fn get_f64(target: &JsValue, key: f64) -> Result; + + /// The same as [`Reflect::get`](#method.get) except the key is a `u32`, which is slightly faster. + #[wasm_bindgen(static_method_of = Reflect, js_name = "get", catch)] + pub fn get_u32(target: &JsValue, key: u32) -> Result; + /// The static `Reflect.getOwnPropertyDescriptor()` method is similar to /// `Object.getOwnPropertyDescriptor()`. It returns a property descriptor /// of the given property if it exists on the object, `undefined` otherwise. @@ -2712,6 +2720,14 @@ extern "C" { #[wasm_bindgen(static_method_of = Reflect, catch)] pub fn set(target: &JsValue, property_key: &JsValue, value: &JsValue) -> Result; + /// The same as [`Reflect::set`](#method.set) except the key is an `f64`, which is slightly faster. + #[wasm_bindgen(static_method_of = Reflect, js_name = "set", catch)] + pub fn set_f64(target: &JsValue, property_key: f64, value: &JsValue) -> Result; + + /// The same as [`Reflect::set`](#method.set) except the key is a `u32`, which is slightly faster. + #[wasm_bindgen(static_method_of = Reflect, js_name = "set", catch)] + pub fn set_u32(target: &JsValue, property_key: u32, value: &JsValue) -> Result; + /// The static `Reflect.set()` method works like setting a /// property on an object. /// diff --git a/crates/js-sys/tests/wasm/Reflect.rs b/crates/js-sys/tests/wasm/Reflect.rs index 9a7b8fb1e6c..23be554ac2e 100644 --- a/crates/js-sys/tests/wasm/Reflect.rs +++ b/crates/js-sys/tests/wasm/Reflect.rs @@ -105,6 +105,22 @@ fn get() { assert_eq!(Reflect::get(&obj, &"x".into()).unwrap(), 10); } +#[wasm_bindgen_test] +fn get_f64() { + let a = Array::new(); + assert_eq!(Reflect::get_f64(&a, 0.0).unwrap(), JsValue::UNDEFINED); + assert_eq!(a.push(&JsValue::from_str("Hi!")), 1); + assert_eq!(Reflect::get_f64(&a, 0.0).unwrap(), JsValue::from_str("Hi!")); +} + +#[wasm_bindgen_test] +fn get_u32() { + let a = Array::new(); + assert_eq!(Reflect::get_u32(&a, 0).unwrap(), JsValue::UNDEFINED); + assert_eq!(a.push(&JsValue::from_str("Hi!")), 1); + assert_eq!(Reflect::get_u32(&a, 0).unwrap(), JsValue::from_str("Hi!")); +} + #[wasm_bindgen_test] fn get_own_property_descriptor() { let r = Rectangle::new(); @@ -166,6 +182,30 @@ fn set() { assert_eq!(Reflect::get(&obj, &"key".into()).unwrap(), "value"); } +#[wasm_bindgen_test] +fn set_f64() { + let a = Array::new(); + a.push(&JsValue::from_str("Hi!")); + + assert_eq!(Reflect::get_f64(&a, 0.0).unwrap(), JsValue::from_str("Hi!")); + + Reflect::set_f64(&a, 0.0, &JsValue::from_str("Bye!")).unwrap(); + + assert_eq!(Reflect::get_f64(&a, 0.0).unwrap(), JsValue::from_str("Bye!")); +} + +#[wasm_bindgen_test] +fn set_u32() { + let a = Array::new(); + a.push(&JsValue::from_str("Hi!")); + + assert_eq!(Reflect::get_u32(&a, 0).unwrap(), JsValue::from_str("Hi!")); + + Reflect::set_u32(&a, 0, &JsValue::from_str("Bye!")).unwrap(); + + assert_eq!(Reflect::get_u32(&a, 0).unwrap(), JsValue::from_str("Bye!")); +} + #[wasm_bindgen_test] fn set_with_receiver() { let obj1 = JsValue::from(Object::new()); @@ -209,6 +249,8 @@ fn reflect_bindings_handle_proxies_that_just_throw_for_everything() { assert!(Reflect::delete_property(&p, &"a".into()).is_err()); assert!(Reflect::get(p.as_ref(), &"a".into()).is_err()); + assert!(Reflect::get_f64(p.as_ref(), 0.0).is_err()); + assert!(Reflect::get_u32(p.as_ref(), 0).is_err()); assert!(Reflect::get_own_property_descriptor(&p, &"a".into()).is_err()); @@ -223,6 +265,8 @@ fn reflect_bindings_handle_proxies_that_just_throw_for_everything() { assert!(Reflect::prevent_extensions(&p).is_err()); assert!(Reflect::set(p.as_ref(), &"a".into(), &1.into()).is_err()); + assert!(Reflect::set_f64(p.as_ref(), 0.0, &1.into()).is_err()); + assert!(Reflect::set_u32(p.as_ref(), 0, &1.into()).is_err()); assert!(Reflect::set_prototype_of(&p, Object::new().as_ref()).is_err()); }