HxObject.hx 3.8 KB

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