diff --git a/Userland/Libraries/LibJS/Runtime/Array.cpp b/Userland/Libraries/LibJS/Runtime/Array.cpp index 1fc8a89954..d8ab3b4236 100644 --- a/Userland/Libraries/LibJS/Runtime/Array.cpp +++ b/Userland/Libraries/LibJS/Runtime/Array.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2020, Andreas Kling + * Copyright (c) 2020-2021, Linus Groh * * SPDX-License-Identifier: BSD-2-Clause */ @@ -11,9 +12,17 @@ namespace JS { -Array* Array::create(GlobalObject& global_object) +// 10.4.2.2 ArrayCreate, https://tc39.es/ecma262/#sec-arraycreate +Array* Array::create(GlobalObject& global_object, size_t length) { - return global_object.heap().allocate(global_object, *global_object.array_prototype()); + if (length > NumericLimits::max()) { + auto& vm = global_object.vm(); + vm.throw_exception(global_object, ErrorType::InvalidLength, "array"); + return nullptr; + } + auto* array = global_object.heap().allocate(global_object, *global_object.array_prototype()); + array->indexed_properties().set_array_like_size(length); + return array; } // 7.3.17 CreateArrayFromList, https://tc39.es/ecma262/#sec-createarrayfromlist diff --git a/Userland/Libraries/LibJS/Runtime/Array.h b/Userland/Libraries/LibJS/Runtime/Array.h index 58b4b19c8b..ea204a02bb 100644 --- a/Userland/Libraries/LibJS/Runtime/Array.h +++ b/Userland/Libraries/LibJS/Runtime/Array.h @@ -14,7 +14,7 @@ class Array : public Object { JS_OBJECT(Array, Object); public: - static Array* create(GlobalObject&); + static Array* create(GlobalObject&, size_t length = 0); static Array* create_from(GlobalObject&, const Vector&); explicit Array(Object& prototype);