lauxlib.c 18 KB

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