| // Copyright 2023 the V8 project authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| namespace collections { |
| |
| // https://tc39.es/proposal-set-methods/#sec-set.prototype.union |
| @incrementUseCounter('v8::Isolate::kSetMethods') |
| transitioning javascript builtin SetPrototypeUnion( |
| js-implicit context: NativeContext, receiver: JSAny)(other: JSAny): JSSet { |
| const methodName: constexpr string = 'Set.prototype.union'; |
| const fastIteratorResultMap = GetIteratorResultMap(); |
| |
| // 1. Let O be the this value. |
| // 2. Perform ? RequireInternalSlot(O, [[SetData]]). |
| const o = Cast<JSSet>(receiver) otherwise |
| ThrowTypeError( |
| MessageTemplate::kIncompatibleMethodReceiver, methodName, receiver); |
| |
| // 3. Let otherRec be ? GetSetRecord(other). |
| let otherRec = GetSetRecord(other, methodName); |
| |
| let table = NewStableBackingTableWitness(o); |
| |
| let resultSetData: OrderedHashSet; |
| |
| try { |
| typeswitch (other) { |
| case (otherSet: JSSetWithNoCustomIteration): { |
| CheckSetRecordHasJSSetMethods(otherRec) otherwise SlowPath; |
| |
| const otherTable = NewStableBackingTableWitness(otherSet); |
| |
| let otherIterator = collections::NewUnmodifiedOrderedHashSetIterator( |
| otherTable.GetTable()); |
| |
| resultSetData = Cast<OrderedHashSet>(CloneFixedArray( |
| table.GetTable(), ExtractFixedArrayFlag::kFixedArrays)) |
| otherwise unreachable; |
| |
| while (true) { |
| const nextValue = |
| otherIterator.Next() otherwise goto Done(resultSetData); |
| resultSetData = AddToSetTable(resultSetData, nextValue, methodName); |
| } |
| } |
| case (otherMap: JSMapWithNoCustomIteration): { |
| CheckSetRecordHasJSMapMethods(otherRec) otherwise SlowPath; |
| |
| const otherTable = NewStableBackingTableWitness(otherMap); |
| |
| let otherIterator = collections::NewUnmodifiedOrderedHashMapIterator( |
| otherTable.GetTable()); |
| |
| resultSetData = Cast<OrderedHashSet>(CloneFixedArray( |
| table.GetTable(), ExtractFixedArrayFlag::kFixedArrays)) |
| otherwise unreachable; |
| |
| while (true) { |
| const nextValue = |
| otherIterator.Next() otherwise goto Done(resultSetData); |
| resultSetData = |
| AddToSetTable(resultSetData, nextValue.key, methodName); |
| } |
| } |
| case (JSAny): { |
| goto SlowPath; |
| } |
| } |
| } label SlowPath { |
| // 4. Let keysIter be ? GetKeysIterator(otherRec). |
| let keysIter = |
| GetKeysIterator(otherRec.object, UnsafeCast<Callable>(otherRec.keys)); |
| |
| table.ReloadTable(); |
| // 5. Let resultSetData be a copy of O.[[SetData]]. |
| resultSetData = Cast<OrderedHashSet>( |
| CloneFixedArray(table.GetTable(), ExtractFixedArrayFlag::kFixedArrays)) |
| otherwise unreachable; |
| |
| // 6. Let next be true. |
| let nextRecord: JSReceiver; |
| // 7. Repeat, while next is not false, |
| while (true) { |
| // a. Set next to ? IteratorStep(keysIter). |
| nextRecord = iterator::IteratorStep(keysIter, fastIteratorResultMap) |
| otherwise goto Done(resultSetData); |
| |
| // b. If next is not false, then |
| // i. Let nextValue be ? IteratorValue(next). |
| const nextValue = |
| iterator::IteratorValue(nextRecord, fastIteratorResultMap); |
| |
| // ii. If nextValue is -0𝔽, set nextValue to +0𝔽. |
| // iii. If SetDataHas(resultSetData, nextValue) is false, then |
| // 1. Append nextValue to resultSetData. |
| resultSetData = AddToSetTable(resultSetData, nextValue, methodName); |
| } |
| } label Done(resultSetData: OrderedHashSet) { |
| // 8. Let result be |
| // OrdinaryObjectCreate(%Set.prototype%, « [[SetData]]»). |
| // 9. Set result.[[SetData]] to resultSetData. |
| // 10. Return result. |
| return new JSSet{ |
| map: *NativeContextSlot(ContextSlot::JS_SET_MAP_INDEX), |
| properties_or_hash: kEmptyFixedArray, |
| elements: kEmptyFixedArray, |
| table: resultSetData |
| }; |
| } |
| unreachable; |
| } |
| } |