lbuiltin.c 18 KB

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