lbaselib.c 15 KB

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