lbaselib.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. ** $Id: lbaselib.c,v 1.75 2002/05/16 19:09:19 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_rawcall(L, 1, 1);
  29. s = lua_tostring(L, -1); /* get result */
  30. if (s == NULL)
  31. return luaL_verror(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. lua_settop(L, 1);
  67. return lua_errorobj(L);
  68. }
  69. static int luaB_metatable (lua_State *L) {
  70. luaL_check_any(L, 1);
  71. if (lua_isnone(L, 2)) {
  72. if (!lua_getmetatable(L, 1))
  73. return 0; /* no metatable */
  74. else {
  75. lua_pushliteral(L, "__metatable");
  76. lua_rawget(L, -2);
  77. if (lua_isnil(L, -1))
  78. lua_pop(L, 1);
  79. }
  80. }
  81. else {
  82. int t = lua_type(L, 2);
  83. luaL_check_type(L, 1, LUA_TTABLE);
  84. luaL_arg_check(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil/table expected");
  85. lua_settop(L, 2);
  86. lua_setmetatable(L, 1);
  87. }
  88. return 1;
  89. }
  90. static int luaB_globals (lua_State *L) {
  91. lua_getglobals(L); /* value to be returned */
  92. if (!lua_isnone(L, 1)) {
  93. luaL_check_type(L, 1, LUA_TTABLE);
  94. lua_pushvalue(L, 1); /* new table of globals */
  95. lua_setglobals(L);
  96. }
  97. return 1;
  98. }
  99. static int luaB_rawget (lua_State *L) {
  100. luaL_check_type(L, 1, LUA_TTABLE);
  101. luaL_check_any(L, 2);
  102. lua_rawget(L, 1);
  103. return 1;
  104. }
  105. static int luaB_rawset (lua_State *L) {
  106. luaL_check_type(L, 1, LUA_TTABLE);
  107. luaL_check_any(L, 2);
  108. luaL_check_any(L, 3);
  109. lua_rawset(L, 1);
  110. return 1;
  111. }
  112. static int luaB_gcinfo (lua_State *L) {
  113. lua_pushnumber(L, lua_getgccount(L));
  114. lua_pushnumber(L, lua_getgcthreshold(L));
  115. return 2;
  116. }
  117. static int luaB_collectgarbage (lua_State *L) {
  118. lua_setgcthreshold(L, luaL_opt_int(L, 1, 0));
  119. return 0;
  120. }
  121. static int luaB_type (lua_State *L) {
  122. luaL_check_any(L, 1);
  123. lua_pushstring(L, lua_typename(L, lua_type(L, 1)));
  124. return 1;
  125. }
  126. static int luaB_next (lua_State *L) {
  127. luaL_check_type(L, 1, LUA_TTABLE);
  128. lua_settop(L, 2); /* create a 2nd argument if there isn't one */
  129. if (lua_next(L, 1))
  130. return 2;
  131. else {
  132. lua_pushnil(L);
  133. return 1;
  134. }
  135. }
  136. static int luaB_nexti (lua_State *L) {
  137. lua_Number i = lua_tonumber(L, 2);
  138. luaL_check_type(L, 1, LUA_TTABLE);
  139. if (i == 0 && lua_isnull(L, 2)) { /* `for' start? */
  140. lua_getglobal(L, "nexti"); /* return generator, */
  141. lua_pushvalue(L, 1); /* state, */
  142. lua_pushnumber(L, 0); /* and initial value */
  143. return 3;
  144. }
  145. else { /* `for' step */
  146. i++; /* next value */
  147. lua_pushnumber(L, i);
  148. lua_rawgeti(L, 1, (int)i);
  149. return (lua_isnil(L, -1)) ? 0 : 2;
  150. }
  151. }
  152. static int passresults (lua_State *L, int status) {
  153. if (status == 0) return 1;
  154. else {
  155. lua_pushnil(L);
  156. lua_insert(L, -2);
  157. return 2;
  158. }
  159. }
  160. static int luaB_loadstring (lua_State *L) {
  161. size_t l;
  162. const char *s = luaL_check_lstr(L, 1, &l);
  163. const char *chunkname = luaL_opt_string(L, 2, s);
  164. return passresults(L, luaL_loadbuffer(L, s, l, chunkname));
  165. }
  166. static int luaB_loadfile (lua_State *L) {
  167. const char *fname = luaL_opt_string(L, 1, NULL);
  168. return passresults(L, luaL_loadfile(L, fname));
  169. }
  170. static int luaB_assert (lua_State *L) {
  171. luaL_check_any(L, 1);
  172. if (!lua_toboolean(L, 1))
  173. return luaL_verror(L, "%s", luaL_opt_string(L, 2, "assertion failed!"));
  174. lua_settop(L, 1);
  175. return 1;
  176. }
  177. static int luaB_unpack (lua_State *L) {
  178. int n, i;
  179. luaL_check_type(L, 1, LUA_TTABLE);
  180. n = lua_getn(L, 1);
  181. luaL_check_stack(L, n+LUA_MINSTACK, "table too big to unpack");
  182. for (i=1; i<=n; i++) /* push arg[1...n] */
  183. lua_rawgeti(L, 1, i);
  184. return n;
  185. }
  186. static int luaB_pcall (lua_State *L) {
  187. int status;
  188. luaL_check_any(L, 1);
  189. luaL_check_any(L, 2);
  190. status = lua_pcall(L, lua_gettop(L) - 2, LUA_MULTRET, 1);
  191. if (status != 0)
  192. return passresults(L, status);
  193. else {
  194. lua_pushboolean(L, 1);
  195. lua_replace(L, 1);
  196. return lua_gettop(L); /* return `true' + all results */
  197. }
  198. }
  199. static int luaB_tostring (lua_State *L) {
  200. char buff[64];
  201. luaL_checkany(L, 1);
  202. if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */
  203. return 1; /* use its value */
  204. switch (lua_type(L, 1)) {
  205. case LUA_TNUMBER:
  206. lua_pushstring(L, lua_tostring(L, 1));
  207. return 1;
  208. case LUA_TSTRING:
  209. lua_pushvalue(L, 1);
  210. return 1;
  211. case LUA_TBOOLEAN:
  212. lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
  213. return 1;
  214. case LUA_TTABLE:
  215. sprintf(buff, "table: %p", lua_topointer(L, 1));
  216. break;
  217. case LUA_TFUNCTION:
  218. sprintf(buff, "function: %p", lua_topointer(L, 1));
  219. break;
  220. case LUA_TUSERDATA:
  221. case LUA_TUDATAVAL:
  222. sprintf(buff, "userdata: %p", lua_touserdata(L, 1));
  223. break;
  224. case LUA_TNIL:
  225. lua_pushliteral(L, "nil");
  226. return 1;
  227. }
  228. lua_pushstring(L, buff);
  229. return 1;
  230. }
  231. /*
  232. ** {======================================================
  233. ** `require' function
  234. ** =======================================================
  235. */
  236. /* name of global that holds table with loaded packages */
  237. #define REQTAB "_LOADED"
  238. /* name of global that holds the search path for packages */
  239. #define LUA_PATH "LUA_PATH"
  240. #ifndef LUA_PATH_SEP
  241. #define LUA_PATH_SEP ';'
  242. #endif
  243. #ifndef LUA_PATH_DEFAULT
  244. #define LUA_PATH_DEFAULT "?.lua"
  245. #endif
  246. static const char *getpath (lua_State *L) {
  247. const char *path;
  248. lua_getglobal(L, LUA_PATH); /* try global variable */
  249. path = lua_tostring(L, -1);
  250. lua_pop(L, 1);
  251. if (path) return path;
  252. path = getenv(LUA_PATH); /* else try environment variable */
  253. if (path) return path;
  254. return LUA_PATH_DEFAULT; /* else use default */
  255. }
  256. static const char *pushnextpath (lua_State *L, const char *path) {
  257. const char *l;
  258. if (*path == '\0') return NULL; /* no more pathes */
  259. if (*path == LUA_PATH_SEP) path++; /* skip separator */
  260. l = strchr(path, LUA_PATH_SEP); /* find next separator */
  261. if (l == NULL) l = path+strlen(path);
  262. lua_pushlstring(L, path, l - path); /* directory name */
  263. return l;
  264. }
  265. static void pushcomposename (lua_State *L) {
  266. const char *path = lua_tostring(L, -1);
  267. const char *wild = strchr(path, '?');
  268. if (wild == NULL) return; /* no wild char; path is the file name */
  269. lua_pushlstring(L, path, wild - path);
  270. lua_pushvalue(L, 1); /* package name */
  271. lua_pushstring(L, wild + 1);
  272. lua_concat(L, 3);
  273. }
  274. static int luaB_require (lua_State *L) {
  275. const char *path;
  276. int status = LUA_ERRFILE; /* not found (yet) */
  277. luaL_check_string(L, 1);
  278. lua_settop(L, 1);
  279. lua_pushvalue(L, 1);
  280. lua_setglobal(L, "_REQUIREDNAME");
  281. lua_getglobal(L, REQTAB);
  282. if (!lua_istable(L, 2)) return luaL_verror(L, REQTAB " is not a table");
  283. path = getpath(L);
  284. lua_pushvalue(L, 1); /* check package's name in book-keeping table */
  285. lua_gettable(L, 2);
  286. if (!lua_isnil(L, -1)) /* is it there? */
  287. return 0; /* package is already loaded */
  288. else { /* must load it */
  289. while (status == LUA_ERRFILE) {
  290. lua_settop(L, 3); /* reset stack position */
  291. if ((path = pushnextpath(L, path)) == NULL) break;
  292. pushcomposename(L);
  293. status = luaL_loadfile(L, lua_tostring(L, -1)); /* try to load it */
  294. }
  295. }
  296. switch (status) {
  297. case 0: {
  298. lua_rawcall(L, 0, 0); /* run loaded module */
  299. lua_pushvalue(L, 1);
  300. lua_pushboolean(L, 1);
  301. lua_settable(L, 2); /* mark it as loaded */
  302. return 0;
  303. }
  304. case LUA_ERRFILE: { /* file not found */
  305. return luaL_verror(L, "could not load package `%s' from path `%s'",
  306. lua_tostring(L, 1), getpath(L));
  307. }
  308. default: {
  309. return luaL_verror(L, "error loading package\n%s", lua_tostring(L, -1));
  310. }
  311. }
  312. }
  313. /* }====================================================== */
  314. static const luaL_reg base_funcs[] = {
  315. {"error", luaB_error},
  316. {"metatable", luaB_metatable},
  317. {"globals", luaB_globals},
  318. {"next", luaB_next},
  319. {"nexti", luaB_nexti},
  320. {"print", luaB_print},
  321. {"tonumber", luaB_tonumber},
  322. {"tostring", luaB_tostring},
  323. {"type", luaB_type},
  324. {"assert", luaB_assert},
  325. {"unpack", luaB_unpack},
  326. {"rawget", luaB_rawget},
  327. {"rawset", luaB_rawset},
  328. {"pcall", luaB_pcall},
  329. {"collectgarbage", luaB_collectgarbage},
  330. {"gcinfo", luaB_gcinfo},
  331. {"loadfile", luaB_loadfile},
  332. {"loadstring", luaB_loadstring},
  333. {"require", luaB_require},
  334. {NULL, NULL}
  335. };
  336. /*
  337. ** {======================================================
  338. ** Coroutine library
  339. ** =======================================================
  340. */
  341. static int luaB_resume (lua_State *L) {
  342. lua_State *co = (lua_State *)lua_getfrombox(L, lua_upvalueindex(1));
  343. lua_settop(L, 0);
  344. if (lua_resume(L, co) != 0)
  345. return lua_errorobj(L);
  346. return lua_gettop(L);
  347. }
  348. static int gc_coroutine (lua_State *L) {
  349. lua_State *co = (lua_State *)lua_getfrombox(L, 1);
  350. lua_closethread(L, co);
  351. return 0;
  352. }
  353. static int luaB_coroutine (lua_State *L) {
  354. lua_State *NL;
  355. int ref;
  356. int i;
  357. int n = lua_gettop(L);
  358. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
  359. "Lua function expected");
  360. NL = lua_newthread(L);
  361. if (NL == NULL) return luaL_verror(L, "unable to create new thread");
  362. /* move function and arguments from L to NL */
  363. for (i=0; i<n; i++) {
  364. ref = lua_ref(L, 1);
  365. lua_getref(NL, ref);
  366. lua_insert(NL, 1);
  367. lua_unref(L, ref);
  368. }
  369. lua_cobegin(NL, n-1);
  370. lua_newpointerbox(L, NL);
  371. lua_pushliteral(L, "Coroutine");
  372. lua_rawget(L, LUA_REGISTRYINDEX);
  373. lua_setmetatable(L, -2);
  374. lua_pushcclosure(L, luaB_resume, 1);
  375. return 1;
  376. }
  377. static int luaB_yield (lua_State *L) {
  378. return lua_yield(L, lua_gettop(L));
  379. }
  380. static const luaL_reg co_funcs[] = {
  381. {"create", luaB_coroutine},
  382. {"yield", luaB_yield},
  383. {NULL, NULL}
  384. };
  385. static void co_open (lua_State *L) {
  386. luaL_opennamedlib(L, "co", co_funcs, 0);
  387. /* create metatable for coroutines */
  388. lua_pushliteral(L, "Coroutine");
  389. lua_newtable(L);
  390. lua_pushliteral(L, "__gc");
  391. lua_pushcfunction(L, gc_coroutine);
  392. lua_rawset(L, -3);
  393. lua_rawset(L, LUA_REGISTRYINDEX);
  394. }
  395. /* }====================================================== */
  396. static void base_open (lua_State *L) {
  397. lua_pushliteral(L, "_G");
  398. lua_pushvalue(L, LUA_GLOBALSINDEX);
  399. luaL_openlib(L, base_funcs, 0); /* open lib into global table */
  400. lua_pushliteral(L, "_VERSION");
  401. lua_pushliteral(L, LUA_VERSION);
  402. lua_settable(L, -3); /* set global _VERSION */
  403. lua_settable(L, -1); /* set global _G */
  404. }
  405. LUALIB_API int lua_baselibopen (lua_State *L) {
  406. base_open(L);
  407. co_open(L);
  408. lua_newtable(L);
  409. lua_setglobal(L, REQTAB);
  410. return 0;
  411. }