LibJS: Make Array.of(...items) generic

As well as bring it generally closer to the specification.
This commit is contained in:
Idan Horowitz 2021-06-29 15:06:43 +03:00 committed by Linus Groh
parent 7ff363127b
commit 1d94d7a367
Notes: sideshowbarker 2024-07-18 11:21:02 +09:00

View file

@ -155,9 +155,26 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::is_array)
// 23.1.2.3 Array.of ( ...items ), https://tc39.es/ecma262/#sec-array.of
JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::of)
{
auto* array = Array::create(global_object);
for (size_t i = 0; i < vm.argument_count(); ++i)
array->indexed_properties().append(vm.argument(i));
auto this_value = vm.this_value(global_object);
Value array;
if (this_value.is_constructor()) {
MarkedValueList arguments(vm.heap());
arguments.empend(vm.argument_count());
array = vm.construct(this_value.as_function(), this_value.as_function(), move(arguments));
if (vm.exception())
return {};
} else {
array = Array::create(global_object);
}
auto& array_object = array.as_object();
for (size_t k = 0; k < vm.argument_count(); ++k) {
array_object.define_property(k, vm.argument(k));
if (vm.exception())
return {};
}
array_object.put(vm.names.length, Value(vm.argument_count()));
if (vm.exception())
return {};
return array;
}