HxOverrides.hx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. package python.internal;
  23. import python.Syntax;
  24. import python.Syntax.code in py;
  25. @:noDoc
  26. @:native("HxOverrides")
  27. @:access(python.internal.ArrayImpl)
  28. @:access(python.Boot)
  29. class HxOverrides {
  30. // this two cases iterator and shift are like all methods in String and Array and are already handled in Reflect
  31. // we need to modify the transformer to call Reflect directly
  32. @:ifFeature("dynamic_read.iterator", "anon_optional_read.iterator", "anon_read.iterator")
  33. static public function iterator(x) {
  34. if (Boot.isArray(x)) {
  35. return (x : Array<Dynamic>).iterator();
  36. }
  37. return Syntax.callField(x, "iterator");
  38. }
  39. @:ifFeature("dynamic_read.keyValueIterator", "anon_optional_read.keyValueIterator", "anon_read.keyValueIterator")
  40. static public function keyValueIterator(x) {
  41. if (Boot.isArray(x)) {
  42. return (x:Array<Dynamic>).keyValueIterator();
  43. }
  44. return Syntax.callField(x, "keyValueIterator");
  45. }
  46. @:ifFeature("dynamic_binop_==", "dynamic_binop_!=", "type_param_binop_==", "type_param_binop_!=")
  47. static function eq(a:Dynamic, b:Dynamic):Bool {
  48. if (Boot.isArray(a) || Boot.isArray(b)) {
  49. return Syntax.code('a is b');
  50. }
  51. return Syntax.binop(a, "==", b);
  52. }
  53. @:ifFeature("unsafe_string_concat")
  54. static function stringOrNull(s:String):String {
  55. return if (s == null) "null" else s;
  56. }
  57. @:ifFeature("dynamic_read.shift", "anon_optional_read.shift", "anon_read.shift")
  58. static public function shift(x) {
  59. if (Boot.isArray(x)) {
  60. return (x : Array<Dynamic>).shift();
  61. }
  62. return Syntax.callField(x, "shift");
  63. }
  64. @:ifFeature("dynamic_read.pop", "anon_optional_read.pop", "anon_read.pop")
  65. static public function pop(x) {
  66. if (Boot.isArray(x)) {
  67. return (x : Array<Dynamic>).pop();
  68. }
  69. return Syntax.callField(x, "pop");
  70. }
  71. @:ifFeature("dynamic_read.push", "anon_optional_read.push", "anon_read.push")
  72. static public function push(x:Dynamic, e:Dynamic) {
  73. if (Boot.isArray(x)) {
  74. return (x : Array<Dynamic>).push(e);
  75. }
  76. return Syntax.callField(x, "push", e);
  77. }
  78. @:ifFeature("dynamic_read.join", "anon_optional_read.join", "anon_read.join")
  79. static public function join(x, sep) {
  80. if (Boot.isArray(x)) {
  81. return (x : Array<Dynamic>).join(sep);
  82. }
  83. return Syntax.callField(x, "join", sep);
  84. }
  85. @:ifFeature("dynamic_read.filter", "anon_optional_read.filter", "anon_read.filter")
  86. static public function filter(x, f) {
  87. if (Boot.isArray(x)) {
  88. return (x : Array<Dynamic>).filter(f);
  89. }
  90. return Syntax.callField(x, "filter", f);
  91. }
  92. @:ifFeature("dynamic_read.map", "anon_optional_read.map", "anon_read.map")
  93. static public function map(x:Dynamic, f:Dynamic) {
  94. if (Boot.isArray(x)) {
  95. return (x : Array<Dynamic>).map(f);
  96. }
  97. return Syntax.callField(x, "map", f);
  98. }
  99. @:ifFeature("dynamic_read.toUpperCase", "anon_optional_read.toUpperCase", "anon_read.toUpperCase")
  100. static public function toUpperCase(x) {
  101. if (Boot.isString(x)) {
  102. return (x : String).toUpperCase();
  103. }
  104. return Syntax.callField(x, "toUpperCase");
  105. }
  106. @:ifFeature("dynamic_read.toLowerCase", "anon_optional_read.toLowerCase", "anon_read.toLowerCase")
  107. static public function toLowerCase(x) {
  108. if (Boot.isString(x)) {
  109. return (x : String).toLowerCase();
  110. }
  111. return Syntax.callField(x, "toLowerCase");
  112. }
  113. @:ifFeature("dynamic_read.split", "anon_optional_read.split", "anon_read.split")
  114. static public function split(x:Dynamic, delimiter:String) {
  115. if (Boot.isString(x)) {
  116. return (x : String).split(delimiter);
  117. }
  118. return Syntax.callField(x, "split", delimiter);
  119. }
  120. @:ifFeature("dynamic_read.length", "anon_optional_read.length", "anon_read.length")
  121. static public function length(x:Dynamic) {
  122. if (Boot.isString(x)) {
  123. return (x : String).length;
  124. } else if (Boot.isArray(x)) {
  125. return (x : Array<Dynamic>).length;
  126. }
  127. return Syntax.field(x, "length");
  128. }
  129. @:ifFeature("binop_>>>")
  130. static public function rshift(val:Int, n:Int) {
  131. return Syntax.binop(Syntax.binop(val, "%", Syntax.code("0x100000000")), ">>", n);
  132. }
  133. @:ifFeature("binop_%")
  134. static public function modf(a:Float, b:Float) {
  135. if(b == 0.0) {
  136. return Syntax.code("float('nan')");
  137. } else if(a < 0) {
  138. if(b < 0) {
  139. return Syntax.code("-(-{0} % (-{1}))", a, b);
  140. } else {
  141. return Syntax.code("-(-{0} % {1})", a, b);
  142. }
  143. } else if(b < 0) {
  144. return Syntax.code("{0} % (-{1})", a, b);
  145. } else {
  146. return Syntax.code("{0} % {1}", a, b);
  147. }
  148. }
  149. @:ifFeature("binop_%")
  150. static public function mod(a:Int, b:Int) {
  151. if(a < 0) {
  152. if(b < 0) {
  153. return Syntax.code("-(-{0} % (-{1}))", a, b);
  154. } else {
  155. return Syntax.code("-(-{0} % {1})", a, b);
  156. }
  157. } else if(b < 0) {
  158. return Syntax.code("{0} % (-{1})", a, b);
  159. } else {
  160. return Syntax.code("{0} % {1}", a, b);
  161. }
  162. }
  163. @:ifFeature("dynamic_array_read")
  164. static public function arrayGet<T>(a:Dynamic, i:Int):Dynamic {
  165. if (Boot.isArray(a)) {
  166. return ArrayImpl._get(a, i);
  167. } else {
  168. return Syntax.arrayAccess(a, i);
  169. }
  170. }
  171. @:ifFeature("dynamic_array_write")
  172. static public function arraySet(a:Dynamic, i:Int, v:Dynamic) {
  173. if (Boot.isArray(a)) {
  174. return ArrayImpl._set(a, i, v);
  175. } else {
  176. Syntax.assign(Syntax.arrayAccess(a, i), v);
  177. return v;
  178. }
  179. }
  180. @:ifFeature("python._KwArgs.KwArgs_Impl_.fromT")
  181. static public function mapKwArgs(a:{}, v:Dict<String, String>) {
  182. var a = python.Lib.dictAsAnon(python.Lib.anonToDict(a));
  183. for (k in v.keys()) {
  184. var val = v.get(k);
  185. if (Syntax.code('{0}._hx_hasattr({1})', a, k)) {
  186. var x = UBuiltins.getattr(a, k);
  187. UBuiltins.setattr(a, val, x);
  188. UBuiltins.delattr(a, k);
  189. }
  190. }
  191. return a;
  192. }
  193. @:ifFeature("python._KwArgs.KwArgs_Impl_.toDictHelper")
  194. static public function reverseMapKwArgs(a:Dict<String, Dynamic>, v:Dict<String, String>) {
  195. var a = a.copy();
  196. for (k in v.keys()) {
  197. var val = v.get(k);
  198. if (a.hasKey(val)) {
  199. var x = a.get(val, null);
  200. a.set(k, x);
  201. a.remove(val);
  202. }
  203. }
  204. return a;
  205. }
  206. }