Skip to content

Commit

Permalink
[cpp] Null Check Interfaces (HaxeFoundation#11743)
Browse files Browse the repository at this point in the history
* Add a IsNull check

* escape quote

* Add new line and remove unneeded dynamic

* Guard interface null checks behind the same defines as class access

* Add null interface exception test

* move target guard to the right place...
  • Loading branch information
Aidan63 authored Aug 2, 2024
1 parent 8b18a2f commit 62854a7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/generators/gencpp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4715,6 +4715,12 @@ let gen_member_def ctx class_def is_static is_interface field =
let cast = "::hx::interface_cast< ::" ^ join_class_path_remap class_def.cl_path "::" ^ "_obj *>" in
output (" " ^ returnType ^ " (::hx::Object :: *_hx_" ^ remap_name ^ ")(" ^ argList ^ "); \n");
output (" static inline " ^ returnType ^ " " ^ remap_name ^ "( ::Dynamic _hx_" ^ commaArgList ^ ") {\n");
output (" #ifdef HXCPP_CHECK_POINTER\n");
output (" if (::hx::IsNull(_hx_)) ::hx::NullReference(\"Object\", false);\n");
output (" #ifdef HXCPP_GC_CHECK_POINTER\n");
output (" GCCheckPointer(_hx_.mPtr);\n");
output (" #endif\n");
output (" #endif\n");
output (" " ^ returnStr ^ "(_hx_.mPtr->*( " ^ cast ^ "(_hx_.mPtr->_hx_getInterface(" ^ (cpp_class_hash class_def) ^ ")))->_hx_" ^ remap_name ^ ")(" ^ cpp_arg_names args ^ ");\n }\n" );
end
| _ -> ( )
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/src/unit/issues/Issue11743.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package unit.issues;

import utest.Assert;

private interface IFoo {
function foo():Void;
}

class Issue11743 extends Test {

#if cpp
function test() {
final o : IFoo = null;

Assert.raises(() -> o.foo());
}
#end
}

0 comments on commit 62854a7

Please sign in to comment.