1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 06:47:35 +00:00

LibJS: Convert NativeFunction::{call,construct}() to ThrowCompletionOr

Both at the same time because many of them call construct() in call()
and I'm not keen on adding a bunch of temporary plumbing to turn
exceptions into throw completions.
Also changes the return value of construct() to Object* instead of Value
as it always needs to return an object; allowing an arbitrary Value is a
massive foot gun.
This commit is contained in:
Linus Groh 2021-10-20 21:16:30 +01:00
parent 0881f8160f
commit 5832de62fe
99 changed files with 597 additions and 669 deletions

View file

@ -45,17 +45,17 @@ void ArrayConstructor::initialize(GlobalObject& global_object)
}
// 23.1.1.1 Array ( ...values ), https://tc39.es/ecma262/#sec-array
Value ArrayConstructor::call()
ThrowCompletionOr<Value> ArrayConstructor::call()
{
return construct(*this);
return TRY(construct(*this));
}
// 23.1.1.1 Array ( ...values ), https://tc39.es/ecma262/#sec-array
Value ArrayConstructor::construct(FunctionObject& new_target)
ThrowCompletionOr<Object*> ArrayConstructor::construct(FunctionObject& new_target)
{
auto& vm = this->vm();
auto* proto = TRY_OR_DISCARD(get_prototype_from_constructor(global_object(), new_target, &GlobalObject::array_prototype));
auto* proto = TRY(get_prototype_from_constructor(global_object(), new_target, &GlobalObject::array_prototype));
if (vm.argument_count() == 0)
return Array::create(global_object(), 0, proto);
@ -69,18 +69,16 @@ Value ArrayConstructor::construct(FunctionObject& new_target)
int_length = 1;
} else {
int_length = MUST(length.to_u32(global_object()));
if (int_length != length.as_double()) {
vm.throw_exception<RangeError>(global_object(), ErrorType::InvalidLength, "array");
return {};
}
if (int_length != length.as_double())
return vm.throw_completion<RangeError>(global_object(), ErrorType::InvalidLength, "array");
}
TRY_OR_DISCARD(array->set(vm.names.length, Value(int_length), Object::ShouldThrowExceptions::Yes));
TRY(array->set(vm.names.length, Value(int_length), Object::ShouldThrowExceptions::Yes));
return array;
}
auto* array = Array::create(global_object(), vm.argument_count(), proto);
if (vm.exception())
return {};
if (auto* exception = vm.exception())
return throw_completion(exception->value());
for (size_t k = 0; k < vm.argument_count(); ++k)
MUST(array->create_data_property_or_throw(k, vm.argument(k)));