Boot.hx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * Copyright (C)2005-2016 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. // Bit and Table must be imported for basic Haxe datatypes to work.
  24. import lua.Bit;
  25. import lua.Table;
  26. import lua.Thread;
  27. import haxe.io.Path;
  28. import haxe.Constraints.Function;
  29. using lua.PairTools;
  30. @:dox(hide)
  31. class Boot {
  32. // Used temporarily for bind()
  33. static var _;
  34. static var _fid = 0;
  35. public static var platformBigEndian = NativeStringTools.byte(NativeStringTools.dump(function(){}),7) > 0;
  36. public static var hiddenFields = [
  37. "__id__", "hx__closures", "super",
  38. "prototype", "__fields__", "__ifields__", "__class__", "__properties__"
  39. ];
  40. static function __unhtml(s : String)
  41. return s.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
  42. /*
  43. Binds a function definition to the given instance
  44. */
  45. @:keep
  46. public static function bind(o:Dynamic, m: Function) : Function{
  47. if (m == null) return null;
  48. // if (m.__id.__ == nil) m.__id__ = _fid + 1;
  49. var f: Function = null;
  50. if ( o.hx__closures__ == null )
  51. Lua.rawset(o,"hx__closures__", {});
  52. else untyped f = o.hx__closures__[m];
  53. if (f == null){
  54. f = untyped __lua__("function(...) return m(o, ...) end");
  55. untyped o.hx__closures__[m] = f;
  56. }
  57. return f;
  58. }
  59. /*
  60. Indicates if the given object is a class.
  61. */
  62. static inline public function isClass(o:Dynamic) : Bool {
  63. if (Lua.type(o) != "table") return false;
  64. else return untyped __define_feature__("lua.Boot.isClass", o.__name__);
  65. }
  66. /*
  67. Indicates if the given object is a enum.
  68. */
  69. static inline public function isEnum(e:Dynamic) : Bool {
  70. if (Lua.type(e) != "table") return false;
  71. else return untyped __define_feature__("lua.Boot.isEnum", e.__ename__);
  72. }
  73. /*
  74. Returns the class of a given object, and defines the getClass feature
  75. for the given class.
  76. */
  77. static inline public function getClass(o:Dynamic) : Class<Dynamic> {
  78. if (Std.is(o, Array)) return Array;
  79. else {
  80. var cl = untyped __define_feature__("lua.Boot.getClass", o.__class__);
  81. if (cl != null) return cl;
  82. else return null;
  83. }
  84. }
  85. /*
  86. Indicates if the given object is an instance of the given Type
  87. */
  88. @:ifFeature("typed_catch")
  89. private static function __instanceof(o : Dynamic, cl : Dynamic) {
  90. if( cl == null ) return false;
  91. switch( cl ) {
  92. case Int:
  93. // TODO: matching js behavior here, but js behavior clamps. Is that correct?
  94. return (Lua.type(o) == "number" && clamp(o) == o);
  95. case Float:
  96. return Lua.type(o) == "number";
  97. case Bool:
  98. return Lua.type(o) == "boolean";
  99. case String:
  100. return Lua.type(o) == "string";
  101. case Thread:
  102. return Lua.type(o) == "thread";
  103. case UserData:
  104. return Lua.type(o) == "userdata";
  105. case Array:
  106. return Lua.type(o) == "table"
  107. && untyped o.__enum__ == null
  108. && lua.Lua.getmetatable(o) != null
  109. && lua.Lua.getmetatable(o).__index == untyped Array.prototype;
  110. case Table:
  111. return Lua.type(o) == "table";
  112. case Dynamic:
  113. return true;
  114. default: {
  115. if ( o!= null && Lua.type(o) == "table" && Lua.type(cl) == "table"){
  116. if (extendsOrImplements(getClass(o), cl)) return true;
  117. // We've exhausted standard inheritance checks. Check for simple Class/Enum eqauality
  118. // Also, do not use isClass/isEnum here, perform raw checks
  119. untyped __feature__("Class.*",if( cl == Class && o.__name__ != null ) return true);
  120. untyped __feature__("Enum.*",if( cl == Enum && o.__ename__ != null ) return true);
  121. // last chance, is it an enum instance?
  122. return o.__enum__ == cl;
  123. } else {
  124. return false;
  125. }
  126. }
  127. }
  128. }
  129. /*
  130. Indicates if the given object inherits from the given class
  131. */
  132. static function inheritsFrom(o:Dynamic, cl:Class<Dynamic>) : Bool {
  133. while (Lua.getmetatable(o) != null && Lua.getmetatable(o).__index != null){
  134. if (Lua.getmetatable(o).__index == untyped cl.prototype) return true;
  135. o = Lua.getmetatable(o).__index;
  136. }
  137. return false;
  138. }
  139. @:ifFeature("typed_cast")
  140. private static function __cast(o : Dynamic, t : Dynamic) {
  141. if (__instanceof(o, t)) return o;
  142. else throw "Cannot cast " +Std.string(o) + " to " +Std.string(t);
  143. }
  144. /*
  145. Helper method to generate a string representation of an enum
  146. */
  147. static function printEnum(o:Array<Dynamic>, s : String){
  148. if (o.length == 2){
  149. return o[0];
  150. } else {
  151. // parameterized enums are arrays
  152. var str = o[0] + "(";
  153. s += "\t";
  154. for (i in 2...o.length){
  155. if( i != 2 )
  156. str += "," + __string_rec(o[i],s);
  157. else
  158. str += __string_rec(o[i],s);
  159. }
  160. return str + ")";
  161. }
  162. }
  163. /*
  164. Helper method to generate a string representation of a class
  165. */
  166. static inline function printClass(c:Table<String,Dynamic>, s : String) : String {
  167. return '{${printClassRec(c,'',s)}}';
  168. }
  169. /*
  170. Helper method to generate a string representation of a class
  171. */
  172. static function printClassRec(c:Table<String,Dynamic>, result='', s : String) : String {
  173. c.pairsEach(function(k,v){
  174. if (result != "")
  175. result += ", ";
  176. result += '$k: ${__string_rec(v, s + "\t")}';
  177. });
  178. return result;
  179. }
  180. /*
  181. Generate a string representation for arbitrary object.
  182. */
  183. @:ifFeature("has_enum")
  184. static function __string_rec(o : Dynamic, s:String = "") {
  185. return switch(untyped __type__(o)){
  186. case "nil": "null";
  187. case "number" : {
  188. if (o == std.Math.POSITIVE_INFINITY) "Infinity";
  189. else if (o == std.Math.NEGATIVE_INFINITY) "-Infinity";
  190. else if (o != o) "NaN";
  191. else untyped tostring(o);
  192. }
  193. case "boolean" : untyped tostring(o);
  194. case "string" : o;
  195. case "userdata": "<userdata>";
  196. case "function": "<function>";
  197. case "thread" : "<thread>";
  198. case "table": {
  199. if (o.__enum__ != null) printEnum(o,s);
  200. else if (o.toString != null && !__instanceof(o,Array)) o.toString();
  201. else if (__instanceof(o, Array)) {
  202. if (s.length > 5) "[...]"
  203. else '[${[for (i in cast(o,Array<Dynamic>)) __string_rec(i,s+1)].join(",")}]';
  204. } else if (s.length > 5){
  205. "{...}";
  206. }
  207. else if (Reflect.hasField(o,"__tostring")) Lua.tostring(o);
  208. else if (Reflect.hasField(o,"__class__")) printClass(o,s+"\t");
  209. else if (Lua.next(o) == null) "{}";
  210. else {
  211. var fields = Reflect.fields(o);
  212. var buffer = new StringBuf();
  213. var first = true;
  214. buffer.add("{ ");
  215. for (f in fields){
  216. if (first) first = false;
  217. else buffer.add(", ");
  218. buffer.add('${s}${Std.string(f)} : ${untyped Std.string(o[f])}');
  219. }
  220. buffer.add(" }");
  221. buffer.toString();
  222. }
  223. };
  224. default : {
  225. throw "Unknown Lua type";
  226. null;
  227. }
  228. }
  229. }
  230. /*
  231. Define an array from the given table
  232. */
  233. public inline static function defArray<T>(tab: Table<Int,T>, ?length : Int) : Array<T> {
  234. if (length == null) length = Table.maxn(tab) + 1; // maxn doesn't count 0 index
  235. return untyped _hx_tab_array(tab, length);
  236. }
  237. /*
  238. Create a Haxe object from the given table structure
  239. */
  240. public inline static function tableToObject<T>(t:Table<String,T>) : Dynamic<T> {
  241. return untyped _hx_o(t);
  242. }
  243. /*
  244. Get Date object as string representation
  245. */
  246. public static function dateStr( date : std.Date ) : String {
  247. var m = date.getMonth() + 1;
  248. var d = date.getDate();
  249. var h = date.getHours();
  250. var mi = date.getMinutes();
  251. var s = date.getSeconds();
  252. return date.getFullYear()
  253. +"-"+(if( m < 10 ) "0"+m else ""+m)
  254. +"-"+(if( d < 10 ) "0"+d else ""+d)
  255. +" "+(if( h < 10 ) "0"+h else ""+h)
  256. +":"+(if( mi < 10 ) "0"+mi else ""+mi)
  257. +":"+(if( s < 10 ) "0"+s else ""+s);
  258. }
  259. /*
  260. A 32 bit clamp function for integers
  261. */
  262. public inline static function clamp(x:Int){
  263. return untyped _hx_bit_clamp(x);
  264. }
  265. /*
  266. Create a standard date object from a lua string representation
  267. */
  268. public static function strDate( s : String ) : std.Date {
  269. switch( s.length ) {
  270. case 8: // hh:mm:ss
  271. var k = s.split(":");
  272. var t = lua.Os.time({
  273. year : 0,
  274. month : 1,
  275. day : 1,
  276. hour : Std.parseInt(k[0]),
  277. min : Std.parseInt(k[1]),
  278. sec : Std.parseInt(k[2])
  279. });
  280. return std.Date.fromTime(t);
  281. case 10: // YYYY-MM-DD
  282. var k = s.split("-");
  283. return new std.Date(Std.parseInt(k[0]), Std.parseInt(k[1]) - 1, Std.parseInt(k[2]),0,0,0);
  284. case 19: // YYYY-MM-DD hh:mm:ss
  285. var k = s.split(" ");
  286. var y = k[0].split("-");
  287. var t = k[1].split(":");
  288. return new std.Date(cast y[0],Std.parseInt(y[1]) - 1, Std.parseInt(y[2]),Std.parseInt(t[0]),Std.parseInt(t[1]),Std.parseInt(t[2]));
  289. default:
  290. throw "Invalid date format : " + s;
  291. }
  292. }
  293. /*
  294. Helper method to determine if class cl1 extends, implements, or otherwise equals cl2
  295. */
  296. public static function extendsOrImplements(cl1 : Class<Dynamic>, cl2 : Class<Dynamic>) : Bool {
  297. if (cl1 == null || cl2 == null) return false;
  298. else if (cl1 == cl2) return true;
  299. else if (untyped cl1.__interfaces__ != null) {
  300. var intf = untyped cl1.__interfaces__;
  301. for (i in 1...(Table.maxn(intf) + 1)){
  302. // check each interface, including extended interfaces
  303. if (extendsOrImplements(intf[1], cl2)) return true;
  304. }
  305. }
  306. // check standard inheritance
  307. return extendsOrImplements(untyped cl1.__super__, cl2);
  308. }
  309. /*
  310. Create an empty table.
  311. */
  312. public inline static function createTable<K,V>(?arr:Array<V>, ?hsh:Dynamic<V>) : Table<K,V> {
  313. return untyped __lua_table__(arr,hsh);
  314. }
  315. /*
  316. Create an empty table for vectors
  317. */
  318. // TODO: provide a simpler anonymous table generator syntax in genlua
  319. public static function createVectorTable<K,V>(length:Int) : Table<K,V> {
  320. var table : Table<K,V> = untyped __lua__("{}");
  321. untyped table.length = length;
  322. return table;
  323. }
  324. /*
  325. Returns a shell escaped version of "cmd" along with any args
  326. */
  327. public static function shellEscapeCmd(cmd : String, ?args : Array<String>){
  328. if (args != null) {
  329. switch (Sys.systemName()) {
  330. case "Windows":
  331. cmd = [
  332. for (a in [StringTools.replace(cmd, "/", "\\")].concat(args))
  333. StringTools.quoteWinArg(a, true)
  334. ].join(" ");
  335. case _:
  336. cmd = [cmd].concat(args).map(StringTools.quoteUnixArg).join(" ");
  337. }
  338. }
  339. return cmd;
  340. }
  341. /*
  342. Returns a temp file path that can be used for reading and writing
  343. */
  344. public static function tempFile() : String {
  345. switch (Sys.systemName()){
  346. case "Windows" : return Path.join([Os.getenv("TMP"), Os.tmpname()]);
  347. default : return Os.tmpname();
  348. }
  349. }
  350. public static function __init__(){
  351. // anonymous to instance method wrapper
  352. haxe.macro.Compiler.includeFile("lua/_lua/_hx_function_to_instance_function.lua");
  353. // static to instance class wrapper
  354. haxe.macro.Compiler.includeFile("lua/_lua/_hx_static_to_instance_function.lua");
  355. // simple apply method
  356. haxe.macro.Compiler.includeFile("lua/_lua/_hx_apply.lua");
  357. }
  358. }