lauxlib.c 23 KB

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