src/lj_lib.c - luajit-2.0-src

Functions defined

Macros defined

Source code

  1. /*
  2. ** Library function support.
  3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
  4. */

  5. #define lj_lib_c
  6. #define LUA_CORE

  7. #include "lauxlib.h"

  8. #include "lj_obj.h"
  9. #include "lj_gc.h"
  10. #include "lj_err.h"
  11. #include "lj_str.h"
  12. #include "lj_tab.h"
  13. #include "lj_func.h"
  14. #include "lj_bc.h"
  15. #include "lj_dispatch.h"
  16. #include "lj_vm.h"
  17. #include "lj_strscan.h"
  18. #include "lj_strfmt.h"
  19. #include "lj_lex.h"
  20. #include "lj_bcdump.h"
  21. #include "lj_lib.h"

  22. /* -- Library initialization ---------------------------------------------- */

  23. static GCtab *lib_create_table(lua_State *L, const char *libname, int hsize)
  24. {
  25.   if (libname) {
  26.     luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16);
  27.     lua_getfield(L, -1, libname);
  28.     if (!tvistab(L->top-1)) {
  29.       L->top--;
  30.       if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, hsize) != NULL)
  31.         lj_err_callerv(L, LJ_ERR_BADMODN, libname);
  32.       settabV(L, L->top, tabV(L->top-1));
  33.       L->top++;
  34.       lua_setfield(L, -3, libname);  /* _LOADED[libname] = new table */
  35.     }
  36.     L->top--;
  37.     settabV(L, L->top-1, tabV(L->top));
  38.   } else {
  39.     lua_createtable(L, 0, hsize);
  40.   }
  41.   return tabV(L->top-1);
  42. }

  43. static const uint8_t *lib_read_lfunc(lua_State *L, const uint8_t *p, GCtab *tab)
  44. {
  45.   int len = *p++;
  46.   GCstr *name = lj_str_new(L, (const char *)p, len);
  47.   LexState ls;
  48.   GCproto *pt;
  49.   GCfunc *fn;
  50.   memset(&ls, 0, sizeof(ls));
  51.   ls.L = L;
  52.   ls.p = (const char *)(p+len);
  53.   ls.pe = (const char *)~(uintptr_t)0;
  54.   ls.c = -1;
  55.   ls.level = (BCDUMP_F_STRIP|(LJ_BE*BCDUMP_F_BE));
  56.   ls.chunkname = name;
  57.   pt = lj_bcread_proto(&ls);
  58.   pt->firstline = ~(BCLine)0;
  59.   fn = lj_func_newL_empty(L, pt, tabref(L->env));
  60.   /* NOBARRIER: See below for common barrier. */
  61.   setfuncV(L, lj_tab_setstr(L, tab, name), fn);
  62.   return (const uint8_t *)ls.p;
  63. }

  64. void lj_lib_register(lua_State *L, const char *libname,
  65.                      const uint8_t *p, const lua_CFunction *cf)
  66. {
  67.   GCtab *env = tabref(L->env);
  68.   GCfunc *ofn = NULL;
  69.   int ffid = *p++;
  70.   BCIns *bcff = &L2GG(L)->bcff[*p++];
  71.   GCtab *tab = lib_create_table(L, libname, *p++);
  72.   ptrdiff_t tpos = L->top - L->base;

  73.   /* Avoid barriers further down. */
  74.   lj_gc_anybarriert(L, tab);
  75.   tab->nomm = 0;

  76.   for (;;) {
  77.     uint32_t tag = *p++;
  78.     MSize len = tag & LIBINIT_LENMASK;
  79.     tag &= LIBINIT_TAGMASK;
  80.     if (tag != LIBINIT_STRING) {
  81.       const char *name;
  82.       MSize nuv = (MSize)(L->top - L->base - tpos);
  83.       GCfunc *fn = lj_func_newC(L, nuv, env);
  84.       if (nuv) {
  85.         L->top = L->base + tpos;
  86.         memcpy(fn->c.upvalue, L->top, sizeof(TValue)*nuv);
  87.       }
  88.       fn->c.ffid = (uint8_t)(ffid++);
  89.       name = (const char *)p;
  90.       p += len;
  91.       if (tag == LIBINIT_CF)
  92.         setmref(fn->c.pc, &G(L)->bc_cfunc_int);
  93.       else
  94.         setmref(fn->c.pc, bcff++);
  95.       if (tag == LIBINIT_ASM_)
  96.         fn->c.f = ofn->c.f;  /* Copy handler from previous function. */
  97.       else
  98.         fn->c.f = *cf++;  /* Get cf or handler from C function table. */
  99.       if (len) {
  100.         /* NOBARRIER: See above for common barrier. */
  101.         setfuncV(L, lj_tab_setstr(L, tab, lj_str_new(L, name, len)), fn);
  102.       }
  103.       ofn = fn;
  104.     } else {
  105.       switch (tag | len) {
  106.       case LIBINIT_LUA:
  107.         p = lib_read_lfunc(L, p, tab);
  108.         break;
  109.       case LIBINIT_SET:
  110.         L->top -= 2;
  111.         if (tvisstr(L->top+1) && strV(L->top+1)->len == 0)
  112.           env = tabV(L->top);
  113.         else  /* NOBARRIER: See above for common barrier. */
  114.           copyTV(L, lj_tab_set(L, tab, L->top+1), L->top);
  115.         break;
  116.       case LIBINIT_NUMBER:
  117.         memcpy(&L->top->n, p, sizeof(double));
  118.         L->top++;
  119.         p += sizeof(double);
  120.         break;
  121.       case LIBINIT_COPY:
  122.         copyTV(L, L->top, L->top - *p++);
  123.         L->top++;
  124.         break;
  125.       case LIBINIT_LASTCL:
  126.         setfuncV(L, L->top++, ofn);
  127.         break;
  128.       case LIBINIT_FFID:
  129.         ffid++;
  130.         break;
  131.       case LIBINIT_END:
  132.         return;
  133.       default:
  134.         setstrV(L, L->top++, lj_str_new(L, (const char *)p, len));
  135.         p += len;
  136.         break;
  137.       }
  138.     }
  139.   }
  140. }

  141. /* Push internal function on the stack. */
  142. GCfunc *lj_lib_pushcc(lua_State *L, lua_CFunction f, int id, int n)
  143. {
  144.   GCfunc *fn;
  145.   lua_pushcclosure(L, f, n);
  146.   fn = funcV(L->top-1);
  147.   fn->c.ffid = (uint8_t)id;
  148.   setmref(fn->c.pc, &G(L)->bc_cfunc_int);
  149.   return fn;
  150. }

  151. void lj_lib_prereg(lua_State *L, const char *name, lua_CFunction f, GCtab *env)
  152. {
  153.   luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD", 4);
  154.   lua_pushcfunction(L, f);
  155.   /* NOBARRIER: The function is new (marked white). */
  156.   setgcref(funcV(L->top-1)->c.env, obj2gco(env));
  157.   lua_setfield(L, -2, name);
  158.   L->top--;
  159. }

  160. int lj_lib_postreg(lua_State *L, lua_CFunction cf, int id, const char *name)
  161. {
  162.   GCfunc *fn = lj_lib_pushcf(L, cf, id);
  163.   GCtab *t = tabref(curr_func(L)->c.env);  /* Reference to parent table. */
  164.   setfuncV(L, lj_tab_setstr(L, t, lj_str_newz(L, name)), fn);
  165.   lj_gc_anybarriert(L, t);
  166.   setfuncV(L, L->top++, fn);
  167.   return 1;
  168. }

  169. /* -- Type checks --------------------------------------------------------- */

  170. TValue *lj_lib_checkany(lua_State *L, int narg)
  171. {
  172.   TValue *o = L->base + narg-1;
  173.   if (o >= L->top)
  174.     lj_err_arg(L, narg, LJ_ERR_NOVAL);
  175.   return o;
  176. }

  177. GCstr *lj_lib_checkstr(lua_State *L, int narg)
  178. {
  179.   TValue *o = L->base + narg-1;
  180.   if (o < L->top) {
  181.     if (LJ_LIKELY(tvisstr(o))) {
  182.       return strV(o);
  183.     } else if (tvisnumber(o)) {
  184.       GCstr *s = lj_strfmt_number(L, o);
  185.       setstrV(L, o, s);
  186.       return s;
  187.     }
  188.   }
  189.   lj_err_argt(L, narg, LUA_TSTRING);
  190.   return NULL/* unreachable */
  191. }

  192. GCstr *lj_lib_optstr(lua_State *L, int narg)
  193. {
  194.   TValue *o = L->base + narg-1;
  195.   return (o < L->top && !tvisnil(o)) ? lj_lib_checkstr(L, narg) : NULL;
  196. }

  197. #if LJ_DUALNUM
  198. void lj_lib_checknumber(lua_State *L, int narg)
  199. {
  200.   TValue *o = L->base + narg-1;
  201.   if (!(o < L->top && lj_strscan_numberobj(o)))
  202.     lj_err_argt(L, narg, LUA_TNUMBER);
  203. }
  204. #endif

  205. lua_Number lj_lib_checknum(lua_State *L, int narg)
  206. {
  207.   TValue *o = L->base + narg-1;
  208.   if (!(o < L->top &&
  209.         (tvisnumber(o) || (tvisstr(o) && lj_strscan_num(strV(o), o)))))
  210.     lj_err_argt(L, narg, LUA_TNUMBER);
  211.   if (LJ_UNLIKELY(tvisint(o))) {
  212.     lua_Number n = (lua_Number)intV(o);
  213.     setnumV(o, n);
  214.     return n;
  215.   } else {
  216.     return numV(o);
  217.   }
  218. }

  219. int32_t lj_lib_checkint(lua_State *L, int narg)
  220. {
  221.   TValue *o = L->base + narg-1;
  222.   if (!(o < L->top && lj_strscan_numberobj(o)))
  223.     lj_err_argt(L, narg, LUA_TNUMBER);
  224.   if (LJ_LIKELY(tvisint(o))) {
  225.     return intV(o);
  226.   } else {
  227.     int32_t i = lj_num2int(numV(o));
  228.     if (LJ_DUALNUM) setintV(o, i);
  229.     return i;
  230.   }
  231. }

  232. int32_t lj_lib_optint(lua_State *L, int narg, int32_t def)
  233. {
  234.   TValue *o = L->base + narg-1;
  235.   return (o < L->top && !tvisnil(o)) ? lj_lib_checkint(L, narg) : def;
  236. }

  237. GCfunc *lj_lib_checkfunc(lua_State *L, int narg)
  238. {
  239.   TValue *o = L->base + narg-1;
  240.   if (!(o < L->top && tvisfunc(o)))
  241.     lj_err_argt(L, narg, LUA_TFUNCTION);
  242.   return funcV(o);
  243. }

  244. GCtab *lj_lib_checktab(lua_State *L, int narg)
  245. {
  246.   TValue *o = L->base + narg-1;
  247.   if (!(o < L->top && tvistab(o)))
  248.     lj_err_argt(L, narg, LUA_TTABLE);
  249.   return tabV(o);
  250. }

  251. GCtab *lj_lib_checktabornil(lua_State *L, int narg)
  252. {
  253.   TValue *o = L->base + narg-1;
  254.   if (o < L->top) {
  255.     if (tvistab(o))
  256.       return tabV(o);
  257.     else if (tvisnil(o))
  258.       return NULL;
  259.   }
  260.   lj_err_arg(L, narg, LJ_ERR_NOTABN);
  261.   return NULL/* unreachable */
  262. }

  263. int lj_lib_checkopt(lua_State *L, int narg, int def, const char *lst)
  264. {
  265.   GCstr *s = def >= 0 ? lj_lib_optstr(L, narg) : lj_lib_checkstr(L, narg);
  266.   if (s) {
  267.     const char *opt = strdata(s);
  268.     MSize len = s->len;
  269.     int i;
  270.     for (i = 0; *(const uint8_t *)lst; i++) {
  271.       if (*(const uint8_t *)lst == len && memcmp(opt, lst+1, len) == 0)
  272.         return i;
  273.       lst += 1+*(const uint8_t *)lst;
  274.     }
  275.     lj_err_argv(L, narg, LJ_ERR_INVOPTM, opt);
  276.   }
  277.   return def;
  278. }