lbaselib.c 12 KB

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