Reflect.hx 5.5 KB

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