lbuiltin.c 17 KB

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