2
0
Эх сурвалжийг харах

Lua: Use includeFile to inline class-specific lua helper methods

Justin Donaldson 9 жил өмнө
parent
commit
94f291eb08

+ 10 - 72
genlua.ml

@@ -1649,62 +1649,16 @@ let generate com =
 	if has_feature ctx "Class" || has_feature ctx "Type.getClassName" then add_feature ctx "lua.Boot.isClass";
 	if has_feature ctx "Enum" || has_feature ctx "Type.getEnumName" then add_feature ctx "lua.Boot.isEnum";
 
-	sprln ctx "local _hx_bit, _hx_bit_raw";
-	sprln ctx "pcall(require, 'bit32') pcall(require, 'bit') _hx_bit_raw = bit or bit32";
-	sprln ctx "if type(jit) ~= 'table' then";
-	sprln ctx "  _hx_bit = _hx_bit_raw";
-	sprln ctx "else ";
-	sprln ctx " local function _hx_bitfix(v)return(v >= 0)and v or(4294967296 + v)end";
-	sprln ctx " _hx_bit = setmetatable({}, { __index = function(t,k) return function(x,y) return _hx_bitfix(rawget(_hx_bit_raw,k)(x,y)) end end})";
-	sprln ctx "end";
-	sprln ctx "local _hx_print = print or (function()end)";
-	sprln ctx "table.pack=table.pack or pack or function(...)return{n=select('#',...),...}end";
-	sprln ctx "table.unpack=table.unpack or unpack or function(t, i)i = i or 1 if t[i] ~= nil then return t[i],table.unpack(t, i + 1)end end";
-	sprln ctx "table.maxn=table.maxn or function(t) local maxn=0 for i in pairs(t)do maxn=type(i)=='number'and i>maxn and i or maxn end return maxn end";
-
-	sprln ctx "local function _hx_anon_newindex(t,k,v) t.__fields__[k] = true; rawset(t,k,v); end";
-	sprln ctx "local _hx_anon_mt = {__newindex=_hx_anon_newindex}";
-
-	sprln ctx "local function _hx_anon(...)";
-	sprln ctx "   local __fields__ = {};";
-	sprln ctx "   local ret = {__fields__ = __fields__};";
-	sprln ctx "   local max = select('#',...);";
-	sprln ctx "   local tab = {...};";
-	sprln ctx "   local cur = 1;";
-	sprln ctx "   while cur < max do";
-	sprln ctx "      local v = tab[cur];";
-	sprln ctx "      __fields__[v] = true;";
-	sprln ctx "      ret[v] = tab[cur+1];";
-	sprln ctx "      cur = cur + 2";
-	sprln ctx "   end";
-	sprln ctx "   return setmetatable(ret, _hx_anon_mt)";
-	sprln ctx "end";
-
-	sprln ctx "local function _hx_empty()";
-	sprln ctx "   return setmetatable({__fields__ = {}}, _hx_anon_mt)";
-	sprln ctx "end";
-
-	sprln ctx "local function _hx_o(obj)";
-	sprln ctx "   return setmetatable(obj, _hx_anon_mt)";
-	sprln ctx "end";
-
-	sprln ctx "local function _hx_new(prototype)";
-	sprln ctx "   return setmetatable({__fields__ = {}}, {__newindex=_hx_anon_newindex, __index=prototype})";
-	sprln ctx "end";
-
-	sprln ctx "local function _hx_staticToInstance(tab)";
-	sprln ctx "   return setmetatable({}, {";
-	sprln ctx "	__index = function(t,k)";
-	sprln ctx "	    if type(rawget(tab,k)) == 'function' then ";
-	sprln ctx "		return function(self,...)";
-	sprln ctx "		    return rawget(tab,k)(...)";
-	sprln ctx "		end";
-	sprln ctx "	    else";
-	sprln ctx "		return rawget(tab,k)";
-	sprln ctx "	    end";
-	sprln ctx "	end";
-	sprln ctx "   })";
-	sprln ctx "end";
+	let include_files = List.rev com.include_files in
+
+	List.iter (fun file ->
+		match file with
+		| path, "top" ->
+			let file_content = Std.input_file ~bin:true (fst file) in
+			print ctx "%s\n" file_content;
+			()
+		| _ -> ()
+	) include_files;
 
 	sprln ctx "local _hxClasses = {}";
 	let vars = [] in
@@ -1722,28 +1676,12 @@ let generate com =
 
 	List.iter (generate_type_forward ctx) com.types; newline ctx;
 
-	sprln ctx "local _hx_array_mt = {";
-	sprln ctx "   __newindex = function(t,k,v)";
-	sprln ctx "       if type(k) == 'number' and k >= t.length then";
-	sprln ctx "          t.length = k + 1";
-	sprln ctx "       end";
-	sprln ctx "       rawset(t,k,v)";
-	sprln ctx "   end";
-	sprln ctx "}";
-
-	sprln ctx "local function _hx_tabArray(tab,length)";
-	sprln ctx "   tab.length = length";
-	sprln ctx "   return setmetatable(tab, _hx_array_mt)";
-	sprln ctx "end";
-
 	spr ctx "local _hx_bind";
 	List.iter (gen__init__hoist ctx) (List.rev ctx.inits); newline ctx;
 	ctx.inits <- []; (* reset inits *)
 
 	List.iter (generate_type ctx) com.types;
 
-	sprln ctx "_hx_array_mt.__index = Array.prototype";
-
 	let rec chk_features e =
 		if is_dynamic_iterator ctx e then add_feature ctx "use._iterator";
 		match e.eexpr with

+ 3 - 0
std/lua/Bit.hx

@@ -13,4 +13,7 @@ extern class Bit {
 	public static function rshift(x:Float, places:Int) : Int;
 	public static function arshift(x:Float, places:Int) : Int;
 	public static function mod(numerator:Float, denominator:Float) : Int;
+	public static function __init__() : Void {
+		haxe.macro.Compiler.includeFile("lua/_lua/_hx_bit.lua");
+	}
 }

+ 8 - 0
std/lua/Boot.hx

@@ -21,7 +21,10 @@
  */
 package lua;
 
+// Bit and Table must be imported for basic haxe datatypes to work.
+import lua.Bit;
 import lua.Table;
+
 import haxe.Constraints.Function;
 using lua.PairTools;
 
@@ -286,4 +289,9 @@ class Boot {
 	public static function createTable<K,V>() : Table<K,V> {
 		return untyped __lua__("{}");
 	}
+
+	public static function __init__(){
+		// static to instance method wrapper
+		haxe.macro.Compiler.includeFile("lua/_lua/_hx_static_to_instance.lua");
+	}
 }

+ 4 - 0
std/lua/Lua.hx

@@ -20,4 +20,8 @@ extern class Lua {
 	public static function select(n:Dynamic, rest:Rest<Dynamic>) : Dynamic;
 	public static function rawget<K,V>(t:Table<K,V>, k:K) : V;
 	public static function rawset<K,V>(t:Table<K,V>, k:K, v:V) : Void;
+	private static function __init__() : Void {
+		// print polyfill
+		haxe.macro.Compiler.includeFile("lua/_lua/_hx_print.lua");
+	}
 }

+ 9 - 0
std/lua/Table.hx

@@ -19,4 +19,13 @@ extern class Table<A,B> implements ArrayAccess<B> implements Dynamic<B> {
 	public static function maxn<B>(table: Table<Int,B>) : Int;
 	public static function pack<T>(args:T) : Table<Int,T>;
 	public static function unpack(arg:lua.Table<Dynamic,Dynamic>, ?min:Int, ?max:Int) : Dynamic;
+	private static function __init__() : Void {
+		// lua table polyfills
+		haxe.macro.Compiler.includeFile("lua/_lua/_hx_table_polyfill.lua");
+
+		// lua workarounds for basic anonymous object functionality
+		// (built on tables)
+		haxe.macro.Compiler.includeFile("lua/_lua/_hx_anon.lua");
+
+	}
 }

+ 28 - 0
std/lua/_lua/_hx_anon.lua

@@ -0,0 +1,28 @@
+local function _hx_anon_newindex(t,k,v) t.__fields__[k] = true; rawset(t,k,v); end
+local _hx_anon_mt = {__newindex=_hx_anon_newindex}
+local function _hx_anon(...)
+  local __fields__ = {};
+  local ret = {__fields__ = __fields__};
+  local max = select('#',...);
+  local tab = {...};
+  local cur = 1;
+  while cur < max do
+    local v = tab[cur];
+    __fields__[v] = true;
+    ret[v] = tab[cur+1];
+    cur = cur + 2
+  end
+  return setmetatable(ret, _hx_anon_mt)
+end
+
+local function _hx_empty()
+  return setmetatable({__fields__ = {}}, _hx_anon_mt)
+end
+
+local function _hx_o(obj)
+  return setmetatable(obj, _hx_anon_mt)
+end
+
+local function _hx_new(prototype)
+  return setmetatable({__fields__ = {}}, {__newindex=_hx_anon_newindex, __index=prototype})
+end

+ 9 - 0
std/lua/_lua/_hx_bit.lua

@@ -0,0 +1,9 @@
+local _hx_bit
+pcall(require, 'bit32') pcall(require, 'bit')
+if type(jit) == 'table' then
+  local _hx_bit_raw = bit or bit32
+  local function _hx_bitfix(v) return (v >= 0) and v or (4294967296 + v) end
+  _hx_bit = setmetatable({},{__index = function(t,k) return function(...) return _hx_bitfix(rawget(_hx_bit_raw,k)(...)) end end})
+else
+  _hx_bit = bit or bit32
+end

+ 1 - 0
std/lua/_lua/_hx_print.lua

@@ -0,0 +1 @@
+local _hx_print = print or (function()end)

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

@@ -0,0 +1,13 @@
+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

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

@@ -0,0 +1,13 @@
+local _hx_array_mt = {
+  __newindex = function(t,k,v)
+    if type(k) == 'number' and k >= t.length then
+      t.length = k + 1
+    end
+    rawset(t,k,v)
+  end
+}
+
+local function _hx_tabArray(tab,length)
+  tab.length = length
+  return setmetatable(tab, _hx_array_mt)
+end

+ 4 - 0
std/lua/_lua/_hx_table_polyfill.lua

@@ -0,0 +1,4 @@
+
+table.pack=table.pack or pack or function(...) return { n=select('#',...),...} end
+table.unpack=table.unpack or unpack or function(t, i) i = i or 1 if t[i] ~= nil then return t[i], table.unpack(t, i + 1) end end
+table.maxn=table.maxn or function(t) local maxn=0 for i in pairs(t) do maxn=type(i)=='number'and i>maxn and i or maxn end return maxn end

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

@@ -188,5 +188,12 @@ class Array<T> {
 			next : function() return this[cur_length++]
 		}
 	}
+	private static function __init__() : Void{
+		// table-to-array helper
+		haxe.macro.Compiler.includeFile("lua/_lua/_hx_tabArray.lua");
+		// attach the prototype for Array to the metatable for 
+		// the tabToArray helper function
+		untyped __lua__("_hx_array_mt.__index = Array.prototype");
+	}
 
 }