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

 

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

 

Search            copyright infringement  

 

 

 

 

 

 

 

 

 

 

 

Content      ..     25      26      27      28     ..

 

 

 

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

 

 

a.  Let 

fn

 be the sole element of the 

BoundNames

 of 

f

.

b.  Let 

fo

 be 

InstantiateFunctionObject

 of 

f

 with argument 

env

.

c.  Perform ? 

env

.CreateGlobalFunctionBinding(

fn

fo

false

).

17.  For each String 

vn

 of 

declaredVarNames

, do

a.  Perform ? 

env

.CreateGlobalVarBinding(

vn

false

).

18.  Return 

NormalCompletion

(

empty

).

NOTE 2

Module

 

:

ModuleBody

opt

ModuleBody

 

:

ModuleItemList

ModuleItemList

 

:

ModuleItem
ModuleItemList

 

ModuleItem

ModuleItem

 

:

ImportDeclaration
ExportDeclaration
StatementListItem

[~Yield, ~Await, ~Return]

ModuleBody

 

:

 

ModuleItemList

It is a Syntax Error if the 

LexicallyDeclaredNames

 of 

ModuleItemList

 contains any duplicate entries.

It is a Syntax Error if any element of the 

LexicallyDeclaredNames

 of 

ModuleItemList

 also occurs in the

VarDeclaredNames

 of 

ModuleItemList

.

Early errors specified in 

16.1.1

 prevent name conflicts between function/var declarations and

let/const/class declarations as well as redeclaration of let/const/class bindings for declaration
contained within a single 

Script

. However, such conflicts and redeclarations that span more than

one 

Script

 are detected as runtime errors during GlobalDeclarationInstantiation. If any such

errors are detected, no bindings are instantiated for the script. However, if the 

global object

 is

defined using Proxy exotic objects then the runtime tests for conflicting declarations may be
unreliable resulting in an 

abrupt completion

 and some global declarations not being instantiated.

If this occurs, the code for the 

Script

 is not evaluated.

Unlike explicit var or function declarations, properties that are directly created on the 

global

object

 result in global bindings that may be shadowed by let/const/class declarations.

16.2  Modules

Syntax

16.2.1  Module Semantics

16.2.1.1  Static Semantics: Early Errors

432

It is a Syntax Error if the 

ExportedNames

 of 

ModuleItemList

 contains any duplicate entries.

It is a Syntax Error if any element of the 

ExportedBindings

 of 

ModuleItemList

 does not also occur in either the

VarDeclaredNames

 of 

ModuleItemList

, or the 

LexicallyDeclaredNames

 of 

ModuleItemList

.

It is a Syntax Error if 

ModuleItemList

 

Contains

 

super

super

.

It is a Syntax Error if 

ModuleItemList

 

Contains

 

NewTarget

.

It is a Syntax Error if 

ContainsDuplicateLabels

 of 

ModuleItemList

 with argument « » is 

true

.

It is a Syntax Error if 

ContainsUndefinedBreakTarget

 of 

ModuleItemList

 with argument « » is 

true

.

It is a Syntax Error if 

ContainsUndefinedContinueTarget

 of 

ModuleItemList

 with arguments « » and « » is 

true

.

NOTE

The abstract operation ImportedLocalNames takes argument 

importEntries

 (a 

List

 of ImportEntry Records (see 

Table

45

)). It creates a 

List

 of all of the local name bindings defined by 

importEntries

. It performs the following steps when

called:

1.  Let 

localNames

 be a new empty 

List

.

2.  For each 

ImportEntry Record

 

i

 of 

importEntries

, do

a.  Append 

i

.[[LocalName]] to 

localNames

.

3.  Return 

localNames

.

Module

 

:

  [empty]

1.  Return a new empty 

List

.

ModuleItemList

 

:

 

ModuleItem

1.  Return 

ModuleRequests

 of 

ModuleItem

.

ModuleItemList

 

:

 

ModuleItemList

 

ModuleItem

1.  Let 

moduleNames

 be 

ModuleRequests

 of 

ModuleItemList

.

2.  Let 

additionalNames

 be 

ModuleRequests

 of 

ModuleItem

.

3.  Append to 

moduleNames

 each element of 

additionalNames

 that is not already an element of 

moduleNames

.

4.  Return 

moduleNames

.

ModuleItem

 

:

 

StatementListItem

1.  Return a new empty 

List

.

ImportDeclaration

 

:

 

import

 

ImportClause

 

FromClause

 

;

1.  Return 

ModuleRequests

 of 

FromClause

.

ModuleSpecifier

 

:

 

StringLiteral

The duplicate 

ExportedNames

 rule implies that multiple 

export default

export default

 

ExportDeclaration

items within a 

ModuleBody

 is a Syntax Error. Additional error conditions relating to conflicting or

duplicate declarations are checked during module linking prior to evaluation of a 

Module

. If any

such errors are detected the 

Module

 is not evaluated.

16.2.1.2  Static Semantics: ImportedLocalNames ( 

importEntries

 )

16.2.1.3  Static Semantics: ModuleRequests

433

1.  Return a 

List

 whose sole element is the 

SV

 of 

StringLiteral

.

ExportDeclaration

 

:

 

export

 

ExportFromClause

 

FromClause

 

;

1.  Return the 

ModuleRequests

 of 

FromClause

.

ExportDeclaration

 

:

export

 

NamedExports

 

;

export

 

VariableStatement

export

 

Declaration

export

 

default

 

HoistableDeclaration

export

 

default

 

ClassDeclaration

export

 

default

 

AssignmentExpression

 

;

1.  Return a new empty 

List

.

Module Record

 encapsulates structural information about the imports and exports of a single module. This

information is used to link the imports and exports of sets of connected modules. A Module Record includes four
fields that are only used when evaluating a module.

For specification purposes Module Record values are values of the 

Record

 specification type and can be thought of as

existing in a simple object-oriented hierarchy where Module Record is an abstract class with both abstract and
concrete subclasses. This specification defines the abstract subclass named 

Cyclic Module Record

 and its concrete

subclass named 

Source Text Module Record

. Other specifications and implementations may define additional Module

Record subclasses corresponding to alternative module definition facilities that they defined.

Module Record defines the fields listed in 

Table 40

. All Module Definition subclasses include at least those fields.

Module Record also defines the abstract method list in 

Table 41

. All Module definition subclasses must provide

concrete implementations of these abstract methods.

Table 40: 

Module Record

 Fields

Field Name

Value Type

Meaning

[[Realm]]

Realm Record

 |

undefined

The 

Realm

 within which this module was created. 

undefined

 if not yet

assigned.

[[Environment]]

module Environment
Record

 | 

undefined

The 

Environment Record

 containing the top level bindings for this

module. This field is set when the module is linked.

[[Namespace]]

Object | 

undefined

The Module Namespace Object (

28.3

) if one has been created for this

module. Otherwise 

undefined

.

[[HostDefined]] Any, default value is

undefined

.

Field reserved for use by 

host

 environments that need to associate

additional information with a module.

16.2.1.4  Abstract Module Records

434

Table 41: Abstract Methods of Module Records

Method

Purpose

GetExportedNames([

exportStarSet

]) Return a list of all names that are either directly or indirectly exported from

this module.

ResolveExport(

exportName

 [,

resolveSet

])

Return the binding of a name exported by this module. Bindings are
represented by a 

ResolvedBinding Record

, of the form { [[Module]]: 

Module

Record

, [[BindingName]]: String }. If the export is a Module Namespace Object

without a direct binding in any module, [[BindingName]] will be set to

"*namespace*"

. Return 

null

 if the name cannot be resolved, or 

"ambiguous"

 if

multiple bindings were found.

Each time this operation is called with a specific 

exportName

resolveSet

 pair as

arguments it must return the same result if it completes normally.

Link()

Prepare the module for evaluation by transitively resolving all module
dependencies and creating a 

module Environment Record

.

Evaluate()

If this module has already been evaluated successfully, return 

undefined

; if it

has already been evaluated unsuccessfully, throw the exception that was
produced. Otherwise, transitively evaluate all module dependencies of this
module and then evaluate this module.

Link must have completed successfully prior to invoking this method.

Cyclic Module Record

 is used to represent information about a module that can participate in dependency cycles with

other modules that are subclasses of the 

Cyclic Module Record

 type. Module Records that are not subclasses of the

Cyclic Module Record

 type must not participate in dependency cycles with Source Text Module Records.

In addition to the fields defined in 

Table 40

 Cyclic Module Records have the additional fields listed in 

Table 42

16.2.1.5  Cyclic Module Records

435

Table 42: Additional Fields of Cyclic Module Records

Field Name

Value Type

Meaning

[[Status]]

unlinked

 |

linking

 | 

linked

evaluating

 |

evaluated

Initially 

unlinked

. Transitions to 

linking

linked

evaluating

evaluated

 (in

that order) as the module progresses throughout its lifecycle.

[[EvaluationError]]

An 

abrupt

completion

 |

undefined

A completion of type 

throw

 representing the exception that occurred

during evaluation. 

undefined

 if no exception occurred or if [[Status]] is not

evaluated

.

[[DFSIndex]]

Integer

 |

undefined

Auxiliary field used during Link and Evaluate only. If [[Status]] is 

linking

or 

evaluating

, this non-negative number records the point at which the

module was first visited during the ongoing depth-first traversal of the
dependency graph.

[[DFSAncestorIndex]]

Integer

 |

undefined

Auxiliary field used during Link and Evaluate only. If [[Status]] is 

linking

or 

evaluating

, this is either the module's own [[DFSIndex]] or that of an

"earlier" module in the same strongly connected component.

[[RequestedModules]]

List

 of String

List

 of all the 

ModuleSpecifier

 strings used by the module represented by

this record to request the importation of a module. The 

List

 is source code

occurrence ordered.

In addition to the methods defined in 

Table 41

 Cyclic Module Records have the additional methods listed in 

Table 43

Table 43: Additional Abstract Methods of Cyclic Module Records

Method

Purpose

InitializeEnvironment

() Initialize the 

Environment Record

 of the module, including resolving all imported

bindings, and create the module's 

execution context

.

ExecuteModule

()

Evaluate the module's code within its 

execution context

.

The Link concrete method of a 

Cyclic Module Record

 

module

 takes no arguments. On success, Link transitions this

module's [[Status]] from 

unlinked

 to 

linked

. On failure, an exception is thrown and this module's [[Status]] remains

unlinked

. (Most of the work is done by the auxiliary function 

InnerModuleLinking

.) It performs the following steps

when called:

1. 

Assert

module

.[[Status]] is not 

linking

 or 

evaluating

.

2.  Let 

stack

 be a new empty 

List

.

3.  Let 

result

 be 

InnerModuleLinking

(

module

stack

, 0).

4.  If 

result

 is an 

abrupt completion

, then

a.  For each 

Cyclic Module Record

 

m

 of 

stack

, do

16.2.1.5.1  Link ( ) Concrete Method

436

i. 

Assert

m

.[[Status]] is 

linking

.

ii.  Set 

m

.[[Status]] to 

unlinked

.

iii.  Set 

m

.[[Environment]] to 

undefined

.

iv.  Set 

m

.[[DFSIndex]] to 

undefined

.

v.  Set 

m

.[[DFSAncestorIndex]] to 

undefined

.

b. 

Assert

module

.[[Status]] is 

unlinked

.

c.  Return 

result

.

5. 

Assert

module

.[[Status]] is 

linked

 or 

evaluated

.

6. 

Assert

stack

 is empty.

7.  Return 

undefined

.

The abstract operation InnerModuleLinking takes arguments 

module

 (a 

Cyclic Module Record

), 

stack

, and 

index

 (a non-

negative 

integer

). It is used by Link to perform the actual linking process for 

module

, as well as recursively on all other

modules in the dependency graph. The 

stack

 and 

index

 parameters, as well as a module's [[DFSIndex]] and

[[DFSAncestorIndex]] fields, keep track of the depth-first search (DFS) traversal. In particular, [[DFSAncestorIndex]] is
used to discover strongly connected components (SCCs), such that all modules in an SCC transition to 

linked

 together.

It performs the following steps when called:

1.  If 

module

 is not a 

Cyclic Module Record

, then

a.  Perform ? 

module

.Link().

b.  Return 

index

.

2.  If 

module

.[[Status]] is 

linking

linked

, or 

evaluated

, then

a.  Return 

index

.

3. 

Assert

module

.[[Status]] is 

unlinked

.

4.  Set 

module

.[[Status]] to 

linking

.

5.  Set 

module

.[[DFSIndex]] to 

index

.

6.  Set 

module

.[[DFSAncestorIndex]] to 

index

.

7.  Set 

index

 to 

index

 + 1.

8.  Append 

module

 to 

stack

.

9.  For each String 

required

 of 

module

.[[RequestedModules]], do

a.  Let 

requiredModule

 be ? 

HostResolveImportedModule

(

module

required

).

b.  Set 

index

 to ? 

InnerModuleLinking

(

requiredModule

stack

index

).

c.  If 

requiredModule

 is a 

Cyclic Module Record

, then

i. 

Assert

requiredModule

.[[Status]] is either 

linking

linked

, or 

evaluated

.

ii. 

Assert

requiredModule

.[[Status]] is 

linking

 if and only if 

requiredModule

 is in 

stack

.

iii.  If 

requiredModule

.[[Status]] is 

linking

, then

1.  Set 

module

.[[DFSAncestorIndex]] to 

min

(

module

.[[DFSAncestorIndex]], 

requiredModule

.

[[DFSAncestorIndex]]).

10.  Perform ? 

module

.

InitializeEnvironment

().

11. 

Assert

module

 occurs exactly once in 

stack

.

12. 

Assert

module

.[[DFSAncestorIndex]] 

 

module

.[[DFSIndex]].

13.  If 

module

.[[DFSAncestorIndex]] = 

module

.[[DFSIndex]], then

a.  Let 

done

 be 

false

.

b.  Repeat, while 

done

 is 

false

,

i.  Let 

requiredModule

 be the last element in 

stack

.

ii.  Remove the last element of 

stack

.

16.2.1.5.1.1  InnerModuleLinking ( 

module

stack

index

 )

437

iii. 

Assert

requiredModule

 is a 

Cyclic Module Record

.

iv.  Set 

requiredModule

.[[Status]] to 

linked

.

v.  If 

requiredModule

 and 

module

 are the same 

Module Record

, set 

done

 to 

true

.

14.  Return 

index

.

The Evaluate concrete method of a 

Cyclic Module Record

 

module

 takes no arguments. Evaluate transitions this

module's [[Status]] from 

linked

 to 

evaluated

. If execution results in an exception, that exception is recorded in the

[[EvaluationError]] field and rethrown by future invocations of Evaluate. (Most of the work is done by the auxiliary
function 

InnerModuleEvaluation

.) It performs the following steps when called:

1. 

Assert

: This call to Evaluate is not happening at the same time as another call to Evaluate within the

surrounding agent

.

2. 

Assert

module

.[[Status]] is 

linked

 or 

evaluated

.

3.  Let 

stack

 be a new empty 

List

.

4.  Let 

result

 be 

InnerModuleEvaluation

(

module

stack

, 0).

5.  If 

result

 is an 

abrupt completion

, then

a.  For each 

Cyclic Module Record

 

m

 of 

stack

, do

i. 

Assert

m

.[[Status]] is 

evaluating

.

ii.  Set 

m

.[[Status]] to 

evaluated

.

iii.  Set 

m

.[[EvaluationError]] to 

result

.

b. 

Assert

module

.[[Status]] is 

evaluated

 and 

module

.[[EvaluationError]] is 

result

.

c.  Return 

result

.

6. 

Assert

module

.[[Status]] is 

evaluated

 and 

module

.[[EvaluationError]] is 

undefined

.

7. 

Assert

stack

 is empty.

8.  Return 

undefined

.

The abstract operation InnerModuleEvaluation takes arguments 

module

 (a 

Module Record

), 

stack

, and 

index

 (a non-

negative 

integer

). It is used by Evaluate to perform the actual evaluation process for 

module

, as well as recursively on

all other modules in the dependency graph. The 

stack

 and 

index

 parameters, as well as 

module

's [[DFSIndex]] and

[[DFSAncestorIndex]] fields, are used the same way as in 

InnerModuleLinking

. It performs the following steps when

called:

1.  If 

module

 is not a 

Cyclic Module Record

, then

a.  Perform ? 

module

.Evaluate().

b.  Return 

index

.

2.  If 

module

.[[Status]] is 

evaluated

, then

a.  If 

module

.[[EvaluationError]] is 

undefined

, return 

index

.

b.  Otherwise, return 

module

.[[EvaluationError]].

3.  If 

module

.[[Status]] is 

evaluating

, return 

index

.

4. 

Assert

module

.[[Status]] is 

linked

.

5.  Set 

module

.[[Status]] to 

evaluating

.

6.  Set 

module

.[[DFSIndex]] to 

index

.

7.  Set 

module

.[[DFSAncestorIndex]] to 

index

.

8.  Set 

index

 to 

index

 + 1.

9.  Append 

module

 to 

stack

.

16.2.1.5.2  Evaluate ( ) Concrete Method

16.2.1.5.2.1  InnerModuleEvaluation ( 

module

stack

index

 )

438

10.  For each String 

required

 of 

module

.[[RequestedModules]], do

a.  Let 

requiredModule

 be ! 

HostResolveImportedModule

(

module

required

).

b.  NOTE: Link must be completed successfully prior to invoking this method, so every requested module

is guaranteed to resolve successfully.

c.  Set 

index

 to ? 

InnerModuleEvaluation

(

requiredModule

stack

index

).

d.  If 

requiredModule

 is a 

Cyclic Module Record

, then

i. 

Assert

requiredModule

.[[Status]] is either 

evaluating

 or 

evaluated

.

ii. 

Assert

requiredModule

.[[Status]] is 

evaluating

 if and only if 

requiredModule

 is in 

stack

.

iii.  If 

requiredModule

.[[Status]] is 

evaluating

, then

1.  Set 

module

.[[DFSAncestorIndex]] to 

min

(

module

.[[DFSAncestorIndex]], 

requiredModule

.

[[DFSAncestorIndex]]).

11.  Perform ? 

module

.

ExecuteModule

().

12. 

Assert

module

 occurs exactly once in 

stack

.

13. 

Assert

module

.[[DFSAncestorIndex]] 

 

module

.[[DFSIndex]].

14.  If 

module

.[[DFSAncestorIndex]] = 

module

.[[DFSIndex]], then

a.  Let 

done

 be 

false

.

b.  Repeat, while 

done

 is 

false

,

i.  Let 

requiredModule

 be the last element in 

stack

.

ii.  Remove the last element of 

stack

.

iii. 

Assert

requiredModule

 is a 

Cyclic Module Record

.

iv.  Set 

requiredModule

.[[Status]] to 

evaluated

.

v.  If 

requiredModule

 and 

module

 are the same 

Module Record

, set 

done

 to 

true

.

15.  Return 

index

.

This non-normative section gives a series of examples of the linking and evaluation of a few common module graphs,
with a specific focus on how errors can occur.

First consider the following simple module graph:

Figure 2: A simple module graph

A

B

C

Let's first assume that there are no error conditions. When a 

host

 first calls 

A

.Link(), this will complete successfully by

assumption, and recursively link modules 

B

 and 

C

 as well, such that 

A

.[[Status]] = 

B

.[[Status]] = 

C

.[[Status]] = 

linked

.

This preparatory step can be performed at any time. Later, when the 

host

 is ready to incur any possible side effects of

16.2.1.5.3  Example Cyclic Module Record Graphs

439

the modules, it can call 

A

.Evaluate(), which will complete successfully (again by assumption), recursively having

evaluated first 

C

 and then 

B

. Each module's [[Status]] at this point will be 

evaluated

.

Consider then cases involving linking errors. If 

InnerModuleLinking

 of 

C

 succeeds but, thereafter, fails for 

B

, for

example because it imports something that 

C

 does not provide, then the original 

A

.Link() will fail, and both 

A

 and 

B

's

[[Status]] remain 

unlinked

C

's [[Status]] has become 

linked

, though.

Finally, consider a case involving evaluation errors. If 

InnerModuleEvaluation

 of 

C

 succeeds but, thereafter, fails for 

B

,

for example because 

B

 contains code that throws an exception, then the original 

A

.Evaluate() will fail. The resulting

exception will be recorded in both 

A

 and 

B

's [[EvaluationError]] fields, and their [[Status]] will become 

evaluated

C

will also become 

evaluated

 but, in contrast to 

A

 and 

B

, will remain without an [[EvaluationError]], as it successfully

completed evaluation. Storing the exception ensures that any time a 

host

 tries to reuse 

A

 or 

B

 by calling their

Evaluate() method, it will encounter the same exception. (Hosts are not required to reuse Cyclic Module Records;
similarly, hosts are not required to expose the exception objects thrown by these methods. However, the specification
enables such uses.)

The difference here between linking and evaluation errors is due to how evaluation must be only performed once, as
it can cause side effects; it is thus important to remember whether evaluation has already been performed, even if
unsuccessfully. (In the error case, it makes sense to also remember the exception because otherwise subsequent
Evaluate() calls would have to synthesize a new one.) Linking, on the other hand, is side-effect-free, and thus even if it
fails, it can be retried at a later time with no issues.

Now consider a different type of error condition:

Figure 3: A module graph with an unresolvable module

A

???

In this scenario, module 

A

 declares a dependency on some other module, but no 

Module Record

 exists for that

module, i.e. 

HostResolveImportedModule

 throws an exception when asked for it. This could occur for a variety of

reasons, such as the corresponding resource not existing, or the resource existing but 

ParseModule

 throwing an

exception when trying to parse the resulting source text. Hosts can choose to expose the cause of failure via the
exception they throw from 

HostResolveImportedModule

. In any case, this exception causes a linking failure, which as

before results in 

A

's [[Status]] remaining 

unlinked

.

Lastly, consider a module graph with a cycle:

Figure 4: A cyclic module graph

A

B

C

440

Here we assume that the entry point is module 

A

, so that the 

host

 proceeds by calling 

A

.Link(), which performs

InnerModuleLinking

 on 

A

. This in turn calls 

InnerModuleLinking

 on 

B

. Because of the cycle, this again triggers

InnerModuleLinking

 on 

A

, but at this point it is a no-op since 

A

.[[Status]] is already 

linking

B

.[[Status]] itself remains

linking

 when control gets back to 

A

 and 

InnerModuleLinking

 is triggered on 

C

. After this returns with 

C

.[[Status]]

being 

linked

, both 

A

 and 

B

 transition from 

linking

 to 

linked

 together; this is by design, since they form a strongly

connected component.

An analogous story occurs for the evaluation phase of a cyclic module graph, in the success case.

Now consider a case where 

A

 has an linking error; for example, it tries to import a binding from 

C

 that does not exist.

In that case, the above steps still occur, including the early return from the second call to 

InnerModuleLinking

 on 

A

.

However, once we unwind back to the original 

InnerModuleLinking

 on 

A

, it fails during 

InitializeEnvironment

,

namely right after 

C

.ResolveExport(). The thrown 

SyntaxError

 exception propagates up to 

A

.Link, which resets all

modules that are currently on its 

stack

 (these are always exactly the modules that are still 

linking

). Hence both 

A

 and 

B

become 

unlinked

. Note that 

C

 is left as 

linked

.

Finally, consider a case where 

A

 has an evaluation error; for example, its source code throws an exception. In that case,

the evaluation-time analog of the above steps still occurs, including the early return from the second call to

InnerModuleEvaluation

 on 

A

. However, once we unwind back to the original 

InnerModuleEvaluation

 on 

A

, it fails by

assumption. The exception thrown propagates up to 

A

.Evaluate(), which records the error in all modules that are

currently on its 

stack

 (i.e., the modules that are still 

evaluating

). Hence both 

A

 and 

B

 become 

evaluated

 and the

exception is recorded in both 

A

 and 

B

's [[EvaluationError]] fields, while 

C

 is left as 

evaluated

 with no

[[EvaluationError]].

Source Text Module Record

 is used to represent information about a module that was defined from ECMAScript

source text (

11

) that was parsed using the 

goal symbol

 

Module

. Its fields contain digested information about the names

that are imported by the module and its concrete methods use this digest to link, link, and evaluate the module.

Source Text Module Record

 can exist in a module graph with other subclasses of the abstract 

Module Record

 type,

and can participate in cycles with other subclasses of the 

Cyclic Module Record

 type.

In addition to the fields defined in 

Table 42

, Source Text Module Records have the additional fields listed in 

Table 44

.

Each of these fields is initially set in 

ParseModule

.

16.2.1.6  Source Text Module Records

441

Table 44: Additional Fields of Source Text Module Records

Field Name

Value Type

Meaning

[[ECMAScriptCode]]

Parse

Node

The result of parsing the source text of this module using 

Module

 as the

goal symbol

.

[[Context]]

An
ECMAScript

execution
context

.

The 

execution context

 associated with this module.

[[ImportMeta]]

Object

An object exposed through the 

import.meta

import.meta

 meta property. It is 

empty

until it is accessed by ECMAScript code.

[[ImportEntries]]

List

 of

ImportEntry
Records

List

 of ImportEntry records derived from the code of this module.

[[LocalExportEntries]]

List

 of

ExportEntry
Records

List

 of ExportEntry records derived from the code of this module that

correspond to declarations that occur within the module.

[[IndirectExportEntries]]

List

 of

ExportEntry
Records

List

 of ExportEntry records derived from the code of this module that

correspond to reexported imports that occur within the module or exports
from 

export * as namespace

export * as namespace

 declarations.

[[StarExportEntries]]

List

 of

ExportEntry
Records

List

 of ExportEntry records derived from the code of this module that

correspond to 

export *

export *

 declarations that occur within the module, not

including 

export * as namespace

export * as namespace

 declarations.

An 

ImportEntry Record

 is a 

Record

 that digests information about a single declarative import. Each 

ImportEntry

Record

 has the fields defined in 

Table 45

:

Table 45: 

ImportEntry Record

 Fields

Field Name

Value

Type

Meaning

[[ModuleRequest]] String String value of the 

ModuleSpecifier

 of the 

ImportDeclaration

.

[[ImportName]]

String The name under which the desired binding is exported by the module identified by

[[ModuleRequest]]. The value 

"*"

 indicates that the import request is for the target

module's namespace object.

[[LocalName]]

String The name that is used to locally access the imported value from within the importing

module.

442

NOTE 1

An 

ExportEntry Record

 is a 

Record

 that digests information about a single declarative export. Each 

ExportEntry Record

has the fields defined in 

Table 47

:

Table 47: 

ExportEntry Record

 Fields

Field Name

Value

Type

Meaning

[[ExportName]]

String
| null

The name used to export this binding by this module.

[[ModuleRequest]] String

| null

The String value of the 

ModuleSpecifier

 of the 

ExportDeclaration

null

 if the 

ExportDeclaration

 does not have a 

ModuleSpecifier

.

[[ImportName]]

String
| null

The name under which the desired binding is exported by the module identified by
[[ModuleRequest]]. 

null

 if the 

ExportDeclaration

 does not have a 

ModuleSpecifier

"*"

indicates that the export request is for all exported bindings.

[[LocalName]]

String
| null

The name that is used to locally access the exported value from within the importing
module. 

null

 if the exported value is not locally accessible from within the module.

Table 46

 gives examples of ImportEntry records fields used to represent the syntactic import

forms:

Table 46 (Informative): Import Forms Mappings to ImportEntry Records

Import Statement Form

[[ModuleRequest]] [[ImportName]] [[LocalName]]

import v from "mod";

import v from "mod";

"mod"

"default"

"v"

import * as ns from

import * as ns from

"mod";

"mod";

"mod"

"*"

"ns"

import {x} from "mod";

import {x} from "mod";

"mod"

"x"

"x"

import {x as v} from

import {x as v} from

"mod";

"mod";

"mod"

"x"

"v"

import "mod";

import "mod";

An 

ImportEntry Record

 is not created.

443

NOTE 2

The following definitions specify the required concrete methods and other 

abstract operations

 for Source Text Module

Records

The abstract operation ParseModule takes arguments 

sourceText

 (ECMAScript source text), 

realm

, and 

hostDefined

. It

creates a 

Source Text Module Record

 based upon the result of parsing 

sourceText

 as a 

Module

. It performs the following

Table 48

 gives examples of the ExportEntry record fields used to represent the syntactic export

forms:

Table 48 (Informative): Export Forms Mappings to ExportEntry Records

Export Statement

Form

[[ExportName]] [[ModuleRequest]] [[ImportName]] [[LocalName]]

export var v;

export var v;

"v"

null

null

"v"

export

export

default

default

function f()

function f()

{}

{}

"default"

null

null

"f"

export

export

default

default

function ()

function ()

{}

{}

"default"

null

null

"*default*"

export

export

default 42;

default 42;

"default"

null

null

"*default*"

export {x};

export {x};

"x"

null

null

"x"

export {v as

export {v as

x};

x};

"x"

null

null

"v"

export {x}

export {x}

from "mod";

from "mod";

"x"

"mod"

"x"

null

export {v as

export {v as

x} from

x} from

"mod";

"mod";

"x"

"mod"

"v"

null

export * from

export * from

"mod";

"mod";

null

"mod"

"*"

null

export * as

export * as

ns from

ns from

"mod";

"mod";

"ns"

"mod"

"*"

null

16.2.1.6.1  ParseModule ( 

sourceText

realm

hostDefined

 )

444

steps when called:

1. 

Assert

sourceText

 is an ECMAScript source text (see clause 

11

).

2.  Let 

body

 be 

ParseText

(

sourceText

Module

).

3.  If 

body

 is a 

List

 of errors, return 

body

.

4.  Let 

requestedModules

 be the 

ModuleRequests

 of 

body

.

5.  Let 

importEntries

 be 

ImportEntries

 of 

body

.

6.  Let 

importedBoundNames

 be 

ImportedLocalNames

(

importEntries

).

7.  Let 

indirectExportEntries

 be a new empty 

List

.

8.  Let 

localExportEntries

 be a new empty 

List

.

9.  Let 

starExportEntries

 be a new empty 

List

.

10.  Let 

exportEntries

 be 

ExportEntries

 of 

body

.

11.  For each 

ExportEntry Record

 

ee

 of 

exportEntries

, do

a.  If 

ee

.[[ModuleRequest]] is 

null

, then

i.  If 

ee

.[[LocalName]] is not an element of 

importedBoundNames

, then

1.  Append 

ee

 to 

localExportEntries

.

ii.  Else,

1.  Let 

ie

 be the element of 

importEntries

 whose [[LocalName]] is the same as 

ee

.

[[LocalName]].

2.  If 

ie

.[[ImportName]] is 

"*"

, then

a.  NOTE: This is a re-export of an imported module namespace object.

b.  Append 

ee

 to 

localExportEntries

.

3.  Else,

a.  NOTE: This is a re-export of a single name.

b.  Append the 

ExportEntry Record

 { [[ModuleRequest]]: 

ie

.[[ModuleRequest]],

[[ImportName]]: 

ie

.[[ImportName]], [[LocalName]]: 

null

, [[ExportName]]: 

ee

.

[[ExportName]] } to 

indirectExportEntries

.

b.  Else if 

ee

.[[ImportName]] is 

"*"

 and 

ee

.[[ExportName]] is 

null

, then

i.  Append 

ee

 to 

starExportEntries

.

c.  Else,

i.  Append 

ee

 to 

indirectExportEntries

.

12.  Return 

Source Text Module Record

 { [[Realm]]: 

realm

, [[Environment]]: 

undefined

, [[Namespace]]: 

undefined

,

[[Status]]: 

unlinked

, [[EvaluationError]]: 

undefined

, [[HostDefined]]: 

hostDefined

, [[ECMAScriptCode]]: 

body

,

[[Context]]: 

empty

, [[ImportMeta]]: 

empty

, [[RequestedModules]]: 

requestedModules

, [[ImportEntries]]:

importEntries

, [[LocalExportEntries]]: 

localExportEntries

, [[IndirectExportEntries]]: 

indirectExportEntries

,

[[StarExportEntries]]: 

starExportEntries

, [[DFSIndex]]: 

undefined

, [[DFSAncestorIndex]]: 

undefined

 }.

NOTE

The GetExportedNames concrete method of a 

Source Text Module Record

 

module

 takes optional argument

exportStarSet

. It performs the following steps when called:

1.  If 

exportStarSet

 is not present, set 

exportStarSet

 to a new empty 

List

.

An implementation may parse module source text and analyse it for Early Error conditions prior
to the evaluation of ParseModule for that module source text. However, the reporting of any
errors must be deferred until the point where this specification actually performs ParseModule
upon that source text.

16.2.1.6.2  GetExportedNames ( [ 

exportStarSet

 ] ) Concrete Method

445

2. 

Assert

exportStarSet

 is a 

List

 of Source Text Module Records.

3.  If 

exportStarSet

 contains 

module

, then

a. 

Assert

: We've reached the starting point of an 

export *

export *

 circularity.

b.  Return a new empty 

List

.

4.  Append 

module

 to 

exportStarSet

.

5.  Let 

exportedNames

 be a new empty 

List

.

6.  For each 

ExportEntry Record

 

e

 of 

module

.[[LocalExportEntries]], do

a. 

Assert

module

 provides the direct binding for this export.

b.  Append 

e

.[[ExportName]] to 

exportedNames

.

7.  For each 

ExportEntry Record

 

e

 of 

module

.[[IndirectExportEntries]], do

a. 

Assert

module

 imports a specific binding for this export.

b.  Append 

e

.[[ExportName]] to 

exportedNames

.

8.  For each 

ExportEntry Record

 

e

 of 

module

.[[StarExportEntries]], do

a.  Let 

requestedModule

 be ? 

HostResolveImportedModule

(

module

e

.[[ModuleRequest]]).

b.  Let 

starNames

 be ? 

requestedModule

.GetExportedNames(

exportStarSet

).

c.  For each element 

n

 of 

starNames

, do

i.  If 

SameValue

(

n

"default"

) is 

false

, then

1.  If 

n

 is not an element of 

exportedNames

, then

a.  Append 

n

 to 

exportedNames

.

9.  Return 

exportedNames

.

NOTE

The ResolveExport concrete method of a 

Source Text Module Record

 

module

 takes argument 

exportName

 (a String) and

optional argument 

resolveSet

.

ResolveExport attempts to resolve an imported binding to the actual defining module and local binding name. The
defining module may be the module represented by the 

Module Record

 this method was invoked on or some other

module that is imported by that module. The parameter 

resolveSet

 is used to detect unresolved circular import/export

paths. If a pair consisting of specific 

Module Record

 and 

exportName

 is reached that is already in 

resolveSet

, an import

circularity has been encountered. Before recursively calling ResolveExport, a pair consisting of 

module

 and 

exportName

is added to 

resolveSet

.

If a defining module is found, a 

ResolvedBinding Record

 { [[Module]], [[BindingName]] } is returned. This record

identifies the resolved binding of the originally requested export, unless this is the export of a namespace with no
local binding. In this case, [[BindingName]] will be set to 

"*namespace*"

. If no definition was found or the request is

found to be circular, 

null

 is returned. If the request is found to be ambiguous, the string 

"ambiguous"

 is returned.

This concrete method performs the following steps when called:

1.  If 

resolveSet

 is not present, set 

resolveSet

 to a new empty 

List

.

2. 

Assert

resolveSet

 is a 

List

 of 

Record

 { [[Module]], [[ExportName]] }.

3.  For each 

Record

 { [[Module]], [[ExportName]] } 

r

 of 

resolveSet

, do

a.  If 

module

 and 

r

.[[Module]] are the same 

Module Record

 and 

SameValue

(

exportName

r

.[[ExportName]])

is 

true

, then

i. 

Assert

: This is a circular import request.

GetExportedNames does not filter out or throw an exception for names that have ambiguous star
export bindings.

16.2.1.6.3  ResolveExport ( 

exportName

 [ , 

resolveSet

 ] ) Concrete Method

446

ii.  Return 

null

.

4.  Append the 

Record

 { [[Module]]: 

module

, [[ExportName]]: 

exportName

 } to 

resolveSet

.

5.  For each 

ExportEntry Record

 

e

 of 

module

.[[LocalExportEntries]], do

a.  If 

SameValue

(

exportName

e

.[[ExportName]]) is 

true

, then

i. 

Assert

module

 provides the direct binding for this export.

ii.  Return 

ResolvedBinding Record

 { [[Module]]: 

module

, [[BindingName]]: 

e

.[[LocalName]] }.

6.  For each 

ExportEntry Record

 

e

 of 

module

.[[IndirectExportEntries]], do

a.  If 

SameValue

(

exportName

e

.[[ExportName]]) is 

true

, then

i.  Let 

importedModule

 be ? 

HostResolveImportedModule

(

module

e

.[[ModuleRequest]]).

ii.  If 

e

.[[ImportName]] is 

"*"

, then

1. 

Assert

module

 does not provide the direct binding for this export.

2.  Return 

ResolvedBinding Record

 { [[Module]]: 

importedModule

, [[BindingName]]:

"*namespace*"

 }.

iii.  Else,

1. 

Assert

module

 imports a specific binding for this export.

2.  Return 

importedModule

.ResolveExport(

e

.[[ImportName]], 

resolveSet

).

7.  If 

SameValue

(

exportName

"default"

) is 

true

, then

a. 

Assert

: A 

default

default

 export was not explicitly defined by this module.

b.  Return 

null

.

c.  NOTE: A 

default

default

 export cannot be provided by an 

export *

export *

 or 

export * from "mod"

export * from "mod"

declaration.

8.  Let 

starResolution

 be 

null

.

9.  For each 

ExportEntry Record

 

e

 of 

module

.[[StarExportEntries]], do

a.  Let 

importedModule

 be ? 

HostResolveImportedModule

(

module

e

.[[ModuleRequest]]).

b.  Let 

resolution

 be ? 

importedModule

.ResolveExport(

exportName

resolveSet

).

c.  If 

resolution

 is 

"ambiguous"

, return 

"ambiguous"

.

d.  If 

resolution

 is not 

null

, then

i. 

Assert

resolution

 is a 

ResolvedBinding Record

.

ii.  If 

starResolution

 is 

null

, set 

starResolution

 to 

resolution

.

iii.  Else,

1. 

Assert

: There is more than one 

**

 import that includes the requested name.

2.  If 

resolution

.[[Module]] and 

starResolution

.[[Module]] are not the same 

Module Record

 or

SameValue

(

resolution

.[[BindingName]], 

starResolution

.[[BindingName]]) is 

false

, return

"ambiguous"

.

10.  Return 

starResolution

.

The InitializeEnvironment concrete method of a 

Source Text Module Record

 

module

 takes no arguments. It performs

the following steps when called:

1.  For each 

ExportEntry Record

 

e

 of 

module

.[[IndirectExportEntries]], do

a.  Let 

resolution

 be ? 

module

.ResolveExport(

e

.[[ExportName]]).

b.  If 

resolution

 is 

null

 or 

"ambiguous"

, throw a 

SyntaxError

 exception.

c. 

Assert

resolution

 is a 

ResolvedBinding Record

.

2. 

Assert

: All named exports from 

module

 are resolvable.

3.  Let 

realm

 be 

module

.[[Realm]].

4. 

Assert

realm

 is not 

undefined

.

16.2.1.6.4  InitializeEnvironment ( ) Concrete Method

447

 

 

 

 

 

 

 

Content      ..     25      26      27      28     ..