Reflect.hx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. import lua.Lua;
  23. import lua.TableTools;
  24. import lua.Boot;
  25. @:coreApi class Reflect {
  26. public inline static function hasField( o : Dynamic, field : String ) : Bool {
  27. return untyped o.__fields__ != null ? o.__fields__[field] != null : o[field] != null;
  28. }
  29. public static function field( o : Dynamic, field : String ) : Dynamic untyped {
  30. return try o[field] catch( e : Dynamic ) null;
  31. }
  32. public inline static function setField( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
  33. o[field] = value;
  34. }
  35. public static inline function getProperty( o : Dynamic, field : String ) : Dynamic {
  36. var tmp : Dynamic;
  37. return if( o == null ) {
  38. untyped __define_feature__("Reflect.getProperty",null);
  39. } else if( o.__properties__ != null && Reflect.field(o, "get_" + field) != null){
  40. callMethod(o, Reflect.field(o,"get_" + field), []);
  41. } else {
  42. Reflect.field(o,field);
  43. }
  44. }
  45. public static inline function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
  46. var tmp : String;
  47. if( o.__properties__ && o.__properties__["set_"+field]) {
  48. tmp = o.__properties__["set_" + field];
  49. if (o.__name__ != null){
  50. callMethod(null,Reflect.field(o, tmp), [value]) ;
  51. } else {
  52. callMethod(o,Reflect.field(o, tmp), [value]) ;
  53. }
  54. } else {
  55. o[field] = __define_feature__("Reflect.setProperty",value);
  56. }
  57. }
  58. public static function callMethod( o : Dynamic, func : haxe.Constraints.Function, args : Array<Dynamic> ) : Dynamic {
  59. if (args == null || args.length == 0){
  60. return func(o);
  61. } else {
  62. var self_arg = false;
  63. if (o != null ){
  64. // if o is not null, it means we need to pass it as the "self"
  65. // parameter. However, we should also check to see if it's
  66. // a valid class instance in the first place.
  67. // TODO: Find a more flexible way of determining if we need
  68. // self or not
  69. self_arg = true;
  70. }
  71. var new_args = lua.Table.create();
  72. for (i in 0...args.length){
  73. // copy args to 1-indexed table
  74. new_args[i + 1] = args[i];
  75. }
  76. return if (self_arg){
  77. // call with o as leading self param
  78. func(o, lua.TableTools.unpack(new_args, 1, TableTools.maxn(new_args)));
  79. } else {
  80. // call with no self param
  81. func(lua.TableTools.unpack(new_args, 1, TableTools.maxn(new_args)));
  82. }
  83. }
  84. }
  85. public static function fields( o : Dynamic ) : Array<String> {
  86. return [for (f in lua.Boot.fieldIterator(o)) f];
  87. }
  88. public static function isFunction( f : Dynamic ) : Bool {
  89. return Lua.type(f) == "function" && !(Boot.isClass(f) || Boot.isEnum(f));
  90. }
  91. public static function compare<T>( a : T, b : T ) : Int {
  92. if (a == b) return 0
  93. else if (a == null) return -1
  94. else if (b == null) return 1
  95. else return (cast a) > (cast b) ? 1 : -1;
  96. }
  97. public static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool {
  98. return f1 == f2;
  99. }
  100. public static function isObject( v : Dynamic ) : Bool untyped {
  101. if( v == null )
  102. return false;
  103. var t = __lua__("type(v)");
  104. return (t == "string" || (t == "table" && v.__enum__ == null)) || (t == "function" && (lua.Boot.isClass(v) || lua.Boot.isEnum(v)) != null);
  105. }
  106. public static function isEnumValue( v : Dynamic ) : Bool {
  107. return v != null && Std.is(v,lua.Table) && v.__enum__ != null;
  108. }
  109. public static function deleteField( o : Dynamic, field : String ) : Bool untyped {
  110. if( !hasField(o,field) ) return false;
  111. o[field] = null;
  112. o.__fields__[field]=null;
  113. return true;
  114. }
  115. public static function copy<T>( o : T ) : T {
  116. var o2 : Dynamic = {};
  117. for( f in Reflect.fields(o) )
  118. Reflect.setField(o2,f,Reflect.field(o,f));
  119. return o2;
  120. }
  121. @:overload(function( f : Array<Dynamic> -> Void ) : Dynamic {})
  122. public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic {
  123. /*
  124. - Convert var arg to table
  125. - Set indexing from zero
  126. - Extract real table length
  127. - Recreate as dynamic array
  128. - Return function called with this array
  129. */
  130. return untyped __lua__("function(...)
  131. local a = {...}
  132. local b = {}
  133. local l = 0
  134. for k, v in pairs(a) do
  135. b[k-1] = v
  136. l = math.max(k,l)
  137. end
  138. return f(_hx_tab_array(b, l))
  139. end");
  140. }
  141. }