lapi.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. ** $Id: lapi.c,v 1.89 2000/08/29 14:52:27 roberto Exp roberto $
  3. ** Lua API
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <string.h>
  7. #include "lua.h"
  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 "lstate.h"
  16. #include "lstring.h"
  17. #include "ltable.h"
  18. #include "ltm.h"
  19. #include "lvm.h"
  20. const char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
  21. "$Authors: " LUA_AUTHORS " $";
  22. #define Index(L,i) ((i) >= 0 ? (L->Cbase+((i)-1)) : (L->top+(i)))
  23. #define api_incr_top(L) (++L->top)
  24. TObject *luaA_index (lua_State *L, int index) {
  25. return Index(L, index);
  26. }
  27. void luaA_pushobject (lua_State *L, const TObject *o) {
  28. *L->top = *o;
  29. incr_top;
  30. }
  31. int lua_stackspace (lua_State *L) {
  32. return (L->stack_last - L->top);
  33. }
  34. /*
  35. ** basic stack manipulation
  36. */
  37. int lua_gettop (lua_State *L) {
  38. return (L->top - L->Cbase);
  39. }
  40. void lua_settop (lua_State *L, int index) {
  41. if (index >= 0)
  42. luaD_adjusttop(L, L->Cbase, index);
  43. else
  44. L->top += index; /* index is negative */
  45. }
  46. void lua_pushobject (lua_State *L, int index) {
  47. *L->top = *Index(L, index);
  48. api_incr_top(L);
  49. }
  50. /*
  51. ** access functions (stack -> C)
  52. */
  53. #define btest(L,i,value,default) { \
  54. StkId o; \
  55. if ((i) >= 0) { \
  56. o = L->Cbase+((i)-1); \
  57. if (o >= L->top) return (default); \
  58. } \
  59. else o = L->top+(i); \
  60. return (value); }
  61. #define access(L,i,test,default,value) { \
  62. StkId o; \
  63. if ((i) >= 0) { \
  64. o = L->Cbase+((i)-1); \
  65. if (o >= L->top) return (default); \
  66. } \
  67. else o = L->top+(i); \
  68. return ((test) ? (value) : (default)); }
  69. const char *lua_type (lua_State *L, int index) {
  70. btest(L, index, luaO_typename(o), "NO VALUE");
  71. }
  72. int lua_iscfunction (lua_State *L, int index) {
  73. btest(L, index, (ttype(o) == TAG_CCLOSURE), 0);
  74. }
  75. int lua_isnumber (lua_State *L, int index) {
  76. btest(L, index, (tonumber(Index(L, index)) == 0), 0);
  77. }
  78. int lua_tag (lua_State *L, int index) {
  79. btest(L, index,
  80. ((ttype(o) == TAG_USERDATA) ? tsvalue(o)->u.d.tag : luaT_effectivetag(L, o)),
  81. -1);
  82. }
  83. int lua_equal(lua_State *L, int index1, int index2) {
  84. StkId o1 = Index(L, index1);
  85. StkId o2 = Index(L, index2);
  86. if (o1 >= L->top || o2 >= L->top) return 0; /* index out-of-range */
  87. else return luaO_equalObj(o1, o2);
  88. }
  89. double lua_tonumber (lua_State *L, int index) {
  90. access(L, index, (tonumber(o) == 0), 0.0, nvalue(o));
  91. }
  92. const char *lua_tostring (lua_State *L, int index) {
  93. luaC_checkGC(L); /* `tostring' may create a new string */
  94. access(L, index, (tostring(L, o) == 0), NULL, svalue(o));
  95. }
  96. size_t lua_strlen (lua_State *L, int index) {
  97. access(L, index, (tostring(L, o) == 0), 0, tsvalue(o)->u.s.len);
  98. }
  99. lua_CFunction lua_tocfunction (lua_State *L, int index) {
  100. access(L, index, (ttype(o) == TAG_CCLOSURE), NULL, clvalue(o)->f.c);
  101. }
  102. void *lua_touserdata (lua_State *L, int index) {
  103. access(L, index, (ttype(o) == TAG_USERDATA), NULL, tsvalue(o)->u.d.value);
  104. }
  105. /*
  106. ** push functions (C -> stack)
  107. */
  108. void lua_pushnil (lua_State *L) {
  109. ttype(L->top) = TAG_NIL;
  110. api_incr_top(L);
  111. }
  112. void lua_pushnumber (lua_State *L, double n) {
  113. ttype(L->top) = TAG_NUMBER;
  114. nvalue(L->top) = n;
  115. api_incr_top(L);
  116. }
  117. void lua_pushlstring (lua_State *L, const char *s, size_t len) {
  118. luaC_checkGC(L);
  119. tsvalue(L->top) = luaS_newlstr(L, s, len);
  120. ttype(L->top) = TAG_STRING;
  121. api_incr_top(L);
  122. }
  123. void lua_pushstring (lua_State *L, const char *s) {
  124. if (s == NULL)
  125. lua_pushnil(L);
  126. else
  127. lua_pushlstring(L, s, strlen(s));
  128. }
  129. void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  130. luaC_checkGC(L);
  131. luaV_Cclosure(L, fn, n);
  132. }
  133. void lua_pushusertag (lua_State *L, void *u, int tag) { /* ORDER LUA_T */
  134. luaC_checkGC(L);
  135. if (tag != LUA_ANYTAG && tag != TAG_USERDATA && tag < NUM_TAGS)
  136. luaL_verror(L, "invalid tag for a userdata (%d)", tag);
  137. tsvalue(L->top) = luaS_createudata(L, u, tag);
  138. ttype(L->top) = TAG_USERDATA;
  139. api_incr_top(L);
  140. }
  141. /*
  142. ** get functions (Lua -> stack)
  143. */
  144. void lua_getglobal (lua_State *L, const char *name) {
  145. luaV_getglobal(L, luaS_new(L, name), L->top++);
  146. }
  147. void lua_gettable (lua_State *L) {
  148. luaV_gettable(L, L->top--);
  149. }
  150. void lua_rawget (lua_State *L) {
  151. if (ttype(L->top - 2) != TAG_TABLE)
  152. lua_error(L, "indexed expression not a table");
  153. *(L->top - 2) = *luaH_get(L, hvalue(L->top - 2), L->top - 1);
  154. L->top--;
  155. }
  156. void lua_getglobals (lua_State *L) {
  157. hvalue(L->top) = L->gt;
  158. ttype(L->top) = TAG_TABLE;
  159. api_incr_top(L);
  160. }
  161. void lua_gettagmethod (lua_State *L, int tag, const char *event) {
  162. *L->top = *luaT_gettagmethod(L, tag, event);
  163. api_incr_top(L);
  164. }
  165. int lua_getref (lua_State *L, int ref) {
  166. if (ref == LUA_REFNIL)
  167. ttype(L->top) = TAG_NIL;
  168. else if (0 <= ref && ref < L->refSize &&
  169. (L->refArray[ref].st == LOCK || L->refArray[ref].st == HOLD))
  170. *L->top = L->refArray[ref].o;
  171. else
  172. return 0;
  173. api_incr_top(L);
  174. return 1;
  175. }
  176. void lua_newtable (lua_State *L) {
  177. luaC_checkGC(L);
  178. hvalue(L->top) = luaH_new(L, 0);
  179. ttype(L->top) = TAG_TABLE;
  180. api_incr_top(L);
  181. }
  182. /*
  183. ** set functions (stack -> Lua)
  184. */
  185. void lua_setglobal (lua_State *L, const char *name) {
  186. luaV_setglobal(L, luaS_new(L, name), L->top--);
  187. }
  188. void lua_settable (lua_State *L) {
  189. StkId top = L->top;
  190. luaV_settable(L, top-3, top);
  191. L->top = top-3; /* pop table, index, and value */
  192. }
  193. void lua_rawset (lua_State *L) {
  194. if (ttype(L->top-3) != TAG_TABLE)
  195. lua_error(L, "indexed expression not a table");
  196. *luaH_set(L, hvalue(L->top-3), L->top-2) = *(L->top-1);
  197. L->top -= 3;
  198. }
  199. void lua_setglobals (lua_State *L) {
  200. TObject *newtable = --L->top;
  201. if (ttype(newtable) != TAG_TABLE)
  202. lua_error(L, "Lua API error - invalid value for global table");
  203. L->gt = hvalue(newtable);
  204. }
  205. void lua_settagmethod (lua_State *L, int tag, const char *event) {
  206. TObject *method = L->top - 1;
  207. if (ttype(method) != TAG_NIL &&
  208. ttype(method) != TAG_CCLOSURE &&
  209. ttype(method) != TAG_LCLOSURE)
  210. lua_error(L, "Lua API error - tag method must be a function or nil");
  211. luaT_settagmethod(L, tag, event, method);
  212. }
  213. int lua_ref (lua_State *L, int lock) {
  214. int ref;
  215. if (ttype(L->top-1) == TAG_NIL)
  216. ref = LUA_REFNIL;
  217. else {
  218. if (L->refFree != NONEXT) { /* is there a free place? */
  219. ref = L->refFree;
  220. L->refFree = L->refArray[ref].st;
  221. }
  222. else { /* no more free places */
  223. luaM_growvector(L, L->refArray, L->refSize, 1, struct Ref,
  224. "reference table overflow", MAX_INT);
  225. ref = L->refSize++;
  226. }
  227. L->refArray[ref].o = *(L->top-1);
  228. L->refArray[ref].st = lock ? LOCK : HOLD;
  229. }
  230. L->top--;
  231. return ref;
  232. }
  233. /*
  234. ** miscelaneous functions
  235. */
  236. void lua_settag (lua_State *L, int tag) {
  237. luaT_realtag(L, tag);
  238. switch (ttype(L->top-1)) {
  239. case TAG_TABLE:
  240. hvalue(L->top-1)->htag = tag;
  241. break;
  242. case TAG_USERDATA:
  243. tsvalue(L->top-1)->u.d.tag = tag;
  244. break;
  245. default:
  246. luaL_verror(L, "cannot change the tag of a %.20s",
  247. luaO_typename(L->top-1));
  248. }
  249. L->top--;
  250. }
  251. void lua_unref (lua_State *L, int ref) {
  252. if (ref >= 0) {
  253. if (ref >= L->refSize || L->refArray[ref].st >= 0)
  254. lua_error(L, "Lua API error - "
  255. "invalid argument for function `lua_unref'");
  256. L->refArray[ref].st = L->refFree;
  257. L->refFree = ref;
  258. }
  259. }
  260. int luaA_next (lua_State *L, const Hash *t, int i) {
  261. int tsize = t->size;
  262. for (; i<tsize; i++) {
  263. Node *n = node(t, i);
  264. if (ttype(val(n)) != TAG_NIL) {
  265. luaA_pushobject(L, key(n));
  266. luaA_pushobject(L, val(n));
  267. return i+1; /* index to be used next time */
  268. }
  269. }
  270. return 0; /* no more elements */
  271. }
  272. int lua_next (lua_State *L, int index, int i) {
  273. const TObject *t = Index(L, index);
  274. if (ttype(t) != TAG_TABLE)
  275. lua_error(L, "Lua API error - object is not a table in `lua_next'");
  276. return luaA_next(L, hvalue(t), i);
  277. }