1

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.
Ben Aston
  • 45,997
  • 54
  • 176
  • 303

1 Answers1

2

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.

Bergi
  • 513,640
  • 108
  • 821
  • 1,164
  • Although named `ScriptEvaulationJob`, this is really script parsing, yes? Evaluation occurs on `Step 11` of `15.1.7 Runtime Semantics: ScriptEvaluation`. Or is there a well-defined meaning of evaluation I am missing? – Ben Aston Apr 20 '15 at 14:52
  • Does the evaluation of source code supplied to `setTimeout` fall under your `PerformEval` category for source code parsing? – Ben Aston Apr 20 '15 at 14:56
  • It's part of that, yes. `ScriptEvaluation` is called as step 4b of `ScriptEvaluationJob`. Those jobs are really started by the [ECMAScript initialisation](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-ecmascript-initialization) – Bergi Apr 20 '15 at 14:57
  • 1
    `setTimeout` is not specced by ES6, but rather in HTML5 Timers module. I guess it relies on `eval`, yes, but you can look that up yourself. – Bergi Apr 20 '15 at 14:58
  • My question earlier in these comments was specifically regarding the meaning of evaluation. It would appear that the term is used in the spec to cover both the parsing and the execution of the code. Is it correct therefore to say that the meaning of "evaluation" is loosely defined in the spec? – Ben Aston Apr 20 '15 at 14:59
  • 1
    I don't think the term is defined at all. It's just used :-) But usually, *evaluate* refers to execution of already parsed code. See the [static semantic rules](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-static-semantic-rules): "*A conforming implementation must, prior to the first evaluation of a Script, validate all of the early error rules of the productions used to parse that Script. If any of the early error rules are violated the Script is invalid and cannot be evaluated.*". – Bergi Apr 20 '15 at 15:04
  • The goal of the `ScriptEvaluationJob` is to evaluate (execute) the script. If it needs to parse a string because no AST is available yet, that's a minor annoyance but does not affect the naming :-) – Bergi Apr 20 '15 at 15:13