lbaselib.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /*
  2. ** $Id: lbaselib.c,v 1.29 2001/03/06 20:09:38 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. static void aux_setn (lua_State *L, int t, int n) {
  15. lua_pushliteral(L, l_s("n"));
  16. lua_pushnumber(L, n);
  17. lua_settable(L, t);
  18. }
  19. /*
  20. ** If your system does not support `stderr', redefine this function, or
  21. ** redefine _ERRORMESSAGE so that it won't need _ALERT.
  22. */
  23. static int luaB__ALERT (lua_State *L) {
  24. fputs(luaL_check_string(L, 1), stderr);
  25. return 0;
  26. }
  27. /*
  28. ** Basic implementation of _ERRORMESSAGE.
  29. ** The library `liolib' redefines _ERRORMESSAGE for better error information.
  30. */
  31. static int luaB__ERRORMESSAGE (lua_State *L) {
  32. luaL_checktype(L, 1, LUA_TSTRING);
  33. lua_getglobal(L, LUA_ALERT);
  34. if (lua_isfunction(L, -1)) { /* avoid error loop if _ALERT is not defined */
  35. lua_Debug ar;
  36. lua_pushliteral(L, l_s("error: "));
  37. lua_pushvalue(L, 1);
  38. if (lua_getstack(L, 1, &ar)) {
  39. lua_getinfo(L, l_s("Sl"), &ar);
  40. if (ar.source && ar.currentline > 0) {
  41. l_char buff[100];
  42. sprintf(buff, l_s("\n <%.70s: line %d>"), ar.short_src, ar.currentline);
  43. lua_pushstring(L, buff);
  44. lua_concat(L, 2);
  45. }
  46. }
  47. lua_pushliteral(L, l_s("\n"));
  48. lua_concat(L, 3);
  49. lua_rawcall(L, 1, 0);
  50. }
  51. return 0;
  52. }
  53. /*
  54. ** If your system does not support `stdout', you can just remove this function.
  55. ** If you need, you can define your own `print' function, following this
  56. ** model but changing `fputs' to put the strings at a proper place
  57. ** (a console window or a log file, for instance).
  58. */
  59. static int luaB_print (lua_State *L) {
  60. int n = lua_gettop(L); /* number of arguments */
  61. int i;
  62. lua_getglobal(L, l_s("tostring"));
  63. for (i=1; i<=n; i++) {
  64. const l_char *s;
  65. lua_pushvalue(L, -1); /* function to be called */
  66. lua_pushvalue(L, i); /* value to print */
  67. lua_rawcall(L, 1, 1);
  68. s = lua_tostring(L, -1); /* get result */
  69. if (s == NULL)
  70. lua_error(L, l_s("`tostring' must return a string to `print'"));
  71. if (i>1) fputs(l_s("\t"), stdout);
  72. fputs(s, stdout);
  73. lua_pop(L, 1); /* pop result */
  74. }
  75. fputs(l_s("\n"), stdout);
  76. return 0;
  77. }
  78. static int luaB_tonumber (lua_State *L) {
  79. int base = luaL_opt_int(L, 2, 10);
  80. if (base == 10) { /* standard conversion */
  81. luaL_checkany(L, 1);
  82. if (lua_isnumber(L, 1)) {
  83. lua_pushnumber(L, lua_tonumber(L, 1));
  84. return 1;
  85. }
  86. }
  87. else {
  88. const l_char *s1 = luaL_check_string(L, 1);
  89. l_char *s2;
  90. unsigned long n;
  91. luaL_arg_check(L, 2 <= base && base <= 36, 2, l_s("base out of range"));
  92. n = strtoul(s1, &s2, base);
  93. if (s1 != s2) { /* at least one valid digit? */
  94. while (isspace(uchar(*s2))) s2++; /* skip trailing spaces */
  95. if (*s2 == l_c('\0')) { /* no invalid trailing characters? */
  96. lua_pushnumber(L, n);
  97. return 1;
  98. }
  99. }
  100. }
  101. lua_pushnil(L); /* else not a number */
  102. return 1;
  103. }
  104. static int luaB_error (lua_State *L) {
  105. lua_error(L, luaL_opt_string(L, 1, NULL));
  106. return 0; /* to avoid warnings */
  107. }
  108. static int luaB_setglobal (lua_State *L) {
  109. luaL_checkany(L, 2);
  110. lua_setglobal(L, luaL_check_string(L, 1));
  111. return 0;
  112. }
  113. static int luaB_getglobal (lua_State *L) {
  114. lua_getglobal(L, luaL_check_string(L, 1));
  115. return 1;
  116. }
  117. /* auxiliary function to get `tags' */
  118. static int gettag (lua_State *L, int narg) {
  119. switch (lua_type(L, narg)) {
  120. case LUA_TNUMBER:
  121. return (int)lua_tonumber(L, narg);
  122. case LUA_TSTRING: {
  123. const l_char *name = lua_tostring(L, narg);
  124. int tag = lua_type2tag(L, name);
  125. if (tag == LUA_TNONE)
  126. luaL_verror(L, l_s("'%.30s' is not a valid type name"), name);
  127. return tag;
  128. }
  129. default:
  130. luaL_argerror(L, narg, l_s("tag or type name expected"));
  131. return 0; /* to avoid warnings */
  132. }
  133. }
  134. static int luaB_tag (lua_State *L) {
  135. luaL_checkany(L, 1);
  136. lua_pushnumber(L, lua_tag(L, 1));
  137. return 1;
  138. }
  139. static int luaB_settag (lua_State *L) {
  140. luaL_checktype(L, 1, LUA_TTABLE);
  141. lua_pushvalue(L, 1); /* push table */
  142. lua_settag(L, gettag(L, 2));
  143. return 1; /* return table */
  144. }
  145. static int luaB_newtype (lua_State *L) {
  146. const l_char *name = luaL_opt_string(L, 1, NULL);
  147. lua_pushnumber(L, lua_newtype(L, name, LUA_TTABLE));
  148. return 1;
  149. }
  150. static int luaB_copytagmethods (lua_State *L) {
  151. lua_pushnumber(L, lua_copytagmethods(L, gettag(L, 1), gettag(L, 2)));
  152. return 1;
  153. }
  154. static int luaB_globals (lua_State *L) {
  155. lua_getglobals(L); /* value to be returned */
  156. if (!lua_isnull(L, 1)) {
  157. luaL_checktype(L, 1, LUA_TTABLE);
  158. lua_pushvalue(L, 1); /* new table of globals */
  159. lua_setglobals(L);
  160. }
  161. return 1;
  162. }
  163. static int luaB_rawget (lua_State *L) {
  164. luaL_checktype(L, 1, LUA_TTABLE);
  165. luaL_checkany(L, 2);
  166. lua_rawget(L, -2);
  167. return 1;
  168. }
  169. static int luaB_rawset (lua_State *L) {
  170. luaL_checktype(L, 1, LUA_TTABLE);
  171. luaL_checkany(L, 2);
  172. luaL_checkany(L, 3);
  173. lua_rawset(L, -3);
  174. return 1;
  175. }
  176. static int luaB_settagmethod (lua_State *L) {
  177. int tag = gettag(L, 1);
  178. const l_char *event = luaL_check_string(L, 2);
  179. luaL_arg_check(L, lua_isfunction(L, 3) || lua_isnil(L, 3), 3,
  180. l_s("function or nil expected"));
  181. if (strcmp(event, l_s("gc")) == 0)
  182. lua_error(L, l_s("deprecated use: cannot set the `gc' tag method from Lua"));
  183. lua_gettagmethod(L, tag, event);
  184. lua_pushvalue(L, 3);
  185. lua_settagmethod(L, tag, event);
  186. return 1;
  187. }
  188. static int luaB_gettagmethod (lua_State *L) {
  189. int tag = gettag(L, 1);
  190. const l_char *event = luaL_check_string(L, 2);
  191. if (strcmp(event, l_s("gc")) == 0)
  192. lua_error(L, l_s("deprecated use: cannot get the `gc' tag method from Lua"));
  193. lua_gettagmethod(L, tag, event);
  194. return 1;
  195. }
  196. static int luaB_gcinfo (lua_State *L) {
  197. lua_pushnumber(L, lua_getgccount(L));
  198. lua_pushnumber(L, lua_getgcthreshold(L));
  199. return 2;
  200. }
  201. static int luaB_collectgarbage (lua_State *L) {
  202. lua_setgcthreshold(L, luaL_opt_int(L, 1, 0));
  203. return 0;
  204. }
  205. static int luaB_type (lua_State *L) {
  206. luaL_checkany(L, 1);
  207. lua_pushstring(L, lua_typename(L, lua_type(L, 1)));
  208. return 1;
  209. }
  210. static int luaB_xtype (lua_State *L) {
  211. luaL_checkany(L, 1);
  212. lua_pushstring(L, lua_xtype(L, 1));
  213. return 1;
  214. }
  215. static int luaB_next (lua_State *L) {
  216. luaL_checktype(L, 1, LUA_TTABLE);
  217. lua_settop(L, 2); /* create a 2nd argument if there isn't one */
  218. if (lua_next(L, 1))
  219. return 2;
  220. else {
  221. lua_pushnil(L);
  222. return 1;
  223. }
  224. }
  225. static int passresults (lua_State *L, int status, int oldtop) {
  226. static const l_char *const errornames[] =
  227. {l_s("ok"), l_s("run-time error"), l_s("file error"), l_s("syntax error"),
  228. l_s("memory error"), l_s("error in error handling")};
  229. if (status == 0) {
  230. int nresults = lua_gettop(L) - oldtop;
  231. if (nresults > 0)
  232. return nresults; /* results are already on the stack */
  233. else {
  234. lua_pushuserdata(L, NULL); /* at least one result to signal no errors */
  235. return 1;
  236. }
  237. }
  238. else { /* error */
  239. lua_pushnil(L);
  240. lua_pushstring(L, errornames[status]); /* error code */
  241. return 2;
  242. }
  243. }
  244. static int luaB_dostring (lua_State *L) {
  245. int oldtop = lua_gettop(L);
  246. size_t l;
  247. const l_char *s = luaL_check_lstr(L, 1, &l);
  248. const l_char *chunkname = luaL_opt_string(L, 2, s);
  249. return passresults(L, lua_dobuffer(L, s, l, chunkname), oldtop);
  250. }
  251. static int luaB_dofile (lua_State *L) {
  252. int oldtop = lua_gettop(L);
  253. const l_char *fname = luaL_opt_string(L, 1, NULL);
  254. return passresults(L, lua_dofile(L, fname), oldtop);
  255. }
  256. static int luaB_pack (lua_State *L) {
  257. int n = lua_gettop(L);
  258. lua_newtable(L);
  259. aux_setn(L, -3, n);
  260. lua_insert(L, 1);
  261. while (n)
  262. lua_rawseti(L, 1, n--);
  263. return 1;
  264. }
  265. static int aux_unpack (lua_State *L, int arg) {
  266. int n, i;
  267. luaL_checktype(L, arg, LUA_TTABLE);
  268. n = lua_getn(L, arg);
  269. luaL_checkstack(L, n, l_s("too many arguments"));
  270. for (i=1; i<=n; i++) /* push arg[1...n] */
  271. lua_rawgeti(L, arg, i);
  272. return n;
  273. }
  274. static int luaB_unpack (lua_State *L) {
  275. return aux_unpack(L, 1);
  276. }
  277. static int luaB_call (lua_State *L) {
  278. int oldtop;
  279. const l_char *options = luaL_opt_string(L, 3, l_s(""));
  280. int err = 0; /* index of old error method */
  281. int status;
  282. int n;
  283. if (!lua_isnull(L, 4)) { /* set new error method */
  284. lua_getglobal(L, LUA_ERRORMESSAGE);
  285. err = lua_gettop(L); /* get index */
  286. lua_pushvalue(L, 4);
  287. lua_setglobal(L, LUA_ERRORMESSAGE);
  288. }
  289. oldtop = lua_gettop(L); /* top before function-call preparation */
  290. /* push function */
  291. lua_pushvalue(L, 1);
  292. n = aux_unpack(L, 2); /* push arg[1...n] */
  293. status = lua_call(L, n, LUA_MULTRET);
  294. if (err != 0) { /* restore old error method */
  295. lua_pushvalue(L, err);
  296. lua_setglobal(L, LUA_ERRORMESSAGE);
  297. }
  298. if (status != 0) { /* error in call? */
  299. if (strchr(options, l_c('x')))
  300. lua_pushnil(L); /* return nil to signal the error */
  301. else
  302. lua_error(L, NULL); /* propagate error without additional messages */
  303. return 1;
  304. }
  305. if (strchr(options, l_c('p'))) /* pack results? */
  306. lua_error(L, l_s("deprecated option `p' in `call'"));
  307. return lua_gettop(L) - oldtop; /* results are already on the stack */
  308. }
  309. static int luaB_tostring (lua_State *L) {
  310. l_char buff[64];
  311. switch (lua_type(L, 1)) {
  312. case LUA_TNUMBER:
  313. lua_pushstring(L, lua_tostring(L, 1));
  314. return 1;
  315. case LUA_TSTRING:
  316. lua_pushvalue(L, 1);
  317. return 1;
  318. case LUA_TTABLE:
  319. sprintf(buff, l_s("%.40s: %p"), lua_xtype(L, 1), lua_topointer(L, 1));
  320. break;
  321. case LUA_TFUNCTION:
  322. sprintf(buff, l_s("function: %p"), lua_topointer(L, 1));
  323. break;
  324. case LUA_TUSERDATA: {
  325. const l_char *t = lua_xtype(L, 1);
  326. if (strcmp(t, l_s("userdata")) == 0)
  327. sprintf(buff, l_s("userdata(%d): %p"), lua_tag(L, 1), lua_touserdata(L, 1));
  328. else
  329. sprintf(buff, l_s("%.40s: %p"), t, lua_touserdata(L, 1));
  330. break;
  331. }
  332. case LUA_TNIL:
  333. lua_pushliteral(L, l_s("nil"));
  334. return 1;
  335. default:
  336. luaL_argerror(L, 1, l_s("value expected"));
  337. }
  338. lua_pushstring(L, buff);
  339. return 1;
  340. }
  341. static int luaB_foreachi (lua_State *L) {
  342. int n, i;
  343. luaL_checktype(L, 1, LUA_TTABLE);
  344. luaL_checktype(L, 2, LUA_TFUNCTION);
  345. n = lua_getn(L, 1);
  346. for (i=1; i<=n; i++) {
  347. lua_pushvalue(L, 2); /* function */
  348. lua_pushnumber(L, i); /* 1st argument */
  349. lua_rawgeti(L, 1, i); /* 2nd argument */
  350. lua_rawcall(L, 2, 1);
  351. if (!lua_isnil(L, -1))
  352. return 1;
  353. lua_pop(L, 1); /* remove nil result */
  354. }
  355. return 0;
  356. }
  357. static int luaB_foreach (lua_State *L) {
  358. luaL_checktype(L, 1, LUA_TTABLE);
  359. luaL_checktype(L, 2, LUA_TFUNCTION);
  360. lua_pushnil(L); /* first index */
  361. for (;;) {
  362. if (lua_next(L, 1) == 0)
  363. return 0;
  364. lua_pushvalue(L, 2); /* function */
  365. lua_pushvalue(L, -3); /* key */
  366. lua_pushvalue(L, -3); /* value */
  367. lua_rawcall(L, 2, 1);
  368. if (!lua_isnil(L, -1))
  369. return 1;
  370. lua_pop(L, 2); /* remove value and result */
  371. }
  372. }
  373. static int luaB_assert (lua_State *L) {
  374. luaL_checkany(L, 1);
  375. if (lua_isnil(L, 1))
  376. luaL_verror(L, l_s("assertion failed! %.90s"), luaL_opt_string(L, 2, l_s("")));
  377. lua_settop(L, 1);
  378. return 1;
  379. }
  380. static int luaB_getn (lua_State *L) {
  381. luaL_checktype(L, 1, LUA_TTABLE);
  382. lua_pushnumber(L, lua_getn(L, 1));
  383. return 1;
  384. }
  385. static int luaB_tinsert (lua_State *L) {
  386. int v = lua_gettop(L); /* last argument: to be inserted */
  387. int n, pos;
  388. luaL_checktype(L, 1, LUA_TTABLE);
  389. n = lua_getn(L, 1);
  390. if (v == 2) /* called with only 2 arguments */
  391. pos = n+1;
  392. else
  393. pos = luaL_check_int(L, 2); /* 2nd argument is the position */
  394. aux_setn(L, 1, n+1); /* t.n = n+1 */
  395. for (; n>=pos; n--) {
  396. lua_rawgeti(L, 1, n);
  397. lua_rawseti(L, 1, n+1); /* t[n+1] = t[n] */
  398. }
  399. lua_pushvalue(L, v);
  400. lua_rawseti(L, 1, pos); /* t[pos] = v */
  401. return 0;
  402. }
  403. static int luaB_tremove (lua_State *L) {
  404. int pos, n;
  405. luaL_checktype(L, 1, LUA_TTABLE);
  406. n = lua_getn(L, 1);
  407. pos = luaL_opt_int(L, 2, n);
  408. if (n <= 0) return 0; /* table is `empty' */
  409. aux_setn(L, 1, n-1); /* t.n = n-1 */
  410. lua_rawgeti(L, 1, pos); /* result = t[pos] */
  411. for ( ;pos<n; pos++) {
  412. lua_rawgeti(L, 1, pos+1);
  413. lua_rawseti(L, 1, pos); /* a[pos] = a[pos+1] */
  414. }
  415. lua_pushnil(L);
  416. lua_rawseti(L, 1, n); /* t[n] = nil */
  417. return 1;
  418. }
  419. /*
  420. ** {======================================================
  421. ** Quicksort
  422. ** (based on `Algorithms in MODULA-3', Robert Sedgewick;
  423. ** Addison-Wesley, 1993.)
  424. */
  425. static void set2 (lua_State *L, int i, int j) {
  426. lua_rawseti(L, 1, i);
  427. lua_rawseti(L, 1, j);
  428. }
  429. static int sort_comp (lua_State *L, int a, int b) {
  430. /* WARNING: the caller (auxsort) must ensure stack space */
  431. if (!lua_isnil(L, 2)) { /* function? */
  432. int res;
  433. lua_pushvalue(L, 2);
  434. lua_pushvalue(L, a-1); /* -1 to compensate function */
  435. lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */
  436. lua_rawcall(L, 2, 1);
  437. res = !lua_isnil(L, -1);
  438. lua_pop(L, 1);
  439. return res;
  440. }
  441. else /* a < b? */
  442. return lua_lessthan(L, a, b);
  443. }
  444. static void auxsort (lua_State *L, int l, int u) {
  445. while (l < u) { /* for tail recursion */
  446. int i, j;
  447. /* sort elements a[l], a[(l+u)/2] and a[u] */
  448. lua_rawgeti(L, 1, l);
  449. lua_rawgeti(L, 1, u);
  450. if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */
  451. set2(L, l, u); /* swap a[l] - a[u] */
  452. else
  453. lua_pop(L, 2);
  454. if (u-l == 1) break; /* only 2 elements */
  455. i = (l+u)/2;
  456. lua_rawgeti(L, 1, i);
  457. lua_rawgeti(L, 1, l);
  458. if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */
  459. set2(L, i, l);
  460. else {
  461. lua_pop(L, 1); /* remove a[l] */
  462. lua_rawgeti(L, 1, u);
  463. if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */
  464. set2(L, i, u);
  465. else
  466. lua_pop(L, 2);
  467. }
  468. if (u-l == 2) break; /* only 3 elements */
  469. lua_rawgeti(L, 1, i); /* Pivot */
  470. lua_pushvalue(L, -1);
  471. lua_rawgeti(L, 1, u-1);
  472. set2(L, i, u-1);
  473. /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */
  474. i = l; j = u-1;
  475. for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
  476. /* repeat ++i until a[i] >= P */
  477. while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {
  478. if (i>u) lua_error(L, l_s("invalid order function for sorting"));
  479. lua_pop(L, 1); /* remove a[i] */
  480. }
  481. /* repeat --j until a[j] <= P */
  482. while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) {
  483. if (j<l) lua_error(L, l_s("invalid order function for sorting"));
  484. lua_pop(L, 1); /* remove a[j] */
  485. }
  486. if (j<i) {
  487. lua_pop(L, 3); /* pop pivot, a[i], a[j] */
  488. break;
  489. }
  490. set2(L, i, j);
  491. }
  492. lua_rawgeti(L, 1, u-1);
  493. lua_rawgeti(L, 1, i);
  494. set2(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */
  495. /* a[l..i-1] <= a[i] == P <= a[i+1..u] */
  496. /* adjust so that smaller half is in [j..i] and larger one in [l..u] */
  497. if (i-l < u-i) {
  498. j=l; i=i-1; l=i+2;
  499. }
  500. else {
  501. j=i+1; i=u; u=j-2;
  502. }
  503. auxsort(L, j, i); /* call recursively the smaller one */
  504. } /* repeat the routine for the larger one */
  505. }
  506. static int luaB_sort (lua_State *L) {
  507. int n;
  508. luaL_checktype(L, 1, LUA_TTABLE);
  509. n = lua_getn(L, 1);
  510. if (!lua_isnull(L, 2)) /* is there a 2nd argument? */
  511. luaL_checktype(L, 2, LUA_TFUNCTION);
  512. lua_settop(L, 2); /* make sure there is two arguments */
  513. auxsort(L, 1, n);
  514. return 0;
  515. }
  516. /* }====================================================== */
  517. /*
  518. ** {======================================================
  519. ** Deprecated functions to manipulate global environment.
  520. ** =======================================================
  521. */
  522. #define num_deprecated 4
  523. static const luaL_reg deprecated_names [num_deprecated] = {
  524. {l_s("foreachvar"), luaB_foreach},
  525. {l_s("nextvar"), luaB_next},
  526. {l_s("rawgetglobal"), luaB_rawget},
  527. {l_s("rawsetglobal"), luaB_rawset}
  528. };
  529. #ifdef LUA_DEPRECATEDFUNCS
  530. /*
  531. ** call corresponding function inserting `globals' as first argument
  532. */
  533. static int deprecated_func (lua_State *L) {
  534. lua_insert(L, 1); /* upvalue is the function to be called */
  535. lua_getglobals(L);
  536. lua_insert(L, 2); /* table of globals is 1o argument */
  537. lua_rawcall(L, lua_gettop(L)-1, LUA_MULTRET);
  538. return lua_gettop(L); /* return all results */
  539. }
  540. static void deprecated_funcs (lua_State *L) {
  541. int i;
  542. for (i=0; i<num_deprecated; i++) {
  543. lua_pushcfunction(L, deprecated_names[i].func);
  544. lua_pushcclosure(L, deprecated_func, 1);
  545. lua_setglobal(L, deprecated_names[i].name);
  546. }
  547. }
  548. #else
  549. /*
  550. ** gives an explicit error in any attempt to call a deprecated function
  551. */
  552. static int deprecated_func (lua_State *L) {
  553. luaL_verror(L, l_s("function `%.20s' is deprecated"), lua_tostring(L, -1));
  554. return 0; /* to avoid warnings */
  555. }
  556. static void deprecated_funcs (lua_State *L) {
  557. int i;
  558. for (i=0; i<num_deprecated; i++) {
  559. lua_pushstring(L, deprecated_names[i].name);
  560. lua_pushcclosure(L, deprecated_func, 1);
  561. lua_setglobal(L, deprecated_names[i].name);
  562. }
  563. }
  564. #endif
  565. /* }====================================================== */
  566. static const luaL_reg base_funcs[] = {
  567. {LUA_ALERT, luaB__ALERT},
  568. {LUA_ERRORMESSAGE, luaB__ERRORMESSAGE},
  569. {l_s("call"), luaB_call},
  570. {l_s("collectgarbage"), luaB_collectgarbage},
  571. {l_s("copytagmethods"), luaB_copytagmethods},
  572. {l_s("dofile"), luaB_dofile},
  573. {l_s("dostring"), luaB_dostring},
  574. {l_s("error"), luaB_error},
  575. {l_s("foreach"), luaB_foreach},
  576. {l_s("foreachi"), luaB_foreachi},
  577. {l_s("gcinfo"), luaB_gcinfo},
  578. {l_s("getglobal"), luaB_getglobal},
  579. {l_s("gettagmethod"), luaB_gettagmethod},
  580. {l_s("globals"), luaB_globals},
  581. {l_s("newtype"), luaB_newtype},
  582. {l_s("newtag"), luaB_newtype}, /* for compatibility 4.0 */
  583. {l_s("next"), luaB_next},
  584. {l_s("print"), luaB_print},
  585. {l_s("rawget"), luaB_rawget},
  586. {l_s("rawset"), luaB_rawset},
  587. {l_s("rawgettable"), luaB_rawget}, /* for compatibility 3.2 */
  588. {l_s("rawsettable"), luaB_rawset}, /* for compatibility 3.2 */
  589. {l_s("setglobal"), luaB_setglobal},
  590. {l_s("settag"), luaB_settag},
  591. {l_s("settagmethod"), luaB_settagmethod},
  592. {l_s("tag"), luaB_tag},
  593. {l_s("tonumber"), luaB_tonumber},
  594. {l_s("tostring"), luaB_tostring},
  595. {l_s("type"), luaB_type},
  596. {l_s("assert"), luaB_assert},
  597. {l_s("getn"), luaB_getn},
  598. {l_s("sort"), luaB_sort},
  599. {l_s("tinsert"), luaB_tinsert},
  600. {l_s("tremove"), luaB_tremove},
  601. {l_s("pack"), luaB_pack},
  602. {l_s("unpack"), luaB_unpack},
  603. {l_s("xtype"), luaB_xtype},
  604. };
  605. LUALIB_API int lua_baselibopen (lua_State *L) {
  606. luaL_openl(L, base_funcs);
  607. lua_pushliteral(L, LUA_VERSION);
  608. lua_setglobal(L, l_s("_VERSION"));
  609. deprecated_funcs(L);
  610. return 0;
  611. }