Skip to content

Commit

Permalink
Implement Set.isDisjointFrom
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Jan 14, 2024
1 parent 37e5f78 commit 422b48a
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions Jint/Native/Set/SetPrototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ protected override void Initialize()
["forEach"] = new(new ClrFunction(Engine, "forEach", ForEach, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
["has"] = new(new ClrFunction(Engine, "has", Has, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
["intersection"] = new(new ClrFunction(Engine, "intersection", Intersection, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
["isDisjointFrom"] = new(new ClrFunction(Engine, "isDisjointFrom", IsDisjointFrom, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
["isSubsetOf"] = new(new ClrFunction(Engine, "isSubsetOf", IsSubsetOf, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
["isSupersetOf"] = new(new ClrFunction(Engine, "isSupersetOf", IsSupersetOf, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
["keys"] = new(new ClrFunction(Engine, "keys", Values, 0, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
Expand Down Expand Up @@ -149,6 +150,61 @@ private JsSet Difference(JsValue thisObject, JsValue[] arguments)
return resultSetData;
}

private JsBoolean IsDisjointFrom(JsValue thisObject, JsValue[] arguments)
{
var set = AssertSetInstance(thisObject);
var other = arguments.At(0);
var otherRec = GetSetRecord(other);
var resultSetData = new JsSet(_engine, new OrderedSet<JsValue>(set._set._set));

if (set.Size <= otherRec.Size)
{
if (other is JsSet otherSet)
{
// fast path
return set._set._set.Overlaps(otherSet._set._set) ? JsBoolean.False : JsBoolean.True;
}

var index = 0;
var args = new JsValue[1];
while (index < set.Size)
{
var e = resultSetData[index];
index++;
if (e is not null)
{
args[0] = e;
var inOther = TypeConverter.ToBoolean(otherRec.Has.Call(otherRec.Set, args));
if (inOther)
{
return JsBoolean.False;
}
}
}

return JsBoolean.True;
}

var keysIter = otherRec.Set.GetIteratorFromMethod(_realm, otherRec.Keys);
while (true)
{
if (!keysIter.TryIteratorStep(out var next))
{
break;
}

var nextValue = next.Get(CommonProperties.Value);
if (set.Has(nextValue))
{
keysIter.Close(CompletionType.Normal);
return JsBoolean.False;
}
}

return JsBoolean.True;
}


private JsSet Intersection(JsValue thisObject, JsValue[] arguments)
{
var set = AssertSetInstance(thisObject);
Expand Down

0 comments on commit 422b48a

Please sign in to comment.