HxObject.hx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 cs.internal;
  23. import cs.system.Type;
  24. import haxe.ds.Vector;
  25. import cs.internal.FieldLookup;
  26. private typedef StdType = std.Type;
  27. @:keep @:native('haxe.lang.HxObject')
  28. class HxObject implements IHxObject
  29. {
  30. public function __hx_deleteField(field:String, hash:Int):Bool
  31. {
  32. return false;
  33. }
  34. }
  35. @:keep @:native('haxe.lang.IHxObject')
  36. interface IHxObject
  37. {
  38. }
  39. #if core_api_serialize
  40. @:meta(System.Serializable)
  41. #end
  42. @:keep @:native('haxe.lang.DynamicObject')
  43. class DynamicObject extends HxObject
  44. {
  45. @:skipReflection var __hx_hashes:NativeArray<Int>;
  46. @:skipReflection var __hx_dynamics:NativeArray<Dynamic>;
  47. @:skipReflection var __hx_hashes_f:NativeArray<Int>;
  48. @:skipReflection var __hx_dynamics_f:NativeArray<Float>;
  49. @:skipReflection var __hx_length:Int;
  50. @:skipReflection var __hx_length_f:Int;
  51. @:skipReflection var __hx_conflicts:FieldHashConflict;
  52. @:overload public function new()
  53. {
  54. this.__hx_hashes = new NativeArray(0);
  55. this.__hx_dynamics = new NativeArray(0);
  56. this.__hx_hashes_f = new NativeArray(0);
  57. this.__hx_dynamics_f = new NativeArray(0);
  58. this.__hx_conflicts = null;
  59. }
  60. @:overload public function new(hashes:NativeArray<Int>, dynamics:NativeArray<Dynamic>, hashes_f:NativeArray<Int>, dynamics_f:NativeArray<Float>)
  61. {
  62. this.__hx_hashes = hashes;
  63. this.__hx_dynamics = dynamics;
  64. this.__hx_hashes_f = hashes_f;
  65. this.__hx_dynamics_f = dynamics_f;
  66. this.__hx_length = hashes.length;
  67. this.__hx_length_f = hashes_f.length;
  68. this.__hx_conflicts = null;
  69. }
  70. override public function __hx_deleteField(field:String, hash:Int):Bool
  71. {
  72. if (hash < 0)
  73. {
  74. return FieldLookup.deleteHashConflict(this.__hx_conflicts, hash, field);
  75. }
  76. var res = FieldLookup.findHash(hash, this.__hx_hashes, this.__hx_length);
  77. if (res >= 0)
  78. {
  79. FieldLookup.removeInt(this.__hx_hashes, this.__hx_length, res);
  80. FieldLookup.removeDynamic(this.__hx_dynamics, this.__hx_length, res);
  81. this.__hx_length--;
  82. return true;
  83. }
  84. res = FieldLookup.findHash(hash, this.__hx_hashes_f, this.__hx_length_f);
  85. if (res >= 0)
  86. {
  87. FieldLookup.removeInt(this.__hx_hashes_f, this.__hx_length_f, res);
  88. FieldLookup.removeFloat(this.__hx_dynamics_f, this.__hx_length_f, res);
  89. this.__hx_length_f--;
  90. return true;
  91. }
  92. return false;
  93. }
  94. public function __hx_getField(field:String, hash:Int, throwErrors:Bool, isCheck:Bool, handleProperties:Bool):Dynamic
  95. {
  96. if (hash < 0)
  97. {
  98. var conflict = FieldLookup.getHashConflict(this.__hx_conflicts, hash, field);
  99. if (conflict != null)
  100. {
  101. return conflict.value;
  102. }
  103. }
  104. var res = FieldLookup.findHash(hash, this.__hx_hashes, this.__hx_length);
  105. if (res >= 0)
  106. {
  107. return this.__hx_dynamics[res];
  108. }
  109. res = FieldLookup.findHash(hash, this.__hx_hashes_f, this.__hx_length_f);
  110. if (res >= 0)
  111. {
  112. return this.__hx_dynamics_f[res];
  113. }
  114. return isCheck ? Runtime.undefined : null;
  115. }
  116. public function __hx_setField(field:String, hash:Int, value:Dynamic, handleProperties:Bool):Dynamic
  117. {
  118. if (hash < 0)
  119. {
  120. FieldLookup.setHashConflict(this.__hx_conflicts, hash, field, value);
  121. return value;
  122. }
  123. var res = FieldLookup.findHash(hash, this.__hx_hashes, this.__hx_length);
  124. if (res >= 0)
  125. {
  126. return this.__hx_dynamics[res] = value;
  127. } else {
  128. var res = FieldLookup.findHash(hash, this.__hx_hashes_f, this.__hx_length_f);
  129. if (res >= 0)
  130. {
  131. if (Std.is(value, Float))
  132. {
  133. return this.__hx_dynamics_f[res] = value;
  134. }
  135. FieldLookup.removeInt(this.__hx_hashes_f, this.__hx_length_f, res);
  136. FieldLookup.removeFloat(this.__hx_dynamics_f, this.__hx_length_f, res);
  137. this.__hx_length_f--;
  138. }
  139. }
  140. this.__hx_hashes = FieldLookup.insertInt(this.__hx_hashes, this.__hx_length, ~(res) , hash);
  141. this.__hx_dynamics = FieldLookup.insertDynamic(this.__hx_dynamics, this.__hx_length, ~(res) , value);
  142. this.__hx_length++;
  143. return value;
  144. }
  145. public function __hx_getField_f(field:String, hash:Int, throwErrors:Bool, handleProperties:Bool):Float
  146. {
  147. if (hash < 0)
  148. {
  149. var conflict = FieldLookup.getHashConflict(this.__hx_conflicts, hash, field);
  150. if (conflict != null)
  151. {
  152. return conflict.value;
  153. }
  154. }
  155. var res = FieldLookup.findHash(hash, this.__hx_hashes_f, this.__hx_length_f);
  156. if (res >= 0)
  157. {
  158. return this.__hx_dynamics_f[res];
  159. }
  160. res = FieldLookup.findHash(hash, this.__hx_hashes, this.__hx_length);
  161. if (res >= 0)
  162. {
  163. return this.__hx_dynamics[res];
  164. }
  165. return 0.0;
  166. }
  167. public function __hx_setField_f(field:String, hash:Int, value:Float, handleProperties:Bool):Float
  168. {
  169. if (hash < 0)
  170. {
  171. FieldLookup.setHashConflict(this.__hx_conflicts, hash, field, value);
  172. return value;
  173. }
  174. var res = FieldLookup.findHash(hash, this.__hx_hashes_f, this.__hx_length_f);
  175. if (res >= 0)
  176. {
  177. return this.__hx_dynamics_f[res] = value;
  178. } else {
  179. var res = FieldLookup.findHash(hash, this.__hx_hashes, this.__hx_length);
  180. if (res >= 0)
  181. {
  182. // return this.__hx_dynamics[res] = value;
  183. FieldLookup.removeInt(this.__hx_hashes, this.__hx_length, res);
  184. FieldLookup.removeDynamic(this.__hx_dynamics, this.__hx_length, res);
  185. this.__hx_length--;
  186. }
  187. }
  188. this.__hx_hashes_f = FieldLookup.insertInt(this.__hx_hashes_f, this.__hx_length_f, ~(res) , hash);
  189. this.__hx_dynamics_f = FieldLookup.insertFloat(this.__hx_dynamics_f, this.__hx_length_f, ~(res) , value);
  190. this.__hx_length_f++;
  191. return value;
  192. }
  193. public function __hx_getFields(baseArr:Array<String>):Void
  194. {
  195. for (i in 0...this.__hx_length)
  196. {
  197. baseArr.push(FieldLookup.lookupHash(this.__hx_hashes[i]));
  198. }
  199. for (i in 0...this.__hx_length_f)
  200. {
  201. baseArr.push(FieldLookup.lookupHash(this.__hx_hashes_f[i]));
  202. }
  203. FieldLookup.addHashConflictNames(this.__hx_conflicts, baseArr);
  204. }
  205. public function __hx_invokeField(field:String, hash:Int, dynargs:NativeArray<Dynamic>):Dynamic
  206. {
  207. if (field == "toString")
  208. {
  209. return this.toString();
  210. }
  211. var fn:Function = this.__hx_getField(field, hash, false, false, false);
  212. if (fn == null)
  213. {
  214. throw 'Cannot invoke field $field: It does not exist';
  215. }
  216. return untyped fn.__hx_invokeDynamic(dynargs);
  217. }
  218. @:skipReflection public function toString():String
  219. {
  220. var ts = Reflect.field(this, "toString");
  221. if (ts != null)
  222. return ts();
  223. var ret = new StringBuf();
  224. ret.add("{");
  225. var first = true;
  226. for (f in Reflect.fields(this))
  227. {
  228. if( first )
  229. first = false;
  230. else
  231. ret.add(",");
  232. ret.add(" "); ret.add(f);
  233. ret.add(" : ");
  234. ret.add(Reflect.field(this, f));
  235. }
  236. if (!first) ret.add(" ");
  237. ret.add("}");
  238. return ret.toString();
  239. }
  240. }
  241. #if !erase_generics
  242. @:keep @:native('haxe.lang.IGenericObject') interface IGenericObject
  243. {
  244. }
  245. @:nativeGen @:keep @:native('haxe.lang.GenericInterface') class GenericInterface extends cs.system.Attribute
  246. {
  247. @:readOnly public var generic(default,never):cs.system.Type;
  248. public function new(generic)
  249. {
  250. super();
  251. untyped this.generic = generic;
  252. }
  253. }
  254. #end
  255. @:keep @:native('haxe.lang.Enum') @:nativeGen
  256. #if core_api_serialize
  257. @:meta(System.Serializable)
  258. #end
  259. class HxEnum {
  260. @:readOnly var _hx_index(default,never):Int;
  261. @:protected function new(index:Int) {
  262. untyped this._hx_index = index;
  263. }
  264. public function getTag():String {
  265. return throw 'Not Implemented';
  266. }
  267. public function getParams():Array<{}> {
  268. return [];
  269. }
  270. public function toString():String {
  271. return getTag();
  272. }
  273. @:protected static function paramsToString(tag:String, params:Vector<Dynamic>):String {
  274. var ret = new StringBuf();
  275. ret.add(tag);
  276. ret.add("(");
  277. var first = true;
  278. for (p in params)
  279. {
  280. if (first)
  281. first = false;
  282. else
  283. ret.add(",");
  284. ret.add(p);
  285. }
  286. ret.add(")");
  287. return ret.toString();
  288. }
  289. @:protected static function paramsGetHashCode(index:Int, params:Vector<Dynamic>):Int {
  290. var h:Int = 19;
  291. if (params != null) for (p in params)
  292. {
  293. h = h * 31;
  294. if (p != null)
  295. untyped h += p.GetHashCode();
  296. }
  297. h += index;
  298. return h;
  299. }
  300. }