Stage 2 Draft / December 19, 2022

Module Expressions

13 ECMAScript Language: Expressions

13.2 Primary Expression

Syntax

PrimaryExpression[Yield, Await] : this IdentifierReference[?Yield, ?Await] Literal ArrayLiteral[?Yield, ?Await] ObjectLiteral[?Yield, ?Await] FunctionExpression ClassExpression[?Yield, ?Await] GeneratorExpression AsyncFunctionExpression AsyncGeneratorExpression ModuleExpression RegularExpressionLiteral TemplateLiteral[?Yield, ?Await, ~Tagged] CoverParenthesizedExpressionAndArrowParameterList[?Yield, ?Await]

13.2.10 Module Expression

Syntax

ModuleExpression : module [no LineTerminator here] { ModuleBodyopt }

13.2.10.1 Runtime Semantics: Evaluation

ModuleExpression : module { }
  1. Let sourceText be the source text matched by ModuleExpression.
  2. Return CreateModuleObject("", sourceText).
ModuleExpression : module { ModuleBody }
  1. Let body be the source text matched by ModuleBody.
  2. Let sourceText be the source text matched by ModuleExpression.
  3. Return CreateModuleObject(body, sourceText).

13.2.10.1.1 CreateModuleObject ( body, sourceText )

The abstract operation CreateModuleObject takes arguments body (a String) and sourceText (a String) and returns a Module Object. It performs the following steps when called:

  1. Let realm be the current Realm Record.
  2. Let hostDefined be null.
  3. Let currentScriptOrModule be GetActiveScriptOrModule().
  4. If currentScriptOrModule is not null, set hostDefined to currentScriptOrModule.[[HostDefined]].
  5. Let moduleRecord be ParseModule(body, realm, hostDefined).
  6. Assert: moduleRecord is a Module Record, because parse errors would have been reported when parsing the outer script or module.
  7. Let moduleObject be OrdinaryObjectCreate(%Module.prototype%, « [[ModuleRecord]], [[SourceText]] »).
  8. Set moduleObject.[[ModuleRecord]] to moduleRecord.
  9. Set moduleObject.[[SourceText]] to sourceText.
  10. Return moduleObject.

13.3 Left-Hand-Side Expressions

13.3.10 Import Calls

13.3.10.1 Runtime Semantics: Evaluation

ImportCall : import ( AssignmentExpression )
  1. Let referrer be GetActiveScriptOrModule().
  2. If referrer is null, set referrer to the current Realm Record.
  3. Let argRef be ? Evaluation of AssignmentExpression.
  4. Let specifier be ? GetValue(argRef).
  5. Let promiseCapability be ! NewPromiseCapability(%Promise%).
  6. If Type(specifier) is Object and specifier has a [[ModuleRecord]] internal slot, then
    1. Perform ContinueDynamicImport(promiseCapability, specifier.[[ModuleRecord]]).
  7. Else,
    1. Let specifierString be Completion(ToString(specifier)).
    2. IfAbruptRejectPromise(specifierString, promiseCapability).
    3. Perform HostLoadImportedModule(referrer, specifierString, empty, promiseCapability).
  8. Return promiseCapability.[[Promise]].

14 ECMAScript Language: Statements and Declarations

14.1 Expression Statement

Syntax

ExpressionStatement[Yield, Await] : [lookahead ∉ { {, function, async [no LineTerminator here] function, class, let [, module [no LineTerminator here] { }] Expression[+In, ?Yield, ?Await] ; Note

An ExpressionStatement cannot start with a U+007B (LEFT CURLY BRACKET) because that might make it ambiguous with a Block. An ExpressionStatement cannot start with the function or class keywords because that would make it ambiguous with a FunctionDeclaration, a GeneratorDeclaration, or a ClassDeclaration. An ExpressionStatement cannot start with async function because that would make it ambiguous with an AsyncFunctionDeclaration or a AsyncGeneratorDeclaration. An ExpressionStatement cannot start with the two token sequence let [ because that would make it ambiguous with a let LexicalDeclaration whose first LexicalBinding was an ArrayBindingPattern. An ExpressionStatement cannot start with module { for forward compatibility with the Module Declarations proposal.

16 ECMAScript Language: Scripts and Modules

16.2.3 Exports

Syntax

ExportDeclaration : export ExportFromClause FromClause ; export NamedExports ; export VariableStatement[~Yield, +Await] export Declaration[~Yield, +Await] export default HoistableDeclaration[~Yield, +Await, +Default] export default ClassDeclaration[~Yield, +Await, +Default] export default [lookahead ∉ { function, async [no LineTerminator here] function, class, module [no LineTerminator here] { }] AssignmentExpression[+In, ~Yield, +Await] ; export default ModuleExpression ; Editor's Note

The export default ModuleExpression ; production is necessary for forward-compatibility with the module declarations proposal, which will replace it with export default ModuleDeclaration ;.

We cannot simply remove the ∉ { module { } lookahead in the export default AssignmentExpression ; production, because that would make the following code valid:

export default module {}.prop;

20 Fundamental Objects

20.6 Module Objects

20.6.1 The Module Constructor

The Module constructor:

  • is %Module%.
  • is the initial value of the "Module" property of the global object.
  • is not intended to be called as a function and will throw an exception when called in that manner.
  • is not intended to be used with the new operator or to be subclassed. It may be used as the value of an extends clause of a class definition but a super call to the Module constructor will cause an exception.

20.6.1.1 Module ( body )

  1. Throw a TypeError exception.

20.6.1.2 Module.prototype

The initial value of Module.prototype is the Module prototype object.

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

20.6.2 Properties of the Module Prototype Object

The Module prototype object:

20.6.2.1 Module.prototype.toString ( )

The following steps are taken:

  1. Let module be the this value.
  2. If Type(module) is Object and has the internal slot [[ModuleRecord]], then
    1. Return module.[[SourceText]].
  3. Throw a TypeError exception.

A Copyright & Software License

Copyright Notice

© 2022 Surma, Dan Ehrenberg, Nicolò Ribaudo

Software License

All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT https://ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the authors nor Ecma International may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.