Reflect.hx 4.2 KB

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