Boot.hx 4.1 KB

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