lbaselib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. ** $Id: lbaselib.c,v 1.65 2002/04/09 19:46:56 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. return 0;
  21. }
  22. /*
  23. ** Basic implementation of _ERRORMESSAGE.
  24. ** The library `liolib' redefines _ERRORMESSAGE for better error information.
  25. */
  26. static int luaB__ERRORMESSAGE (lua_State *L) {
  27. luaL_check_type(L, 1, LUA_TSTRING);
  28. lua_getglobal(L, LUA_ALERT);
  29. if (lua_isfunction(L, -1)) { /* avoid error loop if _ALERT is not defined */
  30. lua_Debug ar;
  31. lua_pushliteral(L, "error: ");
  32. lua_pushvalue(L, 1);
  33. if (lua_getstack(L, 1, &ar)) {
  34. lua_getinfo(L, "Sl", &ar);
  35. if (ar.source && ar.currentline > 0) {
  36. char buff[100];
  37. sprintf(buff, "\n <%.70s: line %d>", ar.short_src, ar.currentline);
  38. lua_pushstring(L, buff);
  39. lua_concat(L, 2);
  40. }
  41. }
  42. lua_pushliteral(L, "\n");
  43. lua_concat(L, 3);
  44. lua_rawcall(L, 1, 0);
  45. }
  46. return 0;
  47. }
  48. /*
  49. ** If your system does not support `stdout', you can just remove this function.
  50. ** If you need, you can define your own `print' function, following this
  51. ** model but changing `fputs' to put the strings at a proper place
  52. ** (a console window or a log file, for instance).
  53. */
  54. static int luaB_print (lua_State *L) {
  55. int n = lua_gettop(L); /* number of arguments */
  56. int i;
  57. lua_getglobal(L, "tostring");
  58. for (i=1; i<=n; i++) {
  59. const char *s;
  60. lua_pushvalue(L, -1); /* function to be called */
  61. lua_pushvalue(L, i); /* value to print */
  62. lua_rawcall(L, 1, 1);
  63. s = lua_tostring(L, -1); /* get result */
  64. if (s == NULL)
  65. lua_error(L, "`tostring' must return a string to `print'");
  66. if (i>1) fputs("\t", stdout);
  67. fputs(s, stdout);
  68. lua_pop(L, 1); /* pop result */
  69. }
  70. fputs("\n", stdout);
  71. return 0;
  72. }
  73. static int luaB_tonumber (lua_State *L) {
  74. int base = luaL_opt_int(L, 2, 10);
  75. if (base == 10) { /* standard conversion */
  76. luaL_check_any(L, 1);
  77. if (lua_isnumber(L, 1)) {
  78. lua_pushnumber(L, lua_tonumber(L, 1));
  79. return 1;
  80. }
  81. }
  82. else {
  83. const char *s1 = luaL_check_string(L, 1);
  84. char *s2;
  85. unsigned long n;
  86. luaL_arg_check(L, 2 <= base && base <= 36, 2, "base out of range");
  87. n = strtoul(s1, &s2, base);
  88. if (s1 != s2) { /* at least one valid digit? */
  89. while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
  90. if (*s2 == '\0') { /* no invalid trailing characters? */
  91. lua_pushnumber(L, n);
  92. return 1;
  93. }
  94. }
  95. }
  96. lua_pushnil(L); /* else not a number */
  97. return 1;
  98. }
  99. static int luaB_error (lua_State *L) {
  100. lua_error(L, luaL_opt_string(L, 1, NULL));
  101. return 0; /* to avoid warnings */
  102. }
  103. static int luaB_metatable (lua_State *L) {
  104. luaL_check_any(L, 1);
  105. if (lua_isnone(L, 2)) {
  106. if (!lua_getmetatable(L, 1))
  107. return 0; /* no metatable */
  108. else {
  109. lua_pushliteral(L, "__metatable");
  110. lua_rawget(L, -2);
  111. if (lua_isnil(L, -1))
  112. lua_pop(L, 1);
  113. }
  114. }
  115. else {
  116. int t = lua_type(L, 2);
  117. luaL_check_type(L, 1, LUA_TTABLE);
  118. luaL_arg_check(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil/table expected");
  119. lua_settop(L, 2);
  120. lua_setmetatable(L, 1);
  121. }
  122. return 1;
  123. }
  124. static int luaB_globals (lua_State *L) {
  125. lua_getglobals(L); /* value to be returned */
  126. if (!lua_isnone(L, 1)) {
  127. luaL_check_type(L, 1, LUA_TTABLE);
  128. lua_pushvalue(L, 1); /* new table of globals */
  129. lua_setglobals(L);
  130. }
  131. return 1;
  132. }
  133. static int luaB_rawget (lua_State *L) {
  134. luaL_check_type(L, 1, LUA_TTABLE);
  135. luaL_check_any(L, 2);
  136. lua_rawget(L, 1);
  137. return 1;
  138. }
  139. static int luaB_rawset (lua_State *L) {
  140. luaL_check_type(L, 1, LUA_TTABLE);
  141. luaL_check_any(L, 2);
  142. luaL_check_any(L, 3);
  143. lua_rawset(L, 1);
  144. return 1;
  145. }
  146. static int luaB_gcinfo (lua_State *L) {
  147. lua_pushnumber(L, lua_getgccount(L));
  148. lua_pushnumber(L, lua_getgcthreshold(L));
  149. return 2;
  150. }
  151. static int luaB_collectgarbage (lua_State *L) {
  152. lua_setgcthreshold(L, luaL_opt_int(L, 1, 0));
  153. return 0;
  154. }
  155. static int luaB_type (lua_State *L) {
  156. luaL_check_any(L, 1);
  157. lua_pushstring(L, lua_typename(L, lua_type(L, 1)));
  158. return 1;
  159. }
  160. static int luaB_next (lua_State *L) {
  161. luaL_check_type(L, 1, LUA_TTABLE);
  162. lua_settop(L, 2); /* create a 2nd argument if there isn't one */
  163. if (lua_next(L, 1))
  164. return 2;
  165. else {
  166. lua_pushnil(L);
  167. return 1;
  168. }
  169. }
  170. static int passresults (lua_State *L, int status, int oldtop) {
  171. if (status == 0) {
  172. int nresults = lua_gettop(L) - oldtop;
  173. if (nresults > 0)
  174. return nresults; /* results are already on the stack */
  175. else {
  176. lua_pushboolean(L, 1); /* at least one result to signal no errors */
  177. return 1;
  178. }
  179. }
  180. else { /* error */
  181. lua_pushnil(L);
  182. lua_pushstring(L, luaL_errstr(status)); /* error code */
  183. return 2;
  184. }
  185. }
  186. static int luaB_dostring (lua_State *L) {
  187. int oldtop = lua_gettop(L);
  188. size_t l;
  189. const char *s = luaL_check_lstr(L, 1, &l);
  190. const char *chunkname = luaL_opt_string(L, 2, s);
  191. return passresults(L, lua_dobuffer(L, s, l, chunkname), oldtop);
  192. }
  193. static int luaB_loadstring (lua_State *L) {
  194. int oldtop = lua_gettop(L);
  195. size_t l;
  196. const char *s = luaL_check_lstr(L, 1, &l);
  197. const char *chunkname = luaL_opt_string(L, 2, s);
  198. return passresults(L, lua_loadbuffer(L, s, l, chunkname), oldtop);
  199. }
  200. static int luaB_dofile (lua_State *L) {
  201. int oldtop = lua_gettop(L);
  202. const char *fname = luaL_opt_string(L, 1, NULL);
  203. return passresults(L, lua_dofile(L, fname), oldtop);
  204. }
  205. static int luaB_loadfile (lua_State *L) {
  206. int oldtop = lua_gettop(L);
  207. const char *fname = luaL_opt_string(L, 1, NULL);
  208. return passresults(L, lua_loadfile(L, fname), oldtop);
  209. }
  210. static int luaB_assert (lua_State *L) {
  211. luaL_check_any(L, 1);
  212. if (!lua_toboolean(L, 1))
  213. luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, ""));
  214. lua_settop(L, 1);
  215. return 1;
  216. }
  217. #define LUA_PATH "LUA_PATH"
  218. #define LUA_PATH_SEP ";"
  219. #ifndef LUA_PATH_DEFAULT
  220. #define LUA_PATH_DEFAULT "./"
  221. #endif
  222. static int luaB_require (lua_State *L) {
  223. const char *path;
  224. luaL_check_string(L, 1);
  225. lua_settop(L, 1);
  226. lua_getglobal(L, LUA_PATH); /* get path */
  227. if (lua_isstring(L, 2)) /* is LUA_PATH defined? */
  228. path = lua_tostring(L, 2);
  229. else { /* LUA_PATH not defined */
  230. lua_pop(L, 1); /* pop old global value */
  231. path = getenv(LUA_PATH); /* try environment variable */
  232. if (path == NULL) path = LUA_PATH_DEFAULT; /* else use default */
  233. lua_pushstring(L, path);
  234. lua_pushvalue(L, -1); /* duplicate to leave a copy on stack */
  235. lua_setglobal(L, LUA_PATH);
  236. }
  237. lua_pushvalue(L, 1); /* check package's name in book-keeping table */
  238. lua_gettable(L, lua_upvalueindex(1));
  239. if (!lua_isnil(L, -1)) /* is it there? */
  240. return 0; /* package is already loaded */
  241. else { /* must load it */
  242. for (;;) { /* traverse path */
  243. int res;
  244. int l = strcspn(path, LUA_PATH_SEP); /* find separator */
  245. lua_pushlstring(L, path, l); /* directory name */
  246. lua_pushvalue(L, 1); /* package name */
  247. lua_concat(L, 2); /* concat directory with package name */
  248. res = lua_dofile(L, lua_tostring(L, -1)); /* try to load it */
  249. lua_settop(L, 2); /* pop string and eventual results from dofile */
  250. if (res == 0) break; /* ok; file done */
  251. else if (res != LUA_ERRFILE)
  252. lua_error(L, NULL); /* error running package; propagate it */
  253. if (*(path+l) == '\0') /* no more directories? */
  254. luaL_verror(L, "could not load package `%.20s' from path `%.200s'",
  255. lua_tostring(L, 1), lua_tostring(L, 2));
  256. path += l+1; /* try next directory */
  257. }
  258. }
  259. lua_pushvalue(L, 1);
  260. lua_pushnumber(L, 1);
  261. lua_settable(L, lua_upvalueindex(1)); /* mark it as loaded */
  262. return 0;
  263. }
  264. static int aux_unpack (lua_State *L, int arg) {
  265. int n, i;
  266. luaL_check_type(L, arg, LUA_TTABLE);
  267. n = lua_getn(L, arg);
  268. luaL_check_stack(L, n, "table too big to unpack");
  269. for (i=1; i<=n; i++) /* push arg[1...n] */
  270. lua_rawgeti(L, arg, i);
  271. return n;
  272. }
  273. static int luaB_unpack (lua_State *L) {
  274. return aux_unpack(L, 1);
  275. }
  276. static int luaB_call (lua_State *L) {
  277. int oldtop;
  278. const char *options = luaL_opt_string(L, 3, "");
  279. int err = 0; /* index of old error method */
  280. int status;
  281. int n;
  282. if (!lua_isnone(L, 4)) { /* set new error method */
  283. lua_getglobal(L, LUA_ERRORMESSAGE);
  284. err = lua_gettop(L); /* get index */
  285. lua_pushvalue(L, 4);
  286. lua_setglobal(L, LUA_ERRORMESSAGE);
  287. }
  288. oldtop = lua_gettop(L); /* top before function-call preparation */
  289. /* push function */
  290. lua_pushvalue(L, 1);
  291. n = aux_unpack(L, 2); /* push arg[1...n] */
  292. status = lua_call(L, n, LUA_MULTRET);
  293. if (err != 0) { /* restore old error method */
  294. lua_pushvalue(L, err);
  295. lua_setglobal(L, LUA_ERRORMESSAGE);
  296. }
  297. if (status != 0) { /* error in call? */
  298. if (strchr(options, 'x'))
  299. lua_pushnil(L); /* return nil to signal the error */
  300. else
  301. lua_error(L, NULL); /* propagate error without additional messages */
  302. return 1;
  303. }
  304. if (strchr(options, 'p')) /* pack results? */
  305. lua_error(L, "obsolete option `p' in `call'");
  306. return lua_gettop(L) - oldtop; /* results are already on the stack */
  307. }
  308. static int luaB_tostring (lua_State *L) {
  309. char buff[64];
  310. luaL_checkany(L, 1);
  311. if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */
  312. return 1; /* use its value */
  313. switch (lua_type(L, 1)) {
  314. case LUA_TNUMBER:
  315. lua_pushstring(L, lua_tostring(L, 1));
  316. return 1;
  317. case LUA_TSTRING:
  318. lua_pushvalue(L, 1);
  319. return 1;
  320. case LUA_TBOOLEAN:
  321. lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
  322. return 1;
  323. case LUA_TTABLE:
  324. sprintf(buff, "table: %p", lua_topointer(L, 1));
  325. break;
  326. case LUA_TFUNCTION:
  327. sprintf(buff, "function: %p", lua_topointer(L, 1));
  328. break;
  329. case LUA_TUSERDATA:
  330. case LUA_TUDATAVAL:
  331. sprintf(buff, "userdata: %p", lua_touserdata(L, 1));
  332. break;
  333. case LUA_TNIL:
  334. lua_pushliteral(L, "nil");
  335. return 1;
  336. }
  337. lua_pushstring(L, buff);
  338. return 1;
  339. }
  340. static const luaL_reg base_funcs[] = {
  341. {LUA_ALERT, luaB__ALERT},
  342. {LUA_ERRORMESSAGE, luaB__ERRORMESSAGE},
  343. {"error", luaB_error},
  344. {"metatable", luaB_metatable},
  345. {"globals", luaB_globals},
  346. {"next", luaB_next},
  347. {"print", luaB_print},
  348. {"tonumber", luaB_tonumber},
  349. {"tostring", luaB_tostring},
  350. {"type", luaB_type},
  351. {"assert", luaB_assert},
  352. {"unpack", luaB_unpack},
  353. {"rawget", luaB_rawget},
  354. {"rawset", luaB_rawset},
  355. {"call", luaB_call},
  356. {"collectgarbage", luaB_collectgarbage},
  357. {"gcinfo", luaB_gcinfo},
  358. {"loadfile", luaB_loadfile},
  359. {"loadstring", luaB_loadstring},
  360. {"dofile", luaB_dofile},
  361. {"dostring", luaB_dostring},
  362. {NULL, NULL}
  363. };
  364. static void base_open (lua_State *L) {
  365. lua_pushliteral(L, "_G");
  366. lua_pushvalue(L, LUA_GLOBALSINDEX);
  367. luaL_openlib(L, base_funcs, 0); /* open lib into global table */
  368. lua_pushliteral(L, "_VERSION");
  369. lua_pushliteral(L, LUA_VERSION);
  370. lua_settable(L, -3); /* set global _VERSION */
  371. lua_settable(L, -1); /* set global _G */
  372. }
  373. LUALIB_API int lua_baselibopen (lua_State *L) {
  374. base_open(L);
  375. /* `require' needs an empty table as upvalue */
  376. lua_newtable(L);
  377. lua_pushcclosure(L, luaB_require, 1);
  378. lua_setglobal(L, "require");
  379. return 0;
  380. }