1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 13:37:45 +00:00

LibJS: Convert FinalizationRegistryPrototype funcs to ThrowCompletionOr

This commit is contained in:
Idan Horowitz 2021-10-29 01:04:26 +03:00
parent 909e13c5e6
commit 84681788c4
2 changed files with 22 additions and 32 deletions

View file

@ -20,9 +20,9 @@ void FinalizationRegistryPrototype::initialize(GlobalObject& global_object)
Object::initialize(global_object); Object::initialize(global_object);
u8 attr = Attribute::Writable | Attribute::Configurable; u8 attr = Attribute::Writable | Attribute::Configurable;
define_old_native_function(vm.names.cleanupSome, cleanup_some, 0, attr); define_native_function(vm.names.cleanupSome, cleanup_some, 0, attr);
define_old_native_function(vm.names.register_, register_, 2, attr); define_native_function(vm.names.register_, register_, 2, attr);
define_old_native_function(vm.names.unregister, unregister, 1, attr); define_native_function(vm.names.unregister, unregister, 1, attr);
// 26.2.3.4 FinalizationRegistry.prototype [ @@toStringTag ], https://tc39.es/ecma262/#sec-finalization-registry.prototype-@@tostringtag // 26.2.3.4 FinalizationRegistry.prototype [ @@toStringTag ], https://tc39.es/ecma262/#sec-finalization-registry.prototype-@@tostringtag
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(global_object.heap(), vm.names.FinalizationRegistry.as_string()), Attribute::Configurable); define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(global_object.heap(), vm.names.FinalizationRegistry.as_string()), Attribute::Configurable);
@ -33,15 +33,13 @@ FinalizationRegistryPrototype::~FinalizationRegistryPrototype()
} }
// @STAGE 2@ FinalizationRegistry.prototype.cleanupSome ( [ callback ] ), https://github.com/tc39/proposal-cleanup-some/blob/master/spec/finalization-registry.html // @STAGE 2@ FinalizationRegistry.prototype.cleanupSome ( [ callback ] ), https://github.com/tc39/proposal-cleanup-some/blob/master/spec/finalization-registry.html
JS_DEFINE_OLD_NATIVE_FUNCTION(FinalizationRegistryPrototype::cleanup_some) JS_DEFINE_NATIVE_FUNCTION(FinalizationRegistryPrototype::cleanup_some)
{ {
auto* finalization_registry = TRY_OR_DISCARD(typed_this_object(global_object)); auto* finalization_registry = TRY(typed_this_object(global_object));
auto callback = vm.argument(0); auto callback = vm.argument(0);
if (vm.argument_count() > 0 && !callback.is_function()) { if (vm.argument_count() > 0 && !callback.is_function())
vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, callback.to_string_without_side_effects()); return vm.throw_completion<TypeError>(global_object, ErrorType::NotAFunction, callback.to_string_without_side_effects());
return {};
}
finalization_registry->cleanup(callback.is_undefined() ? nullptr : &callback.as_function()); finalization_registry->cleanup(callback.is_undefined() ? nullptr : &callback.as_function());
@ -49,27 +47,21 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(FinalizationRegistryPrototype::cleanup_some)
} }
// 26.2.3.2 FinalizationRegistry.prototype.register ( target, heldValue [ , unregisterToken ] ), https://tc39.es/ecma262/#sec-finalization-registry.prototype.register // 26.2.3.2 FinalizationRegistry.prototype.register ( target, heldValue [ , unregisterToken ] ), https://tc39.es/ecma262/#sec-finalization-registry.prototype.register
JS_DEFINE_OLD_NATIVE_FUNCTION(FinalizationRegistryPrototype::register_) JS_DEFINE_NATIVE_FUNCTION(FinalizationRegistryPrototype::register_)
{ {
auto* finalization_registry = TRY_OR_DISCARD(typed_this_object(global_object)); auto* finalization_registry = TRY(typed_this_object(global_object));
auto target = vm.argument(0); auto target = vm.argument(0);
if (!target.is_object()) { if (!target.is_object())
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects()); return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
return {};
}
auto held_value = vm.argument(1); auto held_value = vm.argument(1);
if (same_value(target, held_value)) { if (same_value(target, held_value))
vm.throw_exception<TypeError>(global_object, ErrorType::FinalizationRegistrySameTargetAndValue); return vm.throw_completion<TypeError>(global_object, ErrorType::FinalizationRegistrySameTargetAndValue);
return {};
}
auto unregister_token = vm.argument(2); auto unregister_token = vm.argument(2);
if (!unregister_token.is_object() && !unregister_token.is_undefined()) { if (!unregister_token.is_object() && !unregister_token.is_undefined())
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, unregister_token.to_string_without_side_effects()); return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, unregister_token.to_string_without_side_effects());
return {};
}
finalization_registry->add_finalization_record(target.as_cell(), held_value, unregister_token.is_undefined() ? nullptr : &unregister_token.as_object()); finalization_registry->add_finalization_record(target.as_cell(), held_value, unregister_token.is_undefined() ? nullptr : &unregister_token.as_object());
@ -77,15 +69,13 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(FinalizationRegistryPrototype::register_)
} }
// 26.2.3.3 FinalizationRegistry.prototype.unregister ( unregisterToken ), https://tc39.es/ecma262/#sec-finalization-registry.prototype.unregister // 26.2.3.3 FinalizationRegistry.prototype.unregister ( unregisterToken ), https://tc39.es/ecma262/#sec-finalization-registry.prototype.unregister
JS_DEFINE_OLD_NATIVE_FUNCTION(FinalizationRegistryPrototype::unregister) JS_DEFINE_NATIVE_FUNCTION(FinalizationRegistryPrototype::unregister)
{ {
auto* finalization_registry = TRY_OR_DISCARD(typed_this_object(global_object)); auto* finalization_registry = TRY(typed_this_object(global_object));
auto unregister_token = vm.argument(0); auto unregister_token = vm.argument(0);
if (!unregister_token.is_object()) { if (!unregister_token.is_object())
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, unregister_token.to_string_without_side_effects()); return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, unregister_token.to_string_without_side_effects());
return {};
}
return Value(finalization_registry->remove_by_token(unregister_token.as_object())); return Value(finalization_registry->remove_by_token(unregister_token.as_object()));
} }

View file

@ -20,9 +20,9 @@ public:
virtual ~FinalizationRegistryPrototype() override; virtual ~FinalizationRegistryPrototype() override;
private: private:
JS_DECLARE_OLD_NATIVE_FUNCTION(cleanup_some); JS_DECLARE_NATIVE_FUNCTION(cleanup_some);
JS_DECLARE_OLD_NATIVE_FUNCTION(register_); JS_DECLARE_NATIVE_FUNCTION(register_);
JS_DECLARE_OLD_NATIVE_FUNCTION(unregister); JS_DECLARE_NATIVE_FUNCTION(unregister);
}; };
} }