lapi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /*
  2. ** $Id: lapi.c,v 1.47 1999/06/22 20:37:23 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. 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. static lua_Object put_luaObjectonTop (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 put_luaObjectonTop();
  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 put_luaObjectonTop();
  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 put_luaObjectonTop();
  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 put_luaObjectonTop();
  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 put_luaObjectonTop();
  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. int lua_isnil (lua_Object o) {
  162. return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_NIL);
  163. }
  164. int lua_istable (lua_Object o) {
  165. return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_ARRAY);
  166. }
  167. int lua_isuserdata (lua_Object o) {
  168. return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_USERDATA);
  169. }
  170. int lua_iscfunction (lua_Object o) {
  171. return (lua_tag(o) == LUA_T_CPROTO);
  172. }
  173. int lua_isnumber (lua_Object o) {
  174. return (o!= LUA_NOOBJECT) && (tonumber(Address(o)) == 0);
  175. }
  176. int lua_isstring (lua_Object o) {
  177. int t = lua_tag(o);
  178. return (t == LUA_T_STRING) || (t == LUA_T_NUMBER);
  179. }
  180. int lua_isfunction (lua_Object o) {
  181. int t = lua_tag(o);
  182. return (t == LUA_T_PROTO) || (t == LUA_T_CPROTO);
  183. }
  184. double lua_getnumber (lua_Object object) {
  185. if (object == LUA_NOOBJECT) return 0.0;
  186. if (tonumber(Address(object))) return 0.0;
  187. else return (nvalue(Address(object)));
  188. }
  189. const char *lua_getstring (lua_Object object) {
  190. luaC_checkGC(); /* "tostring" may create a new string */
  191. if (object == LUA_NOOBJECT || tostring(Address(object)))
  192. return NULL;
  193. else return (svalue(Address(object)));
  194. }
  195. long lua_strlen (lua_Object object) {
  196. luaC_checkGC(); /* "tostring" may create a new string */
  197. if (object == LUA_NOOBJECT || tostring(Address(object)))
  198. return 0L;
  199. else return (tsvalue(Address(object))->u.s.len);
  200. }
  201. void *lua_getuserdata (lua_Object object) {
  202. if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
  203. return NULL;
  204. else return tsvalue(Address(object))->u.d.v;
  205. }
  206. lua_CFunction lua_getcfunction (lua_Object object) {
  207. if (!lua_iscfunction(object))
  208. return NULL;
  209. else return fvalue(luaA_protovalue(Address(object)));
  210. }
  211. void lua_pushnil (void) {
  212. ttype(L->stack.top) = LUA_T_NIL;
  213. incr_top;
  214. }
  215. void lua_pushnumber (double n) {
  216. ttype(L->stack.top) = LUA_T_NUMBER;
  217. nvalue(L->stack.top) = n;
  218. incr_top;
  219. }
  220. void lua_pushlstring (const char *s, long len) {
  221. tsvalue(L->stack.top) = luaS_newlstr(s, len);
  222. ttype(L->stack.top) = LUA_T_STRING;
  223. incr_top;
  224. luaC_checkGC();
  225. }
  226. void lua_pushstring (const char *s) {
  227. if (s == NULL)
  228. lua_pushnil();
  229. else
  230. lua_pushlstring(s, strlen(s));
  231. }
  232. void lua_pushcclosure (lua_CFunction fn, int n) {
  233. if (fn == NULL)
  234. lua_error("API error - attempt to push a NULL Cfunction");
  235. checkCparams(n);
  236. ttype(L->stack.top) = LUA_T_CPROTO;
  237. fvalue(L->stack.top) = fn;
  238. incr_top;
  239. luaV_closure(n);
  240. luaC_checkGC();
  241. }
  242. void lua_pushusertag (void *u, int tag) {
  243. if (tag < 0 && tag != LUA_ANYTAG)
  244. luaT_realtag(tag); /* error if tag is not valid */
  245. tsvalue(L->stack.top) = luaS_createudata(u, tag);
  246. ttype(L->stack.top) = LUA_T_USERDATA;
  247. incr_top;
  248. luaC_checkGC();
  249. }
  250. void luaA_pushobject (const TObject *o) {
  251. *L->stack.top = *o;
  252. incr_top;
  253. }
  254. void lua_pushobject (lua_Object o) {
  255. if (o == LUA_NOOBJECT)
  256. lua_error("API error - attempt to push a NOOBJECT");
  257. set_normalized(L->stack.top, Address(o));
  258. incr_top;
  259. }
  260. int lua_tag (lua_Object lo) {
  261. if (lo == LUA_NOOBJECT)
  262. return LUA_T_NIL;
  263. else {
  264. const TObject *o = Address(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("invalid type");
  280. #endif
  281. default:
  282. return t;
  283. }
  284. }
  285. }
  286. void lua_settag (int tag) {
  287. checkCparams(1);
  288. luaT_realtag(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("cannot change the tag of a %.20s",
  298. luaO_typename(L->stack.top-1));
  299. }
  300. L->stack.top--;
  301. }
  302. TaggedString *luaA_nextvar (TaggedString *g) {
  303. if (g == NULL)
  304. g = (TaggedString *)L->rootglobal.next; /* first variable */
  305. else {
  306. /* check whether name is in global var list */
  307. luaL_arg_check((GCnode *)g != g->head.next, 1, "variable name expected");
  308. g = (TaggedString *)g->head.next; /* get next */
  309. }
  310. while (g && g->u.s.globalval.ttype == LUA_T_NIL) /* skip globals with nil */
  311. g = (TaggedString *)g->head.next;
  312. if (g) {
  313. ttype(L->stack.top) = LUA_T_STRING; tsvalue(L->stack.top) = g;
  314. incr_top;
  315. luaA_pushobject(&g->u.s.globalval);
  316. }
  317. return g;
  318. }
  319. const char *lua_nextvar (const char *varname) {
  320. TaggedString *g = (varname == NULL) ? NULL : luaS_new(varname);
  321. g = luaA_nextvar(g);
  322. if (g) {
  323. top2LC(2);
  324. return g->str;
  325. }
  326. else {
  327. top2LC(0);
  328. return NULL;
  329. }
  330. }
  331. int luaA_next (const Hash *t, int i) {
  332. int tsize = nhash(t);
  333. for (; i<tsize; i++) {
  334. Node *n = node(t, i);
  335. if (ttype(val(n)) != LUA_T_NIL) {
  336. luaA_pushobject(ref(n));
  337. luaA_pushobject(val(n));
  338. return i+1; /* index to be used next time */
  339. }
  340. }
  341. return 0; /* no more elements */
  342. }
  343. int lua_next (lua_Object o, int i) {
  344. const TObject *t = Address(o);
  345. if (ttype(t) != LUA_T_ARRAY)
  346. lua_error("API error - object is not a table in `lua_next'");
  347. i = luaA_next(avalue(t), i);
  348. top2LC((i==0) ? 0 : 2);
  349. return i;
  350. }
  351. /*
  352. ** {======================================================
  353. ** To manipulate some state information
  354. ** =======================================================
  355. */
  356. lua_State *lua_setstate (lua_State *st) {
  357. lua_State *old = lua_state;
  358. lua_state = st;
  359. return old;
  360. }
  361. lua_LHFunction lua_setlinehook (lua_LHFunction func) {
  362. lua_LHFunction old = L->linehook;
  363. L->linehook = func;
  364. return old;
  365. }
  366. lua_CHFunction lua_setcallhook (lua_CHFunction func) {
  367. lua_CHFunction old = L->callhook;
  368. L->callhook = func;
  369. return old;
  370. }
  371. int lua_setdebug (int debug) {
  372. int old = L->debug;
  373. L->debug = debug;
  374. return old;
  375. }
  376. /* }====================================================== */
  377. /*
  378. ** {======================================================
  379. ** Debug interface
  380. ** =======================================================
  381. */
  382. lua_Function lua_stackedfunction (int level) {
  383. StkId i;
  384. for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--) {
  385. int t = L->stack.stack[i].ttype;
  386. if (t == LUA_T_CLMARK || t == LUA_T_PMARK || t == LUA_T_CMARK)
  387. if (level-- == 0)
  388. return Ref(L->stack.stack+i);
  389. }
  390. return LUA_NOOBJECT;
  391. }
  392. int lua_nups (lua_Function func) {
  393. const TObject *o = luaA_Address(func);
  394. return (!o || normalized_type(o) != LUA_T_CLOSURE) ? 0 : o->value.cl->nelems;
  395. }
  396. int lua_currentline (lua_Function func) {
  397. const TObject *f = Address(func);
  398. return (f+1 < L->stack.top && (f+1)->ttype == LUA_T_LINE) ?
  399. (f+1)->value.i : -1;
  400. }
  401. lua_Object lua_getlocal (lua_Function func, int local_number,
  402. const char **name) {
  403. /* check whether func is a Lua function */
  404. if (lua_tag(func) != LUA_T_PROTO)
  405. return LUA_NOOBJECT;
  406. else {
  407. TObject *f = Address(func);
  408. TProtoFunc *fp = luaA_protovalue(f)->value.tf;
  409. *name = luaF_getlocalname(fp, local_number, lua_currentline(func));
  410. if (*name) {
  411. /* if "*name", there must be a LUA_T_LINE */
  412. /* therefore, f+2 points to function base */
  413. return put_luaObject((f+2)+(local_number-1));
  414. }
  415. else
  416. return LUA_NOOBJECT;
  417. }
  418. }
  419. int lua_setlocal (lua_Function func, int local_number) {
  420. /* check whether func is a Lua function */
  421. if (lua_tag(func) != LUA_T_PROTO)
  422. return 0;
  423. else {
  424. TObject *f = Address(func);
  425. TProtoFunc *fp = luaA_protovalue(f)->value.tf;
  426. const char *name = luaF_getlocalname(fp, local_number, lua_currentline(func));
  427. checkCparams(1);
  428. --L->stack.top;
  429. if (name) {
  430. /* if "name", there must be a LUA_T_LINE */
  431. /* therefore, f+2 points to function base */
  432. *((f+2)+(local_number-1)) = *L->stack.top;
  433. return 1;
  434. }
  435. else
  436. return 0;
  437. }
  438. }
  439. void lua_funcinfo (lua_Object func, const char **source, int *linedefined) {
  440. if (!lua_isfunction(func))
  441. lua_error("API error - `funcinfo' called with a non-function value");
  442. else {
  443. const TObject *f = luaA_protovalue(Address(func));
  444. if (normalized_type(f) == LUA_T_PROTO) {
  445. *source = tfvalue(f)->source->str;
  446. *linedefined = tfvalue(f)->lineDefined;
  447. }
  448. else {
  449. *source = "(C)";
  450. *linedefined = -1;
  451. }
  452. }
  453. }
  454. static int checkfunc (TObject *o) {
  455. return luaO_equalObj(o, L->stack.top);
  456. }
  457. const char *lua_getobjname (lua_Object o, const char **name) {
  458. /* try to find a name for given function */
  459. set_normalized(L->stack.top, Address(o)); /* to be accessed by "checkfunc" */
  460. if ((*name = luaS_travsymbol(checkfunc)) != NULL)
  461. return "global";
  462. else if ((*name = luaT_travtagmethods(checkfunc)) != NULL)
  463. return "tag-method";
  464. else return "";
  465. }
  466. /* }====================================================== */
  467. /*
  468. ** {======================================================
  469. ** BLOCK mechanism
  470. ** =======================================================
  471. */
  472. #ifndef MAX_C_BLOCKS
  473. #define MAX_C_BLOCKS 1000 /* arbitrary limit */
  474. #endif
  475. void lua_beginblock (void) {
  476. luaM_growvector(L->Cblocks, L->numCblocks, 1, struct C_Lua_Stack,
  477. "too many nested blocks", MAX_C_BLOCKS);
  478. L->Cblocks[L->numCblocks] = L->Cstack;
  479. L->numCblocks++;
  480. }
  481. void lua_endblock (void) {
  482. --L->numCblocks;
  483. L->Cstack = L->Cblocks[L->numCblocks];
  484. luaD_adjusttop(L->Cstack.base);
  485. }
  486. int lua_ref (int lock) {
  487. int ref;
  488. checkCparams(1);
  489. ref = luaC_ref(L->stack.top-1, lock);
  490. L->stack.top--;
  491. return ref;
  492. }
  493. lua_Object lua_getref (int ref) {
  494. const TObject *o = luaC_getref(ref);
  495. return (o ? put_luaObject(o) : LUA_NOOBJECT);
  496. }
  497. /* }====================================================== */
  498. #ifdef LUA_COMPAT2_5
  499. /*
  500. ** API: set a function as a fallback
  501. */
  502. static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults) {
  503. luaD_openstack(nParams);
  504. (L->stack.top-nParams)->ttype = LUA_T_CPROTO;
  505. (L->stack.top-nParams)->value.f = f;
  506. luaD_calln(nParams, nResults);
  507. }
  508. lua_Object lua_setfallback (char *name, lua_CFunction fallback) {
  509. lua_pushstring(name);
  510. lua_pushcfunction(fallback);
  511. do_unprotectedrun(luaT_setfallback, 2, 1);
  512. return put_luaObjectonTop();
  513. }
  514. #endif