2
0

lbuiltin.c 12 KB

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