Boot.hx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package neko;
  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 = 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. private static function __instanceof(o,cl) {
  66. untyped {
  67. if( cl == Dynamic )
  68. return true;
  69. switch __dollar__typeof(o) {
  70. case __dollar__tint: return (cl == Int || cl == Float);
  71. case __dollar__tfloat: return cl == Float;
  72. case __dollar__tbool: return cl == Bool;
  73. case __dollar__tobject:
  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. String = NekoString__;
  134. Array = NekoArray__;
  135. __dollar__exports.__unserialize = __unserialize;
  136. __dollar__exports.__classes = neko.Boot.__classes;
  137. }
  138. }
  139. }