lbaselib.c 12 KB

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