Reflect.hx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (C)2005-2012 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.KeywordHandler;
  23. import python.lib.Builtin;
  24. import python.lib.Inspect;
  25. import python.lib.Types;
  26. @:coreApi
  27. class Reflect {
  28. public static function hasField( o : Dynamic, field : String ) : Bool
  29. {
  30. var field = KeywordHandler.handleKeywords(field);
  31. return Builtin.hasattr(o, field);
  32. }
  33. @:keep public static function field( o : Dynamic, field : String ) : Dynamic
  34. {
  35. if (field == null) return null;
  36. var field = KeywordHandler.handleKeywords(field);
  37. return if (Builtin.hasattr(o, field)) Builtin.getattr(o, field) else null;
  38. }
  39. @:keep public static function setField( o : Dynamic, field : String, value : Dynamic ) : Void untyped
  40. {
  41. var field = KeywordHandler.handleKeywords(field);
  42. return __define_feature__("Reflect.setField",Builtin.setattr(o,field,value));
  43. }
  44. public static function getProperty( o : Dynamic, field : String ) : Dynamic
  45. {
  46. var field = KeywordHandler.handleKeywords(field);
  47. var tmp = null;
  48. if (o == null) {
  49. return null;
  50. } else {
  51. tmp = Reflect.field(o, "get_" + field);
  52. if (tmp != null && Builtin.callable(tmp)) {
  53. return tmp();
  54. } else {
  55. return Reflect.field(o, field);
  56. }
  57. }
  58. }
  59. public static function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void {
  60. var field = KeywordHandler.handleKeywords(field);
  61. return if (Builtin.hasattr(o,"set_"+field)) {
  62. var tmp = Builtin.getattr(o,"set_"+field);
  63. tmp(value);
  64. }
  65. else Builtin.setattr(o,field, untyped __define_feature__("Reflect.setProperty",value));
  66. }
  67. public static function callMethod( o : Dynamic, func : Dynamic, args : Array<Dynamic> ) : Dynamic
  68. {
  69. var args:VarArgs = args;
  70. return if (Builtin.callable(func)) func(untyped __python_varargs__(args)) else null;
  71. }
  72. public static function fields( o : Dynamic ) : Array<String>
  73. {
  74. var a = [];
  75. if (o != null)
  76. {
  77. if (Builtin.hasattr(o, "_hx_fields"))
  78. {
  79. var fields:Array<String> = o._hx_fields;
  80. return fields.copy();
  81. }
  82. if (Builtin.isinstance(o, untyped __python__("_hx_c._hx_AnonObject")))
  83. {
  84. var d:Dict<String, Dynamic> = Builtin.getattr(o, "__dict__");
  85. var keys = d.keys();
  86. var handler = python.internal.KeywordHandler.unhandleKeywords;
  87. untyped __python__("for k in keys:");
  88. untyped __python__(" a.append(handler(k))");
  89. }
  90. else if (Builtin.hasattr(o, "__dict__"))
  91. {
  92. var a = [];
  93. var d:Dict<String, Dynamic> = Builtin.getattr(o, "__dict__");
  94. var keys = untyped d.keys();
  95. untyped __python__("for k in keys:");
  96. untyped __python__(" a.append(k)");
  97. }
  98. }
  99. return a;
  100. }
  101. public static function isFunction( f : Dynamic ) : Bool
  102. {
  103. return Inspect.isfunction(f) || Inspect.ismethod(f);
  104. }
  105. public static function compare<T>( a : T, b : T ) : Int {
  106. if (a == null && b == null) return 0;
  107. return
  108. if (a == null) 1 else if (b == null) -1 else
  109. ( a == b ) ? 0 : (((cast a) > (cast b)) ? 1 : -1);
  110. }
  111. public static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool {
  112. if( f1 == f2 )
  113. return true;
  114. if( !isFunction(f1) || !isFunction(f2) )
  115. return false;
  116. return false;
  117. }
  118. public static function isObject( v : Dynamic ) : Bool {
  119. return switch (Type.typeof(v)) {
  120. case TObject, TClass(_): true;
  121. case _ : false;
  122. }
  123. }
  124. public static function isEnumValue( v : Dynamic ) : Bool {
  125. return v != Enum && Builtin.isinstance(v, untyped Enum);
  126. }
  127. public static function deleteField( o : Dynamic, field : String ) : Bool {
  128. if( !hasField(o,field) ) return false;
  129. untyped __python__("o.__delattr__")(field);
  130. return true;
  131. }
  132. public static function copy<T>( o : T ) : T {
  133. var o2 : Dynamic = {};
  134. for( f in Reflect.fields(o) )
  135. Reflect.setField(o2,f,Reflect.field(o,f));
  136. return o2;
  137. }
  138. @:overload(function( f : Array<Dynamic> -> Void ) : Dynamic {})
  139. public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic {
  140. return throw "not implemented";
  141. }
  142. }