lapi.c 14 KB

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