Reflect.hx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 static function callMethod( o : Dynamic, func : haxe.Constraints.Function, args : Array<Dynamic> ) : Dynamic untyped {
  44. var a = args.__neko();
  45. // pad missing args with null's
  46. var n = $nargs(func);
  47. if( n > $asize(a) ) {
  48. var a2 = $amake(n);
  49. $ablit(a2,0,a,0,$asize(a));
  50. a = a2;
  51. }
  52. return $call(func,o,a);
  53. }
  54. public static function fields( o : Dynamic ) : Array<String> untyped {
  55. if( $typeof(o) != $tobject )
  56. return new Array<String>();
  57. else {
  58. var a : neko.NativeArray<Int> = $objfields(o);
  59. var i = 0;
  60. var hasid = false;
  61. var l = $asize(a);
  62. while( i < l ) {
  63. var fid = a[i];
  64. if( fid == -190054693 ) hasid = true; // $hash("__id__")
  65. a[i] = new String($field(fid));
  66. i++;
  67. }
  68. var a : Array<String> = Array.new1(a, l);
  69. if( hasid ) a.remove("__id__");
  70. return a;
  71. }
  72. }
  73. public static function isFunction( f : Dynamic ) : Bool untyped {
  74. return $typeof(f) == $tfunction;
  75. }
  76. public inline static function compare<T>( a : T, b : T ) : Int {
  77. return untyped $compare(a,b);
  78. }
  79. public inline static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool {
  80. return same_closure(f1,f2);
  81. }
  82. public static function isObject( v : Dynamic ) : Bool untyped {
  83. return $typeof(v) == $tobject && v.__enum__ == null;
  84. }
  85. public static function isEnumValue( v : Dynamic ) : Bool untyped {
  86. return $typeof(v) == $tobject && v.__enum__ != null;
  87. }
  88. public inline static function deleteField( o : Dynamic, field : String ) : Bool untyped {
  89. return $objremove(o,$fasthash(field.__s));
  90. }
  91. public inline static function copy<T>( o : T ) : T {
  92. return untyped $new(o);
  93. }
  94. @:overload(function( f : Array<Dynamic> -> Void ) : Dynamic {})
  95. public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic {
  96. return untyped $varargs(function(a) { return f(Array.new1(a,$asize(a))); });
  97. }
  98. static var same_closure = try neko.Lib.load("std","same_closure",2) catch( e : Dynamic ) function(f1,f2) return f1 == f2;
  99. }