Reflect.hx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 untyped {
  24. return $typeof(o) == $tobject && $objfield(o,$fasthash(field.__s));
  25. }
  26. public inline static function field( o : Dynamic, field : String ) : Dynamic untyped {
  27. return if( $typeof(o) != $tobject ) null else $objget(o,$fasthash(field.__s));
  28. }
  29. public inline static function setField( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
  30. if( $typeof(o) == $tobject )
  31. $objset(o,$hash(field.__s),value);
  32. }
  33. public static inline function getProperty( o : Dynamic, field : String ) : Dynamic untyped {
  34. var tmp;
  35. return if( $typeof(o) != $tobject ) null else if( o.__properties__ != null && (tmp=$objget(o.__properties__,$fasthash("get_".__s+field.__s))) != null ) $call($objget(o,$fasthash(tmp)),o,$array()) else $objget(o,$fasthash(field.__s));
  36. }
  37. public static inline function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
  38. if( $typeof(o) == $tobject ) {
  39. var tmp;
  40. if( o.__properties__ != null && (tmp=$objget(o.__properties__,$fasthash("set_".__s+field.__s))) != null ) $call($objget(o,$fasthash(tmp)),o,$array(value)) else $objset(o,$hash(field.__s),value);
  41. }
  42. }
  43. public inline static function callMethod( o : Dynamic, func : Dynamic, args : Array<Dynamic> ) : Dynamic untyped {
  44. return $call(func,o,args.__neko());
  45. }
  46. public static function fields( o : Dynamic ) : Array<String> untyped {
  47. if( $typeof(o) != $tobject )
  48. return new Array<String>();
  49. else {
  50. var a : neko.NativeArray<Int> = $objfields(o);
  51. var i = 0;
  52. var hasid = false;
  53. var l = $asize(a);
  54. while( i < l ) {
  55. var fid = a[i];
  56. if( fid == -190054693 ) hasid = true; // $hash("__id__")
  57. a[i] = new String($field(fid));
  58. i++;
  59. }
  60. var a : Array<String> = Array.new1(a, l);
  61. if( hasid ) a.remove("__id__");
  62. return a;
  63. }
  64. }
  65. public static function isFunction( f : Dynamic ) : Bool untyped {
  66. return $typeof(f) == $tfunction;
  67. }
  68. public inline static function compare<T>( a : T, b : T ) : Int {
  69. return untyped $compare(a,b);
  70. }
  71. public inline static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool {
  72. return same_closure(f1,f2);
  73. }
  74. public static function isObject( v : Dynamic ) : Bool untyped {
  75. return $typeof(v) == $tobject && v.__enum__ == null;
  76. }
  77. public static function isEnumValue( v : Dynamic ) : Bool untyped {
  78. return $typeof(v) == $tobject && v.__enum__ != null;
  79. }
  80. public inline static function deleteField( o : Dynamic, field : String ) : Bool untyped {
  81. return $objremove(o,$fasthash(field.__s));
  82. }
  83. public inline static function copy<T>( o : T ) : T {
  84. return untyped $new(o);
  85. }
  86. @:overload(function( f : Array<Dynamic> -> Void ) : Dynamic {})
  87. public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic {
  88. return untyped $varargs(function(a) { return f(Array.new1(a,$asize(a))); });
  89. }
  90. static var same_closure = try neko.Lib.load("std","same_closure",2) catch( e : Dynamic ) function(f1,f2) return f1 == f2;
  91. }