Boot.hx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. private static function __unhtml(s : String) {
  25. return s.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
  26. }
  27. private static function __trace(v,i : haxe.PosInfos) {
  28. untyped {
  29. var msg = if( i != null ) i.fileName+":"+i.lineNumber+": " else "";
  30. msg += __string_rec(v, "");
  31. if( i != null && i.customParams != null )
  32. for( v in i.customParams )
  33. msg += "," + __string_rec(v, "");
  34. var d;
  35. if( __js__("typeof")(document) != "undefined" && (d = document.getElementById("haxe:trace")) != null )
  36. d.innerHTML += __unhtml(msg)+"<br/>";
  37. else if( __js__("typeof console") != "undefined" && __js__("console").log != null )
  38. __js__("console").log(msg);
  39. }
  40. }
  41. private static function __clear_trace() {
  42. untyped {
  43. var d = document.getElementById("haxe:trace");
  44. if( d != null )
  45. d.innerHTML = "";
  46. }
  47. }
  48. static inline function isClass(o:Dynamic) : Bool {
  49. return untyped __define_feature__("lua.Boot.isClass", o.__name__);
  50. }
  51. static inline function isEnum(e:Dynamic) : Bool {
  52. return untyped __define_feature__("lua.Boot.isEnum", e.__ename__);
  53. }
  54. static inline function getClass(o:Dynamic) : Dynamic {
  55. return null;
  56. }
  57. @:keep
  58. public static function defArray(tabobj: Dynamic, length : Int) : Array<Dynamic> {
  59. untyped __lua__("tabobj.__methods = {Array.mt};
  60. setmetatable(tabobj, {__index = lua.Boot.resolveMethod})");
  61. tabobj.length = length;
  62. return tabobj;
  63. }
  64. @:keep
  65. public static function resolveMethod(table : Dynamic, key:Dynamic){
  66. untyped __lua__("for index, value in ipairs(table.__methods) do
  67. if value[key] ~= nil then return value[key] end
  68. end
  69. return nil");
  70. }
  71. @:ifFeature("may_print_enum")
  72. private static function __string_rec(o : Dynamic, s = '') {
  73. untyped {
  74. switch(__type__(o)){
  75. case "nil": return "null";
  76. case"number" : {
  77. if (o == Math.INFINITY) return "Infinity";
  78. else if (o == Math.NEGATIVE_INFINITY) return "-Infinity";
  79. else if (o != o) return "NaN";
  80. else return untyped tostring(o);
  81. }
  82. case "boolean" : return untyped tostring(o);
  83. case "string": return o;
  84. case "userdata": return "<userdata>";
  85. case "function": return "<function>";
  86. case "thread": return "<thread>";
  87. case "table": { __lua__("local result = '';
  88. if o.toString ~= nil then result = o:toString()
  89. elseif o.__tostring ~= nil then result = tostring(o)
  90. elseif next(o) == nil then return '{}'
  91. else
  92. result = result .. '{ ';
  93. local first = true
  94. for i, v in pairs(o) do
  95. if (first) then first = false
  96. else result = result .. ','
  97. end
  98. result = result .. i .. ' => ' .. lua.Boot.__string_rec(v, s .. 'o');
  99. end
  100. result = result .. ' }';
  101. end");
  102. return result; }
  103. default : throw "Unknown Lua type";
  104. }
  105. }
  106. };
  107. }