Reflect.hx 4.5 KB

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