lbaselib.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. #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. #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) fputs("\t", stdout);
  37. fputs(s, stdout);
  38. #else
  39. if (i>1) luai_writestring("\t", 1);
  40. luai_writestring(s, strlen(s));
  41. #endif
  42. lua_pop(L, 1); /* pop result */
  43. }
  44. #if defined(LUA_USE_STDIO)
  45. fputs("\n", 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 = 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. return load_aux(L, luaL_loadfile(L, fname));
  250. }
  251. /*
  252. ** Reader for generic `load' function: `lua_load' uses the
  253. ** stack for internal stuff, so the reader cannot change the
  254. ** stack top. Instead, it keeps its resulting string in a
  255. ** reserved slot inside the stack.
  256. */
  257. static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
  258. (void)ud; /* to avoid warnings */
  259. if (L == NULL && size == NULL) // direct mode check, doesn't happen
  260. return NULL;
  261. luaL_checkstack(L, 2, "too many nested functions");
  262. lua_pushvalue(L, 1); /* get function */
  263. lua_call(L, 0, 1); /* call it */
  264. if (lua_isnil(L, -1)) {
  265. *size = 0;
  266. return NULL;
  267. }
  268. else if (lua_isstring(L, -1)) {
  269. lua_replace(L, 3); /* save string in a reserved stack slot */
  270. return lua_tolstring(L, 3, size);
  271. }
  272. else luaL_error(L, "reader function must return a string");
  273. return NULL; /* to avoid warnings */
  274. }
  275. static int luaB_load (lua_State *L) {
  276. int status;
  277. const char *cname = luaL_optstring(L, 2, "=(load)");
  278. luaL_checktype(L, 1, LUA_TFUNCTION);
  279. lua_settop(L, 3); /* function, eventual name, plus one reserved slot */
  280. status = lua_load(L, generic_reader, NULL, cname);
  281. return load_aux(L, status);
  282. }
  283. static int luaB_dofile (lua_State *L) {
  284. const char *fname = luaL_optstring(L, 1, NULL);
  285. int n = lua_gettop(L);
  286. if (luaL_loadfile(L, fname) != 0) lua_error(L);
  287. lua_call(L, 0, LUA_MULTRET);
  288. return lua_gettop(L) - n;
  289. }
  290. static int luaB_assert (lua_State *L) {
  291. luaL_checkany(L, 1);
  292. if (!lua_toboolean(L, 1))
  293. return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!"));
  294. return lua_gettop(L);
  295. }
  296. static int luaB_unpack (lua_State *L) {
  297. int i, e, n;
  298. luaL_checktype(L, 1, LUA_TTABLE);
  299. i = luaL_optint(L, 2, 1);
  300. e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1));
  301. if (i > e) return 0; /* empty range */
  302. n = e - i + 1; /* number of elements */
  303. if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */
  304. return luaL_error(L, "too many results to unpack");
  305. lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */
  306. while (i++ < e) /* push arg[i + 1...e] */
  307. lua_rawgeti(L, 1, i);
  308. return n;
  309. }
  310. static int luaB_select (lua_State *L) {
  311. int n = lua_gettop(L);
  312. if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') {
  313. lua_pushinteger(L, n-1);
  314. return 1;
  315. }
  316. else {
  317. int i = luaL_checkint(L, 1);
  318. if (i < 0) i = n + i;
  319. else if (i > n) i = n;
  320. luaL_argcheck(L, 1 <= i, 1, "index out of range");
  321. return n - i;
  322. }
  323. }
  324. static int luaB_pcall (lua_State *L) {
  325. int status;
  326. luaL_checkany(L, 1);
  327. status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0);
  328. lua_pushboolean(L, (status == 0));
  329. lua_insert(L, 1);
  330. return lua_gettop(L); /* return status + all results */
  331. }
  332. static int luaB_xpcall (lua_State *L) {
  333. int status;
  334. luaL_checkany(L, 2);
  335. lua_settop(L, 2);
  336. lua_insert(L, 1); /* put error function under function to be called */
  337. status = lua_pcall(L, 0, LUA_MULTRET, 1);
  338. lua_pushboolean(L, (status == 0));
  339. lua_replace(L, 1);
  340. return lua_gettop(L); /* return status + all results */
  341. }
  342. static int luaB_tostring (lua_State *L) {
  343. luaL_checkany(L, 1);
  344. if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */
  345. return 1; /* use its value */
  346. switch (lua_type(L, 1)) {
  347. case LUA_TNUMBER:
  348. lua_pushstring(L, lua_tostring(L, 1));
  349. break;
  350. case LUA_TSTRING:
  351. lua_pushvalue(L, 1);
  352. break;
  353. case LUA_TBOOLEAN:
  354. lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
  355. break;
  356. case LUA_TNIL:
  357. lua_pushliteral(L, "nil");
  358. break;
  359. default:
  360. lua_pushfstring(L, "%s: %p", luaL_typename(L, 1), lua_topointer(L, 1));
  361. break;
  362. }
  363. return 1;
  364. }
  365. static int luaB_newproxy (lua_State *L) {
  366. lua_settop(L, 1);
  367. lua_newuserdata(L, 0); /* create proxy */
  368. if (lua_toboolean(L, 1) == 0)
  369. return 1; /* no metatable */
  370. else if (lua_isboolean(L, 1)) {
  371. lua_newtable(L); /* create a new metatable `m' ... */
  372. lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */
  373. lua_pushboolean(L, 1);
  374. lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */
  375. }
  376. else {
  377. int validproxy = 0; /* to check if weaktable[metatable(u)] == true */
  378. if (lua_getmetatable(L, 1)) {
  379. lua_rawget(L, lua_upvalueindex(1));
  380. validproxy = lua_toboolean(L, -1);
  381. lua_pop(L, 1); /* remove value */
  382. }
  383. luaL_argcheck(L, validproxy, 1, "boolean or proxy expected");
  384. lua_getmetatable(L, 1); /* metatable is valid; get it */
  385. }
  386. lua_setmetatable(L, 2);
  387. return 1;
  388. }
  389. #define LUA_BASELIB_FUNCLIST\
  390. {LSTRKEY("assert"), LFUNCVAL(luaB_assert)},\
  391. {LSTRKEY("collectgarbage"), LFUNCVAL(luaB_collectgarbage)},\
  392. {LSTRKEY("dofile"), LFUNCVAL(luaB_dofile)},\
  393. {LSTRKEY("error"), LFUNCVAL(luaB_error)},\
  394. {LSTRKEY("gcinfo"), LFUNCVAL(luaB_gcinfo)},\
  395. {LSTRKEY("getfenv"), LFUNCVAL(luaB_getfenv)},\
  396. {LSTRKEY("getmetatable"), LFUNCVAL(luaB_getmetatable)},\
  397. {LSTRKEY("loadfile"), LFUNCVAL(luaB_loadfile)},\
  398. {LSTRKEY("load"), LFUNCVAL(luaB_load)},\
  399. {LSTRKEY("loadstring"), LFUNCVAL(luaB_loadstring)},\
  400. {LSTRKEY("next"), LFUNCVAL(luaB_next)},\
  401. {LSTRKEY("pcall"), LFUNCVAL(luaB_pcall)},\
  402. {LSTRKEY("print"), LFUNCVAL(luaB_print)},\
  403. {LSTRKEY("rawequal"), LFUNCVAL(luaB_rawequal)},\
  404. {LSTRKEY("rawget"), LFUNCVAL(luaB_rawget)},\
  405. {LSTRKEY("rawset"), LFUNCVAL(luaB_rawset)},\
  406. {LSTRKEY("select"), LFUNCVAL(luaB_select)},\
  407. {LSTRKEY("setfenv"), LFUNCVAL(luaB_setfenv)},\
  408. {LSTRKEY("setmetatable"), LFUNCVAL(luaB_setmetatable)},\
  409. {LSTRKEY("tonumber"), LFUNCVAL(luaB_tonumber)},\
  410. {LSTRKEY("tostring"), LFUNCVAL(luaB_tostring)},\
  411. {LSTRKEY("type"), LFUNCVAL(luaB_type)},\
  412. {LSTRKEY("unpack"), LFUNCVAL(luaB_unpack)},\
  413. {LSTRKEY("xpcall"), LFUNCVAL(luaB_xpcall)}
  414. #if LUA_OPTIMIZE_MEMORY == 2
  415. #define MIN_OPT_LEVEL 2
  416. #include "lrodefs.h"
  417. const LUA_REG_TYPE base_funcs_list[] = {
  418. LUA_BASELIB_FUNCLIST,
  419. {LNILKEY, LNILVAL}
  420. };
  421. #endif
  422. static int luaB_index(lua_State *L) {
  423. #if LUA_OPTIMIZE_MEMORY == 2
  424. int fres;
  425. if ((fres = luaR_findfunction(L, base_funcs_list)) != 0)
  426. return fres;
  427. #endif
  428. const char *keyname = luaL_checkstring(L, 2);
  429. if (!strcmp(keyname, "_VERSION")) {
  430. lua_pushliteral(L, LUA_VERSION);
  431. return 1;
  432. }
  433. void *res = luaR_findglobal(keyname, strlen(keyname));
  434. if (!res)
  435. return 0;
  436. else {
  437. lua_pushrotable(L, res);
  438. return 1;
  439. }
  440. }
  441. static const luaL_Reg base_funcs[] = {
  442. #if LUA_OPTIMIZE_MEMORY != 2
  443. #undef MIN_OPT_LEVEL
  444. #define MIN_OPT_LEVEL 0
  445. #include "lrodefs.h"
  446. LUA_BASELIB_FUNCLIST,
  447. #endif
  448. {"__index", luaB_index},
  449. {NULL, NULL}
  450. };
  451. /*
  452. ** {======================================================
  453. ** Coroutine library
  454. ** =======================================================
  455. */
  456. #define CO_RUN 0 /* running */
  457. #define CO_SUS 1 /* suspended */
  458. #define CO_NOR 2 /* 'normal' (it resumed another coroutine) */
  459. #define CO_DEAD 3
  460. static const char *const statnames[] =
  461. {"running", "suspended", "normal", "dead"};
  462. static int costatus (lua_State *L, lua_State *co) {
  463. if (L == co) return CO_RUN;
  464. switch (lua_status(co)) {
  465. case LUA_YIELD:
  466. return CO_SUS;
  467. case 0: {
  468. lua_Debug ar;
  469. if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */
  470. return CO_NOR; /* it is running */
  471. else if (lua_gettop(co) == 0)
  472. return CO_DEAD;
  473. else
  474. return CO_SUS; /* initial state */
  475. }
  476. default: /* some error occured */
  477. return CO_DEAD;
  478. }
  479. }
  480. static int luaB_costatus (lua_State *L) {
  481. lua_State *co = lua_tothread(L, 1);
  482. luaL_argcheck(L, co, 1, "coroutine expected");
  483. lua_pushstring(L, statnames[costatus(L, co)]);
  484. return 1;
  485. }
  486. static int auxresume (lua_State *L, lua_State *co, int narg) {
  487. int status = costatus(L, co);
  488. if (!lua_checkstack(co, narg))
  489. luaL_error(L, "too many arguments to resume");
  490. if (status != CO_SUS) {
  491. lua_pushfstring(L, "cannot resume %s coroutine", statnames[status]);
  492. return -1; /* error flag */
  493. }
  494. lua_xmove(L, co, narg);
  495. lua_setlevel(L, co);
  496. status = lua_resume(co, narg);
  497. if (status == 0 || status == LUA_YIELD) {
  498. int nres = lua_gettop(co);
  499. if (!lua_checkstack(L, nres + 1))
  500. luaL_error(L, "too many results to resume");
  501. lua_xmove(co, L, nres); /* move yielded values */
  502. return nres;
  503. }
  504. else {
  505. lua_xmove(co, L, 1); /* move error message */
  506. return -1; /* error flag */
  507. }
  508. }
  509. static int luaB_coresume (lua_State *L) {
  510. lua_State *co = lua_tothread(L, 1);
  511. int r;
  512. luaL_argcheck(L, co, 1, "coroutine expected");
  513. r = auxresume(L, co, lua_gettop(L) - 1);
  514. if (r < 0) {
  515. lua_pushboolean(L, 0);
  516. lua_insert(L, -2);
  517. return 2; /* return false + error message */
  518. }
  519. else {
  520. lua_pushboolean(L, 1);
  521. lua_insert(L, -(r + 1));
  522. return r + 1; /* return true + `resume' returns */
  523. }
  524. }
  525. static int luaB_auxwrap (lua_State *L) {
  526. lua_State *co = lua_tothread(L, lua_upvalueindex(1));
  527. int r = auxresume(L, co, lua_gettop(L));
  528. if (r < 0) {
  529. if (lua_isstring(L, -1)) { /* error object is a string? */
  530. luaL_where(L, 1); /* add extra info */
  531. lua_insert(L, -2);
  532. lua_concat(L, 2);
  533. }
  534. lua_error(L); /* propagate error */
  535. }
  536. return r;
  537. }
  538. static int luaB_cocreate (lua_State *L) {
  539. lua_State *NL = lua_newthread(L);
  540. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
  541. "Lua function expected");
  542. lua_pushvalue(L, 1); /* move function to top */
  543. lua_xmove(L, NL, 1); /* move function from L to NL */
  544. return 1;
  545. }
  546. static int luaB_cowrap (lua_State *L) {
  547. luaB_cocreate(L);
  548. lua_pushcclosure(L, luaB_auxwrap, 1);
  549. return 1;
  550. }
  551. static int luaB_yield (lua_State *L) {
  552. return lua_yield(L, lua_gettop(L));
  553. }
  554. static int luaB_corunning (lua_State *L) {
  555. if (lua_pushthread(L))
  556. lua_pushnil(L); /* main thread is not a coroutine */
  557. return 1;
  558. }
  559. #undef MIN_OPT_LEVEL
  560. #define MIN_OPT_LEVEL 1
  561. #include "lrodefs.h"
  562. const LUA_REG_TYPE co_funcs[] = {
  563. {LSTRKEY("create"), LFUNCVAL(luaB_cocreate)},
  564. {LSTRKEY("resume"), LFUNCVAL(luaB_coresume)},
  565. {LSTRKEY("running"), LFUNCVAL(luaB_corunning)},
  566. {LSTRKEY("status"), LFUNCVAL(luaB_costatus)},
  567. {LSTRKEY("wrap"), LFUNCVAL(luaB_cowrap)},
  568. {LSTRKEY("yield"), LFUNCVAL(luaB_yield)},
  569. {LNILKEY, LNILVAL}
  570. };
  571. /* }====================================================== */
  572. static void auxopen (lua_State *L, const char *name,
  573. lua_CFunction f, lua_CFunction u) {
  574. lua_pushcfunction(L, u);
  575. lua_pushcclosure(L, f, 1);
  576. lua_setfield(L, -2, name);
  577. }
  578. static void base_open (lua_State *L) {
  579. /* set global _G */
  580. lua_pushvalue(L, LUA_GLOBALSINDEX);
  581. lua_setglobal(L, "_G");
  582. /* open lib into global table */
  583. luaL_register_light(L, "_G", base_funcs);
  584. #if LUA_OPTIMIZE_MEMORY > 0
  585. lua_pushvalue(L, -1);
  586. lua_setmetatable(L, -2);
  587. #else
  588. lua_pushliteral(L, LUA_VERSION);
  589. lua_setglobal(L, "_VERSION"); /* set global _VERSION */
  590. #endif
  591. /* `ipairs' and `pairs' need auxliliary functions as upvalues */
  592. auxopen(L, "ipairs", luaB_ipairs, ipairsaux);
  593. auxopen(L, "pairs", luaB_pairs, luaB_next);
  594. /* `newproxy' needs a weaktable as upvalue */
  595. lua_createtable(L, 0, 1); /* new table `w' */
  596. lua_pushvalue(L, -1); /* `w' will be its own metatable */
  597. lua_setmetatable(L, -2);
  598. lua_pushliteral(L, "kv");
  599. lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */
  600. lua_pushcclosure(L, luaB_newproxy, 1);
  601. lua_setglobal(L, "newproxy"); /* set global `newproxy' */
  602. }
  603. LUALIB_API int luaopen_base (lua_State *L) {
  604. base_open(L);
  605. #if LUA_OPTIMIZE_MEMORY == 0
  606. luaL_register(L, LUA_COLIBNAME, co_funcs);
  607. return 2;
  608. #else
  609. return 1;
  610. #endif
  611. }