lbuiltin.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. ** $Id: lbuiltin.c,v 1.30 1998/06/19 16:14:09 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. static void pushstring (TaggedString *s)
  24. {
  25. TObject o;
  26. o.ttype = LUA_T_STRING;
  27. o.value.ts = s;
  28. luaA_pushobject(&o);
  29. }
  30. static void nextvar (void)
  31. {
  32. TObject *o = luaA_Address(luaL_nonnullarg(1));
  33. TaggedString *g;
  34. if (ttype(o) == LUA_T_NIL)
  35. g = (TaggedString *)L->rootglobal.next;
  36. else {
  37. luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected");
  38. g = tsvalue(o);
  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;
  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. }
  50. static void foreachvar (void)
  51. {
  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. ttype(L->stack.stack+name) = LUA_T_NIL;
  56. L->stack.top++;
  57. for (g = L->rootglobal.next; g; g = g->next) {
  58. TaggedString *s = (TaggedString *)g;
  59. if (s->u.s.globalval.ttype != LUA_T_NIL) {
  60. ttype(L->stack.stack+name) = LUA_T_STRING;
  61. tsvalue(L->stack.stack+name) = s; /* keep s on stack to avoid GC */
  62. luaA_pushobject(&f);
  63. pushstring(s);
  64. luaA_pushobject(&s->u.s.globalval);
  65. luaD_call((L->stack.top-L->stack.stack)-2, 1);
  66. if (ttype(L->stack.top-1) != LUA_T_NIL)
  67. return;
  68. L->stack.top--;
  69. }
  70. }
  71. }
  72. static void next (void)
  73. {
  74. lua_Object o = luaL_tablearg(1);
  75. lua_Object r = luaL_nonnullarg(2);
  76. Node *n = luaH_next(luaA_Address(o), luaA_Address(r));
  77. if (n) {
  78. luaA_pushobject(&n->ref);
  79. luaA_pushobject(&n->val);
  80. }
  81. }
  82. static void foreach (void)
  83. {
  84. TObject t = *luaA_Address(luaL_tablearg(1));
  85. TObject f = *luaA_Address(luaL_functionarg(2));
  86. int i;
  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. luaA_pushobject(&f);
  91. luaA_pushobject(ref(nd));
  92. luaA_pushobject(val(nd));
  93. luaD_call((L->stack.top-L->stack.stack)-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. char *buff = luaL_openspace(30);
  120. TObject *o = luaA_Address(obj);
  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_call((L->stack.top-L->stack.stack)-1, 1);
  164. if (ttype(L->stack.top-1) != LUA_T_STRING)
  165. lua_error("`tostring' must return a string to `print'");
  166. printf("%s\t", svalue(L->stack.top-1));
  167. L->stack.top--;
  168. }
  169. printf("\n");
  170. }
  171. static void luaI_type (void)
  172. {
  173. lua_Object o = luaL_nonnullarg(1);
  174. lua_pushstring(luaO_typenames[-ttype(luaA_Address(o))]);
  175. lua_pushnumber(lua_tag(o));
  176. }
  177. static void tonumber (void)
  178. {
  179. int base = luaL_opt_number(2, 10);
  180. if (base == 10) { /* standard conversion */
  181. lua_Object o = lua_getparam(1);
  182. if (lua_isnumber(o))
  183. lua_pushnumber(lua_getnumber(o));
  184. }
  185. else {
  186. char *s = luaL_check_string(1);
  187. unsigned long n;
  188. luaL_arg_check(0 <= base && base <= 36, 2, "base out of range");
  189. n = strtol(s, &s, base);
  190. while (isspace(*s)) s++; /* skip trailing spaces */
  191. if (*s) return; /* invalid format: return nil */
  192. lua_pushnumber(n);
  193. }
  194. }
  195. static void luaI_error (void)
  196. {
  197. lua_error(lua_getstring(lua_getparam(1)));
  198. }
  199. static void luaI_assert (void)
  200. {
  201. lua_Object p = lua_getparam(1);
  202. if (p == LUA_NOOBJECT || lua_isnil(p))
  203. luaL_verror("assertion failed! %.100s", luaL_opt_string(2, ""));
  204. }
  205. static void setglobal (void)
  206. {
  207. char *n = luaL_check_string(1);
  208. lua_Object value = luaL_nonnullarg(2);
  209. lua_pushobject(value);
  210. lua_setglobal(n);
  211. lua_pushobject(value); /* return given value */
  212. }
  213. static void rawsetglobal (void)
  214. {
  215. char *n = luaL_check_string(1);
  216. lua_Object value = luaL_nonnullarg(2);
  217. lua_pushobject(value);
  218. lua_rawsetglobal(n);
  219. lua_pushobject(value); /* return given value */
  220. }
  221. static void getglobal (void)
  222. {
  223. lua_pushobject(lua_getglobal(luaL_check_string(1)));
  224. }
  225. static void rawgetglobal (void)
  226. {
  227. lua_pushobject(lua_rawgetglobal(luaL_check_string(1)));
  228. }
  229. static void luatag (void)
  230. {
  231. lua_pushnumber(lua_tag(lua_getparam(1)));
  232. }
  233. static int getnarg (lua_Object table)
  234. {
  235. lua_Object temp;
  236. /* temp = table.n */
  237. lua_pushobject(table); lua_pushstring("n"); temp = lua_rawgettable();
  238. return (lua_isnumber(temp) ? lua_getnumber(temp) : MAX_INT);
  239. }
  240. static void luaI_call (void)
  241. {
  242. lua_Object f = luaL_nonnullarg(1);
  243. lua_Object arg = luaL_tablearg(2);
  244. char *options = luaL_opt_string(3, "");
  245. lua_Object err = lua_getparam(4);
  246. int narg = getnarg(arg);
  247. int i, status;
  248. if (err != LUA_NOOBJECT) { /* set new error method */
  249. lua_pushobject(err);
  250. err = lua_seterrormethod();
  251. }
  252. /* push arg[1...n] */
  253. for (i=0; i<narg; i++) {
  254. lua_Object temp;
  255. /* temp = arg[i+1] */
  256. lua_pushobject(arg); lua_pushnumber(i+1); temp = lua_rawgettable();
  257. if (narg == MAX_INT && lua_isnil(temp))
  258. break;
  259. lua_pushobject(temp);
  260. }
  261. status = lua_callfunction(f);
  262. if (err != LUA_NOOBJECT) { /* restore old error method */
  263. lua_pushobject(err);
  264. lua_seterrormethod();
  265. }
  266. if (status != 0) { /* error in call? */
  267. if (strchr(options, 'x'))
  268. return; /* return nil to signal the error */
  269. else
  270. lua_error(NULL);
  271. }
  272. else { /* no errors */
  273. if (strchr(options, 'p'))
  274. luaA_packresults();
  275. else
  276. luaA_passresults();
  277. }
  278. }
  279. static void settag (void)
  280. {
  281. lua_Object o = luaL_tablearg(1);
  282. lua_pushobject(o);
  283. lua_settag(luaL_check_number(2));
  284. lua_pushobject(o); /* returns first argument */
  285. }
  286. static void newtag (void)
  287. {
  288. lua_pushnumber(lua_newtag());
  289. }
  290. static void copytagmethods (void)
  291. {
  292. lua_pushnumber(lua_copytagmethods(luaL_check_number(1),
  293. luaL_check_number(2)));
  294. }
  295. static void rawgettable (void)
  296. {
  297. lua_pushobject(luaL_nonnullarg(1));
  298. lua_pushobject(luaL_nonnullarg(2));
  299. lua_pushobject(lua_rawgettable());
  300. }
  301. static void rawsettable (void)
  302. {
  303. lua_pushobject(luaL_nonnullarg(1));
  304. lua_pushobject(luaL_nonnullarg(2));
  305. lua_pushobject(luaL_nonnullarg(3));
  306. lua_rawsettable();
  307. }
  308. static void settagmethod (void)
  309. {
  310. lua_Object nf = luaL_nonnullarg(3);
  311. lua_pushobject(nf);
  312. lua_pushobject(lua_settagmethod((int)luaL_check_number(1),
  313. luaL_check_string(2)));
  314. }
  315. static void gettagmethod (void)
  316. {
  317. lua_pushobject(lua_gettagmethod((int)luaL_check_number(1),
  318. luaL_check_string(2)));
  319. }
  320. static void seterrormethod (void)
  321. {
  322. lua_Object nf = luaL_functionarg(1);
  323. lua_pushobject(nf);
  324. lua_pushobject(lua_seterrormethod());
  325. }
  326. static void luaI_collectgarbage (void)
  327. {
  328. lua_pushnumber(lua_collectgarbage(luaL_opt_number(1, 0)));
  329. }
  330. /*
  331. ** =======================================================
  332. ** some DEBUG functions
  333. ** =======================================================
  334. */
  335. #ifdef DEBUG
  336. static void mem_query (void)
  337. {
  338. lua_pushnumber(totalmem);
  339. lua_pushnumber(numblocks);
  340. }
  341. static void countlist (void)
  342. {
  343. char *s = luaL_check_string(1);
  344. GCnode *l = (s[0]=='t') ? L->roottable.next : (s[0]=='c') ? L->rootcl.next :
  345. (s[0]=='p') ? L->rootproto.next : L->rootglobal.next;
  346. int i=0;
  347. while (l) {
  348. i++;
  349. l = l->next;
  350. }
  351. lua_pushnumber(i);
  352. }
  353. static void testC (void)
  354. {
  355. #define getnum(s) ((*s++) - '0')
  356. #define getname(s) (nome[0] = *s++, nome)
  357. static int locks[10];
  358. lua_Object reg[10];
  359. char nome[2];
  360. char *s = luaL_check_string(1);
  361. nome[1] = 0;
  362. while (1) {
  363. switch (*s++) {
  364. case '0': case '1': case '2': case '3': case '4':
  365. case '5': case '6': case '7': case '8': case '9':
  366. lua_pushnumber(*(s-1) - '0');
  367. break;
  368. case 'c': reg[getnum(s)] = lua_createtable(); break;
  369. case 'C': { lua_CFunction f = lua_getcfunction(lua_getglobal(getname(s)));
  370. lua_pushcclosure(f, getnum(s));
  371. break;
  372. }
  373. case 'P': reg[getnum(s)] = lua_pop(); break;
  374. case 'g': { int n=getnum(s); reg[n]=lua_getglobal(getname(s)); break; }
  375. case 'G': { int n = getnum(s);
  376. reg[n] = lua_rawgetglobal(getname(s));
  377. break;
  378. }
  379. case 'l': locks[getnum(s)] = lua_ref(1); break;
  380. case 'L': locks[getnum(s)] = lua_ref(0); break;
  381. case 'r': { int n=getnum(s); reg[n]=lua_getref(locks[getnum(s)]); break; }
  382. case 'u': lua_unref(locks[getnum(s)]); break;
  383. case 'p': { int n = getnum(s); reg[n] = lua_getparam(getnum(s)); break; }
  384. case '=': lua_setglobal(getname(s)); break;
  385. case 's': lua_pushstring(getname(s)); break;
  386. case 'o': lua_pushobject(reg[getnum(s)]); break;
  387. case 'f': (lua_call)(getname(s)); break;
  388. case 'i': reg[getnum(s)] = lua_gettable(); break;
  389. case 'I': reg[getnum(s)] = lua_rawgettable(); break;
  390. case 't': lua_settable(); break;
  391. case 'T': lua_rawsettable(); break;
  392. default: luaL_verror("unknown command in `testC': %c", *(s-1));
  393. }
  394. if (*s == 0) return;
  395. if (*s++ != ' ') lua_error("missing ` ' between commands in `testC'");
  396. }
  397. }
  398. #endif
  399. /*
  400. ** Internal functions
  401. */
  402. static struct luaL_reg int_funcs[] = {
  403. #ifdef LUA_COMPAT2_5
  404. {"setfallback", luaT_setfallback},
  405. #endif
  406. #ifdef DEBUG
  407. {"testC", testC},
  408. {"totalmem", mem_query},
  409. {"count", countlist},
  410. #endif
  411. {"assert", luaI_assert},
  412. {"call", luaI_call},
  413. {"collectgarbage", luaI_collectgarbage},
  414. {"dofile", internaldofile},
  415. {"copytagmethods", copytagmethods},
  416. {"dostring", internaldostring},
  417. {"error", luaI_error},
  418. {"foreach", foreach},
  419. {"foreachvar", foreachvar},
  420. {"getglobal", getglobal},
  421. {"newtag", newtag},
  422. {"next", next},
  423. {"nextvar", nextvar},
  424. {"print", luaI_print},
  425. {"rawgetglobal", rawgetglobal},
  426. {"rawgettable", rawgettable},
  427. {"rawsetglobal", rawsetglobal},
  428. {"rawsettable", rawsettable},
  429. {"seterrormethod", seterrormethod},
  430. {"setglobal", setglobal},
  431. {"settagmethod", settagmethod},
  432. {"gettagmethod", gettagmethod},
  433. {"settag", settag},
  434. {"tonumber", tonumber},
  435. {"tostring", to_string},
  436. {"tag", luatag},
  437. {"type", luaI_type}
  438. };
  439. #define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0]))
  440. void luaB_predefine (void)
  441. {
  442. /* pre-register mem error messages, to avoid loop when error arises */
  443. luaS_newfixedstring(tableEM);
  444. luaS_newfixedstring(memEM);
  445. luaL_openlib(int_funcs, (sizeof(int_funcs)/sizeof(int_funcs[0])));
  446. lua_pushstring(LUA_VERSION);
  447. lua_setglobal("_VERSION");
  448. }