lbuiltin.c 11 KB

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