lbaselib.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. ** $Id: lbaselib.c,v 1.314.1.1 2017/04/19 17:39:34 roberto Exp $
  3. ** Basic library
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lbaselib_c
  7. #define LUA_LIB
  8. #include "lprefix.h"
  9. #include <ctype.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "lua.h"
  14. #include "lauxlib.h"
  15. #include "lualib.h"
  16. #include "lnodemcu.h"
  17. static int luaB_print (lua_State *L) {
  18. int n = lua_gettop(L); /* number of arguments */
  19. int i;
  20. lua_getglobal(L, "tostring");
  21. for (i=1; i<=n; i++) {
  22. const char *s;
  23. size_t l;
  24. lua_pushvalue(L, -1); /* function to be called */
  25. lua_pushvalue(L, i); /* value to print */
  26. lua_call(L, 1, 1);
  27. s = lua_tolstring(L, -1, &l); /* get result */
  28. if (s == NULL)
  29. return luaL_error(L, "'tostring' must return a string to 'print'");
  30. if (i>1) lua_writestring("\t", 1);
  31. lua_writestring(s, l);
  32. lua_pop(L, 1); /* pop result */
  33. }
  34. lua_writeline();
  35. return 0;
  36. }
  37. #define SPACECHARS " \f\n\r\t\v"
  38. static const char *b_str2int (const char *s, int base, lua_Integer *pn) {
  39. lua_Unsigned n = 0;
  40. int neg = 0;
  41. s += strspn(s, SPACECHARS); /* skip initial spaces */
  42. if (*s == '-') { s++; neg = 1; } /* handle signal */
  43. else if (*s == '+') s++;
  44. if (!isalnum((unsigned char)*s)) /* no digit? */
  45. return NULL;
  46. do {
  47. int digit = (isdigit((unsigned char)*s)) ? *s - '0'
  48. : (toupper((unsigned char)*s) - 'A') + 10;
  49. if (digit >= base) return NULL; /* invalid numeral */
  50. n = n * base + digit;
  51. s++;
  52. } while (isalnum((unsigned char)*s));
  53. s += strspn(s, SPACECHARS); /* skip trailing spaces */
  54. *pn = (lua_Integer)((neg) ? (0u - n) : n);
  55. return s;
  56. }
  57. static int luaB_tonumber (lua_State *L) {
  58. if (lua_isnoneornil(L, 2)) { /* standard conversion? */
  59. luaL_checkany(L, 1);
  60. if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */
  61. lua_settop(L, 1); /* yes; return it */
  62. return 1;
  63. }
  64. else {
  65. size_t l;
  66. const char *s = lua_tolstring(L, 1, &l);
  67. if (s != NULL && lua_stringtonumber(L, s) == l + 1)
  68. return 1; /* successful conversion to number */
  69. /* else not a number */
  70. }
  71. }
  72. else {
  73. size_t l;
  74. const char *s;
  75. lua_Integer n = 0; /* to avoid warnings */
  76. lua_Integer base = luaL_checkinteger(L, 2);
  77. luaL_checktype(L, 1, LUA_TSTRING); /* no numbers as strings */
  78. s = lua_tolstring(L, 1, &l);
  79. luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
  80. if (b_str2int(s, (int)base, &n) == s + l) {
  81. lua_pushinteger(L, n);
  82. return 1;
  83. } /* else not a number */
  84. } /* else not a number */
  85. lua_pushnil(L); /* not a number */
  86. return 1;
  87. }
  88. static int luaB_error (lua_State *L) {
  89. int level = (int)luaL_optinteger(L, 2, 1);
  90. lua_settop(L, 1);
  91. if (lua_type(L, 1) == LUA_TSTRING && level > 0) {
  92. luaL_where(L, level); /* add extra information */
  93. lua_pushvalue(L, 1);
  94. lua_concat(L, 2);
  95. }
  96. return lua_error(L);
  97. }
  98. static int luaB_getmetatable (lua_State *L) {
  99. luaL_checkany(L, 1);
  100. if (!lua_getmetatable(L, 1)) {
  101. lua_pushnil(L);
  102. return 1; /* no metatable */
  103. }
  104. luaL_getmetafield(L, 1, "__metatable");
  105. return 1; /* returns either __metatable field (if present) or metatable */
  106. }
  107. static int luaB_setmetatable (lua_State *L) {
  108. int t = lua_type(L, 2);
  109. luaL_checktype(L, 1, LUA_TTABLE);
  110. luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
  111. "nil or table expected");
  112. if (luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL)
  113. return luaL_error(L, "cannot change a protected metatable");
  114. lua_settop(L, 2);
  115. lua_setmetatable(L, 1);
  116. return 1;
  117. }
  118. static int luaB_rawequal (lua_State *L) {
  119. luaL_checkany(L, 1);
  120. luaL_checkany(L, 2);
  121. lua_pushboolean(L, lua_rawequal(L, 1, 2));
  122. return 1;
  123. }
  124. static int luaB_rawlen (lua_State *L) {
  125. int t = lua_type(L, 1);
  126. luaL_argcheck(L, t == LUA_TTABLE || t == LUA_TSTRING, 1,
  127. "table or string expected");
  128. lua_pushinteger(L, lua_rawlen(L, 1));
  129. return 1;
  130. }
  131. static int luaB_rawget (lua_State *L) {
  132. luaL_checktype(L, 1, LUA_TTABLE);
  133. luaL_checkany(L, 2);
  134. lua_settop(L, 2);
  135. lua_rawget(L, 1);
  136. return 1;
  137. }
  138. static int luaB_rawset (lua_State *L) {
  139. luaL_checktype(L, 1, LUA_TTABLE);
  140. luaL_checkany(L, 2);
  141. luaL_checkany(L, 3);
  142. lua_settop(L, 3);
  143. lua_rawset(L, 1);
  144. return 1;
  145. }
  146. static int luaB_collectgarbage (lua_State *L) {
  147. static const char *const opts[] = {"stop", "restart", "collect",
  148. "count", "step", "setpause", "setstepmul", "setmemlimit",
  149. "isrunning", NULL};
  150. static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
  151. LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL,
  152. LUA_GCISRUNNING};
  153. int o = optsnum[luaL_checkoption(L, 1, "collect", opts)];
  154. int ex = (int)luaL_optinteger(L, 2, 0);
  155. int res = lua_gc(L, o, ex);
  156. switch (o) {
  157. case LUA_GCCOUNT: {
  158. int b = lua_gc(L, LUA_GCCOUNTB, 0);
  159. lua_pushnumber(L, (lua_Number)res + ((lua_Number)b/1024));
  160. return 1;
  161. }
  162. case LUA_GCSTEP: case LUA_GCISRUNNING: {
  163. lua_pushboolean(L, res);
  164. return 1;
  165. }
  166. default: {
  167. lua_pushinteger(L, res);
  168. return 1;
  169. }
  170. }
  171. }
  172. static int luaB_type (lua_State *L) {
  173. int t = lua_type(L, 1);
  174. luaL_argcheck(L, t != LUA_TNONE, 1, "value expected");
  175. lua_pushstring(L, lua_typename(L, t));
  176. return 1;
  177. }
  178. static int pairsmeta (lua_State *L, const char *method, int iszero,
  179. lua_CFunction iter) {
  180. luaL_checkany(L, 1);
  181. if (luaL_getmetafield(L, 1, method) == LUA_TNIL) { /* no metamethod? */
  182. lua_pushcfunction(L, iter); /* will return generator, */
  183. lua_pushvalue(L, 1); /* state, */
  184. if (iszero) lua_pushinteger(L, 0); /* and initial value */
  185. else lua_pushnil(L);
  186. }
  187. else {
  188. lua_pushvalue(L, 1); /* argument 'self' to metamethod */
  189. lua_call(L, 1, 3); /* get 3 values from metamethod */
  190. }
  191. return 3;
  192. }
  193. static int luaB_next (lua_State *L) {
  194. luaL_checktype(L, 1, LUA_TTABLE);
  195. lua_settop(L, 2); /* create a 2nd argument if there isn't one */
  196. if (lua_next(L, 1))
  197. return 2;
  198. else {
  199. lua_pushnil(L);
  200. return 1;
  201. }
  202. }
  203. static int luaB_pairs (lua_State *L) {
  204. return pairsmeta(L, "__pairs", 0, luaB_next);
  205. }
  206. /*
  207. ** Traversal function for 'ipairs'
  208. */
  209. static int ipairsaux (lua_State *L) {
  210. lua_Integer i = luaL_checkinteger(L, 2) + 1;
  211. lua_pushinteger(L, i);
  212. return (lua_geti(L, 1, i) == LUA_TNIL) ? 1 : 2;
  213. }
  214. /*
  215. ** 'ipairs' function. Returns 'ipairsaux', given "table", 0.
  216. ** (The given "table" may not be a table.)
  217. */
  218. static int luaB_ipairs (lua_State *L) {
  219. #if defined(LUA_COMPAT_IPAIRS)
  220. return pairsmeta(L, "__ipairs", 1, ipairsaux);
  221. #else
  222. luaL_checkany(L, 1);
  223. lua_pushcfunction(L, ipairsaux); /* iteration function */
  224. lua_pushvalue(L, 1); /* state */
  225. lua_pushinteger(L, 0); /* initial value */
  226. return 3;
  227. #endif
  228. }
  229. static int load_aux (lua_State *L, int status, int envidx) {
  230. if (status == LUA_OK) {
  231. if (envidx != 0) { /* 'env' parameter? */
  232. lua_pushvalue(L, envidx); /* environment for loaded function */
  233. if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */
  234. lua_pop(L, 1); /* remove 'env' if not used by previous call */
  235. }
  236. return 1;
  237. }
  238. else { /* error (message is on top of the stack) */
  239. lua_pushnil(L);
  240. lua_insert(L, -2); /* put before error message */
  241. return 2; /* return nil plus error message */
  242. }
  243. }
  244. static int luaB_loadfile (lua_State *L) {
  245. const char *fname = luaL_optstring(L, 1, NULL);
  246. const char *mode = luaL_optstring(L, 2, NULL);
  247. int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */
  248. int status = luaL_loadfilex(L, fname, mode);
  249. return load_aux(L, status, env);
  250. }
  251. /*
  252. ** {======================================================
  253. ** Generic Read function
  254. ** =======================================================
  255. */
  256. /*
  257. ** reserved slot, above all arguments, to hold a copy of the returned
  258. ** string to avoid it being collected while parsed. 'load' has four
  259. ** optional arguments (chunk, source name, mode, and environment).
  260. */
  261. #define RESERVEDSLOT 5
  262. /*
  263. ** Reader for generic 'load' function: 'lua_load' uses the
  264. ** stack for internal stuff, so the reader cannot change the
  265. ** stack top. Instead, it keeps its resulting string in a
  266. ** reserved slot inside the stack.
  267. */
  268. static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
  269. (void)(ud); /* not used */
  270. luaL_checkstack(L, 2, "too many nested functions");
  271. lua_pushvalue(L, 1); /* get function */
  272. lua_call(L, 0, 1); /* call it */
  273. if (lua_isnil(L, -1)) {
  274. lua_pop(L, 1); /* pop result */
  275. *size = 0;
  276. return NULL;
  277. }
  278. else if (!lua_isstring(L, -1))
  279. luaL_error(L, "reader function must return a string");
  280. lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */
  281. return lua_tolstring(L, RESERVEDSLOT, size);
  282. }
  283. static int luaB_load (lua_State *L) {
  284. int status;
  285. size_t l;
  286. const char *s = lua_tolstring(L, 1, &l);
  287. const char *mode = luaL_optstring(L, 3, "bt");
  288. int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */
  289. if (s != NULL) { /* loading a string? */
  290. const char *chunkname = luaL_optstring(L, 2, s);
  291. status = luaL_loadbufferx(L, s, l, chunkname, mode);
  292. }
  293. else { /* loading from a reader function */
  294. const char *chunkname = luaL_optstring(L, 2, "=(load)");
  295. luaL_checktype(L, 1, LUA_TFUNCTION);
  296. lua_settop(L, RESERVEDSLOT); /* create reserved slot */
  297. status = lua_load(L, generic_reader, NULL, chunkname, mode);
  298. }
  299. return load_aux(L, status, env);
  300. }
  301. /* }====================================================== */
  302. static int dofilecont (lua_State *L, int d1, lua_KContext d2) {
  303. (void)d1; (void)d2; /* only to match 'lua_Kfunction' prototype */
  304. return lua_gettop(L) - 1;
  305. }
  306. static int luaB_dofile (lua_State *L) {
  307. const char *fname = luaL_optstring(L, 1, NULL);
  308. lua_settop(L, 1);
  309. if (luaL_loadfile(L, fname) != LUA_OK)
  310. return lua_error(L);
  311. lua_callk(L, 0, LUA_MULTRET, 0, dofilecont);
  312. return dofilecont(L, 0, 0);
  313. }
  314. static int luaB_assert (lua_State *L) {
  315. if (lua_toboolean(L, 1)) /* condition is true? */
  316. return lua_gettop(L); /* return all arguments */
  317. else { /* error */
  318. luaL_checkany(L, 1); /* there must be a condition */
  319. lua_remove(L, 1); /* remove it */
  320. lua_pushliteral(L, "assertion failed!"); /* default message */
  321. lua_settop(L, 1); /* leave only message (default if no other one) */
  322. return luaB_error(L); /* call 'error' */
  323. }
  324. }
  325. static int luaB_select (lua_State *L) {
  326. int n = lua_gettop(L);
  327. if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') {
  328. lua_pushinteger(L, n-1);
  329. return 1;
  330. }
  331. else {
  332. lua_Integer i = luaL_checkinteger(L, 1);
  333. if (i < 0) i = n + i;
  334. else if (i > n) i = n;
  335. luaL_argcheck(L, 1 <= i, 1, "index out of range");
  336. return n - (int)i;
  337. }
  338. }
  339. /*
  340. ** Continuation function for 'pcall' and 'xpcall'. Both functions
  341. ** already pushed a 'true' before doing the call, so in case of success
  342. ** 'finishpcall' only has to return everything in the stack minus
  343. ** 'extra' values (where 'extra' is exactly the number of items to be
  344. ** ignored).
  345. */
  346. static int finishpcall (lua_State *L, int status, lua_KContext extra) {
  347. if (status != LUA_OK && status != LUA_YIELD) { /* error? */
  348. lua_pushboolean(L, 0); /* first result (false) */
  349. lua_pushvalue(L, -2); /* error message */
  350. return 2; /* return false, msg */
  351. }
  352. else
  353. return lua_gettop(L) - (int)extra; /* return all results */
  354. }
  355. static int luaB_pcall (lua_State *L) {
  356. int status;
  357. luaL_checkany(L, 1);
  358. lua_pushboolean(L, 1); /* first result if no errors */
  359. lua_insert(L, 1); /* put it in place */
  360. status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, finishpcall);
  361. return finishpcall(L, status, 0);
  362. }
  363. /*
  364. ** Do a protected call with error handling. After 'lua_rotate', the
  365. ** stack will have <f, err, true, f, [args...]>; so, the function passes
  366. ** 2 to 'finishpcall' to skip the 2 first values when returning results.
  367. */
  368. static int luaB_xpcall (lua_State *L) {
  369. int status;
  370. int n = lua_gettop(L);
  371. luaL_checktype(L, 2, LUA_TFUNCTION); /* check error function */
  372. lua_pushboolean(L, 1); /* first result */
  373. lua_pushvalue(L, 1); /* function */
  374. lua_rotate(L, 3, 2); /* move them below function's arguments */
  375. status = lua_pcallk(L, n - 2, LUA_MULTRET, 2, 2, finishpcall);
  376. return finishpcall(L, status, 2);
  377. }
  378. static int luaB_tostring (lua_State *L) {
  379. luaL_checkany(L, 1);
  380. luaL_tolstring(L, 1, NULL);
  381. return 1;
  382. }
  383. #if defined(LUA_COMPAT_UNPACK)
  384. static int luaB_unpack (lua_State *L) {
  385. int n = lua_gettop(L); /* number of elements to pack */
  386. lua_getglobal(L, "table");
  387. lua_getfield(L, -1, "unpack");
  388. lua_insert(L, 1);
  389. lua_pop(L, 1);
  390. lua_call(L, n, LUA_MULTRET);
  391. return lua_gettop(L);
  392. }
  393. #endif
  394. /*
  395. ** ESP builds use specific linker directives to marshal all the ROTable entries
  396. ** for the library modules including the base library into an entry vector in
  397. ** the PSECT ".lua_rotable" including the base library entries; this is bound
  398. ** into a ROTable in linit.c which then hooked into the __index metaentry for
  399. ** _G so that base library and ROM tables are directly resolved through _G.
  400. **
  401. ** The host-based luac.cross builds which must use a standard GNU link or
  402. ** MSVC so this linker-specfic assembly approach can't be used. In this case
  403. ** luaopen_base returns a base_func ROTable so a two cascade resolution. See
  404. ** description in init.c for further details.
  405. */
  406. #ifdef LUA_CROSS_COMPILER
  407. LROT_BEGIN(base_func, NULL, 0)
  408. #else
  409. LROT_ENTRIES_IN_SECTION(base_func, rotable)
  410. #endif
  411. LROT_FUNCENTRY(assert, luaB_assert)
  412. LROT_FUNCENTRY(collectgarbage, luaB_collectgarbage)
  413. LROT_FUNCENTRY(dofile, luaB_dofile)
  414. LROT_FUNCENTRY(error, luaB_error)
  415. LROT_FUNCENTRY(getmetatable, luaB_getmetatable)
  416. LROT_FUNCENTRY(ipairs, luaB_ipairs)
  417. LROT_FUNCENTRY(loadfile, luaB_loadfile)
  418. LROT_FUNCENTRY(load, luaB_load)
  419. #if defined(LUA_COMPAT_LOADSTRING)
  420. LROT_FUNCENTRY(loadstring, luaB_load)
  421. #endif
  422. LROT_FUNCENTRY(next, luaB_next)
  423. LROT_FUNCENTRY(pairs, luaB_pairs)
  424. LROT_FUNCENTRY(pcall, luaB_pcall)
  425. LROT_FUNCENTRY(print, luaB_print)
  426. LROT_FUNCENTRY(rawequal, luaB_rawequal)
  427. LROT_FUNCENTRY(rawlen, luaB_rawlen)
  428. LROT_FUNCENTRY(rawget, luaB_rawget)
  429. LROT_FUNCENTRY(rawset, luaB_rawset)
  430. LROT_FUNCENTRY(select, luaB_select)
  431. LROT_FUNCENTRY(setmetatable, luaB_setmetatable)
  432. LROT_FUNCENTRY(tonumber, luaB_tonumber)
  433. LROT_FUNCENTRY(tostring, luaB_tostring)
  434. LROT_FUNCENTRY(type, luaB_type)
  435. #if defined(LUA_COMPAT_UNPACK)
  436. LROT_FUNCENTRY(unpack, luaB_unpack)
  437. #endif
  438. LROT_FUNCENTRY(xpcall, luaB_xpcall)
  439. #ifdef LUA_CROSS_COMPILER
  440. LROT_END(base_func, NULL, 0)
  441. #else
  442. LROT_BREAK(base_func)
  443. #endif
  444. extern LROT_TABLE(rotables);
  445. LUAMOD_API int luaopen_base (lua_State *L) {
  446. lua_pushglobaltable(L); /* set global _G */
  447. lua_pushliteral(L, LUA_VERSION); /* set global _VERSION */
  448. lua_setfield(L, -2, "_VERSION");
  449. lua_createtable (L, 0, 1); /* mt for _G */
  450. lua_pushrotable(L, LROT_TABLEREF(rotables));
  451. lua_setfield(L, -2, "__index"); /* mt.__index=ROM table */
  452. lua_setmetatable(L, -2);
  453. return 1; /* returns _G */
  454. }