lj_lib.c 9.5 KB

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