lapi.c 10 KB

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