Reflect.hx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. import cs.system.reflection.*;
  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. @:keep @:coreApi class Reflect {
  49. @:functionCode('
  50. if (o is haxe.lang.IHxObject)
  51. return ((haxe.lang.IHxObject) o).__hx_getField(field, haxe.lang.FieldLookup.hash(field), false, true, false) != haxe.lang.Runtime.undefined;
  52. return haxe.lang.Runtime.slowHasField(o, field);
  53. ')
  54. public static function hasField( o : Dynamic, field : String ) : Bool
  55. {
  56. return false;
  57. }
  58. @:functionCode('
  59. if (o is haxe.lang.IHxObject)
  60. return ((haxe.lang.IHxObject) o).__hx_getField(field, haxe.lang.FieldLookup.hash(field), false, false, false);
  61. return haxe.lang.Runtime.slowGetField(o, field, false);
  62. ')
  63. public static function field( o : Dynamic, field : String ) : Dynamic
  64. {
  65. return null;
  66. }
  67. @:functionCode('
  68. if (o is haxe.lang.IHxObject)
  69. ((haxe.lang.IHxObject) o).__hx_setField(field, haxe.lang.FieldLookup.hash(field), value, false);
  70. else
  71. haxe.lang.Runtime.slowSetField(o, field, value);
  72. ')
  73. public static function setField( o : Dynamic, field : String, value : Dynamic ) : Void
  74. {
  75. }
  76. @:functionCode('
  77. if (o is haxe.lang.IHxObject)
  78. return ((haxe.lang.IHxObject) o).__hx_getField(field, haxe.lang.FieldLookup.hash(field), false, false, true);
  79. if (haxe.lang.Runtime.slowHasField(o, "get_" + field))
  80. return haxe.lang.Runtime.slowCallField(o, "get_" + field, null);
  81. return haxe.lang.Runtime.slowGetField(o, field, false);
  82. ')
  83. public static function getProperty( o : Dynamic, field : String ) : Dynamic
  84. {
  85. return null;
  86. }
  87. @:functionCode('
  88. if (o is haxe.lang.IHxObject)
  89. ((haxe.lang.IHxObject) o).__hx_setField(field, haxe.lang.FieldLookup.hash(field), value, true);
  90. else if (haxe.lang.Runtime.slowHasField(o, "set_" + field))
  91. haxe.lang.Runtime.slowCallField(o, "set_" + field, new Array<object>(new object[]{value}));
  92. else
  93. haxe.lang.Runtime.slowSetField(o, field, value);
  94. ')
  95. public static function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void
  96. {
  97. }
  98. @:functionCode('
  99. return ((haxe.lang.Function) func).__hx_invokeDynamic(args);
  100. ')
  101. public static function callMethod( o : Dynamic, func : haxe.Constraints.Function, args : Array<Dynamic> ) : Dynamic
  102. {
  103. return null;
  104. }
  105. @:functionCode('
  106. if (o is haxe.lang.IHxObject)
  107. {
  108. Array<object> ret = new Array<object>();
  109. ((haxe.lang.IHxObject) o).__hx_getFields(ret);
  110. return ret;
  111. } else if (o is System.Type) {
  112. return Type.getClassFields( (System.Type) o);
  113. } else {
  114. return instanceFields( (System.Type) o );
  115. }
  116. ')
  117. public static function fields( o : Dynamic ) : Array<String>
  118. {
  119. return null;
  120. }
  121. private static function instanceFields( c : Class<Dynamic> ) : Array<String>
  122. {
  123. var c = cs.Lib.toNativeType(c);
  124. var ret = [];
  125. var bindingFlags:BindingFlags = cast cast(BindingFlags.Public, Int) | cast(BindingFlags.Instance, Int) | cast(BindingFlags.FlattenHierarchy,Int);
  126. var mis = c.GetFields(bindingFlags);
  127. for (i in 0...mis.Length)
  128. {
  129. var i = mis[i];
  130. ret.push(i.Name);
  131. }
  132. return ret;
  133. }
  134. @:functionCode('
  135. return f is haxe.lang.Function;
  136. ')
  137. public static function isFunction( f : Dynamic ) : Bool
  138. {
  139. return false;
  140. }
  141. public static function compare<T>( a : T, b : T ) : Int
  142. {
  143. return cs.internal.Runtime.compare(a, b);
  144. }
  145. @:functionCode('
  146. if (f1 == f2)
  147. return true;
  148. if (f1 is haxe.lang.Closure && f2 is haxe.lang.Closure)
  149. {
  150. haxe.lang.Closure f1c = (haxe.lang.Closure) f1;
  151. haxe.lang.Closure f2c = (haxe.lang.Closure) f2;
  152. return haxe.lang.Runtime.refEq(f1c.obj, f2c.obj) && f1c.field.Equals(f2c.field);
  153. }
  154. return false;
  155. ')
  156. public static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool
  157. {
  158. return false;
  159. }
  160. @:functionCode('
  161. return v != null && !(v is haxe.lang.Enum || v is haxe.lang.Function || v is System.ValueType);
  162. ')
  163. public static function isObject( v : Dynamic ) : Bool
  164. {
  165. return false;
  166. }
  167. @:functionCode('
  168. return v != null && (v is haxe.lang.Enum || v is System.Enum);
  169. ')
  170. public static function isEnumValue( v : Dynamic ) : Bool {
  171. return switch(Type.typeof(v)) {
  172. case TEnum(_): true;
  173. case _: false;
  174. }
  175. }
  176. @:functionCode('
  177. return (o is haxe.lang.DynamicObject && ((haxe.lang.DynamicObject) o).__hx_deleteField(field, haxe.lang.FieldLookup.hash(field)));
  178. ')
  179. public static function deleteField( o : Dynamic, field : String ) : Bool
  180. {
  181. return false;
  182. }
  183. public static function copy<T>( o : T ) : T
  184. {
  185. var o2 : Dynamic = {};
  186. for( f in Reflect.fields(o) )
  187. Reflect.setField(o2,f,Reflect.field(o,f));
  188. return cast o2;
  189. }
  190. @:overload(function( f : Array<Dynamic> -> Void ) : Dynamic {})
  191. public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic
  192. {
  193. return new VarArgsFunction(f);
  194. }
  195. }