lauxlib.c 18 KB

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