1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-23 09:27:35 +00:00

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.
This commit is contained in:
Linus Groh 2021-02-24 09:48:29 +01:00 committed by Andreas Kling
parent 84996c6567
commit a72276407b
5 changed files with 13 additions and 9 deletions

View file

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

View file

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

View file

@ -44,14 +44,14 @@ namespace JS {
static HashTable<Object*> 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)

View file

@ -27,12 +27,12 @@
#pragma once
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/Array.h>
namespace JS {
class ArrayPrototype final : public Object {
JS_OBJECT(ArrayPrototype, Object);
class ArrayPrototype final : public Array {
JS_OBJECT(ArrayPrototype, Array);
public:
ArrayPrototype(GlobalObject&);

View file

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