From 5f09d78b9da8a9cc4c2eace7bce8b169466ac102 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Tue, 29 Jun 2021 02:54:41 +0300 Subject: [PATCH] LibJS: Bring the Array constructor slightly closer to the specification Specifically, we now cast to a u32 instead of an i32, as well as use the validity check required by the specification. The current constructor is still quite far from the specification, as we directly set the indexed properties' length instead of going through the Array's overriden DefineOwnProperty. (and as a result the checks imposed by the ArraySetLength abstract operation) --- Userland/Libraries/LibJS/Runtime/ArrayConstructor.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/ArrayConstructor.cpp b/Userland/Libraries/LibJS/Runtime/ArrayConstructor.cpp index f405e95019..59f96779c7 100644 --- a/Userland/Libraries/LibJS/Runtime/ArrayConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/ArrayConstructor.cpp @@ -51,13 +51,14 @@ Value ArrayConstructor::call() return Array::create(global_object()); if (vm().argument_count() == 1 && vm().argument(0).is_number()) { - auto array_length_value = vm().argument(0); - if (!array_length_value.is_integral_number() || array_length_value.as_i32() < 0) { + auto length = vm().argument(0); + auto int_length = length.to_u32(global_object()); + if (int_length != length.as_double()) { vm().throw_exception(global_object(), ErrorType::InvalidLength, "array"); return {}; } auto* array = Array::create(global_object()); - array->indexed_properties().set_array_like_size(array_length_value.as_i32()); + array->indexed_properties().set_array_like_size(int_length); return array; }