Browse Source

Lua : More Array fixes and cleanup

The Haxe array object in Lua has a customized __newindex routine that
will automatically update the length.  However, it wasn't getting added
properly for situations like anonymous arrays.  I added all of the logic
for setting array internals to the defArray helper in Boot, and use that
now.
Justin Donaldson 10 years ago
parent
commit
cfbfce670f
2 changed files with 40 additions and 19 deletions
  1. 14 3
      std/lua/Boot.hx
  2. 26 16
      std/lua/_std/Array.hx

+ 14 - 3
std/lua/Boot.hx

@@ -63,10 +63,21 @@ class Boot {
 	}
 
 	@:keep
-	public static function defArray(tabobj: Dynamic, length : Int) : Array<Dynamic> {
-		untyped __lua__("tabobj.__methods = {Array.mt};
-			setmetatable(tabobj, {__index = lua.Boot.resolveMethod})");
+	public static function arrayNewIndex(tab:Dynamic, key:Int, value:Dynamic){
+		untyped rawset(tab, key, value);
+		if (key+1 > tab.length){
+			tab.length = key + 1;
+		}
+	}
+
+	@:keep
+	public static function defArray(tabobj: Dynamic, length : Int) : Array<Dynamic>  untyped {
+		tabobj.__methods = __lua__("{Array.mt}");
 		tabobj.length = length;
+		setmetatable(tabobj, {
+			__index : lua.Boot.resolveMethod,
+			__newindex : lua.Boot.arrayNewIndex
+		});
 		return tabobj;
 	}
 

+ 26 - 16
std/lua/_std/Array.hx

@@ -25,15 +25,13 @@ class Array<T> {
 	public var length(default,null) : Int;
 
 	public function new() : Void  {
-		this.length = 0;
-		untyped __lua__("self.__newindex = function (tab, key, value)
-		rawset(tab, key,value)
-		if key + 1> tab.length then
-			tab.length = key + 1
-		end
-		end");
+		lua.Boot.defArray(this,0);
+	}
+	public function concat( a : Array<T> ) : Array<T> {
+		var ret = this.copy();
+		for (i in a) ret.push(i);
+		return ret;
 	}
-	public function concat( a : Array<T> ) : Array<T> return [];
 	public function join( sep : String ) : String {
 		var sb = new StringBuf();
 		var first = true;
@@ -45,12 +43,23 @@ class Array<T> {
 		return sb.toString();
 	}
 
-	public function pop() : Null<T> return null;
+	public function pop() : Null<T> {
+		return this.length == 0 ? null : this[this.length-- -1];
+	}
 	public function push(x : T) : Int {
 		this[this.length++] = x;
 		return this.length;
 	}
-	public function reverse() : Void return;
+	public function reverse() : Void {
+		var tmp:T;
+		var i = 0;
+		while(i < Std.int(this.length/2)){
+			tmp = this[i];
+			this[i] = this[this.length-i-1];
+			this[this.length-i-1] = tmp;
+			i++;
+		}
+	}
 	public function shift() : Null<T> return null;
 	public function slice( pos : Int, ?end : Int ) : Array<T> return [];
 	public function sort( f : T -> T -> Int ) : Void return;
@@ -74,14 +83,15 @@ class Array<T> {
 
 	public function indexOf( x : T, ?fromIndex:Int ) : Int return 1;
 	public function lastIndexOf( x : T, ?fromIndex:Int ) : Int return 1;
-
-
 	public inline function copy() : Array<T> {
-		return (untyped this).slice();
+		return [for (i in this) i];
+	}
+	public function map<S>(f:T->S):Array<S> {
+		return [for (i in this) f(i)];
+	}
+	public function filter(f:T->Bool):Array<T> {
+		return [for (i in this) if (f(i)) i];
 	}
-
-	public function map<S>(f:T->S):Array<S> return [];
-	public function filter(f:T->Bool):Array<T> return [];
 	public inline function iterator() : Iterator<T> {
 		var cur_length = 0;
 		return {