Reflect.hx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 function getProperty( o : Dynamic, field : String ) : Dynamic untyped {
  37. try {
  38. return o["get_" + field]();
  39. } catch( e : Dynamic ) {
  40. return o[field];
  41. } catch( e : Dynamic ) {
  42. return null;
  43. }
  44. }
  45. public static function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
  46. try {
  47. o["set_" + field](value);
  48. } catch( e : Dynamic ) {
  49. o[field] = value;
  50. }
  51. }
  52. public inline static function callMethod( o : Dynamic, func : Dynamic, args : Array<Dynamic> ) : Dynamic untyped {
  53. return func.apply(o,args);
  54. }
  55. public static function fields( o : Dynamic ) : Array<String> untyped {
  56. if( o == null ) return new Array();
  57. #if as3
  58. var a : Array<String> = __keys__(o);
  59. var i = 0;
  60. while( i < a.length ){
  61. if( !o.hasOwnProperty(a[i]) )
  62. a.splice(i,1);
  63. else
  64. ++i;
  65. }
  66. #else
  67. var i = 0;
  68. var a = [];
  69. while( untyped __has_next__(o,i) ) {
  70. var prop = untyped __forin__(o,i);
  71. if( o.hasOwnProperty(prop) )
  72. a.push(prop);
  73. }
  74. #end
  75. return a;
  76. }
  77. public static function isFunction( f : Dynamic ) : Bool untyped {
  78. return __typeof__(f) == "function";
  79. }
  80. public static function compare<T>( a : T, b : T ) : Int {
  81. var a : Dynamic = a;
  82. var b : Dynamic = b;
  83. return ( a == b ) ? 0 : ((a > b) ? 1 : -1);
  84. }
  85. public static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool {
  86. return f1 == f2; // VM-level closures
  87. }
  88. public static function isObject( v : Dynamic ) : Bool untyped {
  89. if( v == null )
  90. return false;
  91. var t = __typeof__(v);
  92. if( t == "object" ) {
  93. try {
  94. if( v.__enum__ == true )
  95. return false;
  96. } catch( e : Dynamic ) {
  97. }
  98. return true;
  99. }
  100. return (t == "string");
  101. }
  102. public static function deleteField( o : Dynamic, f : String ) : Bool untyped {
  103. if( o.hasOwnProperty(f) != true ) return false;
  104. __delete__(o,f);
  105. return true;
  106. }
  107. public static function copy<T>( o : T ) : T {
  108. var o2 : Dynamic = {};
  109. for( f in Reflect.fields(o) )
  110. Reflect.setField(o2,f,Reflect.field(o,f));
  111. return o2;
  112. }
  113. public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic {
  114. return function(__arguments__) { return f(__arguments__); };
  115. }
  116. }