lauxlib.c 15 KB

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