PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/ports/dingux/tags/v3-20101128/Source_Files/Lua/lbaselib.c

#
C | 646 lines | 520 code | 98 blank | 28 comment | 92 complexity | f50e9ee3b5941bc5350ceb61f386af3e MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1, BSD-3-Clause, GPL-3.0, LGPL-3.0, MPL-2.0-no-copyleft-exception, Zlib, GPL-2.0
  1. /*
  2. ** $Id: lbaselib.c 4370 2011-03-23 16:02:29Z jeremiahmorris $
  3. ** Basic library
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include "config.h"
  7. #ifdef HAVE_LUA
  8. #include <ctype.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #define lbaselib_c
  13. #define LUA_LIB
  14. #include "lua.h"
  15. #include "lauxlib.h"
  16. #include "lualib.h"
  17. /*
  18. ** If your system does not support `stdout', you can just remove this function.
  19. ** If you need, you can define your own `print' function, following this
  20. ** model but changing `fputs' to put the strings at a proper place
  21. ** (a console window or a log file, for instance).
  22. */
  23. static int luaB_print (lua_State *L) {
  24. int n = lua_gettop(L); /* number of arguments */
  25. int i;
  26. lua_getglobal(L, "tostring");
  27. for (i=1; i<=n; i++) {
  28. const char *s;
  29. lua_pushvalue(L, -1); /* function to be called */
  30. lua_pushvalue(L, i); /* value to print */
  31. lua_call(L, 1, 1);
  32. s = lua_tostring(L, -1); /* get result */
  33. if (s == NULL)
  34. return luaL_error(L, LUA_QL("tostring") " must return a string to "
  35. LUA_QL("print"));
  36. if (i>1) fputs("\t", stdout);
  37. fputs(s, stdout);
  38. lua_pop(L, 1); /* pop result */
  39. }
  40. fputs("\n", stdout);
  41. return 0;
  42. }
  43. static int luaB_tonumber (lua_State *L) {
  44. int base = luaL_optint(L, 2, 10);
  45. if (base == 10) { /* standard conversion */
  46. luaL_checkany(L, 1);
  47. if (lua_isnumber(L, 1)) {
  48. lua_pushnumber(L, lua_tonumber(L, 1));
  49. return 1;
  50. }
  51. }
  52. else {
  53. const char *s1 = luaL_checkstring(L, 1);
  54. char *s2;
  55. unsigned long n;
  56. luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
  57. n = strtoul(s1, &s2, base);
  58. if (s1 != s2) { /* at least one valid digit? */
  59. while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
  60. if (*s2 == '\0') { /* no invalid trailing characters? */
  61. lua_pushnumber(L, (lua_Number)n);
  62. return 1;
  63. }
  64. }
  65. }
  66. lua_pushnil(L); /* else not a number */
  67. return 1;
  68. }
  69. static int luaB_error (lua_State *L) {
  70. int level = luaL_optint(L, 2, 1);
  71. lua_settop(L, 1);
  72. if (lua_isstring(L, 1) && level > 0) { /* add extra information? */
  73. luaL_where(L, level);
  74. lua_pushvalue(L, 1);
  75. lua_concat(L, 2);
  76. }
  77. return lua_error(L);
  78. }
  79. static int luaB_getmetatable (lua_State *L) {
  80. luaL_checkany(L, 1);
  81. if (!lua_getmetatable(L, 1)) {
  82. lua_pushnil(L);
  83. return 1; /* no metatable */
  84. }
  85. luaL_getmetafield(L, 1, "__metatable");
  86. return 1; /* returns either __metatable field (if present) or metatable */
  87. }
  88. static int luaB_setmetatable (lua_State *L) {
  89. int t = lua_type(L, 2);
  90. luaL_checktype(L, 1, LUA_TTABLE);
  91. luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
  92. "nil or table expected");
  93. if (luaL_getmetafield(L, 1, "__metatable"))
  94. luaL_error(L, "cannot change a protected metatable");
  95. lua_settop(L, 2);
  96. lua_setmetatable(L, 1);
  97. return 1;
  98. }
  99. static void getfunc (lua_State *L, int opt) {
  100. if (lua_isfunction(L, 1)) lua_pushvalue(L, 1);
  101. else {
  102. lua_Debug ar;
  103. int level = opt ? luaL_optint(L, 1, 1) : luaL_checkint(L, 1);
  104. luaL_argcheck(L, level >= 0, 1, "level must be non-negative");
  105. if (lua_getstack(L, level, &ar) == 0)
  106. luaL_argerror(L, 1, "invalid level");
  107. lua_getinfo(L, "f", &ar);
  108. if (lua_isnil(L, -1))
  109. luaL_error(L, "no function environment for tail call at level %d",
  110. level);
  111. }
  112. }
  113. static int luaB_getfenv (lua_State *L) {
  114. getfunc(L, 1);
  115. if (lua_iscfunction(L, -1)) /* is a C function? */
  116. lua_pushvalue(L, LUA_GLOBALSINDEX); /* return the thread's global env. */
  117. else
  118. lua_getfenv(L, -1);
  119. return 1;
  120. }
  121. static int luaB_setfenv (lua_State *L) {
  122. luaL_checktype(L, 2, LUA_TTABLE);
  123. getfunc(L, 0);
  124. lua_pushvalue(L, 2);
  125. if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) {
  126. /* change environment of current thread */
  127. lua_pushthread(L);
  128. lua_insert(L, -2);
  129. lua_setfenv(L, -2);
  130. return 0;
  131. }
  132. else if (lua_iscfunction(L, -2) || lua_setfenv(L, -2) == 0)
  133. luaL_error(L,
  134. LUA_QL("setfenv") " cannot change environment of given object");
  135. return 1;
  136. }
  137. static int luaB_rawequal (lua_State *L) {
  138. luaL_checkany(L, 1);
  139. luaL_checkany(L, 2);
  140. lua_pushboolean(L, lua_rawequal(L, 1, 2));
  141. return 1;
  142. }
  143. static int luaB_rawget (lua_State *L) {
  144. luaL_checktype(L, 1, LUA_TTABLE);
  145. luaL_checkany(L, 2);
  146. lua_settop(L, 2);
  147. lua_rawget(L, 1);
  148. return 1;
  149. }
  150. static int luaB_rawset (lua_State *L) {
  151. luaL_checktype(L, 1, LUA_TTABLE);
  152. luaL_checkany(L, 2);
  153. luaL_checkany(L, 3);
  154. lua_settop(L, 3);
  155. lua_rawset(L, 1);
  156. return 1;
  157. }
  158. static int luaB_gcinfo (lua_State *L) {
  159. lua_pushinteger(L, lua_getgccount(L));
  160. return 1;
  161. }
  162. static int luaB_collectgarbage (lua_State *L) {
  163. static const char *const opts[] = {"stop", "restart", "collect",
  164. "count", "step", "setpause", "setstepmul", NULL};
  165. static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
  166. LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL};
  167. int o = luaL_checkoption(L, 1, "collect", opts);
  168. int ex = luaL_optint(L, 2, 0);
  169. int res = lua_gc(L, optsnum[o], ex);
  170. switch (optsnum[o]) {
  171. case LUA_GCCOUNT: {
  172. int b = lua_gc(L, LUA_GCCOUNTB, 0);
  173. lua_pushnumber(L, res + ((lua_Number)b/1024));
  174. return 1;
  175. }
  176. case LUA_GCSTEP: {
  177. lua_pushboolean(L, res);
  178. return 1;
  179. }
  180. default: {
  181. lua_pushnumber(L, res);
  182. return 1;
  183. }
  184. }
  185. }
  186. static int luaB_type (lua_State *L) {
  187. luaL_checkany(L, 1);
  188. lua_pushstring(L, luaL_typename(L, 1));
  189. return 1;
  190. }
  191. static int luaB_next (lua_State *L) {
  192. luaL_checktype(L, 1, LUA_TTABLE);
  193. lua_settop(L, 2); /* create a 2nd argument if there isn't one */
  194. if (lua_next(L, 1))
  195. return 2;
  196. else {
  197. lua_pushnil(L);
  198. return 1;
  199. }
  200. }
  201. static int luaB_pairs (lua_State *L) {
  202. luaL_checktype(L, 1, LUA_TTABLE);
  203. lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
  204. lua_pushvalue(L, 1); /* state, */
  205. lua_pushnil(L); /* and initial value */
  206. return 3;
  207. }
  208. static int ipairsaux (lua_State *L) {
  209. int i = luaL_checkint(L, 2);
  210. luaL_checktype(L, 1, LUA_TTABLE);
  211. i++; /* next value */
  212. lua_pushinteger(L, i);
  213. lua_rawgeti(L, 1, i);
  214. return (lua_isnil(L, -1)) ? 0 : 2;
  215. }
  216. static int luaB_ipairs (lua_State *L) {
  217. luaL_checktype(L, 1, LUA_TTABLE);
  218. lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
  219. lua_pushvalue(L, 1); /* state, */
  220. lua_pushinteger(L, 0); /* and initial value */
  221. return 3;
  222. }
  223. static int load_aux (lua_State *L, int status) {
  224. if (status == 0) /* OK? */
  225. return 1;
  226. else {
  227. lua_pushnil(L);
  228. lua_insert(L, -2); /* put before error message */
  229. return 2; /* return nil plus error message */
  230. }
  231. }
  232. static int luaB_loadstring (lua_State *L) {
  233. size_t l;
  234. const char *s = luaL_checklstring(L, 1, &l);
  235. const char *chunkname = luaL_optstring(L, 2, s);
  236. return load_aux(L, luaL_loadbuffer(L, s, l, chunkname));
  237. }
  238. static int luaB_loadfile (lua_State *L) {
  239. const char *fname = luaL_optstring(L, 1, NULL);
  240. return load_aux(L, luaL_loadfile(L, fname));
  241. }
  242. /*
  243. ** Reader for generic `load' function: `lua_load' uses the
  244. ** stack for internal stuff, so the reader cannot change the
  245. ** stack top. Instead, it keeps its resulting string in a
  246. ** reserved slot inside the stack.
  247. */
  248. static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
  249. (void)ud; /* to avoid warnings */
  250. luaL_checkstack(L, 2, "too many nested functions");
  251. lua_pushvalue(L, 1); /* get function */
  252. lua_call(L, 0, 1); /* call it */
  253. if (lua_isnil(L, -1)) {
  254. *size = 0;
  255. return NULL;
  256. }
  257. else if (lua_isstring(L, -1)) {
  258. lua_replace(L, 3); /* save string in a reserved stack slot */
  259. return lua_tolstring(L, 3, size);
  260. }
  261. else luaL_error(L, "reader function must return a string");
  262. return NULL; /* to avoid warnings */
  263. }
  264. static int luaB_load (lua_State *L) {
  265. int status;
  266. const char *cname = luaL_optstring(L, 2, "=(load)");
  267. luaL_checktype(L, 1, LUA_TFUNCTION);
  268. lua_settop(L, 3); /* function, eventual name, plus one reserved slot */
  269. status = lua_load(L, generic_reader, NULL, cname);
  270. return load_aux(L, status);
  271. }
  272. static int luaB_dofile (lua_State *L) {
  273. const char *fname = luaL_optstring(L, 1, NULL);
  274. int n = lua_gettop(L);
  275. if (luaL_loadfile(L, fname) != 0) lua_error(L);
  276. lua_call(L, 0, LUA_MULTRET);
  277. return lua_gettop(L) - n;
  278. }
  279. static int luaB_assert (lua_State *L) {
  280. luaL_checkany(L, 1);
  281. if (!lua_toboolean(L, 1))
  282. return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!"));
  283. return lua_gettop(L);
  284. }
  285. static int luaB_unpack (lua_State *L) {
  286. int i, e, n;
  287. luaL_checktype(L, 1, LUA_TTABLE);
  288. i = luaL_optint(L, 2, 1);
  289. e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1));
  290. n = e - i + 1; /* number of elements */
  291. if (n <= 0) return 0; /* empty range */
  292. luaL_checkstack(L, n, "table too big to unpack");
  293. for (; i<=e; i++) /* push arg[i...e] */
  294. lua_rawgeti(L, 1, i);
  295. return n;
  296. }
  297. static int luaB_select (lua_State *L) {
  298. int n = lua_gettop(L);
  299. if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') {
  300. lua_pushinteger(L, n-1);
  301. return 1;
  302. }
  303. else {
  304. int i = luaL_checkint(L, 1);
  305. if (i < 0) i = n + i;
  306. else if (i > n) i = n;
  307. luaL_argcheck(L, 1 <= i, 1, "index out of range");
  308. return n - i;
  309. }
  310. }
  311. static int luaB_pcall (lua_State *L) {
  312. int status;
  313. luaL_checkany(L, 1);
  314. status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0);
  315. lua_pushboolean(L, (status == 0));
  316. lua_insert(L, 1);
  317. return lua_gettop(L); /* return status + all results */
  318. }
  319. static int luaB_xpcall (lua_State *L) {
  320. int status;
  321. luaL_checkany(L, 2);
  322. lua_settop(L, 2);
  323. lua_insert(L, 1); /* put error function under function to be called */
  324. status = lua_pcall(L, 0, LUA_MULTRET, 1);
  325. lua_pushboolean(L, (status == 0));
  326. lua_replace(L, 1);
  327. return lua_gettop(L); /* return status + all results */
  328. }
  329. static int luaB_tostring (lua_State *L) {
  330. luaL_checkany(L, 1);
  331. if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */
  332. return 1; /* use its value */
  333. switch (lua_type(L, 1)) {
  334. case LUA_TNUMBER:
  335. lua_pushstring(L, lua_tostring(L, 1));
  336. break;
  337. case LUA_TSTRING:
  338. lua_pushvalue(L, 1);
  339. break;
  340. case LUA_TBOOLEAN:
  341. lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
  342. break;
  343. case LUA_TNIL:
  344. lua_pushliteral(L, "nil");
  345. break;
  346. default:
  347. lua_pushfstring(L, "%s: %p", luaL_typename(L, 1), lua_topointer(L, 1));
  348. break;
  349. }
  350. return 1;
  351. }
  352. static int luaB_newproxy (lua_State *L) {
  353. lua_settop(L, 1);
  354. lua_newuserdata(L, 0); /* create proxy */
  355. if (lua_toboolean(L, 1) == 0)
  356. return 1; /* no metatable */
  357. else if (lua_isboolean(L, 1)) {
  358. lua_newtable(L); /* create a new metatable `m' ... */
  359. lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */
  360. lua_pushboolean(L, 1);
  361. lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */
  362. }
  363. else {
  364. int validproxy = 0; /* to check if weaktable[metatable(u)] == true */
  365. if (lua_getmetatable(L, 1)) {
  366. lua_rawget(L, lua_upvalueindex(1));
  367. validproxy = lua_toboolean(L, -1);
  368. lua_pop(L, 1); /* remove value */
  369. }
  370. luaL_argcheck(L, validproxy, 1, "boolean or proxy expected");
  371. lua_getmetatable(L, 1); /* metatable is valid; get it */
  372. }
  373. lua_setmetatable(L, 2);
  374. return 1;
  375. }
  376. static const luaL_Reg base_funcs[] = {
  377. {"assert", luaB_assert},
  378. {"collectgarbage", luaB_collectgarbage},
  379. {"dofile", luaB_dofile},
  380. {"error", luaB_error},
  381. {"gcinfo", luaB_gcinfo},
  382. {"getfenv", luaB_getfenv},
  383. {"getmetatable", luaB_getmetatable},
  384. {"loadfile", luaB_loadfile},
  385. {"load", luaB_load},
  386. {"loadstring", luaB_loadstring},
  387. {"next", luaB_next},
  388. {"pcall", luaB_pcall},
  389. {"print", luaB_print},
  390. {"rawequal", luaB_rawequal},
  391. {"rawget", luaB_rawget},
  392. {"rawset", luaB_rawset},
  393. {"select", luaB_select},
  394. {"setfenv", luaB_setfenv},
  395. {"setmetatable", luaB_setmetatable},
  396. {"tonumber", luaB_tonumber},
  397. {"tostring", luaB_tostring},
  398. {"type", luaB_type},
  399. {"unpack", luaB_unpack},
  400. {"xpcall", luaB_xpcall},
  401. {NULL, NULL}
  402. };
  403. /*
  404. ** {======================================================
  405. ** Coroutine library
  406. ** =======================================================
  407. */
  408. static int auxresume (lua_State *L, lua_State *co, int narg) {
  409. int status;
  410. if (!lua_checkstack(co, narg))
  411. luaL_error(L, "too many arguments to resume");
  412. if (lua_status(co) == 0 && lua_gettop(co) == 0) {
  413. lua_pushliteral(L, "cannot resume dead coroutine");
  414. return -1; /* error flag */
  415. }
  416. lua_xmove(L, co, narg);
  417. status = lua_resume(co, narg);
  418. if (status == 0 || status == LUA_YIELD) {
  419. int nres = lua_gettop(co);
  420. if (!lua_checkstack(L, nres))
  421. luaL_error(L, "too many results to resume");
  422. lua_xmove(co, L, nres); /* move yielded values */
  423. return nres;
  424. }
  425. else {
  426. lua_xmove(co, L, 1); /* move error message */
  427. return -1; /* error flag */
  428. }
  429. }
  430. static int luaB_coresume (lua_State *L) {
  431. lua_State *co = lua_tothread(L, 1);
  432. int r;
  433. luaL_argcheck(L, co, 1, "coroutine expected");
  434. r = auxresume(L, co, lua_gettop(L) - 1);
  435. if (r < 0) {
  436. lua_pushboolean(L, 0);
  437. lua_insert(L, -2);
  438. return 2; /* return false + error message */
  439. }
  440. else {
  441. lua_pushboolean(L, 1);
  442. lua_insert(L, -(r + 1));
  443. return r + 1; /* return true + `resume' returns */
  444. }
  445. }
  446. static int luaB_auxwrap (lua_State *L) {
  447. lua_State *co = lua_tothread(L, lua_upvalueindex(1));
  448. int r = auxresume(L, co, lua_gettop(L));
  449. if (r < 0) {
  450. if (lua_isstring(L, -1)) { /* error object is a string? */
  451. luaL_where(L, 1); /* add extra info */
  452. lua_insert(L, -2);
  453. lua_concat(L, 2);
  454. }
  455. lua_error(L); /* propagate error */
  456. }
  457. return r;
  458. }
  459. static int luaB_cocreate (lua_State *L) {
  460. lua_State *NL = lua_newthread(L);
  461. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
  462. "Lua function expected");
  463. lua_pushvalue(L, 1); /* move function to top */
  464. lua_xmove(L, NL, 1); /* move function from L to NL */
  465. return 1;
  466. }
  467. static int luaB_cowrap (lua_State *L) {
  468. luaB_cocreate(L);
  469. lua_pushcclosure(L, luaB_auxwrap, 1);
  470. return 1;
  471. }
  472. static int luaB_yield (lua_State *L) {
  473. return lua_yield(L, lua_gettop(L));
  474. }
  475. static int luaB_costatus (lua_State *L) {
  476. lua_State *co = lua_tothread(L, 1);
  477. luaL_argcheck(L, co, 1, "coroutine expected");
  478. if (L == co) lua_pushliteral(L, "running");
  479. else {
  480. switch (lua_status(co)) {
  481. case LUA_YIELD:
  482. lua_pushliteral(L, "suspended");
  483. break;
  484. case 0: {
  485. lua_Debug ar;
  486. if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */
  487. lua_pushliteral(L, "normal"); /* it is running */
  488. else if (lua_gettop(co) == 0)
  489. lua_pushliteral(L, "dead");
  490. else
  491. lua_pushliteral(L, "suspended"); /* initial state */
  492. break;
  493. }
  494. default: /* some error occured */
  495. lua_pushliteral(L, "dead");
  496. break;
  497. }
  498. }
  499. return 1;
  500. }
  501. static int luaB_corunning (lua_State *L) {
  502. if (lua_pushthread(L))
  503. return 0; /* main thread is not a coroutine */
  504. else
  505. return 1;
  506. }
  507. static const luaL_Reg co_funcs[] = {
  508. {"create", luaB_cocreate},
  509. {"resume", luaB_coresume},
  510. {"running", luaB_corunning},
  511. {"status", luaB_costatus},
  512. {"wrap", luaB_cowrap},
  513. {"yield", luaB_yield},
  514. {NULL, NULL}
  515. };
  516. /* }====================================================== */
  517. static void auxopen (lua_State *L, const char *name,
  518. lua_CFunction f, lua_CFunction u) {
  519. lua_pushcfunction(L, u);
  520. lua_pushcclosure(L, f, 1);
  521. lua_setfield(L, -2, name);
  522. }
  523. static void base_open (lua_State *L) {
  524. /* set global _G */
  525. lua_pushvalue(L, LUA_GLOBALSINDEX);
  526. lua_setglobal(L, "_G");
  527. /* open lib into global table */
  528. luaL_register(L, "_G", base_funcs);
  529. lua_pushliteral(L, LUA_VERSION);
  530. lua_setglobal(L, "_VERSION"); /* set global _VERSION */
  531. /* `ipairs' and `pairs' need auxliliary functions as upvalues */
  532. auxopen(L, "ipairs", luaB_ipairs, ipairsaux);
  533. auxopen(L, "pairs", luaB_pairs, luaB_next);
  534. /* `newproxy' needs a weaktable as upvalue */
  535. lua_createtable(L, 0, 1); /* new table `w' */
  536. lua_pushvalue(L, -1); /* `w' will be its own metatable */
  537. lua_setmetatable(L, -2);
  538. lua_pushliteral(L, "kv");
  539. lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */
  540. lua_pushcclosure(L, luaB_newproxy, 1);
  541. lua_setglobal(L, "newproxy"); /* set global `newproxy' */
  542. }
  543. LUALIB_API int luaopen_base (lua_State *L) {
  544. base_open(L);
  545. luaL_register(L, LUA_COLIBNAME, co_funcs);
  546. return 2;
  547. }
  548. #endif