lbuiltin.c 11 KB

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