Skip to content

Commit 200c757

Browse files
Lubrsiawesomekling
authored andcommitted
LibJS: Implement Object.prototype.propertyIsEnumerable
Spec: https://tc39.es/ecma262/#sec-object.prototype.propertyisenumerable This is used by core-js, which is used by frameworks such as Vue.
1 parent d46de3a commit 200c757

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

Libraries/LibJS/Runtime/CommonPropertyNames.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ namespace JS {
176176
P(pop) \
177177
P(pow) \
178178
P(preventExtensions) \
179+
P(propertyIsEnumerable) \
179180
P(prototype) \
180181
P(push) \
181182
P(random) \

Libraries/LibJS/Runtime/ObjectPrototype.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void ObjectPrototype::initialize(GlobalObject& global_object)
4949
define_native_function(vm.names.toString, to_string, 0, attr);
5050
define_native_function(vm.names.toLocaleString, to_locale_string, 0, attr);
5151
define_native_function(vm.names.valueOf, value_of, 0, attr);
52+
define_native_function(vm.names.propertyIsEnumerable, property_is_enumerable, 1, attr);
5253
}
5354

5455
ObjectPrototype::~ObjectPrototype()
@@ -123,4 +124,18 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::value_of)
123124
return this_object->value_of();
124125
}
125126

127+
JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::property_is_enumerable)
128+
{
129+
auto name = vm.argument(0).to_string(global_object);
130+
if (vm.exception())
131+
return {};
132+
auto* this_object = vm.this_value(global_object).to_object(global_object);
133+
if (!this_object)
134+
return {};
135+
auto property_descriptor = this_object->get_own_property_descriptor(name);
136+
if (!property_descriptor.has_value())
137+
return Value(false);
138+
return Value(property_descriptor.value().attributes.is_enumerable());
139+
}
140+
126141
}

Libraries/LibJS/Runtime/ObjectPrototype.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class ObjectPrototype final : public Object {
4545
JS_DECLARE_NATIVE_FUNCTION(has_own_property);
4646
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
4747
JS_DECLARE_NATIVE_FUNCTION(value_of);
48+
JS_DECLARE_NATIVE_FUNCTION(property_is_enumerable);
4849
};
4950

5051
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
test("basic functionality", () => {
2+
var o = {};
3+
4+
o.foo = 1;
5+
expect(o.propertyIsEnumerable("foo")).toBeTrue();
6+
expect(o.propertyIsEnumerable("bar")).toBeFalse();
7+
expect(o.propertyIsEnumerable()).toBeFalse();
8+
expect(o.propertyIsEnumerable(undefined)).toBeFalse();
9+
10+
o.undefined = 2;
11+
expect(o.propertyIsEnumerable()).toBeTrue();
12+
expect(o.propertyIsEnumerable(undefined)).toBeTrue();
13+
14+
expect(globalThis.propertyIsEnumerable("globalThis")).toBeFalse();
15+
});

0 commit comments

Comments
 (0)