Reflect.hx 4.9 KB

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