HxObject.hx 9.1 KB

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