lbuiltin.c 18 KB

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