lapi.c 13 KB

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