Reflect.hx 6.3 KB

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