Reflect.hx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.StringImpl;
  23. import python.internal.ArrayImpl;
  24. import python.lib.Builtin;
  25. import python.lib.Inspect;
  26. import python.lib.Types;
  27. @:access(python.Boot)
  28. @:coreApi
  29. class Reflect {
  30. static inline function handleKeywords(name:String):String {
  31. return python.Boot.handleKeywords(name);
  32. }
  33. static function unhandleKeywords(name:String):String {
  34. return python.Boot.unhandleKeywords(name);
  35. }
  36. public static function hasField( o : Dynamic, field : String ) : Bool
  37. {
  38. var field = handleKeywords(field);
  39. return Builtin.hasattr(o, field);
  40. }
  41. static inline function isString (o:Dynamic):Bool {
  42. return Builtin.isinstance(o, String);
  43. }
  44. static inline function isArray (o:Dynamic):Bool {
  45. return Builtin.isinstance(o, Array);
  46. }
  47. @:access(python.Boot)
  48. @:keep public static function field( o : Dynamic, field : String ) : Dynamic
  49. {
  50. return python.Boot.field(o, field);
  51. /*
  52. if (field == null) return null;
  53. switch (field) {
  54. case "length" if (isString(o)): return StringImpl.get_length(o);
  55. case "length" if (isArray(o)): return ArrayImpl.get_length(o);
  56. case "toLowerCase" if (isString(o)): return StringImpl.toLowerCase.bind(o);
  57. case "toUpperCase" if (isString(o)): return StringImpl.toUpperCase.bind(o);
  58. case "charAt" if (isString(o)): return StringImpl.charAt.bind(o);
  59. case "charCodeAt" if (isString(o)): return StringImpl.charCodeAt.bind(o);
  60. case "indexOf" if (isString(o)): return StringImpl.indexOf.bind(o);
  61. case "lastIndexOf" if (isString(o)): return StringImpl.lastIndexOf.bind(o);
  62. case "split" if (isString(o)): return StringImpl.split.bind(o);
  63. case "substr" if (isString(o)): return StringImpl.substr.bind(o);
  64. case "substring" if (isString(o)): return StringImpl.substring.bind(o);
  65. case "toString" if (isString(o)): return StringImpl.toString.bind(o);
  66. case "map" if (isArray(o)): return ArrayImpl.map.bind(o);
  67. case "filter" if (isArray(o)): return ArrayImpl.filter.bind(o);
  68. case "concat" if (isArray(o)): return ArrayImpl.concat.bind(o);
  69. case "copy" if (isArray(o)): return ArrayImpl.copy.bind(o);
  70. case "iterator" if (isArray(o)): return ArrayImpl.iterator.bind(o);
  71. case "insert" if (isArray(o)): return ArrayImpl.insert.bind(o);
  72. case "join" if (isArray(o)): return ArrayImpl.join.bind(o);
  73. case "toString" if (isArray(o)): return ArrayImpl.toString.bind(o);
  74. case "pop" if (isArray(o)): return ArrayImpl.pop.bind(o);
  75. case "push" if (isArray(o)): return ArrayImpl.push.bind(o);
  76. case "unshift" if (isArray(o)): return ArrayImpl.unshift.bind(o);
  77. case "indexOf" if (isArray(o)): return ArrayImpl.indexOf.bind(o);
  78. case "lastIndexOf" if (isArray(o)): return ArrayImpl.lastIndexOf.bind(o);
  79. case "remove" if (isArray(o)): return ArrayImpl.remove.bind(o);
  80. case "reverse" if (isArray(o)): return ArrayImpl.reverse.bind(o);
  81. case "shift" if (isArray(o)): return ArrayImpl.shift.bind(o);
  82. case "slice" if (isArray(o)): return ArrayImpl.slice.bind(o);
  83. case "sort" if (isArray(o)): return ArrayImpl.sort.bind(o);
  84. case "splice" if (isArray(o)): return ArrayImpl.splice.bind(o);
  85. }
  86. var field = handleKeywords(field);
  87. return if (Builtin.hasattr(o, field)) Builtin.getattr(o, field) else null;
  88. */
  89. }
  90. @:keep public static function setField( o : Dynamic, field : String, value : Dynamic ) : Void untyped
  91. {
  92. var field = handleKeywords(field);
  93. return __define_feature__("Reflect.setField",Builtin.setattr(o,field,value));
  94. }
  95. public static function getProperty( o : Dynamic, field : String ) : Dynamic
  96. {
  97. var field = handleKeywords(field);
  98. var tmp = null;
  99. if (o == null) {
  100. return null;
  101. } else {
  102. tmp = Reflect.field(o, "get_" + field);
  103. if (tmp != null && Builtin.callable(tmp)) {
  104. return tmp();
  105. } else {
  106. return Reflect.field(o, field);
  107. }
  108. }
  109. }
  110. public static function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void {
  111. var field = handleKeywords(field);
  112. return if (Builtin.hasattr(o,"set_"+field)) {
  113. var tmp = Builtin.getattr(o,"set_"+field);
  114. tmp(value);
  115. }
  116. else Builtin.setattr(o,field, untyped __define_feature__("Reflect.setProperty",value));
  117. }
  118. public static function callMethod( o : Dynamic, func : Dynamic, args : Array<Dynamic> ) : Dynamic
  119. {
  120. var args:VarArgs = args;
  121. return if (Builtin.callable(func)) func(untyped __python_varargs__(args)) else null;
  122. }
  123. public static function fields( o : Dynamic ) : Array<String>
  124. {
  125. var a = [];
  126. if (o != null)
  127. {
  128. if (Builtin.hasattr(o, "_hx_fields"))
  129. {
  130. var fields:Array<String> = o._hx_fields;
  131. return fields.copy();
  132. }
  133. if (Builtin.isinstance(o, untyped __python__("_hx_c._hx_AnonObject")))
  134. {
  135. var d:Dict<String, Dynamic> = Builtin.getattr(o, "__dict__");
  136. var keys = d.keys();
  137. var handler = unhandleKeywords;
  138. untyped __python__("for k in keys:");
  139. untyped __python__(" a.append(handler(k))");
  140. }
  141. else if (Builtin.hasattr(o, "__dict__"))
  142. {
  143. var a = [];
  144. var d:Dict<String, Dynamic> = Builtin.getattr(o, "__dict__");
  145. var keys = untyped d.keys();
  146. untyped __python__("for k in keys:");
  147. untyped __python__(" a.append(k)");
  148. }
  149. }
  150. return a;
  151. }
  152. public static function isFunction( f : Dynamic ) : Bool
  153. {
  154. return Inspect.isfunction(f) || Inspect.ismethod(f);
  155. }
  156. public static function compare<T>( a : T, b : T ) : Int {
  157. if (a == null && b == null) return 0;
  158. return
  159. if (a == null) 1 else if (b == null) -1 else
  160. ( a == b ) ? 0 : (((cast a) > (cast b)) ? 1 : -1);
  161. }
  162. public static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool {
  163. if( f1 == f2 )
  164. return true;
  165. if( !isFunction(f1) || !isFunction(f2) )
  166. return false;
  167. return false;
  168. }
  169. public static function isObject( v : Dynamic ) : Bool {
  170. return switch (Type.typeof(v)) {
  171. case TObject, TClass(_): true;
  172. case _ : false;
  173. }
  174. }
  175. public static function isEnumValue( v : Dynamic ) : Bool {
  176. return v != Enum && Builtin.isinstance(v, untyped Enum);
  177. }
  178. public static function deleteField( o : Dynamic, field : String ) : Bool {
  179. if( !hasField(o,field) ) return false;
  180. untyped __python__("o.__delattr__")(field);
  181. return true;
  182. }
  183. public static function copy<T>( o : T ) : T {
  184. var o2 : Dynamic = {};
  185. for( f in Reflect.fields(o) )
  186. Reflect.setField(o2,f,Reflect.field(o,f));
  187. return o2;
  188. }
  189. @:overload(function( f : Array<Dynamic> -> Void ) : Dynamic {})
  190. public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic {
  191. return throw "not implemented";
  192. }
  193. }