5.2 Algorithm Conventions
The specification often uses a numbered list to specify steps in an algorithm. These algorithms are used to precisely specify the required semantics of ECMAScript language constructs. The algorithms are not intended to imply the use of any specific implementation technique. In practice, there may be more efficient algorithms available to implement a given feature.
Algorithms may be explicitly parameterized with an ordered, comma-separated sequence of alias names which may be used within the algorithm steps to reference the argument passed in that position. Optional parameters are denoted with surrounding brackets ([ , name ]) and are no different from required parameters within algorithm steps. A rest parameter may appear at the end of a parameter list, denoted with leading ellipsis (, ...name). The rest parameter captures all of the arguments provided following the required and optional parameters into a
Algorithm steps may be subdivided into sequential substeps. Substeps are indented and may themselves be further divided into indented substeps. Outline numbering conventions are used to identify substeps with the first level of substeps labelled with lowercase alphabetic characters and the second level of substeps labelled with lowercase roman numerals. If more than three levels are required these rules repeat with the fourth level using numeric labels. For example:
- Top-level step
- Substep.
- Substep.
- Subsubstep.
- Subsubsubstep
- Subsubsubsubstep
- Subsubsubsubsubstep
- Subsubsubsubstep
- Subsubsubstep
- Subsubstep.
A step or substep may be written as an “if” predicate that conditions its substeps. In this case, the substeps are only applied if the predicate is true. If a step or substep begins with the word “else”, it is a predicate that is the negation of the preceding “if” predicate step at the same level.
A step may specify the iterative application of its substeps.
A step that begins with “Assert:” asserts an invariant condition of its algorithm. Such assertions are used to make explicit algorithmic invariants that would otherwise be implicit. Such assertions add no additional semantic requirements and hence need not be checked by an implementation. They are used simply to clarify algorithms.
Algorithm steps may declare named aliases for any value using the form “Let x be someValue”. These aliases are reference-like in that both x and someValue refer to the same underlying data and modifications to either are visible to both. Algorithm steps that want to avoid this reference-like behaviour should explicitly make a copy of the right-hand side: “Let x be a copy of someValue” creates a shallow copy of someValue.
Once declared, an alias may be referenced in any subsequent steps and must not be referenced from steps prior to the alias's declaration. Aliases may be modified using the form “Set x to someOtherValue”.
5.2.1 Abstract Operations
In order to facilitate their use in multiple parts of this specification, some algorithms, called abstract operations, are named and written in parameterized functional form so that they may be referenced by name from within other algorithms. Abstract operations are typically referenced using a functional application style such as OperationName(arg1, arg2). Some abstract operations are treated as polymorphically dispatched methods of class-like specification abstractions. Such method-like abstract operations are typically referenced using a method application style such as someValue.OperationName(arg1, arg2).
5.2.2 Syntax-Directed Operations
A syntax-directed operation is a named operation whose definition consists of algorithms, each of which is associated with one or more productions from one of the ECMAScript grammars. A production that has multiple alternative definitions will typically have a distinct algorithm for each alternative. When an algorithm is associated with a grammar production, it may reference the terminal and nonterminal symbols of the production alternative as if they were parameters of the algorithm. When used in this manner, nonterminal symbols refer to the actual alternative definition that is matched when parsing the source text. The source text matched by a grammar production or
When an algorithm is associated with a production alternative, the alternative is typically shown without any “[ ]” grammar annotations. Such annotations should only affect the syntactic recognition of the alternative and have no effect on the associated semantics for the alternative.
Syntax-directed operations are invoked with a parse node and, optionally, other parameters by using the conventions on steps
- Let status be SyntaxDirectedOperation of
SomeNonTerminal . - Let someParseNode be the parse of some source text.
- Perform SyntaxDirectedOperation of someParseNode.
- Perform SyntaxDirectedOperation of someParseNode with argument
"value" .
Unless explicitly specified otherwise, all
but the
Runtime Semantics:
- Return
Evaluation ofStatementList .
5.2.3 Runtime Semantics
Algorithms which specify semantics that must be called at runtime are called runtime semantics. Runtime semantics are defined by
5.2.3.1 Completion ( completionRecord )
The abstract operation Completion takes argument completionRecord (a
Assert : completionRecord is aCompletion Record .- Return completionRecord.
5.2.3.2 Throw an Exception
Algorithms steps that say to throw an exception, such as
- Throw a
TypeError exception.
mean the same things as:
- Return
ThrowCompletion (a newly createdTypeError object).
5.2.3.3 ReturnIfAbrupt
Algorithms steps that say or are otherwise equivalent to:
ReturnIfAbrupt (argument).
mean the same thing as:
Assert : argument is aCompletion Record .- If argument is an
abrupt completion , returnCompletion (argument). - Else, set argument to argument.[[Value]].
Algorithms steps that say or are otherwise equivalent to:
ReturnIfAbrupt (AbstractOperation()).
mean the same thing as:
- Let hygienicTemp be AbstractOperation().
Assert : hygienicTemp is aCompletion Record .- If hygienicTemp is an
abrupt completion , returnCompletion (hygienicTemp). - Else, set hygienicTemp to hygienicTemp.[[Value]].
Where hygienicTemp is ephemeral and visible only in the steps pertaining to ReturnIfAbrupt.
Algorithms steps that say or are otherwise equivalent to:
- Let result be AbstractOperation(
ReturnIfAbrupt (argument)).
mean the same thing as:
Assert : argument is aCompletion Record .- If argument is an
abrupt completion , returnCompletion (argument). - Else, set argument to argument.[[Value]].
- Let result be AbstractOperation(argument).
5.2.3.4 ReturnIfAbrupt Shorthands
Invocations of ?
indicate that
- ? OperationName().
is equivalent to the following step:
ReturnIfAbrupt (OperationName()).
Similarly, for method application style, the step:
- ? someValue.OperationName().
is equivalent to:
ReturnIfAbrupt (someValue.OperationName()).
Similarly, prefix !
is used to indicate that the following invocation of an abstract or
- Let val be ! OperationName().
is equivalent to the following steps:
- Let val be OperationName().
Assert : val is anormal completion .- Set val to val.[[Value]].
!
or ?
before the invocation of the operation:
- Perform ! SyntaxDirectedOperation of
NonTerminal .
5.2.3.5 Implicit Normal Completion
In algorithms within
- when the result of applying
Completion ,NormalCompletion , orThrowCompletion is directly returned - when the result of constructing a
Completion Record is directly returned
It is an editorial error if a
- Return
true .
means the same things as any of
- Return
NormalCompletion (true ).
or
- Let completion be
NormalCompletion (true ). - Return
Completion (completion).
or
- Return
Completion Record { [[Type]]:normal , [[Value]]:true , [[Target]]:empty }.
Note that, through the
- Return ? completion.
The following example would be an editorial error because a
- Let completion be
NormalCompletion (true ). - Return completion.
5.2.4 Static Semantics
Context-free grammars are not sufficiently powerful to express all the rules that define whether a stream of input elements form a valid ECMAScript
Static Semantic Rules have names and typically are defined using an algorithm. Named Static Semantic Rules are associated with grammar productions and a production that has multiple alternative definitions will typically have for each alternative a distinct algorithm for each applicable named static semantic rule.
A special kind of static semantic rule is an Early Error Rule.
5.2.5 Mathematical Operations
This specification makes reference to these kinds of numeric values:
- Mathematical values: Arbitrary real numbers, used as the default numeric type.
- Extended mathematical values:
Mathematical values together with +∞ and -∞. - Numbers:
IEEE 754-2019 double-precision floating point values. - BigInts:
ECMAScript language values representing arbitraryintegers in a one-to-one correspondence.
In the language of this specification, numerical values are distinguished among different numeric kinds using subscript suffixes. The subscript 𝔽 refers to Numbers, and the subscript ℤ refers to BigInts. Numeric values without a subscript suffix refer to
Numeric operators such as +, ×, =, and ≥ refer to those operations as determined by the type of the operands. When applied to
In general, when this specification refers to a numerical value, such as in the phrase, "the length of y" or "the
Numeric operators applied to mixed-type operands (such as a Number and a
This specification denotes most numeric values in base 10; it also uses numeric values of the form 0x followed by digits 0-9 or A-F as base-16 values.
When the term integer is used in this specification, it refers to a
Conversions between
The mathematical function
The mathematical function
The notation “
The phrase "the result of clamping x between lower and upper" (where x is an
The mathematical function
The mathematical function
Mathematical functions
An interval from lower bound a to upper bound b is a possibly-infinite, possibly-empty set of numeric values of the same numeric type. Each bound will be described as either inclusive or exclusive, but not both. There are four kinds of intervals, as follows:
- An
interval from a (inclusive) to b (inclusive), also called an inclusive interval from a to b, includes all values x of the same numeric type such that a ≤ x ≤ b, and no others. - An
interval from a (inclusive) to b (exclusive) includes all values x of the same numeric type such that a ≤ x < b, and no others. - An
interval from a (exclusive) to b (inclusive) includes all values x of the same numeric type such that a < x ≤ b, and no others. - An
interval from a (exclusive) to b (exclusive) includes all values x of the same numeric type such that a < x < b, and no others.
For example, the
5.2.6 Value Notation
In this specification, Function.prototype.apply
or let n = 42;
.
5.2.7 Identity
In this specification, both specification values and
From the perspective of this specification, the word “is” is used to compare two values for equality, as in “If bool is
From the perspective of the ECMAScript language, language values are compared for equality using the
For specification values, examples of values without specification identity include, but are not limited to:
Specification identity agrees with language identity for all