overlookmotel/livepack

Simplify class instrumentation

Closed this issue · 0 comments

Classes which use super are instrumented with a temp var to capture the "super target". This involves complications to retain class name. The complications are most severe where class is defined as an object property with a computed property key, and the instrumentation code has to cover a lot of edge cases. e.g.:

const obj = {
  [n]: class extends S {
    constructor() {
      super();
    }
  }
};

Is instrumented as:

let livepack_temp_1, livepack_temp_2;
const obj = {
  [livepack_temp_2 = n + ""]: livepack_temp_1 = {
    [livepack_temp_2]: class extends S {
      constructor() {
        super();
      }
    }
  }[livepack_temp_2]
};

This can be simplified using a static block instead:

let livepack_temp_1;
const C = class extends S {
  static {
    livepack_temp_1 = this;
  }
  constructor() {
    super();
  }
};

Static blocks are not supported in Node v14, but once support for v14 can be dropped (end of April 2023), it'll be possible. Node v16.13.0 (first LTS version of Node) does support static blocks, as does v18.0.0.