Skip to content

Commit

Permalink
LibJS: Implement Set.prototype.isDisjointFrom
Browse files Browse the repository at this point in the history
  • Loading branch information
IdanHo authored and linusg committed Dec 2, 2022
1 parent 3470f33 commit 2e806da
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ namespace JS {
P(intersection) \
P(is) \
P(isArray) \
P(isDisjointFrom) \
P(isExtensible) \
P(isFinite) \
P(isFrozen) \
Expand Down
56 changes: 56 additions & 0 deletions Userland/Libraries/LibJS/Runtime/SetPrototype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ void SetPrototype::initialize(Realm& realm)
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_function(realm, vm.names.isDisjointFrom, is_disjoint_from, 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);
Expand Down Expand Up @@ -516,4 +517,59 @@ JS_DEFINE_NATIVE_FUNCTION(SetPrototype::is_superset_of)
return true;
}

// 7 Set.prototype.isDisjointFrom ( other ), https://tc39.es/proposal-set-methods/#sec-set.prototype.isdisjointfrom
JS_DEFINE_NATIVE_FUNCTION(SetPrototype::is_disjoint_from)
{
// 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]], then
if (this_size <= other_record.size) {
// a. For each element e of O.[[SetData]], do
for (auto& element : *set) {
// i. If e is not empty, then
// 1. Let inOther be ToBoolean(? Call(otherRec.[[Has]], otherRec.[[Set]], « e »)).
auto in_other = TRY(call(vm, *other_record.has, other_record.set, element.key)).to_boolean();
// 2. If inOther is true, return false.
if (in_other)
return false;
}
}
// 6. Else,
else {
// a. Let keysIter be ? GetKeysIterator(otherRec).
auto keys_iterator = TRY(get_keys_iterator(vm, other_record));

// b. Let next be true.
auto next = true;

// c. Repeat, while next is not false,
while (next) {
// i. Set next to ? IteratorStep(keysIter).
auto* iterator_result = TRY(iterator_step(vm, keys_iterator));
next = iterator_result;

// ii. If next is not false, then
if (next) {
// 1. Let nextValue be ? IteratorValue(next).
auto next_value = TRY(iterator_value(vm, *iterator_result));

// 2. If SetDataHas(O.[[SetData]], nextValue) is true, return false.
if (set->set_has(next_value))
return false;
}
}
}

// 7. Return true.
return true;
}

}
1 change: 1 addition & 0 deletions Userland/Libraries/LibJS/Runtime/SetPrototype.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class SetPrototype final : public PrototypeObject<SetPrototype, Set> {
JS_DECLARE_NATIVE_FUNCTION(symmetric_difference);
JS_DECLARE_NATIVE_FUNCTION(is_subset_of);
JS_DECLARE_NATIVE_FUNCTION(is_superset_of);
JS_DECLARE_NATIVE_FUNCTION(is_disjoint_from);

JS_DECLARE_NATIVE_FUNCTION(size_getter);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
test("basic functionality", () => {
expect(Set.prototype.isDisjointFrom).toHaveLength(1);

const set1 = new Set(["a", "b"]);
const set2 = new Set(["c"]);
expect(set1.isDisjointFrom(set1)).toBeFalse();
expect(set1.isDisjointFrom(set2)).toBeTrue();
expect(set2.isDisjointFrom(set1)).toBeTrue();
});

0 comments on commit 2e806da

Please sign in to comment.