Browse Source

[lua] upgrade __lua_table__ so that it can accept both an array and a hash argument, and combine them in one table. Provide interface through lua.Boot.createTable

Justin Donaldson 9 years ago
parent
commit
d92801b855
4 changed files with 23 additions and 8 deletions
  1. 18 3
      src/generators/genlua.ml
  2. 1 1
      std/haxe/ds/Vector.hx
  3. 2 2
      std/lua/Boot.hx
  4. 2 2
      std/lua/_std/Std.hx

+ 18 - 3
src/generators/genlua.ml

@@ -358,9 +358,24 @@ let rec gen_call ctx e el in_value =
 		concat ctx ", " (gen_value ctx) el;
 		concat ctx ", " (gen_value ctx) el;
 		spr ctx ")";
 		spr ctx ")";
 	| TLocal { v_name = "__lua_table__" }, el ->
 	| TLocal { v_name = "__lua_table__" }, el ->
-		spr ctx "{";
-		concat ctx ", " (gen_value ctx) el;
-		spr ctx "}";
+		let count = ref 0 in
+		spr ctx "({";
+		List.iter (fun e ->
+		    (match e with
+		    | { eexpr = TArrayDecl arr } ->
+			    if (!count > 0 && List.length(arr) > 0) then spr ctx ",";
+			    concat ctx "," (gen_value ctx) arr;
+			    if List.length(arr) > 0 then incr count;
+		    | { eexpr = TObjectDecl fields } ->
+			    if (!count > 0 && List.length(fields) > 0) then spr ctx ",";
+			    concat ctx ", " (fun (f,e) ->
+				print ctx "%s = " (anon_field f);
+				gen_value ctx e
+			    ) fields;
+			    if List.length(fields) > 0 then incr count;
+		    | _ ->()
+		)) el;
+		spr ctx "})";
 	| TLocal { v_name = "__lua__" }, [{ eexpr = TConst (TString code) }] ->
 	| TLocal { v_name = "__lua__" }, [{ eexpr = TConst (TString code) }] ->
 		spr ctx (String.concat "\n" (ExtString.String.nsplit code "\r\n"))
 		spr ctx (String.concat "\n" (ExtString.String.nsplit code "\r\n"))
 	| TLocal { v_name = "__lua__" }, { eexpr = TConst (TString code); epos = p } :: tl ->
 	| TLocal { v_name = "__lua__" }, { eexpr = TConst (TString code); epos = p } :: tl ->

+ 1 - 1
std/haxe/ds/Vector.hx

@@ -74,7 +74,7 @@ abstract Vector<T>(VectorData<T>) {
 		#elseif python
 		#elseif python
 			this = python.Syntax.pythonCode("[{0}]*{1}", null, length);
 			this = python.Syntax.pythonCode("[{0}]*{1}", null, length);
 		#elseif lua
 		#elseif lua
-			this = lua.Boot.createVectorTable(length);
+			this = untyped __lua_table__({length:length});
 		#else
 		#else
 			this = [];
 			this = [];
 			untyped this.length = length;
 			untyped this.length = length;

+ 2 - 2
std/lua/Boot.hx

@@ -337,8 +337,8 @@ class Boot {
 	/*
 	/*
 	   Create an empty table.
 	   Create an empty table.
 	*/
 	*/
-	public inline static function createTable<K,V>() : Table<K,V> {
-		return untyped __lua__("{}");
+	public inline static function createTable<K,V>(?arr:Array<V>, ?hsh:Dynamic<V>) : Table<K,V> {
+		return untyped __lua_table__(arr,hsh);
 	}
 	}
 
 
 	/*
 	/*

+ 2 - 2
std/lua/_std/Std.hx

@@ -86,9 +86,9 @@ import lua.NativeStringTools;
 		// class reflection metadata
 		// class reflection metadata
 		haxe.macro.Compiler.includeFile("lua/_lua/_hx_classes.lua");
 		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.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__("lua.Boot.isClass", String.__name__ = __feature__("Type.getClassName", __lua_table__(["String"]),true));
 		__feature__("Type.resolveClass",_hxClasses["Array"] = Array);
 		__feature__("Type.resolveClass",_hxClasses["Array"] = Array);
-		__feature__("lua.Boot.isClass",Array.__name__ = __feature__("Type.getClassName",__lua_table__("Array"),true));
+		__feature__("lua.Boot.isClass",Array.__name__ = __feature__("Type.getClassName",__lua_table__(["Array"]),true));
 	}
 	}
 
 
 }
 }