HxObject.hx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 cs.internal;
  23. import cs.system.Type;
  24. import haxe.ds.Vector;
  25. private typedef StdType = std.Type;
  26. @:keep @:native('haxe.lang.HxObject')
  27. class HxObject implements IHxObject
  28. {
  29. }
  30. @:keep @:native('haxe.lang.IHxObject')
  31. interface IHxObject
  32. {
  33. }
  34. #if core_api_serialize
  35. @:meta(System.Serializable)
  36. #end
  37. @:keep @:native('haxe.lang.DynamicObject')
  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. #if !erase_generics
  64. @:keep @:native('haxe.lang.IGenericObject') interface IGenericObject
  65. {
  66. }
  67. @:nativeGen @:keep @:native('haxe.lang.GenericInterface') class GenericInterface extends cs.system.Attribute
  68. {
  69. @:readOnly public var generic(default,never):cs.system.Type;
  70. public function new(generic)
  71. {
  72. super();
  73. untyped this.generic = generic;
  74. }
  75. }
  76. #end
  77. @:keep @:native('haxe.lang.Enum') @:nativeGen
  78. #if core_api_serialize
  79. @:meta(System.Serializable)
  80. #end
  81. class HxEnum
  82. {
  83. @:readOnly private var index(default,never):Int;
  84. public function new(index:Int)
  85. {
  86. untyped this.index = index;
  87. }
  88. public function getTag():String
  89. {
  90. return throw 'Not Implemented';
  91. }
  92. public function getParams():Array<{}>
  93. {
  94. return [];
  95. }
  96. public function toString():String
  97. {
  98. return getTag();
  99. }
  100. }
  101. @:keep @:native('haxe.lang.ParamEnum') @:nativeGen
  102. private class ParamEnum extends HxEnum
  103. {
  104. @:readOnly private var params(default,never):Vector<Dynamic>;
  105. public function new(index:Int, params:Vector<Dynamic>)
  106. {
  107. super(index);
  108. untyped this.params = params;
  109. }
  110. override public function getParams():Array<{}>
  111. {
  112. return params == null ? [] : cs.Lib.array(cast params.toData());
  113. }
  114. override public function toString():String
  115. {
  116. if (params == null || params.length == 0) return getTag();
  117. var ret = new StringBuf();
  118. ret.add(getTag()); ret.add("(");
  119. var first = true;
  120. for (p in params)
  121. {
  122. if (first)
  123. first = false;
  124. else
  125. ret.add(",");
  126. ret.add(p);
  127. }
  128. ret.add(")");
  129. return ret.toString();
  130. }
  131. public function Equals(obj:Dynamic)
  132. {
  133. if (obj == this) //we cannot use == as .Equals !
  134. return true;
  135. var obj:ParamEnum = Std.instance(obj, ParamEnum);
  136. var ret = obj != null && Std.is(obj, StdType.getClass(this)) && obj.index == this.index;
  137. if (!ret)
  138. return false;
  139. if (obj.params == this.params)
  140. return true;
  141. var len = 0;
  142. if (obj.params == null || this.params == null || (len = this.params.length) != obj.params.length)
  143. return false;
  144. for (i in 0...len)
  145. {
  146. if (!StdType.enumEq(obj.params[i], this.params[i]))
  147. return false;
  148. }
  149. return true;
  150. }
  151. public function GetHashCode():Int
  152. {
  153. var h:Int = 19;
  154. if (params != null) for (p in params)
  155. {
  156. h = h * 31;
  157. if (p != null)
  158. untyped h += p.GetHashCode();
  159. }
  160. h += index;
  161. return h;
  162. }
  163. }