Ben Aston
Ben Aston

Reputation: 55779

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

The part of the specification for script evaluation is here.

Is it correct to say that the lexing and parsing of the contents of a function is performed on step 10? If not when is the lexing and parsing performed?

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

Is it at this point (step 10) that the [[Scope]] on the LexicalEnvironment gets populated with declared functions and variables?

Is step 11 the step at which the code in the function is actually "executed"?

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

Upvotes: 1

Views: 69

Answers (1)

Bergi
Bergi

Reputation: 665554

No, ScriptBody is an already parsed abstract sytax tree. The parsing does happen before the evaluation, in the 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.

As you can see from the hightlighted sentence, ES does not really distinguish between parsing and lexing.
And of course, there is the following note that allows premature optimisation speculative parsing or 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.

Parsing source code also happens in step 3 of PerformEval, inside Function and GeneratorFunction constructors and somewhen for modules.

Upvotes: 2

Related Questions