lauxlib.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*
  2. ** $Id: lauxlib.c,v 1.159.1.3 2008/01/21 13:20:51 roberto Exp $
  3. ** Auxiliary functions for building Lua libraries
  4. ** See Copyright Notice in lua.h
  5. */
  6. #if 0
  7. #include <ctype.h>
  8. #include <errno.h>
  9. #include <stdarg.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #endif
  14. #include <grub/file.h>
  15. /* This file uses only the official API of Lua.
  16. ** Any function declared here could be written as an application function.
  17. */
  18. #define lauxlib_c
  19. #define LUA_LIB
  20. #include "lua.h"
  21. #include "lauxlib.h"
  22. #define FREELIST_REF 0 /* free list of references */
  23. /* convert a stack index to positive */
  24. #define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \
  25. lua_gettop(L) + (i) + 1)
  26. /*
  27. ** {======================================================
  28. ** Error-report functions
  29. ** =======================================================
  30. */
  31. LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {
  32. lua_Debug ar;
  33. if (!lua_getstack(L, 0, &ar)) /* no stack frame? */
  34. return luaL_error(L, "bad argument #%d (%s)", narg, extramsg);
  35. lua_getinfo(L, "n", &ar);
  36. if (strcmp(ar.namewhat, "method") == 0) {
  37. narg--; /* do not count `self' */
  38. if (narg == 0) /* error is in the self argument itself? */
  39. return luaL_error(L, "calling " LUA_QS " on bad self (%s)",
  40. ar.name, extramsg);
  41. }
  42. if (ar.name == NULL)
  43. ar.name = "?";
  44. return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)",
  45. narg, ar.name, extramsg);
  46. }
  47. LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) {
  48. const char *msg = lua_pushfstring(L, "%s expected, got %s",
  49. tname, luaL_typename(L, narg));
  50. return luaL_argerror(L, narg, msg);
  51. }
  52. static void tag_error (lua_State *L, int narg, int tag) {
  53. luaL_typerror(L, narg, lua_typename(L, tag));
  54. }
  55. LUALIB_API void luaL_where (lua_State *L, int level) {
  56. lua_Debug ar;
  57. if (lua_getstack(L, level, &ar)) { /* check function at level */
  58. lua_getinfo(L, "Sl", &ar); /* get info about it */
  59. if (ar.currentline > 0) { /* is there info? */
  60. lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline);
  61. return;
  62. }
  63. }
  64. lua_pushliteral(L, ""); /* else, no information available... */
  65. }
  66. LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
  67. va_list argp;
  68. va_start(argp, fmt);
  69. luaL_where(L, 1);
  70. lua_pushvfstring(L, fmt, argp);
  71. va_end(argp);
  72. lua_concat(L, 2);
  73. return lua_error(L);
  74. }
  75. /* }====================================================== */
  76. LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def,
  77. const char *const lst[]) {
  78. const char *name = (def) ? luaL_optstring(L, narg, def) :
  79. luaL_checkstring(L, narg);
  80. int i;
  81. for (i=0; lst[i]; i++)
  82. if (strcmp(lst[i], name) == 0)
  83. return i;
  84. return luaL_argerror(L, narg,
  85. lua_pushfstring(L, "invalid option " LUA_QS, name));
  86. }
  87. LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
  88. lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get registry.name */
  89. if (!lua_isnil(L, -1)) /* name already in use? */
  90. return 0; /* leave previous value on top, but return 0 */
  91. lua_pop(L, 1);
  92. lua_newtable(L); /* create metatable */
  93. lua_pushvalue(L, -1);
  94. lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */
  95. return 1;
  96. }
  97. LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
  98. void *p = lua_touserdata(L, ud);
  99. if (p != NULL) { /* value is a userdata? */
  100. if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
  101. lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */
  102. if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */
  103. lua_pop(L, 2); /* remove both metatables */
  104. return p;
  105. }
  106. }
  107. }
  108. luaL_typerror(L, ud, tname); /* else error */
  109. return NULL; /* to avoid warnings */
  110. }
  111. LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) {
  112. if (!lua_checkstack(L, space))
  113. luaL_error(L, "stack overflow (%s)", mes);
  114. }
  115. LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) {
  116. if (lua_type(L, narg) != t)
  117. tag_error(L, narg, t);
  118. }
  119. LUALIB_API void luaL_checkany (lua_State *L, int narg) {
  120. if (lua_type(L, narg) == LUA_TNONE)
  121. luaL_argerror(L, narg, "value expected");
  122. }
  123. LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) {
  124. const char *s = lua_tolstring(L, narg, len);
  125. if (!s) tag_error(L, narg, LUA_TSTRING);
  126. return s;
  127. }
  128. LUALIB_API const char *luaL_optlstring (lua_State *L, int narg,
  129. const char *def, size_t *len) {
  130. if (lua_isnoneornil(L, narg)) {
  131. if (len)
  132. *len = (def ? strlen(def) : 0);
  133. return def;
  134. }
  135. else return luaL_checklstring(L, narg, len);
  136. }
  137. LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) {
  138. lua_Number d = lua_tonumber(L, narg);
  139. if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
  140. tag_error(L, narg, LUA_TNUMBER);
  141. return d;
  142. }
  143. LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) {
  144. return luaL_opt(L, luaL_checknumber, narg, def);
  145. }
  146. LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
  147. lua_Integer d = lua_tointeger(L, narg);
  148. if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
  149. tag_error(L, narg, LUA_TNUMBER);
  150. return d;
  151. }
  152. LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg,
  153. lua_Integer def) {
  154. return luaL_opt(L, luaL_checkinteger, narg, def);
  155. }
  156. LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
  157. if (!lua_getmetatable(L, obj)) /* no metatable? */
  158. return 0;
  159. lua_pushstring(L, event);
  160. lua_rawget(L, -2);
  161. if (lua_isnil(L, -1)) {
  162. lua_pop(L, 2); /* remove metatable and metafield */
  163. return 0;
  164. }
  165. else {
  166. lua_remove(L, -2); /* remove only metatable */
  167. return 1;
  168. }
  169. }
  170. LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
  171. obj = abs_index(L, obj);
  172. if (!luaL_getmetafield(L, obj, event)) /* no metafield? */
  173. return 0;
  174. lua_pushvalue(L, obj);
  175. lua_call(L, 1, 1);
  176. return 1;
  177. }
  178. LUALIB_API void (luaL_register) (lua_State *L, const char *libname,
  179. const luaL_Reg *l) {
  180. luaI_openlib(L, libname, l, 0);
  181. }
  182. static int libsize (const luaL_Reg *l) {
  183. int size = 0;
  184. for (; l->name; l++) size++;
  185. return size;
  186. }
  187. LUALIB_API void luaI_openlib (lua_State *L, const char *libname,
  188. const luaL_Reg *l, int nup) {
  189. if (libname) {
  190. int size = libsize(l);
  191. /* check whether lib already exists */
  192. luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1);
  193. lua_getfield(L, -1, libname); /* get _LOADED[libname] */
  194. if (!lua_istable(L, -1)) { /* not found? */
  195. lua_pop(L, 1); /* remove previous result */
  196. /* try global variable (and create one if it does not exist) */
  197. if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL)
  198. luaL_error(L, "name conflict for module " LUA_QS, libname);
  199. lua_pushvalue(L, -1);
  200. lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */
  201. }
  202. lua_remove(L, -2); /* remove _LOADED table */
  203. lua_insert(L, -(nup+1)); /* move library table to below upvalues */
  204. }
  205. for (; l->name; l++) {
  206. int i;
  207. for (i=0; i<nup; i++) /* copy upvalues to the top */
  208. lua_pushvalue(L, -nup);
  209. lua_pushcclosure(L, l->func, nup);
  210. lua_setfield(L, -(nup+2), l->name);
  211. }
  212. lua_pop(L, nup); /* remove upvalues */
  213. }
  214. /*
  215. ** {======================================================
  216. ** getn-setn: size for arrays
  217. ** =======================================================
  218. */
  219. #if defined(LUA_COMPAT_GETN)
  220. static int checkint (lua_State *L, int topop) {
  221. int n = (lua_type(L, -1) == LUA_TNUMBER) ? lua_tointeger(L, -1) : -1;
  222. lua_pop(L, topop);
  223. return n;
  224. }
  225. static void getsizes (lua_State *L) {
  226. lua_getfield(L, LUA_REGISTRYINDEX, "LUA_SIZES");
  227. if (lua_isnil(L, -1)) { /* no `size' table? */
  228. lua_pop(L, 1); /* remove nil */
  229. lua_newtable(L); /* create it */
  230. lua_pushvalue(L, -1); /* `size' will be its own metatable */
  231. lua_setmetatable(L, -2);
  232. lua_pushliteral(L, "kv");
  233. lua_setfield(L, -2, "__mode"); /* metatable(N).__mode = "kv" */
  234. lua_pushvalue(L, -1);
  235. lua_setfield(L, LUA_REGISTRYINDEX, "LUA_SIZES"); /* store in register */
  236. }
  237. }
  238. LUALIB_API void luaL_setn (lua_State *L, int t, int n) {
  239. t = abs_index(L, t);
  240. lua_pushliteral(L, "n");
  241. lua_rawget(L, t);
  242. if (checkint(L, 1) >= 0) { /* is there a numeric field `n'? */
  243. lua_pushliteral(L, "n"); /* use it */
  244. lua_pushinteger(L, n);
  245. lua_rawset(L, t);
  246. }
  247. else { /* use `sizes' */
  248. getsizes(L);
  249. lua_pushvalue(L, t);
  250. lua_pushinteger(L, n);
  251. lua_rawset(L, -3); /* sizes[t] = n */
  252. lua_pop(L, 1); /* remove `sizes' */
  253. }
  254. }
  255. LUALIB_API int luaL_getn (lua_State *L, int t) {
  256. int n;
  257. t = abs_index(L, t);
  258. lua_pushliteral(L, "n"); /* try t.n */
  259. lua_rawget(L, t);
  260. if ((n = checkint(L, 1)) >= 0) return n;
  261. getsizes(L); /* else try sizes[t] */
  262. lua_pushvalue(L, t);
  263. lua_rawget(L, -2);
  264. if ((n = checkint(L, 2)) >= 0) return n;
  265. return (int)lua_objlen(L, t);
  266. }
  267. #endif
  268. /* }====================================================== */
  269. LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
  270. const char *r) {
  271. const char *wild;
  272. size_t l = strlen(p);
  273. luaL_Buffer b;
  274. luaL_buffinit(L, &b);
  275. while ((wild = strstr(s, p)) != NULL) {
  276. luaL_addlstring(&b, s, wild - s); /* push prefix */
  277. luaL_addstring(&b, r); /* push replacement in place of pattern */
  278. s = wild + l; /* continue after `p' */
  279. }
  280. luaL_addstring(&b, s); /* push last suffix */
  281. luaL_pushresult(&b);
  282. return lua_tostring(L, -1);
  283. }
  284. LUALIB_API const char *luaL_findtable (lua_State *L, int idx,
  285. const char *fname, int szhint) {
  286. const char *e;
  287. lua_pushvalue(L, idx);
  288. do {
  289. e = strchr(fname, '.');
  290. if (e == NULL) e = fname + strlen(fname);
  291. lua_pushlstring(L, fname, e - fname);
  292. lua_rawget(L, -2);
  293. if (lua_isnil(L, -1)) { /* no such field? */
  294. lua_pop(L, 1); /* remove this nil */
  295. lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */
  296. lua_pushlstring(L, fname, e - fname);
  297. lua_pushvalue(L, -2);
  298. lua_settable(L, -4); /* set new table into field */
  299. }
  300. else if (!lua_istable(L, -1)) { /* field has a non-table value? */
  301. lua_pop(L, 2); /* remove table and value */
  302. return fname; /* return problematic part of the name */
  303. }
  304. lua_remove(L, -2); /* remove previous table */
  305. fname = e + 1;
  306. } while (*e == '.');
  307. return NULL;
  308. }
  309. /*
  310. ** {======================================================
  311. ** Generic Buffer manipulation
  312. ** =======================================================
  313. */
  314. #define bufflen(B) ((B)->p - (B)->buffer)
  315. #define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B)))
  316. #define LIMIT (LUA_MINSTACK/2)
  317. static int emptybuffer (luaL_Buffer *B) {
  318. size_t l = bufflen(B);
  319. if (l == 0) return 0; /* put nothing on stack */
  320. else {
  321. lua_pushlstring(B->L, B->buffer, l);
  322. B->p = B->buffer;
  323. B->lvl++;
  324. return 1;
  325. }
  326. }
  327. static void adjuststack (luaL_Buffer *B) {
  328. if (B->lvl > 1) {
  329. lua_State *L = B->L;
  330. int toget = 1; /* number of levels to concat */
  331. size_t toplen = lua_strlen(L, -1);
  332. do {
  333. size_t l = lua_strlen(L, -(toget+1));
  334. if (B->lvl - toget + 1 >= LIMIT || toplen > l) {
  335. toplen += l;
  336. toget++;
  337. }
  338. else break;
  339. } while (toget < B->lvl);
  340. lua_concat(L, toget);
  341. B->lvl = B->lvl - toget + 1;
  342. }
  343. }
  344. LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) {
  345. if (emptybuffer(B))
  346. adjuststack(B);
  347. return B->buffer;
  348. }
  349. LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
  350. while (l--)
  351. luaL_addchar(B, *s++);
  352. }
  353. LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
  354. luaL_addlstring(B, s, strlen(s));
  355. }
  356. LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
  357. emptybuffer(B);
  358. lua_concat(B->L, B->lvl);
  359. B->lvl = 1;
  360. }
  361. LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
  362. lua_State *L = B->L;
  363. size_t vl;
  364. const char *s = lua_tolstring(L, -1, &vl);
  365. if (vl <= bufffree(B)) { /* fit into buffer? */
  366. memcpy(B->p, s, vl); /* put it there */
  367. B->p += vl;
  368. lua_pop(L, 1); /* remove from stack */
  369. }
  370. else {
  371. if (emptybuffer(B))
  372. lua_insert(L, -2); /* put buffer before new value */
  373. B->lvl++; /* add new value into B stack */
  374. adjuststack(B);
  375. }
  376. }
  377. LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
  378. B->L = L;
  379. B->p = B->buffer;
  380. B->lvl = 0;
  381. }
  382. /* }====================================================== */
  383. LUALIB_API int luaL_ref (lua_State *L, int t) {
  384. int ref;
  385. t = abs_index(L, t);
  386. if (lua_isnil(L, -1)) {
  387. lua_pop(L, 1); /* remove from stack */
  388. return LUA_REFNIL; /* `nil' has a unique fixed reference */
  389. }
  390. lua_rawgeti(L, t, FREELIST_REF); /* get first free element */
  391. ref = (int)lua_tointeger(L, -1); /* ref = t[FREELIST_REF] */
  392. lua_pop(L, 1); /* remove it from stack */
  393. if (ref != 0) { /* any free element? */
  394. lua_rawgeti(L, t, ref); /* remove it from list */
  395. lua_rawseti(L, t, FREELIST_REF); /* (t[FREELIST_REF] = t[ref]) */
  396. }
  397. else { /* no free elements */
  398. ref = (int)lua_objlen(L, t);
  399. ref++; /* create new reference */
  400. }
  401. lua_rawseti(L, t, ref);
  402. return ref;
  403. }
  404. LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
  405. if (ref >= 0) {
  406. t = abs_index(L, t);
  407. lua_rawgeti(L, t, FREELIST_REF);
  408. lua_rawseti(L, t, ref); /* t[ref] = t[FREELIST_REF] */
  409. lua_pushinteger(L, ref);
  410. lua_rawseti(L, t, FREELIST_REF); /* t[FREELIST_REF] = ref */
  411. }
  412. }
  413. /*
  414. ** {======================================================
  415. ** Load functions
  416. ** =======================================================
  417. */
  418. #define GRUB_EOF -1
  419. static int
  420. grub_getc (grub_file_t file)
  421. {
  422. grub_uint8_t c = 0;
  423. return (grub_file_read (file, &c, 1) != 1) ? GRUB_EOF : c;
  424. return GRUB_EOF;
  425. }
  426. #define grub_eof(file) (file->offset >= file->size)
  427. typedef struct LoadF {
  428. int extraline;
  429. grub_file_t f;
  430. int ungetc;
  431. char buff[LUAL_BUFFERSIZE];
  432. } LoadF;
  433. static const char *getF (lua_State *L, void *ud, size_t *size) {
  434. LoadF *lf = (LoadF *)ud;
  435. char *p;
  436. int s;
  437. (void)L;
  438. if (lf->extraline) {
  439. lf->extraline = 0;
  440. *size = 1;
  441. return "\n";
  442. }
  443. s = sizeof (lf->buff);
  444. p = lf->buff;
  445. if (lf->ungetc >= 0)
  446. {
  447. lf->buff[0] = lf->ungetc;
  448. lf->ungetc = -1;
  449. s--;
  450. p++;
  451. }
  452. if (grub_eof(lf->f)) return NULL;
  453. s = grub_file_read(lf->f, p, s);
  454. if (s <= 0)
  455. return NULL;
  456. *size = s + (p - lf->buff);
  457. return (*size > 0) ? lf->buff : NULL;
  458. }
  459. static int errfile (lua_State *L, const char *what, int fnameindex) {
  460. const char *serr = grub_errmsg;
  461. const char *filename = lua_tostring(L, fnameindex) + 1;
  462. lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
  463. lua_remove(L, fnameindex);
  464. return LUA_ERRFILE;
  465. }
  466. LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
  467. LoadF lf;
  468. int status, readstatus;
  469. int c;
  470. int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
  471. lf.extraline = 0;
  472. if (filename == NULL) {
  473. lua_pushliteral(L, "=stdin");
  474. return errfile(L, "open", fnameindex);
  475. }
  476. else {
  477. lua_pushfstring(L, "@%s", filename);
  478. lf.f = grub_file_open(filename);
  479. if (lf.f == NULL) return errfile(L, "open", fnameindex);
  480. }
  481. c = grub_getc(lf.f);
  482. if (c == '#') { /* Unix exec. file? */
  483. lf.extraline = 1;
  484. while ((c = grub_getc(lf.f)) != GRUB_EOF && c != '\n') ; /* skip first line */
  485. if (c == '\n') c = grub_getc(lf.f);
  486. }
  487. if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
  488. /* skip eventual `#!...' */
  489. while ((c = grub_getc(lf.f)) != GRUB_EOF && c != LUA_SIGNATURE[0]) ;
  490. lf.extraline = 0;
  491. }
  492. lf.ungetc = c;
  493. status = lua_load(L, getF, &lf, lua_tostring(L, -1));
  494. readstatus = grub_errno;
  495. grub_file_close(lf.f); /* close file (even in case of errors) */
  496. if (readstatus) {
  497. lua_settop(L, fnameindex); /* ignore results from `lua_load' */
  498. return errfile(L, "read", fnameindex);
  499. }
  500. lua_remove(L, fnameindex);
  501. return status;
  502. }
  503. typedef struct LoadS {
  504. const char *s;
  505. size_t size;
  506. } LoadS;
  507. static const char *getS (lua_State *L, void *ud, size_t *size) {
  508. LoadS *ls = (LoadS *)ud;
  509. (void)L;
  510. if (ls->size == 0) return NULL;
  511. *size = ls->size;
  512. ls->size = 0;
  513. return ls->s;
  514. }
  515. LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size,
  516. const char *name) {
  517. LoadS ls;
  518. ls.s = buff;
  519. ls.size = size;
  520. return lua_load(L, getS, &ls, name);
  521. }
  522. LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s) {
  523. return luaL_loadbuffer(L, s, strlen(s), s);
  524. }
  525. /* }====================================================== */
  526. static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
  527. (void)ud;
  528. (void)osize;
  529. if (nsize == 0) {
  530. free(ptr);
  531. return NULL;
  532. }
  533. else
  534. return realloc(ptr, nsize);
  535. }
  536. static int panic (lua_State *L) {
  537. (void)L; /* to avoid warnings */
  538. #if 0
  539. fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n",
  540. lua_tostring(L, -1));
  541. #else
  542. grub_fatal ("PANIC: unprotected error in call to Lua API (%s)\n",
  543. lua_tostring(L, -1));
  544. #endif
  545. return 0;
  546. }
  547. LUALIB_API lua_State *luaL_newstate (void) {
  548. lua_State *L = lua_newstate(l_alloc, NULL);
  549. if (L) lua_atpanic(L, &panic);
  550. return L;
  551. }