ARM: Add partial support for FFI.
[luajit-2.0.git] / src / lj_api.c
blob3a8448b525c03f1bf53bf253056a80e08bba47e4
1 /*
2 ** Public Lua/C API.
3 ** Copyright (C) 2005-2011 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 */
9 #define lj_api_c
10 #define LUA_CORE
12 #include "lj_obj.h"
13 #include "lj_gc.h"
14 #include "lj_err.h"
15 #include "lj_str.h"
16 #include "lj_tab.h"
17 #include "lj_func.h"
18 #include "lj_udata.h"
19 #include "lj_meta.h"
20 #include "lj_state.h"
21 #include "lj_bc.h"
22 #include "lj_frame.h"
23 #include "lj_trace.h"
24 #include "lj_vm.h"
25 #include "lj_lex.h"
26 #include "lj_parse.h"
28 /* -- Common helper functions --------------------------------------------- */
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))
33 static TValue *index2adr(lua_State *L, int idx)
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);
61 static TValue *stkindex2adr(lua_State *L, int idx)
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;
72 static GCtab *getcurrenv(lua_State *L)
74 GCfunc *fn = curr_func(L);
75 return fn->c.gct == ~LJ_TFUNC ? tabref(fn->c.env) : tabref(L->env);
78 /* -- Miscellaneous API functions ----------------------------------------- */
80 LUA_API int lua_status(lua_State *L)
82 return L->status;
85 LUA_API int lua_checkstack(lua_State *L, int size)
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);
92 return 1;
95 LUALIB_API void luaL_checkstack(lua_State *L, int size, const char *msg)
97 if (!lua_checkstack(L, size))
98 lj_err_callerv(L, LJ_ERR_STKOVM, msg);
101 LUA_API void lua_xmove(lua_State *from, lua_State *to, int n)
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;
114 /* -- Stack manipulation -------------------------------------------------- */
116 LUA_API int lua_gettop(lua_State *L)
118 return (int)(L->top - L->base);
121 LUA_API void lua_settop(lua_State *L, int idx)
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;
132 } else {
133 api_check(L, -(idx+1) <= (L->top - L->base));
134 L->top += idx+1; /* Shrinks top (idx < 0). */
138 LUA_API void lua_remove(lua_State *L, int idx)
140 TValue *p = stkindex2adr(L, idx);
141 api_checkvalidindex(L, p);
142 while (++p < L->top) copyTV(L, p-1, p);
143 L->top--;
146 LUA_API void lua_insert(lua_State *L, int idx)
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);
154 LUA_API void lua_replace(lua_State *L, int idx)
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);
175 L->top--;
178 LUA_API void lua_pushvalue(lua_State *L, int idx)
180 copyTV(L, L->top, index2adr(L, idx));
181 incr_top(L);
184 /* -- Stack getters ------------------------------------------------------- */
186 LUA_API int lua_type(lua_State *L, int idx)
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;
209 LUALIB_API void luaL_checktype(lua_State *L, int idx, int tt)
211 if (lua_type(L, idx) != tt)
212 lj_err_argt(L, idx, tt);
215 LUALIB_API void luaL_checkany(lua_State *L, int idx)
217 if (index2adr(L, idx) == niltv(L))
218 lj_err_arg(L, idx, LJ_ERR_NOVAL);
221 LUA_API const char *lua_typename(lua_State *L, int t)
223 UNUSED(L);
224 return lj_obj_typename[t+1];
227 LUA_API int lua_iscfunction(lua_State *L, int idx)
229 cTValue *o = index2adr(L, idx);
230 return tvisfunc(o) && !isluafunc(funcV(o));
233 LUA_API int lua_isnumber(lua_State *L, int idx)
235 cTValue *o = index2adr(L, idx);
236 TValue tmp;
237 return (tvisnumber(o) || (tvisstr(o) && lj_str_tonumber(strV(o), &tmp)));
240 LUA_API int lua_isstring(lua_State *L, int idx)
242 cTValue *o = index2adr(L, idx);
243 return (tvisstr(o) || tvisnumber(o));
246 LUA_API int lua_isuserdata(lua_State *L, int idx)
248 cTValue *o = index2adr(L, idx);
249 return (tvisudata(o) || tvislightud(o));
252 LUA_API int lua_rawequal(lua_State *L, int idx1, int idx2)
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);
259 LUA_API int lua_equal(lua_State *L, int idx1, int idx2)
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);
292 LUA_API int lua_lessthan(lua_State *L, int idx1, int idx2)
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);
315 LUA_API lua_Number lua_tonumber(lua_State *L, int idx)
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_str_tonum(strV(o), &tmp))
322 return numV(&tmp);
323 else
324 return 0;
327 LUALIB_API lua_Number luaL_checknumber(lua_State *L, int idx)
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_str_tonum(strV(o), &tmp)))
334 lj_err_argt(L, idx, LUA_TNUMBER);
335 return numV(&tmp);
338 LUALIB_API lua_Number luaL_optnumber(lua_State *L, int idx, lua_Number def)
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_str_tonum(strV(o), &tmp)))
347 lj_err_argt(L, idx, LUA_TNUMBER);
348 return numV(&tmp);
351 LUA_API lua_Integer lua_tointeger(lua_State *L, int idx)
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_str_tonumber(strV(o), &tmp)))
362 return 0;
363 if (tvisint(&tmp))
364 return (lua_Integer)intV(&tmp);
365 n = numV(&tmp);
367 #if LJ_64
368 return (lua_Integer)n;
369 #else
370 return lj_num2int(n);
371 #endif
374 LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int idx)
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_str_tonumber(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);
390 #if LJ_64
391 return (lua_Integer)n;
392 #else
393 return lj_num2int(n);
394 #endif
397 LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int idx, lua_Integer def)
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_str_tonumber(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);
415 #if LJ_64
416 return (lua_Integer)n;
417 #else
418 return lj_num2int(n);
419 #endif
422 LUA_API int lua_toboolean(lua_State *L, int idx)
424 cTValue *o = index2adr(L, idx);
425 return tvistruecond(o);
428 LUA_API const char *lua_tolstring(lua_State *L, int idx, size_t *len)
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 } else {
439 if (len != NULL) *len = 0;
440 return NULL;
442 if (len != NULL) *len = s->len;
443 return strdata(s);
446 LUALIB_API const char *luaL_checklstring(lua_State *L, int idx, size_t *len)
448 TValue *o = index2adr(L, idx);
449 GCstr *s;
450 if (LJ_LIKELY(tvisstr(o))) {
451 s = strV(o);
452 } else if (tvisnumber(o)) {
453 lj_gc_check(L);
454 o = index2adr(L, idx); /* GC may move the stack. */
455 s = lj_str_fromnumber(L, o);
456 } else {
457 lj_err_argt(L, idx, LUA_TSTRING);
459 if (len != NULL) *len = s->len;
460 return strdata(s);
463 LUALIB_API const char *luaL_optlstring(lua_State *L, int idx,
464 const char *def, size_t *len)
466 TValue *o = index2adr(L, idx);
467 GCstr *s;
468 if (LJ_LIKELY(tvisstr(o))) {
469 s = strV(o);
470 } else if (tvisnil(o)) {
471 if (len != NULL) *len = def ? strlen(def) : 0;
472 return def;
473 } else if (tvisnumber(o)) {
474 lj_gc_check(L);
475 o = index2adr(L, idx); /* GC may move the stack. */
476 s = lj_str_fromnumber(L, o);
477 } else {
478 lj_err_argt(L, idx, LUA_TSTRING);
480 if (len != NULL) *len = s->len;
481 return strdata(s);
484 LUALIB_API int luaL_checkoption(lua_State *L, int idx, const char *def,
485 const char *const lst[])
487 ptrdiff_t i;
488 const char *s = lua_tolstring(L, idx, NULL);
489 if (s == NULL && (s = def) == NULL)
490 lj_err_argt(L, idx, LUA_TSTRING);
491 for (i = 0; lst[i]; i++)
492 if (strcmp(lst[i], s) == 0)
493 return (int)i;
494 lj_err_argv(L, idx, LJ_ERR_INVOPTM, s);
497 LUA_API size_t lua_objlen(lua_State *L, int idx)
499 TValue *o = index2adr(L, idx);
500 if (tvisstr(o))
501 return strV(o)->len;
502 else if (tvistab(o))
503 return (size_t)lj_tab_len(tabV(o));
504 else if (tvisudata(o))
505 return udataV(o)->len;
506 else if (tvisnumber(o))
507 return lj_str_fromnumber(L, o)->len;
508 else
509 return 0;
512 LUA_API lua_CFunction lua_tocfunction(lua_State *L, int idx)
514 cTValue *o = index2adr(L, idx);
515 if (tvisfunc(o)) {
516 BCOp op = bc_op(*mref(funcV(o)->c.pc, BCIns));
517 if (op == BC_FUNCC || op == BC_FUNCCW)
518 return funcV(o)->c.f;
520 return NULL;
523 LUA_API void *lua_touserdata(lua_State *L, int idx)
525 cTValue *o = index2adr(L, idx);
526 if (tvisudata(o))
527 return uddata(udataV(o));
528 else if (tvislightud(o))
529 return lightudV(o);
530 else
531 return NULL;
534 LUA_API lua_State *lua_tothread(lua_State *L, int idx)
536 cTValue *o = index2adr(L, idx);
537 return (!tvisthread(o)) ? NULL : threadV(o);
540 LUA_API const void *lua_topointer(lua_State *L, int idx)
542 cTValue *o = index2adr(L, idx);
543 if (tvisudata(o))
544 return uddata(udataV(o));
545 else if (tvislightud(o))
546 return lightudV(o);
547 else if (tviscdata(o))
548 return cdataptr(cdataV(o));
549 else if (tvisgcv(o))
550 return gcV(o);
551 else
552 return NULL;
555 /* -- Stack setters (object creation) ------------------------------------- */
557 LUA_API void lua_pushnil(lua_State *L)
559 setnilV(L->top);
560 incr_top(L);
563 LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
565 setnumV(L->top, n);
566 if (LJ_UNLIKELY(tvisnan(L->top)))
567 setnanV(L->top); /* Canonicalize injected NaNs. */
568 incr_top(L);
571 LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
573 setintptrV(L->top, n);
574 incr_top(L);
577 LUA_API void lua_pushlstring(lua_State *L, const char *str, size_t len)
579 GCstr *s;
580 lj_gc_check(L);
581 s = lj_str_new(L, str, len);
582 setstrV(L, L->top, s);
583 incr_top(L);
586 LUA_API void lua_pushstring(lua_State *L, const char *str)
588 if (str == NULL) {
589 setnilV(L->top);
590 } else {
591 GCstr *s;
592 lj_gc_check(L);
593 s = lj_str_newz(L, str);
594 setstrV(L, L->top, s);
596 incr_top(L);
599 LUA_API const char *lua_pushvfstring(lua_State *L, const char *fmt,
600 va_list argp)
602 lj_gc_check(L);
603 return lj_str_pushvf(L, fmt, argp);
606 LUA_API const char *lua_pushfstring(lua_State *L, const char *fmt, ...)
608 const char *ret;
609 va_list argp;
610 lj_gc_check(L);
611 va_start(argp, fmt);
612 ret = lj_str_pushvf(L, fmt, argp);
613 va_end(argp);
614 return ret;
617 LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction f, int n)
619 GCfunc *fn;
620 lj_gc_check(L);
621 api_checknelems(L, n);
622 fn = lj_func_newC(L, (MSize)n, getcurrenv(L));
623 fn->c.f = f;
624 L->top -= n;
625 while (n--)
626 copyTV(L, &fn->c.upvalue[n], L->top+n);
627 setfuncV(L, L->top, fn);
628 lua_assert(iswhite(obj2gco(fn)));
629 incr_top(L);
632 LUA_API void lua_pushboolean(lua_State *L, int b)
634 setboolV(L->top, (b != 0));
635 incr_top(L);
638 LUA_API void lua_pushlightuserdata(lua_State *L, void *p)
640 setlightudV(L->top, checklightudptr(L, p));
641 incr_top(L);
644 LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
646 GCtab *t;
647 lj_gc_check(L);
648 t = lj_tab_new(L, (uint32_t)(narray > 0 ? narray+1 : 0), hsize2hbits(nrec));
649 settabV(L, L->top, t);
650 incr_top(L);
653 LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname)
655 GCtab *regt = tabV(registry(L));
656 TValue *tv = lj_tab_setstr(L, regt, lj_str_newz(L, tname));
657 if (tvisnil(tv)) {
658 GCtab *mt = lj_tab_new(L, 0, 1);
659 settabV(L, tv, mt);
660 settabV(L, L->top++, mt);
661 lj_gc_anybarriert(L, regt);
662 return 1;
663 } else {
664 copyTV(L, L->top++, tv);
665 return 0;
669 LUA_API int lua_pushthread(lua_State *L)
671 setthreadV(L, L->top, L);
672 incr_top(L);
673 return (mainthread(G(L)) == L);
676 LUA_API lua_State *lua_newthread(lua_State *L)
678 lua_State *L1;
679 lj_gc_check(L);
680 L1 = lj_state_new(L);
681 setthreadV(L, L->top, L1);
682 incr_top(L);
683 return L1;
686 LUA_API void *lua_newuserdata(lua_State *L, size_t size)
688 GCudata *ud;
689 lj_gc_check(L);
690 if (size > LJ_MAX_UDATA)
691 lj_err_msg(L, LJ_ERR_UDATAOV);
692 ud = lj_udata_new(L, (MSize)size, getcurrenv(L));
693 setudataV(L, L->top, ud);
694 incr_top(L);
695 return uddata(ud);
698 LUA_API void lua_concat(lua_State *L, int n)
700 api_checknelems(L, n);
701 if (n >= 2) {
702 n--;
703 do {
704 TValue *top = lj_meta_cat(L, L->top-1, n);
705 if (top == NULL) {
706 L->top -= n;
707 break;
709 n -= (int)(L->top - top);
710 L->top = top+2;
711 lj_vm_call(L, top, 1+1);
712 L->top--;
713 copyTV(L, L->top-1, L->top);
714 } while (--n > 0);
715 } else if (n == 0) { /* Push empty string. */
716 setstrV(L, L->top, &G(L)->strempty);
717 incr_top(L);
719 /* else n == 1: nothing to do. */
722 /* -- Object getters ------------------------------------------------------ */
724 LUA_API void lua_gettable(lua_State *L, int idx)
726 cTValue *v, *t = index2adr(L, idx);
727 api_checkvalidindex(L, t);
728 v = lj_meta_tget(L, t, L->top-1);
729 if (v == NULL) {
730 L->top += 2;
731 lj_vm_call(L, L->top-2, 1+1);
732 L->top -= 2;
733 v = L->top+1;
735 copyTV(L, L->top-1, v);
738 LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
740 cTValue *v, *t = index2adr(L, idx);
741 TValue key;
742 api_checkvalidindex(L, t);
743 setstrV(L, &key, lj_str_newz(L, k));
744 v = lj_meta_tget(L, t, &key);
745 if (v == NULL) {
746 L->top += 2;
747 lj_vm_call(L, L->top-2, 1+1);
748 L->top -= 2;
749 v = L->top+1;
751 copyTV(L, L->top, v);
752 incr_top(L);
755 LUA_API void lua_rawget(lua_State *L, int idx)
757 cTValue *t = index2adr(L, idx);
758 api_check(L, tvistab(t));
759 copyTV(L, L->top-1, lj_tab_get(L, tabV(t), L->top-1));
762 LUA_API void lua_rawgeti(lua_State *L, int idx, int n)
764 cTValue *v, *t = index2adr(L, idx);
765 api_check(L, tvistab(t));
766 v = lj_tab_getint(tabV(t), n);
767 if (v) {
768 copyTV(L, L->top, v);
769 } else {
770 setnilV(L->top);
772 incr_top(L);
775 LUA_API int lua_getmetatable(lua_State *L, int idx)
777 cTValue *o = index2adr(L, idx);
778 GCtab *mt = NULL;
779 if (tvistab(o))
780 mt = tabref(tabV(o)->metatable);
781 else if (tvisudata(o))
782 mt = tabref(udataV(o)->metatable);
783 else
784 mt = tabref(basemt_obj(G(L), o));
785 if (mt == NULL)
786 return 0;
787 settabV(L, L->top, mt);
788 incr_top(L);
789 return 1;
792 LUALIB_API int luaL_getmetafield(lua_State *L, int idx, const char *field)
794 if (lua_getmetatable(L, idx)) {
795 cTValue *tv = lj_tab_getstr(tabV(L->top-1), lj_str_newz(L, field));
796 if (tv && !tvisnil(tv)) {
797 copyTV(L, L->top-1, tv);
798 return 1;
800 L->top--;
802 return 0;
805 LUA_API void lua_getfenv(lua_State *L, int idx)
807 cTValue *o = index2adr(L, idx);
808 api_checkvalidindex(L, o);
809 if (tvisfunc(o)) {
810 settabV(L, L->top, tabref(funcV(o)->c.env));
811 } else if (tvisudata(o)) {
812 settabV(L, L->top, tabref(udataV(o)->env));
813 } else if (tvisthread(o)) {
814 settabV(L, L->top, tabref(threadV(o)->env));
815 } else {
816 setnilV(L->top);
818 incr_top(L);
821 LUA_API int lua_next(lua_State *L, int idx)
823 cTValue *t = index2adr(L, idx);
824 int more;
825 api_check(L, tvistab(t));
826 more = lj_tab_next(L, tabV(t), L->top-1);
827 if (more) {
828 incr_top(L); /* Return new key and value slot. */
829 } else { /* End of traversal. */
830 L->top--; /* Remove key slot. */
832 return more;
835 static const char *aux_upvalue(cTValue *f, uint32_t idx, TValue **val)
837 GCfunc *fn;
838 if (!tvisfunc(f)) return NULL;
839 fn = funcV(f);
840 if (isluafunc(fn)) {
841 GCproto *pt = funcproto(fn);
842 if (idx < pt->sizeuv) {
843 *val = uvval(&gcref(fn->l.uvptr[idx])->uv);
844 return strdata(proto_uvname(pt, idx));
846 } else {
847 if (idx < fn->c.nupvalues) {
848 *val = &fn->c.upvalue[idx];
849 return "";
852 return NULL;
855 LUA_API const char *lua_getupvalue(lua_State *L, int idx, int n)
857 TValue *val;
858 const char *name = aux_upvalue(index2adr(L, idx), (uint32_t)(n-1), &val);
859 if (name) {
860 copyTV(L, L->top, val);
861 incr_top(L);
863 return name;
866 LUALIB_API void *luaL_checkudata(lua_State *L, int idx, const char *tname)
868 cTValue *o = index2adr(L, idx);
869 if (tvisudata(o)) {
870 GCudata *ud = udataV(o);
871 cTValue *tv = lj_tab_getstr(tabV(registry(L)), lj_str_newz(L, tname));
872 if (tv && tvistab(tv) && tabV(tv) == tabref(ud->metatable))
873 return uddata(ud);
875 lj_err_argtype(L, idx, tname);
876 return NULL; /* unreachable */
879 /* -- Object setters ------------------------------------------------------ */
881 LUA_API void lua_settable(lua_State *L, int idx)
883 TValue *o;
884 cTValue *t = index2adr(L, idx);
885 api_checknelems(L, 2);
886 api_checkvalidindex(L, t);
887 o = lj_meta_tset(L, t, L->top-2);
888 if (o) {
889 /* NOBARRIER: lj_meta_tset ensures the table is not black. */
890 copyTV(L, o, L->top-1);
891 L->top -= 2;
892 } else {
893 L->top += 3;
894 copyTV(L, L->top-1, L->top-6);
895 lj_vm_call(L, L->top-3, 0+1);
896 L->top -= 3;
900 LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
902 TValue *o;
903 TValue key;
904 cTValue *t = index2adr(L, idx);
905 api_checknelems(L, 1);
906 api_checkvalidindex(L, t);
907 setstrV(L, &key, lj_str_newz(L, k));
908 o = lj_meta_tset(L, t, &key);
909 if (o) {
910 L->top--;
911 /* NOBARRIER: lj_meta_tset ensures the table is not black. */
912 copyTV(L, o, L->top);
913 } else {
914 L->top += 3;
915 copyTV(L, L->top-1, L->top-6);
916 lj_vm_call(L, L->top-3, 0+1);
917 L->top -= 2;
921 LUA_API void lua_rawset(lua_State *L, int idx)
923 GCtab *t = tabV(index2adr(L, idx));
924 TValue *dst, *key;
925 api_checknelems(L, 2);
926 key = L->top-2;
927 dst = lj_tab_set(L, t, key);
928 copyTV(L, dst, key+1);
929 lj_gc_anybarriert(L, t);
930 L->top = key;
933 LUA_API void lua_rawseti(lua_State *L, int idx, int n)
935 GCtab *t = tabV(index2adr(L, idx));
936 TValue *dst, *src;
937 api_checknelems(L, 1);
938 dst = lj_tab_setint(L, t, n);
939 src = L->top-1;
940 copyTV(L, dst, src);
941 lj_gc_barriert(L, t, dst);
942 L->top = src;
945 LUA_API int lua_setmetatable(lua_State *L, int idx)
947 global_State *g;
948 GCtab *mt;
949 cTValue *o = index2adr(L, idx);
950 api_checknelems(L, 1);
951 api_checkvalidindex(L, o);
952 if (tvisnil(L->top-1)) {
953 mt = NULL;
954 } else {
955 api_check(L, tvistab(L->top-1));
956 mt = tabV(L->top-1);
958 g = G(L);
959 if (tvistab(o)) {
960 setgcref(tabV(o)->metatable, obj2gco(mt));
961 if (mt)
962 lj_gc_objbarriert(L, tabV(o), mt);
963 } else if (tvisudata(o)) {
964 setgcref(udataV(o)->metatable, obj2gco(mt));
965 if (mt)
966 lj_gc_objbarrier(L, udataV(o), mt);
967 } else {
968 /* Flush cache, since traces specialize to basemt. But not during __gc. */
969 if (lj_trace_flushall(L))
970 lj_err_caller(L, LJ_ERR_NOGCMM);
971 if (tvisbool(o)) {
972 /* NOBARRIER: basemt is a GC root. */
973 setgcref(basemt_it(g, LJ_TTRUE), obj2gco(mt));
974 setgcref(basemt_it(g, LJ_TFALSE), obj2gco(mt));
975 } else {
976 /* NOBARRIER: basemt is a GC root. */
977 setgcref(basemt_obj(g, o), obj2gco(mt));
980 L->top--;
981 return 1;
984 LUA_API int lua_setfenv(lua_State *L, int idx)
986 cTValue *o = index2adr(L, idx);
987 GCtab *t;
988 api_checknelems(L, 1);
989 api_checkvalidindex(L, o);
990 api_check(L, tvistab(L->top-1));
991 t = tabV(L->top-1);
992 if (tvisfunc(o)) {
993 setgcref(funcV(o)->c.env, obj2gco(t));
994 } else if (tvisudata(o)) {
995 setgcref(udataV(o)->env, obj2gco(t));
996 } else if (tvisthread(o)) {
997 setgcref(threadV(o)->env, obj2gco(t));
998 } else {
999 L->top--;
1000 return 0;
1002 lj_gc_objbarrier(L, gcV(o), t);
1003 L->top--;
1004 return 1;
1007 LUA_API const char *lua_setupvalue(lua_State *L, int idx, int n)
1009 cTValue *f = index2adr(L, idx);
1010 TValue *val;
1011 const char *name;
1012 api_checknelems(L, 1);
1013 name = aux_upvalue(f, (uint32_t)(n-1), &val);
1014 if (name) {
1015 L->top--;
1016 copyTV(L, val, L->top);
1017 lj_gc_barrier(L, funcV(f), L->top);
1019 return name;
1022 /* -- Calls --------------------------------------------------------------- */
1024 LUA_API void lua_call(lua_State *L, int nargs, int nresults)
1026 api_check(L, L->status == 0 || L->status == LUA_ERRERR);
1027 api_checknelems(L, nargs+1);
1028 lj_vm_call(L, L->top - nargs, nresults+1);
1031 LUA_API int lua_pcall(lua_State *L, int nargs, int nresults, int errfunc)
1033 global_State *g = G(L);
1034 uint8_t oldh = hook_save(g);
1035 ptrdiff_t ef;
1036 int status;
1037 api_check(L, L->status == 0 || L->status == LUA_ERRERR);
1038 api_checknelems(L, nargs+1);
1039 if (errfunc == 0) {
1040 ef = 0;
1041 } else {
1042 cTValue *o = stkindex2adr(L, errfunc);
1043 api_checkvalidindex(L, o);
1044 ef = savestack(L, o);
1046 status = lj_vm_pcall(L, L->top - nargs, nresults+1, ef);
1047 if (status) hook_restore(g, oldh);
1048 return status;
1051 static TValue *cpcall(lua_State *L, lua_CFunction func, void *ud)
1053 GCfunc *fn = lj_func_newC(L, 0, getcurrenv(L));
1054 fn->c.f = func;
1055 setfuncV(L, L->top, fn);
1056 setlightudV(L->top+1, checklightudptr(L, ud));
1057 cframe_nres(L->cframe) = 1+0; /* Zero results. */
1058 L->top += 2;
1059 return L->top-1; /* Now call the newly allocated C function. */
1062 LUA_API int lua_cpcall(lua_State *L, lua_CFunction func, void *ud)
1064 global_State *g = G(L);
1065 uint8_t oldh = hook_save(g);
1066 int status;
1067 api_check(L, L->status == 0 || L->status == LUA_ERRERR);
1068 status = lj_vm_cpcall(L, func, ud, cpcall);
1069 if (status) hook_restore(g, oldh);
1070 return status;
1073 LUALIB_API int luaL_callmeta(lua_State *L, int idx, const char *field)
1075 if (luaL_getmetafield(L, idx, field)) {
1076 TValue *base = L->top--;
1077 copyTV(L, base, index2adr(L, idx));
1078 L->top = base+1;
1079 lj_vm_call(L, base, 1+1);
1080 return 1;
1082 return 0;
1085 /* -- Coroutine yield and resume ------------------------------------------ */
1087 LUA_API int lua_yield(lua_State *L, int nresults)
1089 void *cf = L->cframe;
1090 global_State *g = G(L);
1091 if (cframe_canyield(cf)) {
1092 cf = cframe_raw(cf);
1093 if (!hook_active(g)) { /* Regular yield: move results down if needed. */
1094 cTValue *f = L->top - nresults;
1095 if (f > L->base) {
1096 TValue *t = L->base;
1097 while (--nresults >= 0) copyTV(L, t++, f++);
1098 L->top = t;
1100 } else { /* Yield from hook: add a pseudo-frame. */
1101 TValue *top = L->top;
1102 hook_leave(g);
1103 top->u64 = cframe_multres(cf);
1104 setcont(top+1, lj_cont_hook);
1105 setframe_pc(top+1, cframe_pc(cf)-1);
1106 setframe_gc(top+2, obj2gco(L));
1107 setframe_ftsz(top+2, (int)((char *)(top+3)-(char *)L->base)+FRAME_CONT);
1108 L->top = L->base = top+3;
1110 #if LJ_TARGET_X64
1111 lj_err_throw(L, LUA_YIELD);
1112 #else
1113 L->cframe = NULL;
1114 L->status = LUA_YIELD;
1115 lj_vm_unwind_c(cf, LUA_YIELD);
1116 #endif
1118 lj_err_msg(L, LJ_ERR_CYIELD);
1119 return 0; /* unreachable */
1122 LUA_API int lua_resume(lua_State *L, int nargs)
1124 if (L->cframe == NULL && L->status <= LUA_YIELD)
1125 return lj_vm_resume(L, L->top - nargs, 0, 0);
1126 L->top = L->base;
1127 setstrV(L, L->top, lj_err_str(L, LJ_ERR_COSUSP));
1128 incr_top(L);
1129 return LUA_ERRRUN;
1132 /* -- Load and dump Lua code ---------------------------------------------- */
1134 static TValue *cpparser(lua_State *L, lua_CFunction dummy, void *ud)
1136 LexState *ls = (LexState *)ud;
1137 GCfunc *fn;
1138 UNUSED(dummy);
1139 cframe_errfunc(L->cframe) = -1; /* Inherit error function. */
1140 lj_lex_setup(L, ls);
1141 fn = lj_func_newL(L, lj_parse(ls), tabref(L->env));
1142 /* Parser may realloc stack. Don't combine above/below into one statement. */
1143 setfuncV(L, L->top++, fn);
1144 return NULL;
1147 LUA_API int lua_load(lua_State *L, lua_Reader reader, void *data,
1148 const char *chunkname)
1150 LexState ls;
1151 int status;
1152 ls.rfunc = reader;
1153 ls.rdata = data;
1154 ls.chunkarg = chunkname ? chunkname : "?";
1155 lj_str_initbuf(L, &ls.sb);
1156 status = lj_vm_cpcall(L, NULL, &ls, cpparser);
1157 lj_lex_cleanup(L, &ls);
1158 lj_gc_check(L);
1159 return status;
1162 LUA_API int lua_dump(lua_State *L, lua_Writer writer, void *data)
1164 api_checknelems(L, 1);
1165 UNUSED(L); UNUSED(writer); UNUSED(data);
1166 return 1; /* Error, not supported. */
1169 /* -- GC and memory management -------------------------------------------- */
1171 LUA_API int lua_gc(lua_State *L, int what, int data)
1173 global_State *g = G(L);
1174 int res = 0;
1175 switch (what) {
1176 case LUA_GCSTOP:
1177 g->gc.threshold = LJ_MAX_MEM;
1178 break;
1179 case LUA_GCRESTART:
1180 g->gc.threshold = data == -1 ? (g->gc.total/100)*g->gc.pause : g->gc.total;
1181 break;
1182 case LUA_GCCOLLECT:
1183 lj_gc_fullgc(L);
1184 break;
1185 case LUA_GCCOUNT:
1186 res = (int)(g->gc.total >> 10);
1187 break;
1188 case LUA_GCCOUNTB:
1189 res = (int)(g->gc.total & 0x3ff);
1190 break;
1191 case LUA_GCSTEP: {
1192 MSize a = (MSize)data << 10;
1193 g->gc.threshold = (a <= g->gc.total) ? (g->gc.total - a) : 0;
1194 while (g->gc.total >= g->gc.threshold)
1195 if (lj_gc_step(L)) {
1196 res = 1;
1197 break;
1199 break;
1201 case LUA_GCSETPAUSE:
1202 res = (int)(g->gc.pause);
1203 g->gc.pause = (MSize)data;
1204 break;
1205 case LUA_GCSETSTEPMUL:
1206 res = (int)(g->gc.stepmul);
1207 g->gc.stepmul = (MSize)data;
1208 break;
1209 default:
1210 res = -1; /* Invalid option. */
1212 return res;
1215 LUA_API lua_Alloc lua_getallocf(lua_State *L, void **ud)
1217 global_State *g = G(L);
1218 if (ud) *ud = g->allocd;
1219 return g->allocf;
1222 LUA_API void lua_setallocf(lua_State *L, lua_Alloc f, void *ud)
1224 global_State *g = G(L);
1225 g->allocd = ud;
1226 g->allocf = f;