diff --git a/src/hx/cppia/Cppia.cpp b/src/hx/cppia/Cppia.cpp index 4ef92a0a7..669a892c2 100644 --- a/src/hx/cppia/Cppia.cpp +++ b/src/hx/cppia/Cppia.cpp @@ -1507,6 +1507,13 @@ struct NewExpr : public CppiaDynamicExpr if (!type->cppiaClass && type->haxeClass.mPtr) constructor = type->haxeClass.mPtr->mConstructArgs; + // Nothing to construct: runObject and genCode would dereference the null cppiaClass. + if (!type->arrayType && !constructor && !type->cppiaClass) + { + CPPIALOG("Could not link new %s\n", type->name.c_str()); + throw "Bad link"; + } + LinkExpressions(args,inModule); return this; } diff --git a/test/cppia/ClientUnlinked.hx b/test/cppia/ClientUnlinked.hx new file mode 100644 index 000000000..c48769c07 --- /dev/null +++ b/test/cppia/ClientUnlinked.hx @@ -0,0 +1,13 @@ +// The host does not have this class: a module constructing it must fail to link. +extern class Unlinked +{ + public function new(); +} + +class ClientUnlinked +{ + public static function main() + { + new Unlinked(); + } +} diff --git a/test/cppia/CppiaHost.hx b/test/cppia/CppiaHost.hx index 46f93dc62..5eae36229 100644 --- a/test/cppia/CppiaHost.hx +++ b/test/cppia/CppiaHost.hx @@ -8,6 +8,6 @@ class CppiaHost extends Test // load cppia client Host.main(); - UTest.run(Common.CLIENT_CASES.concat([ new cases.TestCommon() ])); + UTest.run(Common.CLIENT_CASES.concat([new cases.TestCommon(), new cases.TestLoad()])); } } diff --git a/test/cppia/cases/TestLoad.hx b/test/cppia/cases/TestLoad.hx new file mode 100644 index 000000000..51553ac0d --- /dev/null +++ b/test/cppia/cases/TestLoad.hx @@ -0,0 +1,12 @@ +package cases; + +import utest.Test; +import utest.Assert; + +class TestLoad extends Test { + function testNewOfUnlinkedClass() { + final source = sys.io.File.getBytes('bin/unlinked.cppia'); + + Assert.exception(() -> cpp.cppia.Module.fromData(source.getData()), String, (e) -> e == 'Bad link', 'Linked a new of a class the host does not have'); + } +} diff --git a/test/cppia/compile-client.hxml b/test/cppia/compile-client.hxml index 686135e2c..18c0996d5 100644 --- a/test/cppia/compile-client.hxml +++ b/test/cppia/compile-client.hxml @@ -3,3 +3,8 @@ cases.client.TestClient -D dll_import=host_classes.info -L utest --cppia bin/client.cppia + +--next + +-m ClientUnlinked +--cppia bin/unlinked.cppia