Boot.hx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (C)2005-2012 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. class Boot {
  24. static function __unhtml(s : String) {
  25. return s.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
  26. }
  27. public static function patternQuote(str:String){
  28. return lua.StringTools.gsub(str, "[%(%)%.%%%+%-%*%?%[%]%^%$]", function(c:String){ return "%" + c; });
  29. }
  30. public static function luaIteratorToArray<T>(itr:Void->T) : Array<T> {
  31. var i: T = null;
  32. var ret : Array<T> = [];
  33. while({i = itr(); i != null;}){
  34. ret.push(i);
  35. }
  36. return ret;
  37. }
  38. static inline function isClass(o:Dynamic) : Bool {
  39. return untyped __define_feature__("lua.Boot.isClass", o.__name__);
  40. }
  41. static inline function isEnum(e:Dynamic) : Bool {
  42. return untyped __define_feature__("lua.Boot.isEnum", e.__ename__);
  43. }
  44. static inline function getClass(o:Dynamic) : Dynamic {
  45. if (Std.is(o, Array)) return Array;
  46. else {
  47. var cl = untyped __define_feature__("lua.Boot.getClass", o.__class__);
  48. if (cl != null) return cl;
  49. else return null;
  50. }
  51. }
  52. @:ifFeature("typed_catch") private static function __instanceof(o : Dynamic,cl : Dynamic) {
  53. if( cl == null ) return false;
  54. switch( cl ) {
  55. case Int:
  56. return (untyped __lua__("bitor(o,0) == o"));
  57. case Float:
  58. return untyped __type__(o) == "number";
  59. case Bool:
  60. return untyped __type__(o) == "boolean";
  61. case String:
  62. return untyped __type__(o) == "string";
  63. case Array:
  64. // TODO: Better array check
  65. return untyped __type__(o) == "table"
  66. && o.__enum__ == null
  67. && o.length != null;
  68. case Dynamic:
  69. return true;
  70. default:
  71. if( o != null ) {
  72. // Check if o is an instance of a Haxe class or a native Lua object
  73. if (untyped __type__(cl) == "table" ) {
  74. // TODO: Fixme
  75. return true;
  76. }
  77. } else {
  78. return false;
  79. }
  80. // do not use isClass/isEnum here
  81. untyped __feature__("Class.*",if( cl == Class && o.__name__ != null ) return true);
  82. untyped __feature__("Enum.*",if( cl == Enum && o.__ename__ != null ) return true);
  83. return o.__enum__ == cl;
  84. }
  85. }
  86. @:ifFeature("typed_cast") private static function __cast(o : Dynamic, t : Dynamic) {
  87. if (__instanceof(o, t)) return o;
  88. else throw "Cannot cast " +Std.string(o) + " to " +Std.string(t);
  89. }
  90. @:keep
  91. public static function arrayNewIndex(tab:Dynamic, key:Int, value:Dynamic){
  92. untyped rawset(tab, key, value);
  93. if (key+1 > tab.length){
  94. tab.length = key + 1;
  95. }
  96. }
  97. @:keep
  98. public static function defArray(tabobj: Dynamic, length : Int) : Array<Dynamic> untyped {
  99. tabobj.length = length;
  100. setmetatable(tabobj, {
  101. __index : __lua__("Array.prototype"),
  102. __newindex : lua.Boot.arrayNewIndex
  103. });
  104. return tabobj;
  105. }
  106. public static function urlEncode(str:String){
  107. if (str != null) {
  108. str = lua.StringTools.gsub(str, "\n", "\r\n");
  109. str = lua.StringTools.gsub(str, "([^%w %-%_%.%~])", function (c) {
  110. return lua.StringTools.format("%%%02X", lua.StringTools.byte(c) + '');
  111. });
  112. str = lua.StringTools.gsub(str, " ", "+");
  113. }
  114. return str;
  115. }
  116. @:ifFeature("may_print_enum")
  117. static function __string_rec(o : Dynamic, s = '') {
  118. untyped {
  119. switch(__type__(o)){
  120. case "nil": return "null";
  121. case"number" : {
  122. if (o == Math.INFINITY) return "Infinity";
  123. else if (o == Math.NEGATIVE_INFINITY) return "-Infinity";
  124. else if (o != o) return "NaN";
  125. else return untyped tostring(o);
  126. }
  127. case "boolean" : return untyped tostring(o);
  128. case "string": return o;
  129. case "userdata": return "<userdata>";
  130. case "function": return "<function>";
  131. case "thread": return "<thread>";
  132. // TODO: come up with better fix for infinite recursive loop due to __class__
  133. case "table": { __lua__("local result = '';
  134. if o.toString ~= nil then result = o:toString()
  135. elseif o.__tostring ~= nil then result = tostring(o)
  136. elseif next(o) == nil then return '{}'
  137. else
  138. result = result .. '{ ';
  139. local first = true
  140. for i, v in pairs(o) do
  141. if i ~= '__class__' then
  142. if (first) then
  143. first = false
  144. else
  145. result = result .. ', '
  146. end
  147. result = result .. i .. ': ' .. lua.Boot.__string_rec(v, s .. 'o');
  148. end
  149. end
  150. result = result .. ' }';
  151. end");
  152. return result; }
  153. default : throw "Unknown Lua type";
  154. }
  155. }
  156. };
  157. }