HxObject.hx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright (C)2005-2018 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
  39. {
  40. @:skipReflection var __hx_fields:java.NativeArray<String>;
  41. @:skipReflection var __hx_dynamics:java.NativeArray<Dynamic>;
  42. @:skipReflection var __hx_fields_f:java.NativeArray<String>;
  43. @:skipReflection var __hx_dynamics_f:java.NativeArray<Float>;
  44. @:skipReflection var __hx_length:Int;
  45. @:skipReflection var __hx_length_f:Int;
  46. @:overload public function new()
  47. {
  48. this.__hx_fields = new java.NativeArray(0);
  49. this.__hx_dynamics = new java.NativeArray(0);
  50. this.__hx_fields_f = new java.NativeArray(0);
  51. this.__hx_dynamics_f = new java.NativeArray(0);
  52. }
  53. @:overload public function new(fields:NativeArray<String>, dynamics:NativeArray<Dynamic>, fields_f:NativeArray<String>, dynamics_f:NativeArray<Float>)
  54. {
  55. this.__hx_fields = fields;
  56. this.__hx_dynamics = dynamics;
  57. this.__hx_fields_f = fields_f;
  58. this.__hx_dynamics_f = dynamics_f;
  59. this.__hx_length = fields.length;
  60. this.__hx_length_f = fields_f.length;
  61. }
  62. public function __hx_deleteField(field:String):Bool
  63. {
  64. var res = FieldLookup.findHash(field, this.__hx_fields, this.__hx_length);
  65. if (res >= 0)
  66. {
  67. FieldLookup.removeString(this.__hx_fields, this.__hx_length, res);
  68. FieldLookup.removeDynamic(this.__hx_dynamics, this.__hx_length, res);
  69. this.__hx_length--;
  70. return true;
  71. }
  72. var res = FieldLookup.findHash(field, this.__hx_fields_f, this.__hx_length_f);
  73. if (res >= 0)
  74. {
  75. FieldLookup.removeString(this.__hx_fields_f, this.__hx_length_f, res);
  76. FieldLookup.removeFloat(this.__hx_dynamics_f, this.__hx_length_f, res);
  77. this.__hx_length_f--;
  78. return true;
  79. }
  80. return false;
  81. }
  82. public function __hx_getField(field:String, throwErrors:Bool, isCheck:Bool, handleProperties:Bool):Dynamic
  83. {
  84. var res = FieldLookup.findHash(field, this.__hx_fields, this.__hx_length);
  85. if (res >= 0)
  86. {
  87. return this.__hx_dynamics[res];
  88. }
  89. res = FieldLookup.findHash(field, this.__hx_fields_f, this.__hx_length_f);
  90. if (res >= 0)
  91. {
  92. return this.__hx_dynamics_f[res];
  93. }
  94. return isCheck ? Runtime.undefined : null;
  95. }
  96. public function __hx_setField(field:String, value:Dynamic, handleProperties:Bool):Dynamic
  97. {
  98. var res = FieldLookup.findHash(field, this.__hx_fields, this.__hx_length);
  99. if (res >= 0)
  100. {
  101. return this.__hx_dynamics[res] = value;
  102. } else {
  103. var res = FieldLookup.findHash(field, this.__hx_fields_f, this.__hx_length_f);
  104. if (res >= 0)
  105. {
  106. if (Std.is(value, Float))
  107. {
  108. return this.__hx_dynamics_f[res] = value;
  109. }
  110. FieldLookup.removeString(this.__hx_fields_f, this.__hx_length_f, res);
  111. FieldLookup.removeFloat(this.__hx_dynamics_f, this.__hx_length_f, res);
  112. this.__hx_length_f--;
  113. }
  114. }
  115. this.__hx_fields = FieldLookup.insertString(this.__hx_fields, this.__hx_length, ~(res) , field);
  116. this.__hx_dynamics = FieldLookup.insertDynamic(this.__hx_dynamics, this.__hx_length, ~(res) , value);
  117. this.__hx_length++;
  118. return value;
  119. }
  120. public function __hx_getField_f(field:String, throwErrors:Bool, handleProperties:Bool):Float
  121. {
  122. var res = FieldLookup.findHash(field, this.__hx_fields_f, this.__hx_length_f);
  123. if (res >= 0)
  124. {
  125. return this.__hx_dynamics_f[res];
  126. }
  127. res = FieldLookup.findHash(field, this.__hx_fields, this.__hx_length);
  128. if (res >= 0)
  129. {
  130. return this.__hx_dynamics[res];
  131. }
  132. return 0.0;
  133. }
  134. public function __hx_setField_f(field:String, value:Float, handleProperties:Bool):Float
  135. {
  136. var res = FieldLookup.findHash(field, this.__hx_fields_f, this.__hx_length_f);
  137. if (res >= 0)
  138. {
  139. return this.__hx_dynamics_f[res] = value;
  140. } else {
  141. var res = FieldLookup.findHash(field, this.__hx_fields, this.__hx_length);
  142. if (res >= 0)
  143. {
  144. // return this.__hx_dynamics[res] = value;
  145. FieldLookup.removeString(this.__hx_fields, this.__hx_length, res);
  146. FieldLookup.removeDynamic(this.__hx_dynamics, this.__hx_length, res);
  147. this.__hx_length--;
  148. }
  149. }
  150. this.__hx_fields_f = FieldLookup.insertString(this.__hx_fields_f, this.__hx_length_f, ~(res) , field);
  151. this.__hx_dynamics_f = FieldLookup.insertFloat(this.__hx_dynamics_f, this.__hx_length_f, ~(res) , value);
  152. this.__hx_length_f++;
  153. return value;
  154. }
  155. public function __hx_getFields(baseArr:Array<String>):Void
  156. {
  157. for (i in 0...this.__hx_length)
  158. {
  159. baseArr.push(this.__hx_fields[i]);
  160. }
  161. for (i in 0...this.__hx_length_f)
  162. {
  163. baseArr.push(this.__hx_fields_f[i]);
  164. }
  165. }
  166. public function __hx_invokeField(field:String, dynargs:NativeArray<Dynamic>):Dynamic
  167. {
  168. if (field == "toString")
  169. {
  170. return this.toString();
  171. }
  172. var fn:Function = this.__hx_getField(field, false, false, false);
  173. if (fn == null)
  174. {
  175. throw 'Cannot invoke field $field: It does not exist';
  176. }
  177. return untyped fn.__hx_invokeDynamic(dynargs);
  178. }
  179. public function toString():String
  180. {
  181. var ts = this.__hx_getField("toString", false, false, false);
  182. if (ts != null)
  183. return ts();
  184. var ret = new StringBuf();
  185. ret.add("{");
  186. var first = true;
  187. for (f in Reflect.fields(this))
  188. {
  189. if( first )
  190. first = false;
  191. else
  192. ret.add(",");
  193. ret.add(" "); ret.add(f);
  194. ret.add(" : ");
  195. ret.add(Reflect.field(this, f));
  196. }
  197. if (!first) ret.add(" ");
  198. ret.add("}");
  199. return ret.toString();
  200. }
  201. }
  202. @:keep @:native('haxe.lang.Enum') @:nativeGen
  203. class HxEnum
  204. {
  205. @:readOnly private var index(default,never):Int;
  206. public function new(index:Int)
  207. {
  208. untyped this.index = index;
  209. }
  210. public function getTag():String
  211. {
  212. return throw 'Not Implemented';
  213. }
  214. public function getParams():Array<{}>
  215. {
  216. return [];
  217. }
  218. public function toString():String
  219. {
  220. return getTag();
  221. }
  222. }
  223. @:keep @:native('haxe.lang.ParamEnum') @:nativeGen
  224. private class ParamEnum extends HxEnum
  225. {
  226. @:readOnly private var params(default,never):Vector<Dynamic>;
  227. public function new(index:Int, params:Vector<Dynamic>)
  228. {
  229. super(index);
  230. untyped this.params = params;
  231. }
  232. override public function getParams():Array<{}>
  233. {
  234. return params == null ? [] : cast params.toArray();
  235. }
  236. override public function toString():String
  237. {
  238. if (params == null || params.length == 0) return getTag();
  239. var ret = new StringBuf();
  240. ret.add(getTag()); ret.add("(");
  241. var first = true;
  242. for (p in params)
  243. {
  244. if (first)
  245. first = false;
  246. else
  247. ret.add(",");
  248. ret.add(p);
  249. }
  250. ret.add(")");
  251. return ret.toString();
  252. }
  253. public function equals(obj:Dynamic)
  254. {
  255. if (obj == this) //we cannot use == as .Equals !
  256. return true;
  257. var obj:ParamEnum = Std.is(obj,ParamEnum) ? cast obj : null;
  258. var ret = obj != null && Std.is(obj, StdType.getEnum(cast this)) && obj.index == this.index;
  259. if (!ret)
  260. return false;
  261. if (obj.params == this.params)
  262. return true;
  263. var len = 0;
  264. if (obj.params == null || this.params == null || (len = this.params.length) != obj.params.length)
  265. return false;
  266. for (i in 0...len)
  267. {
  268. if (!StdType.enumEq(obj.params[i], this.params[i]))
  269. return false;
  270. }
  271. return true;
  272. }
  273. public function hashCode():Int
  274. {
  275. var h = 19;
  276. if (params != null) for (p in params)
  277. {
  278. h = h * 31;
  279. if (p != null)
  280. untyped h += p.hashCode();
  281. }
  282. h += index;
  283. return h;
  284. }
  285. }