Boot.hx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (C)2005-2017 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,cl) {
  66. untyped {
  67. if( cl == Dynamic )
  68. return true;
  69. switch( __dollar__typeof(o) ) {
  70. case 1: return (cl == Int || cl == Float);
  71. case 2: return cl == Float || (cl == Int && __dollar__int(o) == o);
  72. case 3: return cl == Bool;
  73. case 5:
  74. if( cl == null )
  75. return false;
  76. return __interfLoop(o.__class__,cl) || ( o.__enum__ == cl ) || (cl == Class && o.__name__ != null) || (cl == Enum && o.__ename__ != null );
  77. default:
  78. return false;
  79. }
  80. }
  81. }
  82. private static function __serialize(o) {
  83. untyped {
  84. if( o.__class__ != null ) {
  85. var n = o.__class__.__name__;
  86. var x = __dollar__amake(n.length);
  87. for( i in 0...n.length )
  88. x[i] = n[i].__s;
  89. return x;
  90. }
  91. if( o.__enum__ != null ) {
  92. var n = o.__enum__.__ename__;
  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. throw "Can't serialize";
  99. }
  100. }
  101. private static function __tagserialize(o) untyped {
  102. var n = o.__enum__.__ename__;
  103. var x = __dollar__amake(n.length + 1);
  104. for( i in 0...n.length )
  105. x[i] = n[i].__s;
  106. x[n.length] = o.tag;
  107. return x;
  108. }
  109. private static function __unserialize(v) {
  110. untyped {
  111. if( __dollar__typeof(v) != __dollar__tarray )
  112. throw "Invalid serialized class data";
  113. for( i in 0...__dollar__asize(v) )
  114. if( __dollar__typeof(v[i]) != __dollar__tstring )
  115. throw "Invalid serialized class data";
  116. var cl = neko.Boot.__classes;
  117. for( i in 0...__dollar__asize(v) ) {
  118. cl = __dollar__objget(cl,__dollar__hash(v[i]));
  119. if( cl == null )
  120. throw ("Class not found " + Std.string(v));
  121. }
  122. if( __dollar__typeof(cl) == __dollar__tobject ) {
  123. if( cl.__name__ != null || cl.__ename__ != null )
  124. return cl.prototype;
  125. if( cl.__enum__ != null && __dollar__typeof(cl.tag) == __dollar__tstring )
  126. return cl;
  127. }
  128. throw "Invalid class " + Std.string(v);
  129. }
  130. }
  131. private static function __init() {
  132. untyped {
  133. __dollar__exports.__unserialize = __unserialize;
  134. __dollar__exports.__classes = neko.Boot.__classes;
  135. }
  136. }
  137. }