Reflect.hx 8.1 KB

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