HxOverrides.hx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (C)2005-2012 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.lib.Builtins;
  24. import python.Syntax;
  25. import python.Syntax.pythonCode in py;
  26. @:noDoc
  27. @:native("HxOverrides")
  28. @:access(python.internal.ArrayImpl)
  29. @:access(python.Boot)
  30. class HxOverrides {
  31. // this two cases iterator and shift are like all methods in String and Array and are already handled in Reflect
  32. // we need to modify the transformer to call Reflect directly
  33. @:ifFeature("dynamic_read.iterator", "anon_optional_read.iterator", "anon_read.iterator")
  34. static public function iterator(x) {
  35. if (Boot.isArray(x)) {
  36. return (x:Array<Dynamic>).iterator();
  37. }
  38. return Syntax.callField(x, "iterator");
  39. }
  40. @:ifFeature("dynamic_binop_==", "dynamic_binop_!=")
  41. static function eq( a:Dynamic, b:Dynamic ) : Bool {
  42. if (Boot.isArray(a) || Boot.isArray(b)) {
  43. return Syntax.pythonCode('a is b');
  44. }
  45. return Syntax.binop(a, "==", b);
  46. }
  47. @:ifFeature("unsafe_string_concat")
  48. static function stringOrNull (s:String):String {
  49. return if (s == null) "null" else s;
  50. }
  51. @:ifFeature("dynamic_read.shift", "anon_optional_read.shift", "anon_read.shift")
  52. static public function shift(x) {
  53. if (Boot.isArray(x)) {
  54. return (x:Array<Dynamic>).shift();
  55. }
  56. return Syntax.callField(x, "shift");
  57. }
  58. @:ifFeature("dynamic_read.pop", "anon_optional_read.pop", "anon_read.pop")
  59. static public function pop(x) {
  60. if (Boot.isArray(x)) {
  61. return (x:Array<Dynamic>).pop();
  62. }
  63. return Syntax.callField(x, "pop");
  64. }
  65. @:ifFeature("dynamic_read.push", "anon_optional_read.push", "anon_read.push")
  66. static public function push(x, e) {
  67. if (Boot.isArray(x)) {
  68. return (x:Array<Dynamic>).push(e);
  69. }
  70. return Syntax.callField(x, "push", e);
  71. }
  72. @:ifFeature("dynamic_read.join", "anon_optional_read.join", "anon_read.join")
  73. static public function join(x, sep) {
  74. if (Boot.isArray(x)) {
  75. return (x:Array<Dynamic>).join(sep);
  76. }
  77. return Syntax.callField(x, "join", sep);
  78. }
  79. @:ifFeature("dynamic_read.filter", "anon_optional_read.filter", "anon_read.filter")
  80. static public function filter(x, f) {
  81. if (Boot.isArray(x)) {
  82. return (x:Array<Dynamic>).filter(f);
  83. }
  84. return Syntax.callField(x, "filter", f);
  85. }
  86. @:ifFeature("dynamic_read.map", "anon_optional_read.map", "anon_read.map")
  87. static public function map(x, f) {
  88. if (Boot.isArray(x)) {
  89. return (x:Array<Dynamic>).map(f);
  90. }
  91. return Syntax.callField(x, "map", f);
  92. }
  93. @:ifFeature("dynamic_read.toUpperCase", "anon_optional_read.toUpperCase", "anon_read.toUpperCase")
  94. static public function toUpperCase(x) {
  95. if (Boot.isString(x)) {
  96. return (x:String).toUpperCase();
  97. }
  98. return Syntax.callField(x, "toUpperCase");
  99. }
  100. @:ifFeature("dynamic_read.toLowerCase", "anon_optional_read.toLowerCase", "anon_read.toLowerCase")
  101. static public function toLowerCase(x) {
  102. if (Boot.isString(x)) {
  103. return (x:String).toLowerCase();
  104. }
  105. return Syntax.callField(x, "toLowerCase");
  106. }
  107. @:ifFeature("binop_>>>")
  108. static public function rshift(val:Int, n:Int) {
  109. return Syntax.binop(Syntax.binop(val, "%", Syntax.pythonCode("0x100000000")), ">>", n);
  110. }
  111. @:ifFeature("binop_%")
  112. static public function modf(a:Float, b:Float) {
  113. return Syntax.pythonCode("float('nan') if (b == 0.0) else a % b if a > 0 else -(-a % b)");
  114. }
  115. @:ifFeature("dynamic_array_read")
  116. static public function arrayGet<T>(a:Dynamic, i:Int):Dynamic {
  117. if (Boot.isArray(a)) {
  118. return ArrayImpl._get(a, i);
  119. } else {
  120. return Syntax.arrayAccess(a, i);
  121. }
  122. }
  123. @:ifFeature("dynamic_array_write")
  124. static public function arraySet(a:Dynamic, i:Int, v:Dynamic) {
  125. if (Boot.isArray(a)) {
  126. return ArrayImpl._set(a, i, v);
  127. } else {
  128. Syntax.assign(Syntax.arrayAccess(a, i), v);
  129. return v;
  130. }
  131. }
  132. @:ifFeature("python._KwArgs.KwArgs_Impl_.fromT")
  133. static public function mapKwArgs(a:{}, v:Dict<String,String>)
  134. {
  135. for (k in v.keys()) {
  136. var val = v.get(k);
  137. if (Builtins.hasattr(a, k)) {
  138. var x = Builtins.getattr(a, k);
  139. Builtins.setattr(a, val, x);
  140. Builtins.delattr(a, k);
  141. }
  142. }
  143. return a;
  144. }
  145. }