Reflect.hx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 java.internal.Function;
  23. import java.Boot;
  24. /*
  25. * Copyright (c) 2005, The haXe Project Contributors
  26. * All rights reserved.
  27. * Redistribution and use in source and binary forms, with or without
  28. * modification, are permitted provided that the following conditions are met:
  29. *
  30. * - Redistributions of source code must retain the above copyright
  31. * notice, this list of conditions and the following disclaimer.
  32. * - Redistributions in binary form must reproduce the above copyright
  33. * notice, this list of conditions and the following disclaimer in the
  34. * documentation and/or other materials provided with the distribution.
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  37. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  38. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  40. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  41. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  42. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  43. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  44. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  45. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  46. * DAMAGE.
  47. */
  48. /**
  49. The Reflect API is a way to manipulate values dynamicly through an
  50. abstract interface in an untyped manner. Use with care.
  51. **/
  52. @:keep @:coreApi class Reflect {
  53. /**
  54. Tells if an object has a field set. This doesn't take into account the object prototype (class methods).
  55. **/
  56. @:functionCode('
  57. if (o instanceof haxe.lang.IHxObject)
  58. return ((haxe.lang.IHxObject) o).__hx_getField(field, false, true, false) != haxe.lang.Runtime.undefined;
  59. return haxe.lang.Runtime.slowHasField(o, field);
  60. ')
  61. public static function hasField( o : Dynamic, field : String ) : Bool
  62. {
  63. return false;
  64. }
  65. /**
  66. Returns the field of an object, or null if [o] is not an object or doesn't have this field.
  67. **/
  68. @:functionCode('
  69. if (o instanceof haxe.lang.IHxObject)
  70. return ((haxe.lang.IHxObject) o).__hx_getField(field, false, false, false);
  71. return haxe.lang.Runtime.slowGetField(o, field, false);
  72. ')
  73. public static function field( o : Dynamic, field : String ) : Dynamic
  74. {
  75. return null;
  76. }
  77. /**
  78. Set an object field value.
  79. **/
  80. @:functionCode('
  81. if (o instanceof haxe.lang.IHxObject)
  82. ((haxe.lang.IHxObject) o).__hx_setField(field, value, false);
  83. else
  84. haxe.lang.Runtime.slowSetField(o, field, value);
  85. ')
  86. public static function setField( o : Dynamic, field : String, value : Dynamic ) : Void
  87. {
  88. }
  89. /**
  90. Similar to field but also supports property (might be slower).
  91. **/
  92. @:functionCode('
  93. if (o instanceof haxe.lang.IHxObject)
  94. return ((haxe.lang.IHxObject) o).__hx_getField(field, false, false, true);
  95. if (haxe.lang.Runtime.slowHasField(o, "get_" + field))
  96. return haxe.lang.Runtime.slowCallField(o, "get_" + field, null);
  97. return haxe.lang.Runtime.slowGetField(o, field, false);
  98. ')
  99. public static function getProperty( o : Dynamic, field : String ) : Dynamic
  100. {
  101. return null;
  102. }
  103. /**
  104. Similar to setField but also supports property (might be slower).
  105. **/
  106. @:functionCode('
  107. if (o instanceof haxe.lang.IHxObject)
  108. ((haxe.lang.IHxObject) o).__hx_setField(field, value, true);
  109. else if (haxe.lang.Runtime.slowHasField(o, "set_" + field))
  110. haxe.lang.Runtime.slowCallField(o, "set_" + field, new Array( new java.lang.Object[]{value} ));
  111. else
  112. haxe.lang.Runtime.slowSetField(o, field, value);
  113. ')
  114. public static function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void
  115. {
  116. }
  117. /**
  118. Call a method with the given object and arguments.
  119. **/
  120. @:functionCode('
  121. return ((haxe.lang.Function) func).__hx_invokeDynamic(args);
  122. ')
  123. public static function callMethod( o : Dynamic, func : Dynamic, args : Array<Dynamic> ) : Dynamic
  124. {
  125. return null;
  126. }
  127. /**
  128. Returns the list of fields of an object, excluding its prototype (class methods).
  129. **/
  130. @:functionCode('
  131. if (o instanceof haxe.lang.IHxObject)
  132. {
  133. Array<String> ret = new Array<String>();
  134. ((haxe.lang.IHxObject) o).__hx_getFields(ret);
  135. return ret;
  136. } else if (o instanceof java.lang.Class) {
  137. return Type.getClassFields( (java.lang.Class) o);
  138. } else {
  139. return new Array<String>();
  140. }
  141. ')
  142. public static function fields( o : Dynamic ) : Array<String>
  143. {
  144. return null;
  145. }
  146. /**
  147. Tells if a value is a function or not.
  148. **/
  149. @:functionCode('
  150. return f instanceof haxe.lang.Function;
  151. ')
  152. public static function isFunction( f : Dynamic ) : Bool
  153. {
  154. return false;
  155. }
  156. /**
  157. Generic comparison function, does not work for methods, see [compareMethods]
  158. **/
  159. @:functionCode('
  160. return haxe.lang.Runtime.compare(a, b);
  161. ')
  162. public static function compare<T>( a : T, b : T ) : Int
  163. {
  164. return 0;
  165. }
  166. /**
  167. Compare two methods closures. Returns true if it's the same method of the same instance.
  168. **/
  169. @:functionCode('
  170. if (f1 == f2)
  171. return true;
  172. if (f1 instanceof haxe.lang.Closure && f2 instanceof haxe.lang.Closure)
  173. {
  174. haxe.lang.Closure f1c = (haxe.lang.Closure) f1;
  175. haxe.lang.Closure f2c = (haxe.lang.Closure) f2;
  176. return haxe.lang.Runtime.refEq(f1c.obj, f2c.obj) && f1c.field.equals(f2c.field);
  177. }
  178. return false;
  179. ')
  180. public static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool
  181. {
  182. return false;
  183. }
  184. /**
  185. Tells if a value is an object or not.
  186. **/
  187. @:functionCode('
  188. return v != null && !(v instanceof haxe.lang.Enum || v instanceof haxe.lang.Function || v instanceof java.lang.Enum || v instanceof java.lang.Number || v instanceof java.lang.Boolean);
  189. ')
  190. public static function isObject( v : Dynamic ) : Bool
  191. {
  192. return false;
  193. }
  194. @:functionCode('
  195. return v != null && (v instanceof haxe.lang.Enum || v instanceof java.lang.Enum);
  196. ')
  197. public static function isEnumValue( v : Dynamic ) : Bool {
  198. return switch(Type.typeof(v)) {
  199. case TEnum(_): true;
  200. case _: false;
  201. }
  202. }
  203. /**
  204. Delete an object field.
  205. **/
  206. @:functionCode('
  207. return (o instanceof haxe.lang.DynamicObject && ((haxe.lang.DynamicObject) o).__hx_deleteField(field));
  208. ')
  209. public static function deleteField( o : Dynamic, field : String ) : Bool
  210. {
  211. return false;
  212. }
  213. /**
  214. Make a copy of the fields of an object.
  215. **/
  216. public static function copy<T>( o : T ) : T
  217. {
  218. var o2 : Dynamic = {};
  219. for( f in Reflect.fields(o) )
  220. Reflect.setField(o2,f,Reflect.field(o,f));
  221. return cast o2;
  222. }
  223. /**
  224. Transform a function taking an array of arguments into a function that can
  225. be called with any number of arguments.
  226. **/
  227. @:overload(function( f : Array<Dynamic> -> Void ) : Dynamic {})
  228. public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic
  229. {
  230. return new VarArgsFunction(f);
  231. }
  232. }