Reflect.hx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. @:core_api class Reflect {
  26. public inline static function hasField( o : Dynamic, field : String ) : Bool untyped {
  27. return __this__["hasOwnProperty"]["call"](o,field);
  28. }
  29. public inline static function field( o : Dynamic, field : String ) : Dynamic untyped {
  30. return o[field];
  31. }
  32. public inline static function setField( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
  33. o[field] = value;
  34. }
  35. static function findAccessor( c : Class<Dynamic>, name : String ):Dynamic untyped {
  36. do {
  37. var getter = c.__properties__[name];
  38. if (getter != null)
  39. return getter;
  40. c = c.__super__;
  41. } while (c != null);
  42. return null;
  43. }
  44. public static function getProperty( o : Dynamic, field : String ) : Dynamic untyped {
  45. var getter = findAccessor( Std.is(o,Class) ? o : o.__class__, "get_" +field);
  46. return if (getter != null)
  47. o[getter]["apply"](o, [field]);
  48. else
  49. Reflect.field(o, field);
  50. }
  51. public static inline function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
  52. var setter = findAccessor( Std.is(o,Class) ? o : o.__class__, "set_" +field);
  53. return if (setter != null)
  54. o[setter]["apply"](o, [value]);
  55. else
  56. Reflect.setField(o, field, value);
  57. }
  58. public inline static function callMethod( o : Dynamic, func : Dynamic, args : Array<Dynamic> ) : Dynamic untyped {
  59. return func["apply"](o,args);
  60. }
  61. public static function fields( o : Dynamic ) : Array<String> untyped {
  62. if( o == null ) return new Array();
  63. var a : Array<String> = __keys__(o);
  64. var i = 0;
  65. while( i < a.length ) {
  66. if( !a["hasOwnProperty"]["call"](o,a[i]) )
  67. a.splice(i,1);
  68. else
  69. ++i;
  70. }
  71. return a;
  72. }
  73. public static function isFunction( f : Dynamic ) : Bool untyped {
  74. return __typeof__(f) == "function" && f.__name__ == null;
  75. }
  76. public static function compare<T>( a : T, b : T ) : Int {
  77. return ( a == b ) ? 0 : (((cast a) > (cast b)) ? 1 : -1);
  78. }
  79. public static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool {
  80. return untyped f1 == f2 || (f1["f"] == f2["f"] && f1["o"] == f2["o"] && f1["f"] != null);
  81. }
  82. public static function isObject( v : Dynamic ) : Bool untyped {
  83. var t = __typeof__(v);
  84. return (t == "string" || (t == "object" && !v.__enum__) || (t == "function" && v.__name__ != null));
  85. }
  86. public static function deleteField( o : Dynamic, f : String ) : Bool untyped {
  87. if( __this__["hasOwnProperty"]["call"](o,f) != true ) return false;
  88. __delete__(o,f);
  89. return true;
  90. }
  91. public static function copy<T>( o : T ) : T {
  92. var o2 : Dynamic = {};
  93. for( f in Reflect.fields(o) )
  94. Reflect.setField(o2,f,Reflect.field(o,f));
  95. return o2;
  96. }
  97. public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic {
  98. return function() { return f(untyped __arguments__); };
  99. }
  100. }