Reflect.hx 8.0 KB

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