Skip to content

Commit

Permalink
Adding in Reflect::get_f64, Reflect::get_u32, Reflect::set_f64, and R…
Browse files Browse the repository at this point in the history
…eflect::set_u32
  • Loading branch information
Pauan committed Feb 4, 2019
1 parent 9f00664 commit 8b6e951
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
16 changes: 16 additions & 0 deletions crates/js-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2654,6 +2654,14 @@ extern "C" {
#[wasm_bindgen(static_method_of = Reflect, catch)]
pub fn get(target: &JsValue, key: &JsValue) -> Result<JsValue, JsValue>;

/// 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<JsValue, JsValue>;

/// 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<JsValue, JsValue>;

/// 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.
Expand Down Expand Up @@ -2712,6 +2720,14 @@ extern "C" {
#[wasm_bindgen(static_method_of = Reflect, catch)]
pub fn set(target: &JsValue, property_key: &JsValue, value: &JsValue) -> Result<bool, JsValue>;

/// 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<bool, JsValue>;

/// 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<bool, JsValue>;

/// The static `Reflect.set()` method works like setting a
/// property on an object.
///
Expand Down
44 changes: 44 additions & 0 deletions crates/js-sys/tests/wasm/Reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());

Expand All @@ -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());
}

0 comments on commit 8b6e951

Please sign in to comment.