forked from skulpt/skulpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.js
127 lines (115 loc) · 4.02 KB
/
generator.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* @constructor
* @param {Function} code javascript code object for the function
* @param {Object} globals where this function was defined
* @param {Object} args arguments to the original call (stored into locals for
* the generator to reenter)
* @param {Object=} closure dict of free variables
* @param {Object=} closure2 another dict of free variables that will be
* merged into 'closure'. there's 2 to simplify generated code (one is $free,
* the other is $cell)
*
* co_varnames and co_name come from generated code, must access as dict.
*/
Sk.builtin.generator = function (code, globals, args, closure, closure2) {
var k;
var i;
if (!code) {
return;
} // ctor hack
this.func_code = code;
this.func_globals = globals || null;
this["gi$running"] = false;
this["gi$resumeat"] = 0;
this["gi$sentvalue"] = undefined;
this["gi$locals"] = {};
this["gi$cells"] = {};
if (args.length > 0) {
// store arguments into locals because they have to be maintained
// too. 'fast' var lookups are locals in generator functions.
for (i = 0; i < code["co_varnames"].length; ++i) {
this["gi$locals"][code["co_varnames"][i]] = args[i];
}
}
if (closure2 !== undefined) {
// todo; confirm that modification here can't cause problems
for (k in closure2) {
closure[k] = closure2[k];
}
}
//print(JSON.stringify(closure));
this.func_closure = closure;
return this;
};
goog.exportSymbol("Sk.builtin.generator", Sk.builtin.generator);
Sk.builtin.generator.prototype.tp$getattr = Sk.builtin.object.prototype.GenericGetAttr;
Sk.builtin.generator.prototype.tp$iter = function () {
return this;
};
Sk.builtin.generator.prototype.tp$iternext = function (canSuspend, yielded) {
var ret;
var args;
var self = this;
this["gi$running"] = true;
if (yielded === undefined) {
yielded = null;
}
this["gi$sentvalue"] = yielded;
// note: functions expect 'this' to be globals to avoid having to
// slice/unshift onto the main args
args = [ this ];
if (this.func_closure) {
args.push(this.func_closure);
}
ret = this.func_code.apply(this.func_globals, args);
return (function finishIteration(ret) {
if (ret instanceof Sk.misceval.Suspension) {
if (canSuspend) {
return new Sk.misceval.Suspension(finishIteration, ret);
} else {
ret = Sk.misceval.retryOptionalSuspensionOrThrow(ret);
}
}
//print("ret", JSON.stringify(ret));
self["gi$running"] = false;
goog.asserts.assert(ret !== undefined);
if (ret !== Sk.builtin.none.none$) {
// returns a pair: resume target and yielded value
self["gi$resumeat"] = ret[0];
ret = ret[1];
}
else {
// todo; StopIteration
return undefined;
}
//print("returning:", JSON.stringify(ret));
return ret;
})(ret);
};
Sk.builtin.generator.prototype["next"] = new Sk.builtin.func(function (self) {
return self.tp$iternext(true);
});
Sk.builtin.generator.prototype.ob$type = Sk.builtin.type.makeIntoTypeObj("generator", Sk.builtin.generator);
Sk.builtin.generator.prototype["$r"] = function () {
return new Sk.builtin.str("<generator object " + this.func_code["co_name"].v + ">");
};
Sk.builtin.generator.prototype["send"] = new Sk.builtin.func(function (self, value) {
return self.tp$iternext(true, value);
});
/**
* Creates a generator with the specified next function and additional
* instance data. Useful in Javascript-implemented modules to implement
* the __iter__ method.
*/
Sk.builtin.makeGenerator = function (next, data) {
var key;
var gen = new Sk.builtin.generator(null, null, null);
gen.tp$iternext = next;
for (key in data) {
if (data.hasOwnProperty(key)) {
gen[key] = data[key];
}
}
return gen;
};
goog.exportSymbol("Sk.builtin.makeGenerator", Sk.builtin.makeGenerator);