HxObject.hx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 cs.internal;
  23. import cs.system.Type;
  24. import haxe.ds.Vector;
  25. private typedef StdType = std.Type;
  26. @:keep @:native('haxe.lang.HxObject')
  27. class HxObject implements IHxObject
  28. {
  29. }
  30. @:keep @:native('haxe.lang.IHxObject')
  31. interface IHxObject
  32. {
  33. }
  34. #if core_api_serialize
  35. @:meta(System.Serializable)
  36. #end
  37. @:keep @:native('haxe.lang.DynamicObject')
  38. class DynamicObject extends HxObject implements Dynamic
  39. {
  40. @:skipReflection public function toString():String
  41. {
  42. var ts = Reflect.field(this, "toString");
  43. if (ts != null)
  44. return ts();
  45. var ret = new StringBuf();
  46. ret.add("{");
  47. var first = true;
  48. for (f in Reflect.fields(this))
  49. {
  50. if( first )
  51. first = false;
  52. else
  53. ret.add(",");
  54. ret.add(" "); ret.add(f);
  55. ret.add(" : ");
  56. ret.add(Reflect.field(this, f));
  57. }
  58. if (!first) ret.add(" ");
  59. ret.add("}");
  60. return ret.toString();
  61. }
  62. }
  63. @:keep @:native('haxe.lang.IGenericObject') interface IGenericObject
  64. {
  65. }
  66. @:keep @:native('haxe.lang.Enum') @:nativeGen
  67. #if core_api_serialize
  68. @:meta(System.Serializable)
  69. #end
  70. class HxEnum
  71. {
  72. @:readOnly private var index(default,never):Int;
  73. public function new(index:Int)
  74. {
  75. untyped this.index = index;
  76. }
  77. public function getTag():String
  78. {
  79. return throw 'Not Implemented';
  80. }
  81. public function getParams():Array<{}>
  82. {
  83. return [];
  84. }
  85. public function toString():String
  86. {
  87. return getTag();
  88. }
  89. }
  90. @:keep @:native('haxe.lang.ParamEnum') @:nativeGen
  91. private class ParamEnum extends HxEnum
  92. {
  93. @:readOnly private var params(default,never):Vector<Dynamic>;
  94. public function new(index:Int, params:Vector<Dynamic>)
  95. {
  96. super(index);
  97. untyped this.params = params;
  98. }
  99. override public function getParams():Array<{}>
  100. {
  101. return params == null ? [] : cs.Lib.array(cast params.toData());
  102. }
  103. override public function toString():String
  104. {
  105. if (params == null || params.length == 0) return getTag();
  106. var ret = new StringBuf();
  107. ret.add(getTag()); ret.add("(");
  108. var first = true;
  109. for (p in params)
  110. {
  111. if (first)
  112. first = false;
  113. else
  114. ret.add(",");
  115. ret.add(p);
  116. }
  117. ret.add(")");
  118. return ret.toString();
  119. }
  120. public function Equals(obj:Dynamic)
  121. {
  122. if (obj == this) //we cannot use == as .Equals !
  123. return true;
  124. var obj:ParamEnum = Std.instance(obj, ParamEnum);
  125. var ret = obj != null && Std.is(obj, StdType.getClass(this)) && obj.index == this.index;
  126. if (!ret)
  127. return false;
  128. if (obj.params == this.params)
  129. return true;
  130. var len = 0;
  131. if (obj.params == null || this.params == null || (len = this.params.length) != obj.params.length)
  132. return false;
  133. for (i in 0...len)
  134. {
  135. if (!StdType.enumEq(obj.params[i], this.params[i]))
  136. return false;
  137. }
  138. return true;
  139. }
  140. public function GetHashCode():Int
  141. {
  142. var h:Int = 19;
  143. if (params != null) for (p in params)
  144. {
  145. h = h * 31;
  146. if (p != null)
  147. untyped h += p.GetHashCode();
  148. }
  149. h += index;
  150. return h;
  151. }
  152. }