tuple.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /**
  2. * @constructor
  3. * @param {Array.<Object>|Object} L
  4. */
  5. Sk.builtin.tuple = function (L) {
  6. var it, i;
  7. if (!(this instanceof Sk.builtin.tuple)) {
  8. return new Sk.builtin.tuple(L);
  9. }
  10. if (L === undefined) {
  11. L = [];
  12. }
  13. if (Object.prototype.toString.apply(L) === "[object Array]") {
  14. this.v = L;
  15. } else {
  16. if (Sk.builtin.checkIterable(L)) {
  17. this.v = [];
  18. for (it = Sk.abstr.iter(L), i = it.tp$iternext(); i !== undefined; i = it.tp$iternext()) {
  19. this.v.push(i);
  20. }
  21. } else {
  22. throw new Sk.builtin.TypeError("expecting Array or iterable");
  23. }
  24. }
  25. this.__class__ = Sk.builtin.tuple;
  26. this["v"] = this.v;
  27. return this;
  28. };
  29. Sk.abstr.setUpInheritance("tuple", Sk.builtin.tuple, Sk.builtin.seqtype);
  30. Sk.builtin.tuple.prototype["$r"] = function () {
  31. var ret;
  32. var i;
  33. var bits;
  34. if (this.v.length === 0) {
  35. return new Sk.builtin.str("()");
  36. }
  37. bits = [];
  38. for (i = 0; i < this.v.length; ++i) {
  39. bits[i] = Sk.misceval.objectRepr(this.v[i]).v;
  40. }
  41. ret = bits.join(", ");
  42. if (this.v.length === 1) {
  43. ret += ",";
  44. }
  45. return new Sk.builtin.str("(" + ret + ")");
  46. };
  47. Sk.builtin.tuple.prototype.mp$subscript = function (index) {
  48. var ret;
  49. var i;
  50. if (Sk.misceval.isIndex(index)) {
  51. i = Sk.misceval.asIndex(index);
  52. if (i !== undefined) {
  53. if (i < 0) {
  54. i = this.v.length + i;
  55. }
  56. if (i < 0 || i >= this.v.length) {
  57. throw new Sk.builtin.IndexError("tuple index out of range");
  58. }
  59. return this.v[i];
  60. }
  61. } else if (index instanceof Sk.builtin.slice) {
  62. ret = [];
  63. index.sssiter$(this, function (i, wrt) {
  64. ret.push(wrt.v[i]);
  65. });
  66. return new Sk.builtin.tuple(ret);
  67. }
  68. throw new Sk.builtin.TypeError("tuple indices must be integers, not " + Sk.abstr.typeName(index));
  69. };
  70. // todo; the numbers and order are taken from python, but the answer's
  71. // obviously not the same because there's no int wrapping. shouldn't matter,
  72. // but would be nice to make the hash() values the same if it's not too
  73. // expensive to simplify tests.
  74. Sk.builtin.tuple.prototype.tp$hash = function () {
  75. var y;
  76. var i;
  77. var mult = 1000003;
  78. var x = 0x345678;
  79. var len = this.v.length;
  80. for (i = 0; i < len; ++i) {
  81. y = Sk.builtin.hash(this.v[i]).v;
  82. if (y === -1) {
  83. return new Sk.builtin.int_(-1);
  84. }
  85. x = (x ^ y) * mult;
  86. mult += 82520 + len + len;
  87. }
  88. x += 97531;
  89. if (x === -1) {
  90. x = -2;
  91. }
  92. return new Sk.builtin.int_(x | 0);
  93. };
  94. Sk.builtin.tuple.prototype.sq$repeat = function (n) {
  95. var j;
  96. var i;
  97. var ret;
  98. n = Sk.misceval.asIndex(n);
  99. ret = [];
  100. for (i = 0; i < n; ++i) {
  101. for (j = 0; j < this.v.length; ++j) {
  102. ret.push(this.v[j]);
  103. }
  104. }
  105. return new Sk.builtin.tuple(ret);
  106. };
  107. Sk.builtin.tuple.prototype.nb$multiply = Sk.builtin.tuple.prototype.sq$repeat;
  108. Sk.builtin.tuple.prototype.nb$inplace_multiply = Sk.builtin.tuple.prototype.sq$repeat;
  109. Sk.builtin.tuple.prototype.__iter__ = new Sk.builtin.func(function (self) {
  110. Sk.builtin.pyCheckArgs("__iter__", arguments, 1, 1);
  111. return new Sk.builtin.tuple_iter_(self);
  112. });
  113. Sk.builtin.tuple.prototype.tp$iter = function () {
  114. return new Sk.builtin.tuple_iter_(this);
  115. };
  116. Sk.builtin.tuple.prototype.tp$richcompare = function (w, op) {
  117. //print(" tup rc", JSON.stringify(this.v), JSON.stringify(w), op);
  118. // w not a tuple
  119. var k;
  120. var i;
  121. var wl;
  122. var vl;
  123. var v;
  124. if (!w.__class__ ||
  125. !Sk.misceval.isTrue(Sk.builtin.isinstance(w, Sk.builtin.tuple))) {
  126. // shortcuts for eq/not
  127. if (op === "Eq") {
  128. return false;
  129. }
  130. if (op === "NotEq") {
  131. return true;
  132. }
  133. // todo; other types should have an arbitrary order
  134. return false;
  135. }
  136. v = this.v;
  137. w = w.v;
  138. vl = v.length;
  139. wl = w.length;
  140. for (i = 0; i < vl && i < wl; ++i) {
  141. k = Sk.misceval.richCompareBool(v[i], w[i], "Eq");
  142. if (!k) {
  143. break;
  144. }
  145. }
  146. if (i >= vl || i >= wl) {
  147. // no more items to compare, compare sizes
  148. switch (op) {
  149. case "Lt":
  150. return vl < wl;
  151. case "LtE":
  152. return vl <= wl;
  153. case "Eq":
  154. return vl === wl;
  155. case "NotEq":
  156. return vl !== wl;
  157. case "Gt":
  158. return vl > wl;
  159. case "GtE":
  160. return vl >= wl;
  161. default:
  162. goog.asserts.fail();
  163. }
  164. }
  165. // we have an item that's different
  166. // shortcuts for eq/not
  167. if (op === "Eq") {
  168. return false;
  169. }
  170. if (op === "NotEq") {
  171. return true;
  172. }
  173. // or, compare the differing element using the proper operator
  174. //print(" tup rcb end", i, v[i] instanceof Sk.builtin.str, JSON.stringify(v[i]), w[i] instanceof Sk.builtin.str, JSON.stringify(w[i]), op);
  175. return Sk.misceval.richCompareBool(v[i], w[i], op);
  176. };
  177. Sk.builtin.tuple.prototype.sq$concat = function (other) {
  178. var msg;
  179. if (other.__class__ != Sk.builtin.tuple) {
  180. msg = "can only concatenate tuple (not \"";
  181. msg += Sk.abstr.typeName(other) + "\") to tuple";
  182. throw new Sk.builtin.TypeError(msg);
  183. }
  184. return new Sk.builtin.tuple(this.v.concat(other.v));
  185. };
  186. Sk.builtin.tuple.prototype.sq$contains = function (ob) {
  187. var it, i;
  188. for (it = this.tp$iter(), i = it.tp$iternext(); i !== undefined; i = it.tp$iternext()) {
  189. if (Sk.misceval.richCompareBool(i, ob, "Eq")) {
  190. return true;
  191. }
  192. }
  193. return false;
  194. };
  195. Sk.builtin.tuple.prototype.nb$add = Sk.builtin.tuple.prototype.sq$concat;
  196. Sk.builtin.tuple.prototype.nb$inplace_add = Sk.builtin.tuple.prototype.sq$concat;
  197. Sk.builtin.tuple.prototype.sq$length = function () {
  198. return this.v.length;
  199. };
  200. Sk.builtin.tuple.prototype["index"] = new Sk.builtin.func(function (self, item) {
  201. var i;
  202. var len = self.v.length;
  203. var obj = self.v;
  204. for (i = 0; i < len; ++i) {
  205. if (Sk.misceval.richCompareBool(obj[i], item, "Eq")) {
  206. return new Sk.builtin.int_(i);
  207. }
  208. }
  209. throw new Sk.builtin.ValueError("tuple.index(x): x not in tuple");
  210. });
  211. Sk.builtin.tuple.prototype["count"] = new Sk.builtin.func(function (self, item) {
  212. var i;
  213. var len = self.v.length;
  214. var obj = self.v;
  215. var count = 0;
  216. for (i = 0; i < len; ++i) {
  217. if (Sk.misceval.richCompareBool(obj[i], item, "Eq")) {
  218. count += 1;
  219. }
  220. }
  221. return new Sk.builtin.int_(count);
  222. });
  223. goog.exportSymbol("Sk.builtin.tuple", Sk.builtin.tuple);
  224. /**
  225. * @constructor
  226. * @param {Object} obj
  227. */
  228. Sk.builtin.tuple_iter_ = function (obj) {
  229. if (!(this instanceof Sk.builtin.tuple_iter_)) {
  230. return new Sk.builtin.tuple_iter_(obj);
  231. }
  232. this.$index = 0;
  233. this.$obj = obj.v.slice();
  234. this.sq$length = this.$obj.length;
  235. this.tp$iter = this;
  236. this.tp$iternext = function () {
  237. if (this.$index >= this.sq$length) {
  238. return undefined;
  239. }
  240. return this.$obj[this.$index++];
  241. };
  242. this.$r = function () {
  243. return new Sk.builtin.str("tupleiterator");
  244. };
  245. return this;
  246. };
  247. Sk.abstr.setUpInheritance("tupleiterator", Sk.builtin.tuple_iter_, Sk.builtin.object);
  248. Sk.builtin.tuple_iter_.prototype.__class__ = Sk.builtin.tuple_iter_;
  249. Sk.builtin.tuple_iter_.prototype.__iter__ = new Sk.builtin.func(function (self) {
  250. return self;
  251. });
  252. Sk.builtin.tuple_iter_.prototype["next"] = new Sk.builtin.func(function (self) {
  253. var ret = self.tp$iternext();
  254. if (ret === undefined) {
  255. throw new Sk.builtin.StopIteration();
  256. }
  257. return ret;
  258. });