1/*
2** Public Lua/C API.
3** Copyright (C) 2005-2014 Mike Pall. See Copyright Notice in luajit.h
4**
5** Major portions taken verbatim or adapted from the Lua interpreter.
6** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
7*/
8
9#define lj_api_c
10#define LUA_CORE
11
12#include "lj_obj.h"
13#include "lj_gc.h"
14#include "lj_err.h"
15#include "lj_debug.h"
16#include "lj_str.h"
17#include "lj_tab.h"
18#include "lj_func.h"
19#include "lj_udata.h"
20#include "lj_meta.h"
21#include "lj_state.h"
22#include "lj_bc.h"
23#include "lj_frame.h"
24#include "lj_trace.h"
25#include "lj_vm.h"
26#include "lj_strscan.h"
27
28/* -- Common helper functions --------------------------------------------- */
29
30#define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base))
31#define api_checkvalidindex(L, i) api_check(L, (i) != niltv(L))
32
33static TValue *index2adr(lua_State *L, int idx)
34{
35 if (idx > 0) {
36 TValue *o = L->base + (idx - 1);
37 return o < L->top ? o : niltv(L);
38 } else if (idx > LUA_REGISTRYINDEX) {
39 api_check(L, idx != 0 && -idx <= L->top - L->base);
40 return L->top + idx;
41 } else if (idx == LUA_GLOBALSINDEX) {
42 TValue *o = &G(L)->tmptv;
43 settabV(L, o, tabref(L->env));
44 return o;
45 } else if (idx == LUA_REGISTRYINDEX) {
46 return registry(L);
47 } else {
48 GCfunc *fn = curr_func(L);
49 api_check(L, fn->c.gct == ~LJ_TFUNC && !isluafunc(fn));
50 if (idx == LUA_ENVIRONINDEX) {
51 TValue *o = &G(L)->tmptv;
52 settabV(L, o, tabref(fn->c.env));
53 return o;
54 } else {
55 idx = LUA_GLOBALSINDEX - idx;
56 return idx <= fn->c.nupvalues ? &fn->c.upvalue[idx-1] : niltv(L);
57 }
58 }
59}
60
61static TValue *stkindex2adr(lua_State *L, int idx)
62{
63 if (idx > 0) {
64 TValue *o = L->base + (idx - 1);
65 return o < L->top ? o : niltv(L);
66 } else {
67 api_check(L, idx != 0 && -idx <= L->top - L->base);
68 return L->top + idx;
69 }
70}
71
72static GCtab *getcurrenv(lua_State *L)
73{
74 GCfunc *fn = curr_func(L);
75 return fn->c.gct == ~LJ_TFUNC ? tabref(fn->c.env) : tabref(L->env);
76}
77
78/* -- Miscellaneous API functions ----------------------------------------- */
79
80LUA_API int lua_status(lua_State *L)
81{
82 return L->status;
83}
84
85LUA_API int lua_checkstack(lua_State *L, int size)
86{
87 if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK) {
88 return 0; /* Stack overflow. */
89 } else if (size > 0) {
90 lj_state_checkstack(L, (MSize)size);
91 }
92 return 1;
93}
94
95LUALIB_API void luaL_checkstack(lua_State *L, int size, const char *msg)
96{
97 if (!lua_checkstack(L, size))
98 lj_err_callerv(L, LJ_ERR_STKOVM, msg);
99}
100
101LUA_API void lua_xmove(lua_State *from, lua_State *to, int n)
102{
103 TValue *f, *t;
104 if (from == to) return;
105 api_checknelems(from, n);
106 api_check(from, G(from) == G(to));
107 lj_state_checkstack(to, (MSize)n);
108 f = from->top;
109 t = to->top = to->top + n;
110 while (--n >= 0) copyTV(to, --t, --f);
111 from->top = f;
112}
113
114/* -- Stack manipulation -------------------------------------------------- */
115
116LUA_API int lua_gettop(lua_State *L)
117{
118 return (int)(L->top - L->base);
119}
120
121LUA_API void lua_settop(lua_State *L, int idx)
122{
123 if (idx >= 0) {
124 api_check(L, idx <= tvref(L->maxstack) - L->base);
125 if (L->base + idx > L->top) {
126 if (L->base + idx >= tvref(L->maxstack))
127 lj_state_growstack(L, (MSize)idx - (MSize)(L->top - L->base));
128 do { setnilV(L->top++); } while (L->top < L->base + idx);
129 } else {
130 L->top = L->base + idx;
131 }
132 } else {
133 api_check(L, -(idx+1) <= (L->top - L->base));
134 L->top += idx+1; /* Shrinks top (idx < 0). */
135 }
136}
137
138LUA_API void lua_remove(lua_State *L, int idx)
139{
140 TValue *p = stkindex2adr(L, idx);
141 api_checkvalidindex(L, p);
142 while (++p < L->top) copyTV(L, p-1, p);
143 L->top--;
144}
145
146LUA_API void lua_insert(lua_State *L, int idx)
147{
148 TValue *q, *p = stkindex2adr(L, idx);
149 api_checkvalidindex(L, p);
150 for (q = L->top; q > p; q--) copyTV(L, q, q-1);
151 copyTV(L, p, L->top);
152}
153
154LUA_API void lua_replace(lua_State *L, int idx)
155{
156 api_checknelems(L, 1);
157 if (idx == LUA_GLOBALSINDEX) {
158 api_check(L, tvistab(L->top-1));
159 /* NOBARRIER: A thread (i.e. L) is never black. */
160 setgcref(L->env, obj2gco(tabV(L->top-1)));
161 } else if (idx == LUA_ENVIRONINDEX) {
162 GCfunc *fn = curr_func(L);
163 if (fn->c.gct != ~LJ_TFUNC)
164 lj_err_msg(L, LJ_ERR_NOENV);
165 api_check(L, tvistab(L->top-1));
166 setgcref(fn->c.env, obj2gco(tabV(L->top-1)));
167 lj_gc_barrier(L, fn, L->top-1);
168 } else {
169 TValue *o = index2adr(L, idx);
170 api_checkvalidindex(L, o);
171 copyTV(L, o, L->top-1);
172 if (idx < LUA_GLOBALSINDEX) /* Need a barrier for upvalues. */
173 lj_gc_barrier(L, curr_func(L), L->top-1);
174 }
175 L->top--;
176}
177
178LUA_API void lua_pushvalue(lua_State *L, int idx)
179{
180 copyTV(L, L->top, index2adr(L, idx));
181 incr_top(L);
182}
183
184/* -- Stack getters ------------------------------------------------------- */
185
186LUA_API int lua_type(lua_State *L, int idx)
187{
188 cTValue *o = index2adr(L, idx);
189 if (tvisnumber(o)) {
190 return LUA_TNUMBER;
191#if LJ_64
192 } else if (tvislightud(o)) {
193 return LUA_TLIGHTUSERDATA;
194#endif
195 } else if (o == niltv(L)) {
196 return LUA_TNONE;
197 } else { /* Magic internal/external tag conversion. ORDER LJ_T */
198 uint32_t t = ~itype(o);
199#if LJ_64
200 int tt = (int)((U64x(75a06,98042110) >> 4*t) & 15u);
201#else
202 int tt = (int)(((t < 8 ? 0x98042110u : 0x75a06u) >> 4*(t&7)) & 15u);
203#endif
204 lua_assert(tt != LUA_TNIL || tvisnil(o));
205 return tt;
206 }
207}
208
209LUALIB_API void luaL_checktype(lua_State *L, int idx, int tt)
210{
211 if (lua_type(L, idx) != tt)
212 lj_err_argt(L, idx, tt);
213}
214
215LUALIB_API void luaL_checkany(lua_State *L, int idx)
216{
217 if (index2adr(L, idx) == niltv(L))
218 lj_err_arg(L, idx, LJ_ERR_NOVAL);
219}
220
221LUA_API const char *lua_typename(lua_State *L, int t)
222{
223 UNUSED(L);
224 return lj_obj_typename[t+1];
225}
226
227LUA_API int lua_iscfunction(lua_State *L, int idx)
228{
229 cTValue *o = index2adr(L, idx);
230 return tvisfunc(o) && !isluafunc(funcV(o));
231}
232
233LUA_API int lua_isnumber(lua_State *L, int idx)
234{
235 cTValue *o = index2adr(L, idx);
236 TValue tmp;
237 return (tvisnumber(o) || (tvisstr(o) && lj_strscan_number(strV(o), &tmp)));
238}
239
240LUA_API int lua_isstring(lua_State *L, int idx)
241{
242 cTValue *o = index2adr(L, idx);
243 return (tvisstr(o) || tvisnumber(o));
244}
245
246LUA_API int lua_isuserdata(lua_State *L, int idx)
247{
248 cTValue *o = index2adr(L, idx);
249 return (tvisudata(o) || tvislightud(o));
250}
251
252LUA_API int lua_rawequal(lua_State *L, int idx1, int idx2)
253{
254 cTValue *o1 = index2adr(L, idx1);
255 cTValue *o2 = index2adr(L, idx2);
256 return (o1 == niltv(L) || o2 == niltv(L)) ? 0 : lj_obj_equal(o1, o2);
257}
258
259LUA_API int lua_equal(lua_State *L, int idx1, int idx2)
260{
261 cTValue *o1 = index2adr(L, idx1);
262 cTValue *o2 = index2adr(L, idx2);
263 if (tvisint(o1) && tvisint(o2)) {
264 return intV(o1) == intV(o2);
265 } else if (tvisnumber(o1) && tvisnumber(o2)) {
266 return numberVnum(o1) == numberVnum(o2);
267 } else if (itype(o1) != itype(o2)) {
268 return 0;
269 } else if (tvispri(o1)) {
270 return o1 != niltv(L) && o2 != niltv(L);
271#if LJ_64
272 } else if (tvislightud(o1)) {
273 return o1->u64 == o2->u64;
274#endif
275 } else if (gcrefeq(o1->gcr, o2->gcr)) {
276 return 1;
277 } else if (!tvistabud(o1)) {
278 return 0;
279 } else {
280 TValue *base = lj_meta_equal(L, gcV(o1), gcV(o2), 0);
281 if ((uintptr_t)base <= 1) {
282 return (int)(uintptr_t)base;
283 } else {
284 L->top = base+2;
285 lj_vm_call(L, base, 1+1);
286 L->top -= 2;
287 return tvistruecond(L->top+1);
288 }
289 }
290}
291
292LUA_API int lua_lessthan(lua_State *L, int idx1, int idx2)
293{
294 cTValue *o1 = index2adr(L, idx1);
295 cTValue *o2 = index2adr(L, idx2);
296 if (o1 == niltv(L) || o2 == niltv(L)) {
297 return 0;
298 } else if (tvisint(o1) && tvisint(o2)) {
299 return intV(o1) < intV(o2);
300 } else if (tvisnumber(o1) && tvisnumber(o2)) {
301 return numberVnum(o1) < numberVnum(o2);
302 } else {
303 TValue *base = lj_meta_comp(L, o1, o2, 0);
304 if ((uintptr_t)base <= 1) {
305 return (int)(uintptr_t)base;
306 } else {
307 L->top = base+2;
308 lj_vm_call(L, base, 1+1);
309 L->top -= 2;
310 return tvistruecond(L->top+1);
311 }
312 }
313}
314
315LUA_API lua_Number lua_tonumber(lua_State *L, int idx)
316{
317 cTValue *o = index2adr(L, idx);
318 TValue tmp;
319 if (LJ_LIKELY(tvisnumber(o)))
320 return numberVnum(o);
321 else if (tvisstr(o) && lj_strscan_num(strV(o), &tmp))
322 return numV(&tmp);
323 else
324 return 0;
325}
326
327LUALIB_API lua_Number luaL_checknumber(lua_State *L, int idx)
328{
329 cTValue *o = index2adr(L, idx);
330 TValue tmp;
331 if (LJ_LIKELY(tvisnumber(o)))
332 return numberVnum(o);
333 else if (!(tvisstr(o) && lj_strscan_num(strV(o), &tmp)))
334 lj_err_argt(L, idx, LUA_TNUMBER);
335 return numV(&tmp);
336}
337
338LUALIB_API lua_Number luaL_optnumber(lua_State *L, int idx, lua_Number def)
339{
340 cTValue *o = index2adr(L, idx);
341 TValue tmp;
342 if (LJ_LIKELY(tvisnumber(o)))
343 return numberVnum(o);
344 else if (tvisnil(o))
345 return def;
346 else if (!(tvisstr(o) && lj_strscan_num(strV(o), &tmp)))
347 lj_err_argt(L, idx, LUA_TNUMBER);
348 return numV(&tmp);
349}
350
351LUA_API lua_Integer lua_tointeger(lua_State *L, int idx)
352{
353 cTValue *o = index2adr(L, idx);
354 TValue tmp;
355 lua_Number n;
356 if (LJ_LIKELY(tvisint(o))) {
357 return intV(o);
358 } else if (LJ_LIKELY(tvisnum(o))) {
359 n = numV(o);
360 } else {
361 if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp)))
362 return 0;
363 if (tvisint(&tmp))
364 return (lua_Integer)intV(&tmp);
365 n = numV(&tmp);
366 }
367#if LJ_64
368 return (lua_Integer)n;
369#else
370 return lj_num2int(n);
371#endif
372}
373
374LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int idx)
375{
376 cTValue *o = index2adr(L, idx);
377 TValue tmp;
378 lua_Number n;
379 if (LJ_LIKELY(tvisint(o))) {
380 return intV(o);
381 } else if (LJ_LIKELY(tvisnum(o))) {
382 n = numV(o);
383 } else {
384 if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp)))
385 lj_err_argt(L, idx, LUA_TNUMBER);
386 if (tvisint(&tmp))
387 return (lua_Integer)intV(&tmp);
388 n = numV(&tmp);
389 }
390#if LJ_64
391 return (lua_Integer)n;
392#else
393 return lj_num2int(n);
394#endif
395}
396
397LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int idx, lua_Integer def)
398{
399 cTValue *o = index2adr(L, idx);
400 TValue tmp;
401 lua_Number n;
402 if (LJ_LIKELY(tvisint(o))) {
403 return intV(o);
404 } else if (LJ_LIKELY(tvisnum(o))) {
405 n = numV(o);
406 } else if (tvisnil(o)) {
407 return def;
408 } else {
409 if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp)))
410 lj_err_argt(L, idx, LUA_TNUMBER);
411 if (tvisint(&tmp))
412 return (lua_Integer)intV(&tmp);
413 n = numV(&tmp);
414 }
415#if LJ_64
416 return (lua_Integer)n;
417#else
418 return lj_num2int(n);
419#endif
420}
421
422LUA_API int lua_toboolean(lua_State *L, int idx)
423{
424 cTValue *o = index2adr(L, idx);
425 return tvistruecond(o);
426}
427
428LUA_API const char *lua_tolstring(lua_State *L, int idx, size_t *len)
429{
430 TValue *o = index2adr(L, idx);
431 GCstr *s;
432 if (LJ_LIKELY(tvisstr(o))) {
433 s = strV(o);
434 } else if (tvisnumber(o)) {
435 lj_gc_check(L);
436 o = index2adr(L, idx); /* GC may move the stack. */
437 s = lj_str_fromnumber(L, o);
438 setstrV(L, o, s);
439 } else {
440 if (len != NULL) *len = 0;
441 return NULL;
442 }
443 if (len != NULL) *len = s->len;
444 return strdata(s);
445}
446
447LUALIB_API const char *luaL_checklstring(lua_State *L, int idx, size_t *len)
448{
449 TValue *o = index2adr(L, idx);
450 GCstr *s;
451 if (LJ_LIKELY(tvisstr(o))) {
452 s = strV(o);
453 } else if (tvisnumber(o)) {
454 lj_gc_check(L);
455 o = index2adr(L, idx); /* GC may move the stack. */
456 s = lj_str_fromnumber(L, o);
457 setstrV(L, o, s);
458 } else {
459 lj_err_argt(L, idx, LUA_TSTRING);
460 }
461 if (len != NULL) *len = s->len;
462 return strdata(s);
463}
464
465LUALIB_API const char *luaL_optlstring(lua_State *L, int idx,
466 const char *def, size_t *len)
467{
468 TValue *o = index2adr(L, idx);
469 GCstr *s;
470 if (LJ_LIKELY(tvisstr(o))) {
471 s = strV(o);
472 } else if (tvisnil(o)) {
473 if (len != NULL) *len = def ? strlen(def) : 0;
474 return def;
475 } else if (tvisnumber(o)) {
476 lj_gc_check(L);
477 o = index2adr(L, idx); /* GC may move the stack. */
478 s = lj_str_fromnumber(L, o);
479 setstrV(L, o, s);
480 } else {
481 lj_err_argt(L, idx, LUA_TSTRING);
482 }
483 if (len != NULL) *len = s->len;
484 return strdata(s);
485}
486
487LUALIB_API int luaL_checkoption(lua_State *L, int idx, const char *def,
488 const char *const lst[])
489{
490 ptrdiff_t i;
491 const char *s = lua_tolstring(L, idx, NULL);
492 if (s == NULL && (s = def) == NULL)
493 lj_err_argt(L, idx, LUA_TSTRING);
494 for (i = 0; lst[i]; i++)
495 if (strcmp(lst[i], s) == 0)
496 return (int)i;
497 lj_err_argv(L, idx, LJ_ERR_INVOPTM, s);
498}
499
500LUA_API size_t lua_objlen(lua_State *L, int idx)
501{
502 TValue *o = index2adr(L, idx);
503 if (tvisstr(o)) {
504 return strV(o)->len;
505 } else if (tvistab(o)) {
506 return (size_t)lj_tab_len(tabV(o));
507 } else if (tvisudata(o)) {
508 return udataV(o)->len;
509 } else if (tvisnumber(o)) {
510 GCstr *s = lj_str_fromnumber(L, o);
511 setstrV(L, o, s);
512 return s->len;
513 } else {
514 return 0;
515 }
516}
517
518LUA_API lua_CFunction lua_tocfunction(lua_State *L, int idx)
519{
520 cTValue *o = index2adr(L, idx);
521 if (tvisfunc(o)) {
522 BCOp op = bc_op(*mref(funcV(o)->c.pc, BCIns));
523 if (op == BC_FUNCC || op == BC_FUNCCW)
524 return funcV(o)->c.f;
525 }
526 return NULL;
527}
528
529LUA_API void *lua_touserdata(lua_State *L, int idx)
530{
531 cTValue *o = index2adr(L, idx);
532 if (tvisudata(o))
533 return uddata(udataV(o));
534 else if (tvislightud(o))
535 return lightudV(o);
536 else
537 return NULL;
538}
539
540LUA_API lua_State *lua_tothread(lua_State *L, int idx)
541{
542 cTValue *o = index2adr(L, idx);
543 return (!tvisthread(o)) ? NULL : threadV(o);
544}
545
546LUA_API const void *lua_topointer(lua_State *L, int idx)
547{
548 cTValue *o = index2adr(L, idx);
549 if (tvisudata(o))
550 return uddata(udataV(o));
551 else if (tvislightud(o))
552 return lightudV(o);
553 else if (tviscdata(o))
554 return cdataptr(cdataV(o));
555 else if (tvisgcv(o))
556 return gcV(o);
557 else
558 return NULL;
559}
560
561/* -- Stack setters (object creation) ------------------------------------- */
562
563LUA_API void lua_pushnil(lua_State *L)
564{
565 setnilV(L->top);
566 incr_top(L);
567}
568
569LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
570{
571 setnumV(L->top, n);
572 if (LJ_UNLIKELY(tvisnan(L->top)))
573 setnanV(L->top); /* Canonicalize injected NaNs. */
574 incr_top(L);
575}
576
577LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
578{
579 setintptrV(L->top, n);
580 incr_top(L);
581}
582
583LUA_API void lua_pushlstring(lua_State *L, const char *str, size_t len)
584{
585 GCstr *s;
586 lj_gc_check(L);
587 s = lj_str_new(L, str, len);
588 setstrV(L, L->top, s);
589 incr_top(L);
590}
591
592LUA_API void lua_pushstring(lua_State *L, const char *str)
593{
594 if (str == NULL) {
595 setnilV(L->top);
596 } else {
597 GCstr *s;
598 lj_gc_check(L);
599 s = lj_str_newz(L, str);
600 setstrV(L, L->top, s);
601 }
602 incr_top(L);
603}
604
605LUA_API const char *lua_pushvfstring(lua_State *L, const char *fmt,
606 va_list argp)
607{
608 lj_gc_check(L);
609 return lj_str_pushvf(L, fmt, argp);
610}
611
612LUA_API const char *lua_pushfstring(lua_State *L, const char *fmt, ...)
613{
614 const char *ret;
615 va_list argp;
616 lj_gc_check(L);
617 va_start(argp, fmt);
618 ret = lj_str_pushvf(L, fmt, argp);
619 va_end(argp);
620 return ret;
621}
622
623LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction f, int n)
624{
625 GCfunc *fn;
626 lj_gc_check(L);
627 api_checknelems(L, n);
628 fn = lj_func_newC(L, (MSize)n, getcurrenv(L));
629 fn->c.f = f;
630 L->top -= n;
631 while (n--)
632 copyTV(L, &fn->c.upvalue[n], L->top+n);
633 setfuncV(L, L->top, fn);
634 lua_assert(iswhite(obj2gco(fn)));
635 incr_top(L);
636}
637
638LUA_API void lua_pushboolean(lua_State *L, int b)
639{
640 setboolV(L->top, (b != 0));
641 incr_top(L);
642}
643
644LUA_API void lua_pushlightuserdata(lua_State *L, void *p)
645{
646 setlightudV(L->top, checklightudptr(L, p));
647 incr_top(L);
648}
649
650LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
651{
652 GCtab *t;
653 lj_gc_check(L);
654 t = lj_tab_new(L, (uint32_t)(narray > 0 ? narray+1 : 0), hsize2hbits(nrec));
655 settabV(L, L->top, t);
656 incr_top(L);
657}
658
659LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname)
660{
661 GCtab *regt = tabV(registry(L));
662 TValue *tv = lj_tab_setstr(L, regt, lj_str_newz(L, tname));
663 if (tvisnil(tv)) {
664 GCtab *mt = lj_tab_new(L, 0, 1);
665 settabV(L, tv, mt);
666 settabV(L, L->top++, mt);
667 lj_gc_anybarriert(L, regt);
668 return 1;
669 } else {
670 copyTV(L, L->top++, tv);
671 return 0;
672 }
673}
674
675LUA_API int lua_pushthread(lua_State *L)
676{
677 setthreadV(L, L->top, L);
678 incr_top(L);
679 return (mainthread(G(L)) == L);
680}
681
682LUA_API lua_State *lua_newthread(lua_State *L)
683{
684 lua_State *L1;
685 lj_gc_check(L);
686 L1 = lj_state_new(L);
687 setthreadV(L, L->top, L1);
688 incr_top(L);
689 return L1;
690}
691
692LUA_API void *lua_newuserdata(lua_State *L, size_t size)
693{
694 GCudata *ud;
695 lj_gc_check(L);
696 if (size > LJ_MAX_UDATA)
697 lj_err_msg(L, LJ_ERR_UDATAOV);
698 ud = lj_udata_new(L, (MSize)size, getcurrenv(L));
699 setudataV(L, L->top, ud);
700 incr_top(L);
701 return uddata(ud);
702}
703
704LUA_API void lua_concat(lua_State *L, int n)
705{
706 api_checknelems(L, n);
707 if (n >= 2) {
708 n--;
709 do {
710 TValue *top = lj_meta_cat(L, L->top-1, -n);
711 if (top == NULL) {
712 L->top -= n;
713 break;
714 }
715 n -= (int)(L->top - top);
716 L->top = top+2;
717 lj_vm_call(L, top, 1+1);
718 L->top--;
719 copyTV(L, L->top-1, L->top);
720 } while (--n > 0);
721 } else if (n == 0) { /* Push empty string. */
722 setstrV(L, L->top, &G(L)->strempty);
723 incr_top(L);
724 }
725 /* else n == 1: nothing to do. */
726}
727
728/* -- Object getters ------------------------------------------------------ */
729
730LUA_API void lua_gettable(lua_State *L, int idx)
731{
732 cTValue *v, *t = index2adr(L, idx);
733 api_checkvalidindex(L, t);
734 v = lj_meta_tget(L, t, L->top-1);
735 if (v == NULL) {
736 L->top += 2;
737 lj_vm_call(L, L->top-2, 1+1);
738 L->top -= 2;
739 v = L->top+1;
740 }
741 copyTV(L, L->top-1, v);
742}
743
744LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
745{
746 cTValue *v, *t = index2adr(L, idx);
747 TValue key;
748 api_checkvalidindex(L, t);
749 setstrV(L, &key, lj_str_newz(L, k));
750 v = lj_meta_tget(L, t, &key);
751 if (v == NULL) {
752 L->top += 2;
753 lj_vm_call(L, L->top-2, 1+1);
754 L->top -= 2;
755 v = L->top+1;
756 }
757 copyTV(L, L->top, v);
758 incr_top(L);
759}
760
761LUA_API void lua_rawget(lua_State *L, int idx)
762{
763 cTValue *t = index2adr(L, idx);
764 api_check(L, tvistab(t));
765 copyTV(L, L->top-1, lj_tab_get(L, tabV(t), L->top-1));
766}
767
768LUA_API void lua_rawgeti(lua_State *L, int idx, int n)
769{
770 cTValue *v, *t = index2adr(L, idx);
771 api_check(L, tvistab(t));
772 v = lj_tab_getint(tabV(t), n);
773 if (v) {
774 copyTV(L, L->top, v);
775 } else {
776 setnilV(L->top);
777 }
778 incr_top(L);
779}
780
781LUA_API int lua_getmetatable(lua_State *L, int idx)
782{
783 cTValue *o = index2adr(L, idx);
784 GCtab *mt = NULL;
785 if (tvistab(o))
786 mt = tabref(tabV(o)->metatable);
787 else if (tvisudata(o))
788 mt = tabref(udataV(o)->metatable);
789 else
790 mt = tabref(basemt_obj(G(L), o));
791 if (mt == NULL)
792 return 0;
793 settabV(L, L->top, mt);
794 incr_top(L);
795 return 1;
796}
797
798LUALIB_API int luaL_getmetafield(lua_State *L, int idx, const char *field)
799{
800 if (lua_getmetatable(L, idx)) {
801 cTValue *tv = lj_tab_getstr(tabV(L->top-1), lj_str_newz(L, field));
802 if (tv && !tvisnil(tv)) {
803 copyTV(L, L->top-1, tv);
804 return 1;
805 }
806 L->top--;
807 }
808 return 0;
809}
810
811LUA_API void lua_getfenv(lua_State *L, int idx)
812{
813 cTValue *o = index2adr(L, idx);
814 api_checkvalidindex(L, o);
815 if (tvisfunc(o)) {
816 settabV(L, L->top, tabref(funcV(o)->c.env));
817 } else if (tvisudata(o)) {
818 settabV(L, L->top, tabref(udataV(o)->env));
819 } else if (tvisthread(o)) {
820 settabV(L, L->top, tabref(threadV(o)->env));
821 } else {
822 setnilV(L->top);
823 }
824 incr_top(L);
825}
826
827LUA_API int lua_next(lua_State *L, int idx)
828{
829 cTValue *t = index2adr(L, idx);
830 int more;
831 api_check(L, tvistab(t));
832 more = lj_tab_next(L, tabV(t), L->top-1);
833 if (more) {
834 incr_top(L); /* Return new key and value slot. */
835 } else { /* End of traversal. */
836 L->top--; /* Remove key slot. */
837 }
838 return more;
839}
840
841LUA_API const char *lua_getupvalue(lua_State *L, int idx, int n)
842{
843 TValue *val;
844 const char *name = lj_debug_uvnamev(index2adr(L, idx), (uint32_t)(n-1), &val);
845 if (name) {
846 copyTV(L, L->top, val);
847 incr_top(L);
848 }
849 return name;
850}
851
852LUA_API void *lua_upvalueid(lua_State *L, int idx, int n)
853{
854 GCfunc *fn = funcV(index2adr(L, idx));
855 n--;
856 api_check(L, (uint32_t)n < fn->l.nupvalues);
857 return isluafunc(fn) ? (void *)gcref(fn->l.uvptr[n]) :
858 (void *)&fn->c.upvalue[n];
859}
860
861LUA_API void lua_upvaluejoin(lua_State *L, int idx1, int n1, int idx2, int n2)
862{
863 GCfunc *fn1 = funcV(index2adr(L, idx1));
864 GCfunc *fn2 = funcV(index2adr(L, idx2));
865 n1--; n2--;
866 api_check(L, isluafunc(fn1) && (uint32_t)n1 < fn1->l.nupvalues);
867 api_check(L, isluafunc(fn2) && (uint32_t)n2 < fn2->l.nupvalues);
868 setgcrefr(fn1->l.uvptr[n1], fn2->l.uvptr[n2]);
869 lj_gc_objbarrier(L, fn1, gcref(fn1->l.uvptr[n1]));
870}
871
872LUALIB_API void *luaL_checkudata(lua_State *L, int idx, const char *tname)
873{
874 cTValue *o = index2adr(L, idx);
875 if (tvisudata(o)) {
876 GCudata *ud = udataV(o);
877 cTValue *tv = lj_tab_getstr(tabV(registry(L)), lj_str_newz(L, tname));
878 if (tv && tvistab(tv) && tabV(tv) == tabref(ud->metatable))
879 return uddata(ud);
880 }
881 lj_err_argtype(L, idx, tname);
882 return NULL; /* unreachable */
883}
884
885/* -- Object setters ------------------------------------------------------ */
886
887LUA_API void lua_settable(lua_State *L, int idx)
888{
889 TValue *o;
890 cTValue *t = index2adr(L, idx);
891 api_checknelems(L, 2);
892 api_checkvalidindex(L, t);
893 o = lj_meta_tset(L, t, L->top-2);
894 if (o) {
895 /* NOBARRIER: lj_meta_tset ensures the table is not black. */
896 copyTV(L, o, L->top-1);
897 L->top -= 2;
898 } else {
899 L->top += 3;
900 copyTV(L, L->top-1, L->top-6);
901 lj_vm_call(L, L->top-3, 0+1);
902 L->top -= 3;
903 }
904}
905
906LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
907{
908 TValue *o;
909 TValue key;
910 cTValue *t = index2adr(L, idx);
911 api_checknelems(L, 1);
912 api_checkvalidindex(L, t);
913 setstrV(L, &key, lj_str_newz(L, k));
914 o = lj_meta_tset(L, t, &key);
915 if (o) {
916 L->top--;
917 /* NOBARRIER: lj_meta_tset ensures the table is not black. */
918 copyTV(L, o, L->top);
919 } else {
920 L->top += 3;
921 copyTV(L, L->top-1, L->top-6);
922 lj_vm_call(L, L->top-3, 0+1);
923 L->top -= 2;
924 }
925}
926
927LUA_API void lua_rawset(lua_State *L, int idx)
928{
929 GCtab *t = tabV(index2adr(L, idx));
930 TValue *dst, *key;
931 api_checknelems(L, 2);
932 key = L->top-2;
933 dst = lj_tab_set(L, t, key);
934 copyTV(L, dst, key+1);
935 lj_gc_anybarriert(L, t);
936 L->top = key;
937}
938
939LUA_API void lua_rawseti(lua_State *L, int idx, int n)
940{
941 GCtab *t = tabV(index2adr(L, idx));
942 TValue *dst, *src;
943 api_checknelems(L, 1);
944 dst = lj_tab_setint(L, t, n);
945 src = L->top-1;
946 copyTV(L, dst, src);
947 lj_gc_barriert(L, t, dst);
948 L->top = src;
949}
950
951LUA_API int lua_setmetatable(lua_State *L, int idx)
952{
953 global_State *g;
954 GCtab *mt;
955 cTValue *o = index2adr(L, idx);
956 api_checknelems(L, 1);
957 api_checkvalidindex(L, o);
958 if (tvisnil(L->top-1)) {
959 mt = NULL;
960 } else {
961 api_check(L, tvistab(L->top-1));
962 mt = tabV(L->top-1);
963 }
964 g = G(L);
965 if (tvistab(o)) {
966 setgcref(tabV(o)->metatable, obj2gco(mt));
967 if (mt)
968 lj_gc_objbarriert(L, tabV(o), mt);
969 } else if (tvisudata(o)) {
970 setgcref(udataV(o)->metatable, obj2gco(mt));
971 if (mt)
972 lj_gc_objbarrier(L, udataV(o), mt);
973 } else {
974 /* Flush cache, since traces specialize to basemt. But not during __gc. */
975 if (lj_trace_flushall(L))
976 lj_err_caller(L, LJ_ERR_NOGCMM);
977 if (tvisbool(o)) {
978 /* NOBARRIER: basemt is a GC root. */
979 setgcref(basemt_it(g, LJ_TTRUE), obj2gco(mt));
980 setgcref(basemt_it(g, LJ_TFALSE), obj2gco(mt));
981 } else {
982 /* NOBARRIER: basemt is a GC root. */
983 setgcref(basemt_obj(g, o), obj2gco(mt));
984 }
985 }
986 L->top--;
987 return 1;
988}
989
990LUA_API int lua_setfenv(lua_State *L, int idx)
991{
992 cTValue *o = index2adr(L, idx);
993 GCtab *t;
994 api_checknelems(L, 1);
995 api_checkvalidindex(L, o);
996 api_check(L, tvistab(L->top-1));
997 t = tabV(L->top-1);
998 if (tvisfunc(o)) {
999 setgcref(funcV(o)->c.env, obj2gco(t));
1000 } else if (tvisudata(o)) {
1001 setgcref(udataV(o)->env, obj2gco(t));
1002 } else if (tvisthread(o)) {
1003 setgcref(threadV(o)->env, obj2gco(t));
1004 } else {
1005 L->top--;
1006 return 0;
1007 }
1008 lj_gc_objbarrier(L, gcV(o), t);
1009 L->top--;
1010 return 1;
1011}
1012
1013LUA_API const char *lua_setupvalue(lua_State *L, int idx, int n)
1014{
1015 cTValue *f = index2adr(L, idx);
1016 TValue *val;
1017 const char *name;
1018 api_checknelems(L, 1);
1019 name = lj_debug_uvnamev(f, (uint32_t)(n-1), &val);
1020 if (name) {
1021 L->top--;
1022 copyTV(L, val, L->top);
1023 lj_gc_barrier(L, funcV(f), L->top);
1024 }
1025 return name;
1026}
1027
1028/* -- Calls --------------------------------------------------------------- */
1029
1030LUA_API void lua_call(lua_State *L, int nargs, int nresults)
1031{
1032 api_check(L, L->status == 0 || L->status == LUA_ERRERR);
1033 api_checknelems(L, nargs+1);
1034 lj_vm_call(L, L->top - nargs, nresults+1);
1035}
1036
1037LUA_API int lua_pcall(lua_State *L, int nargs, int nresults, int errfunc)
1038{
1039 global_State *g = G(L);
1040 uint8_t oldh = hook_save(g);
1041 ptrdiff_t ef;
1042 int status;
1043 api_check(L, L->status == 0 || L->status == LUA_ERRERR);
1044 api_checknelems(L, nargs+1);
1045 if (errfunc == 0) {
1046 ef = 0;
1047 } else {
1048 cTValue *o = stkindex2adr(L, errfunc);
1049 api_checkvalidindex(L, o);
1050 ef = savestack(L, o);
1051 }
1052 status = lj_vm_pcall(L, L->top - nargs, nresults+1, ef);
1053 if (status) hook_restore(g, oldh);
1054 return status;
1055}
1056
1057static TValue *cpcall(lua_State *L, lua_CFunction func, void *ud)
1058{
1059 GCfunc *fn = lj_func_newC(L, 0, getcurrenv(L));
1060 fn->c.f = func;
1061 setfuncV(L, L->top, fn);
1062 setlightudV(L->top+1, checklightudptr(L, ud));
1063 cframe_nres(L->cframe) = 1+0; /* Zero results. */
1064 L->top += 2;
1065 return L->top-1; /* Now call the newly allocated C function. */
1066}
1067
1068LUA_API int lua_cpcall(lua_State *L, lua_CFunction func, void *ud)
1069{
1070 global_State *g = G(L);
1071 uint8_t oldh = hook_save(g);
1072 int status;
1073 api_check(L, L->status == 0 || L->status == LUA_ERRERR);
1074 status = lj_vm_cpcall(L, func, ud, cpcall);
1075 if (status) hook_restore(g, oldh);
1076 return status;
1077}
1078
1079LUALIB_API int luaL_callmeta(lua_State *L, int idx, const char *field)
1080{
1081 if (luaL_getmetafield(L, idx, field)) {
1082 TValue *base = L->top--;
1083 copyTV(L, base, index2adr(L, idx));
1084 L->top = base+1;
1085 lj_vm_call(L, base, 1+1);
1086 return 1;
1087 }
1088 return 0;
1089}
1090
1091/* -- Coroutine yield and resume ------------------------------------------ */
1092
1093LUA_API int lua_yield(lua_State *L, int nresults)
1094{
1095 void *cf = L->cframe;
1096 global_State *g = G(L);
1097 if (cframe_canyield(cf)) {
1098 cf = cframe_raw(cf);
1099 if (!hook_active(g)) { /* Regular yield: move results down if needed. */
1100 cTValue *f = L->top - nresults;
1101 if (f > L->base) {
1102 TValue *t = L->base;
1103 while (--nresults >= 0) copyTV(L, t++, f++);
1104 L->top = t;
1105 }
1106 L->cframe = NULL;
1107 L->status = LUA_YIELD;
1108 return -1;
1109 } else { /* Yield from hook: add a pseudo-frame. */
1110 TValue *top = L->top;
1111 hook_leave(g);
1112 top->u64 = cframe_multres(cf);
1113 setcont(top+1, lj_cont_hook);
1114 setframe_pc(top+1, cframe_pc(cf)-1);
1115 setframe_gc(top+2, obj2gco(L));
1116 setframe_ftsz(top+2, (int)((char *)(top+3)-(char *)L->base)+FRAME_CONT);
1117 L->top = L->base = top+3;
1118#if LJ_TARGET_X64
1119 lj_err_throw(L, LUA_YIELD);
1120#else
1121 L->cframe = NULL;
1122 L->status = LUA_YIELD;
1123 lj_vm_unwind_c(cf, LUA_YIELD);
1124#endif
1125 }
1126 }
1127 lj_err_msg(L, LJ_ERR_CYIELD);
1128 return 0; /* unreachable */
1129}
1130
1131LUA_API int lua_resume(lua_State *L, int nargs)
1132{
1133 if (L->cframe == NULL && L->status <= LUA_YIELD)
1134 return lj_vm_resume(L, L->top - nargs, 0, 0);
1135 L->top = L->base;
1136 setstrV(L, L->top, lj_err_str(L, LJ_ERR_COSUSP));
1137 incr_top(L);
1138 return LUA_ERRRUN;
1139}
1140
1141/* -- GC and memory management -------------------------------------------- */
1142
1143LUA_API int lua_gc(lua_State *L, int what, int data)
1144{
1145 global_State *g = G(L);
1146 int res = 0;
1147 switch (what) {
1148 case LUA_GCSTOP:
1149 g->gc.threshold = LJ_MAX_MEM;
1150 break;
1151 case LUA_GCRESTART:
1152 g->gc.threshold = data == -1 ? (g->gc.total/100)*g->gc.pause : g->gc.total;
1153 break;
1154 case LUA_GCCOLLECT:
1155 lj_gc_fullgc(L);
1156 break;
1157 case LUA_GCCOUNT:
1158 res = (int)(g->gc.total >> 10);
1159 break;
1160 case LUA_GCCOUNTB:
1161 res = (int)(g->gc.total & 0x3ff);
1162 break;
1163 case LUA_GCSTEP: {
1164 MSize a = (MSize)data << 10;
1165 g->gc.threshold = (a <= g->gc.total) ? (g->gc.total - a) : 0;
1166 while (g->gc.total >= g->gc.threshold)
1167 if (lj_gc_step(L) > 0) {
1168 res = 1;
1169 break;
1170 }
1171 break;
1172 }
1173 case LUA_GCSETPAUSE:
1174 res = (int)(g->gc.pause);
1175 g->gc.pause = (MSize)data;
1176 break;
1177 case LUA_GCSETSTEPMUL:
1178 res = (int)(g->gc.stepmul);
1179 g->gc.stepmul = (MSize)data;
1180 break;
1181 default:
1182 res = -1; /* Invalid option. */
1183 }
1184 return res;
1185}
1186
1187LUA_API lua_Alloc lua_getallocf(lua_State *L, void **ud)
1188{
1189 global_State *g = G(L);
1190 if (ud) *ud = g->allocd;
1191 return g->allocf;
1192}
1193
1194LUA_API void lua_setallocf(lua_State *L, lua_Alloc f, void *ud)
1195{
1196 global_State *g = G(L);
1197 g->allocd = ud;
1198 g->allocf = f;
1199}
1200
1201