소스 검색

[lua] Fix reflection issues in __init__ and static variables.

I was using "feature" statements to generate type containers for Ints,
    Floats, etc.  These were generated in __init__ blocks, but were
    generated after static variable declaration.  This meant that it
    wasn't possible to reference a Type by reflection in static variables
    or blocks.

    The fix is that the base Int, Float, etc. types are handled with
    special logic now.
Justin Donaldson 9 년 전
부모
커밋
360d33486d
5개의 변경된 파일43개의 추가작업 그리고 40개의 파일을 삭제
  1. 29 7
      src/generators/genlua.ml
  2. 13 0
      std/lua/_lua/_hx_classes.lua
  3. 0 3
      std/lua/_std/Array.hx
  4. 1 24
      std/lua/_std/Std.hx
  5. 0 6
      std/lua/_std/String.hx

+ 29 - 7
src/generators/genlua.ml

@@ -298,7 +298,10 @@ let rec should_wrap_int_op ctx op e1 e2 =
 let gen_constant ctx p = function
 	| TInt i -> print ctx "%ld" i
 	| TFloat s -> spr ctx s
-	| TString s -> print ctx "\"%s\"" (Ast.s_escape s)
+	| TString s -> begin
+	    add_feature ctx "use.string";
+	    print ctx "\"%s\"" (Ast.s_escape s)
+	end
 	| TBool b -> spr ctx (if b then "true" else "false")
 	| TNull -> spr ctx "nil"
 	| TThis -> spr ctx (this ctx)
@@ -851,13 +854,13 @@ and gen__init__hoist ctx e =
 				    | e :: _ -> gen__init__hoist ctx e)
 		    |_->());
 	| _ -> ()
-    end
+    end;
 
 and gen__init__impl ctx e =
     begin match e.eexpr with
 	| TVar (v,eo) ->
 		newline ctx;
-		gen_expr ~local:false ctx e
+		gen_expr ctx e
 	| TBlock el ->
 		List.iter (gen__init__impl ctx) el
 	| TCall (e, el) ->
@@ -875,7 +878,7 @@ and gen__init__impl ctx e =
 			end;
 			    );
 	| _ -> gen_block_element ctx e;
-    end
+    end;
 
 and gen_block_element ?(after=false) ctx e  =
     newline ctx;
@@ -1691,7 +1694,6 @@ let generate com =
 		| _ -> ()
 	) include_files;
 
-	sprln ctx "local _hxClasses = {}";
 	let vars = [] in
 	(* let vars = (if has_feature ctx "Type.resolveClass" || has_feature ctx "Type.resolveEnum" then ("_hxClasses = " ^ "{}") :: vars else vars) in *)
 	let vars = if has_feature ctx "may_print_enum"
@@ -1713,6 +1715,26 @@ let generate com =
 
 	List.iter (generate_type ctx) com.types;
 
+	(* If we use haxe Strings, patch Lua's string *)
+	if has_feature ctx "use.string" then begin
+	    sprln ctx "_G.getmetatable('').__index = String.__index;";
+	    sprln ctx "_G.getmetatable('').__add = function(a,b) return Std.string(a)..Std.string(b) end;";
+	    sprln ctx "_G.getmetatable('').__concat = getmetatable('').__add";
+	end;
+
+	(* Array is required, always patch it *)
+	sprln ctx "_hx_array_mt.__index = Array.prototype";
+
+	(* Generate statics *)
+	List.iter (generate_static ctx) (List.rev ctx.statics);
+
+	(* Localize init variables inside a do-block *)
+	(* Note: __init__ logic can modify static variables. *)
+	sprln ctx "do";
+	List.iter (gen__init__impl ctx) (List.rev ctx.inits);
+	newline ctx;
+	sprln ctx "end";
+
 	let rec chk_features e =
 		if is_dynamic_iterator ctx e then add_feature ctx "use._iterator";
 		match e.eexpr with
@@ -1721,7 +1743,9 @@ let generate com =
 		| _ ->
 			Type.iter chk_features e
 	in
+
 	List.iter chk_features ctx.inits;
+
 	List.iter (fun (_,_,e) -> chk_features e) ctx.statics;
 	if has_feature ctx "use._iterator" then begin
 		add_feature ctx "use._hx_bind";
@@ -1729,9 +1753,7 @@ let generate com =
 	end;
 	if has_feature ctx "use._hx_bind" then println ctx "_hx_bind = lua.Boot.bind";
 
-	List.iter (gen__init__impl ctx) (List.rev ctx.inits);
 	List.iter (generate_enumMeta_fields ctx) com.types;
-	List.iter (generate_static ctx) (List.rev ctx.statics);
 
 	(match com.main with
 	| None -> ()

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

@@ -0,0 +1,13 @@
+local _hxClasses = {}
+Int = (function() _hxClasses.Int = _hx_o({__fields__={__name__=true},__name__={"Int"}}); return _hxClasses.Int end)();
+Dynamic = (function() 
+_hxClasses.Dynamic = _hx_o({__fields__={__name__=true},__name__={"Dynamic"}}); return _hxClasses.Dynamic end)();
+Float = (function() 
+_hxClasses.Float = _hx_empty(); return _hxClasses.Float end)();
+Float.__name__ = {"Float"}
+Bool = (function() 
+_hxClasses.Bool = _hx_empty(); return _hxClasses.Bool end)();
+Bool.__ename__ = {"Bool"}
+Class = (function() 
+_hxClasses.Class = _hx_o({__fields__={__name__=true},__name__={"Class"}}); return _hxClasses.Class end)();
+Enum = _hx_empty();

+ 0 - 3
std/lua/_std/Array.hx

@@ -191,9 +191,6 @@ class Array<T> {
 	private static function __init__() : Void{
 		// table-to-array helper
 		haxe.macro.Compiler.includeFile("lua/_lua/_hx_tab_array.lua");
-		// attach the prototype for Array to the metatable for 
-		// the tabToArray helper function
-		untyped __lua__("_hx_array_mt.__index = Array.prototype");
 	}
 
 }

+ 1 - 24
std/lua/_std/Std.hx

@@ -80,34 +80,11 @@ import lua.NativeStringTools;
 	}
 
 	static function __init__() : Void untyped {
+		haxe.macro.Compiler.includeFile("lua/_lua/_hx_classes.lua");
 		__feature__("lua.Boot.getClass", String.prototype.__class__ = __feature__("Type.resolveClass",_hxClasses["String"] = String,String));
 		__feature__("lua.Boot.isClass", String.__name__ = __feature__("Type.getClassName", __lua_table__("String"),true));
 		__feature__("Type.resolveClass",_hxClasses["Array"] = Array);
 		__feature__("lua.Boot.isClass",Array.__name__ = __feature__("Type.getClassName",__lua_table__("Array"),true));
-		__feature__("Int.*",{
-			var Int = __feature__("Type.resolveClass", _hxClasses["Int"] = { __name__ : __lua_table__("Int") }, { __name__ : __lua_table__("Int") });
-		});
-		__feature__("Dynamic.*",{
-			var Dynamic = __feature__("Type.resolveClass", _hxClasses["Dynamic"] = { __name__ : __lua_table__("Dynamic") }, { __name__ : __lua_table__("Dynamic") });
-		});
-		__feature__("Float.*",{
-			var Float = __feature__("Type.resolveClass", _hxClasses["Float"]={}, {});
-			Float.__name__ = __lua_table__("Float");
-		});
-		__feature__("Bool.*",{
-			var Bool = __feature__("Type.resolveEnum",_hxClasses["Bool"] = {}, {});
-			Bool.__ename__ = __lua_table__("Bool");
-		});
-		__feature__("Class.*",{
-			var Class = __feature__("Type.resolveClass", _hxClasses["Class"] = { __name__ : __lua_table__("Class") }, { __name__ : __lua_table__("Class") });
-		});
-		__feature__("Enum.*",{
-			var Enum = {};
-		});
-		__feature__("Void.*",{
-			var Void = __feature__("Type.resolveEnum", _hxClasses["Void"] = { __ename__ : ["Void"] }, { __ename__ : ["Void"] });
-		});
-
 	}
 
 }

+ 0 - 6
std/lua/_std/String.hx

@@ -32,12 +32,6 @@ class String {
 
 	public function new(string:String) untyped {}
 
-	static function __init__() : Void untyped{
-		__lua__("getmetatable('').__index = String.__index;");
-		__lua__("getmetatable('').__add = function(a,b) return Std.string(a)..Std.string(b) end;");
-		__lua__("getmetatable('').__concat = getmetatable('').__add");
-	}
-
 	@:keep
 	static function __index(s:Dynamic, k:Dynamic) : Dynamic {
 		if (k == "length") return untyped __lua__("#s");