Reflect.hx 5.5 KB

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