lbuiltin.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. ** $Id: lbuiltin.c,v 1.27 1998/03/09 21:49:52 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. static void pushstring (TaggedString *s)
  23. {
  24. TObject o;
  25. o.ttype = LUA_T_STRING;
  26. o.value.ts = s;
  27. luaA_pushobject(&o);
  28. }
  29. static void nextvar (void)
  30. {
  31. TObject *o = luaA_Address(luaL_nonnullarg(1));
  32. TaggedString *g;
  33. if (ttype(o) == LUA_T_NIL)
  34. g = (TaggedString *)L->rootglobal.next;
  35. else {
  36. luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected");
  37. g = tsvalue(o);
  38. /* check whether name is in global var list */
  39. luaL_arg_check((GCnode *)g != g->head.next, 1, "variable name expected");
  40. g = (TaggedString *)g->head.next;
  41. }
  42. while (g && g->u.s.globalval.ttype == LUA_T_NIL) /* skip globals with nil */
  43. g = (TaggedString *)g->head.next;
  44. if (g) {
  45. pushstring(g);
  46. luaA_pushobject(&g->u.s.globalval);
  47. }
  48. }
  49. static void foreachvar (void)
  50. {
  51. TObject f = *luaA_Address(luaL_functionarg(1));
  52. GCnode *g;
  53. StkId name = L->Cstack.base++; /* place to keep var name (to avoid GC) */
  54. ttype(L->stack.stack+name) = LUA_T_NIL;
  55. L->stack.top++;
  56. for (g = L->rootglobal.next; g; g = g->next) {
  57. TaggedString *s = (TaggedString *)g;
  58. if (s->u.s.globalval.ttype != LUA_T_NIL) {
  59. ttype(L->stack.stack+name) = LUA_T_STRING;
  60. tsvalue(L->stack.stack+name) = s; /* keep s on stack to avoid GC */
  61. luaA_pushobject(&f);
  62. pushstring(s);
  63. luaA_pushobject(&s->u.s.globalval);
  64. luaD_call((L->stack.top-L->stack.stack)-2, 1);
  65. if (ttype(L->stack.top-1) != LUA_T_NIL)
  66. return;
  67. L->stack.top--;
  68. }
  69. }
  70. }
  71. static void next (void)
  72. {
  73. lua_Object o = luaL_tablearg(1);
  74. lua_Object r = luaL_nonnullarg(2);
  75. Node *n = luaH_next(luaA_Address(o), luaA_Address(r));
  76. if (n) {
  77. luaA_pushobject(&n->ref);
  78. luaA_pushobject(&n->val);
  79. }
  80. }
  81. static void foreach (void)
  82. {
  83. TObject t = *luaA_Address(luaL_tablearg(1));
  84. TObject f = *luaA_Address(luaL_functionarg(2));
  85. int i;
  86. for (i=0; i<avalue(&t)->nhash; i++) {
  87. Node *nd = &(avalue(&t)->node[i]);
  88. if (ttype(ref(nd)) != LUA_T_NIL && ttype(val(nd)) != LUA_T_NIL) {
  89. luaA_pushobject(&f);
  90. luaA_pushobject(ref(nd));
  91. luaA_pushobject(val(nd));
  92. luaD_call((L->stack.top-L->stack.stack)-2, 1);
  93. if (ttype(L->stack.top-1) != LUA_T_NIL)
  94. return;
  95. L->stack.top--;
  96. }
  97. }
  98. }
  99. static void internaldostring (void)
  100. {
  101. if (lua_getparam(2) != LUA_NOOBJECT)
  102. lua_error("invalid 2nd argument (probably obsolete code)");
  103. if (lua_dostring(luaL_check_string(1)) == 0)
  104. if (luaA_passresults() == 0)
  105. lua_pushuserdata(NULL); /* at least one result to signal no errors */
  106. }
  107. static void internaldofile (void)
  108. {
  109. char *fname = luaL_opt_string(1, NULL);
  110. if (lua_dofile(fname) == 0)
  111. if (luaA_passresults() == 0)
  112. lua_pushuserdata(NULL); /* at least one result to signal no errors */
  113. }
  114. static void to_string (void) {
  115. lua_Object obj = lua_getparam(1);
  116. char *buff = luaL_openspace(30);
  117. TObject *o = luaA_Address(obj);
  118. switch (ttype(o)) {
  119. case LUA_T_NUMBER:
  120. lua_pushstring(lua_getstring(obj));
  121. return;
  122. case LUA_T_STRING:
  123. lua_pushobject(obj);
  124. return;
  125. case LUA_T_ARRAY: {
  126. sprintf(buff, "table: %p", (void *)o->value.a);
  127. break;
  128. }
  129. case LUA_T_CLOSURE: {
  130. sprintf(buff, "function: %p", (void *)o->value.cl);
  131. break;
  132. }
  133. case LUA_T_PROTO: {
  134. sprintf(buff, "function: %p", (void *)o->value.tf);
  135. break;
  136. }
  137. case LUA_T_CPROTO: {
  138. sprintf(buff, "function: %p", (void *)o->value.f);
  139. break;
  140. }
  141. case LUA_T_USERDATA: {
  142. sprintf(buff, "userdata: %p", o->value.ts->u.d.v);
  143. break;
  144. }
  145. case LUA_T_NIL:
  146. lua_pushstring("nil");
  147. return;
  148. default:
  149. LUA_INTERNALERROR("invalid type");
  150. }
  151. lua_pushstring(buff);
  152. }
  153. static void luaI_print (void) {
  154. TaggedString *ts = luaS_new("tostring");
  155. lua_Object obj;
  156. int i = 1;
  157. while ((obj = lua_getparam(i++)) != LUA_NOOBJECT) {
  158. luaA_pushobject(&ts->u.s.globalval);
  159. lua_pushobject(obj);
  160. luaD_call((L->stack.top-L->stack.stack)-1, 1);
  161. if (ttype(L->stack.top-1) != LUA_T_STRING)
  162. lua_error("`tostring' must return a string to `print'");
  163. printf("%s\t", svalue(L->stack.top-1));
  164. L->stack.top--;
  165. }
  166. printf("\n");
  167. }
  168. static void luaI_type (void)
  169. {
  170. lua_Object o = luaL_nonnullarg(1);
  171. lua_pushstring(luaO_typenames[-ttype(luaA_Address(o))]);
  172. lua_pushnumber(lua_tag(o));
  173. }
  174. static void tonumber (void)
  175. {
  176. int base = luaL_opt_number(2, 10);
  177. if (base == 10) { /* standard convertion */
  178. lua_Object o = lua_getparam(1);
  179. if (lua_isnumber(o))
  180. lua_pushnumber(lua_getnumber(o));
  181. }
  182. else {
  183. char *s = luaL_check_string(1);
  184. unsigned long n;
  185. luaL_arg_check(0 <= base && base <= 36, 2, "base out of range");
  186. n = strtol(s, &s, base);
  187. while (isspace(*s)) s++; /* skip trailing spaces */
  188. if (*s) return; /* invalid format: return nil */
  189. lua_pushnumber(n);
  190. }
  191. }
  192. static void luaI_error (void)
  193. {
  194. lua_error(lua_getstring(lua_getparam(1)));
  195. }
  196. static void luaI_assert (void)
  197. {
  198. lua_Object p = lua_getparam(1);
  199. if (p == LUA_NOOBJECT || lua_isnil(p))
  200. luaL_verror("assertion failed! %.100s", luaL_opt_string(2, ""));
  201. }
  202. static void setglobal (void)
  203. {
  204. char *n = luaL_check_string(1);
  205. lua_Object value = luaL_nonnullarg(2);
  206. lua_pushobject(value);
  207. lua_setglobal(n);
  208. lua_pushobject(value); /* return given value */
  209. }
  210. static void rawsetglobal (void)
  211. {
  212. char *n = luaL_check_string(1);
  213. lua_Object value = luaL_nonnullarg(2);
  214. lua_pushobject(value);
  215. lua_rawsetglobal(n);
  216. lua_pushobject(value); /* return given value */
  217. }
  218. static void getglobal (void)
  219. {
  220. lua_pushobject(lua_getglobal(luaL_check_string(1)));
  221. }
  222. static void rawgetglobal (void)
  223. {
  224. lua_pushobject(lua_rawgetglobal(luaL_check_string(1)));
  225. }
  226. static void luatag (void)
  227. {
  228. lua_pushnumber(lua_tag(lua_getparam(1)));
  229. }
  230. static int getnarg (lua_Object table)
  231. {
  232. lua_Object temp;
  233. /* temp = table.n */
  234. lua_pushobject(table); lua_pushstring("n"); temp = lua_rawgettable();
  235. return (lua_isnumber(temp) ? lua_getnumber(temp) : MAX_INT);
  236. }
  237. static void luaI_call (void)
  238. {
  239. lua_Object f = luaL_nonnullarg(1);
  240. lua_Object arg = luaL_tablearg(2);
  241. char *options = luaL_opt_string(3, "");
  242. lua_Object err = lua_getparam(4);
  243. int narg = 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. for (i=0; i<narg; i++) {
  251. lua_Object temp;
  252. /* temp = arg[i+1] */
  253. lua_pushobject(arg); lua_pushnumber(i+1); temp = lua_rawgettable();
  254. if (narg == MAX_INT && lua_isnil(temp))
  255. break;
  256. lua_pushobject(temp);
  257. }
  258. status = lua_callfunction(f);
  259. if (err != LUA_NOOBJECT) { /* restore old error method */
  260. lua_pushobject(err);
  261. lua_seterrormethod();
  262. }
  263. if (status != 0) { /* error in call? */
  264. if (strchr(options, 'x'))
  265. return; /* return nil to signal the error */
  266. else
  267. lua_error(NULL);
  268. }
  269. else { /* no errors */
  270. if (strchr(options, 'p'))
  271. luaA_packresults();
  272. else
  273. luaA_passresults();
  274. }
  275. }
  276. static void settag (void)
  277. {
  278. lua_Object o = luaL_tablearg(1);
  279. lua_pushobject(o);
  280. lua_settag(luaL_check_number(2));
  281. lua_pushobject(o); /* returns first argument */
  282. }
  283. static void newtag (void)
  284. {
  285. lua_pushnumber(lua_newtag());
  286. }
  287. static void copytagmethods (void)
  288. {
  289. lua_pushnumber(lua_copytagmethods(luaL_check_number(1),
  290. luaL_check_number(2)));
  291. }
  292. static void rawgettable (void)
  293. {
  294. lua_pushobject(luaL_nonnullarg(1));
  295. lua_pushobject(luaL_nonnullarg(2));
  296. lua_pushobject(lua_rawgettable());
  297. }
  298. static void rawsettable (void)
  299. {
  300. lua_pushobject(luaL_nonnullarg(1));
  301. lua_pushobject(luaL_nonnullarg(2));
  302. lua_pushobject(luaL_nonnullarg(3));
  303. lua_rawsettable();
  304. }
  305. static void settagmethod (void)
  306. {
  307. lua_Object nf = luaL_nonnullarg(3);
  308. lua_pushobject(nf);
  309. lua_pushobject(lua_settagmethod((int)luaL_check_number(1),
  310. luaL_check_string(2)));
  311. }
  312. static void gettagmethod (void)
  313. {
  314. lua_pushobject(lua_gettagmethod((int)luaL_check_number(1),
  315. luaL_check_string(2)));
  316. }
  317. static void seterrormethod (void)
  318. {
  319. lua_Object nf = luaL_functionarg(1);
  320. lua_pushobject(nf);
  321. lua_pushobject(lua_seterrormethod());
  322. }
  323. static void luaI_collectgarbage (void)
  324. {
  325. lua_pushnumber(lua_collectgarbage(luaL_opt_number(1, 0)));
  326. }
  327. /*
  328. ** =======================================================
  329. ** some DEBUG functions
  330. ** =======================================================
  331. */
  332. #ifdef DEBUG
  333. static void mem_query (void)
  334. {
  335. lua_pushnumber(totalmem);
  336. lua_pushnumber(numblocks);
  337. }
  338. static void countlist (void)
  339. {
  340. char *s = luaL_check_string(1);
  341. GCnode *l = (s[0]=='t') ? L->roottable.next : (s[0]=='c') ? L->rootcl.next :
  342. (s[0]=='p') ? L->rootproto.next : L->rootglobal.next;
  343. int i=0;
  344. while (l) {
  345. i++;
  346. l = l->next;
  347. }
  348. lua_pushnumber(i);
  349. }
  350. static void testC (void)
  351. {
  352. #define getnum(s) ((*s++) - '0')
  353. #define getname(s) (nome[0] = *s++, nome)
  354. static int locks[10];
  355. lua_Object reg[10];
  356. char nome[2];
  357. char *s = luaL_check_string(1);
  358. nome[1] = 0;
  359. while (1) {
  360. switch (*s++) {
  361. case '0': case '1': case '2': case '3': case '4':
  362. case '5': case '6': case '7': case '8': case '9':
  363. lua_pushnumber(*(s-1) - '0');
  364. break;
  365. case 'c': reg[getnum(s)] = lua_createtable(); break;
  366. case 'C': { lua_CFunction f = lua_getcfunction(lua_getglobal(getname(s)));
  367. lua_pushCclosure(f, getnum(s));
  368. break;
  369. }
  370. case 'P': reg[getnum(s)] = lua_pop(); break;
  371. case 'g': { int n=getnum(s); reg[n]=lua_getglobal(getname(s)); break; }
  372. case 'G': { int n = getnum(s);
  373. reg[n] = lua_rawgetglobal(getname(s));
  374. break;
  375. }
  376. case 'l': locks[getnum(s)] = lua_ref(1); break;
  377. case 'L': locks[getnum(s)] = lua_ref(0); break;
  378. case 'r': { int n=getnum(s); reg[n]=lua_getref(locks[getnum(s)]); break; }
  379. case 'u': lua_unref(locks[getnum(s)]); break;
  380. case 'p': { int n = getnum(s); reg[n] = lua_getparam(getnum(s)); break; }
  381. case '=': lua_setglobal(getname(s)); break;
  382. case 's': lua_pushstring(getname(s)); break;
  383. case 'o': lua_pushobject(reg[getnum(s)]); break;
  384. case 'f': (lua_call)(getname(s)); break;
  385. case 'i': reg[getnum(s)] = lua_gettable(); break;
  386. case 'I': reg[getnum(s)] = lua_rawgettable(); break;
  387. case 't': lua_settable(); break;
  388. case 'T': lua_rawsettable(); break;
  389. default: luaL_verror("unknown command in `testC': %c", *(s-1));
  390. }
  391. if (*s == 0) return;
  392. if (*s++ != ' ') lua_error("missing ` ' between commands in `testC'");
  393. }
  394. }
  395. #endif
  396. /*
  397. ** Internal functions
  398. */
  399. static struct luaL_reg int_funcs[] = {
  400. #ifdef LUA_COMPAT2_5
  401. {"setfallback", luaT_setfallback},
  402. #endif
  403. #ifdef DEBUG
  404. {"testC", testC},
  405. {"totalmem", mem_query},
  406. {"count", countlist},
  407. #endif
  408. {"assert", luaI_assert},
  409. {"call", luaI_call},
  410. {"collectgarbage", luaI_collectgarbage},
  411. {"dofile", internaldofile},
  412. {"copytagmethods", copytagmethods},
  413. {"dostring", internaldostring},
  414. {"error", luaI_error},
  415. {"foreach", foreach},
  416. {"foreachvar", foreachvar},
  417. {"getglobal", getglobal},
  418. {"newtag", newtag},
  419. {"next", next},
  420. {"nextvar", nextvar},
  421. {"print", luaI_print},
  422. {"rawgetglobal", rawgetglobal},
  423. {"rawgettable", rawgettable},
  424. {"rawsetglobal", rawsetglobal},
  425. {"rawsettable", rawsettable},
  426. {"seterrormethod", seterrormethod},
  427. {"setglobal", setglobal},
  428. {"settagmethod", settagmethod},
  429. {"gettagmethod", gettagmethod},
  430. {"settag", settag},
  431. {"tonumber", tonumber},
  432. {"tostring", to_string},
  433. {"tag", luatag},
  434. {"type", luaI_type}
  435. };
  436. #define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0]))
  437. void luaB_predefine (void)
  438. {
  439. /* pre-register mem error messages, to avoid loop when error arises */
  440. luaS_newfixedstring(tableEM);
  441. luaS_newfixedstring(memEM);
  442. luaL_openlib(int_funcs, (sizeof(int_funcs)/sizeof(int_funcs[0])));
  443. lua_pushstring(LUA_VERSION);
  444. lua_setglobal("_VERSION");
  445. }