HxOverrides.hx 6.4 KB

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