lbaselib.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /*
  2. ** $Id: lbaselib.c,v 1.191.1.6 2008/02/14 16:46:22 roberto Exp $
  3. ** Basic library
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lbaselib_c
  7. #define LUA_LIB
  8. #define LUAC_CROSS_FILE
  9. #include "lua.h"
  10. #include C_HEADER_STDIO
  11. #include C_HEADER_STRING
  12. #include C_HEADER_STDLIB
  13. #include "lauxlib.h"
  14. #include "lualib.h"
  15. #include "lrotable.h"
  16. /*
  17. ** If your system does not support `stdout', you can just remove this function.
  18. ** If you need, you can define your own `print' function, following this
  19. ** model but changing `fputs' to put the strings at a proper place
  20. ** (a console window or a log file, for instance).
  21. */
  22. static int luaB_print (lua_State *L) {
  23. int n = lua_gettop(L); /* number of arguments */
  24. int i;
  25. lua_getglobal(L, "tostring");
  26. for (i=1; i<=n; i++) {
  27. const char *s;
  28. lua_pushvalue(L, -1); /* function to be called */
  29. lua_pushvalue(L, i); /* value to print */
  30. lua_call(L, 1, 1);
  31. s = lua_tostring(L, -1); /* get result */
  32. if (s == NULL)
  33. return luaL_error(L, LUA_QL("tostring") " must return a string to "
  34. LUA_QL("print"));
  35. #if defined(LUA_USE_STDIO)
  36. if (i>1) c_fputs("\t", c_stdout);
  37. c_fputs(s, c_stdout);
  38. #else
  39. if (i>1) luai_writestring("\t", 1);
  40. luai_writestring(s, c_strlen(s));
  41. #endif
  42. lua_pop(L, 1); /* pop result */
  43. }
  44. #if defined(LUA_USE_STDIO)
  45. c_fputs("\n", c_stdout);
  46. #else
  47. luai_writeline();
  48. #endif
  49. return 0;
  50. }
  51. static int luaB_tonumber (lua_State *L) {
  52. int base = luaL_optint(L, 2, 10);
  53. if (base == 10) { /* standard conversion */
  54. luaL_checkany(L, 1);
  55. if (lua_isnumber(L, 1)) {
  56. lua_pushnumber(L, lua_tonumber(L, 1));
  57. return 1;
  58. }
  59. }
  60. else {
  61. const char *s1 = luaL_checkstring(L, 1);
  62. char *s2;
  63. unsigned long n;
  64. luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
  65. n = c_strtoul(s1, &s2, base);
  66. if (s1 != s2) { /* at least one valid digit? */
  67. while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
  68. if (*s2 == '\0') { /* no invalid trailing characters? */
  69. lua_pushnumber(L, (lua_Number)n);
  70. return 1;
  71. }
  72. }
  73. }
  74. lua_pushnil(L); /* else not a number */
  75. return 1;
  76. }
  77. static int luaB_error (lua_State *L) {
  78. int level = luaL_optint(L, 2, 1);
  79. lua_settop(L, 1);
  80. if (lua_isstring(L, 1) && level > 0) { /* add extra information? */
  81. luaL_where(L, level);
  82. lua_pushvalue(L, 1);
  83. lua_concat(L, 2);
  84. }
  85. return lua_error(L);
  86. }
  87. static int luaB_getmetatable (lua_State *L) {
  88. luaL_checkany(L, 1);
  89. if (!lua_getmetatable(L, 1)) {
  90. lua_pushnil(L);
  91. return 1; /* no metatable */
  92. }
  93. luaL_getmetafield(L, 1, "__metatable");
  94. return 1; /* returns either __metatable field (if present) or metatable */
  95. }
  96. static int luaB_setmetatable (lua_State *L) {
  97. int t = lua_type(L, 2);
  98. luaL_checktype(L, 1, LUA_TTABLE);
  99. luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE || t == LUA_TROTABLE, 2,
  100. "nil or table expected");
  101. if (luaL_getmetafield(L, 1, "__metatable"))
  102. luaL_error(L, "cannot change a protected metatable");
  103. lua_settop(L, 2);
  104. lua_setmetatable(L, 1);
  105. return 1;
  106. }
  107. static void getfunc (lua_State *L, int opt) {
  108. if (lua_isfunction(L, 1)) lua_pushvalue(L, 1);
  109. else {
  110. lua_Debug ar;
  111. int level = opt ? luaL_optint(L, 1, 1) : luaL_checkint(L, 1);
  112. luaL_argcheck(L, level >= 0, 1, "level must be non-negative");
  113. if (lua_getstack(L, level, &ar) == 0)
  114. luaL_argerror(L, 1, "invalid level");
  115. lua_getinfo(L, "f", &ar);
  116. if (lua_isnil(L, -1))
  117. luaL_error(L, "no function environment for tail call at level %d",
  118. level);
  119. }
  120. }
  121. static int luaB_getfenv (lua_State *L) {
  122. getfunc(L, 1);
  123. if (lua_iscfunction(L, -1)) /* is a C function? */
  124. lua_pushvalue(L, LUA_GLOBALSINDEX); /* return the thread's global env. */
  125. else
  126. lua_getfenv(L, -1);
  127. return 1;
  128. }
  129. static int luaB_setfenv (lua_State *L) {
  130. luaL_checktype(L, 2, LUA_TTABLE);
  131. getfunc(L, 0);
  132. lua_pushvalue(L, 2);
  133. if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) {
  134. /* change environment of current thread */
  135. lua_pushthread(L);
  136. lua_insert(L, -2);
  137. lua_setfenv(L, -2);
  138. return 0;
  139. }
  140. else if (lua_iscfunction(L, -2) || lua_setfenv(L, -2) == 0)
  141. luaL_error(L,
  142. LUA_QL("setfenv") " cannot change environment of given object");
  143. return 1;
  144. }
  145. static int luaB_rawequal (lua_State *L) {
  146. luaL_checkany(L, 1);
  147. luaL_checkany(L, 2);
  148. lua_pushboolean(L, lua_rawequal(L, 1, 2));
  149. return 1;
  150. }
  151. static int luaB_rawget (lua_State *L) {
  152. luaL_checkanytable(L, 1);
  153. luaL_checkany(L, 2);
  154. lua_settop(L, 2);
  155. lua_rawget(L, 1);
  156. return 1;
  157. }
  158. static int luaB_rawset (lua_State *L) {
  159. luaL_checktype(L, 1, LUA_TTABLE);
  160. luaL_checkany(L, 2);
  161. luaL_checkany(L, 3);
  162. lua_settop(L, 3);
  163. lua_rawset(L, 1);
  164. return 1;
  165. }
  166. static int luaB_gcinfo (lua_State *L) {
  167. lua_pushinteger(L, lua_getgccount(L));
  168. return 1;
  169. }
  170. static int luaB_collectgarbage (lua_State *L) {
  171. static const char *const opts[] = {"stop", "restart", "collect",
  172. "count", "step", "setpause", "setstepmul","setmemlimit","getmemlimit", NULL};
  173. static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
  174. LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL,
  175. LUA_GCSETMEMLIMIT,LUA_GCGETMEMLIMIT};
  176. int o = luaL_checkoption(L, 1, "collect", opts);
  177. int ex = luaL_optint(L, 2, 0);
  178. int res = lua_gc(L, optsnum[o], ex);
  179. switch (optsnum[o]) {
  180. case LUA_GCCOUNT: {
  181. int b = lua_gc(L, LUA_GCCOUNTB, 0);
  182. lua_pushnumber(L, res + ((lua_Number)b/1024));
  183. return 1;
  184. }
  185. case LUA_GCSTEP: {
  186. lua_pushboolean(L, res);
  187. return 1;
  188. }
  189. default: {
  190. lua_pushnumber(L, res);
  191. return 1;
  192. }
  193. }
  194. }
  195. static int luaB_type (lua_State *L) {
  196. luaL_checkany(L, 1);
  197. lua_pushstring(L, luaL_typename(L, 1));
  198. return 1;
  199. }
  200. static int luaB_next (lua_State *L) {
  201. luaL_checkanytable(L, 1);
  202. lua_settop(L, 2); /* create a 2nd argument if there isn't one */
  203. if (lua_next(L, 1))
  204. return 2;
  205. else {
  206. lua_pushnil(L);
  207. return 1;
  208. }
  209. }
  210. static int luaB_pairs (lua_State *L) {
  211. luaL_checkanytable(L, 1);
  212. lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
  213. lua_pushvalue(L, 1); /* state, */
  214. lua_pushnil(L); /* and initial value */
  215. return 3;
  216. }
  217. static int ipairsaux (lua_State *L) {
  218. int i = luaL_checkint(L, 2);
  219. luaL_checkanytable(L, 1);
  220. i++; /* next value */
  221. lua_pushinteger(L, i);
  222. lua_rawgeti(L, 1, i);
  223. return (lua_isnil(L, -1)) ? 0 : 2;
  224. }
  225. static int luaB_ipairs (lua_State *L) {
  226. luaL_checkanytable(L, 1);
  227. lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
  228. lua_pushvalue(L, 1); /* state, */
  229. lua_pushinteger(L, 0); /* and initial value */
  230. return 3;
  231. }
  232. static int load_aux (lua_State *L, int status) {
  233. if (status == 0) /* OK? */
  234. return 1;
  235. else {
  236. lua_pushnil(L);
  237. lua_insert(L, -2); /* put before error message */
  238. return 2; /* return nil plus error message */
  239. }
  240. }
  241. static int luaB_loadstring (lua_State *L) {
  242. size_t l;
  243. const char *s = luaL_checklstring(L, 1, &l);
  244. const char *chunkname = luaL_optstring(L, 2, s);
  245. return load_aux(L, luaL_loadbuffer(L, s, l, chunkname));
  246. }
  247. static int luaB_loadfile (lua_State *L) {
  248. const char *fname = luaL_optstring(L, 1, NULL);
  249. #ifdef LUA_CROSS_COMPILER
  250. return load_aux(L, luaL_loadfile(L, fname));
  251. #else
  252. return load_aux(L, luaL_loadfsfile(L, fname));
  253. #endif
  254. }
  255. /*
  256. ** Reader for generic `load' function: `lua_load' uses the
  257. ** stack for internal stuff, so the reader cannot change the
  258. ** stack top. Instead, it keeps its resulting string in a
  259. ** reserved slot inside the stack.
  260. */
  261. static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
  262. (void)ud; /* to avoid warnings */
  263. if (L == NULL && size == NULL) // direct mode check, doesn't happen
  264. return NULL;
  265. luaL_checkstack(L, 2, "too many nested functions");
  266. lua_pushvalue(L, 1); /* get function */
  267. lua_call(L, 0, 1); /* call it */
  268. if (lua_isnil(L, -1)) {
  269. *size = 0;
  270. return NULL;
  271. }
  272. else if (lua_isstring(L, -1)) {
  273. lua_replace(L, 3); /* save string in a reserved stack slot */
  274. return lua_tolstring(L, 3, size);
  275. }
  276. else luaL_error(L, "reader function must return a string");
  277. return NULL; /* to avoid warnings */
  278. }
  279. static int luaB_load (lua_State *L) {
  280. int status;
  281. const char *cname = luaL_optstring(L, 2, "=(load)");
  282. luaL_checktype(L, 1, LUA_TFUNCTION);
  283. lua_settop(L, 3); /* function, eventual name, plus one reserved slot */
  284. status = lua_load(L, generic_reader, NULL, cname);
  285. return load_aux(L, status);
  286. }
  287. static int luaB_dofile (lua_State *L) {
  288. const char *fname = luaL_optstring(L, 1, NULL);
  289. int n = lua_gettop(L);
  290. #ifdef LUA_CROSS_COMPILER
  291. if (luaL_loadfile(L, fname) != 0) lua_error(L);
  292. #else
  293. if (luaL_loadfsfile(L, fname) != 0) lua_error(L);
  294. #endif
  295. lua_call(L, 0, LUA_MULTRET);
  296. return lua_gettop(L) - n;
  297. }
  298. static int luaB_assert (lua_State *L) {
  299. luaL_checkany(L, 1);
  300. if (!lua_toboolean(L, 1))
  301. return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!"));
  302. return lua_gettop(L);
  303. }
  304. static int luaB_unpack (lua_State *L) {
  305. int i, e, n;
  306. luaL_checktype(L, 1, LUA_TTABLE);
  307. i = luaL_optint(L, 2, 1);
  308. e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1));
  309. if (i > e) return 0; /* empty range */
  310. n = e - i + 1; /* number of elements */
  311. if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */
  312. return luaL_error(L, "too many results to unpack");
  313. lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */
  314. while (i++ < e) /* push arg[i + 1...e] */
  315. lua_rawgeti(L, 1, i);
  316. return n;
  317. }
  318. static int luaB_select (lua_State *L) {
  319. int n = lua_gettop(L);
  320. if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') {
  321. lua_pushinteger(L, n-1);
  322. return 1;
  323. }
  324. else {
  325. int i = luaL_checkint(L, 1);
  326. if (i < 0) i = n + i;
  327. else if (i > n) i = n;
  328. luaL_argcheck(L, 1 <= i, 1, "index out of range");
  329. return n - i;
  330. }
  331. }
  332. static int luaB_pcall (lua_State *L) {
  333. int status;
  334. luaL_checkany(L, 1);
  335. status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0);
  336. lua_pushboolean(L, (status == 0));
  337. lua_insert(L, 1);
  338. return lua_gettop(L); /* return status + all results */
  339. }
  340. static int luaB_xpcall (lua_State *L) {
  341. int status;
  342. luaL_checkany(L, 2);
  343. lua_settop(L, 2);
  344. lua_insert(L, 1); /* put error function under function to be called */
  345. status = lua_pcall(L, 0, LUA_MULTRET, 1);
  346. lua_pushboolean(L, (status == 0));
  347. lua_replace(L, 1);
  348. return lua_gettop(L); /* return status + all results */
  349. }
  350. static int luaB_tostring (lua_State *L) {
  351. luaL_checkany(L, 1);
  352. if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */
  353. return 1; /* use its value */
  354. switch (lua_type(L, 1)) {
  355. case LUA_TNUMBER:
  356. lua_pushstring(L, lua_tostring(L, 1));
  357. break;
  358. case LUA_TSTRING:
  359. lua_pushvalue(L, 1);
  360. break;
  361. case LUA_TBOOLEAN:
  362. lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
  363. break;
  364. case LUA_TNIL:
  365. lua_pushliteral(L, "nil");
  366. break;
  367. default:
  368. lua_pushfstring(L, "%s: %p", luaL_typename(L, 1), lua_topointer(L, 1));
  369. break;
  370. }
  371. return 1;
  372. }
  373. static int luaB_newproxy (lua_State *L) {
  374. lua_settop(L, 1);
  375. lua_newuserdata(L, 0); /* create proxy */
  376. if (lua_toboolean(L, 1) == 0)
  377. return 1; /* no metatable */
  378. else if (lua_isboolean(L, 1)) {
  379. lua_newtable(L); /* create a new metatable `m' ... */
  380. lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */
  381. lua_pushboolean(L, 1);
  382. lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */
  383. }
  384. else {
  385. int validproxy = 0; /* to check if weaktable[metatable(u)] == true */
  386. if (lua_getmetatable(L, 1)) {
  387. lua_rawget(L, lua_upvalueindex(1));
  388. validproxy = lua_toboolean(L, -1);
  389. lua_pop(L, 1); /* remove value */
  390. }
  391. luaL_argcheck(L, validproxy, 1, "boolean or proxy expected");
  392. lua_getmetatable(L, 1); /* metatable is valid; get it */
  393. }
  394. lua_setmetatable(L, 2);
  395. return 1;
  396. }
  397. #define LUA_BASELIB_FUNCLIST\
  398. {LSTRKEY("assert"), LFUNCVAL(luaB_assert)},\
  399. {LSTRKEY("collectgarbage"), LFUNCVAL(luaB_collectgarbage)},\
  400. {LSTRKEY("dofile"), LFUNCVAL(luaB_dofile)},\
  401. {LSTRKEY("error"), LFUNCVAL(luaB_error)},\
  402. {LSTRKEY("gcinfo"), LFUNCVAL(luaB_gcinfo)},\
  403. {LSTRKEY("getfenv"), LFUNCVAL(luaB_getfenv)},\
  404. {LSTRKEY("getmetatable"), LFUNCVAL(luaB_getmetatable)},\
  405. {LSTRKEY("loadfile"), LFUNCVAL(luaB_loadfile)},\
  406. {LSTRKEY("load"), LFUNCVAL(luaB_load)},\
  407. {LSTRKEY("loadstring"), LFUNCVAL(luaB_loadstring)},\
  408. {LSTRKEY("next"), LFUNCVAL(luaB_next)},\
  409. {LSTRKEY("pcall"), LFUNCVAL(luaB_pcall)},\
  410. {LSTRKEY("print"), LFUNCVAL(luaB_print)},\
  411. {LSTRKEY("rawequal"), LFUNCVAL(luaB_rawequal)},\
  412. {LSTRKEY("rawget"), LFUNCVAL(luaB_rawget)},\
  413. {LSTRKEY("rawset"), LFUNCVAL(luaB_rawset)},\
  414. {LSTRKEY("select"), LFUNCVAL(luaB_select)},\
  415. {LSTRKEY("setfenv"), LFUNCVAL(luaB_setfenv)},\
  416. {LSTRKEY("setmetatable"), LFUNCVAL(luaB_setmetatable)},\
  417. {LSTRKEY("tonumber"), LFUNCVAL(luaB_tonumber)},\
  418. {LSTRKEY("tostring"), LFUNCVAL(luaB_tostring)},\
  419. {LSTRKEY("type"), LFUNCVAL(luaB_type)},\
  420. {LSTRKEY("unpack"), LFUNCVAL(luaB_unpack)},\
  421. {LSTRKEY("xpcall"), LFUNCVAL(luaB_xpcall)}
  422. #if LUA_OPTIMIZE_MEMORY == 2
  423. #define MIN_OPT_LEVEL 2
  424. #include "lrodefs.h"
  425. const LUA_REG_TYPE base_funcs_list[] = {
  426. LUA_BASELIB_FUNCLIST,
  427. {LNILKEY, LNILVAL}
  428. };
  429. #endif
  430. static int luaB_index(lua_State *L) {
  431. #if LUA_OPTIMIZE_MEMORY == 2
  432. int fres;
  433. if ((fres = luaR_findfunction(L, base_funcs_list)) != 0)
  434. return fres;
  435. #endif
  436. const char *keyname = luaL_checkstring(L, 2);
  437. if (!c_strcmp(keyname, "_VERSION")) {
  438. lua_pushliteral(L, LUA_VERSION);
  439. return 1;
  440. }
  441. void *res = luaR_findglobal(keyname, c_strlen(keyname));
  442. if (!res)
  443. return 0;
  444. else {
  445. lua_pushrotable(L, res);
  446. return 1;
  447. }
  448. }
  449. static const luaL_Reg base_funcs[] = {
  450. #if LUA_OPTIMIZE_MEMORY != 2
  451. #undef MIN_OPT_LEVEL
  452. #define MIN_OPT_LEVEL 0
  453. #include "lrodefs.h"
  454. LUA_BASELIB_FUNCLIST,
  455. #endif
  456. {"__index", luaB_index},
  457. {NULL, NULL}
  458. };
  459. /*
  460. ** {======================================================
  461. ** Coroutine library
  462. ** =======================================================
  463. */
  464. #define CO_RUN 0 /* running */
  465. #define CO_SUS 1 /* suspended */
  466. #define CO_NOR 2 /* 'normal' (it resumed another coroutine) */
  467. #define CO_DEAD 3
  468. static const char *const statnames[] =
  469. {"running", "suspended", "normal", "dead"};
  470. static int costatus (lua_State *L, lua_State *co) {
  471. if (L == co) return CO_RUN;
  472. switch (lua_status(co)) {
  473. case LUA_YIELD:
  474. return CO_SUS;
  475. case 0: {
  476. lua_Debug ar;
  477. if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */
  478. return CO_NOR; /* it is running */
  479. else if (lua_gettop(co) == 0)
  480. return CO_DEAD;
  481. else
  482. return CO_SUS; /* initial state */
  483. }
  484. default: /* some error occured */
  485. return CO_DEAD;
  486. }
  487. }
  488. static int luaB_costatus (lua_State *L) {
  489. lua_State *co = lua_tothread(L, 1);
  490. luaL_argcheck(L, co, 1, "coroutine expected");
  491. lua_pushstring(L, statnames[costatus(L, co)]);
  492. return 1;
  493. }
  494. static int auxresume (lua_State *L, lua_State *co, int narg) {
  495. int status = costatus(L, co);
  496. if (!lua_checkstack(co, narg))
  497. luaL_error(L, "too many arguments to resume");
  498. if (status != CO_SUS) {
  499. lua_pushfstring(L, "cannot resume %s coroutine", statnames[status]);
  500. return -1; /* error flag */
  501. }
  502. lua_xmove(L, co, narg);
  503. lua_setlevel(L, co);
  504. status = lua_resume(co, narg);
  505. if (status == 0 || status == LUA_YIELD) {
  506. int nres = lua_gettop(co);
  507. if (!lua_checkstack(L, nres + 1))
  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_corunning (lua_State *L) {
  563. if (lua_pushthread(L))
  564. lua_pushnil(L); /* main thread is not a coroutine */
  565. return 1;
  566. }
  567. #undef MIN_OPT_LEVEL
  568. #define MIN_OPT_LEVEL 1
  569. #include "lrodefs.h"
  570. const LUA_REG_TYPE co_funcs[] = {
  571. {LSTRKEY("create"), LFUNCVAL(luaB_cocreate)},
  572. {LSTRKEY("resume"), LFUNCVAL(luaB_coresume)},
  573. {LSTRKEY("running"), LFUNCVAL(luaB_corunning)},
  574. {LSTRKEY("status"), LFUNCVAL(luaB_costatus)},
  575. {LSTRKEY("wrap"), LFUNCVAL(luaB_cowrap)},
  576. {LSTRKEY("yield"), LFUNCVAL(luaB_yield)},
  577. {LNILKEY, LNILVAL}
  578. };
  579. /* }====================================================== */
  580. static void auxopen (lua_State *L, const char *name,
  581. lua_CFunction f, lua_CFunction u) {
  582. lua_pushcfunction(L, u);
  583. lua_pushcclosure(L, f, 1);
  584. lua_setfield(L, -2, name);
  585. }
  586. static void base_open (lua_State *L) {
  587. /* set global _G */
  588. lua_pushvalue(L, LUA_GLOBALSINDEX);
  589. lua_setglobal(L, "_G");
  590. /* open lib into global table */
  591. luaL_register_light(L, "_G", base_funcs);
  592. #if LUA_OPTIMIZE_MEMORY > 0
  593. lua_pushvalue(L, -1);
  594. lua_setmetatable(L, -2);
  595. #else
  596. lua_pushliteral(L, LUA_VERSION);
  597. lua_setglobal(L, "_VERSION"); /* set global _VERSION */
  598. #endif
  599. /* `ipairs' and `pairs' need auxliliary functions as upvalues */
  600. auxopen(L, "ipairs", luaB_ipairs, ipairsaux);
  601. auxopen(L, "pairs", luaB_pairs, luaB_next);
  602. /* `newproxy' needs a weaktable as upvalue */
  603. lua_createtable(L, 0, 1); /* new table `w' */
  604. lua_pushvalue(L, -1); /* `w' will be its own metatable */
  605. lua_setmetatable(L, -2);
  606. lua_pushliteral(L, "kv");
  607. lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */
  608. lua_pushcclosure(L, luaB_newproxy, 1);
  609. lua_setglobal(L, "newproxy"); /* set global `newproxy' */
  610. }
  611. LUALIB_API int luaopen_base (lua_State *L) {
  612. base_open(L);
  613. #if LUA_OPTIMIZE_MEMORY == 0
  614. luaL_register(L, LUA_COLIBNAME, co_funcs);
  615. return 2;
  616. #else
  617. return 1;
  618. #endif
  619. }