Reflect.hx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (C)2005-2017 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. @:coreApi class Reflect {
  23. public inline static function hasField( o : Dynamic, field : String ) : Bool {
  24. return untyped __call__("_hx_has_field", o, field);
  25. }
  26. public static function field( o : Dynamic, field : String ) : Dynamic {
  27. return untyped __call__("_hx_field", o, field);
  28. }
  29. public inline static function setField( o : Dynamic, field : String, value : Dynamic ) : Void {
  30. untyped __setfield__(o, field, value);
  31. }
  32. public static function getProperty( o : Dynamic, field : String ) : Dynamic {
  33. #if php untyped __call__('var_dump', field); #end
  34. if (null == o) return null;
  35. var cls : String = Std.is(o, Class) ? untyped __php__("$o->__tname__") : untyped __call__("get_class", o);
  36. var cls_vars : php.NativeArray = untyped __call__("get_class_vars", cls);
  37. if (untyped __php__("isset($cls_vars['__properties__']) && isset($cls_vars['__properties__']['get_'.$field]) && ($field = $cls_vars['__properties__']['get_'.$field])"))
  38. return untyped __php__("$o->$field()");
  39. else
  40. return untyped __call__("_hx_field", o, field);
  41. }
  42. public static function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void {
  43. if (null == o) return null;
  44. var cls : String = Std.is(o, Class) ? untyped __php__("$o->__tname__") : untyped __call__("get_class", o);
  45. var cls_vars : php.NativeArray = untyped __call__("get_class_vars", cls);
  46. if (untyped __php__("isset($cls_vars['__properties__']) && isset($cls_vars['__properties__']['set_'.$field]) && ($field = $cls_vars['__properties__']['set_'.$field])"))
  47. untyped __php__("$o->$field($value)");
  48. else
  49. untyped __setfield__(o, field, value);
  50. }
  51. public static function callMethod( o : Dynamic, func : haxe.Constraints.Function, args : Array<Dynamic> ) : Dynamic untyped {
  52. return __call__("call_user_func_array", __call__("is_callable", func) ? func : __call__("array", o, func), (null == args ? __call__("array") : __field__(args, "a")));
  53. }
  54. public static function fields( o : Dynamic ) : Array<String> {
  55. if( o == null ) return new Array();
  56. return untyped __php__("$o instanceof _hx_array")
  57. ? __php__("new _hx_array(array('concat','copy','insert','iterator','length','join','pop','push','remove','reverse','shift','slice','sort','splice','toString','unshift'))")
  58. : (__call__('is_string', o)
  59. ? __php__("new _hx_array(array('charAt','charCodeAt','indexOf','lastIndexOf','length','split','substr','toLowerCase','toString','toUpperCase'))")
  60. : __php__("new _hx_array(_hx_get_object_vars($o))"));
  61. }
  62. public static function isFunction( f : Dynamic ) : Bool {
  63. return untyped __php__("(is_array($f) && is_callable($f)) || _hx_is_lambda($f)") || (__php__("is_array($f)") && hasField(__php__("$f[0]"), __php__("$f[1]")) && __php__("$f[1]") != "length");
  64. }
  65. public static function compare<T>( a : T, b : T ) : Int {
  66. return ( a == b ) ? 0 : untyped __php__("is_string($a)") ? untyped __php__("strcmp($a, $b)") : (((cast a) > (cast b)) ? 1 : -1);
  67. }
  68. public static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool {
  69. if(untyped __call__("is_array", f1) && untyped __call__("is_array", f1))
  70. return untyped __php__("$f1[0] === $f2[0] && $f1[1] == $f2[1]");
  71. if(untyped __call__("is_string", f1) && untyped __call__("is_string", f2))
  72. return f1 == f2;
  73. return false;
  74. }
  75. public static function isObject( v : Dynamic ) : Bool {
  76. if( v == null )
  77. return false;
  78. if(untyped __call__("is_object", v))
  79. return untyped __php__("$v instanceof _hx_anonymous") || Type.getClass(v) != null
  80. || untyped __php__("$v instanceof _hx_class")
  81. || untyped __php__("$v instanceof _hx_enum");
  82. return untyped __php__("is_string($v) && !_hx_is_lambda($v)");
  83. }
  84. public static function isEnumValue( v : Dynamic ) : Bool {
  85. return untyped __php__("$v instanceof Enum");
  86. }
  87. public static function deleteField( o : Dynamic, field : String ) : Bool {
  88. if(!hasField(o,field)) return false;
  89. untyped __php__("if(isset($o->__dynamics[$field])) unset($o->__dynamics[$field]); else if($o instanceof _hx_anonymous) unset($o->$field); else $o->$field = null");
  90. return true;
  91. }
  92. public static function copy<T>( o : T ) : T {
  93. if (untyped __call__("is_string", o)) return o;
  94. var o2 : Dynamic = {};
  95. for( f in Reflect.fields(o) )
  96. Reflect.setField(o2,f,Reflect.field(o,f));
  97. return o2;
  98. }
  99. @:overload(function( f : Array<Dynamic> -> Void ) : Dynamic {})
  100. public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic {
  101. return untyped __php__("array(new _hx_lambda(array(&$f), '_hx_make_var_args'), 'execute')");
  102. }
  103. }