Boot.hx 4.2 KB

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