haxe icon indicating copy to clipboard operation
haxe copied to clipboard

[js] generate arrow functions for ES6 mode

Open nadako opened this issue 4 years ago • 1 comments

to make the output closer to the "native" ES6 and make the a bit code more readable we can generate native JS arrow functions for our anonymous functions.

in addition we could look into using js arrow function this-binding semantics to avoid extra tempvaring, but that's a nice-to-have bonus

nadako avatar Aug 16 '21 08:08 nadako

There is enough of places with untyped __this__ usage, like: https://github.com/HaxeFoundation/haxe/blob/c713c0a8155ed2251f8921a3a06db01987edfc06/std/js/_std/haxe/ds/IntMap.hx#L62

// broken example
final map = [1 => 1];
for (i in map) trace(i);

So generator should detect if function has __this__ expression and generate non-arrow function, and also get_this should detect same situation and generate temp this for "untyped" non-arrow functions. https://github.com/HaxeFoundation/haxe/blob/4f3b0330ea33bf23386b050d5e1f9761ef1692b4/src/typing/typerBase.ml#L42 https://github.com/HaxeFoundation/haxe/blob/4f3b0330ea33bf23386b050d5e1f9761ef1692b4/src/generators/genjs.ml#L738 Maybe untyped __this__ usage should be checked before function body typing, and mark parent field someway, that will be accessed in two places (to generate tempvar this and non-arrow function). As alternative, something like @:js.function {...} can be added for internal corner cases to generate non-arrow function and have control of this in js-native way inside expression.

RblSb avatar Dec 05 '21 05:12 RblSb