Skip to content

lexical bindings and strategies for emitting them

icefapper edited this page Mar 5, 2017 · 2 revisions

(This is mostly a note to myself)

during the emit phase, lexical bindings are treated, based on their declaration site and their reference sites, as either a "s-type lexical" (s for simple), or "v-type lexical" (v for the representing object's entry of the same name, which in turn stands for value).

a lexical is v-type if:

  • it has been declared inside a loop, and
  • it has been indirectly referenced

a lexical is s-type if it is not v-type.

Example:

while (false) {
  if (false) {
    let a; // s-type -- it is in a loop, but it has not been indirectly referenced
    (function() { return 12; });
  }
}

but

while (false) {
  if (false) {
    let a; // v-type -- it is in a loop, and indirectly referenced
    (function() { return a; });
  }
}

testing references for the tdz

In every given scope, a lexical binding must only be tested for tdz when it is a let or class lexical (i.e, not a function lexical because it gets hoisted), and either:

  • that scope effectively precedes that lexical binding's declaration, or,
  • that scope is the same scope as the binding's, but the lexical variable's declaration has not yet been reached.

In all the above cases, if the referencing scope has the same surrounding concrete scope as the binding, the test will effectively be a tz err and no actual check is needed, because we are sure it has indeed been tz-accessed.