Reflect.hx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (C)2005-2018 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 php.Boot;
  23. import php.Syntax;
  24. import php.Closure;
  25. import haxe.Constraints;
  26. using php.Global;
  27. @:coreApi class Reflect {
  28. public static function hasField( o : Dynamic, field : String ) : Bool {
  29. if (!o.is_object()) return false;
  30. if (o.property_exists(field)) return true;
  31. if (Boot.isClass(o)) {
  32. var phpClassName = Boot.castClass(o).phpClassName;
  33. return Global.property_exists(phpClassName, field) || Global.method_exists(phpClassName, field) || Global.defined('$phpClassName::$field');
  34. }
  35. return false;
  36. }
  37. public static function field( o : Dynamic, field : String ) : Dynamic {
  38. if (o.is_string()) {
  39. return Syntax.getField(Boot.dynamicString(o), field);
  40. }
  41. if (!o.is_object()) return null;
  42. if (o.property_exists(field)) {
  43. return Syntax.getField(o, field);
  44. }
  45. if (o.method_exists(field)) {
  46. return Boot.closure(o, field);
  47. }
  48. if (Boot.isClass(o)) {
  49. var phpClassName = Boot.castClass(o).phpClassName;
  50. if (Global.defined('$phpClassName::$field')) {
  51. return Global.constant('$phpClassName::$field');
  52. }
  53. if (Global.property_exists(phpClassName, field)) {
  54. return Syntax.getField(o, field);
  55. }
  56. if (Global.method_exists(phpClassName, field)) {
  57. return Boot.closure(phpClassName, field);
  58. }
  59. }
  60. return null;
  61. }
  62. public static function setField( o : Dynamic, field : String, value : Dynamic ) : Void {
  63. Syntax.setField(o, field, value);
  64. }
  65. public static function getProperty( o : Dynamic, field : String ) : Dynamic {
  66. if (o.is_object()) {
  67. if (Boot.isClass(o)) {
  68. var phpClassName = Boot.castClass(o).phpClassName;
  69. if (Boot.hasGetter(phpClassName, field)) {
  70. return Syntax.staticCall(phpClassName, 'get_$field');
  71. }
  72. } else if (Boot.hasGetter(Global.get_class(o), field)) {
  73. return Syntax.call(o, 'get_$field');
  74. }
  75. }
  76. return Reflect.field(o, field);
  77. }
  78. public static function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void {
  79. if (o.is_object()) {
  80. if (Boot.hasSetter(Global.get_class(o), field)) {
  81. Syntax.call(o, 'set_$field', value);
  82. } else {
  83. Syntax.setField(o, field, value);
  84. }
  85. }
  86. }
  87. public static function callMethod( o : Dynamic, func : Function, args : Array<Dynamic> ) : Dynamic {
  88. if (Std.is(func, Closure)) {
  89. if (o != null) {
  90. func = cast cast(func, Closure).bindTo(o);
  91. }
  92. return Global.call_user_func_array(func, @:privateAccess args.arr);
  93. } else {
  94. return Boot.castClosure(func).callWith(o, @:privateAccess args.arr);
  95. }
  96. }
  97. public static function fields( o : Dynamic ) : Array<String> {
  98. if (Global.is_object(o)) {
  99. return @:privateAccess Array.wrap(Global.get_object_vars(o).array_keys());
  100. }
  101. return [];
  102. }
  103. public static inline function isFunction( f : Dynamic ) : Bool {
  104. return Boot.isFunction(f);
  105. }
  106. public static function compare<T>( a : T, b : T ) : Int {
  107. if (a == b) return 0;
  108. if (Global.is_string(a)){
  109. return Global.strcmp(cast a, cast b);
  110. } else {
  111. return ((cast a) > (cast b) ? 1 : -1);
  112. }
  113. }
  114. public static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool {
  115. if (Boot.isHxClosure(f1) && Boot.isHxClosure(f2)) {
  116. return f1.equals(f2);
  117. } else {
  118. return f1 == f2;
  119. }
  120. }
  121. public static function isObject( v : Dynamic ) : Bool {
  122. if (Boot.isEnumValue(v)) {
  123. return false;
  124. } else {
  125. return v.is_object() || v.is_string();
  126. }
  127. }
  128. public static inline function isEnumValue( v : Dynamic ) : Bool {
  129. return Boot.isEnumValue(v);
  130. }
  131. public static function deleteField( o : Dynamic, field : String ) : Bool {
  132. if (hasField(o, field)) {
  133. Global.unset(Syntax.getField(o, field));
  134. return true;
  135. } else {
  136. return false;
  137. }
  138. }
  139. public static function copy<T>( o : T ) : T {
  140. if (Global.is_object(o)) {
  141. var fields = Global.get_object_vars(cast o);
  142. var hxAnon = Boot.getHxAnon().phpClassName;
  143. return Syntax.construct(hxAnon, fields);
  144. } else {
  145. return null;
  146. }
  147. }
  148. @:overload(function( f : Array<Dynamic> -> Void ) : Dynamic {})
  149. public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic {
  150. return function () {
  151. return Global.call_user_func(f, @:privateAccess Array.wrap(Global.func_get_args()));
  152. }
  153. }
  154. }