lapi.c 16 KB

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