lbaselib.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. ** $Id: lbaselib.c,v 1.88 2002/06/26 20:36:17 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. #include "lua.h"
  11. #include "lauxlib.h"
  12. #include "luadebug.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_opt_int(L, 2, 10);
  41. if (base == 10) { /* standard conversion */
  42. luaL_check_any(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_check_string(L, 1);
  50. char *s2;
  51. unsigned long n;
  52. luaL_arg_check(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. luaL_check_any(L, 1);
  67. return lua_error(L);
  68. }
  69. static int luaB_getmetatable (lua_State *L) {
  70. luaL_check_any(L, 1);
  71. if (!lua_getmetatable(L, 1)) {
  72. lua_pushnil(L);
  73. return 1; /* no metatable */
  74. }
  75. else {
  76. lua_pushliteral(L, "__metatable");
  77. lua_rawget(L, -2);
  78. if (lua_isnil(L, -1))
  79. lua_pop(L, 1);
  80. /* otherwise returns metatable.__metatable */
  81. }
  82. return 1;
  83. }
  84. static int luaB_setmetatable (lua_State *L) {
  85. int t = lua_type(L, 2);
  86. luaL_check_type(L, 1, LUA_TTABLE);
  87. luaL_arg_check(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
  88. "nil or table expected");
  89. lua_settop(L, 2);
  90. lua_setmetatable(L, 1);
  91. return 1;
  92. }
  93. static int luaB_getglobals (lua_State *L) {
  94. int level = luaL_opt_int(L, 1, 1);
  95. luaL_arg_check(L, level >= 0, 2, "level must be non-negative");
  96. lua_getglobals(L, level); /* value to be returned */
  97. return 1;
  98. }
  99. static int luaB_setglobals (lua_State *L) {
  100. int level = luaL_opt_int(L, 2, 1);
  101. luaL_arg_check(L, level >= 1, 2, "level must be positive");
  102. luaL_check_type(L, 1, LUA_TTABLE);
  103. lua_settop(L, 1);
  104. if (lua_setglobals(L, level) == 0)
  105. luaL_error(L, "cannot change global table at level %d", level);
  106. return 0;
  107. }
  108. static int luaB_rawequal (lua_State *L) {
  109. luaL_check_any(L, 1);
  110. luaL_check_any(L, 2);
  111. lua_pushboolean(L, lua_rawequal(L, 1, 2));
  112. return 1;
  113. }
  114. static int luaB_rawget (lua_State *L) {
  115. luaL_check_type(L, 1, LUA_TTABLE);
  116. luaL_check_any(L, 2);
  117. lua_rawget(L, 1);
  118. return 1;
  119. }
  120. static int luaB_rawset (lua_State *L) {
  121. luaL_check_type(L, 1, LUA_TTABLE);
  122. luaL_check_any(L, 2);
  123. luaL_check_any(L, 3);
  124. lua_rawset(L, 1);
  125. return 1;
  126. }
  127. static int luaB_gcinfo (lua_State *L) {
  128. lua_pushnumber(L, lua_getgccount(L));
  129. lua_pushnumber(L, lua_getgcthreshold(L));
  130. return 2;
  131. }
  132. static int luaB_collectgarbage (lua_State *L) {
  133. lua_setgcthreshold(L, luaL_opt_int(L, 1, 0));
  134. return 0;
  135. }
  136. static int luaB_type (lua_State *L) {
  137. luaL_check_any(L, 1);
  138. lua_pushstring(L, lua_typename(L, lua_type(L, 1)));
  139. return 1;
  140. }
  141. static int luaB_next (lua_State *L) {
  142. luaL_check_type(L, 1, LUA_TTABLE);
  143. lua_settop(L, 2); /* create a 2nd argument if there isn't one */
  144. if (lua_next(L, 1))
  145. return 2;
  146. else {
  147. lua_pushnil(L);
  148. return 1;
  149. }
  150. }
  151. static int luaB_pairs (lua_State *L) {
  152. luaL_check_type(L, 1, LUA_TTABLE);
  153. lua_getglobal(L, "next"); /* return generator, */
  154. lua_pushvalue(L, 1); /* state, */
  155. lua_pushnil(L); /* and initial value */
  156. return 3;
  157. }
  158. static int luaB_ipairs (lua_State *L) {
  159. lua_Number i = lua_tonumber(L, 2);
  160. luaL_check_type(L, 1, LUA_TTABLE);
  161. if (i == 0 && lua_isnull(L, 2)) { /* `for' start? */
  162. lua_getglobal(L, "ipairs"); /* return generator, */
  163. lua_pushvalue(L, 1); /* state, */
  164. lua_pushnumber(L, 0); /* and initial value */
  165. return 3;
  166. }
  167. else { /* `for' step */
  168. i++; /* next value */
  169. lua_pushnumber(L, i);
  170. lua_rawgeti(L, 1, (int)i);
  171. return (lua_isnil(L, -1)) ? 0 : 2;
  172. }
  173. }
  174. static int passresults (lua_State *L, int status) {
  175. if (status == 0) return 1;
  176. else {
  177. int numres = (status == LUA_ERRRUN) ? 3 : 2;
  178. lua_pushnil(L);
  179. lua_insert(L, -numres);
  180. return numres;
  181. }
  182. }
  183. static int luaB_loadstring (lua_State *L) {
  184. size_t l;
  185. const char *s = luaL_check_lstr(L, 1, &l);
  186. const char *chunkname = luaL_opt_string(L, 2, s);
  187. return passresults(L, luaL_loadbuffer(L, s, l, chunkname));
  188. }
  189. static int luaB_loadfile (lua_State *L) {
  190. const char *fname = luaL_opt_string(L, 1, NULL);
  191. return passresults(L, luaL_loadfile(L, fname));
  192. }
  193. static int luaB_dofile (lua_State *L) {
  194. const char *fname = luaL_opt_string(L, 1, NULL);
  195. int status = luaL_loadfile(L, fname);
  196. if (status != 0) lua_error(L);
  197. lua_call(L, 0, LUA_MULTRET);
  198. return lua_gettop(L) - 1;
  199. }
  200. static int luaB_assert (lua_State *L) {
  201. luaL_check_any(L, 1);
  202. if (!lua_toboolean(L, 1))
  203. return luaL_error(L, "%s", luaL_opt_string(L, 2, "assertion failed!"));
  204. lua_settop(L, 1);
  205. return 1;
  206. }
  207. static int luaB_unpack (lua_State *L) {
  208. int n, i;
  209. luaL_check_type(L, 1, LUA_TTABLE);
  210. lua_pushliteral(L, "n");
  211. lua_rawget(L, 1);
  212. n = (lua_isnumber(L, -1)) ? (int)lua_tonumber(L, -1) : -1;
  213. for (i=0; i<n || n==-1; i++) { /* push arg[1...n] */
  214. luaL_check_stack(L, 1, "table too big to unpack");
  215. lua_rawgeti(L, 1, i+1);
  216. if (n == -1) { /* no explicit limit? */
  217. if (lua_isnil(L, -1)) { /* stop at first `nil' element */
  218. lua_pop(L, 1); /* remove `nil' */
  219. break;
  220. }
  221. }
  222. }
  223. return i;
  224. }
  225. static int luaB_pcall (lua_State *L) {
  226. int status;
  227. luaL_check_any(L, 1);
  228. status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET);
  229. if (status != 0)
  230. return passresults(L, status);
  231. else {
  232. lua_pushboolean(L, 1);
  233. lua_insert(L, 1);
  234. return lua_gettop(L); /* return `true' + all results */
  235. }
  236. }
  237. static int luaB_tostring (lua_State *L) {
  238. char buff[64];
  239. luaL_checkany(L, 1);
  240. if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */
  241. return 1; /* use its value */
  242. switch (lua_type(L, 1)) {
  243. case LUA_TNUMBER:
  244. lua_pushstring(L, lua_tostring(L, 1));
  245. return 1;
  246. case LUA_TSTRING:
  247. lua_pushvalue(L, 1);
  248. return 1;
  249. case LUA_TBOOLEAN:
  250. lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
  251. return 1;
  252. case LUA_TTABLE:
  253. sprintf(buff, "table: %p", lua_topointer(L, 1));
  254. break;
  255. case LUA_TFUNCTION:
  256. sprintf(buff, "function: %p", lua_topointer(L, 1));
  257. break;
  258. case LUA_TUSERDATA:
  259. case LUA_TUDATAVAL:
  260. sprintf(buff, "userdata: %p", lua_touserdata(L, 1));
  261. break;
  262. case LUA_TNIL:
  263. lua_pushliteral(L, "nil");
  264. return 1;
  265. }
  266. lua_pushstring(L, buff);
  267. return 1;
  268. }
  269. static int luaB_newproxy (lua_State *L) {
  270. lua_settop(L, 1);
  271. lua_newuserdata(L, 0); /* create proxy */
  272. if (lua_toboolean(L, 1) == 0)
  273. return 1; /* no metatable */
  274. else if (lua_isboolean(L, 1)) {
  275. lua_newtable(L); /* create a new metatable `m' ... */
  276. lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */
  277. lua_pushboolean(L, 1);
  278. lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */
  279. }
  280. else {
  281. int validproxy = 0; /* to check if weaktable[metatable(u)] == true */
  282. if (lua_getmetatable(L, 1)) {
  283. lua_rawget(L, lua_upvalueindex(1));
  284. validproxy = lua_toboolean(L, -1);
  285. lua_pop(L, 1); /* remove value */
  286. }
  287. luaL_arg_check(L, validproxy, 1, "boolean/proxy expected");
  288. lua_getmetatable(L, 1); /* metatable is valid; get it */
  289. }
  290. lua_setmetatable(L, 2);
  291. return 1;
  292. }
  293. /*
  294. ** {======================================================
  295. ** `require' function
  296. ** =======================================================
  297. */
  298. /* name of global that holds table with loaded packages */
  299. #define REQTAB "_LOADED"
  300. /* name of global that holds the search path for packages */
  301. #define LUA_PATH "LUA_PATH"
  302. #ifndef LUA_PATH_SEP
  303. #define LUA_PATH_SEP ';'
  304. #endif
  305. #ifndef LUA_PATH_DEFAULT
  306. #define LUA_PATH_DEFAULT "?;?.lua"
  307. #endif
  308. static const char *getpath (lua_State *L) {
  309. const char *path;
  310. lua_getglobal(L, LUA_PATH); /* try global variable */
  311. path = lua_tostring(L, -1);
  312. lua_pop(L, 1);
  313. if (path) return path;
  314. path = getenv(LUA_PATH); /* else try environment variable */
  315. if (path) return path;
  316. return LUA_PATH_DEFAULT; /* else use default */
  317. }
  318. static const char *pushnextpath (lua_State *L, const char *path) {
  319. const char *l;
  320. if (*path == '\0') return NULL; /* no more pathes */
  321. if (*path == LUA_PATH_SEP) path++; /* skip separator */
  322. l = strchr(path, LUA_PATH_SEP); /* find next separator */
  323. if (l == NULL) l = path+strlen(path);
  324. lua_pushlstring(L, path, l - path); /* directory name */
  325. return l;
  326. }
  327. static void pushcomposename (lua_State *L) {
  328. const char *path = lua_tostring(L, -1);
  329. const char *wild = strchr(path, '?');
  330. if (wild == NULL) return; /* no wild char; path is the file name */
  331. lua_pushlstring(L, path, wild - path);
  332. lua_pushvalue(L, 1); /* package name */
  333. lua_pushstring(L, wild + 1);
  334. lua_concat(L, 3);
  335. }
  336. static int luaB_require (lua_State *L) {
  337. const char *path;
  338. int status = LUA_ERRFILE; /* not found (yet) */
  339. luaL_check_string(L, 1);
  340. lua_settop(L, 1);
  341. lua_pushvalue(L, 1);
  342. lua_setglobal(L, "_REQUIREDNAME");
  343. lua_getglobal(L, REQTAB);
  344. if (!lua_istable(L, 2)) return luaL_error(L, "`" REQTAB "' is not a table");
  345. path = getpath(L);
  346. lua_pushvalue(L, 1); /* check package's name in book-keeping table */
  347. lua_rawget(L, 2);
  348. if (!lua_isnil(L, -1)) /* is it there? */
  349. return 0; /* package is already loaded */
  350. else { /* must load it */
  351. while (status == LUA_ERRFILE) {
  352. lua_settop(L, 3); /* reset stack position */
  353. if ((path = pushnextpath(L, path)) == NULL) break;
  354. pushcomposename(L);
  355. status = luaL_loadfile(L, lua_tostring(L, -1)); /* try to load it */
  356. }
  357. }
  358. switch (status) {
  359. case 0: {
  360. lua_call(L, 0, 0); /* run loaded module */
  361. lua_pushvalue(L, 1);
  362. lua_pushboolean(L, 1);
  363. lua_rawset(L, 2); /* mark it as loaded */
  364. return 0;
  365. }
  366. case LUA_ERRFILE: { /* file not found */
  367. return luaL_error(L, "could not load package `%s' from path `%s'",
  368. lua_tostring(L, 1), getpath(L));
  369. }
  370. default: {
  371. return luaL_error(L, "error loading package\n%s", lua_tostring(L, -1));
  372. }
  373. }
  374. }
  375. /* }====================================================== */
  376. static const luaL_reg base_funcs[] = {
  377. {"error", luaB_error},
  378. {"getmetatable", luaB_getmetatable},
  379. {"setmetatable", luaB_setmetatable},
  380. {"getglobals", luaB_getglobals},
  381. {"setglobals", luaB_setglobals},
  382. {"next", luaB_next},
  383. {"ipairs", luaB_ipairs},
  384. {"pairs", luaB_pairs},
  385. {"print", luaB_print},
  386. {"tonumber", luaB_tonumber},
  387. {"tostring", luaB_tostring},
  388. {"type", luaB_type},
  389. {"assert", luaB_assert},
  390. {"unpack", luaB_unpack},
  391. {"rawequal", luaB_rawequal},
  392. {"rawget", luaB_rawget},
  393. {"rawset", luaB_rawset},
  394. {"pcall", luaB_pcall},
  395. {"collectgarbage", luaB_collectgarbage},
  396. {"gcinfo", luaB_gcinfo},
  397. {"loadfile", luaB_loadfile},
  398. {"dofile", luaB_dofile},
  399. {"loadstring", luaB_loadstring},
  400. {"require", luaB_require},
  401. {NULL, NULL}
  402. };
  403. /*
  404. ** {======================================================
  405. ** Coroutine library
  406. ** =======================================================
  407. */
  408. static int luaB_resume (lua_State *L) {
  409. lua_State *co = (lua_State *)lua_getfrombox(L, lua_upvalueindex(1));
  410. int status;
  411. lua_settop(L, 0);
  412. status = lua_resume(L, co);
  413. if (status != 0) {
  414. if (status == LUA_ERRRUN) {
  415. if (lua_isstring(L, -1) && lua_isstring(L, -2))
  416. lua_concat(L, 2);
  417. else
  418. lua_pop(L, 1);
  419. }
  420. return lua_error(L);
  421. }
  422. return lua_gettop(L);
  423. }
  424. static int gc_coroutine (lua_State *L) {
  425. lua_State *co = (lua_State *)lua_getfrombox(L, 1);
  426. lua_closethread(L, co);
  427. return 0;
  428. }
  429. static int luaB_coroutine (lua_State *L) {
  430. lua_State *NL;
  431. int ref;
  432. int i;
  433. int n = lua_gettop(L);
  434. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
  435. "Lua function expected");
  436. NL = lua_newthread(L);
  437. if (NL == NULL) return luaL_error(L, "unable to create new thread");
  438. /* move function and arguments from L to NL */
  439. for (i=0; i<n; i++) {
  440. ref = lua_ref(L, 1);
  441. lua_getref(NL, ref);
  442. lua_insert(NL, 1);
  443. lua_unref(L, ref);
  444. }
  445. lua_cobegin(NL, n-1);
  446. lua_newpointerbox(L, NL);
  447. lua_pushliteral(L, "Coroutine");
  448. lua_rawget(L, LUA_REGISTRYINDEX);
  449. lua_setmetatable(L, -2);
  450. lua_pushcclosure(L, luaB_resume, 1);
  451. return 1;
  452. }
  453. static int luaB_yield (lua_State *L) {
  454. return lua_yield(L, lua_gettop(L));
  455. }
  456. static const luaL_reg co_funcs[] = {
  457. {"create", luaB_coroutine},
  458. {"yield", luaB_yield},
  459. {NULL, NULL}
  460. };
  461. static void co_open (lua_State *L) {
  462. luaL_opennamedlib(L, LUA_COLIBNAME, co_funcs, 0);
  463. /* create metatable for coroutines */
  464. lua_pushliteral(L, "Coroutine");
  465. lua_newtable(L);
  466. lua_pushliteral(L, "__gc");
  467. lua_pushcfunction(L, gc_coroutine);
  468. lua_rawset(L, -3);
  469. lua_rawset(L, LUA_REGISTRYINDEX);
  470. }
  471. /* }====================================================== */
  472. static void base_open (lua_State *L) {
  473. lua_pushliteral(L, "_G");
  474. lua_pushvalue(L, LUA_GLOBALSINDEX);
  475. luaL_openlib(L, base_funcs, 0); /* open lib into global table */
  476. lua_pushliteral(L, "_VERSION");
  477. lua_pushliteral(L, LUA_VERSION);
  478. lua_rawset(L, -3); /* set global _VERSION */
  479. /* `newproxy' needs a weaktable as upvalue */
  480. lua_pushliteral(L, "newproxy");
  481. lua_newtable(L); /* new table `w' */
  482. lua_newtable(L); /* create `w's metatable */
  483. lua_pushliteral(L, "__mode");
  484. lua_pushliteral(L, "k");
  485. lua_rawset(L, -3); /* metatable(w).__mode = "k" */
  486. lua_setmetatable(L, -2);
  487. lua_pushcclosure(L, luaB_newproxy, 1);
  488. lua_rawset(L, -3); /* set global `newproxy' */
  489. lua_rawset(L, -1); /* set global _G */
  490. }
  491. LUALIB_API int lua_baselibopen (lua_State *L) {
  492. base_open(L);
  493. co_open(L);
  494. lua_newtable(L);
  495. lua_setglobal(L, REQTAB);
  496. return 0;
  497. }