1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 12:17:45 +00:00

LibJS: Convert SetPrototype functions to ThrowCompletionOr

This commit is contained in:
Idan Horowitz 2021-10-29 00:19:11 +03:00
parent c2e0753d8a
commit d46d8c9016
2 changed files with 36 additions and 41 deletions

View file

@ -22,14 +22,14 @@ void SetPrototype::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.add, add, 1, attr); define_native_function(vm.names.add, add, 1, attr);
define_old_native_function(vm.names.clear, clear, 0, attr); define_native_function(vm.names.clear, clear, 0, attr);
define_old_native_function(vm.names.delete_, delete_, 1, attr); define_native_function(vm.names.delete_, delete_, 1, attr);
define_old_native_function(vm.names.entries, entries, 0, attr); define_native_function(vm.names.entries, entries, 0, attr);
define_old_native_function(vm.names.forEach, for_each, 1, attr); define_native_function(vm.names.forEach, for_each, 1, attr);
define_old_native_function(vm.names.has, has, 1, attr); define_native_function(vm.names.has, has, 1, attr);
define_old_native_function(vm.names.values, values, 0, attr); define_native_function(vm.names.values, values, 0, attr);
define_old_native_accessor(vm.names.size, size_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.size, size_getter, {}, Attribute::Configurable);
define_direct_property(vm.names.keys, get_without_side_effects(vm.names.values), attr); define_direct_property(vm.names.keys, get_without_side_effects(vm.names.values), attr);
@ -45,9 +45,9 @@ SetPrototype::~SetPrototype()
} }
// 24.2.3.1 Set.prototype.add ( value ), https://tc39.es/ecma262/#sec-set.prototype.add // 24.2.3.1 Set.prototype.add ( value ), https://tc39.es/ecma262/#sec-set.prototype.add
JS_DEFINE_OLD_NATIVE_FUNCTION(SetPrototype::add) JS_DEFINE_NATIVE_FUNCTION(SetPrototype::add)
{ {
auto* set = TRY_OR_DISCARD(typed_this_object(global_object)); auto* set = TRY(typed_this_object(global_object));
auto value = vm.argument(0); auto value = vm.argument(0);
if (value.is_negative_zero()) if (value.is_negative_zero())
value = Value(0); value = Value(0);
@ -56,65 +56,60 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(SetPrototype::add)
} }
// 24.2.3.2 Set.prototype.clear ( ), https://tc39.es/ecma262/#sec-set.prototype.clear // 24.2.3.2 Set.prototype.clear ( ), https://tc39.es/ecma262/#sec-set.prototype.clear
JS_DEFINE_OLD_NATIVE_FUNCTION(SetPrototype::clear) JS_DEFINE_NATIVE_FUNCTION(SetPrototype::clear)
{ {
auto* set = TRY_OR_DISCARD(typed_this_object(global_object)); auto* set = TRY(typed_this_object(global_object));
set->values().clear(); set->values().clear();
return js_undefined(); return js_undefined();
} }
// 24.2.3.4 Set.prototype.delete ( value ), https://tc39.es/ecma262/#sec-set.prototype.delete // 24.2.3.4 Set.prototype.delete ( value ), https://tc39.es/ecma262/#sec-set.prototype.delete
JS_DEFINE_OLD_NATIVE_FUNCTION(SetPrototype::delete_) JS_DEFINE_NATIVE_FUNCTION(SetPrototype::delete_)
{ {
auto* set = TRY_OR_DISCARD(typed_this_object(global_object)); auto* set = TRY(typed_this_object(global_object));
return Value(set->values().remove(vm.argument(0))); return Value(set->values().remove(vm.argument(0)));
} }
// 24.2.3.5 Set.prototype.entries ( ), https://tc39.es/ecma262/#sec-set.prototype.entries // 24.2.3.5 Set.prototype.entries ( ), https://tc39.es/ecma262/#sec-set.prototype.entries
JS_DEFINE_OLD_NATIVE_FUNCTION(SetPrototype::entries) JS_DEFINE_NATIVE_FUNCTION(SetPrototype::entries)
{ {
auto* set = TRY_OR_DISCARD(typed_this_object(global_object)); auto* set = TRY(typed_this_object(global_object));
return SetIterator::create(global_object, *set, Object::PropertyKind::KeyAndValue); return SetIterator::create(global_object, *set, Object::PropertyKind::KeyAndValue);
} }
// 24.2.3.6 Set.prototype.forEach ( callbackfn [ , thisArg ] ), https://tc39.es/ecma262/#sec-set.prototype.foreach // 24.2.3.6 Set.prototype.forEach ( callbackfn [ , thisArg ] ), https://tc39.es/ecma262/#sec-set.prototype.foreach
JS_DEFINE_OLD_NATIVE_FUNCTION(SetPrototype::for_each) JS_DEFINE_NATIVE_FUNCTION(SetPrototype::for_each)
{ {
auto* set = TRY_OR_DISCARD(typed_this_object(global_object)); auto* set = TRY(typed_this_object(global_object));
if (!vm.argument(0).is_function()) { if (!vm.argument(0).is_function())
vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, vm.argument(0).to_string_without_side_effects()); return vm.throw_completion<TypeError>(global_object, ErrorType::NotAFunction, vm.argument(0).to_string_without_side_effects());
return {};
}
auto this_value = vm.this_value(global_object); auto this_value = vm.this_value(global_object);
for (auto& value : set->values()) { for (auto& value : set->values())
(void)vm.call(vm.argument(0).as_function(), vm.argument(1), value, value, this_value); TRY(vm.call(vm.argument(0).as_function(), vm.argument(1), value, value, this_value));
if (vm.exception())
return {};
}
return js_undefined(); return js_undefined();
} }
// 24.2.3.7 Set.prototype.has ( value ), https://tc39.es/ecma262/#sec-set.prototype.has // 24.2.3.7 Set.prototype.has ( value ), https://tc39.es/ecma262/#sec-set.prototype.has
JS_DEFINE_OLD_NATIVE_FUNCTION(SetPrototype::has) JS_DEFINE_NATIVE_FUNCTION(SetPrototype::has)
{ {
auto* set = TRY_OR_DISCARD(typed_this_object(global_object)); auto* set = TRY(typed_this_object(global_object));
auto& values = set->values(); auto& values = set->values();
return Value(values.find(vm.argument(0)) != values.end()); return Value(values.find(vm.argument(0)) != values.end());
} }
// 24.2.3.10 Set.prototype.values ( ), https://tc39.es/ecma262/#sec-set.prototype.values // 24.2.3.10 Set.prototype.values ( ), https://tc39.es/ecma262/#sec-set.prototype.values
JS_DEFINE_OLD_NATIVE_FUNCTION(SetPrototype::values) JS_DEFINE_NATIVE_FUNCTION(SetPrototype::values)
{ {
auto* set = TRY_OR_DISCARD(typed_this_object(global_object)); auto* set = TRY(typed_this_object(global_object));
return SetIterator::create(global_object, *set, Object::PropertyKind::Value); return SetIterator::create(global_object, *set, Object::PropertyKind::Value);
} }
// 24.2.3.9 get Set.prototype.size, https://tc39.es/ecma262/#sec-get-set.prototype.size // 24.2.3.9 get Set.prototype.size, https://tc39.es/ecma262/#sec-get-set.prototype.size
JS_DEFINE_OLD_NATIVE_FUNCTION(SetPrototype::size_getter) JS_DEFINE_NATIVE_FUNCTION(SetPrototype::size_getter)
{ {
auto* set = TRY_OR_DISCARD(typed_this_object(global_object)); auto* set = TRY(typed_this_object(global_object));
return Value(set->values().size()); return Value(set->values().size());
} }

View file

@ -20,15 +20,15 @@ public:
virtual ~SetPrototype() override; virtual ~SetPrototype() override;
private: private:
JS_DECLARE_OLD_NATIVE_FUNCTION(add); JS_DECLARE_NATIVE_FUNCTION(add);
JS_DECLARE_OLD_NATIVE_FUNCTION(clear); JS_DECLARE_NATIVE_FUNCTION(clear);
JS_DECLARE_OLD_NATIVE_FUNCTION(delete_); JS_DECLARE_NATIVE_FUNCTION(delete_);
JS_DECLARE_OLD_NATIVE_FUNCTION(entries); JS_DECLARE_NATIVE_FUNCTION(entries);
JS_DECLARE_OLD_NATIVE_FUNCTION(for_each); JS_DECLARE_NATIVE_FUNCTION(for_each);
JS_DECLARE_OLD_NATIVE_FUNCTION(has); JS_DECLARE_NATIVE_FUNCTION(has);
JS_DECLARE_OLD_NATIVE_FUNCTION(values); JS_DECLARE_NATIVE_FUNCTION(values);
JS_DECLARE_OLD_NATIVE_FUNCTION(size_getter); JS_DECLARE_NATIVE_FUNCTION(size_getter);
}; };
} }