lapi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /*
  2. ** $Id: lapi.c,v 1.41 1999/03/04 21:17:26 roberto Exp roberto $
  3. ** Lua API
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "lapi.h"
  9. #include "lauxlib.h"
  10. #include "ldo.h"
  11. #include "lfunc.h"
  12. #include "lgc.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. #include "luadebug.h"
  21. #include "lvm.h"
  22. char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
  23. "$Authors: " LUA_AUTHORS " $";
  24. TObject *luaA_Address (lua_Object o) {
  25. return (o != LUA_NOOBJECT) ? Address(o) : NULL;
  26. }
  27. static lua_Type normalized_type (TObject *o)
  28. {
  29. int t = ttype(o);
  30. switch (t) {
  31. case LUA_T_PMARK:
  32. return LUA_T_PROTO;
  33. case LUA_T_CMARK:
  34. return LUA_T_CPROTO;
  35. case LUA_T_CLMARK:
  36. return LUA_T_CLOSURE;
  37. default:
  38. return t;
  39. }
  40. }
  41. static void set_normalized (TObject *d, TObject *s)
  42. {
  43. d->value = s->value;
  44. d->ttype = normalized_type(s);
  45. }
  46. static TObject *luaA_protovalue (TObject *o)
  47. {
  48. return (normalized_type(o) == LUA_T_CLOSURE) ? protovalue(o) : o;
  49. }
  50. void luaA_packresults (void)
  51. {
  52. luaV_pack(L->Cstack.lua2C, L->Cstack.num, L->stack.top);
  53. incr_top;
  54. }
  55. int luaA_passresults (void) {
  56. L->Cstack.base = L->Cstack.lua2C; /* position of first result */
  57. return L->Cstack.num;
  58. }
  59. static void checkCparams (int nParams)
  60. {
  61. if (L->stack.top-L->stack.stack < L->Cstack.base+nParams)
  62. lua_error("API error - wrong number of arguments in C2lua stack");
  63. }
  64. static lua_Object put_luaObject (TObject *o) {
  65. luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base);
  66. L->stack.stack[L->Cstack.base++] = *o;
  67. return L->Cstack.base; /* this is +1 real position (see Ref) */
  68. }
  69. static lua_Object put_luaObjectonTop (void) {
  70. luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base);
  71. L->stack.stack[L->Cstack.base++] = *(--L->stack.top);
  72. return L->Cstack.base; /* this is +1 real position (see Ref) */
  73. }
  74. static void top2LC (int n) {
  75. /* Put the 'n' elements on the top as the Lua2C contents */
  76. L->Cstack.base = (L->stack.top-L->stack.stack); /* new base */
  77. L->Cstack.lua2C = L->Cstack.base-n; /* position of the new results */
  78. L->Cstack.num = n; /* number of results */
  79. }
  80. lua_Object lua_pop (void) {
  81. checkCparams(1);
  82. return put_luaObjectonTop();
  83. }
  84. /*
  85. ** Get a parameter, returning the object handle or LUA_NOOBJECT on error.
  86. ** 'number' must be 1 to get the first parameter.
  87. */
  88. lua_Object lua_lua2C (int number)
  89. {
  90. if (number <= 0 || number > L->Cstack.num) return LUA_NOOBJECT;
  91. /* Ref(L->stack.stack+(L->Cstack.lua2C+number-1)) ==
  92. L->stack.stack+(L->Cstack.lua2C+number-1)-L->stack.stack+1 == */
  93. return L->Cstack.lua2C+number;
  94. }
  95. int lua_callfunction (lua_Object function)
  96. {
  97. if (function == LUA_NOOBJECT)
  98. return 1;
  99. else {
  100. luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base);
  101. set_normalized(L->stack.stack+L->Cstack.base, Address(function));
  102. return luaD_protectedrun(MULT_RET);
  103. }
  104. }
  105. lua_Object lua_gettagmethod (int tag, char *event)
  106. {
  107. return put_luaObject(luaT_gettagmethod(tag, event));
  108. }
  109. lua_Object lua_settagmethod (int tag, char *event)
  110. {
  111. checkCparams(1);
  112. luaT_settagmethod(tag, event, L->stack.top-1);
  113. return put_luaObjectonTop();
  114. }
  115. lua_Object lua_seterrormethod (void) {
  116. lua_Object temp;
  117. checkCparams(1);
  118. temp = lua_getglobal("_ERRORMESSAGE");
  119. lua_setglobal("_ERRORMESSAGE");
  120. return temp;
  121. }
  122. lua_Object lua_gettable (void)
  123. {
  124. checkCparams(2);
  125. luaV_gettable();
  126. return put_luaObjectonTop();
  127. }
  128. lua_Object lua_rawgettable (void)
  129. {
  130. checkCparams(2);
  131. if (ttype(L->stack.top-2) != LUA_T_ARRAY)
  132. lua_error("indexed expression not a table in rawgettable");
  133. else {
  134. *(L->stack.top-2) = *luaH_get(avalue(L->stack.top-2), L->stack.top-1);
  135. --L->stack.top;
  136. }
  137. return put_luaObjectonTop();
  138. }
  139. void lua_settable (void) {
  140. checkCparams(3);
  141. luaV_settable(L->stack.top-3);
  142. L->stack.top -= 2; /* pop table and index */
  143. }
  144. void lua_rawsettable (void) {
  145. checkCparams(3);
  146. luaV_rawsettable(L->stack.top-3);
  147. }
  148. lua_Object lua_createtable (void)
  149. {
  150. TObject o;
  151. luaC_checkGC();
  152. avalue(&o) = luaH_new(0);
  153. ttype(&o) = LUA_T_ARRAY;
  154. return put_luaObject(&o);
  155. }
  156. lua_Object lua_getglobal (char *name)
  157. {
  158. luaD_checkstack(2); /* may need that to call T.M. */
  159. luaV_getglobal(luaS_new(name));
  160. return put_luaObjectonTop();
  161. }
  162. lua_Object lua_rawgetglobal (char *name)
  163. {
  164. TaggedString *ts = luaS_new(name);
  165. return put_luaObject(&ts->u.s.globalval);
  166. }
  167. void lua_setglobal (char *name)
  168. {
  169. checkCparams(1);
  170. luaD_checkstack(2); /* may need that to call T.M. */
  171. luaV_setglobal(luaS_new(name));
  172. }
  173. void lua_rawsetglobal (char *name)
  174. {
  175. TaggedString *ts = luaS_new(name);
  176. checkCparams(1);
  177. luaS_rawsetglobal(ts, --L->stack.top);
  178. }
  179. int lua_isnil (lua_Object o)
  180. {
  181. return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_NIL);
  182. }
  183. int lua_istable (lua_Object o)
  184. {
  185. return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_ARRAY);
  186. }
  187. int lua_isuserdata (lua_Object o)
  188. {
  189. return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_USERDATA);
  190. }
  191. int lua_iscfunction (lua_Object o)
  192. {
  193. return (lua_tag(o) == LUA_T_CPROTO);
  194. }
  195. int lua_isnumber (lua_Object o)
  196. {
  197. return (o!= LUA_NOOBJECT) && (tonumber(Address(o)) == 0);
  198. }
  199. int lua_isstring (lua_Object o)
  200. {
  201. int t = lua_tag(o);
  202. return (t == LUA_T_STRING) || (t == LUA_T_NUMBER);
  203. }
  204. int lua_isfunction (lua_Object o)
  205. {
  206. int t = lua_tag(o);
  207. return (t == LUA_T_PROTO) || (t == LUA_T_CPROTO);
  208. }
  209. double lua_getnumber (lua_Object object)
  210. {
  211. if (object == LUA_NOOBJECT) return 0.0;
  212. if (tonumber(Address(object))) return 0.0;
  213. else return (nvalue(Address(object)));
  214. }
  215. char *lua_getstring (lua_Object object)
  216. {
  217. luaC_checkGC(); /* "tostring" may create a new string */
  218. if (object == LUA_NOOBJECT || tostring(Address(object)))
  219. return NULL;
  220. else return (svalue(Address(object)));
  221. }
  222. long lua_strlen (lua_Object object)
  223. {
  224. luaC_checkGC(); /* "tostring" may create a new string */
  225. if (object == LUA_NOOBJECT || tostring(Address(object)))
  226. return 0L;
  227. else return (tsvalue(Address(object))->u.s.len);
  228. }
  229. void *lua_getuserdata (lua_Object object)
  230. {
  231. if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
  232. return NULL;
  233. else return tsvalue(Address(object))->u.d.v;
  234. }
  235. lua_CFunction lua_getcfunction (lua_Object object)
  236. {
  237. if (!lua_iscfunction(object))
  238. return NULL;
  239. else return fvalue(luaA_protovalue(Address(object)));
  240. }
  241. void lua_pushnil (void)
  242. {
  243. ttype(L->stack.top) = LUA_T_NIL;
  244. incr_top;
  245. }
  246. void lua_pushnumber (double n)
  247. {
  248. ttype(L->stack.top) = LUA_T_NUMBER;
  249. nvalue(L->stack.top) = n;
  250. incr_top;
  251. }
  252. void lua_pushlstring (char *s, long len)
  253. {
  254. tsvalue(L->stack.top) = luaS_newlstr(s, len);
  255. ttype(L->stack.top) = LUA_T_STRING;
  256. incr_top;
  257. luaC_checkGC();
  258. }
  259. void lua_pushstring (char *s)
  260. {
  261. if (s == NULL)
  262. lua_pushnil();
  263. else
  264. lua_pushlstring(s, strlen(s));
  265. }
  266. void lua_pushcclosure (lua_CFunction fn, int n)
  267. {
  268. if (fn == NULL)
  269. lua_error("API error - attempt to push a NULL Cfunction");
  270. checkCparams(n);
  271. ttype(L->stack.top) = LUA_T_CPROTO;
  272. fvalue(L->stack.top) = fn;
  273. incr_top;
  274. luaV_closure(n);
  275. luaC_checkGC();
  276. }
  277. void lua_pushusertag (void *u, int tag)
  278. {
  279. if (tag < 0 && tag != LUA_ANYTAG)
  280. luaT_realtag(tag); /* error if tag is not valid */
  281. tsvalue(L->stack.top) = luaS_createudata(u, tag);
  282. ttype(L->stack.top) = LUA_T_USERDATA;
  283. incr_top;
  284. luaC_checkGC();
  285. }
  286. void luaA_pushobject (TObject *o)
  287. {
  288. *L->stack.top = *o;
  289. incr_top;
  290. }
  291. void lua_pushobject (lua_Object o)
  292. {
  293. if (o == LUA_NOOBJECT)
  294. lua_error("API error - attempt to push a NOOBJECT");
  295. else {
  296. set_normalized(L->stack.top, Address(o));
  297. incr_top;
  298. }
  299. }
  300. int lua_tag (lua_Object lo)
  301. {
  302. if (lo == LUA_NOOBJECT)
  303. return LUA_T_NIL;
  304. else {
  305. TObject *o = Address(lo);
  306. int t;
  307. switch (t = ttype(o)) {
  308. case LUA_T_USERDATA:
  309. return o->value.ts->u.d.tag;
  310. case LUA_T_ARRAY:
  311. return o->value.a->htag;
  312. case LUA_T_PMARK:
  313. return LUA_T_PROTO;
  314. case LUA_T_CMARK:
  315. return LUA_T_CPROTO;
  316. case LUA_T_CLOSURE: case LUA_T_CLMARK:
  317. return o->value.cl->consts[0].ttype;
  318. #ifdef DEBUG
  319. case LUA_T_LINE:
  320. LUA_INTERNALERROR("invalid type");
  321. #endif
  322. default:
  323. return t;
  324. }
  325. }
  326. }
  327. void lua_settag (int tag)
  328. {
  329. checkCparams(1);
  330. luaT_realtag(tag);
  331. switch (ttype(L->stack.top-1)) {
  332. case LUA_T_ARRAY:
  333. (L->stack.top-1)->value.a->htag = tag;
  334. break;
  335. case LUA_T_USERDATA:
  336. (L->stack.top-1)->value.ts->u.d.tag = tag;
  337. break;
  338. default:
  339. luaL_verror("cannot change the tag of a %.20s",
  340. luaO_typename(L->stack.top-1));
  341. }
  342. L->stack.top--;
  343. }
  344. TaggedString *luaA_nextvar (TaggedString *g) {
  345. if (g == NULL)
  346. g = (TaggedString *)L->rootglobal.next; /* first variable */
  347. else {
  348. /* check whether name is in global var list */
  349. luaL_arg_check((GCnode *)g != g->head.next, 1, "variable name expected");
  350. g = (TaggedString *)g->head.next; /* get next */
  351. }
  352. while (g && g->u.s.globalval.ttype == LUA_T_NIL) /* skip globals with nil */
  353. g = (TaggedString *)g->head.next;
  354. if (g) {
  355. ttype(L->stack.top) = LUA_T_STRING; tsvalue(L->stack.top) = g;
  356. incr_top;
  357. luaA_pushobject(&g->u.s.globalval);
  358. }
  359. return g;
  360. }
  361. char *lua_nextvar (char *varname) {
  362. TaggedString *g = (varname == NULL) ? NULL : luaS_new(varname);
  363. g = luaA_nextvar(g);
  364. if (g) {
  365. top2LC(2);
  366. return g->str;
  367. }
  368. else {
  369. top2LC(0);
  370. return NULL;
  371. }
  372. }
  373. int luaA_next (Hash *t, int i) {
  374. int tsize = nhash(t);
  375. for (; i<tsize; i++) {
  376. Node *n = node(t, i);
  377. if (ttype(val(n)) != LUA_T_NIL) {
  378. luaA_pushobject(ref(n));
  379. luaA_pushobject(val(n));
  380. return i+1; /* index to be used next time */
  381. }
  382. }
  383. return 0; /* no more elements */
  384. }
  385. int lua_next (lua_Object o, int i) {
  386. TObject *t = Address(o);
  387. if (ttype(t) != LUA_T_ARRAY)
  388. lua_error("API error - object is not a table in `lua_next'");
  389. i = luaA_next(avalue(t), i);
  390. top2LC((i==0) ? 0 : 2);
  391. return i;
  392. }
  393. /*
  394. ** {======================================================
  395. ** To manipulate some state information
  396. ** =======================================================
  397. */
  398. lua_State *lua_setstate (lua_State *st) {
  399. lua_State *old = lua_state;
  400. lua_state = st;
  401. return old;
  402. }
  403. lua_LHFunction lua_setlinehook (lua_LHFunction func) {
  404. lua_LHFunction old = L->linehook;
  405. L->linehook = func;
  406. return old;
  407. }
  408. lua_CHFunction lua_setcallhook (lua_CHFunction func) {
  409. lua_CHFunction old = L->callhook;
  410. L->callhook = func;
  411. return old;
  412. }
  413. int lua_setdebug (int debug) {
  414. int old = L->debug;
  415. L->debug = debug;
  416. return old;
  417. }
  418. /* }====================================================== */
  419. /*
  420. ** {======================================================
  421. ** Debug interface
  422. ** =======================================================
  423. */
  424. lua_Function lua_stackedfunction (int level)
  425. {
  426. StkId i;
  427. for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--) {
  428. int t = L->stack.stack[i].ttype;
  429. if (t == LUA_T_CLMARK || t == LUA_T_PMARK || t == LUA_T_CMARK)
  430. if (level-- == 0)
  431. return Ref(L->stack.stack+i);
  432. }
  433. return LUA_NOOBJECT;
  434. }
  435. int lua_nups (lua_Function func) {
  436. TObject *o = luaA_Address(func);
  437. return (!o || normalized_type(o) != LUA_T_CLOSURE) ? 0 : o->value.cl->nelems;
  438. }
  439. int lua_currentline (lua_Function func)
  440. {
  441. TObject *f = Address(func);
  442. return (f+1 < L->stack.top && (f+1)->ttype == LUA_T_LINE) ?
  443. (f+1)->value.i : -1;
  444. }
  445. lua_Object lua_getlocal (lua_Function func, int local_number, char **name) {
  446. /* check whether func is a Lua function */
  447. if (lua_tag(func) != LUA_T_PROTO)
  448. return LUA_NOOBJECT;
  449. else {
  450. TObject *f = Address(func);
  451. TProtoFunc *fp = luaA_protovalue(f)->value.tf;
  452. *name = luaF_getlocalname(fp, local_number, lua_currentline(func));
  453. if (*name) {
  454. /* if "*name", there must be a LUA_T_LINE */
  455. /* therefore, f+2 points to function base */
  456. return put_luaObject((f+2)+(local_number-1));
  457. }
  458. else
  459. return LUA_NOOBJECT;
  460. }
  461. }
  462. int lua_setlocal (lua_Function func, int local_number)
  463. {
  464. /* check whether func is a Lua function */
  465. if (lua_tag(func) != LUA_T_PROTO)
  466. return 0;
  467. else {
  468. TObject *f = Address(func);
  469. TProtoFunc *fp = luaA_protovalue(f)->value.tf;
  470. char *name = luaF_getlocalname(fp, local_number, lua_currentline(func));
  471. checkCparams(1);
  472. --L->stack.top;
  473. if (name) {
  474. /* if "name", there must be a LUA_T_LINE */
  475. /* therefore, f+2 points to function base */
  476. *((f+2)+(local_number-1)) = *L->stack.top;
  477. return 1;
  478. }
  479. else
  480. return 0;
  481. }
  482. }
  483. void lua_funcinfo (lua_Object func, char **source, int *linedefined) {
  484. if (!lua_isfunction(func))
  485. lua_error("API - `funcinfo' called with a non-function value");
  486. else {
  487. TObject *f = luaA_protovalue(Address(func));
  488. if (normalized_type(f) == LUA_T_PROTO) {
  489. *source = tfvalue(f)->source->str;
  490. *linedefined = tfvalue(f)->lineDefined;
  491. }
  492. else {
  493. *source = "(C)";
  494. *linedefined = -1;
  495. }
  496. }
  497. }
  498. static int checkfunc (TObject *o)
  499. {
  500. return luaO_equalObj(o, L->stack.top);
  501. }
  502. char *lua_getobjname (lua_Object o, char **name)
  503. { /* try to find a name for given function */
  504. set_normalized(L->stack.top, Address(o)); /* to be accessed by "checkfunc" */
  505. if ((*name = luaS_travsymbol(checkfunc)) != NULL)
  506. return "global";
  507. else if ((*name = luaT_travtagmethods(checkfunc)) != NULL)
  508. return "tag-method";
  509. else return "";
  510. }
  511. /* }====================================================== */
  512. /*
  513. ** {======================================================
  514. ** BLOCK mechanism
  515. ** =======================================================
  516. */
  517. void lua_beginblock (void)
  518. {
  519. if (L->numCblocks >= MAX_C_BLOCKS)
  520. lua_error("too many nested blocks");
  521. L->Cblocks[L->numCblocks] = L->Cstack;
  522. L->numCblocks++;
  523. }
  524. void lua_endblock (void)
  525. {
  526. --L->numCblocks;
  527. L->Cstack = L->Cblocks[L->numCblocks];
  528. luaD_adjusttop(L->Cstack.base);
  529. }
  530. int lua_ref (int lock)
  531. {
  532. int ref;
  533. checkCparams(1);
  534. ref = luaC_ref(L->stack.top-1, lock);
  535. L->stack.top--;
  536. return ref;
  537. }
  538. lua_Object lua_getref (int ref)
  539. {
  540. TObject *o = luaC_getref(ref);
  541. return (o ? put_luaObject(o) : LUA_NOOBJECT);
  542. }
  543. /* }====================================================== */
  544. #ifdef LUA_COMPAT2_5
  545. /*
  546. ** API: set a function as a fallback
  547. */
  548. static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults)
  549. {
  550. StkId base = (L->stack.top-L->stack.stack)-nParams;
  551. luaD_openstack(nParams);
  552. L->stack.stack[base].ttype = LUA_T_CPROTO;
  553. L->stack.stack[base].value.f = f;
  554. luaD_call(base+1, nResults);
  555. }
  556. lua_Object lua_setfallback (char *name, lua_CFunction fallback)
  557. {
  558. lua_pushstring(name);
  559. lua_pushcfunction(fallback);
  560. do_unprotectedrun(luaT_setfallback, 2, 1);
  561. return put_luaObjectonTop();
  562. }
  563. #endif