lj_lib.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. ** Library function support.
  3. ** Copyright (C) 2005-2021 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. #if LJ_HASFFI
  17. #include "lj_ctype.h"
  18. #endif
  19. #include "lj_vm.h"
  20. #include "lj_strscan.h"
  21. #include "lj_strfmt.h"
  22. #include "lj_lex.h"
  23. #include "lj_bcdump.h"
  24. #include "lj_lib.h"
  25. /* -- Library initialization ---------------------------------------------- */
  26. static GCtab *lib_create_table(lua_State *L, const char *libname, int hsize)
  27. {
  28. if (libname) {
  29. luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16);
  30. lua_getfield(L, -1, libname);
  31. if (!tvistab(L->top-1)) {
  32. L->top--;
  33. if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, hsize) != NULL)
  34. lj_err_callerv(L, LJ_ERR_BADMODN, libname);
  35. settabV(L, L->top, tabV(L->top-1));
  36. L->top++;
  37. lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */
  38. }
  39. L->top--;
  40. settabV(L, L->top-1, tabV(L->top));
  41. } else {
  42. lua_createtable(L, 0, hsize);
  43. }
  44. return tabV(L->top-1);
  45. }
  46. static const uint8_t *lib_read_lfunc(lua_State *L, const uint8_t *p, GCtab *tab)
  47. {
  48. int len = *p++;
  49. GCstr *name = lj_str_new(L, (const char *)p, len);
  50. LexState ls;
  51. GCproto *pt;
  52. GCfunc *fn;
  53. memset(&ls, 0, sizeof(ls));
  54. ls.L = L;
  55. ls.p = (const char *)(p+len);
  56. ls.pe = (const char *)~(uintptr_t)0;
  57. ls.c = -1;
  58. ls.level = (BCDUMP_F_STRIP|(LJ_BE*BCDUMP_F_BE));
  59. ls.chunkname = name;
  60. pt = lj_bcread_proto(&ls);
  61. pt->firstline = ~(BCLine)0;
  62. fn = lj_func_newL_empty(L, pt, tabref(L->env));
  63. /* NOBARRIER: See below for common barrier. */
  64. setfuncV(L, lj_tab_setstr(L, tab, name), fn);
  65. return (const uint8_t *)ls.p;
  66. }
  67. void lj_lib_register(lua_State *L, const char *libname,
  68. const uint8_t *p, const lua_CFunction *cf)
  69. {
  70. GCtab *env = tabref(L->env);
  71. GCfunc *ofn = NULL;
  72. int ffid = *p++;
  73. BCIns *bcff = &L2GG(L)->bcff[*p++];
  74. GCtab *tab = lib_create_table(L, libname, *p++);
  75. ptrdiff_t tpos = L->top - L->base;
  76. /* Avoid barriers further down. */
  77. lj_gc_anybarriert(L, tab);
  78. tab->nomm = 0;
  79. for (;;) {
  80. uint32_t tag = *p++;
  81. MSize len = tag & LIBINIT_LENMASK;
  82. tag &= LIBINIT_TAGMASK;
  83. if (tag != LIBINIT_STRING) {
  84. const char *name;
  85. MSize nuv = (MSize)(L->top - L->base - tpos);
  86. GCfunc *fn = lj_func_newC(L, nuv, env);
  87. if (nuv) {
  88. L->top = L->base + tpos;
  89. memcpy(fn->c.upvalue, L->top, sizeof(TValue)*nuv);
  90. }
  91. fn->c.ffid = (uint8_t)(ffid++);
  92. name = (const char *)p;
  93. p += len;
  94. if (tag == LIBINIT_CF)
  95. setmref(fn->c.pc, &G(L)->bc_cfunc_int);
  96. else
  97. setmref(fn->c.pc, bcff++);
  98. if (tag == LIBINIT_ASM_)
  99. fn->c.f = ofn->c.f; /* Copy handler from previous function. */
  100. else
  101. fn->c.f = *cf++; /* Get cf or handler from C function table. */
  102. if (len) {
  103. /* NOBARRIER: See above for common barrier. */
  104. setfuncV(L, lj_tab_setstr(L, tab, lj_str_new(L, name, len)), fn);
  105. }
  106. ofn = fn;
  107. } else {
  108. switch (tag | len) {
  109. case LIBINIT_LUA:
  110. p = lib_read_lfunc(L, p, tab);
  111. break;
  112. case LIBINIT_SET:
  113. L->top -= 2;
  114. if (tvisstr(L->top+1) && strV(L->top+1)->len == 0)
  115. env = tabV(L->top);
  116. else /* NOBARRIER: See above for common barrier. */
  117. copyTV(L, lj_tab_set(L, tab, L->top+1), L->top);
  118. break;
  119. case LIBINIT_NUMBER:
  120. memcpy(&L->top->n, p, sizeof(double));
  121. L->top++;
  122. p += sizeof(double);
  123. break;
  124. case LIBINIT_COPY:
  125. copyTV(L, L->top, L->top - *p++);
  126. L->top++;
  127. break;
  128. case LIBINIT_LASTCL:
  129. setfuncV(L, L->top++, ofn);
  130. break;
  131. case LIBINIT_FFID:
  132. ffid++;
  133. break;
  134. case LIBINIT_END:
  135. return;
  136. default:
  137. setstrV(L, L->top++, lj_str_new(L, (const char *)p, len));
  138. p += len;
  139. break;
  140. }
  141. }
  142. }
  143. }
  144. /* Push internal function on the stack. */
  145. GCfunc *lj_lib_pushcc(lua_State *L, lua_CFunction f, int id, int n)
  146. {
  147. GCfunc *fn;
  148. lua_pushcclosure(L, f, n);
  149. fn = funcV(L->top-1);
  150. fn->c.ffid = (uint8_t)id;
  151. setmref(fn->c.pc, &G(L)->bc_cfunc_int);
  152. return fn;
  153. }
  154. void lj_lib_prereg(lua_State *L, const char *name, lua_CFunction f, GCtab *env)
  155. {
  156. luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD", 4);
  157. lua_pushcfunction(L, f);
  158. /* NOBARRIER: The function is new (marked white). */
  159. setgcref(funcV(L->top-1)->c.env, obj2gco(env));
  160. lua_setfield(L, -2, name);
  161. L->top--;
  162. }
  163. int lj_lib_postreg(lua_State *L, lua_CFunction cf, int id, const char *name)
  164. {
  165. GCfunc *fn = lj_lib_pushcf(L, cf, id);
  166. GCtab *t = tabref(curr_func(L)->c.env); /* Reference to parent table. */
  167. setfuncV(L, lj_tab_setstr(L, t, lj_str_newz(L, name)), fn);
  168. lj_gc_anybarriert(L, t);
  169. setfuncV(L, L->top++, fn);
  170. return 1;
  171. }
  172. /* -- Type checks --------------------------------------------------------- */
  173. TValue *lj_lib_checkany(lua_State *L, int narg)
  174. {
  175. TValue *o = L->base + narg-1;
  176. if (o >= L->top)
  177. lj_err_arg(L, narg, LJ_ERR_NOVAL);
  178. return o;
  179. }
  180. GCstr *lj_lib_checkstr(lua_State *L, int narg)
  181. {
  182. TValue *o = L->base + narg-1;
  183. if (o < L->top) {
  184. if (LJ_LIKELY(tvisstr(o))) {
  185. return strV(o);
  186. } else if (tvisnumber(o)) {
  187. GCstr *s = lj_strfmt_number(L, o);
  188. setstrV(L, o, s);
  189. return s;
  190. }
  191. }
  192. lj_err_argt(L, narg, LUA_TSTRING);
  193. return NULL; /* unreachable */
  194. }
  195. GCstr *lj_lib_optstr(lua_State *L, int narg)
  196. {
  197. TValue *o = L->base + narg-1;
  198. return (o < L->top && !tvisnil(o)) ? lj_lib_checkstr(L, narg) : NULL;
  199. }
  200. #if LJ_DUALNUM
  201. void lj_lib_checknumber(lua_State *L, int narg)
  202. {
  203. TValue *o = L->base + narg-1;
  204. if (!(o < L->top && lj_strscan_numberobj(o)))
  205. lj_err_argt(L, narg, LUA_TNUMBER);
  206. }
  207. #endif
  208. lua_Number lj_lib_checknum(lua_State *L, int narg)
  209. {
  210. TValue *o = L->base + narg-1;
  211. if (!(o < L->top &&
  212. (tvisnumber(o) || (tvisstr(o) && lj_strscan_num(strV(o), o)))))
  213. lj_err_argt(L, narg, LUA_TNUMBER);
  214. if (LJ_UNLIKELY(tvisint(o))) {
  215. lua_Number n = (lua_Number)intV(o);
  216. setnumV(o, n);
  217. return n;
  218. } else {
  219. return numV(o);
  220. }
  221. }
  222. int32_t lj_lib_checkint(lua_State *L, int narg)
  223. {
  224. TValue *o = L->base + narg-1;
  225. if (!(o < L->top && lj_strscan_numberobj(o)))
  226. lj_err_argt(L, narg, LUA_TNUMBER);
  227. if (LJ_LIKELY(tvisint(o))) {
  228. return intV(o);
  229. } else {
  230. int32_t i = lj_num2int(numV(o));
  231. if (LJ_DUALNUM) setintV(o, i);
  232. return i;
  233. }
  234. }
  235. int32_t lj_lib_optint(lua_State *L, int narg, int32_t def)
  236. {
  237. TValue *o = L->base + narg-1;
  238. return (o < L->top && !tvisnil(o)) ? lj_lib_checkint(L, narg) : def;
  239. }
  240. GCfunc *lj_lib_checkfunc(lua_State *L, int narg)
  241. {
  242. TValue *o = L->base + narg-1;
  243. if (!(o < L->top && tvisfunc(o)))
  244. lj_err_argt(L, narg, LUA_TFUNCTION);
  245. return funcV(o);
  246. }
  247. GCtab *lj_lib_checktab(lua_State *L, int narg)
  248. {
  249. TValue *o = L->base + narg-1;
  250. if (!(o < L->top && tvistab(o)))
  251. lj_err_argt(L, narg, LUA_TTABLE);
  252. return tabV(o);
  253. }
  254. GCtab *lj_lib_checktabornil(lua_State *L, int narg)
  255. {
  256. TValue *o = L->base + narg-1;
  257. if (o < L->top) {
  258. if (tvistab(o))
  259. return tabV(o);
  260. else if (tvisnil(o))
  261. return NULL;
  262. }
  263. lj_err_arg(L, narg, LJ_ERR_NOTABN);
  264. return NULL; /* unreachable */
  265. }
  266. int lj_lib_checkopt(lua_State *L, int narg, int def, const char *lst)
  267. {
  268. GCstr *s = def >= 0 ? lj_lib_optstr(L, narg) : lj_lib_checkstr(L, narg);
  269. if (s) {
  270. const char *opt = strdata(s);
  271. MSize len = s->len;
  272. int i;
  273. for (i = 0; *(const uint8_t *)lst; i++) {
  274. if (*(const uint8_t *)lst == len && memcmp(opt, lst+1, len) == 0)
  275. return i;
  276. lst += 1+*(const uint8_t *)lst;
  277. }
  278. lj_err_argv(L, narg, LJ_ERR_INVOPTM, opt);
  279. }
  280. return def;
  281. }
  282. /* -- Strict type checks -------------------------------------------------- */
  283. /* The following type checks do not coerce between strings and numbers.
  284. ** And they handle plain int64_t/uint64_t FFI numbers, too.
  285. */
  286. #if LJ_HASBUFFER
  287. GCstr *lj_lib_checkstrx(lua_State *L, int narg)
  288. {
  289. TValue *o = L->base + narg-1;
  290. if (!(o < L->top && tvisstr(o))) lj_err_argt(L, narg, LUA_TSTRING);
  291. return strV(o);
  292. }
  293. int32_t lj_lib_checkintrange(lua_State *L, int narg, int32_t a, int32_t b)
  294. {
  295. TValue *o = L->base + narg-1;
  296. lj_assertL(b >= 0, "expected range must be non-negative");
  297. if (o < L->top) {
  298. if (LJ_LIKELY(tvisint(o))) {
  299. int32_t i = intV(o);
  300. if (i >= a && i <= b) return i;
  301. } else if (LJ_LIKELY(tvisnum(o))) {
  302. /* For performance reasons, this doesn't check for integerness or
  303. ** integer overflow. Overflow detection still works, since all FPUs
  304. ** return either MININT or MAXINT, which is then out of range.
  305. */
  306. int32_t i = (int32_t)numV(o);
  307. if (i >= a && i <= b) return i;
  308. #if LJ_HASFFI
  309. } else if (tviscdata(o)) {
  310. GCcdata *cd = cdataV(o);
  311. if (cd->ctypeid == CTID_INT64) {
  312. int64_t i = *(int64_t *)cdataptr(cd);
  313. if (i >= (int64_t)a && i <= (int64_t)b) return (int32_t)i;
  314. } else if (cd->ctypeid == CTID_UINT64) {
  315. uint64_t i = *(uint64_t *)cdataptr(cd);
  316. if ((a < 0 || i >= (uint64_t)a) && i <= (uint64_t)b) return (int32_t)i;
  317. } else {
  318. goto badtype;
  319. }
  320. #endif
  321. } else {
  322. goto badtype;
  323. }
  324. lj_err_arg(L, narg, LJ_ERR_NUMRNG);
  325. }
  326. badtype:
  327. lj_err_argt(L, narg, LUA_TNUMBER);
  328. return 0; /* unreachable */
  329. }
  330. #endif