lauxlib.c 18 KB

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