Reflect.hx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. @:coreApi class Reflect {
  23. public static function hasField(o:Dynamic, field:String):Bool untyped {
  24. return $typeof(o) == $tobject && $objfield(o, $fasthash(field.__s));
  25. }
  26. public inline static function field(o:Dynamic, field:String):Dynamic untyped {
  27. return if ($typeof(o) != $tobject) null else $objget(o, $fasthash(field.__s));
  28. }
  29. public inline static function setField(o:Dynamic, field:String, value:Dynamic):Void untyped {
  30. if ($typeof(o) == $tobject)
  31. $objset(o, $hash(field.__s), value);
  32. }
  33. public static inline function getProperty(o:Dynamic, field:String):Dynamic untyped {
  34. var tmp;
  35. return if ($typeof(o) != $tobject) null else if (o.__properties__ != null
  36. && (tmp = $objget(o.__properties__, $fasthash("get_".__s + field.__s))) != null) $call($objget(o, $fasthash(tmp)), o, $array()) else $objget(o,
  37. $fasthash(field.__s));
  38. }
  39. public static inline function setProperty(o:Dynamic, field:String, value:Dynamic):Void untyped {
  40. if ($typeof(o) == $tobject) {
  41. var tmp;
  42. if (o.__properties__ != null && (tmp = $objget(o.__properties__, $fasthash("set_".__s + field.__s))) != null)
  43. $call($objget(o, $fasthash(tmp)), o, $array(value))
  44. else
  45. $objset(o, $hash(field.__s), value);
  46. }
  47. }
  48. public static function callMethod(o:Dynamic, func:haxe.Constraints.Function, args:Array<Dynamic>):Dynamic untyped {
  49. var a = args.__neko();
  50. // pad missing args with null's
  51. var n = $nargs(func);
  52. if (n > $asize(a)) {
  53. var a2 = $amake(n);
  54. $ablit(a2, 0, a, 0, $asize(a));
  55. a = a2;
  56. }
  57. return $call(func, o, a);
  58. }
  59. public static function fields(o:Dynamic):Array<String> untyped {
  60. if ($typeof(o) != $tobject)
  61. return new Array<String>();
  62. else {
  63. var a:neko.NativeArray<Int> = $objfields(o);
  64. var i = 0;
  65. var hasid = false;
  66. var l = $asize(a);
  67. while (i < l) {
  68. var fid = a[i];
  69. if (fid == -190054693)
  70. hasid = true; // $hash("__id__")
  71. a[i] = new String($field(fid));
  72. i++;
  73. }
  74. var a:Array<String> = Array.new1(a, l);
  75. if (hasid)
  76. a.remove("__id__");
  77. return a;
  78. }
  79. }
  80. public static function isFunction(f:Dynamic):Bool untyped {
  81. return $typeof(f) == $tfunction;
  82. }
  83. public inline static function compare<T>(a:T, b:T):Int {
  84. return untyped $compare(a, b);
  85. }
  86. public inline static function compareMethods(f1:Dynamic, f2:Dynamic):Bool {
  87. return same_closure(f1, f2);
  88. }
  89. public static function isObject(v:Dynamic):Bool untyped {
  90. return $typeof(v) == $tobject && v.__enum__ == null;
  91. }
  92. public static function isEnumValue(v:Dynamic):Bool untyped {
  93. return $typeof(v) == $tobject && v.__enum__ != null;
  94. }
  95. public inline static function deleteField(o:Dynamic, field:String):Bool untyped {
  96. return $objremove(o, $fasthash(field.__s));
  97. }
  98. public static function copy<T>(o:Null<T>):Null<T> {
  99. if (o == null)
  100. return null;
  101. return untyped $new(o);
  102. }
  103. public static function makeVarArgs<T>(f:Array<Dynamic>->T):Dynamic {
  104. return untyped $varargs(function(a) {
  105. return f(Array.new1(a, $asize(a)));
  106. });
  107. }
  108. static var same_closure = try neko.Lib.load("std", "same_closure", 2) catch (e:Dynamic) function(f1, f2) return f1 == f2;
  109. }