Bladeren bron

Lua : Fix for null values in string concatenation

Briefly, Lua doesn't allow for arbitrary string concatenation with nil
values (equivalent to haxe null).  Haxe expects this (or at least, it
tests for it in the unit tests).  In order to support this, we have to
check for an add operation where at least one value is a string, and
then wrap both arguments in a method that will guarantee a string
output.  For the Lua target, this method should be Std.string.  It
is generated accordingly in this case using the logic here.
Justin Donaldson 10 jaren geleden
bovenliggende
commit
12de2a1c29
1 gewijzigde bestanden met toevoegingen van 18 en 8 verwijderingen
  1. 18 8
      genlua.ml

+ 18 - 8
genlua.ml

@@ -956,15 +956,25 @@ and gen_tbinop ctx op e1 e2 =
     | Ast.OpXor | Ast.OpAnd  | Ast.OpShl | Ast.OpShr | Ast.OpUShr | Ast.OpOr ->
         gen_bitop ctx op e1 e2;
     | _->
-        gen_value ctx e1;
-        (match op with
+        match op with
             | Ast.OpAdd when (is_string_expr e1 || is_string_expr e2) ->
-                    print ctx " .. "
-            | Ast.OpNotEq -> print ctx " ~= "
-            | Ast.OpBoolAnd -> print ctx " and "
-            | Ast.OpBoolOr -> print ctx " or "
-            | _ -> print ctx " %s " (Ast.s_binop op));
-        gen_value ctx e2)
+		    spr ctx "Std.string(";
+		    gen_value ctx e1;
+		    spr ctx ") ";
+		    print ctx " .. ";
+		    spr ctx "Std.string(";
+		    gen_value ctx e2;
+		    spr ctx ") ";
+	    | _ -> begin
+		    gen_value ctx e1;
+	    	    (match op with
+			| Ast.OpNotEq -> print ctx " ~= ";
+			| Ast.OpBoolAnd -> print ctx " and ";
+			| Ast.OpBoolOr -> print ctx " or ";
+			| _ -> print ctx " %s " (Ast.s_binop op));
+		    gen_value ctx e2;
+	    end;
+    );
 
 and gen_bitop ctx op e1 e2 =
     print ctx "bit.%s(" (match op with