Boot.hx 4.1 KB

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