lapi.c 9.6 KB

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