Skip to content

Commit a414c3d

Browse files
MeguminBOTdimensionscape
authored andcommitted
Raise a jitted cppia exception at the boundary back to native code
Jitted cppia does not throw. ThrowExpr::genCode writes the value to ctx->exception and jumps to the function epilogue, and jitted code checks the context after every call it makes to other jitted code, so within jitted code the unwind is complete. Nothing did that where jitted code returns to native code. runFunction and runFunctionClosure call compiled(ctx) and never look at ctx->exception, and the compiled branch of CppiaClosure::__run looks only to decide against reading a return value, then answers null() with the exception still set. The caller reads null, and since nothing clears the context, every later call into jitted cppia returns at its first checkException. All three sites now raise it and clear the context, which is the shape TryExpr::runVoid already uses on the interpreted path. The cppia test suite covers it, run with -jit. HaxeFoundation/hxcpp#1368 Fixes #66
1 parent d1d7d83 commit a414c3d

4 files changed

Lines changed: 54 additions & 1 deletion

File tree

‎Changes.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ The first libhxcpp version. It is based on hxcpp 4.3 (the changes listed under 4
223223
* Fixed Tracy builds (-D HXCPP_TRACY) throwing "Not implemented" from the hxtelemetry API: starting telemetry is a no-op and dumping returns no frame
224224
* Fixed the CFFI val_gc_ptr always throwing "Finalizer not supported here": it attaches the finalizer to the allocation
225225
* Fixed DEFINE_PRIM_MULT exporting nothing with HXCPP_JS_PRIME (Emscripten): primitives with more than five arguments are exported and take their arguments as a JavaScript array
226+
* Fixed a jitted cppia exception thrown back to native code returning null, and every later jitted call returning null with it
226227
* Added the libhxcpp define: a build that uses the library gets -D libhxcpp=<version>. Compare it with version("x.y.z"); a plain string comparison compares text
227228
* Added separate Linux ARMv7 and ARM64 build architectures (hxcpp #1355)
228229
* Fixed AndroidCompat.cpp closing its extern "C" section in the wrong place (hxcpp #1404)

‎src/hx/cppia/CppiaFunction.cpp‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,10 +514,18 @@ void ScriptCallable::runFunction(CppiaCtx *ctx)
514514
#ifdef CPPIA_JIT
515515
if (compiled)
516516
{
517+
{
517518
AutoFrame frame(ctx);
518519
//printf("Running compiled code...\n");
519520
compiled(ctx);
520521
//printf("Done.\n");
522+
}
523+
if (ctx->exception)
524+
{
525+
Dynamic caught = ctx->exception;
526+
ctx->exception = nullptr;
527+
HX_STACK_DO_THROW(caught);
528+
}
521529
}
522530
else
523531
#endif
@@ -553,10 +561,18 @@ void ScriptCallable::runFunctionClosure(CppiaCtx *ctx)
553561
#ifdef CPPIA_JIT
554562
if (compiled)
555563
{
564+
{
556565
AutoFrame frame(ctx);
557566
//printf("Running compiled code...\n");
558567
compiled(ctx);
559568
//printf("Done.\n");
569+
}
570+
if (ctx->exception)
571+
{
572+
Dynamic caught = ctx->exception;
573+
ctx->exception = nullptr;
574+
HX_STACK_DO_THROW(caught);
575+
}
560576
}
561577
else
562578
#endif
@@ -759,7 +775,13 @@ class CppiaClosure : public hx::Object
759775
AutoFrame frame(ctx);
760776
function->compiled(ctx);
761777
}
762-
if (!ctx->exception)
778+
if (ctx->exception)
779+
{
780+
Dynamic caught = ctx->exception;
781+
ctx->exception = nullptr;
782+
HX_STACK_DO_THROW(caught);
783+
}
784+
763785
{
764786
switch(function->returnType)
765787
{

‎test/cppia/Client.hx‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ class ClientFoo implements IFoo {
1414
}
1515
}
1616

17+
class ClientThrower {
18+
19+
public static function boom():String {
20+
throw "boom";
21+
}
22+
23+
public static function fine():String {
24+
return "still here";
25+
}
26+
}
27+
1728
private interface IClientAbstract {
1829
function implemented():Int;
1930
}

‎test/cppia/cases/TestCommon.hx‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,25 @@ class TestCommon extends Test {
220220
Assert.equals('zero', Std.string(Type.createInstance(Type.resolveClass('ClientZeroArgToString'), [])));
221221
}
222222

223+
@:depends(testStatus)
224+
function testThrowReachesTheCaller() {
225+
final cls = Type.resolveClass('ClientThrower');
226+
227+
if (Assert.notNull(cls, 'Unable to resolve ClientThrower')) {
228+
var caught:String = null;
229+
230+
try {
231+
Reflect.callMethod(null, Reflect.field(cls, 'boom'), []);
232+
} catch (e:Dynamic) {
233+
caught = Std.string(e);
234+
}
235+
236+
Assert.equals('boom', caught, 'The throw did not reach the caller');
237+
Assert.equals('still here', Std.string(Reflect.callMethod(null, Reflect.field(cls, 'fine'), [])),
238+
'A later call answered null, so the exception was left on the context');
239+
}
240+
}
241+
223242
@:depends(testStatus)
224243
function testInterfaceCalling() {
225244
final obj : IFoo = Type.createInstance(Type.resolveClass('ClientFoo'), []);

0 commit comments

Comments
 (0)