From 3470f33a0f758dd2fbb65a95c2cc4dcd655c11b8 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Thu, 1 Dec 2022 22:54:43 +0200 Subject: [PATCH] LibJS: Implement Set.prototype.isSupersetOf --- .../LibJS/Runtime/CommonPropertyNames.h | 1 + .../Libraries/LibJS/Runtime/SetPrototype.cpp | 45 +++++++++++++++++++ .../Libraries/LibJS/Runtime/SetPrototype.h | 1 + .../Set/Set.prototype.isSupersetOf.js | 8 ++++ 4 files changed, 55 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Set/Set.prototype.isSupersetOf.js diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index a6d9e47c6a..29c8e9f27a 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -301,6 +301,7 @@ namespace JS { P(isSafeInteger) \ P(isSealed) \ P(isSubsetOf) \ + P(isSupersetOf) \ P(isView) \ P(isoDay) \ P(isoHour) \ diff --git a/Userland/Libraries/LibJS/Runtime/SetPrototype.cpp b/Userland/Libraries/LibJS/Runtime/SetPrototype.cpp index 03ee6483b5..88f8eaeb99 100644 --- a/Userland/Libraries/LibJS/Runtime/SetPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/SetPrototype.cpp @@ -37,6 +37,7 @@ void SetPrototype::initialize(Realm& realm) define_native_function(realm, vm.names.difference, difference, 1, attr); define_native_function(realm, vm.names.symmetricDifference, symmetric_difference, 1, attr); define_native_function(realm, vm.names.isSubsetOf, is_subset_of, 1, attr); + define_native_function(realm, vm.names.isSupersetOf, is_superset_of, 1, attr); define_native_accessor(realm, vm.names.size, size_getter, {}, Attribute::Configurable); define_direct_property(vm.names.keys, get_without_side_effects(vm.names.values), attr); @@ -471,4 +472,48 @@ JS_DEFINE_NATIVE_FUNCTION(SetPrototype::is_subset_of) return true; } +// 6 Set.prototype.isSupersetOf ( other ), https://tc39.es/proposal-set-methods/#sec-set.prototype.issupersetof +JS_DEFINE_NATIVE_FUNCTION(SetPrototype::is_superset_of) +{ + // 1. Let O be the this value. + // 2. Perform ? RequireInternalSlot(O, [[SetData]]). + auto* set = TRY(typed_this_object(vm)); + + // 3. Let otherRec be ? GetSetRecord(other). + auto other_record = TRY(get_set_record(vm, vm.argument(0))); + + // 4. Let thisSize be the number of elements in O.[[SetData]]. + auto this_size = set->set_size(); + + // 5. If thisSize < otherRec.[[Size]], return false. + if (this_size < other_record.size) + return false; + + // 6. Let keysIter be ? GetKeysIterator(otherRec). + auto keys_iterator = TRY(get_keys_iterator(vm, other_record)); + + // 7. Let next be true. + auto next = true; + + // 8. Repeat, while next is not false, + while (next) { + // a. Set next to ? IteratorStep(keysIter). + auto* iterator_result = TRY(iterator_step(vm, keys_iterator)); + next = iterator_result; + + // b. If next is not false, then + if (next) { + // i. Let nextValue be ? IteratorValue(next). + auto next_value = TRY(iterator_value(vm, *iterator_result)); + + // ii. If SetDataHas(O.[[SetData]], nextValue) is false, return false. + if (!set->set_has(next_value)) + return false; + } + } + + // 9. Return true. + return true; +} + } diff --git a/Userland/Libraries/LibJS/Runtime/SetPrototype.h b/Userland/Libraries/LibJS/Runtime/SetPrototype.h index ed0dd74ad9..a8eee9558a 100644 --- a/Userland/Libraries/LibJS/Runtime/SetPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/SetPrototype.h @@ -33,6 +33,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(difference); JS_DECLARE_NATIVE_FUNCTION(symmetric_difference); JS_DECLARE_NATIVE_FUNCTION(is_subset_of); + JS_DECLARE_NATIVE_FUNCTION(is_superset_of); JS_DECLARE_NATIVE_FUNCTION(size_getter); }; diff --git a/Userland/Libraries/LibJS/Tests/builtins/Set/Set.prototype.isSupersetOf.js b/Userland/Libraries/LibJS/Tests/builtins/Set/Set.prototype.isSupersetOf.js new file mode 100644 index 0000000000..3475ab2787 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Set/Set.prototype.isSupersetOf.js @@ -0,0 +1,8 @@ +test("basic functionality", () => { + expect(Set.prototype.isSupersetOf).toHaveLength(1); + + const set1 = new Set(["a", "b", "c"]); + const set2 = new Set(["b", "c"]); + expect(set1.isSupersetOf(set2)).toBeTrue(); + expect(set2.isSupersetOf(set1)).toBeFalse(); +});