HxObject.hx 8.6 KB

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