lauxlib.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. /*
  2. ** $Id: lauxlib.c,v 1.159.1.3 2008/01/21 13:20:51 roberto Exp $
  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 "lrotable.h"
  19. #include "lauxlib.h"
  20. #include "lgc.h"
  21. #include "ldo.h"
  22. #include "lobject.h"
  23. #include "lstate.h"
  24. #include "legc.h"
  25. #define FREELIST_REF 0 /* free list of references */
  26. /* convert a stack index to positive */
  27. #define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \
  28. lua_gettop(L) + (i) + 1)
  29. // Parameters for luaI_openlib
  30. #define LUA_USECCLOSURES 0
  31. #define LUA_USELIGHTFUNCTIONS 1
  32. /*
  33. ** {======================================================
  34. ** Error-report functions
  35. ** =======================================================
  36. */
  37. LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *extramsg)
  38. {
  39. lua_Debug ar;
  40. if (!lua_getstack(L, 0, &ar)) /* no stack frame? */
  41. return luaL_error(L, "bad argument #%d (%s)", narg, extramsg);
  42. lua_getinfo(L, "n", &ar);
  43. if (strcmp(ar.namewhat, "method") == 0)
  44. {
  45. narg--; /* do not count `self' */
  46. if (narg == 0) /* error is in the self argument itself? */
  47. return luaL_error(L, "calling " LUA_QS " on bad self (%s)",
  48. ar.name, extramsg);
  49. }
  50. if (ar.name == NULL)
  51. ar.name = "?";
  52. return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)",
  53. narg, ar.name, extramsg);
  54. }
  55. LUALIB_API int luaL_typerror(lua_State *L, int narg, const char *tname)
  56. {
  57. const char *msg = lua_pushfstring(L, "%s expected, got %s",
  58. tname, luaL_typename(L, narg));
  59. return luaL_argerror(L, narg, msg);
  60. }
  61. static void tag_error(lua_State *L, int narg, int tag)
  62. {
  63. luaL_typerror(L, narg, lua_typename(L, tag));
  64. }
  65. LUALIB_API void luaL_where(lua_State *L, int level)
  66. {
  67. lua_Debug ar;
  68. if (lua_getstack(L, level, &ar)) /* check function at level */
  69. {
  70. lua_getinfo(L, "Sl", &ar); /* get info about it */
  71. if (ar.currentline > 0) /* is there info? */
  72. {
  73. lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline);
  74. return;
  75. }
  76. }
  77. lua_pushliteral(L, ""); /* else, no information available... */
  78. }
  79. LUALIB_API int luaL_error(lua_State *L, const char *fmt, ...)
  80. {
  81. va_list argp;
  82. va_start(argp, fmt);
  83. luaL_where(L, 1);
  84. lua_pushvfstring(L, fmt, argp);
  85. va_end(argp);
  86. lua_concat(L, 2);
  87. return lua_error(L);
  88. }
  89. /* }====================================================== */
  90. LUALIB_API int luaL_checkoption(lua_State *L, int narg, const char *def,
  91. const char *const lst[])
  92. {
  93. const char *name = (def) ? luaL_optstring(L, narg, def) :
  94. luaL_checkstring(L, narg);
  95. int i;
  96. for (i = 0; lst[i]; i++)
  97. if (strcmp(lst[i], name) == 0)
  98. return i;
  99. return luaL_argerror(L, narg,
  100. lua_pushfstring(L, "invalid option " LUA_QS, name));
  101. }
  102. LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname)
  103. {
  104. lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get registry.name */
  105. if (!lua_isnil(L, -1)) /* name already in use? */
  106. return 0; /* leave previous value on top, but return 0 */
  107. lua_pop(L, 1);
  108. lua_newtable(L); /* create metatable */
  109. lua_pushvalue(L, -1);
  110. lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */
  111. return 1;
  112. }
  113. LUALIB_API int luaL_rometatable(lua_State *L, const char *tname, void *p)
  114. {
  115. lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get registry.name */
  116. if (!lua_isnil(L, -1)) /* name already in use? */
  117. return 0; /* leave previous value on top, but return 0 */
  118. lua_pop(L, 1);
  119. lua_pushrotable(L, p);
  120. lua_pushvalue(L, -1);
  121. lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */
  122. return 1;
  123. }
  124. LUALIB_API void *luaL_checkudata(lua_State *L, int ud, const char *tname)
  125. {
  126. void *p = lua_touserdata(L, ud);
  127. if (p != NULL) /* value is a userdata? */
  128. {
  129. if (lua_getmetatable(L, ud)) /* does it have a metatable? */
  130. {
  131. lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */
  132. if (lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
  133. {
  134. lua_pop(L, 2); /* remove both metatables */
  135. return p;
  136. }
  137. }
  138. }
  139. luaL_typerror(L, ud, tname); /* else error */
  140. return NULL; /* to avoid warnings */
  141. }
  142. LUALIB_API void luaL_checkstack(lua_State *L, int space, const char *mes)
  143. {
  144. if (!lua_checkstack(L, space))
  145. luaL_error(L, "stack overflow (%s)", mes);
  146. }
  147. LUALIB_API void luaL_checktype(lua_State *L, int narg, int t)
  148. {
  149. if (lua_type(L, narg) != t)
  150. tag_error(L, narg, t);
  151. }
  152. LUALIB_API void luaL_checkanyfunction(lua_State *L, int narg)
  153. {
  154. if (lua_type(L, narg) != LUA_TFUNCTION && lua_type(L, narg) != LUA_TLIGHTFUNCTION)
  155. {
  156. const char *msg = lua_pushfstring(L, "function or lightfunction expected, got %s",
  157. luaL_typename(L, narg));
  158. luaL_argerror(L, narg, msg);
  159. }
  160. }
  161. LUALIB_API void luaL_checkanytable(lua_State *L, int narg)
  162. {
  163. if (lua_type(L, narg) != LUA_TTABLE && lua_type(L, narg) != LUA_TROTABLE)
  164. {
  165. const char *msg = lua_pushfstring(L, "table or rotable expected, got %s",
  166. luaL_typename(L, narg));
  167. luaL_argerror(L, narg, msg);
  168. }
  169. }
  170. LUALIB_API void luaL_checkany(lua_State *L, int narg)
  171. {
  172. if (lua_type(L, narg) == LUA_TNONE)
  173. luaL_argerror(L, narg, "value expected");
  174. }
  175. LUALIB_API const char *luaL_checklstring(lua_State *L, int narg, size_t *len)
  176. {
  177. const char *s = lua_tolstring(L, narg, len);
  178. if (!s) tag_error(L, narg, LUA_TSTRING);
  179. return s;
  180. }
  181. LUALIB_API const char *luaL_optlstring(lua_State *L, int narg,
  182. const char *def, size_t *len)
  183. {
  184. if (lua_isnoneornil(L, narg))
  185. {
  186. if (len)
  187. *len = (def ? strlen(def) : 0);
  188. return def;
  189. }
  190. else return luaL_checklstring(L, narg, len);
  191. }
  192. LUALIB_API lua_Number luaL_checknumber(lua_State *L, int narg)
  193. {
  194. lua_Number d = lua_tonumber(L, narg);
  195. if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
  196. tag_error(L, narg, LUA_TNUMBER);
  197. return d;
  198. }
  199. LUALIB_API lua_Number luaL_optnumber(lua_State *L, int narg, lua_Number def)
  200. {
  201. return luaL_opt(L, luaL_checknumber, narg, def);
  202. }
  203. LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int narg)
  204. {
  205. lua_Integer d = lua_tointeger(L, narg);
  206. if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
  207. tag_error(L, narg, LUA_TNUMBER);
  208. return d;
  209. }
  210. LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int narg,
  211. lua_Integer def)
  212. {
  213. return luaL_opt(L, luaL_checkinteger, narg, def);
  214. }
  215. LUALIB_API int luaL_getmetafield(lua_State *L, int obj, const char *event)
  216. {
  217. if (!lua_getmetatable(L, obj)) /* no metatable? */
  218. return 0;
  219. lua_pushstring(L, event);
  220. lua_rawget(L, -2);
  221. if (lua_isnil(L, -1))
  222. {
  223. lua_pop(L, 2); /* remove metatable and metafield */
  224. return 0;
  225. }
  226. else
  227. {
  228. lua_remove(L, -2); /* remove only metatable */
  229. return 1;
  230. }
  231. }
  232. LUALIB_API int luaL_callmeta(lua_State *L, int obj, const char *event)
  233. {
  234. obj = abs_index(L, obj);
  235. if (!luaL_getmetafield(L, obj, event)) /* no metafield? */
  236. return 0;
  237. lua_pushvalue(L, obj);
  238. lua_call(L, 1, 1);
  239. return 1;
  240. }
  241. LUALIB_API void (luaL_register)(lua_State *L, const char *libname,
  242. const luaL_Reg *l)
  243. {
  244. luaI_openlib(L, libname, l, 0, LUA_USECCLOSURES);
  245. }
  246. LUALIB_API void (luaL_register_light)(lua_State *L, const char *libname,
  247. const luaL_Reg *l)
  248. {
  249. #if LUA_OPTIMIZE_MEMORY > 0
  250. luaI_openlib(L, libname, l, 0, LUA_USELIGHTFUNCTIONS);
  251. #else
  252. luaI_openlib(L, libname, l, 0, LUA_USECCLOSURES);
  253. #endif
  254. }
  255. static int libsize(const luaL_Reg *l)
  256. {
  257. int size = 0;
  258. for (; l->name; l++) size++;
  259. return size;
  260. }
  261. LUALIB_API void luaI_openlib(lua_State *L, const char *libname,
  262. const luaL_Reg *l, int nup, int ftype)
  263. {
  264. if (libname)
  265. {
  266. int size = libsize(l);
  267. /* check whether lib already exists */
  268. luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1);
  269. lua_getfield(L, -1, libname); /* get _LOADED[libname] */
  270. if (!lua_istable(L, -1)) /* not found? */
  271. {
  272. lua_pop(L, 1); /* remove previous result */
  273. /* try global variable (and create one if it does not exist) */
  274. if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL)
  275. luaL_error(L, "name conflict for module " LUA_QS, libname);
  276. lua_pushvalue(L, -1);
  277. lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */
  278. }
  279. lua_remove(L, -2); /* remove _LOADED table */
  280. lua_insert(L, -(nup + 1)); /* move library table to below upvalues */
  281. }
  282. for (; l->name; l++)
  283. {
  284. int i;
  285. for (i = 0; i < nup; i++) /* copy upvalues to the top */
  286. lua_pushvalue(L, -nup);
  287. if (ftype == LUA_USELIGHTFUNCTIONS)
  288. lua_pushlightfunction(L, l->func);
  289. else
  290. lua_pushcclosure(L, l->func, nup);
  291. lua_setfield(L, -(nup + 2), l->name);
  292. }
  293. lua_pop(L, nup); /* remove upvalues */
  294. }
  295. /*
  296. ** {======================================================
  297. ** getn-setn: size for arrays
  298. ** =======================================================
  299. */
  300. #if defined(LUA_COMPAT_GETN)
  301. static int checkint(lua_State *L, int topop)
  302. {
  303. int n = (lua_type(L, -1) == LUA_TNUMBER) ? lua_tointeger(L, -1) : -1;
  304. lua_pop(L, topop);
  305. return n;
  306. }
  307. static void getsizes(lua_State *L)
  308. {
  309. lua_getfield(L, LUA_REGISTRYINDEX, "LUA_SIZES");
  310. if (lua_isnil(L, -1)) /* no `size' table? */
  311. {
  312. lua_pop(L, 1); /* remove nil */
  313. lua_newtable(L); /* create it */
  314. lua_pushvalue(L, -1); /* `size' will be its own metatable */
  315. lua_setmetatable(L, -2);
  316. lua_pushliteral(L, "kv");
  317. lua_setfield(L, -2, "__mode"); /* metatable(N).__mode = "kv" */
  318. lua_pushvalue(L, -1);
  319. lua_setfield(L, LUA_REGISTRYINDEX, "LUA_SIZES"); /* store in register */
  320. }
  321. }
  322. LUALIB_API void luaL_setn(lua_State *L, int t, int n)
  323. {
  324. t = abs_index(L, t);
  325. lua_pushliteral(L, "n");
  326. lua_rawget(L, t);
  327. if (checkint(L, 1) >= 0) /* is there a numeric field `n'? */
  328. {
  329. lua_pushliteral(L, "n"); /* use it */
  330. lua_pushinteger(L, n);
  331. lua_rawset(L, t);
  332. }
  333. else /* use `sizes' */
  334. {
  335. getsizes(L);
  336. lua_pushvalue(L, t);
  337. lua_pushinteger(L, n);
  338. lua_rawset(L, -3); /* sizes[t] = n */
  339. lua_pop(L, 1); /* remove `sizes' */
  340. }
  341. }
  342. LUALIB_API int luaL_getn(lua_State *L, int t)
  343. {
  344. int n;
  345. t = abs_index(L, t);
  346. lua_pushliteral(L, "n"); /* try t.n */
  347. lua_rawget(L, t);
  348. if ((n = checkint(L, 1)) >= 0) return n;
  349. getsizes(L); /* else try sizes[t] */
  350. lua_pushvalue(L, t);
  351. lua_rawget(L, -2);
  352. if ((n = checkint(L, 2)) >= 0) return n;
  353. return (int)lua_objlen(L, t);
  354. }
  355. #endif
  356. /* }====================================================== */
  357. LUALIB_API const char *luaL_gsub(lua_State *L, const char *s, const char *p,
  358. const char *r)
  359. {
  360. const char *wild;
  361. size_t l = strlen(p);
  362. luaL_Buffer b;
  363. luaL_buffinit(L, &b);
  364. while ((wild = strstr(s, p)) != NULL)
  365. {
  366. luaL_addlstring(&b, s, wild - s); /* push prefix */
  367. luaL_addstring(&b, r); /* push replacement in place of pattern */
  368. s = wild + l; /* continue after `p' */
  369. }
  370. luaL_addstring(&b, s); /* push last suffix */
  371. luaL_pushresult(&b);
  372. return lua_tostring(L, -1);
  373. }
  374. LUALIB_API const char *luaL_findtable(lua_State *L, int idx,
  375. const char *fname, int szhint)
  376. {
  377. const char *e;
  378. lua_pushvalue(L, idx);
  379. do
  380. {
  381. e = strchr(fname, '.');
  382. if (e == NULL) e = fname + strlen(fname);
  383. lua_pushlstring(L, fname, e - fname);
  384. lua_rawget(L, -2);
  385. if (lua_isnil(L, -1))
  386. {
  387. /* If looking for a global variable, check the rotables too */
  388. void *ptable = luaR_findglobal(fname, e - fname);
  389. if (ptable)
  390. {
  391. lua_pop(L, 1);
  392. lua_pushrotable(L, ptable);
  393. }
  394. }
  395. if (lua_isnil(L, -1)) /* no such field? */
  396. {
  397. lua_pop(L, 1); /* remove this nil */
  398. lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */
  399. lua_pushlstring(L, fname, e - fname);
  400. lua_pushvalue(L, -2);
  401. lua_settable(L, -4); /* set new table into field */
  402. }
  403. else if (!lua_istable(L, -1) && !lua_isrotable(L, -1)) /* field has a non-table value? */
  404. {
  405. lua_pop(L, 2); /* remove table and value */
  406. return fname; /* return problematic part of the name */
  407. }
  408. lua_remove(L, -2); /* remove previous table */
  409. fname = e + 1;
  410. }
  411. while (*e == '.');
  412. return NULL;
  413. }
  414. /*
  415. ** {======================================================
  416. ** Generic Buffer manipulation
  417. ** =======================================================
  418. */
  419. #define bufflen(B) ((B)->p - (B)->buffer)
  420. #define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B)))
  421. #define LIMIT (LUA_MINSTACK/2)
  422. static int emptybuffer(luaL_Buffer *B)
  423. {
  424. size_t l = bufflen(B);
  425. if (l == 0) return 0; /* put nothing on stack */
  426. else
  427. {
  428. lua_pushlstring(B->L, B->buffer, l);
  429. B->p = B->buffer;
  430. B->lvl++;
  431. return 1;
  432. }
  433. }
  434. static void adjuststack(luaL_Buffer *B)
  435. {
  436. if (B->lvl > 1)
  437. {
  438. lua_State *L = B->L;
  439. int toget = 1; /* number of levels to concat */
  440. size_t toplen = lua_strlen(L, -1);
  441. do
  442. {
  443. size_t l = lua_strlen(L, -(toget + 1));
  444. if (B->lvl - toget + 1 >= LIMIT || toplen > l)
  445. {
  446. toplen += l;
  447. toget++;
  448. }
  449. else break;
  450. }
  451. while (toget < B->lvl);
  452. lua_concat(L, toget);
  453. B->lvl = B->lvl - toget + 1;
  454. }
  455. }
  456. LUALIB_API char *luaL_prepbuffer(luaL_Buffer *B)
  457. {
  458. if (emptybuffer(B))
  459. adjuststack(B);
  460. return B->buffer;
  461. }
  462. LUALIB_API void luaL_addlstring(luaL_Buffer *B, const char *s, size_t l)
  463. {
  464. while (l--)
  465. luaL_addchar(B, *s++);
  466. }
  467. LUALIB_API void luaL_addstring(luaL_Buffer *B, const char *s)
  468. {
  469. luaL_addlstring(B, s, strlen(s));
  470. }
  471. LUALIB_API void luaL_pushresult(luaL_Buffer *B)
  472. {
  473. emptybuffer(B);
  474. lua_concat(B->L, B->lvl);
  475. B->lvl = 1;
  476. }
  477. LUALIB_API void luaL_addvalue(luaL_Buffer *B)
  478. {
  479. lua_State *L = B->L;
  480. size_t vl;
  481. const char *s = lua_tolstring(L, -1, &vl);
  482. if (vl <= bufffree(B)) /* fit into buffer? */
  483. {
  484. memcpy(B->p, s, vl); /* put it there */
  485. B->p += vl;
  486. lua_pop(L, 1); /* remove from stack */
  487. }
  488. else
  489. {
  490. if (emptybuffer(B))
  491. lua_insert(L, -2); /* put buffer before new value */
  492. B->lvl++; /* add new value into B stack */
  493. adjuststack(B);
  494. }
  495. }
  496. LUALIB_API void luaL_buffinit(lua_State *L, luaL_Buffer *B)
  497. {
  498. B->L = L;
  499. B->p = B->buffer;
  500. B->lvl = 0;
  501. }
  502. /* }====================================================== */
  503. LUALIB_API int luaL_ref(lua_State *L, int t)
  504. {
  505. int ref;
  506. t = abs_index(L, t);
  507. if (lua_isnil(L, -1))
  508. {
  509. lua_pop(L, 1); /* remove from stack */
  510. return LUA_REFNIL; /* `nil' has a unique fixed reference */
  511. }
  512. lua_rawgeti(L, t, FREELIST_REF); /* get first free element */
  513. ref = (int)lua_tointeger(L, -1); /* ref = t[FREELIST_REF] */
  514. lua_pop(L, 1); /* remove it from stack */
  515. if (ref != 0) /* any free element? */
  516. {
  517. lua_rawgeti(L, t, ref); /* remove it from list */
  518. lua_rawseti(L, t, FREELIST_REF); /* (t[FREELIST_REF] = t[ref]) */
  519. }
  520. else /* no free elements */
  521. {
  522. ref = (int)lua_objlen(L, t);
  523. ref++; /* create new reference */
  524. }
  525. lua_rawseti(L, t, ref);
  526. return ref;
  527. }
  528. LUALIB_API void luaL_unref(lua_State *L, int t, int ref)
  529. {
  530. if (ref >= 0)
  531. {
  532. t = abs_index(L, t);
  533. lua_rawgeti(L, t, FREELIST_REF);
  534. lua_rawseti(L, t, ref); /* t[ref] = t[FREELIST_REF] */
  535. lua_pushinteger(L, ref);
  536. lua_rawseti(L, t, FREELIST_REF); /* t[FREELIST_REF] = ref */
  537. }
  538. }
  539. /*
  540. ** {======================================================
  541. ** Load functions
  542. ** =======================================================
  543. */
  544. typedef struct LoadF
  545. {
  546. int extraline;
  547. FILE *f;
  548. char buff[LUAL_BUFFERSIZE];
  549. } LoadF;
  550. static const char *getF(lua_State *L, void *ud, size_t *size)
  551. {
  552. LoadF *lf = (LoadF *)ud;
  553. (void)L;
  554. if (lf->extraline)
  555. {
  556. lf->extraline = 0;
  557. *size = 1;
  558. return "\n";
  559. }
  560. if (feof(lf->f)) return NULL;
  561. *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f);
  562. return (*size > 0) ? lf->buff : NULL;
  563. }
  564. static int errfile(lua_State *L, const char *what, int fnameindex)
  565. {
  566. const char *serr = strerror(errno);
  567. const char *filename = lua_tostring(L, fnameindex) + 1;
  568. lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
  569. lua_remove(L, fnameindex);
  570. return LUA_ERRFILE;
  571. }
  572. LUALIB_API int luaL_loadfile(lua_State *L, const char *filename)
  573. {
  574. LoadF lf;
  575. int status, readstatus;
  576. int c;
  577. int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
  578. lf.extraline = 0;
  579. if (filename == NULL)
  580. {
  581. lua_pushliteral(L, "=stdin");
  582. lf.f = stdin;
  583. }
  584. else
  585. {
  586. lua_pushfstring(L, "@%s", filename);
  587. lf.f = fopen(filename, "r");
  588. if (lf.f == NULL) return errfile(L, "open", fnameindex);
  589. }
  590. c = getc(lf.f);
  591. if (c == '#') /* Unix exec. file? */
  592. {
  593. lf.extraline = 1;
  594. while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */
  595. if (c == '\n') c = getc(lf.f);
  596. }
  597. if (c == LUA_SIGNATURE[0] && filename) /* binary file? */
  598. {
  599. lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
  600. if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
  601. /* skip eventual `#!...' */
  602. while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ;
  603. lf.extraline = 0;
  604. }
  605. ungetc(c, lf.f);
  606. status = lua_load(L, getF, &lf, lua_tostring(L, -1));
  607. readstatus = ferror(lf.f);
  608. if (filename) fclose(lf.f); /* close file (even in case of errors) */
  609. if (readstatus)
  610. {
  611. lua_settop(L, fnameindex); /* ignore results from `lua_load' */
  612. return errfile(L, "read", fnameindex);
  613. }
  614. lua_remove(L, fnameindex);
  615. return status;
  616. }
  617. typedef struct LoadS
  618. {
  619. const char *s;
  620. size_t size;
  621. } LoadS;
  622. static const char *getS(lua_State *L, void *ud, size_t *size)
  623. {
  624. LoadS *ls = (LoadS *)ud;
  625. (void)L;
  626. if (L == NULL && size == NULL) // direct mode check
  627. return NULL;
  628. if (ls->size == 0) return NULL;
  629. *size = ls->size;
  630. ls->size = 0;
  631. return ls->s;
  632. }
  633. LUALIB_API int luaL_loadbuffer(lua_State *L, const char *buff, size_t size,
  634. const char *name)
  635. {
  636. LoadS ls;
  637. ls.s = buff;
  638. ls.size = size;
  639. return lua_load(L, getS, &ls, name);
  640. }
  641. LUALIB_API int (luaL_loadstring)(lua_State *L, const char *s)
  642. {
  643. return luaL_loadbuffer(L, s, strlen(s), s);
  644. }
  645. /* }====================================================== */
  646. static int l_check_memlimit(lua_State *L, size_t needbytes)
  647. {
  648. global_State *g = G(L);
  649. int cycle_count = 0;
  650. lu_mem limit = g->memlimit - needbytes;
  651. /* make sure the GC is not disabled. */
  652. if (!is_block_gc(L))
  653. {
  654. while (g->totalbytes >= limit)
  655. {
  656. /* only allow the GC to finished atleast 1 full cycle. */
  657. if (g->gcstate == GCSpause && ++cycle_count > 1) break;
  658. luaC_step(L);
  659. }
  660. }
  661. return (g->totalbytes >= limit) ? 1 : 0;
  662. }
  663. static void *l_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
  664. {
  665. lua_State *L = (lua_State *)ud;
  666. int mode = L == NULL ? 0 : G(L)->egcmode;
  667. void *nptr;
  668. if (nsize == 0)
  669. {
  670. free(ptr);
  671. return NULL;
  672. }
  673. if (L != NULL && (mode & EGC_ALWAYS)) /* always collect memory if requested */
  674. luaC_fullgc(L);
  675. if (nsize > osize && L != NULL)
  676. {
  677. #if defined(LUA_STRESS_EMERGENCY_GC)
  678. luaC_fullgc(L);
  679. #endif
  680. if (G(L)->memlimit > 0 && (mode & EGC_ON_MEM_LIMIT) && l_check_memlimit(L, nsize - osize))
  681. return NULL;
  682. }
  683. nptr = realloc(ptr, nsize);
  684. if (nptr == NULL && L != NULL && (mode & EGC_ON_ALLOC_FAILURE))
  685. {
  686. luaC_fullgc(L); /* emergency full collection. */
  687. nptr = realloc(ptr, nsize); /* try allocation again */
  688. }
  689. return nptr;
  690. }
  691. static int panic(lua_State *L)
  692. {
  693. (void)L; /* to avoid warnings */
  694. #if defined(LUA_USE_STDIO)
  695. fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n",
  696. lua_tostring(L, -1));
  697. #else
  698. luai_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n",
  699. lua_tostring(L, -1));
  700. #endif
  701. return 0;
  702. }
  703. LUALIB_API lua_State *luaL_newstate(void)
  704. {
  705. lua_State *L = lua_newstate(l_alloc, NULL);
  706. lua_setallocf(L, l_alloc, L); /* allocator need lua_State. */
  707. if (L) lua_atpanic(L, &panic);
  708. return L;
  709. }