Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ libhxcpp 6.0.3 (unreleased)
------------------------------------------------------------

* Fixed the C++ build tool, built with haxe compile-cpp.hxml in tools/hxcpp, not compiling since 6.0.0: ThreadPool.hx declared the class that ends a compile thread's job before one of its imports, which Haxe refuses with "import and using may not appear after a declaration". The neko build tool, hxcpp.n, was unaffected, and run.n uses a C++ build tool only once one is built, so only rebuilding it failed. CI now type-checks it (libhxcpp #471)
* Fixed a cppia cast to a class or interface whose value goes unused, cast(o, SomeInterface) as a statement, checking nothing under the JIT: a cast that fails ran on, where the interpreter and compiled code throw. Haxe's TestCasts.testInterfaces makes such casts in closures, which the JIT now compiles. The JIT leaves out only the unused casts that cannot fail
* Fixed the collector marking and moving the variables a cppia closure captures in place in the closure: a closure packs its captures at 4-byte offsets, so on 64-bit a String or object captured after an Int, as text in var count = 1; var text = "x"; function() return count + text, sits at an odd multiple of 4, which is undefined behaviour, reported by UBSan, and a fault on a CPU that requires alignment. Each capture is copied out to be marked or visited now, and written back if the moving collector moved it. Interpreted and jitted closures were both affected
* Fixed the cppia interpreter reading an argument's slot in place in the script frame when it fills in the argument's default value, and writing a Float or Dynamic default the same way: a frame keeps 4-byte alignment, so on 64-bit an argument after an Int, as b in f(a:Int, ?b:String = "x"), sits at an odd multiple of 4, which is undefined behaviour, reported by UBSan, and a fault on a CPU that requires alignment. The slots are copied in and out now, as the interpreter's other frame accesses have been since 6.0.0. The JIT, which compiles defaults into the function, was not affected
* Fixed Semaphore.tryAcquire(-1) waiting for ever where any other negative timeout returns at once, so a timeout worked out in whole seconds as deadline - now could wait with no limit once the deadline had passed. It was kept for Haxe 5.0.0-preview.1's Lock.wait() (libhxcpp #461), but preview.1's Lock waits through the runtime's own lock, which still takes a negative timeout as no limit, as Haxe 4's does. Only Haxe 5 nightlies from March to November 2026, which libhxcpp does not support, passed -1 to the semaphore; later ones call acquire() for no timeout. Every negative timeout returns at once now, as with stock hxcpp on Linux and macOS (HaxeFoundation/haxe#13114)
Expand Down
5 changes: 4 additions & 1 deletion src/hx/cppia/Cppia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,10 @@ struct CastExpr : public CppiaDynamicExpr
return;
}

if (destType==etNull || destType==etVoid)
// An unused cast that cannot fail is only its value. A cast to a class or
// interface still checks, and an array conversion still runs, as the
// interpreter's do
if ((destType==etNull || destType==etVoid) && op!=castInstance && op!=castDataArray && op!=castDynArray)
{
value->genCode(compiler);
return;
Expand Down
78 changes: 78 additions & 0 deletions test/regression/CppiaJitUnusedCasts/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Under the JIT, a cast to a class or interface whose value goes unused, as a
// statement, checked nothing, so a cast that fails ran on, where the
// interpreter and compiled code throw. Haxe's TestCasts.testInterfaces makes
// such casts in closures, which the JIT compiles since local functions are
// compiled.
// The same cases run as compiled code, then as a script with the JIT off and on,
// and print the same lines each time.
function main() {
Sys.println("-- compiled");
Compiled.run();
var script = haxe.io.Path.join([haxe.io.Path.directory(Sys.programPath()), "script.cppia"]);
for (jit in [false, true]) {
Sys.println(jit ? "-- script, JIT on" : "-- script, JIT off");
cpp.cppia.Host.enableJit(jit);
try {
cpp.cppia.Host.runFile(script);
} catch (e:Dynamic) {
Sys.println("error: " + e);
}
}
}

// The same cases as Script.hx, compiled into the host
interface CompiledFirst {}
interface CompiledSecond {}

class CompiledPlain implements CompiledFirst {
public function new() {}
}

class CompiledBoth extends CompiledPlain implements CompiledSecond {
public function new() {
super();
}
}

class Compiled {
static function attempt(label:String, f:Void->Void) {
try {
f();
Sys.println('$label passes');
} catch (e:Dynamic) {
Sys.println('$label throws');
}
}

// A cast whose value goes unused, as a statement of a method
static function toSecond(o:Dynamic) {
cast(o, CompiledSecond);
}

static function toBoth(o:Dynamic) {
cast(o, CompiledBoth);
}

public static function run() {
var plain = new CompiledPlain();
var both = new CompiledBoth();
attempt("plain to first in a closure", function() cast(plain, CompiledFirst));
attempt("plain to second in a closure", function() cast(plain, CompiledSecond));
attempt("both to second in a closure", function() cast(both, CompiledSecond));
attempt("plain to both in a closure", function() cast(plain, CompiledBoth));
attempt("plain to second in a method", () -> toSecond(plain));
attempt("both to second in a method", () -> toSecond(both));
attempt("plain to both in a method", () -> toBoth(plain));
attempt("both to both in a method", () -> toBoth(both));
attempt("plain to second, used", function() {
var s:CompiledSecond = cast(plain, CompiledSecond);
Sys.println('used ${s == null}');
});
var after = 0;
attempt("a statement after a failed cast", function() {
cast(plain, CompiledSecond);
after++;
});
Sys.println('ran on past it $after');
}
}
56 changes: 56 additions & 0 deletions test/regression/CppiaJitUnusedCasts/Script.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// The same cases as Compiled in Main.hx, run as a script
interface First {}
interface Second {}

class Plain implements First {
public function new() {}
}

class Both extends Plain implements Second {
public function new() {
super();
}
}

class Script {
static function attempt(label:String, f:Void->Void) {
try {
f();
Sys.println('$label passes');
} catch (e:Dynamic) {
Sys.println('$label throws');
}
}

// A cast whose value goes unused, as a statement of a method
static function toSecond(o:Dynamic) {
cast(o, Second);
}

static function toBoth(o:Dynamic) {
cast(o, Both);
}

static function main() {
var plain = new Plain();
var both = new Both();
attempt("plain to first in a closure", function() cast(plain, First));
attempt("plain to second in a closure", function() cast(plain, Second));
attempt("both to second in a closure", function() cast(both, Second));
attempt("plain to both in a closure", function() cast(plain, Both));
attempt("plain to second in a method", () -> toSecond(plain));
attempt("both to second in a method", () -> toSecond(both));
attempt("plain to both in a method", () -> toBoth(plain));
attempt("both to both in a method", () -> toBoth(both));
attempt("plain to second, used", function() {
var s:Second = cast(plain, Second);
Sys.println('used ${s == null}');
});
var after = 0;
attempt("a statement after a failed cast", function() {
cast(plain, Second);
after++;
});
Sys.println('ran on past it $after');
}
}
7 changes: 7 additions & 0 deletions test/regression/CppiaJitUnusedCasts/build.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
--main Script
-cppia bin/script.cppia
--next
--main Main
-cpp bin
-D scriptable
--dce no
36 changes: 36 additions & 0 deletions test/regression/CppiaJitUnusedCasts/stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- compiled
plain to first in a closure passes
plain to second in a closure throws
both to second in a closure passes
plain to both in a closure throws
plain to second in a method throws
both to second in a method passes
plain to both in a method throws
both to both in a method passes
plain to second, used throws
a statement after a failed cast throws
ran on past it 0
-- script, JIT off
plain to first in a closure passes
plain to second in a closure throws
both to second in a closure passes
plain to both in a closure throws
plain to second in a method throws
both to second in a method passes
plain to both in a method throws
both to both in a method passes
plain to second, used throws
a statement after a failed cast throws
ran on past it 0
-- script, JIT on
plain to first in a closure passes
plain to second in a closure throws
both to second in a closure passes
plain to both in a closure throws
plain to second in a method throws
both to second in a method passes
plain to both in a method throws
both to both in a method passes
plain to second, used throws
a statement after a failed cast throws
ran on past it 0
Loading