lapi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. /*
  2. ** $Id: lapi.c,v 1.46 1999/06/17 17:04:03 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();
  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. checkCparams(2);
  130. if (ttype(L->stack.top-2) != LUA_T_ARRAY)
  131. lua_error("indexed expression not a table in rawgettable");
  132. *(L->stack.top-2) = *luaH_get(avalue(L->stack.top-2), L->stack.top-1);
  133. --L->stack.top;
  134. return put_luaObjectonTop();
  135. }
  136. void lua_settable (void) {
  137. checkCparams(3);
  138. luaV_settable(L->stack.top-3);
  139. L->stack.top -= 2; /* pop table and index */
  140. }
  141. void lua_rawsettable (void) {
  142. checkCparams(3);
  143. luaV_rawsettable(L->stack.top-3);
  144. }
  145. lua_Object lua_createtable (void)
  146. {
  147. TObject o;
  148. luaC_checkGC();
  149. avalue(&o) = luaH_new(0);
  150. ttype(&o) = LUA_T_ARRAY;
  151. return put_luaObject(&o);
  152. }
  153. lua_Object lua_getglobal (char *name)
  154. {
  155. luaD_checkstack(2); /* may need that to call T.M. */
  156. luaV_getglobal(luaS_new(name));
  157. return put_luaObjectonTop();
  158. }
  159. lua_Object lua_rawgetglobal (char *name)
  160. {
  161. TaggedString *ts = luaS_new(name);
  162. return put_luaObject(&ts->u.s.globalval);
  163. }
  164. void lua_setglobal (char *name)
  165. {
  166. checkCparams(1);
  167. luaD_checkstack(2); /* may need that to call T.M. */
  168. luaV_setglobal(luaS_new(name));
  169. }
  170. void lua_rawsetglobal (char *name)
  171. {
  172. TaggedString *ts = luaS_new(name);
  173. checkCparams(1);
  174. luaS_rawsetglobal(ts, --L->stack.top);
  175. }
  176. int lua_isnil (lua_Object o)
  177. {
  178. return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_NIL);
  179. }
  180. int lua_istable (lua_Object o)
  181. {
  182. return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_ARRAY);
  183. }
  184. int lua_isuserdata (lua_Object o)
  185. {
  186. return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_USERDATA);
  187. }
  188. int lua_iscfunction (lua_Object o)
  189. {
  190. return (lua_tag(o) == LUA_T_CPROTO);
  191. }
  192. int lua_isnumber (lua_Object o)
  193. {
  194. return (o!= LUA_NOOBJECT) && (tonumber(Address(o)) == 0);
  195. }
  196. int lua_isstring (lua_Object o)
  197. {
  198. int t = lua_tag(o);
  199. return (t == LUA_T_STRING) || (t == LUA_T_NUMBER);
  200. }
  201. int lua_isfunction (lua_Object o)
  202. {
  203. int t = lua_tag(o);
  204. return (t == LUA_T_PROTO) || (t == LUA_T_CPROTO);
  205. }
  206. double lua_getnumber (lua_Object object)
  207. {
  208. if (object == LUA_NOOBJECT) return 0.0;
  209. if (tonumber(Address(object))) return 0.0;
  210. else return (nvalue(Address(object)));
  211. }
  212. char *lua_getstring (lua_Object object)
  213. {
  214. luaC_checkGC(); /* "tostring" may create a new string */
  215. if (object == LUA_NOOBJECT || tostring(Address(object)))
  216. return NULL;
  217. else return (svalue(Address(object)));
  218. }
  219. long lua_strlen (lua_Object object)
  220. {
  221. luaC_checkGC(); /* "tostring" may create a new string */
  222. if (object == LUA_NOOBJECT || tostring(Address(object)))
  223. return 0L;
  224. else return (tsvalue(Address(object))->u.s.len);
  225. }
  226. void *lua_getuserdata (lua_Object object)
  227. {
  228. if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
  229. return NULL;
  230. else return tsvalue(Address(object))->u.d.v;
  231. }
  232. lua_CFunction lua_getcfunction (lua_Object object)
  233. {
  234. if (!lua_iscfunction(object))
  235. return NULL;
  236. else return fvalue(luaA_protovalue(Address(object)));
  237. }
  238. void lua_pushnil (void)
  239. {
  240. ttype(L->stack.top) = LUA_T_NIL;
  241. incr_top;
  242. }
  243. void lua_pushnumber (double n)
  244. {
  245. ttype(L->stack.top) = LUA_T_NUMBER;
  246. nvalue(L->stack.top) = n;
  247. incr_top;
  248. }
  249. void lua_pushlstring (char *s, long len)
  250. {
  251. tsvalue(L->stack.top) = luaS_newlstr(s, len);
  252. ttype(L->stack.top) = LUA_T_STRING;
  253. incr_top;
  254. luaC_checkGC();
  255. }
  256. void lua_pushstring (char *s)
  257. {
  258. if (s == NULL)
  259. lua_pushnil();
  260. else
  261. lua_pushlstring(s, strlen(s));
  262. }
  263. void lua_pushcclosure (lua_CFunction fn, int n)
  264. {
  265. if (fn == NULL)
  266. lua_error("API error - attempt to push a NULL Cfunction");
  267. checkCparams(n);
  268. ttype(L->stack.top) = LUA_T_CPROTO;
  269. fvalue(L->stack.top) = fn;
  270. incr_top;
  271. luaV_closure(n);
  272. luaC_checkGC();
  273. }
  274. void lua_pushusertag (void *u, int tag)
  275. {
  276. if (tag < 0 && tag != LUA_ANYTAG)
  277. luaT_realtag(tag); /* error if tag is not valid */
  278. tsvalue(L->stack.top) = luaS_createudata(u, tag);
  279. ttype(L->stack.top) = LUA_T_USERDATA;
  280. incr_top;
  281. luaC_checkGC();
  282. }
  283. void luaA_pushobject (TObject *o)
  284. {
  285. *L->stack.top = *o;
  286. incr_top;
  287. }
  288. void lua_pushobject (lua_Object o) {
  289. if (o == LUA_NOOBJECT)
  290. lua_error("API error - attempt to push a NOOBJECT");
  291. set_normalized(L->stack.top, Address(o));
  292. incr_top;
  293. }
  294. int lua_tag (lua_Object lo)
  295. {
  296. if (lo == LUA_NOOBJECT)
  297. return LUA_T_NIL;
  298. else {
  299. TObject *o = Address(lo);
  300. int t;
  301. switch (t = ttype(o)) {
  302. case LUA_T_USERDATA:
  303. return o->value.ts->u.d.tag;
  304. case LUA_T_ARRAY:
  305. return o->value.a->htag;
  306. case LUA_T_PMARK:
  307. return LUA_T_PROTO;
  308. case LUA_T_CMARK:
  309. return LUA_T_CPROTO;
  310. case LUA_T_CLOSURE: case LUA_T_CLMARK:
  311. return o->value.cl->consts[0].ttype;
  312. #ifdef DEBUG
  313. case LUA_T_LINE:
  314. LUA_INTERNALERROR("invalid type");
  315. #endif
  316. default:
  317. return t;
  318. }
  319. }
  320. }
  321. void lua_settag (int tag)
  322. {
  323. checkCparams(1);
  324. luaT_realtag(tag);
  325. switch (ttype(L->stack.top-1)) {
  326. case LUA_T_ARRAY:
  327. (L->stack.top-1)->value.a->htag = tag;
  328. break;
  329. case LUA_T_USERDATA:
  330. (L->stack.top-1)->value.ts->u.d.tag = tag;
  331. break;
  332. default:
  333. luaL_verror("cannot change the tag of a %.20s",
  334. luaO_typename(L->stack.top-1));
  335. }
  336. L->stack.top--;
  337. }
  338. TaggedString *luaA_nextvar (TaggedString *g) {
  339. if (g == NULL)
  340. g = (TaggedString *)L->rootglobal.next; /* first variable */
  341. else {
  342. /* check whether name is in global var list */
  343. luaL_arg_check((GCnode *)g != g->head.next, 1, "variable name expected");
  344. g = (TaggedString *)g->head.next; /* get next */
  345. }
  346. while (g && g->u.s.globalval.ttype == LUA_T_NIL) /* skip globals with nil */
  347. g = (TaggedString *)g->head.next;
  348. if (g) {
  349. ttype(L->stack.top) = LUA_T_STRING; tsvalue(L->stack.top) = g;
  350. incr_top;
  351. luaA_pushobject(&g->u.s.globalval);
  352. }
  353. return g;
  354. }
  355. char *lua_nextvar (char *varname) {
  356. TaggedString *g = (varname == NULL) ? NULL : luaS_new(varname);
  357. g = luaA_nextvar(g);
  358. if (g) {
  359. top2LC(2);
  360. return g->str;
  361. }
  362. else {
  363. top2LC(0);
  364. return NULL;
  365. }
  366. }
  367. int luaA_next (Hash *t, int i) {
  368. int tsize = nhash(t);
  369. for (; i<tsize; i++) {
  370. Node *n = node(t, i);
  371. if (ttype(val(n)) != LUA_T_NIL) {
  372. luaA_pushobject(ref(n));
  373. luaA_pushobject(val(n));
  374. return i+1; /* index to be used next time */
  375. }
  376. }
  377. return 0; /* no more elements */
  378. }
  379. int lua_next (lua_Object o, int i) {
  380. TObject *t = Address(o);
  381. if (ttype(t) != LUA_T_ARRAY)
  382. lua_error("API error - object is not a table in `lua_next'");
  383. i = luaA_next(avalue(t), i);
  384. top2LC((i==0) ? 0 : 2);
  385. return i;
  386. }
  387. /*
  388. ** {======================================================
  389. ** To manipulate some state information
  390. ** =======================================================
  391. */
  392. lua_State *lua_setstate (lua_State *st) {
  393. lua_State *old = lua_state;
  394. lua_state = st;
  395. return old;
  396. }
  397. lua_LHFunction lua_setlinehook (lua_LHFunction func) {
  398. lua_LHFunction old = L->linehook;
  399. L->linehook = func;
  400. return old;
  401. }
  402. lua_CHFunction lua_setcallhook (lua_CHFunction func) {
  403. lua_CHFunction old = L->callhook;
  404. L->callhook = func;
  405. return old;
  406. }
  407. int lua_setdebug (int debug) {
  408. int old = L->debug;
  409. L->debug = debug;
  410. return old;
  411. }
  412. /* }====================================================== */
  413. /*
  414. ** {======================================================
  415. ** Debug interface
  416. ** =======================================================
  417. */
  418. lua_Function lua_stackedfunction (int level)
  419. {
  420. StkId i;
  421. for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--) {
  422. int t = L->stack.stack[i].ttype;
  423. if (t == LUA_T_CLMARK || t == LUA_T_PMARK || t == LUA_T_CMARK)
  424. if (level-- == 0)
  425. return Ref(L->stack.stack+i);
  426. }
  427. return LUA_NOOBJECT;
  428. }
  429. int lua_nups (lua_Function func) {
  430. TObject *o = luaA_Address(func);
  431. return (!o || normalized_type(o) != LUA_T_CLOSURE) ? 0 : o->value.cl->nelems;
  432. }
  433. int lua_currentline (lua_Function func)
  434. {
  435. TObject *f = Address(func);
  436. return (f+1 < L->stack.top && (f+1)->ttype == LUA_T_LINE) ?
  437. (f+1)->value.i : -1;
  438. }
  439. lua_Object lua_getlocal (lua_Function func, int local_number, char **name) {
  440. /* check whether func is a Lua function */
  441. if (lua_tag(func) != LUA_T_PROTO)
  442. return LUA_NOOBJECT;
  443. else {
  444. TObject *f = Address(func);
  445. TProtoFunc *fp = luaA_protovalue(f)->value.tf;
  446. *name = luaF_getlocalname(fp, local_number, lua_currentline(func));
  447. if (*name) {
  448. /* if "*name", there must be a LUA_T_LINE */
  449. /* therefore, f+2 points to function base */
  450. return put_luaObject((f+2)+(local_number-1));
  451. }
  452. else
  453. return LUA_NOOBJECT;
  454. }
  455. }
  456. int lua_setlocal (lua_Function func, int local_number)
  457. {
  458. /* check whether func is a Lua function */
  459. if (lua_tag(func) != LUA_T_PROTO)
  460. return 0;
  461. else {
  462. TObject *f = Address(func);
  463. TProtoFunc *fp = luaA_protovalue(f)->value.tf;
  464. char *name = luaF_getlocalname(fp, local_number, lua_currentline(func));
  465. checkCparams(1);
  466. --L->stack.top;
  467. if (name) {
  468. /* if "name", there must be a LUA_T_LINE */
  469. /* therefore, f+2 points to function base */
  470. *((f+2)+(local_number-1)) = *L->stack.top;
  471. return 1;
  472. }
  473. else
  474. return 0;
  475. }
  476. }
  477. void lua_funcinfo (lua_Object func, char **source, int *linedefined) {
  478. if (!lua_isfunction(func))
  479. lua_error("API error - `funcinfo' called with a non-function value");
  480. else {
  481. TObject *f = luaA_protovalue(Address(func));
  482. if (normalized_type(f) == LUA_T_PROTO) {
  483. *source = tfvalue(f)->source->str;
  484. *linedefined = tfvalue(f)->lineDefined;
  485. }
  486. else {
  487. *source = "(C)";
  488. *linedefined = -1;
  489. }
  490. }
  491. }
  492. static int checkfunc (TObject *o)
  493. {
  494. return luaO_equalObj(o, L->stack.top);
  495. }
  496. char *lua_getobjname (lua_Object o, char **name)
  497. { /* try to find a name for given function */
  498. set_normalized(L->stack.top, Address(o)); /* to be accessed by "checkfunc" */
  499. if ((*name = luaS_travsymbol(checkfunc)) != NULL)
  500. return "global";
  501. else if ((*name = luaT_travtagmethods(checkfunc)) != NULL)
  502. return "tag-method";
  503. else return "";
  504. }
  505. /* }====================================================== */
  506. /*
  507. ** {======================================================
  508. ** BLOCK mechanism
  509. ** =======================================================
  510. */
  511. #ifndef MAX_C_BLOCKS
  512. #define MAX_C_BLOCKS 1000 /* arbitrary limit */
  513. #endif
  514. void lua_beginblock (void) {
  515. luaM_growvector(L->Cblocks, L->numCblocks, 1, struct C_Lua_Stack,
  516. "too many nested blocks", MAX_C_BLOCKS);
  517. L->Cblocks[L->numCblocks] = L->Cstack;
  518. L->numCblocks++;
  519. }
  520. void lua_endblock (void) {
  521. --L->numCblocks;
  522. L->Cstack = L->Cblocks[L->numCblocks];
  523. luaD_adjusttop(L->Cstack.base);
  524. }
  525. int lua_ref (int lock) {
  526. int ref;
  527. checkCparams(1);
  528. ref = luaC_ref(L->stack.top-1, lock);
  529. L->stack.top--;
  530. return ref;
  531. }
  532. lua_Object lua_getref (int ref) {
  533. TObject *o = luaC_getref(ref);
  534. return (o ? put_luaObject(o) : LUA_NOOBJECT);
  535. }
  536. /* }====================================================== */
  537. #ifdef LUA_COMPAT2_5
  538. /*
  539. ** API: set a function as a fallback
  540. */
  541. static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults) {
  542. luaD_openstack(nParams);
  543. (L->stack.top-nParams)->ttype = LUA_T_CPROTO;
  544. (L->stack.top-nParams)->value.f = f;
  545. luaD_calln(nParams, nResults);
  546. }
  547. lua_Object lua_setfallback (char *name, lua_CFunction fallback) {
  548. lua_pushstring(name);
  549. lua_pushcfunction(fallback);
  550. do_unprotectedrun(luaT_setfallback, 2, 1);
  551. return put_luaObjectonTop();
  552. }
  553. #endif