Reflect.hx 3.9 KB

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