Reflect.hx 4.3 KB

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