Переглянути джерело

[lua] make the staticToInstance and funcToField helpers features as well

Justin Donaldson 9 роки тому
батько
коміт
9ee06d98f9

+ 34 - 3
src/generators/genlua.ml

@@ -1017,6 +1017,7 @@ and gen_value ctx e =
 	(* TODO: this is just a hack because this specific case is a TestReflect unit test. I don't know how to address this properly
 	   at the moment. - Simon *)
 	| TCast ({ eexpr = TTypeExpr mt } as e1, None) when (match mt with TClassDecl {cl_path = ([],"Array")} -> false | _ -> true) ->
+	    add_feature ctx "use._hx_staticToInstance";
 	    spr ctx "_hx_staticToInstance(";
 	    gen_expr ctx e1;
 	    spr ctx ")";
@@ -1139,13 +1140,15 @@ and gen_tbinop ctx op e1 e2 =
 	    | TField(e3, FInstance _ ), TField(e4, FClosure _ )  ->
 		gen_value ctx e1;
 		print ctx " %s " (Ast.s_binop op);
-		spr ctx "_hx_functionToInstanceFunction(";
+		add_feature ctx "use._hx_funcToField";
+		spr ctx "_hx_funcToField(";
 		gen_value ctx e2;
 		spr ctx ")";
 	    | TField(_, FInstance _ ), TLocal t  when (is_function_type ctx t.v_type)   ->
 		gen_value ctx e1;
 		print ctx " %s " (Ast.s_binop op);
-		spr ctx "_hx_functionToInstanceFunction(";
+		add_feature ctx "use._hx_funcToField";
+		spr ctx "_hx_funcToField(";
 		gen_value ctx e2;
 		spr ctx ")";
 	    | _ ->
@@ -1806,7 +1809,7 @@ let generate com =
 	List.iter (generate_type_forward ctx) com.types; newline ctx;
 
 	(* Generate some dummy placeholders for utility libs that may be required*)
-	println ctx "local _hx_bind,_hx_bit";
+	println ctx "local _hx_bind,_hx_bit, _hx_staticToInstance, _hx_funcToField";
 
 	if has_feature ctx "use._bitop" || has_feature ctx "lua.Boot.clamp" then begin
 	    println ctx "pcall(require, 'bit32') pcall(require, 'bit')";
@@ -1884,6 +1887,34 @@ let generate com =
 	    println ctx "end";
 	end;
 
+	if has_feature ctx "use._hx_staticToInstance" then begin
+	    println ctx "_hx_staticToInstance = function (tab)";
+	    println ctx "  return setmetatable({}, {";
+	    println ctx "    __index = function(t,k)";
+	    println ctx "      if type(rawget(tab,k)) == 'function' then ";
+	    println ctx "	return function(self,...)";
+	    println ctx "	  return rawget(tab,k)(...)";
+	    println ctx "	end";
+	    println ctx "      else";
+	    println ctx "	return rawget(tab,k)";
+	    println ctx "      end";
+	    println ctx "    end";
+	    println ctx "  })";
+	    println ctx "end";
+	end;
+
+	if has_feature ctx "use._hx_funcToField" then begin
+	    println ctx "_hx_funcToField = function(f)";
+	    println ctx "  if type(f) == 'function' then ";
+	    println ctx "    return function(self,...) ";
+	    println ctx "      return f(...) ";
+	    println ctx "    end";
+	    println ctx "  else ";
+	    println ctx "    return f";
+	    println ctx "  end";
+	    println ctx "end";
+	end;
+
 	if has_feature ctx "Math.random" then begin
 	    println ctx "_G.math.randomseed(_G.os.time());"
 	end;

+ 0 - 4
std/lua/Boot.hx

@@ -370,10 +370,6 @@ class Boot {
 	}
 
 	public static function __init__(){
-		// anonymous to instance method wrapper
-		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");
 	}

+ 0 - 10
std/lua/_lua/_hx_function_to_instance_function.lua

@@ -1,10 +0,0 @@
-local function _hx_functionToInstanceFunction(f)
-  if type(f) == "function" then 
-    return function(self,...) 
-      return f(...) 
-    end
-  else 
-    return f
-  end
-end
-    

+ 0 - 13
std/lua/_lua/_hx_static_to_instance_function.lua

@@ -1,13 +0,0 @@
-local function _hx_staticToInstance(tab)
-  return setmetatable({}, {
-    __index = function(t,k)
-      if type(rawget(tab,k)) == 'function' then 
-	return function(self,...)
-	  return rawget(tab,k)(...)
-	end
-      else
-	return rawget(tab,k)
-      end
-    end
-  })
-end