lbuiltin.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /*
  2. ** $Id: lbuiltin.c,v 1.124 2000/08/29 14:41:56 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. int luaB_next (lua_State *L) {
  225. luaL_checktype(L, 1, "table");
  226. lua_settop(L, 2); /* create a 2nd argument if there isn't one */
  227. if (lua_next(L))
  228. return 2;
  229. else {
  230. lua_pushnil(L);
  231. return 1;
  232. }
  233. }
  234. /* }====================================================== */
  235. /*
  236. ** {======================================================
  237. ** Functions that could use only the official API but
  238. ** do not, for efficiency.
  239. ** =======================================================
  240. */
  241. static int passresults (lua_State *L, int status, int oldtop) {
  242. if (status == 0) {
  243. int nresults = lua_gettop(L) - oldtop;
  244. if (nresults > 0)
  245. return nresults; /* results are already on the stack */
  246. else {
  247. lua_pushuserdata(L, NULL); /* at least one result to signal no errors */
  248. return 1;
  249. }
  250. }
  251. else { /* error */
  252. lua_pushnil(L);
  253. lua_pushnumber(L, status); /* error code */
  254. return 2;
  255. }
  256. }
  257. int luaB_dostring (lua_State *L) {
  258. int oldtop = lua_gettop(L);
  259. size_t l;
  260. const char *s = luaL_check_lstr(L, 1, &l);
  261. if (*s == ID_CHUNK)
  262. lua_error(L, "`dostring' cannot run pre-compiled code");
  263. return passresults(L, lua_dobuffer(L, s, l, luaL_opt_string(L, 2, s)), oldtop);
  264. }
  265. int luaB_dofile (lua_State *L) {
  266. int oldtop = lua_gettop(L);
  267. const char *fname = luaL_opt_string(L, 1, NULL);
  268. return passresults(L, lua_dofile(L, fname), oldtop);
  269. }
  270. int luaB_call (lua_State *L) {
  271. int oldtop;
  272. const Hash *arg = gettable(L, 2);
  273. const char *options = luaL_opt_string(L, 3, "");
  274. int err = 0; /* index of old error method */
  275. int n = (int)getnarg(L, arg);
  276. int i, status;
  277. if (!lua_isnull(L, 4)) { /* set new error method */
  278. lua_getglobal(L, LUA_ERRORMESSAGE);
  279. err = lua_gettop(L); /* get index */
  280. lua_pushobject(L, 4);
  281. lua_setglobal(L, LUA_ERRORMESSAGE);
  282. }
  283. oldtop = lua_gettop(L); /* top before function-call preparation */
  284. /* push function */
  285. lua_pushobject(L, 1);
  286. /* push arg[1...n] */
  287. luaD_checkstack(L, n);
  288. for (i=0; i<n; i++)
  289. *(L->top++) = *luaH_getnum(arg, i+1);
  290. status = lua_call(L, n, LUA_MULTRET);
  291. if (err != 0) { /* restore old error method */
  292. lua_pushobject(L, err);
  293. lua_setglobal(L, LUA_ERRORMESSAGE);
  294. }
  295. if (status != 0) { /* error in call? */
  296. if (strchr(options, 'x'))
  297. lua_pushnil(L); /* return nil to signal the error */
  298. else
  299. lua_error(L, NULL); /* propagate error without additional messages */
  300. return 1;
  301. }
  302. else { /* no errors */
  303. if (strchr(options, 'p')) { /* pack results? */
  304. luaV_pack(L, luaA_index(L, oldtop+1));
  305. return 1; /* only table is returned */
  306. }
  307. else
  308. return lua_gettop(L) - oldtop; /* results are already on the stack */
  309. }
  310. }
  311. int luaB_tostring (lua_State *L) {
  312. char buff[64];
  313. const TObject *o;
  314. luaL_checktype(L, 1, "any");
  315. o = luaA_index(L, 1);
  316. switch (ttype(o)) {
  317. case TAG_NUMBER:
  318. lua_pushstring(L, lua_tostring(L, 1));
  319. return 1;
  320. case TAG_STRING:
  321. lua_pushobject(L, 1);
  322. return 1;
  323. case TAG_TABLE:
  324. sprintf(buff, "table: %p", hvalue(o));
  325. break;
  326. case TAG_LCLOSURE: case TAG_CCLOSURE:
  327. sprintf(buff, "function: %p", clvalue(o));
  328. break;
  329. case TAG_USERDATA:
  330. sprintf(buff, "userdata(%d): %p", tsvalue(o)->u.d.tag,
  331. tsvalue(o)->u.d.value);
  332. break;
  333. case TAG_NIL:
  334. lua_pushstring(L, "nil");
  335. return 1;
  336. default:
  337. LUA_INTERNALERROR("invalid type");
  338. }
  339. lua_pushstring(L, buff);
  340. return 1;
  341. }
  342. /* }====================================================== */
  343. /*
  344. ** {======================================================
  345. ** "Extra" functions
  346. ** These functions can be written in Lua, so you can
  347. ** delete them if you need a tiny Lua implementation.
  348. ** If you delete them, remove their entries in array
  349. ** "builtin_funcs".
  350. ** =======================================================
  351. */
  352. int luaB_assert (lua_State *L) {
  353. luaL_checktype(L, 1, "any");
  354. if (lua_isnil(L, 1))
  355. luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, ""));
  356. return 0;
  357. }
  358. int luaB_getn (lua_State *L) {
  359. lua_pushnumber(L, getnarg(L, gettable(L, 1)));
  360. return 1;
  361. }
  362. /* auxiliary function */
  363. static void t_move (lua_State *L, Hash *t, int from, int to) {
  364. TObject *p = luaH_setint(L, t, to); /* may change following `get' */
  365. *p = *luaH_getnum(t, from);
  366. }
  367. int luaB_tinsert (lua_State *L) {
  368. Hash *a = gettable(L, 1);
  369. int n = (int)getnarg(L, a);
  370. int v = lua_gettop(L); /* last argument: to be inserted */
  371. int pos;
  372. if (v == 2) /* called with only 2 arguments */
  373. pos = n+1;
  374. else
  375. pos = luaL_check_int(L, 2); /* 2nd argument is the position */
  376. luaH_setstrnum(L, a, luaS_new(L, "n"), n+1); /* a.n = n+1 */
  377. for (; n>=pos; n--)
  378. t_move(L, a, n, n+1); /* a[n+1] = a[n] */
  379. *luaH_setint(L, a, pos) = *luaA_index(L, v); /* a[pos] = v */
  380. return 0;
  381. }
  382. int luaB_tremove (lua_State *L) {
  383. Hash *a = gettable(L, 1);
  384. int n = (int)getnarg(L, a);
  385. int pos = luaL_opt_int(L, 2, n);
  386. if (n <= 0) return 0; /* table is "empty" */
  387. luaA_pushobject(L, luaH_getnum(a, pos)); /* result = a[pos] */
  388. for ( ;pos<n; pos++)
  389. t_move(L, a, pos+1, pos); /* a[pos] = a[pos+1] */
  390. luaH_setstrnum(L, a, luaS_new(L, "n"), n-1); /* a.n = n-1 */
  391. ttype(luaH_setint(L, a, n)) = TAG_NIL; /* a[n] = nil */
  392. return 1;
  393. }
  394. static int luaB_foreachi (lua_State *L) {
  395. const Hash *t = gettable(L, 1);
  396. int n = (int)getnarg(L, t);
  397. int i;
  398. luaL_checktype(L, 2, "function");
  399. for (i=1; i<=n; i++) {
  400. lua_pushobject(L, 2);
  401. ttype(L->top) = TAG_NUMBER; nvalue(L->top++) = i;
  402. *(L->top++) = *luaH_getnum(t, i);
  403. luaD_call(L, L->top-3, 1);
  404. if (ttype(L->top-1) != TAG_NIL)
  405. return 1;
  406. L->top--; /* remove nil result */
  407. }
  408. return 0;
  409. }
  410. static int luaB_foreach (lua_State *L) {
  411. const Hash *a = gettable(L, 1);
  412. int i;
  413. luaL_checktype(L, 2, "function");
  414. for (i=0; i<a->size; i++) {
  415. const Node *nd = &(a->node[i]);
  416. if (ttype(val(nd)) != TAG_NIL) {
  417. lua_pushobject(L, 2);
  418. *(L->top++) = *key(nd);
  419. *(L->top++) = *val(nd);
  420. luaD_call(L, L->top-3, 1);
  421. if (ttype(L->top-1) != TAG_NIL)
  422. return 1;
  423. L->top--; /* remove result */
  424. }
  425. }
  426. return 0;
  427. }
  428. /*
  429. ** {======================================================
  430. ** Quicksort
  431. ** (based on `Algorithms in MODULA-3', Robert Sedgewick;
  432. ** Addison-Wesley, 1993.)
  433. */
  434. static void swap (lua_State *L, Hash *a, int i, int j) {
  435. TObject temp;
  436. temp = *luaH_getnum(a, i);
  437. t_move(L, a, j, i);
  438. *luaH_setint(L, a, j) = temp;
  439. }
  440. static int sort_comp (lua_State *L, const TObject *f, const TObject *a,
  441. const TObject *b) {
  442. /* WARNING: the caller (auxsort) must ensure stack space */
  443. if (f != NULL) {
  444. *(L->top) = *f;
  445. *(L->top+1) = *a;
  446. *(L->top+2) = *b;
  447. L->top += 3;
  448. luaD_call(L, L->top-3, 1);
  449. L->top--;
  450. return (ttype(L->top) != TAG_NIL);
  451. }
  452. else /* a < b? */
  453. return luaV_lessthan(L, a, b, L->top);
  454. }
  455. static void auxsort (lua_State *L, Hash *a, int l, int u, const TObject *f) {
  456. StkId P = L->top++; /* temporary place for pivot */
  457. ttype(P) = TAG_NIL;
  458. while (l < u) { /* for tail recursion */
  459. int i, j;
  460. /* sort elements a[l], a[(l+u)/2] and a[u] */
  461. if (sort_comp(L, f, luaH_getnum(a, u), luaH_getnum(a, l)))
  462. swap(L, a, l, u); /* a[u]<a[l] */
  463. if (u-l == 1) break; /* only 2 elements */
  464. i = (l+u)/2;
  465. *P = *luaH_getnum(a, i); /* P = a[i] */
  466. if (sort_comp(L, f, P, luaH_getnum(a, l))) /* a[i]<a[l]? */
  467. swap(L, a, l, i);
  468. else if (sort_comp(L, f, luaH_getnum(a, u), P)) /* a[u]<a[i]? */
  469. swap(L, a, i, u);
  470. if (u-l == 2) break; /* only 3 elements */
  471. *P = *luaH_getnum(a, i); /* save pivot on stack (GC) */
  472. swap(L, a, i, u-1); /* put median element as pivot (a[u-1]) */
  473. /* a[l] <= P == a[u-1] <= a[u], only needs 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 (sort_comp(L, f, luaH_getnum(a, ++i), P))
  478. if (i>u) lua_error(L, "invalid order function for sorting");
  479. /* repeat j-- until a[j] <= P */
  480. while (sort_comp(L, f, P, luaH_getnum(a, --j)))
  481. if (j<l) lua_error(L, "invalid order function for sorting");
  482. if (j<i) break;
  483. swap(L, a, i, j);
  484. }
  485. swap(L, a, u-1, i); /* swap pivot (a[u-1]) with a[i] */
  486. /* a[l..i-1] <= a[i] == P <= a[i+1..u] */
  487. /* adjust so that smaller "half" is in [j..i] and larger one in [l..u] */
  488. if (i-l < u-i) {
  489. j=l; i=i-1; l=i+2;
  490. }
  491. else {
  492. j=i+1; i=u; u=j-2;
  493. }
  494. auxsort(L, a, j, i, f); /* call recursively the smaller one */
  495. } /* repeat the routine for the larger one */
  496. L->top--; /* remove pivot from stack */
  497. }
  498. int luaB_sort (lua_State *L) {
  499. Hash *a = gettable(L, 1);
  500. int n = (int)getnarg(L, a);
  501. const TObject *func = NULL;
  502. if (!lua_isnull(L, 2)) { /* is there a 2nd argument? */
  503. luaL_checktype(L, 2, "function");
  504. func = luaA_index(L, 2);
  505. }
  506. auxsort(L, a, 1, n, func);
  507. return 0;
  508. }
  509. /* }====================================================== */
  510. /* }====================================================== */
  511. /*
  512. ** {======================================================
  513. ** Deprecated functions to manipulate global environment:
  514. ** some of them can be simulated through table operations
  515. ** over the global table.
  516. ** =======================================================
  517. */
  518. #ifdef LUA_DEPRECATETFUNCS
  519. #define num_deprecated 4
  520. static const struct luaL_reg deprecated_global_funcs[num_deprecated] = {
  521. {"foreachvar", luaB_foreach},
  522. {"nextvar", luaB_next},
  523. {"rawgetglobal", luaB_rawget},
  524. {"rawsetglobal", luaB_rawset}
  525. };
  526. static const struct luaL_reg other_deprecated_global_funcs[] = {
  527. {"rawgettable", luaB_rawget},
  528. {"rawsettable", luaB_rawset}
  529. };
  530. static void deprecated_funcs (lua_State *L) {
  531. TObject gt;
  532. int i;
  533. ttype(&gt) = TAG_TABLE;
  534. hvalue(&gt) = L->gt;
  535. for (i=0; i<num_deprecated; i++) {
  536. lua_pushobject(L, &gt);
  537. lua_pushcclosure(L, deprecated_global_funcs[i].func, 1); ??
  538. lua_setglobal(L, deprecated_global_funcs[i].name);
  539. }
  540. luaL_openl(L, other_deprecated_global_funcs);
  541. }
  542. #else
  543. /*
  544. ** gives an explicit error in any attempt to call a deprecated function
  545. */
  546. static int deprecated_func (lua_State *L) {
  547. luaL_verror(L, "function `%.20s' is deprecated", luaL_check_string(L, -1));
  548. return 0; /* to avoid warnings */
  549. }
  550. #define num_deprecated 6
  551. static const char *const deprecated_names [num_deprecated] = {
  552. "foreachvar", "nextvar", "rawgetglobal",
  553. "rawgettable", "rawsetglobal", "rawsettable"
  554. };
  555. static void deprecated_funcs (lua_State *L) {
  556. int i;
  557. for (i=0; i<num_deprecated; i++) {
  558. lua_pushstring(L, deprecated_names[i]);
  559. lua_pushcclosure(L, deprecated_func, 1);
  560. lua_setglobal(L, deprecated_names[i]);
  561. }
  562. }
  563. #endif
  564. /* }====================================================== */
  565. static const struct luaL_reg builtin_funcs[] = {
  566. {LUA_ALERT, luaB__ALERT},
  567. {LUA_ERRORMESSAGE, luaB__ERRORMESSAGE},
  568. {"call", luaB_call},
  569. {"collectgarbage", luaB_collectgarbage},
  570. {"copytagmethods", luaB_copytagmethods},
  571. {"dofile", luaB_dofile},
  572. {"dostring", luaB_dostring},
  573. {"error", luaB_error},
  574. {"foreach", luaB_foreach},
  575. {"foreachi", luaB_foreachi},
  576. {"getglobal", luaB_getglobal},
  577. {"gettagmethod", luaB_gettagmethod},
  578. {"globals", luaB_globals},
  579. {"newtag", luaB_newtag},
  580. {"next", luaB_next},
  581. {"print", luaB_print},
  582. {"rawget", luaB_rawget},
  583. {"rawset", luaB_rawset},
  584. {"setglobal", luaB_setglobal},
  585. {"settag", luaB_settag},
  586. {"settagmethod", luaB_settagmethod},
  587. {"tag", luaB_tag},
  588. {"tonumber", luaB_tonumber},
  589. {"tostring", luaB_tostring},
  590. {"type", luaB_type},
  591. /* "Extra" functions */
  592. {"assert", luaB_assert},
  593. {"getn", luaB_getn},
  594. {"sort", luaB_sort},
  595. {"tinsert", luaB_tinsert},
  596. {"tremove", luaB_tremove}
  597. };
  598. void luaB_predefine (lua_State *L) {
  599. luaL_openl(L, builtin_funcs);
  600. #ifdef DEBUG
  601. luaB_opentests(L); /* internal test functions */
  602. #endif
  603. lua_pushstring(L, LUA_VERSION);
  604. lua_setglobal(L, "_VERSION");
  605. deprecated_funcs(L);
  606. }