lbaselib.c 19 KB

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