lbaselib.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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. #include "lua.h"
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.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. #ifdef LUA_CROSS_COMPILER
  21. #undef puts
  22. #define puts(s) printf("%s",s)
  23. #endif
  24. static int luaB_print (lua_State *L) {
  25. int n = lua_gettop(L); /* number of arguments */
  26. int i;
  27. lua_getglobal(L, "tostring");
  28. for (i=1; i<=n; i++) {
  29. const char *s;
  30. lua_pushvalue(L, -1); /* function to be called */
  31. lua_pushvalue(L, i); /* value to print */
  32. lua_call(L, 1, 1);
  33. s = lua_tostring(L, -1); /* get result */
  34. if (s == NULL)
  35. return luaL_error(L, LUA_QL("tostring") " must return a string to "
  36. LUA_QL("print"));
  37. if (i>1) puts("\t");
  38. puts(s);
  39. lua_pop(L, 1); /* pop result */
  40. }
  41. puts("\n");
  42. return 0;
  43. }
  44. static int luaB_tonumber (lua_State *L) {
  45. int base = luaL_optint(L, 2, 10);
  46. if (base == 10) { /* standard conversion */
  47. luaL_checkany(L, 1);
  48. if (lua_isnumber(L, 1)) {
  49. lua_pushnumber(L, lua_tonumber(L, 1));
  50. return 1;
  51. }
  52. }
  53. else {
  54. const char *s1 = luaL_checkstring(L, 1);
  55. char *s2;
  56. unsigned long n;
  57. luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
  58. n = strtoul(s1, &s2, base);
  59. if (s1 != s2) { /* at least one valid digit? */
  60. while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
  61. if (*s2 == '\0') { /* no invalid trailing characters? */
  62. lua_pushnumber(L, (lua_Number)n);
  63. return 1;
  64. }
  65. }
  66. }
  67. lua_pushnil(L); /* else not a number */
  68. return 1;
  69. }
  70. static int luaB_error (lua_State *L) {
  71. int level = luaL_optint(L, 2, 1);
  72. lua_settop(L, 1);
  73. if (lua_isstring(L, 1) && level > 0) { /* add extra information? */
  74. luaL_where(L, level);
  75. lua_pushvalue(L, 1);
  76. lua_concat(L, 2);
  77. }
  78. return lua_error(L);
  79. }
  80. static int luaB_getmetatable (lua_State *L) {
  81. luaL_checkany(L, 1);
  82. if (!lua_getmetatable(L, 1)) {
  83. lua_pushnil(L);
  84. return 1; /* no metatable */
  85. }
  86. luaL_getmetafield(L, 1, "__metatable");
  87. return 1; /* returns either __metatable field (if present) or metatable */
  88. }
  89. static int luaB_setmetatable (lua_State *L) {
  90. int t = lua_type(L, 2);
  91. luaL_checktype(L, 1, LUA_TTABLE);
  92. luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
  93. "nil or table expected");
  94. if (luaL_getmetafield(L, 1, "__metatable"))
  95. luaL_error(L, "cannot change a protected metatable");
  96. lua_settop(L, 2);
  97. lua_setmetatable(L, 1);
  98. return 1;
  99. }
  100. static void getfunc (lua_State *L, int opt) {
  101. if (lua_isfunction(L, 1)) lua_pushvalue(L, 1);
  102. else {
  103. lua_Debug ar;
  104. int level = opt ? luaL_optint(L, 1, 1) : luaL_checkint(L, 1);
  105. luaL_argcheck(L, level >= 0, 1, "level must be non-negative");
  106. if (lua_getstack(L, level, &ar) == 0)
  107. luaL_argerror(L, 1, "invalid level");
  108. lua_getinfo(L, "f", &ar);
  109. if (lua_isnil(L, -1))
  110. luaL_error(L, "no function environment for tail call at level %d",
  111. level);
  112. }
  113. }
  114. static int luaB_getfenv (lua_State *L) {
  115. getfunc(L, 1);
  116. if (lua_iscfunction(L, -1)) /* is a C function? */
  117. lua_pushvalue(L, LUA_GLOBALSINDEX); /* return the thread's global env. */
  118. else
  119. lua_getfenv(L, -1);
  120. return 1;
  121. }
  122. static int luaB_setfenv (lua_State *L) {
  123. luaL_checktype(L, 2, LUA_TTABLE);
  124. getfunc(L, 0);
  125. lua_pushvalue(L, 2);
  126. if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) {
  127. /* change environment of current thread */
  128. lua_pushthread(L);
  129. lua_insert(L, -2);
  130. lua_setfenv(L, -2);
  131. return 0;
  132. }
  133. else if (lua_iscfunction(L, -2) || lua_setfenv(L, -2) == 0)
  134. luaL_error(L,
  135. LUA_QL("setfenv") " cannot change environment of given object");
  136. return 1;
  137. }
  138. static int luaB_rawequal (lua_State *L) {
  139. luaL_checkany(L, 1);
  140. luaL_checkany(L, 2);
  141. lua_pushboolean(L, lua_rawequal(L, 1, 2));
  142. return 1;
  143. }
  144. static int luaB_rawget (lua_State *L) {
  145. luaL_checktable(L, 1);
  146. luaL_checkany(L, 2);
  147. lua_settop(L, 2);
  148. lua_rawget(L, 1);
  149. return 1;
  150. }
  151. static int luaB_rawset (lua_State *L) {
  152. luaL_checktable(L, 1);
  153. luaL_checkany(L, 2);
  154. luaL_checkany(L, 3);
  155. lua_settop(L, 3);
  156. lua_rawset(L, 1);
  157. return 1;
  158. }
  159. static int luaB_gcinfo (lua_State *L) {
  160. lua_pushinteger(L, lua_getgccount(L));
  161. return 1;
  162. }
  163. static int luaB_collectgarbage (lua_State *L) {
  164. static const char *const opts[] = {"stop", "restart", "collect",
  165. "count", "step", "setpause", "setstepmul","setmemlimit","getmemlimit", NULL};
  166. static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
  167. LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL,
  168. LUA_GCSETMEMLIMIT,LUA_GCGETMEMLIMIT};
  169. int o = luaL_checkoption(L, 1, "collect", opts);
  170. int ex = luaL_optint(L, 2, 0);
  171. int res = lua_gc(L, optsnum[o], ex);
  172. switch (optsnum[o]) {
  173. case LUA_GCCOUNT: {
  174. int b = lua_gc(L, LUA_GCCOUNTB, 0);
  175. lua_pushnumber(L, res + ((lua_Number)b/1024));
  176. return 1;
  177. }
  178. case LUA_GCSTEP: {
  179. lua_pushboolean(L, res);
  180. return 1;
  181. }
  182. default: {
  183. lua_pushnumber(L, res);
  184. return 1;
  185. }
  186. }
  187. }
  188. static int luaB_type (lua_State *L) {
  189. luaL_checkany(L, 1);
  190. lua_pushstring(L, luaL_typename(L, 1));
  191. return 1;
  192. }
  193. static int luaB_next (lua_State *L) {
  194. luaL_checktable(L, 1);
  195. lua_settop(L, 2); /* create a 2nd argument if there isn't one */
  196. if (lua_next(L, 1))
  197. return 2;
  198. else {
  199. lua_pushnil(L);
  200. return 1;
  201. }
  202. }
  203. static int luaB_pairs (lua_State *L) {
  204. luaL_checktable(L, 1);
  205. lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
  206. lua_pushvalue(L, 1); /* state, */
  207. lua_pushnil(L); /* and initial value */
  208. return 3;
  209. }
  210. static int ipairsaux (lua_State *L) {
  211. int i = luaL_checkint(L, 2);
  212. luaL_checktable(L, 1);
  213. i++; /* next value */
  214. lua_pushinteger(L, i);
  215. lua_rawgeti(L, 1, i);
  216. return (lua_isnil(L, -1)) ? 0 : 2;
  217. }
  218. static int luaB_ipairs (lua_State *L) {
  219. luaL_checktable(L, 1);
  220. lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
  221. lua_pushvalue(L, 1); /* state, */
  222. lua_pushinteger(L, 0); /* and initial value */
  223. return 3;
  224. }
  225. static int load_aux (lua_State *L, int status) {
  226. if (status == 0) /* OK? */
  227. return 1;
  228. else {
  229. lua_pushnil(L);
  230. lua_insert(L, -2); /* put before error message */
  231. return 2; /* return nil plus error message */
  232. }
  233. }
  234. static int luaB_loadstring (lua_State *L) {
  235. size_t l;
  236. const char *s = luaL_checklstring(L, 1, &l);
  237. const char *chunkname = luaL_optstring(L, 2, s);
  238. return load_aux(L, luaL_loadbuffer(L, s, l, chunkname));
  239. }
  240. static int luaB_loadfile (lua_State *L) {
  241. const char *fname = luaL_optstring(L, 1, NULL);
  242. #ifdef LUA_CROSS_COMPILER
  243. return load_aux(L, luaL_loadfile(L, fname));
  244. #else
  245. return load_aux(L, luaL_loadfile(L, fname));
  246. #endif
  247. }
  248. /*
  249. ** Reader for generic `load' function: `lua_load' uses the
  250. ** stack for internal stuff, so the reader cannot change the
  251. ** stack top. Instead, it keeps its resulting string in a
  252. ** reserved slot inside the stack.
  253. */
  254. static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
  255. (void)ud; /* to avoid warnings */
  256. if (L == NULL && size == NULL) // direct mode check, doesn't happen
  257. return NULL;
  258. luaL_checkstack(L, 2, "too many nested functions");
  259. lua_pushvalue(L, 1); /* get function */
  260. lua_call(L, 0, 1); /* call it */
  261. if (lua_isnil(L, -1)) {
  262. *size = 0;
  263. return NULL;
  264. }
  265. else if (lua_isstring(L, -1)) {
  266. lua_replace(L, 3); /* save string in a reserved stack slot */
  267. return lua_tolstring(L, 3, size);
  268. }
  269. else luaL_error(L, "reader function must return a string");
  270. return NULL; /* to avoid warnings */
  271. }
  272. static int luaB_load (lua_State *L) {
  273. int status;
  274. const char *cname = luaL_optstring(L, 2, "=(load)");
  275. luaL_checktype(L, 1, LUA_TFUNCTION);
  276. lua_settop(L, 3); /* function, eventual name, plus one reserved slot */
  277. status = lua_load(L, generic_reader, NULL, cname);
  278. return load_aux(L, status);
  279. }
  280. static int luaB_dofile (lua_State *L) {
  281. const char *fname = luaL_optstring(L, 1, NULL);
  282. int n = lua_gettop(L);
  283. #ifdef LUA_CROSS_COMPILER
  284. if (luaL_loadfile(L, fname) != 0) lua_error(L);
  285. #else
  286. if (luaL_loadfile(L, fname) != 0) lua_error(L);
  287. #endif
  288. lua_call(L, 0, LUA_MULTRET);
  289. return lua_gettop(L) - n;
  290. }
  291. static int luaB_assert (lua_State *L) {
  292. luaL_checkany(L, 1);
  293. if (!lua_toboolean(L, 1))
  294. return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!"));
  295. return lua_gettop(L);
  296. }
  297. static int luaB_unpack (lua_State *L) {
  298. int i, e, n;
  299. luaL_checktype(L, 1, LUA_TTABLE);
  300. i = luaL_optint(L, 2, 1);
  301. e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1));
  302. if (i > e) return 0; /* empty range */
  303. n = e - i + 1; /* number of elements */
  304. if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */
  305. return luaL_error(L, "too many results to unpack");
  306. lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */
  307. while (i++ < e) /* push arg[i + 1...e] */
  308. lua_rawgeti(L, 1, i);
  309. return n;
  310. }
  311. static int luaB_select (lua_State *L) {
  312. int n = lua_gettop(L);
  313. if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') {
  314. lua_pushinteger(L, n-1);
  315. return 1;
  316. }
  317. else {
  318. int i = luaL_checkint(L, 1);
  319. if (i < 0) i = n + i;
  320. else if (i > n) i = n;
  321. luaL_argcheck(L, 1 <= i, 1, "index out of range");
  322. return n - i;
  323. }
  324. }
  325. static int luaB_pcall (lua_State *L) {
  326. int status;
  327. luaL_checkany(L, 1);
  328. status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0);
  329. lua_pushboolean(L, (status == 0));
  330. lua_insert(L, 1);
  331. return lua_gettop(L); /* return status + all results */
  332. }
  333. static int luaB_xpcall (lua_State *L) {
  334. int status;
  335. luaL_checkany(L, 2);
  336. lua_settop(L, 2);
  337. lua_insert(L, 1); /* put error function under function to be called */
  338. status = lua_pcall(L, 0, LUA_MULTRET, 1);
  339. lua_pushboolean(L, (status == 0));
  340. lua_replace(L, 1);
  341. return lua_gettop(L); /* return status + all results */
  342. }
  343. static int luaB_tostring (lua_State *L) {
  344. luaL_checkany(L, 1);
  345. if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */
  346. return 1; /* use its value */
  347. switch (lua_type(L, 1)) {
  348. case LUA_TNUMBER:
  349. lua_pushstring(L, lua_tostring(L, 1));
  350. break;
  351. case LUA_TSTRING:
  352. lua_pushvalue(L, 1);
  353. break;
  354. case LUA_TBOOLEAN:
  355. lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
  356. break;
  357. case LUA_TNIL:
  358. lua_pushliteral(L, "nil");
  359. break;
  360. default:
  361. lua_pushfstring(L, "%s: %p", luaL_typename(L, 1), lua_topointer(L, 1));
  362. break;
  363. }
  364. return 1;
  365. }
  366. static int luaB_newproxy (lua_State *L) {
  367. lua_settop(L, 1);
  368. lua_newuserdata(L, 0); /* create proxy */
  369. if (lua_toboolean(L, 1) == 0)
  370. return 1; /* no metatable */
  371. else if (lua_isboolean(L, 1)) {
  372. lua_newtable(L); /* create a new metatable `m' ... */
  373. lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */
  374. lua_pushboolean(L, 1);
  375. lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */
  376. }
  377. else {
  378. int validproxy = 0; /* to check if weaktable[metatable(u)] == true */
  379. if (lua_getmetatable(L, 1)) {
  380. lua_rawget(L, lua_upvalueindex(1));
  381. validproxy = lua_toboolean(L, -1);
  382. lua_pop(L, 1); /* remove value */
  383. }
  384. luaL_argcheck(L, validproxy, 1, "boolean or proxy expected");
  385. lua_getmetatable(L, 1); /* metatable is valid; get it */
  386. }
  387. lua_setmetatable(L, 2);
  388. return 1;
  389. }
  390. /*
  391. ** ESP builds use specific linker directives to marshal all the ROTable entries
  392. ** for the library modules including the base library into an entry vector in
  393. ** the PSECT ".lua_rotable" including the base library entries; this is bound
  394. ** into a ROTable in linit.c which then hooked into the __index metaentry for
  395. ** _G so that base library and ROM tables are directly resolved through _G.
  396. **
  397. ** The host-based luac.cross builds which must use a standard GNU link or
  398. ** MSVC so this linker-specfic assembly approach can't be used. In this case
  399. ** luaopen_base returns a base_func ROTable so a two cascade resolution. See
  400. ** description in init.c for further details.
  401. */
  402. #ifdef LUA_CROSS_COMPILER
  403. LROT_BEGIN(base_func, NULL, 0)
  404. #else
  405. LROT_ENTRIES_IN_SECTION(base_func, rotable)
  406. #endif
  407. LROT_FUNCENTRY(assert, luaB_assert)
  408. LROT_FUNCENTRY(collectgarbage, luaB_collectgarbage)
  409. LROT_FUNCENTRY(dofile, luaB_dofile)
  410. LROT_FUNCENTRY(error, luaB_error)
  411. LROT_FUNCENTRY(gcinfo, luaB_gcinfo)
  412. LROT_FUNCENTRY(getfenv, luaB_getfenv)
  413. LROT_FUNCENTRY(getmetatable, luaB_getmetatable)
  414. LROT_FUNCENTRY(loadfile, luaB_loadfile)
  415. LROT_FUNCENTRY(load, luaB_load)
  416. LROT_FUNCENTRY(loadstring, luaB_loadstring)
  417. LROT_FUNCENTRY(next, luaB_next)
  418. LROT_FUNCENTRY(pcall, luaB_pcall)
  419. LROT_FUNCENTRY(print, luaB_print)
  420. LROT_FUNCENTRY(rawequal, luaB_rawequal)
  421. LROT_FUNCENTRY(rawget, luaB_rawget)
  422. LROT_FUNCENTRY(rawset, luaB_rawset)
  423. LROT_FUNCENTRY(select, luaB_select)
  424. LROT_FUNCENTRY(setfenv, luaB_setfenv)
  425. LROT_FUNCENTRY(setmetatable, luaB_setmetatable)
  426. LROT_FUNCENTRY(tonumber, luaB_tonumber)
  427. LROT_FUNCENTRY(tostring, luaB_tostring)
  428. LROT_FUNCENTRY(type, luaB_type)
  429. LROT_FUNCENTRY(unpack, luaB_unpack)
  430. LROT_FUNCENTRY(xpcall, luaB_xpcall)
  431. #ifdef LUA_CROSS_COMPILER
  432. LROT_END(base_func, NULL, 0)
  433. #else
  434. LROT_BREAK(base_func)
  435. #endif
  436. /*
  437. ** {======================================================
  438. ** Coroutine library
  439. ** =======================================================
  440. */
  441. #define CO_RUN 0 /* running */
  442. #define CO_SUS 1 /* suspended */
  443. #define CO_NOR 2 /* 'normal' (it resumed another coroutine) */
  444. #define CO_DEAD 3
  445. static const char *const statnames[] =
  446. {"running", "suspended", "normal", "dead"};
  447. static int costatus (lua_State *L, lua_State *co) {
  448. if (L == co) return CO_RUN;
  449. switch (lua_status(co)) {
  450. case LUA_YIELD:
  451. return CO_SUS;
  452. case 0: {
  453. lua_Debug ar;
  454. if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */
  455. return CO_NOR; /* it is running */
  456. else if (lua_gettop(co) == 0)
  457. return CO_DEAD;
  458. else
  459. return CO_SUS; /* initial state */
  460. }
  461. default: /* some error occured */
  462. return CO_DEAD;
  463. }
  464. }
  465. static int luaB_costatus (lua_State *L) {
  466. lua_State *co = lua_tothread(L, 1);
  467. luaL_argcheck(L, co, 1, "coroutine expected");
  468. lua_pushstring(L, statnames[costatus(L, co)]);
  469. return 1;
  470. }
  471. static int auxresume (lua_State *L, lua_State *co, int narg) {
  472. int status = costatus(L, co);
  473. if (!lua_checkstack(co, narg))
  474. luaL_error(L, "too many arguments to resume");
  475. if (status != CO_SUS) {
  476. lua_pushfstring(L, "cannot resume %s coroutine", statnames[status]);
  477. return -1; /* error flag */
  478. }
  479. lua_xmove(L, co, narg);
  480. lua_setlevel(L, co);
  481. status = lua_resume(co, narg);
  482. if (status == 0 || status == LUA_YIELD) {
  483. int nres = lua_gettop(co);
  484. if (!lua_checkstack(L, nres + 1))
  485. luaL_error(L, "too many results to resume");
  486. lua_xmove(co, L, nres); /* move yielded values */
  487. return nres;
  488. }
  489. else {
  490. lua_xmove(co, L, 1); /* move error message */
  491. return -1; /* error flag */
  492. }
  493. }
  494. static int luaB_coresume (lua_State *L) {
  495. lua_State *co = lua_tothread(L, 1);
  496. int r;
  497. luaL_argcheck(L, co, 1, "coroutine expected");
  498. r = auxresume(L, co, lua_gettop(L) - 1);
  499. if (r < 0) {
  500. lua_pushboolean(L, 0);
  501. lua_insert(L, -2);
  502. return 2; /* return false + error message */
  503. }
  504. else {
  505. lua_pushboolean(L, 1);
  506. lua_insert(L, -(r + 1));
  507. return r + 1; /* return true + `resume' returns */
  508. }
  509. }
  510. static int luaB_auxwrap (lua_State *L) {
  511. lua_State *co = lua_tothread(L, lua_upvalueindex(1));
  512. int r = auxresume(L, co, lua_gettop(L));
  513. if (r < 0) {
  514. if (lua_isstring(L, -1)) { /* error object is a string? */
  515. luaL_where(L, 1); /* add extra info */
  516. lua_insert(L, -2);
  517. lua_concat(L, 2);
  518. }
  519. lua_error(L); /* propagate error */
  520. }
  521. return r;
  522. }
  523. static int luaB_cocreate (lua_State *L) {
  524. lua_State *NL = lua_newthread(L);
  525. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
  526. "Lua function expected");
  527. lua_pushvalue(L, 1); /* move function to top */
  528. lua_xmove(L, NL, 1); /* move function from L to NL */
  529. return 1;
  530. }
  531. static int luaB_cowrap (lua_State *L) {
  532. luaB_cocreate(L);
  533. lua_pushcclosure(L, luaB_auxwrap, 1);
  534. return 1;
  535. }
  536. static int luaB_yield (lua_State *L) {
  537. return lua_yield(L, lua_gettop(L));
  538. }
  539. static int luaB_corunning (lua_State *L) {
  540. if (lua_pushthread(L))
  541. lua_pushnil(L); /* main thread is not a coroutine */
  542. return 1;
  543. }
  544. LROT_BEGIN(co_funcs, NULL, 0)
  545. LROT_FUNCENTRY( create, luaB_cocreate )
  546. LROT_FUNCENTRY( resume, luaB_coresume )
  547. LROT_FUNCENTRY( running, luaB_corunning )
  548. LROT_FUNCENTRY( status, luaB_costatus )
  549. LROT_FUNCENTRY( wrap, luaB_cowrap )
  550. LROT_FUNCENTRY( yield, luaB_yield )
  551. LROT_END(co_funcs, NULL, 0)
  552. /* }====================================================== */
  553. static void auxopen (lua_State *L, const char *name,
  554. lua_CFunction f, lua_CFunction u) {
  555. lua_pushcfunction(L, u);
  556. lua_pushcclosure(L, f, 1);
  557. lua_setglobal(L, name);
  558. }
  559. extern LROT_TABLE(rotables);
  560. LUALIB_API int luaopen_base (lua_State *L) {
  561. lua_pushvalue(L, LUA_GLOBALSINDEX);
  562. lua_settable(L, LUA_GLOBALSINDEX); /* set global _G */
  563. lua_pushliteral(L, LUA_VERSION);
  564. lua_setglobal(L, "_VERSION"); /* set global _VERSION */
  565. /* `ipairs' and `pairs' need auxliliary functions as upvalues */
  566. auxopen(L, "ipairs", luaB_ipairs, ipairsaux);
  567. auxopen(L, "pairs", luaB_pairs, luaB_next);
  568. /* `newproxy' needs a weaktable as upvalue */
  569. lua_createtable(L, 0, 1); /* new table `w' */
  570. lua_pushliteral(L, "kv");
  571. lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */
  572. lua_pushvalue(L, -1); /* `w' will be its own metatable */
  573. lua_setmetatable(L, -2);
  574. lua_pushcclosure(L, luaB_newproxy, 1); /* Upval is table w */
  575. lua_setglobal(L, "newproxy"); /* set global `newproxy' */
  576. lua_pushrotable(L, LROT_TABLEREF(rotables));
  577. lua_setglobal(L, "__index");
  578. lua_pushvalue(L, LUA_GLOBALSINDEX); /* _G is its own metatable */
  579. lua_setmetatable(L, LUA_GLOBALSINDEX);
  580. return 0;
  581. }