lbuiltin.c 20 KB

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