lbaselib.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /*
  2. ** $Id: lbaselib.c,v 1.114 2002/12/04 17:38:31 roberto Exp roberto $
  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. #include "lua.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. static int luaB_print (lua_State *L) {
  21. int n = lua_gettop(L); /* number of arguments */
  22. int i;
  23. lua_getglobal(L, "tostring");
  24. for (i=1; i<=n; i++) {
  25. const char *s;
  26. lua_pushvalue(L, -1); /* function to be called */
  27. lua_pushvalue(L, i); /* value to print */
  28. lua_call(L, 1, 1);
  29. s = lua_tostring(L, -1); /* get result */
  30. if (s == NULL)
  31. return luaL_error(L, "`tostring' must return a string to `print'");
  32. if (i>1) fputs("\t", stdout);
  33. fputs(s, stdout);
  34. lua_pop(L, 1); /* pop result */
  35. }
  36. fputs("\n", stdout);
  37. return 0;
  38. }
  39. static int luaB_tonumber (lua_State *L) {
  40. int base = luaL_optint(L, 2, 10);
  41. if (base == 10) { /* standard conversion */
  42. luaL_checkany(L, 1);
  43. if (lua_isnumber(L, 1)) {
  44. lua_pushnumber(L, lua_tonumber(L, 1));
  45. return 1;
  46. }
  47. }
  48. else {
  49. const char *s1 = luaL_checkstring(L, 1);
  50. char *s2;
  51. unsigned long n;
  52. luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
  53. n = strtoul(s1, &s2, base);
  54. if (s1 != s2) { /* at least one valid digit? */
  55. while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
  56. if (*s2 == '\0') { /* no invalid trailing characters? */
  57. lua_pushnumber(L, n);
  58. return 1;
  59. }
  60. }
  61. }
  62. lua_pushnil(L); /* else not a number */
  63. return 1;
  64. }
  65. static int luaB_error (lua_State *L) {
  66. int level = luaL_optint(L, 2, 1);
  67. luaL_checkany(L, 1);
  68. if (!lua_isstring(L, 1) || level == 0)
  69. lua_pushvalue(L, 1); /* propagate error message without changes */
  70. else { /* add extra information */
  71. luaL_where(L, level);
  72. lua_pushvalue(L, 1);
  73. lua_concat(L, 2);
  74. }
  75. return lua_error(L);
  76. }
  77. static int luaB_getmetatable (lua_State *L) {
  78. luaL_checkany(L, 1);
  79. if (!lua_getmetatable(L, 1)) {
  80. lua_pushnil(L);
  81. return 1; /* no metatable */
  82. }
  83. luaL_getmetafield(L, 1, "__metatable");
  84. return 1; /* returns either __metatable field (if present) or metatable */
  85. }
  86. static int luaB_setmetatable (lua_State *L) {
  87. int t = lua_type(L, 2);
  88. luaL_checktype(L, 1, LUA_TTABLE);
  89. luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
  90. "nil or table expected");
  91. if (luaL_getmetafield(L, 1, "__metatable"))
  92. luaL_error(L, "cannot change a protected metatable");
  93. lua_settop(L, 2);
  94. lua_setmetatable(L, 1);
  95. return 1;
  96. }
  97. static void getfunc (lua_State *L) {
  98. if (lua_isfunction(L, 1)) lua_pushvalue(L, 1);
  99. else {
  100. lua_Debug ar;
  101. int level = luaL_optint(L, 1, 1);
  102. luaL_argcheck(L, level >= 0, 1, "level must be non-negative");
  103. if (lua_getstack(L, level, &ar) == 0)
  104. luaL_argerror(L, 1, "invalid level");
  105. lua_getinfo(L, "f", &ar);
  106. }
  107. }
  108. static int aux_getglobals (lua_State *L) {
  109. lua_getglobals(L, -1);
  110. lua_pushliteral(L, "__globals");
  111. lua_rawget(L, -2);
  112. return !lua_isnil(L, -1);
  113. }
  114. static int luaB_getglobals (lua_State *L) {
  115. getfunc(L);
  116. if (!aux_getglobals(L)) /* __globals not defined? */
  117. lua_pop(L, 1); /* remove it, to return real globals */
  118. return 1;
  119. }
  120. static int luaB_setglobals (lua_State *L) {
  121. luaL_checktype(L, 2, LUA_TTABLE);
  122. getfunc(L);
  123. if (aux_getglobals(L)) /* __globals defined? */
  124. luaL_error(L, "cannot change a protected global table");
  125. else
  126. lua_pop(L, 2); /* remove __globals and real global table */
  127. lua_pushvalue(L, 2);
  128. if (lua_setglobals(L, -2) == 0)
  129. luaL_error(L, "cannot change global table of given function");
  130. return 0;
  131. }
  132. static int luaB_rawequal (lua_State *L) {
  133. luaL_checkany(L, 1);
  134. luaL_checkany(L, 2);
  135. lua_pushboolean(L, lua_rawequal(L, 1, 2));
  136. return 1;
  137. }
  138. static int luaB_rawget (lua_State *L) {
  139. luaL_checktype(L, 1, LUA_TTABLE);
  140. luaL_checkany(L, 2);
  141. lua_rawget(L, 1);
  142. return 1;
  143. }
  144. static int luaB_rawset (lua_State *L) {
  145. luaL_checktype(L, 1, LUA_TTABLE);
  146. luaL_checkany(L, 2);
  147. luaL_checkany(L, 3);
  148. lua_rawset(L, 1);
  149. return 1;
  150. }
  151. static int luaB_gcinfo (lua_State *L) {
  152. lua_pushnumber(L, lua_getgccount(L));
  153. lua_pushnumber(L, lua_getgcthreshold(L));
  154. return 2;
  155. }
  156. static int luaB_collectgarbage (lua_State *L) {
  157. lua_setgcthreshold(L, luaL_optint(L, 1, 0));
  158. return 0;
  159. }
  160. static int luaB_type (lua_State *L) {
  161. luaL_checkany(L, 1);
  162. lua_pushstring(L, lua_typename(L, lua_type(L, 1)));
  163. return 1;
  164. }
  165. static int luaB_next (lua_State *L) {
  166. luaL_checktype(L, 1, LUA_TTABLE);
  167. lua_settop(L, 2); /* create a 2nd argument if there isn't one */
  168. if (lua_next(L, 1))
  169. return 2;
  170. else {
  171. lua_pushnil(L);
  172. return 1;
  173. }
  174. }
  175. static int luaB_pairs (lua_State *L) {
  176. luaL_checktype(L, 1, LUA_TTABLE);
  177. lua_pushliteral(L, "next");
  178. lua_rawget(L, LUA_GLOBALSINDEX); /* return generator, */
  179. lua_pushvalue(L, 1); /* state, */
  180. lua_pushnil(L); /* and initial value */
  181. return 3;
  182. }
  183. static int luaB_ipairs (lua_State *L) {
  184. lua_Number i = lua_tonumber(L, 2);
  185. luaL_checktype(L, 1, LUA_TTABLE);
  186. if (i == 0 && lua_isnone(L, 2)) { /* `for' start? */
  187. lua_pushliteral(L, "ipairs");
  188. lua_rawget(L, LUA_GLOBALSINDEX); /* return generator, */
  189. lua_pushvalue(L, 1); /* state, */
  190. lua_pushnumber(L, 0); /* and initial value */
  191. return 3;
  192. }
  193. else { /* `for' step */
  194. i++; /* next value */
  195. lua_pushnumber(L, i);
  196. lua_rawgeti(L, 1, (int)i);
  197. return (lua_isnil(L, -1)) ? 0 : 2;
  198. }
  199. }
  200. static int load_aux (lua_State *L, int status) {
  201. if (status == 0) { /* OK? */
  202. lua_Debug ar;
  203. lua_getstack(L, 1, &ar);
  204. lua_getinfo(L, "f", &ar); /* get calling function */
  205. lua_getglobals(L, -1); /* get its global table */
  206. lua_setglobals(L, -3); /* set it as the global table of the new chunk */
  207. lua_pop(L, 1); /* remove calling function */
  208. return 1;
  209. }
  210. else {
  211. lua_pushnil(L);
  212. lua_insert(L, -2);
  213. return 2;
  214. }
  215. }
  216. static int luaB_loadstring (lua_State *L) {
  217. size_t l;
  218. const char *s = luaL_checklstring(L, 1, &l);
  219. const char *chunkname = luaL_optstring(L, 2, s);
  220. return load_aux(L, luaL_loadbuffer(L, s, l, chunkname));
  221. }
  222. static int luaB_loadfile (lua_State *L) {
  223. const char *fname = luaL_optstring(L, 1, NULL);
  224. return load_aux(L, luaL_loadfile(L, fname));
  225. }
  226. static int luaB_dofile (lua_State *L) {
  227. const char *fname = luaL_optstring(L, 1, NULL);
  228. int status = luaL_loadfile(L, fname);
  229. if (status != 0) lua_error(L);
  230. lua_call(L, 0, LUA_MULTRET);
  231. return lua_gettop(L) - 1;
  232. }
  233. static int luaB_assert (lua_State *L) {
  234. luaL_checkany(L, 1);
  235. if (!lua_toboolean(L, 1))
  236. return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!"));
  237. lua_settop(L, 1);
  238. return 1;
  239. }
  240. static int luaB_unpack (lua_State *L) {
  241. int n, i;
  242. luaL_checktype(L, 1, LUA_TTABLE);
  243. lua_pushliteral(L, "n");
  244. lua_rawget(L, 1);
  245. n = (lua_isnumber(L, -1)) ? (int)lua_tonumber(L, -1) : -1;
  246. for (i=0; i<n || n==-1; i++) { /* push arg[1...n] */
  247. luaL_checkstack(L, LUA_MINSTACK, "table too big to unpack");
  248. lua_rawgeti(L, 1, i+1);
  249. if (n == -1) { /* no explicit limit? */
  250. if (lua_isnil(L, -1)) { /* stop at first `nil' element */
  251. lua_pop(L, 1); /* remove `nil' */
  252. break;
  253. }
  254. }
  255. }
  256. return i;
  257. }
  258. static int luaB_pcall (lua_State *L) {
  259. int status;
  260. luaL_checkany(L, 1);
  261. status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0);
  262. lua_pushboolean(L, (status == 0));
  263. lua_insert(L, 1);
  264. return lua_gettop(L); /* return status + all results */
  265. }
  266. static int luaB_xpcall (lua_State *L) {
  267. int status;
  268. luaL_checkany(L, 2);
  269. lua_settop(L, 2);
  270. lua_insert(L, 1); /* put error function under function to be called */
  271. status = lua_pcall(L, 0, LUA_MULTRET, 1);
  272. lua_pushboolean(L, (status == 0));
  273. lua_replace(L, 1);
  274. return lua_gettop(L); /* return status + all results */
  275. }
  276. static int luaB_tostring (lua_State *L) {
  277. char buff[64];
  278. luaL_checkany(L, 1);
  279. if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */
  280. return 1; /* use its value */
  281. switch (lua_type(L, 1)) {
  282. case LUA_TNUMBER:
  283. lua_pushstring(L, lua_tostring(L, 1));
  284. return 1;
  285. case LUA_TSTRING:
  286. lua_pushvalue(L, 1);
  287. return 1;
  288. case LUA_TBOOLEAN:
  289. lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
  290. return 1;
  291. case LUA_TTABLE:
  292. sprintf(buff, "table: %p", lua_topointer(L, 1));
  293. break;
  294. case LUA_TFUNCTION:
  295. sprintf(buff, "function: %p", lua_topointer(L, 1));
  296. break;
  297. case LUA_TUSERDATA:
  298. case LUA_TLIGHTUSERDATA:
  299. sprintf(buff, "userdata: %p", lua_touserdata(L, 1));
  300. break;
  301. case LUA_TTHREAD:
  302. sprintf(buff, "thread: %p", (void *)lua_tothread(L, 1));
  303. break;
  304. case LUA_TNIL:
  305. lua_pushliteral(L, "nil");
  306. return 1;
  307. }
  308. lua_pushstring(L, buff);
  309. return 1;
  310. }
  311. static int luaB_newproxy (lua_State *L) {
  312. lua_settop(L, 1);
  313. lua_newuserdata(L, 0); /* create proxy */
  314. if (lua_toboolean(L, 1) == 0)
  315. return 1; /* no metatable */
  316. else if (lua_isboolean(L, 1)) {
  317. lua_newtable(L); /* create a new metatable `m' ... */
  318. lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */
  319. lua_pushboolean(L, 1);
  320. lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */
  321. }
  322. else {
  323. int validproxy = 0; /* to check if weaktable[metatable(u)] == true */
  324. if (lua_getmetatable(L, 1)) {
  325. lua_rawget(L, lua_upvalueindex(1));
  326. validproxy = lua_toboolean(L, -1);
  327. lua_pop(L, 1); /* remove value */
  328. }
  329. luaL_argcheck(L, validproxy, 1, "boolean or proxy expected");
  330. lua_getmetatable(L, 1); /* metatable is valid; get it */
  331. }
  332. lua_setmetatable(L, 2);
  333. return 1;
  334. }
  335. /*
  336. ** {======================================================
  337. ** `require' function
  338. ** =======================================================
  339. */
  340. /* name of global that holds table with loaded packages */
  341. #define REQTAB "_LOADED"
  342. /* name of global that holds the search path for packages */
  343. #define LUA_PATH "LUA_PATH"
  344. #ifndef LUA_PATH_SEP
  345. #define LUA_PATH_SEP ';'
  346. #endif
  347. #ifndef LUA_PATH_DEFAULT
  348. #define LUA_PATH_DEFAULT "?;?.lua"
  349. #endif
  350. static const char *getpath (lua_State *L) {
  351. const char *path;
  352. lua_getglobal(L, LUA_PATH); /* try global variable */
  353. path = lua_tostring(L, -1);
  354. lua_pop(L, 1);
  355. if (path) return path;
  356. path = getenv(LUA_PATH); /* else try environment variable */
  357. if (path) return path;
  358. return LUA_PATH_DEFAULT; /* else use default */
  359. }
  360. static const char *pushnextpath (lua_State *L, const char *path) {
  361. const char *l;
  362. if (*path == '\0') return NULL; /* no more paths */
  363. if (*path == LUA_PATH_SEP) path++; /* skip separator */
  364. l = strchr(path, LUA_PATH_SEP); /* find next separator */
  365. if (l == NULL) l = path+strlen(path);
  366. lua_pushlstring(L, path, l - path); /* directory name */
  367. return l;
  368. }
  369. static void pushcomposename (lua_State *L) {
  370. const char *path = lua_tostring(L, -1);
  371. const char *wild = strchr(path, '?');
  372. if (wild == NULL) return; /* no wild char; path is the file name */
  373. lua_pushlstring(L, path, wild - path);
  374. lua_pushvalue(L, 1); /* package name */
  375. lua_pushstring(L, wild + 1);
  376. lua_concat(L, 3);
  377. }
  378. static int luaB_require (lua_State *L) {
  379. const char *path;
  380. int status = LUA_ERRFILE; /* not found (yet) */
  381. luaL_checkstring(L, 1);
  382. lua_settop(L, 1);
  383. lua_pushvalue(L, 1);
  384. lua_setglobal(L, "_REQUIREDNAME");
  385. lua_getglobal(L, REQTAB);
  386. if (!lua_istable(L, 2)) return luaL_error(L, "`" REQTAB "' is not a table");
  387. path = getpath(L);
  388. lua_pushvalue(L, 1); /* check package's name in book-keeping table */
  389. lua_rawget(L, 2);
  390. if (!lua_isnil(L, -1)) /* is it there? */
  391. return 0; /* package is already loaded */
  392. else { /* must load it */
  393. while (status == LUA_ERRFILE) {
  394. lua_settop(L, 3); /* reset stack position */
  395. if ((path = pushnextpath(L, path)) == NULL) break;
  396. pushcomposename(L);
  397. status = luaL_loadfile(L, lua_tostring(L, -1)); /* try to load it */
  398. }
  399. }
  400. switch (status) {
  401. case 0: {
  402. lua_call(L, 0, 0); /* run loaded module */
  403. lua_pushvalue(L, 1);
  404. lua_pushboolean(L, 1);
  405. lua_rawset(L, 2); /* mark it as loaded */
  406. return 0;
  407. }
  408. case LUA_ERRFILE: { /* file not found */
  409. return luaL_error(L, "could not load package `%s' from path `%s'",
  410. lua_tostring(L, 1), getpath(L));
  411. }
  412. default: {
  413. return luaL_error(L, "error loading package\n%s", lua_tostring(L, -1));
  414. }
  415. }
  416. }
  417. /* }====================================================== */
  418. static const luaL_reg base_funcs[] = {
  419. {"error", luaB_error},
  420. {"getmetatable", luaB_getmetatable},
  421. {"setmetatable", luaB_setmetatable},
  422. {"getglobals", luaB_getglobals},
  423. {"setglobals", luaB_setglobals},
  424. {"next", luaB_next},
  425. {"ipairs", luaB_ipairs},
  426. {"pairs", luaB_pairs},
  427. {"print", luaB_print},
  428. {"tonumber", luaB_tonumber},
  429. {"tostring", luaB_tostring},
  430. {"type", luaB_type},
  431. {"assert", luaB_assert},
  432. {"unpack", luaB_unpack},
  433. {"rawequal", luaB_rawequal},
  434. {"rawget", luaB_rawget},
  435. {"rawset", luaB_rawset},
  436. {"pcall", luaB_pcall},
  437. {"xpcall", luaB_xpcall},
  438. {"collectgarbage", luaB_collectgarbage},
  439. {"gcinfo", luaB_gcinfo},
  440. {"loadfile", luaB_loadfile},
  441. {"dofile", luaB_dofile},
  442. {"loadstring", luaB_loadstring},
  443. {"require", luaB_require},
  444. {NULL, NULL}
  445. };
  446. /*
  447. ** {======================================================
  448. ** Coroutine library
  449. ** =======================================================
  450. */
  451. static int auxresume (lua_State *L, lua_State *co, int narg) {
  452. int status;
  453. if (!lua_checkstack(co, narg))
  454. luaL_error(L, "too many arguments to resume");
  455. lua_xmove(L, co, narg);
  456. status = lua_resume(co, narg);
  457. if (status == 0) {
  458. int nres = lua_gettop(co);
  459. if (!lua_checkstack(L, narg))
  460. luaL_error(L, "too many results to resume");
  461. lua_xmove(co, L, nres); /* move yielded values */
  462. return nres;
  463. }
  464. else {
  465. lua_xmove(co, L, 1); /* move error message */
  466. return -1; /* error flag */
  467. }
  468. }
  469. static int luaB_coresume (lua_State *L) {
  470. lua_State *co = lua_tothread(L, 1);
  471. int r;
  472. luaL_argcheck(L, co, 1, "coroutine expected");
  473. r = auxresume(L, co, lua_gettop(L) - 1);
  474. if (r < 0) {
  475. lua_pushboolean(L, 0);
  476. lua_insert(L, -2);
  477. return 2; /* return false + error message */
  478. }
  479. else {
  480. lua_pushboolean(L, 1);
  481. lua_insert(L, -(r + 1));
  482. return r + 1; /* return true + `resume' returns */
  483. }
  484. }
  485. static int luaB_auxwrap (lua_State *L) {
  486. lua_State *co = lua_tothread(L, lua_upvalueindex(1));
  487. int r = auxresume(L, co, lua_gettop(L));
  488. if (r < 0) lua_error(L); /* propagate error */
  489. return r;
  490. }
  491. static int luaB_cocreate (lua_State *L) {
  492. lua_State *NL = lua_newthread(L);
  493. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
  494. "Lua function expected");
  495. lua_pushvalue(L, 1); /* move function to top */
  496. lua_xmove(L, NL, 1); /* move function from L to NL */
  497. return 1;
  498. }
  499. static int luaB_cowrap (lua_State *L) {
  500. luaB_cocreate(L);
  501. lua_pushcclosure(L, luaB_auxwrap, 1);
  502. return 1;
  503. }
  504. static int luaB_yield (lua_State *L) {
  505. return lua_yield(L, lua_gettop(L));
  506. }
  507. static int luaB_costatus (lua_State *L) {
  508. lua_State *co = lua_tothread(L, 1);
  509. luaL_argcheck(L, co, 1, "coroutine expected");
  510. if (L == co) lua_pushliteral(L, "running");
  511. else {
  512. lua_Debug ar;
  513. if (lua_getstack(co, 0, &ar) == 0 && lua_gettop(co) == 0)
  514. lua_pushliteral(L, "dead");
  515. else
  516. lua_pushliteral(L, "suspended");
  517. }
  518. return 1;
  519. }
  520. static const luaL_reg co_funcs[] = {
  521. {"create", luaB_cocreate},
  522. {"wrap", luaB_cowrap},
  523. {"resume", luaB_coresume},
  524. {"yield", luaB_yield},
  525. {"status", luaB_costatus},
  526. {NULL, NULL}
  527. };
  528. /* }====================================================== */
  529. static void base_open (lua_State *L) {
  530. lua_pushliteral(L, "_G");
  531. lua_pushvalue(L, LUA_GLOBALSINDEX);
  532. luaL_openlib(L, NULL, base_funcs, 0); /* open lib into global table */
  533. lua_pushliteral(L, "_VERSION");
  534. lua_pushliteral(L, LUA_VERSION);
  535. lua_rawset(L, -3); /* set global _VERSION */
  536. /* `newproxy' needs a weaktable as upvalue */
  537. lua_pushliteral(L, "newproxy");
  538. lua_newtable(L); /* new table `w' */
  539. lua_pushvalue(L, -1); /* `w' will be its own metatable */
  540. lua_setmetatable(L, -2);
  541. lua_pushliteral(L, "__mode");
  542. lua_pushliteral(L, "k");
  543. lua_rawset(L, -3); /* metatable(w).__mode = "k" */
  544. lua_pushcclosure(L, luaB_newproxy, 1);
  545. lua_rawset(L, -3); /* set global `newproxy' */
  546. lua_rawset(L, -1); /* set global _G */
  547. }
  548. LUALIB_API int lua_baselibopen (lua_State *L) {
  549. base_open(L);
  550. luaL_openlib(L, LUA_COLIBNAME, co_funcs, 0);
  551. lua_newtable(L);
  552. lua_setglobal(L, REQTAB);
  553. return 0;
  554. }