Boot.hx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. package lua;
  23. import haxe.SysTools;
  24. @:dox(hide)
  25. class Boot {
  26. // Used temporarily for bind()
  27. static var _:Dynamic;
  28. static var _fid = 0;
  29. static var Max_Int32 = 2147483647;
  30. static var Min_Int32 = -2147483648;
  31. // A max stack size to respect for unpack operations
  32. public static var MAXSTACKSIZE(default, null) = 1000;
  33. public static var platformBigEndian = NativeStringTools.byte(NativeStringTools.dump(function() {}), 7) > 0;
  34. 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}");
  35. static function __unhtml(s:String)
  36. return s.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
  37. /**
  38. Indicates if the given object is a class.
  39. **/
  40. static inline public function isClass(o:Dynamic):Bool {
  41. if (Lua.type(o) != "table")
  42. return false;
  43. else
  44. return untyped __define_feature__("lua.Boot.isClass", o.__name__);
  45. }
  46. /**
  47. Indicates if the given object is a enum.
  48. **/
  49. static inline public function isEnum(e:Dynamic):Bool {
  50. if (Lua.type(e) != "table")
  51. return false;
  52. else
  53. return untyped __define_feature__("lua.Boot.isEnum", e.__ename__);
  54. }
  55. /**
  56. Returns the class of a given object, and defines the getClass feature
  57. for the given class.
  58. **/
  59. static inline public function getClass(o:Dynamic):Class<Dynamic> {
  60. if (Std.isOfType(o, Array))
  61. return Array;
  62. else if (Std.isOfType(o, String))
  63. return String;
  64. else {
  65. var cl = untyped __define_feature__("lua.Boot.getClass", o.__class__);
  66. if (cl != null)
  67. return cl;
  68. else
  69. return null;
  70. }
  71. }
  72. /**
  73. Indicates if the given object is an instance of the given Type
  74. **/
  75. @:ifFeature("typed_catch")
  76. private static function __instanceof(o:Dynamic, cl:Dynamic) {
  77. if (cl == null)
  78. return false;
  79. switch (cl) {
  80. case Int:
  81. return (Lua.type(o) == "number" && clampInt32(o) == o);
  82. case Float:
  83. return Lua.type(o) == "number";
  84. case Bool:
  85. return Lua.type(o) == "boolean";
  86. case String:
  87. return Lua.type(o) == "string";
  88. case Thread:
  89. return Lua.type(o) == "thread";
  90. case UserData:
  91. return Lua.type(o) == "userdata";
  92. case Array:
  93. return isArray(o);
  94. case Table:
  95. return Lua.type(o) == "table";
  96. case Dynamic:
  97. return o != null;
  98. default:
  99. {
  100. if (o != null && Lua.type(o) == "table" && Lua.type(cl) == "table") {
  101. if (extendsOrImplements(getClass(o), cl))
  102. return true;
  103. // We've exhausted standard inheritance checks. Check for simple Class/Enum eqauality
  104. // Also, do not use isClass/isEnum here, perform raw checks
  105. untyped __feature__("Class.*", if (cl == Class && o.__name__ != null) return true);
  106. untyped __feature__("Enum.*", if (cl == Enum && o.__ename__ != null) return true);
  107. // last chance, is it an enum instance?
  108. return o.__enum__ == cl;
  109. } else {
  110. return false;
  111. }
  112. }
  113. }
  114. }
  115. static function isArray(o:Dynamic):Bool {
  116. return Lua.type(o) == "table"
  117. && untyped o.__enum__ == null && Lua.getmetatable(o) != null && Lua.getmetatable(o).__index == untyped Array.prototype;
  118. }
  119. /**
  120. Indicates if the given object inherits from the given class
  121. **/
  122. static function inheritsFrom(o:Dynamic, cl:Class<Dynamic>):Bool {
  123. while (Lua.getmetatable(o) != null && Lua.getmetatable(o).__index != null) {
  124. if (Lua.getmetatable(o).__index == untyped cl.prototype)
  125. return true;
  126. o = Lua.getmetatable(o).__index;
  127. }
  128. return false;
  129. }
  130. @:ifFeature("typed_cast")
  131. private static function __cast(o:Dynamic, t:Dynamic) {
  132. if (o == null || __instanceof(o, t))
  133. return o;
  134. else
  135. throw "Cannot cast " + Std.string(o) + " to " + Std.string(t);
  136. }
  137. /**
  138. Define an array from the given table
  139. **/
  140. public inline static function defArray<T>(tab:Table<Int, T>, ?length:Int):Array<T> {
  141. if (length == null) {
  142. length = TableTools.maxn(tab);
  143. if (length > 0) {
  144. var head = tab[1];
  145. Table.remove(tab, 1);
  146. tab[0] = head;
  147. return untyped _hx_tab_array(tab, length);
  148. } else {
  149. return [];
  150. }
  151. } else {
  152. return untyped _hx_tab_array(tab, length);
  153. }
  154. }
  155. /**
  156. Create a Haxe object from the given table structure
  157. **/
  158. public inline static function tableToObject<T>(t:Table<String, T>):Dynamic<T> {
  159. return untyped _hx_o(t);
  160. }
  161. /**
  162. Get Date object as string representation
  163. **/
  164. public static function dateStr(date:std.Date):String {
  165. var m = date.getMonth() + 1;
  166. var d = date.getDate();
  167. var h = date.getHours();
  168. var mi = date.getMinutes();
  169. var s = date.getSeconds();
  170. return date.getFullYear() + "-" + (if (m < 10) "0" + m else "" + m) + "-" + (if (d < 10) "0" + d else "" + d) + " "
  171. + (if (h < 10) "0" + h else "" + h) + ":" + (if (mi < 10) "0" + mi else "" + mi) + ":" + (if (s < 10) "0" + s else "" + s);
  172. }
  173. /**
  174. A 32 bit clamp function for numbers
  175. **/
  176. public inline static function clampInt32(x:Float) {
  177. return untyped _hx_bit_clamp(x);
  178. }
  179. /**
  180. Create a standard date object from a lua string representation
  181. **/
  182. public static function strDate(s:String):std.Date {
  183. switch (s.length) {
  184. case 8: // hh:mm:ss
  185. var k = s.split(":");
  186. return std.Date.fromTime(Lua.tonumber(k[0]) * 3600000. + Lua.tonumber(k[1]) * 60000. + Lua.tonumber(k[2]) * 1000.);
  187. case 10: // YYYY-MM-DD
  188. var k = s.split("-");
  189. return new std.Date(Lua.tonumber(k[0]), Lua.tonumber(k[1]) - 1, Lua.tonumber(k[2]), 0, 0, 0);
  190. case 19: // YYYY-MM-DD hh:mm:ss
  191. var k = s.split(" ");
  192. var y = k[0].split("-");
  193. var t = k[1].split(":");
  194. return new std.Date(Lua.tonumber(y[0]), Lua.tonumber(y[1]) - 1, Lua.tonumber(y[2]), Lua.tonumber(t[0]), Lua.tonumber(t[1]), Lua.tonumber(t[2]));
  195. default:
  196. throw "Invalid date format : " + s;
  197. }
  198. }
  199. /**
  200. Helper method to determine if class cl1 extends, implements, or otherwise equals cl2
  201. **/
  202. public static function extendsOrImplements(cl1:Class<Dynamic>, cl2:Class<Dynamic>):Bool {
  203. if (cl1 == null || cl2 == null)
  204. return false;
  205. else if (cl1 == cl2)
  206. return true;
  207. else if (untyped cl1.__interfaces__ != null) {
  208. var intf = untyped cl1.__interfaces__;
  209. for (i in 1...(TableTools.maxn(intf) + 1)) {
  210. // check each interface, including extended interfaces
  211. if (extendsOrImplements(intf[i], cl2))
  212. return true;
  213. }
  214. }
  215. // check standard inheritance
  216. return extendsOrImplements(untyped cl1.__super__, cl2);
  217. }
  218. /**
  219. Returns a shell escaped version of "cmd" along with any args
  220. **/
  221. public static function shellEscapeCmd(cmd:String, ?args:Array<String>) {
  222. if (args != null) {
  223. switch (Sys.systemName()) {
  224. case "Windows":
  225. cmd = [
  226. for (a in [StringTools.replace(cmd, "/", "\\")].concat(args))
  227. SysTools.quoteWinArg(a, true)
  228. ].join(" ");
  229. case _:
  230. cmd = [cmd].concat(args).map(SysTools.quoteUnixArg).join(" ");
  231. }
  232. }
  233. return cmd;
  234. }
  235. /**
  236. Returns a temp file path that can be used for reading and writing
  237. **/
  238. public static function tempFile():String {
  239. switch (Sys.systemName()) {
  240. case "Windows":
  241. return haxe.io.Path.join([Os.getenv("TMP"), Os.tmpname()]);
  242. default:
  243. return Os.tmpname();
  244. }
  245. }
  246. static var os_patterns = [
  247. 'Windows' => ['windows', '^mingw', '^cygwin'],
  248. 'Linux' => ['linux'],
  249. 'Mac' => ['mac', 'darwin', 'osx'],
  250. 'BSD' => ['bsd$'],
  251. 'Solaris' => ['SunOS']
  252. ];
  253. public static function systemName():String {
  254. var os:String = null;
  255. if (untyped jit != null && untyped jit.os != null) {
  256. os = untyped jit.os;
  257. os = os.toLowerCase();
  258. } else {
  259. var popen_status:Bool = false;
  260. var popen_result:lua.FileHandle = null;
  261. untyped __lua__("popen_status, popen_result = pcall(_G.io.popen, '')");
  262. if (popen_status) {
  263. popen_result.close();
  264. os = lua.Io.popen('uname -s', 'r').read('*l').toLowerCase();
  265. } else {
  266. os = lua.Os.getenv('OS').toLowerCase();
  267. }
  268. }
  269. for (k in os_patterns.keys()) {
  270. for (p in os_patterns.get(k)) {
  271. if (lua.NativeStringTools.match(os, p) != null) {
  272. return k;
  273. }
  274. }
  275. }
  276. return null;
  277. }
  278. }