lapi.c 15 KB

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