lauxlib.c 15 KB

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