Boot.hx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 neko;
  23. @:dox(hide)
  24. @:keep
  25. class Boot {
  26. private static function __tmp_str() {
  27. return untyped "<...>".__s;
  28. }
  29. private static function __enum_str(e:Dynamic) {
  30. if (e.args == null)
  31. return e.tag;
  32. var s:String = e.tag + untyped "(".__s;
  33. var i = 0;
  34. var l = untyped __dollar__asize(e.args);
  35. var old = e.__string;
  36. e.__string = __tmp_str;
  37. while (i < l) {
  38. if (i != 0)
  39. s += untyped ",".__s;
  40. try {
  41. s += untyped __dollar__string(e.args[i]);
  42. } catch (err:Dynamic) {
  43. s += __tmp_str();
  44. }
  45. i += 1;
  46. }
  47. e.__string = old;
  48. #if haxe_next
  49. return NativeString.ofString(s + untyped ")".__s);
  50. #else
  51. return s + untyped ")".__s;
  52. #end
  53. }
  54. private static function __interfLoop(cc:Dynamic, cl:Dynamic) {
  55. if (cc == null)
  56. return false;
  57. if (cc == cl)
  58. return true;
  59. var intf:Dynamic = cc.__interfaces__;
  60. if (intf != null)
  61. for (i in 0...intf.length) {
  62. var i = intf[i];
  63. if (i == cl || __interfLoop(i, cl))
  64. return true;
  65. }
  66. return __interfLoop(cc.__super__, cl);
  67. }
  68. @:ifFeature("typed_catch")
  69. private static function __instanceof(o:Dynamic, cl:Dynamic) {
  70. untyped {
  71. if (cl == Dynamic)
  72. return o != null;
  73. switch (__dollar__typeof(o)) {
  74. case 1:
  75. return (cl == Int || cl == Float);
  76. case 2:
  77. return cl == Float || (cl == Int && __dollar__int(o) == o);
  78. case 3:
  79. return cl == Bool;
  80. case 5:
  81. if (cl == null)
  82. return false;
  83. return __interfLoop(o.__class__, cl) || (o.__enum__ == cl) || (cl == Class && o.__name__ != null) || (cl == Enum && o.__ename__ != null);
  84. default:
  85. return false;
  86. }
  87. }
  88. }
  89. private static function __serialize(o) {
  90. untyped {
  91. if (o.__class__ != null) {
  92. var n = o.__class__.__name__;
  93. var x = __dollar__amake(n.length);
  94. for (i in 0...n.length)
  95. x[i] = n[i].__s;
  96. return x;
  97. }
  98. if (o.__enum__ != null) {
  99. var n = o.__enum__.__ename__;
  100. var x = __dollar__amake(n.length);
  101. for (i in 0...n.length)
  102. x[i] = n[i].__s;
  103. return x;
  104. }
  105. throw "Can't serialize";
  106. }
  107. }
  108. private static function __tagserialize(o:Dynamic) untyped {
  109. var n = o.__enum__.__ename__;
  110. var x = __dollar__amake(n.length + 1);
  111. for (i in 0...n.length)
  112. x[i] = n[i].__s;
  113. x[n.length] = o.tag;
  114. return x;
  115. }
  116. private static function __unserialize(v:Dynamic) {
  117. untyped {
  118. if (__dollar__typeof(v) != __dollar__tarray)
  119. throw "Invalid serialized class data";
  120. for (i in 0...__dollar__asize(v))
  121. if (__dollar__typeof(v[i]) != __dollar__tstring)
  122. throw "Invalid serialized class data";
  123. var cl:Dynamic = neko.Boot.__classes;
  124. for (i in 0...__dollar__asize(v)) {
  125. cl = __dollar__objget(cl, __dollar__hash(v[i]));
  126. if (cl == null)
  127. throw("Class not found " + Std.string(v));
  128. }
  129. if (__dollar__typeof(cl) == __dollar__tobject) {
  130. if (cl.__name__ != null || cl.__ename__ != null)
  131. return cl.prototype;
  132. if (cl.__enum__ != null && __dollar__typeof(cl.tag) == __dollar__tstring)
  133. return cl;
  134. }
  135. throw "Invalid class " + Std.string(v);
  136. }
  137. }
  138. private static function __init() {
  139. untyped {
  140. __dollar__exports.__unserialize = __unserialize;
  141. __dollar__exports.__classes = neko.Boot.__classes;
  142. }
  143. }
  144. }