7 ////////////////////////////////////////////////////////////////////////
8 // Debugging stuff. ////////////////////////////////////////////////////
9 ////////////////////////////////////////////////////////////////////////
10 //#define INTERPRETER_DUMP
11 #if !defined(INTERPRETER_DUMP)
12 # define DBG(expr) /* noop */
14 # define DBG(expr) expr
18 char* opStringDEBUG(int op);
19 void printOpDEBUG(int ip, int op);
20 void printStackDEBUG(naContext ctx);
21 ////////////////////////////////////////////////////////////////////////
23 struct Globals* globals = 0;
25 static naRef bindFunction(naContext ctx, struct Frame* f, naRef code);
27 #define ERR(c, msg) naRuntimeError((c),(msg))
28 void naRuntimeError(naContext c, const char* fmt, ...)
32 vsnprintf(c->error, sizeof(c->error), fmt, ap);
34 longjmp(c->jumpHandle, 1);
37 void naRethrowError(naContext subc)
39 strncpy(subc->callParent->error, subc->error, sizeof(subc->error));
40 subc->callParent->dieArg = subc->dieArg;
41 longjmp(subc->callParent->jumpHandle, 1);
44 #define END_PTR ((void*)1)
45 #define IS_END(r) (IS_REF((r)) && PTR((r)).obj == END_PTR)
46 static naRef endToken()
53 static int boolify(naContext ctx, naRef r)
55 if(IS_NUM(r)) return r.num != 0;
56 if(IS_NIL(r) || IS_END(r)) return 0;
59 if(naStr_len(r) == 0) return 0;
60 if(naStr_tonum(r, &d)) return d != 0;
63 ERR(ctx, "non-scalar used in boolean context");
67 static double numify(naContext ctx, naRef o)
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");
78 static naRef stringify(naContext ctx, naRef r)
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");
86 static int checkVec(naContext ctx, naRef vec, naRef idx)
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)",
96 static int checkStr(naContext ctx, naRef str, naRef idx)
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)",
106 static naRef containerGet(naContext ctx, naRef box, naRef key)
108 naRef result = naNil();
109 if(!IS_SCALAR(key)) ERR(ctx, "container index not scalar");
111 naHash_get(box, key, &result);
113 result = naVec_get(box, checkVec(ctx, box, key));
115 result = naNum((unsigned char)naStr_data(box)[checkStr(ctx, box, key)]);
117 ERR(ctx, "extract from non-container");
121 static void containerSet(naContext ctx, naRef box, naRef key, naRef val)
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");
133 static void initTemps(naContext c)
136 c->temps = naAlloc(c->tempsz * sizeof(struct naObj*));
140 static void initContext(naContext c)
143 c->fTop = c->opTop = c->markTop = 0;
144 for(i=0; i<NUM_NASAL_TYPES; i++)
159 static void initGlobals()
163 globals = (struct Globals*)naAlloc(sizeof(struct Globals));
164 naBZero(globals, sizeof(struct Globals));
166 globals->sem = naNewSem();
167 globals->lock = naNewLock();
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;
174 globals->deadBlocks = naAlloc(sizeof(void*) * globals->deadsz);
176 // Initialize a single context
177 globals->freeContexts = 0;
178 globals->allContexts = 0;
181 globals->symbols = naNewHash(c);
182 globals->save = naNewVector(c);
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));
192 naContext naNewContext()
199 c = globals->freeContexts;
201 globals->freeContexts = c->nextFree;
207 c = (naContext)naAlloc(sizeof(struct Context));
211 c->nextAll = globals->allContexts;
213 globals->allContexts = c;
219 naContext naSubContext(naContext super)
221 naContext ctx = naNewContext();
222 if(super->callChild) naFreeContext(super->callChild);
223 ctx->callParent = super;
224 super->callChild = ctx;
228 void naFreeContext(naContext c)
231 if(c->callChild) naFreeContext(c->callChild);
232 if(c->callParent) c->callParent->callChild = 0;
234 c->nextFree = globals->freeContexts;
235 globals->freeContexts = c;
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; \
248 static void setupArgs(naContext ctx, struct Frame* f, naRef* args, int nargs)
251 struct naCode* c = PTR(PTR(f->func).func->code).code;
253 // Set the argument symbols, and put any remaining args in a vector
255 naRuntimeError(ctx, "too few function args (have %d need %d)",
257 for(i=0; i<c->nArgs; i++)
258 naiHash_newsym(PTR(f->locals).hash,
259 &c->constants[ARGSYMS(c)[i]], &args[i]);
262 for(i=0; i<c->nOptArgs; i++, nargs--) {
263 naRef val = nargs > 0 ? args[i] : c->constants[OPTARGVALS(c)[i]];
265 val = bindFunction(ctx, &ctx->fStack[ctx->fTop-2], val);
266 naiHash_newsym(PTR(f->locals).hash, &c->constants[OPTARGSYMS(c)[i]],
270 if(c->needArgVector || nargs > 0) {
271 naRef argv = naNewVector(ctx);
272 naVec_setsize(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);
279 static void checkNamedArgs(naContext ctx, struct naCode* c, struct naHash* h)
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));
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]]);
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);
302 static struct Frame* setupFuncall(naContext ctx, int nargs, int mcall, int named)
304 naRef *args, func, code, obj = naNil();
306 int opf = ctx->opTop - nargs;
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];
316 naRef result = (*PTR(code).ccode->fptr)(ctx, obj, nargs, args);
317 if(named) ERR(ctx, "native functions have no named arguments");
318 ctx->opTop = ctx->opFrame;
320 return &(ctx->fStack[ctx->fTop-1]);
323 if(ctx->fTop >= MAX_RECURSION) ERR(ctx, "call stack overflow");
325 f = &(ctx->fStack[ctx->fTop]);
326 f->locals = named ? args[0] : naNewHash(ctx);
329 f->bp = ctx->opFrame;
331 if(mcall) naHash_set(f->locals, globals->meRef, obj);
333 if(named) checkNamedArgs(ctx, PTR(code).code, PTR(f->locals).hash);
334 else setupArgs(ctx, f, args, nargs);
337 ctx->opTop = f->bp; /* Pop the stack last, to avoid GC lossage */
341 static naRef evalEquality(int op, naRef ra, naRef rb)
343 int result = naEqual(ra, rb);
344 return naNum((op==OP_EQ) ? result : !result);
347 static naRef evalCat(naContext ctx, naRef l, naRef r)
349 if(IS_VEC(l) && IS_VEC(r)) {
350 int i, ls = naVec_size(l), rs = naVec_size(r);
351 naRef v = naNewVector(ctx);
352 naVec_setsize(v, ls + rs);
353 for(i=0; i<ls; i+=1) naVec_set(v, i, naVec_get(l, i));
354 for(i=0; i<rs; i+=1) naVec_set(v, i+ls, naVec_get(r, i));
357 naRef a = stringify(ctx, l);
358 naRef b = stringify(ctx, r);
359 return naStr_concat(naNewString(ctx), a, b);
363 // When a code object comes out of the constant pool and shows up on
364 // the stack, it needs to be bound with the lexical context.
365 static naRef bindFunction(naContext ctx, struct Frame* f, naRef code)
367 naRef result = naNewFunc(ctx, code);
368 PTR(result).func->namespace = f->locals;
369 PTR(result).func->next = f->func;
373 static int getClosure(struct naFunc* c, naRef sym, naRef* result)
376 if(naHash_get(c->namespace, sym, result)) return 1;
377 c = PTR(c->next).func;
382 static naRef getLocal2(naContext ctx, struct Frame* f, naRef sym)
385 if(!naHash_get(f->locals, sym, &result))
386 if(!getClosure(PTR(f->func).func, sym, &result))
387 naRuntimeError(ctx, "undefined symbol: %s", naStr_data(sym));
391 static void getLocal(naContext ctx, struct Frame* f, naRef* sym, naRef* out)
394 struct naStr* str = PTR(*sym).str;
395 if(naiHash_sym(PTR(f->locals).hash, str, out))
397 func = PTR(f->func).func;
398 while(func && PTR(func->namespace).hash) {
399 if(naiHash_sym(PTR(func->namespace).hash, str, out))
401 func = PTR(func->next).func;
403 // Now do it again using the more general naHash_get(). This will
404 // only be necessary if something has created the value in the
405 // namespace using the more generic hash syntax
406 // (e.g. namespace["symbol"] and not namespace.symbol).
407 *out = getLocal2(ctx, f, *sym);
410 static int setClosure(naRef func, naRef sym, naRef val)
412 struct naFunc* c = PTR(func).func;
414 if(naiHash_tryset(c->namespace, sym, val)) return 1;
415 return setClosure(c->next, sym, val);
418 static void setSymbol(struct Frame* f, naRef sym, naRef val)
420 // Try the locals first, if not already there try the closures in
421 // order. Finally put it in the locals if nothing matched.
422 if(!naiHash_tryset(f->locals, sym, val))
423 if(!setClosure(f->func, sym, val))
424 naHash_set(f->locals, sym, val);
427 // Funky API: returns null to indicate no member, an empty string to
428 // indicate success, or a non-empty error message. Works this way so
429 // we can generate smart error messages without throwing them with a
430 // longjmp -- this gets called under naMember_get() from C code.
431 static const char* getMember_r(naRef obj, naRef field, naRef* out, int count)
436 if(--count < 0) return "too many parents";
437 if(!IS_HASH(obj)) return "non-objects have no members";
438 if(naHash_get(obj, field, out)) return "";
439 if(!naHash_get(obj, globals->parentsRef, &p)) return 0;
440 if(!IS_VEC(p)) return "object \"parents\" field not vector";
441 pv = PTR(p).vec->rec;
442 for(i=0; pv && i<pv->size; i++) {
443 const char* err = getMember_r(pv->array[i], field, out, count);
444 if(err) return err; /* either an error or success */
449 static void getMember(naContext ctx, naRef obj, naRef fld,
450 naRef* result, int count)
452 const char* err = getMember_r(obj, fld, result, count);
453 if(!err) naRuntimeError(ctx, "No such member: %s", naStr_data(fld));
454 if(err[0]) naRuntimeError(ctx, err);
457 int naMember_get(naRef obj, naRef field, naRef* out)
459 const char* err = getMember_r(obj, field, out, 64);
460 return err && !err[0];
463 // OP_EACH works like a vector get, except that it leaves the vector
464 // and index on the stack, increments the index after use, and
465 // pushes a nil if the index is beyond the end.
466 static void evalEach(naContext ctx, int useIndex)
468 int idx = (int)(ctx->opStack[ctx->opTop-1].num);
469 naRef vec = ctx->opStack[ctx->opTop-2];
470 if(!IS_VEC(vec)) ERR(ctx, "foreach enumeration of non-vector");
471 if(!PTR(vec).vec->rec || idx >= PTR(vec).vec->rec->size) {
475 ctx->opStack[ctx->opTop-1].num = idx+1; // modify in place
476 PUSH(useIndex ? naNum(idx) : naVec_get(vec, idx));
479 static void evalUnpack(naContext ctx, int count)
481 naRef vec = ctx->opStack[--ctx->opTop];
482 if(!IS_VEC(vec) || naVec_size(vec) < count)
483 ERR(ctx, "short or invalid multi-assignment vector");
484 while(count--) PUSH(naVec_get(vec, count));
487 // FIXME: unify with almost identical checkVec() above
488 static int vbound(naContext ctx, naRef v, naRef ir, int end)
490 int sz=naVec_size(v), i = IS_NIL(ir) ? (end ? -1 : 0) : numify(ctx, ir);
491 if(IS_NIL(ir) && !sz) return i;
494 naRuntimeError(ctx, "slice index %d out of bounds (size: %d)",
499 static void evalSlice(naContext ctx, naRef src, naRef dst, naRef idx)
501 if(!IS_VEC(src)) ERR(ctx, "cannot slice non-vector");
502 naVec_append(dst, naVec_get(src, checkVec(ctx, src, idx)));
505 static void evalSlice2(naContext ctx, naRef src, naRef dst,
506 naRef start, naRef endr)
509 if(!IS_VEC(src)) ERR(ctx, "cannot slice non-vector");
510 end = vbound(ctx, src, endr, 1);
511 for(i = vbound(ctx, src, start, 0); i<=end; i++)
512 naVec_append(dst, naVec_get(src, i));
515 #define ARG() BYTECODE(cd)[f->ip++]
516 #define CONSTARG() cd->constants[ARG()]
517 #define POP() ctx->opStack[--ctx->opTop]
518 #define STK(n) (ctx->opStack[ctx->opTop-(n)])
519 #define SETFRAME(F) f = (F); cd = PTR(PTR(f->func).func->code).code;
520 #define FIXFRAME() SETFRAME(&(ctx->fStack[ctx->fTop-1]))
521 static naRef run(naContext ctx)
528 ctx->dieArg = naNil();
534 op = BYTECODE(cd)[f->ip++];
535 DBG(printf("Stack Depth: %d\n", ctx->opTop));
536 DBG(printOpDEBUG(f->ip-1, op));
538 case OP_POP: ctx->opTop--; break;
539 case OP_DUP: PUSH(STK(1)); break;
540 case OP_DUP2: PUSH(STK(2)); PUSH(STK(2)); break;
541 case OP_XCHG: a=STK(1); STK(1)=STK(2); STK(2)=a; break;
542 case OP_XCHG2: a=STK(1); STK(1)=STK(2); STK(2)=STK(3); STK(3)=a; break;
544 #define BINOP(expr) do { \
545 double l = IS_NUM(STK(2)) ? STK(2).num : numify(ctx, STK(2)); \
546 double r = IS_NUM(STK(1)) ? STK(1).num : numify(ctx, STK(1)); \
547 SETNUM(STK(2), expr); \
548 ctx->opTop--; } while(0)
550 case OP_PLUS: BINOP(l + r); break;
551 case OP_MINUS: BINOP(l - r); break;
552 case OP_MUL: BINOP(l * r); break;
553 case OP_DIV: BINOP(l / r); break;
554 case OP_LT: BINOP(l < r ? 1 : 0); break;
555 case OP_LTE: BINOP(l <= r ? 1 : 0); break;
556 case OP_GT: BINOP(l > r ? 1 : 0); break;
557 case OP_GTE: BINOP(l >= r ? 1 : 0); break;
560 case OP_EQ: case OP_NEQ:
561 STK(2) = evalEquality(op, STK(2), STK(1));
565 STK(2) = evalCat(ctx, STK(2), STK(1));
569 STK(1) = naNum(-numify(ctx, STK(1)));
572 STK(1) = naNum(boolify(ctx, STK(1)) ? 0 : 1);
576 if(IS_CODE(a)) a = bindFunction(ctx, f, a);
592 PUSH(naNewVector(ctx));
595 naVec_append(STK(2), STK(1));
599 PUSH(naNewHash(ctx));
602 naHash_set(STK(3), STK(2), STK(1));
607 getLocal(ctx, f, &a, &b);
611 setSymbol(f, STK(1), STK(2));
615 naHash_set(f->locals, STK(1), STK(2));
619 getMember(ctx, STK(1), CONSTARG(), &STK(1), 64);
622 if(!IS_HASH(STK(2))) ERR(ctx, "non-objects have no members");
623 naHash_set(STK(2), STK(1), STK(3));
627 containerSet(ctx, STK(2), STK(1), STK(3));
631 STK(2) = containerGet(ctx, STK(2), STK(1));
635 evalSlice(ctx, STK(3), STK(2), STK(1));
639 evalSlice2(ctx, STK(4), STK(3), STK(2), STK(1));
643 // Identical to JMP, except for locking
645 f->ip = BYTECODE(cd)[f->ip];
646 DBG(printf(" [Jump to: %d]\n", f->ip));
649 f->ip = BYTECODE(cd)[f->ip];
650 DBG(printf(" [Jump to: %d]\n", f->ip));
655 ctx->opTop--; // Pops **ONLY** if it's nil!
657 DBG(printf(" [Jump to: %d]\n", f->ip));
662 if(boolify(ctx, STK(1))) {
664 DBG(printf(" [Jump to: %d]\n", f->ip));
669 if(!boolify(ctx, STK(1))) {
671 DBG(printf(" [Jump to: %d]\n", f->ip));
676 if(!boolify(ctx, POP())) {
678 DBG(printf(" [Jump to: %d]\n", f->ip));
681 case OP_FCALL: SETFRAME(setupFuncall(ctx, ARG(), 0, 0)); break;
682 case OP_MCALL: SETFRAME(setupFuncall(ctx, ARG(), 1, 0)); break;
683 case OP_FCALLH: SETFRAME(setupFuncall(ctx, 1, 0, 1)); break;
684 case OP_MCALLH: SETFRAME(setupFuncall(ctx, 1, 1, 1)); break;
687 ctx->dieArg = naNil();
688 if(ctx->callChild) naFreeContext(ctx->callChild);
689 if(--ctx->fTop <= 0) return a;
690 ctx->opTop = f->bp + 1; // restore the correct opstack frame!
700 case OP_MARK: // save stack state (e.g. "setjmp")
701 if(ctx->markTop >= MAX_MARK_DEPTH)
702 ERR(ctx, "mark stack overflow");
703 ctx->markStack[ctx->markTop++] = ctx->opTop;
705 case OP_UNMARK: // pop stack state set by mark
708 case OP_BREAK: // restore stack state (FOLLOW WITH JMP!)
709 ctx->opTop = ctx->markStack[ctx->markTop-1];
711 case OP_BREAK2: // same, but also pop the mark stack
712 ctx->opTop = ctx->markStack[--ctx->markTop];
715 evalUnpack(ctx, ARG());
718 ERR(ctx, "BUG: bad opcode");
720 ctx->ntemps = 0; // reset GC temp vector
721 DBG(printStackDEBUG(ctx));
723 return naNil(); // unreachable
730 void naSave(naContext ctx, naRef obj)
732 naVec_append(globals->save, obj);
735 int naStackDepth(naContext ctx)
737 return ctx ? ctx->fTop + naStackDepth(ctx->callChild): 0;
740 static int findFrame(naContext ctx, naContext* out, int fn)
742 int sd = naStackDepth(ctx->callChild);
743 if(fn < sd) return findFrame(ctx->callChild, out, fn);
745 return ctx->fTop - 1 - (fn - sd);
748 int naGetLine(naContext ctx, int frame)
751 frame = findFrame(ctx, &ctx, frame);
752 f = &ctx->fStack[frame];
753 if(IS_FUNC(f->func) && IS_CODE(PTR(f->func).func->code)) {
754 struct naCode* c = PTR(PTR(f->func).func->code).code;
755 unsigned short* p = LINEIPS(c) + c->nLines - 2;
756 while(p >= LINEIPS(c) && p[0] > f->ip)
763 naRef naGetSourceFile(naContext ctx, int frame)
766 frame = findFrame(ctx, &ctx, frame);
767 f = ctx->fStack[frame].func;
768 f = PTR(f).func->code;
769 return PTR(f).code->srcFile;
772 char* naGetError(naContext ctx)
774 if(IS_STR(ctx->dieArg))
775 return naStr_data(ctx->dieArg);
776 return ctx->error[0] ? ctx->error : 0;
779 naRef naBindFunction(naContext ctx, naRef code, naRef closure)
781 naRef func = naNewFunc(ctx, code);
782 PTR(func).func->namespace = closure;
783 PTR(func).func->next = naNil();
787 naRef naBindToContext(naContext ctx, naRef code)
789 naRef func = naNewFunc(ctx, code);
791 struct Frame* f = &ctx->fStack[ctx->fTop-1];
792 PTR(func).func->namespace = f->locals;
793 PTR(func).func->next = f->func;
798 naRef naCall(naContext ctx, naRef func, int argc, naRef* args,
799 naRef obj, naRef locals)
803 if(!ctx->callParent) naModLock();
805 // We might have to allocate objects, which can call the GC. But
806 // the call isn't on the Nasal stack yet, so the GC won't find our
807 // C-space arguments.
808 naTempSave(ctx, func);
809 for(i=0; i<argc; i++)
810 naTempSave(ctx, args[i]);
811 naTempSave(ctx, obj);
812 naTempSave(ctx, locals);
814 // naRuntimeError() calls end up here:
815 if(setjmp(ctx->jumpHandle)) {
816 if(!ctx->callParent) naModUnlock();
820 if(IS_CCODE(PTR(func).func->code)) {
821 naCFunction fp = PTR(PTR(func).func->code).ccode->fptr;
822 result = (*fp)(ctx, obj, argc, args);
823 if(!ctx->callParent) naModUnlock();
828 locals = naNewHash(ctx);
830 func = naNewFunc(ctx, func);
831 PTR(func).func->namespace = locals;
834 naHash_set(locals, globals->meRef, obj);
836 ctx->opTop = ctx->markTop = 0;
838 ctx->fStack[0].func = func;
840 ctx->fStack[0].locals = locals;
841 ctx->fStack[0].ip = 0;
842 ctx->fStack[0].bp = ctx->opTop;
844 setupArgs(ctx, ctx->fStack, args, argc);
847 if(!ctx->callParent) naModUnlock();
851 naRef naContinue(naContext ctx)
854 if(!ctx->callParent) naModLock();
856 ctx->dieArg = naNil();
859 if(setjmp(ctx->jumpHandle)) {
860 if(!ctx->callParent) naModUnlock();
861 else naRethrowError(ctx);
865 // Wipe off the old function arguments, and push the expected
866 // result (either the result of our subcontext, or a synthesized
867 // nil if the thrown error was from an extension function or
868 // in-script die() call) before re-running the code from the
869 // instruction following the error.
870 ctx->opTop = ctx->opFrame;
871 PUSH(ctx->callChild ? naContinue(ctx->callChild) : naNil());
873 // Getting here means the child completed successfully. But
874 // because its original C stack was longjmp'd out of existence,
875 // there is no one left to free the context, so we have to do it.
876 // This is fragile, but unfortunately required.
877 if(ctx->callChild) naFreeContext(ctx->callChild);
880 if(!ctx->callParent) naModUnlock();