lauxlib.c 17 KB

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