HxObject.hx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. private typedef StdType = std.Type;
  25. @:keep @:native('haxe.lang.HxObject')
  26. private class HxObject implements IHxObject
  27. {
  28. }
  29. @:keep @:native('haxe.lang.IHxObject')
  30. private interface IHxObject
  31. {
  32. }
  33. @:keep @:native('haxe.lang.DynamicObject')
  34. private class DynamicObject extends HxObject implements Dynamic
  35. {
  36. @:skipReflection public function toString():String
  37. {
  38. var ts = Reflect.field(this, "toString");
  39. if (ts != null)
  40. return ts();
  41. var ret = new StringBuf();
  42. ret.add("{");
  43. var first = true;
  44. for (f in Reflect.fields(this))
  45. {
  46. if( first )
  47. first = false;
  48. else
  49. ret.add(",");
  50. ret.add(" "); ret.add(f);
  51. ret.add(" : ");
  52. ret.add(Reflect.field(this, f));
  53. }
  54. if (!first) ret.add(" ");
  55. ret.add("}");
  56. return ret.toString();
  57. }
  58. }
  59. @:keep @:native('haxe.lang.IGenericObject') interface IGenericObject
  60. {
  61. }
  62. @:native('haxe.lang.Enum')
  63. @:keep @:skipCtor
  64. private class Enum
  65. {
  66. @:readOnly private var index:Int;
  67. @:readOnly private var params:Array<{}>;
  68. public function new(index:Int, params:Array<{}>)
  69. {
  70. this.index = index;
  71. this.params = params;
  72. }
  73. @:final public function getTag():String
  74. {
  75. var cl:Dynamic = StdType.getClass(this);
  76. return cl.constructs[index];
  77. }
  78. public function toString():String
  79. {
  80. if (params == null || params.length == 0) return getTag();
  81. var ret = new StringBuf();
  82. ret.add(getTag()); ret.add("(");
  83. var first = true;
  84. for (p in params)
  85. {
  86. if (first)
  87. first = false;
  88. else
  89. ret.add(",");
  90. ret.add(p);
  91. }
  92. ret.add(")");
  93. return ret.toString();
  94. }
  95. public function Equals(obj:Dynamic)
  96. {
  97. if (obj == this) //we cannot use == as .Equals !
  98. return true;
  99. var obj:Enum = cast obj;
  100. var ret = obj != null && Std.is(obj, StdType.getClass(this)) && obj.index == this.index;
  101. if (!ret)
  102. return false;
  103. if (obj.params == this.params)
  104. return true;
  105. var len = 0;
  106. if (obj.params == null || this.params == null || (len = this.params.length) != obj.params.length)
  107. return false;
  108. for (i in 0...len)
  109. {
  110. if (!StdType.enumEq(obj.params[i], this.params[i]))
  111. return false;
  112. }
  113. return true;
  114. }
  115. public function GetHashCode():Int
  116. {
  117. var h = 19;
  118. if (params != null) for (p in params)
  119. {
  120. h = h * 31;
  121. if (p != null)
  122. h += untyped p.GetHashCode();
  123. }
  124. h += index;
  125. return h;
  126. }
  127. }