From 65eb1ee67a45384b4d4276444779bece9677cd66 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 3 Jul 2022 16:21:35 +0200 Subject: [PATCH] LibJS: Add spec comments to ArrayCreate --- Userland/Libraries/LibJS/Runtime/Array.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Userland/Libraries/LibJS/Runtime/Array.cpp b/Userland/Libraries/LibJS/Runtime/Array.cpp index 2adb9bf2e0d..0f893068f04 100644 --- a/Userland/Libraries/LibJS/Runtime/Array.cpp +++ b/Userland/Libraries/LibJS/Runtime/Array.cpp @@ -20,12 +20,24 @@ namespace JS { ThrowCompletionOr 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::max()) return vm.throw_completion(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(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; }