Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Effects

Christopher Blappert edited this page Jun 18, 2018 · 2 revisions

Effects

Prepack's front-end (the abstract interpreter) produces “effects”, and the back-end (the serializer) creates code for those effects.

“Effects” is a data-structure that encodes anything a piece of JavaScript code (an AST node) could have done:

  • The “result” --- either a normal completion or an abrupt completion e.g. an exception or a break, continue, return, or a conditional combination of effects where the conditions are mutually exclusive (possibly normal completion, joined abrupt completions)
    • A possibly normal completion and joined abrupt completions have effects --- so we actually have an effects tree. In this case, the nested effects conceptually apply after the outer effects; the outer effects can be seen as a prefix, and the nested effects as a suffix
  • modified bindings for variables (possibly with conditional values)
  • modified object properties for objects (possibly with conditional values)
  • a set of newly created objects
  • a recording of environment interactions (internally called “generator” for historical reasons); actually, a tree (or DAG?) or generators if there are conditional environment interactions

At the end of global code, and at the end of interpreting an optimized function, only a subset of effects are possible; in particular, intermediate effects involving a BreakCompletion, ContinueCompletion or a ReturnCompletion are no longer allowed.

For a “Program” AST node, all effects get merged, and if there are any potential exceptions thrown, then at the very end of the main generator, a throw-statement is added, or possibly many under conditions. Thus, all effects are dealt with, and the serializer will never have to worry about them.

For optimized functions, no such transformation is applied at this time.

Effects always have the following properties:

  • When its result refers to other nested effects, i.e. via a JoinedAbruptCompletion or a PossiblyNormalCompletion, then...
    • The outer effects must have a generator entry that is the result of joining or composing the nested generators : A generator entry that depends on the generators of the nested effects, with a build node that emits an if-statement that guards their execution (for joins) or that serializes their execution (for composition).
    • The outer effects must mention all created objects of the nested effects.
    • The outer effects must mention all modified object properties of the nested effects, with the new values being the conditionally merged values of the nested effects.
    • The same for modified plain bindings.