Reflect.hx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (C)2005-2018 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 {
  37. return try o[field] catch( e : Dynamic ) null;
  38. }
  39. }
  40. public inline static function setField( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
  41. o[field] = value;
  42. }
  43. public static function getProperty( o : Dynamic, field : String ) : Dynamic {
  44. return if( o == null ) {
  45. untyped __define_feature__("Reflect.getProperty",null);
  46. } else if( o.__properties__ != null && Reflect.field(o, "get_" + field) != null){
  47. callMethod(o, Reflect.field(o,"get_" + field), []);
  48. } else {
  49. Reflect.field(o,field);
  50. }
  51. }
  52. public static function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
  53. if( o.__properties__ != null && o.__properties__["set_"+field]) {
  54. var tmp : String = o.__properties__["set_" + field];
  55. callMethod(o,Reflect.field(o, tmp), [value]) ;
  56. } else {
  57. o[field] = __define_feature__("Reflect.setProperty",value);
  58. }
  59. }
  60. public static function callMethod( o : Dynamic, func : haxe.Constraints.Function, args : Array<Dynamic> ) : Dynamic {
  61. if (args == null || args.length == 0){
  62. return func(o);
  63. } else {
  64. var self_arg = false;
  65. if (o != null && o.__name__ == null){
  66. self_arg = true;
  67. }
  68. return if (self_arg){
  69. func(o, lua.TableTools.unpack(cast args, 0, args.length-1));
  70. } else {
  71. func(lua.TableTools.unpack(cast args, 0, args.length-1));
  72. }
  73. }
  74. }
  75. public static function fields( o : Dynamic ) : Array<String> {
  76. if (lua.Lua.type(o) == "string"){
  77. return Reflect.fields(untyped String.prototype);
  78. } else {
  79. return [for (f in lua.Boot.fieldIterator(o)) f];
  80. }
  81. }
  82. public static function isFunction( f : Dynamic ) : Bool {
  83. return Lua.type(f) == "function" && !(Boot.isClass(f) || Boot.isEnum(f));
  84. }
  85. public static function compare<T>( a : T, b : T ) : Int {
  86. if (a == b) return 0
  87. else if (a == null) return -1
  88. else if (b == null) return 1
  89. else return (cast a) > (cast b) ? 1 : -1;
  90. }
  91. public static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool {
  92. return f1 == f2;
  93. }
  94. public static function isObject( v : Dynamic ) : Bool untyped {
  95. if( v == null )
  96. return false;
  97. var t = __lua__("type(v)");
  98. return (t == "string" || (t == "table" && v.__enum__ == null)) || (t == "function" && (lua.Boot.isClass(v) || lua.Boot.isEnum(v)) != null);
  99. }
  100. public static function isEnumValue( v : Dynamic ) : Bool {
  101. return v != null && Std.is(v,lua.Table) && v.__enum__ != null;
  102. }
  103. public static function deleteField( o : Dynamic, field : String ) : Bool untyped {
  104. if( !hasField(o,field) ) return false;
  105. o[field] = null;
  106. o.__fields__[field]=null;
  107. return true;
  108. }
  109. public static function copy<T>( o : T ) : T {
  110. var o2 : Dynamic = {};
  111. for( f in Reflect.fields(o) )
  112. Reflect.setField(o2,f,Reflect.field(o,f));
  113. return o2;
  114. }
  115. @:overload(function( f : Array<Dynamic> -> Void ) : Dynamic {})
  116. public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic {
  117. /*
  118. - Convert var arg to table
  119. - Set indexing from zero
  120. - Extract real table length
  121. - Recreate as dynamic array
  122. - Return function called with this array
  123. */
  124. return untyped __lua__("function(...)
  125. local a = {...}
  126. local b = {}
  127. local l = 0
  128. for k, v in pairs(a) do
  129. b[k-1] = v
  130. l = math.max(k,l)
  131. end
  132. return f(_hx_tab_array(b, l))
  133. end");
  134. }
  135. }