Stage 2 Draft / January 11, 2023

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 ? GetKeysIterator(otherRec).
  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. Let thisSize be the number of elements in O.[[SetData]].
  6. If thisSizeotherRec.[[Size]], then
    1. Let index be 0.
    2. 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]].
  7. Else,
    1. Let keysIter be ? GetKeysIterator(otherRec).
    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.
    4. NOTE: It is possible for resultSetData not to be a subset of O.[[SetData]] at this point because arbitrary code may have been executed by the iterator, including code which modifies O.[[SetData]].
    5. Sort the elements of resultSetData so that all elements which are also in O.[[SetData]] are ordered as they are in O.[[SetData]], and any additional elements are moved to the end of the list in the same order as they were before sorting resultSetData.
  8. Let result be OrdinaryObjectCreate(%Set.prototype%, « [[SetData]] »).
  9. Set result.[[SetData]] to resultSetData.
  10. Return result.
Note
It is expected that implementations can achieve the order required by step 7.e in time proportional only to the size of the output, not O. For example, using this data structure, the relative order of two elements within O.[[SetData]] can be determined without needing to iterate the entire dataTable array by directly comparing the pointers to their entries in dataTable.

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. Let thisSize be the number of elements in O.[[SetData]].
  6. If thisSizeotherRec.[[Size]], then
    1. Let index be 0.
    2. Repeat, while index < thisSize,
      1. Let e be resultSetData[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. Set resultSetData[index] to empty.
  7. Else,
    1. Let keysIter be ? GetKeysIterator(otherRec).
    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. If SetDataHas(resultSetData, nextValue) is true, then
          1. Remove nextValue from resultSetData.
  8. Let result be OrdinaryObjectCreate(%Set.prototype%, « [[SetData]] »).
  9. Set result.[[SetData]] to resultSetData.
  10. 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 ? GetKeysIterator(otherRec).
  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 inResult be SetDataHas(resultSetData, nextValue).
      4. If SetDataHas(O.[[SetData]], nextValue) is true, then
        1. If inResult is true, remove nextValue from resultSetData.
      5. Else,
        1. If inResult is false, 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. Let thisSize be the number of elements in O.[[SetData]].
  5. If thisSize > otherRec.[[Size]], return false.
  6. Let index be 0.
  7. Repeat, while index < thisSize,
    1. Let e be O.[[SetData]][index].
    2. Set index to index + 1.
    3. Let inOther be ToBoolean(? Call(otherRec.[[Has]], otherRec.[[Set]], « e »)).
    4. If inOther is false, return false.
    5. NOTE: The number of elements in O.[[SetData]] may have increased during execution of otherRec.[[Has]].
    6. 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. Let thisSize be the number of elements in O.[[SetData]].
  5. If thisSize < otherRec.[[Size]], return false.
  6. Let keysIter be ? GetKeysIterator(otherRec).
  7. Let next be true.
  8. 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.
  9. 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. Let thisSize be the number of elements in O.[[SetData]].
  5. If thisSizeotherRec.[[Size]], then
    1. Let index be 0.
    2. 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]].
  6. Else,
    1. Let keysIter be ? GetKeysIterator(otherRec).
    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.
  7. 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. Let has be ? Get(obj, "has").
  8. If IsCallable(has) is false, throw a TypeError exception.
  9. Let keys be ? Get(obj, "keys").
  10. If IsCallable(keys) is false, throw a TypeError exception.
  11. Return a new Set Record { [[Set]]: obj, [[Size]]: intSize, [[Has]]: has, [[Keys]]: keys }.

10 GetKeysIterator ( setRec )

The abstract operation GetKeysIterator takes argument setRec (a Set Record) and returns either a normal completion containing an Iterator Record or a throw completion. It performs the following steps when called:

  1. Let keysIter be ? Call(setRec.[[Keys]], setRec.[[Set]]).
  2. If keysIter is not an Object, throw a TypeError exception.
  3. Let nextMethod be ? Get(keysIter, "next").
  4. If IsCallable(nextMethod) is false, throw a TypeError exception.
  5. Return a new Iterator Record { [[Iterator]]: keysIter, [[NextMethod]]: nextMethod, [[Done]]: false }.

11 SetDataHas ( resultSetData, value )

The abstract operation SetDataHas takes arguments resultSetData (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. For each element e of resultSetData, do
    1. If e is not empty and SameValueZero(e, value) is true, return true.
  2. Return false.

A Copyright & Software License

Copyright Notice

© 2023 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.