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

 

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

 

Search            copyright infringement  

 

 

 

 

 

 

 

 

 

 

 

Content      ..     27      28      29      30     ..

 

 

 

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

 

 

The value of the 

"name"

 property is explicitly specified for each built-in functions whose property key is a Symbol

value. If such an explicitly specified value starts with the prefix 

"get "

 or 

"set "

 and the function for which it is

specified is a get or set accessor function of a built-in property, the value without the prefix is passed to the 

name

parameter, and the value 

"get"

 or 

"set"

 (respectively) is passed to the 

prefix

 parameter when calling

CreateBuiltinFunction

.

Unless otherwise specified, the 

"name"

 property of a built-in 

function object

 has the attributes { [[Writable]]: 

false

,

[[Enumerable]]: 

false

, [[Configurable]]: 

true

 }.

Every other 

data property

 described in clauses 

19

 through 

28

 and in Annex 

B.2

 has the attributes { [[Writable]]: 

true

,

[[Enumerable]]: 

false

, [[Configurable]]: 

true

 } unless otherwise specified.

Every 

accessor property

 described in clauses 

19

 through 

28

 and in Annex 

B.2

 has the attributes { [[Enumerable]]: 

false

,

[[Configurable]]: 

true

 } unless otherwise specified. If only a get accessor function is described, the set accessor function

is the default value, 

undefined

. If only a set accessor is described the get accessor is the default value, 

undefined

.

The 

global object

:

is created before control enters any 

execution context

.

does not have a [[Construct]] internal method; it cannot be used as a 

constructor

 with the 

new

new

 operator.

does not have a [[Call]] internal method; it cannot be invoked as a function.
has a [[Prototype]] internal slot whose value is 

host-defined

.

may have 

host

 defined properties in addition to the properties defined in this specification. This may include a

property whose value is the global object itself.

The initial value of the 

"globalThis"

 property of the 

global object

 in a 

Realm Record

 

realm

 is 

realm

.[[GlobalEnv]].

[[GlobalThisValue]].

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

true

, [[Enumerable]]: 

false

, [[Configurable]]: 

true

 }.

The value of 

Infinity

Infinity

 is 

+

𝔽

 (see 

6.1.6.1

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

false

, [[Enumerable]]:

false

, [[Configurable]]: 

false

 }.

The value of 

NaN

NaN

 is 

NaN

 (see 

6.1.6.1

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

false

, [[Enumerable]]: 

false

,

[[Configurable]]: 

false

 }.

19  The Global Object

19.1  Value Properties of the Global Object

19.1.1  globalThis

19.1.2  Infinity

19.1.3  NaN

464

The value of 

undefined

undefined

 is 

undefined

 (see 

6.1.1

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

false

,

[[Enumerable]]: 

false

, [[Configurable]]: 

false

 }.

The 

eval

eval

 function is the 

%eval%

 intrinsic object. When the 

eval

eval

 function is called with one argument 

x

, the

following steps are taken:

1. 

Assert

: The 

execution context stack

 has at least two elements.

2.  Let 

callerContext

 be the second to top element of the 

execution context stack

.

3.  Let 

callerRealm

 be 

callerContext

's 

Realm

.

4.  Return ? 

PerformEval

(

x

callerRealm

false

false

).

The abstract operation PerformEval takes arguments 

x

callerRealm

strictCaller

, and 

direct

. It performs the following

steps when called:

1. 

Assert

: If 

direct

 is 

false

, then 

strictCaller

 is also 

false

.

2.  If 

Type

(

x

) is not String, return 

x

.

3.  Let 

evalRealm

 be 

the current Realm Record

.

4.  Perform ? 

HostEnsureCanCompileStrings

(

callerRealm

evalRealm

).

5.  Let 

inFunction

 be 

false

.

6.  Let 

inMethod

 be 

false

.

7.  Let 

inDerivedConstructor

 be 

false

.

8.  If 

direct

 is 

true

, then

a.  Let 

thisEnvRec

 be ! 

GetThisEnvironment

().

b.  If 

thisEnvRec

 is a 

function Environment Record

, then

i.  Let 

F

 be 

thisEnvRec

.[[FunctionObject]].

ii.  Set 

inFunction

 to 

true

.

iii.  Set 

inMethod

 to 

thisEnvRec

.HasSuperBinding().

iv.  If 

F

.[[ConstructorKind]] is 

derived

, set 

inDerivedConstructor

 to 

true

.

9.  Perform the following substeps in an 

implementation-defined

 order, possibly interleaving parsing and error

detection:

a.  Let 

script

 be 

ParseText

(! 

StringToCodePoints

(

x

), 

Script

).

b.  If 

script

 is a 

List

 of errors, throw a 

SyntaxError

 exception.

c.  If 

script

 

Contains

 

ScriptBody

 is 

false

, return 

undefined

.

d.  Let 

body

 be the 

ScriptBody

 of 

script

.

e.  If 

inFunction

 is 

false

, and 

body

 

Contains

 

NewTarget

, throw a 

SyntaxError

 exception.

f.  If 

inMethod

 is 

false

, and 

body

 

Contains

 

SuperProperty

, throw a 

SyntaxError

 exception.

g.  If 

inDerivedConstructor

 is 

false

, and 

body

 

Contains

 

SuperCall

, throw a 

SyntaxError

 exception.

10.  If 

strictCaller

 is 

true

, let 

strictEval

 be 

true

.

11.  Else, let 

strictEval

 be 

IsStrict

 of 

script

.

19.1.4  undefined

19.2  Function Properties of the Global Object

19.2.1  eval ( 

x

 )

19.2.1.1  PerformEval ( 

x

callerRealm

strictCaller

direct

 )

465

12.  Let 

runningContext

 be the 

running execution context

.

13.  NOTE: If 

direct

 is 

true

runningContext

 will be the 

execution context

 that performed the 

direct eval

. If 

direct

 is

false

runningContext

 will be the 

execution context

 for the invocation of the 

eval

eval

 function.

14.  If 

direct

 is 

true

, then

a.  Let 

lexEnv

 be 

NewDeclarativeEnvironment

(

runningContext

's LexicalEnvironment).

b.  Let 

varEnv

 be 

runningContext

's VariableEnvironment.

15.  Else,

a.  Let 

lexEnv

 be 

NewDeclarativeEnvironment

(

evalRealm

.[[GlobalEnv]]).

b.  Let 

varEnv

 be 

evalRealm

.[[GlobalEnv]].

16.  If 

strictEval

 is 

true

, set 

varEnv

 to 

lexEnv

.

17.  If 

runningContext

 is not already suspended, suspend 

runningContext

.

18.  Let 

evalContext

 be a new ECMAScript code 

execution context

.

19.  Set 

evalContext

's Function to 

null

.

20.  Set 

evalContext

's 

Realm

 to 

evalRealm

.

21.  Set 

evalContext

's ScriptOrModule to 

runningContext

's ScriptOrModule.

22.  Set 

evalContext

's VariableEnvironment to 

varEnv

.

23.  Set 

evalContext

's LexicalEnvironment to 

lexEnv

.

24.  Push 

evalContext

 onto the 

execution context stack

evalContext

 is now the 

running execution context

.

25.  Let 

result

 be 

EvalDeclarationInstantiation

(

body

varEnv

lexEnv

strictEval

).

26.  If 

result

.[[Type]] is 

normal

, then

a.  Set 

result

 to the result of evaluating 

body

.

27.  If 

result

.[[Type]] is 

normal

 and 

result

.[[Value]] is 

empty

, then

a.  Set 

result

 to 

NormalCompletion

(

undefined

).

28.  Suspend 

evalContext

 and remove it from the 

execution context stack

.

29.  Resume the context that is now on the top of the 

execution context stack

 as the 

running execution context

.

30.  Return 

Completion

(

result

).

NOTE

The 

host-defined

 abstract operation HostEnsureCanCompileStrings takes arguments 

callerRealm

 (a 

Realm Record

) and

calleeRealm

 (a 

Realm Record

). It allows 

host

 environments to block certain ECMAScript functions which allow

developers to compile strings into ECMAScript code.

An implementation of HostEnsureCanCompileStrings may complete normally or abruptly. Any abrupt completions
will be propagated to its callers. The default implementation of HostEnsureCanCompileStrings is to unconditionally
return an empty normal completion.

The abstract operation EvalDeclarationInstantiation takes arguments 

body

varEnv

lexEnv

, and 

strict

. It performs the

The eval code cannot instantiate variable or function bindings in the variable environment of the
calling context that invoked the eval if the calling context is evaluating formal parameter
initializers or if either the code of the calling context or the eval code is 

strict mode code

. Instead

such bindings are instantiated in a new VariableEnvironment that is only accessible to the eval
code. Bindings introduced by 

let

let

const

const

, or 

class

class

 declarations are always instantiated in a

new LexicalEnvironment.

19.2.1.2  HostEnsureCanCompileStrings ( 

callerRealm

calleeRealm

 )

19.2.1.3  EvalDeclarationInstantiation ( 

body

varEnv

lexEnv

strict

 )

466

following steps when called:

1.  Let 

varNames

 be the 

VarDeclaredNames

 of 

body

.

2.  Let 

varDeclarations

 be the 

VarScopedDeclarations

 of 

body

.

3.  If 

strict

 is 

false

, then

a.  If 

varEnv

 is a 

global Environment Record

, then

i.  For each element 

name

 of 

varNames

, do

1.  If 

varEnv

.HasLexicalDeclaration(

name

) is 

true

, throw a 

SyntaxError

 exception.

2.  NOTE: 

eval

eval

 will not create a global var declaration that would be shadowed by a global

lexical declaration.

b.  Let 

thisEnv

 be 

lexEnv

.

c. 

Assert

: The following loop will terminate.

d.  Repeat, while 

thisEnv

 is not the same as 

varEnv

,

i.  If 

thisEnv

 is not an 

object Environment Record

, then

1.  NOTE: The environment of with statements cannot contain any lexical declaration so it

doesn't need to be checked for var/let hoisting conflicts.

2.  For each element 

name

 of 

varNames

, do

a.  If 

thisEnv

.HasBinding(

name

) is 

true

, then

i.  Throw a 

SyntaxError

 exception.

ii.  NOTE: Annex 

B.3.5

 defines alternate semantics for the above step.

b.  NOTE: A 

direct eval

 will not hoist var declaration over a like-named lexical

declaration.

ii.  Set 

thisEnv

 to 

thisEnv

.[[OuterEnv]].

4.  Let 

functionsToInitialize

 be a new empty 

List

.

5.  Let 

declaredFunctionNames

 be a new empty 

List

.

6.  For each element 

d

 of 

varDeclarations

, in reverse 

List

 order, do

a.  If 

d

 is neither a 

VariableDeclaration

 nor a 

ForBinding

 nor a 

BindingIdentifier

, then

i. 

Assert

d

 is either a 

FunctionDeclaration

, a 

GeneratorDeclaration

, an 

AsyncFunctionDeclaration

, or an 

AsyncGeneratorDeclaration

.

ii.  NOTE: If there are multiple function declarations for the same name, the last declaration is used.

iii.  Let 

fn

 be the sole element of the 

BoundNames

 of 

d

.

iv.  If 

fn

 is not an element of 

declaredFunctionNames

, then

1.  If 

varEnv

 is a 

global Environment Record

, then

a.  Let 

fnDefinable

 be ? 

varEnv

.CanDeclareGlobalFunction(

fn

).

b.  If 

fnDefinable

 is 

false

, throw a 

TypeError

 exception.

2.  Append 

fn

 to 

declaredFunctionNames

.

3.  Insert 

d

 as the first element of 

functionsToInitialize

.

7.  NOTE: Annex 

B.3.3.3

 adds additional steps at this point.

8.  Let 

declaredVarNames

 be a new empty 

List

.

9.  For each element 

d

 of 

varDeclarations

, do

a.  If 

d

 is a 

VariableDeclaration

, a 

ForBinding

, or a 

BindingIdentifier

, then

i.  For each String 

vn

 of the 

BoundNames

 of 

d

, do

1.  If 

vn

 is not an element of 

declaredFunctionNames

, then

a.  If 

varEnv

 is a 

global Environment Record

, then

i.  Let 

vnDefinable

 be ? 

varEnv

.CanDeclareGlobalVar(

vn

).

ii.  If 

vnDefinable

 is 

false

, throw a 

TypeError

 exception.

b.  If 

vn

 is not an element of 

declaredVarNames

, then

i.  Append 

vn

 to 

declaredVarNames

.

467

10.  NOTE: No abnormal terminations occur after this algorithm step unless 

varEnv

 is a 

global Environment Record

and the 

global object

 is a 

Proxy exotic object

.

11.  Let 

lexDeclarations

 be the 

LexicallyScopedDeclarations

 of 

body

.

12.  For each element 

d

 of 

lexDeclarations

, do

a.  NOTE: Lexically declared names are only instantiated here but not initialized.

b.  For each element 

dn

 of the 

BoundNames

 of 

d

, do

i.  If 

IsConstantDeclaration

 of 

d

 is 

true

, then

1.  Perform ? 

lexEnv

.CreateImmutableBinding(

dn

true

).

ii.  Else,

1.  Perform ? 

lexEnv

.CreateMutableBinding(

dn

false

).

13.  For each 

Parse Node

 

f

 of 

functionsToInitialize

, do

a.  Let 

fn

 be the sole element of the 

BoundNames

 of 

f

.

b.  Let 

fo

 be 

InstantiateFunctionObject

 of 

f

 with argument 

lexEnv

.

c.  If 

varEnv

 is a 

global Environment Record

, then

i.  Perform ? 

varEnv

.CreateGlobalFunctionBinding(

fn

fo

true

).

d.  Else,

i.  Let 

bindingExists

 be 

varEnv

.HasBinding(

fn

).

ii.  If 

bindingExists

 is 

false

, then

1.  Let 

status

 be ! 

varEnv

.CreateMutableBinding(

fn

true

).

2. 

Assert

status

 is not an 

abrupt completion

 because of validation preceding step 

10

.

3.  Perform ! 

varEnv

.InitializeBinding(

fn

fo

).

iii.  Else,

1.  Perform ! 

varEnv

.SetMutableBinding(

fn

fo

false

).

14.  For each String 

vn

 of 

declaredVarNames

, do

a.  If 

varEnv

 is a 

global Environment Record

, then

i.  Perform ? 

varEnv

.CreateGlobalVarBinding(

vn

true

).

b.  Else,

i.  Let 

bindingExists

 be 

varEnv

.HasBinding(

vn

).

ii.  If 

bindingExists

 is 

false

, then

1.  Let 

status

 be ! 

varEnv

.CreateMutableBinding(

vn

true

).

2. 

Assert

status

 is not an 

abrupt completion

 because of validation preceding step 

10

.

3.  Perform ! 

varEnv

.InitializeBinding(

vn

undefined

).

15.  Return 

NormalCompletion

(

empty

).

NOTE

The 

isFinite

isFinite

 function is the 

%isFinite%

 intrinsic object. When the 

isFinite

isFinite

 function is called with one

argument 

number

, the following steps are taken:

1.  Let 

num

 be ? 

ToNumber

(

number

).

2.  If 

num

 is 

NaN

+

𝔽

, or 

-

𝔽

, return 

false

.

3.  Otherwise, return 

true

.

An alternative version of this algorithm is described in 

B.3.5

.

19.2.2  isFinite ( 

number

 )

19.2.3  isNaN ( 

number

 )

468

The 

isNaN

isNaN

 function is the 

%isNaN%

 intrinsic object. When the 

isNaN

isNaN

 function is called with one argument 

number

,

the following steps are taken:

1.  Let 

num

 be ? 

ToNumber

(

number

).

2.  If 

num

 is 

NaN

, return 

true

.

3.  Otherwise, return 

false

.

NOTE

The 

parseFloat

parseFloat

 function produces a 

Number value

 dictated by interpretation of the contents of the 

string

argument as a decimal literal.

The 

parseFloat

parseFloat

 function is the 

%parseFloat%

 intrinsic object. When the 

parseFloat

parseFloat

 function is called with one

argument 

string

, the following steps are taken:

1.  Let 

inputString

 be ? 

ToString

(

string

).

2.  Let 

trimmedString

 be ! 

TrimString

(

inputString

start

).

3.  If neither 

trimmedString

 nor any prefix of 

trimmedString

 satisfies the syntax of a 

StrDecimalLiteral

 (see 

7.1.4.1

),

return 

NaN

.

4.  Let 

numberString

 be the longest prefix of 

trimmedString

, which might be 

trimmedString

 itself, that satisfies the

syntax of a 

StrDecimalLiteral

.

5.  Let 

mathFloat

 be MV of 

numberString

.

6.  If 

mathFloat

 = 0, then

a.  If the first code unit of 

trimmedString

 is the code unit 0x002D (HYPHEN-MINUS), return 

-0

𝔽

.

b.  Return 

+0

𝔽

.

7.  Return 

mathFloat

).

NOTE

The 

parseInt

parseInt

 function produces an 

integral Number

 dictated by interpretation of the contents of the 

string

argument according to the specified 

radix

. Leading white space in 

string

 is ignored. If 

radix

 is 

undefined

 or 0, it is

assumed to be 10 except when the number begins with the code unit pairs 

0x

0x

 or 

0X

0X

, in which case a radix of 16 is

assumed. If 

radix

 is 16, the number may also optionally begin with the code unit pairs 

0x

0x

 or 

0X

0X

.

The 

parseInt

parseInt

 function is the 

%parseInt%

 intrinsic object. When the 

parseInt

parseInt

 function is called, the following

steps are taken:

1.  Let 

inputString

 be ? 

ToString

(

string

).

2.  Let 

S

 be ! 

TrimString

(

inputString

start

).

3.  Let 

sign

 be 1.

A reliable way for ECMAScript code to test if a value 

XX

 is a 

NaN

 is an expression of the form

X !== X

X !== X

. The result will be 

true

 if and only if 

XX

 is a 

NaN

.

parseFloat

parseFloat

 may interpret only a leading portion of 

string

 as a 

Number value

; it ignores any

code units that cannot be interpreted as part of the notation of a decimal literal, and no indication
is given that any such code units were ignored.

19.2.4  parseFloat ( 

string

 )

19.2.5  parseInt ( 

string

radix

 )

469

4.  If 

S

 is not empty and the first code unit of 

S

 is the code unit 0x002D (HYPHEN-MINUS), set 

sign

 to -1.

5.  If 

S

 is not empty and the first code unit of 

S

 is the code unit 0x002B (PLUS SIGN) or the code unit 0x002D

(HYPHEN-MINUS), remove the first code unit from 

S

.

6.  Let 

R

ToInt32

(

radix

)).

7.  Let 

stripPrefix

 be 

true

.

8.  If 

R

 

 0, then

a.  If 

R

 < 2 or 

R

 > 36, return 

NaN

.

b.  If 

R

 

 16, set 

stripPrefix

 to 

false

.

9.  Else,

a.  Set 

R

 to 10.

10.  If 

stripPrefix

 is 

true

, then

a.  If the length of 

S

 is at least 2 and the first two code units of 

S

 are either 

"0x"

 or 

"0X"

, then

i.  Remove the first two code units from 

S

.

ii.  Set 

R

 to 16.

11.  If 

S

 contains a code unit that is not a radix-

R

 digit, let 

end

 be the index within 

S

 of the first such code unit;

otherwise, let 

end

 be the length of 

S

.

12.  Let 

Z

 be the 

substring

 of 

S

 from 0 to 

end

.

13.  If 

Z

 is empty, return 

NaN

.

14.  Let 

mathInt

 be the 

integer

 value that is represented by 

Z

 in radix-

R

 notation, using the letters 

A

-

Z

 and 

a

-

z

 for

digits with values 10 through 35. (However, if 

R

 is 10 and 

Z

 contains more than 20 significant digits, every

significant digit after the 20th may be replaced by a 0 digit, at the option of the implementation; and if 

R

 is not

2, 4, 8, 10, 16, or 32, then 

mathInt

 may be an 

implementation-approximated

 value representing the 

integer

 value

that is represented by 

Z

 in radix-

R

 notation.)

15.  If 

mathInt

 = 0, then

a.  If 

sign

 = -1, return 

-0

𝔽

.

b.  Return 

+0

𝔽

.

16.  Return 

sign

 

×

 

mathInt

).

NOTE

Uniform Resource Identifiers, or URIs, are Strings that identify resources (e.g. web pages or files) and transport
protocols by which to access them (e.g. HTTP or FTP) on the Internet. The ECMAScript language itself does not
provide any support for using URIs except for functions that encode and decode URIs as described in 

19.2.6.2

,

19.2.6.3

19.2.6.4

 and 

19.2.6.5

NOTE

A URI is composed of a sequence of components separated by component separators. The general form is:

parseInt

parseInt

 may interpret only a leading portion of 

string

 as an 

integer

 value; it ignores any code

units that cannot be interpreted as part of the notation of an 

integer

, and no indication is given

that any such code units were ignored.

Many implementations of ECMAScript provide additional functions and methods that
manipulate web pages; these functions are beyond the scope of this standard.

19.2.6  URI Handling Functions

19.2.6.1  URI Syntax and Semantics

470

Scheme

 

::

 

First

 

//

 

Second

 

;;

 

Third

 

??

 

Fourth

where the italicized names represent components and “

::

”, “

//

”, “

;;

” and “

??

” are reserved for use as separators. The

encodeURI

encodeURI

 and 

decodeURI

decodeURI

 functions are intended to work with complete URIs; they assume that any reserved

code units in the URI are intended to have special meaning and so are not encoded. The 

encodeURIComponent

encodeURIComponent

and 

decodeURIComponent

decodeURIComponent

 functions are intended to work with the individual component parts of a URI; they

assume that any reserved code units represent text and so must be encoded so that they are not interpreted as
reserved code units when the component is part of a complete URI.

The following lexical grammar specifies the form of encoded URIs.

uri

 

:::

uriCharacters

opt

uriCharacters

 

:::

uriCharacter

 

uriCharacters

opt

uriCharacter

 

:::

uriReserved
uriUnescaped
uriEscaped

uriReserved

 

:::

 

one of

;

 

/

 

?

 

:

 

@

 

&

 

=

 

+

 

$

 

,

uriUnescaped

 

:::

uriAlpha
DecimalDigit
uriMark

uriEscaped

 

:::

%

 

HexDigit

 

HexDigit

uriAlpha

 

:::

 

one of

a

 

b

 

c

 

d

 

e

 

f

 

g

 

h

 

i

 

j

 

k

 

l

 

m

 

n

 

o

 

p

 

q

 

r

 

s

 

t

 

u

 

v

 

w

 

x

 

y

 

z

 

A

 

B

 

C

 

D

 

E

 

F

 

G

 

H

 

I

 

J

 

K

 

L

 

M

 

N

 

O

 

P

 

Q

 

R

 

S

 

T

 

U

 

V

 

W

 

X

 

Y

 

Z

uriMark

 

:::

 

one of

-

 

_

 

.

 

!

 

~

 

*

 

'

 

(

 

)

NOTE

When a code unit to be included in a URI is not listed above or is not intended to have the special meaning sometimes
given to the reserved code units, that code unit must be encoded. The code unit is transformed into its UTF-8
encoding, with 

surrogate pairs

 first converted from UTF-16 to the corresponding code point value. (Note that for code

The above syntax is based upon RFC 2396 and does not reflect changes introduced by the more
recent RFC 3986.

Syntax

Runtime Semantics

471

units in the range [0, 127] this results in a single octet with the same value.) The resulting sequence of octets is then
transformed into a String with each octet represented by an escape sequence of the form 

"%xx"

.

The abstract operation Encode takes arguments 

string

 (a String) and 

unescapedSet

 (a String). It performs URI encoding

and escaping. It performs the following steps when called:

1.  Let 

strLen

 be the number of code units in 

string

.

2.  Let 

R

 be the empty String.

3.  Let 

k

 be 0.

4.  Repeat,

a.  If 

k

 = 

strLen

, return 

R

.

b.  Let 

C

 be the code unit at index 

k

 within 

string

.

c.  If 

C

 is in 

unescapedSet

, then

i.  Set 

k

 to 

k

 + 1.

ii.  Set 

R

 to the 

string-concatenation

 of 

R

 and 

C

.

d.  Else,

i.  Let 

cp

 be ! 

CodePointAt

(

string

k

).

ii.  If 

cp

.[[IsUnpairedSurrogate]] is 

true

, throw a 

URIError

 exception.

iii.  Set 

k

 to 

k

 + 

cp

.[[CodeUnitCount]].

iv.  Let 

Octets

 be the 

List

 of octets resulting by applying the UTF-8 transformation to 

cp

.

[[CodePoint]].

v.  For each element 

octet

 of 

Octets

, do

1.  Set 

R

 to the 

string-concatenation

 of:

R

"%"

the String representation of 

octet

, formatted as a two-digit uppercase hexadecimal

number, padded to the left with a zero if necessary

The abstract operation Decode takes arguments 

string

 (a String) and 

reservedSet

 (a String). It performs URI unescaping

and decoding. It performs the following steps when called:

1.  Let 

strLen

 be the length of 

string

.

2.  Let 

R

 be the empty String.

3.  Let 

k

 be 0.

4.  Repeat,

a.  If 

k

 = 

strLen

, return 

R

.

b.  Let 

C

 be the code unit at index 

k

 within 

string

.

c.  If 

C

 is not the code unit 0x0025 (PERCENT SIGN), then

i.  Let 

S

 be the String value containing only the code unit 

C

.

d.  Else,

i.  Let 

start

 be 

k

.

ii.  If 

k

 + 2 

 

strLen

, throw a 

URIError

 exception.

iii.  If the code units at index (

k

 + 1) and (

k

 + 2) within 

string

 do not represent hexadecimal digits,

throw a 

URIError

 exception.

iv.  Let 

B

 be the 8-bit value represented by the two hexadecimal digits at index (

k

 + 1) and (

k

 + 2).

19.2.6.1.1  Encode ( 

string

unescapedSet

 )

19.2.6.1.2  Decode ( 

string

reservedSet

 )

472

v.  Set 

k

 to 

k

 + 2.

vi.  Let 

n

 be the number of leading 1 bits in 

B

.

vii.  If 

n

 = 0, then

1.  Let 

C

 be the code unit whose value is 

B

.

2.  If 

C

 is not in 

reservedSet

, then

a.  Let 

S

 be the String value containing only the code unit 

C

.

3.  Else,

a.  Let 

S

 be the 

substring

 of 

string

 from 

start

 to 

k

 + 1.

viii.  Else,

1.  If 

n

 = 1 or 

n

 > 4, throw a 

URIError

 exception.

2.  If 

k

 + (3 

×

 (

n

 - 1)) 

 

strLen

, throw a 

URIError

 exception.

3.  Let 

Octets

 be a 

List

 whose sole element is 

B

.

4.  Let 

j

 be 1.

5.  Repeat, while 

j

 < 

n

,

a.  Set 

k

 to 

k

 + 1.

b.  If the code unit at index 

k

 within 

string

 is not the code unit 0x0025 (PERCENT

SIGN), throw a 

URIError

 exception.

c.  If the code units at index (

k

 + 1) and (

k

 + 2) within 

string

 do not represent

hexadecimal digits, throw a 

URIError

 exception.

d.  Let 

B

 be the 8-bit value represented by the two hexadecimal digits at index (

k

 + 1)

and (

k

 + 2).

e.  Set 

k

 to 

k

 + 2.

f.  Append 

B

 to 

Octets

.

g.  Set 

j

 to 

j

 + 1.

6. 

Assert

: The length of 

Octets

 is 

n

.

7.  If 

Octets

 does not contain a valid UTF-8 encoding of a Unicode code point, throw a

URIError

 exception.

8.  Let 

V

 be the code point obtained by applying the UTF-8 transformation to 

Octets

, that is,

from a 

List

 of octets into a 21-bit value.

9.  Let 

S

 be 

UTF16EncodeCodePoint

(

V

).

e.  Set 

R

 to the 

string-concatenation

 of 

R

 and 

S

.

f.  Set 

k

 to 

k

 + 1.

NOTE

This syntax of Uniform Resource Identifiers is based upon RFC 2396 and does not reflect the
more recent RFC 3986 which replaces RFC 2396. A formal description and implementation of
UTF-8 is given in RFC 3629.

In UTF-8, characters are encoded using sequences of 1 to 6 octets. The only octet of a sequence of
one has the higher-order bit set to 0, the remaining 7 bits being used to encode the character
value. In a sequence of n octets, n > 1, the initial octet has the n higher-order bits set to 1,
followed by a bit set to 0. The remaining bits of that octet contain bits from the value of the
character to be encoded. The following octets all have the higher-order bit set to 1 and the
following bit set to 0, leaving 6 bits in each to contain bits from the character to be encoded. The
possible UTF-8 encodings of ECMAScript characters are specified in 

Table 49

.

473

The 

decodeURI

decodeURI

 function computes a new version of a URI in which each escape sequence and UTF-8 encoding of

the sort that might be introduced by the 

encodeURI

encodeURI

 function is replaced with the UTF-16 encoding of the code

points that it represents. Escape sequences that could not have been introduced by 

encodeURI

encodeURI

 are not replaced.

Table 49 (Informative): UTF-8 Encodings

Code Unit

Value

Representation

1

st

 Octet

2

nd

 Octet

3

rd

 Octet

4

th

 Octet

0x0000 -

0x0000 -

0x007F

0x007F

00000000

00000000

00

zzzzzzz

zzzzzzz

00

zzzzzzz

zzzzzzz

0x0080 -

0x0080 -

0x07FF

0x07FF

00000

00000

yyy

yyy

yyzzzzzz

yyzzzzzz

110

110

yyyyy

yyyyy

10

10

zzzzzz

zzzzzz

0x0800 -

0x0800 -

0xD7FF

0xD7FF

xxxxyyyy

xxxxyyyy

yyzzzzzz

yyzzzzzz

1110

1110

xxxx

xxxx

10

10

yyyyyy

yyyyyy

10

10

zzzzzz

zzzzzz

0xD800 -

0xD800 -

0xDBFF

0xDBFF

 

followed by 

0xDC00 -

0xDC00 -

0xDFFF

0xDFFF

110110

110110

vv

vv

vvwwwwxx

vvwwwwxx

 

followed by 

110111

110111

yy

yy

yyzzzzzz

yyzzzzzz

11110

11110

uuu

uuu

10

10

uuwwww

uuwwww

10

10

xxyyyy

xxyyyy

10

10

zzzzzz

zzzzzz

0xD800 -

0xD800 -

0xDBFF

0xDBFF

 

not followed by

0xDC00 -

0xDC00 -

0xDFFF

0xDFFF

causes 

URIError

URIError

0xDC00 -

0xDC00 -

0xDFFF

0xDFFF

causes 

URIError

URIError

0xE000 -

0xE000 -

0xFFFF

0xFFFF

xxxxyyyy

xxxxyyyy

yyzzzzzz

yyzzzzzz

1110

1110

xxxx

xxxx

10

10

yyyyyy

yyyyyy

10

10

zzzzzz

zzzzzz

Where 

uuuuu

 = 

vvvv

 + 1 

to account for the addition of 0x10000 as in section 3.8 of the Unicode Standard (Surrogates).

The above transformation combines each 

surrogate pair

 (for which code unit values in the

inclusive range 0xD800 to 0xDFFF are reserved) into a UTF-32 representation and encodes the
resulting 21-bit value into UTF-8. Decoding reconstructs the 

surrogate pair

.

RFC 3629 prohibits the decoding of invalid UTF-8 octet sequences. For example, the invalid
sequence C0 80 must not decode into the code unit 0x0000. Implementations of the Decode
algorithm are required to throw a 

URIError

 when encountering such invalid sequences.

19.2.6.2  decodeURI ( 

encodedURI

 )

474

The 

decodeURI

decodeURI

 function is the 

%decodeURI%

 intrinsic object. When the 

decodeURI

decodeURI

 function is called with one

argument 

encodedURI

, the following steps are taken:

1.  Let 

uriString

 be ? 

ToString

(

encodedURI

).

2.  Let 

reservedURISet

 be a String containing one instance of each code unit valid in 

uriReserved

 plus 

"#"

.

3.  Return ? 

Decode

(

uriString

reservedURISet

).

NOTE

The 

decodeURIComponent

decodeURIComponent

 function computes a new version of a URI in which each escape sequence and UTF-8

encoding of the sort that might be introduced by the 

encodeURIComponent

encodeURIComponent

 function is replaced with the UTF-16

encoding of the code points that it represents.

The 

decodeURIComponent

decodeURIComponent

 function is the 

%decodeURIComponent%

 intrinsic object. When the

decodeURIComponent

decodeURIComponent

 function is called with one argument 

encodedURIComponent

, the following steps are taken:

1.  Let 

componentString

 be ? 

ToString

(

encodedURIComponent

).

2.  Let 

reservedURIComponentSet

 be the empty String.

3.  Return ? 

Decode

(

componentString

reservedURIComponentSet

).

The 

encodeURI

encodeURI

 function computes a new version of a UTF-16 encoded (

6.1.4

) URI in which each instance of certain

code points is replaced by one, two, three, or four escape sequences representing the UTF-8 encoding of the code
points.

The 

encodeURI

encodeURI

 function is the 

%encodeURI%

 intrinsic object. When the 

encodeURI

encodeURI

 function is called with one

argument 

uri

, the following steps are taken:

1.  Let 

uriString

 be ? 

ToString

(

uri

).

2.  Let 

unescapedURISet

 be a String containing one instance of each code unit valid in 

uriReserved

 and 

uriUnescaped

plus 

"#"

.

3.  Return ? 

Encode

(

uriString

unescapedURISet

).

NOTE

The 

encodeURIComponent

encodeURIComponent

 function computes a new version of a UTF-16 encoded (

6.1.4

) URI in which each

instance of certain code points is replaced by one, two, three, or four escape sequences representing the UTF-8
encoding of the code point.

The 

encodeURIComponent

encodeURIComponent

 function is the 

%encodeURIComponent%

 intrinsic object. When the

encodeURIComponent

encodeURIComponent

 function is called with one argument 

uriComponent

, the following steps are taken:

The code point 

##

 is not decoded from escape sequences even though it is not a reserved URI code

point.

The code point 

##

 is not encoded to an escape sequence even though it is not a reserved or

unescaped URI code point.

19.2.6.3  decodeURIComponent ( 

encodedURIComponent

 )

19.2.6.4  encodeURI ( 

uri

 )

19.2.6.5  encodeURIComponent ( 

uriComponent

 )

475

1.  Let 

componentString

 be ? 

ToString

(

uriComponent

).

2.  Let 

unescapedURIComponentSet

 be a String containing one instance of each code unit valid in 

uriUnescaped

.

3.  Return ? 

Encode

(

componentString

unescapedURIComponentSet

).

See 

23.1.1

.

See 

25.1.3

.

See 

21.2.1

.

See 

23.2.5

.

See 

23.2.5

.

See 

20.3.1

.

See 

25.3.2

.

See 

21.4.2

.

See 

20.5.1

.

See 

20.5.5.1

.

19.3  Constructor Properties of the Global Object

19.3.1  Array ( . . . )

19.3.2  ArrayBuffer ( . . . )

19.3.3  BigInt ( . . . )

19.3.4  BigInt64Array ( . . . )

19.3.5  BigUint64Array ( . . . )

19.3.6  Boolean ( . . . )

19.3.7  DataView ( . . . )

19.3.8  Date ( . . . )

19.3.9  Error ( . . . )

19.3.10  EvalError ( . . . )

476

See 

26.2.1

.

See 

23.2.5

.

See 

23.2.5

.

See 

20.2.1

.

See 

23.2.5

.

See 

23.2.5

.

See 

23.2.5

.

See 

24.1.1

.

See 

21.1.1

.

See 

20.1.1

.

See 

27.2.3

.

19.3.11  FinalizationRegistry ( . . . )

19.3.12  Float32Array ( . . . )

19.3.13  Float64Array ( . . . )

19.3.14  Function ( . . . )

19.3.15  Int8Array ( . . . )

19.3.16  Int16Array ( . . . )

19.3.17  Int32Array ( . . . )

19.3.18  Map ( . . . )

19.3.19  Number ( . . . )

19.3.20  Object ( . . . )

19.3.21  Promise ( . . . )

19.3.22  Proxy ( . . . )

477

See 

28.2.1

.

See 

20.5.5.2

.

See 

20.5.5.3

.

See 

22.2.3

.

See 

24.2.1

.

See 

25.2.2

.

See 

22.1.1

.

See 

20.4.1

.

See 

20.5.5.4

.

See 

20.5.5.5

.

See 

23.2.5

.

See 

23.2.5

.

19.3.23  RangeError ( . . . )

19.3.24  ReferenceError ( . . . )

19.3.25  RegExp ( . . . )

19.3.26  Set ( . . . )

19.3.27  SharedArrayBuffer ( . . . )

19.3.28  String ( . . . )

19.3.29  Symbol ( . . . )

19.3.30  SyntaxError ( . . . )

19.3.31  TypeError ( . . . )

19.3.32  Uint8Array ( . . . )

19.3.33  Uint8ClampedArray ( . . . )

478

See 

23.2.5

.

See 

23.2.5

.

See 

20.5.5.6

.

See 

24.3.1

.

See 

26.1.1

.

See 

24.4

.

See 

25.4

.

See 

25.5

.

See 

21.3

.

See 

28.1

.

19.3.34  Uint16Array ( . . . )

19.3.35  Uint32Array ( . . . )

19.3.36  URIError ( . . . )

19.3.37  WeakMap ( . . . )

19.3.38  WeakRef ( . . . )

19.3.39  WeakSet ( . . . )

19.4  Other Properties of the Global Object

19.4.1  Atomics

19.4.2  JSON

19.4.3  Math

19.4.4  Reflect

20  Fundamental Objects

479

 

 

 

 

 

 

 

Content      ..     27      28      29      30     ..