lj_lib.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. ** Library function support.
  3. ** Copyright (C) 2005-2013 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. void lj_lib_prereg(lua_State *L, const char *name, lua_CFunction f, GCtab *env)
  142. {
  143. luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD", 4);
  144. lua_pushcfunction(L, f);
  145. /* NOBARRIER: The function is new (marked white). */
  146. setgcref(funcV(L->top-1)->c.env, obj2gco(env));
  147. lua_setfield(L, -2, name);
  148. L->top--;
  149. }
  150. /* -- Type checks --------------------------------------------------------- */
  151. TValue *lj_lib_checkany(lua_State *L, int narg)
  152. {
  153. TValue *o = L->base + narg-1;
  154. if (o >= L->top)
  155. lj_err_arg(L, narg, LJ_ERR_NOVAL);
  156. return o;
  157. }
  158. GCstr *lj_lib_checkstr(lua_State *L, int narg)
  159. {
  160. TValue *o = L->base + narg-1;
  161. if (o < L->top) {
  162. if (LJ_LIKELY(tvisstr(o))) {
  163. return strV(o);
  164. } else if (tvisnumber(o)) {
  165. GCstr *s = lj_strfmt_number(L, o);
  166. setstrV(L, o, s);
  167. return s;
  168. }
  169. }
  170. lj_err_argt(L, narg, LUA_TSTRING);
  171. return NULL; /* unreachable */
  172. }
  173. GCstr *lj_lib_optstr(lua_State *L, int narg)
  174. {
  175. TValue *o = L->base + narg-1;
  176. return (o < L->top && !tvisnil(o)) ? lj_lib_checkstr(L, narg) : NULL;
  177. }
  178. #if LJ_DUALNUM
  179. void lj_lib_checknumber(lua_State *L, int narg)
  180. {
  181. TValue *o = L->base + narg-1;
  182. if (!(o < L->top && lj_strscan_numberobj(o)))
  183. lj_err_argt(L, narg, LUA_TNUMBER);
  184. }
  185. #endif
  186. lua_Number lj_lib_checknum(lua_State *L, int narg)
  187. {
  188. TValue *o = L->base + narg-1;
  189. if (!(o < L->top &&
  190. (tvisnumber(o) || (tvisstr(o) && lj_strscan_num(strV(o), o)))))
  191. lj_err_argt(L, narg, LUA_TNUMBER);
  192. if (LJ_UNLIKELY(tvisint(o))) {
  193. lua_Number n = (lua_Number)intV(o);
  194. setnumV(o, n);
  195. return n;
  196. } else {
  197. return numV(o);
  198. }
  199. }
  200. int32_t lj_lib_checkint(lua_State *L, int narg)
  201. {
  202. TValue *o = L->base + narg-1;
  203. if (!(o < L->top && lj_strscan_numberobj(o)))
  204. lj_err_argt(L, narg, LUA_TNUMBER);
  205. if (LJ_LIKELY(tvisint(o))) {
  206. return intV(o);
  207. } else {
  208. int32_t i = lj_num2int(numV(o));
  209. if (LJ_DUALNUM) setintV(o, i);
  210. return i;
  211. }
  212. }
  213. int32_t lj_lib_optint(lua_State *L, int narg, int32_t def)
  214. {
  215. TValue *o = L->base + narg-1;
  216. return (o < L->top && !tvisnil(o)) ? lj_lib_checkint(L, narg) : def;
  217. }
  218. GCfunc *lj_lib_checkfunc(lua_State *L, int narg)
  219. {
  220. TValue *o = L->base + narg-1;
  221. if (!(o < L->top && tvisfunc(o)))
  222. lj_err_argt(L, narg, LUA_TFUNCTION);
  223. return funcV(o);
  224. }
  225. GCtab *lj_lib_checktab(lua_State *L, int narg)
  226. {
  227. TValue *o = L->base + narg-1;
  228. if (!(o < L->top && tvistab(o)))
  229. lj_err_argt(L, narg, LUA_TTABLE);
  230. return tabV(o);
  231. }
  232. GCtab *lj_lib_checktabornil(lua_State *L, int narg)
  233. {
  234. TValue *o = L->base + narg-1;
  235. if (o < L->top) {
  236. if (tvistab(o))
  237. return tabV(o);
  238. else if (tvisnil(o))
  239. return NULL;
  240. }
  241. lj_err_arg(L, narg, LJ_ERR_NOTABN);
  242. return NULL; /* unreachable */
  243. }
  244. int lj_lib_checkopt(lua_State *L, int narg, int def, const char *lst)
  245. {
  246. GCstr *s = def >= 0 ? lj_lib_optstr(L, narg) : lj_lib_checkstr(L, narg);
  247. if (s) {
  248. const char *opt = strdata(s);
  249. MSize len = s->len;
  250. int i;
  251. for (i = 0; *(const uint8_t *)lst; i++) {
  252. if (*(const uint8_t *)lst == len && memcmp(opt, lst+1, len) == 0)
  253. return i;
  254. lst += 1+*(const uint8_t *)lst;
  255. }
  256. lj_err_argv(L, narg, LJ_ERR_INVOPTM, opt);
  257. }
  258. return def;
  259. }