lbaselib.c 15 KB

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