在 ES6 中执行一个函数时,什么时候进行词法分析和解析(这采用什么形式)?

When executing a function in ES6, when is the lexing and parsing performed (and what form does this take)?

脚本评估规范部分is here

step 10 上执行函数内容的词法分析和解析是否正确?如果不是,何时进行词法分析和解析?

  1. Let result be GlobalDeclarationInstantiation(ScriptBody, globalEnv).

此时 (step 10) LexicalEnvironment 上的 [[Scope]] 是否填充了已声明的函数和变量?

step 11函数中的代码实际上是"executed"那一步吗?

  1. If result.[[type]] is normal, then Let result be the result of evaluating ScriptBody.

不,ScriptBody 是一个已经解析过的抽象语法树。解析确实发生在评估之前,在 ScriptEvaluationJob (sourceText):

  1. Parse sourceText using Script as the goal symbol and analyze the parse result for any Early Error conditions. If the parse was successful and no early errors were found, let code be the resulting parse tree. Otherwise, let code be an indication of one or more parsing errors and/or early errors. Parsing and early error detection may be interweaved in an implementation dependent manner. If more than one parse or early error is present, the number and ordering of reported errors is implementation dependent but at least one error must be reported.

从高亮的句子可以看出,ES并没有真正区分parsing和lexing。
当然,还有以下注释允许过早优化推测性解析或using cached compilation results:

An implementation may parse a sourceText as a Script and analyze it for Early Error conditions prior to the execution of the ScriptEvaluationJob for that sourceText.

解析源代码也发生在PerformEval, inside Function and GeneratorFunction constructors and somewhen for modules的第3步。