Reflect.hx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (C)2005-2019 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. /**
  23. The Reflect API is a way to manipulate values dynamically through an
  24. abstract interface in an untyped manner. Use with care.
  25. @see https://haxe.org/manual/std-reflection.html
  26. **/
  27. extern class Reflect {
  28. /**
  29. Tells if structure `o` has a field named `field`.
  30. This is only guaranteed to work for anonymous structures. Refer to
  31. `Type.getInstanceFields` for a function supporting class instances.
  32. If `o` or `field` are null, the result is unspecified.
  33. **/
  34. public static function hasField(o:Dynamic, field:String):Bool;
  35. /**
  36. Returns the value of the field named `field` on object `o`.
  37. If `o` is not an object or has no field named `field`, the result is
  38. null.
  39. If the field is defined as a property, its accessors are ignored. Refer
  40. to `Reflect.getProperty` for a function supporting property accessors.
  41. If `field` is null, the result is unspecified.
  42. (As3) If used on a property field, the getter will be invoked. It is
  43. not possible to obtain the value directly.
  44. **/
  45. public static function field(o:Dynamic, field:String):Dynamic;
  46. /**
  47. Sets the field named `field` of object `o` to value `value`.
  48. If `o` has no field named `field`, this function is only guaranteed to
  49. work for anonymous structures.
  50. If `o` or `field` are null, the result is unspecified.
  51. (As3) If used on a property field, the setter will be invoked. It is
  52. not possible to set the value directly.
  53. **/
  54. public static function setField(o:Dynamic, field:String, value:Dynamic):Void;
  55. /**
  56. Returns the value of the field named `field` on object `o`, taking
  57. property getter functions into account.
  58. If the field is not a property, this function behaves like
  59. `Reflect.field`, but might be slower.
  60. If `o` or `field` are null, the result is unspecified.
  61. **/
  62. public static function getProperty(o:Dynamic, field:String):Dynamic;
  63. /**
  64. Sets the field named `field` of object `o` to value `value`, taking
  65. property setter functions into account.
  66. If the field is not a property, this function behaves like
  67. `Reflect.setField`, but might be slower.
  68. If `field` is null, the result is unspecified.
  69. **/
  70. public static function setProperty(o:Dynamic, field:String, value:Dynamic):Void;
  71. /**
  72. Call a method `func` with the given arguments `args`.
  73. The object `o` is ignored in most cases. It serves as the `this`-context in the following
  74. situations:
  75. * (neko) Allows switching the context to `o` in all cases.
  76. * (macro) Same as neko for Haxe 3. No context switching in Haxe 4.
  77. * (js, lua) Require the `o` argument if `func` does not, but should have a context.
  78. This can occur by accessing a function field natively, e.g. through `Reflect.field`
  79. or by using `(object : Dynamic).field`. However, if `func` has a context, `o` is
  80. ignored like on other targets.
  81. **/
  82. public static function callMethod(o:Dynamic, func:haxe.Constraints.Function, args:Array<Dynamic>):Dynamic;
  83. /**
  84. Returns the fields of structure `o`.
  85. This method is only guaranteed to work on anonymous structures. Refer to
  86. `Type.getInstanceFields` for a function supporting class instances.
  87. If `o` is null, the result is unspecified.
  88. **/
  89. public static function fields(o:Dynamic):Array<String>;
  90. /**
  91. Returns true if `f` is a function, false otherwise.
  92. If `f` is null, the result is false.
  93. **/
  94. public static function isFunction(f:Dynamic):Bool;
  95. /**
  96. Compares `a` and `b`.
  97. If `a` is less than `b`, the result is negative. If `b` is less than
  98. `a`, the result is positive. If `a` and `b` are equal, the result is 0.
  99. This function is only defined if `a` and `b` are of the same type.
  100. If that type is a function, the result is unspecified and
  101. `Reflect.compareMethods` should be used instead.
  102. For all other types, the result is 0 if `a` and `b` are equal. If they
  103. are not equal, the result depends on the type and is negative if:
  104. - Numeric types: a is less than b
  105. - String: a is lexicographically less than b
  106. - Other: unspecified
  107. If `a` and `b` are null, the result is 0. If only one of them is null,
  108. the result is unspecified.
  109. **/
  110. public static function compare<T>(a:T, b:T):Int;
  111. /**
  112. Compares the functions `f1` and `f2`.
  113. If `f1` or `f2` are null, the result is false.
  114. If `f1` or `f2` are not functions, the result is unspecified.
  115. Otherwise the result is true if `f1` and the `f2` are physically equal,
  116. false otherwise.
  117. If `f1` or `f2` are member method closures, the result is true if they
  118. are closures of the same method on the same object value, false otherwise.
  119. **/
  120. public static function compareMethods(f1:Dynamic, f2:Dynamic):Bool;
  121. /**
  122. Tells if `v` is an object.
  123. The result is true if `v` is one of the following:
  124. - class instance
  125. - structure
  126. - `Class<T>`
  127. - `Enum<T>`
  128. Otherwise, including if `v` is null, the result is false.
  129. **/
  130. public static function isObject(v:Dynamic):Bool;
  131. /**
  132. Tells if `v` is an enum value.
  133. The result is true if `v` is of type EnumValue, i.e. an enum
  134. constructor.
  135. Otherwise, including if `v` is null, the result is false.
  136. **/
  137. public static function isEnumValue(v:Dynamic):Bool;
  138. /**
  139. Removes the field named `field` from structure `o`.
  140. This method is only guaranteed to work on anonymous structures.
  141. If `o` or `field` are null, the result is unspecified.
  142. **/
  143. public static function deleteField(o:Dynamic, field:String):Bool;
  144. /**
  145. Copies the fields of structure `o`.
  146. This is only guaranteed to work on anonymous structures.
  147. If `o` is null, the result is `null`.
  148. **/
  149. public static function copy<T>(o:Null<T>):Null<T>;
  150. /**
  151. Transform a function taking an array of arguments into a function that can
  152. be called with any number of arguments.
  153. **/
  154. @:overload(function(f:Array<Dynamic>->Void):Dynamic {})
  155. public static function makeVarArgs(f:Array<Dynamic>->Dynamic):Dynamic;
  156. }