/src/testdir/build/lua-master/source/lapi.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | ** $Id: lapi.c $ |
3 | | ** Lua API |
4 | | ** See Copyright Notice in lua.h |
5 | | */ |
6 | | |
7 | | #define lapi_c |
8 | | #define LUA_CORE |
9 | | |
10 | | #include "lprefix.h" |
11 | | |
12 | | |
13 | | #include <limits.h> |
14 | | #include <stdarg.h> |
15 | | #include <string.h> |
16 | | |
17 | | #include "lua.h" |
18 | | |
19 | | #include "lapi.h" |
20 | | #include "ldebug.h" |
21 | | #include "ldo.h" |
22 | | #include "lfunc.h" |
23 | | #include "lgc.h" |
24 | | #include "lmem.h" |
25 | | #include "lobject.h" |
26 | | #include "lstate.h" |
27 | | #include "lstring.h" |
28 | | #include "ltable.h" |
29 | | #include "ltm.h" |
30 | | #include "lundump.h" |
31 | | #include "lvm.h" |
32 | | |
33 | | |
34 | | |
35 | | const char lua_ident[] = |
36 | | "$LuaVersion: " LUA_COPYRIGHT " $" |
37 | | "$LuaAuthors: " LUA_AUTHORS " $"; |
38 | | |
39 | | |
40 | | |
41 | | /* |
42 | | ** Test for a valid index (one that is not the 'nilvalue'). |
43 | | ** '!ttisnil(o)' implies 'o != &G(L)->nilvalue', so it is not needed. |
44 | | ** However, it covers the most common cases in a faster way. |
45 | | */ |
46 | 0 | #define isvalid(L, o) (!ttisnil(o) || o != &G(L)->nilvalue) |
47 | | |
48 | | |
49 | | /* test for pseudo index */ |
50 | 1.96k | #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX) |
51 | | |
52 | | /* test for upvalue */ |
53 | 0 | #define isupvalue(i) ((i) < LUA_REGISTRYINDEX) |
54 | | |
55 | | |
56 | | /* |
57 | | ** Convert an acceptable index to a pointer to its respective value. |
58 | | ** Non-valid indices return the special nil value 'G(L)->nilvalue'. |
59 | | */ |
60 | 2.46k | static TValue *index2value (lua_State *L, int idx) { |
61 | 2.46k | CallInfo *ci = L->ci; |
62 | 2.46k | if (idx > 0) { |
63 | 492 | StkId o = ci->func.p + idx; |
64 | 492 | api_check(L, idx <= ci->top.p - (ci->func.p + 1), "unacceptable index"); |
65 | 492 | if (o >= L->top.p) return &G(L)->nilvalue; |
66 | 492 | else return s2v(o); |
67 | 492 | } |
68 | 1.96k | else if (!ispseudo(idx)) { /* negative index */ |
69 | 1.47k | api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1), |
70 | 1.47k | "invalid index"); |
71 | 1.47k | return s2v(L->top.p + idx); |
72 | 1.47k | } |
73 | 492 | else if (idx == LUA_REGISTRYINDEX) |
74 | 492 | return &G(L)->l_registry; |
75 | 0 | else { /* upvalues */ |
76 | 0 | idx = LUA_REGISTRYINDEX - idx; |
77 | 0 | api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large"); |
78 | 0 | if (ttisCclosure(s2v(ci->func.p))) { /* C closure? */ |
79 | 0 | CClosure *func = clCvalue(s2v(ci->func.p)); |
80 | 0 | return (idx <= func->nupvalues) ? &func->upvalue[idx-1] |
81 | 0 | : &G(L)->nilvalue; |
82 | 0 | } |
83 | 0 | else { /* light C function or Lua function (through a hook)?) */ |
84 | 0 | api_check(L, ttislcf(s2v(ci->func.p)), "caller not a C function"); |
85 | 0 | return &G(L)->nilvalue; /* no upvalues */ |
86 | 0 | } |
87 | 0 | } |
88 | 2.46k | } |
89 | | |
90 | | |
91 | | |
92 | | /* |
93 | | ** Convert a valid actual index (not a pseudo-index) to its address. |
94 | | */ |
95 | 1.48k | l_sinline StkId index2stack (lua_State *L, int idx) { |
96 | 1.48k | CallInfo *ci = L->ci; |
97 | 1.48k | if (idx > 0) { |
98 | 0 | StkId o = ci->func.p + idx; |
99 | 0 | api_check(L, o < L->top.p, "invalid index"); |
100 | 0 | return o; |
101 | 0 | } |
102 | 1.48k | else { /* non-positive index */ |
103 | 1.48k | api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1), |
104 | 1.48k | "invalid index"); |
105 | 1.48k | api_check(L, !ispseudo(idx), "invalid index"); |
106 | 1.48k | return L->top.p + idx; |
107 | 1.48k | } |
108 | 1.48k | } |
109 | | |
110 | | |
111 | 246 | LUA_API int lua_checkstack (lua_State *L, int n) { |
112 | 246 | int res; |
113 | 246 | CallInfo *ci; |
114 | 246 | lua_lock(L); |
115 | 246 | ci = L->ci; |
116 | 246 | api_check(L, n >= 0, "negative 'n'"); |
117 | 246 | if (L->stack_last.p - L->top.p > n) /* stack large enough? */ |
118 | 246 | res = 1; /* yes; check is OK */ |
119 | 0 | else /* need to grow stack */ |
120 | 0 | res = luaD_growstack(L, n, 0); |
121 | 246 | if (res && ci->top.p < L->top.p + n) |
122 | 0 | ci->top.p = L->top.p + n; /* adjust frame top */ |
123 | 246 | lua_unlock(L); |
124 | 246 | return res; |
125 | 246 | } |
126 | | |
127 | | |
128 | 0 | LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) { |
129 | 0 | int i; |
130 | 0 | if (from == to) return; |
131 | 0 | lua_lock(to); |
132 | 0 | api_checknelems(from, n); |
133 | 0 | api_check(from, G(from) == G(to), "moving among independent states"); |
134 | 0 | api_check(from, to->ci->top.p - to->top.p >= n, "stack overflow"); |
135 | 0 | from->top.p -= n; |
136 | 0 | for (i = 0; i < n; i++) { |
137 | 0 | setobjs2s(to, to->top.p, from->top.p + i); |
138 | 0 | to->top.p++; /* stack already checked by previous 'api_check' */ |
139 | 0 | } |
140 | 0 | lua_unlock(to); |
141 | 0 | } |
142 | | |
143 | | |
144 | 496 | LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) { |
145 | 496 | lua_CFunction old; |
146 | 496 | lua_lock(L); |
147 | 496 | old = G(L)->panic; |
148 | 496 | G(L)->panic = panicf; |
149 | 496 | lua_unlock(L); |
150 | 496 | return old; |
151 | 496 | } |
152 | | |
153 | | |
154 | 0 | LUA_API lua_Number lua_version (lua_State *L) { |
155 | 0 | UNUSED(L); |
156 | 0 | return LUA_VERSION_NUM; |
157 | 0 | } |
158 | | |
159 | | |
160 | | |
161 | | /* |
162 | | ** basic stack manipulation |
163 | | */ |
164 | | |
165 | | |
166 | | /* |
167 | | ** convert an acceptable stack index into an absolute index |
168 | | */ |
169 | 0 | LUA_API int lua_absindex (lua_State *L, int idx) { |
170 | 0 | return (idx > 0 || ispseudo(idx)) |
171 | 0 | ? idx |
172 | 0 | : cast_int(L->top.p - L->ci->func.p) + idx; |
173 | 0 | } |
174 | | |
175 | | |
176 | 0 | LUA_API int lua_gettop (lua_State *L) { |
177 | 0 | return cast_int(L->top.p - (L->ci->func.p + 1)); |
178 | 0 | } |
179 | | |
180 | | |
181 | 1.73k | LUA_API void lua_settop (lua_State *L, int idx) { |
182 | 1.73k | CallInfo *ci; |
183 | 1.73k | StkId func, newtop; |
184 | 1.73k | ptrdiff_t diff; /* difference for new top */ |
185 | 1.73k | lua_lock(L); |
186 | 1.73k | ci = L->ci; |
187 | 1.73k | func = ci->func.p; |
188 | 1.73k | if (idx >= 0) { |
189 | 496 | api_check(L, idx <= ci->top.p - (func + 1), "new top too large"); |
190 | 496 | diff = ((func + 1) + idx) - L->top.p; |
191 | 496 | for (; diff > 0; diff--) |
192 | 496 | setnilvalue(s2v(L->top.p++)); /* clear new slots */ |
193 | 496 | } |
194 | 1.23k | else { |
195 | 1.23k | api_check(L, -(idx+1) <= (L->top.p - (func + 1)), "invalid new top"); |
196 | 1.23k | diff = idx + 1; /* will "subtract" index (as it is negative) */ |
197 | 1.23k | } |
198 | 1.73k | api_check(L, L->tbclist.p < L->top.p, "previous pop of an unclosed slot"); |
199 | 1.73k | newtop = L->top.p + diff; |
200 | 1.73k | if (diff < 0 && L->tbclist.p >= newtop) { |
201 | 0 | lua_assert(hastocloseCfunc(ci->nresults)); |
202 | 0 | newtop = luaF_close(L, newtop, CLOSEKTOP, 0); |
203 | 0 | } |
204 | 1.73k | L->top.p = newtop; /* correct top only after closing any upvalue */ |
205 | 1.73k | lua_unlock(L); |
206 | 1.73k | } |
207 | | |
208 | | |
209 | 246 | LUA_API void lua_closeslot (lua_State *L, int idx) { |
210 | 246 | StkId level; |
211 | 246 | lua_lock(L); |
212 | 246 | level = index2stack(L, idx); |
213 | 246 | api_check(L, hastocloseCfunc(L->ci->nresults) && L->tbclist.p == level, |
214 | 246 | "no variable to close at given level"); |
215 | 246 | level = luaF_close(L, level, CLOSEKTOP, 0); |
216 | 246 | setnilvalue(s2v(level)); |
217 | 246 | lua_unlock(L); |
218 | 246 | } |
219 | | |
220 | | |
221 | | /* |
222 | | ** Reverse the stack segment from 'from' to 'to' |
223 | | ** (auxiliary to 'lua_rotate') |
224 | | ** Note that we move(copy) only the value inside the stack. |
225 | | ** (We do not move additional fields that may exist.) |
226 | | */ |
227 | 2.96k | l_sinline void reverse (lua_State *L, StkId from, StkId to) { |
228 | 3.46k | for (; from < to; from++, to--) { |
229 | 496 | TValue temp; |
230 | 496 | setobj(L, &temp, s2v(from)); |
231 | 496 | setobjs2s(L, from, to); |
232 | 496 | setobj2s(L, to, &temp); |
233 | 496 | } |
234 | 2.96k | } |
235 | | |
236 | | |
237 | | /* |
238 | | ** Let x = AB, where A is a prefix of length 'n'. Then, |
239 | | ** rotate x n == BA. But BA == (A^r . B^r)^r. |
240 | | */ |
241 | 988 | LUA_API void lua_rotate (lua_State *L, int idx, int n) { |
242 | 988 | StkId p, t, m; |
243 | 988 | lua_lock(L); |
244 | 988 | t = L->top.p - 1; /* end of stack segment being rotated */ |
245 | 988 | p = index2stack(L, idx); /* start of segment */ |
246 | 988 | api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'"); |
247 | 988 | m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */ |
248 | 988 | reverse(L, p, m); /* reverse the prefix with length 'n' */ |
249 | 988 | reverse(L, m + 1, t); /* reverse the suffix */ |
250 | 988 | reverse(L, p, t); /* reverse the entire segment */ |
251 | 988 | lua_unlock(L); |
252 | 988 | } |
253 | | |
254 | | |
255 | 0 | LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { |
256 | 0 | TValue *fr, *to; |
257 | 0 | lua_lock(L); |
258 | 0 | fr = index2value(L, fromidx); |
259 | 0 | to = index2value(L, toidx); |
260 | 0 | api_check(L, isvalid(L, to), "invalid index"); |
261 | 0 | setobj(L, to, fr); |
262 | 0 | if (isupvalue(toidx)) /* function upvalue? */ |
263 | 0 | luaC_barrier(L, clCvalue(s2v(L->ci->func.p)), fr); |
264 | | /* LUA_REGISTRYINDEX does not need gc barrier |
265 | | (collector revisits it before finishing collection) */ |
266 | 0 | lua_unlock(L); |
267 | 0 | } |
268 | | |
269 | | |
270 | 246 | LUA_API void lua_pushvalue (lua_State *L, int idx) { |
271 | 246 | lua_lock(L); |
272 | 246 | setobj2s(L, L->top.p, index2value(L, idx)); |
273 | 246 | api_incr_top(L); |
274 | 246 | lua_unlock(L); |
275 | 246 | } |
276 | | |
277 | | |
278 | | |
279 | | /* |
280 | | ** access functions (stack -> C) |
281 | | */ |
282 | | |
283 | | |
284 | 0 | LUA_API int lua_type (lua_State *L, int idx) { |
285 | 0 | const TValue *o = index2value(L, idx); |
286 | 0 | return (isvalid(L, o) ? ttype(o) : LUA_TNONE); |
287 | 0 | } |
288 | | |
289 | | |
290 | 0 | LUA_API const char *lua_typename (lua_State *L, int t) { |
291 | 0 | UNUSED(L); |
292 | 0 | api_check(L, LUA_TNONE <= t && t < LUA_NUMTYPES, "invalid type"); |
293 | 0 | return ttypename(t); |
294 | 0 | } |
295 | | |
296 | | |
297 | 0 | LUA_API int lua_iscfunction (lua_State *L, int idx) { |
298 | 0 | const TValue *o = index2value(L, idx); |
299 | 0 | return (ttislcf(o) || (ttisCclosure(o))); |
300 | 0 | } |
301 | | |
302 | | |
303 | 0 | LUA_API int lua_isinteger (lua_State *L, int idx) { |
304 | 0 | const TValue *o = index2value(L, idx); |
305 | 0 | return ttisinteger(o); |
306 | 0 | } |
307 | | |
308 | | |
309 | 0 | LUA_API int lua_isnumber (lua_State *L, int idx) { |
310 | 0 | lua_Number n; |
311 | 0 | const TValue *o = index2value(L, idx); |
312 | 0 | return tonumber(o, &n); |
313 | 0 | } |
314 | | |
315 | | |
316 | 0 | LUA_API int lua_isstring (lua_State *L, int idx) { |
317 | 0 | const TValue *o = index2value(L, idx); |
318 | 0 | return (ttisstring(o) || cvt2str(o)); |
319 | 0 | } |
320 | | |
321 | | |
322 | 0 | LUA_API int lua_isuserdata (lua_State *L, int idx) { |
323 | 0 | const TValue *o = index2value(L, idx); |
324 | 0 | return (ttisfulluserdata(o) || ttislightuserdata(o)); |
325 | 0 | } |
326 | | |
327 | | |
328 | 0 | LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { |
329 | 0 | const TValue *o1 = index2value(L, index1); |
330 | 0 | const TValue *o2 = index2value(L, index2); |
331 | 0 | return (isvalid(L, o1) && isvalid(L, o2)) ? luaV_rawequalobj(o1, o2) : 0; |
332 | 0 | } |
333 | | |
334 | | |
335 | 0 | LUA_API void lua_arith (lua_State *L, int op) { |
336 | 0 | lua_lock(L); |
337 | 0 | if (op != LUA_OPUNM && op != LUA_OPBNOT) |
338 | 0 | api_checknelems(L, 2); /* all other operations expect two operands */ |
339 | 0 | else { /* for unary operations, add fake 2nd operand */ |
340 | 0 | api_checknelems(L, 1); |
341 | 0 | setobjs2s(L, L->top.p, L->top.p - 1); |
342 | 0 | api_incr_top(L); |
343 | 0 | } |
344 | | /* first operand at top - 2, second at top - 1; result go to top - 2 */ |
345 | 0 | luaO_arith(L, op, s2v(L->top.p - 2), s2v(L->top.p - 1), L->top.p - 2); |
346 | 0 | L->top.p--; /* remove second operand */ |
347 | 0 | lua_unlock(L); |
348 | 0 | } |
349 | | |
350 | | |
351 | 0 | LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { |
352 | 0 | const TValue *o1; |
353 | 0 | const TValue *o2; |
354 | 0 | int i = 0; |
355 | 0 | lua_lock(L); /* may call tag method */ |
356 | 0 | o1 = index2value(L, index1); |
357 | 0 | o2 = index2value(L, index2); |
358 | 0 | if (isvalid(L, o1) && isvalid(L, o2)) { |
359 | 0 | switch (op) { |
360 | 0 | case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break; |
361 | 0 | case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break; |
362 | 0 | case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break; |
363 | 0 | default: api_check(L, 0, "invalid option"); |
364 | 0 | } |
365 | 0 | } |
366 | 0 | lua_unlock(L); |
367 | 0 | return i; |
368 | 0 | } |
369 | | |
370 | | |
371 | 0 | LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) { |
372 | 0 | size_t sz = luaO_str2num(s, s2v(L->top.p)); |
373 | 0 | if (sz != 0) |
374 | 0 | api_incr_top(L); |
375 | 0 | return sz; |
376 | 0 | } |
377 | | |
378 | | |
379 | 0 | LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) { |
380 | 0 | lua_Number n = 0; |
381 | 0 | const TValue *o = index2value(L, idx); |
382 | 0 | int isnum = tonumber(o, &n); |
383 | 0 | if (pisnum) |
384 | 0 | *pisnum = isnum; |
385 | 0 | return n; |
386 | 0 | } |
387 | | |
388 | | |
389 | 0 | LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) { |
390 | 0 | lua_Integer res = 0; |
391 | 0 | const TValue *o = index2value(L, idx); |
392 | 0 | int isnum = tointeger(o, &res); |
393 | 0 | if (pisnum) |
394 | 0 | *pisnum = isnum; |
395 | 0 | return res; |
396 | 0 | } |
397 | | |
398 | | |
399 | 0 | LUA_API int lua_toboolean (lua_State *L, int idx) { |
400 | 0 | const TValue *o = index2value(L, idx); |
401 | 0 | return !l_isfalse(o); |
402 | 0 | } |
403 | | |
404 | | |
405 | 0 | LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { |
406 | 0 | TValue *o; |
407 | 0 | lua_lock(L); |
408 | 0 | o = index2value(L, idx); |
409 | 0 | if (!ttisstring(o)) { |
410 | 0 | if (!cvt2str(o)) { /* not convertible? */ |
411 | 0 | if (len != NULL) *len = 0; |
412 | 0 | lua_unlock(L); |
413 | 0 | return NULL; |
414 | 0 | } |
415 | 0 | luaO_tostring(L, o); |
416 | 0 | luaC_checkGC(L); |
417 | 0 | o = index2value(L, idx); /* previous call may reallocate the stack */ |
418 | 0 | } |
419 | 0 | if (len != NULL) |
420 | 0 | *len = tsslen(tsvalue(o)); |
421 | 0 | lua_unlock(L); |
422 | 0 | return getstr(tsvalue(o)); |
423 | 0 | } |
424 | | |
425 | | |
426 | 0 | LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) { |
427 | 0 | const TValue *o = index2value(L, idx); |
428 | 0 | switch (ttypetag(o)) { |
429 | 0 | case LUA_VSHRSTR: return tsvalue(o)->shrlen; |
430 | 0 | case LUA_VLNGSTR: return tsvalue(o)->u.lnglen; |
431 | 0 | case LUA_VUSERDATA: return uvalue(o)->len; |
432 | 0 | case LUA_VTABLE: return luaH_getn(hvalue(o)); |
433 | 0 | default: return 0; |
434 | 0 | } |
435 | 0 | } |
436 | | |
437 | | |
438 | 0 | LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { |
439 | 0 | const TValue *o = index2value(L, idx); |
440 | 0 | if (ttislcf(o)) return fvalue(o); |
441 | 0 | else if (ttisCclosure(o)) |
442 | 0 | return clCvalue(o)->f; |
443 | 0 | else return NULL; /* not a C function */ |
444 | 0 | } |
445 | | |
446 | | |
447 | 738 | l_sinline void *touserdata (const TValue *o) { |
448 | 738 | switch (ttype(o)) { |
449 | 738 | case LUA_TUSERDATA: return getudatamem(uvalue(o)); |
450 | 0 | case LUA_TLIGHTUSERDATA: return pvalue(o); |
451 | 0 | default: return NULL; |
452 | 738 | } |
453 | 738 | } |
454 | | |
455 | | |
456 | 738 | LUA_API void *lua_touserdata (lua_State *L, int idx) { |
457 | 738 | const TValue *o = index2value(L, idx); |
458 | 738 | return touserdata(o); |
459 | 738 | } |
460 | | |
461 | | |
462 | 0 | LUA_API lua_State *lua_tothread (lua_State *L, int idx) { |
463 | 0 | const TValue *o = index2value(L, idx); |
464 | 0 | return (!ttisthread(o)) ? NULL : thvalue(o); |
465 | 0 | } |
466 | | |
467 | | |
468 | | /* |
469 | | ** Returns a pointer to the internal representation of an object. |
470 | | ** Note that ANSI C does not allow the conversion of a pointer to |
471 | | ** function to a 'void*', so the conversion here goes through |
472 | | ** a 'size_t'. (As the returned pointer is only informative, this |
473 | | ** conversion should not be a problem.) |
474 | | */ |
475 | 0 | LUA_API const void *lua_topointer (lua_State *L, int idx) { |
476 | 0 | const TValue *o = index2value(L, idx); |
477 | 0 | switch (ttypetag(o)) { |
478 | 0 | case LUA_VLCF: return cast_voidp(cast_sizet(fvalue(o))); |
479 | 0 | case LUA_VUSERDATA: case LUA_VLIGHTUSERDATA: |
480 | 0 | return touserdata(o); |
481 | 0 | default: { |
482 | 0 | if (iscollectable(o)) |
483 | 0 | return gcvalue(o); |
484 | 0 | else |
485 | 0 | return NULL; |
486 | 0 | } |
487 | 0 | } |
488 | 0 | } |
489 | | |
490 | | |
491 | | |
492 | | /* |
493 | | ** push functions (C -> stack) |
494 | | */ |
495 | | |
496 | | |
497 | 0 | LUA_API void lua_pushnil (lua_State *L) { |
498 | 0 | lua_lock(L); |
499 | 0 | setnilvalue(s2v(L->top.p)); |
500 | 0 | api_incr_top(L); |
501 | 0 | lua_unlock(L); |
502 | 0 | } |
503 | | |
504 | | |
505 | 0 | LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { |
506 | 0 | lua_lock(L); |
507 | 0 | setfltvalue(s2v(L->top.p), n); |
508 | 0 | api_incr_top(L); |
509 | 0 | lua_unlock(L); |
510 | 0 | } |
511 | | |
512 | | |
513 | 0 | LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { |
514 | 0 | lua_lock(L); |
515 | 0 | setivalue(s2v(L->top.p), n); |
516 | 0 | api_incr_top(L); |
517 | 0 | lua_unlock(L); |
518 | 0 | } |
519 | | |
520 | | |
521 | | /* |
522 | | ** Pushes on the stack a string with given length. Avoid using 's' when |
523 | | ** 'len' == 0 (as 's' can be NULL in that case), due to later use of |
524 | | ** 'memcmp' and 'memcpy'. |
525 | | */ |
526 | 496 | LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { |
527 | 496 | TString *ts; |
528 | 496 | lua_lock(L); |
529 | 496 | ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len); |
530 | 496 | setsvalue2s(L, L->top.p, ts); |
531 | 496 | api_incr_top(L); |
532 | 496 | luaC_checkGC(L); |
533 | 496 | lua_unlock(L); |
534 | 496 | return getstr(ts); |
535 | 496 | } |
536 | | |
537 | | |
538 | 246 | LUA_API const char *lua_pushstring (lua_State *L, const char *s) { |
539 | 246 | lua_lock(L); |
540 | 246 | if (s == NULL) |
541 | 246 | setnilvalue(s2v(L->top.p)); |
542 | 246 | else { |
543 | 246 | TString *ts; |
544 | 246 | ts = luaS_new(L, s); |
545 | 246 | setsvalue2s(L, L->top.p, ts); |
546 | 246 | s = getstr(ts); /* internal copy's address */ |
547 | 246 | } |
548 | 246 | api_incr_top(L); |
549 | 246 | luaC_checkGC(L); |
550 | 246 | lua_unlock(L); |
551 | 246 | return s; |
552 | 246 | } |
553 | | |
554 | | |
555 | | LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt, |
556 | 0 | va_list argp) { |
557 | 0 | const char *ret; |
558 | 0 | lua_lock(L); |
559 | 0 | ret = luaO_pushvfstring(L, fmt, argp); |
560 | 0 | luaC_checkGC(L); |
561 | 0 | lua_unlock(L); |
562 | 0 | return ret; |
563 | 0 | } |
564 | | |
565 | | |
566 | 0 | LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { |
567 | 0 | const char *ret; |
568 | 0 | va_list argp; |
569 | 0 | lua_lock(L); |
570 | 0 | va_start(argp, fmt); |
571 | 0 | ret = luaO_pushvfstring(L, fmt, argp); |
572 | 0 | va_end(argp); |
573 | 0 | luaC_checkGC(L); |
574 | 0 | lua_unlock(L); |
575 | 0 | return ret; |
576 | 0 | } |
577 | | |
578 | | |
579 | 492 | LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { |
580 | 492 | lua_lock(L); |
581 | 492 | if (n == 0) { |
582 | 492 | setfvalue(s2v(L->top.p), fn); |
583 | 492 | api_incr_top(L); |
584 | 492 | } |
585 | 0 | else { |
586 | 0 | CClosure *cl; |
587 | 0 | api_checknelems(L, n); |
588 | 0 | api_check(L, n <= MAXUPVAL, "upvalue index too large"); |
589 | 0 | cl = luaF_newCclosure(L, n); |
590 | 0 | cl->f = fn; |
591 | 0 | L->top.p -= n; |
592 | 0 | while (n--) { |
593 | 0 | setobj2n(L, &cl->upvalue[n], s2v(L->top.p + n)); |
594 | | /* does not need barrier because closure is white */ |
595 | 0 | lua_assert(iswhite(cl)); |
596 | 0 | } |
597 | 0 | setclCvalue(L, s2v(L->top.p), cl); |
598 | 0 | api_incr_top(L); |
599 | 0 | luaC_checkGC(L); |
600 | 0 | } |
601 | 492 | lua_unlock(L); |
602 | 492 | } |
603 | | |
604 | | |
605 | 0 | LUA_API void lua_pushboolean (lua_State *L, int b) { |
606 | 0 | lua_lock(L); |
607 | 0 | if (b) |
608 | 0 | setbtvalue(s2v(L->top.p)); |
609 | 0 | else |
610 | 0 | setbfvalue(s2v(L->top.p)); |
611 | 0 | api_incr_top(L); |
612 | 0 | lua_unlock(L); |
613 | 0 | } |
614 | | |
615 | | |
616 | 496 | LUA_API void lua_pushlightuserdata (lua_State *L, void *p) { |
617 | 496 | lua_lock(L); |
618 | 496 | setpvalue(s2v(L->top.p), p); |
619 | 496 | api_incr_top(L); |
620 | 496 | lua_unlock(L); |
621 | 496 | } |
622 | | |
623 | | |
624 | 0 | LUA_API int lua_pushthread (lua_State *L) { |
625 | 0 | lua_lock(L); |
626 | 0 | setthvalue(L, s2v(L->top.p), L); |
627 | 0 | api_incr_top(L); |
628 | 0 | lua_unlock(L); |
629 | 0 | return (G(L)->mainthread == L); |
630 | 0 | } |
631 | | |
632 | | |
633 | | |
634 | | /* |
635 | | ** get functions (Lua -> stack) |
636 | | */ |
637 | | |
638 | | |
639 | 246 | l_sinline int auxgetstr (lua_State *L, const TValue *t, const char *k) { |
640 | 246 | const TValue *slot; |
641 | 246 | TString *str = luaS_new(L, k); |
642 | 246 | if (luaV_fastget(L, t, str, slot, luaH_getstr)) { |
643 | 0 | setobj2s(L, L->top.p, slot); |
644 | 0 | api_incr_top(L); |
645 | 0 | } |
646 | 246 | else { |
647 | 246 | setsvalue2s(L, L->top.p, str); |
648 | 246 | api_incr_top(L); |
649 | 246 | luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot); |
650 | 246 | } |
651 | 246 | lua_unlock(L); |
652 | 246 | return ttype(s2v(L->top.p - 1)); |
653 | 246 | } |
654 | | |
655 | | |
656 | | /* |
657 | | ** Get the global table in the registry. Since all predefined |
658 | | ** indices in the registry were inserted right when the registry |
659 | | ** was created and never removed, they must always be in the array |
660 | | ** part of the registry. |
661 | | */ |
662 | | #define getGtable(L) \ |
663 | 0 | (&hvalue(&G(L)->l_registry)->array[LUA_RIDX_GLOBALS - 1]) |
664 | | |
665 | | |
666 | 0 | LUA_API int lua_getglobal (lua_State *L, const char *name) { |
667 | 0 | const TValue *G; |
668 | 0 | lua_lock(L); |
669 | 0 | G = getGtable(L); |
670 | 0 | return auxgetstr(L, G, name); |
671 | 0 | } |
672 | | |
673 | | |
674 | 0 | LUA_API int lua_gettable (lua_State *L, int idx) { |
675 | 0 | const TValue *slot; |
676 | 0 | TValue *t; |
677 | 0 | lua_lock(L); |
678 | 0 | t = index2value(L, idx); |
679 | 0 | if (luaV_fastget(L, t, s2v(L->top.p - 1), slot, luaH_get)) { |
680 | 0 | setobj2s(L, L->top.p - 1, slot); |
681 | 0 | } |
682 | 0 | else |
683 | 0 | luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot); |
684 | 0 | lua_unlock(L); |
685 | 0 | return ttype(s2v(L->top.p - 1)); |
686 | 0 | } |
687 | | |
688 | | |
689 | 246 | LUA_API int lua_getfield (lua_State *L, int idx, const char *k) { |
690 | 246 | lua_lock(L); |
691 | 246 | return auxgetstr(L, index2value(L, idx), k); |
692 | 246 | } |
693 | | |
694 | | |
695 | 0 | LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { |
696 | 0 | TValue *t; |
697 | 0 | const TValue *slot; |
698 | 0 | lua_lock(L); |
699 | 0 | t = index2value(L, idx); |
700 | 0 | if (luaV_fastgeti(L, t, n, slot)) { |
701 | 0 | setobj2s(L, L->top.p, slot); |
702 | 0 | } |
703 | 0 | else { |
704 | 0 | TValue aux; |
705 | 0 | setivalue(&aux, n); |
706 | 0 | luaV_finishget(L, t, &aux, L->top.p, slot); |
707 | 0 | } |
708 | 0 | api_incr_top(L); |
709 | 0 | lua_unlock(L); |
710 | 0 | return ttype(s2v(L->top.p - 1)); |
711 | 0 | } |
712 | | |
713 | | |
714 | 0 | l_sinline int finishrawget (lua_State *L, const TValue *val) { |
715 | 0 | if (isempty(val)) /* avoid copying empty items to the stack */ |
716 | 0 | setnilvalue(s2v(L->top.p)); |
717 | 0 | else |
718 | 0 | setobj2s(L, L->top.p, val); |
719 | 0 | api_incr_top(L); |
720 | 0 | lua_unlock(L); |
721 | 0 | return ttype(s2v(L->top.p - 1)); |
722 | 0 | } |
723 | | |
724 | | |
725 | 0 | static Table *gettable (lua_State *L, int idx) { |
726 | 0 | TValue *t = index2value(L, idx); |
727 | 0 | api_check(L, ttistable(t), "table expected"); |
728 | 0 | return hvalue(t); |
729 | 0 | } |
730 | | |
731 | | |
732 | 0 | LUA_API int lua_rawget (lua_State *L, int idx) { |
733 | 0 | Table *t; |
734 | 0 | const TValue *val; |
735 | 0 | lua_lock(L); |
736 | 0 | api_checknelems(L, 1); |
737 | 0 | t = gettable(L, idx); |
738 | 0 | val = luaH_get(t, s2v(L->top.p - 1)); |
739 | 0 | L->top.p--; /* remove key */ |
740 | 0 | return finishrawget(L, val); |
741 | 0 | } |
742 | | |
743 | | |
744 | 0 | LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { |
745 | 0 | Table *t; |
746 | 0 | lua_lock(L); |
747 | 0 | t = gettable(L, idx); |
748 | 0 | return finishrawget(L, luaH_getint(t, n)); |
749 | 0 | } |
750 | | |
751 | | |
752 | 0 | LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { |
753 | 0 | Table *t; |
754 | 0 | TValue k; |
755 | 0 | lua_lock(L); |
756 | 0 | t = gettable(L, idx); |
757 | 0 | setpvalue(&k, cast_voidp(p)); |
758 | 0 | return finishrawget(L, luaH_get(t, &k)); |
759 | 0 | } |
760 | | |
761 | | |
762 | 246 | LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { |
763 | 246 | Table *t; |
764 | 246 | lua_lock(L); |
765 | 246 | t = luaH_new(L); |
766 | 246 | sethvalue2s(L, L->top.p, t); |
767 | 246 | api_incr_top(L); |
768 | 246 | if (narray > 0 || nrec > 0) |
769 | 246 | luaH_resize(L, t, narray, nrec); |
770 | 246 | luaC_checkGC(L); |
771 | 246 | lua_unlock(L); |
772 | 246 | } |
773 | | |
774 | | |
775 | 0 | LUA_API int lua_getmetatable (lua_State *L, int objindex) { |
776 | 0 | const TValue *obj; |
777 | 0 | Table *mt; |
778 | 0 | int res = 0; |
779 | 0 | lua_lock(L); |
780 | 0 | obj = index2value(L, objindex); |
781 | 0 | switch (ttype(obj)) { |
782 | 0 | case LUA_TTABLE: |
783 | 0 | mt = hvalue(obj)->metatable; |
784 | 0 | break; |
785 | 0 | case LUA_TUSERDATA: |
786 | 0 | mt = uvalue(obj)->metatable; |
787 | 0 | break; |
788 | 0 | default: |
789 | 0 | mt = G(L)->mt[ttype(obj)]; |
790 | 0 | break; |
791 | 0 | } |
792 | 0 | if (mt != NULL) { |
793 | 0 | sethvalue2s(L, L->top.p, mt); |
794 | 0 | api_incr_top(L); |
795 | 0 | res = 1; |
796 | 0 | } |
797 | 0 | lua_unlock(L); |
798 | 0 | return res; |
799 | 0 | } |
800 | | |
801 | | |
802 | 0 | LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) { |
803 | 0 | TValue *o; |
804 | 0 | int t; |
805 | 0 | lua_lock(L); |
806 | 0 | o = index2value(L, idx); |
807 | 0 | api_check(L, ttisfulluserdata(o), "full userdata expected"); |
808 | 0 | if (n <= 0 || n > uvalue(o)->nuvalue) { |
809 | 0 | setnilvalue(s2v(L->top.p)); |
810 | 0 | t = LUA_TNONE; |
811 | 0 | } |
812 | 0 | else { |
813 | 0 | setobj2s(L, L->top.p, &uvalue(o)->uv[n - 1].uv); |
814 | 0 | t = ttype(s2v(L->top.p)); |
815 | 0 | } |
816 | 0 | api_incr_top(L); |
817 | 0 | lua_unlock(L); |
818 | 0 | return t; |
819 | 0 | } |
820 | | |
821 | | |
822 | | /* |
823 | | ** set functions (stack -> Lua) |
824 | | */ |
825 | | |
826 | | /* |
827 | | ** t[k] = value at the top of the stack (where 'k' is a string) |
828 | | */ |
829 | 984 | static void auxsetstr (lua_State *L, const TValue *t, const char *k) { |
830 | 984 | const TValue *slot; |
831 | 984 | TString *str = luaS_new(L, k); |
832 | 984 | api_checknelems(L, 1); |
833 | 984 | if (luaV_fastget(L, t, str, slot, luaH_getstr)) { |
834 | 0 | luaV_finishfastset(L, t, slot, s2v(L->top.p - 1)); |
835 | 0 | L->top.p--; /* pop value */ |
836 | 0 | } |
837 | 984 | else { |
838 | 984 | setsvalue2s(L, L->top.p, str); /* push 'str' (to make it a TValue) */ |
839 | 984 | api_incr_top(L); |
840 | 984 | luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), slot); |
841 | 984 | L->top.p -= 2; /* pop value and key */ |
842 | 984 | } |
843 | 984 | lua_unlock(L); /* lock done by caller */ |
844 | 984 | } |
845 | | |
846 | | |
847 | 0 | LUA_API void lua_setglobal (lua_State *L, const char *name) { |
848 | 0 | const TValue *G; |
849 | 0 | lua_lock(L); /* unlock done in 'auxsetstr' */ |
850 | 0 | G = getGtable(L); |
851 | 0 | auxsetstr(L, G, name); |
852 | 0 | } |
853 | | |
854 | | |
855 | 0 | LUA_API void lua_settable (lua_State *L, int idx) { |
856 | 0 | TValue *t; |
857 | 0 | const TValue *slot; |
858 | 0 | lua_lock(L); |
859 | 0 | api_checknelems(L, 2); |
860 | 0 | t = index2value(L, idx); |
861 | 0 | if (luaV_fastget(L, t, s2v(L->top.p - 2), slot, luaH_get)) { |
862 | 0 | luaV_finishfastset(L, t, slot, s2v(L->top.p - 1)); |
863 | 0 | } |
864 | 0 | else |
865 | 0 | luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), slot); |
866 | 0 | L->top.p -= 2; /* pop index and value */ |
867 | 0 | lua_unlock(L); |
868 | 0 | } |
869 | | |
870 | | |
871 | 984 | LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { |
872 | 984 | lua_lock(L); /* unlock done in 'auxsetstr' */ |
873 | 984 | auxsetstr(L, index2value(L, idx), k); |
874 | 984 | } |
875 | | |
876 | | |
877 | 0 | LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { |
878 | 0 | TValue *t; |
879 | 0 | const TValue *slot; |
880 | 0 | lua_lock(L); |
881 | 0 | api_checknelems(L, 1); |
882 | 0 | t = index2value(L, idx); |
883 | 0 | if (luaV_fastgeti(L, t, n, slot)) { |
884 | 0 | luaV_finishfastset(L, t, slot, s2v(L->top.p - 1)); |
885 | 0 | } |
886 | 0 | else { |
887 | 0 | TValue aux; |
888 | 0 | setivalue(&aux, n); |
889 | 0 | luaV_finishset(L, t, &aux, s2v(L->top.p - 1), slot); |
890 | 0 | } |
891 | 0 | L->top.p--; /* pop value */ |
892 | 0 | lua_unlock(L); |
893 | 0 | } |
894 | | |
895 | | |
896 | 0 | static void aux_rawset (lua_State *L, int idx, TValue *key, int n) { |
897 | 0 | Table *t; |
898 | 0 | lua_lock(L); |
899 | 0 | api_checknelems(L, n); |
900 | 0 | t = gettable(L, idx); |
901 | 0 | luaH_set(L, t, key, s2v(L->top.p - 1)); |
902 | 0 | invalidateTMcache(t); |
903 | 0 | luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1)); |
904 | 0 | L->top.p -= n; |
905 | 0 | lua_unlock(L); |
906 | 0 | } |
907 | | |
908 | | |
909 | 0 | LUA_API void lua_rawset (lua_State *L, int idx) { |
910 | 0 | aux_rawset(L, idx, s2v(L->top.p - 2), 2); |
911 | 0 | } |
912 | | |
913 | | |
914 | 0 | LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { |
915 | 0 | TValue k; |
916 | 0 | setpvalue(&k, cast_voidp(p)); |
917 | 0 | aux_rawset(L, idx, &k, 1); |
918 | 0 | } |
919 | | |
920 | | |
921 | 0 | LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { |
922 | 0 | Table *t; |
923 | 0 | lua_lock(L); |
924 | 0 | api_checknelems(L, 1); |
925 | 0 | t = gettable(L, idx); |
926 | 0 | luaH_setint(L, t, n, s2v(L->top.p - 1)); |
927 | 0 | luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1)); |
928 | 0 | L->top.p--; |
929 | 0 | lua_unlock(L); |
930 | 0 | } |
931 | | |
932 | | |
933 | 246 | LUA_API int lua_setmetatable (lua_State *L, int objindex) { |
934 | 246 | TValue *obj; |
935 | 246 | Table *mt; |
936 | 246 | lua_lock(L); |
937 | 246 | api_checknelems(L, 1); |
938 | 246 | obj = index2value(L, objindex); |
939 | 246 | if (ttisnil(s2v(L->top.p - 1))) |
940 | 0 | mt = NULL; |
941 | 246 | else { |
942 | 246 | api_check(L, ttistable(s2v(L->top.p - 1)), "table expected"); |
943 | 246 | mt = hvalue(s2v(L->top.p - 1)); |
944 | 246 | } |
945 | 246 | switch (ttype(obj)) { |
946 | 0 | case LUA_TTABLE: { |
947 | 0 | hvalue(obj)->metatable = mt; |
948 | 0 | if (mt) { |
949 | 0 | luaC_objbarrier(L, gcvalue(obj), mt); |
950 | 0 | luaC_checkfinalizer(L, gcvalue(obj), mt); |
951 | 0 | } |
952 | 0 | break; |
953 | 0 | } |
954 | 246 | case LUA_TUSERDATA: { |
955 | 246 | uvalue(obj)->metatable = mt; |
956 | 246 | if (mt) { |
957 | 246 | luaC_objbarrier(L, uvalue(obj), mt); |
958 | 246 | luaC_checkfinalizer(L, gcvalue(obj), mt); |
959 | 246 | } |
960 | 246 | break; |
961 | 0 | } |
962 | 0 | default: { |
963 | 0 | G(L)->mt[ttype(obj)] = mt; |
964 | 0 | break; |
965 | 0 | } |
966 | 246 | } |
967 | 246 | L->top.p--; |
968 | 246 | lua_unlock(L); |
969 | 246 | return 1; |
970 | 246 | } |
971 | | |
972 | | |
973 | 0 | LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) { |
974 | 0 | TValue *o; |
975 | 0 | int res; |
976 | 0 | lua_lock(L); |
977 | 0 | api_checknelems(L, 1); |
978 | 0 | o = index2value(L, idx); |
979 | 0 | api_check(L, ttisfulluserdata(o), "full userdata expected"); |
980 | 0 | if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue))) |
981 | 0 | res = 0; /* 'n' not in [1, uvalue(o)->nuvalue] */ |
982 | 0 | else { |
983 | 0 | setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top.p - 1)); |
984 | 0 | luaC_barrierback(L, gcvalue(o), s2v(L->top.p - 1)); |
985 | 0 | res = 1; |
986 | 0 | } |
987 | 0 | L->top.p--; |
988 | 0 | lua_unlock(L); |
989 | 0 | return res; |
990 | 0 | } |
991 | | |
992 | | |
993 | | /* |
994 | | ** 'load' and 'call' functions (run Lua code) |
995 | | */ |
996 | | |
997 | | |
998 | | #define checkresults(L,na,nr) \ |
999 | 0 | api_check(L, (nr) == LUA_MULTRET \ |
1000 | 0 | || (L->ci->top.p - L->top.p >= (nr) - (na)), \ |
1001 | 0 | "results from function overflow current stack size") |
1002 | | |
1003 | | |
1004 | | LUA_API void lua_callk (lua_State *L, int nargs, int nresults, |
1005 | 0 | lua_KContext ctx, lua_KFunction k) { |
1006 | 0 | StkId func; |
1007 | 0 | lua_lock(L); |
1008 | 0 | api_check(L, k == NULL || !isLua(L->ci), |
1009 | 0 | "cannot use continuations inside hooks"); |
1010 | 0 | api_checknelems(L, nargs+1); |
1011 | 0 | api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); |
1012 | 0 | checkresults(L, nargs, nresults); |
1013 | 0 | func = L->top.p - (nargs+1); |
1014 | 0 | if (k != NULL && yieldable(L)) { /* need to prepare continuation? */ |
1015 | 0 | L->ci->u.c.k = k; /* save continuation */ |
1016 | 0 | L->ci->u.c.ctx = ctx; /* save context */ |
1017 | 0 | luaD_call(L, func, nresults); /* do the call */ |
1018 | 0 | } |
1019 | 0 | else /* no continuation or no yieldable */ |
1020 | 0 | luaD_callnoyield(L, func, nresults); /* just do the call */ |
1021 | 0 | adjustresults(L, nresults); |
1022 | 0 | lua_unlock(L); |
1023 | 0 | } |
1024 | | |
1025 | | |
1026 | | |
1027 | | /* |
1028 | | ** Execute a protected call. |
1029 | | */ |
1030 | | struct CallS { /* data to 'f_call' */ |
1031 | | StkId func; |
1032 | | int nresults; |
1033 | | }; |
1034 | | |
1035 | | |
1036 | 0 | static void f_call (lua_State *L, void *ud) { |
1037 | 0 | struct CallS *c = cast(struct CallS *, ud); |
1038 | 0 | luaD_callnoyield(L, c->func, c->nresults); |
1039 | 0 | } |
1040 | | |
1041 | | |
1042 | | |
1043 | | LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, |
1044 | 0 | lua_KContext ctx, lua_KFunction k) { |
1045 | 0 | struct CallS c; |
1046 | 0 | int status; |
1047 | 0 | ptrdiff_t func; |
1048 | 0 | lua_lock(L); |
1049 | 0 | api_check(L, k == NULL || !isLua(L->ci), |
1050 | 0 | "cannot use continuations inside hooks"); |
1051 | 0 | api_checknelems(L, nargs+1); |
1052 | 0 | api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); |
1053 | 0 | checkresults(L, nargs, nresults); |
1054 | 0 | if (errfunc == 0) |
1055 | 0 | func = 0; |
1056 | 0 | else { |
1057 | 0 | StkId o = index2stack(L, errfunc); |
1058 | 0 | api_check(L, ttisfunction(s2v(o)), "error handler must be a function"); |
1059 | 0 | func = savestack(L, o); |
1060 | 0 | } |
1061 | 0 | c.func = L->top.p - (nargs+1); /* function to be called */ |
1062 | 0 | if (k == NULL || !yieldable(L)) { /* no continuation or no yieldable? */ |
1063 | 0 | c.nresults = nresults; /* do a 'conventional' protected call */ |
1064 | 0 | status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func); |
1065 | 0 | } |
1066 | 0 | else { /* prepare continuation (call is already protected by 'resume') */ |
1067 | 0 | CallInfo *ci = L->ci; |
1068 | 0 | ci->u.c.k = k; /* save continuation */ |
1069 | 0 | ci->u.c.ctx = ctx; /* save context */ |
1070 | | /* save information for error recovery */ |
1071 | 0 | ci->u2.funcidx = cast_int(savestack(L, c.func)); |
1072 | 0 | ci->u.c.old_errfunc = L->errfunc; |
1073 | 0 | L->errfunc = func; |
1074 | 0 | setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */ |
1075 | 0 | ci->callstatus |= CIST_YPCALL; /* function can do error recovery */ |
1076 | 0 | luaD_call(L, c.func, nresults); /* do the call */ |
1077 | 0 | ci->callstatus &= ~CIST_YPCALL; |
1078 | 0 | L->errfunc = ci->u.c.old_errfunc; |
1079 | 0 | status = LUA_OK; /* if it is here, there were no errors */ |
1080 | 0 | } |
1081 | 0 | adjustresults(L, nresults); |
1082 | 0 | lua_unlock(L); |
1083 | 0 | return status; |
1084 | 0 | } |
1085 | | |
1086 | | |
1087 | | LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, |
1088 | 0 | const char *chunkname, const char *mode) { |
1089 | 0 | ZIO z; |
1090 | 0 | int status; |
1091 | 0 | lua_lock(L); |
1092 | 0 | if (!chunkname) chunkname = "?"; |
1093 | 0 | luaZ_init(L, &z, reader, data); |
1094 | 0 | status = luaD_protectedparser(L, &z, chunkname, mode); |
1095 | 0 | if (status == LUA_OK) { /* no errors? */ |
1096 | 0 | LClosure *f = clLvalue(s2v(L->top.p - 1)); /* get new function */ |
1097 | 0 | if (f->nupvalues >= 1) { /* does it have an upvalue? */ |
1098 | | /* get global table from registry */ |
1099 | 0 | const TValue *gt = getGtable(L); |
1100 | | /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ |
1101 | 0 | setobj(L, f->upvals[0]->v.p, gt); |
1102 | 0 | luaC_barrier(L, f->upvals[0], gt); |
1103 | 0 | } |
1104 | 0 | } |
1105 | 0 | lua_unlock(L); |
1106 | 0 | return status; |
1107 | 0 | } |
1108 | | |
1109 | | |
1110 | 0 | LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) { |
1111 | 0 | int status; |
1112 | 0 | TValue *o; |
1113 | 0 | lua_lock(L); |
1114 | 0 | api_checknelems(L, 1); |
1115 | 0 | o = s2v(L->top.p - 1); |
1116 | 0 | if (isLfunction(o)) |
1117 | 0 | status = luaU_dump(L, getproto(o), writer, data, strip); |
1118 | 0 | else |
1119 | 0 | status = 1; |
1120 | 0 | lua_unlock(L); |
1121 | 0 | return status; |
1122 | 0 | } |
1123 | | |
1124 | | |
1125 | 0 | LUA_API int lua_status (lua_State *L) { |
1126 | 0 | return L->status; |
1127 | 0 | } |
1128 | | |
1129 | | |
1130 | | /* |
1131 | | ** Garbage-collection function |
1132 | | */ |
1133 | 0 | LUA_API int lua_gc (lua_State *L, int what, ...) { |
1134 | 0 | va_list argp; |
1135 | 0 | int res = 0; |
1136 | 0 | global_State *g = G(L); |
1137 | 0 | if (g->gcstp & GCSTPGC) /* internal stop? */ |
1138 | 0 | return -1; /* all options are invalid when stopped */ |
1139 | 0 | lua_lock(L); |
1140 | 0 | va_start(argp, what); |
1141 | 0 | switch (what) { |
1142 | 0 | case LUA_GCSTOP: { |
1143 | 0 | g->gcstp = GCSTPUSR; /* stopped by the user */ |
1144 | 0 | break; |
1145 | 0 | } |
1146 | 0 | case LUA_GCRESTART: { |
1147 | 0 | luaE_setdebt(g, 0); |
1148 | 0 | g->gcstp = 0; /* (GCSTPGC must be already zero here) */ |
1149 | 0 | break; |
1150 | 0 | } |
1151 | 0 | case LUA_GCCOLLECT: { |
1152 | 0 | luaC_fullgc(L, 0); |
1153 | 0 | break; |
1154 | 0 | } |
1155 | 0 | case LUA_GCCOUNT: { |
1156 | | /* GC values are expressed in Kbytes: #bytes/2^10 */ |
1157 | 0 | res = cast_int(gettotalbytes(g) >> 10); |
1158 | 0 | break; |
1159 | 0 | } |
1160 | 0 | case LUA_GCCOUNTB: { |
1161 | 0 | res = cast_int(gettotalbytes(g) & 0x3ff); |
1162 | 0 | break; |
1163 | 0 | } |
1164 | 0 | case LUA_GCSTEP: { |
1165 | 0 | int data = va_arg(argp, int); |
1166 | 0 | l_mem debt = 1; /* =1 to signal that it did an actual step */ |
1167 | 0 | lu_byte oldstp = g->gcstp; |
1168 | 0 | g->gcstp = 0; /* allow GC to run (GCSTPGC must be zero here) */ |
1169 | 0 | if (data == 0) { |
1170 | 0 | luaE_setdebt(g, 0); /* do a basic step */ |
1171 | 0 | luaC_step(L); |
1172 | 0 | } |
1173 | 0 | else { /* add 'data' to total debt */ |
1174 | 0 | debt = cast(l_mem, data) * 1024 + g->GCdebt; |
1175 | 0 | luaE_setdebt(g, debt); |
1176 | 0 | luaC_checkGC(L); |
1177 | 0 | } |
1178 | 0 | g->gcstp = oldstp; /* restore previous state */ |
1179 | 0 | if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */ |
1180 | 0 | res = 1; /* signal it */ |
1181 | 0 | break; |
1182 | 0 | } |
1183 | 0 | case LUA_GCSETPAUSE: { |
1184 | 0 | int data = va_arg(argp, int); |
1185 | 0 | res = getgcparam(g->gcpause); |
1186 | 0 | setgcparam(g->gcpause, data); |
1187 | 0 | break; |
1188 | 0 | } |
1189 | 0 | case LUA_GCSETSTEPMUL: { |
1190 | 0 | int data = va_arg(argp, int); |
1191 | 0 | res = getgcparam(g->gcstepmul); |
1192 | 0 | setgcparam(g->gcstepmul, data); |
1193 | 0 | break; |
1194 | 0 | } |
1195 | 0 | case LUA_GCISRUNNING: { |
1196 | 0 | res = gcrunning(g); |
1197 | 0 | break; |
1198 | 0 | } |
1199 | 0 | case LUA_GCGEN: { |
1200 | 0 | int minormul = va_arg(argp, int); |
1201 | 0 | int majormul = va_arg(argp, int); |
1202 | 0 | res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC; |
1203 | 0 | if (minormul != 0) |
1204 | 0 | g->genminormul = minormul; |
1205 | 0 | if (majormul != 0) |
1206 | 0 | setgcparam(g->genmajormul, majormul); |
1207 | 0 | luaC_changemode(L, KGC_GEN); |
1208 | 0 | break; |
1209 | 0 | } |
1210 | 0 | case LUA_GCINC: { |
1211 | 0 | int pause = va_arg(argp, int); |
1212 | 0 | int stepmul = va_arg(argp, int); |
1213 | 0 | int stepsize = va_arg(argp, int); |
1214 | 0 | res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC; |
1215 | 0 | if (pause != 0) |
1216 | 0 | setgcparam(g->gcpause, pause); |
1217 | 0 | if (stepmul != 0) |
1218 | 0 | setgcparam(g->gcstepmul, stepmul); |
1219 | 0 | if (stepsize != 0) |
1220 | 0 | g->gcstepsize = stepsize; |
1221 | 0 | luaC_changemode(L, KGC_INC); |
1222 | 0 | break; |
1223 | 0 | } |
1224 | 0 | default: res = -1; /* invalid option */ |
1225 | 0 | } |
1226 | 0 | va_end(argp); |
1227 | 0 | lua_unlock(L); |
1228 | 0 | return res; |
1229 | 0 | } |
1230 | | |
1231 | | |
1232 | | |
1233 | | /* |
1234 | | ** miscellaneous functions |
1235 | | */ |
1236 | | |
1237 | | |
1238 | 0 | LUA_API int lua_error (lua_State *L) { |
1239 | 0 | TValue *errobj; |
1240 | 0 | lua_lock(L); |
1241 | 0 | errobj = s2v(L->top.p - 1); |
1242 | 0 | api_checknelems(L, 1); |
1243 | | /* error object is the memory error message? */ |
1244 | 0 | if (ttisshrstring(errobj) && eqshrstr(tsvalue(errobj), G(L)->memerrmsg)) |
1245 | 0 | luaM_error(L); /* raise a memory error */ |
1246 | 0 | else |
1247 | 0 | luaG_errormsg(L); /* raise a regular error */ |
1248 | | /* code unreachable; will unlock when control actually leaves the kernel */ |
1249 | 0 | return 0; /* to avoid warnings */ |
1250 | 0 | } |
1251 | | |
1252 | | |
1253 | 0 | LUA_API int lua_next (lua_State *L, int idx) { |
1254 | 0 | Table *t; |
1255 | 0 | int more; |
1256 | 0 | lua_lock(L); |
1257 | 0 | api_checknelems(L, 1); |
1258 | 0 | t = gettable(L, idx); |
1259 | 0 | more = luaH_next(L, t, L->top.p - 1); |
1260 | 0 | if (more) { |
1261 | 0 | api_incr_top(L); |
1262 | 0 | } |
1263 | 0 | else /* no more elements */ |
1264 | 0 | L->top.p -= 1; /* remove key */ |
1265 | 0 | lua_unlock(L); |
1266 | 0 | return more; |
1267 | 0 | } |
1268 | | |
1269 | | |
1270 | 246 | LUA_API void lua_toclose (lua_State *L, int idx) { |
1271 | 246 | int nresults; |
1272 | 246 | StkId o; |
1273 | 246 | lua_lock(L); |
1274 | 246 | o = index2stack(L, idx); |
1275 | 246 | nresults = L->ci->nresults; |
1276 | 246 | api_check(L, L->tbclist.p < o, "given index below or equal a marked one"); |
1277 | 246 | luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */ |
1278 | 246 | if (!hastocloseCfunc(nresults)) /* function not marked yet? */ |
1279 | 246 | L->ci->nresults = codeNresults(nresults); /* mark it */ |
1280 | 246 | lua_assert(hastocloseCfunc(L->ci->nresults)); |
1281 | 246 | lua_unlock(L); |
1282 | 246 | } |
1283 | | |
1284 | | |
1285 | 0 | LUA_API void lua_concat (lua_State *L, int n) { |
1286 | 0 | lua_lock(L); |
1287 | 0 | api_checknelems(L, n); |
1288 | 0 | if (n > 0) |
1289 | 0 | luaV_concat(L, n); |
1290 | 0 | else { /* nothing to concatenate */ |
1291 | 0 | setsvalue2s(L, L->top.p, luaS_newlstr(L, "", 0)); /* push empty string */ |
1292 | 0 | api_incr_top(L); |
1293 | 0 | } |
1294 | 0 | luaC_checkGC(L); |
1295 | 0 | lua_unlock(L); |
1296 | 0 | } |
1297 | | |
1298 | | |
1299 | 0 | LUA_API void lua_len (lua_State *L, int idx) { |
1300 | 0 | TValue *t; |
1301 | 0 | lua_lock(L); |
1302 | 0 | t = index2value(L, idx); |
1303 | 0 | luaV_objlen(L, L->top.p, t); |
1304 | 0 | api_incr_top(L); |
1305 | 0 | lua_unlock(L); |
1306 | 0 | } |
1307 | | |
1308 | | |
1309 | 738 | LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) { |
1310 | 738 | lua_Alloc f; |
1311 | 738 | lua_lock(L); |
1312 | 738 | if (ud) *ud = G(L)->ud; |
1313 | 738 | f = G(L)->frealloc; |
1314 | 738 | lua_unlock(L); |
1315 | 738 | return f; |
1316 | 738 | } |
1317 | | |
1318 | | |
1319 | 0 | LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) { |
1320 | 0 | lua_lock(L); |
1321 | 0 | G(L)->ud = ud; |
1322 | 0 | G(L)->frealloc = f; |
1323 | 0 | lua_unlock(L); |
1324 | 0 | } |
1325 | | |
1326 | | |
1327 | 496 | void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud) { |
1328 | 496 | lua_lock(L); |
1329 | 496 | G(L)->ud_warn = ud; |
1330 | 496 | G(L)->warnf = f; |
1331 | 496 | lua_unlock(L); |
1332 | 496 | } |
1333 | | |
1334 | | |
1335 | 0 | void lua_warning (lua_State *L, const char *msg, int tocont) { |
1336 | 0 | lua_lock(L); |
1337 | 0 | luaE_warning(L, msg, tocont); |
1338 | 0 | lua_unlock(L); |
1339 | 0 | } |
1340 | | |
1341 | | |
1342 | | |
1343 | 246 | LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) { |
1344 | 246 | Udata *u; |
1345 | 246 | lua_lock(L); |
1346 | 246 | api_check(L, 0 <= nuvalue && nuvalue < USHRT_MAX, "invalid value"); |
1347 | 246 | u = luaS_newudata(L, size, nuvalue); |
1348 | 246 | setuvalue(L, s2v(L->top.p), u); |
1349 | 246 | api_incr_top(L); |
1350 | 246 | luaC_checkGC(L); |
1351 | 246 | lua_unlock(L); |
1352 | 246 | return getudatamem(u); |
1353 | 246 | } |
1354 | | |
1355 | | |
1356 | | |
1357 | | static const char *aux_upvalue (TValue *fi, int n, TValue **val, |
1358 | 0 | GCObject **owner) { |
1359 | 0 | switch (ttypetag(fi)) { |
1360 | 0 | case LUA_VCCL: { /* C closure */ |
1361 | 0 | CClosure *f = clCvalue(fi); |
1362 | 0 | if (!(cast_uint(n) - 1u < cast_uint(f->nupvalues))) |
1363 | 0 | return NULL; /* 'n' not in [1, f->nupvalues] */ |
1364 | 0 | *val = &f->upvalue[n-1]; |
1365 | 0 | if (owner) *owner = obj2gco(f); |
1366 | 0 | return ""; |
1367 | 0 | } |
1368 | 0 | case LUA_VLCL: { /* Lua closure */ |
1369 | 0 | LClosure *f = clLvalue(fi); |
1370 | 0 | TString *name; |
1371 | 0 | Proto *p = f->p; |
1372 | 0 | if (!(cast_uint(n) - 1u < cast_uint(p->sizeupvalues))) |
1373 | 0 | return NULL; /* 'n' not in [1, p->sizeupvalues] */ |
1374 | 0 | *val = f->upvals[n-1]->v.p; |
1375 | 0 | if (owner) *owner = obj2gco(f->upvals[n - 1]); |
1376 | 0 | name = p->upvalues[n-1].name; |
1377 | 0 | return (name == NULL) ? "(no name)" : getstr(name); |
1378 | 0 | } |
1379 | 0 | default: return NULL; /* not a closure */ |
1380 | 0 | } |
1381 | 0 | } |
1382 | | |
1383 | | |
1384 | 0 | LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { |
1385 | 0 | const char *name; |
1386 | 0 | TValue *val = NULL; /* to avoid warnings */ |
1387 | 0 | lua_lock(L); |
1388 | 0 | name = aux_upvalue(index2value(L, funcindex), n, &val, NULL); |
1389 | 0 | if (name) { |
1390 | 0 | setobj2s(L, L->top.p, val); |
1391 | 0 | api_incr_top(L); |
1392 | 0 | } |
1393 | 0 | lua_unlock(L); |
1394 | 0 | return name; |
1395 | 0 | } |
1396 | | |
1397 | | |
1398 | 0 | LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { |
1399 | 0 | const char *name; |
1400 | 0 | TValue *val = NULL; /* to avoid warnings */ |
1401 | 0 | GCObject *owner = NULL; /* to avoid warnings */ |
1402 | 0 | TValue *fi; |
1403 | 0 | lua_lock(L); |
1404 | 0 | fi = index2value(L, funcindex); |
1405 | 0 | api_checknelems(L, 1); |
1406 | 0 | name = aux_upvalue(fi, n, &val, &owner); |
1407 | 0 | if (name) { |
1408 | 0 | L->top.p--; |
1409 | 0 | setobj(L, val, s2v(L->top.p)); |
1410 | 0 | luaC_barrier(L, owner, val); |
1411 | 0 | } |
1412 | 0 | lua_unlock(L); |
1413 | 0 | return name; |
1414 | 0 | } |
1415 | | |
1416 | | |
1417 | 0 | static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) { |
1418 | 0 | static const UpVal *const nullup = NULL; |
1419 | 0 | LClosure *f; |
1420 | 0 | TValue *fi = index2value(L, fidx); |
1421 | 0 | api_check(L, ttisLclosure(fi), "Lua function expected"); |
1422 | 0 | f = clLvalue(fi); |
1423 | 0 | if (pf) *pf = f; |
1424 | 0 | if (1 <= n && n <= f->p->sizeupvalues) |
1425 | 0 | return &f->upvals[n - 1]; /* get its upvalue pointer */ |
1426 | 0 | else |
1427 | 0 | return (UpVal**)&nullup; |
1428 | 0 | } |
1429 | | |
1430 | | |
1431 | 0 | LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) { |
1432 | 0 | TValue *fi = index2value(L, fidx); |
1433 | 0 | switch (ttypetag(fi)) { |
1434 | 0 | case LUA_VLCL: { /* lua closure */ |
1435 | 0 | return *getupvalref(L, fidx, n, NULL); |
1436 | 0 | } |
1437 | 0 | case LUA_VCCL: { /* C closure */ |
1438 | 0 | CClosure *f = clCvalue(fi); |
1439 | 0 | if (1 <= n && n <= f->nupvalues) |
1440 | 0 | return &f->upvalue[n - 1]; |
1441 | | /* else */ |
1442 | 0 | } /* FALLTHROUGH */ |
1443 | 0 | case LUA_VLCF: |
1444 | 0 | return NULL; /* light C functions have no upvalues */ |
1445 | 0 | default: { |
1446 | 0 | api_check(L, 0, "function expected"); |
1447 | 0 | return NULL; |
1448 | 0 | } |
1449 | 0 | } |
1450 | 0 | } |
1451 | | |
1452 | | |
1453 | | LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1, |
1454 | 0 | int fidx2, int n2) { |
1455 | 0 | LClosure *f1; |
1456 | 0 | UpVal **up1 = getupvalref(L, fidx1, n1, &f1); |
1457 | 0 | UpVal **up2 = getupvalref(L, fidx2, n2, NULL); |
1458 | 0 | api_check(L, *up1 != NULL && *up2 != NULL, "invalid upvalue index"); |
1459 | 0 | *up1 = *up2; |
1460 | 0 | luaC_objbarrier(L, f1, *up1); |
1461 | 0 | } |
1462 | | |
1463 | | |