Reflect.hx 7.4 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 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. @:functionBody('
  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. @:functionBody('
  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. @:functionBody('
  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. @:functionBody('
  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. return haxe.lang.Runtime.slowGetField(o, field, false);
  95. ')
  96. public static function getProperty( o : Dynamic, field : String ) : Dynamic
  97. {
  98. return null;
  99. }
  100. /**
  101. Similar to setField but also supports property (might be slower).
  102. **/
  103. @:functionBody('
  104. if (o is haxe.lang.IHxObject)
  105. ((haxe.lang.IHxObject) o).__hx_setField(field, haxe.lang.FieldLookup.hash(field), value, true);
  106. else
  107. haxe.lang.Runtime.slowSetField(o, field, value);
  108. ')
  109. public static function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void
  110. {
  111. }
  112. /**
  113. Call a method with the given object and arguments.
  114. **/
  115. @:functionBody('
  116. return ((haxe.lang.Function) func).__hx_invokeDynamic(args);
  117. ')
  118. public static function callMethod( o : Dynamic, func : Dynamic, args : Array<Dynamic> ) : Dynamic
  119. {
  120. return null;
  121. }
  122. /**
  123. Returns the list of fields of an object, excluding its prototype (class methods).
  124. **/
  125. @:functionBody('
  126. if (o is haxe.lang.IHxObject)
  127. {
  128. Array<object> ret = new Array<object>();
  129. ((haxe.lang.IHxObject) o).__hx_getFields(ret);
  130. return ret;
  131. } else if (o is System.Type) {
  132. return Type.getClassFields( (System.Type) o);
  133. } else {
  134. return new Array<object>();
  135. }
  136. ')
  137. public static function fields( o : Dynamic ) : Array<String>
  138. {
  139. return null;
  140. }
  141. /**
  142. Tells if a value is a function or not.
  143. **/
  144. @:functionBody('
  145. return f is haxe.lang.Function;
  146. ')
  147. public static function isFunction( f : Dynamic ) : Bool
  148. {
  149. return false;
  150. }
  151. /**
  152. Generic comparison function, does not work for methods, see [compareMethods]
  153. **/
  154. @:functionBody('
  155. return haxe.lang.Runtime.compare(a, b);
  156. ')
  157. public static function compare<T>( a : T, b : T ) : Int
  158. {
  159. return 0;
  160. }
  161. /**
  162. Compare two methods closures. Returns true if it's the same method of the same instance.
  163. **/
  164. @:functionBody('
  165. if (f1 == f2)
  166. return true;
  167. if (f1 is haxe.lang.Closure && f2 is haxe.lang.Closure)
  168. {
  169. haxe.lang.Closure f1c = (haxe.lang.Closure) f1;
  170. haxe.lang.Closure f2c = (haxe.lang.Closure) f2;
  171. return haxe.lang.Runtime.refEq(f1c.obj, f2c.obj) && f1c.field.Equals(f2c.field);
  172. }
  173. return false;
  174. ')
  175. public static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool
  176. {
  177. return false;
  178. }
  179. /**
  180. Tells if a value is an object or not.
  181. **/
  182. @:functionBody('
  183. return v is haxe.lang.DynamicObject;
  184. ')
  185. public static function isObject( v : Dynamic ) : Bool
  186. {
  187. return false;
  188. }
  189. /**
  190. Delete an object field.
  191. **/
  192. @:functionBody('
  193. return (o is haxe.lang.DynamicObject && ((haxe.lang.DynamicObject) o).__hx_deleteField(f, haxe.lang.FieldLookup.hash(f)));
  194. ')
  195. public static function deleteField( o : Dynamic, f : String ) : Bool
  196. {
  197. return false;
  198. }
  199. /**
  200. Make a copy of the fields of an object.
  201. **/
  202. public static function copy<T>( o : T ) : T
  203. {
  204. var o2 : Dynamic = {};
  205. for( f in Reflect.fields(o) )
  206. Reflect.setField(o2,f,Reflect.field(o,f));
  207. return cast o2;
  208. }
  209. /**
  210. Transform a function taking an array of arguments into a function that can
  211. be called with any number of arguments.
  212. **/
  213. public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic
  214. {
  215. return new VarArgsFunction(f);
  216. }
  217. }