HxObject.hx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 java.internal;
  23. import java.internal.IEquatable;
  24. private typedef StdType = Type;
  25. @:native('haxe.lang.HxObject')
  26. @:keep
  27. private class HxObject implements IHxObject
  28. {
  29. }
  30. @:native('haxe.lang.IHxObject')
  31. @:keep
  32. private interface IHxObject
  33. {
  34. }
  35. @:native('haxe.lang.DynamicObject')
  36. @:replaceReflection
  37. @:keep
  38. private 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. @:native('haxe.lang.Enum')
  64. //@:skipCtor
  65. @:nativeGen
  66. @:keep
  67. private class Enum
  68. {
  69. @:readOnly private var index(default,never):Int;
  70. @:readOnly private var params(default,never):Array<{}>;
  71. public function new(index:Int, params:Array<{}>)
  72. {
  73. untyped this.index = index;
  74. untyped this.params = params;
  75. }
  76. public function getTag():String
  77. {
  78. var cl:Dynamic = StdType.getEnum(cast this);
  79. return cl.constructs[index];
  80. }
  81. public function toString():String
  82. {
  83. if (params == null || params.length == 0) return getTag();
  84. var ret = new StringBuf();
  85. ret.add(getTag()); ret.add("(");
  86. var first = true;
  87. for (p in params)
  88. {
  89. if (first)
  90. first = false;
  91. else
  92. ret.add(",");
  93. ret.add(p);
  94. }
  95. ret.add(")");
  96. return ret.toString();
  97. }
  98. public function equals(obj:Dynamic)
  99. {
  100. if (obj == this) //we cannot use == as .Equals !
  101. return true;
  102. var obj:Enum = cast obj;
  103. var ret = obj != null && Std.is(obj, StdType.getEnum(cast this)) && obj.index == this.index;
  104. if (!ret)
  105. return false;
  106. if (obj.params == this.params)
  107. return true;
  108. var len = 0;
  109. if (obj.params == null || this.params == null || (len = this.params.length) != obj.params.length)
  110. return false;
  111. for (i in 0...len)
  112. {
  113. if (!StdType.enumEq(obj.params[i], this.params[i]))
  114. return false;
  115. }
  116. return true;
  117. }
  118. public function hashCode():Int
  119. {
  120. var h = 19;
  121. if (params != null) for (p in params)
  122. {
  123. h = h * 31;
  124. if (p != null)
  125. h += untyped p.hashCode();
  126. }
  127. h += index;
  128. return h;
  129. }
  130. }