lbaselib.c 15 KB

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