Fix shrinking of direct mapped block in builtin allocator.
[luajit-2.0/celess22.git] / src / lj_api.c
blobb807900dd4e39d8936e0323e77c3353699b7ee94
1 /*
2 ** Public Lua/C API.
3 ** Copyright (C) 2005-2012 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 setstrV(L, o, s);
441 } else {
442 if (len != NULL) *len = 0;
443 return NULL;
445 if (len != NULL) *len = s->len;
446 return strdata(s);
449 LUALIB_API const char *luaL_checklstring(lua_State *L, int idx, size_t *len)
451 TValue *o = index2adr(L, idx);
452 GCstr *s;
453 if (LJ_LIKELY(tvisstr(o))) {
454 s = strV(o);
455 } else if (tvisnumber(o)) {
456 lj_gc_check(L);
457 o = index2adr(L, idx); /* GC may move the stack. */
458 s = lj_str_fromnumber(L, o);
459 setstrV(L, o, s);
460 } else {
461 lj_err_argt(L, idx, LUA_TSTRING);
463 if (len != NULL) *len = s->len;
464 return strdata(s);
467 LUALIB_API const char *luaL_optlstring(lua_State *L, int idx,
468 const char *def, size_t *len)
470 TValue *o = index2adr(L, idx);
471 GCstr *s;
472 if (LJ_LIKELY(tvisstr(o))) {
473 s = strV(o);
474 } else if (tvisnil(o)) {
475 if (len != NULL) *len = def ? strlen(def) : 0;
476 return def;
477 } else if (tvisnumber(o)) {
478 lj_gc_check(L);
479 o = index2adr(L, idx); /* GC may move the stack. */
480 s = lj_str_fromnumber(L, o);
481 setstrV(L, o, s);
482 } else {
483 lj_err_argt(L, idx, LUA_TSTRING);
485 if (len != NULL) *len = s->len;
486 return strdata(s);
489 LUALIB_API int luaL_checkoption(lua_State *L, int idx, const char *def,
490 const char *const lst[])
492 ptrdiff_t i;
493 const char *s = lua_tolstring(L, idx, NULL);
494 if (s == NULL && (s = def) == NULL)
495 lj_err_argt(L, idx, LUA_TSTRING);
496 for (i = 0; lst[i]; i++)
497 if (strcmp(lst[i], s) == 0)
498 return (int)i;
499 lj_err_argv(L, idx, LJ_ERR_INVOPTM, s);
502 LUA_API size_t lua_objlen(lua_State *L, int idx)
504 TValue *o = index2adr(L, idx);
505 if (tvisstr(o)) {
506 return strV(o)->len;
507 } else if (tvistab(o)) {
508 return (size_t)lj_tab_len(tabV(o));
509 } else if (tvisudata(o)) {
510 return udataV(o)->len;
511 } else if (tvisnumber(o)) {
512 GCstr *s = lj_str_fromnumber(L, o);
513 setstrV(L, o, s);
514 return s->len;
515 } else {
516 return 0;
520 LUA_API lua_CFunction lua_tocfunction(lua_State *L, int idx)
522 cTValue *o = index2adr(L, idx);
523 if (tvisfunc(o)) {
524 BCOp op = bc_op(*mref(funcV(o)->c.pc, BCIns));
525 if (op == BC_FUNCC || op == BC_FUNCCW)
526 return funcV(o)->c.f;
528 return NULL;
531 LUA_API void *lua_touserdata(lua_State *L, int idx)
533 cTValue *o = index2adr(L, idx);
534 if (tvisudata(o))
535 return uddata(udataV(o));
536 else if (tvislightud(o))
537 return lightudV(o);
538 else
539 return NULL;
542 LUA_API lua_State *lua_tothread(lua_State *L, int idx)
544 cTValue *o = index2adr(L, idx);
545 return (!tvisthread(o)) ? NULL : threadV(o);
548 LUA_API const void *lua_topointer(lua_State *L, int idx)
550 cTValue *o = index2adr(L, idx);
551 if (tvisudata(o))
552 return uddata(udataV(o));
553 else if (tvislightud(o))
554 return lightudV(o);
555 else if (tviscdata(o))
556 return cdataptr(cdataV(o));
557 else if (tvisgcv(o))
558 return gcV(o);
559 else
560 return NULL;
563 /* -- Stack setters (object creation) ------------------------------------- */
565 LUA_API void lua_pushnil(lua_State *L)
567 setnilV(L->top);
568 incr_top(L);
571 LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
573 setnumV(L->top, n);
574 if (LJ_UNLIKELY(tvisnan(L->top)))
575 setnanV(L->top); /* Canonicalize injected NaNs. */
576 incr_top(L);
579 LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
581 setintptrV(L->top, n);
582 incr_top(L);
585 LUA_API void lua_pushlstring(lua_State *L, const char *str, size_t len)
587 GCstr *s;
588 lj_gc_check(L);
589 s = lj_str_new(L, str, len);
590 setstrV(L, L->top, s);
591 incr_top(L);
594 LUA_API void lua_pushstring(lua_State *L, const char *str)
596 if (str == NULL) {
597 setnilV(L->top);
598 } else {
599 GCstr *s;
600 lj_gc_check(L);
601 s = lj_str_newz(L, str);
602 setstrV(L, L->top, s);
604 incr_top(L);
607 LUA_API const char *lua_pushvfstring(lua_State *L, const char *fmt,
608 va_list argp)
610 lj_gc_check(L);
611 return lj_str_pushvf(L, fmt, argp);
614 LUA_API const char *lua_pushfstring(lua_State *L, const char *fmt, ...)
616 const char *ret;
617 va_list argp;
618 lj_gc_check(L);
619 va_start(argp, fmt);
620 ret = lj_str_pushvf(L, fmt, argp);
621 va_end(argp);
622 return ret;
625 LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction f, int n)
627 GCfunc *fn;
628 lj_gc_check(L);
629 api_checknelems(L, n);
630 fn = lj_func_newC(L, (MSize)n, getcurrenv(L));
631 fn->c.f = f;
632 L->top -= n;
633 while (n--)
634 copyTV(L, &fn->c.upvalue[n], L->top+n);
635 setfuncV(L, L->top, fn);
636 lua_assert(iswhite(obj2gco(fn)));
637 incr_top(L);
640 LUA_API void lua_pushboolean(lua_State *L, int b)
642 setboolV(L->top, (b != 0));
643 incr_top(L);
646 LUA_API void lua_pushlightuserdata(lua_State *L, void *p)
648 setlightudV(L->top, checklightudptr(L, p));
649 incr_top(L);
652 LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
654 GCtab *t;
655 lj_gc_check(L);
656 t = lj_tab_new(L, (uint32_t)(narray > 0 ? narray+1 : 0), hsize2hbits(nrec));
657 settabV(L, L->top, t);
658 incr_top(L);
661 LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname)
663 GCtab *regt = tabV(registry(L));
664 TValue *tv = lj_tab_setstr(L, regt, lj_str_newz(L, tname));
665 if (tvisnil(tv)) {
666 GCtab *mt = lj_tab_new(L, 0, 1);
667 settabV(L, tv, mt);
668 settabV(L, L->top++, mt);
669 lj_gc_anybarriert(L, regt);
670 return 1;
671 } else {
672 copyTV(L, L->top++, tv);
673 return 0;
677 LUA_API int lua_pushthread(lua_State *L)
679 setthreadV(L, L->top, L);
680 incr_top(L);
681 return (mainthread(G(L)) == L);
684 LUA_API lua_State *lua_newthread(lua_State *L)
686 lua_State *L1;
687 lj_gc_check(L);
688 L1 = lj_state_new(L);
689 setthreadV(L, L->top, L1);
690 incr_top(L);
691 return L1;
694 LUA_API void *lua_newuserdata(lua_State *L, size_t size)
696 GCudata *ud;
697 lj_gc_check(L);
698 if (size > LJ_MAX_UDATA)
699 lj_err_msg(L, LJ_ERR_UDATAOV);
700 ud = lj_udata_new(L, (MSize)size, getcurrenv(L));
701 setudataV(L, L->top, ud);
702 incr_top(L);
703 return uddata(ud);
706 LUA_API void lua_concat(lua_State *L, int n)
708 api_checknelems(L, n);
709 if (n >= 2) {
710 n--;
711 do {
712 TValue *top = lj_meta_cat(L, L->top-1, -n);
713 if (top == NULL) {
714 L->top -= n;
715 break;
717 n -= (int)(L->top - top);
718 L->top = top+2;
719 lj_vm_call(L, top, 1+1);
720 L->top--;
721 copyTV(L, L->top-1, L->top);
722 } while (--n > 0);
723 } else if (n == 0) { /* Push empty string. */
724 setstrV(L, L->top, &G(L)->strempty);
725 incr_top(L);
727 /* else n == 1: nothing to do. */
730 /* -- Object getters ------------------------------------------------------ */
732 LUA_API void lua_gettable(lua_State *L, int idx)
734 cTValue *v, *t = index2adr(L, idx);
735 api_checkvalidindex(L, t);
736 v = lj_meta_tget(L, t, L->top-1);
737 if (v == NULL) {
738 L->top += 2;
739 lj_vm_call(L, L->top-2, 1+1);
740 L->top -= 2;
741 v = L->top+1;
743 copyTV(L, L->top-1, v);
746 LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
748 cTValue *v, *t = index2adr(L, idx);
749 TValue key;
750 api_checkvalidindex(L, t);
751 setstrV(L, &key, lj_str_newz(L, k));
752 v = lj_meta_tget(L, t, &key);
753 if (v == NULL) {
754 L->top += 2;
755 lj_vm_call(L, L->top-2, 1+1);
756 L->top -= 2;
757 v = L->top+1;
759 copyTV(L, L->top, v);
760 incr_top(L);
763 LUA_API void lua_rawget(lua_State *L, int idx)
765 cTValue *t = index2adr(L, idx);
766 api_check(L, tvistab(t));
767 copyTV(L, L->top-1, lj_tab_get(L, tabV(t), L->top-1));
770 LUA_API void lua_rawgeti(lua_State *L, int idx, int n)
772 cTValue *v, *t = index2adr(L, idx);
773 api_check(L, tvistab(t));
774 v = lj_tab_getint(tabV(t), n);
775 if (v) {
776 copyTV(L, L->top, v);
777 } else {
778 setnilV(L->top);
780 incr_top(L);
783 LUA_API int lua_getmetatable(lua_State *L, int idx)
785 cTValue *o = index2adr(L, idx);
786 GCtab *mt = NULL;
787 if (tvistab(o))
788 mt = tabref(tabV(o)->metatable);
789 else if (tvisudata(o))
790 mt = tabref(udataV(o)->metatable);
791 else
792 mt = tabref(basemt_obj(G(L), o));
793 if (mt == NULL)
794 return 0;
795 settabV(L, L->top, mt);
796 incr_top(L);
797 return 1;
800 LUALIB_API int luaL_getmetafield(lua_State *L, int idx, const char *field)
802 if (lua_getmetatable(L, idx)) {
803 cTValue *tv = lj_tab_getstr(tabV(L->top-1), lj_str_newz(L, field));
804 if (tv && !tvisnil(tv)) {
805 copyTV(L, L->top-1, tv);
806 return 1;
808 L->top--;
810 return 0;
813 LUA_API void lua_getfenv(lua_State *L, int idx)
815 cTValue *o = index2adr(L, idx);
816 api_checkvalidindex(L, o);
817 if (tvisfunc(o)) {
818 settabV(L, L->top, tabref(funcV(o)->c.env));
819 } else if (tvisudata(o)) {
820 settabV(L, L->top, tabref(udataV(o)->env));
821 } else if (tvisthread(o)) {
822 settabV(L, L->top, tabref(threadV(o)->env));
823 } else {
824 setnilV(L->top);
826 incr_top(L);
829 LUA_API int lua_next(lua_State *L, int idx)
831 cTValue *t = index2adr(L, idx);
832 int more;
833 api_check(L, tvistab(t));
834 more = lj_tab_next(L, tabV(t), L->top-1);
835 if (more) {
836 incr_top(L); /* Return new key and value slot. */
837 } else { /* End of traversal. */
838 L->top--; /* Remove key slot. */
840 return more;
843 LUA_API const char *lua_getupvalue(lua_State *L, int idx, int n)
845 TValue *val;
846 const char *name = lj_debug_uvnamev(index2adr(L, idx), (uint32_t)(n-1), &val);
847 if (name) {
848 copyTV(L, L->top, val);
849 incr_top(L);
851 return name;
854 LUALIB_API void *luaL_checkudata(lua_State *L, int idx, const char *tname)
856 cTValue *o = index2adr(L, idx);
857 if (tvisudata(o)) {
858 GCudata *ud = udataV(o);
859 cTValue *tv = lj_tab_getstr(tabV(registry(L)), lj_str_newz(L, tname));
860 if (tv && tvistab(tv) && tabV(tv) == tabref(ud->metatable))
861 return uddata(ud);
863 lj_err_argtype(L, idx, tname);
864 return NULL; /* unreachable */
867 /* -- Object setters ------------------------------------------------------ */
869 LUA_API void lua_settable(lua_State *L, int idx)
871 TValue *o;
872 cTValue *t = index2adr(L, idx);
873 api_checknelems(L, 2);
874 api_checkvalidindex(L, t);
875 o = lj_meta_tset(L, t, L->top-2);
876 if (o) {
877 /* NOBARRIER: lj_meta_tset ensures the table is not black. */
878 copyTV(L, o, L->top-1);
879 L->top -= 2;
880 } else {
881 L->top += 3;
882 copyTV(L, L->top-1, L->top-6);
883 lj_vm_call(L, L->top-3, 0+1);
884 L->top -= 3;
888 LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
890 TValue *o;
891 TValue key;
892 cTValue *t = index2adr(L, idx);
893 api_checknelems(L, 1);
894 api_checkvalidindex(L, t);
895 setstrV(L, &key, lj_str_newz(L, k));
896 o = lj_meta_tset(L, t, &key);
897 if (o) {
898 L->top--;
899 /* NOBARRIER: lj_meta_tset ensures the table is not black. */
900 copyTV(L, o, L->top);
901 } else {
902 L->top += 3;
903 copyTV(L, L->top-1, L->top-6);
904 lj_vm_call(L, L->top-3, 0+1);
905 L->top -= 2;
909 LUA_API void lua_rawset(lua_State *L, int idx)
911 GCtab *t = tabV(index2adr(L, idx));
912 TValue *dst, *key;
913 api_checknelems(L, 2);
914 key = L->top-2;
915 dst = lj_tab_set(L, t, key);
916 copyTV(L, dst, key+1);
917 lj_gc_anybarriert(L, t);
918 L->top = key;
921 LUA_API void lua_rawseti(lua_State *L, int idx, int n)
923 GCtab *t = tabV(index2adr(L, idx));
924 TValue *dst, *src;
925 api_checknelems(L, 1);
926 dst = lj_tab_setint(L, t, n);
927 src = L->top-1;
928 copyTV(L, dst, src);
929 lj_gc_barriert(L, t, dst);
930 L->top = src;
933 LUA_API int lua_setmetatable(lua_State *L, int idx)
935 global_State *g;
936 GCtab *mt;
937 cTValue *o = index2adr(L, idx);
938 api_checknelems(L, 1);
939 api_checkvalidindex(L, o);
940 if (tvisnil(L->top-1)) {
941 mt = NULL;
942 } else {
943 api_check(L, tvistab(L->top-1));
944 mt = tabV(L->top-1);
946 g = G(L);
947 if (tvistab(o)) {
948 setgcref(tabV(o)->metatable, obj2gco(mt));
949 if (mt)
950 lj_gc_objbarriert(L, tabV(o), mt);
951 } else if (tvisudata(o)) {
952 setgcref(udataV(o)->metatable, obj2gco(mt));
953 if (mt)
954 lj_gc_objbarrier(L, udataV(o), mt);
955 } else {
956 /* Flush cache, since traces specialize to basemt. But not during __gc. */
957 if (lj_trace_flushall(L))
958 lj_err_caller(L, LJ_ERR_NOGCMM);
959 if (tvisbool(o)) {
960 /* NOBARRIER: basemt is a GC root. */
961 setgcref(basemt_it(g, LJ_TTRUE), obj2gco(mt));
962 setgcref(basemt_it(g, LJ_TFALSE), obj2gco(mt));
963 } else {
964 /* NOBARRIER: basemt is a GC root. */
965 setgcref(basemt_obj(g, o), obj2gco(mt));
968 L->top--;
969 return 1;
972 LUA_API int lua_setfenv(lua_State *L, int idx)
974 cTValue *o = index2adr(L, idx);
975 GCtab *t;
976 api_checknelems(L, 1);
977 api_checkvalidindex(L, o);
978 api_check(L, tvistab(L->top-1));
979 t = tabV(L->top-1);
980 if (tvisfunc(o)) {
981 setgcref(funcV(o)->c.env, obj2gco(t));
982 } else if (tvisudata(o)) {
983 setgcref(udataV(o)->env, obj2gco(t));
984 } else if (tvisthread(o)) {
985 setgcref(threadV(o)->env, obj2gco(t));
986 } else {
987 L->top--;
988 return 0;
990 lj_gc_objbarrier(L, gcV(o), t);
991 L->top--;
992 return 1;
995 LUA_API const char *lua_setupvalue(lua_State *L, int idx, int n)
997 cTValue *f = index2adr(L, idx);
998 TValue *val;
999 const char *name;
1000 api_checknelems(L, 1);
1001 name = lj_debug_uvnamev(f, (uint32_t)(n-1), &val);
1002 if (name) {
1003 L->top--;
1004 copyTV(L, val, L->top);
1005 lj_gc_barrier(L, funcV(f), L->top);
1007 return name;
1010 /* -- Calls --------------------------------------------------------------- */
1012 LUA_API void lua_call(lua_State *L, int nargs, int nresults)
1014 api_check(L, L->status == 0 || L->status == LUA_ERRERR);
1015 api_checknelems(L, nargs+1);
1016 lj_vm_call(L, L->top - nargs, nresults+1);
1019 LUA_API int lua_pcall(lua_State *L, int nargs, int nresults, int errfunc)
1021 global_State *g = G(L);
1022 uint8_t oldh = hook_save(g);
1023 ptrdiff_t ef;
1024 int status;
1025 api_check(L, L->status == 0 || L->status == LUA_ERRERR);
1026 api_checknelems(L, nargs+1);
1027 if (errfunc == 0) {
1028 ef = 0;
1029 } else {
1030 cTValue *o = stkindex2adr(L, errfunc);
1031 api_checkvalidindex(L, o);
1032 ef = savestack(L, o);
1034 status = lj_vm_pcall(L, L->top - nargs, nresults+1, ef);
1035 if (status) hook_restore(g, oldh);
1036 return status;
1039 static TValue *cpcall(lua_State *L, lua_CFunction func, void *ud)
1041 GCfunc *fn = lj_func_newC(L, 0, getcurrenv(L));
1042 fn->c.f = func;
1043 setfuncV(L, L->top, fn);
1044 setlightudV(L->top+1, checklightudptr(L, ud));
1045 cframe_nres(L->cframe) = 1+0; /* Zero results. */
1046 L->top += 2;
1047 return L->top-1; /* Now call the newly allocated C function. */
1050 LUA_API int lua_cpcall(lua_State *L, lua_CFunction func, void *ud)
1052 global_State *g = G(L);
1053 uint8_t oldh = hook_save(g);
1054 int status;
1055 api_check(L, L->status == 0 || L->status == LUA_ERRERR);
1056 status = lj_vm_cpcall(L, func, ud, cpcall);
1057 if (status) hook_restore(g, oldh);
1058 return status;
1061 LUALIB_API int luaL_callmeta(lua_State *L, int idx, const char *field)
1063 if (luaL_getmetafield(L, idx, field)) {
1064 TValue *base = L->top--;
1065 copyTV(L, base, index2adr(L, idx));
1066 L->top = base+1;
1067 lj_vm_call(L, base, 1+1);
1068 return 1;
1070 return 0;
1073 /* -- Coroutine yield and resume ------------------------------------------ */
1075 LUA_API int lua_yield(lua_State *L, int nresults)
1077 void *cf = L->cframe;
1078 global_State *g = G(L);
1079 if (cframe_canyield(cf)) {
1080 cf = cframe_raw(cf);
1081 if (!hook_active(g)) { /* Regular yield: move results down if needed. */
1082 cTValue *f = L->top - nresults;
1083 if (f > L->base) {
1084 TValue *t = L->base;
1085 while (--nresults >= 0) copyTV(L, t++, f++);
1086 L->top = t;
1088 } else { /* Yield from hook: add a pseudo-frame. */
1089 TValue *top = L->top;
1090 hook_leave(g);
1091 top->u64 = cframe_multres(cf);
1092 setcont(top+1, lj_cont_hook);
1093 setframe_pc(top+1, cframe_pc(cf)-1);
1094 setframe_gc(top+2, obj2gco(L));
1095 setframe_ftsz(top+2, (int)((char *)(top+3)-(char *)L->base)+FRAME_CONT);
1096 L->top = L->base = top+3;
1098 #if LJ_TARGET_X64
1099 lj_err_throw(L, LUA_YIELD);
1100 #else
1101 L->cframe = NULL;
1102 L->status = LUA_YIELD;
1103 lj_vm_unwind_c(cf, LUA_YIELD);
1104 #endif
1106 lj_err_msg(L, LJ_ERR_CYIELD);
1107 return 0; /* unreachable */
1110 LUA_API int lua_resume(lua_State *L, int nargs)
1112 if (L->cframe == NULL && L->status <= LUA_YIELD)
1113 return lj_vm_resume(L, L->top - nargs, 0, 0);
1114 L->top = L->base;
1115 setstrV(L, L->top, lj_err_str(L, LJ_ERR_COSUSP));
1116 incr_top(L);
1117 return LUA_ERRRUN;
1120 /* -- Load and dump Lua code ---------------------------------------------- */
1122 static TValue *cpparser(lua_State *L, lua_CFunction dummy, void *ud)
1124 LexState *ls = (LexState *)ud;
1125 GCproto *pt;
1126 GCfunc *fn;
1127 UNUSED(dummy);
1128 cframe_errfunc(L->cframe) = -1; /* Inherit error function. */
1129 pt = lj_lex_setup(L, ls) ? lj_bcread(ls) : lj_parse(ls);
1130 fn = lj_func_newL_empty(L, pt, tabref(L->env));
1131 /* Don't combine above/below into one statement. */
1132 setfuncV(L, L->top++, fn);
1133 return NULL;
1136 LUA_API int lua_load(lua_State *L, lua_Reader reader, void *data,
1137 const char *chunkname)
1139 LexState ls;
1140 int status;
1141 ls.rfunc = reader;
1142 ls.rdata = data;
1143 ls.chunkarg = chunkname ? chunkname : "?";
1144 lj_str_initbuf(&ls.sb);
1145 status = lj_vm_cpcall(L, NULL, &ls, cpparser);
1146 lj_lex_cleanup(L, &ls);
1147 lj_gc_check(L);
1148 return status;
1151 LUA_API int lua_dump(lua_State *L, lua_Writer writer, void *data)
1153 cTValue *o = L->top-1;
1154 api_checknelems(L, 1);
1155 if (tvisfunc(o) && isluafunc(funcV(o)))
1156 return lj_bcwrite(L, funcproto(funcV(o)), writer, data, 0);
1157 else
1158 return 1;
1161 /* -- GC and memory management -------------------------------------------- */
1163 LUA_API int lua_gc(lua_State *L, int what, int data)
1165 global_State *g = G(L);
1166 int res = 0;
1167 switch (what) {
1168 case LUA_GCSTOP:
1169 g->gc.threshold = LJ_MAX_MEM;
1170 break;
1171 case LUA_GCRESTART:
1172 g->gc.threshold = data == -1 ? (g->gc.total/100)*g->gc.pause : g->gc.total;
1173 break;
1174 case LUA_GCCOLLECT:
1175 lj_gc_fullgc(L);
1176 break;
1177 case LUA_GCCOUNT:
1178 res = (int)(g->gc.total >> 10);
1179 break;
1180 case LUA_GCCOUNTB:
1181 res = (int)(g->gc.total & 0x3ff);
1182 break;
1183 case LUA_GCSTEP: {
1184 MSize a = (MSize)data << 10;
1185 g->gc.threshold = (a <= g->gc.total) ? (g->gc.total - a) : 0;
1186 while (g->gc.total >= g->gc.threshold)
1187 if (lj_gc_step(L)) {
1188 res = 1;
1189 break;
1191 break;
1193 case LUA_GCSETPAUSE:
1194 res = (int)(g->gc.pause);
1195 g->gc.pause = (MSize)data;
1196 break;
1197 case LUA_GCSETSTEPMUL:
1198 res = (int)(g->gc.stepmul);
1199 g->gc.stepmul = (MSize)data;
1200 break;
1201 default:
1202 res = -1; /* Invalid option. */
1204 return res;
1207 LUA_API lua_Alloc lua_getallocf(lua_State *L, void **ud)
1209 global_State *g = G(L);
1210 if (ud) *ud = g->allocd;
1211 return g->allocf;
1214 LUA_API void lua_setallocf(lua_State *L, lua_Alloc f, void *ud)
1216 global_State *g = G(L);
1217 g->allocd = ud;
1218 g->allocf = f;