Reflect.hx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. @:keep @:coreApi class Reflect {
  48. @:functionCode('
  49. if (o is haxe.lang.IHxObject)
  50. return ((haxe.lang.IHxObject) o).__hx_getField(field, haxe.lang.FieldLookup.hash(field), false, true, false) != haxe.lang.Runtime.undefined;
  51. return haxe.lang.Runtime.slowHasField(o, field);
  52. ')
  53. public static function hasField( o : Dynamic, field : String ) : Bool
  54. {
  55. return false;
  56. }
  57. @:functionCode('
  58. if (o is haxe.lang.IHxObject)
  59. return ((haxe.lang.IHxObject) o).__hx_getField(field, haxe.lang.FieldLookup.hash(field), false, false, false);
  60. return haxe.lang.Runtime.slowGetField(o, field, false);
  61. ')
  62. public static function field( o : Dynamic, field : String ) : Dynamic
  63. {
  64. return null;
  65. }
  66. @:functionCode('
  67. if (o is haxe.lang.IHxObject)
  68. ((haxe.lang.IHxObject) o).__hx_setField(field, haxe.lang.FieldLookup.hash(field), value, false);
  69. else
  70. haxe.lang.Runtime.slowSetField(o, field, value);
  71. ')
  72. public static function setField( o : Dynamic, field : String, value : Dynamic ) : Void
  73. {
  74. }
  75. @:functionCode('
  76. if (o is haxe.lang.IHxObject)
  77. return ((haxe.lang.IHxObject) o).__hx_getField(field, haxe.lang.FieldLookup.hash(field), false, false, true);
  78. if (haxe.lang.Runtime.slowHasField(o, "get_" + field))
  79. return haxe.lang.Runtime.slowCallField(o, "get_" + field, null);
  80. return haxe.lang.Runtime.slowGetField(o, field, false);
  81. ')
  82. public static function getProperty( o : Dynamic, field : String ) : Dynamic
  83. {
  84. return null;
  85. }
  86. @:functionCode('
  87. if (o is haxe.lang.IHxObject)
  88. ((haxe.lang.IHxObject) o).__hx_setField(field, haxe.lang.FieldLookup.hash(field), value, true);
  89. else if (haxe.lang.Runtime.slowHasField(o, "set_" + field))
  90. haxe.lang.Runtime.slowCallField(o, "set_" + field, new Array<object>(new object[]{value}));
  91. else
  92. haxe.lang.Runtime.slowSetField(o, field, value);
  93. ')
  94. public static function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void
  95. {
  96. }
  97. @:functionCode('
  98. return ((haxe.lang.Function) func).__hx_invokeDynamic(args);
  99. ')
  100. public static function callMethod( o : Dynamic, func : haxe.Constraints.Function, args : Array<Dynamic> ) : Dynamic
  101. {
  102. return null;
  103. }
  104. @:functionCode('
  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. @:functionCode('
  121. return f is haxe.lang.Function;
  122. ')
  123. public static function isFunction( f : Dynamic ) : Bool
  124. {
  125. return false;
  126. }
  127. public static function compare<T>( a : T, b : T ) : Int
  128. {
  129. return cs.internal.Runtime.compare(a, b);
  130. }
  131. @:functionCode('
  132. if (f1 == f2)
  133. return true;
  134. if (f1 is haxe.lang.Closure && f2 is 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.obj, f2c.obj) && 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. @:functionCode('
  147. return v != null && !(v is haxe.lang.Enum || v is haxe.lang.Function || v is System.ValueType);
  148. ')
  149. public static function isObject( v : Dynamic ) : Bool
  150. {
  151. return false;
  152. }
  153. @:functionCode('
  154. return v != null && (v is haxe.lang.Enum || v is System.Enum);
  155. ')
  156. public static function isEnumValue( v : Dynamic ) : Bool {
  157. return switch(Type.typeof(v)) {
  158. case TEnum(_): true;
  159. case _: false;
  160. }
  161. }
  162. @:functionCode('
  163. return (o is haxe.lang.DynamicObject && ((haxe.lang.DynamicObject) o).__hx_deleteField(field, haxe.lang.FieldLookup.hash(field)));
  164. ')
  165. public static function deleteField( o : Dynamic, field : String ) : Bool
  166. {
  167. return false;
  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. @:overload(function( f : Array<Dynamic> -> Void ) : Dynamic {})
  177. public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic
  178. {
  179. return new VarArgsFunction(f);
  180. }
  181. }