Boot.hx 12 KB

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