lbaselib.c 18 KB

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