Browse Source

[lua] featurize some table polyfills

Justin Donaldson 9 years ago
parent
commit
ee417dde87
5 changed files with 21 additions and 14 deletions
  1. 13 1
      src/generators/genlua.ml
  2. 6 2
      std/lua/Boot.hx
  3. 0 5
      std/lua/Table.hx
  4. 0 4
      std/lua/_lua/_hx_table_polyfill.lua
  5. 2 2
      std/lua/_std/Reflect.hx

+ 13 - 1
src/generators/genlua.ml

@@ -354,6 +354,8 @@ let rec gen_call ctx e el in_value =
 		spr ctx "(";
 		concat ctx ", " (gen_value ctx) el;
 		spr ctx ")";
+	| TLocal { v_name = "__lua_length__" }, [e]->
+		spr ctx "#"; gen_value ctx e;
 	| TLocal { v_name = "__lua_table__" }, el ->
 		let count = ref 0 in
 		spr ctx "({";
@@ -1809,7 +1811,7 @@ let generate com =
 	List.iter (generate_type_forward ctx) com.types; newline ctx;
 
 	(* Generate some dummy placeholders for utility libs that may be required*)
-	println ctx "local _hx_bind,_hx_bit, _hx_staticToInstance, _hx_funcToField";
+	println ctx "local _hx_bind, _hx_bit, _hx_staticToInstance, _hx_funcToField, _hx_maxn";
 
 	if has_feature ctx "use._bitop" || has_feature ctx "lua.Boot.clamp" then begin
 	    println ctx "pcall(require, 'bit32') pcall(require, 'bit')";
@@ -1919,6 +1921,16 @@ let generate com =
 	    println ctx "_G.math.randomseed(_G.os.time());"
 	end;
 
+	if has_feature ctx "use._hx_maxn" then begin
+	    println ctx "_hx_maxn = table.maxn or function(t)";
+	    println ctx "  local maxn=0;";
+	    println ctx "  for i in pairs(t) do";
+	    println ctx "    maxn=type(i)=='number'and i>maxn and i or maxn";
+	    println ctx "  end";
+	    println ctx "  return maxn";
+	    println ctx "end;";
+	end;
+
 
 	List.iter (generate_enumMeta_fields ctx) com.types;
 

+ 6 - 2
std/lua/Boot.hx

@@ -223,7 +223,7 @@ class Boot {
 	   Define an array from the given table
 	*/
 	public inline static function defArray<T>(tab: Table<Int,T>, ?length : Int) : Array<T> {
-		if (length == null) length = Table.maxn(tab) + 1; // maxn doesn't count 0 index
+		if (length == null) length = tableMaxN(tab) + 1; // maxn doesn't count 0 index
 		return untyped _hx_tab_array(tab, length);
 	}
 
@@ -295,7 +295,7 @@ class Boot {
 		else if (cl1 == cl2) return true;
 		else if (untyped cl1.__interfaces__ != null) {
 			var intf = untyped cl1.__interfaces__;
-			for (i in 1...(Table.maxn(intf) + 1)){
+			for (i in 1...( tableMaxN(intf) + 1)){
 				// check each interface, including extended interfaces
 				if (extendsOrImplements(intf[1], cl2)) return true;
 			}
@@ -304,6 +304,10 @@ class Boot {
 		return extendsOrImplements(untyped cl1.__super__, cl2);
 	}
 
+	public inline static function tableMaxN(t:Table.AnyTable) : Int {
+		return untyped __define_feature__("use._hx_maxn", _hx_maxn(t));
+	}
+
 	/*
 	   Create an empty table.
 	*/

+ 0 - 5
std/lua/Table.hx

@@ -23,13 +23,8 @@ extern class Table<A,B> implements ArrayAccess<B> implements Dynamic<B> {
 	@: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;
 	public static function pack<T>(args:T) : Table<Int,T>;
 	public static function unpack(arg:Table<Dynamic,Dynamic>, ?min:Int, ?max:Int) : Dynamic;
-	private static function __init__() : Void {
-		// lua table polyfills
-		haxe.macro.Compiler.includeFile("lua/_lua/_hx_table_polyfill.lua");
-	}
 }
 
 typedef AnyTable = Table<Dynamic, Dynamic>;

+ 0 - 4
std/lua/_lua/_hx_table_polyfill.lua

@@ -1,4 +0,0 @@
-
-table.pack=table.pack or pack or function(...) return { n=select('#',...),...} end
-table.unpack=table.unpack or unpack or function(t, i) i = i or 1 if t[i] ~= nil then return t[i], table.unpack(t, i + 1) end end
-table.maxn=table.maxn or function(t) local maxn=0 for i in pairs(t) do maxn=type(i)=='number'and i>maxn and i or maxn end return maxn end

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

@@ -81,10 +81,10 @@ import lua.Boot;
 			}
 			return if (self_arg){
 				// call with o as leading self param
-				func(o, lua.Table.unpack(new_args, 1, lua.Table.maxn(new_args)));
+				func(o, lua.Table.unpack(new_args, 1, Boot.tableMaxN(new_args)));
 			} else {
 				// call with no self param
-				func(lua.Table.unpack(new_args, 1, lua.Table.maxn(new_args)));
+				func(lua.Table.unpack(new_args, 1,  Boot.tableMaxN(new_args)));
 			}
 		}
 	}