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.
This commit is contained in:
Luke 2020-12-24 17:51:28 +00:00 committed by Andreas Kling
parent d46de3aeb4
commit 200c7572b7
Notes: sideshowbarker 2024-07-19 00:37:58 +09:00
4 changed files with 32 additions and 0 deletions

View file

@ -176,6 +176,7 @@ namespace JS {
P(pop) \
P(pow) \
P(preventExtensions) \
P(propertyIsEnumerable) \
P(prototype) \
P(push) \
P(random) \

View file

@ -49,6 +49,7 @@ void ObjectPrototype::initialize(GlobalObject& global_object)
define_native_function(vm.names.toString, to_string, 0, attr);
define_native_function(vm.names.toLocaleString, to_locale_string, 0, attr);
define_native_function(vm.names.valueOf, value_of, 0, attr);
define_native_function(vm.names.propertyIsEnumerable, property_is_enumerable, 1, attr);
}
ObjectPrototype::~ObjectPrototype()
@ -123,4 +124,18 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::value_of)
return this_object->value_of();
}
JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::property_is_enumerable)
{
auto name = vm.argument(0).to_string(global_object);
if (vm.exception())
return {};
auto* this_object = vm.this_value(global_object).to_object(global_object);
if (!this_object)
return {};
auto property_descriptor = this_object->get_own_property_descriptor(name);
if (!property_descriptor.has_value())
return Value(false);
return Value(property_descriptor.value().attributes.is_enumerable());
}
}

View file

@ -45,6 +45,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(has_own_property);
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
JS_DECLARE_NATIVE_FUNCTION(value_of);
JS_DECLARE_NATIVE_FUNCTION(property_is_enumerable);
};
}

View file

@ -0,0 +1,15 @@
test("basic functionality", () => {
var o = {};
o.foo = 1;
expect(o.propertyIsEnumerable("foo")).toBeTrue();
expect(o.propertyIsEnumerable("bar")).toBeFalse();
expect(o.propertyIsEnumerable()).toBeFalse();
expect(o.propertyIsEnumerable(undefined)).toBeFalse();
o.undefined = 2;
expect(o.propertyIsEnumerable()).toBeTrue();
expect(o.propertyIsEnumerable(undefined)).toBeTrue();
expect(globalThis.propertyIsEnumerable("globalThis")).toBeFalse();
});