lbuiltin.c 18 KB

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