lapi.c 9.1 KB

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