lapi.c 15 KB

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