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

 

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

 

Search            copyright infringement  

 

 

 

 

 

 

 

 

 

 

 

Content      ..     12      13      14      15     ..

 

 

 

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

 

 

3.  If 

V

 is not a 

Reference Record

, throw a 

ReferenceError

 exception.

4.  If 

IsUnresolvableReference

(

V

) is 

true

, then

a.  If 

V

.[[Strict]] is 

true

, throw a 

ReferenceError

 exception.

b.  Let 

globalObj

 be 

GetGlobalObject

().

c.  Return ? 

Set

(

globalObj

V

.[[ReferencedName]], 

W

false

).

5.  If 

IsPropertyReference

(

V

) is 

true

, then

a.  Let 

baseObj

 be ! 

ToObject

(

V

.[[Base]]).

b.  Let 

succeeded

 be ? 

baseObj

.[[Set]](

V

.[[ReferencedName]], 

W

GetThisValue

(

V

)).

c.  If 

succeeded

 is 

false

 and 

V

.[[Strict]] is 

true

, throw a 

TypeError

 exception.

d.  Return.

6.  Else,

a.  Let 

base

 be 

V

.[[Base]].

b. 

Assert

base

 is an 

Environment Record

.

c.  Return ? 

base

.SetMutableBinding(

V

.[[ReferencedName]], 

W

V

.[[Strict]]) (see 

9.1

).

NOTE

The abstract operation GetThisValue takes argument 

V

. It performs the following steps when called:

1. 

Assert

IsPropertyReference

(

V

) is 

true

.

2.  If 

IsSuperReference

(

V

) is 

true

, return 

V

.[[ThisValue]]; otherwise return 

V

.[[Base]].

The abstract operation InitializeReferencedBinding takes arguments 

V

 and 

W

. It performs the following steps when

called:

1. 

ReturnIfAbrupt

(

V

).

2. 

ReturnIfAbrupt

(

W

).

3. 

Assert

V

 is a 

Reference Record

.

4. 

Assert

IsUnresolvableReference

(

V

) is 

false

.

5.  Let 

base

 be 

V

.[[Base]].

6. 

Assert

base

 is an 

Environment Record

.

7.  Return 

base

.InitializeBinding(

V

.[[ReferencedName]], 

W

).

The 

Property Descriptor

 type is used to explain the manipulation and reification of Object property attributes. Values of

the Property Descriptor type are Records. Each field's name is an attribute name and its value is a corresponding
attribute value as specified in 

6.1.7.1

. In addition, any field may be present or absent. The schema name used within

this specification to tag literal descriptions of Property Descriptor records is “PropertyDescriptor”.

Property Descriptor values may be further classified as data Property Descriptors and accessor Property Descriptors
based upon the existence or use of certain fields. A data Property Descriptor is one that includes any fields named

The object that may be created in step 

5.a

 is not accessible outside of the above abstract operation

and the 

ordinary object

 [[Set]] internal method. An implementation might choose to avoid the

actual creation of that object.

6.2.4.6  GetThisValue ( 

V

 )

6.2.4.7  InitializeReferencedBinding ( 

V

W

 )

6.2.5  The Property Descriptor Specification Type

106

either [[Value]] or [[Writable]]. An accessor Property Descriptor is one that includes any fields named either [[Get]] or
[[Set]]. Any Property Descriptor may have fields named [[Enumerable]] and [[Configurable]]. A Property Descriptor
value may not be both a data Property Descriptor and an accessor Property Descriptor; however, it may be neither. A
generic Property Descriptor is a Property Descriptor value that is neither a data Property Descriptor nor an accessor
Property Descriptor. A fully populated Property Descriptor is one that is either an accessor Property Descriptor or a
data Property Descriptor and that has all of the fields that correspond to the property attributes defined in either 

Table

3

 or 

Table 4

.

The following 

abstract operations

 are used in this specification to operate upon Property Descriptor values:

The abstract operation IsAccessorDescriptor takes argument 

Desc

 (a 

Property Descriptor

 or 

undefined

). It performs

the following steps when called:

1.  If 

Desc

 is 

undefined

, return 

false

.

2.  If both 

Desc

.[[Get]] and 

Desc

.[[Set]] are absent, return 

false

.

3.  Return 

true

.

The abstract operation IsDataDescriptor takes argument 

Desc

 (a 

Property Descriptor

 or 

undefined

). It performs the

following steps when called:

1.  If 

Desc

 is 

undefined

, return 

false

.

2.  If both 

Desc

.[[Value]] and 

Desc

.[[Writable]] are absent, return 

false

.

3.  Return 

true

.

The abstract operation IsGenericDescriptor takes argument 

Desc

 (a 

Property Descriptor

 or 

undefined

). It performs the

following steps when called:

1.  If 

Desc

 is 

undefined

, return 

false

.

2.  If 

IsAccessorDescriptor

(

Desc

) and 

IsDataDescriptor

(

Desc

) are both 

false

, return 

true

.

3.  Return 

false

.

The abstract operation FromPropertyDescriptor takes argument 

Desc

 (a 

Property Descriptor

 or 

undefined

). It

performs the following steps when called:

1.  If 

Desc

 is 

undefined

, return 

undefined

.

2.  Let 

obj

 be ! 

OrdinaryObjectCreate

(

%Object.prototype%

).

3. 

Assert

obj

 is an extensible 

ordinary object

 with no own properties.

4.  If 

Desc

 has a [[Value]] field, then

a.  Perform ! 

CreateDataPropertyOrThrow

(

obj

"value"

Desc

.[[Value]]).

5.  If 

Desc

 has a [[Writable]] field, then

a.  Perform ! 

CreateDataPropertyOrThrow

(

obj

"writable"

Desc

.[[Writable]]).

6.  If 

Desc

 has a [[Get]] field, then

6.2.5.1  IsAccessorDescriptor ( 

Desc

 )

6.2.5.2  IsDataDescriptor ( 

Desc

 )

6.2.5.3  IsGenericDescriptor ( 

Desc

 )

6.2.5.4  FromPropertyDescriptor ( 

Desc

 )

107

a.  Perform ! 

CreateDataPropertyOrThrow

(

obj

"get"

Desc

.[[Get]]).

7.  If 

Desc

 has a [[Set]] field, then

a.  Perform ! 

CreateDataPropertyOrThrow

(

obj

"set"

Desc

.[[Set]]).

8.  If 

Desc

 has an [[Enumerable]] field, then

a.  Perform ! 

CreateDataPropertyOrThrow

(

obj

"enumerable"

Desc

.[[Enumerable]]).

9.  If 

Desc

 has a [[Configurable]] field, then

a.  Perform ! 

CreateDataPropertyOrThrow

(

obj

"configurable"

Desc

.[[Configurable]]).

10.  Return 

obj

.

The abstract operation ToPropertyDescriptor takes argument 

Obj

. It performs the following steps when called:

1.  If 

Type

(

Obj

) is not Object, throw a 

TypeError

 exception.

2.  Let 

desc

 be a new 

Property Descriptor

 that initially has no fields.

3.  Let 

hasEnumerable

 be ? 

HasProperty

(

Obj

"enumerable"

).

4.  If 

hasEnumerable

 is 

true

, then

a.  Let 

enumerable

 be ! 

ToBoolean

(? 

Get

(

Obj

"enumerable"

)).

b.  Set 

desc

.[[Enumerable]] to 

enumerable

.

5.  Let 

hasConfigurable

 be ? 

HasProperty

(

Obj

"configurable"

).

6.  If 

hasConfigurable

 is 

true

, then

a.  Let 

configurable

 be ! 

ToBoolean

(? 

Get

(

Obj

"configurable"

)).

b.  Set 

desc

.[[Configurable]] to 

configurable

.

7.  Let 

hasValue

 be ? 

HasProperty

(

Obj

"value"

).

8.  If 

hasValue

 is 

true

, then

a.  Let 

value

 be ? 

Get

(

Obj

"value"

).

b.  Set 

desc

.[[Value]] to 

value

.

9.  Let 

hasWritable

 be ? 

HasProperty

(

Obj

"writable"

).

10.  If 

hasWritable

 is 

true

, then

a.  Let 

writable

 be ! 

ToBoolean

(? 

Get

(

Obj

"writable"

)).

b.  Set 

desc

.[[Writable]] to 

writable

.

11.  Let 

hasGet

 be ? 

HasProperty

(

Obj

"get"

).

12.  If 

hasGet

 is 

true

, then

a.  Let 

getter

 be ? 

Get

(

Obj

"get"

).

b.  If 

IsCallable

(

getter

) is 

false

 and 

getter

 is not 

undefined

, throw a 

TypeError

 exception.

c.  Set 

desc

.[[Get]] to 

getter

.

13.  Let 

hasSet

 be ? 

HasProperty

(

Obj

"set"

).

14.  If 

hasSet

 is 

true

, then

a.  Let 

setter

 be ? 

Get

(

Obj

"set"

).

b.  If 

IsCallable

(

setter

) is 

false

 and 

setter

 is not 

undefined

, throw a 

TypeError

 exception.

c.  Set 

desc

.[[Set]] to 

setter

.

15.  If 

desc

.[[Get]] is present or 

desc

.[[Set]] is present, then

a.  If 

desc

.[[Value]] is present or 

desc

.[[Writable]] is present, throw a 

TypeError

 exception.

16.  Return 

desc

.

The abstract operation CompletePropertyDescriptor takes argument 

Desc

 (a 

Property Descriptor

). It performs the

6.2.5.5  ToPropertyDescriptor ( 

Obj

 )

6.2.5.6  CompletePropertyDescriptor ( 

Desc

 )

108

following steps when called:

1. 

Assert

Desc

 is a 

Property Descriptor

.

2.  Let 

like

 be the 

Record

 { [[Value]]: 

undefined

, [[Writable]]: 

false

, [[Get]]: 

undefined

, [[Set]]: 

undefined

,

[[Enumerable]]: 

false

, [[Configurable]]: 

false

 }.

3.  If 

IsGenericDescriptor

(

Desc

) is 

true

 or 

IsDataDescriptor

(

Desc

) is 

true

, then

a.  If 

Desc

 does not have a [[Value]] field, set 

Desc

.[[Value]] to 

like

.[[Value]].

b.  If 

Desc

 does not have a [[Writable]] field, set 

Desc

.[[Writable]] to 

like

.[[Writable]].

4.  Else,

a.  If 

Desc

 does not have a [[Get]] field, set 

Desc

.[[Get]] to 

like

.[[Get]].

b.  If 

Desc

 does not have a [[Set]] field, set 

Desc

.[[Set]] to 

like

.[[Set]].

5.  If 

Desc

 does not have an [[Enumerable]] field, set 

Desc

.[[Enumerable]] to 

like

.[[Enumerable]].

6.  If 

Desc

 does not have a [[Configurable]] field, set 

Desc

.[[Configurable]] to 

like

.[[Configurable]].

7.  Return 

Desc

.

The 

Environment Record

 type is used to explain the behaviour of name resolution in nested functions and blocks.

This type and the operations upon it are defined in 

9.1

.

The 

Abstract Closure

 specification type is used to refer to algorithm steps together with a collection of values. Abstract

Closures are meta-values and are invoked using function application style such as 

closure

(

arg1

arg2

). Like 

abstract

operations

, invocations perform the algorithm steps described by the Abstract Closure.

In algorithm steps that create an Abstract Closure, values are captured with the verb "capture" followed by a list of
aliases. When an Abstract Closure is created, it captures the value that is associated with each alias at that time. In
steps that specify the algorithm to be performed when an Abstract Closure is called, each captured value is referred to
by the alias that was used to capture the value.

If an Abstract Closure returns a 

Completion Record

, that 

Completion Record

's [[Type]] must be either 

normal

 or 

throw

.

Abstract Closures are created inline as part of other algorithms, shown in the following example.

1.  Let 

addend

 be 41.

2.  Let 

closure

 be a new 

Abstract Closure

 with parameters (

x

) that captures 

addend

 and performs the following

steps when called:

a.  Return 

x

 + 

addend

.

3.  Let 

val

 be 

closure

(1).

4. 

Assert

val

 is 42.

The 

Data Block

 specification type is used to describe a distinct and mutable sequence of byte-sized (8 bit) numeric

values. A 

byte value

 is an 

integer

 value in the range 0 through 255, inclusive. A Data Block value is created with a fixed

number of bytes that each have the initial value 0.

For notational convenience within this specification, an array-like syntax can be used to access the individual bytes of

6.2.6  The Environment Record Specification Type

6.2.7  The Abstract Closure Specification Type

6.2.8  Data Blocks

109

a Data Block value. This notation presents a Data Block value as a 0-origined 

integer

-indexed sequence of bytes. For

example, if 

db

 is a 5 byte Data Block value then 

db

[2] can be used to access its 3

rd

 byte.

A data block that resides in memory that can be referenced from multiple agents concurrently is designated a 

Shared

Data Block

. A Shared Data Block has an identity (for the purposes of equality testing Shared Data Block values) that is

address-free

: it is tied not to the virtual addresses the block is mapped to in any process, but to the set of locations in

memory that the block represents. Two data blocks are equal only if the sets of the locations they contain are equal;
otherwise, they are not equal and the intersection of the sets of locations they contain is empty. Finally, Shared Data
Blocks can be distinguished from Data Blocks.

The semantics of Shared Data Blocks is defined using Shared Data Block events by the 

memory model

Abstract

operations

 below introduce Shared Data Block events and act as the interface between evaluation semantics and the

event semantics of the 

memory model

. The events form a 

candidate execution

, on which the 

memory model

 acts as a

filter. Please consult the 

memory model

 for full semantics.

Shared Data Block events are modeled by Records, defined in the 

memory model

.

The following 

abstract operations

 are used in this specification to operate upon Data Block values:

The abstract operation CreateByteDataBlock takes argument 

size

 (an 

integer

). It performs the following steps when

called:

1. 

Assert

size

 

 0.

2.  Let 

db

 be a new 

Data Block

 value consisting of 

size

 bytes. If it is impossible to create such a 

Data Block

, throw a

RangeError

 exception.

3.  Set all of the bytes of 

db

 to 0.

4.  Return 

db

.

The abstract operation CreateSharedByteDataBlock takes argument 

size

 (a non-negative 

integer

). It performs the

following steps when called:

1. 

Assert

size

 

 0.

2.  Let 

db

 be a new 

Shared Data Block

 value consisting of 

size

 bytes. If it is impossible to create such a 

Shared Data

Block

, throw a 

RangeError

 exception.

3.  Let 

execution

 be the [[CandidateExecution]] field of the 

surrounding agent

's 

Agent Record

.

4.  Let 

eventList

 be the [[EventList]] field of the element in 

execution

.[[EventsRecords]] whose [[AgentSignifier]] is

AgentSignifier

().

5.  Let 

zero

 be « 0 ».

6.  For each index 

i

 of 

db

, do

a.  Append 

WriteSharedMemory

 { [[Order]]: 

Init

, [[NoTear]]: 

true

, [[Block]]: 

db

, [[ByteIndex]]: 

i

,

[[ElementSize]]: 1, [[Payload]]: 

zero

 } to 

eventList

.

7.  Return 

db

.

The abstract operation CopyDataBlockBytes takes arguments 

toBlock

toIndex

 (a non-negative 

integer

), 

fromBlock

,

6.2.8.1  CreateByteDataBlock ( 

size

 )

6.2.8.2  CreateSharedByteDataBlock ( 

size

 )

6.2.8.3  CopyDataBlockBytes ( 

toBlock

toIndex

fromBlock

fromIndex

count

 )

110

fromIndex

 (a non-negative 

integer

), and 

count

 (a non-negative 

integer

). It performs the following steps when called:

1. 

Assert

fromBlock

 and 

toBlock

 are distinct 

Data Block

 or 

Shared Data Block

 values.

2.  Let 

fromSize

 be the number of bytes in 

fromBlock

.

3. 

Assert

fromIndex

 + 

count

 

 

fromSize

.

4.  Let 

toSize

 be the number of bytes in 

toBlock

.

5. 

Assert

toIndex

 + 

count

 

 

toSize

.

6.  Repeat, while 

count

 > 0,

a.  If 

fromBlock

 is a 

Shared Data Block

, then

i.  Let 

execution

 be the [[CandidateExecution]] field of the 

surrounding agent

's 

Agent Record

.

ii.  Let 

eventList

 be the [[EventList]] field of the element in 

execution

.[[EventsRecords]] whose

[[AgentSignifier]] is 

AgentSignifier

().

iii.  Let 

bytes

 be a 

List

 whose sole element is a nondeterministically chosen 

byte value

.

iv.  NOTE: In implementations, 

bytes

 is the result of a non-atomic read instruction on the underlying

hardware. The nondeterminism is a semantic prescription of the 

memory model

 to describe

observable behaviour of hardware with weak consistency.

v.  Let 

readEvent

 be 

ReadSharedMemory

 { [[Order]]: 

Unordered

, [[NoTear]]: 

true

, [[Block]]: 

fromBlock

,

[[ByteIndex]]: 

fromIndex

, [[ElementSize]]: 1 }.

vi.  Append 

readEvent

 to 

eventList

.

vii.  Append 

Chosen Value Record

 { [[Event]]: 

readEvent

, [[ChosenValue]]: 

bytes

 } to 

execution

.

[[ChosenValues]].

viii.  If 

toBlock

 is a 

Shared Data Block

, then

1.  Append 

WriteSharedMemory

 { [[Order]]: 

Unordered

, [[NoTear]]: 

true

, [[Block]]: 

toBlock

,

[[ByteIndex]]: 

toIndex

, [[ElementSize]]: 1, [[Payload]]: 

bytes

 } to 

eventList

.

ix.  Else,

1.  Set 

toBlock

[

toIndex

] to 

bytes

[0].

b.  Else,

i. 

Assert

toBlock

 is not a 

Shared Data Block

.

ii.  Set 

toBlock

[

toIndex

] to 

fromBlock

[

fromIndex

].

c.  Set 

toIndex

 to 

toIndex

 + 1.

d.  Set 

fromIndex

 to 

fromIndex

 + 1.

e.  Set 

count

 to 

count

 - 1.

7.  Return 

NormalCompletion

(

empty

).

These operations are not a part of the ECMAScript language; they are defined here solely to aid the specification of the
semantics of the ECMAScript language. Other, more specialized 

abstract operations

 are defined throughout this

specification.

The ECMAScript language implicitly performs automatic type conversion as needed. To clarify the semantics of
certain constructs it is useful to define a set of conversion 

abstract operations

. The conversion 

abstract operations

 are

polymorphic; they can accept a value of any 

ECMAScript language type

. But no other specification types are used

7  Abstract Operations

7.1  Type Conversion

111

with these operations.

The BigInt type has no implicit conversions in the ECMAScript language; programmers must call BigInt explicitly to
convert values from other types.

The abstract operation ToPrimitive takes argument 

input

 and optional argument 

preferredType

. It converts its 

input

argument to a non-Object type. If an object is capable of converting to more than one primitive type, it may use the
optional hint 

preferredType

 to favour that type. It performs the following steps when called:

1. 

Assert

input

 is an 

ECMAScript language value

.

2.  If 

Type

(

input

) is Object, then

a.  Let 

exoticToPrim

 be ? 

GetMethod

(

input

@@toPrimitive

).

b.  If 

exoticToPrim

 is not 

undefined

, then

i.  If 

preferredType

 is not present, let 

hint

 be 

"default"

.

ii.  Else if 

preferredType

 is 

string

, let 

hint

 be 

"string"

.

iii.  Else,

1. 

Assert

preferredType

 is 

number

.

2.  Let 

hint

 be 

"number"

.

iv.  Let 

result

 be ? 

Call

(

exoticToPrim

input

, « 

hint

 »).

v.  If 

Type

(

result

) is not Object, return 

result

.

vi.  Throw a 

TypeError

 exception.

c.  If 

preferredType

 is not present, let 

preferredType

 be 

number

.

d.  Return ? 

OrdinaryToPrimitive

(

input

preferredType

).

3.  Return 

input

.

NOTE

The abstract operation OrdinaryToPrimitive takes arguments 

O

 and 

hint

. It performs the following steps when called:

1. 

Assert

Type

(

O

) is Object.

2. 

Assert

hint

 is either 

string

 or 

number

.

3.  If 

hint

 is 

string

, then

a.  Let 

methodNames

 be « 

"toString"

"valueOf"

 ».

4.  Else,

a.  Let 

methodNames

 be « 

"valueOf"

"toString"

 ».

5.  For each element 

name

 of 

methodNames

, do

a.  Let 

method

 be ? 

Get

(

O

name

).

b.  If 

IsCallable

(

method

) is 

true

, then

i.  Let 

result

 be ? 

Call

(

method

O

).

ii.  If 

Type

(

result

) is not Object, return 

result

.

When ToPrimitive is called with no hint, then it generally behaves as if the hint were 

number

.

However, objects may over-ride this behaviour by defining a 

@@toPrimitive

 method. Of the

objects defined in this specification only Date objects (see 

21.4.4.45

) and Symbol objects (see

20.4.3.5

) over-ride the default ToPrimitive behaviour. Date objects treat no hint as if the hint were

string

.

7.1.1  ToPrimitive ( 

input

 [ , 

preferredType

 ] )

7.1.1.1  OrdinaryToPrimitive ( 

O

hint

 )

112

6.  Throw a 

TypeError

 exception.

The abstract operation ToBoolean takes argument 

argument

. It converts 

argument

 to a value of type Boolean according

to 

Table 11

:

Table 11: 

ToBoolean

 Conversions

Argument

Type

Result

Undefined

Return 

false

.

Null

Return 

false

.

Boolean

Return 

argument

.

Number

If 

argument

 is 

+0

𝔽

-0

𝔽

, or 

NaN

, return 

false

; otherwise return 

true

.

String

If 

argument

 is the empty String (its length is 0), return 

false

; otherwise return 

true

.

Symbol

Return 

true

.

BigInt

If 

argument

 is 

0

, return 

false

; otherwise return 

true

.

Object

Return 

true

.

NOTE

The abstract operation ToNumeric takes argument 

value

. It returns 

value

 converted to a Number or a BigInt. It

performs the following steps when called:

1.  Let 

primValue

 be ? 

ToPrimitive

(

value

number

).

2.  If 

Type

(

primValue

) is BigInt, return 

primValue

.

3.  Return ? 

ToNumber

(

primValue

).

The abstract operation ToNumber takes argument 

argument

. It converts 

argument

 to a value of type Number according

to 

Table 12

:

An alternate algorithm related to the [[IsHTMLDDA]] internal slot is mandated
in section 

B.3.7.1

.

7.1.2  ToBoolean ( 

argument

 )

7.1.3  ToNumeric ( 

value

 )

7.1.4  ToNumber ( 

argument

 )

113

Table 12: 

ToNumber

 Conversions

Argument Type

Result

Undefined

Return 

NaN

.

Null

Return 

+0

𝔽

.

Boolean

If 

argument

 is 

true

, return 

1

𝔽

. If 

argument

 is 

false

, return 

+0

𝔽

.

Number

Return 

argument

 (no conversion).

String

See grammar and conversion algorithm below.

Symbol

Throw a 

TypeError

 exception.

BigInt

Throw a 

TypeError

 exception.

Object

Apply the following steps:

1.  Let 

primValue

 be ? 

ToPrimitive

(

argument

number

).

2.  Return ? 

ToNumber

(

primValue

).

ToNumber

 applied to Strings applies the following grammar to the input String interpreted as a sequence of UTF-16

encoded code points (

6.1.4

). If the grammar cannot interpret the String as an expansion of 

StringNumericLiteral

, then

the result of 

ToNumber

 is 

NaN

.

NOTE 1

StringNumericLiteral

 

:::

StrWhiteSpace

opt

StrWhiteSpace

opt

 

StrNumericLiteral

 

StrWhiteSpace

opt

StrWhiteSpace

 

:::

StrWhiteSpaceChar

 

StrWhiteSpace

opt

StrWhiteSpaceChar

 

:::

WhiteSpace
LineTerminator

StrNumericLiteral

 

:::

StrDecimalLiteral
NonDecimalIntegerLiteral

[~Sep]

The terminal symbols of this grammar are all composed of characters in the Unicode Basic
Multilingual Plane (BMP). Therefore, the result of 

ToNumber

 will be 

NaN

 if the string contains

any 

leading surrogate

 or 

trailing surrogate

 code units, whether paired or unpaired.

7.1.4.1  ToNumber Applied to the String Type

Syntax

114

StrDecimalLiteral

 

:::

StrUnsignedDecimalLiteral

+

 

StrUnsignedDecimalLiteral

-

 

StrUnsignedDecimalLiteral

StrUnsignedDecimalLiteral

 

:::

Infinity

DecimalDigits

[~Sep]

 

.

 

DecimalDigits

[~Sep]

opt

 

ExponentPart

[~Sep]

opt

.

 

DecimalDigits

[~Sep]

 

ExponentPart

[~Sep]

opt

DecimalDigits

[~Sep]

 

ExponentPart

[~Sep]

opt

All grammar symbols not explicitly defined above have the definitions used in the Lexical Grammar for numeric
literals (

12.8.3

)

NOTE 2

The conversion of a String to a 

Number value

 is similar overall to the determination of the 

Number value

 for a

numeric literal (see 

12.8.3

), but some of the details are different, so the process for converting a String numeric literal

to a value of Number type is given here. This value is determined in two steps: first, a 

mathematical value

 (MV) is

derived from the String numeric literal; second, this 

mathematical value

 is rounded as described below. The MV on

any grammar symbol, not provided below, is the MV for that symbol defined in 

12.8.3.1

.

The MV of 

StringNumericLiteral

 

:::

  [empty]  is 0.

The MV of 

StringNumericLiteral

 

:::

 

StrWhiteSpace

 is 0.

The MV of 

StringNumericLiteral

 

:::

 

StrWhiteSpace

opt

 

StrNumericLiteral

 

StrWhiteSpace

opt

 is the MV of 

StrNumericLiteral

, no matter whether white space is present or not.

The MV of 

StrDecimalLiteral

 

:::

 

-

 

StrUnsignedDecimalLiteral

 is the negative of the MV of 

StrUnsignedDecimalLiteral

. (Note that if the MV of 

StrUnsignedDecimalLiteral

 is 0, the negative of this MV is also

0. The rounding rule described below handles the conversion of this signless mathematical zero to a floating-
point 

+0

𝔽

 or 

-0

𝔽

 as appropriate.)

The MV of 

StrUnsignedDecimalLiteral

 

:::

 

Infinity

 is 10

10000

 (a value so large that it will round to 

+

𝔽

).

The MV of 

StrUnsignedDecimalLiteral

 

:::

 

DecimalDigits

 

.

 

DecimalDigits

 is the MV of the first 

DecimalDigits

plus (the MV of the second 

DecimalDigits

 times 10

-

n

), where 

n

 is the number of code points in the second 

DecimalDigits

.

Some differences should be noted between the syntax of a 

StringNumericLiteral

 and a 

NumericLiteral

:

StringNumericLiteral

 may include leading and/or trailing white space and/or line

terminators.

StringNumericLiteral

 that is decimal may have any number of leading 

00

 digits.

StringNumericLiteral

 that is decimal may include a 

++

 or 

--

 to indicate its sign.

StringNumericLiteral

 that is empty or contains only white space is converted to 

+0

𝔽

.

Infinity

Infinity

 and 

-Infinity

-Infinity

 are recognized as a 

StringNumericLiteral

 but not as a 

NumericLiteral

.

StringNumericLiteral

 cannot include a 

BigIntLiteralSuffix

.

7.1.4.1.1  Runtime Semantics: MV

115

The MV of 

StrUnsignedDecimalLiteral

 

:::

 

DecimalDigits

 

.

 

ExponentPart

 is the MV of 

DecimalDigits

 times 10

e

,

where 

e

 is the MV of 

ExponentPart

.

The MV of 

StrUnsignedDecimalLiteral

 

:::

 

DecimalDigits

 

.

 

DecimalDigits

 

ExponentPart

 is (the MV of the first 

DecimalDigits

 plus (the MV of the second 

DecimalDigits

 times 10

-

n

)) times 10

e

, where 

n

 is the number of code

points in the second 

DecimalDigits

 and 

e

 is the MV of 

ExponentPart

.

The MV of 

StrUnsignedDecimalLiteral

 

:::

 

.

 

DecimalDigits

 is the MV of 

DecimalDigits

 times 10

-

n

, where 

n

 is the

number of code points in 

DecimalDigits

.

The MV of 

StrUnsignedDecimalLiteral

 

:::

 

.

 

DecimalDigits

 

ExponentPart

 is the MV of 

DecimalDigits

 times 10

e

 - 

n

,

where 

n

 is the number of code points in 

DecimalDigits

 and 

e

 is the MV of 

ExponentPart

.

The MV of 

StrUnsignedDecimalLiteral

 

:::

 

DecimalDigits

 

ExponentPart

 is the MV of 

DecimalDigits

 times 10

e

,

where 

e

 is the MV of 

ExponentPart

.

Once the exact MV for a String numeric literal has been determined, it is then rounded to a value of the Number type.
If the MV is 0, then the rounded value is 

+0

𝔽

 unless the first non white space code point in the String numeric literal is

--

, in which case the rounded value is 

-0

𝔽

. Otherwise, the rounded value must be the 

Number value

 for the MV (in the

sense defined in 

6.1.6.1

), unless the literal includes a 

StrUnsignedDecimalLiteral

 and the literal has more than 20

significant digits, in which case the 

Number value

 may be either the 

Number value

 for the MV of a literal produced

by replacing each significant digit after the 20th with a 0 digit or the 

Number value

 for the MV of a literal produced by

replacing each significant digit after the 20th with a 0 digit and then incrementing the literal at the 20th digit position.
A digit is significant if it is not part of an 

ExponentPart

 and

it is not 

00

; or

there is a non-zero digit to its left and there is a non-zero digit, not in the 

ExponentPart

, to its right.

The abstract operation ToIntegerOrInfinity takes argument 

argument

. It converts 

argument

 to an 

integer

, +

, or -

. It

performs the following steps when called:

1.  Let 

number

 be ? 

ToNumber

(

argument

).

2.  If 

number

 is 

NaN

+0

𝔽

, or 

-0

𝔽

, return 0.

3.  If 

number

 is 

+

𝔽

, return +

.

4.  If 

number

 is 

-

𝔽

, return -

.

5.  Let 

integer

 be 

floor

(

abs

number

))).

6.  If 

number

 < 

+0

𝔽

, set 

integer

 to -

integer

.

7.  Return 

integer

.

The abstract operation ToInt32 takes argument 

argument

. It converts 

argument

 to one of 2

32

 

integral Number

 values in

the range 

31

(2

31

 - 1), inclusive. It performs the following steps when called:

1.  Let 

number

 be ? 

ToNumber

(

argument

).

2.  If 

number

 is 

NaN

+0

𝔽

-0

𝔽

+

𝔽

, or 

-

𝔽

, return 

+0

𝔽

.

3.  Let 

int

 be the 

mathematical value

 that is the same sign as 

number

 and whose magnitude is

floor

(

abs

(

(

number

))).

4.  Let 

int32bit

 be 

int

 

modulo

 2

32

.

7.1.5  ToIntegerOrInfinity ( 

argument

 )

7.1.6  ToInt32 ( 

argument

 )

116

5.  If 

int32bit

 

 2

31

(

int32bit

 - 2

32

); otherwise return 

(

int32bit

).

NOTE

The abstract operation ToUint32 takes argument 

argument

. It converts 

argument

 to one of 2

32

 

integral Number

 values

in the range 

+0

𝔽

(2

32

 - 1), inclusive. It performs the following steps when called:

1.  Let 

number

 be ? 

ToNumber

(

argument

).

2.  If 

number

 is 

NaN

+0

𝔽

-0

𝔽

+

𝔽

, or 

-

𝔽

, return 

+0

𝔽

.

3.  Let 

int

 be the 

mathematical value

 that is the same sign as 

number

 and whose magnitude is

floor

(

abs

(

(

number

))).

4.  Let 

int32bit

 be 

int

 

modulo

 2

32

.

5.  Return 

int32bit

).

NOTE

The abstract operation ToInt16 takes argument 

argument

. It converts 

argument

 to one of 2

16

 

integral Number

 values in

the range 

15

(2

15

 - 1), inclusive. It performs the following steps when called:

1.  Let 

number

 be ? 

ToNumber

(

argument

).

2.  If 

number

 is 

NaN

+0

𝔽

-0

𝔽

+

𝔽

, or 

-

𝔽

, return 

+0

𝔽

.

3.  Let 

int

 be the 

mathematical value

 that is the same sign as 

number

 and whose magnitude is

floor

(

abs

(

(

number

))).

4.  Let 

int16bit

 be 

int

 

modulo

 2

16

.

5.  If 

int16bit

 

 2

15

(

int16bit

 - 2

16

); otherwise return 

(

int16bit

).

Given the above definition of ToInt32:

The ToInt32 abstract operation is idempotent: if applied to a result that it produced, the
second application leaves that value unchanged.
ToInt32(

ToUint32

(

x

)) is the same value as ToInt32(

x

) for all values of 

x

. (It is to preserve

this latter property that 

+

𝔽

 and 

-

𝔽

 are mapped to 

+0

𝔽

.)

ToInt32 maps 

-0

𝔽

 to 

+0

𝔽

.

Given the above definition of ToUint32:

Step 

5

 is the only difference between ToUint32 and 

ToInt32

.

The ToUint32 abstract operation is idempotent: if applied to a result that it produced, the
second application leaves that value unchanged.
ToUint32(

ToInt32

(

x

)) is the same value as ToUint32(

x

) for all values of 

x

. (It is to preserve

this latter property that 

+

𝔽

 and 

-

𝔽

 are mapped to 

+0

𝔽

.)

ToUint32 maps 

-0

𝔽

 to 

+0

𝔽

.

7.1.7  ToUint32 ( 

argument

 )

7.1.8  ToInt16 ( 

argument

 )

117

The abstract operation ToUint16 takes argument 

argument

. It converts 

argument

 to one of 2

16

 

integral Number

 values

in the range 

+0

𝔽

(2

16

 - 1), inclusive. It performs the following steps when called:

1.  Let 

number

 be ? 

ToNumber

(

argument

).

2.  If 

number

 is 

NaN

+0

𝔽

-0

𝔽

+

𝔽

, or 

-

𝔽

, return 

+0

𝔽

.

3.  Let 

int

 be the 

mathematical value

 that is the same sign as 

number

 and whose magnitude is

floor

(

abs

(

(

number

))).

4.  Let 

int16bit

 be 

int

 

modulo

 2

16

.

5.  Return 

int16bit

).

NOTE

The abstract operation ToInt8 takes argument 

argument

. It converts 

argument

 to one of 2

8

 

integral Number

 values in

the range 

-128

𝔽

 through 

127

𝔽

, inclusive. It performs the following steps when called:

1.  Let 

number

 be ? 

ToNumber

(

argument

).

2.  If 

number

 is 

NaN

+0

𝔽

-0

𝔽

+

𝔽

, or 

-

𝔽

, return 

+0

𝔽

.

3.  Let 

int

 be the 

mathematical value

 that is the same sign as 

number

 and whose magnitude is

floor

(

abs

(

(

number

))).

4.  Let 

int8bit

 be 

int

 

modulo

 2

8

.

5.  If 

int8bit

 

 2

7

, return 

int8bit

 - 2

8

(

int8bit

).

The abstract operation ToUint8 takes argument 

argument

. It converts 

argument

 to one of 2

8

 

integral Number

 values in

the range 

+0

𝔽

 through 

255

𝔽

, inclusive. It performs the following steps when called:

1.  Let 

number

 be ? 

ToNumber

(

argument

).

2.  If 

number

 is 

NaN

+0

𝔽

-0

𝔽

+

𝔽

, or 

-

𝔽

, return 

+0

𝔽

.

3.  Let 

int

 be the 

mathematical value

 that is the same sign as 

number

 and whose magnitude is

floor

(

abs

(

(

number

))).

4.  Let 

int8bit

 be 

int

 

modulo

 2

8

.

5.  Return 

int8bit

).

The abstract operation ToUint8Clamp takes argument 

argument

. It converts 

argument

 to one of 2

8

 

integral Number

Given the above definition of ToUint16:

The substitution of 2

16

 for 2

32

 in step 

4

 is the only difference between 

ToUint32

 and

ToUint16.
ToUint16 maps 

-0

𝔽

 to 

+0

𝔽

.

7.1.9  ToUint16 ( 

argument

 )

7.1.10  ToInt8 ( 

argument

 )

7.1.11  ToUint8 ( 

argument

 )

7.1.12  ToUint8Clamp ( 

argument

 )

118

values in the range 

+0

𝔽

 through 

255

𝔽

, inclusive. It performs the following steps when called:

1.  Let 

number

 be ? 

ToNumber

(

argument

).

2.  If 

number

 is 

NaN

, return 

+0

𝔽

.

number

 0, return 

+0

𝔽

.

number

 255, return 

255

𝔽

.

5.  Let 

f

 be 

floor

(

(

number

)).

6.  If 

f

 + 0.5 < 

(

number

(

f

 + 1).

number

) < 

f

(

f

).

8.  If 

f

(

f

 + 1).

9.  Return 

f

).

NOTE

The abstract operation ToBigInt takes argument 

argument

. It converts 

argument

 to a BigInt value, or throws if an

implicit conversion from Number would be required. It performs the following steps when called:

1.  Let 

prim

 be ? 

ToPrimitive

(

argument

number

).

2.  Return the value that 

prim

 corresponds to in 

Table 13

.

Table 13: BigInt Conversions

Argument Type

Result

Undefined

Throw a 

TypeError

 exception.

Null

Throw a 

TypeError

 exception.

Boolean

Return 

1n

1n

 if 

prim

 is 

true

 and 

0n

0n

 if 

prim

 is 

false

.

BigInt

Return 

prim

.

Number

Throw a 

TypeError

 exception.

String

1.  Let 

n

 be ! 

StringToBigInt

(

prim

).

2.  If 

n

 is 

NaN

, throw a 

SyntaxError

 exception.

3.  Return 

n

.

Symbol

Throw a 

TypeError

 exception.

Apply the algorithm in 

7.1.4.1

 with the following changes:

Unlike the other ECMAScript 

integer

 conversion abstract operation, ToUint8Clamp rounds

rather than truncates non-integral values and does not convert 

+

𝔽

 to 

+0

𝔽

. ToUint8Clamp does

“round half to even” tie-breaking. This differs from 

Math.round

Math.round

 which does “round half up”

tie-breaking.

7.1.13  ToBigInt ( 

argument

 )

7.1.14  StringToBigInt ( 

argument

 )

119

Replace the 

StrUnsignedDecimalLiteral

 production with 

DecimalDigits

 to not allow 

Infinity

, decimal points, or

exponents.
If the MV is 

NaN

, return 

NaN

, otherwise return the BigInt which exactly corresponds to the MV, rather than

rounding to a Number.

The abstract operation ToBigInt64 takes argument 

argument

. It converts 

argument

 to one of 2

64

 BigInt values in the

(-2

63

) through 

63

-1), inclusive. It performs the following steps when called:

1.  Let 

n

 be ? 

ToBigInt

(

argument

).

2.  Let 

int64bit

n

modulo

 2

64

.

3.  If 

int64bit

 

 2

63

(

int64bit

 - 2

64

(

int64bit

).

The abstract operation ToBigUint64 takes argument 

argument

. It converts 

argument

 to one of 2

64

 BigInt values in the

range 

0

 through the BigInt value for 

(2

64

-1), inclusive. It performs the following steps when called:

1.  Let 

n

 be ? 

ToBigInt

(

argument

).

2.  Let 

int64bit

n

modulo

 2

64

.

3.  Return 

int64bit

).

The abstract operation ToString takes argument 

argument

. It converts 

argument

 to a value of type String according to

Table 14

:

7.1.15  ToBigInt64 ( 

argument

 )

7.1.16  ToBigUint64 ( 

argument

 )

7.1.17  ToString ( 

argument

 )

120

Table 14: 

ToString

 Conversions

Argument Type

Result

Undefined

Return 

"undefined"

.

Null

Return 

"null"

.

Boolean

If 

argument

 is 

true

, return 

"true"

.

If 

argument

 is 

false

, return 

"false"

.

Number

Return ! 

Number::toString

(

argument

).

String

Return 

argument

.

Symbol

Throw a 

TypeError

 exception.

BigInt

Return ! 

BigInt::toString

(

argument

).

Object

Apply the following steps:

1.  Let 

primValue

 be ? 

ToPrimitive

(

argument

string

).

2.  Return ? 

ToString

(

primValue

).

The abstract operation ToObject takes argument 

argument

. It converts 

argument

 to a value of type Object according to

Table 15

:

7.1.18  ToObject ( 

argument

 )

121

 

 

 

 

 

 

 

Content      ..     12      13      14      15     ..