lapi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. ** $Id: lapi.c,v 1.65 1999/12/23 18:19:57 roberto Exp roberto $
  3. ** Lua API
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <string.h>
  7. #define LUA_REENTRANT
  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 "lref.h"
  16. #include "lstate.h"
  17. #include "lstring.h"
  18. #include "ltable.h"
  19. #include "ltm.h"
  20. #include "lua.h"
  21. #include "lvm.h"
  22. const char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
  23. "$Authors: " LUA_AUTHORS " $";
  24. const lua_Type luaA_normtype[] = { /* ORDER LUA_T */
  25. LUA_T_USERDATA, LUA_T_NUMBER, LUA_T_STRING, LUA_T_ARRAY,
  26. LUA_T_LPROTO, LUA_T_CPROTO, LUA_T_NIL,
  27. LUA_T_LCLOSURE, LUA_T_CCLOSURE,
  28. LUA_T_LCLOSURE, LUA_T_CCLOSURE, /* LUA_T_LCLMARK, LUA_T_CCLMARK */
  29. LUA_T_LPROTO, LUA_T_CPROTO /* LUA_T_LMARK, LUA_T_CMARK */
  30. };
  31. void luaA_setnormalized (TObject *d, const TObject *s) {
  32. d->value = s->value;
  33. d->ttype = luaA_normalizedtype(s);
  34. }
  35. const TObject *luaA_protovalue (const TObject *o) {
  36. switch (luaA_normalizedtype(o)) {
  37. case LUA_T_CCLOSURE: case LUA_T_LCLOSURE:
  38. return protovalue(o);
  39. default:
  40. LUA_ASSERT(L, luaA_normalizedtype(o) == LUA_T_LPROTO ||
  41. luaA_normalizedtype(o) == LUA_T_CPROTO,
  42. "invalid `function'");
  43. return o;
  44. }
  45. }
  46. void luaA_checkCparams (lua_State *L, int nParams) {
  47. if (nParams > L->top-L->Cstack.base)
  48. lua_error(L, "API error - wrong number of arguments in C2lua stack");
  49. }
  50. lua_Object luaA_putluaObject (lua_State *L, const TObject *o) {
  51. luaD_openstack(L, L->Cstack.base);
  52. *L->Cstack.base++ = *o;
  53. return L->Cstack.base-1;
  54. }
  55. lua_Object luaA_putObjectOnTop (lua_State *L) {
  56. luaD_openstack(L, L->Cstack.base);
  57. *L->Cstack.base++ = *(--L->top);
  58. return L->Cstack.base-1;
  59. }
  60. static void top2LC (lua_State *L, int n) {
  61. /* Put the `n' elements on the top as the Lua2C contents */
  62. L->Cstack.base = L->top; /* new base */
  63. L->Cstack.lua2C = L->Cstack.base-n; /* position of the new results */
  64. L->Cstack.num = n; /* number of results */
  65. }
  66. lua_Object lua_pop (lua_State *L) {
  67. luaA_checkCparams(L, 1);
  68. return luaA_putObjectOnTop(L);
  69. }
  70. /*
  71. ** Get a parameter, returning the object handle or LUA_NOOBJECT on error.
  72. ** `number' must be 1 to get the first parameter.
  73. */
  74. lua_Object lua_lua2C (lua_State *L, int number) {
  75. if (number <= 0 || number > L->Cstack.num) return LUA_NOOBJECT;
  76. return L->Cstack.lua2C+number-1;
  77. }
  78. int lua_callfunction (lua_State *L, lua_Object function) {
  79. if (function == LUA_NOOBJECT)
  80. return 1;
  81. else {
  82. luaD_openstack(L, L->Cstack.base);
  83. luaA_setnormalized(L->Cstack.base, function);
  84. return luaD_protectedrun(L);
  85. }
  86. }
  87. lua_Object lua_gettagmethod (lua_State *L, int tag, const char *event) {
  88. return luaA_putluaObject(L, luaT_gettagmethod(L, tag, event));
  89. }
  90. lua_Object lua_settagmethod (lua_State *L, int tag, const char *event) {
  91. luaA_checkCparams(L, 1);
  92. luaT_settagmethod(L, tag, event, L->top-1);
  93. return luaA_putObjectOnTop(L);
  94. }
  95. lua_Object lua_seterrormethod (lua_State *L) {
  96. lua_Object temp;
  97. luaA_checkCparams(L, 1);
  98. temp = lua_getglobal(L, "_ERRORMESSAGE");
  99. lua_setglobal(L, "_ERRORMESSAGE");
  100. return temp;
  101. }
  102. lua_Object lua_gettable (lua_State *L) {
  103. luaA_checkCparams(L, 2);
  104. luaV_gettable(L);
  105. return luaA_putObjectOnTop(L);
  106. }
  107. lua_Object lua_rawgettable (lua_State *L) {
  108. luaA_checkCparams(L, 2);
  109. if (ttype(L->top-2) != LUA_T_ARRAY)
  110. lua_error(L, "indexed expression not a table in rawgettable");
  111. *(L->top-2) = *luaH_get(L, avalue(L->top-2), L->top-1);
  112. --L->top;
  113. return luaA_putObjectOnTop(L);
  114. }
  115. void lua_settable (lua_State *L) {
  116. luaA_checkCparams(L, 3);
  117. luaV_settable(L, L->top-3);
  118. L->top -= 2; /* pop table and index */
  119. }
  120. void lua_rawsettable (lua_State *L) {
  121. luaA_checkCparams(L, 3);
  122. luaV_rawsettable(L, L->top-3);
  123. }
  124. lua_Object lua_createtable (lua_State *L) {
  125. TObject o;
  126. luaC_checkGC(L);
  127. avalue(&o) = luaH_new(L, 0);
  128. ttype(&o) = LUA_T_ARRAY;
  129. return luaA_putluaObject(L, &o);
  130. }
  131. lua_Object lua_getglobal (lua_State *L, const char *name) {
  132. luaD_checkstack(L, 2); /* may need that to call a tag method */
  133. luaV_getglobal(L, luaS_assertglobalbyname(L, name));
  134. return luaA_putObjectOnTop(L);
  135. }
  136. lua_Object lua_rawgetglobal (lua_State *L, const char *name) {
  137. GlobalVar *gv = luaS_assertglobalbyname(L, name);
  138. return luaA_putluaObject(L, &gv->value);
  139. }
  140. void lua_setglobal (lua_State *L, const char *name) {
  141. luaA_checkCparams(L, 1);
  142. luaD_checkstack(L, 2); /* may need that to call a tag method */
  143. luaV_setglobal(L, luaS_assertglobalbyname(L, name));
  144. }
  145. void lua_rawsetglobal (lua_State *L, const char *name) {
  146. GlobalVar *gv = luaS_assertglobalbyname(L, name);
  147. luaA_checkCparams(L, 1);
  148. gv->value = *(--L->top);
  149. }
  150. const char *lua_type (lua_State *L, lua_Object o) {
  151. UNUSED(L);
  152. return (o == LUA_NOOBJECT) ? "NOOBJECT" : luaO_typename(o);
  153. }
  154. int lua_isnil (lua_State *L, lua_Object o) {
  155. UNUSED(L);
  156. return (o != LUA_NOOBJECT) && (ttype(o) == LUA_T_NIL);
  157. }
  158. int lua_istable (lua_State *L, lua_Object o) {
  159. UNUSED(L);
  160. return (o != LUA_NOOBJECT) && (ttype(o) == LUA_T_ARRAY);
  161. }
  162. int lua_isuserdata (lua_State *L, lua_Object o) {
  163. UNUSED(L);
  164. return (o != LUA_NOOBJECT) && (ttype(o) == LUA_T_USERDATA);
  165. }
  166. int lua_iscfunction (lua_State *L, lua_Object o) {
  167. return (lua_tag(L, o) == LUA_T_CPROTO);
  168. }
  169. int lua_isnumber (lua_State *L, lua_Object o) {
  170. UNUSED(L);
  171. return (o != LUA_NOOBJECT) && (tonumber(o) == 0);
  172. }
  173. int lua_isstring (lua_State *L, lua_Object o) {
  174. UNUSED(L);
  175. return (o != LUA_NOOBJECT && (ttype(o) == LUA_T_STRING ||
  176. ttype(o) == LUA_T_NUMBER));
  177. }
  178. int lua_isfunction (lua_State *L, lua_Object o) {
  179. return *lua_type(L, o) == 'f';
  180. }
  181. int lua_equal(lua_State *L, lua_Object o1, lua_Object o2) {
  182. UNUSED(L);
  183. if (o1 == LUA_NOOBJECT || o2 == LUA_NOOBJECT)
  184. return (o1 == o2);
  185. else {
  186. TObject obj1, obj2;
  187. luaA_setnormalized(&obj1, o1);
  188. luaA_setnormalized(&obj2, o2);
  189. return luaO_equalObj(&obj1, &obj2);
  190. }
  191. }
  192. double lua_getnumber (lua_State *L, lua_Object obj) {
  193. UNUSED(L);
  194. if (obj == LUA_NOOBJECT || tonumber(obj))
  195. return 0.0;
  196. else return (nvalue(obj));
  197. }
  198. const char *lua_getstring (lua_State *L, lua_Object obj) {
  199. luaC_checkGC(L); /* `tostring' may create a new string */
  200. if (obj == LUA_NOOBJECT || tostring(L, obj))
  201. return NULL;
  202. else return (svalue(obj));
  203. }
  204. long lua_strlen (lua_State *L, lua_Object obj) {
  205. if (obj == LUA_NOOBJECT || tostring(L, obj))
  206. return 0L;
  207. else return (tsvalue(obj)->u.s.len);
  208. }
  209. void *lua_getuserdata (lua_State *L, lua_Object obj) {
  210. UNUSED(L);
  211. if (obj == LUA_NOOBJECT || ttype(obj) != LUA_T_USERDATA)
  212. return NULL;
  213. else return tsvalue(obj)->u.d.value;
  214. }
  215. lua_CFunction lua_getcfunction (lua_State *L, lua_Object obj) {
  216. if (!lua_iscfunction(L, obj))
  217. return NULL;
  218. else return fvalue(luaA_protovalue(obj));
  219. }
  220. void lua_pushnil (lua_State *L) {
  221. ttype(L->top) = LUA_T_NIL;
  222. incr_top;
  223. }
  224. void lua_pushnumber (lua_State *L, double n) {
  225. ttype(L->top) = LUA_T_NUMBER;
  226. nvalue(L->top) = n;
  227. incr_top;
  228. }
  229. void lua_pushlstring (lua_State *L, const char *s, long len) {
  230. tsvalue(L->top) = luaS_newlstr(L, s, len);
  231. ttype(L->top) = LUA_T_STRING;
  232. incr_top;
  233. luaC_checkGC(L);
  234. }
  235. void lua_pushstring (lua_State *L, const char *s) {
  236. if (s == NULL)
  237. lua_pushnil(L);
  238. else
  239. lua_pushlstring(L, s, strlen(s));
  240. }
  241. void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  242. if (fn == NULL)
  243. lua_error(L, "API error - attempt to push a NULL Cfunction");
  244. luaA_checkCparams(L, n);
  245. ttype(L->top) = LUA_T_CPROTO;
  246. fvalue(L->top) = fn;
  247. incr_top;
  248. luaV_closure(L, n);
  249. luaC_checkGC(L);
  250. }
  251. void lua_pushusertag (lua_State *L, void *u, int tag) {
  252. if (tag < 0 && tag != LUA_ANYTAG)
  253. luaT_realtag(L, tag); /* error if tag is not valid */
  254. tsvalue(L->top) = luaS_createudata(L, u, tag);
  255. ttype(L->top) = LUA_T_USERDATA;
  256. incr_top;
  257. luaC_checkGC(L);
  258. }
  259. void luaA_pushobject (lua_State *L, const TObject *o) {
  260. *L->top = *o;
  261. incr_top;
  262. }
  263. void lua_pushobject (lua_State *L, lua_Object o) {
  264. if (o == LUA_NOOBJECT)
  265. lua_error(L, "API error - attempt to push a NOOBJECT");
  266. luaA_setnormalized(L->top, o);
  267. incr_top;
  268. }
  269. int lua_tag (lua_State *L, lua_Object o) {
  270. UNUSED(L);
  271. if (o == LUA_NOOBJECT)
  272. return LUA_T_NIL;
  273. else if (ttype(o) == LUA_T_USERDATA) /* to allow `old' tags (deprecated) */
  274. return o->value.ts->u.d.tag;
  275. else
  276. return luaT_effectivetag(o);
  277. }
  278. void lua_settag (lua_State *L, int tag) {
  279. luaA_checkCparams(L, 1);
  280. luaT_realtag(L, tag);
  281. switch (ttype(L->top-1)) {
  282. case LUA_T_ARRAY:
  283. (L->top-1)->value.a->htag = tag;
  284. break;
  285. case LUA_T_USERDATA:
  286. (L->top-1)->value.ts->u.d.tag = tag;
  287. break;
  288. default:
  289. luaL_verror(L, "cannot change the tag of a %.20s",
  290. luaO_typename(L->top-1));
  291. }
  292. L->top--;
  293. }
  294. GlobalVar *luaA_nextvar (lua_State *L, TaggedString *ts) {
  295. GlobalVar *gv;
  296. if (ts == NULL)
  297. gv = L->rootglobal; /* first variable */
  298. else {
  299. /* check whether name is in global var list */
  300. luaL_arg_check(L, ts->u.s.gv, 1, "variable name expected");
  301. gv = ts->u.s.gv->next; /* get next */
  302. }
  303. while (gv && gv->value.ttype == LUA_T_NIL) /* skip globals with nil */
  304. gv = gv->next;
  305. if (gv) {
  306. ttype(L->top) = LUA_T_STRING; tsvalue(L->top) = gv->name;
  307. incr_top;
  308. luaA_pushobject(L, &gv->value);
  309. }
  310. return gv;
  311. }
  312. const char *lua_nextvar (lua_State *L, const char *varname) {
  313. TaggedString *ts = (varname == NULL) ? NULL : luaS_new(L, varname);
  314. GlobalVar *gv = luaA_nextvar(L, ts);
  315. if (gv) {
  316. top2LC(L, 2);
  317. return gv->name->str;
  318. }
  319. else {
  320. top2LC(L, 0);
  321. return NULL;
  322. }
  323. }
  324. int luaA_next (lua_State *L, const Hash *t, int i) {
  325. int tsize = t->size;
  326. for (; i<tsize; i++) {
  327. Node *n = node(t, i);
  328. if (ttype(val(n)) != LUA_T_NIL) {
  329. luaA_pushobject(L, key(n));
  330. luaA_pushobject(L, val(n));
  331. return i+1; /* index to be used next time */
  332. }
  333. }
  334. return 0; /* no more elements */
  335. }
  336. int lua_next (lua_State *L, lua_Object t, int i) {
  337. if (ttype(t) != LUA_T_ARRAY)
  338. lua_error(L, "API error - object is not a table in `lua_next'");
  339. i = luaA_next(L, avalue(t), i);
  340. top2LC(L, (i==0) ? 0 : 2);
  341. return i;
  342. }