lapi.c 12 KB

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