lbuiltin.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. /*
  2. ** $Id: lbuiltin.c,v 1.52 1999/02/22 14:17:24 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(*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); /* returns 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, NULL)) == 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. /* }====================================================== */
  254. /*
  255. ** {======================================================
  256. ** "Extra" functions
  257. ** These functions can be written in Lua, so you can
  258. ** delete them if you need a tiny Lua implementation.
  259. ** If you delete them, remove their entries in array
  260. ** "builtin_funcs".
  261. ** =======================================================
  262. */
  263. static void luaB_assert (void) {
  264. lua_Object p = lua_getparam(1);
  265. if (p == LUA_NOOBJECT || lua_isnil(p))
  266. luaL_verror("assertion failed! %.100s", luaL_opt_string(2, ""));
  267. }
  268. static void luaB_foreachi (void) {
  269. Hash *t = gethash(1);
  270. TObject *f = luaA_Address(luaL_functionarg(2));
  271. int i;
  272. int n = (int)getnarg(t);
  273. luaD_checkstack(3); /* for f, ref, and val */
  274. for (i=1; i<=n; i++) {
  275. *(L->stack.top++) = *f;
  276. ttype(L->stack.top) = LUA_T_NUMBER; nvalue(L->stack.top++) = i;
  277. *(L->stack.top++) = *luaH_getint(t, i);
  278. luaD_calln(2, 1);
  279. if (ttype(L->stack.top-1) != LUA_T_NIL)
  280. return;
  281. L->stack.top--;
  282. }
  283. }
  284. static void luaB_foreach (void) {
  285. Hash *a = gethash(1);
  286. TObject *f = luaA_Address(luaL_functionarg(2));
  287. int i;
  288. luaD_checkstack(3); /* for f, ref, and val */
  289. for (i=0; i<a->nhash; i++) {
  290. Node *nd = &(a->node[i]);
  291. if (ttype(val(nd)) != LUA_T_NIL) {
  292. *(L->stack.top++) = *f;
  293. *(L->stack.top++) = *ref(nd);
  294. *(L->stack.top++) = *val(nd);
  295. luaD_calln(2, 1);
  296. if (ttype(L->stack.top-1) != LUA_T_NIL)
  297. return;
  298. L->stack.top--;
  299. }
  300. }
  301. }
  302. static void luaB_foreachvar (void) {
  303. TObject *f = luaA_Address(luaL_functionarg(1));
  304. GCnode *g;
  305. StkId name = L->Cstack.base++; /* place to keep var name (to avoid GC) */
  306. luaD_checkstack(4); /* for var name, f, s, and globalvar */
  307. ttype(L->stack.stack+name) = LUA_T_NIL;
  308. L->stack.top++; /* top == base */
  309. for (g = L->rootglobal.next; g; g = g->next) {
  310. TaggedString *s = (TaggedString *)g;
  311. if (s->u.s.globalval.ttype != LUA_T_NIL) {
  312. ttype(L->stack.stack+name) = LUA_T_STRING;
  313. tsvalue(L->stack.stack+name) = s; /* keep s on stack to avoid GC */
  314. *(L->stack.top++) = *f;
  315. pushtagstring(s);
  316. *(L->stack.top++) = s->u.s.globalval;
  317. luaD_calln(2, 1);
  318. if (ttype(L->stack.top-1) != LUA_T_NIL)
  319. return;
  320. L->stack.top--;
  321. }
  322. }
  323. }
  324. static void luaB_getn (void) {
  325. lua_pushnumber(getnarg(gethash(1)));
  326. }
  327. static void luaB_tinsert (void) {
  328. Hash *a = gethash(1);
  329. lua_Object v = lua_getparam(3);
  330. int n = (int)getnarg(a);
  331. int pos;
  332. if (v != LUA_NOOBJECT)
  333. pos = luaL_check_int(2);
  334. else { /* called with only 2 arguments */
  335. v = luaL_nonnullarg(2);
  336. pos = n+1;
  337. }
  338. luaV_setn(a, n+1); /* increment field "n" */
  339. for ( ;n>=pos; n--)
  340. luaH_move(a, n, n+1);
  341. luaH_setint(a, pos, luaA_Address(v));
  342. }
  343. static void luaB_tremove (void) {
  344. Hash *a = gethash(1);
  345. int n = (int)getnarg(a);
  346. int pos = luaL_opt_int(2, n);
  347. if (n <= 0) return; /* table is "empty" */
  348. luaA_pushobject(luaH_getint(a, pos)); /* push result */
  349. luaV_setn(a, n-1); /* decrement field "n" */
  350. for ( ;pos<n; pos++)
  351. luaH_move(a, pos+1, pos);
  352. }
  353. /* {
  354. ** Quicksort
  355. */
  356. static void swap (Hash *a, int i, int j) {
  357. TObject temp;
  358. temp = *luaH_getint(a, i);
  359. luaH_move(a, j, i);
  360. luaH_setint(a, j, &temp);
  361. }
  362. static int sort_comp (TObject *f, TObject *a, TObject *b) {
  363. /* notice: the caller (auxsort) must check stack space */
  364. if (f) {
  365. *(L->stack.top) = *f;
  366. *(L->stack.top+1) = *a;
  367. *(L->stack.top+2) = *b;
  368. L->stack.top += 3;
  369. luaD_calln(2, 1);
  370. }
  371. else { /* a < b? */
  372. *(L->stack.top) = *a;
  373. *(L->stack.top+1) = *b;
  374. L->stack.top += 2;
  375. luaV_comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, IM_LT);
  376. }
  377. return ttype(--(L->stack.top)) != LUA_T_NIL;
  378. }
  379. static void auxsort (Hash *a, int l, int u, TObject *f) {
  380. while (l < u) { /* for tail recursion */
  381. TObject *P;
  382. int i, j;
  383. /* sort elements a[l], a[(l+u)/2] and a[u] */
  384. if (sort_comp(f, luaH_getint(a, u), luaH_getint(a, l))) /* a[l]>a[u]? */
  385. swap(a, l, u);
  386. if (u-l == 1) break; /* only 2 elements */
  387. i = (l+u)/2;
  388. P = luaH_getint(a, i);
  389. if (sort_comp(f, P, luaH_getint(a, l))) /* a[l]>a[i]? */
  390. swap(a, l, i);
  391. else if (sort_comp(f, luaH_getint(a, u), P)) /* a[i]>a[u]? */
  392. swap(a, i, u);
  393. if (u-l == 2) break; /* only 3 elements */
  394. P = L->stack.top++;
  395. *P = *luaH_getint(a, i); /* save pivot on stack (for GC) */
  396. swap(a, i, u-1); /* put median element as pivot (a[u-1]) */
  397. /* a[l] <= P == a[u-1] <= a[u], only needs to sort from l+1 to u-2 */
  398. i = l; j = u-1;
  399. for (;;) {
  400. /* invariant: a[l..i] <= P <= a[j..u] */
  401. while (sort_comp(f, luaH_getint(a, ++i), P)) /* stop when a[i] >= P */
  402. if (i>u) lua_error("invalid order function for sorting");
  403. while (sort_comp(f, P, luaH_getint(a, --j))) /* stop when a[j] <= P */
  404. if (j<l) lua_error("invalid order function for sorting");
  405. if (j<i) break;
  406. swap(a, i, j);
  407. }
  408. swap(a, u-1, i); /* swap pivot (a[u-1]) with a[i] */
  409. L->stack.top--; /* remove pivot from stack */
  410. /* a[l..i-1] <= a[i] == P <= a[i+1..u] */
  411. /* adjust so that smaller "half" is in [j..i] and larger one in [l..u] */
  412. if (i-l < u-i) {
  413. j=l; i=i-1; l=i+2;
  414. }
  415. else {
  416. j=i+1; i=u; u=j-2;
  417. }
  418. auxsort(a, j, i, f); /* call recursively the smaller one */
  419. } /* repeat the routine for the larger one */
  420. }
  421. static void luaB_sort (void) {
  422. lua_Object t = lua_getparam(1);
  423. Hash *a = gethash(1);
  424. int n = (int)getnarg(a);
  425. lua_Object func = lua_getparam(2);
  426. TObject *f = luaA_Address(func);
  427. luaL_arg_check(!f || lua_isfunction(func), 2, "function expected");
  428. luaD_checkstack(4); /* for Pivot, f, a, b (sort_comp) */
  429. auxsort(a, 1, n, f);
  430. lua_pushobject(t);
  431. }
  432. /* }}===================================================== */
  433. /*
  434. ** {======================================================
  435. ** Internal Functions.
  436. ** These functions need access to internal structures
  437. ** to be implemented.
  438. ** =======================================================
  439. */
  440. static void luaB_nextvar (void) {
  441. TObject *o = luaA_Address(luaL_nonnullarg(1));
  442. TaggedString *g;
  443. if (ttype(o) == LUA_T_NIL)
  444. g = NULL;
  445. else {
  446. luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected");
  447. g = tsvalue(o);
  448. }
  449. g = luaA_nextvar(g);
  450. if (g) {
  451. pushtagstring(g);
  452. luaA_pushobject(&g->u.s.globalval);
  453. }
  454. else lua_pushnil(); /* no more globals */
  455. }
  456. static void luaB_next (void) {
  457. Node *n = luaH_next(gethash(1), luaA_Address(luaL_nonnullarg(2)));
  458. if (n) {
  459. luaA_pushobject(&n->ref);
  460. luaA_pushobject(&n->val);
  461. }
  462. else lua_pushnil();
  463. }
  464. static void luaB_tostring (void) {
  465. lua_Object obj = lua_getparam(1);
  466. TObject *o = luaA_Address(obj);
  467. char buff[64];
  468. switch (ttype(o)) {
  469. case LUA_T_NUMBER:
  470. lua_pushstring(lua_getstring(obj));
  471. return;
  472. case LUA_T_STRING:
  473. lua_pushobject(obj);
  474. return;
  475. case LUA_T_ARRAY:
  476. sprintf(buff, "table: %p", (void *)o->value.a);
  477. break;
  478. case LUA_T_CLOSURE:
  479. sprintf(buff, "function: %p", (void *)o->value.cl);
  480. break;
  481. case LUA_T_PROTO:
  482. sprintf(buff, "function: %p", (void *)o->value.tf);
  483. break;
  484. case LUA_T_CPROTO:
  485. sprintf(buff, "function: %p", (void *)o->value.f);
  486. break;
  487. case LUA_T_USERDATA:
  488. sprintf(buff, "userdata: %p", o->value.ts->u.d.v);
  489. break;
  490. case LUA_T_NIL:
  491. lua_pushstring("nil");
  492. return;
  493. default:
  494. LUA_INTERNALERROR("invalid type");
  495. }
  496. lua_pushstring(buff);
  497. }
  498. static void luaB_type (void) {
  499. lua_Object o = luaL_nonnullarg(1);
  500. lua_pushstring(luaO_typename(luaA_Address(o)));
  501. lua_pushnumber(lua_tag(o));
  502. }
  503. /* }====================================================== */
  504. #ifdef DEBUG
  505. /*
  506. ** {======================================================
  507. ** some DEBUG functions
  508. ** =======================================================
  509. */
  510. static void mem_query (void) {
  511. lua_pushnumber(totalmem);
  512. lua_pushnumber(numblocks);
  513. }
  514. static void query_strings (void) {
  515. lua_pushnumber(L->string_root[luaL_check_int(1)].nuse);
  516. }
  517. static void countlist (void) {
  518. char *s = luaL_check_string(1);
  519. GCnode *l = (s[0]=='t') ? L->roottable.next : (s[0]=='c') ? L->rootcl.next :
  520. (s[0]=='p') ? L->rootproto.next : L->rootglobal.next;
  521. int i=0;
  522. while (l) {
  523. i++;
  524. l = l->next;
  525. }
  526. lua_pushnumber(i);
  527. }
  528. static void testC (void) {
  529. #define getnum(s) ((*s++) - '0')
  530. #define getname(s) (nome[0] = *s++, nome)
  531. static int locks[10];
  532. lua_Object reg[10];
  533. char nome[2];
  534. char *s = luaL_check_string(1);
  535. nome[1] = 0;
  536. for (;;) {
  537. switch (*s++) {
  538. case '0': case '1': case '2': case '3': case '4':
  539. case '5': case '6': case '7': case '8': case '9':
  540. lua_pushnumber(*(s-1) - '0');
  541. break;
  542. case 'c': reg[getnum(s)] = lua_createtable(); break;
  543. case 'C': { lua_CFunction f = lua_getcfunction(lua_getglobal(getname(s)));
  544. lua_pushcclosure(f, getnum(s));
  545. break;
  546. }
  547. case 'P': reg[getnum(s)] = lua_pop(); break;
  548. case 'g': { int n=getnum(s); reg[n]=lua_getglobal(getname(s)); break; }
  549. case 'G': { int n = getnum(s);
  550. reg[n] = lua_rawgetglobal(getname(s));
  551. break;
  552. }
  553. case 'l': locks[getnum(s)] = lua_ref(1); break;
  554. case 'L': locks[getnum(s)] = lua_ref(0); break;
  555. case 'r': { int n=getnum(s); reg[n]=lua_getref(locks[getnum(s)]); break; }
  556. case 'u': lua_unref(locks[getnum(s)]); break;
  557. case 'p': { int n = getnum(s); reg[n] = lua_getparam(getnum(s)); break; }
  558. case '=': lua_setglobal(getname(s)); break;
  559. case 's': lua_pushstring(getname(s)); break;
  560. case 'o': lua_pushobject(reg[getnum(s)]); break;
  561. case 'f': lua_call(getname(s)); break;
  562. case 'i': reg[getnum(s)] = lua_gettable(); break;
  563. case 'I': reg[getnum(s)] = lua_rawgettable(); break;
  564. case 't': lua_settable(); break;
  565. case 'T': lua_rawsettable(); break;
  566. default: luaL_verror("unknown command in `testC': %c", *(s-1));
  567. }
  568. if (*s == 0) return;
  569. if (*s++ != ' ') lua_error("missing ` ' between commands in `testC'");
  570. }
  571. }
  572. /* }====================================================== */
  573. #endif
  574. static struct luaL_reg builtin_funcs[] = {
  575. #ifdef LUA_COMPAT2_5
  576. {"setfallback", luaT_setfallback},
  577. #endif
  578. #ifdef DEBUG
  579. {"testC", testC},
  580. {"totalmem", mem_query},
  581. {"count", countlist},
  582. {"querystr", query_strings},
  583. #endif
  584. {"_ALERT", luaB_alert},
  585. {"_ERRORMESSAGE", error_message},
  586. {"call", luaB_call},
  587. {"collectgarbage", luaB_collectgarbage},
  588. {"copytagmethods", luaB_copytagmethods},
  589. {"dofile", luaB_dofile},
  590. {"dostring", luaB_dostring},
  591. {"error", luaB_error},
  592. {"getglobal", luaB_getglobal},
  593. {"gettagmethod", luaB_gettagmethod},
  594. {"newtag", luaB_newtag},
  595. {"next", luaB_next},
  596. {"nextvar", luaB_nextvar},
  597. {"print", luaB_print},
  598. {"rawgetglobal", luaB_rawgetglobal},
  599. {"rawgettable", luaB_rawgettable},
  600. {"rawsetglobal", luaB_rawsetglobal},
  601. {"rawsettable", luaB_rawsettable},
  602. {"seterrormethod", luaB_seterrormethod},
  603. {"setglobal", luaB_setglobal},
  604. {"settag", luaB_settag},
  605. {"settagmethod", luaB_settagmethod},
  606. {"tag", luaB_luatag},
  607. {"tonumber", luaB_tonumber},
  608. {"tostring", luaB_tostring},
  609. {"type", luaB_type},
  610. /* "Extra" functions */
  611. {"assert", luaB_assert},
  612. {"foreach", luaB_foreach},
  613. {"foreachi", luaB_foreachi},
  614. {"foreachvar", luaB_foreachvar},
  615. {"getn", luaB_getn},
  616. {"sort", luaB_sort},
  617. {"tinsert", luaB_tinsert},
  618. {"tremove", luaB_tremove}
  619. };
  620. #define INTFUNCSIZE (sizeof(builtin_funcs)/sizeof(builtin_funcs[0]))
  621. void luaB_predefine (void) {
  622. /* pre-register mem error messages, to avoid loop when error arises */
  623. luaS_newfixedstring(tableEM);
  624. luaS_newfixedstring(memEM);
  625. luaL_openlib(builtin_funcs, (sizeof(builtin_funcs)/sizeof(builtin_funcs[0])));
  626. lua_pushstring(LUA_VERSION);
  627. lua_setglobal("_VERSION");
  628. }