Reflect.hx 4.6 KB

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