Stage 2 Draft / October 20, 2022

JavaScript Module Blocks

This document is a diff on top of tc39/ecma262#2905.

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 ModuleBlockExpression RegularExpressionLiteral TemplateLiteral[?Yield, ?Await, ~Tagged] CoverParenthesizedExpressionAndArrowParameterList[?Yield, ?Await]

13.2.10 Module Block Expression

Syntax

ModuleBlockExpression : module [no LineTerminator here] { ModuleBodyopt }

13.2.10.1 Runtime Semantics: Evaluation

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

13.2.10.1.1 CreateModuleBlockObject ( body, sourceText )

The abstract operation CreateModuleBlockObject takes arguments body (a String) and sourceText (a String) and returns a ModuleBlock 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 moduleBlock be OrdinaryObjectCreate(%ModuleBlock.prototype%, « [[ModuleRecord]], [[SourceText]] »).
  8. Set moduleBlock.[[ModuleRecord]] to moduleRecord.
  9. Set moduleBlock.[[SourceText]] to sourceText.
  10. Return moduleBlock.

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]].

20 Fundamental Objects

20.6 ModuleBlock Objects

20.6.1 The ModuleBlock Constructor

The ModuleBlock constructor:

  • is %ModuleBlock%.
  • is the initial value of the "ModuleBlock" 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 ModuleBlock constructor will cause an exception.

20.6.1.1 ModuleBlock ( body )

  1. Throw a TypeError exception.

20.6.1.2 ModuleBlock.prototype

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

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

20.6.2 Properties of the ModuleBlock Prototype Object

The ModuleBlock prototype object:

20.6.2.1 ModuleBlock.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.