Ver Fonte

[cpp] Use hx::Throw instead of creating a temp dynamic when accessing a known-null object. Closes https://github.com/HaxeFoundation/hxcpp/issues/193

Hugh há 10 anos atrás
pai
commit
a5f45ec708
2 ficheiros alterados com 34 adições e 1 exclusões
  1. 2 1
      gencpp.ml
  2. 32 0
      tests/unit/src/unit/hxcpp_issues/Issue193.hx

+ 2 - 1
gencpp.ml

@@ -2160,7 +2160,8 @@ and gen_expression ctx retval expression =
       end
    (* Get precidence matching haxe ? *)
    | TBinop (op,expr1,expr2) -> gen_bin_op op expr1 expr2
-   | TField (expr,_) | TEnumParameter (expr,_,_) when (is_null expr) -> output "Dynamic()"
+   | TField (expr,_) | TEnumParameter (expr,_,_) when (is_null expr) ->
+         output "hx::Throw(HX_CSTRING(\"Invalid field access on null object\"))"
    | TEnumParameter (expr,ef,i) ->
       let enum = match follow ef.ef_type with
          | TEnum(en,_) | TFun(_,TEnum(en,_)) -> en

+ 32 - 0
tests/unit/src/unit/hxcpp_issues/Issue193.hx

@@ -0,0 +1,32 @@
+package unit.hxcpp_issues;
+
+import haxe.macro.Context;
+import haxe.macro.Expr;
+
+#if !macro
+@:build(unit.hxcpp_issues.Issue193.build())
+#end
+class Issue193 extends Test
+{
+   var field:Int;
+
+   inline function incField()  field++;
+
+   inline static function doInc(t:Issue193) t.incField();
+
+   macro public static function build ():Array<Field> {
+      var fields = Context.getBuildFields();
+      var newField = macro { 
+        doInc(null);
+      };
+      fields.push ({ name: "genField", access: [ APublic ], kind: FFun({ args: [], expr: newField, params: [], ret: null }), pos: Context.currentPos() });
+      return fields;
+   }
+
+   function test()
+   {
+      var field = Reflect.field(this,"genField");
+      t(field!=null);
+   }
+}
+