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

 

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

 

Search            copyright infringement  

 

 

 

 

 

 

 

 

 

 

 

Content      ..     51      52      53      54     ..

 

 

 

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

 

 

NOTE 1

NOTE 2

An 

Atom

 followed by a 

Quantifier

 is repeated the number of times specified by the 

Quantifier

. A 

Quantifier

 can be non-greedy, in which case the 

Atom

 pattern is repeated as few times as possible

while still matching the sequel, or it can be greedy, in which case the 

Atom

 pattern is repeated as

many times as possible while still matching the sequel. The 

Atom

 pattern is repeated rather than

the input character sequence that it matches, so different repetitions of the 

Atom

 can match

different input substrings.

If the 

Atom

 and the sequel of the regular expression all have choice points, the 

Atom

 is first

matched as many (or as few, if non-greedy) times as possible. All choices in the sequel are tried

before moving on to the next choice in the last repetition of 

Atom

. All choices in the last (n

th

)

repetition of 

Atom

 are tried before moving on to the next choice in the next-to-last (n - 1)

st

repetition of 

Atom

; at which point it may turn out that more or fewer repetitions of 

Atom

 are now

possible; these are exhausted (again, starting with either as few or as many as possible) before

moving on to the next choice in the (n - 1)

st

 repetition of 

Atom

 and so on.

Compare

which returns 

"abcde"

 with

which returns 

"abc"

.

Consider also

which, by the choice point ordering above, returns the array

and not any of:

The above ordering of choice points can be used to write a regular expression that calculates the
greatest common divisor of two numbers (represented in unary notation). The following example
calculates the gcd of 10 and 15:

which returns the gcd in unary notation 

"aaaaa"

.

/a[a-z]{

2

,

4

}/.exec(

"abcdefghi"

)

/a[a-z]{

2

,

4

}?/.exec(

"abcdefghi"

)

/(aa|aabaac|ba|b|c)*/.exec(

"aabaac"

)

[

"aaba"

"ba"

]

[

"aabaac"

"aabaac"

]

[

"aabaac"

"c"

]

"aaaaaaaaaa,aaaaaaaaaaaaaaa"

.replace(

/^(a+)\1*,\1+$/

"$1"

)

589

NOTE 3

NOTE 4

The production 

Assertion

 

::

 

^

 evaluates as follows:

1.  Return a new Matcher with parameters (

x

c

) that captures nothing and performs the following steps when

called:

a. 

Assert

x

 is a State.

b. 

Assert

c

 is a Continuation.

c.  Let 

e

 be 

x

's 

endIndex

.

d.  If 

e

 = 0, or if 

Multiline

 is 

true

 and the character 

Input

[

e

 - 1] is one of 

LineTerminator

, then

i.  Return 

c

(

x

).

e.  Return 

failure

.

NOTE

The production 

Assertion

 

::

 

$

 evaluates as follows:

Step 

4

 of the RepeatMatcher clears 

Atom

's captures each time 

Atom

 is repeated. We can see its

behaviour in the regular expression

which returns the array

and not

because each iteration of the outermost 

**

 clears all captured Strings contained in the quantified 

Atom

, which in this case includes capture Strings numbered 2, 3, 4, and 5.

/(z)((a+)?(b+)?(c))*/.exec(

"zaacbbbcac"

)

[

"zaacbbbcac"

"z"

"ac"

"a"

undefined

"c"

]

[

"zaacbbbcac"

"z"

"ac"

"a"

"bbb"

"c"

]

Step 

2.b

 of the RepeatMatcher states that once the minimum number of repetitions has been

satisfied, any more expansions of 

Atom

 that match the empty character sequence are not

considered for further repetitions. This prevents the regular expression engine from falling into
an infinite loop on patterns such as:

or the slightly more complicated:

which returns the array

/(a*)*/.exec(

"b"

)

/(a*)b\

1

+/.exec(

"baaaac"

)

[

"b"

""

]

Even when the 

yy

 flag is used with a pattern, 

^^

 always matches only at the beginning of 

Input

, or

(if 

Multiline

 is 

true

) at the beginning of a line.

22.2.2.6  Assertion

590

1.  Return a new Matcher with parameters (

x

c

) that captures nothing and performs the following steps when

called:

a. 

Assert

x

 is a State.

b. 

Assert

c

 is a Continuation.

c.  Let 

e

 be 

x

's 

endIndex

.

d.  If 

e

 = 

InputLength

, or if 

Multiline

 is 

true

 and the character 

Input

[

e

] is one of 

LineTerminator

, then

i.  Return 

c

(

x

).

e.  Return 

failure

.

The production 

Assertion

 

::

 

\

 

b

 evaluates as follows:

1.  Return a new Matcher with parameters (

x

c

) that captures nothing and performs the following steps when

called:

a. 

Assert

x

 is a State.

b. 

Assert

c

 is a Continuation.

c.  Let 

e

 be 

x

's 

endIndex

.

d.  Let 

a

 be ! 

IsWordChar

(

e

 - 1).

e.  Let 

b

 be ! 

IsWordChar

(

e

).

f.  If 

a

 is 

true

 and 

b

 is 

false

, or if 

a

 is 

false

 and 

b

 is 

true

, return 

c

(

x

).

g.  Return 

failure

.

The production 

Assertion

 

::

 

\

 

B

 evaluates as follows:

1.  Return a new Matcher with parameters (

x

c

) that captures nothing and performs the following steps when

called:

a. 

Assert

x

 is a State.

b. 

Assert

c

 is a Continuation.

c.  Let 

e

 be 

x

's 

endIndex

.

d.  Let 

a

 be ! 

IsWordChar

(

e

 - 1).

e.  Let 

b

 be ! 

IsWordChar

(

e

).

f.  If 

a

 is 

true

 and 

b

 is 

true

, or if 

a

 is 

false

 and 

b

 is 

false

, return 

c

(

x

).

g.  Return 

failure

.

The production 

Assertion

 

::

 

(

 

?

 

=

 

Disjunction

 

)

 evaluates as follows:

1.  Evaluate 

Disjunction

 with 1 as its 

direction

 argument to obtain a Matcher 

m

.

2.  Return a new Matcher with parameters (

x

c

) that captures 

m

 and performs the following steps when called:

a. 

Assert

x

 is a State.

b. 

Assert

c

 is a Continuation.

c.  Let 

d

 be a new Continuation with parameters (

y

) that captures nothing and performs the following steps

when called:

i. 

Assert

y

 is a State.

ii.  Return 

y

.

d.  Let 

r

 be 

m

(

x

d

).

e.  If 

r

 is 

failure

, return 

failure

.

f.  Let 

y

 be 

r

's State.

g.  Let 

cap

 be 

y

's 

captures

 

List

.

h.  Let 

xe

 be 

x

's 

endIndex

.

i.  Let 

z

 be the State (

xe

cap

).

j.  Return 

c

(

z

).

591

The production 

Assertion

 

::

 

(

 

?

 

!

 

Disjunction

 

)

 evaluates as follows:

1.  Evaluate 

Disjunction

 with 1 as its 

direction

 argument to obtain a Matcher 

m

.

2.  Return a new Matcher with parameters (

x

c

) that captures 

m

 and performs the following steps when called:

a. 

Assert

x

 is a State.

b. 

Assert

c

 is a Continuation.

c.  Let 

d

 be a new Continuation with parameters (

y

) that captures nothing and performs the following steps

when called:

i. 

Assert

y

 is a State.

ii.  Return 

y

.

d.  Let 

r

 be 

m

(

x

d

).

e.  If 

r

 is not 

failure

, return 

failure

.

f.  Return 

c

(

x

).

The production 

Assertion

 

::

 

(

 

?

 

<=

 

Disjunction

 

)

 evaluates as follows:

1.  Evaluate 

Disjunction

 with -1 as its 

direction

 argument to obtain a Matcher 

m

.

2.  Return a new Matcher with parameters (

x

c

) that captures 

m

 and performs the following steps when called:

a. 

Assert

x

 is a State.

b. 

Assert

c

 is a Continuation.

c.  Let 

d

 be a new Continuation with parameters (

y

) that captures nothing and performs the following steps

when called:

i. 

Assert

y

 is a State.

ii.  Return 

y

.

d.  Let 

r

 be 

m

(

x

d

).

e.  If 

r

 is 

failure

, return 

failure

.

f.  Let 

y

 be 

r

's State.

g.  Let 

cap

 be 

y

's 

captures

 

List

.

h.  Let 

xe

 be 

x

's 

endIndex

.

i.  Let 

z

 be the State (

xe

cap

).

j.  Return 

c

(

z

).

The production 

Assertion

 

::

 

(

 

?

 

<!

 

Disjunction

 

)

 evaluates as follows:

1.  Evaluate 

Disjunction

 with -1 as its 

direction

 argument to obtain a Matcher 

m

.

2.  Return a new Matcher with parameters (

x

c

) that captures 

m

 and performs the following steps when called:

a. 

Assert

x

 is a State.

b. 

Assert

c

 is a Continuation.

c.  Let 

d

 be a new Continuation with parameters (

y

) that captures nothing and performs the following steps

when called:

i. 

Assert

y

 is a State.

ii.  Return 

y

.

d.  Let 

r

 be 

m

(

x

d

).

e.  If 

r

 is not 

failure

, return 

failure

.

f.  Return 

c

(

x

).

The abstract operation IsWordChar takes argument 

e

 (an 

integer

). It performs the following steps when called:

22.2.2.6.1  IsWordChar ( 

e

 )

592

1.  If 

e

 = -1 or 

e

 is 

InputLength

, return 

false

.

2.  Let 

c

 be the character 

Input

[

e

].

3.  If 

c

 is in 

WordCharacters

, return 

true

.

4.  Return 

false

.

The production 

Quantifier

 

::

 

QuantifierPrefix

 evaluates as follows:

1.  Evaluate 

QuantifierPrefix

 to obtain the two results: an 

integer

 

min

 and an 

integer

 (or +

max

.

2.  Return the three results 

min

max

, and 

true

.

The production 

Quantifier

 

::

 

QuantifierPrefix

 

?

 evaluates as follows:

1.  Evaluate 

QuantifierPrefix

 to obtain the two results: an 

integer

 

min

 and an 

integer

 (or +

max

.

2.  Return the three results 

min

max

, and 

false

.

The production 

QuantifierPrefix

 

::

 

*

 evaluates as follows:

1.  Return the two results 0 and +

.

The production 

QuantifierPrefix

 

::

 

+

 evaluates as follows:

1.  Return the two results 1 and +

.

The production 

QuantifierPrefix

 

::

 

?

 evaluates as follows:

1.  Return the two results 0 and 1.

The production 

QuantifierPrefix

 

::

 

{

 

DecimalDigits

 

}

 evaluates as follows:

1.  Let 

i

 be the MV of 

DecimalDigits

 (see 

12.8.3

).

2.  Return the two results 

i

 and 

i

.

The production 

QuantifierPrefix

 

::

 

{

 

DecimalDigits

 

,

 

}

 evaluates as follows:

1.  Let 

i

 be the MV of 

DecimalDigits

.

2.  Return the two results 

i

 and +

.

The production 

QuantifierPrefix

 

::

 

{

 

DecimalDigits

 

,

 

DecimalDigits

 

}

 evaluates as follows:

1.  Let 

i

 be the MV of the first 

DecimalDigits

.

2.  Let 

j

 be the MV of the second 

DecimalDigits

.

3.  Return the two results 

i

 and 

j

.

With parameter 

direction

.

The production 

Atom

 

::

 

PatternCharacter

 evaluates as follows:

1.  Let 

ch

 be the character matched by 

PatternCharacter

.

2.  Let 

A

 be a one-element CharSet containing the character 

ch

.

3.  Return ! 

CharacterSetMatcher

(

A

false

direction

).

22.2.2.7  Quantifier

22.2.2.8  Atom

593

The production 

Atom

 

::

 

.

 evaluates as follows:

1.  Let 

A

 be the CharSet of all characters.

2.  If 

DotAll

 is not 

true

, then

a.  Remove from 

A

 all characters corresponding to a code point on the right-hand side of the 

LineTerminator

production.

3.  Return ! 

CharacterSetMatcher

(

A

false

direction

).

The production 

Atom

 

::

 

\

 

AtomEscape

 evaluates as follows:

1.  Return the Matcher that is the result of evaluating 

AtomEscape

 with argument 

direction

.

The production 

Atom

 

::

 

CharacterClass

 evaluates as follows:

1.  Evaluate 

CharacterClass

 to obtain a CharSet 

A

 and a Boolean 

invert

.

2.  Return ! 

CharacterSetMatcher

(

A

invert

direction

).

The production 

Atom

 

::

 

(

 

GroupSpecifier

 

Disjunction

 

)

 evaluates as follows:

1.  Evaluate 

Disjunction

 with argument 

direction

 to obtain a Matcher 

m

.

2.  Let 

parenIndex

 be the number of left-capturing parentheses in the entire regular expression that occur to the left

of this 

Atom

. This is the total number of 

Atom

 

::

 

(

 

GroupSpecifier

 

Disjunction

 

)

 Parse Nodes prior to or

enclosing this 

Atom

.

3.  Return a new Matcher with parameters (

x

c

) that captures 

direction

m

, and 

parenIndex

 and performs the

following steps when called:

a. 

Assert

x

 is a State.

b. 

Assert

c

 is a Continuation.

c.  Let 

d

 be a new Continuation with parameters (

y

) that captures 

x

c

direction

, and 

parenIndex

 and

performs the following steps when called:

i. 

Assert

y

 is a State.

ii.  Let 

cap

 be a copy of 

y

's 

captures

 

List

.

iii.  Let 

xe

 be 

x

's 

endIndex

.

iv.  Let 

ye

 be 

y

's 

endIndex

.

v.  If 

direction

 = 1, then

1. 

Assert

xe

 

 

ye

.

2.  Let 

s

 be a 

List

 whose elements are the characters of 

Input

 at indices 

xe

 (inclusive) through

ye

 (exclusive).

vi.  Else,

1. 

Assert

direction

 is -1.

2. 

Assert

ye

 

 

xe

.

3.  Let 

s

 be a 

List

 whose elements are the characters of 

Input

 at indices 

ye

 (inclusive) through

xe

 (exclusive).

vii.  Set 

cap

[

parenIndex

 + 1] to 

s

.

viii.  Let 

z

 be the State (

ye

cap

).

ix.  Return 

c

(

z

).

d.  Return 

m

(

x

d

).

The production 

Atom

 

::

 

(

 

?

 

:

 

Disjunction

 

)

 evaluates as follows:

1.  Return the Matcher that is the result of evaluating 

Disjunction

 with argument 

direction

.

594

The abstract operation CharacterSetMatcher takes arguments 

A

 (a CharSet), 

invert

 (a Boolean), and 

direction

 (1 or -1). It

performs the following steps when called:

1.  Return a new Matcher with parameters (

x

c

) that captures 

A

invert

, and 

direction

 and performs the following

steps when called:

a. 

Assert

x

 is a State.

b. 

Assert

c

 is a Continuation.

c.  Let 

e

 be 

x

's 

endIndex

.

d.  Let 

f

 be 

e

 + 

direction

.

e.  If 

f

 < 0 or 

f

 > 

InputLength

, return 

failure

.

f.  Let 

index

 be 

min

(

e

f

).

g.  Let 

ch

 be the character 

Input

[

index

].

h.  Let 

cc

 be 

Canonicalize

(

ch

).

i.  If there exists a member 

a

 of 

A

 such that 

Canonicalize

(

a

) is 

cc

, let 

found

 be 

true

. Otherwise, let 

found

 be

false

.

j.  If 

invert

 is 

false

 and 

found

 is 

false

, return 

failure

.

k.  If 

invert

 is 

true

 and 

found

 is 

true

, return 

failure

.

l.  Let 

cap

 be 

x

's 

captures

 

List

.

m.  Let 

y

 be the State (

f

cap

).

n.  Return 

c

(

y

).

The abstract operation Canonicalize takes argument 

ch

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

1.  If 

Unicode

 is 

true

 and 

IgnoreCase

 is 

true

, then

a.  If the file CaseFolding.txt of the Unicode Character Database provides a simple or common case folding

mapping for 

ch

, return the result of applying that mapping to 

ch

.

b.  Return 

ch

.

2.  If 

IgnoreCase

 is 

false

, return 

ch

.

3. 

Assert

ch

 is a UTF-16 code unit.

4.  Let 

cp

 be the code point whose numeric value is that of 

ch

.

5.  Let 

u

 be the result of toUppercase(« 

cp

 »), according to the Unicode Default Case Conversion algorithm.

6.  Let 

uStr

 be ! 

CodePointsToString

(

u

).

7.  If 

uStr

 does not consist of a single code unit, return 

ch

.

8.  Let 

cu

 be 

uStr

's single code unit element.

9.  If the numeric value of 

ch

 

 128 and the numeric value of 

cu

 < 128, return 

ch

.

10.  Return 

cu

.

NOTE 1

Parentheses of the form 

((

 

Disjunction

 

))

 serve both to group the components of the 

Disjunction

pattern together and to save the result of the match. The result can be used either in a
backreference (

\\

 followed by a non-zero decimal number), referenced in a replace String, or

returned as part of an array from the regular expression matching 

Abstract Closure

. To inhibit the

capturing behaviour of parentheses, use the form 

(?:

(?:

 

Disjunction

 

))

 instead.

22.2.2.8.1  CharacterSetMatcher ( 

A

invert

direction

 )

22.2.2.8.2  Canonicalize ( 

ch

 )

595

NOTE 2

NOTE 3

The form 

(?=

(?=

 

Disjunction

 

))

 specifies a zero-width positive lookahead. In order for it to succeed,

the pattern inside 

Disjunction

 must match at the current position, but the current position is not

advanced before matching the sequel. If 

Disjunction

 can match at the current position in several

ways, only the first one is tried. Unlike other regular expression operators, there is no
backtracking into a 

(?=

(?=

 form (this unusual behaviour is inherited from Perl). This only matters

when the 

Disjunction

 contains capturing parentheses and the sequel of the pattern contains

backreferences to those captures.

For example,

matches the empty String immediately after the first 

bb

 and therefore returns the array:

To illustrate the lack of backtracking into the lookahead, consider:

This expression returns

and not:

/(?=(a+))/.exec(

"baaabac"

)

[

""

"aaa"

]

/(?=(a+))a*b\

1

/.exec(

"baaabac"

)

[

"aba"

"a"

]

[

"aaaba"

"a"

]

The form 

(?!

(?!

 

Disjunction

 

))

 specifies a zero-width negative lookahead. In order for it to succeed,

the pattern inside 

Disjunction

 must fail to match at the current position. The current position is

not advanced before matching the sequel. 

Disjunction

 can contain capturing parentheses, but

backreferences to them only make sense from within 

Disjunction

 itself. Backreferences to these

capturing parentheses from elsewhere in the pattern always return 

undefined

 because the

negative lookahead must fail for the pattern to succeed. For example,

looks for an 

aa

 not immediately followed by some positive number n of 

aa

's, a 

bb

, another n 

aa

's

(specified by the first 

\2

\2

) and a 

cc

. The second 

\2

\2

 is outside the negative lookahead, so it

matches against 

undefined

 and therefore always succeeds. The whole expression returns the

array:

/(.*?)a(?!(a+)b\2c)\

2

(.*)/.exec(

"baaabaac"

)

[

"baaabaac"

"ba"

undefined

"abaac"

]

596

NOTE 4

The abstract operation UnicodeMatchProperty takes argument 

p

 (a 

List

 of Unicode code points). It performs the

following steps when called:

1. 

Assert

p

 is a 

List

 of Unicode code points that is identical to a 

List

 of Unicode code points that is a Unicode

property name or property alias listed in the “Property name and aliases” column of 

Table 56

 or 

Table 57

.

2.  Let 

c

 be the canonical property name of 

p

 as given in the “Canonical property name” column of the

corresponding row.

3.  Return the 

List

 of Unicode code points of 

c

.

Implementations must support the Unicode property names and aliases listed in 

Table 56

 and 

Table 57

. To ensure

interoperability, implementations must not support any other property names or aliases.

NOTE 1

NOTE 2

Table 56: Non-binary Unicode property aliases and their canonical property names

Property name

 and aliases

Canonical 

property name

General_Category

General_Category

General_Category

General_Category

gc

gc

Script

Script

Script

Script

sc

sc

Script_Extensions

Script_Extensions

Script_Extensions

Script_Extensions

scx

scx

Table 57: Binary Unicode property aliases and their canonical property names

Property name

 and aliases

Canonical 

property name

ASCII

ASCII

ASCII

ASCII

ASCII_Hex_Digit

ASCII_Hex_Digit

ASCII_Hex_Digit

ASCII_Hex_Digit

AHex

AHex

Alphabetic

Alphabetic

Alphabetic

Alphabetic

In case-insignificant matches when 

Unicode

 is 

true

, all characters are implicitly case-folded using

the simple mapping provided by the Unicode standard immediately before they are compared.
The simple mapping always maps to a single code point, so it does not map, for example, 

ßß

(U+00DF) to 

SS

SS

. It may however map a code point outside the Basic Latin range to a character

within, for example, 

ſſ

 (U+017F) to 

ss

. Such characters are not mapped if 

Unicode

 is 

false

. This

prevents Unicode code points such as U+017F and U+212A from matching regular expressions
such as 

/[a-z]/i

/[a-z]/i

, but they will match 

/[a-z]/ui

/[a-z]/ui

.

For example, 

Script_Extensions

Script_Extensions

 (

property name

) and 

scx

scx

 (property alias) are valid, but

script_extensions

script_extensions

 or 

Scx

Scx

 aren't.

The listed properties form a superset of what 

UTS18 RL1.2

 requires.

22.2.2.8.3  UnicodeMatchProperty ( 

p

 )

597

Alpha

Alpha

Any

Any

Any

Any

Assigned

Assigned

Assigned

Assigned

Bidi_Control

Bidi_Control

Bidi_Control

Bidi_Control

Bidi_C

Bidi_C

Bidi_Mirrored

Bidi_Mirrored

Bidi_Mirrored

Bidi_Mirrored

Bidi_M

Bidi_M

Case_Ignorable

Case_Ignorable

Case_Ignorable

Case_Ignorable

CI

CI

Cased

Cased

Cased

Cased

Changes_When_Casefolded

Changes_When_Casefolded

Changes_When_Casefolded

Changes_When_Casefolded

CWCF

CWCF

Changes_When_Casemapped

Changes_When_Casemapped

Changes_When_Casemapped

Changes_When_Casemapped

CWCM

CWCM

Changes_When_Lowercased

Changes_When_Lowercased

Changes_When_Lowercased

Changes_When_Lowercased

CWL

CWL

Changes_When_NFKC_Casefolded

Changes_When_NFKC_Casefolded

Changes_When_NFKC_Casefolded

Changes_When_NFKC_Casefolded

CWKCF

CWKCF

Changes_When_Titlecased

Changes_When_Titlecased

Changes_When_Titlecased

Changes_When_Titlecased

CWT

CWT

Changes_When_Uppercased

Changes_When_Uppercased

Changes_When_Uppercased

Changes_When_Uppercased

CWU

CWU

Dash

Dash

Dash

Dash

Default_Ignorable_Code_Point

Default_Ignorable_Code_Point

Default_Ignorable_Code_Point

Default_Ignorable_Code_Point

DI

DI

Deprecated

Deprecated

Deprecated

Deprecated

Dep

Dep

Diacritic

Diacritic

Diacritic

Diacritic

Dia

Dia

Emoji

Emoji

Emoji

Emoji

Emoji_Component

Emoji_Component

Emoji_Component

Emoji_Component

EComp

EComp

Emoji_Modifier

Emoji_Modifier

Emoji_Modifier

Emoji_Modifier

EMod

EMod

Emoji_Modifier_Base

Emoji_Modifier_Base

Emoji_Modifier_Base

Emoji_Modifier_Base

EBase

EBase

598

2.  Perform ? 

RequireInternalSlot

(

S

, [[SetData]]).

3.  Let 

entries

 be the 

List

 that is 

S

.[[SetData]].

4.  For each element 

e

 of 

entries

, do

a.  If 

e

 is not 

empty

 and 

SameValueZero

(

e

value

) is 

true

, then

i.  Return 

S

.

5.  If 

value

 is 

-0

𝔽

, set 

value

 to 

+0

𝔽

.

6.  Append 

value

 as the last element of 

entries

.

7.  Return 

S

.

The following steps are taken:

1.  Let 

S

 be the 

this

 value.

2.  Perform ? 

RequireInternalSlot

(

S

, [[SetData]]).

3.  Let 

entries

 be the 

List

 that is 

S

.[[SetData]].

4.  For each element 

e

 of 

entries

, do

a.  Replace the element of 

entries

 whose value is 

e

 with an element whose value is 

empty

.

5.  Return 

undefined

.

NOTE

The initial value of 

Set.prototype.constructor

Set.prototype.constructor

 is 

%Set%

.

The following steps are taken:

1.  Let 

S

 be the 

this

 value.

2.  Perform ? 

RequireInternalSlot

(

S

, [[SetData]]).

3.  Let 

entries

 be the 

List

 that is 

S

.[[SetData]].

4.  For each element 

e

 of 

entries

, do

a.  If 

e

 is not 

empty

 and 

SameValueZero

(

e

value

) is 

true

, then

i.  Replace the element of 

entries

 whose value is 

e

 with an element whose value is 

empty

.

ii.  Return 

true

.

5.  Return 

false

.

NOTE

The following steps are taken:

The existing [[SetData]] 

List

 is preserved because there may be existing Set 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.2.3.2  Set.prototype.clear ( )

24.2.3.3  Set.prototype.constructor

24.2.3.4  Set.prototype.delete ( 

value

 )

24.2.3.5  Set.prototype.entries ( )

698

1.  Let 

S

 be the 

this

 value.

2.  Return ? 

CreateSetIterator

(

S

key+value

).

NOTE

When the 

forEach

forEach

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

1.  Let 

S

 be the 

this

 value.

2.  Perform ? 

RequireInternalSlot

(

S

, [[SetData]]).

3.  If 

IsCallable

(

callbackfn

) is 

false

, throw a 

TypeError

 exception.

4.  Let 

entries

 be the 

List

 that is 

S

.[[SetData]].

5.  For each element 

e

 of 

entries

, do

a.  If 

e

 is not 

empty

, then

i.  Perform ? 

Call

(

callbackfn

thisArg

, « 

e

e

S

 »).

6.  Return 

undefined

.

NOTE

The following steps are taken:

1.  Let 

S

 be the 

this

 value.

For iteration purposes, a Set appears similar to a Map where each entry has the same value for its
key and value.

callbackfn

 should be a function that accepts three arguments. 

forEach

forEach

 calls 

callbackfn

 once for

each value present in the set object, in value insertion order. 

callbackfn

 is called only for values of

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

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 first two arguments are a value contained in the Set.

The same value is passed for both arguments. The Set object being traversed is passed as the
third argument.

The 

callbackfn

 is called with three arguments to be consistent with the call back functions used by

forEach

forEach

 methods for Map and Array. For Sets, each item value is considered to be both the key

and the value.

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 value is normally visited only once. However, a value will be revisited if it is deleted after it
has been visited and then re-added before the 

forEach

forEach

 call completes. Values that are deleted

after the call to 

forEach

forEach

 begins and before being visited are not visited unless the value is

added again before the 

forEach

forEach

 call completes. New values added after the call to 

forEach

forEach

begins are visited.

24.2.3.6  Set.prototype.forEach ( 

callbackfn

 [ , 

thisArg

 ] )

24.2.3.7  Set.prototype.has ( 

value

 )

699

2.  Perform ? 

RequireInternalSlot

(

S

, [[SetData]]).

3.  Let 

entries

 be the 

List

 that is 

S

.[[SetData]].

4.  For each element 

e

 of 

entries

, do

a.  If 

e

 is not 

empty

 and 

SameValueZero

(

e

value

) is 

true

, return 

true

.

5.  Return 

false

.

The initial value of the 

"keys"

 property is the same 

function object

 as the initial value of the 

"values"

 property.

NOTE

Set.prototype.size

Set.prototype.size

 is an 

accessor property

 whose set accessor function is 

undefined

. Its get accessor function

performs the following steps:

1.  Let 

S

 be the 

this

 value.

2.  Perform ? 

RequireInternalSlot

(

S

, [[SetData]]).

3.  Let 

entries

 be the 

List

 that is 

S

.[[SetData]].

4.  Let 

count

 be 0.

5.  For each element 

e

 of 

entries

, do

a.  If 

e

 is not 

empty

, set 

count

 to 

count

 + 1.

6.  Return 

count

).

The following steps are taken:

1.  Let 

S

 be the 

this

 value.

2.  Return ? 

CreateSetIterator

(

S

value

).

The initial value of the 

@@iterator

 property is the same 

function object

 as the initial value of the 

"values"

 property.

The initial value of the 

@@toStringTag

 property is the String value 

"Set"

.

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

false

, [[Enumerable]]: 

false

, [[Configurable]]: 

true

 }.

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

For iteration purposes, a Set appears similar to a Map where each entry has the same value for its
key and value.

24.2.3.8  Set.prototype.keys ( )

24.2.3.9  get Set.prototype.size

24.2.3.10  Set.prototype.values ( )

24.2.3.11  Set.prototype [ @@iterator ] ( )

24.2.3.12  Set.prototype [ @@toStringTag ]

24.2.4  Properties of Set Instances

700

A Set Iterator is an 

ordinary object

, with the structure defined below, that represents a specific iteration over some

specific Set instance object. There is not a named 

constructor

 for Set Iterator objects. Instead, set iterator objects are

created by calling certain methods of Set instance objects.

The abstract operation CreateSetIterator takes arguments 

set

 and 

kind

. This operation is used to create iterator objects

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

1. 

Assert

kind

 is 

key+value

 or 

value

.

2.  Perform ? 

RequireInternalSlot

(

set

, [[SetData]]).

3.  Let 

closure

 be a new 

Abstract Closure

 with no parameters that captures 

set

 and 

kind

 and performs the following

steps when called:

a.  Let 

index

 be 0.

b.  Let 

entries

 be the 

List

 that is 

set

.[[SetData]].

c.  Let 

numEntries

 be the number of elements of 

entries

.

d.  Repeat, while 

index

 < 

numEntries

,

i.  Let 

e

 be 

entries

[

index

].

ii.  Set 

index

 to 

index

 + 1.

iii.  If 

e

 is not 

empty

, then

1.  If 

kind

 is 

key+value

, then

a.  Perform ? 

Yield

(! 

CreateArrayFromList

(« 

e

e

 »)).

2.  Else,

a. 

Assert

kind

 is 

value

.

b.  Perform ? 

Yield

(

e

).

3.  NOTE: the number of elements in 

entries

 may have changed while execution of this

abstract operation was paused by 

Yield

.

4.  Set 

numEntries

 to the number of elements of 

entries

.

e.  Return 

undefined

.

4.  Return ! 

CreateIteratorFromClosure

(

closure

"%SetIteratorPrototype%"

%SetIteratorPrototype%

).

The 

%SetIteratorPrototype%

 object:

has properties that are inherited by all Set 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

"%SetIteratorPrototype%"

).

The initial value of the 

@@toStringTag

 property is the String value 

"Set Iterator"

.

24.2.5  Set Iterator Objects

24.2.5.1  CreateSetIterator ( 

set

kind

 )

24.2.5.2  The %SetIteratorPrototype% Object

24.2.5.2.1  %SetIteratorPrototype%.next ( )

24.2.5.2.2  %SetIteratorPrototype% [ @@toStringTag ]

701

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

false

, [[Enumerable]]: 

false

, [[Configurable]]: 

true

 }.

WeakMap objects are collections of key/value pairs where the keys are objects and values may be arbitrary
ECMAScript language values. A WeakMap may be queried to see if it contains a key/value pair with a specific key,
but no mechanism is provided for enumerating the objects it holds as keys. In certain conditions, objects which are not

live

 are removed as WeakMap keys, as described in 

9.9.3

.

An implementation may impose an arbitrarily determined latency between the time a key/value pair of a WeakMap
becomes inaccessible and the time when the key/value pair is removed from the WeakMap. If this latency was
observable to ECMAScript program, it would be a source of indeterminacy that could impact program execution. For
that reason, an ECMAScript implementation must not provide any means to observe a key of a WeakMap that does
not require the observer to present the observed key.

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

NOTE

The WeakMap 

constructor

:

is 

%WeakMap%

.

is the initial value of the 

"WeakMap"

 property of the 

global object

.

creates and initializes a new WeakMap 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

WeakMap and WeakSets are intended to provide mechanisms for dynamically associating state
with an object in a manner that does not “leak” memory resources if, in the absence of the
WeakMap or WeakSet, the object otherwise became inaccessible and subject to resource
reclamation by the implementation's garbage collection mechanisms. This characteristic can be
achieved by using an inverted per-object mapping of weak map instances to keys. Alternatively
each weak map may internally store its key to value mappings but this approach requires
coordination between the WeakMap or WeakSet implementation and the garbage collector. The
following references describe mechanism that may be useful to implementations of WeakMap
and WeakSets:

Barry Hayes. 1997. Ephemerons: a new finalization mechanism. In 

Proceedings of the 12th ACM

SIGPLAN conference on Object-oriented programming, systems, languages, and applications (OOPSLA
'97)

, A. Michael Berman (Ed.). ACM, New York, NY, USA, 176-183,

http://doi.acm.org/10.1145/263698.263733

.

Alexandra Barros, Roberto Ierusalimschy, Eliminating Cycles in Weak Tables. Journal of
Universal Computer Science - J.UCS, vol. 14, no. 21, pp. 3481-3497, 2008,

http://www.jucs.org/jucs_14_21/eliminating_cycles_in_weak

24.3  WeakMap Objects

24.3.1  The WeakMap Constructor

702

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

super

super

 call to the

WeakMap 

constructor

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

the 

WeakMap.prototype

WeakMap.prototype

 built-in methods.

When the 

WeakMap

WeakMap

 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, 

"%WeakMap.prototype%"

, « [[WeakMapData]] »).

3.  Set 

map

.[[WeakMapData]] 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 WeakMap 

constructor

:

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

%Function.prototype%

.

has the following properties:

The initial value of 

WeakMap.prototype

WeakMap.prototype

 is the 

WeakMap prototype object

.

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

false

, [[Enumerable]]: 

false

, [[Configurable]]: 

false

 }.

The 

WeakMap prototype object

:

is 

%WeakMap.prototype%

.

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

%Object.prototype%

.

is an 

ordinary object

.

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

The initial value of 

WeakMap.prototype.constructor

WeakMap.prototype.constructor

 is 

%WeakMap%

.

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 WeakMap key and whose second element is the value to
associate with that key.

24.3.1.1  WeakMap ( [ 

iterable

 ] )

24.3.2  Properties of the WeakMap Constructor

24.3.2.1  WeakMap.prototype

24.3.3  Properties of the WeakMap Prototype Object

24.3.3.1  WeakMap.prototype.constructor

24.3.3.2  WeakMap.prototype.delete ( 

key

 )

703

 

 

 

 

 

 

 

Content      ..     51      52      53      54     ..