Reflect.hx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. @:pure
  24. public inline static function hasField(o:Dynamic, field:String):Bool {
  25. return js.lib.Object.prototype.hasOwnProperty.call(o, field);
  26. }
  27. @:pure
  28. public static function field(o:Dynamic, field:String):Dynamic {
  29. try
  30. return o[cast field]
  31. catch (e:Dynamic)
  32. return null;
  33. }
  34. public inline static function setField(o:Dynamic, field:String, value:Dynamic):Void {
  35. o[cast field] = value;
  36. }
  37. public static function getProperty(o:Dynamic, field:String):Dynamic untyped {
  38. var tmp;
  39. return if (o == null) __define_feature__("Reflect.getProperty",
  40. null) else if (o.__properties__ && (tmp = o.__properties__["get_" + field])) o[tmp]() else o[field];
  41. }
  42. public static function setProperty(o:Dynamic, field:String, value:Dynamic):Void untyped {
  43. var tmp;
  44. if (o.__properties__ && (tmp = o.__properties__["set_" + field]))
  45. o[tmp](value)
  46. else
  47. o[field] = __define_feature__("Reflect.setProperty", value);
  48. }
  49. public inline static function callMethod(o:Dynamic, func:haxe.Constraints.Function, args:Array<Dynamic>):Dynamic {
  50. return (cast func : js.lib.Function).apply(o, args);
  51. }
  52. public static function fields(o:Dynamic):Array<String> {
  53. var a = [];
  54. if (o != null) untyped {
  55. var hasOwnProperty = js.lib.Object.prototype.hasOwnProperty;
  56. js.Syntax.code("for( var f in o ) {");
  57. if (f != "__id__" && f != "hx__closures__" && hasOwnProperty.call(o, f))
  58. a.push(f);
  59. js.Syntax.code("}");
  60. }
  61. return a;
  62. }
  63. @:access(js.Boot)
  64. public static function isFunction(f:Dynamic):Bool {
  65. return js.Syntax.typeof(f) == "function" && !(js.Boot.isClass(f) || js.Boot.isEnum(f));
  66. }
  67. public static function compare<T>(a:T, b:T):Int {
  68. return (a == b) ? 0 : (((cast a) > (cast b)) ? 1 : -1);
  69. }
  70. public static inline function compareMethods(f1:Dynamic, f2:Dynamic):Bool {
  71. return f1 == f2;
  72. }
  73. @:access(js.Boot)
  74. public static function isObject(v:Dynamic):Bool {
  75. if (v == null)
  76. return false;
  77. var t = js.Syntax.typeof(v);
  78. return (t == "string" || (t == "object" && v.__enum__ == null))
  79. || (t == "function" && (js.Boot.isClass(v) || js.Boot.isEnum(v)) != null);
  80. }
  81. public static function isEnumValue(v:Dynamic):Bool {
  82. return v != null && v.__enum__ != null;
  83. }
  84. public static function deleteField(o:Dynamic, field:String):Bool {
  85. if (!hasField(o, field))
  86. return false;
  87. js.Syntax.delete(o, field);
  88. return true;
  89. }
  90. public static function copy<T>(o:Null<T>):Null<T> {
  91. if (o == null)
  92. return null;
  93. var o2:Dynamic = {};
  94. for (f in Reflect.fields(o))
  95. Reflect.setField(o2, f, Reflect.field(o, f));
  96. return o2;
  97. }
  98. public static function makeVarArgs<T>(f:Array<Dynamic>->T):Dynamic {
  99. return function() {
  100. var a = untyped Array.prototype.slice.call(js.Syntax.code("arguments"));
  101. return f(a);
  102. };
  103. }
  104. }