lbuiltin.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. /*
  2. ** $Id: lbuiltin.c,v 1.123 2000/08/29 14:33:31 roberto Exp roberto $
  3. ** Built-in functions
  4. ** See Copyright Notice in lua.h
  5. */
  6. /*
  7. ** =========================================================================
  8. ** All built-in functions are public (i.e. not static) and are named luaB_f,
  9. ** where f is the function name in Lua. So, if you do not need all these
  10. ** functions, you may register manually only the ones that you need.
  11. ** =========================================================================
  12. */
  13. #include <ctype.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "lua.h"
  18. #include "lapi.h"
  19. #include "lauxlib.h"
  20. #include "lbuiltin.h"
  21. #include "ldo.h"
  22. #include "lfunc.h"
  23. #include "lmem.h"
  24. #include "lobject.h"
  25. #include "lstate.h"
  26. #include "lstring.h"
  27. #include "ltable.h"
  28. #include "ltm.h"
  29. #include "lundump.h"
  30. #include "lvm.h"
  31. /*
  32. ** function defined in ltests.c, to open some internal-test functions
  33. */
  34. void luaB_opentests (lua_State *L);
  35. /*
  36. ** {======================================================
  37. ** Auxiliary functions
  38. ** =======================================================
  39. */
  40. static Number getsize (const Hash *h) {
  41. Number max = 0;
  42. int i = h->size;
  43. Node *n = h->node;
  44. while (i--) {
  45. if (ttype(key(n)) == TAG_NUMBER &&
  46. ttype(val(n)) != TAG_NIL &&
  47. nvalue(key(n)) > max)
  48. max = nvalue(key(n));
  49. n++;
  50. }
  51. return max;
  52. }
  53. static Number getnarg (lua_State *L, const Hash *a) {
  54. const TObject *value = luaH_getstr(a, luaS_new(L, "n")); /* value = a.n */
  55. return (ttype(value) == TAG_NUMBER) ? nvalue(value) : getsize(a);
  56. }
  57. static Hash *gettable (lua_State *L, int arg) {
  58. luaL_checktype(L, arg, "table");
  59. return hvalue(luaA_index(L, arg));
  60. }
  61. /* }====================================================== */
  62. /*
  63. ** {======================================================
  64. ** Functions that use only the official API
  65. ** =======================================================
  66. */
  67. /*
  68. ** If your system does not support `stderr', redefine this function, or
  69. ** redefine _ERRORMESSAGE so that it won't need _ALERT.
  70. */
  71. int luaB__ALERT (lua_State *L) {
  72. fputs(luaL_check_string(L, 1), stderr);
  73. return 0;
  74. }
  75. /*
  76. ** Standard implementation of _ERRORMESSAGE.
  77. ** The library `liolib' redefines _ERRORMESSAGE for better error information.
  78. */
  79. int luaB__ERRORMESSAGE (lua_State *L) {
  80. lua_getglobals(L);
  81. lua_pushstring(L, LUA_ALERT);
  82. lua_rawget(L);
  83. if (lua_isfunction(L, -1)) { /* avoid error loop if _ALERT is not defined */
  84. const char *s = luaL_check_string(L, 1);
  85. char *buff = luaL_openspace(L, strlen(s)+sizeof("error: \n"));
  86. strcpy(buff, "error: "); strcat(buff, s); strcat(buff, "\n");
  87. lua_pushobject(L, -1); /* function to be called */
  88. lua_pushstring(L, buff);
  89. lua_call(L, 1, 0);
  90. }
  91. return 0;
  92. }
  93. /*
  94. ** If your system does not support `stdout', you can just remove this function.
  95. ** If you need, you can define your own `print' function, following this
  96. ** model but changing `fputs' to put the strings at a proper place
  97. ** (a console window or a log file, for instance).
  98. */
  99. int luaB_print (lua_State *L) {
  100. int n = lua_gettop(L); /* number of arguments */
  101. int i;
  102. lua_getglobal(L, "tostring");
  103. for (i=1; i<=n; i++) {
  104. const char *s;
  105. lua_pushobject(L, -1); /* function to be called */
  106. lua_pushobject(L, i);
  107. if (lua_call(L, 1, 1) != 0)
  108. lua_error(L, "error in `tostring' called by `print'");
  109. s = lua_tostring(L, -1); /* get result */
  110. if (s == NULL)
  111. lua_error(L, "`tostring' must return a string to `print'");
  112. if (i>1) fputs("\t", stdout);
  113. fputs(s, stdout);
  114. lua_settop(L, -1); /* pop result */
  115. }
  116. fputs("\n", stdout);
  117. return 0;
  118. }
  119. int luaB_tonumber (lua_State *L) {
  120. int base = luaL_opt_int(L, 2, 10);
  121. if (base == 10) { /* standard conversion */
  122. luaL_checktype(L, 1, "any");
  123. if (lua_isnumber(L, 1)) {
  124. lua_pushnumber(L, lua_tonumber(L, 1));
  125. return 1;
  126. }
  127. }
  128. else {
  129. const char *s1 = luaL_check_string(L, 1);
  130. char *s2;
  131. Number n;
  132. luaL_arg_check(L, 2 <= base && base <= 36, 2, "base out of range");
  133. n = strtoul(s1, &s2, base);
  134. if (s1 != s2) { /* at least one valid digit? */
  135. while (isspace((unsigned char)*s2)) s2++; /* skip trailing spaces */
  136. if (*s2 == '\0') { /* no invalid trailing characters? */
  137. lua_pushnumber(L, n);
  138. return 1;
  139. }
  140. }
  141. }
  142. lua_pushnil(L); /* else not a number */
  143. return 1;
  144. }
  145. int luaB_error (lua_State *L) {
  146. lua_error(L, luaL_opt_string(L, 1, NULL));
  147. return 0; /* to avoid errors */
  148. }
  149. int luaB_setglobal (lua_State *L) {
  150. luaL_checktype(L, 2, "any");
  151. lua_setglobal(L, luaL_check_string(L, 1));
  152. return 0;
  153. }
  154. int luaB_getglobal (lua_State *L) {
  155. lua_getglobal(L, luaL_check_string(L, 1));
  156. return 1;
  157. }
  158. int luaB_tag (lua_State *L) {
  159. luaL_checktype(L, 1, "any");
  160. lua_pushnumber(L, lua_tag(L, 1));
  161. return 1;
  162. }
  163. int luaB_settag (lua_State *L) {
  164. luaL_checktype(L, 1, "table");
  165. lua_pushobject(L, 1); /* push table */
  166. lua_settag(L, luaL_check_int(L, 2));
  167. lua_pushobject(L, 1); /* return first argument */
  168. return 1;
  169. }
  170. int luaB_newtag (lua_State *L) {
  171. lua_pushnumber(L, lua_newtag(L));
  172. return 1;
  173. }
  174. int luaB_copytagmethods (lua_State *L) {
  175. lua_pushnumber(L, lua_copytagmethods(L, luaL_check_int(L, 1),
  176. luaL_check_int(L, 2)));
  177. return 1;
  178. }
  179. int luaB_globals (lua_State *L) {
  180. lua_getglobals(L); /* value to be returned */
  181. if (!lua_isnull(L, 1)) {
  182. luaL_checktype(L, 1, "table");
  183. lua_pushobject(L, 1); /* new table of globals */
  184. lua_setglobals(L);
  185. }
  186. return 1;
  187. }
  188. int luaB_rawget (lua_State *L) {
  189. luaL_checktype(L, 1, "table");
  190. luaL_checktype(L, 2, "any");
  191. lua_rawget(L);
  192. return 1;
  193. }
  194. int luaB_rawset (lua_State *L) {
  195. luaL_checktype(L, 1, "table");
  196. luaL_checktype(L, 2, "any");
  197. luaL_checktype(L, 3, "any");
  198. lua_rawset(L);
  199. return 1;
  200. }
  201. int luaB_settagmethod (lua_State *L) {
  202. int tag = (int)luaL_check_int(L, 1);
  203. const char *event = luaL_check_string(L, 2);
  204. luaL_arg_check(L, lua_isfunction(L, 3) || lua_isnil(L, 3), 3,
  205. "function or nil expected");
  206. if (strcmp(event, "gc") == 0 && tag != TAG_NIL)
  207. lua_error(L, "deprecated use: cannot set the `gc' tag method from Lua");
  208. lua_settagmethod(L, tag, event);
  209. return 1;
  210. }
  211. int luaB_gettagmethod (lua_State *L) {
  212. lua_gettagmethod(L, luaL_check_int(L, 1), luaL_check_string(L, 2));
  213. return 1;
  214. }
  215. int luaB_collectgarbage (lua_State *L) {
  216. lua_pushnumber(L, lua_collectgarbage(L, luaL_opt_int(L, 1, 0)));
  217. return 1;
  218. }
  219. int luaB_type (lua_State *L) {
  220. luaL_checktype(L, 1, "any");
  221. lua_pushstring(L, lua_type(L, 1));
  222. return 1;
  223. }
  224. /* }====================================================== */
  225. /*
  226. ** {======================================================
  227. ** Functions that could use only the official API but
  228. ** do not, for efficiency.
  229. ** =======================================================
  230. */
  231. static int passresults (lua_State *L, int status, int oldtop) {
  232. if (status == 0) {
  233. int nresults = lua_gettop(L) - oldtop;
  234. if (nresults > 0)
  235. return nresults; /* results are already on the stack */
  236. else {
  237. lua_pushuserdata(L, NULL); /* at least one result to signal no errors */
  238. return 1;
  239. }
  240. }
  241. else { /* error */
  242. lua_pushnil(L);
  243. lua_pushnumber(L, status); /* error code */
  244. return 2;
  245. }
  246. }
  247. int luaB_dostring (lua_State *L) {
  248. int oldtop = lua_gettop(L);
  249. size_t l;
  250. const char *s = luaL_check_lstr(L, 1, &l);
  251. if (*s == ID_CHUNK)
  252. lua_error(L, "`dostring' cannot run pre-compiled code");
  253. return passresults(L, lua_dobuffer(L, s, l, luaL_opt_string(L, 2, s)), oldtop);
  254. }
  255. int luaB_dofile (lua_State *L) {
  256. int oldtop = lua_gettop(L);
  257. const char *fname = luaL_opt_string(L, 1, NULL);
  258. return passresults(L, lua_dofile(L, fname), oldtop);
  259. }
  260. int luaB_call (lua_State *L) {
  261. int oldtop;
  262. const Hash *arg = gettable(L, 2);
  263. const char *options = luaL_opt_string(L, 3, "");
  264. int err = 0; /* index of old error method */
  265. int n = (int)getnarg(L, arg);
  266. int i, status;
  267. if (!lua_isnull(L, 4)) { /* set new error method */
  268. lua_getglobal(L, LUA_ERRORMESSAGE);
  269. err = lua_gettop(L); /* get index */
  270. lua_pushobject(L, 4);
  271. lua_setglobal(L, LUA_ERRORMESSAGE);
  272. }
  273. oldtop = lua_gettop(L); /* top before function-call preparation */
  274. /* push function */
  275. lua_pushobject(L, 1);
  276. /* push arg[1...n] */
  277. luaD_checkstack(L, n);
  278. for (i=0; i<n; i++)
  279. *(L->top++) = *luaH_getnum(arg, i+1);
  280. status = lua_call(L, n, LUA_MULTRET);
  281. if (err != 0) { /* restore old error method */
  282. lua_pushobject(L, err);
  283. lua_setglobal(L, LUA_ERRORMESSAGE);
  284. }
  285. if (status != 0) { /* error in call? */
  286. if (strchr(options, 'x'))
  287. lua_pushnil(L); /* return nil to signal the error */
  288. else
  289. lua_error(L, NULL); /* propagate error without additional messages */
  290. return 1;
  291. }
  292. else { /* no errors */
  293. if (strchr(options, 'p')) { /* pack results? */
  294. luaV_pack(L, luaA_index(L, oldtop+1));
  295. return 1; /* only table is returned */
  296. }
  297. else
  298. return lua_gettop(L) - oldtop; /* results are already on the stack */
  299. }
  300. }
  301. int luaB_next (lua_State *L) {
  302. const Hash *a = gettable(L, 1);
  303. int i; /* `luaA_next' gets first element after `i' */
  304. if (lua_isnull(L, 2) || lua_isnil(L, 2)) /* no index or nil index? */
  305. i = 0; /* get first */
  306. else {
  307. i = luaH_pos(L, a, luaA_index(L, 2))+1;
  308. luaL_arg_check(L, i != 0, 2, "key not found");
  309. }
  310. if (luaA_next(L, a, i) != 0)
  311. return 2; /* `luaA_next' left them on the stack */
  312. else {
  313. lua_pushnil(L);
  314. return 1;
  315. }
  316. }
  317. int luaB_tostring (lua_State *L) {
  318. char buff[64];
  319. const TObject *o;
  320. luaL_checktype(L, 1, "any");
  321. o = luaA_index(L, 1);
  322. switch (ttype(o)) {
  323. case TAG_NUMBER:
  324. lua_pushstring(L, lua_tostring(L, 1));
  325. return 1;
  326. case TAG_STRING:
  327. lua_pushobject(L, 1);
  328. return 1;
  329. case TAG_TABLE:
  330. sprintf(buff, "table: %p", hvalue(o));
  331. break;
  332. case TAG_LCLOSURE: case TAG_CCLOSURE:
  333. sprintf(buff, "function: %p", clvalue(o));
  334. break;
  335. case TAG_USERDATA:
  336. sprintf(buff, "userdata(%d): %p", tsvalue(o)->u.d.tag,
  337. tsvalue(o)->u.d.value);
  338. break;
  339. case TAG_NIL:
  340. lua_pushstring(L, "nil");
  341. return 1;
  342. default:
  343. LUA_INTERNALERROR("invalid type");
  344. }
  345. lua_pushstring(L, buff);
  346. return 1;
  347. }
  348. /* }====================================================== */
  349. /*
  350. ** {======================================================
  351. ** "Extra" functions
  352. ** These functions can be written in Lua, so you can
  353. ** delete them if you need a tiny Lua implementation.
  354. ** If you delete them, remove their entries in array
  355. ** "builtin_funcs".
  356. ** =======================================================
  357. */
  358. int luaB_assert (lua_State *L) {
  359. luaL_checktype(L, 1, "any");
  360. if (lua_isnil(L, 1))
  361. luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, ""));
  362. return 0;
  363. }
  364. int luaB_getn (lua_State *L) {
  365. lua_pushnumber(L, getnarg(L, gettable(L, 1)));
  366. return 1;
  367. }
  368. /* auxiliary function */
  369. static void t_move (lua_State *L, Hash *t, int from, int to) {
  370. TObject *p = luaH_setint(L, t, to); /* may change following `get' */
  371. *p = *luaH_getnum(t, from);
  372. }
  373. int luaB_tinsert (lua_State *L) {
  374. Hash *a = gettable(L, 1);
  375. int n = (int)getnarg(L, a);
  376. int v = lua_gettop(L); /* last argument: to be inserted */
  377. int pos;
  378. if (v == 2) /* called with only 2 arguments */
  379. pos = n+1;
  380. else
  381. pos = luaL_check_int(L, 2); /* 2nd argument is the position */
  382. luaH_setstrnum(L, a, luaS_new(L, "n"), n+1); /* a.n = n+1 */
  383. for (; n>=pos; n--)
  384. t_move(L, a, n, n+1); /* a[n+1] = a[n] */
  385. *luaH_setint(L, a, pos) = *luaA_index(L, v); /* a[pos] = v */
  386. return 0;
  387. }
  388. int luaB_tremove (lua_State *L) {
  389. Hash *a = gettable(L, 1);
  390. int n = (int)getnarg(L, a);
  391. int pos = luaL_opt_int(L, 2, n);
  392. if (n <= 0) return 0; /* table is "empty" */
  393. luaA_pushobject(L, luaH_getnum(a, pos)); /* result = a[pos] */
  394. for ( ;pos<n; pos++)
  395. t_move(L, a, pos+1, pos); /* a[pos] = a[pos+1] */
  396. luaH_setstrnum(L, a, luaS_new(L, "n"), n-1); /* a.n = n-1 */
  397. ttype(luaH_setint(L, a, n)) = TAG_NIL; /* a[n] = nil */
  398. return 1;
  399. }
  400. static int luaB_foreachi (lua_State *L) {
  401. const Hash *t = gettable(L, 1);
  402. int n = (int)getnarg(L, t);
  403. int i;
  404. luaL_checktype(L, 2, "function");
  405. for (i=1; i<=n; i++) {
  406. lua_pushobject(L, 2);
  407. ttype(L->top) = TAG_NUMBER; nvalue(L->top++) = i;
  408. *(L->top++) = *luaH_getnum(t, i);
  409. luaD_call(L, L->top-3, 1);
  410. if (ttype(L->top-1) != TAG_NIL)
  411. return 1;
  412. L->top--; /* remove nil result */
  413. }
  414. return 0;
  415. }
  416. static int luaB_foreach (lua_State *L) {
  417. const Hash *a = gettable(L, 1);
  418. int i;
  419. luaL_checktype(L, 2, "function");
  420. for (i=0; i<a->size; i++) {
  421. const Node *nd = &(a->node[i]);
  422. if (ttype(val(nd)) != TAG_NIL) {
  423. lua_pushobject(L, 2);
  424. *(L->top++) = *key(nd);
  425. *(L->top++) = *val(nd);
  426. luaD_call(L, L->top-3, 1);
  427. if (ttype(L->top-1) != TAG_NIL)
  428. return 1;
  429. L->top--; /* remove result */
  430. }
  431. }
  432. return 0;
  433. }
  434. /*
  435. ** {======================================================
  436. ** Quicksort
  437. ** (based on `Algorithms in MODULA-3', Robert Sedgewick;
  438. ** Addison-Wesley, 1993.)
  439. */
  440. static void swap (lua_State *L, Hash *a, int i, int j) {
  441. TObject temp;
  442. temp = *luaH_getnum(a, i);
  443. t_move(L, a, j, i);
  444. *luaH_setint(L, a, j) = temp;
  445. }
  446. static int sort_comp (lua_State *L, const TObject *f, const TObject *a,
  447. const TObject *b) {
  448. /* WARNING: the caller (auxsort) must ensure stack space */
  449. if (f != NULL) {
  450. *(L->top) = *f;
  451. *(L->top+1) = *a;
  452. *(L->top+2) = *b;
  453. L->top += 3;
  454. luaD_call(L, L->top-3, 1);
  455. L->top--;
  456. return (ttype(L->top) != TAG_NIL);
  457. }
  458. else /* a < b? */
  459. return luaV_lessthan(L, a, b, L->top);
  460. }
  461. static void auxsort (lua_State *L, Hash *a, int l, int u, const TObject *f) {
  462. StkId P = L->top++; /* temporary place for pivot */
  463. ttype(P) = TAG_NIL;
  464. while (l < u) { /* for tail recursion */
  465. int i, j;
  466. /* sort elements a[l], a[(l+u)/2] and a[u] */
  467. if (sort_comp(L, f, luaH_getnum(a, u), luaH_getnum(a, l)))
  468. swap(L, a, l, u); /* a[u]<a[l] */
  469. if (u-l == 1) break; /* only 2 elements */
  470. i = (l+u)/2;
  471. *P = *luaH_getnum(a, i); /* P = a[i] */
  472. if (sort_comp(L, f, P, luaH_getnum(a, l))) /* a[i]<a[l]? */
  473. swap(L, a, l, i);
  474. else if (sort_comp(L, f, luaH_getnum(a, u), P)) /* a[u]<a[i]? */
  475. swap(L, a, i, u);
  476. if (u-l == 2) break; /* only 3 elements */
  477. *P = *luaH_getnum(a, i); /* save pivot on stack (GC) */
  478. swap(L, a, i, u-1); /* put median element as pivot (a[u-1]) */
  479. /* a[l] <= P == a[u-1] <= a[u], only needs to sort from l+1 to u-2 */
  480. i = l; j = u-1;
  481. for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
  482. /* repeat i++ until a[i] >= P */
  483. while (sort_comp(L, f, luaH_getnum(a, ++i), P))
  484. if (i>u) lua_error(L, "invalid order function for sorting");
  485. /* repeat j-- until a[j] <= P */
  486. while (sort_comp(L, f, P, luaH_getnum(a, --j)))
  487. if (j<l) lua_error(L, "invalid order function for sorting");
  488. if (j<i) break;
  489. swap(L, a, i, j);
  490. }
  491. swap(L, a, u-1, i); /* swap pivot (a[u-1]) with a[i] */
  492. /* a[l..i-1] <= a[i] == P <= a[i+1..u] */
  493. /* adjust so that smaller "half" is in [j..i] and larger one in [l..u] */
  494. if (i-l < u-i) {
  495. j=l; i=i-1; l=i+2;
  496. }
  497. else {
  498. j=i+1; i=u; u=j-2;
  499. }
  500. auxsort(L, a, j, i, f); /* call recursively the smaller one */
  501. } /* repeat the routine for the larger one */
  502. L->top--; /* remove pivot from stack */
  503. }
  504. int luaB_sort (lua_State *L) {
  505. Hash *a = gettable(L, 1);
  506. int n = (int)getnarg(L, a);
  507. const TObject *func = NULL;
  508. if (!lua_isnull(L, 2)) { /* is there a 2nd argument? */
  509. luaL_checktype(L, 2, "function");
  510. func = luaA_index(L, 2);
  511. }
  512. auxsort(L, a, 1, n, func);
  513. return 0;
  514. }
  515. /* }====================================================== */
  516. /* }====================================================== */
  517. /*
  518. ** {======================================================
  519. ** Deprecated functions to manipulate global environment:
  520. ** some of them can be simulated through table operations
  521. ** over the global table.
  522. ** =======================================================
  523. */
  524. #ifdef LUA_DEPRECATETFUNCS
  525. #define num_deprecated 4
  526. static const struct luaL_reg deprecated_global_funcs[num_deprecated] = {
  527. {"foreachvar", luaB_foreach},
  528. {"nextvar", luaB_next},
  529. {"rawgetglobal", luaB_rawget},
  530. {"rawsetglobal", luaB_rawset}
  531. };
  532. static const struct luaL_reg other_deprecated_global_funcs[] = {
  533. {"rawgettable", luaB_rawget},
  534. {"rawsettable", luaB_rawset}
  535. };
  536. static void deprecated_funcs (lua_State *L) {
  537. TObject gt;
  538. int i;
  539. ttype(&gt) = TAG_TABLE;
  540. hvalue(&gt) = L->gt;
  541. for (i=0; i<num_deprecated; i++) {
  542. lua_pushobject(L, &gt);
  543. lua_pushcclosure(L, deprecated_global_funcs[i].func, 1); ??
  544. lua_setglobal(L, deprecated_global_funcs[i].name);
  545. }
  546. luaL_openl(L, other_deprecated_global_funcs);
  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, "function `%.20s' is deprecated", luaL_check_string(L, -1));
  554. return 0; /* to avoid warnings */
  555. }
  556. #define num_deprecated 6
  557. static const char *const deprecated_names [num_deprecated] = {
  558. "foreachvar", "nextvar", "rawgetglobal",
  559. "rawgettable", "rawsetglobal", "rawsettable"
  560. };
  561. static void deprecated_funcs (lua_State *L) {
  562. int i;
  563. for (i=0; i<num_deprecated; i++) {
  564. lua_pushstring(L, deprecated_names[i]);
  565. lua_pushcclosure(L, deprecated_func, 1);
  566. lua_setglobal(L, deprecated_names[i]);
  567. }
  568. }
  569. #endif
  570. /* }====================================================== */
  571. static const struct luaL_reg builtin_funcs[] = {
  572. {LUA_ALERT, luaB__ALERT},
  573. {LUA_ERRORMESSAGE, luaB__ERRORMESSAGE},
  574. {"call", luaB_call},
  575. {"collectgarbage", luaB_collectgarbage},
  576. {"copytagmethods", luaB_copytagmethods},
  577. {"dofile", luaB_dofile},
  578. {"dostring", luaB_dostring},
  579. {"error", luaB_error},
  580. {"foreach", luaB_foreach},
  581. {"foreachi", luaB_foreachi},
  582. {"getglobal", luaB_getglobal},
  583. {"gettagmethod", luaB_gettagmethod},
  584. {"globals", luaB_globals},
  585. {"newtag", luaB_newtag},
  586. {"next", luaB_next},
  587. {"print", luaB_print},
  588. {"rawget", luaB_rawget},
  589. {"rawset", luaB_rawset},
  590. {"setglobal", luaB_setglobal},
  591. {"settag", luaB_settag},
  592. {"settagmethod", luaB_settagmethod},
  593. {"tag", luaB_tag},
  594. {"tonumber", luaB_tonumber},
  595. {"tostring", luaB_tostring},
  596. {"type", luaB_type},
  597. /* "Extra" functions */
  598. {"assert", luaB_assert},
  599. {"getn", luaB_getn},
  600. {"sort", luaB_sort},
  601. {"tinsert", luaB_tinsert},
  602. {"tremove", luaB_tremove}
  603. };
  604. void luaB_predefine (lua_State *L) {
  605. luaL_openl(L, builtin_funcs);
  606. #ifdef DEBUG
  607. luaB_opentests(L); /* internal test functions */
  608. #endif
  609. lua_pushstring(L, LUA_VERSION);
  610. lua_setglobal(L, "_VERSION");
  611. deprecated_funcs(L);
  612. }