lbuiltin.c 11 KB

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