Reflect.hx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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.lib.Builtin;
  23. import python.lib.Inspect;
  24. import python.lib.Types;
  25. @:coreApi
  26. class Reflect {
  27. static var keywords:Set<String> = new Set(
  28. [
  29. "and", "del", "from", "not", "while",
  30. "as", "elif", "global", "or", "with",
  31. "assert", "else", "if", "pass", "yield",
  32. "break", "except", "import", "print", "float",
  33. "class", "exec", "in", "raise",
  34. "continue", "finally", "is", "return",
  35. "def", "for", "lambda", "try",
  36. "None", "list"
  37. ]);
  38. static inline function handleKeywords(name:String):String
  39. {
  40. if (keywords.has(name)) {
  41. return "_hx_" + name;
  42. }
  43. return name;
  44. }
  45. static function unhandleKeywords(name:String):String
  46. {
  47. if (name.substr(0,4) == "_hx_") {
  48. var real = name.substr(4);
  49. if (keywords.has(real)) return real;
  50. }
  51. return name;
  52. }
  53. public static function hasField( o : Dynamic, field : String ) : Bool
  54. {
  55. var field = handleKeywords(field);
  56. return Builtin.hasattr(o, field);
  57. }
  58. @:keep public static function field( o : Dynamic, field : String ) : Dynamic
  59. {
  60. if (field == null) return null;
  61. if (Builtin.isinstance(o, String)) {
  62. switch (field) {
  63. case "length": return Builtin.len.bind(o);
  64. case "toLowerCase": return python.internal.StringImpl.toLowerCase.bind(o);
  65. case "toUpperCase": return python.internal.StringImpl.toUpperCase.bind(o);
  66. }
  67. } else if (Builtin.isinstance(o, Array)) {
  68. switch (field) {
  69. case "map": return python.internal.ArrayImpl.map.bind(o);
  70. case "filter": return python.internal.ArrayImpl.filter.bind(o);
  71. case "length": return python.internal.ArrayImpl.get_length(o);
  72. }
  73. }
  74. var field = handleKeywords(field);
  75. return if (Builtin.hasattr(o, field)) Builtin.getattr(o, field) else null;
  76. }
  77. @:keep public static function setField( o : Dynamic, field : String, value : Dynamic ) : Void untyped
  78. {
  79. var field = handleKeywords(field);
  80. return __define_feature__("Reflect.setField",Builtin.setattr(o,field,value));
  81. }
  82. public static function getProperty( o : Dynamic, field : String ) : Dynamic
  83. {
  84. var field = handleKeywords(field);
  85. var tmp = null;
  86. if (o == null) {
  87. return null;
  88. } else {
  89. tmp = Reflect.field(o, "get_" + field);
  90. if (tmp != null && Builtin.callable(tmp)) {
  91. return tmp();
  92. } else {
  93. return Reflect.field(o, field);
  94. }
  95. }
  96. }
  97. public static function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void {
  98. var field = handleKeywords(field);
  99. return if (Builtin.hasattr(o,"set_"+field)) {
  100. var tmp = Builtin.getattr(o,"set_"+field);
  101. tmp(value);
  102. }
  103. else Builtin.setattr(o,field, untyped __define_feature__("Reflect.setProperty",value));
  104. }
  105. public static function callMethod( o : Dynamic, func : Dynamic, args : Array<Dynamic> ) : Dynamic
  106. {
  107. var args:VarArgs = args;
  108. return if (Builtin.callable(func)) func(untyped __python_varargs__(args)) else null;
  109. }
  110. public static function fields( o : Dynamic ) : Array<String>
  111. {
  112. var a = [];
  113. if (o != null)
  114. {
  115. if (Builtin.hasattr(o, "_hx_fields"))
  116. {
  117. var fields:Array<String> = o._hx_fields;
  118. return fields.copy();
  119. }
  120. if (Builtin.isinstance(o, untyped __python__("_hx_c._hx_AnonObject")))
  121. {
  122. var d:Dict<String, Dynamic> = Builtin.getattr(o, "__dict__");
  123. var keys = d.keys();
  124. var handler = unhandleKeywords;
  125. untyped __python__("for k in keys:");
  126. untyped __python__(" a.append(handler(k))");
  127. }
  128. else if (Builtin.hasattr(o, "__dict__"))
  129. {
  130. var a = [];
  131. var d:Dict<String, Dynamic> = Builtin.getattr(o, "__dict__");
  132. var keys = untyped d.keys();
  133. untyped __python__("for k in keys:");
  134. untyped __python__(" a.append(k)");
  135. }
  136. }
  137. return a;
  138. }
  139. public static function isFunction( f : Dynamic ) : Bool
  140. {
  141. return Inspect.isfunction(f) || Inspect.ismethod(f);
  142. }
  143. public static function compare<T>( a : T, b : T ) : Int {
  144. if (a == null && b == null) return 0;
  145. return
  146. if (a == null) 1 else if (b == null) -1 else
  147. ( a == b ) ? 0 : (((cast a) > (cast b)) ? 1 : -1);
  148. }
  149. public static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool {
  150. if( f1 == f2 )
  151. return true;
  152. if( !isFunction(f1) || !isFunction(f2) )
  153. return false;
  154. return false;
  155. }
  156. public static function isObject( v : Dynamic ) : Bool {
  157. return switch (Type.typeof(v)) {
  158. case TObject, TClass(_): true;
  159. case _ : false;
  160. }
  161. }
  162. public static function isEnumValue( v : Dynamic ) : Bool {
  163. return v != Enum && Builtin.isinstance(v, untyped Enum);
  164. }
  165. public static function deleteField( o : Dynamic, field : String ) : Bool {
  166. if( !hasField(o,field) ) return false;
  167. untyped __python__("o.__delattr__")(field);
  168. return true;
  169. }
  170. public static function copy<T>( o : T ) : T {
  171. var o2 : Dynamic = {};
  172. for( f in Reflect.fields(o) )
  173. Reflect.setField(o2,f,Reflect.field(o,f));
  174. return o2;
  175. }
  176. @:overload(function( f : Array<Dynamic> -> Void ) : Dynamic {})
  177. public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic {
  178. return throw "not implemented";
  179. }
  180. }