From fe6ad5447e7d659c1e74970a3850434039ee16e6 Mon Sep 17 00:00:00 2001 From: Michael Hoffmann Date: Wed, 12 Sep 2018 07:39:39 +0200 Subject: [PATCH] Add binding for Object.defineProperty() --- crates/js-sys/src/lib.rs | 8 ++++++++ crates/js-sys/tests/wasm/Object.rs | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 48c1cddc4..a1e0da3da 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -1996,6 +1996,14 @@ extern "C" { #[wasm_bindgen(static_method_of = Object)] pub fn create(prototype: &Object) -> Object; + /// The static method Object.defineProperty() defines a new + /// property directly on an object, or modifies an existing + /// property on an object, and returns the object. + /// + /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty) + #[wasm_bindgen(static_method_of = Object, js_name = defineProperty)] + pub fn define_property(obj: &Object, prop: &JsValue, descriptor: &Object) -> Object; + /// The `Object.freeze()` method freezes an object: that is, prevents new /// properties from being added to it; prevents existing properties from /// being removed; and prevents existing properties, or their enumerability, diff --git a/crates/js-sys/tests/wasm/Object.rs b/crates/js-sys/tests/wasm/Object.rs index 97cc7b6aa..580b0cbe0 100644 --- a/crates/js-sys/tests/wasm/Object.rs +++ b/crates/js-sys/tests/wasm/Object.rs @@ -9,6 +9,10 @@ extern "C" { type Foo42; #[wasm_bindgen(method, setter, structural)] fn set_foo(this: &Foo42, val: JsValue); + + type DefinePropertyAttrs; + #[wasm_bindgen(method, setter, structural)] + fn set_value(this: &DefinePropertyAttrs, val: &JsValue); } #[wasm_bindgen(module = "tests/wasm/Object.js")] @@ -73,6 +77,16 @@ fn create() { assert!(my_array.is_instance_of::()); } +#[wasm_bindgen_test] +fn define_property() { + let value = DefinePropertyAttrs::from(JsValue::from(Object::new())); + value.set_value(&43.into()); + let descriptor = Object::from(JsValue::from(value)); + let foo = foo_42(); + let foo = Object::define_property(&foo, &"bar".into(), &descriptor); + assert!(foo.has_own_property(&"bar".into())); +} + #[wasm_bindgen_test] fn has_own_property() { assert!(foo_42().has_own_property(&"foo".into()));