lapi.c 12 KB

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