lbuiltin.c 19 KB

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