lauxlib.c 18 KB

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