1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:58:12 +00:00

LibJS: Bring ArrayCreate and ArrayConstructor closer to spec

Specifically, this now explicitly takes the length, adds missing
exceptions checks to calls with user-supplied lengths, takes and uses
the prototype argument, and fixes some spec non-conformance in
ArrayConstructor and its native functions around the use of ArrayCreate
This commit is contained in:
Idan Horowitz 2021-07-04 01:36:44 +03:00 committed by Linus Groh
parent 5ee1ae37b2
commit e480d69130
17 changed files with 172 additions and 96 deletions

View file

@ -13,23 +13,24 @@
namespace JS {
// 10.4.2.2 ArrayCreate ( length [ , proto ] ), https://tc39.es/ecma262/#sec-arraycreate
Array* Array::create(GlobalObject& global_object, size_t length)
Array* Array::create(GlobalObject& global_object, size_t length, Object* prototype)
{
// FIXME: Support proto parameter
auto& vm = global_object.vm();
if (length > NumericLimits<u32>::max()) {
auto& vm = global_object.vm();
vm.throw_exception<RangeError>(global_object, ErrorType::InvalidLength, "array");
return nullptr;
}
auto* array = global_object.heap().allocate<Array>(global_object, *global_object.array_prototype());
array->indexed_properties().set_array_like_size(length);
if (!prototype)
prototype = global_object.array_prototype();
auto* array = global_object.heap().allocate<Array>(global_object, *prototype);
array->put(vm.names.length, Value(length));
return array;
}
// 7.3.17 CreateArrayFromList ( elements ), https://tc39.es/ecma262/#sec-createarrayfromlist
Array* Array::create_from(GlobalObject& global_object, const Vector<Value>& elements)
{
auto* array = Array::create(global_object);
auto* array = Array::create(global_object, 0);
for (size_t i = 0; i < elements.size(); ++i)
array->define_property(i, elements[i]);
return array;