HaxeFoundation / HaxeFoundation/haxe

[hl] Mess with type parameters and macro/inline functions.

Open
#11,344 5 comments 0 reactions 0 assignees View on GitHub
platform-hl
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

I discovered that simple signal implementation i've always used, passes garbage instead of numbers to callback on hl target.
In the original implementation i have macro dispatch() function for listeners call.
Trying to minimize reproduction of the bug i've found that inline function may work the same.

````haxe
class ObsTest {
public static var onChange(default, null):Signal = new Signal();
public static function main() {
onChange.listen(v -> trace("Received value: " + v));
onChange.dispatch(30); //ObsTest.hx:4: Received value: 907739328, the actual number may vary
}
}

class Signal {
var listeners:ArrayVoid> = [];
public function new() {}

public inline function listen(listener:T->Void) {
listeners.push(listener);
}

public function dispatch(a) {
for (l in listeners)
l(a);
}
}
````
if both `listen()` and `dispatch()` are inline, the code works fine. If `dispatch()` only is inline, the code fails in runtime with `Uncaught exception: Access violation` (Hl only again).

Minimal example with macro function works fine, bun on my codebase passes garbage as well.
````haxe
class Signal {
var listeners:Array = [];
public function new() {
}

public inline function listen(listener:T) {
listeners.push(listener);
}

public macro function dispatch(signal, args:Array) {
return macro {
for (listener in $signal.asArray()) listener($a{args});
}
}

public inline function asArray() return listeners;
}
````
https://try.haxe.org/#6Bfb676e

One more curious effect, this sample fails to compile on last release with no dce enabled, but works on nightly.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.