lbaselib.c 18 KB

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