From a72276407ba4a438d4e34edd0d5ebf48cd6d9b63 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Wed, 24 Feb 2021 09:48:29 +0100 Subject: [PATCH] LibJS: Make ArrayPrototype an Array object https://tc39.es/ecma262/#sec-properties-of-the-array-prototype-object The Array prototype object: [...] is an Array exotic object and has the internal methods specified for such objects. NOTE: The Array prototype object is specified to be an Array exotic object to ensure compatibility with ECMAScript code that was created prior to the ECMAScript 2015 specification. --- Userland/Libraries/LibJS/Runtime/Array.cpp | 5 +++++ Userland/Libraries/LibJS/Runtime/Array.h | 3 ++- Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp | 5 ++--- Userland/Libraries/LibJS/Runtime/ArrayPrototype.h | 6 +++--- .../Libraries/LibJS/Tests/builtins/Array/Array.isArray.js | 3 +-- 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Array.cpp b/Userland/Libraries/LibJS/Runtime/Array.cpp index 2509d134223..dabf19f9fb9 100644 --- a/Userland/Libraries/LibJS/Runtime/Array.cpp +++ b/Userland/Libraries/LibJS/Runtime/Array.cpp @@ -38,8 +38,13 @@ Array* Array::create(GlobalObject& global_object) Array::Array(Object& prototype) : Object(prototype) +{ +} + +void Array::initialize(GlobalObject& global_object) { auto& vm = this->vm(); + Object::initialize(global_object); define_native_property(vm.names.length, length_getter, length_setter, Attribute::Writable); } diff --git a/Userland/Libraries/LibJS/Runtime/Array.h b/Userland/Libraries/LibJS/Runtime/Array.h index 2ee4727c356..5beeefa809e 100644 --- a/Userland/Libraries/LibJS/Runtime/Array.h +++ b/Userland/Libraries/LibJS/Runtime/Array.h @@ -30,13 +30,14 @@ namespace JS { -class Array final : public Object { +class Array : public Object { JS_OBJECT(Array, Object); public: static Array* create(GlobalObject&); explicit Array(Object& prototype); + virtual void initialize(GlobalObject&) override; virtual ~Array() override; static Array* typed_this(VM&, GlobalObject&); diff --git a/Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp index 311ea3707b3..d4aba04f209 100644 --- a/Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp @@ -44,14 +44,14 @@ namespace JS { static HashTable s_array_join_seen_objects; ArrayPrototype::ArrayPrototype(GlobalObject& global_object) - : Object(*global_object.object_prototype()) + : Array(*global_object.object_prototype()) { } void ArrayPrototype::initialize(GlobalObject& global_object) { auto& vm = this->vm(); - Object::initialize(global_object); + Array::initialize(global_object); u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(vm.names.filter, filter, 1, attr); @@ -81,7 +81,6 @@ void ArrayPrototype::initialize(GlobalObject& global_object) define_native_function(vm.names.fill, fill, 1, attr); define_native_function(vm.names.values, values, 0, attr); define_native_function(vm.names.flat, flat, 0, attr); - define_property(vm.names.length, Value(0), Attribute::Configurable); // Use define_property here instead of define_native_function so that // Object.is(Array.prototype[Symbol.iterator], Array.prototype.values) diff --git a/Userland/Libraries/LibJS/Runtime/ArrayPrototype.h b/Userland/Libraries/LibJS/Runtime/ArrayPrototype.h index 92ffafe9573..906e0af39ac 100644 --- a/Userland/Libraries/LibJS/Runtime/ArrayPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/ArrayPrototype.h @@ -27,12 +27,12 @@ #pragma once -#include +#include namespace JS { -class ArrayPrototype final : public Object { - JS_OBJECT(ArrayPrototype, Object); +class ArrayPrototype final : public Array { + JS_OBJECT(ArrayPrototype, Array); public: ArrayPrototype(GlobalObject&); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Array/Array.isArray.js b/Userland/Libraries/LibJS/Tests/builtins/Array/Array.isArray.js index bbe12a8c445..e0577a70fa6 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Array/Array.isArray.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Array/Array.isArray.js @@ -21,6 +21,5 @@ test("arguments that evaluate to true", () => { expect(Array.isArray(new Array())).toBeTrue(); expect(Array.isArray(new Array(10))).toBeTrue(); expect(Array.isArray(new Array("a", "b", "c"))).toBeTrue(); - // FIXME: Array.prototype is supposed to be an array! - // expect(Array.isArray(Array.prototype)).toBeTrue(); + expect(Array.isArray(Array.prototype)).toBeTrue(); });