Skip to content

Commit e5af22e

Browse files
authored
Merge pull request #814 from brisad/object-define-property
Add binding for Object.defineProperty()
2 parents 9ca024a + fe6ad54 commit e5af22e

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

crates/js-sys/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1996,6 +1996,14 @@ extern "C" {
19961996
#[wasm_bindgen(static_method_of = Object)]
19971997
pub fn create(prototype: &Object) -> Object;
19981998

1999+
/// The static method Object.defineProperty() defines a new
2000+
/// property directly on an object, or modifies an existing
2001+
/// property on an object, and returns the object.
2002+
///
2003+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty)
2004+
#[wasm_bindgen(static_method_of = Object, js_name = defineProperty)]
2005+
pub fn define_property(obj: &Object, prop: &JsValue, descriptor: &Object) -> Object;
2006+
19992007
/// The `Object.freeze()` method freezes an object: that is, prevents new
20002008
/// properties from being added to it; prevents existing properties from
20012009
/// being removed; and prevents existing properties, or their enumerability,

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ extern "C" {
99
type Foo42;
1010
#[wasm_bindgen(method, setter, structural)]
1111
fn set_foo(this: &Foo42, val: JsValue);
12+
13+
type DefinePropertyAttrs;
14+
#[wasm_bindgen(method, setter, structural)]
15+
fn set_value(this: &DefinePropertyAttrs, val: &JsValue);
1216
}
1317

1418
#[wasm_bindgen(module = "tests/wasm/Object.js")]
@@ -73,6 +77,16 @@ fn create() {
7377
assert!(my_array.is_instance_of::<Array>());
7478
}
7579

80+
#[wasm_bindgen_test]
81+
fn define_property() {
82+
let value = DefinePropertyAttrs::from(JsValue::from(Object::new()));
83+
value.set_value(&43.into());
84+
let descriptor = Object::from(JsValue::from(value));
85+
let foo = foo_42();
86+
let foo = Object::define_property(&foo, &"bar".into(), &descriptor);
87+
assert!(foo.has_own_property(&"bar".into()));
88+
}
89+
7690
#[wasm_bindgen_test]
7791
fn has_own_property() {
7892
assert!(foo_42().has_own_property(&"foo".into()));

0 commit comments

Comments
 (0)