Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add binding for Object.defineProperties() #833

Merged
merged 1 commit into from
Sep 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions crates/js-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2004,6 +2004,14 @@ extern "C" {
#[wasm_bindgen(static_method_of = Object, js_name = defineProperty)]
pub fn define_property(obj: &Object, prop: &JsValue, descriptor: &Object) -> Object;

/// The Object.defineProperties() method defines new or modifies
/// existing properties directly on an object, returning the
/// object.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties)
#[wasm_bindgen(static_method_of = Object, js_name = defineProperties)]
pub fn define_properties(obj: &Object, props: &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,
Expand Down
14 changes: 14 additions & 0 deletions crates/js-sys/tests/wasm/Object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ fn define_property() {
assert!(foo.has_own_property(&"bar".into()));
}

#[wasm_bindgen_test]
fn define_properties() {
let props = Object::new();
let descriptor = DefinePropertyAttrs::from(JsValue::from(Object::new()));
descriptor.set_value(&42.into());
let descriptor = JsValue::from(descriptor);
Reflect::set(props.as_ref(), &JsValue::from("bar"), &descriptor);
Reflect::set(props.as_ref(), &JsValue::from("car"), &descriptor);
let foo = foo_42();
let foo = Object::define_properties(&foo, &props);
assert!(foo.has_own_property(&"bar".into()));
assert!(foo.has_own_property(&"car".into()));
}

#[wasm_bindgen_test]
fn has_own_property() {
assert!(foo_42().has_own_property(&"foo".into()));
Expand Down