Browse Source

[lua] add new luv methods, and use those instead of other libraries

Justin Donaldson 5 years ago
parent
commit
d3f4814ac2

+ 0 - 1
std/lua/Boot.hx

@@ -22,7 +22,6 @@
 
 package lua;
 
-import haxe.Constraints.Function;
 import haxe.SysTools;
 
 @:dox(hide)

+ 16 - 1
std/lua/Table.hx

@@ -25,7 +25,6 @@ package lua;
 /**
 	This library provides generic functions for table manipulation.
 **/
-// TODO: use an abstract here?
 
 @:native("_G.table")
 extern class Table<A, B> implements ArrayAccess<B> implements Dynamic<B> {
@@ -55,6 +54,22 @@ extern class Table<A, B> implements ArrayAccess<B> implements Dynamic<B> {
 		return ret;
 	}
 
+    public inline static function toMap<A,B>(tbl : Table<A,B>) : Map<A,B> {
+        var res = new haxe.ds.ObjectMap();
+        foreach(tbl , (k:A,v:B) -> {
+            res.set(k,v);
+        });
+        return cast res;
+    }
+
+    public inline static function toArray<T>(tbl : Table<Int,T>) : Array<T> {
+        var res = [];
+        foreachi(tbl, (k:Int, v:T, i:Int) ->{
+            res.push(v);
+        });
+        return res;
+    }
+
 	@:overload(function<A, B>(table:Table<A, B>):Void {})
 	public static function concat<A, B>(table:Table<A, B>, ?sep:String, ?i:Int, ?j:Int):String;
 

+ 5 - 2
std/lua/TableTools.hx

@@ -22,14 +22,17 @@
 
 package lua;
 
+import lua.Table.AnyTable;
+
 /**
-	This library provides generic functions for table manipulation.
+	This library is an extern for a polyfill library of common lua table
+    methods.
 **/
 @: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 maxn(t:AnyTable):Int;
 	public static function __init__():Void {
 		untyped __define_feature__("use._hx_table", null);
 	}

+ 6 - 18
std/lua/_std/Sys.hx

@@ -76,22 +76,8 @@ class Sys {
 	}
 
 	public static function environment():Map<String, String> {
-		var map = new Map<String, String>();
-		var cmd = switch (Sys.systemName()) {
-			case "Windows": 'SET';
-			default: 'printenv';
-		}
-		var p = new sys.io.Process(cmd, []);
-		var code = p.exitCode(true);
-		var out = p.stdout.readAll().toString();
-		p.close();
-		var lines = out.split("\n");
-		var m = new Map<String, String>();
-		for (l in lines) {
-			var parts = l.split("=");
-			m.set(parts.shift(), parts.join("="));
-		}
-		return m;
+        var env = lua.lib.luv.Os.environ();
+        return lua.Table.toMap(env);
 	}
 
 	@:deprecated("Use programPath instead") public static function executablePath():String {
@@ -133,6 +119,8 @@ class Sys {
 	public inline static function stdout():haxe.io.Output
 		return new FileOutput(Io.stdout);
 
-	public static function time():Float
-		return lua.lib.luasocket.Socket.gettime();
+	public static function time():Float{
+        var stamp = lua.lib.luv.Misc.gettimeofday();
+        return stamp.seconds + (stamp.microseconds / 100000);
+    }
 }

+ 8 - 3
std/lua/_std/sys/net/Host.hx

@@ -25,6 +25,11 @@ package sys.net;
 import haxe.io.Bytes;
 import haxe.io.BytesInput;
 
+import lua.NativeStringTools.find;
+
+import lua.lib.luv.net.Dns;
+import lua.lib.luv.Os;
+
 @:coreapi
 class Host {
 	public var host(default, null):String;
@@ -35,7 +40,7 @@ class Host {
 
 	public function new(name:String):Void {
 		host = name;
-		if (lua.NativeStringTools.find(name, "(%d+)%.(%d+)%.(%d+)%.(%d+)").begin != null) {
+		if (find(name, "(%d+)%.(%d+)%.(%d+)%.(%d+)").begin != null) {
 			_ip = name;
 		} else {
 			var res = lua.lib.luv.net.Dns.getaddrinfo(name);
@@ -57,10 +62,10 @@ class Host {
 	}
 
 	public function reverse():String {
-		return lua.lib.luv.net.Dns.getnameinfo({ip: _ip}).result;
+		return Dns.getnameinfo({ip: _ip}).result;
 	}
 
 	static public function localhost():String {
-		return lua.lib.luasocket.socket.Dns.gethostname();
+        return Os.gethostname();
 	}
 }

+ 18 - 8
std/lua/lib/luv/Misc.hx

@@ -35,18 +35,15 @@ extern class Misc {
 	public static function get_free_memory():Int;
 	public static function getpid():Int;
 
-	// TODO Windows only?
-	public static function getuid():Int;
-	public static function setuid(from:Int, to:Int):String;
-	public static function getgid():Int;
-	public static function setgid(from:Int, to:Int):Void;
-
 	public static function getrusage():ResourceUsage;
 	public static function guess_handle(handle:Int):String;
 	public static function hrtime():Float;
 
+    public static function gettimeofday() : TimeOfDay;
+
 	// TODO: implement this
-	// public static function interface_addresses() : String;
+	public static function interface_addresses() : Dynamic;
+
 	public static function loadavg():Float;
 	public static function resident_set_memory():Int;
 	public static function set_process_title(title:String):Bool;
@@ -54,9 +51,16 @@ extern class Misc {
 	public static function version():Int;
 	public static function version_string():String;
 
-	// TODO : Windows only
+	// Windows only
+	public static function getuid():Int;
+	public static function setuid(from:Int, to:Int):String;
+	public static function getgid():Int;
+	public static function setgid(from:Int, to:Int):Void;
+
+	// Windows only
 	public static function print_all_handles():Table<Int, String>;
 	public static function print_active_handles():Table<Int, String>;
+
 }
 
 typedef CpuInfo = {
@@ -95,3 +99,9 @@ typedef MicroTimeStamp = {
 	usec:Int,
 	sec:Int
 }
+
+@:multiReturn
+extern class TimeOfDay {
+    var seconds : Int;
+    var microseconds : Int;
+}

+ 32 - 2
std/lua/lib/luv/Os.hx

@@ -3,8 +3,7 @@
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  * and/or sell copies of the Software, and to permit persons to whom the
  * Software is furnished to do so, subject to the following conditions:
  *
@@ -39,5 +38,36 @@ extern class Os {
     @:native("os_setenv")
 	public static function setenv(env:String, value:String):Void;
 
+    @:native("os_unsetenv")
+	public static function unsetenv(env:String):Void;
+
+    @:native("os_gethostname")
+    public static function gethostname():String;
+
+    @:native("os_environ")
+    public static function environ() : Table<String,String>;
+
+    @:native("os_uname")
+    public static function uname() : Uname;
+
+    @:native("os_getpid")
+    public static function getpid() : Int;
+
+    @:native("os_getppid")
+    public static function getppid() : Int;
+
+    @:native("os_getpriority")
+    public static function getpriority(pid :Int) : Int;
+
+    @:native("os_setpriority")
+    public static function setpriority(pid :Int, priority : Int) : Bool;
+
+}
+
+typedef Uname = {
+    sysname : String,
+    release : String,
+    version : String,
+    machine : String
 }
 

+ 1 - 1
std/lua/lib/luv/net/Dns.hx

@@ -27,7 +27,7 @@ extern class Dns {
 	@:overload(function(node:String, ?service:String, ?hints:AddrInfo, cb:String->Table<Int, AddrInfo>->Void):Request {})
 	public static function getaddrinfo(node:String, ?service:String, ?hints:AddrInfo):Result<Table<Int, AddrInfo>>;
 
-	@:overload(function(ip:String, ?port:Int, ?family:String, cb:String->AddrInfo->Void):Request {})
+	@:overload(function(ip:String, ?port:Int, ?family:String, ?cb:String->AddrInfo->Void):Request {})
 	public static function getnameinfo(info:AddrInfo):Result<String>;
 }
 

+ 1 - 1
tests/sys/compile-lua.hxml

@@ -16,4 +16,4 @@ compile-each.hxml
 --next
 compile-each.hxml
 --main UtilityProcess
--lua bin/lua/UtilityProcess.lua
+-lua bin/lua/UtilityProcess.lua