summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/lapi.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua/lapi.c')
-rw-r--r--apps/plugins/lua/lapi.c867
1 files changed, 532 insertions, 335 deletions
diff --git a/apps/plugins/lua/lapi.c b/apps/plugins/lua/lapi.c
index 487d6b173a..d011431ead 100644
--- a/apps/plugins/lua/lapi.c
+++ b/apps/plugins/lua/lapi.c
@@ -1,12 +1,10 @@
1/* 1/*
2** $Id: lapi.c,v 2.55.1.5 2008/07/04 18:41:18 roberto Exp $ 2** $Id: lapi.c,v 2.171.1.1 2013/04/12 18:48:47 roberto Exp $
3** Lua API 3** Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
6 6
7 7
8/* #include <assert.h> */
9#include <math.h>
10#include <stdarg.h> 8#include <stdarg.h>
11#include <string.h> 9#include <string.h>
12 10
@@ -32,76 +30,80 @@
32 30
33 31
34const char lua_ident[] = 32const char lua_ident[] =
35 "$Lua: " LUA_RELEASE " " LUA_COPYRIGHT " $\n" 33 "$LuaVersion: " LUA_COPYRIGHT " $"
36 "$Authors: " LUA_AUTHORS " $\n" 34 "$LuaAuthors: " LUA_AUTHORS " $";
37 "$URL: www.lua.org $\n";
38 35
39 36
37/* value at a non-valid index */
38#define NONVALIDVALUE cast(TValue *, luaO_nilobject)
40 39
41#define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base)) 40/* corresponding test */
41#define isvalid(o) ((o) != luaO_nilobject)
42 42
43#define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject) 43/* test for pseudo index */
44#define ispseudo(i) ((i) <= LUA_REGISTRYINDEX)
44 45
45#define api_incr_top(L) {api_check(L, L->top < L->ci->top); L->top++;} 46/* test for valid but not pseudo index */
47#define isstackindex(i, o) (isvalid(o) && !ispseudo(i))
46 48
49#define api_checkvalidindex(L, o) api_check(L, isvalid(o), "invalid index")
47 50
51#define api_checkstackindex(L, i, o) \
52 api_check(L, isstackindex(i, o), "index not in the stack")
48 53
49static TValue *index2adr (lua_State *L, int idx) { 54
55static TValue *index2addr (lua_State *L, int idx) {
56 CallInfo *ci = L->ci;
50 if (idx > 0) { 57 if (idx > 0) {
51 TValue *o = L->base + (idx - 1); 58 TValue *o = ci->func + idx;
52 api_check(L, idx <= L->ci->top - L->base); 59 api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index");
53 if (o >= L->top) return cast(TValue *, luaO_nilobject); 60 if (o >= L->top) return NONVALIDVALUE;
54 else return o; 61 else return o;
55 } 62 }
56 else if (idx > LUA_REGISTRYINDEX) { 63 else if (!ispseudo(idx)) { /* negative index */
57 api_check(L, idx != 0 && -idx <= L->top - L->base); 64 api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
58 return L->top + idx; 65 return L->top + idx;
59 } 66 }
60 else switch (idx) { /* pseudo-indices */ 67 else if (idx == LUA_REGISTRYINDEX)
61 case LUA_REGISTRYINDEX: return registry(L); 68 return &G(L)->l_registry;
62 case LUA_ENVIRONINDEX: { 69 else { /* upvalues */
63 Closure *func = curr_func(L); 70 idx = LUA_REGISTRYINDEX - idx;
64 sethvalue(L, &L->env, func->c.env); 71 api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
65 return &L->env; 72 if (ttislcf(ci->func)) /* light C function? */
66 } 73 return NONVALIDVALUE; /* it has no upvalues */
67 case LUA_GLOBALSINDEX: return gt(L); 74 else {
68 default: { 75 CClosure *func = clCvalue(ci->func);
69 Closure *func = curr_func(L); 76 return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE;
70 idx = LUA_GLOBALSINDEX - idx;
71 return (idx <= func->c.nupvalues)
72 ? &func->c.upvalue[idx-1]
73 : cast(TValue *, luaO_nilobject);
74 } 77 }
75 } 78 }
76} 79}
77 80
78 81
79static Table *getcurrenv (lua_State *L) { 82/*
80 if (L->ci == L->base_ci) /* no enclosing function? */ 83** to be called by 'lua_checkstack' in protected mode, to grow stack
81 return hvalue(gt(L)); /* use global table as environment */ 84** capturing memory errors
82 else { 85*/
83 Closure *func = curr_func(L); 86static void growstack (lua_State *L, void *ud) {
84 return func->c.env; 87 int size = *(int *)ud;
85 } 88 luaD_growstack(L, size);
86}
87
88
89void luaA_pushobject (lua_State *L, const TValue *o) {
90 setobj2s(L, L->top, o);
91 api_incr_top(L);
92} 89}
93 90
94 91
95LUA_API int lua_checkstack (lua_State *L, int size) { 92LUA_API int lua_checkstack (lua_State *L, int size) {
96 int res = 1; 93 int res;
94 CallInfo *ci = L->ci;
97 lua_lock(L); 95 lua_lock(L);
98 if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK) 96 if (L->stack_last - L->top > size) /* stack large enough? */
99 res = 0; /* stack overflow */ 97 res = 1; /* yes; check is OK */
100 else if (size > 0) { 98 else { /* no; need to grow stack */
101 luaD_checkstack(L, size); 99 int inuse = cast_int(L->top - L->stack) + EXTRA_STACK;
102 if (L->ci->top < L->top + size) 100 if (inuse > LUAI_MAXSTACK - size) /* can grow without overflow? */
103 L->ci->top = L->top + size; 101 res = 0; /* no */
102 else /* try to grow stack */
103 res = (luaD_rawrunprotected(L, &growstack, &size) == LUA_OK);
104 } 104 }
105 if (res && ci->top < L->top + size)
106 ci->top = L->top + size; /* adjust frame top */
105 lua_unlock(L); 107 lua_unlock(L);
106 return res; 108 return res;
107} 109}
@@ -112,8 +114,8 @@ LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
112 if (from == to) return; 114 if (from == to) return;
113 lua_lock(to); 115 lua_lock(to);
114 api_checknelems(from, n); 116 api_checknelems(from, n);
115 api_check(from, G(from) == G(to)); 117 api_check(from, G(from) == G(to), "moving among independent states");
116 api_check(from, to->ci->top - to->top >= n); 118 api_check(from, to->ci->top - to->top >= n, "not enough elements to move");
117 from->top -= n; 119 from->top -= n;
118 for (i = 0; i < n; i++) { 120 for (i = 0; i < n; i++) {
119 setobj2s(to, to->top++, from->top + i); 121 setobj2s(to, to->top++, from->top + i);
@@ -122,11 +124,6 @@ LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
122} 124}
123 125
124 126
125LUA_API void lua_setlevel (lua_State *from, lua_State *to) {
126 to->nCcalls = from->nCcalls;
127}
128
129
130LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) { 127LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
131 lua_CFunction old; 128 lua_CFunction old;
132 lua_lock(L); 129 lua_lock(L);
@@ -137,16 +134,10 @@ LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
137} 134}
138 135
139 136
140LUA_API lua_State *lua_newthread (lua_State *L) { 137LUA_API const lua_Number *lua_version (lua_State *L) {
141 lua_State *L1; 138 static const lua_Number version = LUA_VERSION_NUM;
142 lua_lock(L); 139 if (L == NULL) return &version;
143 luaC_checkGC(L); 140 else return G(L)->version;
144 L1 = luaE_newthread(L);
145 setthvalue(L, L->top, L1);
146 api_incr_top(L);
147 lua_unlock(L);
148 luai_userstatethread(L, L1);
149 return L1;
150} 141}
151 142
152 143
@@ -156,21 +147,32 @@ LUA_API lua_State *lua_newthread (lua_State *L) {
156*/ 147*/
157 148
158 149
150/*
151** convert an acceptable stack index into an absolute index
152*/
153LUA_API int lua_absindex (lua_State *L, int idx) {
154 return (idx > 0 || ispseudo(idx))
155 ? idx
156 : cast_int(L->top - L->ci->func + idx);
157}
158
159
159LUA_API int lua_gettop (lua_State *L) { 160LUA_API int lua_gettop (lua_State *L) {
160 return cast_int(L->top - L->base); 161 return cast_int(L->top - (L->ci->func + 1));
161} 162}
162 163
163 164
164LUA_API void lua_settop (lua_State *L, int idx) { 165LUA_API void lua_settop (lua_State *L, int idx) {
166 StkId func = L->ci->func;
165 lua_lock(L); 167 lua_lock(L);
166 if (idx >= 0) { 168 if (idx >= 0) {
167 api_check(L, idx <= L->stack_last - L->base); 169 api_check(L, idx <= L->stack_last - (func + 1), "new top too large");
168 while (L->top < L->base + idx) 170 while (L->top < (func + 1) + idx)
169 setnilvalue(L->top++); 171 setnilvalue(L->top++);
170 L->top = L->base + idx; 172 L->top = (func + 1) + idx;
171 } 173 }
172 else { 174 else {
173 api_check(L, -(idx+1) <= (L->top - L->base)); 175 api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
174 L->top += idx+1; /* `subtract' index (index is negative) */ 176 L->top += idx+1; /* `subtract' index (index is negative) */
175 } 177 }
176 lua_unlock(L); 178 lua_unlock(L);
@@ -180,8 +182,8 @@ LUA_API void lua_settop (lua_State *L, int idx) {
180LUA_API void lua_remove (lua_State *L, int idx) { 182LUA_API void lua_remove (lua_State *L, int idx) {
181 StkId p; 183 StkId p;
182 lua_lock(L); 184 lua_lock(L);
183 p = index2adr(L, idx); 185 p = index2addr(L, idx);
184 api_checkvalidindex(L, p); 186 api_checkstackindex(L, idx, p);
185 while (++p < L->top) setobjs2s(L, p-1, p); 187 while (++p < L->top) setobjs2s(L, p-1, p);
186 L->top--; 188 L->top--;
187 lua_unlock(L); 189 lua_unlock(L);
@@ -192,42 +194,47 @@ LUA_API void lua_insert (lua_State *L, int idx) {
192 StkId p; 194 StkId p;
193 StkId q; 195 StkId q;
194 lua_lock(L); 196 lua_lock(L);
195 p = index2adr(L, idx); 197 p = index2addr(L, idx);
196 api_checkvalidindex(L, p); 198 api_checkstackindex(L, idx, p);
197 for (q = L->top; q>p; q--) setobjs2s(L, q, q-1); 199 for (q = L->top; q > p; q--) /* use L->top as a temporary */
200 setobjs2s(L, q, q - 1);
198 setobjs2s(L, p, L->top); 201 setobjs2s(L, p, L->top);
199 lua_unlock(L); 202 lua_unlock(L);
200} 203}
201 204
202 205
206static void moveto (lua_State *L, TValue *fr, int idx) {
207 TValue *to = index2addr(L, idx);
208 api_checkvalidindex(L, to);
209 setobj(L, to, fr);
210 if (idx < LUA_REGISTRYINDEX) /* function upvalue? */
211 luaC_barrier(L, clCvalue(L->ci->func), fr);
212 /* LUA_REGISTRYINDEX does not need gc barrier
213 (collector revisits it before finishing collection) */
214}
215
216
203LUA_API void lua_replace (lua_State *L, int idx) { 217LUA_API void lua_replace (lua_State *L, int idx) {
204 StkId o;
205 lua_lock(L); 218 lua_lock(L);
206 /* explicit test for incompatible code */
207 if (idx == LUA_ENVIRONINDEX && L->ci == L->base_ci)
208 luaG_runerror(L, "no calling environment");
209 api_checknelems(L, 1); 219 api_checknelems(L, 1);
210 o = index2adr(L, idx); 220 moveto(L, L->top - 1, idx);
211 api_checkvalidindex(L, o);
212 if (idx == LUA_ENVIRONINDEX) {
213 Closure *func = curr_func(L);
214 api_check(L, ttistable(L->top - 1));
215 func->c.env = hvalue(L->top - 1);
216 luaC_barrier(L, func, L->top - 1);
217 }
218 else {
219 setobj(L, o, L->top - 1);
220 if (idx < LUA_GLOBALSINDEX) /* function upvalue? */
221 luaC_barrier(L, curr_func(L), L->top - 1);
222 }
223 L->top--; 221 L->top--;
224 lua_unlock(L); 222 lua_unlock(L);
225} 223}
226 224
227 225
226LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
227 TValue *fr;
228 lua_lock(L);
229 fr = index2addr(L, fromidx);
230 moveto(L, fr, toidx);
231 lua_unlock(L);
232}
233
234
228LUA_API void lua_pushvalue (lua_State *L, int idx) { 235LUA_API void lua_pushvalue (lua_State *L, int idx) {
229 lua_lock(L); 236 lua_lock(L);
230 setobj2s(L, L->top, index2adr(L, idx)); 237 setobj2s(L, L->top, index2addr(L, idx));
231 api_incr_top(L); 238 api_incr_top(L);
232 lua_unlock(L); 239 lua_unlock(L);
233} 240}
@@ -240,26 +247,26 @@ LUA_API void lua_pushvalue (lua_State *L, int idx) {
240 247
241 248
242LUA_API int lua_type (lua_State *L, int idx) { 249LUA_API int lua_type (lua_State *L, int idx) {
243 StkId o = index2adr(L, idx); 250 StkId o = index2addr(L, idx);
244 return (o == luaO_nilobject) ? LUA_TNONE : ttype(o); 251 return (isvalid(o) ? ttypenv(o) : LUA_TNONE);
245} 252}
246 253
247 254
248LUA_API const char *lua_typename (lua_State *L, int t) { 255LUA_API const char *lua_typename (lua_State *L, int t) {
249 UNUSED(L); 256 UNUSED(L);
250 return (t == LUA_TNONE) ? "no value" : luaT_typenames[t]; 257 return ttypename(t);
251} 258}
252 259
253 260
254LUA_API int lua_iscfunction (lua_State *L, int idx) { 261LUA_API int lua_iscfunction (lua_State *L, int idx) {
255 StkId o = index2adr(L, idx); 262 StkId o = index2addr(L, idx);
256 return iscfunction(o); 263 return (ttislcf(o) || (ttisCclosure(o)));
257} 264}
258 265
259 266
260LUA_API int lua_isnumber (lua_State *L, int idx) { 267LUA_API int lua_isnumber (lua_State *L, int idx) {
261 TValue n; 268 TValue n;
262 const TValue *o = index2adr(L, idx); 269 const TValue *o = index2addr(L, idx);
263 return tonumber(o, &n); 270 return tonumber(o, &n);
264} 271}
265 272
@@ -271,77 +278,116 @@ LUA_API int lua_isstring (lua_State *L, int idx) {
271 278
272 279
273LUA_API int lua_isuserdata (lua_State *L, int idx) { 280LUA_API int lua_isuserdata (lua_State *L, int idx) {
274 const TValue *o = index2adr(L, idx); 281 const TValue *o = index2addr(L, idx);
275 return (ttisuserdata(o) || ttislightuserdata(o)); 282 return (ttisuserdata(o) || ttislightuserdata(o));
276} 283}
277 284
278 285
279LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { 286LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
280 StkId o1 = index2adr(L, index1); 287 StkId o1 = index2addr(L, index1);
281 StkId o2 = index2adr(L, index2); 288 StkId o2 = index2addr(L, index2);
282 return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 289 return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0;
283 : luaO_rawequalObj(o1, o2);
284} 290}
285 291
286 292
287LUA_API int lua_equal (lua_State *L, int index1, int index2) { 293LUA_API void lua_arith (lua_State *L, int op) {
288 StkId o1, o2; 294 StkId o1; /* 1st operand */
289 int i; 295 StkId o2; /* 2nd operand */
290 lua_lock(L); /* may call tag method */ 296 lua_lock(L);
291 o1 = index2adr(L, index1); 297 if (op != LUA_OPUNM) /* all other operations expect two operands */
292 o2 = index2adr(L, index2); 298 api_checknelems(L, 2);
293 i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 : equalobj(L, o1, o2); 299 else { /* for unary minus, add fake 2nd operand */
300 api_checknelems(L, 1);
301 setobjs2s(L, L->top, L->top - 1);
302 L->top++;
303 }
304 o1 = L->top - 2;
305 o2 = L->top - 1;
306 if (ttisnumber(o1) && ttisnumber(o2)) {
307 setnvalue(o1, luaO_arith(op, nvalue(o1), nvalue(o2)));
308 }
309 else
310 luaV_arith(L, o1, o1, o2, cast(TMS, op - LUA_OPADD + TM_ADD));
311 L->top--;
294 lua_unlock(L); 312 lua_unlock(L);
295 return i;
296} 313}
297 314
298 315
299LUA_API int lua_lessthan (lua_State *L, int index1, int index2) { 316LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
300 StkId o1, o2; 317 StkId o1, o2;
301 int i; 318 int i = 0;
302 lua_lock(L); /* may call tag method */ 319 lua_lock(L); /* may call tag method */
303 o1 = index2adr(L, index1); 320 o1 = index2addr(L, index1);
304 o2 = index2adr(L, index2); 321 o2 = index2addr(L, index2);
305 i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 322 if (isvalid(o1) && isvalid(o2)) {
306 : luaV_lessthan(L, o1, o2); 323 switch (op) {
324 case LUA_OPEQ: i = equalobj(L, o1, o2); break;
325 case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
326 case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
327 default: api_check(L, 0, "invalid option");
328 }
329 }
307 lua_unlock(L); 330 lua_unlock(L);
308 return i; 331 return i;
309} 332}
310 333
311 334
312 335LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *isnum) {
313LUA_API lua_Number lua_tonumber (lua_State *L, int idx) {
314 TValue n; 336 TValue n;
315 const TValue *o = index2adr(L, idx); 337 const TValue *o = index2addr(L, idx);
316 if (tonumber(o, &n)) 338 if (tonumber(o, &n)) {
339 if (isnum) *isnum = 1;
317 return nvalue(o); 340 return nvalue(o);
318 else 341 }
342 else {
343 if (isnum) *isnum = 0;
319 return 0; 344 return 0;
345 }
320} 346}
321 347
322 348
323LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) { 349LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum) {
324 TValue n; 350 TValue n;
325 const TValue *o = index2adr(L, idx); 351 const TValue *o = index2addr(L, idx);
326 if (tonumber(o, &n)) { 352 if (tonumber(o, &n)) {
327 lua_Integer res; 353 lua_Integer res;
328 lua_Number num = nvalue(o); 354 lua_Number num = nvalue(o);
329 lua_number2integer(res, num); 355 lua_number2integer(res, num);
356 if (isnum) *isnum = 1;
330 return res; 357 return res;
331 } 358 }
332 else 359 else {
360 if (isnum) *isnum = 0;
361 return 0;
362 }
363}
364
365
366LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *isnum) {
367 TValue n;
368 const TValue *o = index2addr(L, idx);
369 if (tonumber(o, &n)) {
370 lua_Unsigned res;
371 lua_Number num = nvalue(o);
372 lua_number2unsigned(res, num);
373 if (isnum) *isnum = 1;
374 return res;
375 }
376 else {
377 if (isnum) *isnum = 0;
333 return 0; 378 return 0;
379 }
334} 380}
335 381
336 382
337LUA_API int lua_toboolean (lua_State *L, int idx) { 383LUA_API int lua_toboolean (lua_State *L, int idx) {
338 const TValue *o = index2adr(L, idx); 384 const TValue *o = index2addr(L, idx);
339 return !l_isfalse(o); 385 return !l_isfalse(o);
340} 386}
341 387
342 388
343LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { 389LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
344 StkId o = index2adr(L, idx); 390 StkId o = index2addr(L, idx);
345 if (!ttisstring(o)) { 391 if (!ttisstring(o)) {
346 lua_lock(L); /* `luaV_tostring' may create a new string */ 392 lua_lock(L); /* `luaV_tostring' may create a new string */
347 if (!luaV_tostring(L, o)) { /* conversion failed? */ 393 if (!luaV_tostring(L, o)) { /* conversion failed? */
@@ -350,7 +396,7 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
350 return NULL; 396 return NULL;
351 } 397 }
352 luaC_checkGC(L); 398 luaC_checkGC(L);
353 o = index2adr(L, idx); /* previous call may reallocate the stack */ 399 o = index2addr(L, idx); /* previous call may reallocate the stack */
354 lua_unlock(L); 400 lua_unlock(L);
355 } 401 }
356 if (len != NULL) *len = tsvalue(o)->len; 402 if (len != NULL) *len = tsvalue(o)->len;
@@ -358,33 +404,29 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
358} 404}
359 405
360 406
361LUA_API size_t lua_objlen (lua_State *L, int idx) { 407LUA_API size_t lua_rawlen (lua_State *L, int idx) {
362 StkId o = index2adr(L, idx); 408 StkId o = index2addr(L, idx);
363 switch (ttype(o)) { 409 switch (ttypenv(o)) {
364 case LUA_TSTRING: return tsvalue(o)->len; 410 case LUA_TSTRING: return tsvalue(o)->len;
365 case LUA_TUSERDATA: return uvalue(o)->len; 411 case LUA_TUSERDATA: return uvalue(o)->len;
366 case LUA_TTABLE: return luaH_getn(hvalue(o)); 412 case LUA_TTABLE: return luaH_getn(hvalue(o));
367 case LUA_TNUMBER: {
368 size_t l;
369 lua_lock(L); /* `luaV_tostring' may create a new string */
370 l = (luaV_tostring(L, o) ? tsvalue(o)->len : 0);
371 lua_unlock(L);
372 return l;
373 }
374 default: return 0; 413 default: return 0;
375 } 414 }
376} 415}
377 416
378 417
379LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { 418LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
380 StkId o = index2adr(L, idx); 419 StkId o = index2addr(L, idx);
381 return (!iscfunction(o)) ? NULL : clvalue(o)->c.f; 420 if (ttislcf(o)) return fvalue(o);
421 else if (ttisCclosure(o))
422 return clCvalue(o)->f;
423 else return NULL; /* not a C function */
382} 424}
383 425
384 426
385LUA_API void *lua_touserdata (lua_State *L, int idx) { 427LUA_API void *lua_touserdata (lua_State *L, int idx) {
386 StkId o = index2adr(L, idx); 428 StkId o = index2addr(L, idx);
387 switch (ttype(o)) { 429 switch (ttypenv(o)) {
388 case LUA_TUSERDATA: return (rawuvalue(o) + 1); 430 case LUA_TUSERDATA: return (rawuvalue(o) + 1);
389 case LUA_TLIGHTUSERDATA: return pvalue(o); 431 case LUA_TLIGHTUSERDATA: return pvalue(o);
390 default: return NULL; 432 default: return NULL;
@@ -393,16 +435,18 @@ LUA_API void *lua_touserdata (lua_State *L, int idx) {
393 435
394 436
395LUA_API lua_State *lua_tothread (lua_State *L, int idx) { 437LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
396 StkId o = index2adr(L, idx); 438 StkId o = index2addr(L, idx);
397 return (!ttisthread(o)) ? NULL : thvalue(o); 439 return (!ttisthread(o)) ? NULL : thvalue(o);
398} 440}
399 441
400 442
401LUA_API const void *lua_topointer (lua_State *L, int idx) { 443LUA_API const void *lua_topointer (lua_State *L, int idx) {
402 StkId o = index2adr(L, idx); 444 StkId o = index2addr(L, idx);
403 switch (ttype(o)) { 445 switch (ttype(o)) {
404 case LUA_TTABLE: return hvalue(o); 446 case LUA_TTABLE: return hvalue(o);
405 case LUA_TFUNCTION: return clvalue(o); 447 case LUA_TLCL: return clLvalue(o);
448 case LUA_TCCL: return clCvalue(o);
449 case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o)));
406 case LUA_TTHREAD: return thvalue(o); 450 case LUA_TTHREAD: return thvalue(o);
407 case LUA_TUSERDATA: 451 case LUA_TUSERDATA:
408 case LUA_TLIGHTUSERDATA: 452 case LUA_TLIGHTUSERDATA:
@@ -429,6 +473,8 @@ LUA_API void lua_pushnil (lua_State *L) {
429LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { 473LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
430 lua_lock(L); 474 lua_lock(L);
431 setnvalue(L->top, n); 475 setnvalue(L->top, n);
476 luai_checknum(L, L->top,
477 luaG_runerror(L, "C API - attempt to push a signaling NaN"));
432 api_incr_top(L); 478 api_incr_top(L);
433 lua_unlock(L); 479 lua_unlock(L);
434} 480}
@@ -442,20 +488,43 @@ LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
442} 488}
443 489
444 490
445LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) { 491LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) {
492 lua_Number n;
493 lua_lock(L);
494 n = lua_unsigned2number(u);
495 setnvalue(L->top, n);
496 api_incr_top(L);
497 lua_unlock(L);
498}
499
500
501LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
502 TString *ts;
446 lua_lock(L); 503 lua_lock(L);
447 luaC_checkGC(L); 504 luaC_checkGC(L);
448 setsvalue2s(L, L->top, luaS_newlstr(L, s, len)); 505 ts = luaS_newlstr(L, s, len);
506 setsvalue2s(L, L->top, ts);
449 api_incr_top(L); 507 api_incr_top(L);
450 lua_unlock(L); 508 lua_unlock(L);
509 return getstr(ts);
451} 510}
452 511
453 512
454LUA_API void lua_pushstring (lua_State *L, const char *s) { 513LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
455 if (s == NULL) 514 if (s == NULL) {
456 lua_pushnil(L); 515 lua_pushnil(L);
457 else 516 return NULL;
458 lua_pushlstring(L, s, strlen(s)); 517 }
518 else {
519 TString *ts;
520 lua_lock(L);
521 luaC_checkGC(L);
522 ts = luaS_new(L, s);
523 setsvalue2s(L, L->top, ts);
524 api_incr_top(L);
525 lua_unlock(L);
526 return getstr(ts);
527 }
459} 528}
460 529
461 530
@@ -484,17 +553,22 @@ LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
484 553
485 554
486LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { 555LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
487 Closure *cl;
488 lua_lock(L); 556 lua_lock(L);
489 luaC_checkGC(L); 557 if (n == 0) {
490 api_checknelems(L, n); 558 setfvalue(L->top, fn);
491 cl = luaF_newCclosure(L, n, getcurrenv(L)); 559 }
492 cl->c.f = fn; 560 else {
493 L->top -= n; 561 Closure *cl;
494 while (n--) 562 api_checknelems(L, n);
495 setobj2n(L, &cl->c.upvalue[n], L->top+n); 563 api_check(L, n <= MAXUPVAL, "upvalue index too large");
496 setclvalue(L, L->top, cl); 564 luaC_checkGC(L);
497 lua_assert(iswhite(obj2gco(cl))); 565 cl = luaF_newCclosure(L, n);
566 cl->c.f = fn;
567 L->top -= n;
568 while (n--)
569 setobj2n(L, &cl->c.upvalue[n], L->top + n);
570 setclCvalue(L, L->top, cl);
571 }
498 api_incr_top(L); 572 api_incr_top(L);
499 lua_unlock(L); 573 lua_unlock(L);
500} 574}
@@ -531,11 +605,21 @@ LUA_API int lua_pushthread (lua_State *L) {
531*/ 605*/
532 606
533 607
608LUA_API void lua_getglobal (lua_State *L, const char *var) {
609 Table *reg = hvalue(&G(L)->l_registry);
610 const TValue *gt; /* global table */
611 lua_lock(L);
612 gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
613 setsvalue2s(L, L->top++, luaS_new(L, var));
614 luaV_gettable(L, gt, L->top - 1, L->top - 1);
615 lua_unlock(L);
616}
617
618
534LUA_API void lua_gettable (lua_State *L, int idx) { 619LUA_API void lua_gettable (lua_State *L, int idx) {
535 StkId t; 620 StkId t;
536 lua_lock(L); 621 lua_lock(L);
537 t = index2adr(L, idx); 622 t = index2addr(L, idx);
538 api_checkvalidindex(L, t);
539 luaV_gettable(L, t, L->top - 1, L->top - 1); 623 luaV_gettable(L, t, L->top - 1, L->top - 1);
540 lua_unlock(L); 624 lua_unlock(L);
541} 625}
@@ -543,13 +627,11 @@ LUA_API void lua_gettable (lua_State *L, int idx) {
543 627
544LUA_API void lua_getfield (lua_State *L, int idx, const char *k) { 628LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
545 StkId t; 629 StkId t;
546 TValue key;
547 lua_lock(L); 630 lua_lock(L);
548 t = index2adr(L, idx); 631 t = index2addr(L, idx);
549 api_checkvalidindex(L, t); 632 setsvalue2s(L, L->top, luaS_new(L, k));
550 setsvalue(L, &key, luaS_new(L, k));
551 luaV_gettable(L, t, &key, L->top);
552 api_incr_top(L); 633 api_incr_top(L);
634 luaV_gettable(L, t, L->top - 1, L->top - 1);
553 lua_unlock(L); 635 lua_unlock(L);
554} 636}
555 637
@@ -557,29 +639,46 @@ LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
557LUA_API void lua_rawget (lua_State *L, int idx) { 639LUA_API void lua_rawget (lua_State *L, int idx) {
558 StkId t; 640 StkId t;
559 lua_lock(L); 641 lua_lock(L);
560 t = index2adr(L, idx); 642 t = index2addr(L, idx);
561 api_check(L, ttistable(t)); 643 api_check(L, ttistable(t), "table expected");
562 setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1)); 644 setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
563 lua_unlock(L); 645 lua_unlock(L);
564} 646}
565 647
566 648
567LUA_API void lua_rawgeti (lua_State *L, int idx, int n) { 649LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
568 StkId o; 650 StkId t;
569 lua_lock(L); 651 lua_lock(L);
570 o = index2adr(L, idx); 652 t = index2addr(L, idx);
571 api_check(L, ttistable(o)); 653 api_check(L, ttistable(t), "table expected");
572 setobj2s(L, L->top, luaH_getnum(hvalue(o), n)); 654 setobj2s(L, L->top, luaH_getint(hvalue(t), n));
655 api_incr_top(L);
656 lua_unlock(L);
657}
658
659
660LUA_API void lua_rawgetp (lua_State *L, int idx, const void *p) {
661 StkId t;
662 TValue k;
663 lua_lock(L);
664 t = index2addr(L, idx);
665 api_check(L, ttistable(t), "table expected");
666 setpvalue(&k, cast(void *, p));
667 setobj2s(L, L->top, luaH_get(hvalue(t), &k));
573 api_incr_top(L); 668 api_incr_top(L);
574 lua_unlock(L); 669 lua_unlock(L);
575} 670}
576 671
577 672
578LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { 673LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
674 Table *t;
579 lua_lock(L); 675 lua_lock(L);
580 luaC_checkGC(L); 676 luaC_checkGC(L);
581 sethvalue(L, L->top, luaH_new(L, narray, nrec)); 677 t = luaH_new(L);
678 sethvalue(L, L->top, t);
582 api_incr_top(L); 679 api_incr_top(L);
680 if (narray > 0 || nrec > 0)
681 luaH_resize(L, t, narray, nrec);
583 lua_unlock(L); 682 lua_unlock(L);
584} 683}
585 684
@@ -589,8 +688,8 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) {
589 Table *mt = NULL; 688 Table *mt = NULL;
590 int res; 689 int res;
591 lua_lock(L); 690 lua_lock(L);
592 obj = index2adr(L, objindex); 691 obj = index2addr(L, objindex);
593 switch (ttype(obj)) { 692 switch (ttypenv(obj)) {
594 case LUA_TTABLE: 693 case LUA_TTABLE:
595 mt = hvalue(obj)->metatable; 694 mt = hvalue(obj)->metatable;
596 break; 695 break;
@@ -598,7 +697,7 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) {
598 mt = uvalue(obj)->metatable; 697 mt = uvalue(obj)->metatable;
599 break; 698 break;
600 default: 699 default:
601 mt = G(L)->mt[ttype(obj)]; 700 mt = G(L)->mt[ttypenv(obj)];
602 break; 701 break;
603 } 702 }
604 if (mt == NULL) 703 if (mt == NULL)
@@ -613,25 +712,15 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) {
613} 712}
614 713
615 714
616LUA_API void lua_getfenv (lua_State *L, int idx) { 715LUA_API void lua_getuservalue (lua_State *L, int idx) {
617 StkId o; 716 StkId o;
618 lua_lock(L); 717 lua_lock(L);
619 o = index2adr(L, idx); 718 o = index2addr(L, idx);
620 api_checkvalidindex(L, o); 719 api_check(L, ttisuserdata(o), "userdata expected");
621 switch (ttype(o)) { 720 if (uvalue(o)->env) {
622 case LUA_TFUNCTION: 721 sethvalue(L, L->top, uvalue(o)->env);
623 sethvalue(L, L->top, clvalue(o)->c.env); 722 } else
624 break; 723 setnilvalue(L->top);
625 case LUA_TUSERDATA:
626 sethvalue(L, L->top, uvalue(o)->env);
627 break;
628 case LUA_TTHREAD:
629 setobj2s(L, L->top, gt(thvalue(o)));
630 break;
631 default:
632 setnilvalue(L->top);
633 break;
634 }
635 api_incr_top(L); 724 api_incr_top(L);
636 lua_unlock(L); 725 lua_unlock(L);
637} 726}
@@ -642,12 +731,24 @@ LUA_API void lua_getfenv (lua_State *L, int idx) {
642*/ 731*/
643 732
644 733
734LUA_API void lua_setglobal (lua_State *L, const char *var) {
735 Table *reg = hvalue(&G(L)->l_registry);
736 const TValue *gt; /* global table */
737 lua_lock(L);
738 api_checknelems(L, 1);
739 gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
740 setsvalue2s(L, L->top++, luaS_new(L, var));
741 luaV_settable(L, gt, L->top - 1, L->top - 2);
742 L->top -= 2; /* pop value and key */
743 lua_unlock(L);
744}
745
746
645LUA_API void lua_settable (lua_State *L, int idx) { 747LUA_API void lua_settable (lua_State *L, int idx) {
646 StkId t; 748 StkId t;
647 lua_lock(L); 749 lua_lock(L);
648 api_checknelems(L, 2); 750 api_checknelems(L, 2);
649 t = index2adr(L, idx); 751 t = index2addr(L, idx);
650 api_checkvalidindex(L, t);
651 luaV_settable(L, t, L->top - 2, L->top - 1); 752 luaV_settable(L, t, L->top - 2, L->top - 1);
652 L->top -= 2; /* pop index and value */ 753 L->top -= 2; /* pop index and value */
653 lua_unlock(L); 754 lua_unlock(L);
@@ -656,14 +757,12 @@ LUA_API void lua_settable (lua_State *L, int idx) {
656 757
657LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { 758LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
658 StkId t; 759 StkId t;
659 TValue key;
660 lua_lock(L); 760 lua_lock(L);
661 api_checknelems(L, 1); 761 api_checknelems(L, 1);
662 t = index2adr(L, idx); 762 t = index2addr(L, idx);
663 api_checkvalidindex(L, t); 763 setsvalue2s(L, L->top++, luaS_new(L, k));
664 setsvalue(L, &key, luaS_new(L, k)); 764 luaV_settable(L, t, L->top - 1, L->top - 2);
665 luaV_settable(L, t, &key, L->top - 1); 765 L->top -= 2; /* pop value and key */
666 L->top--; /* pop value */
667 lua_unlock(L); 766 lua_unlock(L);
668} 767}
669 768
@@ -672,23 +771,39 @@ LUA_API void lua_rawset (lua_State *L, int idx) {
672 StkId t; 771 StkId t;
673 lua_lock(L); 772 lua_lock(L);
674 api_checknelems(L, 2); 773 api_checknelems(L, 2);
675 t = index2adr(L, idx); 774 t = index2addr(L, idx);
676 api_check(L, ttistable(t)); 775 api_check(L, ttistable(t), "table expected");
677 setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1); 776 setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
678 luaC_barriert(L, hvalue(t), L->top-1); 777 invalidateTMcache(hvalue(t));
778 luaC_barrierback(L, gcvalue(t), L->top-1);
679 L->top -= 2; 779 L->top -= 2;
680 lua_unlock(L); 780 lua_unlock(L);
681} 781}
682 782
683 783
684LUA_API void lua_rawseti (lua_State *L, int idx, int n) { 784LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
685 StkId o; 785 StkId t;
786 lua_lock(L);
787 api_checknelems(L, 1);
788 t = index2addr(L, idx);
789 api_check(L, ttistable(t), "table expected");
790 luaH_setint(L, hvalue(t), n, L->top - 1);
791 luaC_barrierback(L, gcvalue(t), L->top-1);
792 L->top--;
793 lua_unlock(L);
794}
795
796
797LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
798 StkId t;
799 TValue k;
686 lua_lock(L); 800 lua_lock(L);
687 api_checknelems(L, 1); 801 api_checknelems(L, 1);
688 o = index2adr(L, idx); 802 t = index2addr(L, idx);
689 api_check(L, ttistable(o)); 803 api_check(L, ttistable(t), "table expected");
690 setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1); 804 setpvalue(&k, cast(void *, p));
691 luaC_barriert(L, hvalue(o), L->top-1); 805 setobj2t(L, luaH_set(L, hvalue(t), &k), L->top - 1);
806 luaC_barrierback(L, gcvalue(t), L->top - 1);
692 L->top--; 807 L->top--;
693 lua_unlock(L); 808 lua_unlock(L);
694} 809}
@@ -699,29 +814,32 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) {
699 Table *mt; 814 Table *mt;
700 lua_lock(L); 815 lua_lock(L);
701 api_checknelems(L, 1); 816 api_checknelems(L, 1);
702 obj = index2adr(L, objindex); 817 obj = index2addr(L, objindex);
703 api_checkvalidindex(L, obj);
704 if (ttisnil(L->top - 1)) 818 if (ttisnil(L->top - 1))
705 mt = NULL; 819 mt = NULL;
706 else { 820 else {
707 api_check(L, ttistable(L->top - 1)); 821 api_check(L, ttistable(L->top - 1), "table expected");
708 mt = hvalue(L->top - 1); 822 mt = hvalue(L->top - 1);
709 } 823 }
710 switch (ttype(obj)) { 824 switch (ttypenv(obj)) {
711 case LUA_TTABLE: { 825 case LUA_TTABLE: {
712 hvalue(obj)->metatable = mt; 826 hvalue(obj)->metatable = mt;
713 if (mt) 827 if (mt) {
714 luaC_objbarriert(L, hvalue(obj), mt); 828 luaC_objbarrierback(L, gcvalue(obj), mt);
829 luaC_checkfinalizer(L, gcvalue(obj), mt);
830 }
715 break; 831 break;
716 } 832 }
717 case LUA_TUSERDATA: { 833 case LUA_TUSERDATA: {
718 uvalue(obj)->metatable = mt; 834 uvalue(obj)->metatable = mt;
719 if (mt) 835 if (mt) {
720 luaC_objbarrier(L, rawuvalue(obj), mt); 836 luaC_objbarrier(L, rawuvalue(obj), mt);
837 luaC_checkfinalizer(L, gcvalue(obj), mt);
838 }
721 break; 839 break;
722 } 840 }
723 default: { 841 default: {
724 G(L)->mt[ttype(obj)] = mt; 842 G(L)->mt[ttypenv(obj)] = mt;
725 break; 843 break;
726 } 844 }
727 } 845 }
@@ -731,32 +849,21 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) {
731} 849}
732 850
733 851
734LUA_API int lua_setfenv (lua_State *L, int idx) { 852LUA_API void lua_setuservalue (lua_State *L, int idx) {
735 StkId o; 853 StkId o;
736 int res = 1;
737 lua_lock(L); 854 lua_lock(L);
738 api_checknelems(L, 1); 855 api_checknelems(L, 1);
739 o = index2adr(L, idx); 856 o = index2addr(L, idx);
740 api_checkvalidindex(L, o); 857 api_check(L, ttisuserdata(o), "userdata expected");
741 api_check(L, ttistable(L->top - 1)); 858 if (ttisnil(L->top - 1))
742 switch (ttype(o)) { 859 uvalue(o)->env = NULL;
743 case LUA_TFUNCTION: 860 else {
744 clvalue(o)->c.env = hvalue(L->top - 1); 861 api_check(L, ttistable(L->top - 1), "table expected");
745 break; 862 uvalue(o)->env = hvalue(L->top - 1);
746 case LUA_TUSERDATA: 863 luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
747 uvalue(o)->env = hvalue(L->top - 1);
748 break;
749 case LUA_TTHREAD:
750 sethvalue(L, gt(thvalue(o)), hvalue(L->top - 1));
751 break;
752 default:
753 res = 0;
754 break;
755 } 864 }
756 if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
757 L->top--; 865 L->top--;
758 lua_unlock(L); 866 lua_unlock(L);
759 return res;
760} 867}
761 868
762 869
@@ -765,21 +872,37 @@ LUA_API int lua_setfenv (lua_State *L, int idx) {
765*/ 872*/
766 873
767 874
768#define adjustresults(L,nres) \ 875#define checkresults(L,na,nr) \
769 { if (nres == LUA_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; } 876 api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
877 "results from function overflow current stack size")
770 878
771 879
772#define checkresults(L,na,nr) \ 880LUA_API int lua_getctx (lua_State *L, int *ctx) {
773 api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na))) 881 if (L->ci->callstatus & CIST_YIELDED) {
774 882 if (ctx) *ctx = L->ci->u.c.ctx;
883 return L->ci->u.c.status;
884 }
885 else return LUA_OK;
886}
775 887
776LUA_API void lua_call (lua_State *L, int nargs, int nresults) { 888
889LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
890 lua_CFunction k) {
777 StkId func; 891 StkId func;
778 lua_lock(L); 892 lua_lock(L);
893 api_check(L, k == NULL || !isLua(L->ci),
894 "cannot use continuations inside hooks");
779 api_checknelems(L, nargs+1); 895 api_checknelems(L, nargs+1);
896 api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
780 checkresults(L, nargs, nresults); 897 checkresults(L, nargs, nresults);
781 func = L->top - (nargs+1); 898 func = L->top - (nargs+1);
782 luaD_call(L, func, nresults); 899 if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
900 L->ci->u.c.k = k; /* save continuation */
901 L->ci->u.c.ctx = ctx; /* save context */
902 luaD_call(L, func, nresults, 1); /* do the call */
903 }
904 else /* no continuation or no yieldable */
905 luaD_call(L, func, nresults, 0); /* just do the call */
783 adjustresults(L, nresults); 906 adjustresults(L, nresults);
784 lua_unlock(L); 907 lua_unlock(L);
785} 908}
@@ -797,76 +920,75 @@ struct CallS { /* data to `f_call' */
797 920
798static void f_call (lua_State *L, void *ud) { 921static void f_call (lua_State *L, void *ud) {
799 struct CallS *c = cast(struct CallS *, ud); 922 struct CallS *c = cast(struct CallS *, ud);
800 luaD_call(L, c->func, c->nresults); 923 luaD_call(L, c->func, c->nresults, 0);
801} 924}
802 925
803 926
804 927
805LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) { 928LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
929 int ctx, lua_CFunction k) {
806 struct CallS c; 930 struct CallS c;
807 int status; 931 int status;
808 ptrdiff_t func; 932 ptrdiff_t func;
809 lua_lock(L); 933 lua_lock(L);
934 api_check(L, k == NULL || !isLua(L->ci),
935 "cannot use continuations inside hooks");
810 api_checknelems(L, nargs+1); 936 api_checknelems(L, nargs+1);
937 api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
811 checkresults(L, nargs, nresults); 938 checkresults(L, nargs, nresults);
812 if (errfunc == 0) 939 if (errfunc == 0)
813 func = 0; 940 func = 0;
814 else { 941 else {
815 StkId o = index2adr(L, errfunc); 942 StkId o = index2addr(L, errfunc);
816 api_checkvalidindex(L, o); 943 api_checkstackindex(L, errfunc, o);
817 func = savestack(L, o); 944 func = savestack(L, o);
818 } 945 }
819 c.func = L->top - (nargs+1); /* function to be called */ 946 c.func = L->top - (nargs+1); /* function to be called */
820 c.nresults = nresults; 947 if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */
821 status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func); 948 c.nresults = nresults; /* do a 'conventional' protected call */
949 status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
950 }
951 else { /* prepare continuation (call is already protected by 'resume') */
952 CallInfo *ci = L->ci;
953 ci->u.c.k = k; /* save continuation */
954 ci->u.c.ctx = ctx; /* save context */
955 /* save information for error recovery */
956 ci->extra = savestack(L, c.func);
957 ci->u.c.old_allowhook = L->allowhook;
958 ci->u.c.old_errfunc = L->errfunc;
959 L->errfunc = func;
960 /* mark that function may do error recovery */
961 ci->callstatus |= CIST_YPCALL;
962 luaD_call(L, c.func, nresults, 1); /* do the call */
963 ci->callstatus &= ~CIST_YPCALL;
964 L->errfunc = ci->u.c.old_errfunc;
965 status = LUA_OK; /* if it is here, there were no errors */
966 }
822 adjustresults(L, nresults); 967 adjustresults(L, nresults);
823 lua_unlock(L); 968 lua_unlock(L);
824 return status; 969 return status;
825} 970}
826 971
827 972
828/*
829** Execute a protected C call.
830*/
831struct CCallS { /* data to `f_Ccall' */
832 lua_CFunction func;
833 void *ud;
834};
835
836
837static void f_Ccall (lua_State *L, void *ud) {
838 struct CCallS *c = cast(struct CCallS *, ud);
839 Closure *cl;
840 cl = luaF_newCclosure(L, 0, getcurrenv(L));
841 cl->c.f = c->func;
842 setclvalue(L, L->top, cl); /* push function */
843 api_incr_top(L);
844 setpvalue(L->top, c->ud); /* push only argument */
845 api_incr_top(L);
846 luaD_call(L, L->top - 2, 0);
847}
848
849
850LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) {
851 struct CCallS c;
852 int status;
853 lua_lock(L);
854 c.func = func;
855 c.ud = ud;
856 status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0);
857 lua_unlock(L);
858 return status;
859}
860
861
862LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, 973LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
863 const char *chunkname) { 974 const char *chunkname, const char *mode) {
864 ZIO z; 975 ZIO z;
865 int status; 976 int status;
866 lua_lock(L); 977 lua_lock(L);
867 if (!chunkname) chunkname = "?"; 978 if (!chunkname) chunkname = "?";
868 luaZ_init(L, &z, reader, data); 979 luaZ_init(L, &z, reader, data);
869 status = luaD_protectedparser(L, &z, chunkname); 980 status = luaD_protectedparser(L, &z, chunkname, mode);
981 if (status == LUA_OK) { /* no errors? */
982 LClosure *f = clLvalue(L->top - 1); /* get newly created function */
983 if (f->nupvalues == 1) { /* does it have one upvalue? */
984 /* get global table from registry */
985 Table *reg = hvalue(&G(L)->l_registry);
986 const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
987 /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
988 setobj(L, f->upvals[0]->v, gt);
989 luaC_barrier(L, f->upvals[0], gt);
990 }
991 }
870 lua_unlock(L); 992 lua_unlock(L);
871 return status; 993 return status;
872} 994}
@@ -879,7 +1001,7 @@ LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
879 api_checknelems(L, 1); 1001 api_checknelems(L, 1);
880 o = L->top - 1; 1002 o = L->top - 1;
881 if (isLfunction(o)) 1003 if (isLfunction(o))
882 status = luaU_dump(L, clvalue(o)->l.p, writer, data, 0); 1004 status = luaU_dump(L, getproto(o), writer, data, 0);
883 else 1005 else
884 status = 1; 1006 status = 1;
885 lua_unlock(L); 1007 lua_unlock(L);
@@ -887,7 +1009,7 @@ LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
887} 1009}
888 1010
889 1011
890LUA_API int lua_status (lua_State *L) { 1012LUA_API int lua_status (lua_State *L) {
891 return L->status; 1013 return L->status;
892} 1014}
893 1015
@@ -903,38 +1025,40 @@ LUA_API int lua_gc (lua_State *L, int what, int data) {
903 g = G(L); 1025 g = G(L);
904 switch (what) { 1026 switch (what) {
905 case LUA_GCSTOP: { 1027 case LUA_GCSTOP: {
906 g->GCthreshold = MAX_LUMEM; 1028 g->gcrunning = 0;
907 break; 1029 break;
908 } 1030 }
909 case LUA_GCRESTART: { 1031 case LUA_GCRESTART: {
910 g->GCthreshold = g->totalbytes; 1032 luaE_setdebt(g, 0);
1033 g->gcrunning = 1;
911 break; 1034 break;
912 } 1035 }
913 case LUA_GCCOLLECT: { 1036 case LUA_GCCOLLECT: {
914 luaC_fullgc(L); 1037 luaC_fullgc(L, 0);
915 break; 1038 break;
916 } 1039 }
917 case LUA_GCCOUNT: { 1040 case LUA_GCCOUNT: {
918 /* GC values are expressed in Kbytes: #bytes/2^10 */ 1041 /* GC values are expressed in Kbytes: #bytes/2^10 */
919 res = cast_int(g->totalbytes >> 10); 1042 res = cast_int(gettotalbytes(g) >> 10);
920 break; 1043 break;
921 } 1044 }
922 case LUA_GCCOUNTB: { 1045 case LUA_GCCOUNTB: {
923 res = cast_int(g->totalbytes & 0x3ff); 1046 res = cast_int(gettotalbytes(g) & 0x3ff);
924 break; 1047 break;
925 } 1048 }
926 case LUA_GCSTEP: { 1049 case LUA_GCSTEP: {
927 lu_mem a = (cast(lu_mem, data) << 10); 1050 if (g->gckind == KGC_GEN) { /* generational mode? */
928 if (a <= g->totalbytes) 1051 res = (g->GCestimate == 0); /* true if it will do major collection */
929 g->GCthreshold = g->totalbytes - a; 1052 luaC_forcestep(L); /* do a single step */
930 else 1053 }
931 g->GCthreshold = 0; 1054 else {
932 while (g->GCthreshold <= g->totalbytes) { 1055 lu_mem debt = cast(lu_mem, data) * 1024 - GCSTEPSIZE;
933 luaC_step(L); 1056 if (g->gcrunning)
934 if (g->gcstate == GCSpause) { /* end of cycle? */ 1057 debt += g->GCdebt; /* include current debt */
935 res = 1; /* signal it */ 1058 luaE_setdebt(g, debt);
936 break; 1059 luaC_forcestep(L);
937 } 1060 if (g->gcstate == GCSpause) /* end of cycle? */
1061 res = 1; /* signal it */
938 } 1062 }
939 break; 1063 break;
940 } 1064 }
@@ -943,11 +1067,28 @@ LUA_API int lua_gc (lua_State *L, int what, int data) {
943 g->gcpause = data; 1067 g->gcpause = data;
944 break; 1068 break;
945 } 1069 }
1070 case LUA_GCSETMAJORINC: {
1071 res = g->gcmajorinc;
1072 g->gcmajorinc = data;
1073 break;
1074 }
946 case LUA_GCSETSTEPMUL: { 1075 case LUA_GCSETSTEPMUL: {
947 res = g->gcstepmul; 1076 res = g->gcstepmul;
948 g->gcstepmul = data; 1077 g->gcstepmul = data;
949 break; 1078 break;
950 } 1079 }
1080 case LUA_GCISRUNNING: {
1081 res = g->gcrunning;
1082 break;
1083 }
1084 case LUA_GCGEN: { /* change collector to generational mode */
1085 luaC_changemode(L, KGC_GEN);
1086 break;
1087 }
1088 case LUA_GCINC: { /* change collector to incremental mode */
1089 luaC_changemode(L, KGC_NORMAL);
1090 break;
1091 }
951 default: res = -1; /* invalid option */ 1092 default: res = -1; /* invalid option */
952 } 1093 }
953 lua_unlock(L); 1094 lua_unlock(L);
@@ -965,7 +1106,7 @@ LUA_API int lua_error (lua_State *L) {
965 lua_lock(L); 1106 lua_lock(L);
966 api_checknelems(L, 1); 1107 api_checknelems(L, 1);
967 luaG_errormsg(L); 1108 luaG_errormsg(L);
968 lua_unlock(L); 1109 /* code unreachable; will unlock when control actually leaves the kernel */
969 return 0; /* to avoid warnings */ 1110 return 0; /* to avoid warnings */
970} 1111}
971 1112
@@ -974,8 +1115,8 @@ LUA_API int lua_next (lua_State *L, int idx) {
974 StkId t; 1115 StkId t;
975 int more; 1116 int more;
976 lua_lock(L); 1117 lua_lock(L);
977 t = index2adr(L, idx); 1118 t = index2addr(L, idx);
978 api_check(L, ttistable(t)); 1119 api_check(L, ttistable(t), "table expected");
979 more = luaH_next(L, hvalue(t), L->top - 1); 1120 more = luaH_next(L, hvalue(t), L->top - 1);
980 if (more) { 1121 if (more) {
981 api_incr_top(L); 1122 api_incr_top(L);
@@ -992,8 +1133,7 @@ LUA_API void lua_concat (lua_State *L, int n) {
992 api_checknelems(L, n); 1133 api_checknelems(L, n);
993 if (n >= 2) { 1134 if (n >= 2) {
994 luaC_checkGC(L); 1135 luaC_checkGC(L);
995 luaV_concat(L, n, cast_int(L->top - L->base) - 1); 1136 luaV_concat(L, n);
996 L->top -= (n-1);
997 } 1137 }
998 else if (n == 0) { /* push empty string */ 1138 else if (n == 0) { /* push empty string */
999 setsvalue2s(L, L->top, luaS_newlstr(L, "", 0)); 1139 setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
@@ -1004,6 +1144,16 @@ LUA_API void lua_concat (lua_State *L, int n) {
1004} 1144}
1005 1145
1006 1146
1147LUA_API void lua_len (lua_State *L, int idx) {
1148 StkId t;
1149 lua_lock(L);
1150 t = index2addr(L, idx);
1151 luaV_objlen(L, L->top, t);
1152 api_incr_top(L);
1153 lua_unlock(L);
1154}
1155
1156
1007LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) { 1157LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
1008 lua_Alloc f; 1158 lua_Alloc f;
1009 lua_lock(L); 1159 lua_lock(L);
@@ -1026,7 +1176,7 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
1026 Udata *u; 1176 Udata *u;
1027 lua_lock(L); 1177 lua_lock(L);
1028 luaC_checkGC(L); 1178 luaC_checkGC(L);
1029 u = luaS_newudata(L, size, getcurrenv(L)); 1179 u = luaS_newudata(L, size, NULL);
1030 setuvalue(L, L->top, u); 1180 setuvalue(L, L->top, u);
1031 api_incr_top(L); 1181 api_incr_top(L);
1032 lua_unlock(L); 1182 lua_unlock(L);
@@ -1035,30 +1185,36 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
1035 1185
1036 1186
1037 1187
1038 1188static const char *aux_upvalue (StkId fi, int n, TValue **val,
1039static const char *aux_upvalue (StkId fi, int n, TValue **val) { 1189 GCObject **owner) {
1040 Closure *f; 1190 switch (ttype(fi)) {
1041 if (!ttisfunction(fi)) return NULL; 1191 case LUA_TCCL: { /* C closure */
1042 f = clvalue(fi); 1192 CClosure *f = clCvalue(fi);
1043 if (f->c.isC) { 1193 if (!(1 <= n && n <= f->nupvalues)) return NULL;
1044 if (!(1 <= n && n <= f->c.nupvalues)) return NULL; 1194 *val = &f->upvalue[n-1];
1045 *val = &f->c.upvalue[n-1]; 1195 if (owner) *owner = obj2gco(f);
1046 return ""; 1196 return "";
1047 } 1197 }
1048 else { 1198 case LUA_TLCL: { /* Lua closure */
1049 Proto *p = f->l.p; 1199 LClosure *f = clLvalue(fi);
1050 if (!(1 <= n && n <= p->sizeupvalues)) return NULL; 1200 TString *name;
1051 *val = f->l.upvals[n-1]->v; 1201 Proto *p = f->p;
1052 return getstr(p->upvalues[n-1]); 1202 if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
1203 *val = f->upvals[n-1]->v;
1204 if (owner) *owner = obj2gco(f->upvals[n - 1]);
1205 name = p->upvalues[n-1].name;
1206 return (name == NULL) ? "" : getstr(name);
1207 }
1208 default: return NULL; /* not a closure */
1053 } 1209 }
1054} 1210}
1055 1211
1056 1212
1057LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { 1213LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
1058 const char *name; 1214 const char *name;
1059 TValue *val; 1215 TValue *val = NULL; /* to avoid warnings */
1060 lua_lock(L); 1216 lua_lock(L);
1061 name = aux_upvalue(index2adr(L, funcindex), n, &val); 1217 name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL);
1062 if (name) { 1218 if (name) {
1063 setobj2s(L, L->top, val); 1219 setobj2s(L, L->top, val);
1064 api_incr_top(L); 1220 api_incr_top(L);
@@ -1070,18 +1226,59 @@ LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
1070 1226
1071LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { 1227LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
1072 const char *name; 1228 const char *name;
1073 TValue *val; 1229 TValue *val = NULL; /* to avoid warnings */
1230 GCObject *owner = NULL; /* to avoid warnings */
1074 StkId fi; 1231 StkId fi;
1075 lua_lock(L); 1232 lua_lock(L);
1076 fi = index2adr(L, funcindex); 1233 fi = index2addr(L, funcindex);
1077 api_checknelems(L, 1); 1234 api_checknelems(L, 1);
1078 name = aux_upvalue(fi, n, &val); 1235 name = aux_upvalue(fi, n, &val, &owner);
1079 if (name) { 1236 if (name) {
1080 L->top--; 1237 L->top--;
1081 setobj(L, val, L->top); 1238 setobj(L, val, L->top);
1082 luaC_barrier(L, clvalue(fi), L->top); 1239 luaC_barrier(L, owner, L->top);
1083 } 1240 }
1084 lua_unlock(L); 1241 lua_unlock(L);
1085 return name; 1242 return name;
1086} 1243}
1087 1244
1245
1246static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
1247 LClosure *f;
1248 StkId fi = index2addr(L, fidx);
1249 api_check(L, ttisLclosure(fi), "Lua function expected");
1250 f = clLvalue(fi);
1251 api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
1252 if (pf) *pf = f;
1253 return &f->upvals[n - 1]; /* get its upvalue pointer */
1254}
1255
1256
1257LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
1258 StkId fi = index2addr(L, fidx);
1259 switch (ttype(fi)) {
1260 case LUA_TLCL: { /* lua closure */
1261 return *getupvalref(L, fidx, n, NULL);
1262 }
1263 case LUA_TCCL: { /* C closure */
1264 CClosure *f = clCvalue(fi);
1265 api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
1266 return &f->upvalue[n - 1];
1267 }
1268 default: {
1269 api_check(L, 0, "closure expected");
1270 return NULL;
1271 }
1272 }
1273}
1274
1275
1276LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
1277 int fidx2, int n2) {
1278 LClosure *f1;
1279 UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
1280 UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
1281 *up1 = *up2;
1282 luaC_objbarrier(L, f1, *up2);
1283}
1284