Reflect.hx 4.8 KB

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