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