lbuiltin.c 22 KB

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