Parcourir la source

[lua] start refactoring externs based on versions

Justin Donaldson il y a 8 ans
Parent
commit
bfb06afb00

+ 15 - 21
src/generators/genlua.ml

@@ -276,8 +276,8 @@ let mk_mr_box ctx e =
 		| _ -> assert false
 	in
 	add_feature ctx "use._hx_box_mr";
-	add_feature ctx "use._hx_tbl_pack";
-	let code = Printf.sprintf "_hx_box_mr(_G.table.pack({0}), {%s})" s_fields in
+	add_feature ctx "use._hx_table";
+	let code = Printf.sprintf "_hx_box_mr(_hx_table.pack({0}), {%s})" s_fields in
 	mk_lua_code ctx.com code [e] e.etype e.epos
 
 (* create a multi-return select call for given expr and field name *)
@@ -1937,7 +1937,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, _hx_maxn, _hx_print, _hx_apply_self, _hx_box_mr, _hx_bit_clamp";
+	println ctx "local _hx_bind, _hx_bit, _hx_staticToInstance, _hx_funcToField, _hx_maxn, _hx_print, _hx_apply_self, _hx_box_mr, _hx_bit_clamp, _hx_table";
 
 	List.iter (transform_multireturn ctx) com.types;
 	List.iter (generate_type ctx) com.types;
@@ -2050,16 +2050,6 @@ 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;
-
 	if has_feature ctx "use._hx_print" then
 	    println ctx "_hx_print = print or (function() end)";
 
@@ -2079,16 +2069,20 @@ let generate com =
 	    println ctx "end";
 	end;
 
-	(* if has_feature ctx "use.table" then begin *)
-	    println ctx "if not _G.table.pack then";
-	    println ctx "  _G.table.pack = function(...)";
+	if has_feature ctx "use._hx_table" then begin
+	    println ctx "_hx_table = {}";
+	    println ctx "_hx_table.pack = _G.table.pack or function(...)";
 	    println ctx "    return {...}";
-	    println ctx "  end";
 	    println ctx "end";
-	    println ctx "if not _G.table.unpack then";
-	    println ctx " _G.table.unpack = _G.unpack";
-	    println ctx "end";
-	(* end; *)
+	    println ctx "_hx_table.unpack = _G.table.unpack or _G.unpack";
+	    println ctx "_hx_table.maxn = _G.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;

+ 2 - 6
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 = tableMaxN(tab) + 1; // maxn doesn't count 0 index
+		if (length == null) length = TableTools.maxn(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...( tableMaxN(intf) + 1)){
+			for (i in 1...( TableTools.maxn(intf) + 1)){
 				// check each interface, including extended interfaces
 				if (extendsOrImplements(intf[1], cl2)) return true;
 			}
@@ -304,10 +304,6 @@ 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.
 	*/

+ 1 - 0
std/lua/FileHandle.hx

@@ -28,6 +28,7 @@ extern class FileHandle extends UserData {
 	public function flush() : Void;
 	public function read(arg : Rest<EitherType<ReadArgument,Int>>) : String;
 	public function close() : Void;
+
 	public function write(str : String) : Void;
 
 	@:overload(function () : Int {})

+ 7 - 2
std/lua/Lib.hx

@@ -70,7 +70,12 @@ class Lib {
 		return ret;
 	}
 
-	public inline static function isShellAvailable() : Bool {
-		return Os.execute().status > 0;
+	public static function isShellAvailable() : Bool {
+		var ret : Dynamic = Os.execute();
+		if (Lua.type(ret) == "bool"){
+			return ret;
+		} else {
+			return ret != 0;
+		}
 	}
 }

+ 9 - 2
std/lua/Os.hx

@@ -28,7 +28,13 @@ extern class Os {
 		which is system-dependent. If command is absent, then it returns 
 		nonzero if a shell is available and zero otherwise.
 	**/
+#if (lua_ver < 5.2)
+	public static function execute(?command:String) : Int;
+#elseif (lua_ver >= 5.2)
 	public static function execute(?command:String) : OsExecute;
+#else 
+	public static function execute(?command:String) : Dynamic;
+#end
 
 	/**
 		Calls the C function exit, with an optional code, to terminate the host program. 
@@ -110,6 +116,7 @@ typedef DateType = {
 }
 
 @:multiReturn extern class OsExecute {
-	var output : String;
-	var status : Int;
+	var success : Bool;
+	var output  : String;
+	var status  : Int;
 }

+ 2 - 0
std/lua/Table.hx

@@ -23,8 +23,10 @@ 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;
 
+#if (lua_ver >= 5.2)
 	public static function pack<T>(args:haxe.extern.Rest<T>) : Table<Int,T>;
 	public static function unpack<Int,V>(args:lua.Table<Int,V>, ?min : Int, ?max : Int) : Dynamic;
+#end
 
 }
 

+ 15 - 0
std/lua/TableTools.hx

@@ -0,0 +1,15 @@
+
+package lua;
+
+/**
+	This library provides generic functions for table manipulation.
+**/
+@:native("_hx_table")
+extern class TableTools {
+	public static function pack<T>(args:haxe.extern.Rest<T>) : Table<Int,T>;
+	public static function unpack<Int,V>(args:lua.Table<Int,V>, ?min : Int, ?max : Int) : Dynamic;
+	public static function maxn(t:Table.AnyTable) : Int;
+	public static function __init__() : Void {
+	  untyped _hx_table = __define_feature__("use._hx_table", _hx_table);
+	}
+}

+ 3 - 3
std/lua/_std/EReg.hx

@@ -51,7 +51,7 @@ class EReg {
 	}
 
 	public function match( s : String ) : Bool {
-		this.m = lua.Table.pack(r.exec(s));
+		this.m = lua.TableTools.pack(r.exec(s));
 		this.s = s;
 		return  m[1] != null;
 	}
@@ -93,14 +93,14 @@ class EReg {
 		var ss = s.substr(0, len < 0 ? s.length : pos + len);
 
 		if (global){
-			m = lua.Table.pack(r.exec(ss, pos + 1));
+			m = lua.TableTools.pack(r.exec(ss, pos + 1));
 			var b = m[1] != null;
 			if (b){
 				this.s = s;
 			}
 			return b;
 		} else {
-			m = lua.Table.pack(r.exec(ss, pos + 1));
+			m = lua.TableTools.pack(r.exec(ss, pos + 1));
 			var b = m[1] != null;
 			if (b){
 				this.s = s;

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

@@ -20,6 +20,7 @@
  * DEALINGS IN THE SOFTWARE.
  */
 import lua.Lua;
+import lua.TableTools;
 import lua.Boot;
 @:coreApi class Reflect {
 
@@ -81,10 +82,10 @@ import lua.Boot;
 			}
 			return if (self_arg){
 				// call with o as leading self param
-				func(o, lua.Table.unpack(new_args, 1, Boot.tableMaxN(new_args)));
+				func(o, lua.TableTools.unpack(new_args, 1, TableTools.maxn(new_args)));
 			} else {
 				// call with no self param
-				func(lua.Table.unpack(new_args, 1,  Boot.tableMaxN(new_args)));
+				func(lua.TableTools.unpack(new_args, 1,  TableTools.maxn(new_args)));
 			}
 		}
 	}

+ 11 - 2
std/lua/_std/Sys.hx

@@ -25,6 +25,7 @@ using lua.NativeStringTools;
 import lua.Package;
 import lua.Lua;
 import lua.Table;
+import lua.TableTools;
 import lua.Os;
 import lua.lib.lfs.Lfs;
 import lua.FileHandle;
@@ -48,7 +49,15 @@ class Sys {
 	}
 	public static function command( cmd : String, ?args : Array<String> ) : Int  {
 		cmd = Boot.shellEscapeCmd(cmd, args);
+#if (lua_ver < 5.2)
+		return Os.execute(cmd);
+#elseif (lua_ver >= 5.2)
 		return Os.execute(cmd).status;
+#else
+		var ret = TableTools.pack(untyped os.execute(cmd));
+		if (ret[3] != null) return ret[3]
+		else return ret[1];
+#end
 	}
 
 
@@ -119,9 +128,9 @@ class Sys {
 	public static function sleep(seconds : Float) : Void {
 		if (seconds <= 0) return;
 		if (Sys.systemName() == "Windows") {
-			Os.execute("ping -n " + (seconds+1) + " localhost > NUL");
+			Sys.command("ping -n " + (seconds+1) + " localhost > NUL");
 		} else {
-			Os.execute('sleep $seconds');
+			Sys.command('sleep $seconds');
 		}
 	}
 

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

@@ -82,7 +82,7 @@ enum ValueType {
 	}
 
 	public static function createInstance<T>( cl : Class<T>, args : Array<Dynamic> ) : T untyped {
-		return __new__(cl, lua.Table.unpack(cast args, 0));
+		return __new__(cl, lua.TableTools.unpack(cast args, 0));
 	}
 
 	public static function createEmptyInstance<T>( cl : Class<T> ) : T untyped {

+ 3 - 0
std/lua/lib/socket/Socket.hx

@@ -0,0 +1,3 @@
+package lua.lib.socket;
+extern class Socket {
+}