lbuiltin.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. ** $Id: lbuiltin.c,v 1.33 1998/07/12 16:16:43 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. static void pushstring (TaggedString *s)
  25. {
  26. TObject o;
  27. o.ttype = LUA_T_STRING;
  28. o.value.ts = s;
  29. luaA_pushobject(&o);
  30. }
  31. static void nextvar (void) {
  32. TObject *o = luaA_Address(luaL_nonnullarg(1));
  33. TaggedString *g;
  34. if (ttype(o) == LUA_T_NIL)
  35. g = (TaggedString *)L->rootglobal.next; /* first variable */
  36. else {
  37. luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected");
  38. g = tsvalue(o); /* find given variable name */
  39. /* check whether name is in global var list */
  40. luaL_arg_check((GCnode *)g != g->head.next, 1, "variable name expected");
  41. g = (TaggedString *)g->head.next; /* get next */
  42. }
  43. while (g && g->u.s.globalval.ttype == LUA_T_NIL) /* skip globals with nil */
  44. g = (TaggedString *)g->head.next;
  45. if (g) {
  46. pushstring(g);
  47. luaA_pushobject(&g->u.s.globalval);
  48. }
  49. else lua_pushnil(); /* no more globals */
  50. }
  51. static void foreachvar (void) {
  52. TObject f = *luaA_Address(luaL_functionarg(1));
  53. GCnode *g;
  54. StkId name = L->Cstack.base++; /* place to keep var name (to avoid GC) */
  55. luaD_checkstack(4); /* for var name, f, s, and globalvar */
  56. ttype(L->stack.stack+name) = LUA_T_NIL;
  57. L->stack.top++; /* top == base */
  58. for (g = L->rootglobal.next; g; g = g->next) {
  59. TaggedString *s = (TaggedString *)g;
  60. if (s->u.s.globalval.ttype != LUA_T_NIL) {
  61. ttype(L->stack.stack+name) = LUA_T_STRING;
  62. tsvalue(L->stack.stack+name) = s; /* keep s on stack to avoid GC */
  63. *(L->stack.top++) = f;
  64. pushstring(s);
  65. *(L->stack.top++) = s->u.s.globalval;
  66. luaD_calln(2, 1);
  67. if (ttype(L->stack.top-1) != LUA_T_NIL)
  68. return;
  69. L->stack.top--;
  70. }
  71. }
  72. }
  73. static void next (void) {
  74. Node *n = luaH_next(luaA_Address(luaL_tablearg(1)),
  75. luaA_Address(luaL_nonnullarg(2)));
  76. if (n) {
  77. luaA_pushobject(&n->ref);
  78. luaA_pushobject(&n->val);
  79. }
  80. else lua_pushnil();
  81. }
  82. static void foreach (void) {
  83. TObject t = *luaA_Address(luaL_tablearg(1));
  84. TObject f = *luaA_Address(luaL_functionarg(2));
  85. int i;
  86. luaD_checkstack(3); /* for f, ref, and val */
  87. for (i=0; i<avalue(&t)->nhash; i++) {
  88. Node *nd = &(avalue(&t)->node[i]);
  89. if (ttype(ref(nd)) != LUA_T_NIL && ttype(val(nd)) != LUA_T_NIL) {
  90. *(L->stack.top++) = f;
  91. *(L->stack.top++) = *ref(nd);
  92. *(L->stack.top++) = *val(nd);
  93. luaD_calln(2, 1);
  94. if (ttype(L->stack.top-1) != LUA_T_NIL)
  95. return;
  96. L->stack.top--;
  97. }
  98. }
  99. }
  100. static void internaldostring (void)
  101. {
  102. long l;
  103. char *s = luaL_check_lstr(1, &l);
  104. if (*s == ID_CHUNK)
  105. lua_error("`dostring' cannot run pre-compiled code");
  106. if (lua_dobuffer(s, l, luaL_opt_string(2, NULL)) == 0)
  107. if (luaA_passresults() == 0)
  108. lua_pushuserdata(NULL); /* at least one result to signal no errors */
  109. }
  110. static void internaldofile (void)
  111. {
  112. char *fname = luaL_opt_string(1, NULL);
  113. if (lua_dofile(fname) == 0)
  114. if (luaA_passresults() == 0)
  115. lua_pushuserdata(NULL); /* at least one result to signal no errors */
  116. }
  117. static void to_string (void) {
  118. lua_Object obj = lua_getparam(1);
  119. TObject *o = luaA_Address(obj);
  120. char buff[32];
  121. switch (ttype(o)) {
  122. case LUA_T_NUMBER:
  123. lua_pushstring(lua_getstring(obj));
  124. return;
  125. case LUA_T_STRING:
  126. lua_pushobject(obj);
  127. return;
  128. case LUA_T_ARRAY: {
  129. sprintf(buff, "table: %p", (void *)o->value.a);
  130. break;
  131. }
  132. case LUA_T_CLOSURE: {
  133. sprintf(buff, "function: %p", (void *)o->value.cl);
  134. break;
  135. }
  136. case LUA_T_PROTO: {
  137. sprintf(buff, "function: %p", (void *)o->value.tf);
  138. break;
  139. }
  140. case LUA_T_CPROTO: {
  141. sprintf(buff, "function: %p", (void *)o->value.f);
  142. break;
  143. }
  144. case LUA_T_USERDATA: {
  145. sprintf(buff, "userdata: %p", o->value.ts->u.d.v);
  146. break;
  147. }
  148. case LUA_T_NIL:
  149. lua_pushstring("nil");
  150. return;
  151. default:
  152. LUA_INTERNALERROR("invalid type");
  153. }
  154. lua_pushstring(buff);
  155. }
  156. static void luaI_print (void) {
  157. TaggedString *ts = luaS_new("tostring");
  158. lua_Object obj;
  159. int i = 1;
  160. while ((obj = lua_getparam(i++)) != LUA_NOOBJECT) {
  161. luaA_pushobject(&ts->u.s.globalval);
  162. lua_pushobject(obj);
  163. luaD_calln(1, 1);
  164. if (ttype(L->stack.top-1) != LUA_T_STRING)
  165. lua_error("`tostring' must return a string to `print'");
  166. printf("%.200s\t", svalue(L->stack.top-1));
  167. L->stack.top--;
  168. }
  169. printf("\n");
  170. }
  171. static void luaB_message (void) {
  172. fputs(luaL_check_string(1), stderr);
  173. }
  174. static void error_message (void) {
  175. char buff[200];
  176. sprintf(buff, "lua error: %.180s\n", luaL_check_string(1));
  177. lua_pushstring(buff);
  178. lua_call("_ALERT");
  179. }
  180. static void luaI_type (void)
  181. {
  182. lua_Object o = luaL_nonnullarg(1);
  183. lua_pushstring(luaO_typename(luaA_Address(o)));
  184. lua_pushnumber(lua_tag(o));
  185. }
  186. static void luaB_tonumber (void)
  187. {
  188. int base = luaL_opt_number(2, 10);
  189. if (base == 10) { /* standard conversion */
  190. lua_Object o = lua_getparam(1);
  191. if (lua_isnumber(o))
  192. lua_pushnumber(lua_getnumber(o));
  193. }
  194. else {
  195. char *s = luaL_check_string(1);
  196. unsigned long n;
  197. luaL_arg_check(0 <= base && base <= 36, 2, "base out of range");
  198. n = strtol(s, &s, base);
  199. while (isspace(*s)) s++; /* skip trailing spaces */
  200. if (*s) lua_pushnil(); /* invalid format: return nil */
  201. else lua_pushnumber(n);
  202. }
  203. }
  204. static void luaI_error (void)
  205. {
  206. lua_error(lua_getstring(lua_getparam(1)));
  207. }
  208. static void luaI_assert (void)
  209. {
  210. lua_Object p = lua_getparam(1);
  211. if (p == LUA_NOOBJECT || lua_isnil(p))
  212. luaL_verror("assertion failed! %.100s", luaL_opt_string(2, ""));
  213. }
  214. static void setglobal (void)
  215. {
  216. char *n = luaL_check_string(1);
  217. lua_Object value = luaL_nonnullarg(2);
  218. lua_pushobject(value);
  219. lua_setglobal(n);
  220. lua_pushobject(value); /* return given value */
  221. }
  222. static void rawsetglobal (void)
  223. {
  224. char *n = luaL_check_string(1);
  225. lua_Object value = luaL_nonnullarg(2);
  226. lua_pushobject(value);
  227. lua_rawsetglobal(n);
  228. lua_pushobject(value); /* return given value */
  229. }
  230. static void getglobal (void)
  231. {
  232. lua_pushobject(lua_getglobal(luaL_check_string(1)));
  233. }
  234. static void rawgetglobal (void)
  235. {
  236. lua_pushobject(lua_rawgetglobal(luaL_check_string(1)));
  237. }
  238. static void luatag (void)
  239. {
  240. lua_pushnumber(lua_tag(lua_getparam(1)));
  241. }
  242. static int getsize (TObject *t) {
  243. int max = 0;
  244. int i;
  245. Hash *h = avalue(t);
  246. LUA_ASSERT(ttype(t) == LUA_T_ARRAY, "table expected");
  247. for (i = 0; i<nhash(h); i++) {
  248. Node *n = h->node+i;
  249. if (ttype(ref(n)) == LUA_T_NUMBER && ttype(val(n)) != LUA_T_NIL &&
  250. (int)nvalue(ref(n)) > max)
  251. max = nvalue(ref(n));
  252. }
  253. return max;
  254. }
  255. static int getnarg (lua_Object table) {
  256. lua_Object temp;
  257. /* temp = table.n */
  258. lua_pushobject(table); lua_pushstring("n"); temp = lua_rawgettable();
  259. return (lua_isnumber(temp) ? lua_getnumber(temp) :
  260. getsize(luaA_Address(table)));
  261. }
  262. static void luaI_call (void) {
  263. lua_Object f = luaL_nonnullarg(1);
  264. lua_Object arg = luaL_tablearg(2);
  265. char *options = luaL_opt_string(3, "");
  266. lua_Object err = lua_getparam(4);
  267. int narg = getnarg(arg);
  268. int i, status;
  269. if (err != LUA_NOOBJECT) { /* set new error method */
  270. lua_pushobject(err);
  271. err = lua_seterrormethod();
  272. }
  273. /* push arg[1...n] */
  274. luaD_checkstack(narg);
  275. for (i=0; i<narg; i++)
  276. *(L->stack.top++) = *luaH_getint(avalue(luaA_Address(arg)), i+1);
  277. status = lua_callfunction(f);
  278. if (err != LUA_NOOBJECT) { /* restore old error method */
  279. lua_pushobject(err);
  280. lua_seterrormethod();
  281. }
  282. if (status != 0) { /* error in call? */
  283. if (strchr(options, 'x')) {
  284. lua_pushnil();
  285. return; /* return nil to signal the error */
  286. }
  287. else
  288. lua_error(NULL);
  289. }
  290. else { /* no errors */
  291. if (strchr(options, 'p'))
  292. luaA_packresults();
  293. else
  294. luaA_passresults();
  295. }
  296. }
  297. static void settag (void)
  298. {
  299. lua_Object o = luaL_tablearg(1);
  300. lua_pushobject(o);
  301. lua_settag(luaL_check_number(2));
  302. lua_pushobject(o); /* returns first argument */
  303. }
  304. static void newtag (void)
  305. {
  306. lua_pushnumber(lua_newtag());
  307. }
  308. static void copytagmethods (void)
  309. {
  310. lua_pushnumber(lua_copytagmethods(luaL_check_number(1),
  311. luaL_check_number(2)));
  312. }
  313. static void rawgettable (void)
  314. {
  315. lua_pushobject(luaL_nonnullarg(1));
  316. lua_pushobject(luaL_nonnullarg(2));
  317. lua_pushobject(lua_rawgettable());
  318. }
  319. static void rawsettable (void)
  320. {
  321. lua_pushobject(luaL_nonnullarg(1));
  322. lua_pushobject(luaL_nonnullarg(2));
  323. lua_pushobject(luaL_nonnullarg(3));
  324. lua_rawsettable();
  325. }
  326. static void settagmethod (void)
  327. {
  328. lua_Object nf = luaL_nonnullarg(3);
  329. lua_pushobject(nf);
  330. lua_pushobject(lua_settagmethod((int)luaL_check_number(1),
  331. luaL_check_string(2)));
  332. }
  333. static void gettagmethod (void)
  334. {
  335. lua_pushobject(lua_gettagmethod((int)luaL_check_number(1),
  336. luaL_check_string(2)));
  337. }
  338. static void seterrormethod (void)
  339. {
  340. lua_Object nf = luaL_functionarg(1);
  341. lua_pushobject(nf);
  342. lua_pushobject(lua_seterrormethod());
  343. }
  344. static void luaI_collectgarbage (void)
  345. {
  346. lua_pushnumber(lua_collectgarbage(luaL_opt_number(1, 0)));
  347. }
  348. static void swap (Hash *a, int i, int j) {
  349. /* notice: must use two temporary vars, because luaH_setint may cause a
  350. rehash and change the addresses of values in the array */
  351. TObject ai = *luaH_getint(a, i);
  352. TObject aj = *luaH_getint(a, j);
  353. luaH_setint(a, i, &aj);
  354. luaH_setint(a, j, &ai);
  355. }
  356. static int sort_comp (TObject *f, TObject *a, TObject *b) {
  357. /* notice: the caller (auxsort) must check stack space */
  358. if (f) {
  359. *(L->stack.top++) = *f;
  360. *(L->stack.top++) = *a;
  361. *(L->stack.top++) = *b;
  362. luaD_calln(2, 1);
  363. }
  364. else { /* a < b? */
  365. *(L->stack.top++) = *a;
  366. *(L->stack.top++) = *b;
  367. luaV_comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, IM_LT);
  368. }
  369. return ttype(--(L->stack.top)) != LUA_T_NIL;
  370. }
  371. /*
  372. ** quicksort algorithm from "Programming Pearls", pg. 112
  373. */
  374. static void auxsort (Hash *a, int l, int u, TObject *f) {
  375. if (u <= l) return; /* 0 or 1 element */
  376. luaD_checkstack(4); /* for pivot, f, a, b (sort_comp) */
  377. if (u-l == 1) { /* only two elements? */
  378. if (sort_comp(f, luaH_getint(a, u), luaH_getint(a, l))) /* a[u] < a[l]? */
  379. swap(a, l, u);
  380. }
  381. else {
  382. int i;
  383. int m = l;
  384. swap(a, l, (l+u)/2); /* put middle element as pivot (a[l]) */
  385. *(L->stack.top++) = *luaH_getint(a, l); /* save pivot on stack (for GC) */
  386. for (i=l+1; i<=u; i++) {
  387. /* invariant: a[l+1..m] < P <= a[m+1..i-1] */
  388. if (sort_comp(f, luaH_getint(a, i), L->stack.top-1)) { /* a[i] < P? */
  389. m++;
  390. swap(a, m, i);
  391. }
  392. }
  393. L->stack.top--; /* remove pivot from stack */
  394. swap(a, l, m);
  395. /* a[l..m-1] < a[m] <= a[m+1..u] */
  396. auxsort(a, l, m-1, f);
  397. auxsort(a, m+1, u, f);
  398. }
  399. }
  400. static void luaB_sort (void) {
  401. lua_Object t = luaL_tablearg(1);
  402. int n = getnarg(t);
  403. Hash *a = avalue(luaA_Address(t));
  404. lua_Object func = lua_getparam(2);
  405. TObject *f;
  406. if (func == LUA_NOOBJECT)
  407. f = NULL;
  408. else {
  409. luaL_arg_check(lua_isfunction(func), 2, "function expected");
  410. f = luaA_Address(func);
  411. }
  412. auxsort(a, 1, n, f);
  413. lua_pushobject(t);
  414. }
  415. /*
  416. ** =======================================================
  417. ** some DEBUG functions
  418. ** =======================================================
  419. */
  420. #ifdef DEBUG
  421. static void mem_query (void)
  422. {
  423. lua_pushnumber(totalmem);
  424. lua_pushnumber(numblocks);
  425. }
  426. static void countlist (void)
  427. {
  428. char *s = luaL_check_string(1);
  429. GCnode *l = (s[0]=='t') ? L->roottable.next : (s[0]=='c') ? L->rootcl.next :
  430. (s[0]=='p') ? L->rootproto.next : L->rootglobal.next;
  431. int i=0;
  432. while (l) {
  433. i++;
  434. l = l->next;
  435. }
  436. lua_pushnumber(i);
  437. }
  438. static void testC (void)
  439. {
  440. #define getnum(s) ((*s++) - '0')
  441. #define getname(s) (nome[0] = *s++, nome)
  442. static int locks[10];
  443. lua_Object reg[10];
  444. char nome[2];
  445. char *s = luaL_check_string(1);
  446. nome[1] = 0;
  447. while (1) {
  448. switch (*s++) {
  449. case '0': case '1': case '2': case '3': case '4':
  450. case '5': case '6': case '7': case '8': case '9':
  451. lua_pushnumber(*(s-1) - '0');
  452. break;
  453. case 'c': reg[getnum(s)] = lua_createtable(); break;
  454. case 'C': { lua_CFunction f = lua_getcfunction(lua_getglobal(getname(s)));
  455. lua_pushcclosure(f, getnum(s));
  456. break;
  457. }
  458. case 'P': reg[getnum(s)] = lua_pop(); break;
  459. case 'g': { int n=getnum(s); reg[n]=lua_getglobal(getname(s)); break; }
  460. case 'G': { int n = getnum(s);
  461. reg[n] = lua_rawgetglobal(getname(s));
  462. break;
  463. }
  464. case 'l': locks[getnum(s)] = lua_ref(1); break;
  465. case 'L': locks[getnum(s)] = lua_ref(0); break;
  466. case 'r': { int n=getnum(s); reg[n]=lua_getref(locks[getnum(s)]); break; }
  467. case 'u': lua_unref(locks[getnum(s)]); break;
  468. case 'p': { int n = getnum(s); reg[n] = lua_getparam(getnum(s)); break; }
  469. case '=': lua_setglobal(getname(s)); break;
  470. case 's': lua_pushstring(getname(s)); break;
  471. case 'o': lua_pushobject(reg[getnum(s)]); break;
  472. case 'f': (lua_call)(getname(s)); break;
  473. case 'i': reg[getnum(s)] = lua_gettable(); break;
  474. case 'I': reg[getnum(s)] = lua_rawgettable(); break;
  475. case 't': lua_settable(); break;
  476. case 'T': lua_rawsettable(); break;
  477. default: luaL_verror("unknown command in `testC': %c", *(s-1));
  478. }
  479. if (*s == 0) return;
  480. if (*s++ != ' ') lua_error("missing ` ' between commands in `testC'");
  481. }
  482. }
  483. #endif
  484. /*
  485. ** Internal functions
  486. */
  487. static struct luaL_reg int_funcs[] = {
  488. #ifdef LUA_COMPAT2_5
  489. {"setfallback", luaT_setfallback},
  490. #endif
  491. #ifdef DEBUG
  492. {"testC", testC},
  493. {"totalmem", mem_query},
  494. {"count", countlist},
  495. #endif
  496. {"assert", luaI_assert},
  497. {"call", luaI_call},
  498. {"collectgarbage", luaI_collectgarbage},
  499. {"dofile", internaldofile},
  500. {"copytagmethods", copytagmethods},
  501. {"dostring", internaldostring},
  502. {"error", luaI_error},
  503. {"_ERRORMESSAGE", error_message},
  504. {"foreach", foreach},
  505. {"foreachvar", foreachvar},
  506. {"getglobal", getglobal},
  507. {"newtag", newtag},
  508. {"next", next},
  509. {"nextvar", nextvar},
  510. {"print", luaI_print},
  511. {"rawgetglobal", rawgetglobal},
  512. {"rawgettable", rawgettable},
  513. {"rawsetglobal", rawsetglobal},
  514. {"rawsettable", rawsettable},
  515. {"seterrormethod", seterrormethod},
  516. {"setglobal", setglobal},
  517. {"settagmethod", settagmethod},
  518. {"gettagmethod", gettagmethod},
  519. {"settag", settag},
  520. {"sort", luaB_sort},
  521. {"tonumber", luaB_tonumber},
  522. {"tostring", to_string},
  523. {"tag", luatag},
  524. {"type", luaI_type},
  525. {"_ALERT", luaB_message}
  526. };
  527. #define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0]))
  528. void luaB_predefine (void)
  529. {
  530. /* pre-register mem error messages, to avoid loop when error arises */
  531. luaS_newfixedstring(tableEM);
  532. luaS_newfixedstring(memEM);
  533. luaL_openlib(int_funcs, (sizeof(int_funcs)/sizeof(int_funcs[0])));
  534. lua_pushstring(LUA_VERSION);
  535. lua_setglobal("_VERSION");
  536. }