To process the declaration instantiation, the engine needs things like VarDeclaredNames
, LexicallyDeclaredNames
, VarScopedDeclarations
, and LexicallyScopedDeclarations
in a piece of code.
In this article, we will see how this information is collected from the code according to the specification.
Declaration
What is declaration
? The declaration is one kind of ECMAScript statement. Here’s all kinds of declarations listed in the specification:
14 ECMAScript Language: Statements and DeclarationsSyntaxDeclaration[Yield, Await] :HoistableDeclaration[?Yield, ?Await, ~Default]ClassDeclaration[?Yield, ?Await, ~Default]LexicalDeclaration[+In, ?Yield, ?Await]HoistableDeclaration[Yield, Await, Default] :FunctionDeclaration[?Yield, ?Await, ?Default]GeneratorDeclaration[?Yield, ?Await, ?Default]AsyncFunctionDeclaration[?Yield, ?Await, ?Default]AsyncGeneratorDeclaration[?Yield, ?Await, ?Default]
Global
The information needed in GlobalDeclarationInstantiation(scriptBody, globalEnv)
:
- LexicallyDeclaredNames of
script
- LexicallyScopedDeclarations of
script
- VarDeclaredNames of
script
- VarScopedDeclarations of
script
How are VarScopedDeclarations
collected from scriptBody
?
In short, it collects top-level HoistableDeclarations (function
), top-level VariableStatements (var
), and block-nested VariableStatements.
Static Semantics: VarScopedDeclarationsScriptBody : StatementList1. Return TopLevelVarScopedDeclarations of StatementList.
Static Semantics: TopLevelVarScopedDeclarationsStatementList : StatementList StatementListItem1. Let declarations1 be TopLevelVarScopedDeclarations of StatementList.2. Let declarations2 be TopLevelVarScopedDeclarations of StatementListItem.3. Return the list-concatenation of declarations1 and declarations2.StatementListItem : Statement1. If Statement is Statement : LabelledStatement , return TopLevelVarScopedDeclarations of Statement.2. Return VarScopedDeclarations of Statement.StatementListItem : Declaration1. If Declaration is Declaration : HoistableDeclaration , thena. Let declaration be DeclarationPart of HoistableDeclaration.b. Return « declaration ».2. Return a new empty List.
// implicit definition of chain productionsStatic Semantics: VarScopedDeclarationsStatement : BlockStatement1. Return VarScopedDeclarations of BlockStatement.BlockStatement : Block1. Return VarScopedDeclarations of Block.Block : { StatementList }1. Return VarScopedDeclarations of StatementList.
About “implicit definition of chain productions,” see: What is the implicit definition of “chain productions”?
Static Semantics: VarScopedDeclarationsStatementList : StatementList StatementListItem1. Let declarations1 be VarScopedDeclarations of StatementList.2. Let declarations2 be VarScopedDeclarations of StatementListItem.3. Return the list-concatenation of declarations1 and declarations2.StatementListItem : Declaration1. Return a new empty List.VariableDeclarationList : VariableDeclaration1. Return « VariableDeclaration ».
How are VarDeclaredNames
collected from scriptBody
?
Same as VarScopedDeclarations
.
How are LexicallyScopedDeclarations
collected from scriptBody
?
In short, it collects top-level ClassDeclarations (class
) and LexicalDeclarations (let
and const
).
Static Semantics: LexicallyScopedDeclarationsScriptBody : StatementList1. Return TopLevelLexicallyScopedDeclarations of StatementList.
Static Semantics: TopLevelLexicallyScopedDeclarationsStatementList : StatementList StatementListItem1. Let declarations1 be TopLevelLexicallyScopedDeclarations of StatementList.2. Let declarations2 be TopLevelLexicallyScopedDeclarations of StatementListItem.3. Return the list-concatenation of declarations1 and declarations2.StatementListItem : Statement1. Return a new empty List.StatementListItem : Declaration1. If Declaration is Declaration : HoistableDeclaration , thena. Return a new empty List.2. Return « Declaration ».
14 ECMAScript Language: Statements and DeclarationsSyntaxDeclaration[Yield, Await] :HoistableDeclaration[?Yield, ?Await, ~Default]ClassDeclaration[?Yield, ?Await, ~Default]LexicalDeclaration[+In, ?Yield, ?Await]HoistableDeclaration[Yield, Await, Default] :FunctionDeclaration[?Yield, ?Await, ?Default]GeneratorDeclaration[?Yield, ?Await, ?Default]AsyncFunctionDeclaration[?Yield, ?Await, ?Default]AsyncGeneratorDeclaration[?Yield, ?Await, ?Default]
How are LexicallyDeclaredNames
collected from scriptBody
?
Same as LexicallyScopedDeclarations
.
Function
The information needed in FunctionDeclarationInstantiation(functionObject, argumentsList)
- Let
code
befunctionObject.[[ECMAScriptCode]]
- VarDeclaredNames of
code
- VarScopedDeclarations of
code
- LexicallyDeclaredNames of
code
- LexicallyScopedDeclarations of
code
- VarDeclaredNames of
- Let
formals
befunctionObject.[[FormalParameters]]
- BoundNames of
formals
- IteratorBindingInitialization of
formals
(initialized withargumentsList
)
- BoundNames of
How are VarScopedDeclarations
collected from functionObject.[[ECMAScriptCode]]
?
In short, it collects top-level HoistableDeclarations (function
), top-level VariableStatements (var
), and block-nested VariableStatements.
Static Semantics: VarScopedDeclarationsFunctionStatementList : [empty]1. Return a new empty List.FunctionStatementList : StatementList1. Return the TopLevelVarScopedDeclarations of StatementList.
Static Semantics: TopLevelVarScopedDeclarationsStatementList : StatementList StatementListItem1. Let declarations1 be TopLevelVarScopedDeclarations of StatementList.2. Let declarations2 be TopLevelVarScopedDeclarations of StatementListItem.3. Return the list-concatenation of declarations1 and declarations2.StatementListItem : Statement1. If Statement is Statement : LabelledStatement , return TopLevelVarScopedDeclarations of Statement.2. Return VarScopedDeclarations of Statement.StatementListItem : Declaration1. If Declaration is Declaration : HoistableDeclaration , thena. Let declaration be DeclarationPart of HoistableDeclaration.b. Return « declaration ».2. Return a new empty List.
// implicit definition of chain productionsStatic Semantics: VarScopedDeclarationsStatement : BlockStatement1. Return VarScopedDeclarations of BlockStatement.BlockStatement : Block1. Return VarScopedDeclarations of Block.Block : { StatementList }1. Return VarScopedDeclarations of StatementList.
About “implicit definition of chain productions,” see: What is the implicit definition of “chain productions”?
Static Semantics: VarScopedDeclarationsStatementList : StatementList StatementListItem1. Let declarations1 be VarScopedDeclarations of StatementList.2. Let declarations2 be VarScopedDeclarations of StatementListItem.3. Return the list-concatenation of declarations1 and declarations2.StatementListItem : Declaration1. Return a new empty List.VariableDeclarationList : VariableDeclaration1. Return « VariableDeclaration ».
How are VarDeclaredNames
collected?
Same as VarScopedDeclarations
.
How are LexicallyScopedDeclarations
collected from functionObject.[[ECMAScriptCode]]
?
In short, it collects top-level ClassDeclarations (class
) and LexicalDeclarations (let
and const
).
Static Semantics: LexicallyScopedDeclarationsScriptBody : StatementList1. Return TopLevelLexicallyScopedDeclarations of StatementList.
Static Semantics: TopLevelLexicallyScopedDeclarationsStatementList : StatementList StatementListItem1. Let declarations1 be TopLevelLexicallyScopedDeclarations of StatementList.2. Let declarations2 be TopLevelLexicallyScopedDeclarations of StatementListItem.3. Return the list-concatenation of declarations1 and declarations2.StatementListItem : Statement1. Return a new empty List.StatementListItem : Declaration1. If Declaration is Declaration : HoistableDeclaration , thena. Return a new empty List.2. Return « Declaration ».
How are LexicallyDeclaredNames
collected?
Same as LexicallyScopedDeclarations
.
Block
The information needed in BlockDeclarationInstantiation(StatementList, blockEnv)
- LexicallyScopedDeclarations of StatementList
How are LexicallyScopedDeclarations collected from StatementList?
Static Semantics: LexicallyScopedDeclarationsStatementList : StatementList StatementListItem1. Let declarations1 be LexicallyScopedDeclarations of StatementList.2. Let declarations2 be LexicallyScopedDeclarations of StatementListItem.3. Return the list-concatenation of declarations1 and declarations2.StatementListItem : Statement1. If Statement is Statement : LabelledStatement , return LexicallyScopedDeclarations of LabelledStatement.2. Return a new empty List.
Since the LexicallyScopedDeclarations
return empty list for the most of the statements except declarations and labelled statements, it means that collect the declarations from the current level are collected, while the declarations in nested blocks or functions are not.