Reflect.hx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. if( o.hasOwnProperty != null )
  28. return o.hasOwnProperty(field);
  29. var arr = fields(o);
  30. for( t in arr.iterator() )
  31. if( t == field ) return true;
  32. return false;
  33. }
  34. public inline static function field( o : Dynamic, field : String ) : Dynamic untyped {
  35. var v = null;
  36. try {
  37. v = o[field];
  38. } catch( e : Dynamic ) {
  39. }
  40. return v;
  41. }
  42. public inline static function setField( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
  43. o[field] = value;
  44. }
  45. public static inline function getProperty( o : Dynamic, field : String ) : Dynamic untyped {
  46. var tmp;
  47. return if( o == null ) null else if( o.__properties__ && (tmp=o.__properties__["get_"+field]) ) o[tmp]() else o[field];
  48. }
  49. public static inline function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
  50. var tmp;
  51. if( o.__properties__ && (tmp=o.__properties__["set_"+field]) ) o[tmp](value) else o[field] = value;
  52. }
  53. public inline static function callMethod( o : Dynamic, func : Dynamic, args : Array<Dynamic> ) : Dynamic untyped {
  54. return func.apply(o,args);
  55. }
  56. public static function fields( o : Dynamic ) : Array<String> untyped {
  57. if( o == null ) return new Array();
  58. var a = new Array();
  59. if( o.hasOwnProperty ) {
  60. __js__("for(var i in o) if( o.hasOwnProperty(i) ) a.push(i)");
  61. } else {
  62. var t;
  63. try{ t = o.__proto__; } catch( e : Dynamic ) { t = null; }
  64. if( t != null )
  65. o.__proto__ = null;
  66. __js__("for(var i in o) if( i != \"__proto__\" ) a.push(i)");
  67. if( t != null )
  68. o.__proto__ = t;
  69. }
  70. return a;
  71. }
  72. public static function isFunction( f : Dynamic ) : Bool untyped {
  73. return __js__("typeof(f)") == "function" && f.__name__ == null;
  74. }
  75. public static function compare<T>( a : T, b : T ) : Int {
  76. return ( a == b ) ? 0 : (((cast a) > (cast b)) ? 1 : -1);
  77. }
  78. public static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool {
  79. if( f1 == f2 )
  80. return true;
  81. if( !isFunction(f1) || !isFunction(f2) )
  82. return false;
  83. return f1.scope == f2.scope && f1.method == f2.method && f1.method != null;
  84. }
  85. public static function isObject( v : Dynamic ) : Bool untyped {
  86. if( v == null )
  87. return false;
  88. var t = __js__("typeof(v)");
  89. return (t == "string" || (t == "object" && !v.__enum__) || (t == "function" && v.__name__ != null));
  90. }
  91. public static function deleteField( o : Dynamic, f : String ) : Bool untyped {
  92. if( !hasField(o,f) ) return false;
  93. __js__("delete")(o[f]);
  94. return true;
  95. }
  96. public static function copy<T>( o : T ) : T {
  97. var o2 : Dynamic = {};
  98. for( f in Reflect.fields(o) )
  99. Reflect.setField(o2,f,Reflect.field(o,f));
  100. return o2;
  101. }
  102. public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic {
  103. return function() untyped {
  104. var a = new Array();
  105. for( i in 0...arguments.length )
  106. a.push(arguments[i]);
  107. return f(a);
  108. };
  109. }
  110. }