HaxeFoundation / HaxeFoundation/haxe

[js] static `call` method breaks `super()`

Open
#8,509 6 comments 0 reactions 0 assignees View on GitHub
bug platform-javascript
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

The following will currently fail on JS target in ES5 mode:

```haxe
class A {
function new() trace("A.new");
static function call() throw "FAIL";
}

class B extends A {
public function new() super();
}

class Main {
static function main() {
new B();
}
}
```

This is because of how we generate `super()` calls:
```js
var B = function() {
A.call(this);
};
```

There are at least two ways to fix this:
- Generate `Function.prototype.call.call(A, this)` instead of `A.call(this)`, so the correct `call` function is used (we can store `Function.prototype.call` in a var like `$call` to shorten the output). My concerns here are: performance implications (any JS experts here? can this cause slowdowns?) and native interop (maybe there are important libs that assume that `Function.call` is not overriden? tho they can/should do the `Function.prototype` trick too)
- Escape the `call` static method name with `$`, like we do with `length` and `name`. This will break reflection and structure access (similar to `length` and `name`), but will have no performance impact (yet to measure) and will also avoid an issue with Google Closure minifier that is very confused by static `call` methods and can produce broken output.

This is somewhat relevant for other JS `Function` methods: `bind` and `apply`, although they are not breaking `super()` calls.

Contributor guide

Open the contributing guide

Research direction

Start with the ES5 JavaScript code-generation path that emits `A.call(this)` for the `super()` call, and reproduce the issue with the Haxe classes shown. Evaluate the proposed call escaping or `Function.prototype.call` approaches, then verify that constructing `B` invokes `A.new` without the static `call` method interfering.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.