js-sys: Add bindings to Object.assign

This commit is contained in:
Nick Fitzgerald 2018-08-10 11:41:58 -07:00
parent f0444d1614
commit 4ea1603ddb
2 changed files with 51 additions and 0 deletions

View File

@ -1890,6 +1890,30 @@ extern "C" {
#[derive(Clone, Debug)]
pub type Object;
/// The Object.assign() method is used to copy the values of all enumerable
/// own properties from one or more source objects to a target object. It
/// will return the target object.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
#[wasm_bindgen(static_method_of = Object)]
pub fn assign(target: &Object, source: &Object) -> Object;
/// The Object.assign() method is used to copy the values of all enumerable
/// own properties from one or more source objects to a target object. It
/// will return the target object.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
#[wasm_bindgen(static_method_of = Object, js_name = assign)]
pub fn assign2(target: &Object, source1: &Object, source2: &Object) -> Object;
/// The Object.assign() method is used to copy the values of all enumerable
/// own properties from one or more source objects to a target object. It
/// will return the target object.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
#[wasm_bindgen(static_method_of = Object, js_name = assign)]
pub fn assign3(target: &Object, source1: &Object, source2: &Object, source3: &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,

View File

@ -36,6 +36,33 @@ fn new() {
assert!(JsValue::from(Object::new()).is_object());
}
#[wasm_bindgen_test]
fn assign() {
let a = JsValue::from("a");
let b = JsValue::from("b");
let c = JsValue::from("c");
let target = Object::new();
Reflect::set(target.as_ref(), a.as_ref(), a.as_ref());
let src1 = Object::new();
Reflect::set(src1.as_ref(), &a, &c);
let src2 = Object::new();
Reflect::set(src2.as_ref(), &b, &b);
let src3 = Object::new();
Reflect::set(src3.as_ref(), &c, &c);
let res = Object::assign3(&target, &src1, &src2, &src3);
assert!(Object::is(target.as_ref(), res.as_ref()));
assert_eq!(Reflect::get(target.as_ref(), &a), c);
assert_eq!(Reflect::get(target.as_ref(), &b), b);
assert_eq!(Reflect::get(target.as_ref(), &c), c);
}
#[wasm_bindgen_test]
fn has_own_property() {
assert!(foo_42().has_own_property(&"foo".into()));