HaxeFoundation / HaxeFoundation/haxe

[js] runtime error when covering branches on safe navigation operator

Open
#11,246 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

I'm trying to update my coverage library to support new safe navigation operator. and I've run into an issue on javascript target. since it's a coverage library I want to log which branch was taken in a safe navigation operation. so I have to introduce some code to make sure each branch produces a log entry. unfortunately it runs into a runtime error in javascript.

I've come up with a [reduced sample](https://try.haxe.org/#DA2A7459):
```haxe
import haxe.macro.Expr;
import haxe.macro.ExprTools;

class SafeNav {
static macro function instrument(expr:Expr):Expr {
function iterate(expr:Expr):Expr {
return switch (expr.expr) {
case EField(e, field, kind):
{
expr: EBlock([
macro logBranch(1),
{expr: EField(e, field, Safe), pos: expr.pos}
]),
pos: expr.pos
};
default:
ExprTools.map(expr, iterate);
}
}
return iterate(expr);
}

static function logBranch(id:Int) {
trace("branch " + id);
}

static function main() {
trace(instrument(Std.string(1234)?.split(":") ?? []));
}
}
```

depending on whether you run it with or without optimize option it will either `$bind` on a string const or a tmp variable - runtime error is the same in both cases. it says `TypeError: Cannot create property 'hx__closures__' on string …` and points to `$bind` function.

might be connected / similar to https://github.com/HaxeFoundation/haxe/issues/5078

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.