lbaselib.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. /*
  2. ** $Id: lbaselib.c,v 1.143 2004/05/10 17:50:51 roberto Exp roberto $
  3. ** Basic library
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #define lbaselib_c
  11. #define LUA_LIB
  12. #include "lua.h"
  13. #include "lauxlib.h"
  14. #include "lualib.h"
  15. /*
  16. ** If your system does not support `stdout', you can just remove this function.
  17. ** If you need, you can define your own `print' function, following this
  18. ** model but changing `fputs' to put the strings at a proper place
  19. ** (a console window or a log file, for instance).
  20. */
  21. static int luaB_print (lua_State *L) {
  22. int n = lua_gettop(L); /* number of arguments */
  23. int i;
  24. lua_getglobal(L, "tostring");
  25. for (i=1; i<=n; i++) {
  26. const char *s;
  27. lua_pushvalue(L, -1); /* function to be called */
  28. lua_pushvalue(L, i); /* value to print */
  29. lua_call(L, 1, 1);
  30. s = lua_tostring(L, -1); /* get result */
  31. if (s == NULL)
  32. return luaL_error(L, "`tostring' must return a string to `print'");
  33. if (i>1) fputs("\t", stdout);
  34. fputs(s, stdout);
  35. lua_pop(L, 1); /* pop result */
  36. }
  37. fputs("\n", stdout);
  38. return 0;
  39. }
  40. static int luaB_tonumber (lua_State *L) {
  41. int base = luaL_optint(L, 2, 10);
  42. if (base == 10) { /* standard conversion */
  43. luaL_checkany(L, 1);
  44. if (lua_isnumber(L, 1)) {
  45. lua_pushnumber(L, lua_tonumber(L, 1));
  46. return 1;
  47. }
  48. }
  49. else {
  50. const char *s1 = luaL_checkstring(L, 1);
  51. char *s2;
  52. unsigned long n;
  53. luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
  54. n = strtoul(s1, &s2, base);
  55. if (s1 != s2) { /* at least one valid digit? */
  56. while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
  57. if (*s2 == '\0') { /* no invalid trailing characters? */
  58. lua_pushnumber(L, (lua_Number)n);
  59. return 1;
  60. }
  61. }
  62. }
  63. lua_pushnil(L); /* else not a number */
  64. return 1;
  65. }
  66. static int luaB_error (lua_State *L) {
  67. int level = luaL_optint(L, 2, 1);
  68. lua_settop(L, 1);
  69. if (lua_isstring(L, 1) && level > 0) { /* add extra information? */
  70. luaL_where(L, level);
  71. lua_pushvalue(L, 1);
  72. lua_concat(L, 2);
  73. }
  74. return lua_error(L);
  75. }
  76. static int luaB_getmetatable (lua_State *L) {
  77. luaL_checkany(L, 1);
  78. if (!lua_getmetatable(L, 1)) {
  79. lua_pushnil(L);
  80. return 1; /* no metatable */
  81. }
  82. luaL_getmetafield(L, 1, "__metatable");
  83. return 1; /* returns either __metatable field (if present) or metatable */
  84. }
  85. static int luaB_setmetatable (lua_State *L) {
  86. int t = lua_type(L, 2);
  87. luaL_checktype(L, 1, LUA_TTABLE);
  88. luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
  89. "nil or table expected");
  90. if (luaL_getmetafield(L, 1, "__metatable"))
  91. luaL_error(L, "cannot change a protected metatable");
  92. lua_settop(L, 2);
  93. lua_setmetatable(L, 1);
  94. return 1;
  95. }
  96. static void getfunc (lua_State *L) {
  97. if (lua_isfunction(L, 1)) lua_pushvalue(L, 1);
  98. else {
  99. lua_Debug ar;
  100. int level = luaL_optint(L, 1, 1);
  101. luaL_argcheck(L, level >= 0, 1, "level must be non-negative");
  102. if (lua_getstack(L, level, &ar) == 0)
  103. luaL_argerror(L, 1, "invalid level");
  104. lua_getinfo(L, "f", &ar);
  105. if (lua_isnil(L, -1))
  106. luaL_error(L, "no function environment for tail call at level %d",
  107. level);
  108. }
  109. }
  110. static int aux_getfenv (lua_State *L) {
  111. lua_getfenv(L, -1);
  112. lua_pushliteral(L, "__fenv");
  113. lua_rawget(L, -2);
  114. return !lua_isnil(L, -1);
  115. }
  116. static int luaB_getfenv (lua_State *L) {
  117. getfunc(L);
  118. if (!aux_getfenv(L)) /* __fenv not defined? */
  119. lua_pop(L, 1); /* remove it, to return real environment */
  120. return 1;
  121. }
  122. static int luaB_setfenv (lua_State *L) {
  123. luaL_checktype(L, 2, LUA_TTABLE);
  124. getfunc(L);
  125. if (aux_getfenv(L)) /* __fenv defined? */
  126. luaL_error(L, "`setfenv' cannot change a protected environment");
  127. else
  128. lua_pop(L, 2); /* remove __fenv and real environment table */
  129. lua_pushvalue(L, 2);
  130. if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0)
  131. lua_replace(L, LUA_GLOBALSINDEX);
  132. else if (lua_setfenv(L, -2) == 0)
  133. luaL_error(L, "`setfenv' cannot change environment of given function");
  134. return 0;
  135. }
  136. static int luaB_rawequal (lua_State *L) {
  137. luaL_checkany(L, 1);
  138. luaL_checkany(L, 2);
  139. lua_pushboolean(L, lua_rawequal(L, 1, 2));
  140. return 1;
  141. }
  142. static int luaB_rawget (lua_State *L) {
  143. luaL_checktype(L, 1, LUA_TTABLE);
  144. luaL_checkany(L, 2);
  145. lua_rawget(L, 1);
  146. return 1;
  147. }
  148. static int luaB_rawset (lua_State *L) {
  149. luaL_checktype(L, 1, LUA_TTABLE);
  150. luaL_checkany(L, 2);
  151. luaL_checkany(L, 3);
  152. lua_rawset(L, 1);
  153. return 1;
  154. }
  155. static int luaB_gcinfo (lua_State *L) {
  156. lua_pushinteger(L, lua_getgccount(L));
  157. return 1;
  158. }
  159. static int luaB_collectgarbage (lua_State *L) {
  160. static const char *const opts[] = {"stop", "restart", "collect", "count",
  161. NULL};
  162. static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART,
  163. LUA_GCCOLLECT, LUA_GCCOUNT};
  164. int o;
  165. int ex;
  166. #if 1
  167. if (lua_isnumber(L, 1)) {
  168. int v = lua_tointeger(L, 1);
  169. lua_settop(L, 0);
  170. if (v == 0) lua_pushstring(L, "collect");
  171. else if (v >= 10000) lua_pushstring(L, "stop");
  172. else lua_pushstring(L, "restart");
  173. }
  174. #endif
  175. o = luaL_findstring(luaL_optstring(L, 1, "collect"), opts);
  176. ex = luaL_optint(L, 2, 0);
  177. luaL_argcheck(L, o >= 0, 1, "invalid option");
  178. lua_pushinteger(L, lua_gc(L, optsnum[o], ex));
  179. return 1;
  180. }
  181. static int luaB_type (lua_State *L) {
  182. luaL_checkany(L, 1);
  183. lua_pushstring(L, lua_typename(L, lua_type(L, 1)));
  184. return 1;
  185. }
  186. static int luaB_next (lua_State *L) {
  187. luaL_checktype(L, 1, LUA_TTABLE);
  188. lua_settop(L, 2); /* create a 2nd argument if there isn't one */
  189. if (lua_next(L, 1))
  190. return 2;
  191. else {
  192. lua_pushnil(L);
  193. return 1;
  194. }
  195. }
  196. static int luaB_pairs (lua_State *L) {
  197. luaL_checktype(L, 1, LUA_TTABLE);
  198. lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
  199. lua_pushvalue(L, 1); /* state, */
  200. lua_pushnil(L); /* and initial value */
  201. return 3;
  202. }
  203. static int ipairsaux (lua_State *L) {
  204. int i = luaL_checkint(L, 2);
  205. luaL_checktype(L, 1, LUA_TTABLE);
  206. i++; /* next value */
  207. lua_pushinteger(L, i);
  208. lua_rawgeti(L, 1, i);
  209. return (lua_isnil(L, -1)) ? 0 : 2;
  210. }
  211. static int luaB_ipairs (lua_State *L) {
  212. luaL_checktype(L, 1, LUA_TTABLE);
  213. lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
  214. lua_pushvalue(L, 1); /* state, */
  215. lua_pushinteger(L, LUA_FIRSTINDEX - 1); /* and initial value */
  216. return 3;
  217. }
  218. static int load_aux (lua_State *L, int status) {
  219. if (status == 0) /* OK? */
  220. return 1;
  221. else {
  222. lua_pushnil(L);
  223. lua_insert(L, -2); /* put before error message */
  224. return 2; /* return nil plus error message */
  225. }
  226. }
  227. static int luaB_loadstring (lua_State *L) {
  228. size_t l;
  229. const char *s = luaL_checklstring(L, 1, &l);
  230. const char *chunkname = luaL_optstring(L, 2, s);
  231. return load_aux(L, luaL_loadbuffer(L, s, l, chunkname));
  232. }
  233. static int luaB_loadfile (lua_State *L) {
  234. const char *fname = luaL_optstring(L, 1, NULL);
  235. return load_aux(L, luaL_loadfile(L, fname));
  236. }
  237. struct Aux_load {
  238. int func;
  239. int res;
  240. };
  241. static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
  242. struct Aux_load *al = (struct Aux_load *)ud;
  243. luaL_unref(L, al->res, LUA_REGISTRYINDEX);
  244. lua_getref(L, al->func);
  245. lua_call(L, 0, 1);
  246. if (lua_isnil(L, -1)) {
  247. *size = 0;
  248. return NULL;
  249. }
  250. else if (lua_isstring(L, -1)) {
  251. const char *res = lua_tostring(L, -1);
  252. *size = lua_strlen(L, -1);
  253. al->res = luaL_ref(L, LUA_REGISTRYINDEX);
  254. return res;
  255. }
  256. else luaL_error(L, "reader function must return a string");
  257. return NULL; /* to avoid warnings */
  258. }
  259. static int luaB_load (lua_State *L) {
  260. struct Aux_load al;
  261. int status;
  262. const char *cname = luaL_optstring(L, 2, "=(load)");
  263. luaL_checktype(L, 1, LUA_TFUNCTION);
  264. lua_settop(L, 1);
  265. al.func = luaL_ref(L, LUA_REGISTRYINDEX);
  266. al.res = LUA_REFNIL;
  267. status = lua_load(L, generic_reader, &al, cname);
  268. luaL_unref(L, al.func, LUA_REGISTRYINDEX);
  269. luaL_unref(L, al.res, LUA_REGISTRYINDEX);
  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. lua_settop(L, 1);
  284. return 1;
  285. }
  286. static int luaB_unpack (lua_State *L) {
  287. int i = luaL_optint(L, 2, LUA_FIRSTINDEX);
  288. int e = luaL_optint(L, 3, -1);
  289. int n;
  290. luaL_checktype(L, 1, LUA_TTABLE);
  291. if (e == -1)
  292. e = luaL_getn(L, 1) + LUA_FIRSTINDEX - 1;
  293. n = e - i + 1; /* number of elements */
  294. if (n <= 0) return 0; /* empty range */
  295. luaL_checkstack(L, n, "table too big to unpack");
  296. for (; i<=e; i++) /* push arg[i...e] */
  297. lua_rawgeti(L, 1, i);
  298. return n;
  299. }
  300. static int luaB_select (lua_State *L) {
  301. int i = luaL_checkint(L, 1);
  302. int n = lua_gettop(L);
  303. if (i < 0 || i >= n) /* index out of range? */
  304. return 0;
  305. if (i == 0)
  306. lua_pushinteger(L, n-1);
  307. else
  308. lua_pushvalue(L, i+1);
  309. return 1;
  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. char buff[4*sizeof(void *) + 2]; /* enough space for a `%p' */
  331. const char *tn = "";
  332. const void *p = NULL;
  333. luaL_checkany(L, 1);
  334. if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */
  335. return 1; /* use its value */
  336. switch (lua_type(L, 1)) {
  337. case LUA_TNUMBER:
  338. lua_pushstring(L, lua_tostring(L, 1));
  339. return 1;
  340. case LUA_TSTRING:
  341. lua_pushvalue(L, 1);
  342. return 1;
  343. case LUA_TBOOLEAN:
  344. lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
  345. return 1;
  346. case LUA_TNIL:
  347. lua_pushliteral(L, "nil");
  348. return 1;
  349. case LUA_TTABLE:
  350. p = lua_topointer(L, 1);
  351. tn = "table";
  352. break;
  353. case LUA_TFUNCTION:
  354. p = lua_topointer(L, 1);
  355. tn = "function";
  356. break;
  357. case LUA_TUSERDATA:
  358. case LUA_TLIGHTUSERDATA:
  359. p = lua_touserdata(L, 1);
  360. tn = "userdata";
  361. break;
  362. case LUA_TTHREAD:
  363. p = lua_tothread(L, 1);
  364. tn = "thread";
  365. break;
  366. }
  367. sprintf(buff, "%p", p);
  368. lua_pushfstring(L, "%s: %s", tn, buff);
  369. return 1;
  370. }
  371. static int luaB_newproxy (lua_State *L) {
  372. lua_settop(L, 1);
  373. lua_newuserdata(L, 0); /* create proxy */
  374. if (lua_toboolean(L, 1) == 0)
  375. return 1; /* no metatable */
  376. else if (lua_isboolean(L, 1)) {
  377. lua_newtable(L); /* create a new metatable `m' ... */
  378. lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */
  379. lua_pushboolean(L, 1);
  380. lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */
  381. }
  382. else {
  383. int validproxy = 0; /* to check if weaktable[metatable(u)] == true */
  384. if (lua_getmetatable(L, 1)) {
  385. lua_rawget(L, lua_upvalueindex(1));
  386. validproxy = lua_toboolean(L, -1);
  387. lua_pop(L, 1); /* remove value */
  388. }
  389. luaL_argcheck(L, validproxy, 1, "boolean or proxy expected");
  390. lua_getmetatable(L, 1); /* metatable is valid; get it */
  391. }
  392. lua_setmetatable(L, 2);
  393. return 1;
  394. }
  395. /*
  396. ** {======================================================
  397. ** `require' function
  398. ** =======================================================
  399. */
  400. static const char *getpath (lua_State *L) {
  401. const char *path;
  402. lua_getglobal(L, LUA_PATH); /* try global variable */
  403. path = lua_tostring(L, -1);
  404. lua_pop(L, 1);
  405. if (path) return path;
  406. path = getenv(LUA_PATH); /* else try environment variable */
  407. if (path) return path;
  408. return LUA_PATH_DEFAULT; /* else use default */
  409. }
  410. static const char *pushnextpath (lua_State *L, const char *path) {
  411. const char *l;
  412. if (*path == '\0') return NULL; /* no more paths */
  413. if (*path == LUA_PATH_SEP) path++; /* skip separator */
  414. l = strchr(path, LUA_PATH_SEP); /* find next separator */
  415. if (l == NULL) l = path+strlen(path);
  416. lua_pushlstring(L, path, l - path); /* directory name */
  417. return l;
  418. }
  419. static void pushcomposename (lua_State *L) {
  420. const char *path = lua_tostring(L, -1);
  421. const char *wild;
  422. int n = 1;
  423. while ((wild = strchr(path, LUA_PATH_MARK)) != NULL) {
  424. /* is there stack space for prefix, name, and eventual last suffix? */
  425. luaL_checkstack(L, 3, "too many marks in a path component");
  426. lua_pushlstring(L, path, wild - path); /* push prefix */
  427. lua_pushvalue(L, 1); /* push package name (in place of MARK) */
  428. path = wild + 1; /* continue after MARK */
  429. n += 2;
  430. }
  431. lua_pushstring(L, path); /* push last suffix (`n' already includes this) */
  432. lua_concat(L, n);
  433. }
  434. static int luaB_require (lua_State *L) {
  435. const char *path;
  436. int status = LUA_ERRFILE; /* not found (yet) */
  437. luaL_checkstring(L, 1);
  438. lua_settop(L, 1);
  439. lua_getglobal(L, REQTAB);
  440. if (!lua_istable(L, 2)) return luaL_error(L, "`" REQTAB "' is not a table");
  441. path = getpath(L);
  442. lua_pushvalue(L, 1); /* check package's name in book-keeping table */
  443. lua_rawget(L, 2);
  444. if (lua_toboolean(L, -1)) /* is it there? */
  445. return 1; /* package is already loaded; return its result */
  446. else { /* must load it */
  447. while (status == LUA_ERRFILE) {
  448. lua_settop(L, 3); /* reset stack position */
  449. if ((path = pushnextpath(L, path)) == NULL) break;
  450. pushcomposename(L);
  451. status = luaL_loadfile(L, lua_tostring(L, -1)); /* try to load it */
  452. }
  453. }
  454. switch (status) {
  455. case 0: {
  456. lua_getglobal(L, "_REQUIREDNAME"); /* save previous name */
  457. lua_insert(L, -2); /* put it below function */
  458. lua_pushvalue(L, 1);
  459. lua_setglobal(L, "_REQUIREDNAME"); /* set new name */
  460. lua_call(L, 0, 1); /* run loaded module */
  461. lua_insert(L, -2); /* put result below previous name */
  462. lua_setglobal(L, "_REQUIREDNAME"); /* reset to previous name */
  463. if (lua_isnil(L, -1)) { /* no/nil return? */
  464. lua_pushboolean(L, 1);
  465. lua_replace(L, -2); /* replace to true */
  466. }
  467. lua_pushvalue(L, 1);
  468. lua_pushvalue(L, -2);
  469. lua_rawset(L, 2); /* mark it as loaded */
  470. return 1; /* return value */
  471. }
  472. case LUA_ERRFILE: { /* file not found */
  473. return luaL_error(L, "could not load package `%s' from path `%s'",
  474. lua_tostring(L, 1), getpath(L));
  475. }
  476. default: {
  477. return luaL_error(L, "error loading package `%s' (%s)",
  478. lua_tostring(L, 1), lua_tostring(L, -1));
  479. }
  480. }
  481. }
  482. /* }====================================================== */
  483. static const luaL_reg base_funcs[] = {
  484. {"error", luaB_error},
  485. {"getmetatable", luaB_getmetatable},
  486. {"setmetatable", luaB_setmetatable},
  487. {"getfenv", luaB_getfenv},
  488. {"setfenv", luaB_setfenv},
  489. {"next", luaB_next},
  490. {"print", luaB_print},
  491. {"tonumber", luaB_tonumber},
  492. {"tostring", luaB_tostring},
  493. {"type", luaB_type},
  494. {"assert", luaB_assert},
  495. {"unpack", luaB_unpack},
  496. {"select", luaB_select},
  497. {"rawequal", luaB_rawequal},
  498. {"rawget", luaB_rawget},
  499. {"rawset", luaB_rawset},
  500. {"pcall", luaB_pcall},
  501. {"xpcall", luaB_xpcall},
  502. {"collectgarbage", luaB_collectgarbage},
  503. {"gcinfo", luaB_gcinfo},
  504. {"loadfile", luaB_loadfile},
  505. {"dofile", luaB_dofile},
  506. {"loadstring", luaB_loadstring},
  507. {"load", luaB_load},
  508. {"require", luaB_require},
  509. {NULL, NULL}
  510. };
  511. /*
  512. ** {======================================================
  513. ** Coroutine library
  514. ** =======================================================
  515. */
  516. static int auxresume (lua_State *L, lua_State *co, int narg) {
  517. int status;
  518. if (!lua_checkstack(co, narg))
  519. luaL_error(L, "too many arguments to resume");
  520. lua_xmove(L, co, narg);
  521. status = lua_resume(co, narg);
  522. if (status == 0) {
  523. int nres = lua_gettop(co);
  524. if (!lua_checkstack(L, nres))
  525. luaL_error(L, "too many results to resume");
  526. lua_xmove(co, L, nres); /* move yielded values */
  527. return nres;
  528. }
  529. else {
  530. lua_xmove(co, L, 1); /* move error message */
  531. return -1; /* error flag */
  532. }
  533. }
  534. static int luaB_coresume (lua_State *L) {
  535. lua_State *co = lua_tothread(L, 1);
  536. int r;
  537. luaL_argcheck(L, co, 1, "coroutine expected");
  538. r = auxresume(L, co, lua_gettop(L) - 1);
  539. if (r < 0) {
  540. lua_pushboolean(L, 0);
  541. lua_insert(L, -2);
  542. return 2; /* return false + error message */
  543. }
  544. else {
  545. lua_pushboolean(L, 1);
  546. lua_insert(L, -(r + 1));
  547. return r + 1; /* return true + `resume' returns */
  548. }
  549. }
  550. static int luaB_auxwrap (lua_State *L) {
  551. lua_State *co = lua_tothread(L, lua_upvalueindex(1));
  552. int r = auxresume(L, co, lua_gettop(L));
  553. if (r < 0) {
  554. if (lua_isstring(L, -1)) { /* error object is a string? */
  555. luaL_where(L, 1); /* add extra info */
  556. lua_insert(L, -2);
  557. lua_concat(L, 2);
  558. }
  559. lua_error(L); /* propagate error */
  560. }
  561. return r;
  562. }
  563. static int luaB_cocreate (lua_State *L) {
  564. lua_State *NL = lua_newthread(L);
  565. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
  566. "Lua function expected");
  567. lua_pushvalue(L, 1); /* move function to top */
  568. lua_xmove(L, NL, 1); /* move function from L to NL */
  569. return 1;
  570. }
  571. static int luaB_cowrap (lua_State *L) {
  572. luaB_cocreate(L);
  573. lua_pushcclosure(L, luaB_auxwrap, 1);
  574. return 1;
  575. }
  576. static int luaB_yield (lua_State *L) {
  577. return lua_yield(L, lua_gettop(L));
  578. }
  579. static int luaB_costatus (lua_State *L) {
  580. lua_State *co = lua_tothread(L, 1);
  581. luaL_argcheck(L, co, 1, "coroutine expected");
  582. if (L == co) lua_pushliteral(L, "running");
  583. else {
  584. lua_Debug ar;
  585. if (lua_getstack(co, 0, &ar) == 0 && lua_gettop(co) == 0)
  586. lua_pushliteral(L, "dead");
  587. else
  588. lua_pushliteral(L, "suspended");
  589. }
  590. return 1;
  591. }
  592. static const luaL_reg co_funcs[] = {
  593. {"create", luaB_cocreate},
  594. {"wrap", luaB_cowrap},
  595. {"resume", luaB_coresume},
  596. {"yield", luaB_yield},
  597. {"status", luaB_costatus},
  598. {NULL, NULL}
  599. };
  600. /* }====================================================== */
  601. static void auxopen (lua_State *L, const char *name,
  602. lua_CFunction f, lua_CFunction u) {
  603. lua_pushcfunction(L, u);
  604. lua_pushcclosure(L, f, 1);
  605. lua_setfield(L, -2, name);
  606. }
  607. static void base_open (lua_State *L) {
  608. lua_pushvalue(L, LUA_GLOBALSINDEX);
  609. luaL_openlib(L, NULL, base_funcs, 0); /* open lib into global table */
  610. lua_pushliteral(L, LUA_VERSION);
  611. lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */
  612. /* `ipairs' and `pairs' need auxiliary functions as upvalues */
  613. auxopen(L, "ipairs", luaB_ipairs, ipairsaux);
  614. auxopen(L, "pairs", luaB_pairs, luaB_next);
  615. /* `newproxy' needs a weaktable as upvalue */
  616. lua_newtable(L); /* new table `w' */
  617. lua_pushvalue(L, -1); /* `w' will be its own metatable */
  618. lua_setmetatable(L, -2);
  619. lua_pushliteral(L, "kv");
  620. lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */
  621. lua_pushcclosure(L, luaB_newproxy, 1);
  622. lua_setfield(L, -2, "newproxy"); /* set global `newproxy' */
  623. lua_setfield(L, -1, "_G"); /* set global _G */
  624. }
  625. LUALIB_API int luaopen_base (lua_State *L) {
  626. base_open(L);
  627. luaL_openlib(L, LUA_COLIBNAME, co_funcs, 0);
  628. lua_newtable(L);
  629. lua_setglobal(L, REQTAB);
  630. return 2;
  631. }