HaxeFoundation / HaxeFoundation/hscript
/ binop throws DivisionByZeroError on PHP8 target (Dynamic division by zero)
- Dominant language
- Haxe
- Stars
- 309
- Forks
- 152
- PR merge metrics
- No merged PRs in 30d
Description
I think this should be reported on Haxe github but since I guess that the answer will be to avoid using Dynamic I try to post it here because hscript is by default using Dynamic.
On the PHP target, Interp's default / binop (Interp.hx, initOps()):
binops.set("/", function(e1,e2) return me.expr(e1) / me.expr(e2));
relies on the native / operator on Dynamic operands. Since PHP 8.0, / throws DivisionByZeroError for any division by zero (including Float), whereas other targets (JS, etc.) return Infinity/NaN without throwing — per Haxe's own documented Float/IEEE754 semantics. Since Interp.expr() always operates in Dynamic, this cross-target guarantee silently breaks specifically on PHP8, even when both operands are genuinely Float at runtime.
Repro:
```haxe
var i = new hscript.Interp();
i.variables.set("x", 1.0);
i.variables.set("y", 0.0);
i.execute(new hscript.Parser().parseString("x / y")); // throws on PHP8, returns Infinity on JS
```
Suggested fix guard the divisor explicitly instead of relying on the native operator, restoring cross-target consistency:
```haxe
binops.set("/", function(e1,e2){
var b = me.expr(e2);
return b == 0 ? Math.NaN : me.expr(e1) / b;
});
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start in Interp.hx, specifically initOps() and the default "/" binop, then run the provided Parser and Interp reproduction with PHP 8 and another target. Compare the division-by-zero result with Haxe's documented Float behavior; done means the expression no longer throws on PHP 8 and remains consistent across targets.
Written by the indexing model from the issue text.
Assessment
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 78/100