lbuiltin.c 12 KB

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