lbaselib.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /*
  2. ** $Id: lbaselib.c,v 1.144 2004/05/31 18:50:30 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 luaB_getfenv (lua_State *L) {
  111. getfunc(L);
  112. lua_getfenv(L, -1);
  113. return 1;
  114. }
  115. static int luaB_setfenv (lua_State *L) {
  116. luaL_checktype(L, 2, LUA_TTABLE);
  117. getfunc(L);
  118. lua_pushvalue(L, 2);
  119. if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0)
  120. lua_replace(L, LUA_GLOBALSINDEX);
  121. else if (lua_setfenv(L, -2) == 0)
  122. luaL_error(L, "`setfenv' cannot change environment of given function");
  123. return 0;
  124. }
  125. static int luaB_rawequal (lua_State *L) {
  126. luaL_checkany(L, 1);
  127. luaL_checkany(L, 2);
  128. lua_pushboolean(L, lua_rawequal(L, 1, 2));
  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_rawget(L, 1);
  135. return 1;
  136. }
  137. static int luaB_rawset (lua_State *L) {
  138. luaL_checktype(L, 1, LUA_TTABLE);
  139. luaL_checkany(L, 2);
  140. luaL_checkany(L, 3);
  141. lua_rawset(L, 1);
  142. return 1;
  143. }
  144. static int luaB_gcinfo (lua_State *L) {
  145. lua_pushinteger(L, lua_getgccount(L));
  146. return 1;
  147. }
  148. static int luaB_collectgarbage (lua_State *L) {
  149. static const char *const opts[] = {"stop", "restart", "collect", "count",
  150. NULL};
  151. static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART,
  152. LUA_GCCOLLECT, LUA_GCCOUNT};
  153. int o;
  154. int ex;
  155. #if 1
  156. if (lua_isnumber(L, 1)) {
  157. int v = lua_tointeger(L, 1);
  158. lua_settop(L, 0);
  159. if (v == 0) lua_pushstring(L, "collect");
  160. else if (v >= 10000) lua_pushstring(L, "stop");
  161. else lua_pushstring(L, "restart");
  162. }
  163. #endif
  164. o = luaL_findstring(luaL_optstring(L, 1, "collect"), opts);
  165. ex = luaL_optint(L, 2, 0);
  166. luaL_argcheck(L, o >= 0, 1, "invalid option");
  167. lua_pushinteger(L, lua_gc(L, optsnum[o], ex));
  168. return 1;
  169. }
  170. static int luaB_type (lua_State *L) {
  171. luaL_checkany(L, 1);
  172. lua_pushstring(L, lua_typename(L, lua_type(L, 1)));
  173. return 1;
  174. }
  175. static int luaB_next (lua_State *L) {
  176. luaL_checktype(L, 1, LUA_TTABLE);
  177. lua_settop(L, 2); /* create a 2nd argument if there isn't one */
  178. if (lua_next(L, 1))
  179. return 2;
  180. else {
  181. lua_pushnil(L);
  182. return 1;
  183. }
  184. }
  185. static int luaB_pairs (lua_State *L) {
  186. luaL_checktype(L, 1, LUA_TTABLE);
  187. lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
  188. lua_pushvalue(L, 1); /* state, */
  189. lua_pushnil(L); /* and initial value */
  190. return 3;
  191. }
  192. static int ipairsaux (lua_State *L) {
  193. int i = luaL_checkint(L, 2);
  194. luaL_checktype(L, 1, LUA_TTABLE);
  195. i++; /* next value */
  196. lua_pushinteger(L, i);
  197. lua_rawgeti(L, 1, i);
  198. return (lua_isnil(L, -1)) ? 0 : 2;
  199. }
  200. static int luaB_ipairs (lua_State *L) {
  201. luaL_checktype(L, 1, LUA_TTABLE);
  202. lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
  203. lua_pushvalue(L, 1); /* state, */
  204. lua_pushinteger(L, LUA_FIRSTINDEX - 1); /* and initial value */
  205. return 3;
  206. }
  207. static int load_aux (lua_State *L, int status) {
  208. if (status == 0) /* OK? */
  209. return 1;
  210. else {
  211. lua_pushnil(L);
  212. lua_insert(L, -2); /* put before error message */
  213. return 2; /* return nil plus error message */
  214. }
  215. }
  216. static int luaB_loadstring (lua_State *L) {
  217. size_t l;
  218. const char *s = luaL_checklstring(L, 1, &l);
  219. const char *chunkname = luaL_optstring(L, 2, s);
  220. return load_aux(L, luaL_loadbuffer(L, s, l, chunkname));
  221. }
  222. static int luaB_loadfile (lua_State *L) {
  223. const char *fname = luaL_optstring(L, 1, NULL);
  224. return load_aux(L, luaL_loadfile(L, fname));
  225. }
  226. struct Aux_load {
  227. int func;
  228. int res;
  229. };
  230. static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
  231. struct Aux_load *al = (struct Aux_load *)ud;
  232. luaL_unref(L, al->res, LUA_REGISTRYINDEX);
  233. lua_getref(L, al->func);
  234. lua_call(L, 0, 1);
  235. if (lua_isnil(L, -1)) {
  236. *size = 0;
  237. return NULL;
  238. }
  239. else if (lua_isstring(L, -1)) {
  240. const char *res = lua_tostring(L, -1);
  241. *size = lua_strlen(L, -1);
  242. al->res = luaL_ref(L, LUA_REGISTRYINDEX);
  243. return res;
  244. }
  245. else luaL_error(L, "reader function must return a string");
  246. return NULL; /* to avoid warnings */
  247. }
  248. static int luaB_load (lua_State *L) {
  249. struct Aux_load al;
  250. int status;
  251. const char *cname = luaL_optstring(L, 2, "=(load)");
  252. luaL_checktype(L, 1, LUA_TFUNCTION);
  253. lua_settop(L, 1);
  254. al.func = luaL_ref(L, LUA_REGISTRYINDEX);
  255. al.res = LUA_REFNIL;
  256. status = lua_load(L, generic_reader, &al, cname);
  257. luaL_unref(L, al.func, LUA_REGISTRYINDEX);
  258. luaL_unref(L, al.res, LUA_REGISTRYINDEX);
  259. return load_aux(L, status);
  260. }
  261. static int luaB_dofile (lua_State *L) {
  262. const char *fname = luaL_optstring(L, 1, NULL);
  263. int n = lua_gettop(L);
  264. if (luaL_loadfile(L, fname) != 0) lua_error(L);
  265. lua_call(L, 0, LUA_MULTRET);
  266. return lua_gettop(L) - n;
  267. }
  268. static int luaB_assert (lua_State *L) {
  269. luaL_checkany(L, 1);
  270. if (!lua_toboolean(L, 1))
  271. return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!"));
  272. lua_settop(L, 1);
  273. return 1;
  274. }
  275. static int luaB_unpack (lua_State *L) {
  276. int i = luaL_optint(L, 2, LUA_FIRSTINDEX);
  277. int e = luaL_optint(L, 3, -1);
  278. int n;
  279. luaL_checktype(L, 1, LUA_TTABLE);
  280. if (e == -1)
  281. e = luaL_getn(L, 1) + LUA_FIRSTINDEX - 1;
  282. n = e - i + 1; /* number of elements */
  283. if (n <= 0) return 0; /* empty range */
  284. luaL_checkstack(L, n, "table too big to unpack");
  285. for (; i<=e; i++) /* push arg[i...e] */
  286. lua_rawgeti(L, 1, i);
  287. return n;
  288. }
  289. static int luaB_select (lua_State *L) {
  290. int i = luaL_checkint(L, 1);
  291. int n = lua_gettop(L);
  292. if (i < 0 || i >= n) /* index out of range? */
  293. return 0;
  294. if (i == 0)
  295. lua_pushinteger(L, n-1);
  296. else
  297. lua_pushvalue(L, i+1);
  298. return 1;
  299. }
  300. static int luaB_pcall (lua_State *L) {
  301. int status;
  302. luaL_checkany(L, 1);
  303. status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0);
  304. lua_pushboolean(L, (status == 0));
  305. lua_insert(L, 1);
  306. return lua_gettop(L); /* return status + all results */
  307. }
  308. static int luaB_xpcall (lua_State *L) {
  309. int status;
  310. luaL_checkany(L, 2);
  311. lua_settop(L, 2);
  312. lua_insert(L, 1); /* put error function under function to be called */
  313. status = lua_pcall(L, 0, LUA_MULTRET, 1);
  314. lua_pushboolean(L, (status == 0));
  315. lua_replace(L, 1);
  316. return lua_gettop(L); /* return status + all results */
  317. }
  318. static int luaB_tostring (lua_State *L) {
  319. char buff[4*sizeof(void *) + 2]; /* enough space for a `%p' */
  320. const char *tn = "";
  321. const void *p = NULL;
  322. luaL_checkany(L, 1);
  323. if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */
  324. return 1; /* use its value */
  325. switch (lua_type(L, 1)) {
  326. case LUA_TNUMBER:
  327. lua_pushstring(L, lua_tostring(L, 1));
  328. return 1;
  329. case LUA_TSTRING:
  330. lua_pushvalue(L, 1);
  331. return 1;
  332. case LUA_TBOOLEAN:
  333. lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
  334. return 1;
  335. case LUA_TNIL:
  336. lua_pushliteral(L, "nil");
  337. return 1;
  338. case LUA_TTABLE:
  339. p = lua_topointer(L, 1);
  340. tn = "table";
  341. break;
  342. case LUA_TFUNCTION:
  343. p = lua_topointer(L, 1);
  344. tn = "function";
  345. break;
  346. case LUA_TUSERDATA:
  347. case LUA_TLIGHTUSERDATA:
  348. p = lua_touserdata(L, 1);
  349. tn = "userdata";
  350. break;
  351. case LUA_TTHREAD:
  352. p = lua_tothread(L, 1);
  353. tn = "thread";
  354. break;
  355. }
  356. sprintf(buff, "%p", p);
  357. lua_pushfstring(L, "%s: %s", tn, buff);
  358. return 1;
  359. }
  360. static int luaB_newproxy (lua_State *L) {
  361. lua_settop(L, 1);
  362. lua_newuserdata(L, 0); /* create proxy */
  363. if (lua_toboolean(L, 1) == 0)
  364. return 1; /* no metatable */
  365. else if (lua_isboolean(L, 1)) {
  366. lua_newtable(L); /* create a new metatable `m' ... */
  367. lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */
  368. lua_pushboolean(L, 1);
  369. lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */
  370. }
  371. else {
  372. int validproxy = 0; /* to check if weaktable[metatable(u)] == true */
  373. if (lua_getmetatable(L, 1)) {
  374. lua_rawget(L, lua_upvalueindex(1));
  375. validproxy = lua_toboolean(L, -1);
  376. lua_pop(L, 1); /* remove value */
  377. }
  378. luaL_argcheck(L, validproxy, 1, "boolean or proxy expected");
  379. lua_getmetatable(L, 1); /* metatable is valid; get it */
  380. }
  381. lua_setmetatable(L, 2);
  382. return 1;
  383. }
  384. /*
  385. ** {======================================================
  386. ** `require' function
  387. ** =======================================================
  388. */
  389. static const char *getpath (lua_State *L) {
  390. const char *path;
  391. lua_getglobal(L, LUA_PATH); /* try global variable */
  392. path = lua_tostring(L, -1);
  393. lua_pop(L, 1);
  394. if (path) return path;
  395. path = getenv(LUA_PATH); /* else try environment variable */
  396. if (path) return path;
  397. return LUA_PATH_DEFAULT; /* else use default */
  398. }
  399. static const char *pushnextpath (lua_State *L, const char *path) {
  400. const char *l;
  401. if (*path == '\0') return NULL; /* no more paths */
  402. if (*path == LUA_PATH_SEP) path++; /* skip separator */
  403. l = strchr(path, LUA_PATH_SEP); /* find next separator */
  404. if (l == NULL) l = path+strlen(path);
  405. lua_pushlstring(L, path, l - path); /* directory name */
  406. return l;
  407. }
  408. static void pushcomposename (lua_State *L) {
  409. const char *path = lua_tostring(L, -1);
  410. const char *wild;
  411. int n = 1;
  412. while ((wild = strchr(path, LUA_PATH_MARK)) != NULL) {
  413. /* is there stack space for prefix, name, and eventual last suffix? */
  414. luaL_checkstack(L, 3, "too many marks in a path component");
  415. lua_pushlstring(L, path, wild - path); /* push prefix */
  416. lua_pushvalue(L, 1); /* push package name (in place of MARK) */
  417. path = wild + 1; /* continue after MARK */
  418. n += 2;
  419. }
  420. lua_pushstring(L, path); /* push last suffix (`n' already includes this) */
  421. lua_concat(L, n);
  422. }
  423. static int luaB_require (lua_State *L) {
  424. const char *path;
  425. int status = LUA_ERRFILE; /* not found (yet) */
  426. luaL_checkstring(L, 1);
  427. lua_settop(L, 1);
  428. lua_getglobal(L, REQTAB);
  429. if (!lua_istable(L, 2)) return luaL_error(L, "`" REQTAB "' is not a table");
  430. path = getpath(L);
  431. lua_pushvalue(L, 1); /* check package's name in book-keeping table */
  432. lua_rawget(L, 2);
  433. if (lua_toboolean(L, -1)) /* is it there? */
  434. return 1; /* package is already loaded; return its result */
  435. else { /* must load it */
  436. while (status == LUA_ERRFILE) {
  437. lua_settop(L, 3); /* reset stack position */
  438. if ((path = pushnextpath(L, path)) == NULL) break;
  439. pushcomposename(L);
  440. status = luaL_loadfile(L, lua_tostring(L, -1)); /* try to load it */
  441. }
  442. }
  443. switch (status) {
  444. case 0: {
  445. lua_getglobal(L, "_REQUIREDNAME"); /* save previous name */
  446. lua_insert(L, -2); /* put it below function */
  447. lua_pushvalue(L, 1);
  448. lua_setglobal(L, "_REQUIREDNAME"); /* set new name */
  449. lua_call(L, 0, 1); /* run loaded module */
  450. lua_insert(L, -2); /* put result below previous name */
  451. lua_setglobal(L, "_REQUIREDNAME"); /* reset to previous name */
  452. if (lua_isnil(L, -1)) { /* no/nil return? */
  453. lua_pushboolean(L, 1);
  454. lua_replace(L, -2); /* replace to true */
  455. }
  456. lua_pushvalue(L, 1);
  457. lua_pushvalue(L, -2);
  458. lua_rawset(L, 2); /* mark it as loaded */
  459. return 1; /* return value */
  460. }
  461. case LUA_ERRFILE: { /* file not found */
  462. return luaL_error(L, "could not load package `%s' from path `%s'",
  463. lua_tostring(L, 1), getpath(L));
  464. }
  465. default: {
  466. return luaL_error(L, "error loading package `%s' (%s)",
  467. lua_tostring(L, 1), lua_tostring(L, -1));
  468. }
  469. }
  470. }
  471. /* }====================================================== */
  472. static const luaL_reg base_funcs[] = {
  473. {"error", luaB_error},
  474. {"getmetatable", luaB_getmetatable},
  475. {"setmetatable", luaB_setmetatable},
  476. {"getfenv", luaB_getfenv},
  477. {"setfenv", luaB_setfenv},
  478. {"next", luaB_next},
  479. {"print", luaB_print},
  480. {"tonumber", luaB_tonumber},
  481. {"tostring", luaB_tostring},
  482. {"type", luaB_type},
  483. {"assert", luaB_assert},
  484. {"unpack", luaB_unpack},
  485. {"select", luaB_select},
  486. {"rawequal", luaB_rawequal},
  487. {"rawget", luaB_rawget},
  488. {"rawset", luaB_rawset},
  489. {"pcall", luaB_pcall},
  490. {"xpcall", luaB_xpcall},
  491. {"collectgarbage", luaB_collectgarbage},
  492. {"gcinfo", luaB_gcinfo},
  493. {"loadfile", luaB_loadfile},
  494. {"dofile", luaB_dofile},
  495. {"loadstring", luaB_loadstring},
  496. {"load", luaB_load},
  497. {"require", luaB_require},
  498. {NULL, NULL}
  499. };
  500. /*
  501. ** {======================================================
  502. ** Coroutine library
  503. ** =======================================================
  504. */
  505. static int auxresume (lua_State *L, lua_State *co, int narg) {
  506. int status;
  507. if (!lua_checkstack(co, narg))
  508. luaL_error(L, "too many arguments to resume");
  509. lua_xmove(L, co, narg);
  510. status = lua_resume(co, narg);
  511. if (status == 0) {
  512. int nres = lua_gettop(co);
  513. if (!lua_checkstack(L, nres))
  514. luaL_error(L, "too many results to resume");
  515. lua_xmove(co, L, nres); /* move yielded values */
  516. return nres;
  517. }
  518. else {
  519. lua_xmove(co, L, 1); /* move error message */
  520. return -1; /* error flag */
  521. }
  522. }
  523. static int luaB_coresume (lua_State *L) {
  524. lua_State *co = lua_tothread(L, 1);
  525. int r;
  526. luaL_argcheck(L, co, 1, "coroutine expected");
  527. r = auxresume(L, co, lua_gettop(L) - 1);
  528. if (r < 0) {
  529. lua_pushboolean(L, 0);
  530. lua_insert(L, -2);
  531. return 2; /* return false + error message */
  532. }
  533. else {
  534. lua_pushboolean(L, 1);
  535. lua_insert(L, -(r + 1));
  536. return r + 1; /* return true + `resume' returns */
  537. }
  538. }
  539. static int luaB_auxwrap (lua_State *L) {
  540. lua_State *co = lua_tothread(L, lua_upvalueindex(1));
  541. int r = auxresume(L, co, lua_gettop(L));
  542. if (r < 0) {
  543. if (lua_isstring(L, -1)) { /* error object is a string? */
  544. luaL_where(L, 1); /* add extra info */
  545. lua_insert(L, -2);
  546. lua_concat(L, 2);
  547. }
  548. lua_error(L); /* propagate error */
  549. }
  550. return r;
  551. }
  552. static int luaB_cocreate (lua_State *L) {
  553. lua_State *NL = lua_newthread(L);
  554. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
  555. "Lua function expected");
  556. lua_pushvalue(L, 1); /* move function to top */
  557. lua_xmove(L, NL, 1); /* move function from L to NL */
  558. return 1;
  559. }
  560. static int luaB_cowrap (lua_State *L) {
  561. luaB_cocreate(L);
  562. lua_pushcclosure(L, luaB_auxwrap, 1);
  563. return 1;
  564. }
  565. static int luaB_yield (lua_State *L) {
  566. return lua_yield(L, lua_gettop(L));
  567. }
  568. static int luaB_costatus (lua_State *L) {
  569. lua_State *co = lua_tothread(L, 1);
  570. luaL_argcheck(L, co, 1, "coroutine expected");
  571. if (L == co) lua_pushliteral(L, "running");
  572. else {
  573. lua_Debug ar;
  574. if (lua_getstack(co, 0, &ar) == 0 && lua_gettop(co) == 0)
  575. lua_pushliteral(L, "dead");
  576. else
  577. lua_pushliteral(L, "suspended");
  578. }
  579. return 1;
  580. }
  581. static const luaL_reg co_funcs[] = {
  582. {"create", luaB_cocreate},
  583. {"wrap", luaB_cowrap},
  584. {"resume", luaB_coresume},
  585. {"yield", luaB_yield},
  586. {"status", luaB_costatus},
  587. {NULL, NULL}
  588. };
  589. /* }====================================================== */
  590. static void auxopen (lua_State *L, const char *name,
  591. lua_CFunction f, lua_CFunction u) {
  592. lua_pushcfunction(L, u);
  593. lua_pushcclosure(L, f, 1);
  594. lua_setfield(L, -2, name);
  595. }
  596. static void base_open (lua_State *L) {
  597. lua_pushvalue(L, LUA_GLOBALSINDEX);
  598. luaL_openlib(L, NULL, base_funcs, 0); /* open lib into global table */
  599. lua_pushliteral(L, LUA_VERSION);
  600. lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */
  601. /* `ipairs' and `pairs' need auxiliary functions as upvalues */
  602. auxopen(L, "ipairs", luaB_ipairs, ipairsaux);
  603. auxopen(L, "pairs", luaB_pairs, luaB_next);
  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. }