Reflect.hx 4.5 KB

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