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