lbaselib.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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 defined(LUA_USE_STDIO)
  36. if (i>1) fputs("\t", c_stdout);
  37. fputs(s, c_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", 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 = 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. #include "lrotable.h"
  398. LROT_EXTERN(lua_rotable_base);
  399. /*
  400. * Separate ROTables are used for the base functions and library ROTables, with
  401. * the base functions ROTable declared below. The library ROTable is chained
  402. * from this using its __index meta-method.
  403. *
  404. * ESP builds use specific linker directives to marshal all the ROTable entries
  405. * for the library modules into a single ROTable in the PSECT ".lua_rotable".
  406. * This is not practical on Posix builds using a standard GNU link, so the
  407. * equivalent ROTable for the core libraries defined in linit.c for the cross-
  408. * compiler build.
  409. */
  410. LROT_EXTERN(lua_rotables);
  411. LROT_PUBLIC_BEGIN(base_func_meta)
  412. LROT_TABENTRY( __index, lua_rotables )
  413. LROT_END(base_func, base_func_meta, LROT_MASK_INDEX)
  414. LROT_PUBLIC_BEGIN(base_func)
  415. LROT_FUNCENTRY(assert, luaB_assert)
  416. LROT_FUNCENTRY(collectgarbage, luaB_collectgarbage)
  417. LROT_FUNCENTRY(dofile, luaB_dofile)
  418. LROT_FUNCENTRY(error, luaB_error)
  419. LROT_FUNCENTRY(gcinfo, luaB_gcinfo)
  420. LROT_FUNCENTRY(getfenv, luaB_getfenv)
  421. LROT_FUNCENTRY(getmetatable, luaB_getmetatable)
  422. LROT_FUNCENTRY(loadfile, luaB_loadfile)
  423. LROT_FUNCENTRY(load, luaB_load)
  424. LROT_FUNCENTRY(loadstring, luaB_loadstring)
  425. LROT_FUNCENTRY(next, luaB_next)
  426. LROT_FUNCENTRY(pcall, luaB_pcall)
  427. LROT_FUNCENTRY(print, luaB_print)
  428. LROT_FUNCENTRY(rawequal, luaB_rawequal)
  429. LROT_FUNCENTRY(rawget, luaB_rawget)
  430. LROT_FUNCENTRY(rawset, luaB_rawset)
  431. LROT_FUNCENTRY(select, luaB_select)
  432. LROT_FUNCENTRY(setfenv, luaB_setfenv)
  433. LROT_FUNCENTRY(setmetatable, luaB_setmetatable)
  434. LROT_FUNCENTRY(tonumber, luaB_tonumber)
  435. LROT_FUNCENTRY(tostring, luaB_tostring)
  436. LROT_FUNCENTRY(type, luaB_type)
  437. LROT_FUNCENTRY(unpack, luaB_unpack)
  438. LROT_FUNCENTRY(xpcall, luaB_xpcall)
  439. LROT_TABENTRY(__metatable, base_func_meta)
  440. LROT_END(base_func, base_func_meta, LROT_MASK_INDEX)
  441. LROT_BEGIN(G_meta)
  442. LROT_TABENTRY( __index, base_func )
  443. LROT_END(G_meta, NULL, 0)
  444. /*
  445. ** {======================================================
  446. ** Coroutine library
  447. ** =======================================================
  448. */
  449. #define CO_RUN 0 /* running */
  450. #define CO_SUS 1 /* suspended */
  451. #define CO_NOR 2 /* 'normal' (it resumed another coroutine) */
  452. #define CO_DEAD 3
  453. static const char *const statnames[] =
  454. {"running", "suspended", "normal", "dead"};
  455. static int costatus (lua_State *L, lua_State *co) {
  456. if (L == co) return CO_RUN;
  457. switch (lua_status(co)) {
  458. case LUA_YIELD:
  459. return CO_SUS;
  460. case 0: {
  461. lua_Debug ar;
  462. if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */
  463. return CO_NOR; /* it is running */
  464. else if (lua_gettop(co) == 0)
  465. return CO_DEAD;
  466. else
  467. return CO_SUS; /* initial state */
  468. }
  469. default: /* some error occured */
  470. return CO_DEAD;
  471. }
  472. }
  473. static int luaB_costatus (lua_State *L) {
  474. lua_State *co = lua_tothread(L, 1);
  475. luaL_argcheck(L, co, 1, "coroutine expected");
  476. lua_pushstring(L, statnames[costatus(L, co)]);
  477. return 1;
  478. }
  479. static int auxresume (lua_State *L, lua_State *co, int narg) {
  480. int status = costatus(L, co);
  481. if (!lua_checkstack(co, narg))
  482. luaL_error(L, "too many arguments to resume");
  483. if (status != CO_SUS) {
  484. lua_pushfstring(L, "cannot resume %s coroutine", statnames[status]);
  485. return -1; /* error flag */
  486. }
  487. lua_xmove(L, co, narg);
  488. lua_setlevel(L, co);
  489. status = lua_resume(co, narg);
  490. if (status == 0 || status == LUA_YIELD) {
  491. int nres = lua_gettop(co);
  492. if (!lua_checkstack(L, nres + 1))
  493. luaL_error(L, "too many results to resume");
  494. lua_xmove(co, L, nres); /* move yielded values */
  495. return nres;
  496. }
  497. else {
  498. lua_xmove(co, L, 1); /* move error message */
  499. return -1; /* error flag */
  500. }
  501. }
  502. static int luaB_coresume (lua_State *L) {
  503. lua_State *co = lua_tothread(L, 1);
  504. int r;
  505. luaL_argcheck(L, co, 1, "coroutine expected");
  506. r = auxresume(L, co, lua_gettop(L) - 1);
  507. if (r < 0) {
  508. lua_pushboolean(L, 0);
  509. lua_insert(L, -2);
  510. return 2; /* return false + error message */
  511. }
  512. else {
  513. lua_pushboolean(L, 1);
  514. lua_insert(L, -(r + 1));
  515. return r + 1; /* return true + `resume' returns */
  516. }
  517. }
  518. static int luaB_auxwrap (lua_State *L) {
  519. lua_State *co = lua_tothread(L, lua_upvalueindex(1));
  520. int r = auxresume(L, co, lua_gettop(L));
  521. if (r < 0) {
  522. if (lua_isstring(L, -1)) { /* error object is a string? */
  523. luaL_where(L, 1); /* add extra info */
  524. lua_insert(L, -2);
  525. lua_concat(L, 2);
  526. }
  527. lua_error(L); /* propagate error */
  528. }
  529. return r;
  530. }
  531. static int luaB_cocreate (lua_State *L) {
  532. lua_State *NL = lua_newthread(L);
  533. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
  534. "Lua function expected");
  535. lua_pushvalue(L, 1); /* move function to top */
  536. lua_xmove(L, NL, 1); /* move function from L to NL */
  537. return 1;
  538. }
  539. static int luaB_cowrap (lua_State *L) {
  540. luaB_cocreate(L);
  541. lua_pushcclosure(L, luaB_auxwrap, 1);
  542. return 1;
  543. }
  544. static int luaB_yield (lua_State *L) {
  545. return lua_yield(L, lua_gettop(L));
  546. }
  547. static int luaB_corunning (lua_State *L) {
  548. if (lua_pushthread(L))
  549. lua_pushnil(L); /* main thread is not a coroutine */
  550. return 1;
  551. }
  552. LROT_PUBLIC_BEGIN(co_funcs)
  553. LROT_FUNCENTRY( create, luaB_cocreate )
  554. LROT_FUNCENTRY( resume, luaB_coresume )
  555. LROT_FUNCENTRY( running, luaB_corunning )
  556. LROT_FUNCENTRY( status, luaB_costatus )
  557. LROT_FUNCENTRY( wrap, luaB_cowrap )
  558. LROT_FUNCENTRY( yield, luaB_yield )
  559. LROT_END (co_funcs, NULL, 0)
  560. /* }====================================================== */
  561. static void auxopen (lua_State *L, const char *name,
  562. lua_CFunction f, lua_CFunction u) {
  563. lua_pushcfunction(L, u);
  564. lua_pushcclosure(L, f, 1);
  565. lua_setfield(L, -2, name);
  566. }
  567. static void base_open (lua_State *L) {
  568. /* set global _G */
  569. lua_pushvalue(L, LUA_GLOBALSINDEX);
  570. lua_setglobal(L, "_G");
  571. /* open lib into global table */
  572. luaL_register_light(L, "_G", &((luaL_Reg) {0}));
  573. lua_pushrotable(L, LROT_TABLEREF(G_meta));
  574. lua_setmetatable(L, LUA_GLOBALSINDEX);
  575. lua_pushliteral(L, LUA_VERSION);
  576. lua_setglobal(L, "_VERSION"); /* set global _VERSION */
  577. /* `ipairs' and `pairs' need auxliliary functions as upvalues */
  578. auxopen(L, "ipairs", luaB_ipairs, ipairsaux);
  579. auxopen(L, "pairs", luaB_pairs, luaB_next);
  580. /* `newproxy' needs a weaktable as upvalue */
  581. lua_createtable(L, 0, 1); /* new table `w' */
  582. lua_pushvalue(L, -1); /* `w' will be its own metatable */
  583. lua_setmetatable(L, -2);
  584. lua_pushliteral(L, "kv");
  585. lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */
  586. lua_pushcclosure(L, luaB_newproxy, 1);
  587. lua_setglobal(L, "newproxy"); /* set global `newproxy' */
  588. }
  589. LUALIB_API int luaopen_base (lua_State *L) {
  590. base_open(L);
  591. return 1;
  592. }