ECMA-262 (12th Edition) ECMAScript 2021 Language Specification - page 36

 

  Главная      Manuals     ECMA-262 (12th Edition) ECMAScript 2021 Language Specification

 

Search            copyright infringement  

 

 

 

 

 

 

 

 

 

 

 

Content      ..     34      35      36      37     ..

 

 

 

ECMA-262 (12th Edition) ECMAScript 2021 Language Specification - page 36

 

 

Each 

TypedArray

 

constructor

:

is an intrinsic object that has the structure described below, differing only in the name used as the 

constructor

name instead of 

TypedArray

, in 

Table 60

.

is a function whose behaviour differs based upon the number and types of its arguments. The actual behaviour
of a call of 

TypedArray

 depends upon the number and kind of arguments that are passed to it.

is not intended to be called as a function and will throw an exception when called in that manner.
is designed to be subclassable. It may be used as the value of an 

extends

extends

 clause of a class definition. Subclass

constructors that intend to inherit the specified 

TypedArray

 behaviour must include a 

super

super

 call to the

TypedArray

 

constructor

 to create and initialize the subclass instance with the internal state necessary to support

the 

%TypedArray%

.prototype

.prototype

 built-in methods.

has a 

"length"

 property whose value is 

3

𝔽

.

Each 

TypedArray

 

constructor

 performs the following steps when called:

1.  If NewTarget is 

undefined

, throw a 

TypeError

 exception.

2.  Let 

constructorName

 be the String value of the 

Constructor

 Name value specified in 

Table 60

 for this 

TypedArray

constructor

.

3.  Let 

proto

 be 

"%

"%

TypedArray

TypedArray

.prototype%"

.prototype%"

.

4.  Let 

numberOfArgs

 be the number of elements in 

args

.

5.  If 

numberOfArgs

 = 0, then

a.  Return ? 

AllocateTypedArray

(

constructorName

, NewTarget, 

proto

, 0).

6.  Else,

a.  Let 

firstArgument

 be 

args

[0].

b.  If 

Type

(

firstArgument

) is Object, then

i.  Let 

O

 be ? 

AllocateTypedArray

(

constructorName

, NewTarget, 

proto

).

ii.  If 

firstArgument

 has a [[TypedArrayName]] internal slot, then

1.  Perform ? 

InitializeTypedArrayFromTypedArray

(

O

firstArgument

).

iii.  Else if 

firstArgument

 has an [[ArrayBufferData]] internal slot, then

1.  If 

numberOfArgs

 > 1, let 

byteOffset

 be 

args

[1]; else let 

byteOffset

 be 

undefined

.

2.  If 

numberOfArgs

 > 2, let 

length

 be 

args

[2]; else let 

length

 be 

undefined

.

3.  Perform ? 

InitializeTypedArrayFromArrayBuffer

(

O

firstArgument

byteOffset

length

).

iv.  Else,

1. 

Assert

Type

(

firstArgument

) is Object and 

firstArgument

 does not have either a

[[TypedArrayName]] or an [[ArrayBufferData]] internal slot.

2.  Let 

usingIterator

 be ? 

GetMethod

(

firstArgument

@@iterator

).

3.  If 

usingIterator

 is not 

undefined

, then

a.  Let 

values

 be ? 

IterableToList

(

firstArgument

usingIterator

).

b.  Perform ? 

InitializeTypedArrayFromList

(

O

values

).

4.  Else,

a.  NOTE: 

firstArgument

 is not an Iterable so assume it is already an 

array-like object

.

b.  Perform ? 

InitializeTypedArrayFromArrayLike

(

O

firstArgument

).

v.  Return 

O

.

c.  Else,

i. 

Assert

firstArgument

 is not an Object.

ii.  Let 

elementLength

 be ? 

ToIndex

(

firstArgument

).

23.2.5.1  

TypedArray

 ( ...

args

 )

685

iii.  Return ? 

AllocateTypedArray

(

constructorName

, NewTarget, 

proto

elementLength

).

The abstract operation AllocateTypedArray takes arguments 

constructorName

 (a String which is the name of a

TypedArray 

constructor

 in 

Table 60

), 

newTarget

, and 

defaultProto

 and optional argument 

length

 (a non-negative

integer

). It is used to validate and create an instance of a TypedArray 

constructor

. If the 

length

 argument is passed, an

ArrayBuffer of that length is also allocated and associated with the new TypedArray instance. AllocateTypedArray
provides common semantics that is used by 

TypedArray

. It performs the following steps when called:

1.  Let 

proto

 be ? 

GetPrototypeFromConstructor

(

newTarget

defaultProto

).

2.  Let 

obj

 be ! 

IntegerIndexedObjectCreate

(

proto

).

3. 

Assert

obj

.[[ViewedArrayBuffer]] is 

undefined

.

4.  Set 

obj

.[[TypedArrayName]] to 

constructorName

.

5.  If 

constructorName

 is 

"BigInt64Array"

 or 

"BigUint64Array"

, set 

obj

.[[ContentType]] to 

BigInt

.

6.  Otherwise, set 

obj

.[[ContentType]] to 

Number

.

7.  If 

length

 is not present, then

a.  Set 

obj

.[[ByteLength]] to 0.

b.  Set 

obj

.[[ByteOffset]] to 0.

c.  Set 

obj

.[[ArrayLength]] to 0.

8.  Else,

a.  Perform ? 

AllocateTypedArrayBuffer

(

obj

length

).

9.  Return 

obj

.

The abstract operation InitializeTypedArrayFromTypedArray takes arguments 

O

 (a TypedArray object) and 

srcArray

(a TypedArray object). It performs the following steps when called:

1. 

Assert

O

 is an Object that has a [[TypedArrayName]] internal slot.

2. 

Assert

srcArray

 is an Object that has a [[TypedArrayName]] internal slot.

3.  Let 

srcData

 be 

srcArray

.[[ViewedArrayBuffer]].

4.  If 

IsDetachedBuffer

(

srcData

) is 

true

, throw a 

TypeError

 exception.

5.  Let 

constructorName

 be the String value of 

O

.[[TypedArrayName]].

6.  Let 

elementType

 be the Element Type value in 

Table 60

 for 

constructorName

.

7.  Let 

elementLength

 be 

srcArray

.[[ArrayLength]].

8.  Let 

srcName

 be the String value of 

srcArray

.[[TypedArrayName]].

9.  Let 

srcType

 be the Element Type value in 

Table 60

 for 

srcName

.

10.  Let 

srcElementSize

 be the Element Size value specified in 

Table 60

 for 

srcName

.

11.  Let 

srcByteOffset

 be 

srcArray

.[[ByteOffset]].

12.  Let 

elementSize

 be the Element Size value specified in 

Table 60

 for 

constructorName

.

13.  Let 

byteLength

 be 

elementSize

 

×

 

elementLength

.

14.  If 

IsSharedArrayBuffer

(

srcData

) is 

false

, then

a.  Let 

bufferConstructor

 be ? 

SpeciesConstructor

(

srcData

%ArrayBuffer%

).

15.  Else,

a.  Let 

bufferConstructor

 be 

%ArrayBuffer%

.

16.  If 

elementType

 is the same as 

srcType

, then

a.  Let 

data

 be ? 

CloneArrayBuffer

(

srcData

srcByteOffset

byteLength

bufferConstructor

).

17.  Else,

23.2.5.1.1  AllocateTypedArray ( 

constructorName

newTarget

defaultProto

 [ , 

length

 ] )

23.2.5.1.2  InitializeTypedArrayFromTypedArray ( 

O

srcArray

 )

686

a.  Let 

data

 be ? 

AllocateArrayBuffer

(

bufferConstructor

byteLength

).

b.  If 

IsDetachedBuffer

(

srcData

) is 

true

, throw a 

TypeError

 exception.

c.  If 

srcArray

.[[ContentType]] 

 

O

.[[ContentType]], throw a 

TypeError

 exception.

d.  Let 

srcByteIndex

 be 

srcByteOffset

.

e.  Let 

targetByteIndex

 be 0.

f.  Let 

count

 be 

elementLength

.

g.  Repeat, while 

count

 > 0,

i.  Let 

value

 be 

GetValueFromBuffer

(

srcData

srcByteIndex

srcType

true

Unordered

).

ii.  Perform 

SetValueInBuffer

(

data

targetByteIndex

elementType

value

true

Unordered

).

iii.  Set 

srcByteIndex

 to 

srcByteIndex

 + 

srcElementSize

.

iv.  Set 

targetByteIndex

 to 

targetByteIndex

 + 

elementSize

.

v.  Set 

count

 to 

count

 - 1.

18.  Set 

O

.[[ViewedArrayBuffer]] to 

data

.

19.  Set 

O

.[[ByteLength]] to 

byteLength

.

20.  Set 

O

.[[ByteOffset]] to 0.

21.  Set 

O

.[[ArrayLength]] to 

elementLength

.

The abstract operation InitializeTypedArrayFromArrayBuffer takes arguments 

O

 (a TypedArray object), 

buffer

 (an

ArrayBuffer object), 

byteOffset

 (an 

ECMAScript language value

), and 

length

 (an 

ECMAScript language value

). It

performs the following steps when called:

1. 

Assert

O

 is an Object that has a [[TypedArrayName]] internal slot.

2. 

Assert

buffer

 is an Object that has an [[ArrayBufferData]] internal slot.

3.  Let 

constructorName

 be the String value of 

O

.[[TypedArrayName]].

4.  Let 

elementSize

 be the Element Size value specified in 

Table 60

 for 

constructorName

.

5.  Let 

offset

 be ? 

ToIndex

(

byteOffset

).

6.  If 

offset

 

modulo

 

elementSize

 

 0, throw a 

RangeError

 exception.

7.  If 

length

 is not 

undefined

, then

a.  Let 

newLength

 be ? 

ToIndex

(

length

).

8.  If 

IsDetachedBuffer

(

buffer

) is 

true

, throw a 

TypeError

 exception.

9.  Let 

bufferByteLength

 be 

buffer

.[[ArrayBufferByteLength]].

10.  If 

length

 is 

undefined

, then

a.  If 

bufferByteLength

 

modulo

 

elementSize

 

 0, throw a 

RangeError

 exception.

b.  Let 

newByteLength

 be 

bufferByteLength

 - 

offset

.

c.  If 

newByteLength

 < 0, throw a 

RangeError

 exception.

11.  Else,

a.  Let 

newByteLength

 be 

newLength

 

×

 

elementSize

.

b.  If 

offset

 + 

newByteLength

 > 

bufferByteLength

, throw a 

RangeError

 exception.

12.  Set 

O

.[[ViewedArrayBuffer]] to 

buffer

.

13.  Set 

O

.[[ByteLength]] to 

newByteLength

.

14.  Set 

O

.[[ByteOffset]] to 

offset

.

15.  Set 

O

.[[ArrayLength]] to 

newByteLength

 / 

elementSize

.

The abstract operation InitializeTypedArrayFromList takes arguments 

O

 (a TypedArray object) and 

values

 (a 

List

 of

ECMAScript language values). It performs the following steps when called:

23.2.5.1.3  InitializeTypedArrayFromArrayBuffer ( 

O

buffer

byteOffset

length

 )

23.2.5.1.4  InitializeTypedArrayFromList ( 

O

values

 )

687

1. 

Assert

O

 is an Object that has a [[TypedArrayName]] internal slot.

2.  Let 

len

 be the number of elements in 

values

.

3.  Perform ? 

AllocateTypedArrayBuffer

(

O

len

).

4.  Let 

k

 be 0.

5.  Repeat, while 

k

 < 

len

,

a.  Let 

Pk

 be ! 

ToString

(

(

k

)).

b.  Let 

kValue

 be the first element of 

values

 and remove that element from 

values

.

c.  Perform ? 

Set

(

O

Pk

kValue

true

).

d.  Set 

k

 to 

k

 + 1.

6. 

Assert

values

 is now an empty 

List

.

The abstract operation InitializeTypedArrayFromArrayLike takes arguments 

O

 (a TypedArray object) and 

arrayLike

(an Object that is neither a TypedArray object nor an ArrayBuffer object). It performs the following steps when called:

1. 

Assert

O

 is an Object that has a [[TypedArrayName]] internal slot.

2.  Let 

len

 be ? 

LengthOfArrayLike

(

arrayLike

).

3.  Perform ? 

AllocateTypedArrayBuffer

(

O

len

).

4.  Let 

k

 be 0.

5.  Repeat, while 

k

 < 

len

,

a.  Let 

Pk

 be ! 

ToString

(

(

k

)).

b.  Let 

kValue

 be ? 

Get

(

arrayLike

Pk

).

c.  Perform ? 

Set

(

O

Pk

kValue

true

).

d.  Set 

k

 to 

k

 + 1.

The abstract operation AllocateTypedArrayBuffer takes arguments 

O

 (a TypedArray object) and 

length

 (a non-negative

integer

). It allocates and associates an ArrayBuffer with 

O

. It performs the following steps when called:

1. 

Assert

O

 is an Object that has a [[ViewedArrayBuffer]] internal slot.

2. 

Assert

O

.[[ViewedArrayBuffer]] is 

undefined

.

3.  Let 

constructorName

 be the String value of 

O

.[[TypedArrayName]].

4.  Let 

elementSize

 be the Element Size value specified in 

Table 60

 for 

constructorName

.

5.  Let 

byteLength

 be 

elementSize

 

×

 

length

.

6.  Let 

data

 be ? 

AllocateArrayBuffer

(

%ArrayBuffer%

byteLength

).

7.  Set 

O

.[[ViewedArrayBuffer]] to 

data

.

8.  Set 

O

.[[ByteLength]] to 

byteLength

.

9.  Set 

O

.[[ByteOffset]] to 0.

10.  Set 

O

.[[ArrayLength]] to 

length

.

11.  Return 

O

.

Each 

TypedArray

 

constructor

:

has a [[Prototype]] internal slot whose value is 

%TypedArray%

.

has a 

"name"

 property whose value is the String value of the 

constructor

 name specified for it in 

Table 60

.

23.2.5.1.5  InitializeTypedArrayFromArrayLike ( 

O

arrayLike

 )

23.2.5.1.6  AllocateTypedArrayBuffer ( 

O

length

 )

23.2.6  Properties of the 

TypedArray

 Constructors

688

has the following properties:

The value of 

TypedArray

.BYTES_PER_ELEMENT

.BYTES_PER_ELEMENT

 is the Element Size value specified in 

Table 60

 for 

TypedArray

.

This property has the attributes { [[Writable]]: 

false

, [[Enumerable]]: 

false

, [[Configurable]]: 

false

 }.

The initial value of 

TypedArray

.prototype

.prototype

 is the corresponding 

TypedArray

 prototype intrinsic object (

23.2.7

).

This property has the attributes { [[Writable]]: 

false

, [[Enumerable]]: 

false

, [[Configurable]]: 

false

 }.

Each 

TypedArray

 prototype object:

has a [[Prototype]] internal slot whose value is 

%TypedArray.prototype%

.

is an 

ordinary object

.

does not have a [[ViewedArrayBuffer]] or any other of the internal slots that are specific to 

TypedArray

 instance

objects.

The value of 

TypedArray

.prototype.BYTES_PER_ELEMENT

.prototype.BYTES_PER_ELEMENT

 is the Element Size value specified in 

Table 60

 for

TypedArray

.

This property has the attributes { [[Writable]]: 

false

, [[Enumerable]]: 

false

, [[Configurable]]: 

false

 }.

The initial value of a 

TypedArray

.prototype.constructor

.prototype.constructor

 is the corresponding 

%TypedArray%

 intrinsic

object.

TypedArray

 instances are 

Integer-Indexed exotic objects

. Each 

TypedArray

 instance inherits properties from the

corresponding 

TypedArray

 prototype object. Each 

TypedArray

 instance has the following internal slots:

[[TypedArrayName]], [[ViewedArrayBuffer]], [[ByteLength]], [[ByteOffset]], and [[ArrayLength]].

Map objects are collections of key/value pairs where both the keys and values may be arbitrary ECMAScript

23.2.6.1  

TypedArray

.BYTES_PER_ELEMENT

23.2.6.2  

TypedArray

.prototype

23.2.7  Properties of the 

TypedArray

 Prototype Objects

23.2.7.1  

TypedArray

.prototype.BYTES_PER_ELEMENT

23.2.7.2  

TypedArray

.prototype.constructor

23.2.8  Properties of 

TypedArray

 Instances

24  Keyed Collections

24.1  Map Objects

689

language values. A distinct key value may only occur in one key/value pair within the Map's collection. Distinct key
values are discriminated using the 

SameValueZero

 comparison algorithm.

Map object must be implemented using either hash tables or other mechanisms that, on average, provide access times
that are sublinear on the number of elements in the collection. The data structures used in this Map objects
specification is only intended to describe the required observable semantics of Map objects. It is not intended to be a
viable implementation model.

The Map 

constructor

:

is 

%Map%

.

is the initial value of the 

"Map"

 property of the 

global object

.

creates and initializes a new Map object when called as a 

constructor

.

is not intended to be called as a function and will throw an exception when called in that manner.
is designed to be subclassable. It may be used as the value in an 

extends

extends

 clause of a class definition. Subclass

constructors that intend to inherit the specified Map behaviour must include a 

super

super

 call to the Map

constructor

 to create and initialize the subclass instance with the internal state necessary to support the

Map.prototype

Map.prototype

 built-in methods.

When the 

Map

Map

 function is called with optional argument 

iterable

, the following steps are taken:

1.  If NewTarget is 

undefined

, throw a 

TypeError

 exception.

2.  Let 

map

 be ? 

OrdinaryCreateFromConstructor

(NewTarget, 

"%Map.prototype%"

, « [[MapData]] »).

3.  Set 

map

.[[MapData]] to a new empty 

List

.

4.  If 

iterable

 is either 

undefined

 or 

null

, return 

map

.

5.  Let 

adder

 be ? 

Get

(

map

"set"

).

6.  Return ? 

AddEntriesFromIterable

(

map

iterable

adder

).

NOTE

The abstract operation AddEntriesFromIterable takes arguments 

target

iterable

, and 

adder

 (a 

function object

). 

adder

 will

be invoked, with 

target

 as the receiver. It performs the following steps when called:

1.  If 

IsCallable

(

adder

) is 

false

, throw a 

TypeError

 exception.

2. 

Assert

iterable

 is present, and is neither 

undefined

 nor 

null

.

3.  Let 

iteratorRecord

 be ? 

GetIterator

(

iterable

).

4.  Repeat,

a.  Let 

next

 be ? 

IteratorStep

(

iteratorRecord

).

b.  If 

next

 is 

false

, return 

target

.

If the parameter 

iterable

 is present, it is expected to be an object that implements an 

@@iterator

method that returns an iterator object that produces a two element 

array-like object

 whose first

element is a value that will be used as a Map key and whose second element is the value to
associate with that key.

24.1.1  The Map Constructor

24.1.1.1  Map ( [ 

iterable

 ] )

24.1.1.2  AddEntriesFromIterable ( 

target

iterable

adder

 )

690

c.  Let 

nextItem

 be ? 

IteratorValue

(

next

).

d.  If 

Type

(

nextItem

) is not Object, then

i.  Let 

error

 be 

ThrowCompletion

(a newly created 

TypeError

 object).

ii.  Return ? 

IteratorClose

(

iteratorRecord

error

).

e.  Let 

k

 be 

Get

(

nextItem

"0"

).

f.  If 

k

 is an 

abrupt completion

, return ? 

IteratorClose

(

iteratorRecord

k

).

g.  Let 

v

 be 

Get

(

nextItem

"1"

).

h.  If 

v

 is an 

abrupt completion

, return ? 

IteratorClose

(

iteratorRecord

v

).

i.  Let 

status

 be 

Call

(

adder

target

, « 

k

.[[Value]], 

v

.[[Value]] »).

j.  If 

status

 is an 

abrupt completion

, return ? 

IteratorClose

(

iteratorRecord

status

).

NOTE

The Map 

constructor

:

has a [[Prototype]] internal slot whose value is 

%Function.prototype%

.

has the following properties:

The initial value of 

Map.prototype

Map.prototype

 is the 

Map prototype object

.

This property has the attributes { [[Writable]]: 

false

, [[Enumerable]]: 

false

, [[Configurable]]: 

false

 }.

Map[@@species]

Map[@@species]

 is an 

accessor property

 whose set accessor function is 

undefined

. Its get accessor function

performs the following steps:

1.  Return the 

this

 value.

The value of the 

"name"

 property of this function is 

"get [Symbol.species]"

.

NOTE

The 

Map prototype object

:

is 

%Map.prototype%

.

has a [[Prototype]] internal slot whose value is 

%Object.prototype%

.

The parameter 

iterable

 is expected to be an object that implements an 

@@iterator

 method that

returns an iterator object that produces a two element 

array-like object

 whose first element is a

value that will be used as a Map key and whose second element is the value to associate with
that key.

Methods that create derived collection objects should call 

@@species

 to determine the 

constructor

to use to create the derived objects. Subclass 

constructor

 may over-ride 

@@species

 to change the

default 

constructor

 assignment.

24.1.2  Properties of the Map Constructor

24.1.2.1  Map.prototype

24.1.2.2  get Map [ @@species ]

24.1.3  Properties of the Map Prototype Object

691

is an 

ordinary object

.

does not have a [[MapData]] internal slot.

The following steps are taken:

1.  Let 

M

 be the 

this

 value.

2.  Perform ? 

RequireInternalSlot

(

M

, [[MapData]]).

3.  Let 

entries

 be the 

List

 that is 

M

.[[MapData]].

4.  For each 

Record

 { [[Key]], [[Value]] } 

p

 of 

entries

, do

a.  Set 

p

.[[Key]] to 

empty

.

b.  Set 

p

.[[Value]] to 

empty

.

5.  Return 

undefined

.

NOTE

The initial value of 

Map.prototype.constructor

Map.prototype.constructor

 is 

%Map%

.

The following steps are taken:

1.  Let 

M

 be the 

this

 value.

2.  Perform ? 

RequireInternalSlot

(

M

, [[MapData]]).

3.  Let 

entries

 be the 

List

 that is 

M

.[[MapData]].

4.  For each 

Record

 { [[Key]], [[Value]] } 

p

 of 

entries

, do

a.  If 

p

.[[Key]] is not 

empty

 and 

SameValueZero

(

p

.[[Key]], 

key

) is 

true

, then

i.  Set 

p

.[[Key]] to 

empty

.

ii.  Set 

p

.[[Value]] to 

empty

.

iii.  Return 

true

.

5.  Return 

false

.

NOTE

The following steps are taken:

1.  Let 

M

 be the 

this

 value.

2.  Return ? 

CreateMapIterator

(

M

key+value

).

The existing [[MapData]] 

List

 is preserved because there may be existing Map Iterator objects

that are suspended midway through iterating over that 

List

.

The value 

empty

 is used as a specification device to indicate that an entry has been deleted.

Actual implementations may take other actions such as physically removing the entry from
internal data structures.

24.1.3.1  Map.prototype.clear ( )

24.1.3.2  Map.prototype.constructor

24.1.3.3  Map.prototype.delete ( 

key

 )

24.1.3.4  Map.prototype.entries ( )

692

When the 

forEach

forEach

 method is called with one or two arguments, the following steps are taken:

1.  Let 

M

 be the 

this

 value.

2.  Perform ? 

RequireInternalSlot

(

M

, [[MapData]]).

3.  If 

IsCallable

(

callbackfn

) is 

false

, throw a 

TypeError

 exception.

4.  Let 

entries

 be the 

List

 that is 

M

.[[MapData]].

5.  For each 

Record

 { [[Key]], [[Value]] } 

e

 of 

entries

, do

a.  If 

e

.[[Key]] is not 

empty

, then

i.  Perform ? 

Call

(

callbackfn

thisArg

, « 

e

.[[Value]], 

e

.[[Key]], 

M

 »).

6.  Return 

undefined

.

NOTE

The following steps are taken:

1.  Let 

M

 be the 

this

 value.

2.  Perform ? 

RequireInternalSlot

(

M

, [[MapData]]).

3.  Let 

entries

 be the 

List

 that is 

M

.[[MapData]].

4.  For each 

Record

 { [[Key]], [[Value]] } 

p

 of 

entries

, do

a.  If 

p

.[[Key]] is not 

empty

 and 

SameValueZero

(

p

.[[Key]], 

key

) is 

true

, return 

p

.[[Value]].

5.  Return 

undefined

.

The following steps are taken:

1.  Let 

M

 be the 

this

 value.

2.  Perform ? 

RequireInternalSlot

(

M

, [[MapData]]).

3.  Let 

entries

 be the 

List

 that is 

M

.[[MapData]].

callbackfn

 should be a function that accepts three arguments. 

forEach

forEach

 calls 

callbackfn

 once for

each key/value pair present in the map object, in key insertion order. 

callbackfn

 is called only for

keys of the map which actually exist; it is not called for keys that have been deleted from the
map.

If a 

thisArg

 parameter is provided, it will be used as the 

this

 value for each invocation of

callbackfn

. If it is not provided, 

undefined

 is used instead.

callbackfn

 is called with three arguments: the value of the item, the key of the item, and the Map

object being traversed.

forEach

forEach

 does not directly mutate the object on which it is called but the object may be mutated

by the calls to 

callbackfn

. Each entry of a map's [[MapData]] is only visited once. New keys added

after the call to 

forEach

forEach

 begins are visited. A key will be revisited if it is deleted after it has

been visited and then re-added before the 

forEach

forEach

 call completes. Keys that are deleted after

the call to 

forEach

forEach

 begins and before being visited are not visited unless the key is added

again before the 

forEach

forEach

 call completes.

24.1.3.5  Map.prototype.forEach ( 

callbackfn

 [ , 

thisArg

 ] )

24.1.3.6  Map.prototype.get ( 

key

 )

24.1.3.7  Map.prototype.has ( 

key

 )

693

4.  For each 

Record

 { [[Key]], [[Value]] } 

p

 of 

entries

, do

a.  If 

p

.[[Key]] is not 

empty

 and 

SameValueZero

(

p

.[[Key]], 

key

) is 

true

, return 

true

.

5.  Return 

false

.

The following steps are taken:

1.  Let 

M

 be the 

this

 value.

2.  Return ? 

CreateMapIterator

(

M

key

).

The following steps are taken:

1.  Let 

M

 be the 

this

 value.

2.  Perform ? 

RequireInternalSlot

(

M

, [[MapData]]).

3.  Let 

entries

 be the 

List

 that is 

M

.[[MapData]].

4.  For each 

Record

 { [[Key]], [[Value]] } 

p

 of 

entries

, do

a.  If 

p

.[[Key]] is not 

empty

 and 

SameValueZero

(

p

.[[Key]], 

key

) is 

true

, then

i.  Set 

p

.[[Value]] to 

value

.

ii.  Return 

M

.

5.  If 

key

 is 

-0

𝔽

, set 

key

 to 

+0

𝔽

.

6.  Let 

p

 be the 

Record

 { [[Key]]: 

key

, [[Value]]: 

value

 }.

7.  Append 

p

 as the last element of 

entries

.

8.  Return 

M

.

Map.prototype.size

Map.prototype.size

 is an 

accessor property

 whose set accessor function is 

undefined

. Its get accessor function

performs the following steps:

1.  Let 

M

 be the 

this

 value.

2.  Perform ? 

RequireInternalSlot

(

M

, [[MapData]]).

3.  Let 

entries

 be the 

List

 that is 

M

.[[MapData]].

4.  Let 

count

 be 0.

5.  For each 

Record

 { [[Key]], [[Value]] } 

p

 of 

entries

, do

a.  If 

p

.[[Key]] is not 

empty

, set 

count

 to 

count

 + 1.

6.  Return 

count

).

The following steps are taken:

1.  Let 

M

 be the 

this

 value.

2.  Return ? 

CreateMapIterator

(

M

value

).

24.1.3.8  Map.prototype.keys ( )

24.1.3.9  Map.prototype.set ( 

key

value

 )

24.1.3.10  get Map.prototype.size

24.1.3.11  Map.prototype.values ( )

24.1.3.12  Map.prototype [ @@iterator ] ( )

694

The initial value of the 

@@iterator

 property is the same 

function object

 as the initial value of the 

"entries"

 property.

The initial value of the 

@@toStringTag

 property is the String value 

"Map"

.

This property has the attributes { [[Writable]]: 

false

, [[Enumerable]]: 

false

, [[Configurable]]: 

true

 }.

Map instances are ordinary objects that inherit properties from the Map prototype. Map instances also have a
[[MapData]] internal slot.

A Map Iterator is an object, that represents a specific iteration over some specific Map instance object. There is not a
named 

constructor

 for Map Iterator objects. Instead, map iterator objects are created by calling certain methods of

Map instance objects.

The abstract operation CreateMapIterator takes arguments 

map

 and 

kind

. This operation is used to create iterator

objects for Map methods that return such iterators. It performs the following steps when called:

1. 

Assert

kind

 is 

key+value

key

, or 

value

.

2.  Perform ? 

RequireInternalSlot

(

map

, [[MapData]]).

3.  Let 

closure

 be a new 

Abstract Closure

 with no parameters that captures 

map

 and 

kind

 and performs the

following steps when called:

a.  Let 

entries

 be the 

List

 that is 

map

.[[MapData]].

b.  Let 

index

 be 0.

c.  Let 

numEntries

 be the number of elements of 

entries

.

d.  Repeat, while 

index

 < 

numEntries

,

i.  Let 

e

 be the 

Record

 { [[Key]], [[Value]] } that is the value of 

entries

[

index

].

ii.  Set 

index

 to 

index

 + 1.

iii.  If 

e

.[[Key]] is not 

empty

, then

1.  If 

kind

 is 

key

, let 

result

 be 

e

.[[Key]].

2.  Else if 

kind

 is 

value

, let 

result

 be 

e

.[[Value]].

3.  Else,

a. 

Assert

kind

 is 

key+value

.

b.  Let 

result

 be ! 

CreateArrayFromList

(« 

e

.[[Key]], 

e

.[[Value]] »).

4.  Perform ? 

Yield

(

result

).

5.  NOTE: the number of elements in 

entries

 may have changed while execution of this

abstract operation was paused by 

Yield

.

6.  Set 

numEntries

 to the number of elements of 

entries

.

e.  Return 

undefined

.

4.  Return ! 

CreateIteratorFromClosure

(

closure

"%MapIteratorPrototype%"

%MapIteratorPrototype%

).

24.1.3.13  Map.prototype [ @@toStringTag ]

24.1.4  Properties of Map Instances

24.1.5  Map Iterator Objects

24.1.5.1  CreateMapIterator ( 

map

kind

 )

24.1.5.2  The %MapIteratorPrototype% Object

695

The 

%MapIteratorPrototype%

 object:

has properties that are inherited by all Map Iterator Objects.
is an 

ordinary object

.

has a [[Prototype]] internal slot whose value is 

%IteratorPrototype%

.

has the following properties:

1.  Return ? 

GeneratorResume

(

this

 value, 

empty

"%MapIteratorPrototype%"

).

The initial value of the 

@@toStringTag

 property is the String value 

"Map Iterator"

.

This property has the attributes { [[Writable]]: 

false

, [[Enumerable]]: 

false

, [[Configurable]]: 

true

 }.

Set objects are collections of ECMAScript language values. A distinct value may only occur once as an element of a
Set's collection. Distinct values are discriminated using the 

SameValueZero

 comparison algorithm.

Set objects must be implemented using either hash tables or other mechanisms that, on average, provide access times
that are sublinear on the number of elements in the collection. The data structures used in this Set objects specification
is only intended to describe the required observable semantics of Set objects. It is not intended to be a viable
implementation model.

The Set 

constructor

:

is 

%Set%

.

is the initial value of the 

"Set"

 property of the 

global object

.

creates and initializes a new Set object when called as a 

constructor

.

is not intended to be called as a function and will throw an exception when called in that manner.
is designed to be subclassable. It may be used as the value in an 

extends

extends

 clause of a class definition. Subclass

constructors that intend to inherit the specified Set behaviour must include a 

super

super

 call to the Set 

constructor

to create and initialize the subclass instance with the internal state necessary to support the 

Set.prototype

Set.prototype

built-in methods.

When the 

Set

Set

 function is called with optional argument 

iterable

, the following steps are taken:

1.  If NewTarget is 

undefined

, throw a 

TypeError

 exception.

2.  Let 

set

 be ? 

OrdinaryCreateFromConstructor

(NewTarget, 

"%Set.prototype%"

, « [[SetData]] »).

3.  Set 

set

.[[SetData]] to a new empty 

List

.

4.  If 

iterable

 is either 

undefined

 or 

null

, return 

set

.

5.  Let 

adder

 be ? 

Get

(

set

"add"

).

24.1.5.2.1  %MapIteratorPrototype%.next ( )

24.1.5.2.2  %MapIteratorPrototype% [ @@toStringTag ]

24.2  Set Objects

24.2.1  The Set Constructor

24.2.1.1  Set ( [ 

iterable

 ] )

696

6.  If 

IsCallable

(

adder

) is 

false

, throw a 

TypeError

 exception.

7.  Let 

iteratorRecord

 be ? 

GetIterator

(

iterable

).

8.  Repeat,

a.  Let 

next

 be ? 

IteratorStep

(

iteratorRecord

).

b.  If 

next

 is 

false

, return 

set

.

c.  Let 

nextValue

 be ? 

IteratorValue

(

next

).

d.  Let 

status

 be 

Call

(

adder

set

, « 

nextValue

 »).

e.  If 

status

 is an 

abrupt completion

, return ? 

IteratorClose

(

iteratorRecord

status

).

The Set 

constructor

:

has a [[Prototype]] internal slot whose value is 

%Function.prototype%

.

has the following properties:

The initial value of 

Set.prototype

Set.prototype

 is the 

Set prototype object

.

This property has the attributes { [[Writable]]: 

false

, [[Enumerable]]: 

false

, [[Configurable]]: 

false

 }.

Set[@@species]

Set[@@species]

 is an 

accessor property

 whose set accessor function is 

undefined

. Its get accessor function

performs the following steps:

1.  Return the 

this

 value.

The value of the 

"name"

 property of this function is 

"get [Symbol.species]"

.

NOTE

The 

Set prototype object

:

is 

%Set.prototype%

.

has a [[Prototype]] internal slot whose value is 

%Object.prototype%

.

is an 

ordinary object

.

does not have a [[SetData]] internal slot.

The following steps are taken:

1.  Let 

S

 be the 

this

 value.

Methods that create derived collection objects should call 

@@species

 to determine the 

constructor

to use to create the derived objects. Subclass 

constructor

 may over-ride 

@@species

 to change the

default 

constructor

 assignment.

24.2.2  Properties of the Set Constructor

24.2.2.1  Set.prototype

24.2.2.2  get Set [ @@species ]

24.2.3  Properties of the Set Prototype Object

24.2.3.1  Set.prototype.add ( 

value

 )

697

NOTE

candidate execution

 

execution

 has valid chosen reads if the following abstract operation returns 

true

.

1.  For each 

ReadSharedMemory

 or 

ReadModifyWriteSharedMemory

 event 

R

 of

SharedDataBlockEventSet

(

execution

), do

a.  Let 

chosenValueRecord

 be the element of 

execution

.[[ChosenValues]] whose [[Event]] field is 

R

.

b.  Let 

chosenValue

 be 

chosenValueRecord

.[[ChosenValue]].

c.  Let 

readValue

 be 

ValueOfReadEvent

(

execution

R

).

d.  Let 

chosenLen

 be the number of elements of 

chosenValue

.

e.  Let 

readLen

 be the number of elements of 

readValue

.

f.  If 

chosenLen

 

 

readLen

, then

i.  Return 

false

.

g.  If 

chosenValue

[

i

 

readValue

[

i

] for any 

integer

 value 

i

 in the range 0 through 

chosenLen

, exclusive, then

i.  Return 

false

.

2.  Return 

true

.

candidate execution

 

execution

 has coherent reads if the following abstract operation returns 

true

.

1.  For each 

ReadSharedMemory

 or 

ReadModifyWriteSharedMemory

 event 

R

 of

SharedDataBlockEventSet

(

execution

), do

a.  Let 

Ws

 be 

execution

.[[ReadsBytesFrom]](

R

).

b.  Let 

byteLocation

 be 

R

.[[ByteIndex]].

c.  For each element 

W

 of 

Ws

, do

i.  If (

R

W

) is in 

execution

.[[HappensBefore]], then

1.  Return 

false

.

ii.  If there is a 

WriteSharedMemory

 or 

ReadModifyWriteSharedMemory

 event 

V

 that has

byteLocation

 in its range such that the pairs (

W

V

) and (

V

R

) are in 

execution

.[[HappensBefore]],

then

1.  Return 

false

.

iii.  Set 

byteLocation

 to 

byteLocation

 + 1.

2.  Return 

true

.

candidate execution

 

execution

 has tear free reads if the following abstract operation returns 

true

.

1.  For each 

ReadSharedMemory

 or 

ReadModifyWriteSharedMemory

 event 

R

 of

SharedDataBlockEventSet

(

execution

), do

Because happens-before is a superset of 

agent-order

, candidate executions are consistent with the

single-thread evaluation semantics of ECMAScript.

29.7  Properties of Valid Executions

29.7.1  Valid Chosen Reads

29.7.2  Coherent Reads

29.7.3  Tear Free Reads

807

a.  If 

R

.[[NoTear]] is 

true

, then

i. 

Assert

: The remainder of dividing 

R

.[[ByteIndex]] by 

R

.[[ElementSize]] is 0.

ii.  For each event 

W

 such that (

R

W

) is in 

execution

.[[ReadsFrom]] and 

W

.[[NoTear]] is 

true

, do

1.  If 

R

 and 

W

 have equal ranges, and there is an event 

V

 such that 

V

 and 

W

 have equal

ranges, 

V

.[[NoTear]] is 

true

W

 is not 

V

, and (

R

V

) is in 

execution

.[[ReadsFrom]], then

a.  Return 

false

.

2.  Return 

true

.

NOTE

For a 

candidate execution

 

execution

, memory-order is a 

strict total order

 of all events in 

EventSet

(

execution

) that

satisfies the following.

For each pair (

E

D

) in 

execution

.[[HappensBefore]], (

E

D

) is in memory-order.

For each pair (

R

W

) in 

execution

.[[ReadsFrom]], there is no 

WriteSharedMemory

 or

ReadModifyWriteSharedMemory

 event 

V

 in 

SharedDataBlockEventSet

(

execution

) such that 

V

.[[Order]] is

SeqCst

, the pairs (

W

V

) and (

V

R

) are in memory-order, and any of the following conditions are true.

The pair (

W

R

) is in 

execution

.[[SynchronizesWith]], and 

V

 and 

R

 have equal ranges.

The pairs (

W

R

) and (

V

R

) are in 

execution

.[[HappensBefore]], 

W

.[[Order]] is 

SeqCst

, and 

W

 and 

V

 have

equal ranges.
The pairs (

W

R

) and (

W

V

) are in 

execution

.[[HappensBefore]], 

R

.[[Order]] is 

SeqCst

, and 

V

 and 

R

 have

equal ranges.

NOTE 1

For each 

WriteSharedMemory

 or 

ReadModifyWriteSharedMemory

 event 

W

 in

SharedDataBlockEventSet

(

execution

), if 

W

.[[Order]] is 

SeqCst

, then it is not the case that there is an infinite

number of 

ReadSharedMemory

 or 

ReadModifyWriteSharedMemory

 events in

SharedDataBlockEventSet

(

execution

) with equal range that is memory-order before 

W

.

NOTE 2

candidate execution

 has sequentially consistent atomics if a memory-order exists.

An event's [[NoTear]] field is 

true

 when that event was introduced via accessing an 

integer

TypedArray, and 

false

 when introduced via accessing a floating point TypedArray or DataView.

Intuitively, this requirement says when a memory range is accessed in an aligned fashion via an

integer

 TypedArray, a single write event on that range must "win" when in a data race with other

write events with equal ranges. More precisely, this requirement says an aligned read event
cannot read a value composed of bytes from multiple, different write events all with equal
ranges. It is possible, however, for an aligned read event to read from multiple write events with
overlapping ranges.

This clause additionally constrains 

SeqCst

 events on equal ranges.

This clause together with the forward progress guarantee on agents ensure the liveness
condition that 

SeqCst

 writes become visible to 

SeqCst

 reads with equal range in finite

time.

29.7.4  Sequentially Consistent Atomics

808

NOTE 3

candidate execution

 

execution

 is a valid execution (or simply an execution) if all of the following are true.

The 

host

 provides a 

host-synchronizes-with

 

Relation

 for 

execution

.[[HostSynchronizesWith]].

execution

.[[HappensBefore]] is a 

strict partial order

.

execution

 has valid chosen reads.

execution

 has coherent reads.

execution

 has tear free reads.

execution

 has sequentially consistent atomics.

All programs have at least one valid execution.

For an execution 

execution

, two events 

E

 and 

D

 in 

SharedDataBlockEventSet

(

execution

) are in a race if the following

abstract operation returns 

true

.

1.  If 

E

 is not 

D

, then

a.  If the pairs (

E

D

) and (

D

E

) are not in 

execution

.[[HappensBefore]], then

i.  If 

E

 and 

D

 are both 

WriteSharedMemory

 or 

ReadModifyWriteSharedMemory

 events and 

E

 and 

D

do not have disjoint ranges, then

1.  Return 

true

.

ii.  If either (

E

D

) or (

D

E

) is in 

execution

.[[ReadsFrom]], then

1.  Return 

true

.

2.  Return 

false

.

For an execution 

execution

, two events 

E

 and 

D

 in 

SharedDataBlockEventSet

(

execution

) are in a data race if the

following abstract operation returns 

true

.

1.  If 

E

 and 

D

 are in a race in 

execution

, then

a.  If 

E

.[[Order]] is not 

SeqCst

 or 

D

.[[Order]] is not 

SeqCst

, then

i.  Return 

true

.

b.  If 

E

 and 

D

 have overlapping ranges, then

i.  Return 

true

.

2.  Return 

false

.

An execution 

execution

 is data race free if there are no two events in 

SharedDataBlockEventSet

(

execution

) that are in a

While memory-order includes all events in 

EventSet

(

execution

), those that are not constrained by

happens-before

 or 

synchronizes-with

 are allowed to occur anywhere in the order.

29.7.5  Valid Executions

29.8  Races

29.9  Data Races

29.10  Data Race Freedom

809

 

 

 

 

 

 

 

Content      ..     34      35      36      37     ..