lapi.c 11 KB

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