瀏覽代碼

[lua] turn os detection script into pure haxe

Justin Donaldson 8 年之前
父節點
當前提交
05c2ae137d
共有 7 個文件被更改,包括 46 次插入81 次删除
  1. 2 1
      std/haxe/Log.hx
  2. 40 1
      std/lua/Boot.hx
  3. 1 6
      std/lua/FileHandle.hx
  4. 1 1
      std/lua/Lua.hx
  5. 0 66
      std/lua/_lua/_hx_os_info.lua
  6. 1 5
      std/lua/_std/Sys.hx
  7. 1 1
      std/lua/_std/sys/io/File.hx

+ 2 - 1
std/haxe/Log.hx

@@ -98,7 +98,8 @@ class Log {
 			#elseif java
 			untyped __java__("java.lang.System.out.println(str)");
 			#elseif lua
-			untyped __define_feature__("use._hx_print",_hx_print(Std.string(str)));
+			if (str == null) str = "null";
+			untyped __define_feature__("use._hx_print",_hx_print(str));
 			#end
 		#elseif (python)
 			var str:String = null;

+ 40 - 1
std/lua/Boot.hx

@@ -37,6 +37,7 @@ class Boot {
 	static var hiddenFields : Table<String,Bool> = untyped __lua__("{__id__=true, hx__closures=true, super=true, prototype=true, __fields__=true, __ifields__=true, __class__=true, __properties__=true}");
 
 
+
 	static function __unhtml(s : String)
 		return s.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
 
@@ -192,8 +193,9 @@ class Boot {
 			    if (o.__enum__ != null) printEnum(o,s);
 				else if (o.toString != null && !isArray(o)) o.toString();
 				else if (isArray(o)) {
+					var o2 : Array<Dynamic> = untyped o;
 					if (s.length > 5) "[...]"
-					else '[${[for (i in  cast(o,Array<Dynamic>)) __string_rec(i,s+1)].join(",")}]';
+					else '[${[for (i in o2) __string_rec(i,s+1)].join(",")}]';
 				}
 				else if (o.__class__ != null) printClass(o,s+"\t");
 				else {
@@ -361,4 +363,41 @@ class Boot {
 			hasNext : function() return cur_val !=  null
 		}
 	}
+
+	static var os_patterns = [
+		'Windows' => ['windows','^mingw','^cygwin'],
+		'Linux'   => ['linux'],
+		'Mac'     => ['mac','darwin'],
+		'BSD'     => ['bsd$'],
+		'Solaris' => ['SunOS']
+	];
+
+	public static function systemName() : String {
+		var os : String = null;
+		if (untyped jit != null && untyped jit.os != null ){
+			os = untyped jit.os;
+			os = os.toLowerCase();
+		} else {
+			var popen_status : Bool = false;
+			var popen_result : lua.FileHandle = null;
+			untyped __lua__("popen_status, popen_result = pcall(_G.io.popen, '')");
+			if (popen_status) {
+				popen_result.close;
+				os = lua.Io.popen('uname -s','r').read('*l').toLowerCase();
+			} else {
+				os = lua.Os.getenv('OS').toLowerCase();
+			}
+
+		}
+
+		for (k in os_patterns.keys()){
+			for (p in os_patterns.get(k)) {
+				if (lua.NativeStringTools.match(os,p) != null){
+					return k;
+				}
+			}
+		}
+
+		return null;
+	}
 }

+ 1 - 6
std/lua/FileHandle.hx

@@ -26,7 +26,7 @@ import sys.io.FileInput;
 
 extern class FileHandle extends UserData {
 	public function flush() : Void;
-	public function read(arg : Rest<EitherType<ReadArgument,Int>>) : String;
+	public function read(arg : Rest<EitherType<String,Int>>) : String;
 	public function close() : Void;
 
 	public function write(str : String) : Void;
@@ -36,8 +36,3 @@ extern class FileHandle extends UserData {
 	public function seek(arg : String, pos : Int) : Void;
 }
 
-@:enum
-abstract ReadArgument(String) {
-	var All = "*all";
-	var Line = "*line";
-}

+ 1 - 1
std/lua/Lua.hx

@@ -192,7 +192,7 @@ abstract CollectGarbageOption(String) {
 @:multiReturn
 extern class PCallResult {
 	var status : Bool;
-	var error : String;
+	var value : Dynamic;
 }
 
 @:multiReturn

+ 0 - 66
std/lua/_lua/_hx_os_info.lua

@@ -1,66 +0,0 @@
--- credit : https://gist.github.com/soulik
-local function _hx_os_info()
-	local raw_os_name, raw_arch_name = '', ''
-
-	-- LuaJIT shortcut
-	if jit and jit.os and jit.arch then
-		raw_os_name = jit.os
-		raw_arch_name = jit.arch
-	else
-		-- is popen supported?
-		local popen_status, popen_result = pcall(io.popen, "")
-		if popen_status then
-			popen_result:close()
-			-- Unix-based OS
-			raw_os_name = io.popen('uname -s','r'):read('*l')
-			raw_arch_name = io.popen('uname -m','r'):read('*l')
-		else
-			-- Windows
-			local env_OS = os.getenv('OS')
-			local env_ARCH = os.getenv('PROCESSOR_ARCHITECTURE')
-			if env_OS and env_ARCH then
-				raw_os_name, raw_arch_name = env_OS, env_ARCH
-			end
-		end
-	end
-
-	raw_os_name = (raw_os_name):lower()
-	raw_arch_name = (raw_arch_name):lower()
-
-	local os_patterns = {
-		['windows'] = 'Windows',
-		['linux'] = 'Linux',
-		['mac'] = 'Mac',
-		['darwin'] = 'Mac',
-		['^mingw'] = 'Windows',
-		['^cygwin'] = 'Windows',
-		['bsd$'] = 'BSD',
-		['SunOS'] = 'Solaris',
-	}
-	
-	local arch_patterns = {
-		['^x86$'] = 'x86',
-		['i[%d]86'] = 'x86',
-		['amd64'] = 'x86_64',
-		['x86_64'] = 'x86_64',
-		['Power Macintosh'] = 'powerpc',
-		['^arm'] = 'arm',
-		['^mips'] = 'mips',
-	}
-
-	local os_name, arch_name = 'unknown', 'unknown'
-
-	for pattern, name in pairs(os_patterns) do
-		if raw_os_name:match(pattern) then
-			os_name = name
-			break
-		end
-	end
-	for pattern, name in pairs(arch_patterns) do
-		if raw_arch_name:match(pattern) then
-			arch_name = name
-			break
-		end
-	end
-	return {os_name, arch_name}
-end

+ 1 - 5
std/lua/_std/Sys.hx

@@ -68,7 +68,7 @@ class Sys {
 	}
 
 	static function getSystemName() : String {
-		return untyped _hx_os_info()[1];
+		return lua.Boot.systemName();
 	}
 
 	public static function systemName() : String {
@@ -120,8 +120,4 @@ class Sys {
 	public static function time() : Float
 		return lua.lib.luasocket.Socket.gettime();
 
-	static function __init__() : Void untyped {
-		// os detection helper
-		haxe.macro.Compiler.includeFile("lua/_lua/_hx_os_info.lua");
-	}
 }

+ 1 - 1
std/lua/_std/sys/io/File.hx

@@ -29,7 +29,7 @@ import lua.FileHandle;
 class File {
 	public static function getContent( path : String ) : String {
 		var f = Io.open(path, "r");
-		var s = f.read(All);
+		var s = f.read("*all");
 		f.close();
 		return s;
 	}