Sys.hx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. import lua.Boot;
  23. import lua.Io;
  24. import lua.Lua;
  25. import lua.lib.luv.Os;
  26. import lua.lib.luv.Misc;
  27. import sys.io.FileInput;
  28. import sys.io.FileOutput;
  29. @:coreApi
  30. class Sys {
  31. static var _system_name:String;
  32. public static inline function print(v:Dynamic):Void {
  33. return lua.Lib.print(v);
  34. }
  35. public static inline function println(v:Dynamic):Void {
  36. return lua.Lib.println(v);
  37. }
  38. public inline static function args():Array<String> {
  39. var targs = lua.PairTools.copy(Lua.arg);
  40. var args = lua.Table.toArray(targs);
  41. return args;
  42. }
  43. public static function command(cmd:String, ?args:Array<String>):Int {
  44. var p = new sys.io.Process(cmd, args);
  45. var code = p.exitCode();
  46. p.close();
  47. return code;
  48. }
  49. public inline static function cpuTime():Float {
  50. return lua.Os.clock();
  51. }
  52. public inline static function exit(code:Int):Void {
  53. lua.Os.exit(code);
  54. }
  55. public inline static function getChar(echo:Bool):Int {
  56. return lua.Io.read().charCodeAt(0);
  57. }
  58. static function getSystemName():String {
  59. return lua.Boot.systemName();
  60. }
  61. public static function systemName():String {
  62. if (_system_name == null)
  63. _system_name = getSystemName();
  64. return _system_name;
  65. }
  66. public static function environment():Map<String, String> {
  67. var env = lua.lib.luv.Os.environ();
  68. return lua.Table.toMap(env);
  69. }
  70. @:deprecated("Use programPath instead") public static function executablePath():String {
  71. return Misc.exepath();
  72. }
  73. public inline static function programPath():String {
  74. return haxe.io.Path.join([getCwd(), Lua.arg[0]]);
  75. }
  76. public inline static function getCwd():String
  77. return Misc.cwd();
  78. public inline static function setCwd(s:String):Void
  79. Misc.chdir(s);
  80. public inline static function getEnv(s:String):String {
  81. return Os.getenv(s);
  82. }
  83. public inline static function putEnv(s:String, v:String):Void {
  84. Os.setenv(s, v);
  85. }
  86. public inline static function setTimeLocale(loc:String):Bool {
  87. // TODO Verify
  88. return lua.Os.setlocale(loc) != null;
  89. }
  90. public static function sleep(seconds:Float):Void
  91. lua.lib.luv.Thread.sleep(Math.floor(seconds * 1000));
  92. public inline static function stderr():haxe.io.Output
  93. return @:privateAccess new FileOutput(Io.stderr);
  94. public inline static function stdin():haxe.io.Input
  95. return @:privateAccess new FileInput(Io.stdin);
  96. public inline static function stdout():haxe.io.Output
  97. return @:privateAccess new FileOutput(Io.stdout);
  98. public static function time():Float {
  99. var stamp = lua.lib.luv.Misc.gettimeofday();
  100. return stamp.seconds + (stamp.microseconds / 1000000);
  101. }
  102. }