root/lj_api.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. index2adr
  2. stkindex2adr
  3. getcurrenv
  4. lua_status
  5. lua_checkstack
  6. luaL_checkstack
  7. lua_xmove
  8. lua_version
  9. lua_gettop
  10. lua_settop
  11. lua_remove
  12. lua_insert
  13. copy_slot
  14. lua_replace
  15. lua_copy
  16. lua_pushvalue
  17. lua_type
  18. luaL_checktype
  19. luaL_checkany
  20. lua_typename
  21. lua_iscfunction
  22. lua_isnumber
  23. lua_isstring
  24. lua_isuserdata
  25. lua_rawequal
  26. lua_equal
  27. lua_lessthan
  28. lua_tonumber
  29. lua_tonumberx
  30. luaL_checknumber
  31. luaL_optnumber
  32. lua_tointeger
  33. lua_tointegerx
  34. luaL_checkinteger
  35. luaL_optinteger
  36. lua_toboolean
  37. lua_tolstring
  38. luaL_checklstring
  39. luaL_optlstring
  40. luaL_checkoption
  41. lua_objlen
  42. lua_tocfunction
  43. lua_touserdata
  44. lua_tothread
  45. lua_topointer
  46. lua_pushnil
  47. lua_pushnumber
  48. lua_pushinteger
  49. lua_pushlstring
  50. lua_pushstring
  51. lua_pushvfstring
  52. lua_pushfstring
  53. lua_pushcclosure
  54. lua_pushboolean
  55. lua_pushlightuserdata
  56. lua_createtable
  57. luaL_newmetatable
  58. lua_pushthread
  59. lua_newthread
  60. lua_newuserdata
  61. lua_concat
  62. lua_gettable
  63. lua_getfield
  64. lua_rawget
  65. lua_rawgeti
  66. lua_getmetatable
  67. luaL_getmetafield
  68. lua_getfenv
  69. lua_next
  70. lua_getupvalue
  71. lua_upvalueid
  72. lua_upvaluejoin
  73. luaL_testudata
  74. luaL_checkudata
  75. lua_settable
  76. lua_setfield
  77. lua_rawset
  78. lua_rawseti
  79. lua_setmetatable
  80. luaL_setmetatable
  81. lua_setfenv
  82. lua_setupvalue
  83. api_call_base
  84. lua_call
  85. lua_pcall
  86. cpcall
  87. lua_cpcall
  88. luaL_callmeta
  89. lua_isyieldable
  90. lua_yield
  91. lua_resume
  92. lua_gc
  93. lua_getallocf
  94. lua_setallocf

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

/* [<][>][^][v][top][bottom][index][help] */