Reflect.hx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 o.hasOwnProperty(field);
  25. }
  26. public static function field(o:Dynamic, field:String):Dynamic untyped {
  27. return o != null && __in__(field, o) ? o[field] : null;
  28. }
  29. public inline static function setField(o:Dynamic, field:String, value:Dynamic):Void untyped {
  30. o[field] = value;
  31. }
  32. public static function getProperty(o:Dynamic, field:String):Dynamic untyped {
  33. if (o == null)
  34. return null;
  35. var getter = 'get_$field';
  36. if (__in__(getter, o)) {
  37. return o[getter]();
  38. }
  39. return __in__(field, o) ? o[field] : null;
  40. }
  41. public static function setProperty(o:Dynamic, field:String, value:Dynamic):Void untyped {
  42. var setter = 'set_$field';
  43. if (__in__(setter, o)) {
  44. o[setter](value);
  45. } else {
  46. o[field] = value;
  47. }
  48. }
  49. public inline static function callMethod(o:Dynamic, func:haxe.Constraints.Function, args:Array<Dynamic>):Dynamic untyped {
  50. return func.apply(o, args);
  51. }
  52. public static function fields(o:Dynamic):Array<String> untyped {
  53. if (o == null)
  54. return new Array();
  55. var i = 0;
  56. var a = [];
  57. while (untyped __has_next__(o, i)) {
  58. var prop = untyped __forin__(o, i);
  59. if (o.hasOwnProperty(prop))
  60. a.push(prop);
  61. }
  62. return a;
  63. }
  64. public static function isFunction(f:Dynamic):Bool untyped {
  65. return __typeof__(f) == "function";
  66. }
  67. public static function compare<T>(a:T, b:T):Int {
  68. var a:Dynamic = a;
  69. var b:Dynamic = b;
  70. return (a == b) ? 0 : ((a > b) ? 1 : -1);
  71. }
  72. public static function compareMethods(f1:Dynamic, f2:Dynamic):Bool {
  73. return f1 == f2; // VM-level closures
  74. }
  75. public static function isObject(v:Dynamic):Bool untyped {
  76. if (v == null)
  77. return false;
  78. var t = __typeof__(v);
  79. if (t == "object") {
  80. return !isEnumValue(v);
  81. }
  82. return (t == "string");
  83. }
  84. public static function isEnumValue(v:Dynamic):Bool {
  85. return try v.__enum__ == true catch (e:Dynamic) false;
  86. }
  87. public static function deleteField(o:Dynamic, field:String):Bool untyped {
  88. if (o.hasOwnProperty(field) != true)
  89. return false;
  90. __delete__(o, field);
  91. return true;
  92. }
  93. public static function copy<T>(o:Null<T>):Null<T> {
  94. if (o == null)
  95. return null;
  96. var o2:Dynamic = {};
  97. for (f in Reflect.fields(o))
  98. Reflect.setField(o2, f, Reflect.field(o, f));
  99. return o2;
  100. }
  101. public static function makeVarArgs<T>(f:Array<Dynamic>->T):Dynamic {
  102. return function(__arguments__) {
  103. return f(__arguments__);
  104. };
  105. }
  106. }