forked from skulpt/skulpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enumerate.js
61 lines (52 loc) · 1.76 KB
/
enumerate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* @constructor
* @param {Object} iterable
* @param {number=} start
* @extends Sk.builtin.object
*/
Sk.builtin.enumerate = function (iterable, start) {
var it;
if (!(this instanceof Sk.builtin.enumerate)) {
return new Sk.builtin.enumerate(iterable, start);
}
Sk.builtin.pyCheckArgs("enumerate", arguments, 1, 2);
if (!Sk.builtin.checkIterable(iterable)) {
throw new Sk.builtin.TypeError("'" + Sk.abstr.typeName(iterable) + "' object is not iterable");
}
if (start !== undefined) {
if (!Sk.misceval.isIndex(start)) {
throw new Sk.builtin.TypeError("'" + Sk.abstr.typeName(start) + "' object cannot be interpreted as an index");
} else {
start = Sk.misceval.asIndex(start);
}
}
else {
start = 0;
}
it = iterable.tp$iter();
this.tp$iter = function () {
return this;
};
this.$index = start;
this.tp$iternext = function () {
// todo; StopIteration
var idx;
var next = it.tp$iternext();
if (next === undefined) {
return undefined;
}
idx = Sk.builtin.assk$(this.$index++, Sk.builtin.nmber.int$);
return new Sk.builtin.tuple([idx, next]);
};
this.__class__ = Sk.builtin.enumerate;
return this;
};
Sk.builtin.enumerate.prototype.tp$name = "enumerate";
Sk.builtin.enumerate.prototype.ob$type = Sk.builtin.type.makeIntoTypeObj("enumerate", Sk.builtin.enumerate);
Sk.builtin.enumerate.prototype.tp$getattr = Sk.builtin.object.prototype.GenericGetAttr;
Sk.builtin.enumerate.prototype["__iter__"] = new Sk.builtin.func(function (self) {
return self.tp$iter();
});
Sk.builtin.enumerate.prototype["next"] = new Sk.builtin.func(function (self) {
return self.tp$iternext();
});