Reflect.hx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. import python.internal.AnonObject;
  23. import python.internal.StringImpl;
  24. import python.internal.ArrayImpl;
  25. import python.lib.Builtin;
  26. import python.lib.Inspect;
  27. import python.Syntax;
  28. import python.VarArgs;
  29. @:access(python.Boot)
  30. @:coreApi
  31. class Reflect {
  32. static inline function handleKeywords(name:String):String {
  33. return python.Boot.handleKeywords(name);
  34. }
  35. static function unhandleKeywords(name:String):String {
  36. return python.Boot.unhandleKeywords(name);
  37. }
  38. public static function hasField( o : Dynamic, field : String ) : Bool
  39. {
  40. var field = handleKeywords(field);
  41. return Builtin.hasattr(o, field);
  42. }
  43. static inline function isString (o:Dynamic):Bool {
  44. return Builtin.isinstance(o, String);
  45. }
  46. static inline function isArray (o:Dynamic):Bool {
  47. return Builtin.isinstance(o, Array);
  48. }
  49. @:access(python.Boot)
  50. @:keep public static function field( o : Dynamic, field : String ) : Dynamic
  51. {
  52. return python.Boot.field(o, field);
  53. }
  54. @:keep public static function setField( o : Dynamic, field : String, value : Dynamic ) : Void
  55. {
  56. var field = handleKeywords(field);
  57. return Builtin.setattr(o,field,value);
  58. }
  59. public static function getProperty( o : Dynamic, field : String ) : Dynamic
  60. {
  61. var field = handleKeywords(field);
  62. var tmp = null;
  63. if (o == null) {
  64. return null;
  65. } else {
  66. tmp = Reflect.field(o, "get_" + field);
  67. if (tmp != null && Builtin.callable(tmp)) {
  68. return tmp();
  69. } else {
  70. return Reflect.field(o, field);
  71. }
  72. }
  73. }
  74. public static function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void {
  75. var field = handleKeywords(field);
  76. return if (Builtin.hasattr(o,"set_"+field)) {
  77. var tmp = Builtin.getattr(o,"set_"+field);
  78. tmp(value);
  79. }
  80. else Builtin.setattr(o,field, value);
  81. }
  82. public static function callMethod( o : Dynamic, func : Dynamic, args : Array<Dynamic> ) : Dynamic
  83. {
  84. var args:VarArgs = args;
  85. return if (Builtin.callable(func)) func(python.Syntax.varArgs(args)) else null;
  86. }
  87. public static inline function fields( o : Dynamic ) : Array<String>
  88. {
  89. return python.Boot.fields(o);
  90. }
  91. public static function isFunction( f : Dynamic ) : Bool
  92. {
  93. return Inspect.isfunction(f) || Inspect.ismethod(f);
  94. }
  95. public static function compare<T>( a : T, b : T ) : Int {
  96. if (a == null && b == null) return 0;
  97. return
  98. if (a == null) 1 else if (b == null) -1
  99. else ( a == b ) ? 0 : (((cast a) > (cast b)) ? 1 : -1);
  100. }
  101. public static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool {
  102. if( f1 == f2 )
  103. return true;
  104. if( !isFunction(f1) || !isFunction(f2) )
  105. return false;
  106. return false;
  107. }
  108. public static function isObject( v : Dynamic ) : Bool {
  109. return switch (Type.typeof(v)) {
  110. case TObject, TClass(_): true;
  111. case _ : false;
  112. }
  113. }
  114. public static function isEnumValue( v : Dynamic ) : Bool {
  115. return v != Enum && Builtin.isinstance(v, cast Enum);
  116. }
  117. public static function deleteField( o : Dynamic, field : String ) : Bool {
  118. if( !hasField(o,field) ) return false;
  119. Syntax.callField(o, "__delattr__", field);
  120. return true;
  121. }
  122. public static function copy<T>( o : T ) : T {
  123. var o2 : Dynamic = {};
  124. for( f in Reflect.fields(o) )
  125. Reflect.setField(o2,f,Reflect.field(o,f));
  126. return o2;
  127. }
  128. @:overload(function( f : Array<Dynamic> -> Void ) : Dynamic {})
  129. public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic {
  130. return function (v:VarArgs) {
  131. return f(v);
  132. }
  133. }
  134. }