Reflect.hx 3.6 KB

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