Boot.hx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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.Constraints.Function;
  24. import haxe.SysTools;
  25. @:dox(hide)
  26. class Boot {
  27. // Used temporarily for bind()
  28. static var _:Dynamic;
  29. static var _fid = 0;
  30. static var Max_Int32 = 2147483647;
  31. static var Min_Int32 = -2147483648;
  32. // A max stack size to respect for unpack operations
  33. public static var MAXSTACKSIZE(default, null) = 1000;
  34. public static var platformBigEndian = NativeStringTools.byte(NativeStringTools.dump(function() {}), 7) > 0;
  35. static var hiddenFields:Table<String,
  36. Bool> = untyped __lua__("{__id__=true, hx__closures=true, super=true, prototype=true, __fields__=true, __ifields__=true, __class__=true, __properties__=true}");
  37. static function __unhtml(s:String)
  38. return s.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
  39. /**
  40. Indicates if the given object is a class.
  41. **/
  42. static inline public function isClass(o:Dynamic):Bool {
  43. if (Lua.type(o) != "table")
  44. return false;
  45. else
  46. return untyped __define_feature__("lua.Boot.isClass", o.__name__);
  47. }
  48. /**
  49. Indicates if the given object is a enum.
  50. **/
  51. static inline public function isEnum(e:Dynamic):Bool {
  52. if (Lua.type(e) != "table")
  53. return false;
  54. else
  55. return untyped __define_feature__("lua.Boot.isEnum", e.__ename__);
  56. }
  57. /**
  58. Returns the class of a given object, and defines the getClass feature
  59. for the given class.
  60. **/
  61. static inline public function getClass(o:Dynamic):Class<Dynamic> {
  62. if (Std.is(o, Array))
  63. return Array;
  64. else if (Std.is(o, String))
  65. return String;
  66. else {
  67. var cl = untyped __define_feature__("lua.Boot.getClass", o.__class__);
  68. if (cl != null)
  69. return cl;
  70. else
  71. return null;
  72. }
  73. }
  74. /**
  75. Indicates if the given object is an instance of the given Type
  76. **/
  77. @:ifFeature("typed_catch")
  78. private static function __instanceof(o:Dynamic, cl:Dynamic) {
  79. if (cl == null)
  80. return false;
  81. switch (cl) {
  82. case Int:
  83. return (Lua.type(o) == "number" && clampInt32(o) == o);
  84. case Float:
  85. return Lua.type(o) == "number";
  86. case Bool:
  87. return Lua.type(o) == "boolean";
  88. case String:
  89. return Lua.type(o) == "string";
  90. case Thread:
  91. return Lua.type(o) == "thread";
  92. case UserData:
  93. return Lua.type(o) == "userdata";
  94. case Array:
  95. return isArray(o);
  96. case Table:
  97. return Lua.type(o) == "table";
  98. case Dynamic:
  99. return o != null;
  100. default:
  101. {
  102. if (o != null && Lua.type(o) == "table" && Lua.type(cl) == "table") {
  103. if (extendsOrImplements(getClass(o), cl))
  104. return true;
  105. // We've exhausted standard inheritance checks. Check for simple Class/Enum eqauality
  106. // Also, do not use isClass/isEnum here, perform raw checks
  107. untyped __feature__("Class.*", if (cl == Class && o.__name__ != null) return true);
  108. untyped __feature__("Enum.*", if (cl == Enum && o.__ename__ != null) return true);
  109. // last chance, is it an enum instance?
  110. return o.__enum__ == cl;
  111. } else {
  112. return false;
  113. }
  114. }
  115. }
  116. }
  117. static function isArray(o:Dynamic):Bool {
  118. return Lua.type(o) == "table"
  119. && untyped o.__enum__ == null && Lua.getmetatable(o) != null && Lua.getmetatable(o).__index == untyped Array.prototype;
  120. }
  121. /**
  122. Indicates if the given object inherits from the given class
  123. **/
  124. static function inheritsFrom(o:Dynamic, cl:Class<Dynamic>):Bool {
  125. while (Lua.getmetatable(o) != null && Lua.getmetatable(o).__index != null) {
  126. if (Lua.getmetatable(o).__index == untyped cl.prototype)
  127. return true;
  128. o = Lua.getmetatable(o).__index;
  129. }
  130. return false;
  131. }
  132. @:ifFeature("typed_cast")
  133. private static function __cast(o:Dynamic, t:Dynamic) {
  134. if (o == null || __instanceof(o, t))
  135. return o;
  136. else
  137. throw "Cannot cast " + Std.string(o) + " to " + Std.string(t);
  138. }
  139. /**
  140. Helper method to generate a string representation of an enum
  141. **/
  142. static function printEnum(o:Array<Dynamic>, s:String) {
  143. if (o.length == 2) {
  144. return o[0];
  145. } else {
  146. // parameterized enums are arrays
  147. var str = o[0] + "(";
  148. s += "\t";
  149. for (i in 2...o.length) {
  150. if (i != 2)
  151. str += "," + __string_rec(o[i], s);
  152. else
  153. str += __string_rec(o[i], s);
  154. }
  155. return str + ")";
  156. }
  157. }
  158. /**
  159. Helper method to generate a string representation of a class
  160. **/
  161. static inline function printClass(c:Table<String, Dynamic>, s:String):String {
  162. return '{${printClassRec(c, '', s)}}';
  163. }
  164. /**
  165. Helper method to generate a string representation of a class
  166. **/
  167. static function printClassRec(c:Table<String, Dynamic>, result = '', s:String):String {
  168. var f = Boot.__string_rec;
  169. untyped __lua__("for k,v in pairs(c) do if result ~= '' then result = result .. ', ' end result = result .. k .. ':' .. f(v, s.. '\t') end");
  170. return result;
  171. }
  172. /**
  173. Generate a string representation for arbitrary object.
  174. **/
  175. @:ifFeature("has_enum")
  176. static function __string_rec(o:Dynamic, s:String = "") {
  177. if (s.length >= 5)
  178. return "<...>";
  179. return switch (untyped __type__(o)) {
  180. case "nil": "null";
  181. case "number": {
  182. if (o == std.Math.POSITIVE_INFINITY)
  183. "Infinity";
  184. else if (o == std.Math.NEGATIVE_INFINITY)
  185. "-Infinity";
  186. else if (o == 0)
  187. "0";
  188. else if (o != o)
  189. "NaN";
  190. else
  191. untyped tostring(o);
  192. }
  193. case "boolean": untyped tostring(o);
  194. case "string": o;
  195. case "userdata": {
  196. var mt = lua.Lua.getmetatable(o);
  197. if (mt != null && mt.__tostring != null) {
  198. lua.Lua.tostring(o);
  199. } else {
  200. "<userdata>";
  201. }
  202. }
  203. case "function": "<function>";
  204. case "thread": "<thread>";
  205. case "table": {
  206. if (o.__enum__ != null)
  207. printEnum(o, s);
  208. else if (o.toString != null && !isArray(o))
  209. o.toString();
  210. else if (isArray(o)) {
  211. var o2:Array<Dynamic> = untyped o;
  212. if (s.length > 5)
  213. "[...]"
  214. else
  215. '[${[for (i in o2) __string_rec(i, s + 1)].join(",")}]';
  216. } else if (o.__class__ != null)
  217. printClass(o, s + "\t");
  218. else {
  219. var fields = fieldIterator(o);
  220. var buffer:Table<Int, String> = Table.create();
  221. var first = true;
  222. Table.insert(buffer, "{ ");
  223. for (f in fields) {
  224. if (first)
  225. first = false;
  226. else
  227. Table.insert(buffer, ", ");
  228. Table.insert(buffer, '${Std.string(f)} : ${untyped __string_rec(o[f], s + "\t")}');
  229. }
  230. Table.insert(buffer, " }");
  231. Table.concat(buffer, "");
  232. }
  233. };
  234. default: {
  235. throw "Unknown Lua type";
  236. null;
  237. }
  238. }
  239. }
  240. /**
  241. Define an array from the given table
  242. **/
  243. public inline static function defArray<T>(tab:Table<Int, T>, ?length:Int):Array<T> {
  244. if (length == null) {
  245. length = TableTools.maxn(tab);
  246. if (length > 0) {
  247. var head = tab[1];
  248. Table.remove(tab, 1);
  249. tab[0] = head;
  250. return untyped _hx_tab_array(tab, length);
  251. } else {
  252. return [];
  253. }
  254. } else {
  255. return untyped _hx_tab_array(tab, length);
  256. }
  257. }
  258. /**
  259. Create a Haxe object from the given table structure
  260. **/
  261. public inline static function tableToObject<T>(t:Table<String, T>):Dynamic<T> {
  262. return untyped _hx_o(t);
  263. }
  264. /**
  265. Get Date object as string representation
  266. **/
  267. public static function dateStr(date:std.Date):String {
  268. var m = date.getMonth() + 1;
  269. var d = date.getDate();
  270. var h = date.getHours();
  271. var mi = date.getMinutes();
  272. var s = date.getSeconds();
  273. return date.getFullYear() + "-" + (if (m < 10) "0" + m else "" + m) + "-" + (if (d < 10) "0" + d else "" + d) + " "
  274. + (if (h < 10) "0" + h else "" + h) + ":" + (if (mi < 10) "0" + mi else "" + mi) + ":" + (if (s < 10) "0" + s else "" + s);
  275. }
  276. /**
  277. A 32 bit clamp function for numbers
  278. **/
  279. public inline static function clampInt32(x:Float) {
  280. return untyped _hx_bit_clamp(x);
  281. }
  282. /**
  283. Create a standard date object from a lua string representation
  284. **/
  285. public static function strDate(s:String):std.Date {
  286. switch (s.length) {
  287. case 8: // hh:mm:ss
  288. var k = s.split(":");
  289. return std.Date.fromTime(Lua.tonumber(k[0]) * 3600000. + Lua.tonumber(k[1]) * 60000. + Lua.tonumber(k[2]) * 1000.);
  290. case 10: // YYYY-MM-DD
  291. var k = s.split("-");
  292. return new std.Date(Lua.tonumber(k[0]), Lua.tonumber(k[1]) - 1, Lua.tonumber(k[2]), 0, 0, 0);
  293. case 19: // YYYY-MM-DD hh:mm:ss
  294. var k = s.split(" ");
  295. var y = k[0].split("-");
  296. var t = k[1].split(":");
  297. 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]));
  298. default:
  299. throw "Invalid date format : " + s;
  300. }
  301. }
  302. /**
  303. Helper method to determine if class cl1 extends, implements, or otherwise equals cl2
  304. **/
  305. public static function extendsOrImplements(cl1:Class<Dynamic>, cl2:Class<Dynamic>):Bool {
  306. if (cl1 == null || cl2 == null)
  307. return false;
  308. else if (cl1 == cl2)
  309. return true;
  310. else if (untyped cl1.__interfaces__ != null) {
  311. var intf = untyped cl1.__interfaces__;
  312. for (i in 1...(TableTools.maxn(intf) + 1)) {
  313. // check each interface, including extended interfaces
  314. if (extendsOrImplements(intf[i], cl2))
  315. return true;
  316. }
  317. }
  318. // check standard inheritance
  319. return extendsOrImplements(untyped cl1.__super__, cl2);
  320. }
  321. /**
  322. Returns a shell escaped version of "cmd" along with any args
  323. **/
  324. public static function shellEscapeCmd(cmd:String, ?args:Array<String>) {
  325. if (args != null) {
  326. switch (Sys.systemName()) {
  327. case "Windows":
  328. cmd = [
  329. for (a in [StringTools.replace(cmd, "/", "\\")].concat(args))
  330. SysTools.quoteWinArg(a, true)
  331. ].join(" ");
  332. case _:
  333. cmd = [cmd].concat(args).map(SysTools.quoteUnixArg).join(" ");
  334. }
  335. }
  336. return cmd;
  337. }
  338. /**
  339. Returns a temp file path that can be used for reading and writing
  340. **/
  341. public static function tempFile():String {
  342. switch (Sys.systemName()) {
  343. case "Windows":
  344. return haxe.io.Path.join([Os.getenv("TMP"), Os.tmpname()]);
  345. default:
  346. return Os.tmpname();
  347. }
  348. }
  349. public static function fieldIterator(o:Table<String, Dynamic>):Iterator<String> {
  350. if (Lua.type(o) != "table") {
  351. return {
  352. next: function() return null,
  353. hasNext: function() return false
  354. }
  355. }
  356. var tbl:Table<String, String> = cast(untyped o.__fields__ != null) ? o.__fields__ : o;
  357. var cur = Lua.pairs(tbl).next;
  358. var next_valid = function(tbl, val) {
  359. while (hiddenFields[untyped val] != null) {
  360. val = cur(tbl, val).index;
  361. }
  362. return val;
  363. }
  364. var cur_val = next_valid(tbl, cur(tbl, null).index);
  365. return {
  366. next: function() {
  367. var ret = cur_val;
  368. cur_val = next_valid(tbl, cur(tbl, cur_val).index);
  369. return ret;
  370. },
  371. hasNext: function() return cur_val != null
  372. }
  373. }
  374. static var os_patterns = [
  375. 'Windows' => ['windows', '^mingw', '^cygwin'],
  376. 'Linux' => ['linux'],
  377. 'Mac' => ['mac', 'darwin', 'osx'],
  378. 'BSD' => ['bsd$'],
  379. 'Solaris' => ['SunOS']
  380. ];
  381. public static function systemName():String {
  382. var os:String = null;
  383. if (untyped jit != null && untyped jit.os != null) {
  384. os = untyped jit.os;
  385. os = os.toLowerCase();
  386. } else {
  387. var popen_status:Bool = false;
  388. var popen_result:lua.FileHandle = null;
  389. untyped __lua__("popen_status, popen_result = pcall(_G.io.popen, '')");
  390. if (popen_status) {
  391. popen_result.close();
  392. os = lua.Io.popen('uname -s', 'r').read('*l').toLowerCase();
  393. } else {
  394. os = lua.Os.getenv('OS').toLowerCase();
  395. }
  396. }
  397. for (k in os_patterns.keys()) {
  398. for (p in os_patterns.get(k)) {
  399. if (lua.NativeStringTools.match(os, p) != null) {
  400. return k;
  401. }
  402. }
  403. }
  404. return null;
  405. }
  406. }