lbuiltin.c 18 KB

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