/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 | 5.76M | #define isvalid(L, o) (!ttisnil(o) || o != &G(L)->nilvalue) |
47 | | |
48 | | |
49 | | /* test for pseudo index */ |
50 | 3.49M | #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX) |
51 | | |
52 | | /* test for upvalue */ |
53 | 18.4k | #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 | 10.1M | static TValue *index2value (lua_State *L, int idx) { |
61 | 10.1M | CallInfo *ci = L->ci; |
62 | 10.1M | if (idx > 0) { |
63 | 6.67M | StkId o = ci->func.p + idx; |
64 | 6.67M | api_check(L, idx <= ci->top.p - (ci->func.p + 1), "unacceptable index"); |
65 | 6.67M | if (o >= L->top.p) return &G(L)->nilvalue; |
66 | 6.01M | else return s2v(o); |
67 | 6.67M | } |
68 | 3.48M | else if (!ispseudo(idx)) { /* negative index */ |
69 | 3.31M | api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1), |
70 | 3.31M | "invalid index"); |
71 | 3.31M | return s2v(L->top.p + idx); |
72 | 3.31M | } |
73 | 173k | else if (idx == LUA_REGISTRYINDEX) |
74 | 170k | return &G(L)->l_registry; |
75 | 3.42k | else { /* upvalues */ |
76 | 3.42k | idx = LUA_REGISTRYINDEX - idx; |
77 | 3.42k | api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large"); |
78 | 3.42k | if (ttisCclosure(s2v(ci->func.p))) { /* C closure? */ |
79 | 3.42k | CClosure *func = clCvalue(s2v(ci->func.p)); |
80 | 3.42k | return (idx <= func->nupvalues) ? &func->upvalue[idx-1] |
81 | 3.42k | : &G(L)->nilvalue; |
82 | 3.42k | } |
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 | 3.42k | } |
88 | 10.1M | } |
89 | | |
90 | | |
91 | | |
92 | | /* |
93 | | ** Convert a valid actual index (not a pseudo-index) to its address. |
94 | | */ |
95 | 485k | l_sinline StkId index2stack (lua_State *L, int idx) { |
96 | 485k | CallInfo *ci = L->ci; |
97 | 485k | if (idx > 0) { |
98 | 7.99k | StkId o = ci->func.p + idx; |
99 | 7.99k | api_check(L, o < L->top.p, "invalid index"); |
100 | 7.99k | return o; |
101 | 7.99k | } |
102 | 477k | else { /* non-positive index */ |
103 | 477k | api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1), |
104 | 477k | "invalid index"); |
105 | 477k | api_check(L, !ispseudo(idx), "invalid index"); |
106 | 477k | return L->top.p + idx; |
107 | 477k | } |
108 | 485k | } |
109 | | |
110 | | |
111 | 73.7k | LUA_API int lua_checkstack (lua_State *L, int n) { |
112 | 73.7k | int res; |
113 | 73.7k | CallInfo *ci; |
114 | 73.7k | lua_lock(L); |
115 | 73.7k | ci = L->ci; |
116 | 73.7k | api_check(L, n >= 0, "negative 'n'"); |
117 | 73.7k | if (L->stack_last.p - L->top.p > n) /* stack large enough? */ |
118 | 73.0k | res = 1; /* yes; check is OK */ |
119 | 744 | else /* need to grow stack */ |
120 | 744 | res = luaD_growstack(L, n, 0); |
121 | 73.7k | if (res && ci->top.p < L->top.p + n) |
122 | 7.50k | ci->top.p = L->top.p + n; /* adjust frame top */ |
123 | 73.7k | lua_unlock(L); |
124 | 73.7k | return res; |
125 | 73.7k | } |
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 | 15.8k | LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) { |
145 | 15.8k | lua_CFunction old; |
146 | 15.8k | lua_lock(L); |
147 | 15.8k | old = G(L)->panic; |
148 | 15.8k | G(L)->panic = panicf; |
149 | 15.8k | lua_unlock(L); |
150 | 15.8k | return old; |
151 | 15.8k | } |
152 | | |
153 | | |
154 | 28.8k | LUA_API lua_Number lua_version (lua_State *L) { |
155 | 28.8k | UNUSED(L); |
156 | 28.8k | return LUA_VERSION_NUM; |
157 | 28.8k | } |
158 | | |
159 | | |
160 | | |
161 | | /* |
162 | | ** basic stack manipulation |
163 | | */ |
164 | | |
165 | | |
166 | | /* |
167 | | ** convert an acceptable stack index into an absolute index |
168 | | */ |
169 | 34.9k | LUA_API int lua_absindex (lua_State *L, int idx) { |
170 | 34.9k | return (idx > 0 || ispseudo(idx)) |
171 | 34.9k | ? idx |
172 | 34.9k | : cast_int(L->top.p - L->ci->func.p) + idx; |
173 | 34.9k | } |
174 | | |
175 | | |
176 | 41.1k | LUA_API int lua_gettop (lua_State *L) { |
177 | 41.1k | return cast_int(L->top.p - (L->ci->func.p + 1)); |
178 | 41.1k | } |
179 | | |
180 | | |
181 | 1.03M | LUA_API void lua_settop (lua_State *L, int idx) { |
182 | 1.03M | CallInfo *ci; |
183 | 1.03M | StkId func, newtop; |
184 | 1.03M | ptrdiff_t diff; /* difference for new top */ |
185 | 1.03M | lua_lock(L); |
186 | 1.03M | ci = L->ci; |
187 | 1.03M | func = ci->func.p; |
188 | 1.03M | if (idx >= 0) { |
189 | 37.5k | api_check(L, idx <= ci->top.p - (func + 1), "new top too large"); |
190 | 37.5k | diff = ((func + 1) + idx) - L->top.p; |
191 | 75.1k | for (; diff > 0; diff--) |
192 | 37.6k | setnilvalue(s2v(L->top.p++)); /* clear new slots */ |
193 | 37.5k | } |
194 | 1.00M | else { |
195 | 1.00M | api_check(L, -(idx+1) <= (L->top.p - (func + 1)), "invalid new top"); |
196 | 1.00M | diff = idx + 1; /* will "subtract" index (as it is negative) */ |
197 | 1.00M | } |
198 | 1.03M | api_check(L, L->tbclist.p < L->top.p, "previous pop of an unclosed slot"); |
199 | 1.03M | newtop = L->top.p + diff; |
200 | 1.03M | 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.03M | L->top.p = newtop; /* correct top only after closing any upvalue */ |
205 | 1.03M | lua_unlock(L); |
206 | 1.03M | } |
207 | | |
208 | | |
209 | 50.4k | LUA_API void lua_closeslot (lua_State *L, int idx) { |
210 | 50.4k | StkId level; |
211 | 50.4k | lua_lock(L); |
212 | 50.4k | level = index2stack(L, idx); |
213 | 50.4k | api_check(L, hastocloseCfunc(L->ci->nresults) && L->tbclist.p == level, |
214 | 50.4k | "no variable to close at given level"); |
215 | 50.4k | level = luaF_close(L, level, CLOSEKTOP, 0); |
216 | 50.4k | setnilvalue(s2v(level)); |
217 | 50.4k | lua_unlock(L); |
218 | 50.4k | } |
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 | 1.15M | l_sinline void reverse (lua_State *L, StkId from, StkId to) { |
228 | 1.43M | for (; from < to; from++, to--) { |
229 | 283k | TValue temp; |
230 | 283k | setobj(L, &temp, s2v(from)); |
231 | 283k | setobjs2s(L, from, to); |
232 | 283k | setobj2s(L, to, &temp); |
233 | 283k | } |
234 | 1.15M | } |
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 | 384k | LUA_API void lua_rotate (lua_State *L, int idx, int n) { |
242 | 384k | StkId p, t, m; |
243 | 384k | lua_lock(L); |
244 | 384k | t = L->top.p - 1; /* end of stack segment being rotated */ |
245 | 384k | p = index2stack(L, idx); /* start of segment */ |
246 | 384k | api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'"); |
247 | 384k | m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */ |
248 | 384k | reverse(L, p, m); /* reverse the prefix with length 'n' */ |
249 | 384k | reverse(L, m + 1, t); /* reverse the suffix */ |
250 | 384k | reverse(L, p, t); /* reverse the entire segment */ |
251 | 384k | lua_unlock(L); |
252 | 384k | } |
253 | | |
254 | | |
255 | 18.4k | LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { |
256 | 18.4k | TValue *fr, *to; |
257 | 18.4k | lua_lock(L); |
258 | 18.4k | fr = index2value(L, fromidx); |
259 | 18.4k | to = index2value(L, toidx); |
260 | 18.4k | api_check(L, isvalid(L, to), "invalid index"); |
261 | 18.4k | setobj(L, to, fr); |
262 | 18.4k | 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 | 18.4k | lua_unlock(L); |
267 | 18.4k | } |
268 | | |
269 | | |
270 | 1.16M | LUA_API void lua_pushvalue (lua_State *L, int idx) { |
271 | 1.16M | lua_lock(L); |
272 | 1.16M | setobj2s(L, L->top.p, index2value(L, idx)); |
273 | 1.16M | api_incr_top(L); |
274 | 1.16M | lua_unlock(L); |
275 | 1.16M | } |
276 | | |
277 | | |
278 | | |
279 | | /* |
280 | | ** access functions (stack -> C) |
281 | | */ |
282 | | |
283 | | |
284 | 3.74M | LUA_API int lua_type (lua_State *L, int idx) { |
285 | 3.74M | const TValue *o = index2value(L, idx); |
286 | 3.74M | return (isvalid(L, o) ? ttype(o) : LUA_TNONE); |
287 | 3.74M | } |
288 | | |
289 | | |
290 | 21.4k | LUA_API const char *lua_typename (lua_State *L, int t) { |
291 | 21.4k | UNUSED(L); |
292 | 21.4k | api_check(L, LUA_TNONE <= t && t < LUA_NUMTYPES, "invalid type"); |
293 | 21.4k | return ttypename(t); |
294 | 21.4k | } |
295 | | |
296 | | |
297 | 8 | LUA_API int lua_iscfunction (lua_State *L, int idx) { |
298 | 8 | const TValue *o = index2value(L, idx); |
299 | 8 | return (ttislcf(o) || (ttisCclosure(o))); |
300 | 8 | } |
301 | | |
302 | | |
303 | 149 | LUA_API int lua_isinteger (lua_State *L, int idx) { |
304 | 149 | const TValue *o = index2value(L, idx); |
305 | 149 | return ttisinteger(o); |
306 | 149 | } |
307 | | |
308 | | |
309 | 327 | LUA_API int lua_isnumber (lua_State *L, int idx) { |
310 | 327 | lua_Number n; |
311 | 327 | const TValue *o = index2value(L, idx); |
312 | 327 | return tonumber(o, &n); |
313 | 327 | } |
314 | | |
315 | | |
316 | 404 | LUA_API int lua_isstring (lua_State *L, int idx) { |
317 | 404 | const TValue *o = index2value(L, idx); |
318 | 404 | return (ttisstring(o) || cvt2str(o)); |
319 | 404 | } |
320 | | |
321 | | |
322 | 21 | LUA_API int lua_isuserdata (lua_State *L, int idx) { |
323 | 21 | const TValue *o = index2value(L, idx); |
324 | 21 | return (ttisfulluserdata(o) || ttislightuserdata(o)); |
325 | 21 | } |
326 | | |
327 | | |
328 | 672k | LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { |
329 | 672k | const TValue *o1 = index2value(L, index1); |
330 | 672k | const TValue *o2 = index2value(L, index2); |
331 | 672k | return (isvalid(L, o1) && isvalid(L, o2)) ? luaV_rawequalobj(o1, o2) : 0; |
332 | 672k | } |
333 | | |
334 | | |
335 | 1.02M | LUA_API void lua_arith (lua_State *L, int op) { |
336 | 1.02M | lua_lock(L); |
337 | 1.02M | if (op != LUA_OPUNM && op != LUA_OPBNOT) |
338 | 1.02M | api_checknelems(L, 2); /* all other operations expect two operands */ |
339 | 305 | else { /* for unary operations, add fake 2nd operand */ |
340 | 305 | api_checknelems(L, 1); |
341 | 305 | setobjs2s(L, L->top.p, L->top.p - 1); |
342 | 305 | api_incr_top(L); |
343 | 305 | } |
344 | | /* first operand at top - 2, second at top - 1; result go to top - 2 */ |
345 | 1.02M | luaO_arith(L, op, s2v(L->top.p - 2), s2v(L->top.p - 1), L->top.p - 2); |
346 | 1.02M | L->top.p--; /* remove second operand */ |
347 | 1.02M | lua_unlock(L); |
348 | 1.02M | } |
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 | 1.03M | LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) { |
372 | 1.03M | size_t sz = luaO_str2num(s, s2v(L->top.p)); |
373 | 1.03M | if (sz != 0) |
374 | 1.03M | api_incr_top(L); |
375 | 1.03M | return sz; |
376 | 1.03M | } |
377 | | |
378 | | |
379 | 217 | LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) { |
380 | 217 | lua_Number n = 0; |
381 | 217 | const TValue *o = index2value(L, idx); |
382 | 217 | int isnum = tonumber(o, &n); |
383 | 217 | if (pisnum) |
384 | 85 | *pisnum = isnum; |
385 | 217 | return n; |
386 | 217 | } |
387 | | |
388 | | |
389 | 36.1k | LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) { |
390 | 36.1k | lua_Integer res = 0; |
391 | 36.1k | const TValue *o = index2value(L, idx); |
392 | 36.1k | int isnum = tointeger(o, &res); |
393 | 36.1k | if (pisnum) |
394 | 36.1k | *pisnum = isnum; |
395 | 36.1k | return res; |
396 | 36.1k | } |
397 | | |
398 | | |
399 | 46.1k | LUA_API int lua_toboolean (lua_State *L, int idx) { |
400 | 46.1k | const TValue *o = index2value(L, idx); |
401 | 46.1k | return !l_isfalse(o); |
402 | 46.1k | } |
403 | | |
404 | | |
405 | 1.44M | LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { |
406 | 1.44M | TValue *o; |
407 | 1.44M | lua_lock(L); |
408 | 1.44M | o = index2value(L, idx); |
409 | 1.44M | if (!ttisstring(o)) { |
410 | 39.3k | if (!cvt2str(o)) { /* not convertible? */ |
411 | 16.0k | if (len != NULL) *len = 0; |
412 | 16.0k | lua_unlock(L); |
413 | 16.0k | return NULL; |
414 | 16.0k | } |
415 | 23.3k | luaO_tostring(L, o); |
416 | 23.3k | luaC_checkGC(L); |
417 | 23.3k | o = index2value(L, idx); /* previous call may reallocate the stack */ |
418 | 23.3k | } |
419 | 1.42M | if (len != NULL) |
420 | 1.33M | *len = tsslen(tsvalue(o)); |
421 | 1.42M | lua_unlock(L); |
422 | 1.42M | return getstr(tsvalue(o)); |
423 | 1.44M | } |
424 | | |
425 | | |
426 | 250k | LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) { |
427 | 250k | const TValue *o = index2value(L, idx); |
428 | 250k | switch (ttypetag(o)) { |
429 | 124k | case LUA_VSHRSTR: return tsvalue(o)->shrlen; |
430 | 124k | case LUA_VLNGSTR: return tsvalue(o)->u.lnglen; |
431 | 0 | case LUA_VUSERDATA: return uvalue(o)->len; |
432 | 984 | case LUA_VTABLE: return luaH_getn(hvalue(o)); |
433 | 0 | default: return 0; |
434 | 250k | } |
435 | 250k | } |
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 | 415k | l_sinline void *touserdata (const TValue *o) { |
448 | 415k | switch (ttype(o)) { |
449 | 415k | case LUA_TUSERDATA: return getudatamem(uvalue(o)); |
450 | 0 | case LUA_TLIGHTUSERDATA: return pvalue(o); |
451 | 12 | default: return NULL; |
452 | 415k | } |
453 | 415k | } |
454 | | |
455 | | |
456 | 415k | LUA_API void *lua_touserdata (lua_State *L, int idx) { |
457 | 415k | const TValue *o = index2value(L, idx); |
458 | 415k | return touserdata(o); |
459 | 415k | } |
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 | 7.92k | LUA_API const void *lua_topointer (lua_State *L, int idx) { |
476 | 7.92k | const TValue *o = index2value(L, idx); |
477 | 7.92k | switch (ttypetag(o)) { |
478 | 4 | case LUA_VLCF: return cast_voidp(cast_sizet(fvalue(o))); |
479 | 0 | case LUA_VUSERDATA: case LUA_VLIGHTUSERDATA: |
480 | 0 | return touserdata(o); |
481 | 7.92k | default: { |
482 | 7.92k | if (iscollectable(o)) |
483 | 7.92k | return gcvalue(o); |
484 | 1 | else |
485 | 1 | return NULL; |
486 | 7.92k | } |
487 | 7.92k | } |
488 | 7.92k | } |
489 | | |
490 | | |
491 | | |
492 | | /* |
493 | | ** push functions (C -> stack) |
494 | | */ |
495 | | |
496 | | |
497 | 268k | LUA_API void lua_pushnil (lua_State *L) { |
498 | 268k | lua_lock(L); |
499 | 268k | setnilvalue(s2v(L->top.p)); |
500 | 268k | api_incr_top(L); |
501 | 268k | lua_unlock(L); |
502 | 268k | } |
503 | | |
504 | | |
505 | 18.1k | LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { |
506 | 18.1k | lua_lock(L); |
507 | 18.1k | setfltvalue(s2v(L->top.p), n); |
508 | 18.1k | api_incr_top(L); |
509 | 18.1k | lua_unlock(L); |
510 | 18.1k | } |
511 | | |
512 | | |
513 | 430M | LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { |
514 | 430M | lua_lock(L); |
515 | 430M | setivalue(s2v(L->top.p), n); |
516 | 430M | api_incr_top(L); |
517 | 430M | lua_unlock(L); |
518 | 430M | } |
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 | 70.0k | LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { |
527 | 70.0k | TString *ts; |
528 | 70.0k | lua_lock(L); |
529 | 70.0k | ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len); |
530 | 70.0k | setsvalue2s(L, L->top.p, ts); |
531 | 70.0k | api_incr_top(L); |
532 | 70.0k | luaC_checkGC(L); |
533 | 70.0k | lua_unlock(L); |
534 | 70.0k | return getstr(ts); |
535 | 70.0k | } |
536 | | |
537 | | |
538 | 88.6k | LUA_API const char *lua_pushstring (lua_State *L, const char *s) { |
539 | 88.6k | lua_lock(L); |
540 | 88.6k | if (s == NULL) |
541 | 88.6k | setnilvalue(s2v(L->top.p)); |
542 | 88.6k | else { |
543 | 88.6k | TString *ts; |
544 | 88.6k | ts = luaS_new(L, s); |
545 | 88.6k | setsvalue2s(L, L->top.p, ts); |
546 | 88.6k | s = getstr(ts); /* internal copy's address */ |
547 | 88.6k | } |
548 | 88.6k | api_incr_top(L); |
549 | 88.6k | luaC_checkGC(L); |
550 | 88.6k | lua_unlock(L); |
551 | 88.6k | return s; |
552 | 88.6k | } |
553 | | |
554 | | |
555 | | LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt, |
556 | 9.56k | va_list argp) { |
557 | 9.56k | const char *ret; |
558 | 9.56k | lua_lock(L); |
559 | 9.56k | ret = luaO_pushvfstring(L, fmt, argp); |
560 | 9.56k | luaC_checkGC(L); |
561 | 9.56k | lua_unlock(L); |
562 | 9.56k | return ret; |
563 | 9.56k | } |
564 | | |
565 | | |
566 | 35.3k | LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { |
567 | 35.3k | const char *ret; |
568 | 35.3k | va_list argp; |
569 | 35.3k | lua_lock(L); |
570 | 35.3k | va_start(argp, fmt); |
571 | 35.3k | ret = luaO_pushvfstring(L, fmt, argp); |
572 | 35.3k | va_end(argp); |
573 | 35.3k | luaC_checkGC(L); |
574 | 35.3k | lua_unlock(L); |
575 | 35.3k | return ret; |
576 | 35.3k | } |
577 | | |
578 | | |
579 | 512k | LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { |
580 | 512k | lua_lock(L); |
581 | 512k | if (n == 0) { |
582 | 489k | setfvalue(s2v(L->top.p), fn); |
583 | 489k | api_incr_top(L); |
584 | 489k | } |
585 | 23.1k | else { |
586 | 23.1k | CClosure *cl; |
587 | 23.1k | api_checknelems(L, n); |
588 | 23.1k | api_check(L, n <= MAXUPVAL, "upvalue index too large"); |
589 | 23.1k | cl = luaF_newCclosure(L, n); |
590 | 23.1k | cl->f = fn; |
591 | 23.1k | L->top.p -= n; |
592 | 47.7k | while (n--) { |
593 | 24.6k | setobj2n(L, &cl->upvalue[n], s2v(L->top.p + n)); |
594 | | /* does not need barrier because closure is white */ |
595 | 24.6k | lua_assert(iswhite(cl)); |
596 | 24.6k | } |
597 | 23.1k | setclCvalue(L, s2v(L->top.p), cl); |
598 | 23.1k | api_incr_top(L); |
599 | 23.1k | luaC_checkGC(L); |
600 | 23.1k | } |
601 | 512k | lua_unlock(L); |
602 | 512k | } |
603 | | |
604 | | |
605 | 65.4k | LUA_API void lua_pushboolean (lua_State *L, int b) { |
606 | 65.4k | lua_lock(L); |
607 | 65.4k | if (b) |
608 | 65.4k | setbtvalue(s2v(L->top.p)); |
609 | 58.6k | else |
610 | 65.4k | setbfvalue(s2v(L->top.p)); |
611 | 65.4k | api_incr_top(L); |
612 | 65.4k | lua_unlock(L); |
613 | 65.4k | } |
614 | | |
615 | | |
616 | 63.2k | LUA_API void lua_pushlightuserdata (lua_State *L, void *p) { |
617 | 63.2k | lua_lock(L); |
618 | 63.2k | setpvalue(s2v(L->top.p), p); |
619 | 63.2k | api_incr_top(L); |
620 | 63.2k | lua_unlock(L); |
621 | 63.2k | } |
622 | | |
623 | | |
624 | 2 | LUA_API int lua_pushthread (lua_State *L) { |
625 | 2 | lua_lock(L); |
626 | 2 | setthvalue(L, s2v(L->top.p), L); |
627 | 2 | api_incr_top(L); |
628 | 2 | lua_unlock(L); |
629 | 2 | return (G(L)->mainthread == L); |
630 | 2 | } |
631 | | |
632 | | |
633 | | |
634 | | /* |
635 | | ** get functions (Lua -> stack) |
636 | | */ |
637 | | |
638 | | |
639 | 175k | l_sinline int auxgetstr (lua_State *L, const TValue *t, const char *k) { |
640 | 175k | const TValue *slot; |
641 | 175k | TString *str = luaS_new(L, k); |
642 | 175k | if (luaV_fastget(L, t, str, slot, luaH_getstr)) { |
643 | 129k | setobj2s(L, L->top.p, slot); |
644 | 129k | api_incr_top(L); |
645 | 129k | } |
646 | 46.5k | else { |
647 | 46.5k | setsvalue2s(L, L->top.p, str); |
648 | 46.5k | api_incr_top(L); |
649 | 46.5k | luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot); |
650 | 46.5k | } |
651 | 175k | lua_unlock(L); |
652 | 175k | return ttype(s2v(L->top.p - 1)); |
653 | 175k | } |
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 | 60.9k | (&hvalue(&G(L)->l_registry)->array[LUA_RIDX_GLOBALS - 1]) |
664 | | |
665 | | |
666 | 2 | LUA_API int lua_getglobal (lua_State *L, const char *name) { |
667 | 2 | const TValue *G; |
668 | 2 | lua_lock(L); |
669 | 2 | G = getGtable(L); |
670 | 2 | return auxgetstr(L, G, name); |
671 | 2 | } |
672 | | |
673 | | |
674 | 25 | LUA_API int lua_gettable (lua_State *L, int idx) { |
675 | 25 | const TValue *slot; |
676 | 25 | TValue *t; |
677 | 25 | lua_lock(L); |
678 | 25 | t = index2value(L, idx); |
679 | 25 | 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 | 25 | else |
683 | 25 | luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot); |
684 | 25 | lua_unlock(L); |
685 | 25 | return ttype(s2v(L->top.p - 1)); |
686 | 25 | } |
687 | | |
688 | | |
689 | 175k | LUA_API int lua_getfield (lua_State *L, int idx, const char *k) { |
690 | 175k | lua_lock(L); |
691 | 175k | return auxgetstr(L, index2value(L, idx), k); |
692 | 175k | } |
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 | 11.0k | l_sinline int finishrawget (lua_State *L, const TValue *val) { |
715 | 11.0k | if (isempty(val)) /* avoid copying empty items to the stack */ |
716 | 11.0k | setnilvalue(s2v(L->top.p)); |
717 | 6.80k | else |
718 | 11.0k | setobj2s(L, L->top.p, val); |
719 | 11.0k | api_incr_top(L); |
720 | 11.0k | lua_unlock(L); |
721 | 11.0k | return ttype(s2v(L->top.p - 1)); |
722 | 11.0k | } |
723 | | |
724 | | |
725 | 710k | static Table *gettable (lua_State *L, int idx) { |
726 | 710k | TValue *t = index2value(L, idx); |
727 | 710k | api_check(L, ttistable(t), "table expected"); |
728 | 710k | return hvalue(t); |
729 | 710k | } |
730 | | |
731 | | |
732 | 4.16k | LUA_API int lua_rawget (lua_State *L, int idx) { |
733 | 4.16k | Table *t; |
734 | 4.16k | const TValue *val; |
735 | 4.16k | lua_lock(L); |
736 | 4.16k | api_checknelems(L, 1); |
737 | 4.16k | t = gettable(L, idx); |
738 | 4.16k | val = luaH_get(t, s2v(L->top.p - 1)); |
739 | 4.16k | L->top.p--; /* remove key */ |
740 | 4.16k | return finishrawget(L, val); |
741 | 4.16k | } |
742 | | |
743 | | |
744 | 6.93k | LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { |
745 | 6.93k | Table *t; |
746 | 6.93k | lua_lock(L); |
747 | 6.93k | t = gettable(L, idx); |
748 | 6.93k | return finishrawget(L, luaH_getint(t, n)); |
749 | 6.93k | } |
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 | 56.8k | LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { |
763 | 56.8k | Table *t; |
764 | 56.8k | lua_lock(L); |
765 | 56.8k | t = luaH_new(L); |
766 | 56.8k | sethvalue2s(L, L->top.p, t); |
767 | 56.8k | api_incr_top(L); |
768 | 56.8k | if (narray > 0 || nrec > 0) |
769 | 46.3k | luaH_resize(L, t, narray, nrec); |
770 | 56.8k | luaC_checkGC(L); |
771 | 56.8k | lua_unlock(L); |
772 | 56.8k | } |
773 | | |
774 | | |
775 | 56.6k | LUA_API int lua_getmetatable (lua_State *L, int objindex) { |
776 | 56.6k | const TValue *obj; |
777 | 56.6k | Table *mt; |
778 | 56.6k | int res = 0; |
779 | 56.6k | lua_lock(L); |
780 | 56.6k | obj = index2value(L, objindex); |
781 | 56.6k | switch (ttype(obj)) { |
782 | 15.8k | case LUA_TTABLE: |
783 | 15.8k | mt = hvalue(obj)->metatable; |
784 | 15.8k | break; |
785 | 28.8k | case LUA_TUSERDATA: |
786 | 28.8k | mt = uvalue(obj)->metatable; |
787 | 28.8k | break; |
788 | 11.9k | default: |
789 | 11.9k | mt = G(L)->mt[ttype(obj)]; |
790 | 11.9k | break; |
791 | 56.6k | } |
792 | 56.6k | if (mt != NULL) { |
793 | 32.9k | sethvalue2s(L, L->top.p, mt); |
794 | 32.9k | api_incr_top(L); |
795 | 32.9k | res = 1; |
796 | 32.9k | } |
797 | 56.6k | lua_unlock(L); |
798 | 56.6k | return res; |
799 | 56.6k | } |
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 | 662k | static void auxsetstr (lua_State *L, const TValue *t, const char *k) { |
830 | 662k | const TValue *slot; |
831 | 662k | TString *str = luaS_new(L, k); |
832 | 662k | api_checknelems(L, 1); |
833 | 662k | if (luaV_fastget(L, t, str, slot, luaH_getstr)) { |
834 | 54.4k | luaV_finishfastset(L, t, slot, s2v(L->top.p - 1)); |
835 | 54.4k | L->top.p--; /* pop value */ |
836 | 54.4k | } |
837 | 607k | else { |
838 | 607k | setsvalue2s(L, L->top.p, str); /* push 'str' (to make it a TValue) */ |
839 | 607k | api_incr_top(L); |
840 | 607k | luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), slot); |
841 | 607k | L->top.p -= 2; /* pop value and key */ |
842 | 607k | } |
843 | 662k | lua_unlock(L); /* lock done by caller */ |
844 | 662k | } |
845 | | |
846 | | |
847 | 32.0k | LUA_API void lua_setglobal (lua_State *L, const char *name) { |
848 | 32.0k | const TValue *G; |
849 | 32.0k | lua_lock(L); /* unlock done in 'auxsetstr' */ |
850 | 32.0k | G = getGtable(L); |
851 | 32.0k | auxsetstr(L, G, name); |
852 | 32.0k | } |
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 | 630k | LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { |
872 | 630k | lua_lock(L); /* unlock done in 'auxsetstr' */ |
873 | 630k | auxsetstr(L, index2value(L, idx), k); |
874 | 630k | } |
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 | 39 | static void aux_rawset (lua_State *L, int idx, TValue *key, int n) { |
897 | 39 | Table *t; |
898 | 39 | lua_lock(L); |
899 | 39 | api_checknelems(L, n); |
900 | 39 | t = gettable(L, idx); |
901 | 39 | luaH_set(L, t, key, s2v(L->top.p - 1)); |
902 | 39 | invalidateTMcache(t); |
903 | 39 | luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1)); |
904 | 39 | L->top.p -= n; |
905 | 39 | lua_unlock(L); |
906 | 39 | } |
907 | | |
908 | | |
909 | 39 | LUA_API void lua_rawset (lua_State *L, int idx) { |
910 | 39 | aux_rawset(L, idx, s2v(L->top.p - 2), 2); |
911 | 39 | } |
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 | 23.9k | LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { |
922 | 23.9k | Table *t; |
923 | 23.9k | lua_lock(L); |
924 | 23.9k | api_checknelems(L, 1); |
925 | 23.9k | t = gettable(L, idx); |
926 | 23.9k | luaH_setint(L, t, n, s2v(L->top.p - 1)); |
927 | 23.9k | luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1)); |
928 | 23.9k | L->top.p--; |
929 | 23.9k | lua_unlock(L); |
930 | 23.9k | } |
931 | | |
932 | | |
933 | 66.5k | LUA_API int lua_setmetatable (lua_State *L, int objindex) { |
934 | 66.5k | TValue *obj; |
935 | 66.5k | Table *mt; |
936 | 66.5k | lua_lock(L); |
937 | 66.5k | api_checknelems(L, 1); |
938 | 66.5k | obj = index2value(L, objindex); |
939 | 66.5k | if (ttisnil(s2v(L->top.p - 1))) |
940 | 0 | mt = NULL; |
941 | 66.5k | else { |
942 | 66.5k | api_check(L, ttistable(s2v(L->top.p - 1)), "table expected"); |
943 | 66.5k | mt = hvalue(s2v(L->top.p - 1)); |
944 | 66.5k | } |
945 | 66.5k | switch (ttype(obj)) { |
946 | 3.23k | case LUA_TTABLE: { |
947 | 3.23k | hvalue(obj)->metatable = mt; |
948 | 3.23k | if (mt) { |
949 | 3.23k | luaC_objbarrier(L, gcvalue(obj), mt); |
950 | 3.23k | luaC_checkfinalizer(L, gcvalue(obj), mt); |
951 | 3.23k | } |
952 | 3.23k | break; |
953 | 0 | } |
954 | 60.1k | case LUA_TUSERDATA: { |
955 | 60.1k | uvalue(obj)->metatable = mt; |
956 | 60.1k | if (mt) { |
957 | 60.1k | luaC_objbarrier(L, uvalue(obj), mt); |
958 | 60.1k | luaC_checkfinalizer(L, gcvalue(obj), mt); |
959 | 60.1k | } |
960 | 60.1k | break; |
961 | 0 | } |
962 | 3.20k | default: { |
963 | 3.20k | G(L)->mt[ttype(obj)] = mt; |
964 | 3.20k | break; |
965 | 0 | } |
966 | 66.5k | } |
967 | 66.5k | L->top.p--; |
968 | 66.5k | lua_unlock(L); |
969 | 66.5k | return 1; |
970 | 66.5k | } |
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 | 53.1k | api_check(L, (nr) == LUA_MULTRET \ |
1000 | 53.1k | || (L->ci->top.p - L->top.p >= (nr) - (na)), \ |
1001 | 53.1k | "results from function overflow current stack size") |
1002 | | |
1003 | | |
1004 | | LUA_API void lua_callk (lua_State *L, int nargs, int nresults, |
1005 | 44.8k | lua_KContext ctx, lua_KFunction k) { |
1006 | 44.8k | StkId func; |
1007 | 44.8k | lua_lock(L); |
1008 | 44.8k | api_check(L, k == NULL || !isLua(L->ci), |
1009 | 44.8k | "cannot use continuations inside hooks"); |
1010 | 44.8k | api_checknelems(L, nargs+1); |
1011 | 44.8k | api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); |
1012 | 44.8k | checkresults(L, nargs, nresults); |
1013 | 44.8k | func = L->top.p - (nargs+1); |
1014 | 44.8k | 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 | 44.8k | else /* no continuation or no yieldable */ |
1020 | 44.8k | luaD_callnoyield(L, func, nresults); /* just do the call */ |
1021 | 44.8k | adjustresults(L, nresults); |
1022 | 44.8k | lua_unlock(L); |
1023 | 44.8k | } |
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 | 8.25k | static void f_call (lua_State *L, void *ud) { |
1037 | 8.25k | struct CallS *c = cast(struct CallS *, ud); |
1038 | 8.25k | luaD_callnoyield(L, c->func, c->nresults); |
1039 | 8.25k | } |
1040 | | |
1041 | | |
1042 | | |
1043 | | LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, |
1044 | 8.25k | lua_KContext ctx, lua_KFunction k) { |
1045 | 8.25k | struct CallS c; |
1046 | 8.25k | int status; |
1047 | 8.25k | ptrdiff_t func; |
1048 | 8.25k | lua_lock(L); |
1049 | 8.25k | api_check(L, k == NULL || !isLua(L->ci), |
1050 | 8.25k | "cannot use continuations inside hooks"); |
1051 | 8.25k | api_checknelems(L, nargs+1); |
1052 | 8.25k | api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); |
1053 | 8.25k | checkresults(L, nargs, nresults); |
1054 | 8.25k | if (errfunc == 0) |
1055 | 8.19k | func = 0; |
1056 | 64 | else { |
1057 | 64 | StkId o = index2stack(L, errfunc); |
1058 | 64 | api_check(L, ttisfunction(s2v(o)), "error handler must be a function"); |
1059 | 64 | func = savestack(L, o); |
1060 | 64 | } |
1061 | 8.25k | c.func = L->top.p - (nargs+1); /* function to be called */ |
1062 | 8.25k | if (k == NULL || !yieldable(L)) { /* no continuation or no yieldable? */ |
1063 | 8.25k | c.nresults = nresults; /* do a 'conventional' protected call */ |
1064 | 8.25k | status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func); |
1065 | 8.25k | } |
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 | 8.25k | adjustresults(L, nresults); |
1082 | 8.25k | lua_unlock(L); |
1083 | 8.25k | return status; |
1084 | 8.25k | } |
1085 | | |
1086 | | |
1087 | | LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, |
1088 | 205k | const char *chunkname, const char *mode) { |
1089 | 205k | ZIO z; |
1090 | 205k | int status; |
1091 | 205k | lua_lock(L); |
1092 | 205k | if (!chunkname) chunkname = "?"; |
1093 | 205k | luaZ_init(L, &z, reader, data); |
1094 | 205k | status = luaD_protectedparser(L, &z, chunkname, mode); |
1095 | 205k | if (status == LUA_OK) { /* no errors? */ |
1096 | 28.9k | LClosure *f = clLvalue(s2v(L->top.p - 1)); /* get new function */ |
1097 | 28.9k | if (f->nupvalues >= 1) { /* does it have an upvalue? */ |
1098 | | /* get global table from registry */ |
1099 | 28.9k | const TValue *gt = getGtable(L); |
1100 | | /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ |
1101 | 28.9k | setobj(L, f->upvals[0]->v.p, gt); |
1102 | 28.9k | luaC_barrier(L, f->upvals[0], gt); |
1103 | 28.9k | } |
1104 | 28.9k | } |
1105 | 205k | lua_unlock(L); |
1106 | 205k | return status; |
1107 | 205k | } |
1108 | | |
1109 | | |
1110 | 2.42k | LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) { |
1111 | 2.42k | int status; |
1112 | 2.42k | TValue *o; |
1113 | 2.42k | lua_lock(L); |
1114 | 2.42k | api_checknelems(L, 1); |
1115 | 2.42k | o = s2v(L->top.p - 1); |
1116 | 2.42k | if (isLfunction(o)) |
1117 | 2.42k | status = luaU_dump(L, getproto(o), writer, data, strip); |
1118 | 0 | else |
1119 | 0 | status = 1; |
1120 | 2.42k | lua_unlock(L); |
1121 | 2.42k | return status; |
1122 | 2.42k | } |
1123 | | |
1124 | | |
1125 | 1 | LUA_API int lua_status (lua_State *L) { |
1126 | 1 | return L->status; |
1127 | 1 | } |
1128 | | |
1129 | | |
1130 | | /* |
1131 | | ** Garbage-collection function |
1132 | | */ |
1133 | 9.56k | LUA_API int lua_gc (lua_State *L, int what, ...) { |
1134 | 9.56k | va_list argp; |
1135 | 9.56k | int res = 0; |
1136 | 9.56k | global_State *g = G(L); |
1137 | 9.56k | if (g->gcstp & GCSTPGC) /* internal stop? */ |
1138 | 0 | return -1; /* all options are invalid when stopped */ |
1139 | 9.56k | lua_lock(L); |
1140 | 9.56k | va_start(argp, what); |
1141 | 9.56k | switch (what) { |
1142 | 342 | case LUA_GCSTOP: { |
1143 | 342 | g->gcstp = GCSTPUSR; /* stopped by the user */ |
1144 | 342 | break; |
1145 | 0 | } |
1146 | 271 | case LUA_GCRESTART: { |
1147 | 271 | luaE_setdebt(g, 0); |
1148 | 271 | g->gcstp = 0; /* (GCSTPGC must be already zero here) */ |
1149 | 271 | break; |
1150 | 0 | } |
1151 | 2.46k | case LUA_GCCOLLECT: { |
1152 | 2.46k | luaC_fullgc(L, 0); |
1153 | 2.46k | break; |
1154 | 0 | } |
1155 | 400 | case LUA_GCCOUNT: { |
1156 | | /* GC values are expressed in Kbytes: #bytes/2^10 */ |
1157 | 400 | res = cast_int(gettotalbytes(g) >> 10); |
1158 | 400 | break; |
1159 | 0 | } |
1160 | 384 | case LUA_GCCOUNTB: { |
1161 | 384 | res = cast_int(gettotalbytes(g) & 0x3ff); |
1162 | 384 | break; |
1163 | 0 | } |
1164 | 2.80k | case LUA_GCSTEP: { |
1165 | 2.80k | int data = va_arg(argp, int); |
1166 | 2.80k | l_mem debt = 1; /* =1 to signal that it did an actual step */ |
1167 | 2.80k | lu_byte oldstp = g->gcstp; |
1168 | 2.80k | g->gcstp = 0; /* allow GC to run (GCSTPGC must be zero here) */ |
1169 | 2.80k | if (data == 0) { |
1170 | 2.80k | luaE_setdebt(g, 0); /* do a basic step */ |
1171 | 2.80k | luaC_step(L); |
1172 | 2.80k | } |
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 | 2.80k | g->gcstp = oldstp; /* restore previous state */ |
1179 | 2.80k | if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */ |
1180 | 1.00k | res = 1; /* signal it */ |
1181 | 2.80k | break; |
1182 | 0 | } |
1183 | 1.02k | case LUA_GCSETPAUSE: { |
1184 | 1.02k | int data = va_arg(argp, int); |
1185 | 1.02k | res = getgcparam(g->gcpause); |
1186 | 1.02k | setgcparam(g->gcpause, data); |
1187 | 1.02k | break; |
1188 | 0 | } |
1189 | 209 | case LUA_GCSETSTEPMUL: { |
1190 | 209 | int data = va_arg(argp, int); |
1191 | 209 | res = getgcparam(g->gcstepmul); |
1192 | 209 | setgcparam(g->gcstepmul, data); |
1193 | 209 | break; |
1194 | 0 | } |
1195 | 386 | case LUA_GCISRUNNING: { |
1196 | 386 | res = gcrunning(g); |
1197 | 386 | break; |
1198 | 0 | } |
1199 | 1.27k | case LUA_GCGEN: { |
1200 | 1.27k | int minormul = va_arg(argp, int); |
1201 | 1.27k | int majormul = va_arg(argp, int); |
1202 | 1.27k | res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC; |
1203 | 1.27k | if (minormul != 0) |
1204 | 0 | g->genminormul = minormul; |
1205 | 1.27k | if (majormul != 0) |
1206 | 0 | setgcparam(g->genmajormul, majormul); |
1207 | 1.27k | luaC_changemode(L, KGC_GEN); |
1208 | 1.27k | break; |
1209 | 0 | } |
1210 | 1 | case LUA_GCINC: { |
1211 | 1 | int pause = va_arg(argp, int); |
1212 | 1 | int stepmul = va_arg(argp, int); |
1213 | 1 | int stepsize = va_arg(argp, int); |
1214 | 1 | res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC; |
1215 | 1 | if (pause != 0) |
1216 | 0 | setgcparam(g->gcpause, pause); |
1217 | 1 | if (stepmul != 0) |
1218 | 0 | setgcparam(g->gcstepmul, stepmul); |
1219 | 1 | if (stepsize != 0) |
1220 | 0 | g->gcstepsize = stepsize; |
1221 | 1 | luaC_changemode(L, KGC_INC); |
1222 | 1 | break; |
1223 | 0 | } |
1224 | 0 | default: res = -1; /* invalid option */ |
1225 | 9.56k | } |
1226 | 9.56k | va_end(argp); |
1227 | 9.56k | lua_unlock(L); |
1228 | 9.56k | return res; |
1229 | 9.56k | } |
1230 | | |
1231 | | |
1232 | | |
1233 | | /* |
1234 | | ** miscellaneous functions |
1235 | | */ |
1236 | | |
1237 | | |
1238 | 9.56k | LUA_API int lua_error (lua_State *L) { |
1239 | 9.56k | TValue *errobj; |
1240 | 9.56k | lua_lock(L); |
1241 | 9.56k | errobj = s2v(L->top.p - 1); |
1242 | 9.56k | api_checknelems(L, 1); |
1243 | | /* error object is the memory error message? */ |
1244 | 9.56k | if (ttisshrstring(errobj) && eqshrstr(tsvalue(errobj), G(L)->memerrmsg)) |
1245 | 0 | luaM_error(L); /* raise a memory error */ |
1246 | 9.56k | else |
1247 | 9.56k | 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 | 9.56k | } |
1251 | | |
1252 | | |
1253 | 675k | LUA_API int lua_next (lua_State *L, int idx) { |
1254 | 675k | Table *t; |
1255 | 675k | int more; |
1256 | 675k | lua_lock(L); |
1257 | 675k | api_checknelems(L, 1); |
1258 | 675k | t = gettable(L, idx); |
1259 | 675k | more = luaH_next(L, t, L->top.p - 1); |
1260 | 675k | if (more) { |
1261 | 647k | api_incr_top(L); |
1262 | 647k | } |
1263 | 27.2k | else /* no more elements */ |
1264 | 27.2k | L->top.p -= 1; /* remove key */ |
1265 | 675k | lua_unlock(L); |
1266 | 675k | return more; |
1267 | 675k | } |
1268 | | |
1269 | | |
1270 | 50.5k | LUA_API void lua_toclose (lua_State *L, int idx) { |
1271 | 50.5k | int nresults; |
1272 | 50.5k | StkId o; |
1273 | 50.5k | lua_lock(L); |
1274 | 50.5k | o = index2stack(L, idx); |
1275 | 50.5k | nresults = L->ci->nresults; |
1276 | 50.5k | api_check(L, L->tbclist.p < o, "given index below or equal a marked one"); |
1277 | 50.5k | luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */ |
1278 | 50.5k | if (!hastocloseCfunc(nresults)) /* function not marked yet? */ |
1279 | 50.2k | L->ci->nresults = codeNresults(nresults); /* mark it */ |
1280 | 50.5k | lua_assert(hastocloseCfunc(L->ci->nresults)); |
1281 | 50.5k | lua_unlock(L); |
1282 | 50.5k | } |
1283 | | |
1284 | | |
1285 | 18.8k | LUA_API void lua_concat (lua_State *L, int n) { |
1286 | 18.8k | lua_lock(L); |
1287 | 18.8k | api_checknelems(L, n); |
1288 | 18.8k | if (n > 0) |
1289 | 18.8k | 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 | 18.8k | luaC_checkGC(L); |
1295 | 18.8k | lua_unlock(L); |
1296 | 18.8k | } |
1297 | | |
1298 | | |
1299 | 3.20k | LUA_API void lua_len (lua_State *L, int idx) { |
1300 | 3.20k | TValue *t; |
1301 | 3.20k | lua_lock(L); |
1302 | 3.20k | t = index2value(L, idx); |
1303 | 3.20k | luaV_objlen(L, L->top.p, t); |
1304 | 3.20k | api_incr_top(L); |
1305 | 3.20k | lua_unlock(L); |
1306 | 3.20k | } |
1307 | | |
1308 | | |
1309 | 383k | LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) { |
1310 | 383k | lua_Alloc f; |
1311 | 383k | lua_lock(L); |
1312 | 383k | if (ud) *ud = G(L)->ud; |
1313 | 383k | f = G(L)->frealloc; |
1314 | 383k | lua_unlock(L); |
1315 | 383k | return f; |
1316 | 383k | } |
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 | 18.5k | void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud) { |
1328 | 18.5k | lua_lock(L); |
1329 | 18.5k | G(L)->ud_warn = ud; |
1330 | 18.5k | G(L)->warnf = f; |
1331 | 18.5k | lua_unlock(L); |
1332 | 18.5k | } |
1333 | | |
1334 | | |
1335 | 8.01k | void lua_warning (lua_State *L, const char *msg, int tocont) { |
1336 | 8.01k | lua_lock(L); |
1337 | 8.01k | luaE_warning(L, msg, tocont); |
1338 | 8.01k | lua_unlock(L); |
1339 | 8.01k | } |
1340 | | |
1341 | | |
1342 | | |
1343 | 64.0k | LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) { |
1344 | 64.0k | Udata *u; |
1345 | 64.0k | lua_lock(L); |
1346 | 64.0k | api_check(L, 0 <= nuvalue && nuvalue < USHRT_MAX, "invalid value"); |
1347 | 64.0k | u = luaS_newudata(L, size, nuvalue); |
1348 | 64.0k | setuvalue(L, s2v(L->top.p), u); |
1349 | 64.0k | api_incr_top(L); |
1350 | 64.0k | luaC_checkGC(L); |
1351 | 64.0k | lua_unlock(L); |
1352 | 64.0k | return getudatamem(u); |
1353 | 64.0k | } |
1354 | | |
1355 | | |
1356 | | |
1357 | | static const char *aux_upvalue (TValue *fi, int n, TValue **val, |
1358 | 47 | GCObject **owner) { |
1359 | 47 | 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 | 47 | default: return NULL; /* not a closure */ |
1380 | 47 | } |
1381 | 47 | } |
1382 | | |
1383 | | |
1384 | 25 | LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { |
1385 | 25 | const char *name; |
1386 | 25 | TValue *val = NULL; /* to avoid warnings */ |
1387 | 25 | lua_lock(L); |
1388 | 25 | name = aux_upvalue(index2value(L, funcindex), n, &val, NULL); |
1389 | 25 | if (name) { |
1390 | 0 | setobj2s(L, L->top.p, val); |
1391 | 0 | api_incr_top(L); |
1392 | 0 | } |
1393 | 25 | lua_unlock(L); |
1394 | 25 | return name; |
1395 | 25 | } |
1396 | | |
1397 | | |
1398 | 22 | LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { |
1399 | 22 | const char *name; |
1400 | 22 | TValue *val = NULL; /* to avoid warnings */ |
1401 | 22 | GCObject *owner = NULL; /* to avoid warnings */ |
1402 | 22 | TValue *fi; |
1403 | 22 | lua_lock(L); |
1404 | 22 | fi = index2value(L, funcindex); |
1405 | 22 | api_checknelems(L, 1); |
1406 | 22 | name = aux_upvalue(fi, n, &val, &owner); |
1407 | 22 | 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 | 22 | lua_unlock(L); |
1413 | 22 | return name; |
1414 | 22 | } |
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 | | |