codehag / documenting-invariants

Proposal to document design invariants in TC39
6 stars 3 forks source link

Emergent Invariant: Cannot access partially initialized literals #11

Open bmeck opened 4 years ago

bmeck commented 4 years ago

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.

bmeck commented 4 years ago

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