]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/code.c
Canvas: fix property inheritance.
[simgear.git] / simgear / nasal / code.c
1 #include <stdio.h>
2 #include <stdarg.h>
3 #include <string.h>
4 #include "nasal.h"
5 #include "code.h"
6
7 ////////////////////////////////////////////////////////////////////////
8 // Debugging stuff. ////////////////////////////////////////////////////
9 ////////////////////////////////////////////////////////////////////////
10 //#define INTERPRETER_DUMP
11 #if !defined(INTERPRETER_DUMP)
12 # define DBG(expr) /* noop */
13 #else
14 # define DBG(expr) expr
15 # include <stdio.h>
16 # include <stdlib.h>
17 #endif
18 char* opStringDEBUG(int op);
19 void printOpDEBUG(int ip, int op);
20 void printStackDEBUG(naContext ctx);
21 ////////////////////////////////////////////////////////////////////////
22
23 struct Globals* globals = 0;
24
25 static naRef bindFunction(naContext ctx, struct Frame* f, naRef code);
26
27 #define ERR(c, msg) naRuntimeError((c),(msg))
28 void naRuntimeError(naContext c, const char* fmt, ...)
29 {
30     va_list ap;
31     va_start(ap, fmt);
32     vsnprintf(c->error, sizeof(c->error), fmt, ap);
33     va_end(ap);
34     longjmp(c->jumpHandle, 1);
35 }
36
37 void naRethrowError(naContext subc)
38 {
39     strncpy(subc->callParent->error, subc->error, sizeof(subc->error));
40     subc->callParent->dieArg = subc->dieArg;
41     longjmp(subc->callParent->jumpHandle, 1);
42 }
43
44 #define END_PTR ((void*)1)
45 #define IS_END(r) (IS_REF((r)) && PTR((r)).obj == END_PTR)
46 static naRef endToken()
47 {
48     naRef r;
49     SETPTR(r, END_PTR);
50     return r;
51 }
52
53 static int boolify(naContext ctx, naRef r)
54 {
55     if(IS_NUM(r)) return r.num != 0;
56     if(IS_NIL(r) || IS_END(r)) return 0;
57     if(IS_STR(r)) {
58         double d;
59         if(naStr_len(r) == 0) return 0;
60         if(naStr_tonum(r, &d)) return d != 0;
61         else return 1;
62     }
63     ERR(ctx, "non-scalar used in boolean context");
64     return 0;
65 }
66
67 static double numify(naContext ctx, naRef o)
68 {
69     double n;
70     if(IS_NUM(o)) return o.num;
71     else if(IS_NIL(o)) ERR(ctx, "nil used in numeric context");
72     else if(!IS_STR(o)) ERR(ctx, "non-scalar in numeric context");
73     else if(naStr_tonum(o, &n)) return n;
74     else ERR(ctx, "non-numeric string in numeric context");
75     return 0;
76 }
77
78 static naRef stringify(naContext ctx, naRef r)
79 {
80     if(IS_STR(r)) return r;
81     if(IS_NUM(r)) return naStr_fromnum(naNewString(ctx), r.num);
82     ERR(ctx, "non-scalar in string context");
83     return naNil();
84 }
85
86 static int checkVec(naContext ctx, naRef vec, naRef idx)
87 {
88     int i = (int)numify(ctx, idx);
89     if(i < 0) i += naVec_size(vec);
90     if(i < 0 || i >= naVec_size(vec))
91         naRuntimeError(ctx, "vector index %d out of bounds (size: %d)",
92                        i, naVec_size(vec));
93     return i;
94 }
95
96 static int checkStr(naContext ctx, naRef str, naRef idx)
97 {
98     int i = (int)numify(ctx, idx);
99     if(i < 0) i += naStr_len(str);
100     if(i < 0 || i >= naStr_len(str))
101         naRuntimeError(ctx, "string index %d out of bounds (size: %d)",
102                        i, naStr_len(str));
103     return i;
104 }
105
106 static naRef containerGet(naContext ctx, naRef box, naRef key)
107 {
108     naRef result = naNil();
109     if(!IS_SCALAR(key)) ERR(ctx, "container index not scalar");
110     if(IS_HASH(box))
111         naHash_get(box, key, &result);
112     else if(IS_VEC(box))
113         result = naVec_get(box, checkVec(ctx, box, key));
114     else if(IS_STR(box))
115         result = naNum((unsigned char)naStr_data(box)[checkStr(ctx, box, key)]);
116     else
117         ERR(ctx, "extract from non-container");
118     return result;
119 }
120
121 static void containerSet(naContext ctx, naRef box, naRef key, naRef val)
122 {
123     if(!IS_SCALAR(key))   ERR(ctx, "container index not scalar");
124     else if(IS_HASH(box)) naHash_set(box, key, val);
125     else if(IS_VEC(box))  naVec_set(box, checkVec(ctx, box, key), val);
126     else if(IS_STR(box)) {
127         if(PTR(box).str->hashcode)
128             ERR(ctx, "cannot change immutable string");
129         naStr_data(box)[checkStr(ctx, box, key)] = (char)numify(ctx, val);
130     } else ERR(ctx, "insert into non-container");
131 }
132
133 static void initTemps(naContext c)
134 {
135     c->tempsz = 4;
136     c->temps = naAlloc(c->tempsz * sizeof(struct naObj*));
137     c->ntemps = 0;
138 }
139
140 static void initContext(naContext c)
141 {
142     int i;
143     c->fTop = c->opTop = c->markTop = 0;
144     for(i=0; i<NUM_NASAL_TYPES; i++)
145         c->nfree[i] = 0;
146
147     if(c->tempsz > 32) {
148         naFree(c->temps);
149         initTemps(c);
150     }
151
152     c->callParent = 0;
153     c->callChild = 0;
154     c->dieArg = naNil();
155     c->error[0] = 0;
156     c->userData = 0;
157 }
158
159 static void initGlobals()
160 {
161     int i;
162     naContext c;
163     globals = (struct Globals*)naAlloc(sizeof(struct Globals));
164     naBZero(globals, sizeof(struct Globals));
165
166     globals->sem = naNewSem();
167     globals->lock = naNewLock();
168
169     globals->allocCount = 256; // reasonable starting value
170     for(i=0; i<NUM_NASAL_TYPES; i++)
171         naGC_init(&(globals->pools[i]), i);
172     globals->deadsz = 256;
173     globals->ndead = 0;
174     globals->deadBlocks = naAlloc(sizeof(void*) * globals->deadsz);
175
176     // Initialize a single context
177     globals->freeContexts = 0;
178     globals->allContexts = 0;
179     c = naNewContext();
180
181     globals->symbols = naNewHash(c);
182     globals->save = naNewVector(c);
183
184     // Cache pre-calculated "me", "arg" and "parents" scalars
185     globals->meRef = naInternSymbol(naStr_fromdata(naNewString(c), "me", 2));
186     globals->argRef = naInternSymbol(naStr_fromdata(naNewString(c), "arg", 3));
187     globals->parentsRef = naInternSymbol(naStr_fromdata(naNewString(c), "parents", 7));
188
189     naFreeContext(c);
190 }
191
192 naContext naNewContext()
193 {
194     naContext c;
195     if(globals == 0)
196         initGlobals();
197
198     LOCK();
199     c = globals->freeContexts;
200     if(c) {
201         globals->freeContexts = c->nextFree;
202         c->nextFree = 0;
203         UNLOCK();
204         initContext(c);
205     } else {
206         UNLOCK();
207         c = (naContext)naAlloc(sizeof(struct Context));
208         initTemps(c);
209         initContext(c);
210         LOCK();
211         c->nextAll = globals->allContexts;
212         c->nextFree = 0;
213         globals->allContexts = c;
214         UNLOCK();
215     }
216     return c;
217 }
218
219 naContext naSubContext(naContext super)
220 {
221     naContext ctx = naNewContext();
222     if(super->callChild) naFreeContext(super->callChild);
223     ctx->callParent = super;
224     super->callChild = ctx;
225     return ctx;
226 }
227
228 void naFreeContext(naContext c)
229 {
230     c->ntemps = 0;
231     if(c->callChild) naFreeContext(c->callChild);
232     if(c->callParent) c->callParent->callChild = 0;
233     LOCK();
234     c->nextFree = globals->freeContexts;
235     globals->freeContexts = c;
236     UNLOCK();
237 }
238
239 // Note that opTop is incremented separately, to avoid situations
240 // where the "r" expression also references opTop.  The SGI compiler
241 // is known to have issues with such code.
242 #define PUSH(r) do { \
243     if(ctx->opTop >= MAX_STACK_DEPTH) ERR(ctx, "stack overflow"); \
244     ctx->opStack[ctx->opTop] = r; \
245     ctx->opTop++;                 \
246     } while(0)
247
248 static void setupArgs(naContext ctx, struct Frame* f, naRef* args, int nargs)
249 {
250     int i;
251     struct naCode* c = PTR(PTR(f->func).func->code).code;
252
253     // Set the argument symbols, and put any remaining args in a vector
254     if(nargs < c->nArgs)
255         naRuntimeError(ctx, "too few function args (have %d need %d)",
256             nargs, c->nArgs);
257     for(i=0; i<c->nArgs; i++)
258         naiHash_newsym(PTR(f->locals).hash,
259                       &c->constants[ARGSYMS(c)[i]], &args[i]);
260     args += c->nArgs;
261     nargs -= c->nArgs;
262     for(i=0; i<c->nOptArgs; i++, nargs--) {
263         naRef val = nargs > 0 ? args[i] : c->constants[OPTARGVALS(c)[i]];
264         if(IS_CODE(val))
265             val = bindFunction(ctx, &ctx->fStack[ctx->fTop-2], val);
266         naiHash_newsym(PTR(f->locals).hash, &c->constants[OPTARGSYMS(c)[i]], 
267                       &val);
268     }
269     args += c->nOptArgs;
270     if(c->needArgVector || nargs > 0) {
271         naRef argv = naNewVector(ctx);
272         naVec_setsize(ctx, argv, nargs > 0 ? nargs : 0);
273         for(i=0; i<nargs; i++)
274             PTR(argv).vec->rec->array[i] = *args++;
275         naiHash_newsym(PTR(f->locals).hash, &c->constants[c->restArgSym], &argv);
276     }
277 }
278
279 static void checkNamedArgs(naContext ctx, struct naCode* c, struct naHash* h)
280 {
281     int i;
282     naRef sym, rest, dummy;
283     for(i=0; i<c->nArgs; i++) {
284         sym = c->constants[ARGSYMS(c)[i]];
285         if(!naiHash_sym(h, PTR(sym).str, &dummy))
286             naRuntimeError(ctx, "Missing arg: %s", naStr_data(sym));
287     }
288     for(i=0; i<c->nOptArgs; i++) {
289         sym = c->constants[OPTARGSYMS(c)[i]];
290         if(!naiHash_sym(h, PTR(sym).str, &dummy))
291             naiHash_newsym(h, &sym, &c->constants[OPTARGVALS(c)[i]]);
292     }
293     if(c->needArgVector) {
294         sym = c->constants[c->restArgSym];
295         if(!naiHash_sym(h, PTR(sym).str, &dummy)) {
296             rest = naNewVector(ctx);
297             naiHash_newsym(h, &sym, &rest);
298         }
299     }
300 }
301
302 static struct Frame* setupFuncall(naContext ctx, int nargs, int mcall, int named)
303 {
304     naRef *args, func, code, obj = naNil();
305     struct Frame* f;
306     int opf = ctx->opTop - nargs;
307
308     args = &ctx->opStack[opf];
309     func = ctx->opStack[--opf];
310     if(!IS_FUNC(func)) ERR(ctx, "function/method call on uncallable object");
311     code = PTR(func).func->code;
312     if(mcall) obj = ctx->opStack[--opf];
313     ctx->opFrame = opf;
314
315     if(IS_CCODE(code)) {
316         struct naCCode *ccode = PTR(code).ccode;
317         naRef result = ccode->fptru
318                      ? (*ccode->fptru)(ctx, obj, nargs, args, ccode->user_data)
319                      : (*ccode->fptr)(ctx, obj, nargs, args);
320         if(named) ERR(ctx, "native functions have no named arguments");
321         ctx->opTop = ctx->opFrame;
322         PUSH(result);
323         return &(ctx->fStack[ctx->fTop-1]);
324     }
325     
326     if(ctx->fTop >= MAX_RECURSION) ERR(ctx, "call stack overflow");
327     
328     f = &(ctx->fStack[ctx->fTop]);
329     f->locals = named ? args[0] : naNewHash(ctx);
330     f->func = func;
331     f->ip = 0;
332     f->bp = ctx->opFrame;
333
334     if(mcall) naHash_set(f->locals, globals->meRef, obj);
335
336     if(named) checkNamedArgs(ctx, PTR(code).code, PTR(f->locals).hash);
337     else      setupArgs(ctx, f, args, nargs);
338
339     ctx->fTop++;
340     ctx->opTop = f->bp; /* Pop the stack last, to avoid GC lossage */
341     return f;
342 }
343
344 static naRef evalEquality(int op, naRef ra, naRef rb)
345 {
346     int result = naEqual(ra, rb);
347     return naNum((op==OP_EQ) ? result : !result);
348 }
349
350 static naRef evalCat(naContext ctx, naRef l, naRef r)
351 {
352     if(IS_VEC(l) && IS_VEC(r)) {
353         int i, ls = naVec_size(l), rs = naVec_size(r);
354         naRef v = naNewVector(ctx);
355         naVec_setsize(ctx, v, ls + rs);
356         for(i=0; i<ls; i+=1) naVec_set(v, i, naVec_get(l, i));
357         for(i=0; i<rs; i+=1) naVec_set(v, i+ls, naVec_get(r, i));
358         return v;
359     } else {
360         naRef a = stringify(ctx, l);
361         naRef b = stringify(ctx, r);
362         return naStr_concat(naNewString(ctx), a, b);
363     }
364 }
365
366 // When a code object comes out of the constant pool and shows up on
367 // the stack, it needs to be bound with the lexical context.
368 static naRef bindFunction(naContext ctx, struct Frame* f, naRef code)
369 {
370     naRef result = naNewFunc(ctx, code);
371     PTR(result).func->namespace = f->locals;
372     PTR(result).func->next = f->func;
373     return result;
374 }
375
376 static int getClosure(struct naFunc* c, naRef sym, naRef* result)
377 {
378     while(c) {
379         if(naHash_get(c->namespace, sym, result)) return 1;
380         c = PTR(c->next).func;
381     }
382     return 0;
383 }
384
385 static naRef getLocal2(naContext ctx, struct Frame* f, naRef sym)
386 {
387     naRef result;
388     if(!naHash_get(f->locals, sym, &result))
389         if(!getClosure(PTR(f->func).func, sym, &result))
390             naRuntimeError(ctx, "undefined symbol: %s", naStr_data(sym));
391     return result;
392 }
393
394 static void getLocal(naContext ctx, struct Frame* f, naRef* sym, naRef* out)
395 {
396     struct naFunc* func;
397     struct naStr* str = PTR(*sym).str;
398     if(naiHash_sym(PTR(f->locals).hash, str, out))
399         return;
400     func = PTR(f->func).func;
401     while(func && PTR(func->namespace).hash) {
402         if(naiHash_sym(PTR(func->namespace).hash, str, out))
403             return;
404         func = PTR(func->next).func;
405     }
406     // Now do it again using the more general naHash_get().  This will
407     // only be necessary if something has created the value in the
408     // namespace using the more generic hash syntax
409     // (e.g. namespace["symbol"] and not namespace.symbol).
410     *out = getLocal2(ctx, f, *sym);
411 }
412
413 static int setClosure(naRef func, naRef sym, naRef val)
414 {
415     struct naFunc* c = PTR(func).func;
416     if(c == 0) return 0;
417     if(naiHash_tryset(c->namespace, sym, val)) return 1;
418     return setClosure(c->next, sym, val);
419 }
420
421 static void setSymbol(struct Frame* f, naRef sym, naRef val)
422 {
423     // Try the locals first, if not already there try the closures in
424     // order.  Finally put it in the locals if nothing matched.
425     if(!naiHash_tryset(f->locals, sym, val))
426         if(!setClosure(f->func, sym, val))
427             naHash_set(f->locals, sym, val);
428 }
429
430 static const char* ghostGetMember(naContext ctx, naRef obj, naRef field, naRef* out)
431 {
432     naGhostType* gtype = PTR(obj).ghost->gtype;
433     if (!gtype->get_member) return "ghost does not support member access";
434     return gtype->get_member(ctx, PTR(obj).ghost->ptr, field, out);
435 }
436     
437 // Funky API: returns null to indicate no member, an empty string to
438 // indicate success, or a non-empty error message.  Works this way so
439 // we can generate smart error messages without throwing them with a
440 // longjmp -- this gets called under naMember_get() from C code.
441 static const char* getMember_r(naContext ctx, naRef obj, naRef field, naRef* out, int count)
442 {
443     int i;
444     naRef p;
445     struct VecRec* pv;
446     if(--count < 0) return "too many parents";
447
448     if (IS_GHOST(obj)) {
449         if (ghostGetMember(ctx, obj, field, out)) return "";
450         if(!ghostGetMember(ctx, obj, globals->parentsRef, &p)) return 0;
451     } else if (IS_HASH(obj)) {
452         if(naHash_get(obj, field, out)) return "";
453         if(!naHash_get(obj, globals->parentsRef, &p)) return 0;
454     } else if (IS_STR(obj) ) {
455         return getMember_r(ctx, getStringMethods(ctx), field, out, count);
456     } else {
457         return "non-objects have no members";
458     }
459     
460     if(!IS_VEC(p)) return "object \"parents\" field not vector";
461     pv = PTR(p).vec->rec;
462     for(i=0; pv && i<pv->size; i++) {
463         const char* err = getMember_r(ctx, pv->array[i], field, out, count);
464         if(err) return err; /* either an error or success */
465     }
466     return 0;
467 }
468
469 static void getMember(naContext ctx, naRef obj, naRef fld,
470                       naRef* result, int count)
471 {
472     const char* err = getMember_r(ctx, obj, fld, result, count);
473     if(!err)   naRuntimeError(ctx, "No such member: %s", naStr_data(fld));
474     if(err[0]) naRuntimeError(ctx, err);
475 }
476
477 static void setMember(naContext ctx, naRef obj, naRef fld, naRef value)
478 {
479     if (IS_GHOST(obj)) {
480         naGhostType* gtype = PTR(obj).ghost->gtype;
481         if (!gtype->set_member) ERR(ctx, "ghost does not support member access");
482         gtype->set_member(ctx, PTR(obj).ghost->ptr, fld, value);
483         ctx->opTop -= 2;
484         return;
485     }
486     
487     if(!IS_HASH(obj)) ERR(ctx, "non-objects have no members");
488     naHash_set(obj, fld, value);
489     ctx->opTop -= 2;
490 }
491
492 int naMember_get(naContext ctx, naRef obj, naRef field, naRef* out)
493 {
494     const char* err = getMember_r(ctx, obj, field, out, 64);
495     return err && !err[0];
496 }
497
498 // OP_EACH works like a vector get, except that it leaves the vector
499 // and index on the stack, increments the index after use, and
500 // pushes a nil if the index is beyond the end.
501 static void evalEach(naContext ctx, int useIndex)
502 {
503     int idx = (int)(ctx->opStack[ctx->opTop-1].num);
504     naRef vec = ctx->opStack[ctx->opTop-2];
505     if(!IS_VEC(vec)) ERR(ctx, "foreach enumeration of non-vector");
506     if(!PTR(vec).vec->rec || idx >= PTR(vec).vec->rec->size) {
507         PUSH(endToken());
508         return;
509     }
510     ctx->opStack[ctx->opTop-1].num = idx+1; // modify in place
511     PUSH(useIndex ? naNum(idx) : naVec_get(vec, idx));
512 }
513
514 static void evalUnpack(naContext ctx, int count)
515 {
516     naRef vec = ctx->opStack[--ctx->opTop];
517     if(!IS_VEC(vec) || naVec_size(vec) < count)
518         ERR(ctx, "short or invalid multi-assignment vector");
519     while(count--) PUSH(naVec_get(vec, count));
520 }
521
522 // FIXME: unify with almost identical checkVec() above
523 static int vbound(naContext ctx, naRef v, naRef ir, int end)
524 {
525     int sz=naVec_size(v), i = IS_NIL(ir) ? (end ? -1 : 0) : numify(ctx, ir);
526     if(IS_NIL(ir) && !sz) return i;
527     if(i < 0) i += sz;
528     if(i < 0 || i >= sz)
529         naRuntimeError(ctx, "slice index %d out of bounds (size: %d)",
530                        i, sz);
531     return i;
532 }
533
534 static void evalSlice(naContext ctx, naRef src, naRef dst, naRef idx)
535 {
536     if(!IS_VEC(src)) ERR(ctx, "cannot slice non-vector");
537     naVec_append(dst, naVec_get(src, checkVec(ctx, src, idx)));
538 }
539  
540 static void evalSlice2(naContext ctx, naRef src, naRef dst,
541                        naRef start, naRef endr)
542 {
543     int i, end;
544     if(!IS_VEC(src)) ERR(ctx, "cannot slice non-vector");
545     end = vbound(ctx, src, endr, 1);
546     for(i = vbound(ctx, src, start, 0); i<=end; i++)
547         naVec_append(dst, naVec_get(src, i));
548 }
549
550 #define ARG() BYTECODE(cd)[f->ip++]
551 #define CONSTARG() cd->constants[ARG()]
552 #define POP() ctx->opStack[--ctx->opTop]
553 #define STK(n) (ctx->opStack[ctx->opTop-(n)])
554 #define SETFRAME(F) f = (F); cd = PTR(PTR(f->func).func->code).code;
555 #define FIXFRAME() SETFRAME(&(ctx->fStack[ctx->fTop-1]))
556 static naRef run(naContext ctx)
557 {
558     struct Frame* f;
559     struct naCode* cd;
560     int op, arg;
561     naRef a, b;
562
563     ctx->dieArg = naNil();
564     ctx->error[0] = 0;
565
566     FIXFRAME();
567
568     while(1) {
569         op = BYTECODE(cd)[f->ip++];
570         DBG(printf("Stack Depth: %d\n", ctx->opTop));
571         DBG(printOpDEBUG(f->ip-1, op));
572         switch(op) {
573         case OP_POP:  ctx->opTop--; break;
574         case OP_DUP:  PUSH(STK(1)); break;
575         case OP_DUP2: PUSH(STK(2)); PUSH(STK(2)); break;
576         case OP_XCHG:  a=STK(1); STK(1)=STK(2); STK(2)=a; break;
577         case OP_XCHG2: a=STK(1); STK(1)=STK(2); STK(2)=STK(3); STK(3)=a; break;
578
579 #define BINOP(expr) do { \
580     double l = IS_NUM(STK(2)) ? STK(2).num : numify(ctx, STK(2)); \
581     double r = IS_NUM(STK(1)) ? STK(1).num : numify(ctx, STK(1)); \
582     SETNUM(STK(2), expr);                                         \
583     ctx->opTop--; } while(0)
584
585         case OP_PLUS:  BINOP(l + r);         break;
586         case OP_MINUS: BINOP(l - r);         break;
587         case OP_MUL:   BINOP(l * r);         break;
588         case OP_DIV:   BINOP(l / r);         break;
589         case OP_LT:    BINOP(l <  r ? 1 : 0); break;
590         case OP_LTE:   BINOP(l <= r ? 1 : 0); break;
591         case OP_GT:    BINOP(l >  r ? 1 : 0); break;
592         case OP_GTE:   BINOP(l >= r ? 1 : 0); break;
593 #undef BINOP
594
595         case OP_EQ: case OP_NEQ:
596             STK(2) = evalEquality(op, STK(2), STK(1));
597             ctx->opTop--;
598             break;
599         case OP_CAT:
600             STK(2) = evalCat(ctx, STK(2), STK(1));
601             ctx->opTop--;
602             break;
603         case OP_NEG:
604             STK(1) = naNum(-numify(ctx, STK(1)));
605             break;
606         case OP_NOT:
607             STK(1) = naNum(boolify(ctx, STK(1)) ? 0 : 1);
608             break;
609         case OP_PUSHCONST:
610             a = CONSTARG();
611             if(IS_CODE(a)) a = bindFunction(ctx, f, a);
612             PUSH(a);
613             break;
614         case OP_PUSHONE:
615             PUSH(naNum(1));
616             break;
617         case OP_PUSHZERO:
618             PUSH(naNum(0));
619             break;
620         case OP_PUSHNIL:
621             PUSH(naNil());
622             break;
623         case OP_PUSHEND:
624             PUSH(endToken());
625             break;
626         case OP_NEWVEC:
627             PUSH(naNewVector(ctx));
628             break;
629         case OP_VAPPEND:
630             naVec_append(STK(2), STK(1));
631             ctx->opTop--;
632             break;
633         case OP_NEWHASH:
634             PUSH(naNewHash(ctx));
635             break;
636         case OP_HAPPEND:
637             naHash_set(STK(3), STK(2), STK(1));
638             ctx->opTop -= 2;
639             break;
640         case OP_LOCAL:
641             a = CONSTARG();
642             getLocal(ctx, f, &a, &b);
643             PUSH(b);
644             break;
645         case OP_SETSYM:
646             setSymbol(f, STK(1), STK(2));
647             ctx->opTop--;
648             break;
649         case OP_SETLOCAL:
650             naHash_set(f->locals, STK(1), STK(2));
651             ctx->opTop--;
652             break;
653         case OP_MEMBER:
654             getMember(ctx, STK(1), CONSTARG(), &STK(1), 64);
655             break;
656         case OP_SETMEMBER:
657             setMember(ctx, STK(2), STK(1), STK(3));
658             break;
659         case OP_INSERT:
660             containerSet(ctx, STK(2), STK(1), STK(3));
661             ctx->opTop -= 2;
662             break;
663         case OP_EXTRACT:
664             STK(2) = containerGet(ctx, STK(2), STK(1));
665             ctx->opTop--;
666             break;
667         case OP_SLICE:
668             evalSlice(ctx, STK(3), STK(2), STK(1));
669             ctx->opTop--;
670             break;
671         case OP_SLICE2:
672             evalSlice2(ctx, STK(4), STK(3), STK(2), STK(1));
673             ctx->opTop -= 2;
674             break;
675         case OP_JMPLOOP:
676             // Identical to JMP, except for locking
677             naCheckBottleneck();
678             f->ip = BYTECODE(cd)[f->ip];
679             DBG(printf("   [Jump to: %d]\n", f->ip));
680             break;
681         case OP_JMP:
682             f->ip = BYTECODE(cd)[f->ip];
683             DBG(printf("   [Jump to: %d]\n", f->ip));
684             break;
685         case OP_JIFEND:
686             arg = ARG();
687             if(IS_END(STK(1))) {
688                 ctx->opTop--; // Pops **ONLY** if it's nil!
689                 f->ip = arg;
690                 DBG(printf("   [Jump to: %d]\n", f->ip));
691             }
692             break;
693         case OP_JIFTRUE:
694             arg = ARG();
695             if(boolify(ctx, STK(1))) {
696                 f->ip = arg;
697                 DBG(printf("   [Jump to: %d]\n", f->ip));
698             }
699             break;
700         case OP_JIFNOT:
701             arg = ARG();
702             if(!boolify(ctx, STK(1))) {
703                 f->ip = arg;
704                 DBG(printf("   [Jump to: %d]\n", f->ip));
705             }
706             break;
707         case OP_JIFNOTPOP:
708             arg = ARG();
709             if(!boolify(ctx, POP())) {
710                 f->ip = arg;
711                 DBG(printf("   [Jump to: %d]\n", f->ip));
712             }
713             break;
714         case OP_FCALL:  SETFRAME(setupFuncall(ctx, ARG(), 0, 0)); break;
715         case OP_MCALL:  SETFRAME(setupFuncall(ctx, ARG(), 1, 0)); break;
716         case OP_FCALLH: SETFRAME(setupFuncall(ctx,     1, 0, 1)); break;
717         case OP_MCALLH: SETFRAME(setupFuncall(ctx,     1, 1, 1)); break;
718         case OP_RETURN:
719             a = STK(1);
720             ctx->dieArg = naNil();
721             if(ctx->callChild) naFreeContext(ctx->callChild);
722             if(--ctx->fTop <= 0) return a;
723             ctx->opTop = f->bp + 1; // restore the correct opstack frame!
724             STK(1) = a;
725             FIXFRAME();
726             break;
727         case OP_EACH:
728             evalEach(ctx, 0);
729             break;
730         case OP_INDEX:
731             evalEach(ctx, 1);
732             break;
733         case OP_MARK: // save stack state (e.g. "setjmp")
734             if(ctx->markTop >= MAX_MARK_DEPTH)
735                 ERR(ctx, "mark stack overflow");
736             ctx->markStack[ctx->markTop++] = ctx->opTop;
737             break;
738         case OP_UNMARK: // pop stack state set by mark
739             ctx->markTop--;
740             break;
741         case OP_BREAK: // restore stack state (FOLLOW WITH JMP!)
742             ctx->opTop = ctx->markStack[ctx->markTop-1];
743             break;
744         case OP_BREAK2: // same, but also pop the mark stack
745             ctx->opTop = ctx->markStack[--ctx->markTop];
746             break;
747         case OP_UNPACK:
748             evalUnpack(ctx, ARG());
749             break;
750         default:
751             ERR(ctx, "BUG: bad opcode");
752         }
753         ctx->ntemps = 0; // reset GC temp vector
754         DBG(printStackDEBUG(ctx));
755     }
756     return naNil(); // unreachable
757 }
758 #undef POP
759 #undef CONSTARG
760 #undef STK
761 #undef FIXFRAME
762
763 void naSave(naContext ctx, naRef obj)
764 {
765     naVec_append(globals->save, obj);
766 }
767
768 int naStackDepth(naContext ctx)
769 {
770     return ctx ? ctx->fTop + naStackDepth(ctx->callChild): 0;
771 }
772
773 static int findFrame(naContext ctx, naContext* out, int fn)
774 {
775     int sd = naStackDepth(ctx->callChild);
776     if(fn < sd) return findFrame(ctx->callChild, out, fn);
777     *out = ctx;
778     return ctx->fTop - 1 - (fn - sd);
779 }
780
781 int naGetLine(naContext ctx, int frame)
782 {
783     struct Frame* f;
784     frame = findFrame(ctx, &ctx, frame);
785     f = &ctx->fStack[frame];
786     if(IS_FUNC(f->func) && IS_CODE(PTR(f->func).func->code)) {
787         struct naCode* c = PTR(PTR(f->func).func->code).code;
788         unsigned short* p = LINEIPS(c) + c->nLines - 2;
789         while(p >= LINEIPS(c) && p[0] > f->ip)
790             p -= 2;
791         return p[1];
792     }
793     return -1;
794 }
795
796 naRef naGetSourceFile(naContext ctx, int frame)
797 {
798     naRef f;
799     frame = findFrame(ctx, &ctx, frame);
800     f = ctx->fStack[frame].func;
801     f = PTR(f).func->code;
802     return PTR(f).code->srcFile;
803 }
804
805 char* naGetError(naContext ctx)
806 {
807     if(IS_STR(ctx->dieArg))
808         return naStr_data(ctx->dieArg);
809     return ctx->error[0] ? ctx->error : 0;
810 }
811
812 naRef naBindFunction(naContext ctx, naRef code, naRef closure)
813 {
814     naRef func = naNewFunc(ctx, code);
815     PTR(func).func->namespace = closure;
816     PTR(func).func->next = naNil();
817     return func;
818 }
819
820 naRef naBindToContext(naContext ctx, naRef code)
821 {
822     naRef func = naNewFunc(ctx, code);
823     if(ctx->fTop) {
824         struct Frame* f = &ctx->fStack[ctx->fTop-1];
825         PTR(func).func->namespace = f->locals;
826         PTR(func).func->next = f->func;
827     }
828     return func;
829 }
830
831 naRef naCall(naContext ctx, naRef func, int argc, naRef* args,
832              naRef obj, naRef locals)
833 {
834     int i;
835     naRef result;
836     if(!ctx->callParent) naModLock();
837
838     // We might have to allocate objects, which can call the GC.  But
839     // the call isn't on the Nasal stack yet, so the GC won't find our
840     // C-space arguments.
841     naTempSave(ctx, func);
842     for(i=0; i<argc; i++)
843         naTempSave(ctx, args[i]);
844     naTempSave(ctx, obj);
845     naTempSave(ctx, locals);
846
847     // naRuntimeError() calls end up here:
848     if(setjmp(ctx->jumpHandle)) {
849         if(!ctx->callParent) naModUnlock();
850         return naNil();
851     }
852
853     if(IS_CCODE(PTR(func).func->code)) {
854         naCFunction fp = PTR(PTR(func).func->code).ccode->fptr;
855         result = (*fp)(ctx, obj, argc, args);
856         if(!ctx->callParent) naModUnlock();
857         return result;
858     }
859
860     if(IS_NIL(locals))
861         locals = naNewHash(ctx);
862     if(!IS_FUNC(func)) {
863         func = naNewFunc(ctx, func);
864         PTR(func).func->namespace = locals;
865     }
866     if(!IS_NIL(obj))
867         naHash_set(locals, globals->meRef, obj);
868
869     ctx->opTop = ctx->markTop = 0;
870     ctx->fTop = 1;
871     ctx->fStack[0].func = func;
872
873     ctx->fStack[0].locals = locals;
874     ctx->fStack[0].ip = 0;
875     ctx->fStack[0].bp = ctx->opTop;
876
877     setupArgs(ctx, ctx->fStack, args, argc);
878
879     result = run(ctx);
880     if(!ctx->callParent) naModUnlock();
881     return result;
882 }
883
884 naRef naContinue(naContext ctx)
885 {
886     naRef result;
887     if(!ctx->callParent) naModLock();
888
889     ctx->dieArg = naNil();
890     ctx->error[0] = 0;
891
892     if(setjmp(ctx->jumpHandle)) {
893         if(!ctx->callParent) naModUnlock();
894         else naRethrowError(ctx);
895         return naNil();
896     }
897
898     // Wipe off the old function arguments, and push the expected
899     // result (either the result of our subcontext, or a synthesized
900     // nil if the thrown error was from an extension function or
901     // in-script die() call) before re-running the code from the
902     // instruction following the error.
903     ctx->opTop = ctx->opFrame;
904     PUSH(ctx->callChild ? naContinue(ctx->callChild) : naNil());
905
906     // Getting here means the child completed successfully.  But
907     // because its original C stack was longjmp'd out of existence,
908     // there is no one left to free the context, so we have to do it.
909     // This is fragile, but unfortunately required.
910     if(ctx->callChild) naFreeContext(ctx->callChild);
911
912     result = run(ctx);
913     if(!ctx->callParent) naModUnlock();
914     return result;
915 }