lauxlib.c 18 KB

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