Ver Fonte

[cpp] Null Check Interfaces (#11743)

* 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...
Aidan Lee há 11 meses atrás
pai
commit
313ffef477
2 ficheiros alterados com 24 adições e 0 exclusões
  1. 6 0
      src/generators/gencpp.ml
  2. 18 0
      tests/unit/src/unit/issues/Issue11743.hx

+ 6 - 0
src/generators/gencpp.ml

@@ -4719,6 +4719,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
       | _  ->  ( )

+ 18 - 0
tests/unit/src/unit/issues/Issue11743.hx

@@ -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
+}