lj_lib.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. ** Library function support.
  3. ** Copyright (C) 2005-2014 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_lib.h"
  19. /* -- Library initialization ---------------------------------------------- */
  20. static GCtab *lib_create_table(lua_State *L, const char *libname, int hsize)
  21. {
  22. if (libname) {
  23. luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16);
  24. lua_getfield(L, -1, libname);
  25. if (!tvistab(L->top-1)) {
  26. L->top--;
  27. if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, hsize) != NULL)
  28. lj_err_callerv(L, LJ_ERR_BADMODN, libname);
  29. settabV(L, L->top, tabV(L->top-1));
  30. L->top++;
  31. lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */
  32. }
  33. L->top--;
  34. settabV(L, L->top-1, tabV(L->top));
  35. } else {
  36. lua_createtable(L, 0, hsize);
  37. }
  38. return tabV(L->top-1);
  39. }
  40. void lj_lib_register(lua_State *L, const char *libname,
  41. const uint8_t *p, const lua_CFunction *cf)
  42. {
  43. GCtab *env = tabref(L->env);
  44. GCfunc *ofn = NULL;
  45. int ffid = *p++;
  46. BCIns *bcff = &L2GG(L)->bcff[*p++];
  47. GCtab *tab = lib_create_table(L, libname, *p++);
  48. ptrdiff_t tpos = L->top - L->base;
  49. /* Avoid barriers further down. */
  50. lj_gc_anybarriert(L, tab);
  51. tab->nomm = 0;
  52. for (;;) {
  53. uint32_t tag = *p++;
  54. MSize len = tag & LIBINIT_LENMASK;
  55. tag &= LIBINIT_TAGMASK;
  56. if (tag != LIBINIT_STRING) {
  57. const char *name;
  58. MSize nuv = (MSize)(L->top - L->base - tpos);
  59. GCfunc *fn = lj_func_newC(L, nuv, env);
  60. if (nuv) {
  61. L->top = L->base + tpos;
  62. memcpy(fn->c.upvalue, L->top, sizeof(TValue)*nuv);
  63. }
  64. fn->c.ffid = (uint8_t)(ffid++);
  65. name = (const char *)p;
  66. p += len;
  67. if (tag == LIBINIT_CF)
  68. setmref(fn->c.pc, &G(L)->bc_cfunc_int);
  69. else
  70. setmref(fn->c.pc, bcff++);
  71. if (tag == LIBINIT_ASM_)
  72. fn->c.f = ofn->c.f; /* Copy handler from previous function. */
  73. else
  74. fn->c.f = *cf++; /* Get cf or handler from C function table. */
  75. if (len) {
  76. /* NOBARRIER: See above for common barrier. */
  77. setfuncV(L, lj_tab_setstr(L, tab, lj_str_new(L, name, len)), fn);
  78. }
  79. ofn = fn;
  80. } else {
  81. switch (tag | len) {
  82. case LIBINIT_SET:
  83. L->top -= 2;
  84. if (tvisstr(L->top+1) && strV(L->top+1)->len == 0)
  85. env = tabV(L->top);
  86. else /* NOBARRIER: See above for common barrier. */
  87. copyTV(L, lj_tab_set(L, tab, L->top+1), L->top);
  88. break;
  89. case LIBINIT_NUMBER:
  90. memcpy(&L->top->n, p, sizeof(double));
  91. L->top++;
  92. p += sizeof(double);
  93. break;
  94. case LIBINIT_COPY:
  95. copyTV(L, L->top, L->top - *p++);
  96. L->top++;
  97. break;
  98. case LIBINIT_LASTCL:
  99. setfuncV(L, L->top++, ofn);
  100. break;
  101. case LIBINIT_FFID:
  102. ffid++;
  103. break;
  104. case LIBINIT_END:
  105. return;
  106. default:
  107. setstrV(L, L->top++, lj_str_new(L, (const char *)p, len));
  108. p += len;
  109. break;
  110. }
  111. }
  112. }
  113. }
  114. /* -- Type checks --------------------------------------------------------- */
  115. TValue *lj_lib_checkany(lua_State *L, int narg)
  116. {
  117. TValue *o = L->base + narg-1;
  118. if (o >= L->top)
  119. lj_err_arg(L, narg, LJ_ERR_NOVAL);
  120. return o;
  121. }
  122. GCstr *lj_lib_checkstr(lua_State *L, int narg)
  123. {
  124. TValue *o = L->base + narg-1;
  125. if (o < L->top) {
  126. if (LJ_LIKELY(tvisstr(o))) {
  127. return strV(o);
  128. } else if (tvisnumber(o)) {
  129. GCstr *s = lj_str_fromnumber(L, o);
  130. setstrV(L, o, s);
  131. return s;
  132. }
  133. }
  134. lj_err_argt(L, narg, LUA_TSTRING);
  135. return NULL; /* unreachable */
  136. }
  137. GCstr *lj_lib_optstr(lua_State *L, int narg)
  138. {
  139. TValue *o = L->base + narg-1;
  140. return (o < L->top && !tvisnil(o)) ? lj_lib_checkstr(L, narg) : NULL;
  141. }
  142. #if LJ_DUALNUM
  143. void lj_lib_checknumber(lua_State *L, int narg)
  144. {
  145. TValue *o = L->base + narg-1;
  146. if (!(o < L->top && lj_strscan_numberobj(o)))
  147. lj_err_argt(L, narg, LUA_TNUMBER);
  148. }
  149. #endif
  150. lua_Number lj_lib_checknum(lua_State *L, int narg)
  151. {
  152. TValue *o = L->base + narg-1;
  153. if (!(o < L->top &&
  154. (tvisnumber(o) || (tvisstr(o) && lj_strscan_num(strV(o), o)))))
  155. lj_err_argt(L, narg, LUA_TNUMBER);
  156. if (LJ_UNLIKELY(tvisint(o))) {
  157. lua_Number n = (lua_Number)intV(o);
  158. setnumV(o, n);
  159. return n;
  160. } else {
  161. return numV(o);
  162. }
  163. }
  164. int32_t lj_lib_checkint(lua_State *L, int narg)
  165. {
  166. TValue *o = L->base + narg-1;
  167. if (!(o < L->top && lj_strscan_numberobj(o)))
  168. lj_err_argt(L, narg, LUA_TNUMBER);
  169. if (LJ_LIKELY(tvisint(o))) {
  170. return intV(o);
  171. } else {
  172. int32_t i = lj_num2int(numV(o));
  173. if (LJ_DUALNUM) setintV(o, i);
  174. return i;
  175. }
  176. }
  177. int32_t lj_lib_optint(lua_State *L, int narg, int32_t def)
  178. {
  179. TValue *o = L->base + narg-1;
  180. return (o < L->top && !tvisnil(o)) ? lj_lib_checkint(L, narg) : def;
  181. }
  182. int32_t lj_lib_checkbit(lua_State *L, int narg)
  183. {
  184. TValue *o = L->base + narg-1;
  185. if (!(o < L->top && lj_strscan_numberobj(o)))
  186. lj_err_argt(L, narg, LUA_TNUMBER);
  187. if (LJ_LIKELY(tvisint(o))) {
  188. return intV(o);
  189. } else {
  190. int32_t i = lj_num2bit(numV(o));
  191. if (LJ_DUALNUM) setintV(o, i);
  192. return i;
  193. }
  194. }
  195. GCfunc *lj_lib_checkfunc(lua_State *L, int narg)
  196. {
  197. TValue *o = L->base + narg-1;
  198. if (!(o < L->top && tvisfunc(o)))
  199. lj_err_argt(L, narg, LUA_TFUNCTION);
  200. return funcV(o);
  201. }
  202. GCtab *lj_lib_checktab(lua_State *L, int narg)
  203. {
  204. TValue *o = L->base + narg-1;
  205. if (!(o < L->top && tvistab(o)))
  206. lj_err_argt(L, narg, LUA_TTABLE);
  207. return tabV(o);
  208. }
  209. GCtab *lj_lib_checktabornil(lua_State *L, int narg)
  210. {
  211. TValue *o = L->base + narg-1;
  212. if (o < L->top) {
  213. if (tvistab(o))
  214. return tabV(o);
  215. else if (tvisnil(o))
  216. return NULL;
  217. }
  218. lj_err_arg(L, narg, LJ_ERR_NOTABN);
  219. return NULL; /* unreachable */
  220. }
  221. int lj_lib_checkopt(lua_State *L, int narg, int def, const char *lst)
  222. {
  223. GCstr *s = def >= 0 ? lj_lib_optstr(L, narg) : lj_lib_checkstr(L, narg);
  224. if (s) {
  225. const char *opt = strdata(s);
  226. MSize len = s->len;
  227. int i;
  228. for (i = 0; *(const uint8_t *)lst; i++) {
  229. if (*(const uint8_t *)lst == len && memcmp(opt, lst+1, len) == 0)
  230. return i;
  231. lst += 1+*(const uint8_t *)lst;
  232. }
  233. lj_err_argv(L, narg, LJ_ERR_INVOPTM, opt);
  234. }
  235. return def;
  236. }