codehag / documenting-invariants

Proposal to document design invariants in TC39

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Emergent Invariant: Cannot access partially initialized literals

bmeck opened this issue · comments

ECMAScript initializes properties ad-hoc per spec, but we never expose partially initialized literals...

This affects things like literals defining get/set pairs and avoiding them being seen as partially initialized.

var _ = 0;
var obj = {
  get field() { return _; }
  // ...
  // No possible code can get a Reference to obj.field , so cannot see that set is not yet applied
  // The following line is not possible in this location (accessing `obj` prior to `set`)
  other: getRefToObjEarly().field = 1,
  // ...
  set field(v) { return _ = v; }
}

This invariant makes it seem as if both [[Get]] and [[Set]] are populated at the same time. It greatly simplifies how to think about objects and avoids forcing defensive programming against partially initialized objects.

This might change depending if decorators ever want to decorate object literals, so it seems good to iron out the actual space here.