lapi.c 14 KB

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