Skip to content

Commit 2f6ccfa

Browse files
Tutez64claudetobil4sk
authored
[cppia] Report link error for new of unknown type (#1410)
* Report a cppia new with nothing to construct as a link failure. NewExpr::link left a NEW whose type was neither an array, a host class with a constructor, nor a script class, and both paths then dereferenced the null cppiaClass: runObject when the line ran, and genCode while the JIT compiled the module. It now reports "Could not link new" and throws "Bad link", as the other link failures do. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Test a cppia new of a class the host does not have. ClientUnlinked constructs an extern class, which the cppia module only names. Loading it must fail to link: before the fix, the JIT segfaulted while compiling the module, and without the JIT the load succeeded and left a null class to construct. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Adjust unlinked cppia test Move it out of TestCommon, since that is for testing values shared between client and host. --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Tobiasz Laskowski <tobil4sk@outlook.com>
1 parent e0e0cdc commit 2f6ccfa

5 files changed

Lines changed: 38 additions & 1 deletion

File tree

‎src/hx/cppia/Cppia.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,6 +1507,13 @@ struct NewExpr : public CppiaDynamicExpr
15071507
if (!type->cppiaClass && type->haxeClass.mPtr)
15081508
constructor = type->haxeClass.mPtr->mConstructArgs;
15091509

1510+
// Nothing to construct: runObject and genCode would dereference the null cppiaClass.
1511+
if (!type->arrayType && !constructor && !type->cppiaClass)
1512+
{
1513+
CPPIALOG("Could not link new %s\n", type->name.c_str());
1514+
throw "Bad link";
1515+
}
1516+
15101517
LinkExpressions(args,inModule);
15111518
return this;
15121519
}

‎test/cppia/ClientUnlinked.hx‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// The host does not have this class: a module constructing it must fail to link.
2+
extern class Unlinked
3+
{
4+
public function new();
5+
}
6+
7+
class ClientUnlinked
8+
{
9+
public static function main()
10+
{
11+
new Unlinked();
12+
}
13+
}

‎test/cppia/CppiaHost.hx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ class CppiaHost extends Test
88
// load cppia client
99
Host.main();
1010

11-
UTest.run(Common.CLIENT_CASES.concat([ new cases.TestCommon() ]));
11+
UTest.run(Common.CLIENT_CASES.concat([new cases.TestCommon(), new cases.TestLoad()]));
1212
}
1313
}

‎test/cppia/cases/TestLoad.hx‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package cases;
2+
3+
import utest.Test;
4+
import utest.Assert;
5+
6+
class TestLoad extends Test {
7+
function testNewOfUnlinkedClass() {
8+
final source = sys.io.File.getBytes('bin/unlinked.cppia');
9+
10+
Assert.exception(() -> cpp.cppia.Module.fromData(source.getData()), String, (e) -> e == 'Bad link', 'Linked a new of a class the host does not have');
11+
}
12+
}

‎test/cppia/compile-client.hxml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@ cases.client.TestClient
33
-D dll_import=host_classes.info
44
-L utest
55
--cppia bin/client.cppia
6+
7+
--next
8+
9+
-m ClientUnlinked
10+
--cppia bin/unlinked.cppia

0 commit comments

Comments
 (0)