Reflect.hx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. @:core_api class Reflect {
  26. public static function hasField( o : Dynamic, field : String ) : Bool untyped {
  27. return $typeof(o) == $tobject && $objfield(o,$hash(field.__s));
  28. }
  29. public inline static function field( o : Dynamic, field : String ) : Dynamic untyped {
  30. return if( $typeof(o) != $tobject ) null else $objget(o,$hash(field.__s));
  31. }
  32. public inline static function setField( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
  33. if( $typeof(o) == $tobject )
  34. $objset(o,$hash(field.__s),value);
  35. }
  36. public static inline function getProperty( o : Dynamic, field : String ) : Dynamic untyped {
  37. var tmp;
  38. return if( $typeof(o) != $tobject ) null else if( o.__properties__ != null && (tmp=$objget(o.__properties__,$hash("get_".__s+field.__s))) != null ) $call($objget(o,$hash(tmp)),o,$array()) else $objget(o,$hash(field.__s));
  39. }
  40. public static inline function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
  41. if( $typeof(o) == $tobject ) {
  42. var tmp;
  43. if( o.__properties__ != null && (tmp=$objget(o.__properties__,$hash("set_".__s+field.__s))) != null ) $call($objget(o,$hash(tmp)),o,$array(value)) else $objset(o,$hash(field.__s),value);
  44. }
  45. }
  46. public inline static function callMethod( o : Dynamic, func : Dynamic, args : Array<Dynamic> ) : Dynamic untyped {
  47. return $call(func,o,args.__neko());
  48. }
  49. public static function fields( o : Dynamic ) : Array<String> untyped {
  50. if( $typeof(o) != $tobject )
  51. return new Array<String>();
  52. else {
  53. var a : neko.NativeArray<Int> = $objfields(o);
  54. var i = 0;
  55. var l = $asize(a);
  56. while( i < l ) {
  57. a[i] = new String($field(a[i]));
  58. i++;
  59. }
  60. return Array.new1(a,l);
  61. }
  62. }
  63. public static function isFunction( f : Dynamic ) : Bool untyped {
  64. return $typeof(f) == $tfunction;
  65. }
  66. public inline static function compare<T>( a : T, b : T ) : Int {
  67. return untyped $compare(a,b);
  68. }
  69. public inline static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool {
  70. return same_closure(f1,f2);
  71. }
  72. public static function isObject( v : Dynamic ) : Bool untyped {
  73. return $typeof(v) == $tobject && v.__enum__ == null;
  74. }
  75. public inline static function deleteField( o : Dynamic, f : String ) : Bool untyped {
  76. return $objremove(o,$hash(f.__s));
  77. }
  78. public inline static function copy<T>( o : T ) : T {
  79. return untyped $new(o);
  80. }
  81. public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic {
  82. return untyped $varargs(function(a) { return f(Array.new1(a,$asize(a))); });
  83. }
  84. #if neko
  85. static var same_closure = try neko.Lib.load("std","same_closure",2) catch( e : Dynamic ) function(f1,f2) return f1 == f2;
  86. #end
  87. }