Reflect.hx 4.1 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 static function hasField( o : Dynamic, field : String ) : Bool {
  24. return untyped __js__('Object').prototype.hasOwnProperty.call(o, field);
  25. }
  26. public inline static function field( o : Dynamic, field : String ) : Dynamic untyped {
  27. var v = null;
  28. try {
  29. v = o[field];
  30. } catch( e : Dynamic ) {
  31. }
  32. return v;
  33. }
  34. public inline static function setField( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
  35. o[field] = value;
  36. }
  37. public static inline function getProperty( o : Dynamic, field : String ) : Dynamic untyped {
  38. var tmp;
  39. return if( o == null ) __define_feature__("Reflect.getProperty",null) else if( o.__properties__ && (tmp=o.__properties__["get_"+field]) ) o[tmp]() else o[field];
  40. }
  41. public static inline function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
  42. var tmp;
  43. if( o.__properties__ && (tmp=o.__properties__["set_"+field]) ) o[tmp](value) else o[field] = __define_feature__("Reflect.setProperty",value);
  44. }
  45. public inline static function callMethod( o : Dynamic, func : Dynamic, args : Array<Dynamic> ) : Dynamic untyped {
  46. return func.apply(o,args);
  47. }
  48. public static function fields( o : Dynamic ) : Array<String> {
  49. var a = [];
  50. if (o != null) untyped {
  51. var hasOwnProperty = __js__('Object').prototype.hasOwnProperty;
  52. __js__("for( var f in o ) {");
  53. if( f != "__id__" && f != "hx__closures__" && hasOwnProperty.call(o, f) ) a.push(f);
  54. __js__("}");
  55. }
  56. return a;
  57. }
  58. public static function isFunction( f : Dynamic ) : Bool untyped {
  59. return __js__("typeof(f)") == "function" && !(js.Boot.isClass(f) || js.Boot.isEnum(f));
  60. }
  61. public static function compare<T>( a : T, b : T ) : Int {
  62. return ( a == b ) ? 0 : (((cast a) > (cast b)) ? 1 : -1);
  63. }
  64. public static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool {
  65. if( f1 == f2 )
  66. return true;
  67. if( !isFunction(f1) || !isFunction(f2) )
  68. return false;
  69. return f1.scope == f2.scope && f1.method == f2.method && f1.method != null;
  70. }
  71. public static function isObject( v : Dynamic ) : Bool untyped {
  72. if( v == null )
  73. return false;
  74. var t = __js__("typeof(v)");
  75. return (t == "string" || (t == "object" && v.__enum__ == null)) || (t == "function" && (js.Boot.isClass(v) || js.Boot.isEnum(v)) != null);
  76. }
  77. public static function isEnumValue( v : Dynamic ) : Bool {
  78. return v != null && v.__enum__ != null;
  79. }
  80. public static function deleteField( o : Dynamic, field : String ) : Bool untyped {
  81. if( !hasField(o,field) ) return false;
  82. __js__("delete")(o[field]);
  83. return true;
  84. }
  85. public static function copy<T>( o : T ) : T {
  86. var o2 : Dynamic = {};
  87. for( f in Reflect.fields(o) )
  88. Reflect.setField(o2,f,Reflect.field(o,f));
  89. return o2;
  90. }
  91. @:overload(function( f : Array<Dynamic> -> Void ) : Dynamic {})
  92. public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic {
  93. return function() {
  94. var a = untyped Array.prototype.slice.call(__js__("arguments"));
  95. return f(a);
  96. };
  97. }
  98. }