Browse Source

[lua] use luv for sys environment variable queries/updates

Justin Donaldson 7 years ago
parent
commit
9d75becbe1
3 changed files with 29 additions and 47 deletions
  1. 26 17
      std/lua/_std/Sys.hx
  2. 0 30
      std/lua/lib/environ/Environ.hx
  3. 3 0
      std/lua/lib/luv/Misc.hx

+ 26 - 17
std/lua/_std/Sys.hx

@@ -21,15 +21,12 @@
  * DEALINGS IN THE SOFTWARE.
  * DEALINGS IN THE SOFTWARE.
  */
  */
 
 
-using lua.NativeStringTools;
-import lua.Package;
+import lua.Boot;
+import lua.Io;
 import lua.Lua;
 import lua.Lua;
-import lua.Table;
-import lua.TableTools;
 import lua.Os;
 import lua.Os;
-import lua.FileHandle;
-import lua.Io;
-import lua.Boot;
+import lua.lib.luautf8.Utf8;
+import lua.lib.luv.Misc;
 import sys.io.FileInput;
 import sys.io.FileInput;
 import sys.io.FileOutput;
 import sys.io.FileOutput;
 
 
@@ -43,7 +40,7 @@ class Sys {
 		return lua.Lib.println(v);
 		return lua.Lib.println(v);
 	}
 	}
 	public inline static function args() : Array<String> {
 	public inline static function args() : Array<String> {
-		var targs = lua.PairTools.copy(lua.Lua.arg);
+		var targs = lua.PairTools.copy(Lua.arg);
 		var args = lua.Lib.tableToArray(targs);
 		var args = lua.Lib.tableToArray(targs);
 		return args;
 		return args;
 	}
 	}
@@ -64,7 +61,7 @@ class Sys {
 	}
 	}
 
 
 	public inline static function getChar(echo : Bool) : Int {
 	public inline static function getChar(echo : Bool) : Int {
-		return lua.Io.read(1).byte();
+		return lua.Io.read().charCodeAt(0);
 	}
 	}
 
 
 	static function getSystemName() : String {
 	static function getSystemName() : String {
@@ -78,13 +75,24 @@ class Sys {
 
 
 	public static function environment() : Map<String,String>  {
 	public static function environment() : Map<String,String>  {
 		var map = new Map<String,String>();
 		var map = new Map<String,String>();
-		var f = function(k,v) map.set(k,v);
-		untyped __lua__("for k,v in lua.lib.environ.Environ.enum() do f(k,v) end");
-		return map;
+		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();
+		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;
 	}
 	}
 
 
 	@:deprecated("Use programPath instead") public static function executablePath() : String {
 	@:deprecated("Use programPath instead") public static function executablePath() : String {
-		return lua.lib.luv.Misc.exepath();
+		return Misc.exepath();
 	}
 	}
 
 
 	public inline static function programPath() : String {
 	public inline static function programPath() : String {
@@ -92,16 +100,17 @@ class Sys {
 	}
 	}
 
 
 	public inline static function getCwd() : String
 	public inline static function getCwd() : String
-		return lua.lib.luv.Misc.cwd();
+		return Misc.cwd();
 
 
 	public inline static function setCwd(s : String) : Void
 	public inline static function setCwd(s : String) : Void
-		lua.lib.luv.Misc.chdir(s);
+		Misc.chdir(s);
 
 
 	public inline static function getEnv(s : String) : String {
 	public inline static function getEnv(s : String) : String {
-		return lua.Os.getenv(s);
+		return Misc.os_getenv(s);
 	}
 	}
+
 	public inline static function putEnv(s : String, v : String ) : Void {
 	public inline static function putEnv(s : String, v : String ) : Void {
-		lua.lib.environ.Environ.setenv(s,v);
+		Misc.os_setenv(s,v);
 	}
 	}
 
 
 	public inline static function setTimeLocale(loc : String) : Bool  {
 	public inline static function setTimeLocale(loc : String) : Bool  {

+ 0 - 30
std/lua/lib/environ/Environ.hx

@@ -1,30 +0,0 @@
-/*
- * Copyright (C)2005-2018 Haxe Foundation
- *
- * 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,
- * 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:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- * DEALINGS IN THE SOFTWARE.
- */
-
-package lua.lib.environ;
-@:luaRequire("environ.process")
-extern class Environ {
-	static function getenv(arg : String) : String;
-	static function setenv(arg : String, value : String) : Bool;
-	static var ENV : Table<String, String>;
-}
-

+ 3 - 0
std/lua/lib/luv/Misc.hx

@@ -38,6 +38,9 @@ extern class Misc {
   public static function get_free_memory() : Int;
   public static function get_free_memory() : Int;
   public static function getpid() : Int;
   public static function getpid() : Int;
 
 
+  public static function os_getenv(env : String) : String;
+  public static function os_setenv(env : String, value : String) : Void;
+
   // TODO Windows only?
   // TODO Windows only?
   public static function getuid() : Int;
   public static function getuid() : Int;
   public static function setuid(from : Int, to : Int) : String;
   public static function setuid(from : Int, to : Int) : String;