PPC: Untangle PPC vs. PPCSPE target defines.
[luajit-2.0.git] / src / lj_api.c
bloba6fbb1c6aaf05862a7acec886feffb9185b827af
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_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_lex.h"
27 #include "lj_bcdump.h"
28 #include "lj_parse.h"
30 /* -- Common helper functions --------------------------------------------- */
32 #define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base))
33 #define api_checkvalidindex(L, i) api_check(L, (i) != niltv(L))
35 static TValue *index2adr(lua_State *L, int idx)
37 if (idx > 0) {
38 TValue *o = L->base + (idx - 1);
39 return o < L->top ? o : niltv(L);
40 } else if (idx > LUA_REGISTRYINDEX) {
41 api_check(L, idx != 0 && -idx <= L->top - L->base);
42 return L->top + idx;
43 } else if (idx == LUA_GLOBALSINDEX) {
44 TValue *o = &G(L)->tmptv;
45 settabV(L, o, tabref(L->env));
46 return o;
47 } else if (idx == LUA_REGISTRYINDEX) {
48 return registry(L);
49 } else {
50 GCfunc *fn = curr_func(L);
51 api_check(L, fn->c.gct == ~LJ_TFUNC && !isluafunc(fn));
52 if (idx == LUA_ENVIRONINDEX) {
53 TValue *o = &G(L)->tmptv;
54 settabV(L, o, tabref(fn->c.env));
55 return o;
56 } else {
57 idx = LUA_GLOBALSINDEX - idx;
58 return idx <= fn->c.nupvalues ? &fn->c.upvalue[idx-1] : niltv(L);
63 static TValue *stkindex2adr(lua_State *L, int idx)
65 if (idx > 0) {
66 TValue *o = L->base + (idx - 1);
67 return o < L->top ? o : niltv(L);
68 } else {
69 api_check(L, idx != 0 && -idx <= L->top - L->base);
70 return L->top + idx;
74 static GCtab *getcurrenv(lua_State *L)
76 GCfunc *fn = curr_func(L);
77 return fn->c.gct == ~LJ_TFUNC ? tabref(fn->c.env) : tabref(L->env);
80 /* -- Miscellaneous API functions ----------------------------------------- */
82 LUA_API int lua_status(lua_State *L)
84 return L->status;
87 LUA_API int lua_checkstack(lua_State *L, int size)
89 if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK) {
90 return 0; /* Stack overflow. */
91 } else if (size > 0) {
92 lj_state_checkstack(L, (MSize)size);
94 return 1;
97 LUALIB_API void luaL_checkstack(lua_State *L, int size, const char *msg)
99 if (!lua_checkstack(L, size))
100 lj_err_callerv(L, LJ_ERR_STKOVM, msg);
103 LUA_API void lua_xmove(lua_State *from, lua_State *to, int n)
105 TValue *f, *t;
106 if (from == to) return;
107 api_checknelems(from, n);
108 api_check(from, G(from) == G(to));
109 lj_state_checkstack(to, (MSize)n);
110 f = from->top;
111 t = to->top = to->top + n;
112 while (--n >= 0) copyTV(to, --t, --f);
113 from->top = f;
116 /* -- Stack manipulation -------------------------------------------------- */
118 LUA_API int lua_gettop(lua_State *L)
120 return (int)(L->top - L->base);
123 LUA_API void lua_settop(lua_State *L, int idx)
125 if (idx >= 0) {
126 api_check(L, idx <= tvref(L->maxstack) - L->base);
127 if (L->base + idx > L->top) {
128 if (L->base + idx >= tvref(L->maxstack))
129 lj_state_growstack(L, (MSize)idx - (MSize)(L->top - L->base));
130 do { setnilV(L->top++); } while (L->top < L->base + idx);
131 } else {
132 L->top = L->base + idx;
134 } else {
135 api_check(L, -(idx+1) <= (L->top - L->base));
136 L->top += idx+1; /* Shrinks top (idx < 0). */
140 LUA_API void lua_remove(lua_State *L, int idx)
142 TValue *p = stkindex2adr(L, idx);
143 api_checkvalidindex(L, p);
144 while (++p < L->top) copyTV(L, p-1, p);
145 L->top--;
148 LUA_API void lua_insert(lua_State *L, int idx)
150 TValue *q, *p = stkindex2adr(L, idx);
151 api_checkvalidindex(L, p);
152 for (q = L->top; q > p; q--) copyTV(L, q, q-1);
153 copyTV(L, p, L->top);
156 LUA_API void lua_replace(lua_State *L, int idx)
158 api_checknelems(L, 1);
159 if (idx == LUA_GLOBALSINDEX) {
160 api_check(L, tvistab(L->top-1));
161 /* NOBARRIER: A thread (i.e. L) is never black. */
162 setgcref(L->env, obj2gco(tabV(L->top-1)));
163 } else if (idx == LUA_ENVIRONINDEX) {
164 GCfunc *fn = curr_func(L);
165 if (fn->c.gct != ~LJ_TFUNC)
166 lj_err_msg(L, LJ_ERR_NOENV);
167 api_check(L, tvistab(L->top-1));
168 setgcref(fn->c.env, obj2gco(tabV(L->top-1)));
169 lj_gc_barrier(L, fn, L->top-1);
170 } else {
171 TValue *o = index2adr(L, idx);
172 api_checkvalidindex(L, o);
173 copyTV(L, o, L->top-1);
174 if (idx < LUA_GLOBALSINDEX) /* Need a barrier for upvalues. */
175 lj_gc_barrier(L, curr_func(L), L->top-1);
177 L->top--;
180 LUA_API void lua_pushvalue(lua_State *L, int idx)
182 copyTV(L, L->top, index2adr(L, idx));
183 incr_top(L);
186 /* -- Stack getters ------------------------------------------------------- */
188 LUA_API int lua_type(lua_State *L, int idx)
190 cTValue *o = index2adr(L, idx);
191 if (tvisnumber(o)) {
192 return LUA_TNUMBER;
193 #if LJ_64
194 } else if (tvislightud(o)) {
195 return LUA_TLIGHTUSERDATA;
196 #endif
197 } else if (o == niltv(L)) {
198 return LUA_TNONE;
199 } else { /* Magic internal/external tag conversion. ORDER LJ_T */
200 uint32_t t = ~itype(o);
201 #if LJ_64
202 int tt = (int)((U64x(75a06,98042110) >> 4*t) & 15u);
203 #else
204 int tt = (int)(((t < 8 ? 0x98042110u : 0x75a06u) >> 4*(t&7)) & 15u);
205 #endif
206 lua_assert(tt != LUA_TNIL || tvisnil(o));
207 return tt;
211 LUALIB_API void luaL_checktype(lua_State *L, int idx, int tt)
213 if (lua_type(L, idx) != tt)
214 lj_err_argt(L, idx, tt);
217 LUALIB_API void luaL_checkany(lua_State *L, int idx)
219 if (index2adr(L, idx) == niltv(L))
220 lj_err_arg(L, idx, LJ_ERR_NOVAL);
223 LUA_API const char *lua_typename(lua_State *L, int t)
225 UNUSED(L);
226 return lj_obj_typename[t+1];
229 LUA_API int lua_iscfunction(lua_State *L, int idx)
231 cTValue *o = index2adr(L, idx);
232 return tvisfunc(o) && !isluafunc(funcV(o));
235 LUA_API int lua_isnumber(lua_State *L, int idx)
237 cTValue *o = index2adr(L, idx);
238 TValue tmp;
239 return (tvisnumber(o) || (tvisstr(o) && lj_str_tonumber(strV(o), &tmp)));
242 LUA_API int lua_isstring(lua_State *L, int idx)
244 cTValue *o = index2adr(L, idx);
245 return (tvisstr(o) || tvisnumber(o));
248 LUA_API int lua_isuserdata(lua_State *L, int idx)
250 cTValue *o = index2adr(L, idx);
251 return (tvisudata(o) || tvislightud(o));
254 LUA_API int lua_rawequal(lua_State *L, int idx1, int idx2)
256 cTValue *o1 = index2adr(L, idx1);
257 cTValue *o2 = index2adr(L, idx2);
258 return (o1 == niltv(L) || o2 == niltv(L)) ? 0 : lj_obj_equal(o1, o2);
261 LUA_API int lua_equal(lua_State *L, int idx1, int idx2)
263 cTValue *o1 = index2adr(L, idx1);
264 cTValue *o2 = index2adr(L, idx2);
265 if (tvisint(o1) && tvisint(o2)) {
266 return intV(o1) == intV(o2);
267 } else if (tvisnumber(o1) && tvisnumber(o2)) {
268 return numberVnum(o1) == numberVnum(o2);
269 } else if (itype(o1) != itype(o2)) {
270 return 0;
271 } else if (tvispri(o1)) {
272 return o1 != niltv(L) && o2 != niltv(L);
273 #if LJ_64
274 } else if (tvislightud(o1)) {
275 return o1->u64 == o2->u64;
276 #endif
277 } else if (gcrefeq(o1->gcr, o2->gcr)) {
278 return 1;
279 } else if (!tvistabud(o1)) {
280 return 0;
281 } else {
282 TValue *base = lj_meta_equal(L, gcV(o1), gcV(o2), 0);
283 if ((uintptr_t)base <= 1) {
284 return (int)(uintptr_t)base;
285 } else {
286 L->top = base+2;
287 lj_vm_call(L, base, 1+1);
288 L->top -= 2;
289 return tvistruecond(L->top+1);
294 LUA_API int lua_lessthan(lua_State *L, int idx1, int idx2)
296 cTValue *o1 = index2adr(L, idx1);
297 cTValue *o2 = index2adr(L, idx2);
298 if (o1 == niltv(L) || o2 == niltv(L)) {
299 return 0;
300 } else if (tvisint(o1) && tvisint(o2)) {
301 return intV(o1) < intV(o2);
302 } else if (tvisnumber(o1) && tvisnumber(o2)) {
303 return numberVnum(o1) < numberVnum(o2);
304 } else {
305 TValue *base = lj_meta_comp(L, o1, o2, 0);
306 if ((uintptr_t)base <= 1) {
307 return (int)(uintptr_t)base;
308 } else {
309 L->top = base+2;
310 lj_vm_call(L, base, 1+1);
311 L->top -= 2;
312 return tvistruecond(L->top+1);
317 LUA_API lua_Number lua_tonumber(lua_State *L, int idx)
319 cTValue *o = index2adr(L, idx);
320 TValue tmp;
321 if (LJ_LIKELY(tvisnumber(o)))
322 return numberVnum(o);
323 else if (tvisstr(o) && lj_str_tonum(strV(o), &tmp))
324 return numV(&tmp);
325 else
326 return 0;
329 LUALIB_API lua_Number luaL_checknumber(lua_State *L, int idx)
331 cTValue *o = index2adr(L, idx);
332 TValue tmp;
333 if (LJ_LIKELY(tvisnumber(o)))
334 return numberVnum(o);
335 else if (!(tvisstr(o) && lj_str_tonum(strV(o), &tmp)))
336 lj_err_argt(L, idx, LUA_TNUMBER);
337 return numV(&tmp);
340 LUALIB_API lua_Number luaL_optnumber(lua_State *L, int idx, lua_Number def)
342 cTValue *o = index2adr(L, idx);
343 TValue tmp;
344 if (LJ_LIKELY(tvisnumber(o)))
345 return numberVnum(o);
346 else if (tvisnil(o))
347 return def;
348 else if (!(tvisstr(o) && lj_str_tonum(strV(o), &tmp)))
349 lj_err_argt(L, idx, LUA_TNUMBER);
350 return numV(&tmp);
353 LUA_API lua_Integer lua_tointeger(lua_State *L, int idx)
355 cTValue *o = index2adr(L, idx);
356 TValue tmp;
357 lua_Number n;
358 if (LJ_LIKELY(tvisint(o))) {
359 return intV(o);
360 } else if (LJ_LIKELY(tvisnum(o))) {
361 n = numV(o);
362 } else {
363 if (!(tvisstr(o) && lj_str_tonumber(strV(o), &tmp)))
364 return 0;
365 if (tvisint(&tmp))
366 return (lua_Integer)intV(&tmp);
367 n = numV(&tmp);
369 #if LJ_64
370 return (lua_Integer)n;
371 #else
372 return lj_num2int(n);
373 #endif
376 LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int idx)
378 cTValue *o = index2adr(L, idx);
379 TValue tmp;
380 lua_Number n;
381 if (LJ_LIKELY(tvisint(o))) {
382 return intV(o);
383 } else if (LJ_LIKELY(tvisnum(o))) {
384 n = numV(o);
385 } else {
386 if (!(tvisstr(o) && lj_str_tonumber(strV(o), &tmp)))
387 lj_err_argt(L, idx, LUA_TNUMBER);
388 if (tvisint(&tmp))
389 return (lua_Integer)intV(&tmp);
390 n = numV(&tmp);
392 #if LJ_64
393 return (lua_Integer)n;
394 #else
395 return lj_num2int(n);
396 #endif
399 LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int idx, lua_Integer def)
401 cTValue *o = index2adr(L, idx);
402 TValue tmp;
403 lua_Number n;
404 if (LJ_LIKELY(tvisint(o))) {
405 return intV(o);
406 } else if (LJ_LIKELY(tvisnum(o))) {
407 n = numV(o);
408 } else if (tvisnil(o)) {
409 return def;
410 } else {
411 if (!(tvisstr(o) && lj_str_tonumber(strV(o), &tmp)))
412 lj_err_argt(L, idx, LUA_TNUMBER);
413 if (tvisint(&tmp))
414 return (lua_Integer)intV(&tmp);
415 n = numV(&tmp);
417 #if LJ_64
418 return (lua_Integer)n;
419 #else
420 return lj_num2int(n);
421 #endif
424 LUA_API int lua_toboolean(lua_State *L, int idx)
426 cTValue *o = index2adr(L, idx);
427 return tvistruecond(o);
430 LUA_API const char *lua_tolstring(lua_State *L, int idx, size_t *len)
432 TValue *o = index2adr(L, idx);
433 GCstr *s;
434 if (LJ_LIKELY(tvisstr(o))) {
435 s = strV(o);
436 } else if (tvisnumber(o)) {
437 lj_gc_check(L);
438 o = index2adr(L, idx); /* GC may move the stack. */
439 s = lj_str_fromnumber(L, o);
440 } else {
441 if (len != NULL) *len = 0;
442 return NULL;
444 if (len != NULL) *len = s->len;
445 return strdata(s);
448 LUALIB_API const char *luaL_checklstring(lua_State *L, int idx, size_t *len)
450 TValue *o = index2adr(L, idx);
451 GCstr *s;
452 if (LJ_LIKELY(tvisstr(o))) {
453 s = strV(o);
454 } else if (tvisnumber(o)) {
455 lj_gc_check(L);
456 o = index2adr(L, idx); /* GC may move the stack. */
457 s = lj_str_fromnumber(L, o);
458 } else {
459 lj_err_argt(L, idx, LUA_TSTRING);
461 if (len != NULL) *len = s->len;
462 return strdata(s);
465 LUALIB_API const char *luaL_optlstring(lua_State *L, int idx,
466 const char *def, size_t *len)
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 } else {
480 lj_err_argt(L, idx, LUA_TSTRING);
482 if (len != NULL) *len = s->len;
483 return strdata(s);
486 LUALIB_API int luaL_checkoption(lua_State *L, int idx, const char *def,
487 const char *const lst[])
489 ptrdiff_t i;
490 const char *s = lua_tolstring(L, idx, NULL);
491 if (s == NULL && (s = def) == NULL)
492 lj_err_argt(L, idx, LUA_TSTRING);
493 for (i = 0; lst[i]; i++)
494 if (strcmp(lst[i], s) == 0)
495 return (int)i;
496 lj_err_argv(L, idx, LJ_ERR_INVOPTM, s);
499 LUA_API size_t lua_objlen(lua_State *L, int idx)
501 TValue *o = index2adr(L, idx);
502 if (tvisstr(o))
503 return strV(o)->len;
504 else if (tvistab(o))
505 return (size_t)lj_tab_len(tabV(o));
506 else if (tvisudata(o))
507 return udataV(o)->len;
508 else if (tvisnumber(o))
509 return lj_str_fromnumber(L, o)->len;
510 else
511 return 0;
514 LUA_API lua_CFunction lua_tocfunction(lua_State *L, int idx)
516 cTValue *o = index2adr(L, idx);
517 if (tvisfunc(o)) {
518 BCOp op = bc_op(*mref(funcV(o)->c.pc, BCIns));
519 if (op == BC_FUNCC || op == BC_FUNCCW)
520 return funcV(o)->c.f;
522 return NULL;
525 LUA_API void *lua_touserdata(lua_State *L, int idx)
527 cTValue *o = index2adr(L, idx);
528 if (tvisudata(o))
529 return uddata(udataV(o));
530 else if (tvislightud(o))
531 return lightudV(o);
532 else
533 return NULL;
536 LUA_API lua_State *lua_tothread(lua_State *L, int idx)
538 cTValue *o = index2adr(L, idx);
539 return (!tvisthread(o)) ? NULL : threadV(o);
542 LUA_API const void *lua_topointer(lua_State *L, int idx)
544 cTValue *o = index2adr(L, idx);
545 if (tvisudata(o))
546 return uddata(udataV(o));
547 else if (tvislightud(o))
548 return lightudV(o);
549 else if (tviscdata(o))
550 return cdataptr(cdataV(o));
551 else if (tvisgcv(o))
552 return gcV(o);
553 else
554 return NULL;
557 /* -- Stack setters (object creation) ------------------------------------- */
559 LUA_API void lua_pushnil(lua_State *L)
561 setnilV(L->top);
562 incr_top(L);
565 LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
567 setnumV(L->top, n);
568 if (LJ_UNLIKELY(tvisnan(L->top)))
569 setnanV(L->top); /* Canonicalize injected NaNs. */
570 incr_top(L);
573 LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
575 setintptrV(L->top, n);
576 incr_top(L);
579 LUA_API void lua_pushlstring(lua_State *L, const char *str, size_t len)
581 GCstr *s;
582 lj_gc_check(L);
583 s = lj_str_new(L, str, len);
584 setstrV(L, L->top, s);
585 incr_top(L);
588 LUA_API void lua_pushstring(lua_State *L, const char *str)
590 if (str == NULL) {
591 setnilV(L->top);
592 } else {
593 GCstr *s;
594 lj_gc_check(L);
595 s = lj_str_newz(L, str);
596 setstrV(L, L->top, s);
598 incr_top(L);
601 LUA_API const char *lua_pushvfstring(lua_State *L, const char *fmt,
602 va_list argp)
604 lj_gc_check(L);
605 return lj_str_pushvf(L, fmt, argp);
608 LUA_API const char *lua_pushfstring(lua_State *L, const char *fmt, ...)
610 const char *ret;
611 va_list argp;
612 lj_gc_check(L);
613 va_start(argp, fmt);
614 ret = lj_str_pushvf(L, fmt, argp);
615 va_end(argp);
616 return ret;
619 LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction f, int n)
621 GCfunc *fn;
622 lj_gc_check(L);
623 api_checknelems(L, n);
624 fn = lj_func_newC(L, (MSize)n, getcurrenv(L));
625 fn->c.f = f;
626 L->top -= n;
627 while (n--)
628 copyTV(L, &fn->c.upvalue[n], L->top+n);
629 setfuncV(L, L->top, fn);
630 lua_assert(iswhite(obj2gco(fn)));
631 incr_top(L);
634 LUA_API void lua_pushboolean(lua_State *L, int b)
636 setboolV(L->top, (b != 0));
637 incr_top(L);
640 LUA_API void lua_pushlightuserdata(lua_State *L, void *p)
642 setlightudV(L->top, checklightudptr(L, p));
643 incr_top(L);
646 LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
648 GCtab *t;
649 lj_gc_check(L);
650 t = lj_tab_new(L, (uint32_t)(narray > 0 ? narray+1 : 0), hsize2hbits(nrec));
651 settabV(L, L->top, t);
652 incr_top(L);
655 LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname)
657 GCtab *regt = tabV(registry(L));
658 TValue *tv = lj_tab_setstr(L, regt, lj_str_newz(L, tname));
659 if (tvisnil(tv)) {
660 GCtab *mt = lj_tab_new(L, 0, 1);
661 settabV(L, tv, mt);
662 settabV(L, L->top++, mt);
663 lj_gc_anybarriert(L, regt);
664 return 1;
665 } else {
666 copyTV(L, L->top++, tv);
667 return 0;
671 LUA_API int lua_pushthread(lua_State *L)
673 setthreadV(L, L->top, L);
674 incr_top(L);
675 return (mainthread(G(L)) == L);
678 LUA_API lua_State *lua_newthread(lua_State *L)
680 lua_State *L1;
681 lj_gc_check(L);
682 L1 = lj_state_new(L);
683 setthreadV(L, L->top, L1);
684 incr_top(L);
685 return L1;
688 LUA_API void *lua_newuserdata(lua_State *L, size_t size)
690 GCudata *ud;
691 lj_gc_check(L);
692 if (size > LJ_MAX_UDATA)
693 lj_err_msg(L, LJ_ERR_UDATAOV);
694 ud = lj_udata_new(L, (MSize)size, getcurrenv(L));
695 setudataV(L, L->top, ud);
696 incr_top(L);
697 return uddata(ud);
700 LUA_API void lua_concat(lua_State *L, int n)
702 api_checknelems(L, n);
703 if (n >= 2) {
704 n--;
705 do {
706 TValue *top = lj_meta_cat(L, L->top-1, n);
707 if (top == NULL) {
708 L->top -= n;
709 break;
711 n -= (int)(L->top - top);
712 L->top = top+2;
713 lj_vm_call(L, top, 1+1);
714 L->top--;
715 copyTV(L, L->top-1, L->top);
716 } while (--n > 0);
717 } else if (n == 0) { /* Push empty string. */
718 setstrV(L, L->top, &G(L)->strempty);
719 incr_top(L);
721 /* else n == 1: nothing to do. */
724 /* -- Object getters ------------------------------------------------------ */
726 LUA_API void lua_gettable(lua_State *L, int idx)
728 cTValue *v, *t = index2adr(L, idx);
729 api_checkvalidindex(L, t);
730 v = lj_meta_tget(L, t, L->top-1);
731 if (v == NULL) {
732 L->top += 2;
733 lj_vm_call(L, L->top-2, 1+1);
734 L->top -= 2;
735 v = L->top+1;
737 copyTV(L, L->top-1, v);
740 LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
742 cTValue *v, *t = index2adr(L, idx);
743 TValue key;
744 api_checkvalidindex(L, t);
745 setstrV(L, &key, lj_str_newz(L, k));
746 v = lj_meta_tget(L, t, &key);
747 if (v == NULL) {
748 L->top += 2;
749 lj_vm_call(L, L->top-2, 1+1);
750 L->top -= 2;
751 v = L->top+1;
753 copyTV(L, L->top, v);
754 incr_top(L);
757 LUA_API void lua_rawget(lua_State *L, int idx)
759 cTValue *t = index2adr(L, idx);
760 api_check(L, tvistab(t));
761 copyTV(L, L->top-1, lj_tab_get(L, tabV(t), L->top-1));
764 LUA_API void lua_rawgeti(lua_State *L, int idx, int n)
766 cTValue *v, *t = index2adr(L, idx);
767 api_check(L, tvistab(t));
768 v = lj_tab_getint(tabV(t), n);
769 if (v) {
770 copyTV(L, L->top, v);
771 } else {
772 setnilV(L->top);
774 incr_top(L);
777 LUA_API int lua_getmetatable(lua_State *L, int idx)
779 cTValue *o = index2adr(L, idx);
780 GCtab *mt = NULL;
781 if (tvistab(o))
782 mt = tabref(tabV(o)->metatable);
783 else if (tvisudata(o))
784 mt = tabref(udataV(o)->metatable);
785 else
786 mt = tabref(basemt_obj(G(L), o));
787 if (mt == NULL)
788 return 0;
789 settabV(L, L->top, mt);
790 incr_top(L);
791 return 1;
794 LUALIB_API int luaL_getmetafield(lua_State *L, int idx, const char *field)
796 if (lua_getmetatable(L, idx)) {
797 cTValue *tv = lj_tab_getstr(tabV(L->top-1), lj_str_newz(L, field));
798 if (tv && !tvisnil(tv)) {
799 copyTV(L, L->top-1, tv);
800 return 1;
802 L->top--;
804 return 0;
807 LUA_API void lua_getfenv(lua_State *L, int idx)
809 cTValue *o = index2adr(L, idx);
810 api_checkvalidindex(L, o);
811 if (tvisfunc(o)) {
812 settabV(L, L->top, tabref(funcV(o)->c.env));
813 } else if (tvisudata(o)) {
814 settabV(L, L->top, tabref(udataV(o)->env));
815 } else if (tvisthread(o)) {
816 settabV(L, L->top, tabref(threadV(o)->env));
817 } else {
818 setnilV(L->top);
820 incr_top(L);
823 LUA_API int lua_next(lua_State *L, int idx)
825 cTValue *t = index2adr(L, idx);
826 int more;
827 api_check(L, tvistab(t));
828 more = lj_tab_next(L, tabV(t), L->top-1);
829 if (more) {
830 incr_top(L); /* Return new key and value slot. */
831 } else { /* End of traversal. */
832 L->top--; /* Remove key slot. */
834 return more;
837 LUA_API const char *lua_getupvalue(lua_State *L, int idx, int n)
839 TValue *val;
840 const char *name = lj_debug_uvnamev(index2adr(L, idx), (uint32_t)(n-1), &val);
841 if (name) {
842 copyTV(L, L->top, val);
843 incr_top(L);
845 return name;
848 LUALIB_API void *luaL_checkudata(lua_State *L, int idx, const char *tname)
850 cTValue *o = index2adr(L, idx);
851 if (tvisudata(o)) {
852 GCudata *ud = udataV(o);
853 cTValue *tv = lj_tab_getstr(tabV(registry(L)), lj_str_newz(L, tname));
854 if (tv && tvistab(tv) && tabV(tv) == tabref(ud->metatable))
855 return uddata(ud);
857 lj_err_argtype(L, idx, tname);
858 return NULL; /* unreachable */
861 /* -- Object setters ------------------------------------------------------ */
863 LUA_API void lua_settable(lua_State *L, int idx)
865 TValue *o;
866 cTValue *t = index2adr(L, idx);
867 api_checknelems(L, 2);
868 api_checkvalidindex(L, t);
869 o = lj_meta_tset(L, t, L->top-2);
870 if (o) {
871 /* NOBARRIER: lj_meta_tset ensures the table is not black. */
872 copyTV(L, o, L->top-1);
873 L->top -= 2;
874 } else {
875 L->top += 3;
876 copyTV(L, L->top-1, L->top-6);
877 lj_vm_call(L, L->top-3, 0+1);
878 L->top -= 3;
882 LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
884 TValue *o;
885 TValue key;
886 cTValue *t = index2adr(L, idx);
887 api_checknelems(L, 1);
888 api_checkvalidindex(L, t);
889 setstrV(L, &key, lj_str_newz(L, k));
890 o = lj_meta_tset(L, t, &key);
891 if (o) {
892 L->top--;
893 /* NOBARRIER: lj_meta_tset ensures the table is not black. */
894 copyTV(L, o, L->top);
895 } else {
896 L->top += 3;
897 copyTV(L, L->top-1, L->top-6);
898 lj_vm_call(L, L->top-3, 0+1);
899 L->top -= 2;
903 LUA_API void lua_rawset(lua_State *L, int idx)
905 GCtab *t = tabV(index2adr(L, idx));
906 TValue *dst, *key;
907 api_checknelems(L, 2);
908 key = L->top-2;
909 dst = lj_tab_set(L, t, key);
910 copyTV(L, dst, key+1);
911 lj_gc_anybarriert(L, t);
912 L->top = key;
915 LUA_API void lua_rawseti(lua_State *L, int idx, int n)
917 GCtab *t = tabV(index2adr(L, idx));
918 TValue *dst, *src;
919 api_checknelems(L, 1);
920 dst = lj_tab_setint(L, t, n);
921 src = L->top-1;
922 copyTV(L, dst, src);
923 lj_gc_barriert(L, t, dst);
924 L->top = src;
927 LUA_API int lua_setmetatable(lua_State *L, int idx)
929 global_State *g;
930 GCtab *mt;
931 cTValue *o = index2adr(L, idx);
932 api_checknelems(L, 1);
933 api_checkvalidindex(L, o);
934 if (tvisnil(L->top-1)) {
935 mt = NULL;
936 } else {
937 api_check(L, tvistab(L->top-1));
938 mt = tabV(L->top-1);
940 g = G(L);
941 if (tvistab(o)) {
942 setgcref(tabV(o)->metatable, obj2gco(mt));
943 if (mt)
944 lj_gc_objbarriert(L, tabV(o), mt);
945 } else if (tvisudata(o)) {
946 setgcref(udataV(o)->metatable, obj2gco(mt));
947 if (mt)
948 lj_gc_objbarrier(L, udataV(o), mt);
949 } else {
950 /* Flush cache, since traces specialize to basemt. But not during __gc. */
951 if (lj_trace_flushall(L))
952 lj_err_caller(L, LJ_ERR_NOGCMM);
953 if (tvisbool(o)) {
954 /* NOBARRIER: basemt is a GC root. */
955 setgcref(basemt_it(g, LJ_TTRUE), obj2gco(mt));
956 setgcref(basemt_it(g, LJ_TFALSE), obj2gco(mt));
957 } else {
958 /* NOBARRIER: basemt is a GC root. */
959 setgcref(basemt_obj(g, o), obj2gco(mt));
962 L->top--;
963 return 1;
966 LUA_API int lua_setfenv(lua_State *L, int idx)
968 cTValue *o = index2adr(L, idx);
969 GCtab *t;
970 api_checknelems(L, 1);
971 api_checkvalidindex(L, o);
972 api_check(L, tvistab(L->top-1));
973 t = tabV(L->top-1);
974 if (tvisfunc(o)) {
975 setgcref(funcV(o)->c.env, obj2gco(t));
976 } else if (tvisudata(o)) {
977 setgcref(udataV(o)->env, obj2gco(t));
978 } else if (tvisthread(o)) {
979 setgcref(threadV(o)->env, obj2gco(t));
980 } else {
981 L->top--;
982 return 0;
984 lj_gc_objbarrier(L, gcV(o), t);
985 L->top--;
986 return 1;
989 LUA_API const char *lua_setupvalue(lua_State *L, int idx, int n)
991 cTValue *f = index2adr(L, idx);
992 TValue *val;
993 const char *name;
994 api_checknelems(L, 1);
995 name = lj_debug_uvnamev(f, (uint32_t)(n-1), &val);
996 if (name) {
997 L->top--;
998 copyTV(L, val, L->top);
999 lj_gc_barrier(L, funcV(f), L->top);
1001 return name;
1004 /* -- Calls --------------------------------------------------------------- */
1006 LUA_API void lua_call(lua_State *L, int nargs, int nresults)
1008 api_check(L, L->status == 0 || L->status == LUA_ERRERR);
1009 api_checknelems(L, nargs+1);
1010 lj_vm_call(L, L->top - nargs, nresults+1);
1013 LUA_API int lua_pcall(lua_State *L, int nargs, int nresults, int errfunc)
1015 global_State *g = G(L);
1016 uint8_t oldh = hook_save(g);
1017 ptrdiff_t ef;
1018 int status;
1019 api_check(L, L->status == 0 || L->status == LUA_ERRERR);
1020 api_checknelems(L, nargs+1);
1021 if (errfunc == 0) {
1022 ef = 0;
1023 } else {
1024 cTValue *o = stkindex2adr(L, errfunc);
1025 api_checkvalidindex(L, o);
1026 ef = savestack(L, o);
1028 status = lj_vm_pcall(L, L->top - nargs, nresults+1, ef);
1029 if (status) hook_restore(g, oldh);
1030 return status;
1033 static TValue *cpcall(lua_State *L, lua_CFunction func, void *ud)
1035 GCfunc *fn = lj_func_newC(L, 0, getcurrenv(L));
1036 fn->c.f = func;
1037 setfuncV(L, L->top, fn);
1038 setlightudV(L->top+1, checklightudptr(L, ud));
1039 cframe_nres(L->cframe) = 1+0; /* Zero results. */
1040 L->top += 2;
1041 return L->top-1; /* Now call the newly allocated C function. */
1044 LUA_API int lua_cpcall(lua_State *L, lua_CFunction func, void *ud)
1046 global_State *g = G(L);
1047 uint8_t oldh = hook_save(g);
1048 int status;
1049 api_check(L, L->status == 0 || L->status == LUA_ERRERR);
1050 status = lj_vm_cpcall(L, func, ud, cpcall);
1051 if (status) hook_restore(g, oldh);
1052 return status;
1055 LUALIB_API int luaL_callmeta(lua_State *L, int idx, const char *field)
1057 if (luaL_getmetafield(L, idx, field)) {
1058 TValue *base = L->top--;
1059 copyTV(L, base, index2adr(L, idx));
1060 L->top = base+1;
1061 lj_vm_call(L, base, 1+1);
1062 return 1;
1064 return 0;
1067 /* -- Coroutine yield and resume ------------------------------------------ */
1069 LUA_API int lua_yield(lua_State *L, int nresults)
1071 void *cf = L->cframe;
1072 global_State *g = G(L);
1073 if (cframe_canyield(cf)) {
1074 cf = cframe_raw(cf);
1075 if (!hook_active(g)) { /* Regular yield: move results down if needed. */
1076 cTValue *f = L->top - nresults;
1077 if (f > L->base) {
1078 TValue *t = L->base;
1079 while (--nresults >= 0) copyTV(L, t++, f++);
1080 L->top = t;
1082 } else { /* Yield from hook: add a pseudo-frame. */
1083 TValue *top = L->top;
1084 hook_leave(g);
1085 top->u64 = cframe_multres(cf);
1086 setcont(top+1, lj_cont_hook);
1087 setframe_pc(top+1, cframe_pc(cf)-1);
1088 setframe_gc(top+2, obj2gco(L));
1089 setframe_ftsz(top+2, (int)((char *)(top+3)-(char *)L->base)+FRAME_CONT);
1090 L->top = L->base = top+3;
1092 #if LJ_TARGET_X64
1093 lj_err_throw(L, LUA_YIELD);
1094 #else
1095 L->cframe = NULL;
1096 L->status = LUA_YIELD;
1097 lj_vm_unwind_c(cf, LUA_YIELD);
1098 #endif
1100 lj_err_msg(L, LJ_ERR_CYIELD);
1101 return 0; /* unreachable */
1104 LUA_API int lua_resume(lua_State *L, int nargs)
1106 if (L->cframe == NULL && L->status <= LUA_YIELD)
1107 return lj_vm_resume(L, L->top - nargs, 0, 0);
1108 L->top = L->base;
1109 setstrV(L, L->top, lj_err_str(L, LJ_ERR_COSUSP));
1110 incr_top(L);
1111 return LUA_ERRRUN;
1114 /* -- Load and dump Lua code ---------------------------------------------- */
1116 static TValue *cpparser(lua_State *L, lua_CFunction dummy, void *ud)
1118 LexState *ls = (LexState *)ud;
1119 GCproto *pt;
1120 GCfunc *fn;
1121 UNUSED(dummy);
1122 cframe_errfunc(L->cframe) = -1; /* Inherit error function. */
1123 pt = lj_lex_setup(L, ls) ? lj_bcread(ls) : lj_parse(ls);
1124 fn = lj_func_newL_empty(L, pt, tabref(L->env));
1125 /* Don't combine above/below into one statement. */
1126 setfuncV(L, L->top++, fn);
1127 return NULL;
1130 LUA_API int lua_load(lua_State *L, lua_Reader reader, void *data,
1131 const char *chunkname)
1133 LexState ls;
1134 int status;
1135 ls.rfunc = reader;
1136 ls.rdata = data;
1137 ls.chunkarg = chunkname ? chunkname : "?";
1138 lj_str_initbuf(&ls.sb);
1139 status = lj_vm_cpcall(L, NULL, &ls, cpparser);
1140 lj_lex_cleanup(L, &ls);
1141 lj_gc_check(L);
1142 return status;
1145 LUA_API int lua_dump(lua_State *L, lua_Writer writer, void *data)
1147 cTValue *o = L->top-1;
1148 api_checknelems(L, 1);
1149 if (tvisfunc(o) && isluafunc(funcV(o)))
1150 return lj_bcwrite(L, funcproto(funcV(o)), writer, data, 0);
1151 else
1152 return 1;
1155 /* -- GC and memory management -------------------------------------------- */
1157 LUA_API int lua_gc(lua_State *L, int what, int data)
1159 global_State *g = G(L);
1160 int res = 0;
1161 switch (what) {
1162 case LUA_GCSTOP:
1163 g->gc.threshold = LJ_MAX_MEM;
1164 break;
1165 case LUA_GCRESTART:
1166 g->gc.threshold = data == -1 ? (g->gc.total/100)*g->gc.pause : g->gc.total;
1167 break;
1168 case LUA_GCCOLLECT:
1169 lj_gc_fullgc(L);
1170 break;
1171 case LUA_GCCOUNT:
1172 res = (int)(g->gc.total >> 10);
1173 break;
1174 case LUA_GCCOUNTB:
1175 res = (int)(g->gc.total & 0x3ff);
1176 break;
1177 case LUA_GCSTEP: {
1178 MSize a = (MSize)data << 10;
1179 g->gc.threshold = (a <= g->gc.total) ? (g->gc.total - a) : 0;
1180 while (g->gc.total >= g->gc.threshold)
1181 if (lj_gc_step(L)) {
1182 res = 1;
1183 break;
1185 break;
1187 case LUA_GCSETPAUSE:
1188 res = (int)(g->gc.pause);
1189 g->gc.pause = (MSize)data;
1190 break;
1191 case LUA_GCSETSTEPMUL:
1192 res = (int)(g->gc.stepmul);
1193 g->gc.stepmul = (MSize)data;
1194 break;
1195 default:
1196 res = -1; /* Invalid option. */
1198 return res;
1201 LUA_API lua_Alloc lua_getallocf(lua_State *L, void **ud)
1203 global_State *g = G(L);
1204 if (ud) *ud = g->allocd;
1205 return g->allocf;
1208 LUA_API void lua_setallocf(lua_State *L, lua_Alloc f, void *ud)
1210 global_State *g = G(L);
1211 g->allocd = ud;
1212 g->allocf = f;