lbaselib.c 12 KB

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