Skip to content

/ binop throws DivisionByZeroError on PHP8 target (Dynamic division by zero) #166

Description

@filt3rek

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:

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:

binops.set("/", function(e1,e2){
    var b = me.expr(e2);
    return b == 0 ? Math.NaN : me.expr(e1) / b;
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions