Reflect.hx 3.9 KB

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