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

LibJS: Convert NativeFunction callback to ThrowCompletionOr

This commit is contained in:
Idan Horowitz 2021-10-19 22:53:27 +03:00 committed by Linus Groh
parent 20163c0584
commit ca27e5eff5
13 changed files with 91 additions and 65 deletions

View file

@ -951,6 +951,29 @@ void Object::set_prototype(Object* new_prototype)
}
void Object::define_native_accessor(PropertyName const& property_name, Function<Value(VM&, GlobalObject&)> getter, Function<Value(VM&, GlobalObject&)> setter, PropertyAttributes attribute)
{
Function<ThrowCompletionOr<Value>(VM&, GlobalObject&)> completion_getter = {};
if (getter) {
completion_getter = [getter = move(getter)](auto& vm, auto& global_object) -> ThrowCompletionOr<Value> {
auto result = getter(vm, global_object);
if (auto* exception = vm.exception())
return throw_completion(exception->value());
return result;
};
}
Function<ThrowCompletionOr<Value>(VM&, GlobalObject&)> completion_setter = {};
if (setter) {
completion_setter = [setter = move(setter)](auto& vm, auto& global_object) -> ThrowCompletionOr<Value> {
auto result = setter(vm, global_object);
if (auto* exception = vm.exception())
return throw_completion(exception->value());
return result;
};
}
define_new_native_accessor(property_name, move(completion_getter), move(completion_setter), attribute);
}
void Object::define_new_native_accessor(PropertyName const& property_name, Function<ThrowCompletionOr<Value>(VM&, GlobalObject&)> getter, Function<ThrowCompletionOr<Value>(VM&, GlobalObject&)> setter, PropertyAttributes attribute)
{
auto& vm = this->vm();
String formatted_property_name;
@ -1017,6 +1040,17 @@ Value Object::get_without_side_effects(const PropertyName& property_name) const
}
void Object::define_native_function(PropertyName const& property_name, Function<Value(VM&, GlobalObject&)> native_function, i32 length, PropertyAttributes attribute)
{
auto completion_native_function = [native_function = move(native_function), property_name](auto& vm, auto& global_object) -> ThrowCompletionOr<Value> {
auto result = native_function(vm, global_object);
if (auto* exception = vm.exception())
return throw_completion(exception->value());
return result;
};
define_new_native_function(property_name, move(completion_native_function), length, attribute);
}
void Object::define_new_native_function(PropertyName const& property_name, Function<ThrowCompletionOr<Value>(VM&, GlobalObject&)> native_function, i32 length, PropertyAttributes attribute)
{
auto& vm = this->vm();
String function_name;