lauxlib.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*
  2. ** $Id: lauxlib.c,v 1.92 2003/01/23 11:34:18 roberto Exp roberto $
  3. ** Auxiliary functions for building Lua libraries
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <errno.h>
  8. #include <stdarg.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. /* This file uses only the official API of Lua.
  12. ** Any function declared here could be written as an application function.
  13. */
  14. #define lauxlib_c
  15. #include "lua.h"
  16. #include "lauxlib.h"
  17. /* number of prereserved references (for internal use) */
  18. #define RESERVED_REFS 1
  19. /* reserved reference for array sizes */
  20. #define ARRAYSIZE_REF 1
  21. /*
  22. ** {======================================================
  23. ** Error-report functions
  24. ** =======================================================
  25. */
  26. LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {
  27. lua_Debug ar;
  28. lua_getstack(L, 0, &ar);
  29. lua_getinfo(L, "n", &ar);
  30. if (strcmp(ar.namewhat, "method") == 0) {
  31. narg--; /* do not count `self' */
  32. if (narg == 0) /* error is in the self argument itself? */
  33. return luaL_error(L,
  34. "calling %s on bad self (perhaps using `:' instead of `.')",
  35. ar.name);
  36. }
  37. if (ar.name == NULL)
  38. ar.name = "?";
  39. return luaL_error(L, "bad argument #%d to `%s' (%s)",
  40. narg, ar.name, extramsg);
  41. }
  42. LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) {
  43. const char *msg = lua_pushfstring(L, "%s expected, got %s",
  44. tname, lua_typename(L, lua_type(L,narg)));
  45. return luaL_argerror(L, narg, msg);
  46. }
  47. static void tag_error (lua_State *L, int narg, int tag) {
  48. luaL_typerror(L, narg, lua_typename(L, tag));
  49. }
  50. LUALIB_API void luaL_where (lua_State *L, int level) {
  51. lua_Debug ar;
  52. if (lua_getstack(L, level, &ar)) { /* check function at level */
  53. lua_getinfo(L, "Snl", &ar); /* get info about it */
  54. if (ar.currentline > 0) { /* is there info? */
  55. lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline);
  56. return;
  57. }
  58. }
  59. lua_pushliteral(L, ""); /* else, no information available... */
  60. }
  61. LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
  62. va_list argp;
  63. va_start(argp, fmt);
  64. luaL_where(L, 1);
  65. lua_pushvfstring(L, fmt, argp);
  66. va_end(argp);
  67. lua_concat(L, 2);
  68. return lua_error(L);
  69. }
  70. /* }====================================================== */
  71. LUALIB_API int luaL_findstring (const char *name, const char *const list[]) {
  72. int i;
  73. for (i=0; list[i]; i++)
  74. if (strcmp(list[i], name) == 0)
  75. return i;
  76. return -1; /* name not found */
  77. }
  78. LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) {
  79. if (!lua_checkstack(L, space))
  80. luaL_error(L, "stack overflow (%s)", mes);
  81. }
  82. LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) {
  83. if (lua_type(L, narg) != t)
  84. tag_error(L, narg, t);
  85. }
  86. LUALIB_API void luaL_checkany (lua_State *L, int narg) {
  87. if (lua_type(L, narg) == LUA_TNONE)
  88. luaL_argerror(L, narg, "value expected");
  89. }
  90. LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) {
  91. const char *s = lua_tostring(L, narg);
  92. if (!s) tag_error(L, narg, LUA_TSTRING);
  93. if (len) *len = lua_strlen(L, narg);
  94. return s;
  95. }
  96. LUALIB_API const char *luaL_optlstring (lua_State *L, int narg,
  97. const char *def, size_t *len) {
  98. if (lua_isnoneornil(L, narg)) {
  99. if (len)
  100. *len = (def ? strlen(def) : 0);
  101. return def;
  102. }
  103. else return luaL_checklstring(L, narg, len);
  104. }
  105. LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) {
  106. lua_Number d = lua_tonumber(L, narg);
  107. if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
  108. tag_error(L, narg, LUA_TNUMBER);
  109. return d;
  110. }
  111. LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) {
  112. if (lua_isnoneornil(L, narg)) return def;
  113. else return luaL_checknumber(L, narg);
  114. }
  115. LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
  116. if (!lua_getmetatable(L, obj)) /* no metatable? */
  117. return 0;
  118. lua_pushstring(L, event);
  119. lua_rawget(L, -2);
  120. if (lua_isnil(L, -1)) {
  121. lua_pop(L, 2); /* remove metatable and metafield */
  122. return 0;
  123. }
  124. return 1;
  125. }
  126. LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
  127. if (!luaL_getmetafield(L, obj, event)) /* no metafield? */
  128. return 0;
  129. lua_pushvalue(L, obj);
  130. lua_call(L, 1, 1);
  131. return 1;
  132. }
  133. LUALIB_API void luaL_openlib (lua_State *L, const char *libname,
  134. const luaL_reg *l, int nup) {
  135. if (libname) {
  136. lua_pushstring(L, libname);
  137. lua_gettable(L, LUA_GLOBALSINDEX); /* check whether lib already exists */
  138. if (lua_isnil(L, -1)) { /* no? */
  139. lua_pop(L, 1);
  140. lua_newtable(L); /* create it */
  141. lua_pushstring(L, libname);
  142. lua_pushvalue(L, -2);
  143. lua_settable(L, LUA_GLOBALSINDEX); /* register it with given name */
  144. }
  145. lua_insert(L, -(nup+1)); /* move library table to below upvalues */
  146. }
  147. for (; l->name; l++) {
  148. int i;
  149. lua_pushstring(L, l->name);
  150. for (i=0; i<nup; i++) /* copy upvalues to the top */
  151. lua_pushvalue(L, -(nup+1));
  152. lua_pushcclosure(L, l->func, nup);
  153. lua_settable(L, -(nup+3));
  154. }
  155. lua_pop(L, nup); /* remove upvalues */
  156. }
  157. /*
  158. ** {======================================================
  159. ** getn-setn: size for arrays
  160. ** =======================================================
  161. */
  162. static int checkint (lua_State *L, int topop) {
  163. int n = (int)lua_tonumber(L, -1);
  164. if (n == 0 && !lua_isnumber(L, -1)) n = -1;
  165. lua_pop(L, topop);
  166. return n;
  167. }
  168. static void getsizes (lua_State *L) {
  169. lua_rawgeti(L, LUA_REGISTRYINDEX, ARRAYSIZE_REF);
  170. if (lua_isnil(L, -1)) { /* no `size' table? */
  171. lua_newtable(L); /* create it */
  172. lua_pushvalue(L, -1); /* `size' will be its own metatable */
  173. lua_setmetatable(L, -2);
  174. lua_pushliteral(L, "__mode");
  175. lua_pushliteral(L, "k");
  176. lua_rawset(L, -3); /* metatable(N).__mode = "k" */
  177. lua_pushvalue(L, -1);
  178. lua_rawseti(L, LUA_REGISTRYINDEX, ARRAYSIZE_REF); /* store in register */
  179. }
  180. }
  181. void luaL_setn (lua_State *L, int t, int n) {
  182. lua_pushliteral(L, "n");
  183. lua_rawget(L, t);
  184. if (checkint(L, 1) >= 0) { /* is there a numeric field `n'? */
  185. lua_pushliteral(L, "n"); /* use it */
  186. lua_pushnumber(L, n);
  187. lua_rawset(L, t);
  188. }
  189. else { /* use `sizes' */
  190. getsizes(L);
  191. lua_pushvalue(L, t);
  192. lua_pushnumber(L, n);
  193. lua_rawset(L, -3); /* sizes[t] = n */
  194. lua_pop(L, 1); /* remove `sizes' */
  195. }
  196. }
  197. int luaL_getn (lua_State *L, int t) {
  198. int n;
  199. lua_pushliteral(L, "n"); /* try t.n */
  200. lua_rawget(L, t);
  201. if ((n = checkint(L, 1)) >= 0) return n;
  202. getsizes(L); /* else try sizes[t] */
  203. lua_pushvalue(L, t);
  204. lua_rawget(L, -2);
  205. if ((n = checkint(L, 2)) >= 0) return n;
  206. else { /* must count elements */
  207. for (n = 1; ; n++) {
  208. lua_rawgeti(L, t, n);
  209. if (lua_isnil(L, -1)) break;
  210. lua_pop(L, 1);
  211. }
  212. lua_pop(L, 1);
  213. luaL_setn(L, t, n - 1);
  214. return n - 1;
  215. }
  216. }
  217. /* }====================================================== */
  218. /*
  219. ** {======================================================
  220. ** Generic Buffer manipulation
  221. ** =======================================================
  222. */
  223. #define buffempty(B) ((B)->p == (B)->buffer)
  224. #define bufflen(B) ((B)->p - (B)->buffer)
  225. #define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B)))
  226. #define LIMIT (LUA_MINSTACK/2)
  227. static int emptybuffer (luaL_Buffer *B) {
  228. size_t l = bufflen(B);
  229. if (l == 0) return 0; /* put nothing on stack */
  230. else {
  231. lua_pushlstring(B->L, B->buffer, l);
  232. B->p = B->buffer;
  233. B->lvl++;
  234. return 1;
  235. }
  236. }
  237. static void adjuststack (luaL_Buffer *B) {
  238. if (B->lvl > 1) {
  239. lua_State *L = B->L;
  240. int toget = 1; /* number of levels to concat */
  241. size_t toplen = lua_strlen(L, -1);
  242. do {
  243. size_t l = lua_strlen(L, -(toget+1));
  244. if (B->lvl - toget + 1 >= LIMIT || toplen > l) {
  245. toplen += l;
  246. toget++;
  247. }
  248. else break;
  249. } while (toget < B->lvl);
  250. lua_concat(L, toget);
  251. B->lvl = B->lvl - toget + 1;
  252. }
  253. }
  254. LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) {
  255. if (emptybuffer(B))
  256. adjuststack(B);
  257. return B->buffer;
  258. }
  259. LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
  260. while (l--)
  261. luaL_putchar(B, *s++);
  262. }
  263. LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
  264. luaL_addlstring(B, s, strlen(s));
  265. }
  266. LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
  267. emptybuffer(B);
  268. lua_concat(B->L, B->lvl);
  269. B->lvl = 1;
  270. }
  271. LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
  272. lua_State *L = B->L;
  273. size_t vl = lua_strlen(L, -1);
  274. if (vl <= bufffree(B)) { /* fit into buffer? */
  275. memcpy(B->p, lua_tostring(L, -1), vl); /* put it there */
  276. B->p += vl;
  277. lua_pop(L, 1); /* remove from stack */
  278. }
  279. else {
  280. if (emptybuffer(B))
  281. lua_insert(L, -2); /* put buffer before new value */
  282. B->lvl++; /* add new value into B stack */
  283. adjuststack(B);
  284. }
  285. }
  286. LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
  287. B->L = L;
  288. B->p = B->buffer;
  289. B->lvl = 0;
  290. }
  291. /* }====================================================== */
  292. LUALIB_API int luaL_ref (lua_State *L, int t) {
  293. int ref;
  294. if (lua_isnil(L, -1)) {
  295. lua_pop(L, 1); /* remove from stack */
  296. return LUA_REFNIL; /* `nil' has a unique fixed reference */
  297. }
  298. lua_rawgeti(L, t, 0); /* get first free element */
  299. ref = (int)lua_tonumber(L, -1); /* ref = t[0] */
  300. lua_pop(L, 1); /* remove it from stack */
  301. if (ref != 0) { /* any free element? */
  302. lua_rawgeti(L, t, ref); /* remove it from list */
  303. lua_rawseti(L, t, 0); /* (that is, t[0] = t[ref]) */
  304. }
  305. else { /* no free elements */
  306. lua_pushliteral(L, "n");
  307. lua_pushvalue(L, -1);
  308. lua_rawget(L, t); /* get t.n */
  309. ref = (int)lua_tonumber(L, -1); /* ref = t.n */
  310. lua_pop(L, 1); /* pop t.n */
  311. if (ref == 0) ref = RESERVED_REFS; /* skip reserved references */
  312. ref++; /* create new reference */
  313. lua_pushnumber(L, ref);
  314. lua_rawset(L, t); /* t.n = t.n + 1 */
  315. }
  316. lua_rawseti(L, t, ref);
  317. return ref;
  318. }
  319. LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
  320. if (ref >= 0) {
  321. lua_rawgeti(L, t, 0);
  322. lua_rawseti(L, t, ref); /* t[ref] = t[0] */
  323. lua_pushnumber(L, ref);
  324. lua_rawseti(L, t, 0); /* t[0] = ref */
  325. }
  326. }
  327. /*
  328. ** {======================================================
  329. ** Load functions
  330. ** =======================================================
  331. */
  332. typedef struct LoadF {
  333. FILE *f;
  334. char buff[LUAL_BUFFERSIZE];
  335. } LoadF;
  336. static const char *getF (lua_State *L, void *ud, size_t *size) {
  337. LoadF *lf = (LoadF *)ud;
  338. (void)L;
  339. if (feof(lf->f)) return NULL;
  340. *size = fread(lf->buff, 1, LUAL_BUFFERSIZE, lf->f);
  341. return (*size > 0) ? lf->buff : NULL;
  342. }
  343. static int errfile (lua_State *L, int fnameindex) {
  344. const char *filename = lua_tostring(L, fnameindex) + 1;
  345. lua_pushfstring(L, "cannot read %s: %s", filename, strerror(errno));
  346. lua_remove(L, fnameindex);
  347. return LUA_ERRFILE;
  348. }
  349. LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
  350. LoadF lf;
  351. int status, readstatus;
  352. int c;
  353. int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
  354. if (filename == NULL) {
  355. lua_pushliteral(L, "=stdin");
  356. lf.f = stdin;
  357. }
  358. else {
  359. lua_pushfstring(L, "@%s", filename);
  360. lf.f = fopen(filename, "r");
  361. }
  362. if (lf.f == NULL) return errfile(L, fnameindex); /* unable to open file */
  363. c = ungetc(getc(lf.f), lf.f);
  364. if (!(isspace(c) || isprint(c)) && lf.f != stdin) { /* binary file? */
  365. fclose(lf.f);
  366. lf.f = fopen(filename, "rb"); /* reopen in binary mode */
  367. if (lf.f == NULL) return errfile(L, fnameindex); /* unable to reopen file */
  368. }
  369. status = lua_load(L, getF, &lf, lua_tostring(L, -1));
  370. readstatus = ferror(lf.f);
  371. if (lf.f != stdin) fclose(lf.f); /* close file (even in case of errors) */
  372. if (readstatus) {
  373. lua_settop(L, fnameindex); /* ignore results from `lua_load' */
  374. return errfile(L, fnameindex);
  375. }
  376. lua_remove(L, fnameindex);
  377. return status;
  378. }
  379. typedef struct LoadS {
  380. const char *s;
  381. size_t size;
  382. } LoadS;
  383. static const char *getS (lua_State *L, void *ud, size_t *size) {
  384. LoadS *ls = (LoadS *)ud;
  385. (void)L;
  386. if (ls->size == 0) return NULL;
  387. *size = ls->size;
  388. ls->size = 0;
  389. return ls->s;
  390. }
  391. LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size,
  392. const char *name) {
  393. LoadS ls;
  394. ls.s = buff;
  395. ls.size = size;
  396. return lua_load(L, getS, &ls, name);
  397. }
  398. /* }====================================================== */
  399. /*
  400. ** {======================================================
  401. ** compatibility code
  402. ** =======================================================
  403. */
  404. static void callalert (lua_State *L, int status) {
  405. if (status != 0) {
  406. lua_getglobal(L, "_ALERT");
  407. if (lua_isfunction(L, -1)) {
  408. lua_insert(L, -2);
  409. lua_call(L, 1, 0);
  410. }
  411. else { /* no _ALERT function; print it on stderr */
  412. fprintf(stderr, "%s\n", lua_tostring(L, -2));
  413. lua_pop(L, 2); /* remove error message and _ALERT */
  414. }
  415. }
  416. }
  417. static int aux_do (lua_State *L, int status) {
  418. if (status == 0) { /* parse OK? */
  419. status = lua_pcall(L, 0, LUA_MULTRET, 0); /* call main */
  420. }
  421. callalert(L, status);
  422. return status;
  423. }
  424. LUALIB_API int lua_dofile (lua_State *L, const char *filename) {
  425. return aux_do(L, luaL_loadfile(L, filename));
  426. }
  427. LUALIB_API int lua_dobuffer (lua_State *L, const char *buff, size_t size,
  428. const char *name) {
  429. return aux_do(L, luaL_loadbuffer(L, buff, size, name));
  430. }
  431. LUALIB_API int lua_dostring (lua_State *L, const char *str) {
  432. return lua_dobuffer(L, str, strlen(str), str);
  433. }
  434. /* }====================================================== */