annotate lib/lua/lua-5.2.2/ldo.c @ 2287:4e3236a4ea63

GenerateItemsInChest cleaned up a bit, unified the two switches
author Grumpy7
date Sat, 15 Mar 2014 23:42:51 +0100
parents 41cc4dd3c122
children
rev   line source
1866
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
2 ** $Id: ldo.c,v 2.108 2012/10/01 14:05:04 roberto Exp $
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
3 ** Stack and Call structure of Lua
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
4 ** See Copyright Notice in lua.h
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
5 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
6
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
7
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
8 #include <setjmp.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
9 #include <stdlib.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
10 #include <string.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
11
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
12 #define ldo_c
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
13 #define LUA_CORE
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
14
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
15 #include "lua.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
16
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
17 #include "lapi.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
18 #include "ldebug.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
19 #include "ldo.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
20 #include "lfunc.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
21 #include "lgc.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
22 #include "lmem.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
23 #include "lobject.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
24 #include "lopcodes.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
25 #include "lparser.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
26 #include "lstate.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
27 #include "lstring.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
28 #include "ltable.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
29 #include "ltm.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
30 #include "lundump.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
31 #include "lvm.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
32 #include "lzio.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
33
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
34
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
35
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
36
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
37 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
38 ** {======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
39 ** Error-recovery functions
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
40 ** =======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
41 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
42
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
43 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
44 ** LUAI_THROW/LUAI_TRY define how Lua does exception handling. By
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
45 ** default, Lua handles errors with exceptions when compiling as
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
46 ** C++ code, with _longjmp/_setjmp when asked to use them, and with
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
47 ** longjmp/setjmp otherwise.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
48 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
49 #if !defined(LUAI_THROW)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
50
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
51 #if defined(__cplusplus) && !defined(LUA_USE_LONGJMP)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
52 /* C++ exceptions */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
53 #define LUAI_THROW(L,c) throw(c)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
54 #define LUAI_TRY(L,c,a) \
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
55 try { a } catch(...) { if ((c)->status == 0) (c)->status = -1; }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
56 #define luai_jmpbuf int /* dummy variable */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
57
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
58 #elif defined(LUA_USE_ULONGJMP)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
59 /* in Unix, try _longjmp/_setjmp (more efficient) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
60 #define LUAI_THROW(L,c) _longjmp((c)->b, 1)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
61 #define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
62 #define luai_jmpbuf jmp_buf
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
63
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
64 #else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
65 /* default handling with long jumps */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
66 #define LUAI_THROW(L,c) longjmp((c)->b, 1)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
67 #define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
68 #define luai_jmpbuf jmp_buf
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
69
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
70 #endif
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
71
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
72 #endif
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
73
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
74
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
75
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
76 /* chain list of long jump buffers */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
77 struct lua_longjmp {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
78 struct lua_longjmp *previous;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
79 luai_jmpbuf b;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
80 volatile int status; /* error code */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
81 };
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
82
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
83
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
84 static void seterrorobj (lua_State *L, int errcode, StkId oldtop) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
85 switch (errcode) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
86 case LUA_ERRMEM: { /* memory error? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
87 setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
88 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
89 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
90 case LUA_ERRERR: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
91 setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling"));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
92 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
93 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
94 default: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
95 setobjs2s(L, oldtop, L->top - 1); /* error message on current top */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
96 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
97 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
98 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
99 L->top = oldtop + 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
100 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
101
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
102
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
103 l_noret luaD_throw (lua_State *L, int errcode) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
104 if (L->errorJmp) { /* thread has an error handler? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
105 L->errorJmp->status = errcode; /* set status */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
106 LUAI_THROW(L, L->errorJmp); /* jump to it */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
107 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
108 else { /* thread has no error handler */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
109 L->status = cast_byte(errcode); /* mark it as dead */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
110 if (G(L)->mainthread->errorJmp) { /* main thread has a handler? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
111 setobjs2s(L, G(L)->mainthread->top++, L->top - 1); /* copy error obj. */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
112 luaD_throw(G(L)->mainthread, errcode); /* re-throw in main thread */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
113 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
114 else { /* no handler at all; abort */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
115 if (G(L)->panic) { /* panic function? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
116 lua_unlock(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
117 G(L)->panic(L); /* call it (last chance to jump out) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
118 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
119 abort();
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
120 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
121 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
122 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
123
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
124
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
125 int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
126 unsigned short oldnCcalls = L->nCcalls;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
127 struct lua_longjmp lj;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
128 lj.status = LUA_OK;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
129 lj.previous = L->errorJmp; /* chain new error handler */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
130 L->errorJmp = &lj;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
131 LUAI_TRY(L, &lj,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
132 (*f)(L, ud);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
133 );
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
134 L->errorJmp = lj.previous; /* restore old error handler */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
135 L->nCcalls = oldnCcalls;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
136 return lj.status;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
137 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
138
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
139 /* }====================================================== */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
140
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
141
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
142 static void correctstack (lua_State *L, TValue *oldstack) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
143 CallInfo *ci;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
144 GCObject *up;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
145 L->top = (L->top - oldstack) + L->stack;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
146 for (up = L->openupval; up != NULL; up = up->gch.next)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
147 gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
148 for (ci = L->ci; ci != NULL; ci = ci->previous) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
149 ci->top = (ci->top - oldstack) + L->stack;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
150 ci->func = (ci->func - oldstack) + L->stack;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
151 if (isLua(ci))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
152 ci->u.l.base = (ci->u.l.base - oldstack) + L->stack;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
153 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
154 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
155
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
156
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
157 /* some space for error handling */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
158 #define ERRORSTACKSIZE (LUAI_MAXSTACK + 200)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
159
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
160
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
161 void luaD_reallocstack (lua_State *L, int newsize) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
162 TValue *oldstack = L->stack;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
163 int lim = L->stacksize;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
164 lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
165 lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
166 luaM_reallocvector(L, L->stack, L->stacksize, newsize, TValue);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
167 for (; lim < newsize; lim++)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
168 setnilvalue(L->stack + lim); /* erase new segment */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
169 L->stacksize = newsize;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
170 L->stack_last = L->stack + newsize - EXTRA_STACK;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
171 correctstack(L, oldstack);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
172 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
173
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
174
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
175 void luaD_growstack (lua_State *L, int n) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
176 int size = L->stacksize;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
177 if (size > LUAI_MAXSTACK) /* error after extra size? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
178 luaD_throw(L, LUA_ERRERR);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
179 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
180 int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
181 int newsize = 2 * size;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
182 if (newsize > LUAI_MAXSTACK) newsize = LUAI_MAXSTACK;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
183 if (newsize < needed) newsize = needed;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
184 if (newsize > LUAI_MAXSTACK) { /* stack overflow? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
185 luaD_reallocstack(L, ERRORSTACKSIZE);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
186 luaG_runerror(L, "stack overflow");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
187 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
188 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
189 luaD_reallocstack(L, newsize);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
190 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
191 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
192
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
193
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
194 static int stackinuse (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
195 CallInfo *ci;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
196 StkId lim = L->top;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
197 for (ci = L->ci; ci != NULL; ci = ci->previous) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
198 lua_assert(ci->top <= L->stack_last);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
199 if (lim < ci->top) lim = ci->top;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
200 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
201 return cast_int(lim - L->stack) + 1; /* part of stack in use */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
202 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
203
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
204
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
205 void luaD_shrinkstack (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
206 int inuse = stackinuse(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
207 int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
208 if (goodsize > LUAI_MAXSTACK) goodsize = LUAI_MAXSTACK;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
209 if (inuse > LUAI_MAXSTACK || /* handling stack overflow? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
210 goodsize >= L->stacksize) /* would grow instead of shrink? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
211 condmovestack(L); /* don't change stack (change only for debugging) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
212 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
213 luaD_reallocstack(L, goodsize); /* shrink it */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
214 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
215
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
216
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
217 void luaD_hook (lua_State *L, int event, int line) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
218 lua_Hook hook = L->hook;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
219 if (hook && L->allowhook) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
220 CallInfo *ci = L->ci;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
221 ptrdiff_t top = savestack(L, L->top);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
222 ptrdiff_t ci_top = savestack(L, ci->top);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
223 lua_Debug ar;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
224 ar.event = event;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
225 ar.currentline = line;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
226 ar.i_ci = ci;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
227 luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
228 ci->top = L->top + LUA_MINSTACK;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
229 lua_assert(ci->top <= L->stack_last);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
230 L->allowhook = 0; /* cannot call hooks inside a hook */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
231 ci->callstatus |= CIST_HOOKED;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
232 lua_unlock(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
233 (*hook)(L, &ar);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
234 lua_lock(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
235 lua_assert(!L->allowhook);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
236 L->allowhook = 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
237 ci->top = restorestack(L, ci_top);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
238 L->top = restorestack(L, top);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
239 ci->callstatus &= ~CIST_HOOKED;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
240 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
241 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
242
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
243
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
244 static void callhook (lua_State *L, CallInfo *ci) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
245 int hook = LUA_HOOKCALL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
246 ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
247 if (isLua(ci->previous) &&
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
248 GET_OPCODE(*(ci->previous->u.l.savedpc - 1)) == OP_TAILCALL) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
249 ci->callstatus |= CIST_TAIL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
250 hook = LUA_HOOKTAILCALL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
251 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
252 luaD_hook(L, hook, -1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
253 ci->u.l.savedpc--; /* correct 'pc' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
254 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
255
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
256
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
257 static StkId adjust_varargs (lua_State *L, Proto *p, int actual) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
258 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
259 int nfixargs = p->numparams;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
260 StkId base, fixed;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
261 lua_assert(actual >= nfixargs);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
262 /* move fixed parameters to final position */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
263 fixed = L->top - actual; /* first fixed argument */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
264 base = L->top; /* final position of first argument */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
265 for (i=0; i<nfixargs; i++) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
266 setobjs2s(L, L->top++, fixed + i);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
267 setnilvalue(fixed + i);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
268 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
269 return base;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
270 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
271
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
272
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
273 static StkId tryfuncTM (lua_State *L, StkId func) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
274 const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
275 StkId p;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
276 ptrdiff_t funcr = savestack(L, func);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
277 if (!ttisfunction(tm))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
278 luaG_typeerror(L, func, "call");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
279 /* Open a hole inside the stack at `func' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
280 for (p = L->top; p > func; p--) setobjs2s(L, p, p-1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
281 incr_top(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
282 func = restorestack(L, funcr); /* previous call may change stack */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
283 setobj2s(L, func, tm); /* tag method is the new function to be called */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
284 return func;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
285 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
286
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
287
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
288
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
289 #define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L)))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
290
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
291
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
292 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
293 ** returns true if function has been executed (C function)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
294 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
295 int luaD_precall (lua_State *L, StkId func, int nresults) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
296 lua_CFunction f;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
297 CallInfo *ci;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
298 int n; /* number of arguments (Lua) or returns (C) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
299 ptrdiff_t funcr = savestack(L, func);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
300 switch (ttype(func)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
301 case LUA_TLCF: /* light C function */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
302 f = fvalue(func);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
303 goto Cfunc;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
304 case LUA_TCCL: { /* C closure */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
305 f = clCvalue(func)->f;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
306 Cfunc:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
307 luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
308 ci = next_ci(L); /* now 'enter' new function */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
309 ci->nresults = nresults;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
310 ci->func = restorestack(L, funcr);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
311 ci->top = L->top + LUA_MINSTACK;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
312 lua_assert(ci->top <= L->stack_last);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
313 ci->callstatus = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
314 luaC_checkGC(L); /* stack grow uses memory */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
315 if (L->hookmask & LUA_MASKCALL)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
316 luaD_hook(L, LUA_HOOKCALL, -1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
317 lua_unlock(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
318 n = (*f)(L); /* do the actual call */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
319 lua_lock(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
320 api_checknelems(L, n);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
321 luaD_poscall(L, L->top - n);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
322 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
323 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
324 case LUA_TLCL: { /* Lua function: prepare its call */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
325 StkId base;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
326 Proto *p = clLvalue(func)->p;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
327 luaD_checkstack(L, p->maxstacksize);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
328 func = restorestack(L, funcr);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
329 n = cast_int(L->top - func) - 1; /* number of real arguments */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
330 for (; n < p->numparams; n++)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
331 setnilvalue(L->top++); /* complete missing arguments */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
332 base = (!p->is_vararg) ? func + 1 : adjust_varargs(L, p, n);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
333 ci = next_ci(L); /* now 'enter' new function */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
334 ci->nresults = nresults;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
335 ci->func = func;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
336 ci->u.l.base = base;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
337 ci->top = base + p->maxstacksize;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
338 lua_assert(ci->top <= L->stack_last);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
339 ci->u.l.savedpc = p->code; /* starting point */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
340 ci->callstatus = CIST_LUA;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
341 L->top = ci->top;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
342 luaC_checkGC(L); /* stack grow uses memory */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
343 if (L->hookmask & LUA_MASKCALL)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
344 callhook(L, ci);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
345 return 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
346 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
347 default: { /* not a function */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
348 func = tryfuncTM(L, func); /* retry with 'function' tag method */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
349 return luaD_precall(L, func, nresults); /* now it must be a function */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
350 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
351 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
352 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
353
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
354
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
355 int luaD_poscall (lua_State *L, StkId firstResult) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
356 StkId res;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
357 int wanted, i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
358 CallInfo *ci = L->ci;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
359 if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
360 if (L->hookmask & LUA_MASKRET) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
361 ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
362 luaD_hook(L, LUA_HOOKRET, -1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
363 firstResult = restorestack(L, fr);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
364 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
365 L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
366 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
367 res = ci->func; /* res == final position of 1st result */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
368 wanted = ci->nresults;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
369 L->ci = ci = ci->previous; /* back to caller */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
370 /* move results to correct place */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
371 for (i = wanted; i != 0 && firstResult < L->top; i--)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
372 setobjs2s(L, res++, firstResult++);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
373 while (i-- > 0)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
374 setnilvalue(res++);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
375 L->top = res;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
376 return (wanted - LUA_MULTRET); /* 0 iff wanted == LUA_MULTRET */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
377 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
378
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
379
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
380 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
381 ** Call a function (C or Lua). The function to be called is at *func.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
382 ** The arguments are on the stack, right after the function.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
383 ** When returns, all the results are on the stack, starting at the original
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
384 ** function position.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
385 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
386 void luaD_call (lua_State *L, StkId func, int nResults, int allowyield) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
387 if (++L->nCcalls >= LUAI_MAXCCALLS) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
388 if (L->nCcalls == LUAI_MAXCCALLS)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
389 luaG_runerror(L, "C stack overflow");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
390 else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
391 luaD_throw(L, LUA_ERRERR); /* error while handing stack error */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
392 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
393 if (!allowyield) L->nny++;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
394 if (!luaD_precall(L, func, nResults)) /* is a Lua function? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
395 luaV_execute(L); /* call it */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
396 if (!allowyield) L->nny--;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
397 L->nCcalls--;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
398 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
399
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
400
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
401 static void finishCcall (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
402 CallInfo *ci = L->ci;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
403 int n;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
404 lua_assert(ci->u.c.k != NULL); /* must have a continuation */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
405 lua_assert(L->nny == 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
406 if (ci->callstatus & CIST_YPCALL) { /* was inside a pcall? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
407 ci->callstatus &= ~CIST_YPCALL; /* finish 'lua_pcall' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
408 L->errfunc = ci->u.c.old_errfunc;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
409 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
410 /* finish 'lua_callk'/'lua_pcall' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
411 adjustresults(L, ci->nresults);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
412 /* call continuation function */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
413 if (!(ci->callstatus & CIST_STAT)) /* no call status? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
414 ci->u.c.status = LUA_YIELD; /* 'default' status */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
415 lua_assert(ci->u.c.status != LUA_OK);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
416 ci->callstatus = (ci->callstatus & ~(CIST_YPCALL | CIST_STAT)) | CIST_YIELDED;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
417 lua_unlock(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
418 n = (*ci->u.c.k)(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
419 lua_lock(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
420 api_checknelems(L, n);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
421 /* finish 'luaD_precall' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
422 luaD_poscall(L, L->top - n);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
423 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
424
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
425
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
426 static void unroll (lua_State *L, void *ud) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
427 UNUSED(ud);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
428 for (;;) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
429 if (L->ci == &L->base_ci) /* stack is empty? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
430 return; /* coroutine finished normally */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
431 if (!isLua(L->ci)) /* C function? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
432 finishCcall(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
433 else { /* Lua function */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
434 luaV_finishOp(L); /* finish interrupted instruction */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
435 luaV_execute(L); /* execute down to higher C 'boundary' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
436 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
437 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
438 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
439
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
440
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
441 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
442 ** check whether thread has a suspended protected call
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
443 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
444 static CallInfo *findpcall (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
445 CallInfo *ci;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
446 for (ci = L->ci; ci != NULL; ci = ci->previous) { /* search for a pcall */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
447 if (ci->callstatus & CIST_YPCALL)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
448 return ci;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
449 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
450 return NULL; /* no pending pcall */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
451 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
452
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
453
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
454 static int recover (lua_State *L, int status) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
455 StkId oldtop;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
456 CallInfo *ci = findpcall(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
457 if (ci == NULL) return 0; /* no recovery point */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
458 /* "finish" luaD_pcall */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
459 oldtop = restorestack(L, ci->extra);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
460 luaF_close(L, oldtop);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
461 seterrorobj(L, status, oldtop);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
462 L->ci = ci;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
463 L->allowhook = ci->u.c.old_allowhook;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
464 L->nny = 0; /* should be zero to be yieldable */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
465 luaD_shrinkstack(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
466 L->errfunc = ci->u.c.old_errfunc;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
467 ci->callstatus |= CIST_STAT; /* call has error status */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
468 ci->u.c.status = status; /* (here it is) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
469 return 1; /* continue running the coroutine */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
470 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
471
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
472
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
473 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
474 ** signal an error in the call to 'resume', not in the execution of the
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
475 ** coroutine itself. (Such errors should not be handled by any coroutine
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
476 ** error handler and should not kill the coroutine.)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
477 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
478 static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
479 L->top = firstArg; /* remove args from the stack */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
480 setsvalue2s(L, L->top, luaS_new(L, msg)); /* push error message */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
481 api_incr_top(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
482 luaD_throw(L, -1); /* jump back to 'lua_resume' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
483 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
484
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
485
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
486 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
487 ** do the work for 'lua_resume' in protected mode
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
488 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
489 static void resume (lua_State *L, void *ud) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
490 int nCcalls = L->nCcalls;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
491 StkId firstArg = cast(StkId, ud);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
492 CallInfo *ci = L->ci;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
493 if (nCcalls >= LUAI_MAXCCALLS)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
494 resume_error(L, "C stack overflow", firstArg);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
495 if (L->status == LUA_OK) { /* may be starting a coroutine */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
496 if (ci != &L->base_ci) /* not in base level? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
497 resume_error(L, "cannot resume non-suspended coroutine", firstArg);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
498 /* coroutine is in base level; start running it */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
499 if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
500 luaV_execute(L); /* call it */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
501 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
502 else if (L->status != LUA_YIELD)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
503 resume_error(L, "cannot resume dead coroutine", firstArg);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
504 else { /* resuming from previous yield */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
505 L->status = LUA_OK;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
506 ci->func = restorestack(L, ci->extra);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
507 if (isLua(ci)) /* yielded inside a hook? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
508 luaV_execute(L); /* just continue running Lua code */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
509 else { /* 'common' yield */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
510 if (ci->u.c.k != NULL) { /* does it have a continuation? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
511 int n;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
512 ci->u.c.status = LUA_YIELD; /* 'default' status */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
513 ci->callstatus |= CIST_YIELDED;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
514 lua_unlock(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
515 n = (*ci->u.c.k)(L); /* call continuation */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
516 lua_lock(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
517 api_checknelems(L, n);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
518 firstArg = L->top - n; /* yield results come from continuation */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
519 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
520 luaD_poscall(L, firstArg); /* finish 'luaD_precall' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
521 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
522 unroll(L, NULL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
523 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
524 lua_assert(nCcalls == L->nCcalls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
525 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
526
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
527
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
528 LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
529 int status;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
530 lua_lock(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
531 luai_userstateresume(L, nargs);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
532 L->nCcalls = (from) ? from->nCcalls + 1 : 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
533 L->nny = 0; /* allow yields */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
534 api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
535 status = luaD_rawrunprotected(L, resume, L->top - nargs);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
536 if (status == -1) /* error calling 'lua_resume'? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
537 status = LUA_ERRRUN;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
538 else { /* yield or regular error */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
539 while (status != LUA_OK && status != LUA_YIELD) { /* error? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
540 if (recover(L, status)) /* recover point? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
541 status = luaD_rawrunprotected(L, unroll, NULL); /* run continuation */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
542 else { /* unrecoverable error */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
543 L->status = cast_byte(status); /* mark thread as `dead' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
544 seterrorobj(L, status, L->top);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
545 L->ci->top = L->top;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
546 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
547 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
548 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
549 lua_assert(status == L->status);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
550 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
551 L->nny = 1; /* do not allow yields */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
552 L->nCcalls--;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
553 lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
554 lua_unlock(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
555 return status;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
556 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
557
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
558
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
559 LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
560 CallInfo *ci = L->ci;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
561 luai_userstateyield(L, nresults);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
562 lua_lock(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
563 api_checknelems(L, nresults);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
564 if (L->nny > 0) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
565 if (L != G(L)->mainthread)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
566 luaG_runerror(L, "attempt to yield across a C-call boundary");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
567 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
568 luaG_runerror(L, "attempt to yield from outside a coroutine");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
569 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
570 L->status = LUA_YIELD;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
571 ci->extra = savestack(L, ci->func); /* save current 'func' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
572 if (isLua(ci)) { /* inside a hook? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
573 api_check(L, k == NULL, "hooks cannot continue after yielding");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
574 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
575 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
576 if ((ci->u.c.k = k) != NULL) /* is there a continuation? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
577 ci->u.c.ctx = ctx; /* save context */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
578 ci->func = L->top - nresults - 1; /* protect stack below results */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
579 luaD_throw(L, LUA_YIELD);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
580 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
581 lua_assert(ci->callstatus & CIST_HOOKED); /* must be inside a hook */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
582 lua_unlock(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
583 return 0; /* return to 'luaD_hook' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
584 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
585
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
586
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
587 int luaD_pcall (lua_State *L, Pfunc func, void *u,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
588 ptrdiff_t old_top, ptrdiff_t ef) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
589 int status;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
590 CallInfo *old_ci = L->ci;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
591 lu_byte old_allowhooks = L->allowhook;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
592 unsigned short old_nny = L->nny;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
593 ptrdiff_t old_errfunc = L->errfunc;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
594 L->errfunc = ef;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
595 status = luaD_rawrunprotected(L, func, u);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
596 if (status != LUA_OK) { /* an error occurred? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
597 StkId oldtop = restorestack(L, old_top);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
598 luaF_close(L, oldtop); /* close possible pending closures */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
599 seterrorobj(L, status, oldtop);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
600 L->ci = old_ci;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
601 L->allowhook = old_allowhooks;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
602 L->nny = old_nny;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
603 luaD_shrinkstack(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
604 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
605 L->errfunc = old_errfunc;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
606 return status;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
607 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
608
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
609
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
610
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
611 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
612 ** Execute a protected parser.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
613 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
614 struct SParser { /* data to `f_parser' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
615 ZIO *z;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
616 Mbuffer buff; /* dynamic structure used by the scanner */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
617 Dyndata dyd; /* dynamic structures used by the parser */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
618 const char *mode;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
619 const char *name;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
620 };
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
621
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
622
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
623 static void checkmode (lua_State *L, const char *mode, const char *x) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
624 if (mode && strchr(mode, x[0]) == NULL) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
625 luaO_pushfstring(L,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
626 "attempt to load a %s chunk (mode is " LUA_QS ")", x, mode);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
627 luaD_throw(L, LUA_ERRSYNTAX);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
628 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
629 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
630
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
631
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
632 static void f_parser (lua_State *L, void *ud) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
633 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
634 Closure *cl;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
635 struct SParser *p = cast(struct SParser *, ud);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
636 int c = zgetc(p->z); /* read first character */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
637 if (c == LUA_SIGNATURE[0]) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
638 checkmode(L, p->mode, "binary");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
639 cl = luaU_undump(L, p->z, &p->buff, p->name);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
640 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
641 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
642 checkmode(L, p->mode, "text");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
643 cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
644 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
645 lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
646 for (i = 0; i < cl->l.nupvalues; i++) { /* initialize upvalues */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
647 UpVal *up = luaF_newupval(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
648 cl->l.upvals[i] = up;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
649 luaC_objbarrier(L, cl, up);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
650 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
651 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
652
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
653
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
654 int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
655 const char *mode) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
656 struct SParser p;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
657 int status;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
658 L->nny++; /* cannot yield during parsing */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
659 p.z = z; p.name = name; p.mode = mode;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
660 p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
661 p.dyd.gt.arr = NULL; p.dyd.gt.size = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
662 p.dyd.label.arr = NULL; p.dyd.label.size = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
663 luaZ_initbuffer(L, &p.buff);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
664 status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
665 luaZ_freebuffer(L, &p.buff);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
666 luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
667 luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
668 luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
669 L->nny--;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
670 return status;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
671 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
672
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
673