Forráskód Böngészése

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.
Luke 4 éve
szülő
commit
200c7572b7

+ 1 - 0
Libraries/LibJS/Runtime/CommonPropertyNames.h

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

+ 15 - 0
Libraries/LibJS/Runtime/ObjectPrototype.cpp

@@ -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());
+}
+
 }

+ 1 - 0
Libraries/LibJS/Runtime/ObjectPrototype.h

@@ -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);
 };
 
 }

+ 15 - 0
Libraries/LibJS/Tests/builtins/Object/Object.prototype.propertyIsEnumerable.js

@@ -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();
+});