Reflect.hx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. @:core_api class Reflect {
  26. public inline static function hasField( o : Dynamic, field : String ) : Bool {
  27. return untyped __call__("_hx_has_field", o, field);
  28. }
  29. public static function field( o : Dynamic, field : String ) : Dynamic {
  30. return untyped __call__("_hx_field", o, field);
  31. }
  32. public inline static function setField( o : Dynamic, field : String, value : Dynamic ) : Void {
  33. untyped __setfield__(o, field, value);
  34. }
  35. public static function getProperty( o : Dynamic, field : String ) : Dynamic {
  36. if (null == o) return null;
  37. var cls : String = Std.is(o, Class) ? untyped __php__("$o->__tname__") : untyped __call__("get_class", o);
  38. var cls_vars : php.NativeArray = untyped __call__("get_class_vars", cls);
  39. if (untyped __php__("isset($cls_vars['__properties__']) && isset($cls_vars['__properties__']['get_'.$field]) && ($field = $cls_vars['__properties__']['get_'.$field])"))
  40. return untyped __php__("$o->$field()");
  41. else
  42. return untyped __php__("$o->$field");
  43. }
  44. public static function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
  45. if (null == o) return null;
  46. var cls : String = Std.is(o, Class) ? untyped __php__("$o->__tname__") : untyped __call__("get_class", o);
  47. var cls_vars : php.NativeArray = untyped __call__("get_class_vars", cls);
  48. if (untyped __php__("isset($cls_vars['__properties__']) && isset($cls_vars['__properties__']['set_'.$field]) && ($field = $cls_vars['__properties__']['set_'.$field])"))
  49. return untyped __php__("$o->$field($value)");
  50. else
  51. return untyped __php__("$o->$field = $value");
  52. }
  53. public static function callMethod( o : Dynamic, func : Dynamic, args : Array<Dynamic> ) : Dynamic untyped {
  54. if (__call__("is_string", o) && !__call__("is_array", func)) {
  55. return __call__("call_user_func_array", field(o, func), __field__(args, "»a"));
  56. }
  57. return __call__("call_user_func_array", __call__("is_callable", func) ? func : __call__("array", o, func), (null == args ? __call__("array") : __field__(args, "»a")));
  58. }
  59. public static function fields( o : Dynamic ) : Array<String> {
  60. if( o == null ) return new Array();
  61. return untyped __php__('$o instanceof _hx_array')
  62. ? __php__("new _hx_array(array('concat','copy','insert','iterator','length','join','pop','push','remove','reverse','shift','slice','sort','splice','toString','unshift'))")
  63. : (__call__('is_string', o)
  64. ? __php__("new _hx_array(array('charAt','charCodeAt','indexOf','lastIndexOf','length','split','substr','toLowerCase','toString','toUpperCase'))")
  65. : __php__("new _hx_array(_hx_get_object_vars($o))"));
  66. }
  67. public static function isFunction( f : Dynamic ) : Bool {
  68. 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");
  69. }
  70. public static function compare<T>( a : T, b : T ) : Int {
  71. return ( a == b ) ? 0 : (((cast a) > (cast b)) ? 1 : -1);
  72. }
  73. public static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool {
  74. if(untyped __call__("is_array", f1) && untyped __call__("is_array", f1))
  75. return untyped __php__("$f1[0] === $f2[0] && $f1[1] == $f2[1]");
  76. if(untyped __call__("is_string", f1) && untyped __call__("is_string", f2))
  77. return f1 == f2;
  78. return false;
  79. }
  80. public static function isObject( v : Dynamic ) : Bool {
  81. if( v == null )
  82. return false;
  83. if(untyped __call__("is_object", v))
  84. return untyped __php__("$v instanceof _hx_anonymous") || Type.getClass(v) != null;
  85. return untyped __php__("is_string($v) && !_hx_is_lambda($v)");
  86. }
  87. public static function deleteField( o : Dynamic, f : String ) : Bool {
  88. if(!hasField(o,f)) return false;
  89. untyped __php__("if(isset($o->»dynamics[$f])) unset($o->»dynamics[$f]); else if($o instanceof _hx_anonymous) unset($o->$f); else $o->$f = 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. public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic {
  100. untyped __php__("return array(new _hx_lambda(array(&$f), '_hx_make_var_args'), 'execute')");
  101. }
  102. }