-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfjs-runtime.js
192 lines (154 loc) · 4.93 KB
/
fjs-runtime.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Generated by CoffeeScript 1.6.2
/*
File: fjs-runtime.cof -- Module used by every compiled fjs file
coffee -c fjs-runtime.cof
*/
(function() {
var Context, Frame,
__slice = [].slice;
Frame = (function() {
function Frame(codeFuncSegs, segIdx, stack, args) {
this.codeFuncSegs = codeFuncSegs;
this.segIdx = segIdx != null ? segIdx : 0;
this.stack = stack != null ? stack : [];
this.args = args != null ? args : [];
}
Frame.prototype.clone = function() {
return new Frame(this.codeFuncSegs, this.segIdx, this.stack.slice(0, this.args.slice(0)));
};
return Frame;
})();
Context = (function() {
function Context(curFrame) {
this.curFrame = curFrame != null ? curFrame : null;
this.frames = [];
this.callbacksPending = 0;
}
Context.prototype.clone = function() {
var newCtxt;
return newCtxt = new Context(this.curFrame.clone());
};
Context.prototype.stack = function() {
return this.curFrame.stack;
};
Context.prototype.moveArgsToStack = function(n) {
var frame2;
if (!(frame2 = this.frames.slice(-1)[0])) {
return;
}
if (!n) {
this.curFrame.stack = frame2.args.concat(this.curFrame.stack);
return frame2.args = [];
} else {
return this.curFrame.stack = frame2.args.splice(0, n).concat(this.curFrame.stack);
}
};
Context.prototype.setArgs = function(args) {
return this.curFrame.args = Array.prototype.slice.call(args);
};
Context.prototype.pop = function() {
return this.curFrame.stack.shift();
};
Context.prototype.popAll = function() {
var stk;
stk = this.curFrame.stack;
this.curFrame.stack = [];
return stk;
};
Context.prototype.popN = function(n) {
n || (n = this.curFrame.stack.length);
return this.curFrame.stack.splice(0, n);
};
Context.prototype.push = function(v) {
return this.curFrame.stack.unshift(v);
};
Context.prototype["new"] = function(Class, args) {
return (function() {
var construct;
construct = function() {
return Class.apply(this, args);
};
construct.prototype = Class.prototype;
return new construct;
})();
};
Context.prototype.pushArray = function(array) {
return this.curFrame.stack = array.concat(this.curFrame.stack);
};
Context.prototype.pushReturnValue = function(val) {
if (typeof val === 'undefined') {
return;
}
if (val instanceof Array) {
return this.pushArray(val);
} else if (toString.call(val) === '[object Arguments]') {
return this.pushArray(Array.prototype.slice(call(val)));
} else {
return this.curFrame.stack.unshift(val);
}
};
Context.prototype.pushArgsAndExec = function(f, n) {
if (n == null) {
n = this.curFrame.stack.length;
}
this.overrideDefault = true;
this.pushReturnValue(f.apply(this, this.curFrame.stack.splice(0, n)));
return delete this.overrideDefault;
};
Context.prototype.execOrPush = function(word) {
var stk;
if (typeof word === 'function') {
stk = this.curFrame.stack;
this.curFrame.stack = [];
return this.pushReturnValue(word.apply(this, stk));
} else {
return this.push(word);
}
};
Context.prototype.pushCB = function(debugFunc) {
this.curFrame.stack.unshift(this._callback.bind(this, debugFunc));
return this.callbacksPending++;
};
Context.prototype._callback = function() {
var args, ctxt, debugFunc;
debugFunc = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
if (--this.callbacksPending > 0) {
return;
}
ctxt = (this.callbacksPending === 0 ? this : ctxt = this.savedCtxt);
this.savedCtxt = ctxt.clone();
ctxt.curFrame.stack = args.concat(ctxt.curFrame.stack);
if (debugFunc) {
console.log();
debugFunc.call(ctxt, '<callback>');
}
return ctxt._run();
};
Context.prototype.funcCall = function(debugFunc, segments) {
this.frames.push(this.curFrame);
this.curFrame = new Frame(segments);
if (debugFunc) {
debugFunc.call(this, '(');
}
return this._run();
};
Context.prototype._run = function(args) {
var _ref;
return (_ref = this.curFrame.codeFuncSegs[this.curFrame.segIdx++]) != null ? _ref.call(this) : void 0;
};
Context.prototype.wait = function() {
if (this.callbacksPending < 1) {
return this._run();
}
};
Context.prototype.funcReturn = function() {
var stack;
stack = this.curFrame.stack;
if ((this.curFrame = this.frames.pop())) {
return this.curFrame.stack = stack.concat(this.curFrame.args, this.curFrame.stack);
}
};
return Context;
})();
module.exports = new Context;
}).call(this);