lbuiltin.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*
  2. ** $Id: lbuiltin.c,v 1.21 1998/01/02 17:46:32 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.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.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.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.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 char *to_string (lua_Object obj)
  115. {
  116. char *buff = luaL_openspace(30);
  117. TObject *o = luaA_Address(obj);
  118. switch (ttype(o)) {
  119. case LUA_T_NUMBER: case LUA_T_STRING:
  120. return lua_getstring(obj);
  121. case LUA_T_ARRAY: {
  122. sprintf(buff, "table: %p", (void *)o->value.a);
  123. return buff;
  124. }
  125. case LUA_T_CLOSURE: {
  126. sprintf(buff, "function: %p", (void *)o->value.cl);
  127. return buff;
  128. }
  129. case LUA_T_PROTO: {
  130. sprintf(buff, "function: %p", (void *)o->value.tf);
  131. return buff;
  132. }
  133. case LUA_T_CPROTO: {
  134. sprintf(buff, "function: %p", (void *)o->value.f);
  135. return buff;
  136. }
  137. case LUA_T_USERDATA: {
  138. sprintf(buff, "userdata: %p", o->value.ts->u.d.v);
  139. return buff;
  140. }
  141. case LUA_T_NIL:
  142. return "nil";
  143. default:
  144. lua_error("internal error");
  145. return NULL; /* to avoid warnings */
  146. }
  147. }
  148. static void bi_tostring (void)
  149. {
  150. lua_pushstring(to_string(lua_getparam(1)));
  151. }
  152. static void luaI_print (void)
  153. {
  154. int i = 1;
  155. lua_Object obj;
  156. while ((obj = lua_getparam(i++)) != LUA_NOOBJECT)
  157. printf("%s\t", to_string(obj));
  158. printf("\n");
  159. }
  160. static void luaI_type (void)
  161. {
  162. lua_Object o = luaL_nonnullarg(1);
  163. lua_pushstring(luaO_typenames[-ttype(luaA_Address(o))]);
  164. lua_pushnumber(lua_tag(o));
  165. }
  166. static void tonumber (void)
  167. {
  168. int base = luaL_opt_number(2, 10);
  169. if (base == 10) { /* standard convertion */
  170. lua_Object o = lua_getparam(1);
  171. if (lua_isnumber(o))
  172. lua_pushnumber(lua_getnumber(o));
  173. }
  174. else {
  175. char *s = luaL_check_string(1);
  176. unsigned long n;
  177. luaL_arg_check(0 <= base && base <= 36, 2, "base out of range");
  178. n = strtol(s, &s, base);
  179. while (isspace(*s)) s++; /* skip trailing spaces */
  180. if (*s) return; /* invalid format: return nil */
  181. lua_pushnumber(n);
  182. }
  183. }
  184. static void luaI_error (void)
  185. {
  186. lua_error(lua_getstring(lua_getparam(1)));
  187. }
  188. static void luaI_assert (void)
  189. {
  190. lua_Object p = lua_getparam(1);
  191. if (p == LUA_NOOBJECT || lua_isnil(p))
  192. luaL_verror("assertion failed! %.100s", luaL_opt_string(2, ""));
  193. }
  194. static void setglobal (void)
  195. {
  196. char *n = luaL_check_string(1);
  197. lua_Object value = luaL_nonnullarg(2);
  198. lua_pushobject(value);
  199. lua_setglobal(n);
  200. lua_pushobject(value); /* return given value */
  201. }
  202. static void rawsetglobal (void)
  203. {
  204. char *n = luaL_check_string(1);
  205. lua_Object value = luaL_nonnullarg(2);
  206. lua_pushobject(value);
  207. lua_rawsetglobal(n);
  208. lua_pushobject(value); /* return given value */
  209. }
  210. static void getglobal (void)
  211. {
  212. lua_pushobject(lua_getglobal(luaL_check_string(1)));
  213. }
  214. static void rawgetglobal (void)
  215. {
  216. lua_pushobject(lua_rawgetglobal(luaL_check_string(1)));
  217. }
  218. static void luatag (void)
  219. {
  220. lua_pushnumber(lua_tag(lua_getparam(1)));
  221. }
  222. static int getnarg (lua_Object table)
  223. {
  224. lua_Object temp;
  225. /* temp = table.n */
  226. lua_pushobject(table); lua_pushstring("n"); temp = lua_rawgettable();
  227. return (lua_isnumber(temp) ? lua_getnumber(temp) : MAX_WORD);
  228. }
  229. static void luaI_call (void)
  230. {
  231. lua_Object f = luaL_nonnullarg(1);
  232. lua_Object arg = luaL_tablearg(2);
  233. char *options = luaL_opt_string(3, "");
  234. lua_Object err = lua_getparam(4);
  235. int narg = getnarg(arg);
  236. int i, status;
  237. if (err != LUA_NOOBJECT) { /* set new error method */
  238. lua_pushobject(err);
  239. err = lua_seterrormethod();
  240. }
  241. /* push arg[1...n] */
  242. for (i=0; i<narg; i++) {
  243. lua_Object temp;
  244. /* temp = arg[i+1] */
  245. lua_pushobject(arg); lua_pushnumber(i+1); temp = lua_rawgettable();
  246. if (narg == MAX_WORD && lua_isnil(temp))
  247. break;
  248. lua_pushobject(temp);
  249. }
  250. status = lua_callfunction(f);
  251. if (err != LUA_NOOBJECT) { /* restore old error method */
  252. lua_pushobject(err);
  253. lua_seterrormethod();
  254. }
  255. if (status != 0) { /* error in call? */
  256. if (strchr(options, 'x'))
  257. return; /* return nil to signal the error */
  258. else
  259. lua_error(NULL);
  260. }
  261. else { /* no errors */
  262. if (strchr(options, 'p'))
  263. luaA_packresults();
  264. else
  265. luaA_passresults();
  266. }
  267. }
  268. static void settag (void)
  269. {
  270. lua_Object o = luaL_tablearg(1);
  271. lua_pushobject(o);
  272. lua_settag(luaL_check_number(2));
  273. }
  274. static void newtag (void)
  275. {
  276. lua_pushnumber(lua_newtag());
  277. }
  278. static void copytagmethods (void)
  279. {
  280. lua_pushnumber(lua_copytagmethods(luaL_check_number(1),
  281. luaL_check_number(2)));
  282. }
  283. static void rawgettable (void)
  284. {
  285. lua_Object t = luaL_nonnullarg(1);
  286. lua_Object i = luaL_nonnullarg(2);
  287. lua_pushobject(t);
  288. lua_pushobject(i);
  289. lua_pushobject(lua_rawgettable());
  290. }
  291. static void rawsettable (void)
  292. {
  293. lua_Object t = luaL_nonnullarg(1);
  294. lua_Object i = luaL_nonnullarg(2);
  295. lua_Object v = luaL_nonnullarg(3);
  296. lua_pushobject(t);
  297. lua_pushobject(i);
  298. lua_pushobject(v);
  299. lua_rawsettable();
  300. }
  301. static void settagmethod (void)
  302. {
  303. lua_Object nf = luaL_nonnullarg(3);
  304. lua_pushobject(nf);
  305. lua_pushobject(lua_settagmethod((int)luaL_check_number(1),
  306. luaL_check_string(2)));
  307. }
  308. static void gettagmethod (void)
  309. {
  310. lua_pushobject(lua_gettagmethod((int)luaL_check_number(1),
  311. luaL_check_string(2)));
  312. }
  313. static void seterrormethod (void)
  314. {
  315. lua_Object nf = luaL_functionarg(1);
  316. lua_pushobject(nf);
  317. lua_pushobject(lua_seterrormethod());
  318. }
  319. static void luaI_collectgarbage (void)
  320. {
  321. lua_pushnumber(lua_collectgarbage(luaL_opt_number(1, 0)));
  322. }
  323. /*
  324. ** =======================================================
  325. ** some DEBUG functions
  326. ** =======================================================
  327. */
  328. #ifdef DEBUG
  329. static void mem_query (void)
  330. {
  331. lua_pushnumber(totalmem);
  332. lua_pushnumber(numblocks);
  333. }
  334. static void countlist (void)
  335. {
  336. char *s = luaL_check_string(1);
  337. GCnode *l = (s[0]=='t') ? L->roottable.next : (s[0]=='c') ? L->rootcl.next :
  338. (s[0]=='p') ? L->rootproto.next : L->rootglobal.next;
  339. int i=0;
  340. while (l) {
  341. i++;
  342. l = l->next;
  343. }
  344. lua_pushnumber(i);
  345. }
  346. static void testC (void)
  347. {
  348. #define getnum(s) ((*s++) - '0')
  349. #define getname(s) (nome[0] = *s++, nome)
  350. static int locks[10];
  351. lua_Object reg[10];
  352. char nome[2];
  353. char *s = luaL_check_string(1);
  354. nome[1] = 0;
  355. while (1) {
  356. switch (*s++) {
  357. case '0': case '1': case '2': case '3': case '4':
  358. case '5': case '6': case '7': case '8': case '9':
  359. lua_pushnumber(*(s-1) - '0');
  360. break;
  361. case 'c': reg[getnum(s)] = lua_createtable(); break;
  362. case 'C': { lua_CFunction f = lua_getcfunction(lua_getglobal(getname(s)));
  363. lua_pushCclosure(f, getnum(s));
  364. break;
  365. }
  366. case 'P': reg[getnum(s)] = lua_pop(); break;
  367. case 'g': { int n=getnum(s); reg[n]=lua_getglobal(getname(s)); break; }
  368. case 'G': { int n = getnum(s);
  369. reg[n] = lua_rawgetglobal(getname(s));
  370. break;
  371. }
  372. case 'l': locks[getnum(s)] = lua_ref(1); break;
  373. case 'L': locks[getnum(s)] = lua_ref(0); break;
  374. case 'r': { int n=getnum(s); reg[n]=lua_getref(locks[getnum(s)]); break; }
  375. case 'u': lua_unref(locks[getnum(s)]); break;
  376. case 'p': { int n = getnum(s); reg[n] = lua_getparam(getnum(s)); break; }
  377. case '=': lua_setglobal(getname(s)); break;
  378. case 's': lua_pushstring(getname(s)); break;
  379. case 'o': lua_pushobject(reg[getnum(s)]); break;
  380. case 'f': lua_call(getname(s)); break;
  381. case 'i': reg[getnum(s)] = lua_gettable(); break;
  382. case 'I': reg[getnum(s)] = lua_rawgettable(); break;
  383. case 't': lua_settable(); break;
  384. case 'T': lua_rawsettable(); break;
  385. default: luaL_verror("unknown command in `testC': %c", *(s-1));
  386. }
  387. if (*s == 0) return;
  388. if (*s++ != ' ') lua_error("missing ` ' between commands in `testC'");
  389. }
  390. }
  391. #endif
  392. /*
  393. ** Internal functions
  394. */
  395. static struct luaL_reg int_funcs[] = {
  396. #ifdef LUA_COMPAT2_5
  397. {"setfallback", luaT_setfallback},
  398. #endif
  399. #ifdef DEBUG
  400. {"testC", testC},
  401. {"totalmem", mem_query},
  402. {"count", countlist},
  403. #endif
  404. {"assert", luaI_assert},
  405. {"call", luaI_call},
  406. {"collectgarbage", luaI_collectgarbage},
  407. {"dofile", internaldofile},
  408. {"copytagmethods", copytagmethods},
  409. {"dostring", internaldostring},
  410. {"error", luaI_error},
  411. {"foreach", foreach},
  412. {"foreachvar", foreachvar},
  413. {"getglobal", getglobal},
  414. {"newtag", newtag},
  415. {"next", next},
  416. {"nextvar", nextvar},
  417. {"print", luaI_print},
  418. {"rawgetglobal", rawgetglobal},
  419. {"rawgettable", rawgettable},
  420. {"rawsetglobal", rawsetglobal},
  421. {"rawsettable", rawsettable},
  422. {"seterrormethod", seterrormethod},
  423. {"setglobal", setglobal},
  424. {"settagmethod", settagmethod},
  425. {"gettagmethod", gettagmethod},
  426. {"settag", settag},
  427. {"tonumber", tonumber},
  428. {"tostring", bi_tostring},
  429. {"tag", luatag},
  430. {"type", luaI_type}
  431. };
  432. #define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0]))
  433. void luaB_predefine (void)
  434. {
  435. /* pre-register mem error messages, to avoid loop when error arises */
  436. luaS_newfixedstring(tableEM);
  437. luaS_newfixedstring(memEM);
  438. luaL_openlib(int_funcs, (sizeof(int_funcs)/sizeof(int_funcs[0])));
  439. lua_pushstring(LUA_VERSION);
  440. lua_setglobal("_VERSION");
  441. }