lapi.c 9.3 KB

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