Reflect.hx 4.6 KB

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