Reflect.hx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. return Inspect.isfunction(f) || Inspect.ismethod(f) || Boot.hasField(f, "func_code");
  75. }
  76. public static function compare<T>(a:T, b:T):Int {
  77. if (a == null && b == null)
  78. return 0;
  79. return if (a == null) 1 else if (b == null) -1 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))
  108. return false;
  109. Syntax.callField(o, "__delattr__", field);
  110. return true;
  111. }
  112. public static function copy<T>(o:Null<T>):Null<T> {
  113. if (o == null)
  114. return null;
  115. var o2:Dynamic = {};
  116. for (f in Reflect.fields(o))
  117. Reflect.setField(o2, f, Reflect.field(o, f));
  118. return o2;
  119. }
  120. @:overload(function(f:Array<Dynamic>->Void):Dynamic {})
  121. public static function makeVarArgs(f:Array<Dynamic>->Dynamic):Dynamic {
  122. return function(v:VarArgs<Dynamic>) {
  123. return f(v);
  124. }
  125. }
  126. }