lapi.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. ** $Id: lapi.c,v 1.93 2000/08/31 21:01:43 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 = L->top+index+1; /* index is negative */
  45. }
  46. void lua_remove (lua_State *L, int index) {
  47. StkId p = Index(L, index);
  48. while (++p < L->top) *(p-1) = *p;
  49. L->top--;
  50. }
  51. void lua_insert (lua_State *L, int index) {
  52. TObject temp = *(L->top-1);
  53. StkId p = Index(L, index);
  54. StkId q;
  55. for (q = L->top-1; q>p; q--)
  56. *q = *(q-1);
  57. *p = temp;
  58. }
  59. void lua_pushvalue (lua_State *L, int index) {
  60. *L->top = *Index(L, index);
  61. api_incr_top(L);
  62. }
  63. /*
  64. ** access functions (stack -> C)
  65. */
  66. #define btest(L,i,value,default) { \
  67. StkId o; \
  68. if ((i) >= 0) { \
  69. o = L->Cbase+((i)-1); \
  70. if (o >= L->top) return (default); \
  71. } \
  72. else o = L->top+(i); \
  73. return (value); }
  74. #define access(L,i,test,default,value) { \
  75. StkId o; \
  76. if ((i) >= 0) { \
  77. o = L->Cbase+((i)-1); \
  78. if (o >= L->top) return (default); \
  79. } \
  80. else o = L->top+(i); \
  81. return ((test) ? (value) : (default)); }
  82. const char *lua_type (lua_State *L, int index) {
  83. btest(L, index, luaO_typename(o), "NO VALUE");
  84. }
  85. int lua_iscfunction (lua_State *L, int index) {
  86. btest(L, index, (ttype(o) == TAG_CCLOSURE), 0);
  87. }
  88. int lua_isnumber (lua_State *L, int index) {
  89. btest(L, index, (tonumber(Index(L, index)) == 0), 0);
  90. }
  91. int lua_tag (lua_State *L, int index) {
  92. btest(L, index,
  93. ((ttype(o) == TAG_USERDATA) ? tsvalue(o)->u.d.tag :
  94. luaT_effectivetag(L, o)), -1);
  95. }
  96. int lua_equal (lua_State *L, int index1, int index2) {
  97. StkId o1 = Index(L, index1);
  98. StkId o2 = Index(L, index2);
  99. if (o1 >= L->top || o2 >= L->top) return 0; /* index out-of-range */
  100. else return luaO_equalObj(o1, o2);
  101. }
  102. int lua_lessthan (lua_State *L, int index1, int index2) {
  103. StkId o1 = Index(L, index1);
  104. StkId o2 = Index(L, index2);
  105. if (o1 >= L->top || o2 >= L->top) return 0; /* index out-of-range */
  106. else return luaV_lessthan(L, o1, o2, L->top);
  107. }
  108. double lua_tonumber (lua_State *L, int index) {
  109. access(L, index, (tonumber(o) == 0), 0.0, nvalue(o));
  110. }
  111. const char *lua_tostring (lua_State *L, int index) {
  112. luaC_checkGC(L); /* `tostring' may create a new string */
  113. access(L, index, (tostring(L, o) == 0), NULL, svalue(o));
  114. }
  115. size_t lua_strlen (lua_State *L, int index) {
  116. access(L, index, (tostring(L, o) == 0), 0, tsvalue(o)->u.s.len);
  117. }
  118. lua_CFunction lua_tocfunction (lua_State *L, int index) {
  119. access(L, index, (ttype(o) == TAG_CCLOSURE), NULL, clvalue(o)->f.c);
  120. }
  121. void *lua_touserdata (lua_State *L, int index) {
  122. access(L, index, (ttype(o) == TAG_USERDATA), NULL, tsvalue(o)->u.d.value);
  123. }
  124. const void *lua_topointer (lua_State *L, int index) {
  125. StkId o = Index(L, index);
  126. switch (ttype(o)) {
  127. case TAG_NUMBER: case TAG_NIL:
  128. return NULL;
  129. case TAG_STRING:
  130. case TAG_USERDATA:
  131. return tsvalue(o);
  132. case TAG_TABLE:
  133. return hvalue(o);
  134. case TAG_CCLOSURE: case TAG_LCLOSURE:
  135. return clvalue(o);
  136. default: return NULL;
  137. }
  138. }
  139. /*
  140. ** push functions (C -> stack)
  141. */
  142. void lua_pushnil (lua_State *L) {
  143. ttype(L->top) = TAG_NIL;
  144. api_incr_top(L);
  145. }
  146. void lua_pushnumber (lua_State *L, double n) {
  147. ttype(L->top) = TAG_NUMBER;
  148. nvalue(L->top) = n;
  149. api_incr_top(L);
  150. }
  151. void lua_pushlstring (lua_State *L, const char *s, size_t len) {
  152. luaC_checkGC(L);
  153. tsvalue(L->top) = luaS_newlstr(L, s, len);
  154. ttype(L->top) = TAG_STRING;
  155. api_incr_top(L);
  156. }
  157. void lua_pushstring (lua_State *L, const char *s) {
  158. if (s == NULL)
  159. lua_pushnil(L);
  160. else
  161. lua_pushlstring(L, s, strlen(s));
  162. }
  163. void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  164. luaC_checkGC(L);
  165. luaV_Cclosure(L, fn, n);
  166. }
  167. void lua_pushusertag (lua_State *L, void *u, int tag) { /* ORDER LUA_T */
  168. luaC_checkGC(L);
  169. if (tag != LUA_ANYTAG && tag != TAG_USERDATA && tag < NUM_TAGS)
  170. luaL_verror(L, "invalid tag for a userdata (%d)", tag);
  171. tsvalue(L->top) = luaS_createudata(L, u, tag);
  172. ttype(L->top) = TAG_USERDATA;
  173. api_incr_top(L);
  174. }
  175. /*
  176. ** get functions (Lua -> stack)
  177. */
  178. void lua_getglobal (lua_State *L, const char *name) {
  179. StkId top = L->top;
  180. *top = *luaV_getglobal(L, luaS_new(L, name));
  181. L->top = top+1;
  182. }
  183. void lua_gettable (lua_State *L, int tableindex) {
  184. StkId t = Index(L, tableindex);
  185. StkId top = L->top;
  186. *(top-1) = *luaV_gettable(L, t);
  187. L->top = top; /* tag method may change top */
  188. }
  189. void lua_rawget (lua_State *L, int tableindex) {
  190. StkId t = Index(L, tableindex);
  191. LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected");
  192. *(L->top - 1) = *luaH_get(L, hvalue(t), L->top - 1);
  193. }
  194. void lua_rawgeti (lua_State *L, int index, int n) {
  195. StkId o = Index(L, index);
  196. LUA_ASSERT(ttype(o) == TAG_TABLE, "table expected");
  197. *L->top = *luaH_getnum(hvalue(o), n);
  198. api_incr_top(L);
  199. }
  200. void lua_getglobals (lua_State *L) {
  201. hvalue(L->top) = L->gt;
  202. ttype(L->top) = TAG_TABLE;
  203. api_incr_top(L);
  204. }
  205. int lua_getref (lua_State *L, int ref) {
  206. if (ref == LUA_REFNIL)
  207. ttype(L->top) = TAG_NIL;
  208. else if (0 <= ref && ref < L->refSize &&
  209. (L->refArray[ref].st == LOCK || L->refArray[ref].st == HOLD))
  210. *L->top = L->refArray[ref].o;
  211. else
  212. return 0;
  213. api_incr_top(L);
  214. return 1;
  215. }
  216. void lua_newtable (lua_State *L) {
  217. luaC_checkGC(L);
  218. hvalue(L->top) = luaH_new(L, 0);
  219. ttype(L->top) = TAG_TABLE;
  220. api_incr_top(L);
  221. }
  222. /*
  223. ** set functions (stack -> Lua)
  224. */
  225. void lua_setglobal (lua_State *L, const char *name) {
  226. StkId top = L->top;
  227. luaV_setglobal(L, luaS_new(L, name));
  228. L->top = top-1; /* remove element from the top */
  229. }
  230. void lua_settable (lua_State *L, int tableindex) {
  231. StkId t = Index(L, tableindex);
  232. StkId top = L->top;
  233. luaV_settable(L, t, top-2);
  234. L->top = top-2; /* pop index and value */
  235. }
  236. void lua_rawset (lua_State *L, int tableindex) {
  237. StkId t = Index(L, tableindex);
  238. LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected");
  239. *luaH_set(L, hvalue(t), L->top-2) = *(L->top-1);
  240. L->top -= 2;
  241. }
  242. void lua_rawseti (lua_State *L, int index, int n) {
  243. StkId o = Index(L, index);
  244. LUA_ASSERT(ttype(o) == TAG_TABLE, "table expected");
  245. *luaH_setint(L, hvalue(o), n) = *(L->top-1);
  246. L->top--;
  247. }
  248. void lua_setglobals (lua_State *L) {
  249. StkId newtable = --L->top;
  250. LUA_ASSERT(ttype(newtable) == TAG_TABLE, "table expected");
  251. L->gt = hvalue(newtable);
  252. }
  253. int lua_ref (lua_State *L, int lock) {
  254. int ref;
  255. if (ttype(L->top-1) == TAG_NIL)
  256. ref = LUA_REFNIL;
  257. else {
  258. if (L->refFree != NONEXT) { /* is there a free place? */
  259. ref = L->refFree;
  260. L->refFree = L->refArray[ref].st;
  261. }
  262. else { /* no more free places */
  263. luaM_growvector(L, L->refArray, L->refSize, 1, struct Ref,
  264. "reference table overflow", MAX_INT);
  265. ref = L->refSize++;
  266. }
  267. L->refArray[ref].o = *(L->top-1);
  268. L->refArray[ref].st = lock ? LOCK : HOLD;
  269. }
  270. L->top--;
  271. return ref;
  272. }
  273. /*
  274. ** miscellaneous functions
  275. */
  276. void lua_settag (lua_State *L, int tag) {
  277. luaT_realtag(L, tag);
  278. switch (ttype(L->top-1)) {
  279. case TAG_TABLE:
  280. hvalue(L->top-1)->htag = tag;
  281. break;
  282. case TAG_USERDATA:
  283. tsvalue(L->top-1)->u.d.tag = tag;
  284. break;
  285. default:
  286. luaL_verror(L, "cannot change the tag of a %.20s",
  287. luaO_typename(L->top-1));
  288. }
  289. L->top--;
  290. }
  291. void lua_unref (lua_State *L, int ref) {
  292. if (ref >= 0) {
  293. LUA_ASSERT(ref < L->refSize && L->refArray[ref].st < 0, "invalid ref");
  294. L->refArray[ref].st = L->refFree;
  295. L->refFree = ref;
  296. }
  297. }
  298. int lua_next (lua_State *L, int tableindex) {
  299. StkId t = Index(L, tableindex);
  300. Node *n;
  301. LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected");
  302. n = luaH_next(L, hvalue(t), Index(L, -1));
  303. if (n) {
  304. *(L->top-1) = *key(n);
  305. *L->top = *val(n);
  306. api_incr_top(L);
  307. return 1;
  308. }
  309. else { /* no more elements */
  310. L->top -= 1; /* remove key */
  311. return 0;
  312. }
  313. }
  314. int lua_getn (lua_State *L, int index) {
  315. Hash *h = hvalue(Index(L, index));
  316. const TObject *value = luaH_getstr(h, luaS_new(L, "n")); /* value = h.n */
  317. if (ttype(value) == TAG_NUMBER)
  318. return (int)nvalue(value);
  319. else {
  320. Number max = 0;
  321. int i = h->size;
  322. Node *n = h->node;
  323. while (i--) {
  324. if (ttype(key(n)) == TAG_NUMBER &&
  325. ttype(val(n)) != TAG_NIL &&
  326. nvalue(key(n)) > max)
  327. max = nvalue(key(n));
  328. n++;
  329. }
  330. return (int)max;
  331. }
  332. }
  333. void lua_concat (lua_State *L, int n) {
  334. StkId top = L->top;
  335. luaV_strconc(L, n, top);
  336. L->top = top-(n-1);
  337. }