lbuiltin.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. /*
  2. ** $Id: lbuiltin.c,v 1.94 2000/03/03 14:58:26 roberto Exp $
  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, TaggedString *s) {
  42. ttype(L->top) = LUA_T_STRING;
  43. tsvalue(L->top) = s;
  44. incr_top;
  45. }
  46. static real getsize (const Hash *h) {
  47. real max = 0;
  48. int i = h->size;
  49. Node *n = h->node;
  50. while (i--) {
  51. if (ttype(key(n)) == LUA_T_NUMBER &&
  52. ttype(val(n)) != LUA_T_NIL &&
  53. nvalue(key(n)) > max)
  54. max = nvalue(key(n));
  55. n++;
  56. }
  57. return max;
  58. }
  59. static real getnarg (lua_State *L, const Hash *a) {
  60. TObject index;
  61. const TObject *value;
  62. /* value = table.n */
  63. ttype(&index) = LUA_T_STRING;
  64. tsvalue(&index) = luaS_new(L, "n");
  65. value = luaH_get(L, a, &index);
  66. return (ttype(value) == LUA_T_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. #ifndef MAXPRINT
  105. #define MAXPRINT 40 /* arbitrary limit */
  106. #endif
  107. void luaB_print (lua_State *L) {
  108. lua_Object args[MAXPRINT];
  109. lua_Object obj;
  110. int n = 0;
  111. int i;
  112. while ((obj = lua_getparam(L, n+1)) != LUA_NOOBJECT) {
  113. luaL_arg_check(L, n < MAXPRINT, n+1, "too many arguments");
  114. args[n++] = obj;
  115. }
  116. for (i=0; i<n; i++) {
  117. lua_pushobject(L, args[i]);
  118. if (lua_call(L, "tostring"))
  119. lua_error(L, "error in `tostring' called by `print'");
  120. obj = lua_getresult(L, 1);
  121. if (!lua_isstring(L, obj))
  122. lua_error(L, "`tostring' must return a string to `print'");
  123. if (i>0) fputs("\t", stdout);
  124. fputs(lua_getstring(L, obj), stdout);
  125. }
  126. fputs("\n", stdout);
  127. }
  128. void luaB_tonumber (lua_State *L) {
  129. int base = luaL_opt_int(L, 2, 10);
  130. if (base == 10) { /* standard conversion */
  131. lua_Object o = lua_getparam(L, 1);
  132. if (lua_isnumber(L, o)) lua_pushnumber(L, lua_getnumber(L, o));
  133. else lua_pushnil(L); /* not a number */
  134. }
  135. else {
  136. const char *s1 = luaL_check_string(L, 1);
  137. char *s2;
  138. real n;
  139. luaL_arg_check(L, 0 <= base && base <= 36, 2, "base out of range");
  140. n = strtoul(s1, &s2, base);
  141. if (s1 == s2) return; /* no valid digits: return nil */
  142. while (isspace((unsigned char)*s2)) s2++; /* skip trailing spaces */
  143. if (*s2) return; /* invalid trailing character: return nil */
  144. lua_pushnumber(L, n);
  145. }
  146. }
  147. void luaB_error (lua_State *L) {
  148. lua_error(L, luaL_opt_string(L, 1, NULL));
  149. }
  150. void luaB_setglobal (lua_State *L) {
  151. const char *name = luaL_check_string(L, 1);
  152. lua_Object value = luaL_nonnullarg(L, 2);
  153. lua_pushobject(L, value);
  154. lua_setglobal(L, name);
  155. }
  156. void luaB_rawsetglobal (lua_State *L) {
  157. const char *name = luaL_check_string(L, 1);
  158. lua_Object value = luaL_nonnullarg(L, 2);
  159. lua_pushobject(L, value);
  160. lua_rawsetglobal(L, name);
  161. }
  162. void luaB_getglobal (lua_State *L) {
  163. lua_pushobject(L, lua_getglobal(L, luaL_check_string(L, 1)));
  164. }
  165. void luaB_rawgetglobal (lua_State *L) {
  166. lua_pushobject(L, lua_rawgetglobal(L, luaL_check_string(L, 1)));
  167. }
  168. void luaB_tag (lua_State *L) {
  169. lua_pushnumber(L, lua_tag(L, lua_getparam(L, 1)));
  170. }
  171. void luaB_settag (lua_State *L) {
  172. lua_Object o = luaL_tablearg(L, 1);
  173. lua_pushobject(L, o);
  174. lua_settag(L, luaL_check_int(L, 2));
  175. lua_pushobject(L, o); /* return first argument */
  176. }
  177. void luaB_newtag (lua_State *L) {
  178. lua_pushnumber(L, lua_newtag(L));
  179. }
  180. void luaB_copytagmethods (lua_State *L) {
  181. lua_pushnumber(L, lua_copytagmethods(L, luaL_check_int(L, 1),
  182. luaL_check_int(L, 2)));
  183. }
  184. void luaB_rawgettable (lua_State *L) {
  185. lua_pushobject(L, luaL_nonnullarg(L, 1));
  186. lua_pushobject(L, luaL_nonnullarg(L, 2));
  187. lua_pushobject(L, lua_rawgettable(L));
  188. }
  189. void luaB_rawsettable (lua_State *L) {
  190. lua_pushobject(L, luaL_nonnullarg(L, 1));
  191. lua_pushobject(L, luaL_nonnullarg(L, 2));
  192. lua_pushobject(L, luaL_nonnullarg(L, 3));
  193. lua_rawsettable(L);
  194. }
  195. void luaB_settagmethod (lua_State *L) {
  196. int tag = luaL_check_int(L, 1);
  197. const char *event = luaL_check_string(L, 2);
  198. lua_Object nf = luaL_nonnullarg(L, 3);
  199. luaL_arg_check(L, lua_isnil(L, nf) || lua_isfunction(L, nf), 3,
  200. "function or nil expected");
  201. #ifndef LUA_COMPAT_GC
  202. if (strcmp(event, "gc") == 0 && tag != LUA_T_NIL)
  203. lua_error(L, "cannot set this `gc' tag method from Lua");
  204. #endif
  205. lua_pushobject(L, nf);
  206. lua_pushobject(L, lua_settagmethod(L, tag, event));
  207. }
  208. void luaB_gettagmethod (lua_State *L) {
  209. lua_pushobject(L, lua_gettagmethod(L, luaL_check_int(L, 1),
  210. luaL_check_string(L, 2)));
  211. }
  212. void luaB_seterrormethod (lua_State *L) {
  213. lua_Object nf = luaL_functionarg(L, 1);
  214. lua_pushobject(L, nf);
  215. lua_pushobject(L, lua_seterrormethod(L));
  216. }
  217. void luaB_collectgarbage (lua_State *L) {
  218. lua_pushnumber(L, lua_collectgarbage(L, luaL_opt_int(L, 1, 0)));
  219. }
  220. void luaB_type (lua_State *L) {
  221. lua_Object o = luaL_nonnullarg(L, 1);
  222. lua_pushstring(L, lua_type(L, o));
  223. }
  224. /* }====================================================== */
  225. /*
  226. ** {======================================================
  227. ** Functions that could use only the official API but
  228. ** do not, for efficiency.
  229. ** =======================================================
  230. */
  231. static void passresults (lua_State *L) {
  232. L->Cstack.base = L->Cstack.lua2C; /* position of first result */
  233. if (L->Cstack.num == 0)
  234. lua_pushuserdata(L, NULL); /* at least one result to signal no errors */
  235. }
  236. void luaB_dostring (lua_State *L) {
  237. long l;
  238. const char *s = luaL_check_lstr(L, 1, &l);
  239. if (*s == ID_CHUNK)
  240. lua_error(L, "`dostring' cannot run pre-compiled code");
  241. if (lua_dobuffer(L, s, l, luaL_opt_string(L, 2, s)) == 0)
  242. passresults(L);
  243. /* else return no value */
  244. }
  245. void luaB_dofile (lua_State *L) {
  246. const char *fname = luaL_opt_string(L, 1, NULL);
  247. if (lua_dofile(L, fname) == 0)
  248. passresults(L);
  249. /* else return no value */
  250. }
  251. void luaB_call (lua_State *L) {
  252. lua_Object f = luaL_nonnullarg(L, 1);
  253. const Hash *arg = gettable(L, 2);
  254. const char *options = luaL_opt_string(L, 3, "");
  255. lua_Object err = lua_getparam(L, 4);
  256. int narg = (int)getnarg(L, arg);
  257. int i, status;
  258. if (err != LUA_NOOBJECT) { /* set new error method */
  259. lua_pushobject(L, err);
  260. err = lua_seterrormethod(L);
  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_seterrormethod(L);
  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 = luaL_nonnullarg(L, 1);
  290. TaggedString *name;
  291. if (ttype(o) == LUA_T_NIL)
  292. name = NULL;
  293. else {
  294. luaL_arg_check(L, ttype(o) == LUA_T_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 = luaL_nonnullarg(L, 2);
  303. int i; /* `luaA_next' gets first element after `i' */
  304. if (ttype(k) == LUA_T_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 = lua_getparam(L, 1);
  315. char buff[64];
  316. switch (ttype(o)) {
  317. case LUA_T_NUMBER:
  318. lua_pushstring(L, lua_getstring(L, o));
  319. return;
  320. case LUA_T_STRING:
  321. lua_pushobject(L, o);
  322. return;
  323. case LUA_T_ARRAY:
  324. sprintf(buff, "table: %p", o->value.a);
  325. break;
  326. case LUA_T_LCLOSURE: case LUA_T_CCLOSURE:
  327. sprintf(buff, "function: %p", o->value.cl);
  328. break;
  329. case LUA_T_LPROTO:
  330. sprintf(buff, "function: %p", o->value.tf);
  331. break;
  332. case LUA_T_CPROTO:
  333. sprintf(buff, "function: %p", o->value.f);
  334. break;
  335. case LUA_T_USERDATA:
  336. sprintf(buff, "userdata: %p(%d)", o->value.ts->u.d.value,
  337. o->value.ts->u.d.tag);
  338. break;
  339. case LUA_T_NIL:
  340. lua_pushstring(L, "nil");
  341. return;
  342. default:
  343. LUA_INTERNALERROR(L, "invalid type");
  344. }
  345. lua_pushstring(L, buff);
  346. }
  347. /* }====================================================== */
  348. /*
  349. ** {======================================================
  350. ** "Extra" functions
  351. ** These functions can be written in Lua, so you can
  352. ** delete them if you need a tiny Lua implementation.
  353. ** If you delete them, remove their entries in array
  354. ** "builtin_funcs".
  355. ** =======================================================
  356. */
  357. void luaB_assert (lua_State *L) {
  358. lua_Object p = lua_getparam(L, 1);
  359. if (p == LUA_NOOBJECT || lua_isnil(L, p))
  360. luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, ""));
  361. }
  362. void luaB_foreachi (lua_State *L) {
  363. const Hash *t = gettable(L, 1);
  364. int n = (int)getnarg(L, t);
  365. int i;
  366. lua_Object f = luaL_functionarg(L, 2);
  367. luaD_checkstack(L, 3); /* for f, key, and val */
  368. for (i=1; i<=n; i++) {
  369. *(L->top++) = *f;
  370. ttype(L->top) = LUA_T_NUMBER; nvalue(L->top++) = i;
  371. *(L->top++) = *luaH_getint(L, t, i);
  372. luaD_call(L, L->top-3, 1);
  373. if (ttype(L->top-1) != LUA_T_NIL)
  374. return;
  375. L->top--; /* remove nil result */
  376. }
  377. }
  378. void luaB_foreach (lua_State *L) {
  379. const Hash *a = gettable(L, 1);
  380. lua_Object f = luaL_functionarg(L, 2);
  381. int i;
  382. luaD_checkstack(L, 3); /* for f, key, and val */
  383. for (i=0; i<a->size; i++) {
  384. const Node *nd = &(a->node[i]);
  385. if (ttype(val(nd)) != LUA_T_NIL) {
  386. *(L->top++) = *f;
  387. *(L->top++) = *key(nd);
  388. *(L->top++) = *val(nd);
  389. luaD_call(L, L->top-3, 1);
  390. if (ttype(L->top-1) != LUA_T_NIL)
  391. return;
  392. L->top--; /* remove result */
  393. }
  394. }
  395. }
  396. void luaB_foreachvar (lua_State *L) {
  397. lua_Object f = luaL_functionarg(L, 1);
  398. GlobalVar *gv;
  399. luaD_checkstack(L, 4); /* for extra var name, f, var name, and globalval */
  400. for (gv = L->rootglobal; gv; gv = gv->next) {
  401. if (gv->value.ttype != LUA_T_NIL) {
  402. pushtagstring(L, gv->name); /* keep (extra) name on stack to avoid GC */
  403. *(L->top++) = *f;
  404. pushtagstring(L, gv->name);
  405. *(L->top++) = gv->value;
  406. luaD_call(L, L->top-3, 1);
  407. if (ttype(L->top-1) != LUA_T_NIL) {
  408. *(L->top-2) = *(L->top-1); /* remove extra name */
  409. L->top--;
  410. return;
  411. }
  412. L->top-=2; /* remove result and extra name */
  413. }
  414. }
  415. }
  416. void luaB_getn (lua_State *L) {
  417. lua_pushnumber(L, getnarg(L, gettable(L, 1)));
  418. }
  419. void luaB_tinsert (lua_State *L) {
  420. Hash *a = gettable(L, 1);
  421. lua_Object v = lua_getparam(L, 3);
  422. int n = (int)getnarg(L, a);
  423. int pos;
  424. if (v != LUA_NOOBJECT)
  425. pos = luaL_check_int(L, 2);
  426. else { /* called with only 2 arguments */
  427. v = luaL_nonnullarg(L, 2);
  428. pos = n+1;
  429. }
  430. luaV_setn(L, a, n+1); /* a.n = n+1 */
  431. for (; n>=pos; n--)
  432. luaH_move(L, a, n, n+1); /* a[n+1] = a[n] */
  433. luaH_setint(L, a, pos, v); /* a[pos] = v */
  434. }
  435. void luaB_tremove (lua_State *L) {
  436. Hash *a = gettable(L, 1);
  437. int n = (int)getnarg(L, a);
  438. int pos = luaL_opt_int(L, 2, n);
  439. if (n <= 0) return; /* table is "empty" */
  440. luaA_pushobject(L, luaH_getint(L, a, pos)); /* result = a[pos] */
  441. for ( ;pos<n; pos++)
  442. luaH_move(L, a, pos+1, pos); /* a[pos] = a[pos+1] */
  443. luaV_setn(L, a, n-1); /* a.n = n-1 */
  444. luaH_setint(L, a, n, &luaO_nilobject); /* a[n] = nil */
  445. }
  446. /*
  447. ** {======================================================
  448. ** Quicksort
  449. ** (based on `Algorithms in MODULA-3', Robert Sedgewick; Addison-Wesley, 1993.)
  450. */
  451. static void swap (lua_State *L, Hash *a, int i, int j) {
  452. TObject temp;
  453. temp = *luaH_getint(L, a, i);
  454. luaH_move(L, a, j, i);
  455. luaH_setint(L, a, j, &temp);
  456. }
  457. static int sort_comp (lua_State *L, lua_Object f, const TObject *a,
  458. const TObject *b) {
  459. /* WARNING: the caller (auxsort) must ensure stack space */
  460. if (f != LUA_NOOBJECT) {
  461. *(L->top) = *f;
  462. *(L->top+1) = *a;
  463. *(L->top+2) = *b;
  464. L->top += 3;
  465. luaD_call(L, L->top-3, 1);
  466. L->top--;
  467. return (ttype(L->top) != LUA_T_NIL);
  468. }
  469. else /* a < b? */
  470. return luaV_lessthan(L, a, b, L->top);
  471. }
  472. static void auxsort (lua_State *L, Hash *a, int l, int u, lua_Object f) {
  473. StkId P = L->top++; /* temporary place for pivot */
  474. ttype(P) = LUA_T_NIL;
  475. while (l < u) { /* for tail recursion */
  476. int i, j;
  477. /* sort elements a[l], a[(l+u)/2] and a[u] */
  478. if (sort_comp(L, f, luaH_getint(L, a, u), luaH_getint(L, a, l)))
  479. swap(L, a, l, u); /* a[u]<a[l] */
  480. if (u-l == 1) break; /* only 2 elements */
  481. i = (l+u)/2;
  482. *P = *luaH_getint(L, a, i); /* P = a[i] */
  483. if (sort_comp(L, f, P, luaH_getint(L, a, l))) /* a[i]<a[l]? */
  484. swap(L, a, l, i);
  485. else if (sort_comp(L, f, luaH_getint(L, a, u), P)) /* a[u]<a[i]? */
  486. swap(L, a, i, u);
  487. if (u-l == 2) break; /* only 3 elements */
  488. *P = *luaH_getint(L, a, i); /* save pivot on stack (GC) */
  489. swap(L, a, i, u-1); /* put median element as pivot (a[u-1]) */
  490. /* a[l] <= P == a[u-1] <= a[u], only needs to sort from l+1 to u-2 */
  491. i = l; j = u-1;
  492. for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
  493. /* repeat i++ until a[i] >= P */
  494. while (sort_comp(L, f, luaH_getint(L, a, ++i), P))
  495. if (i>u) lua_error(L, "invalid order function for sorting");
  496. /* repeat j-- until a[j] <= P */
  497. while (sort_comp(L, f, P, luaH_getint(L, a, --j)))
  498. if (j<l) lua_error(L, "invalid order function for sorting");
  499. if (j<i) break;
  500. swap(L, a, i, j);
  501. }
  502. swap(L, a, u-1, i); /* swap pivot (a[u-1]) with a[i] */
  503. /* a[l..i-1] <= a[i] == P <= a[i+1..u] */
  504. /* adjust so that smaller "half" is in [j..i] and larger one in [l..u] */
  505. if (i-l < u-i) {
  506. j=l; i=i-1; l=i+2;
  507. }
  508. else {
  509. j=i+1; i=u; u=j-2;
  510. }
  511. auxsort(L, a, j, i, f); /* call recursively the smaller one */
  512. } /* repeat the routine for the larger one */
  513. L->top--; /* remove pivot from stack */
  514. }
  515. void luaB_sort (lua_State *L) {
  516. lua_Object t = lua_getparam(L, 1);
  517. Hash *a = gettable(L, 1);
  518. int n = (int)getnarg(L, a);
  519. lua_Object func = lua_getparam(L, 2);
  520. luaL_arg_check(L, func == LUA_NOOBJECT || lua_isfunction(L, func), 2,
  521. "function expected");
  522. luaD_checkstack(L, 4); /* for pivot, f, a, b (sort_comp) */
  523. auxsort(L, a, 1, n, func);
  524. lua_pushobject(L, t);
  525. }
  526. /* }====================================================== */
  527. /* }====================================================== */
  528. static const struct luaL_reg builtin_funcs[] = {
  529. {"_ALERT", luaB__ALERT},
  530. {"_ERRORMESSAGE", luaB__ERRORMESSAGE},
  531. {"call", luaB_call},
  532. {"collectgarbage", luaB_collectgarbage},
  533. {"copytagmethods", luaB_copytagmethods},
  534. {"dofile", luaB_dofile},
  535. {"dostring", luaB_dostring},
  536. {"error", luaB_error},
  537. {"getglobal", luaB_getglobal},
  538. {"gettagmethod", luaB_gettagmethod},
  539. {"newtag", luaB_newtag},
  540. {"next", luaB_next},
  541. {"nextvar", luaB_nextvar},
  542. {"print", luaB_print},
  543. {"rawgetglobal", luaB_rawgetglobal},
  544. {"rawgettable", luaB_rawgettable},
  545. {"rawsetglobal", luaB_rawsetglobal},
  546. {"rawsettable", luaB_rawsettable},
  547. {"seterrormethod", luaB_seterrormethod},
  548. {"setglobal", luaB_setglobal},
  549. {"settag", luaB_settag},
  550. {"settagmethod", luaB_settagmethod},
  551. {"tag", luaB_tag},
  552. {"tonumber", luaB_tonumber},
  553. {"tostring", luaB_tostring},
  554. {"type", luaB_type},
  555. /* "Extra" functions */
  556. {"assert", luaB_assert},
  557. {"foreach", luaB_foreach},
  558. {"foreachi", luaB_foreachi},
  559. {"foreachvar", luaB_foreachvar},
  560. {"getn", luaB_getn},
  561. {"sort", luaB_sort},
  562. {"tinsert", luaB_tinsert},
  563. {"tremove", luaB_tremove}
  564. };
  565. void luaB_predefine (lua_State *L) {
  566. /* pre-register mem error messages, to avoid loop when error arises */
  567. luaS_newfixed(L, tableEM);
  568. luaS_newfixed(L, memEM);
  569. luaL_openl(L, builtin_funcs);
  570. #ifdef DEBUG
  571. luaB_opentests(L); /* internal test functions */
  572. #endif
  573. lua_pushstring(L, LUA_VERSION);
  574. lua_setglobal(L, "_VERSION");
  575. }