lauxlib.c 18 KB

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