lbaselib.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. /*
  2. ** $Id: lbaselib.c,v 1.158 2004/09/15 20:39:42 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. return 0;
  122. }
  123. else if (lua_setfenv(L, -2) == 0)
  124. luaL_error(L, "`setfenv' cannot change environment of given function");
  125. return 1;
  126. }
  127. static int luaB_rawequal (lua_State *L) {
  128. luaL_checkany(L, 1);
  129. luaL_checkany(L, 2);
  130. lua_pushboolean(L, lua_rawequal(L, 1, 2));
  131. return 1;
  132. }
  133. static int luaB_rawget (lua_State *L) {
  134. luaL_checktype(L, 1, LUA_TTABLE);
  135. luaL_checkany(L, 2);
  136. lua_rawget(L, 1);
  137. return 1;
  138. }
  139. static int luaB_rawset (lua_State *L) {
  140. luaL_checktype(L, 1, LUA_TTABLE);
  141. luaL_checkany(L, 2);
  142. luaL_checkany(L, 3);
  143. lua_rawset(L, 1);
  144. return 1;
  145. }
  146. static int luaB_gcinfo (lua_State *L) {
  147. lua_pushinteger(L, lua_getgccount(L));
  148. return 1;
  149. }
  150. static int luaB_collectgarbage (lua_State *L) {
  151. static const char *const opts[] = {"stop", "restart", "collect", "count",
  152. "step", NULL};
  153. static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART,
  154. LUA_GCCOLLECT, LUA_GCCOUNT, LUA_GCSTEP};
  155. int o = luaL_findstring(luaL_optstring(L, 1, "collect"), opts);
  156. int ex = luaL_optint(L, 2, 0);
  157. luaL_argcheck(L, o >= 0, 1, "invalid option");
  158. lua_pushinteger(L, lua_gc(L, optsnum[o], ex));
  159. return 1;
  160. }
  161. static int luaB_type (lua_State *L) {
  162. luaL_checkany(L, 1);
  163. lua_pushstring(L, luaL_typename(L, 1));
  164. return 1;
  165. }
  166. static int luaB_next (lua_State *L) {
  167. luaL_checktype(L, 1, LUA_TTABLE);
  168. lua_settop(L, 2); /* create a 2nd argument if there isn't one */
  169. if (lua_next(L, 1))
  170. return 2;
  171. else {
  172. lua_pushnil(L);
  173. return 1;
  174. }
  175. }
  176. static int luaB_pairs (lua_State *L) {
  177. luaL_checktype(L, 1, LUA_TTABLE);
  178. lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
  179. lua_pushvalue(L, 1); /* state, */
  180. lua_pushnil(L); /* and initial value */
  181. return 3;
  182. }
  183. static int ipairsaux (lua_State *L) {
  184. int i = luaL_checkint(L, 2);
  185. luaL_checktype(L, 1, LUA_TTABLE);
  186. i++; /* next value */
  187. lua_pushinteger(L, i);
  188. lua_rawgeti(L, 1, i);
  189. return (lua_isnil(L, -1)) ? 0 : 2;
  190. }
  191. static int luaB_ipairs (lua_State *L) {
  192. luaL_checktype(L, 1, LUA_TTABLE);
  193. lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
  194. lua_pushvalue(L, 1); /* state, */
  195. lua_pushinteger(L, LUA_FIRSTINDEX - 1); /* and initial value */
  196. return 3;
  197. }
  198. static int load_aux (lua_State *L, int status) {
  199. if (status == 0) /* OK? */
  200. return 1;
  201. else {
  202. lua_pushnil(L);
  203. lua_insert(L, -2); /* put before error message */
  204. return 2; /* return nil plus error message */
  205. }
  206. }
  207. static int luaB_loadstring (lua_State *L) {
  208. size_t l;
  209. const char *s = luaL_checklstring(L, 1, &l);
  210. const char *chunkname = luaL_optstring(L, 2, s);
  211. return load_aux(L, luaL_loadbuffer(L, s, l, chunkname));
  212. }
  213. static int luaB_loadfile (lua_State *L) {
  214. const char *fname = luaL_optstring(L, 1, NULL);
  215. const char *path = luaL_optstring(L, 2, NULL);
  216. int status;
  217. if (path == NULL)
  218. status = luaL_loadfile(L, fname);
  219. else {
  220. fname = luaL_searchpath(L, fname, path);
  221. status = (fname) ? luaL_loadfile(L, fname) : 1;
  222. }
  223. return load_aux(L, status);
  224. }
  225. /*
  226. ** Reader for generic `load' function: `lua_load' uses the
  227. ** stack for internal stuff, so the reader cannot change the
  228. ** stack top. Instead, it keeps its resulting string in a
  229. ** reserved slot inside the stack.
  230. */
  231. static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
  232. luaL_checkstack(L, 2, "too many nested functions");
  233. lua_pushvalue(L, 1); /* get function */
  234. lua_call(L, 0, 1); /* call it */
  235. if (lua_isnil(L, -1)) {
  236. *size = 0;
  237. return NULL;
  238. }
  239. else if (lua_isstring(L, -1)) {
  240. const char *res;
  241. lua_replace(L, 3); /* save string in a reserved stack slot */
  242. res = lua_tostring(L, 3);
  243. *size = lua_strlen(L, 3);
  244. return res;
  245. }
  246. else luaL_error(L, "reader function must return a string");
  247. return NULL; /* to avoid warnings */
  248. }
  249. static int luaB_load (lua_State *L) {
  250. int status;
  251. const char *cname = luaL_optstring(L, 2, "=(load)");
  252. luaL_checktype(L, 1, LUA_TFUNCTION);
  253. lua_settop(L, 3); /* function, eventual name, plus one reserved slot */
  254. status = lua_load(L, generic_reader, NULL, cname);
  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 n = lua_gettop(L);
  260. if (luaL_loadfile(L, fname) != 0) lua_error(L);
  261. lua_call(L, 0, LUA_MULTRET);
  262. return lua_gettop(L) - n;
  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. return lua_gettop(L);
  269. }
  270. static int luaB_unpack (lua_State *L) {
  271. int i = luaL_optint(L, 2, LUA_FIRSTINDEX);
  272. int e = luaL_optint(L, 3, -1);
  273. int n;
  274. luaL_checktype(L, 1, LUA_TTABLE);
  275. if (e == -1)
  276. e = luaL_getn(L, 1) + LUA_FIRSTINDEX - 1;
  277. n = e - i + 1; /* number of elements */
  278. if (n <= 0) return 0; /* empty range */
  279. luaL_checkstack(L, n, "table too big to unpack");
  280. for (; i<=e; i++) /* push arg[i...e] */
  281. lua_rawgeti(L, 1, i);
  282. return n;
  283. }
  284. static int luaB_select (lua_State *L) {
  285. int n = lua_gettop(L);
  286. if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') {
  287. lua_pushinteger(L, n-1);
  288. return 1;
  289. }
  290. else {
  291. int i = luaL_checkint(L, 1);
  292. if (i <= 0) i = 1;
  293. else if (i >= n) i = n;
  294. return n - i;
  295. }
  296. }
  297. static int luaB_pcall (lua_State *L) {
  298. int status;
  299. luaL_checkany(L, 1);
  300. status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0);
  301. lua_pushboolean(L, (status == 0));
  302. lua_insert(L, 1);
  303. return lua_gettop(L); /* return status + all results */
  304. }
  305. static int luaB_xpcall (lua_State *L) {
  306. int status;
  307. luaL_checkany(L, 2);
  308. lua_settop(L, 2);
  309. lua_insert(L, 1); /* put error function under function to be called */
  310. status = lua_pcall(L, 0, LUA_MULTRET, 1);
  311. lua_pushboolean(L, (status == 0));
  312. lua_replace(L, 1);
  313. return lua_gettop(L); /* return status + all results */
  314. }
  315. static int luaB_tostring (lua_State *L) {
  316. luaL_checkany(L, 1);
  317. if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */
  318. return 1; /* use its value */
  319. switch (lua_type(L, 1)) {
  320. case LUA_TNUMBER:
  321. lua_pushstring(L, lua_tostring(L, 1));
  322. break;
  323. case LUA_TSTRING:
  324. lua_pushvalue(L, 1);
  325. break;
  326. case LUA_TBOOLEAN:
  327. lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
  328. break;
  329. case LUA_TNIL:
  330. lua_pushliteral(L, "nil");
  331. break;
  332. case LUA_TTABLE:
  333. lua_pushfstring(L, "table: %p", lua_topointer(L, 1));
  334. break;
  335. case LUA_TFUNCTION:
  336. lua_pushfstring(L, "function: %p", lua_topointer(L, 1));
  337. break;
  338. case LUA_TUSERDATA:
  339. case LUA_TLIGHTUSERDATA:
  340. lua_pushfstring(L, "userdata: %p", lua_topointer(L, 1));
  341. break;
  342. case LUA_TTHREAD:
  343. lua_pushfstring(L, "thread: %p", lua_topointer(L, 1));
  344. break;
  345. }
  346. return 1;
  347. }
  348. static int luaB_newproxy (lua_State *L) {
  349. lua_settop(L, 1);
  350. lua_newuserdata(L, 0); /* create proxy */
  351. if (lua_toboolean(L, 1) == 0)
  352. return 1; /* no metatable */
  353. else if (lua_isboolean(L, 1)) {
  354. lua_newtable(L); /* create a new metatable `m' ... */
  355. lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */
  356. lua_pushboolean(L, 1);
  357. lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */
  358. }
  359. else {
  360. int validproxy = 0; /* to check if weaktable[metatable(u)] == true */
  361. if (lua_getmetatable(L, 1)) {
  362. lua_rawget(L, lua_upvalueindex(1));
  363. validproxy = lua_toboolean(L, -1);
  364. lua_pop(L, 1); /* remove value */
  365. }
  366. luaL_argcheck(L, validproxy, 1, "boolean or proxy expected");
  367. lua_getmetatable(L, 1); /* metatable is valid; get it */
  368. }
  369. lua_setmetatable(L, 2);
  370. return 1;
  371. }
  372. /*
  373. ** {======================================================
  374. ** `require' function
  375. ** =======================================================
  376. */
  377. static const char *getpath (lua_State *L) {
  378. /* try first `LUA_PATH' for compatibility */
  379. lua_getfield(L, LUA_GLOBALSINDEX, "LUA_PATH");
  380. if (!lua_isstring(L, -1)) {
  381. lua_pop(L, 1);
  382. lua_getfield(L, LUA_GLOBALSINDEX, "_PATH");
  383. }
  384. if (!lua_isstring(L, -1))
  385. luaL_error(L, "global _PATH must be a string");
  386. return lua_tostring(L, -1);
  387. }
  388. static int luaB_require (lua_State *L) {
  389. const char *name = luaL_checkstring(L, 1);
  390. const char *fname;
  391. lua_settop(L, 1);
  392. lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
  393. lua_getfield(L, 2, name);
  394. if (lua_toboolean(L, -1)) /* is it there? */
  395. return 1; /* package is already loaded; return its result */
  396. /* else must load it; first mark it as loaded */
  397. lua_pushboolean(L, 1);
  398. lua_setfield(L, 2, name); /* _LOADED[name] = true */
  399. fname = luaL_gsub(L, name, ".", LUA_DIRSEP);
  400. fname = luaL_searchpath(L, fname, getpath(L));
  401. if (fname == NULL || luaL_loadfile(L, fname) != 0)
  402. return luaL_error(L, "error loading package `%s' (%s)", name,
  403. lua_tostring(L, -1));
  404. lua_pushvalue(L, 1); /* pass name as argument to module */
  405. lua_call(L, 1, 1); /* run loaded module */
  406. if (!lua_isnil(L, -1)) /* nil return? */
  407. lua_setfield(L, 2, name);
  408. lua_getfield(L, 2, name); /* return _LOADED[name] */
  409. return 1;
  410. }
  411. static void setfenv (lua_State *L) {
  412. lua_Debug ar;
  413. lua_getstack(L, 1, &ar);
  414. lua_getinfo(L, "f", &ar);
  415. lua_pushvalue(L, -2);
  416. lua_setfenv(L, -2);
  417. }
  418. static int luaB_module (lua_State *L) {
  419. const char *modname = luaL_checkstring(L, 1);
  420. const char *dot;
  421. lua_settop(L, 1);
  422. lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
  423. /* try to find given table */
  424. luaL_getfield(L, LUA_GLOBALSINDEX, modname);
  425. if (lua_isnil(L, -1)) { /* not found? */
  426. lua_pop(L, 1); /* remove previous result */
  427. lua_newtable(L); /* create it */
  428. /* register it with given name */
  429. lua_pushvalue(L, -1);
  430. luaL_setfield(L, LUA_GLOBALSINDEX, modname);
  431. }
  432. else if (!lua_istable(L, -1))
  433. return luaL_error(L, "name conflict for module `%s'", modname);
  434. /* check whether table already has a _NAME field */
  435. lua_getfield(L, -1, "_NAME");
  436. if (!lua_isnil(L, -1)) /* is table an initialized module? */
  437. lua_pop(L, 1);
  438. else { /* no; initialize it */
  439. lua_pop(L, 1);
  440. lua_newtable(L); /* create new metatable */
  441. lua_pushvalue(L, LUA_GLOBALSINDEX);
  442. lua_setfield(L, -2, "__index"); /* mt.__index = _G */
  443. lua_setmetatable(L, -2);
  444. lua_pushstring(L, modname);
  445. lua_setfield(L, -2, "_NAME");
  446. dot = strrchr(modname, '.'); /* look for last dot in module name */
  447. if (dot == NULL) dot = modname;
  448. else dot++;
  449. /* set _PACK as package name (full module name minus last part) */
  450. lua_pushlstring(L, modname, dot - modname);
  451. lua_setfield(L, -2, "_PACK");
  452. }
  453. lua_pushvalue(L, -1);
  454. lua_setfield(L, 2, modname); /* _LOADED[modname] = new table */
  455. setfenv(L);
  456. return 0;
  457. }
  458. /* }====================================================== */
  459. static const luaL_reg base_funcs[] = {
  460. {"error", luaB_error},
  461. {"getmetatable", luaB_getmetatable},
  462. {"setmetatable", luaB_setmetatable},
  463. {"getfenv", luaB_getfenv},
  464. {"setfenv", luaB_setfenv},
  465. {"next", luaB_next},
  466. {"print", luaB_print},
  467. {"tonumber", luaB_tonumber},
  468. {"tostring", luaB_tostring},
  469. {"type", luaB_type},
  470. {"assert", luaB_assert},
  471. {"unpack", luaB_unpack},
  472. {"select", luaB_select},
  473. {"rawequal", luaB_rawequal},
  474. {"rawget", luaB_rawget},
  475. {"rawset", luaB_rawset},
  476. {"pcall", luaB_pcall},
  477. {"xpcall", luaB_xpcall},
  478. {"collectgarbage", luaB_collectgarbage},
  479. {"gcinfo", luaB_gcinfo},
  480. {"loadfile", luaB_loadfile},
  481. {"dofile", luaB_dofile},
  482. {"loadstring", luaB_loadstring},
  483. {"load", luaB_load},
  484. {"require", luaB_require},
  485. {"module", luaB_module},
  486. {NULL, NULL}
  487. };
  488. /*
  489. ** {======================================================
  490. ** Coroutine library
  491. ** =======================================================
  492. */
  493. static int auxresume (lua_State *L, lua_State *co, int narg) {
  494. int status;
  495. if (!lua_checkstack(co, narg))
  496. luaL_error(L, "too many arguments to resume");
  497. if (lua_threadstatus(co) == 0 && lua_gettop(co) == 0) {
  498. lua_pushliteral(L, "cannot resume dead coroutine");
  499. return -1; /* error flag */
  500. }
  501. lua_xmove(L, co, narg);
  502. status = lua_resume(co, narg);
  503. if (status == 0 || status == LUA_YIELD) {
  504. int nres = lua_gettop(co);
  505. if (!lua_checkstack(L, nres))
  506. luaL_error(L, "too many results to resume");
  507. lua_xmove(co, L, nres); /* move yielded values */
  508. return nres;
  509. }
  510. else {
  511. lua_xmove(co, L, 1); /* move error message */
  512. return -1; /* error flag */
  513. }
  514. }
  515. static int luaB_coresume (lua_State *L) {
  516. lua_State *co = lua_tothread(L, 1);
  517. int r;
  518. luaL_argcheck(L, co, 1, "coroutine expected");
  519. r = auxresume(L, co, lua_gettop(L) - 1);
  520. if (r < 0) {
  521. lua_pushboolean(L, 0);
  522. lua_insert(L, -2);
  523. return 2; /* return false + error message */
  524. }
  525. else {
  526. lua_pushboolean(L, 1);
  527. lua_insert(L, -(r + 1));
  528. return r + 1; /* return true + `resume' returns */
  529. }
  530. }
  531. static int luaB_auxwrap (lua_State *L) {
  532. lua_State *co = lua_tothread(L, lua_upvalueindex(1));
  533. int r = auxresume(L, co, lua_gettop(L));
  534. if (r < 0) {
  535. if (lua_isstring(L, -1)) { /* error object is a string? */
  536. luaL_where(L, 1); /* add extra info */
  537. lua_insert(L, -2);
  538. lua_concat(L, 2);
  539. }
  540. lua_error(L); /* propagate error */
  541. }
  542. return r;
  543. }
  544. static int luaB_cocreate (lua_State *L) {
  545. lua_State *NL = lua_newthread(L);
  546. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
  547. "Lua function expected");
  548. lua_pushvalue(L, 1); /* move function to top */
  549. lua_xmove(L, NL, 1); /* move function from L to NL */
  550. return 1;
  551. }
  552. static int luaB_cowrap (lua_State *L) {
  553. luaB_cocreate(L);
  554. lua_pushcclosure(L, luaB_auxwrap, 1);
  555. return 1;
  556. }
  557. static int luaB_yield (lua_State *L) {
  558. return lua_yield(L, lua_gettop(L));
  559. }
  560. static int luaB_costatus (lua_State *L) {
  561. lua_State *co = lua_tothread(L, 1);
  562. luaL_argcheck(L, co, 1, "coroutine expected");
  563. if (L == co) lua_pushliteral(L, "running");
  564. else {
  565. switch (lua_threadstatus(co)) {
  566. case LUA_YIELD:
  567. lua_pushliteral(L, "suspended");
  568. break;
  569. case 0: {
  570. lua_Debug ar;
  571. if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */
  572. lua_pushliteral(L, "normal"); /* it is running */
  573. else if (lua_gettop(co) == 0)
  574. lua_pushliteral(L, "dead");
  575. else
  576. lua_pushliteral(L, "suspended"); /* initial state */
  577. break;
  578. }
  579. default: /* some error occured */
  580. lua_pushliteral(L, "dead");
  581. break;
  582. }
  583. }
  584. return 1;
  585. }
  586. static int luaB_cocurrent (lua_State *L) {
  587. if (lua_pushthread(L))
  588. return 0; /* main thread is not a coroutine */
  589. else
  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. {"current", luaB_cocurrent},
  599. {NULL, NULL}
  600. };
  601. /* }====================================================== */
  602. static void auxopen (lua_State *L, const char *name,
  603. lua_CFunction f, lua_CFunction u) {
  604. lua_pushcfunction(L, u);
  605. lua_pushcclosure(L, f, 1);
  606. lua_setfield(L, -2, name);
  607. }
  608. static void base_open (lua_State *L) {
  609. const char *path;
  610. lua_pushvalue(L, LUA_GLOBALSINDEX);
  611. luaL_openlib(L, NULL, base_funcs, 0); /* open lib into global table */
  612. lua_pushliteral(L, LUA_VERSION);
  613. lua_setfield(L, LUA_GLOBALSINDEX, "_VERSION"); /* set global _VERSION */
  614. /* `ipairs' and `pairs' need auxiliary functions as upvalues */
  615. auxopen(L, "ipairs", luaB_ipairs, ipairsaux);
  616. auxopen(L, "pairs", luaB_pairs, luaB_next);
  617. /* `newproxy' needs a weaktable as upvalue */
  618. lua_newtable(L); /* new table `w' */
  619. lua_pushvalue(L, -1); /* `w' will be its own metatable */
  620. lua_setmetatable(L, -2);
  621. lua_pushliteral(L, "kv");
  622. lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */
  623. lua_pushcclosure(L, luaB_newproxy, 1);
  624. lua_setfield(L, LUA_GLOBALSINDEX, "newproxy"); /* set global `newproxy' */
  625. /* `require' needs a table to keep loaded chunks */
  626. lua_newtable(L);
  627. lua_pushvalue(L, -1);
  628. lua_setglobal(L, "_LOADED");
  629. lua_setfield(L, LUA_REGISTRYINDEX, "_LOADED");
  630. /* set global _G */
  631. lua_pushvalue(L, LUA_GLOBALSINDEX);
  632. lua_setfield(L, LUA_GLOBALSINDEX, "_G");
  633. /* set global _PATH */
  634. path = getenv(LUA_PATH);
  635. if (path == NULL) path = LUA_PATH_DEFAULT;
  636. lua_pushstring(L, path);
  637. lua_setfield(L, LUA_GLOBALSINDEX, "_PATH");
  638. }
  639. LUALIB_API int luaopen_base (lua_State *L) {
  640. base_open(L);
  641. luaL_openlib(L, LUA_COLIBNAME, co_funcs, 0);
  642. return 2;
  643. }