Explorar el Código

Lua: Add some more tools for dealing with pairs/ipairs

Justin Donaldson hace 9 años
padre
commit
b2823f3a78
Se han modificado 3 ficheros con 41 adiciones y 15 borrados
  1. 6 5
      std/lua/Boot.hx
  2. 20 0
      std/lua/PairTools.hx
  3. 15 10
      std/lua/_std/Reflect.hx

+ 6 - 5
std/lua/Boot.hx

@@ -202,7 +202,6 @@ class Boot {
 			case "function": "<function>";
 			case "thread"  : "<thread>";
 			case "table": {
-				var mt : Dynamic = untyped Lua.getmetatable(o);
 			    if (Reflect.hasField(o,"__enum__")) printEnum(o);
 				else if (o.toString != null && !__instanceof(o,Array)) o.toString();
 				else if (__instanceof(o, Array)) {
@@ -215,10 +214,12 @@ class Boot {
 				else if (Reflect.hasField(o,"__class__")) printClass(o,s+1);
 				else if (Lua.next(o) == null) "{}";
 				else {
-					(o : Table<Dynamic,Dynamic>).pairsFold(function(a,b,c){
-						if (c != "{") c+= ", ";
-						return c + __string_rec(a, s + 1) + ': ' + __string_rec(b, s + 1);
-					},"{") + "}";
+					var fields = Reflect.fields(o);
+					var buffer = new StringBuf();
+					for (f in fields){
+						buffer.add('${Std.string(f)} : ${untyped Std.string(o[f])}');
+					}
+					buffer.toString();
 				}
 			};
 			default : {

+ 20 - 0
std/lua/PairTools.hx

@@ -27,4 +27,24 @@ class PairTools {
 		untyped __lua__("for k,v in _G.pairs(table) do seed = func(k,v,seed) end");
 		return untyped __lua__("seed");
 	}
+
+	public static function ipairsConcat<T>(table1:Table<Int,T>, table2:Table<Int,T>){
+		var ret:Table<Int,T> = cast {};
+		ipairsFold(table1, function(a,b,c:Table<Int,T>){ c[a] = b; return c;}, ret);
+		var size = lua.Table.maxn(ret);
+		ipairsFold(table2, function(a,b,c:Table<Int,T>){ c[a + size] = b; return c;}, ret);
+		return ret;
+	}
+
+	public static function pairsMerge<A,B>(table1:Table<A,B>, table2:Table<A,B>){
+		var ret = copy(table1);
+		pairsEach(table2, function(a,b:B) ret[cast a] = b);
+		return ret;
+	}
+
+	public static function copy<A,B>(table1:Table<A,B>) : Table<A,B> {
+		var ret : Table<A,B> = cast {};
+		untyped __lua__("for k,v in _G.pairs(table1) do ret[k] = v end");
+		return ret;
+	}
 }

+ 15 - 10
std/lua/_std/Reflect.hx

@@ -22,6 +22,7 @@
 import lua.Lua;
 import lua.Boot;
 @:coreApi class Reflect {
+	static var hidden = ["__id__", "hx__closures", "super", "__index", "new", "prototype"];
 
 	public inline static function hasField( o : Dynamic, field : String ) : Bool {
 		// TODO: Lua can't detect fields that are set to null, figure out a workaround.
@@ -36,29 +37,33 @@ import lua.Boot;
 		o[field] = value;
 	}
 
-	public static inline function getProperty( o : Dynamic, field : String ) : Dynamic untyped {
-		var tmp;
-		return if( o == null ) __define_feature__("Reflect.getProperty",null) else if( o.__properties__ && (tmp=o.__properties__["get_"+field]) ) o[tmp]() else o[field];
+	public static inline function getProperty( o : Dynamic, field : String ) : Dynamic {
+		var tmp : Dynamic;
+		return if( o == null ) untyped __define_feature__("Reflect.getProperty",null) else if( o.__properties__ && (tmp=Reflect.field(o.__properties__,"get_"+field)) ) callMethod(o,Reflect.field(o,tmp), []) else Reflect.field(o,field);
 	}
 
 	public static inline function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
-		var tmp;
-		if( o.__properties__ && (tmp=o.__properties__["set_"+field]) ) o[tmp](value) else o[field] = __define_feature__("Reflect.setProperty",value);
+		var tmp : String;
+		if( o.__properties__ && (tmp=o.__properties__["set_"+field]) ) callMethod(o,untyped tmp, [value]) else o[field] = __define_feature__("Reflect.setProperty",value);
 	}
 
 	public inline static function callMethod( o : Dynamic, func : haxe.Constraints.Function, args : Array<Dynamic> ) : Dynamic  {
 		if (args == null || args.length == 0){
 			return func(o);
 		} else {
-			var new_args = [o].concat(args);
-			return func(lua.Table.unpack(new_args,1,lua.Table.maxn(untyped new_args)));
+			var new_args:lua.Table<Int,String> = untyped __lua_table__(o);
+			// Lua's table concat will skip the first element since it starts from 1
+			new_args[2] = args[0];
+			new_args = lua.PairTools.ipairsConcat(new_args, cast args);
+			return func(lua.Table.unpack(new_args));
 		}
 	}
 
 	public static function fields( o : Dynamic ) : Array<String> {
-		var a = [];
-		untyped __lua__("for i,v in pairs(o) do a:push(i) end");
-		return a;
+		return lua.PairTools.pairsFold(o, function(a,b,c:Array<String>){
+			if (hidden.indexOf(a) == -1) c.push(cast a);
+			return c;
+		}, []);
 	}
 
 	public static function isFunction( f : Dynamic ) : Bool {