src/lj_api.c - luajit-2.0-src

Functions defined

Macros defined

Source code

  1. /*
  2. ** Public Lua/C API.
  3. ** Copyright (C) 2005-2015 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. #define lj_api_c
  9. #define LUA_CORE

  10. #include "lj_obj.h"
  11. #include "lj_gc.h"
  12. #include "lj_err.h"
  13. #include "lj_debug.h"
  14. #include "lj_str.h"
  15. #include "lj_tab.h"
  16. #include "lj_func.h"
  17. #include "lj_udata.h"
  18. #include "lj_meta.h"
  19. #include "lj_state.h"
  20. #include "lj_bc.h"
  21. #include "lj_frame.h"
  22. #include "lj_trace.h"
  23. #include "lj_vm.h"
  24. #include "lj_strscan.h"
  25. #include "lj_strfmt.h"

  26. /* -- Common helper functions --------------------------------------------- */

  27. #define api_checknelems(L, n)                api_check(L, (n) <= (L->top - L->base))
  28. #define api_checkvalidindex(L, i)        api_check(L, (i) != niltv(L))

  29. static TValue *index2adr(lua_State *L, int idx)
  30. {
  31.   if (idx > 0) {
  32.     TValue *o = L->base + (idx - 1);
  33.     return o < L->top ? o : niltv(L);
  34.   } else if (idx > LUA_REGISTRYINDEX) {
  35.     api_check(L, idx != 0 && -idx <= L->top - L->base);
  36.     return L->top + idx;
  37.   } else if (idx == LUA_GLOBALSINDEX) {
  38.     TValue *o = &G(L)->tmptv;
  39.     settabV(L, o, tabref(L->env));
  40.     return o;
  41.   } else if (idx == LUA_REGISTRYINDEX) {
  42.     return registry(L);
  43.   } else {
  44.     GCfunc *fn = curr_func(L);
  45.     api_check(L, fn->c.gct == ~LJ_TFUNC && !isluafunc(fn));
  46.     if (idx == LUA_ENVIRONINDEX) {
  47.       TValue *o = &G(L)->tmptv;
  48.       settabV(L, o, tabref(fn->c.env));
  49.       return o;
  50.     } else {
  51.       idx = LUA_GLOBALSINDEX - idx;
  52.       return idx <= fn->c.nupvalues ? &fn->c.upvalue[idx-1] : niltv(L);
  53.     }
  54.   }
  55. }

  56. static TValue *stkindex2adr(lua_State *L, int idx)
  57. {
  58.   if (idx > 0) {
  59.     TValue *o = L->base + (idx - 1);
  60.     return o < L->top ? o : niltv(L);
  61.   } else {
  62.     api_check(L, idx != 0 && -idx <= L->top - L->base);
  63.     return L->top + idx;
  64.   }
  65. }

  66. static GCtab *getcurrenv(lua_State *L)
  67. {
  68.   GCfunc *fn = curr_func(L);
  69.   return fn->c.gct == ~LJ_TFUNC ? tabref(fn->c.env) : tabref(L->env);
  70. }

  71. /* -- Miscellaneous API functions ----------------------------------------- */

  72. LUA_API int lua_status(lua_State *L)
  73. {
  74.   return L->status;
  75. }

  76. LUA_API int lua_checkstack(lua_State *L, int size)
  77. {
  78.   if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK) {
  79.     return 0/* Stack overflow. */
  80.   } else if (size > 0) {
  81.     lj_state_checkstack(L, (MSize)size);
  82.   }
  83.   return 1;
  84. }

  85. LUALIB_API void luaL_checkstack(lua_State *L, int size, const char *msg)
  86. {
  87.   if (!lua_checkstack(L, size))
  88.     lj_err_callerv(L, LJ_ERR_STKOVM, msg);
  89. }

  90. LUA_API void lua_xmove(lua_State *from, lua_State *to, int n)
  91. {
  92.   TValue *f, *t;
  93.   if (from == to) return;
  94.   api_checknelems(from, n);
  95.   api_check(from, G(from) == G(to));
  96.   lj_state_checkstack(to, (MSize)n);
  97.   f = from->top;
  98.   t = to->top = to->top + n;
  99.   while (--n >= 0) copyTV(to, --t, --f);
  100.   from->top = f;
  101. }

  102. /* -- Stack manipulation -------------------------------------------------- */

  103. LUA_API int lua_gettop(lua_State *L)
  104. {
  105.   return (int)(L->top - L->base);
  106. }

  107. LUA_API void lua_settop(lua_State *L, int idx)
  108. {
  109.   if (idx >= 0) {
  110.     api_check(L, idx <= tvref(L->maxstack) - L->base);
  111.     if (L->base + idx > L->top) {
  112.       if (L->base + idx >= tvref(L->maxstack))
  113.         lj_state_growstack(L, (MSize)idx - (MSize)(L->top - L->base));
  114.       do { setnilV(L->top++); } while (L->top < L->base + idx);
  115.     } else {
  116.       L->top = L->base + idx;
  117.     }
  118.   } else {
  119.     api_check(L, -(idx+1) <= (L->top - L->base));
  120.     L->top += idx+1/* Shrinks top (idx < 0). */
  121.   }
  122. }

  123. LUA_API void lua_remove(lua_State *L, int idx)
  124. {
  125.   TValue *p = stkindex2adr(L, idx);
  126.   api_checkvalidindex(L, p);
  127.   while (++p < L->top) copyTV(L, p-1, p);
  128.   L->top--;
  129. }

  130. LUA_API void lua_insert(lua_State *L, int idx)
  131. {
  132.   TValue *q, *p = stkindex2adr(L, idx);
  133.   api_checkvalidindex(L, p);
  134.   for (q = L->top; q > p; q--) copyTV(L, q, q-1);
  135.   copyTV(L, p, L->top);
  136. }

  137. LUA_API void lua_replace(lua_State *L, int idx)
  138. {
  139.   api_checknelems(L, 1);
  140.   if (idx == LUA_GLOBALSINDEX) {
  141.     api_check(L, tvistab(L->top-1));
  142.     /* NOBARRIER: A thread (i.e. L) is never black. */
  143.     setgcref(L->env, obj2gco(tabV(L->top-1)));
  144.   } else if (idx == LUA_ENVIRONINDEX) {
  145.     GCfunc *fn = curr_func(L);
  146.     if (fn->c.gct != ~LJ_TFUNC)
  147.       lj_err_msg(L, LJ_ERR_NOENV);
  148.     api_check(L, tvistab(L->top-1));
  149.     setgcref(fn->c.env, obj2gco(tabV(L->top-1)));
  150.     lj_gc_barrier(L, fn, L->top-1);
  151.   } else {
  152.     TValue *o = index2adr(L, idx);
  153.     api_checkvalidindex(L, o);
  154.     copyTV(L, o, L->top-1);
  155.     if (idx < LUA_GLOBALSINDEX/* Need a barrier for upvalues. */
  156.       lj_gc_barrier(L, curr_func(L), L->top-1);
  157.   }
  158.   L->top--;
  159. }

  160. LUA_API void lua_pushvalue(lua_State *L, int idx)
  161. {
  162.   copyTV(L, L->top, index2adr(L, idx));
  163.   incr_top(L);
  164. }

  165. /* -- Stack getters ------------------------------------------------------- */

  166. LUA_API int lua_type(lua_State *L, int idx)
  167. {
  168.   cTValue *o = index2adr(L, idx);
  169.   if (tvisnumber(o)) {
  170.     return LUA_TNUMBER;
  171. #if LJ_64 && !LJ_GC64
  172.   } else if (tvislightud(o)) {
  173.     return LUA_TLIGHTUSERDATA;
  174. #endif
  175.   } else if (o == niltv(L)) {
  176.     return LUA_TNONE;
  177.   } else/* Magic internal/external tag conversion. ORDER LJ_T */
  178.     uint32_t t = ~itype(o);
  179. #if LJ_64
  180.     int tt = (int)((U64x(75a06,98042110) >> 4*t) & 15u);
  181. #else
  182.     int tt = (int)(((t < 8 ? 0x98042110u : 0x75a06u) >> 4*(t&7)) & 15u);
  183. #endif
  184.     lua_assert(tt != LUA_TNIL || tvisnil(o));
  185.     return tt;
  186.   }
  187. }

  188. LUALIB_API void luaL_checktype(lua_State *L, int idx, int tt)
  189. {
  190.   if (lua_type(L, idx) != tt)
  191.     lj_err_argt(L, idx, tt);
  192. }

  193. LUALIB_API void luaL_checkany(lua_State *L, int idx)
  194. {
  195.   if (index2adr(L, idx) == niltv(L))
  196.     lj_err_arg(L, idx, LJ_ERR_NOVAL);
  197. }

  198. LUA_API const char *lua_typename(lua_State *L, int t)
  199. {
  200.   UNUSED(L);
  201.   return lj_obj_typename[t+1];
  202. }

  203. LUA_API int lua_iscfunction(lua_State *L, int idx)
  204. {
  205.   cTValue *o = index2adr(L, idx);
  206.   return tvisfunc(o) && !isluafunc(funcV(o));
  207. }

  208. LUA_API int lua_isnumber(lua_State *L, int idx)
  209. {
  210.   cTValue *o = index2adr(L, idx);
  211.   TValue tmp;
  212.   return (tvisnumber(o) || (tvisstr(o) && lj_strscan_number(strV(o), &tmp)));
  213. }

  214. LUA_API int lua_isstring(lua_State *L, int idx)
  215. {
  216.   cTValue *o = index2adr(L, idx);
  217.   return (tvisstr(o) || tvisnumber(o));
  218. }

  219. LUA_API int lua_isuserdata(lua_State *L, int idx)
  220. {
  221.   cTValue *o = index2adr(L, idx);
  222.   return (tvisudata(o) || tvislightud(o));
  223. }

  224. LUA_API int lua_rawequal(lua_State *L, int idx1, int idx2)
  225. {
  226.   cTValue *o1 = index2adr(L, idx1);
  227.   cTValue *o2 = index2adr(L, idx2);
  228.   return (o1 == niltv(L) || o2 == niltv(L)) ? 0 : lj_obj_equal(o1, o2);
  229. }

  230. LUA_API int lua_equal(lua_State *L, int idx1, int idx2)
  231. {
  232.   cTValue *o1 = index2adr(L, idx1);
  233.   cTValue *o2 = index2adr(L, idx2);
  234.   if (tvisint(o1) && tvisint(o2)) {
  235.     return intV(o1) == intV(o2);
  236.   } else if (tvisnumber(o1) && tvisnumber(o2)) {
  237.     return numberVnum(o1) == numberVnum(o2);
  238.   } else if (itype(o1) != itype(o2)) {
  239.     return 0;
  240.   } else if (tvispri(o1)) {
  241.     return o1 != niltv(L) && o2 != niltv(L);
  242. #if LJ_64 && !LJ_GC64
  243.   } else if (tvislightud(o1)) {
  244.     return o1->u64 == o2->u64;
  245. #endif
  246.   } else if (gcrefeq(o1->gcr, o2->gcr)) {
  247.     return 1;
  248.   } else if (!tvistabud(o1)) {
  249.     return 0;
  250.   } else {
  251.     TValue *base = lj_meta_equal(L, gcV(o1), gcV(o2), 0);
  252.     if ((uintptr_t)base <= 1) {
  253.       return (int)(uintptr_t)base;
  254.     } else {
  255.       L->top = base+2;
  256.       lj_vm_call(L, base, 1+1);
  257.       L->top -= 2+LJ_FR2;
  258.       return tvistruecond(L->top+1+LJ_FR2);
  259.     }
  260.   }
  261. }

  262. LUA_API int lua_lessthan(lua_State *L, int idx1, int idx2)
  263. {
  264.   cTValue *o1 = index2adr(L, idx1);
  265.   cTValue *o2 = index2adr(L, idx2);
  266.   if (o1 == niltv(L) || o2 == niltv(L)) {
  267.     return 0;
  268.   } else if (tvisint(o1) && tvisint(o2)) {
  269.     return intV(o1) < intV(o2);
  270.   } else if (tvisnumber(o1) && tvisnumber(o2)) {
  271.     return numberVnum(o1) < numberVnum(o2);
  272.   } else {
  273.     TValue *base = lj_meta_comp(L, o1, o2, 0);
  274.     if ((uintptr_t)base <= 1) {
  275.       return (int)(uintptr_t)base;
  276.     } else {
  277.       L->top = base+2;
  278.       lj_vm_call(L, base, 1+1);
  279.       L->top -= 2+LJ_FR2;
  280.       return tvistruecond(L->top+1+LJ_FR2);
  281.     }
  282.   }
  283. }

  284. LUA_API lua_Number lua_tonumber(lua_State *L, int idx)
  285. {
  286.   cTValue *o = index2adr(L, idx);
  287.   TValue tmp;
  288.   if (LJ_LIKELY(tvisnumber(o)))
  289.     return numberVnum(o);
  290.   else if (tvisstr(o) && lj_strscan_num(strV(o), &tmp))
  291.     return numV(&tmp);
  292.   else
  293.     return 0;
  294. }

  295. LUALIB_API lua_Number luaL_checknumber(lua_State *L, int idx)
  296. {
  297.   cTValue *o = index2adr(L, idx);
  298.   TValue tmp;
  299.   if (LJ_LIKELY(tvisnumber(o)))
  300.     return numberVnum(o);
  301.   else if (!(tvisstr(o) && lj_strscan_num(strV(o), &tmp)))
  302.     lj_err_argt(L, idx, LUA_TNUMBER);
  303.   return numV(&tmp);
  304. }

  305. LUALIB_API lua_Number luaL_optnumber(lua_State *L, int idx, lua_Number def)
  306. {
  307.   cTValue *o = index2adr(L, idx);
  308.   TValue tmp;
  309.   if (LJ_LIKELY(tvisnumber(o)))
  310.     return numberVnum(o);
  311.   else if (tvisnil(o))
  312.     return def;
  313.   else if (!(tvisstr(o) && lj_strscan_num(strV(o), &tmp)))
  314.     lj_err_argt(L, idx, LUA_TNUMBER);
  315.   return numV(&tmp);
  316. }

  317. LUA_API lua_Integer lua_tointeger(lua_State *L, int idx)
  318. {
  319.   cTValue *o = index2adr(L, idx);
  320.   TValue tmp;
  321.   lua_Number n;
  322.   if (LJ_LIKELY(tvisint(o))) {
  323.     return intV(o);
  324.   } else if (LJ_LIKELY(tvisnum(o))) {
  325.     n = numV(o);
  326.   } else {
  327.     if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp)))
  328.       return 0;
  329.     if (tvisint(&tmp))
  330.       return (lua_Integer)intV(&tmp);
  331.     n = numV(&tmp);
  332.   }
  333. #if LJ_64
  334.   return (lua_Integer)n;
  335. #else
  336.   return lj_num2int(n);
  337. #endif
  338. }

  339. LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int idx)
  340. {
  341.   cTValue *o = index2adr(L, idx);
  342.   TValue tmp;
  343.   lua_Number n;
  344.   if (LJ_LIKELY(tvisint(o))) {
  345.     return intV(o);
  346.   } else if (LJ_LIKELY(tvisnum(o))) {
  347.     n = numV(o);
  348.   } else {
  349.     if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp)))
  350.       lj_err_argt(L, idx, LUA_TNUMBER);
  351.     if (tvisint(&tmp))
  352.       return (lua_Integer)intV(&tmp);
  353.     n = numV(&tmp);
  354.   }
  355. #if LJ_64
  356.   return (lua_Integer)n;
  357. #else
  358.   return lj_num2int(n);
  359. #endif
  360. }

  361. LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int idx, lua_Integer def)
  362. {
  363.   cTValue *o = index2adr(L, idx);
  364.   TValue tmp;
  365.   lua_Number n;
  366.   if (LJ_LIKELY(tvisint(o))) {
  367.     return intV(o);
  368.   } else if (LJ_LIKELY(tvisnum(o))) {
  369.     n = numV(o);
  370.   } else if (tvisnil(o)) {
  371.     return def;
  372.   } else {
  373.     if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp)))
  374.       lj_err_argt(L, idx, LUA_TNUMBER);
  375.     if (tvisint(&tmp))
  376.       return (lua_Integer)intV(&tmp);
  377.     n = numV(&tmp);
  378.   }
  379. #if LJ_64
  380.   return (lua_Integer)n;
  381. #else
  382.   return lj_num2int(n);
  383. #endif
  384. }

  385. LUA_API int lua_toboolean(lua_State *L, int idx)
  386. {
  387.   cTValue *o = index2adr(L, idx);
  388.   return tvistruecond(o);
  389. }

  390. LUA_API const char *lua_tolstring(lua_State *L, int idx, size_t *len)
  391. {
  392.   TValue *o = index2adr(L, idx);
  393.   GCstr *s;
  394.   if (LJ_LIKELY(tvisstr(o))) {
  395.     s = strV(o);
  396.   } else if (tvisnumber(o)) {
  397.     lj_gc_check(L);
  398.     o = index2adr(L, idx);  /* GC may move the stack. */
  399.     s = lj_strfmt_number(L, o);
  400.     setstrV(L, o, s);
  401.   } else {
  402.     if (len != NULL) *len = 0;
  403.     return NULL;
  404.   }
  405.   if (len != NULL) *len = s->len;
  406.   return strdata(s);
  407. }

  408. LUALIB_API const char *luaL_checklstring(lua_State *L, int idx, size_t *len)
  409. {
  410.   TValue *o = index2adr(L, idx);
  411.   GCstr *s;
  412.   if (LJ_LIKELY(tvisstr(o))) {
  413.     s = strV(o);
  414.   } else if (tvisnumber(o)) {
  415.     lj_gc_check(L);
  416.     o = index2adr(L, idx);  /* GC may move the stack. */
  417.     s = lj_strfmt_number(L, o);
  418.     setstrV(L, o, s);
  419.   } else {
  420.     lj_err_argt(L, idx, LUA_TSTRING);
  421.   }
  422.   if (len != NULL) *len = s->len;
  423.   return strdata(s);
  424. }

  425. LUALIB_API const char *luaL_optlstring(lua_State *L, int idx,
  426.                                        const char *def, size_t *len)
  427. {
  428.   TValue *o = index2adr(L, idx);
  429.   GCstr *s;
  430.   if (LJ_LIKELY(tvisstr(o))) {
  431.     s = strV(o);
  432.   } else if (tvisnil(o)) {
  433.     if (len != NULL) *len = def ? strlen(def) : 0;
  434.     return def;
  435.   } else if (tvisnumber(o)) {
  436.     lj_gc_check(L);
  437.     o = index2adr(L, idx);  /* GC may move the stack. */
  438.     s = lj_strfmt_number(L, o);
  439.     setstrV(L, o, s);
  440.   } else {
  441.     lj_err_argt(L, idx, LUA_TSTRING);
  442.   }
  443.   if (len != NULL) *len = s->len;
  444.   return strdata(s);
  445. }

  446. LUALIB_API int luaL_checkoption(lua_State *L, int idx, const char *def,
  447.                                 const char *const lst[])
  448. {
  449.   ptrdiff_t i;
  450.   const char *s = lua_tolstring(L, idx, NULL);
  451.   if (s == NULL && (s = def) == NULL)
  452.     lj_err_argt(L, idx, LUA_TSTRING);
  453.   for (i = 0; lst[i]; i++)
  454.     if (strcmp(lst[i], s) == 0)
  455.       return (int)i;
  456.   lj_err_argv(L, idx, LJ_ERR_INVOPTM, s);
  457. }

  458. LUA_API size_t lua_objlen(lua_State *L, int idx)
  459. {
  460.   TValue *o = index2adr(L, idx);
  461.   if (tvisstr(o)) {
  462.     return strV(o)->len;
  463.   } else if (tvistab(o)) {
  464.     return (size_t)lj_tab_len(tabV(o));
  465.   } else if (tvisudata(o)) {
  466.     return udataV(o)->len;
  467.   } else if (tvisnumber(o)) {
  468.     GCstr *s = lj_strfmt_number(L, o);
  469.     setstrV(L, o, s);
  470.     return s->len;
  471.   } else {
  472.     return 0;
  473.   }
  474. }

  475. LUA_API lua_CFunction lua_tocfunction(lua_State *L, int idx)
  476. {
  477.   cTValue *o = index2adr(L, idx);
  478.   if (tvisfunc(o)) {
  479.     BCOp op = bc_op(*mref(funcV(o)->c.pc, BCIns));
  480.     if (op == BC_FUNCC || op == BC_FUNCCW)
  481.       return funcV(o)->c.f;
  482.   }
  483.   return NULL;
  484. }

  485. LUA_API void *lua_touserdata(lua_State *L, int idx)
  486. {
  487.   cTValue *o = index2adr(L, idx);
  488.   if (tvisudata(o))
  489.     return uddata(udataV(o));
  490.   else if (tvislightud(o))
  491.     return lightudV(o);
  492.   else
  493.     return NULL;
  494. }

  495. LUA_API lua_State *lua_tothread(lua_State *L, int idx)
  496. {
  497.   cTValue *o = index2adr(L, idx);
  498.   return (!tvisthread(o)) ? NULL : threadV(o);
  499. }

  500. LUA_API const void *lua_topointer(lua_State *L, int idx)
  501. {
  502.   return lj_obj_ptr(index2adr(L, idx));
  503. }

  504. /* -- Stack setters (object creation) ------------------------------------- */

  505. LUA_API void lua_pushnil(lua_State *L)
  506. {
  507.   setnilV(L->top);
  508.   incr_top(L);
  509. }

  510. LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
  511. {
  512.   setnumV(L->top, n);
  513.   if (LJ_UNLIKELY(tvisnan(L->top)))
  514.     setnanV(L->top);  /* Canonicalize injected NaNs. */
  515.   incr_top(L);
  516. }

  517. LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
  518. {
  519.   setintptrV(L->top, n);
  520.   incr_top(L);
  521. }

  522. LUA_API void lua_pushlstring(lua_State *L, const char *str, size_t len)
  523. {
  524.   GCstr *s;
  525.   lj_gc_check(L);
  526.   s = lj_str_new(L, str, len);
  527.   setstrV(L, L->top, s);
  528.   incr_top(L);
  529. }

  530. LUA_API void lua_pushstring(lua_State *L, const char *str)
  531. {
  532.   if (str == NULL) {
  533.     setnilV(L->top);
  534.   } else {
  535.     GCstr *s;
  536.     lj_gc_check(L);
  537.     s = lj_str_newz(L, str);
  538.     setstrV(L, L->top, s);
  539.   }
  540.   incr_top(L);
  541. }

  542. LUA_API const char *lua_pushvfstring(lua_State *L, const char *fmt,
  543.                                      va_list argp)
  544. {
  545.   lj_gc_check(L);
  546.   return lj_strfmt_pushvf(L, fmt, argp);
  547. }

  548. LUA_API const char *lua_pushfstring(lua_State *L, const char *fmt, ...)
  549. {
  550.   const char *ret;
  551.   va_list argp;
  552.   lj_gc_check(L);
  553.   va_start(argp, fmt);
  554.   ret = lj_strfmt_pushvf(L, fmt, argp);
  555.   va_end(argp);
  556.   return ret;
  557. }

  558. LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction f, int n)
  559. {
  560.   GCfunc *fn;
  561.   lj_gc_check(L);
  562.   api_checknelems(L, n);
  563.   fn = lj_func_newC(L, (MSize)n, getcurrenv(L));
  564.   fn->c.f = f;
  565.   L->top -= n;
  566.   while (n--)
  567.     copyTV(L, &fn->c.upvalue[n], L->top+n);
  568.   setfuncV(L, L->top, fn);
  569.   lua_assert(iswhite(obj2gco(fn)));
  570.   incr_top(L);
  571. }

  572. LUA_API void lua_pushboolean(lua_State *L, int b)
  573. {
  574.   setboolV(L->top, (b != 0));
  575.   incr_top(L);
  576. }

  577. LUA_API void lua_pushlightuserdata(lua_State *L, void *p)
  578. {
  579.   setlightudV(L->top, checklightudptr(L, p));
  580.   incr_top(L);
  581. }

  582. LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
  583. {
  584.   lj_gc_check(L);
  585.   settabV(L, L->top, lj_tab_new_ah(L, narray, nrec));
  586.   incr_top(L);
  587. }

  588. LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname)
  589. {
  590.   GCtab *regt = tabV(registry(L));
  591.   TValue *tv = lj_tab_setstr(L, regt, lj_str_newz(L, tname));
  592.   if (tvisnil(tv)) {
  593.     GCtab *mt = lj_tab_new(L, 0, 1);
  594.     settabV(L, tv, mt);
  595.     settabV(L, L->top++, mt);
  596.     lj_gc_anybarriert(L, regt);
  597.     return 1;
  598.   } else {
  599.     copyTV(L, L->top++, tv);
  600.     return 0;
  601.   }
  602. }

  603. LUA_API int lua_pushthread(lua_State *L)
  604. {
  605.   setthreadV(L, L->top, L);
  606.   incr_top(L);
  607.   return (mainthread(G(L)) == L);
  608. }

  609. LUA_API lua_State *lua_newthread(lua_State *L)
  610. {
  611.   lua_State *L1;
  612.   lj_gc_check(L);
  613.   L1 = lj_state_new(L);
  614.   setthreadV(L, L->top, L1);
  615.   incr_top(L);
  616.   return L1;
  617. }

  618. LUA_API void *lua_newuserdata(lua_State *L, size_t size)
  619. {
  620.   GCudata *ud;
  621.   lj_gc_check(L);
  622.   if (size > LJ_MAX_UDATA)
  623.     lj_err_msg(L, LJ_ERR_UDATAOV);
  624.   ud = lj_udata_new(L, (MSize)size, getcurrenv(L));
  625.   setudataV(L, L->top, ud);
  626.   incr_top(L);
  627.   return uddata(ud);
  628. }

  629. LUA_API void lua_concat(lua_State *L, int n)
  630. {
  631.   api_checknelems(L, n);
  632.   if (n >= 2) {
  633.     n--;
  634.     do {
  635.       TValue *top = lj_meta_cat(L, L->top-1, -n);
  636.       if (top == NULL) {
  637.         L->top -= n;
  638.         break;
  639.       }
  640.       n -= (int)(L->top - top);
  641.       L->top = top+2;
  642.       lj_vm_call(L, top, 1+1);
  643.       L->top -= 1+LJ_FR2;
  644.       copyTV(L, L->top-1, L->top+LJ_FR2);
  645.     } while (--n > 0);
  646.   } else if (n == 0) {  /* Push empty string. */
  647.     setstrV(L, L->top, &G(L)->strempty);
  648.     incr_top(L);
  649.   }
  650.   /* else n == 1: nothing to do. */
  651. }

  652. /* -- Object getters ------------------------------------------------------ */

  653. LUA_API void lua_gettable(lua_State *L, int idx)
  654. {
  655.   cTValue *v, *t = index2adr(L, idx);
  656.   api_checkvalidindex(L, t);
  657.   v = lj_meta_tget(L, t, L->top-1);
  658.   if (v == NULL) {
  659.     L->top += 2;
  660.     lj_vm_call(L, L->top-2, 1+1);
  661.     L->top -= 2+LJ_FR2;
  662.     v = L->top+1+LJ_FR2;
  663.   }
  664.   copyTV(L, L->top-1, v);
  665. }

  666. LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
  667. {
  668.   cTValue *v, *t = index2adr(L, idx);
  669.   TValue key;
  670.   api_checkvalidindex(L, t);
  671.   setstrV(L, &key, lj_str_newz(L, k));
  672.   v = lj_meta_tget(L, t, &key);
  673.   if (v == NULL) {
  674.     L->top += 2;
  675.     lj_vm_call(L, L->top-2, 1+1);
  676.     L->top -= 2+LJ_FR2;
  677.     v = L->top+1+LJ_FR2;
  678.   }
  679.   copyTV(L, L->top, v);
  680.   incr_top(L);
  681. }

  682. LUA_API void lua_rawget(lua_State *L, int idx)
  683. {
  684.   cTValue *t = index2adr(L, idx);
  685.   api_check(L, tvistab(t));
  686.   copyTV(L, L->top-1, lj_tab_get(L, tabV(t), L->top-1));
  687. }

  688. LUA_API void lua_rawgeti(lua_State *L, int idx, int n)
  689. {
  690.   cTValue *v, *t = index2adr(L, idx);
  691.   api_check(L, tvistab(t));
  692.   v = lj_tab_getint(tabV(t), n);
  693.   if (v) {
  694.     copyTV(L, L->top, v);
  695.   } else {
  696.     setnilV(L->top);
  697.   }
  698.   incr_top(L);
  699. }

  700. LUA_API int lua_getmetatable(lua_State *L, int idx)
  701. {
  702.   cTValue *o = index2adr(L, idx);
  703.   GCtab *mt = NULL;
  704.   if (tvistab(o))
  705.     mt = tabref(tabV(o)->metatable);
  706.   else if (tvisudata(o))
  707.     mt = tabref(udataV(o)->metatable);
  708.   else
  709.     mt = tabref(basemt_obj(G(L), o));
  710.   if (mt == NULL)
  711.     return 0;
  712.   settabV(L, L->top, mt);
  713.   incr_top(L);
  714.   return 1;
  715. }

  716. LUALIB_API int luaL_getmetafield(lua_State *L, int idx, const char *field)
  717. {
  718.   if (lua_getmetatable(L, idx)) {
  719.     cTValue *tv = lj_tab_getstr(tabV(L->top-1), lj_str_newz(L, field));
  720.     if (tv && !tvisnil(tv)) {
  721.       copyTV(L, L->top-1, tv);
  722.       return 1;
  723.     }
  724.     L->top--;
  725.   }
  726.   return 0;
  727. }

  728. LUA_API void lua_getfenv(lua_State *L, int idx)
  729. {
  730.   cTValue *o = index2adr(L, idx);
  731.   api_checkvalidindex(L, o);
  732.   if (tvisfunc(o)) {
  733.     settabV(L, L->top, tabref(funcV(o)->c.env));
  734.   } else if (tvisudata(o)) {
  735.     settabV(L, L->top, tabref(udataV(o)->env));
  736.   } else if (tvisthread(o)) {
  737.     settabV(L, L->top, tabref(threadV(o)->env));
  738.   } else {
  739.     setnilV(L->top);
  740.   }
  741.   incr_top(L);
  742. }

  743. LUA_API int lua_next(lua_State *L, int idx)
  744. {
  745.   cTValue *t = index2adr(L, idx);
  746.   int more;
  747.   api_check(L, tvistab(t));
  748.   more = lj_tab_next(L, tabV(t), L->top-1);
  749.   if (more) {
  750.     incr_top(L);  /* Return new key and value slot. */
  751.   } else/* End of traversal. */
  752.     L->top--;  /* Remove key slot. */
  753.   }
  754.   return more;
  755. }

  756. LUA_API const char *lua_getupvalue(lua_State *L, int idx, int n)
  757. {
  758.   TValue *val;
  759.   const char *name = lj_debug_uvnamev(index2adr(L, idx), (uint32_t)(n-1), &val);
  760.   if (name) {
  761.     copyTV(L, L->top, val);
  762.     incr_top(L);
  763.   }
  764.   return name;
  765. }

  766. LUA_API void *lua_upvalueid(lua_State *L, int idx, int n)
  767. {
  768.   GCfunc *fn = funcV(index2adr(L, idx));
  769.   n--;
  770.   api_check(L, (uint32_t)n < fn->l.nupvalues);
  771.   return isluafunc(fn) ? (void *)gcref(fn->l.uvptr[n]) :
  772.                          (void *)&fn->c.upvalue[n];
  773. }

  774. LUA_API void lua_upvaluejoin(lua_State *L, int idx1, int n1, int idx2, int n2)
  775. {
  776.   GCfunc *fn1 = funcV(index2adr(L, idx1));
  777.   GCfunc *fn2 = funcV(index2adr(L, idx2));
  778.   n1--; n2--;
  779.   api_check(L, isluafunc(fn1) && (uint32_t)n1 < fn1->l.nupvalues);
  780.   api_check(L, isluafunc(fn2) && (uint32_t)n2 < fn2->l.nupvalues);
  781.   setgcrefr(fn1->l.uvptr[n1], fn2->l.uvptr[n2]);
  782.   lj_gc_objbarrier(L, fn1, gcref(fn1->l.uvptr[n1]));
  783. }

  784. LUALIB_API void *luaL_checkudata(lua_State *L, int idx, const char *tname)
  785. {
  786.   cTValue *o = index2adr(L, idx);
  787.   if (tvisudata(o)) {
  788.     GCudata *ud = udataV(o);
  789.     cTValue *tv = lj_tab_getstr(tabV(registry(L)), lj_str_newz(L, tname));
  790.     if (tv && tvistab(tv) && tabV(tv) == tabref(ud->metatable))
  791.       return uddata(ud);
  792.   }
  793.   lj_err_argtype(L, idx, tname);
  794.   return NULL/* unreachable */
  795. }

  796. /* -- Object setters ------------------------------------------------------ */

  797. LUA_API void lua_settable(lua_State *L, int idx)
  798. {
  799.   TValue *o;
  800.   cTValue *t = index2adr(L, idx);
  801.   api_checknelems(L, 2);
  802.   api_checkvalidindex(L, t);
  803.   o = lj_meta_tset(L, t, L->top-2);
  804.   if (o) {
  805.     /* NOBARRIER: lj_meta_tset ensures the table is not black. */
  806.     L->top -= 2;
  807.     copyTV(L, o, L->top+1);
  808.   } else {
  809.     TValue *base = L->top;
  810.     copyTV(L, base+2, base-3-2*LJ_FR2);
  811.     L->top = base+3;
  812.     lj_vm_call(L, base, 0+1);
  813.     L->top -= 3+LJ_FR2;
  814.   }
  815. }

  816. LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
  817. {
  818.   TValue *o;
  819.   TValue key;
  820.   cTValue *t = index2adr(L, idx);
  821.   api_checknelems(L, 1);
  822.   api_checkvalidindex(L, t);
  823.   setstrV(L, &key, lj_str_newz(L, k));
  824.   o = lj_meta_tset(L, t, &key);
  825.   if (o) {
  826.     /* NOBARRIER: lj_meta_tset ensures the table is not black. */
  827.     copyTV(L, o, --L->top);
  828.   } else {
  829.     TValue *base = L->top;
  830.     copyTV(L, base+2, base-3-2*LJ_FR2);
  831.     L->top = base+3;
  832.     lj_vm_call(L, base, 0+1);
  833.     L->top -= 2+LJ_FR2;
  834.   }
  835. }

  836. LUA_API void lua_rawset(lua_State *L, int idx)
  837. {
  838.   GCtab *t = tabV(index2adr(L, idx));
  839.   TValue *dst, *key;
  840.   api_checknelems(L, 2);
  841.   key = L->top-2;
  842.   dst = lj_tab_set(L, t, key);
  843.   copyTV(L, dst, key+1);
  844.   lj_gc_anybarriert(L, t);
  845.   L->top = key;
  846. }

  847. LUA_API void lua_rawseti(lua_State *L, int idx, int n)
  848. {
  849.   GCtab *t = tabV(index2adr(L, idx));
  850.   TValue *dst, *src;
  851.   api_checknelems(L, 1);
  852.   dst = lj_tab_setint(L, t, n);
  853.   src = L->top-1;
  854.   copyTV(L, dst, src);
  855.   lj_gc_barriert(L, t, dst);
  856.   L->top = src;
  857. }

  858. LUA_API int lua_setmetatable(lua_State *L, int idx)
  859. {
  860.   global_State *g;
  861.   GCtab *mt;
  862.   cTValue *o = index2adr(L, idx);
  863.   api_checknelems(L, 1);
  864.   api_checkvalidindex(L, o);
  865.   if (tvisnil(L->top-1)) {
  866.     mt = NULL;
  867.   } else {
  868.     api_check(L, tvistab(L->top-1));
  869.     mt = tabV(L->top-1);
  870.   }
  871.   g = G(L);
  872.   if (tvistab(o)) {
  873.     setgcref(tabV(o)->metatable, obj2gco(mt));
  874.     if (mt)
  875.       lj_gc_objbarriert(L, tabV(o), mt);
  876.   } else if (tvisudata(o)) {
  877.     setgcref(udataV(o)->metatable, obj2gco(mt));
  878.     if (mt)
  879.       lj_gc_objbarrier(L, udataV(o), mt);
  880.   } else {
  881.     /* Flush cache, since traces specialize to basemt. But not during __gc. */
  882.     if (lj_trace_flushall(L))
  883.       lj_err_caller(L, LJ_ERR_NOGCMM);
  884.     if (tvisbool(o)) {
  885.       /* NOBARRIER: basemt is a GC root. */
  886.       setgcref(basemt_it(g, LJ_TTRUE), obj2gco(mt));
  887.       setgcref(basemt_it(g, LJ_TFALSE), obj2gco(mt));
  888.     } else {
  889.       /* NOBARRIER: basemt is a GC root. */
  890.       setgcref(basemt_obj(g, o), obj2gco(mt));
  891.     }
  892.   }
  893.   L->top--;
  894.   return 1;
  895. }

  896. LUA_API int lua_setfenv(lua_State *L, int idx)
  897. {
  898.   cTValue *o = index2adr(L, idx);
  899.   GCtab *t;
  900.   api_checknelems(L, 1);
  901.   api_checkvalidindex(L, o);
  902.   api_check(L, tvistab(L->top-1));
  903.   t = tabV(L->top-1);
  904.   if (tvisfunc(o)) {
  905.     setgcref(funcV(o)->c.env, obj2gco(t));
  906.   } else if (tvisudata(o)) {
  907.     setgcref(udataV(o)->env, obj2gco(t));
  908.   } else if (tvisthread(o)) {
  909.     setgcref(threadV(o)->env, obj2gco(t));
  910.   } else {
  911.     L->top--;
  912.     return 0;
  913.   }
  914.   lj_gc_objbarrier(L, gcV(o), t);
  915.   L->top--;
  916.   return 1;
  917. }

  918. LUA_API const char *lua_setupvalue(lua_State *L, int idx, int n)
  919. {
  920.   cTValue *f = index2adr(L, idx);
  921.   TValue *val;
  922.   const char *name;
  923.   api_checknelems(L, 1);
  924.   name = lj_debug_uvnamev(f, (uint32_t)(n-1), &val);
  925.   if (name) {
  926.     L->top--;
  927.     copyTV(L, val, L->top);
  928.     lj_gc_barrier(L, funcV(f), L->top);
  929.   }
  930.   return name;
  931. }

  932. /* -- Calls --------------------------------------------------------------- */

  933. #if LJ_FR2
  934. static TValue *api_call_base(lua_State *L, int nargs)
  935. {
  936.   TValue *o = L->top, *base = o - nargs;
  937.   L->top = o+1;
  938.   for (; o > base; o--) copyTV(L, o, o-1);
  939.   setnilV(o);
  940.   return o+1;
  941. }
  942. #else
  943. #define api_call_base(L, nargs)        (L->top - (nargs))
  944. #endif

  945. LUA_API void lua_call(lua_State *L, int nargs, int nresults)
  946. {
  947.   api_check(L, L->status == 0 || L->status == LUA_ERRERR);
  948.   api_checknelems(L, nargs+1);
  949.   lj_vm_call(L, api_call_base(L, nargs), nresults+1);
  950. }

  951. LUA_API int lua_pcall(lua_State *L, int nargs, int nresults, int errfunc)
  952. {
  953.   global_State *g = G(L);
  954.   uint8_t oldh = hook_save(g);
  955.   ptrdiff_t ef;
  956.   int status;
  957.   api_check(L, L->status == 0 || L->status == LUA_ERRERR);
  958.   api_checknelems(L, nargs+1);
  959.   if (errfunc == 0) {
  960.     ef = 0;
  961.   } else {
  962.     cTValue *o = stkindex2adr(L, errfunc);
  963.     api_checkvalidindex(L, o);
  964.     ef = savestack(L, o);
  965.   }
  966.   status = lj_vm_pcall(L, api_call_base(L, nargs), nresults+1, ef);
  967.   if (status) hook_restore(g, oldh);
  968.   return status;
  969. }

  970. static TValue *cpcall(lua_State *L, lua_CFunction func, void *ud)
  971. {
  972.   GCfunc *fn = lj_func_newC(L, 0, getcurrenv(L));
  973.   TValue *top = L->top;
  974.   fn->c.f = func;
  975.   setfuncV(L, top++, fn);
  976.   if (LJ_FR2) setnilV(top++);
  977.   setlightudV(top++, checklightudptr(L, ud));
  978.   cframe_nres(L->cframe) = 1+0/* Zero results. */
  979.   L->top = top;
  980.   return top-1/* Now call the newly allocated C function. */
  981. }

  982. LUA_API int lua_cpcall(lua_State *L, lua_CFunction func, void *ud)
  983. {
  984.   global_State *g = G(L);
  985.   uint8_t oldh = hook_save(g);
  986.   int status;
  987.   api_check(L, L->status == 0 || L->status == LUA_ERRERR);
  988.   status = lj_vm_cpcall(L, func, ud, cpcall);
  989.   if (status) hook_restore(g, oldh);
  990.   return status;
  991. }

  992. LUALIB_API int luaL_callmeta(lua_State *L, int idx, const char *field)
  993. {
  994.   if (luaL_getmetafield(L, idx, field)) {
  995.     TValue *top = L->top--;
  996.     if (LJ_FR2) setnilV(top++);
  997.     copyTV(L, top++, index2adr(L, idx));
  998.     L->top = top;
  999.     lj_vm_call(L, top-1, 1+1);
  1000.     return 1;
  1001.   }
  1002.   return 0;
  1003. }

  1004. /* -- Coroutine yield and resume ------------------------------------------ */

  1005. LUA_API int lua_yield(lua_State *L, int nresults)
  1006. {
  1007.   void *cf = L->cframe;
  1008.   global_State *g = G(L);
  1009.   if (cframe_canyield(cf)) {
  1010.     cf = cframe_raw(cf);
  1011.     if (!hook_active(g)) {  /* Regular yield: move results down if needed. */
  1012.       cTValue *f = L->top - nresults;
  1013.       if (f > L->base) {
  1014.         TValue *t = L->base;
  1015.         while (--nresults >= 0) copyTV(L, t++, f++);
  1016.         L->top = t;
  1017.       }
  1018.       L->cframe = NULL;
  1019.       L->status = LUA_YIELD;
  1020.       return -1;
  1021.     } else/* Yield from hook: add a pseudo-frame. */
  1022.       TValue *top = L->top;
  1023.       hook_leave(g);
  1024.       (top++)->u64 = cframe_multres(cf);
  1025.       setcont(top, lj_cont_hook);
  1026.       if (LJ_FR2) top++;
  1027.       setframe_pc(top, cframe_pc(cf)-1);
  1028.       if (LJ_FR2) top++;
  1029.       setframe_gc(top, obj2gco(L), LJ_TTHREAD);
  1030.       setframe_ftsz(top, ((char *)(top+1)-(char *)L->base)+FRAME_CONT);
  1031.       L->top = L->base = top+1;
  1032. #if LJ_TARGET_X64
  1033.       lj_err_throw(L, LUA_YIELD);
  1034. #else
  1035.       L->cframe = NULL;
  1036.       L->status = LUA_YIELD;
  1037.       lj_vm_unwind_c(cf, LUA_YIELD);
  1038. #endif
  1039.     }
  1040.   }
  1041.   lj_err_msg(L, LJ_ERR_CYIELD);
  1042.   return 0/* unreachable */
  1043. }

  1044. LUA_API int lua_resume(lua_State *L, int nargs)
  1045. {
  1046.   if (L->cframe == NULL && L->status <= LUA_YIELD)
  1047.     return lj_vm_resume(L,
  1048.       L->status == 0 ? api_call_base(L, nargs) : L->top - nargs,
  1049.       0, 0);
  1050.   L->top = L->base;
  1051.   setstrV(L, L->top, lj_err_str(L, LJ_ERR_COSUSP));
  1052.   incr_top(L);
  1053.   return LUA_ERRRUN;
  1054. }

  1055. /* -- GC and memory management -------------------------------------------- */

  1056. LUA_API int lua_gc(lua_State *L, int what, int data)
  1057. {
  1058.   global_State *g = G(L);
  1059.   int res = 0;
  1060.   switch (what) {
  1061.   case LUA_GCSTOP:
  1062.     g->gc.threshold = LJ_MAX_MEM;
  1063.     break;
  1064.   case LUA_GCRESTART:
  1065.     g->gc.threshold = data == -1 ? (g->gc.total/100)*g->gc.pause : g->gc.total;
  1066.     break;
  1067.   case LUA_GCCOLLECT:
  1068.     lj_gc_fullgc(L);
  1069.     break;
  1070.   case LUA_GCCOUNT:
  1071.     res = (int)(g->gc.total >> 10);
  1072.     break;
  1073.   case LUA_GCCOUNTB:
  1074.     res = (int)(g->gc.total & 0x3ff);
  1075.     break;
  1076.   case LUA_GCSTEP: {
  1077.     GCSize a = (GCSize)data << 10;
  1078.     g->gc.threshold = (a <= g->gc.total) ? (g->gc.total - a) : 0;
  1079.     while (g->gc.total >= g->gc.threshold)
  1080.       if (lj_gc_step(L) > 0) {
  1081.         res = 1;
  1082.         break;
  1083.       }
  1084.     break;
  1085.   }
  1086.   case LUA_GCSETPAUSE:
  1087.     res = (int)(g->gc.pause);
  1088.     g->gc.pause = (MSize)data;
  1089.     break;
  1090.   case LUA_GCSETSTEPMUL:
  1091.     res = (int)(g->gc.stepmul);
  1092.     g->gc.stepmul = (MSize)data;
  1093.     break;
  1094.   default:
  1095.     res = -1/* Invalid option. */
  1096.   }
  1097.   return res;
  1098. }

  1099. LUA_API lua_Alloc lua_getallocf(lua_State *L, void **ud)
  1100. {
  1101.   global_State *g = G(L);
  1102.   if (ud) *ud = g->allocd;
  1103.   return g->allocf;
  1104. }

  1105. LUA_API void lua_setallocf(lua_State *L, lua_Alloc f, void *ud)
  1106. {
  1107.   global_State *g = G(L);
  1108.   g->allocd = ud;
  1109.   g->allocf = f;
  1110. }