Skip to content

Commit b36708f

Browse files
authored
Merge pull request #854 from brisad/object-bindings
Add the last four bindings for Object
2 parents 4ca187c + f7b5115 commit b36708f

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

crates/js-sys/src/lib.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,6 +2014,16 @@ extern "C" {
20142014
#[wasm_bindgen(static_method_of = Object, js_name = defineProperties)]
20152015
pub fn define_properties(obj: &Object, props: &Object) -> Object;
20162016

2017+
/// The Object.entries() method returns an array of a given
2018+
/// object's own enumerable property [key, value] pairs, in the
2019+
/// same order as that provided by a for...in loop (the difference
2020+
/// being that a for-in loop enumerates properties in the
2021+
/// prototype chain as well).
2022+
///
2023+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries)
2024+
#[wasm_bindgen(static_method_of = Object)]
2025+
pub fn entries(object: &Object) -> Array;
2026+
20172027
/// The `Object.freeze()` method freezes an object: that is, prevents new
20182028
/// properties from being added to it; prevents existing properties from
20192029
/// being removed; and prevents existing properties, or their enumerability,
@@ -2040,6 +2050,29 @@ extern "C" {
20402050
#[wasm_bindgen(static_method_of = Object, js_name = getOwnPropertyDescriptors)]
20412051
pub fn get_own_property_descriptors(obj: &Object) -> JsValue;
20422052

2053+
/// The Object.getOwnPropertyNames() method returns an array of
2054+
/// all properties (including non-enumerable properties except for
2055+
/// those which use Symbol) found directly upon a given object.
2056+
///
2057+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames)
2058+
#[wasm_bindgen(static_method_of = Object, js_name = getOwnPropertyNames)]
2059+
pub fn get_own_property_names(obj: &Object) -> Array;
2060+
2061+
/// The Object.getOwnPropertySymbols() method returns an array of
2062+
/// all symbol properties found directly upon a given object.
2063+
///
2064+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertySymbols)
2065+
#[wasm_bindgen(static_method_of = Object, js_name = getOwnPropertySymbols)]
2066+
pub fn get_own_property_symbols(obj: &Object) -> Array;
2067+
2068+
/// The Object.getPrototypeOf() method returns the prototype
2069+
/// (i.e. the value of the internal [[Prototype]] property) of the
2070+
/// specified object.
2071+
///
2072+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getPrototypeOf)
2073+
#[wasm_bindgen(static_method_of = Object, js_name = getPrototypeOf)]
2074+
pub fn get_prototype_of(obj: &JsValue) -> Object;
2075+
20432076
/// The `hasOwnProperty()` method returns a boolean indicating whether the
20442077
/// object has the specified property as its own property (as opposed to
20452078
/// inheriting it).

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ extern "C" {
1010
#[wasm_bindgen(method, setter, structural)]
1111
fn set_foo(this: &Foo42, val: JsValue);
1212

13+
#[wasm_bindgen(js_name = prototype, js_namespace = Object)]
14+
static OBJECT_PROTOTYPE: JsValue;
15+
#[wasm_bindgen(js_name = prototype, js_namespace = Array)]
16+
static ARRAY_PROTOTYPE: JsValue;
17+
1318
type DefinePropertyAttrs;
1419
#[wasm_bindgen(method, setter, structural)]
1520
fn set_value(this: &DefinePropertyAttrs, val: &JsValue);
@@ -105,6 +110,19 @@ fn define_properties() {
105110
assert!(foo.has_own_property(&"car".into()));
106111
}
107112

113+
#[wasm_bindgen_test]
114+
fn entries() {
115+
let entries = Object::entries(&foo_42());
116+
assert_eq!(entries.length(), 1);
117+
entries.for_each(&mut |x, _, _| {
118+
assert!(x.is_object());
119+
let array: Array = x.into();
120+
assert_eq!(array.shift(), "foo");
121+
assert_eq!(array.shift(), 42);
122+
assert_eq!(array.length(), 0);
123+
});
124+
}
125+
108126
#[wasm_bindgen_test]
109127
fn get_own_property_descriptor() {
110128
let foo = foo_42();
@@ -122,6 +140,29 @@ fn get_own_property_descriptors() {
122140
assert_eq!(PropertyDescriptor::from(foo_desc).value(), 42);
123141
}
124142

143+
#[wasm_bindgen_test]
144+
fn get_own_property_names() {
145+
let names = Object::get_own_property_names(&foo_42());
146+
assert_eq!(names.length(), 1);
147+
names.for_each(&mut |x, _, _| {
148+
assert_eq!(x, "foo");
149+
});
150+
}
151+
152+
#[wasm_bindgen_test]
153+
fn get_own_property_symbols() {
154+
let symbols = Object::get_own_property_symbols(&map_with_symbol_key());
155+
assert_eq!(symbols.length(), 1);
156+
}
157+
158+
#[wasm_bindgen_test]
159+
fn get_prototype_of() {
160+
let proto = JsValue::from(Object::get_prototype_of(&Object::new().into()));
161+
assert_eq!(proto, *OBJECT_PROTOTYPE);
162+
let proto = JsValue::from(Object::get_prototype_of(&Array::new().into()));
163+
assert_eq!(proto, *ARRAY_PROTOTYPE);
164+
}
165+
125166
#[wasm_bindgen_test]
126167
fn has_own_property() {
127168
assert!(foo_42().has_own_property(&"foo".into()));

0 commit comments

Comments
 (0)