Stage 4 Draft / July 6, 2024

Set methods

1 Set.prototype.union ( other )

This method performs the following steps when called:

  1. Let O be the this value.
  2. Perform ? RequireInternalSlot(O, [[SetData]]).
  3. Let otherRec be ? GetSetRecord(other).
  4. Let keysIter be ? GetIteratorFromMethod(otherRec.[[Set]], otherRec.[[Keys]]).
  5. Let resultSetData be a copy of O.[[SetData]].
  6. Let next be true.
  7. Repeat, while next is not false,
    1. Set next to ? IteratorStep(keysIter).
    2. If next is not false, then
      1. Let nextValue be ? IteratorValue(next).
      2. If nextValue is -0𝔽, set nextValue to +0𝔽.
      3. If SetDataHas(resultSetData, nextValue) is false, then
        1. Append nextValue to resultSetData.
  8. Let result be OrdinaryObjectCreate(%Set.prototype%, « [[SetData]] »).
  9. Set result.[[SetData]] to resultSetData.
  10. Return result.

2 Set.prototype.intersection ( other )

This method performs the following steps when called:

  1. Let O be the this value.
  2. Perform ? RequireInternalSlot(O, [[SetData]]).
  3. Let otherRec be ? GetSetRecord(other).
  4. Let resultSetData be a new empty List.
  5. If SetDataSize(O.[[SetData]]) ≤ otherRec.[[Size]], then
    1. Let thisSize be the number of elements in O.[[SetData]].
    2. Let index be 0.
    3. Repeat, while index < thisSize,
      1. Let e be O.[[SetData]][index].
      2. Set index to index + 1.
      3. If e is not empty, then
        1. Let inOther be ToBoolean(? Call(otherRec.[[Has]], otherRec.[[Set]], « e »)).
        2. If inOther is true, then
          1. NOTE: It is possible for earlier calls to otherRec.[[Has]] to remove and re-add an element of O.[[SetData]], which can cause the same element to be visited twice during this iteration.
          2. Let alreadyInResult be SetDataHas(resultSetData, e).
          3. If alreadyInResult is false, then
            1. Append e to resultSetData.
        3. NOTE: The number of elements in O.[[SetData]] may have increased during execution of otherRec.[[Has]].
        4. Set thisSize to the number of elements of O.[[SetData]].
  6. Else,
    1. Let keysIter be ? GetIteratorFromMethod(otherRec.[[Set]], otherRec.[[Keys]]).
    2. Let next be true.
    3. Repeat, while next is not false,
      1. Set next to ? IteratorStep(keysIter).
      2. If next is not false, then
        1. Let nextValue be ? IteratorValue(next).
        2. If nextValue is -0𝔽, set nextValue to +0𝔽.
        3. NOTE: Because other is an arbitrary object, it is possible for its "keys" iterator to produce the same value more than once.
        4. Let alreadyInResult be SetDataHas(resultSetData, nextValue).
        5. Let inThis be SetDataHas(O.[[SetData]], nextValue).
        6. If alreadyInResult is false and inThis is true, then
          1. Append nextValue to resultSetData.
  7. Let result be OrdinaryObjectCreate(%Set.prototype%, « [[SetData]] »).
  8. Set result.[[SetData]] to resultSetData.
  9. Return result.

3 Set.prototype.difference ( other )

This method performs the following steps when called:

  1. Let O be the this value.
  2. Perform ? RequireInternalSlot(O, [[SetData]]).
  3. Let otherRec be ? GetSetRecord(other).
  4. Let resultSetData be a copy of O.[[SetData]].
  5. If SetDataSize(O.[[SetData]]) ≤ otherRec.[[Size]], then
    1. Let thisSize be the number of elements in O.[[SetData]].
    2. Let index be 0.
    3. Repeat, while index < thisSize,
      1. Let e be resultSetData[index].
      2. If e is not empty, then
        1. Let inOther be ToBoolean(? Call(otherRec.[[Has]], otherRec.[[Set]], « e »)).
        2. If inOther is true, then
          1. Set resultSetData[index] to empty.
      3. Set index to index + 1.
  6. Else,
    1. Let keysIter be ? GetIteratorFromMethod(otherRec.[[Set]], otherRec.[[Keys]]).
    2. Let next be true.
    3. Repeat, while next is not false,
      1. Set next to ? IteratorStep(keysIter).
      2. If next is not false, then
        1. Let nextValue be ? IteratorValue(next).
        2. If nextValue is -0𝔽, set nextValue to +0𝔽.
        3. Let valueIndex be SetDataIndex(resultSetData, nextValue).
        4. If valueIndex is not missing, then
          1. Set resultSetData[valueIndex] to empty.
  7. Let result be OrdinaryObjectCreate(%Set.prototype%, « [[SetData]] »).
  8. Set result.[[SetData]] to resultSetData.
  9. Return result.

4 Set.prototype.symmetricDifference ( other )

This method performs the following steps when called:

  1. Let O be the this value.
  2. Perform ? RequireInternalSlot(O, [[SetData]]).
  3. Let otherRec be ? GetSetRecord(other).
  4. Let keysIter be ? GetIteratorFromMethod(otherRec.[[Set]], otherRec.[[Keys]]).
  5. Let resultSetData be a copy of O.[[SetData]].
  6. Let next be true.
  7. Repeat, while next is not false,
    1. Set next to ? IteratorStep(keysIter).
    2. If next is not false, then
      1. Let nextValue be ? IteratorValue(next).
      2. If nextValue is -0𝔽, set nextValue to +0𝔽.
      3. Let resultIndex be SetDataIndex(resultSetData, nextValue).
      4. If SetDataHas(O.[[SetData]], nextValue) is true, then
        1. If resultIndex is not missing, set resultSetData[resultIndex] to empty.
      5. Else,
        1. If resultIndex is missing, append nextValue to resultSetData.
  8. Let result be OrdinaryObjectCreate(%Set.prototype%, « [[SetData]] »).
  9. Set result.[[SetData]] to resultSetData.
  10. Return result.

5 Set.prototype.isSubsetOf ( other )

This method performs the following steps when called:

  1. Let O be the this value.
  2. Perform ? RequireInternalSlot(O, [[SetData]]).
  3. Let otherRec be ? GetSetRecord(other).
  4. If SetDataSize(O.[[SetData]]) > otherRec.[[Size]], return false.
  5. Let thisSize be the number of elements in O.[[SetData]].
  6. Let index be 0.
  7. Repeat, while index < thisSize,
    1. Let e be O.[[SetData]][index].
    2. Set index to index + 1.
    3. If e is not empty, then
      1. Let inOther be ToBoolean(? Call(otherRec.[[Has]], otherRec.[[Set]], « e »)).
      2. If inOther is false, return false.
      3. NOTE: The number of elements in O.[[SetData]] may have increased during execution of otherRec.[[Has]].
      4. Set thisSize to the number of elements of O.[[SetData]].
  8. Return true.

6 Set.prototype.isSupersetOf ( other )

This method performs the following steps when called:

  1. Let O be the this value.
  2. Perform ? RequireInternalSlot(O, [[SetData]]).
  3. Let otherRec be ? GetSetRecord(other).
  4. If SetDataSize(O.[[SetData]]) < otherRec.[[Size]], return false.
  5. Let keysIter be ? GetIteratorFromMethod(otherRec.[[Set]], otherRec.[[Keys]]).
  6. Let next be true.
  7. Repeat, while next is not false,
    1. Set next to ? IteratorStep(keysIter).
    2. If next is not false, then
      1. Let nextValue be ? IteratorValue(next).
      2. If SetDataHas(O.[[SetData]], nextValue) is false, then
        1. Perform ? IteratorClose(keysIter, NormalCompletion(unused)).
        2. Return false.
  8. Return true.

7 Set.prototype.isDisjointFrom ( other )

This method performs the following steps when called:

  1. Let O be the this value.
  2. Perform ? RequireInternalSlot(O, [[SetData]]).
  3. Let otherRec be ? GetSetRecord(other).
  4. If SetDataSize(O.[[SetData]]) ≤ otherRec.[[Size]], then
    1. Let thisSize be the number of elements in O.[[SetData]].
    2. Let index be 0.
    3. Repeat, while index < thisSize,
      1. Let e be O.[[SetData]][index].
      2. Set index to index + 1.
      3. If e is not empty, then
        1. Let inOther be ToBoolean(? Call(otherRec.[[Has]], otherRec.[[Set]], « e »)).
        2. If inOther is true, return false.
        3. NOTE: The number of elements in O.[[SetData]] may have increased during execution of otherRec.[[Has]].
        4. Set thisSize to the number of elements of O.[[SetData]].
  5. Else,
    1. Let keysIter be ? GetIteratorFromMethod(otherRec.[[Set]], otherRec.[[Keys]]).
    2. Let next be true.
    3. Repeat, while next is not false,
      1. Set next to ? IteratorStep(keysIter).
      2. If next is not false, then
        1. Let nextValue be ? IteratorValue(next).
        2. If SetDataHas(O.[[SetData]], nextValue) is true, then
          1. Perform ? IteratorClose(keysIter, NormalCompletion(unused)).
          2. Return false.
  6. Return true.

8 Set Records

An Set Record is a Record value used to encapsulate the interface of a Set or similar object.

Set Records have the fields listed in Table 1.

Table 1: Set Record Fields
Field Name Value Meaning
[[Set]] an Object. the Set or similar object.
[[Size]] a non-negative integer or +∞ The reported size of the object.
[[Has]] a function object The has method of the object.
[[Keys]] a function object The keys method of the object.

9 GetSetRecord ( obj )

The abstract operation GetSetRecord takes argument obj (an ECMAScript language value) and returns either a normal completion containing a Set Record or a throw completion. It performs the following steps when called:

  1. If obj is not an Object, throw a TypeError exception.
  2. Let rawSize be ? Get(obj, "size").
  3. Let numSize be ? ToNumber(rawSize).
  4. NOTE: If rawSize is undefined, then numSize will be NaN.
  5. If numSize is NaN, throw a TypeError exception.
  6. Let intSize be ! ToIntegerOrInfinity(numSize).
  7. If intSize < 0, throw a RangeError exception.
  8. Let has be ? Get(obj, "has").
  9. If IsCallable(has) is false, throw a TypeError exception.
  10. Let keys be ? Get(obj, "keys").
  11. If IsCallable(keys) is false, throw a TypeError exception.
  12. Return a new Set Record { [[Set]]: obj, [[Size]]: intSize, [[Has]]: has, [[Keys]]: keys }.

10 SetDataHas ( setData, value )

The abstract operation SetDataHas takes arguments setData (a List of either empty or ECMAScript language values) and value (an ECMAScript language value) and returns a boolean. It performs the following steps when called:

  1. If SetDataIndex(setData, value) is missing, return false.
  2. Return true.

11 SetDataIndex ( setData, value )

The abstract operation SetDataIndex takes arguments setData (a List of either empty or ECMAScript language values) and value (an ECMAScript language value) and returns either missing or a non-negative integer. It performs the following steps when called:

  1. Let _size be the number of elements in setData.
  2. Let index be 0.
  3. Repeat, while index < _size,
    1. Let e be setData[index].
    2. If e is not empty and SameValueZero(e, value) is true, then
      1. Return index.
    3. Set index to index + 1.
  4. Return missing.

12 SetDataSize ( setData )

The abstract operation SetDataSize takes argument setData (a List of either empty or ECMAScript language values) and returns a non-negative integer. It performs the following steps when called:

  1. Let count be 0.
  2. For each element e of setData, do
    1. If e is not empty, set count to count + 1.
  3. Return count.

A Copyright & Software License

Copyright Notice

© 2024 Michał Wadas, Sathya Gunasekaran, Kevin Gibbons

Software License

All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT https://ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the authors nor Ecma International may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.