HxObject.hx 3.5 KB

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