Browse Source

Lua: Simplify defArray declarations.

We don't need to specify the length of an anonymous table declarations,
since the table maxn will consider this in the total count.  This
function was introdced in 2006, so it should have broad enough
support.
Justin Donaldson 10 years ago
parent
commit
81da433bd0
4 changed files with 6 additions and 8 deletions
  1. 1 3
      genlua.ml
  2. 2 2
      std/lua/Boot.hx
  3. 1 1
      std/lua/TableTools.hx
  4. 2 2
      std/lua/_std/Array.hx

+ 1 - 3
genlua.ml

@@ -492,13 +492,11 @@ and gen_expr ctx e =
 		gen_call ctx e el false
 		gen_call ctx e el false
 	| TArrayDecl el ->
 	| TArrayDecl el ->
 		spr ctx "lua.Boot.defArray({";
 		spr ctx "lua.Boot.defArray({";
-		let count = ref 0 in
 		List.iteri (fun i e ->
 		List.iteri (fun i e ->
-		    incr count;
 		    if (i == 0) then spr ctx "[0]="
 		    if (i == 0) then spr ctx "[0]="
 		    else spr ctx ", ";
 		    else spr ctx ", ";
 		    gen_value ctx e) el;
 		    gen_value ctx e) el;
-		print ctx " }, %i)" !count;
+		print ctx " })";
 	| TThrow e ->
 	| TThrow e ->
 		spr ctx "error(";
 		spr ctx "error(";
 		gen_value ctx e;
 		gen_value ctx e;

+ 2 - 2
std/lua/Boot.hx

@@ -96,8 +96,8 @@ class Boot {
 	}
 	}
 
 
 	@:keep
 	@:keep
-	public static function defArray(tabobj: Dynamic, ?length : Int) : Array<Dynamic>  untyped {
-		tabobj.length = length != null ? length : lua.TableTools.getn(tabobj);
+	public static function defArray(tabobj: Dynamic) : Array<Dynamic>  untyped {
+		tabobj.length = lua.TableTools.maxn(tabobj) + 1;
 		setmetatable(tabobj, {
 		setmetatable(tabobj, {
 			__index : __lua__("Array.prototype"),
 			__index : __lua__("Array.prototype"),
 			__newindex : lua.Boot.arrayNewIndex
 			__newindex : lua.Boot.arrayNewIndex

+ 1 - 1
std/lua/TableTools.hx

@@ -16,5 +16,5 @@ extern class TableTools {
 	@:overload(function<B>(table:Table<Int,B>):Void{})
 	@:overload(function<B>(table:Table<Int,B>):Void{})
 	public static function remove<B>(table:Table<Int,B>, ?pos:Int) : Void;
 	public static function remove<B>(table:Table<Int,B>, ?pos:Int) : Void;
 
 
-	public static function getn<B>(table: Table<Int,B>) : Int;
+	public static function maxn<B>(table: Table<Int,B>) : Int;
 }
 }

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

@@ -25,7 +25,7 @@ class Array<T> {
 	public var length(default,null) : Int;
 	public var length(default,null) : Int;
 
 
 	public function new() : Void  {
 	public function new() : Void  {
-		lua.Boot.defArray(this,0);
+		lua.Boot.defArray(this);
 	}
 	}
 	public function concat( a : Array<T> ) : Array<T> {
 	public function concat( a : Array<T> ) : Array<T> {
 		var ret = this.copy();
 		var ret = this.copy();
@@ -82,7 +82,7 @@ class Array<T> {
 			var a = this[i];
 			var a = this[i];
 			if (a == x){
 			if (a == x){
 				lua.TableTools.remove(cast this, i+1);
 				lua.TableTools.remove(cast this, i+1);
-				length-=1;
+				this.length-=1;
 				return true;
 				return true;
 			}
 			}
 		}
 		}