Reflect.hx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C)2005-2012 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 inline static function hasField( o : Dynamic, field : String ) : Bool untyped {
  24. return __this__["hasOwnProperty"]["call"](o,field);
  25. }
  26. public inline static function field( o : Dynamic, field : String ) : Dynamic untyped {
  27. return o[field];
  28. }
  29. public inline static function setField( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
  30. o[field] = value;
  31. }
  32. static function findAccessor( c : Class<Dynamic>, name : String ):Dynamic untyped {
  33. do {
  34. var getter = c.__properties__[name];
  35. if (getter != null)
  36. return getter;
  37. c = c.__super__;
  38. } while (c != null);
  39. return null;
  40. }
  41. public static function getProperty( o : Dynamic, field : String ) : Dynamic untyped {
  42. var getter = findAccessor( Std.is(o,Class) ? o : o.__class__, "get_" +field);
  43. return if (getter != null)
  44. o[getter]["apply"](o, [field]);
  45. else
  46. Reflect.field(o, field);
  47. }
  48. public static inline function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
  49. var setter = findAccessor( Std.is(o,Class) ? o : o.__class__, "set_" +field);
  50. return if (setter != null)
  51. o[setter]["apply"](o, [value]);
  52. else
  53. Reflect.setField(o, field, value);
  54. }
  55. public inline static function callMethod( o : Dynamic, func : Dynamic, args : Array<Dynamic> ) : Dynamic untyped {
  56. return func["apply"](o,args);
  57. }
  58. public static function fields( o : Dynamic ) : Array<String> untyped {
  59. if( o == null ) return new Array();
  60. var a : Array<String> = __keys__(o);
  61. var i = 0;
  62. while( i < a.length ) {
  63. if( !a["hasOwnProperty"]["call"](o,a[i]) )
  64. a.splice(i,1);
  65. else
  66. ++i;
  67. }
  68. return a;
  69. }
  70. public static function isFunction( f : Dynamic ) : Bool untyped {
  71. return __typeof__(f) == "function" && f.__name__ == null;
  72. }
  73. public static function compare<T>( a : T, b : T ) : Int {
  74. return ( a == b ) ? 0 : (((cast a) > (cast b)) ? 1 : -1);
  75. }
  76. public static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool {
  77. return untyped f1 == f2 || (f1["f"] == f2["f"] && f1["o"] == f2["o"] && f1["f"] != null);
  78. }
  79. public static function isObject( v : Dynamic ) : Bool untyped {
  80. var t = __typeof__(v);
  81. return (t == "string" || (t == "object" && !v.__enum__) || (t == "function" && v.__name__ != null));
  82. }
  83. public static function deleteField( o : Dynamic, f : String ) : Bool untyped {
  84. if( __this__["hasOwnProperty"]["call"](o,f) != true ) return false;
  85. __delete__(o,f);
  86. return true;
  87. }
  88. public static function copy<T>( o : T ) : T {
  89. var o2 : Dynamic = {};
  90. for( f in Reflect.fields(o) )
  91. Reflect.setField(o2,f,Reflect.field(o,f));
  92. return o2;
  93. }
  94. @:overload(function( f : Array<Dynamic> -> Void ) : Dynamic {})
  95. public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic {
  96. return function() { return f(untyped __arguments__); };
  97. }
  98. }