lbuiltin.c 18 KB

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