HxObject.hx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 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. @:keep
  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.Enum') @:nativeGen
  64. class HxEnum
  65. {
  66. @:readOnly private var index(default,never):Int;
  67. public function new(index:Int)
  68. {
  69. untyped this.index = index;
  70. }
  71. public function getTag():String
  72. {
  73. return throw 'Not Implemented';
  74. }
  75. public function getParams():Array<{}>
  76. {
  77. return [];
  78. }
  79. public function toString():String
  80. {
  81. return getTag();
  82. }
  83. }
  84. @:keep @:native('haxe.lang.ParamEnum') @:nativeGen
  85. private class ParamEnum extends HxEnum
  86. {
  87. @:readOnly private var params(default,never):Vector<Dynamic>;
  88. public function new(index:Int, params:Vector<Dynamic>)
  89. {
  90. super(index);
  91. untyped this.params = params;
  92. }
  93. override public function getParams():Array<{}>
  94. {
  95. return params == null ? [] : cast params.toArray();
  96. }
  97. override public function toString():String
  98. {
  99. if (params == null || params.length == 0) return getTag();
  100. var ret = new StringBuf();
  101. ret.add(getTag()); ret.add("(");
  102. var first = true;
  103. for (p in params)
  104. {
  105. if (first)
  106. first = false;
  107. else
  108. ret.add(",");
  109. ret.add(p);
  110. }
  111. ret.add(")");
  112. return ret.toString();
  113. }
  114. public function equals(obj:Dynamic)
  115. {
  116. if (obj == this) //we cannot use == as .Equals !
  117. return true;
  118. var obj:ParamEnum = Std.is(obj,ParamEnum) ? cast obj : null;
  119. var ret = obj != null && Std.is(obj, StdType.getEnum(cast this)) && obj.index == this.index;
  120. if (!ret)
  121. return false;
  122. if (obj.params == this.params)
  123. return true;
  124. var len = 0;
  125. if (obj.params == null || this.params == null || (len = this.params.length) != obj.params.length)
  126. return false;
  127. for (i in 0...len)
  128. {
  129. if (!StdType.enumEq(obj.params[i], this.params[i]))
  130. return false;
  131. }
  132. return true;
  133. }
  134. public function hashCode():Int
  135. {
  136. var h = 19;
  137. if (params != null) for (p in params)
  138. {
  139. h = h * 31;
  140. if (p != null)
  141. untyped h += p.hashCode();
  142. }
  143. h += index;
  144. return h;
  145. }
  146. }