129
145
L->top = (L->top - oldstack) + L->stack;
130
146
for (up = L->openupval; up != NULL; up = up->gch.next)
131
147
gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack;
132
for (ci = L->base_ci; ci <= L->ci; ci++) {
148
for (ci = L->ci; ci != NULL; ci = ci->previous) {
133
149
ci->top = (ci->top - oldstack) + L->stack;
134
ci->base = (ci->base - oldstack) + L->stack;
135
150
ci->func = (ci->func - oldstack) + L->stack;
152
ci->u.l.base = (ci->u.l.base - oldstack) + L->stack;
137
L->base = (L->base - oldstack) + L->stack;
157
/* some space for error handling */
158
#define ERRORSTACKSIZE (LUAI_MAXSTACK + 200)
141
161
void luaD_reallocstack (lua_State *L, int newsize) {
142
162
TValue *oldstack = L->stack;
143
int realsize = newsize + 1 + EXTRA_STACK;
144
lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1);
145
luaM_reallocvector(L, L->stack, L->stacksize, realsize, TValue);
146
L->stacksize = realsize;
147
L->stack_last = L->stack+newsize;
163
int lim = L->stacksize;
164
lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE);
165
lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK);
166
luaM_reallocvector(L, L->stack, L->stacksize, newsize, TValue);
167
for (; lim < newsize; lim++)
168
setnilvalue(L->stack + lim); /* erase new segment */
169
L->stacksize = newsize;
170
L->stack_last = L->stack + newsize - EXTRA_STACK;
148
171
correctstack(L, oldstack);
152
void luaD_reallocCI (lua_State *L, int newsize) {
153
CallInfo *oldci = L->base_ci;
154
luaM_reallocvector(L, L->base_ci, L->size_ci, newsize, CallInfo);
155
L->size_ci = newsize;
156
L->ci = (L->ci - oldci) + L->base_ci;
157
L->end_ci = L->base_ci + L->size_ci - 1;
161
175
void luaD_growstack (lua_State *L, int n) {
162
if (n <= L->stacksize) /* double size is enough? */
163
luaD_reallocstack(L, 2*L->stacksize);
165
luaD_reallocstack(L, L->stacksize + n);
169
static CallInfo *growCI (lua_State *L) {
170
if (L->size_ci > LUAI_MAXCALLS) /* overflow while handling overflow? */
176
int size = L->stacksize;
177
if (size > LUAI_MAXSTACK) /* error after extra size? */
171
178
luaD_throw(L, LUA_ERRERR);
173
luaD_reallocCI(L, 2*L->size_ci);
174
if (L->size_ci > LUAI_MAXCALLS)
180
int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK;
181
int newsize = 2 * size;
182
if (newsize > LUAI_MAXSTACK) newsize = LUAI_MAXSTACK;
183
if (newsize < needed) newsize = needed;
184
if (newsize > LUAI_MAXSTACK) { /* stack overflow? */
185
luaD_reallocstack(L, ERRORSTACKSIZE);
175
186
luaG_runerror(L, "stack overflow");
181
void luaD_callhook (lua_State *L, int event, int line) {
189
luaD_reallocstack(L, newsize);
194
static int stackinuse (lua_State *L) {
197
for (ci = L->ci; ci != NULL; ci = ci->previous) {
198
lua_assert(ci->top <= L->stack_last);
199
if (lim < ci->top) lim = ci->top;
201
return cast_int(lim - L->stack) + 1; /* part of stack in use */
205
void luaD_shrinkstack (lua_State *L) {
206
int inuse = stackinuse(L);
207
int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK;
208
if (goodsize > LUAI_MAXSTACK) goodsize = LUAI_MAXSTACK;
209
if (inuse > LUAI_MAXSTACK || /* handling stack overflow? */
210
goodsize >= L->stacksize) /* would grow instead of shrink? */
211
condmovestack(L); /* don't change stack (change only for debugging) */
213
luaD_reallocstack(L, goodsize); /* shrink it */
217
void luaD_hook (lua_State *L, int event, int line) {
182
218
lua_Hook hook = L->hook;
183
219
if (hook && L->allowhook) {
220
CallInfo *ci = L->ci;
184
221
ptrdiff_t top = savestack(L, L->top);
185
ptrdiff_t ci_top = savestack(L, L->ci->top);
222
ptrdiff_t ci_top = savestack(L, ci->top);
187
224
ar.event = event;
188
225
ar.currentline = line;
189
if (event == LUA_HOOKTAILRET)
190
ar.i_ci = 0; /* tail call; no debug information about it */
192
ar.i_ci = cast_int(L->ci - L->base_ci);
193
227
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
194
L->ci->top = L->top + LUA_MINSTACK;
195
lua_assert(L->ci->top <= L->stack_last);
228
ci->top = L->top + LUA_MINSTACK;
229
lua_assert(ci->top <= L->stack_last);
196
230
L->allowhook = 0; /* cannot call hooks inside a hook */
231
ci->callstatus |= CIST_HOOKED;
200
235
lua_assert(!L->allowhook);
201
236
L->allowhook = 1;
202
L->ci->top = restorestack(L, ci_top);
237
ci->top = restorestack(L, ci_top);
203
238
L->top = restorestack(L, top);
239
ci->callstatus &= ~CIST_HOOKED;
244
static void callhook (lua_State *L, CallInfo *ci) {
245
int hook = LUA_HOOKCALL;
246
ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */
247
if (isLua(ci->previous) &&
248
GET_OPCODE(*(ci->previous->u.l.savedpc - 1)) == OP_TAILCALL) {
249
ci->callstatus |= CIST_TAIL;
250
hook = LUA_HOOKTAILCALL;
252
luaD_hook(L, hook, -1);
253
ci->u.l.savedpc--; /* correct 'pc' */
208
257
static StkId adjust_varargs (lua_State *L, Proto *p, int actual) {
210
259
int nfixargs = p->numparams;
212
260
StkId base, fixed;
213
for (; actual < nfixargs; ++actual)
214
setnilvalue(L->top++);
215
#if defined(LUA_COMPAT_VARARG)
216
if (p->is_vararg & VARARG_NEEDSARG) { /* compat. with old-style vararg? */
217
int nvar = actual - nfixargs; /* number of extra arguments */
218
lua_assert(p->is_vararg & VARARG_HASARG);
220
htab = luaH_new(L, nvar, 1); /* create `arg' table */
221
for (i=0; i<nvar; i++) /* put extra arguments into `arg' table */
222
setobj2n(L, luaH_setnum(L, htab, i+1), L->top - nvar + i);
223
/* store counter in field `n' */
224
setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), cast_num(nvar));
261
lua_assert(actual >= nfixargs);
227
262
/* move fixed parameters to final position */
263
luaD_checkstack(L, p->maxstacksize); /* check again for new 'base' */
228
264
fixed = L->top - actual; /* first fixed argument */
229
265
base = L->top; /* final position of first argument */
230
266
for (i=0; i<nfixargs; i++) {
231
setobjs2s(L, L->top++, fixed+i);
232
setnilvalue(fixed+i);
234
/* add `arg' parameter */
236
sethvalue(L, L->top++, htab);
237
lua_assert(iswhite(obj2gco(htab)));
267
setobjs2s(L, L->top++, fixed + i);
268
setnilvalue(fixed + i);
260
((L->ci == L->end_ci) ? growCI(L) : \
261
(condhardstacktests(luaD_reallocCI(L, L->size_ci)), ++L->ci))
290
#define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L)))
294
** returns true if function has been executed (C function)
264
296
int luaD_precall (lua_State *L, StkId func, int nresults) {
267
if (!ttisfunction(func)) /* `func' is not a function? */
268
func = tryfuncTM(L, func); /* check the `function' tag method */
269
funcr = savestack(L, func);
270
cl = &clvalue(func)->l;
271
L->ci->savedpc = L->savedpc;
272
if (!cl->isC) { /* Lua function? prepare its call */
276
luaD_checkstack(L, p->maxstacksize);
277
func = restorestack(L, funcr);
278
if (!p->is_vararg) { /* no varargs? */
280
if (L->top > base + p->numparams)
281
L->top = base + p->numparams;
283
else { /* vararg function */
284
int nargs = cast_int(L->top - func) - 1;
285
base = adjust_varargs(L, p, nargs);
286
func = restorestack(L, funcr); /* previous call may change the stack */
288
ci = inc_ci(L); /* now `enter' new function */
290
L->base = ci->base = base;
291
ci->top = L->base + p->maxstacksize;
292
lua_assert(ci->top <= L->stack_last);
293
L->savedpc = p->code; /* starting point */
295
ci->nresults = nresults;
296
for (st = L->top; st < ci->top; st++)
299
if (L->hookmask & LUA_MASKCALL) {
300
L->savedpc++; /* hooks assume 'pc' is already incremented */
301
luaD_callhook(L, LUA_HOOKCALL, -1);
302
L->savedpc--; /* correct 'pc' */
306
else { /* if is a C function, call it */
309
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
310
ci = inc_ci(L); /* now `enter' new function */
311
ci->func = restorestack(L, funcr);
312
L->base = ci->base = ci->func + 1;
313
ci->top = L->top + LUA_MINSTACK;
314
lua_assert(ci->top <= L->stack_last);
315
ci->nresults = nresults;
316
if (L->hookmask & LUA_MASKCALL)
317
luaD_callhook(L, LUA_HOOKCALL, -1);
319
n = (*curr_func(L)->c.f)(L); /* do the actual call */
321
if (n < 0) /* yielding? */
299
int n; /* number of arguments (Lua) or returns (C) */
300
ptrdiff_t funcr = savestack(L, func);
301
switch (ttype(func)) {
302
case LUA_TLCF: /* light C function */
305
case LUA_TCCL: { /* C closure */
306
f = clCvalue(func)->f;
308
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
309
ci = next_ci(L); /* now 'enter' new function */
310
ci->nresults = nresults;
311
ci->func = restorestack(L, funcr);
312
ci->top = L->top + LUA_MINSTACK;
313
lua_assert(ci->top <= L->stack_last);
315
luaC_checkGC(L); /* stack grow uses memory */
316
if (L->hookmask & LUA_MASKCALL)
317
luaD_hook(L, LUA_HOOKCALL, -1);
319
n = (*f)(L); /* do the actual call */
321
api_checknelems(L, n);
324
322
luaD_poscall(L, L->top - n);
331
static StkId callrethooks (lua_State *L, StkId firstResult) {
332
ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */
333
luaD_callhook(L, LUA_HOOKRET, -1);
334
if (f_isLua(L->ci)) { /* Lua function? */
335
while ((L->hookmask & LUA_MASKRET) && L->ci->tailcalls--) /* tail calls */
336
luaD_callhook(L, LUA_HOOKTAILRET, -1);
338
return restorestack(L, fr);
325
case LUA_TLCL: { /* Lua function: prepare its call */
327
Proto *p = clLvalue(func)->p;
328
n = cast_int(L->top - func) - 1; /* number of real arguments */
329
luaD_checkstack(L, p->maxstacksize);
330
for (; n < p->numparams; n++)
331
setnilvalue(L->top++); /* complete missing arguments */
333
func = restorestack(L, funcr);
337
base = adjust_varargs(L, p, n);
338
func = restorestack(L, funcr); /* previous call can change stack */
340
ci = next_ci(L); /* now 'enter' new function */
341
ci->nresults = nresults;
344
ci->top = base + p->maxstacksize;
345
lua_assert(ci->top <= L->stack_last);
346
ci->u.l.savedpc = p->code; /* starting point */
347
ci->callstatus = CIST_LUA;
349
luaC_checkGC(L); /* stack grow uses memory */
350
if (L->hookmask & LUA_MASKCALL)
354
default: { /* not a function */
355
func = tryfuncTM(L, func); /* retry with 'function' tag method */
356
return luaD_precall(L, func, nresults); /* now it must be a function */
342
362
int luaD_poscall (lua_State *L, StkId firstResult) {
346
if (L->hookmask & LUA_MASKRET)
347
firstResult = callrethooks(L, firstResult);
365
CallInfo *ci = L->ci;
366
if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) {
367
if (L->hookmask & LUA_MASKRET) {
368
ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */
369
luaD_hook(L, LUA_HOOKRET, -1);
370
firstResult = restorestack(L, fr);
372
L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */
349
374
res = ci->func; /* res == final position of 1st result */
350
375
wanted = ci->nresults;
351
L->base = (ci - 1)->base; /* restore base */
352
L->savedpc = (ci - 1)->savedpc; /* restore savedpc */
376
L->ci = ci = ci->previous; /* back to caller */
353
377
/* move results to correct place */
354
378
for (i = wanted; i != 0 && firstResult < L->top; i--)
355
379
setobjs2s(L, res++, firstResult++);
365
389
** The arguments are on the stack, right after the function.
366
390
** When returns, all the results are on the stack, starting at the original
367
391
** function position.
369
void luaD_call (lua_State *L, StkId func, int nResults) {
393
void luaD_call (lua_State *L, StkId func, int nResults, int allowyield) {
370
394
if (++L->nCcalls >= LUAI_MAXCCALLS) {
371
395
if (L->nCcalls == LUAI_MAXCCALLS)
372
396
luaG_runerror(L, "C stack overflow");
373
397
else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
374
398
luaD_throw(L, LUA_ERRERR); /* error while handing stack error */
376
if (luaD_precall(L, func, nResults) == PCRLUA) /* is a Lua function? */
377
luaV_execute(L, 1); /* call it */
400
if (!allowyield) L->nny++;
401
if (!luaD_precall(L, func, nResults)) /* is a Lua function? */
402
luaV_execute(L); /* call it */
403
if (!allowyield) L->nny--;
408
static void finishCcall (lua_State *L) {
409
CallInfo *ci = L->ci;
411
lua_assert(ci->u.c.k != NULL); /* must have a continuation */
412
lua_assert(L->nny == 0);
413
if (ci->callstatus & CIST_YPCALL) { /* was inside a pcall? */
414
ci->callstatus &= ~CIST_YPCALL; /* finish 'lua_pcall' */
415
L->errfunc = ci->u.c.old_errfunc;
417
/* finish 'lua_callk'/'lua_pcall' */
418
adjustresults(L, ci->nresults);
419
/* call continuation function */
420
if (!(ci->callstatus & CIST_STAT)) /* no call status? */
421
ci->u.c.status = LUA_YIELD; /* 'default' status */
422
lua_assert(ci->u.c.status != LUA_OK);
423
ci->callstatus = (ci->callstatus & ~(CIST_YPCALL | CIST_STAT)) | CIST_YIELDED;
427
api_checknelems(L, n);
428
/* finish 'luaD_precall' */
429
luaD_poscall(L, L->top - n);
433
static void unroll (lua_State *L, void *ud) {
436
if (L->ci == &L->base_ci) /* stack is empty? */
437
return; /* coroutine finished normally */
438
if (!isLua(L->ci)) /* C function? */
440
else { /* Lua function */
441
luaV_finishOp(L); /* finish interrupted instruction */
442
luaV_execute(L); /* execute down to higher C 'boundary' */
449
** check whether thread has a suspended protected call
451
static CallInfo *findpcall (lua_State *L) {
453
for (ci = L->ci; ci != NULL; ci = ci->previous) { /* search for a pcall */
454
if (ci->callstatus & CIST_YPCALL)
457
return NULL; /* no pending pcall */
461
static int recover (lua_State *L, int status) {
463
CallInfo *ci = findpcall(L);
464
if (ci == NULL) return 0; /* no recovery point */
465
/* "finish" luaD_pcall */
466
oldtop = restorestack(L, ci->extra);
467
luaF_close(L, oldtop);
468
seterrorobj(L, status, oldtop);
470
L->allowhook = ci->u.c.old_allowhook;
471
L->nny = 0; /* should be zero to be yieldable */
473
L->errfunc = ci->u.c.old_errfunc;
474
ci->callstatus |= CIST_STAT; /* call has error status */
475
ci->u.c.status = status; /* (here it is) */
476
return 1; /* continue running the coroutine */
481
** signal an error in the call to 'resume', not in the execution of the
482
** coroutine itself. (Such errors should not be handled by any coroutine
483
** error handler and should not kill the coroutine.)
485
static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) {
486
L->top = firstArg; /* remove args from the stack */
487
setsvalue2s(L, L->top, luaS_new(L, msg)); /* push error message */
489
luaD_throw(L, -1); /* jump back to 'lua_resume' */
494
** do the work for 'lua_resume' in protected mode
383
496
static void resume (lua_State *L, void *ud) {
497
int nCcalls = L->nCcalls;
384
498
StkId firstArg = cast(StkId, ud);
385
499
CallInfo *ci = L->ci;
386
if (L->status == 0) { /* start coroutine? */
387
lua_assert(ci == L->base_ci && firstArg > L->base);
388
if (luaD_precall(L, firstArg - 1, LUA_MULTRET) != PCRLUA)
500
if (nCcalls >= LUAI_MAXCCALLS)
501
resume_error(L, "C stack overflow", firstArg);
502
if (L->status == LUA_OK) { /* may be starting a coroutine */
503
if (ci != &L->base_ci) /* not in base level? */
504
resume_error(L, "cannot resume non-suspended coroutine", firstArg);
505
/* coroutine is in base level; start running it */
506
if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */
507
luaV_execute(L); /* call it */
509
else if (L->status != LUA_YIELD)
510
resume_error(L, "cannot resume dead coroutine", firstArg);
391
511
else { /* resuming from previous yield */
392
lua_assert(L->status == LUA_YIELD);
394
if (!f_isLua(ci)) { /* `common' yield? */
395
/* finish interrupted execution of `OP_CALL' */
396
lua_assert(GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_CALL ||
397
GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_TAILCALL);
398
if (luaD_poscall(L, firstArg)) /* complete it... */
399
L->top = L->ci->top; /* and correct top if not multiple results */
513
ci->func = restorestack(L, ci->extra);
514
if (isLua(ci)) /* yielded inside a hook? */
515
luaV_execute(L); /* just continue running Lua code */
516
else { /* 'common' yield */
517
if (ci->u.c.k != NULL) { /* does it have a continuation? */
519
ci->u.c.status = LUA_YIELD; /* 'default' status */
520
ci->callstatus |= CIST_YIELDED;
522
n = (*ci->u.c.k)(L); /* call continuation */
524
api_checknelems(L, n);
525
firstArg = L->top - n; /* yield results come from continuation */
527
luaD_poscall(L, firstArg); /* finish 'luaD_precall' */
401
else /* yielded inside a hook: just continue its execution */
402
L->base = L->ci->base;
404
luaV_execute(L, cast_int(L->ci - L->base_ci));
408
static int resume_error (lua_State *L, const char *msg) {
409
L->top = L->ci->base;
410
setsvalue2s(L, L->top, luaS_new(L, msg));
417
LUA_API int lua_resume (lua_State *L, int nargs) {
531
lua_assert(nCcalls == L->nCcalls);
535
LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) {
537
int oldnny = L->nny; /* save 'nny' */
420
if (L->status != LUA_YIELD && (L->status != 0 || L->ci != L->base_ci))
421
return resume_error(L, "cannot resume non-suspended coroutine");
422
if (L->nCcalls >= LUAI_MAXCCALLS)
423
return resume_error(L, "C stack overflow");
424
539
luai_userstateresume(L, nargs);
425
lua_assert(L->errfunc == 0);
426
L->baseCcalls = ++L->nCcalls;
540
L->nCcalls = (from) ? from->nCcalls + 1 : 1;
541
L->nny = 0; /* allow yields */
542
api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs);
427
543
status = luaD_rawrunprotected(L, resume, L->top - nargs);
428
if (status != 0) { /* error? */
429
L->status = cast_byte(status); /* mark thread as `dead' */
430
luaD_seterrorobj(L, status, L->top);
434
lua_assert(L->nCcalls == L->baseCcalls);
544
if (status == -1) /* error calling 'lua_resume'? */
546
else { /* yield or regular error */
547
while (status != LUA_OK && status != LUA_YIELD) { /* error? */
548
if (recover(L, status)) /* recover point? */
549
status = luaD_rawrunprotected(L, unroll, NULL); /* run continuation */
550
else { /* unrecoverable error */
551
L->status = cast_byte(status); /* mark thread as `dead' */
552
seterrorobj(L, status, L->top);
557
lua_assert(status == L->status);
559
L->nny = oldnny; /* restore 'nny' */
561
lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0));
443
LUA_API int lua_yield (lua_State *L, int nresults) {
567
LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) {
568
CallInfo *ci = L->ci;
444
569
luai_userstateyield(L, nresults);
446
if (L->nCcalls > L->baseCcalls)
447
luaG_runerror(L, "attempt to yield across metamethod/C-call boundary");
448
L->base = L->top - nresults; /* protect stack slots below */
571
api_checknelems(L, nresults);
573
if (L != G(L)->mainthread)
574
luaG_runerror(L, "attempt to yield across a C-call boundary");
576
luaG_runerror(L, "attempt to yield from outside a coroutine");
449
578
L->status = LUA_YIELD;
579
ci->extra = savestack(L, ci->func); /* save current 'func' */
580
if (isLua(ci)) { /* inside a hook? */
581
api_check(L, k == NULL, "hooks cannot continue after yielding");
584
if ((ci->u.c.k = k) != NULL) /* is there a continuation? */
585
ci->u.c.ctx = ctx; /* save context */
586
ci->func = L->top - nresults - 1; /* protect stack below results */
587
luaD_throw(L, LUA_YIELD);
589
lua_assert(ci->callstatus & CIST_HOOKED); /* must be inside a hook */
591
return 0; /* return to 'luaD_hook' */
455
595
int luaD_pcall (lua_State *L, Pfunc func, void *u,
456
596
ptrdiff_t old_top, ptrdiff_t ef) {
458
unsigned short oldnCcalls = L->nCcalls;
459
ptrdiff_t old_ci = saveci(L, L->ci);
598
CallInfo *old_ci = L->ci;
460
599
lu_byte old_allowhooks = L->allowhook;
600
unsigned short old_nny = L->nny;
461
601
ptrdiff_t old_errfunc = L->errfunc;
463
603
status = luaD_rawrunprotected(L, func, u);
464
if (status != 0) { /* an error occurred? */
604
if (status != LUA_OK) { /* an error occurred? */
465
605
StkId oldtop = restorestack(L, old_top);
466
luaF_close(L, oldtop); /* close eventual pending closures */
467
luaD_seterrorobj(L, status, oldtop);
468
L->nCcalls = oldnCcalls;
469
L->ci = restoreci(L, old_ci);
470
L->base = L->ci->base;
471
L->savedpc = L->ci->savedpc;
606
luaF_close(L, oldtop); /* close possible pending closures */
607
seterrorobj(L, status, oldtop);
472
609
L->allowhook = old_allowhooks;
473
restore_stack_limit(L);
475
613
L->errfunc = old_errfunc;
484
622
struct SParser { /* data to `f_parser' */
486
Mbuffer buff; /* buffer to be used by the scanner */
624
Mbuffer buff; /* dynamic structure used by the scanner */
625
Dyndata dyd; /* dynamic structures used by the parser */
487
627
const char *name;
631
static void checkmode (lua_State *L, const char *mode, const char *x) {
632
if (mode && strchr(mode, x[0]) == NULL) {
634
"attempt to load a %s chunk (mode is " LUA_QS ")", x, mode);
635
luaD_throw(L, LUA_ERRSYNTAX);
490
640
static void f_parser (lua_State *L, void *ud) {
494
643
struct SParser *p = cast(struct SParser *, ud);
495
int c = luaZ_lookahead(p->z);
497
tf = ((c == LUA_SIGNATURE[0]) ? luaU_undump : luaY_parser)(L, p->z,
499
cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L)));
501
for (i = 0; i < tf->nups; i++) /* initialize eventual upvalues */
502
cl->l.upvals[i] = luaF_newupval(L);
503
setclvalue(L, L->top, cl);
644
int c = zgetc(p->z); /* read first character */
645
if (c == LUA_SIGNATURE[0]) {
646
checkmode(L, p->mode, "binary");
647
cl = luaU_undump(L, p->z, &p->buff, p->name);
650
checkmode(L, p->mode, "text");
651
cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);
653
lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues);
654
for (i = 0; i < cl->l.nupvalues; i++) { /* initialize upvalues */
655
UpVal *up = luaF_newupval(L);
656
cl->l.upvals[i] = up;
657
luaC_objbarrier(L, cl, up);
508
int luaD_protectedparser (lua_State *L, ZIO *z, const char *name) {
662
int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
509
664
struct SParser p;
511
p.z = z; p.name = name;
666
L->nny++; /* cannot yield during parsing */
667
p.z = z; p.name = name; p.mode = mode;
668
p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0;
669
p.dyd.gt.arr = NULL; p.dyd.gt.size = 0;
670
p.dyd.label.arr = NULL; p.dyd.label.size = 0;
512
671
luaZ_initbuffer(L, &p.buff);
513
672
status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);
514
673
luaZ_freebuffer(L, &p.buff);
674
luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size);
675
luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size);
676
luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size);