Selaa lähdekoodia

[lua] Handle more lua keyword escape issues. Close #5041

Justin Donaldson 9 vuotta sitten
vanhempi
commit
791635e8f4
3 muutettua tiedostoa jossa 32 lisäystä ja 12 poistoa
  1. 24 12
      src/generators/genlua.ml
  2. 2 0
      std/lua/Boot.hx
  3. 6 0
      std/lua/_lua/_hx_apply.lua

+ 24 - 12
src/generators/genlua.ml

@@ -399,11 +399,6 @@ let rec gen_call ctx e el in_value =
 			gen_value ctx e;
 			spr ctx ")";
 		end
-	| TCall ({eexpr = TField(e,((FInstance _ | FAnon _) as ef)) }, _), el ->
-		gen_value ctx e;
-		print ctx ":%s(" (field_name ef);
-		concat ctx "," (gen_value ctx) el;
-		spr ctx ")";
 	| TField ( { eexpr = TConst(TInt _ | TFloat _| TString _| TBool _) } as e , ((FInstance _ | FAnon _) as ef)), el ->
 		spr ctx ("(");
 		gen_value ctx e;
@@ -411,12 +406,29 @@ let rec gen_call ctx e el in_value =
 		concat ctx "," (gen_value ctx) el;
 		spr ctx ")";
 	| TField (e, ((FInstance _ | FAnon _ | FDynamic _) as ef)), el ->
-		gen_value ctx e;
-		spr ctx ":";
-		print ctx "%s" (field_name ef);
-		spr ctx "(";
-		concat ctx "," (gen_value ctx) el;
-		spr ctx ")"
+		let s = (field_name ef) in
+		if Hashtbl.mem kwds s || not (valid_lua_ident s) then begin
+		    match e.eexpr with
+		    |TNew _-> (
+			spr ctx "_hx_apply_self(";
+			gen_value ctx e;
+			print ctx ",\"%s\"" (field_name ef);
+			if List.length(el) > 0 then spr ctx ",";
+			concat ctx "," (gen_value ctx) el;
+			spr ctx ")";
+		    );
+		    |_ -> (
+			gen_value ctx e;
+			print ctx "%s(" (anon_field s);
+			concat ctx "," (gen_value ctx) (e::el);
+			spr ctx ")"
+		    )
+		end else begin
+		    gen_value ctx e;
+		    print ctx ":%s(" (field_name ef);
+		    concat ctx "," (gen_value ctx) el;
+		    spr ctx ")"
+		end;
 	| _ ->
 		gen_value ctx e;
 		spr ctx "(";
@@ -1323,7 +1335,7 @@ let gen_class_field ctx c f predelimit =
 		    let old = ctx.in_value, ctx.in_loop in
 		    ctx.in_value <- None;
 		    ctx.in_loop <- false;
-		    print ctx "'%s', function" (anon_field f.cf_name);
+		    print ctx "'%s', function" f.cf_name;
 		    print ctx "(%s) " (String.concat "," ("self" :: List.map ident (List.map arg_name f2.tf_args)));
 		    let fblock = fun_block ctx f2 e.epos in
 		    (match fblock.eexpr with

+ 2 - 0
std/lua/Boot.hx

@@ -358,5 +358,7 @@ class Boot {
 		haxe.macro.Compiler.includeFile("lua/_lua/_hx_function_to_instance_function.lua");
 		// static to instance class wrapper
 		haxe.macro.Compiler.includeFile("lua/_lua/_hx_static_to_instance_function.lua");
+		// simple apply method
+		haxe.macro.Compiler.includeFile("lua/_lua/_hx_apply.lua");
 	}
 }

+ 6 - 0
std/lua/_lua/_hx_apply.lua

@@ -0,0 +1,6 @@
+local function _hx_apply(f, ...)
+  return f(...)
+end
+local function _hx_apply_self(self, f, ...)
+  return self[f](self,...)
+end