Skip to content

[TS] Preserve lexical this of arrow function values in EtsIR #391

Description

@CaelmBleidd

Problem

The native frontend lifts an arrow function and an ordinary function expression through the same closure path. The lifted EtsIR method does not retain that the arrow's this is captured from its creation site. When the function value is stored on another object and called there, this distinction is required for correct execution.

Minimal TypeScript example

class Box {
  callback!: (value: number) => number;

  constructor(public offset: number) {}

  installOn(target: Box): void {
    target.callback = (value: number) => this.offset + value;
  }

  call(value: number): number {
    return this.callback(value);
  }
}

export function lexicalThis(): number {
  const source = new Box(11);
  const target = new Box(100);
  source.installOn(target);
  return target.call(1);
}

lexicalThis() must return 12, because the arrow captures source as this; an ordinary function expression called through target.callback would use the call receiver instead.

Source evidence and observed consumer failure

The source links are pinned to JacoDB neo 873f8e99. The standalone snippet compiles with TypeScript and returns 12 under Node.js. It has not been run as a new JacoDB frontend regression; the reported 101 came from the related #418 test.

Acceptance criteria

  • EtsIR distinguishes arrows from ordinary function expressions and preserves the lexical receiver of an arrow through JSON round trip.
  • A focused frontend regression checks the distinction in the stored-function case above.
  • A consumer can call the stored arrow and obtain 12 while ordinary functions continue to use their call receiver.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions