lbuiltin.c 18 KB

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