Przeglądaj źródła

Lua : go with a table extern instead of an abstract

Justin Donaldson 10 lat temu
rodzic
commit
c77da6f0c4

+ 1 - 1
std/lua/Boot.hx

@@ -22,7 +22,7 @@
 package lua;
 package lua;
 
 
 class Boot {
 class Boot {
-	public static var unpack : Dynamic->lua.Table<Int,Dynamic> = untyped __lua__("function(...) return {...} end");
+	public static var unpack : Dynamic->Table<Int,Dynamic> = untyped __lua__("function(...) return {...} end");
 
 
 	static function __unhtml(s : String) {
 	static function __unhtml(s : String) {
 		return s.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
 		return s.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");

+ 18 - 21
std/lua/Table.hx

@@ -1,23 +1,20 @@
 package lua;
 package lua;
-/**
-  Abstract implementation of a standard Lua table.  Automatically converts to the
-  1-based indexing that Lua uses as default for int-keyed tables.
- **/
-abstract Table<A,B>({}) {
-	inline public function new() this = {};
-	@:arrayAccess inline function getIntV(k:Int) : B{
-		return untyped this[k+1];
-	}
-	@:arrayAccess inline function setIntV(k:Int, v:B){
-		untyped this[k+1] = v;
-		return v;
-	}
-	@:arrayAccess inline function getV(k:A) : B{
-		return untyped this[k];
-	}
-	@:arrayAccess inline function setV(k:A, v:B){
-		untyped this[k] = v;
-		return v;
-	}
-}
 
 
+@:native("_G.table") 
+extern class Table<A,B> implements ArrayAccess<B> {
+	@:overload(function<A,B>(table:Table<A,B>):Void{})
+	public static function concat<A,B>(table:Table<A,B>, ?sep:String) : String;
+
+	public static function foreach<A,B>(table:Table<A,B>, f:A->B->Void) : Void;
+	public static function foreachi<A,B>(table:Table<A,B>, f:A->B->Int->Void) : Void;
+
+	public static function sort<A,B>(table:Table<A,B>, ?order : A->A->Bool) : Void;
+
+	@:overload(function<B>(table:Table<Int,B>, value:B):Void{})
+	public static function insert<B>(table:Table<Int,B>, pos:Int, value: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 maxn<B>(table: Table<Int,B>) : Int;
+}

+ 0 - 20
std/lua/TableTools.hx

@@ -1,20 +0,0 @@
-package lua;
-
-@:native("_G.table") 
-extern class TableTools {
-	@:overload(function<A,B>(table:Table<A,B>):Void{})
-	public static function concat<A,B>(table:Table<A,B>, ?sep:String) : String;
-
-	public static function foreach<A,B>(table:Table<A,B>, f:A->B->Void) : Void;
-	public static function foreachi<A,B>(table:Table<A,B>, f:A->B->Int->Void) : Void;
-
-	public static function sort<A,B>(table:Table<A,B>, ?order : A->A->Bool) : Void;
-
-	@:overload(function<B>(table:Table<Int,B>, value:B):Void{})
-	public static function insert<B>(table:Table<Int,B>, pos:Int, value: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 maxn<B>(table: Table<Int,B>) : Int;
-}

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

@@ -86,10 +86,10 @@ class Array<T> {
 						this[0] = null;
 						this[0] = null;
 					} else {
 					} else {
 						this[0] = this[1];
 						this[0] = this[1];
-						lua.TableTools.remove(cast this, 1);
+						lua.Table.remove(cast this, 1);
 					}
 					}
 				} else {
 				} else {
-					lua.TableTools.remove(cast this, i);
+					lua.Table.remove(cast this, i);
 				}
 				}
 				this.length-=1;
 				this.length-=1;
 				return true;
 				return true;

+ 5 - 5
std/lua/_std/StringBuf.hx

@@ -30,7 +30,7 @@
 	it can be passed as argument to functions which modify it by appending more
 	it can be passed as argument to functions which modify it by appending more
 	values. However, the internal buffer cannot be modified.
 	values. However, the internal buffer cannot be modified.
 **/
 **/
-import lua.TableTools;
+import lua.Table;
 
 
 class StringBuf {
 class StringBuf {
 
 
@@ -66,7 +66,7 @@ class StringBuf {
 	**/
 	**/
 	public inline function add<T>( x : T ) : Void {
 	public inline function add<T>( x : T ) : Void {
 		var res = Std.string(x);
 		var res = Std.string(x);
-		TableTools.insert(b, res);
+		Table.insert(b, res);
 		length +=  res.length;
 		length +=  res.length;
 	}
 	}
 
 
@@ -77,7 +77,7 @@ class StringBuf {
 		unspecified.
 		unspecified.
 	**/
 	**/
 	public inline function addChar( c : Int ) : Void {
 	public inline function addChar( c : Int ) : Void {
-		TableTools.insert(b, String.fromCharCode(c));
+		Table.insert(b, String.fromCharCode(c));
 		length += 1;
 		length += 1;
 	}
 	}
 
 
@@ -95,7 +95,7 @@ class StringBuf {
 	**/
 	**/
 	public inline function addSub( s : String, pos : Int, ?len : Int) : Void {
 	public inline function addSub( s : String, pos : Int, ?len : Int) : Void {
 		var part = len == null ? s.substr(pos) : s.substr(pos, len);
 		var part = len == null ? s.substr(pos) : s.substr(pos, len);
-		TableTools.insert(b, part);
+		Table.insert(b, part);
 		length += part.length;
 		length += part.length;
 	}
 	}
 
 
@@ -105,7 +105,7 @@ class StringBuf {
 		The buffer is not emptied by this operation.
 		The buffer is not emptied by this operation.
 	**/
 	**/
 	public inline function toString() : String {
 	public inline function toString() : String {
-		return TableTools.concat(b);
+		return Table.concat(b);
 	}
 	}
 
 
 }
 }

+ 1 - 1
std/lua/_std/Type.hx

@@ -52,7 +52,7 @@ enum ValueType {
 
 
 	public static function getClassName( c : Class<Dynamic> ) : String {
 	public static function getClassName( c : Class<Dynamic> ) : String {
 		if (untyped c.__name__ == null) return null;
 		if (untyped c.__name__ == null) return null;
-		return lua.TableTools.concat(untyped c.__name__,'.');
+		return lua.Table.concat(untyped c.__name__,'.');
 	}
 	}
 
 
 	public static function getEnumName( e : Enum<Dynamic> ) : String {
 	public static function getEnumName( e : Enum<Dynamic> ) : String {