HxOverrides.hx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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_binop_==", "dynamic_binop_!=")
  40. static function eq(a:Dynamic, b:Dynamic):Bool {
  41. if (Boot.isArray(a) || Boot.isArray(b)) {
  42. return Syntax.code('a is b');
  43. }
  44. return Syntax.binop(a, "==", b);
  45. }
  46. @:ifFeature("unsafe_string_concat")
  47. static function stringOrNull(s:String):String {
  48. return if (s == null) "null" else s;
  49. }
  50. @:ifFeature("dynamic_read.shift", "anon_optional_read.shift", "anon_read.shift")
  51. static public function shift(x) {
  52. if (Boot.isArray(x)) {
  53. return (x : Array<Dynamic>).shift();
  54. }
  55. return Syntax.callField(x, "shift");
  56. }
  57. @:ifFeature("dynamic_read.pop", "anon_optional_read.pop", "anon_read.pop")
  58. static public function pop(x) {
  59. if (Boot.isArray(x)) {
  60. return (x : Array<Dynamic>).pop();
  61. }
  62. return Syntax.callField(x, "pop");
  63. }
  64. @:ifFeature("dynamic_read.push", "anon_optional_read.push", "anon_read.push")
  65. static public function push(x:Dynamic, e:Dynamic) {
  66. if (Boot.isArray(x)) {
  67. return (x : Array<Dynamic>).push(e);
  68. }
  69. return Syntax.callField(x, "push", e);
  70. }
  71. @:ifFeature("dynamic_read.join", "anon_optional_read.join", "anon_read.join")
  72. static public function join(x, sep) {
  73. if (Boot.isArray(x)) {
  74. return (x : Array<Dynamic>).join(sep);
  75. }
  76. return Syntax.callField(x, "join", sep);
  77. }
  78. @:ifFeature("dynamic_read.filter", "anon_optional_read.filter", "anon_read.filter")
  79. static public function filter(x, f) {
  80. if (Boot.isArray(x)) {
  81. return (x : Array<Dynamic>).filter(f);
  82. }
  83. return Syntax.callField(x, "filter", f);
  84. }
  85. @:ifFeature("dynamic_read.map", "anon_optional_read.map", "anon_read.map")
  86. static public function map(x:Dynamic, f:Dynamic) {
  87. if (Boot.isArray(x)) {
  88. return (x : Array<Dynamic>).map(f);
  89. }
  90. return Syntax.callField(x, "map", f);
  91. }
  92. @:ifFeature("dynamic_read.toUpperCase", "anon_optional_read.toUpperCase", "anon_read.toUpperCase")
  93. static public function toUpperCase(x) {
  94. if (Boot.isString(x)) {
  95. return (x : String).toUpperCase();
  96. }
  97. return Syntax.callField(x, "toUpperCase");
  98. }
  99. @:ifFeature("dynamic_read.toLowerCase", "anon_optional_read.toLowerCase", "anon_read.toLowerCase")
  100. static public function toLowerCase(x) {
  101. if (Boot.isString(x)) {
  102. return (x : String).toLowerCase();
  103. }
  104. return Syntax.callField(x, "toLowerCase");
  105. }
  106. @:ifFeature("dynamic_read.split", "anon_optional_read.split", "anon_read.split")
  107. static public function split(x:Dynamic, delimiter:String) {
  108. if (Boot.isString(x)) {
  109. return (x : String).split(delimiter);
  110. }
  111. return Syntax.callField(x, "split", delimiter);
  112. }
  113. @:ifFeature("dynamic_read.length", "anon_optional_read.length", "anon_read.length")
  114. static public function length(x:Dynamic) {
  115. if (Boot.isString(x)) {
  116. return (x : String).length;
  117. } else if (Boot.isArray(x)) {
  118. return (x : Array<Dynamic>).length;
  119. }
  120. return Syntax.field(x, "length");
  121. }
  122. @:ifFeature("binop_>>>")
  123. static public function rshift(val:Int, n:Int) {
  124. return Syntax.binop(Syntax.binop(val, "%", Syntax.code("0x100000000")), ">>", n);
  125. }
  126. @:ifFeature("binop_%")
  127. static public function modf(a:Float, b:Float) {
  128. return Syntax.code("float('nan') if (b == 0.0) else a % b if a >= 0 else -(-a % b)");
  129. }
  130. @:ifFeature("binop_%")
  131. static public function mod(a:Int, b:Int) {
  132. return Syntax.code("a % b if a >= 0 else -(-a % b)");
  133. }
  134. @:ifFeature("dynamic_array_read")
  135. static public function arrayGet<T>(a:Dynamic, i:Int):Dynamic {
  136. if (Boot.isArray(a)) {
  137. return ArrayImpl._get(a, i);
  138. } else {
  139. return Syntax.arrayAccess(a, i);
  140. }
  141. }
  142. @:ifFeature("dynamic_array_write")
  143. static public function arraySet(a:Dynamic, i:Int, v:Dynamic) {
  144. if (Boot.isArray(a)) {
  145. return ArrayImpl._set(a, i, v);
  146. } else {
  147. Syntax.assign(Syntax.arrayAccess(a, i), v);
  148. return v;
  149. }
  150. }
  151. @:ifFeature("python._KwArgs.KwArgs_Impl_.fromT")
  152. static public function mapKwArgs(a:{}, v:Dict<String, String>) {
  153. var a = python.Lib.dictAsAnon(python.Lib.anonToDict(a));
  154. for (k in v.keys()) {
  155. var val = v.get(k);
  156. if (Syntax.code('{0}._hx_hasattr({1})', a, k)) {
  157. var x = UBuiltins.getattr(a, k);
  158. UBuiltins.setattr(a, val, x);
  159. UBuiltins.delattr(a, k);
  160. }
  161. }
  162. return a;
  163. }
  164. @:ifFeature("python._KwArgs.KwArgs_Impl_.toDictHelper")
  165. static public function reverseMapKwArgs(a:Dict<String, Dynamic>, v:Dict<String, String>) {
  166. var a = a.copy();
  167. for (k in v.keys()) {
  168. var val = v.get(k);
  169. if (a.hasKey(val)) {
  170. var x = a.get(val, null);
  171. a.set(k, x);
  172. a.remove(val);
  173. }
  174. }
  175. return a;
  176. }
  177. }