Reflect.hx 5.5 KB

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