-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs_fcns.c
337 lines (252 loc) · 7.79 KB
/
js_fcns.c
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include "js.h"
#include "js_malloc.h"
// closures
value_t newClosure( fcnDeclNode *fd, environment_t *env) {
uint32_t depth = 1, idx;
closure_t *closure;
value_t v;
if (env->closure)
depth += env->closure->depth;
closure = js_alloc(sizeof(closure_t) + sizeof(scope_t) * depth, true);
closure->scope[0] = js_alloc(sizeof(scope_t) + sizeof(value_t) * env->scope->count, true);
closure->scope[0]->symbols = env->scope->symbols;
closure->scope[0]->count = env->scope->count;
closure->scope[0]->frame = env->topFrame;
for (idx = 1; idx < env->scope->count; idx++)
replaceSlot(closure->scope[0]->values + idx, env->scope->values[idx]);
incrScopeCnt(closure->scope[0]);
for (idx=1; idx < depth; idx++) {
closure->scope[idx] = env->closure->scope[idx-1];
incrScopeCnt(closure->scope[idx]);
}
closure->obj = newObject(vt_closure);
incrRefCnt(closure->obj);
closure->protoObj = newObject(vt_object);
incrRefCnt(closure->protoObj);
closure->symbols = &fd->symbols;
closure->table = env->table;
closure->depth = depth;
closure->fd = fd;
v.bits = vt_closure;
v.closure = closure;
v.refcount = 1;
v.objvalue = 1;
return v;
}
// function expressions
value_t eval_fcnexpr (Node *a, environment_t *env) {
fcnDeclNode *fd = (fcnDeclNode *)a;
return newClosure(fd, env);
}
// do function call (or pipeline stage call)
value_t fcnCall (value_t fcnClosure, value_t args, value_t thisVal, bool rtnVal, environment_t *env) {
closure_t *closure = fcnClosure.closure;
fcnDeclNode *fd = closure->fd;
environment_t newEnv[1];
frame_t *frame;
scope_t *scope;
array_t *aval;
uint32_t idx;
value_t v;
memset (newEnv, 0, sizeof(environment_t));
frame = js_alloc(sizeof(value_t) * fd->symbols.frameIdx + sizeof(frame_t), true);
frame->count = fd->symbols.frameIdx;
frame->nextThis.bits = vt_undef;
replaceSlot(&frame->thisVal, thisVal);
replaceSlot(&frame->arguments, args);
scope = js_alloc(sizeof(scope_t) + sizeof(value_t) * fd->symbols.scopeCnt, true);
scope->count = fd->symbols.scopeCnt;
scope->frame = frame;
incrScopeCnt(scope);
aval = js_dbaddr(args, NULL);
for (idx = 0; idx < fd->nparams && idx < (uint32_t)(vec_cnt(aval->valuePtr)); idx++)
replaceSlot(&frame->values[idx + 1], aval->valuePtr[idx]);
// prepare new environment
newEnv->first = findFirstNode(closure->table, fd->body);
newEnv->closure = fcnClosure.closure;
newEnv->table = closure->table;
newEnv->topFrame = frame;
newEnv->scope = scope;
/*
if (env) {
newEnv->timestamp = env->timestamp;
*newEnv->txnBits = *env->txnBits;
}
*/
installFcns(fd->symbols.childFcns, newEnv);
// install function expression closure
if (fd->hdr->type == node_fcnexpr)
if (fd->name) {
value_t slot = dispatch(fd->name, newEnv);
replaceValue(slot, fcnClosure);
}
// pre-load return value with thisVal
if (rtnVal)
replaceSlot(frame->values, thisVal);
dispatch(fd->body, newEnv);
v = frame->values[0];
// passback global environment values
/*
if (env) {
*env->txnBits = *newEnv->txnBits;
env->timestamp = newEnv->timestamp;
}
*/
abandonScope(scope);
decrRefCnt(v);
return v;
}
// return statement
value_t eval_return(Node *a, environment_t *env)
{
exprNode *en = (exprNode *)a;
value_t v;
if (en->expr)
v = dispatch(en->expr, env);
else
v.bits = vt_undef;
replaceSlot(env->topFrame->values, v);
v.bits = vt_control;
v.ctl = a->aux;
return v;
}
// function calls, pipeline stage, property fcn
value_t execbuiltin(fcnCallNode *fc, value_t fcn, environment_t *env);
value_t eval_fcncall(Node *a, environment_t *env) {
fcnCallNode *fc = (fcnCallNode *)a;
value_t fcn, v, thisVal;
bool returnFlag = false;
uint32_t argList;
value_t nextThis;
value_t args;
listNode *ln;
// process arg list
args = newArray(array_value, fc->argCnt);
if ((argList = fc->args)) do {
ln = (listNode *)(env->table + argList);
v = dispatch(ln->elem, env);
incrRefCnt(v);
vec_push(args.aval->valuePtr, v);
argList -= sizeof(listNode) / sizeof(Node);
} while (ln->hdr->type == node_list);
// prepare to calc new this value
nextThis = env->topFrame->nextThis;
env->topFrame->nextThis.bits = vt_undef;
// evaluate a closure or internal property fcn
fcn = dispatch(fc->name, env);
if (fcn.type == vt_lval)
fcn = *fcn.lval;
if (fcn.type == vt_builtin) {
v = execbuiltin(fc, fcn, env);
abandonValue(args);
return v;
}
if (fcn.type == vt_propfcn) {
v = callFcnFcn(fcn, args.aval->valuePtr, env);
abandonValue(args);
return v;
}
if (fcn.type != vt_closure) {
firstNode *fn = findFirstNode(env->table, (uint32_t)(a - env->table));
symNode *sym = (symNode *)(env->table + fc->name);
stringNode *sn = (stringNode *)(env->table + sym->name);
fprintf(stderr, "%s not function closure: %s line: %d\n", sn->str.val, fn->script, (int)a->lineNo);
exit(1);
}
if (fc->hdr->aux == aux_newobj) {
thisVal = newObject(vt_object);
object_t *oval = thisVal.addr;
oval->protoChain = fcn.closure->protoObj;
incrRefCnt(oval->protoChain);
returnFlag = true;
}
else {
thisVal = env->topFrame->nextThis;
returnFlag = false;
}
// args will be pushed into a new frame by callFcn
v = fcnCall(fcn, args, thisVal, returnFlag, env);
env->topFrame->nextThis = nextThis;
abandonValue(fcn);
return v;
}
// make function closures for functions defined in function
void installFcns(uint32_t decl, environment_t *env) {
while (decl) {
fcnDeclNode *fd = (fcnDeclNode *)(env->table + decl);
symNode *sym = (symNode *)(env->table + fd->name);
value_t v = newClosure(fd, env);
incrRefCnt(v);
replaceSlot(&env->topFrame->values[sym->frameIdx], v);
decl = fd->next;
}
}
// execute collection of scripts
double getCpuTime(int type);
void installBuiltIns(frame_t *frame, environment_t *env);
void execScripts(Node *table, uint32_t size, value_t args, symtab_t *symbols, environment_t *oldEnv) {
uint32_t start = 0, depth = 0, idx;
environment_t env[1];
closure_t *closure;
symtab_t block;
frame_t *frame;
firstNode *fn;
value_t v;
if (oldEnv)
depth = oldEnv->closure->depth + 1;
// hoist and assign symbols decls
memset (&block, 0, sizeof(block));
compileScripts(size, table, symbols, &block);
// build new frame
frame = js_alloc(sizeof(value_t) * symbols->frameIdx + sizeof(frame_t), true);
replaceSlot(&frame->arguments, args);
frame->count = symbols->frameIdx;
frame->nextThis.bits = vt_undef;
frame->thisVal.bits = vt_undef;
// allocate the top level closure
closure = js_alloc(sizeof(closure_t) + sizeof(scope_t) * depth, true);
closure->scope[0] = js_alloc(sizeof(scope_t) + sizeof(value_t) * symbols->scopeCnt, true);
closure->scope[0]->frame = oldEnv ? oldEnv->topFrame : NULL;
closure->scope[0]->count = symbols->scopeCnt;
closure->obj = newObject(vt_closure);
closure->protoObj = newObject(vt_object);
incrRefCnt(closure->protoObj);
incrScopeCnt(closure->scope[0]);
closure->symbols = symbols;
closure->table = table;
closure->depth = depth;
if (oldEnv)
closure->scope[0]->frame = oldEnv->topFrame;
for (idx=1; idx < depth; idx++) {
closure->scope[idx] = oldEnv->closure->scope[idx-1];
incrScopeCnt(closure->scope[idx]);
}
memset (env, 0, sizeof(environment_t));
env->scope = closure->scope[0];
env->closure = closure;
env->topFrame = frame;
env->table = table;
if (!oldEnv)
installBuiltIns(frame, env);
installFcns(symbols->childFcns, env);
// run each script in the table
while (start < size) {
fn = (firstNode *)(table + start);
double strtTime, elapsed;
env->first = fn;
start += fn->moduleSize;
strtTime = getCpuTime(0);
dispatch(fn->begin, env);
elapsed = getCpuTime(0) - strtTime;
if (debug)
fprintf (stderr, "Execution: %dm%.6fs %s \n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60, fn->script);
}
/*
if (env->timestamp)
js_free(env->timestamp);
*/
// abandon global scope
v.bits = vt_closure;
v.closure = closure;
deleteValue(v);
}