lbaselib.c 18 KB

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