LibJS: Add spec comments to ArrayCreate

This commit is contained in:
Linus Groh 2022-07-03 16:21:35 +02:00
parent 1132151f3d
commit 65eb1ee67a
Notes: sideshowbarker 2024-07-17 09:45:05 +09:00

View File

@ -20,12 +20,24 @@ namespace JS {
ThrowCompletionOr<Array*> Array::create(GlobalObject& global_object, size_t length, Object* prototype)
{
auto& vm = global_object.vm();
// 1. If length > 2^32 - 1, throw a RangeError exception.
if (length > NumericLimits<u32>::max())
return vm.throw_completion<RangeError>(global_object, ErrorType::InvalidLength, "array");
// 2. If proto is not present, set proto to %Array.prototype%.
if (!prototype)
prototype = global_object.array_prototype();
// 3. Let A be MakeBasicObject(« [[Prototype]], [[Extensible]] »).
// 4. Set A.[[Prototype]] to proto.
// 5. Set A.[[DefineOwnProperty]] as specified in 10.4.2.1.
auto* array = global_object.heap().allocate<Array>(global_object, *prototype);
// 6. Perform ! OrdinaryDefineOwnProperty(A, "length", PropertyDescriptor { [[Value]]: 𝔽(length), [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }).
MUST(array->internal_define_own_property(vm.names.length, { .value = Value(length), .writable = true, .enumerable = false, .configurable = false }));
// 7. Return A.
return array;
}