lapi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. ** $Id: lapi.c,v 1.103 2000/10/02 20:10:55 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 "ldo.h"
  10. #include "lfunc.h"
  11. #include "lgc.h"
  12. #include "lmem.h"
  13. #include "lobject.h"
  14. #include "lstate.h"
  15. #include "lstring.h"
  16. #include "ltable.h"
  17. #include "ltm.h"
  18. #include "lvm.h"
  19. const char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
  20. "$Authors: " LUA_AUTHORS " $";
  21. #define Index(L,i) ((i) >= 0 ? (L->Cbase+((i)-1)) : (L->top+(i)))
  22. #define api_incr_top(L) incr_top
  23. TObject *luaA_index (lua_State *L, int index) {
  24. return Index(L, index);
  25. }
  26. void luaA_pushobject (lua_State *L, const TObject *o) {
  27. *L->top = *o;
  28. incr_top;
  29. }
  30. int lua_stackspace (lua_State *L) {
  31. return (L->stack_last - L->top);
  32. }
  33. /*
  34. ** basic stack manipulation
  35. */
  36. int lua_gettop (lua_State *L) {
  37. return (L->top - L->Cbase);
  38. }
  39. void lua_settop (lua_State *L, int index) {
  40. if (index >= 0)
  41. luaD_adjusttop(L, L->Cbase, index);
  42. else
  43. L->top = L->top+index+1; /* index is negative */
  44. }
  45. void lua_remove (lua_State *L, int index) {
  46. StkId p = Index(L, index);
  47. while (++p < L->top) *(p-1) = *p;
  48. L->top--;
  49. }
  50. void lua_insert (lua_State *L, int index) {
  51. StkId p = Index(L, index);
  52. StkId q;
  53. for (q = L->top; q>p; q--)
  54. *q = *(q-1);
  55. *p = *L->top;
  56. }
  57. void lua_pushvalue (lua_State *L, int index) {
  58. *L->top = *Index(L, index);
  59. api_incr_top(L);
  60. }
  61. /*
  62. ** access functions (stack -> C)
  63. */
  64. #define btest(L,i,value,default) { \
  65. StkId o; \
  66. if ((i) >= 0) { \
  67. o = L->Cbase+((i)-1); \
  68. if (o >= L->top) return (default); \
  69. } \
  70. else o = L->top+(i); \
  71. return (value); }
  72. #define access(L,i,test,default,value) { \
  73. StkId o; \
  74. if ((i) >= 0) { \
  75. o = L->Cbase+((i)-1); \
  76. if (o >= L->top) return (default); \
  77. } \
  78. else o = L->top+(i); \
  79. return ((test) ? (value) : (default)); }
  80. lua_Type lua_type (lua_State *L, int index) {
  81. btest(L, index, luaO_type(o), LUA_NOVALUE);
  82. }
  83. const char *lua_typename (lua_State *L, lua_Type t) {
  84. static const char *const names[] = {
  85. "NO VALUE", "userdata", "number", "string", "table", "function", "nil"
  86. };
  87. UNUSED(L);
  88. return names[(int)t];
  89. }
  90. int lua_iscfunction (lua_State *L, int index) {
  91. btest(L, index, (ttype(o) == TAG_CCLOSURE), 0);
  92. }
  93. int lua_isnumber (lua_State *L, int index) {
  94. btest(L, index, (tonumber(Index(L, index)) == 0), 0);
  95. }
  96. int lua_isstring (lua_State *L, int index) {
  97. lua_Type t = lua_type(L, index);
  98. return (t == LUA_TSTRING || t == LUA_TNUMBER);
  99. }
  100. static int auxtag (const TObject *o) {
  101. return ((ttype(o) == TAG_USERDATA) ? tsvalue(o)->u.d.tag :
  102. (ttype(o) == TAG_TABLE) ? hvalue(o)->htag : (int)ttype(o));
  103. }
  104. int lua_tag (lua_State *L, int index) {
  105. btest(L, index, auxtag(o), LUA_NOTAG);
  106. }
  107. int lua_equal (lua_State *L, int index1, int index2) {
  108. StkId o1 = Index(L, index1);
  109. StkId o2 = Index(L, index2);
  110. if (o1 >= L->top || o2 >= L->top) return 0; /* index out-of-range */
  111. else return luaO_equalObj(o1, o2);
  112. }
  113. int lua_lessthan (lua_State *L, int index1, int index2) {
  114. StkId o1 = Index(L, index1);
  115. StkId o2 = Index(L, index2);
  116. if (o1 >= L->top || o2 >= L->top) return 0; /* index out-of-range */
  117. else return luaV_lessthan(L, o1, o2, L->top);
  118. }
  119. double lua_tonumber (lua_State *L, int index) {
  120. access(L, index, (tonumber(o) == 0), 0.0, nvalue(o));
  121. }
  122. const char *lua_tostring (lua_State *L, int index) {
  123. access(L, index, (tostring(L, o) == 0), NULL, svalue(o));
  124. }
  125. size_t lua_strlen (lua_State *L, int index) {
  126. access(L, index, (tostring(L, o) == 0), 0, tsvalue(o)->u.s.len);
  127. }
  128. lua_CFunction lua_tocfunction (lua_State *L, int index) {
  129. access(L, index, (ttype(o) == TAG_CCLOSURE), NULL, clvalue(o)->f.c);
  130. }
  131. void *lua_touserdata (lua_State *L, int index) {
  132. access(L, index, (ttype(o) == TAG_USERDATA), NULL, tsvalue(o)->u.d.value);
  133. }
  134. const void *lua_topointer (lua_State *L, int index) {
  135. StkId o = Index(L, index);
  136. switch (ttype(o)) {
  137. case TAG_TABLE:
  138. return hvalue(o);
  139. case TAG_CCLOSURE: case TAG_LCLOSURE:
  140. return clvalue(o);
  141. default: return NULL;
  142. }
  143. }
  144. /*
  145. ** push functions (C -> stack)
  146. */
  147. void lua_pushnil (lua_State *L) {
  148. ttype(L->top) = TAG_NIL;
  149. api_incr_top(L);
  150. }
  151. void lua_pushnumber (lua_State *L, double n) {
  152. ttype(L->top) = TAG_NUMBER;
  153. nvalue(L->top) = n;
  154. api_incr_top(L);
  155. }
  156. void lua_pushlstring (lua_State *L, const char *s, size_t len) {
  157. tsvalue(L->top) = luaS_newlstr(L, s, len);
  158. ttype(L->top) = TAG_STRING;
  159. api_incr_top(L);
  160. }
  161. void lua_pushstring (lua_State *L, const char *s) {
  162. if (s == NULL)
  163. lua_pushnil(L);
  164. else
  165. lua_pushlstring(L, s, strlen(s));
  166. }
  167. void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  168. luaV_Cclosure(L, fn, n);
  169. }
  170. void lua_pushusertag (lua_State *L, void *u, int tag) { /* ORDER LUA_T */
  171. if (tag != LUA_ANYTAG && tag != TAG_USERDATA && tag < NUM_TAGS)
  172. luaO_verror(L, "invalid tag for a userdata (%d)", tag);
  173. tsvalue(L->top) = luaS_createudata(L, u, tag);
  174. ttype(L->top) = TAG_USERDATA;
  175. api_incr_top(L);
  176. }
  177. /*
  178. ** get functions (Lua -> stack)
  179. */
  180. void lua_getglobal (lua_State *L, const char *name) {
  181. StkId top = L->top;
  182. *top = *luaV_getglobal(L, luaS_new(L, name));
  183. L->top = top;
  184. api_incr_top(L);
  185. }
  186. void lua_gettable (lua_State *L, int index) {
  187. StkId t = Index(L, index);
  188. StkId top = L->top;
  189. *(top-1) = *luaV_gettable(L, t);
  190. L->top = top; /* tag method may change top */
  191. }
  192. void lua_rawget (lua_State *L, int index) {
  193. StkId t = Index(L, index);
  194. LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected");
  195. *(L->top - 1) = *luaH_get(L, hvalue(t), L->top - 1);
  196. }
  197. void lua_rawgeti (lua_State *L, int index, int n) {
  198. StkId o = Index(L, index);
  199. LUA_ASSERT(ttype(o) == TAG_TABLE, "table expected");
  200. *L->top = *luaH_getnum(hvalue(o), n);
  201. api_incr_top(L);
  202. }
  203. void lua_getglobals (lua_State *L) {
  204. hvalue(L->top) = L->gt;
  205. ttype(L->top) = TAG_TABLE;
  206. api_incr_top(L);
  207. }
  208. int lua_getref (lua_State *L, int ref) {
  209. if (ref == LUA_REFNIL)
  210. ttype(L->top) = TAG_NIL;
  211. else if (0 <= ref && ref < L->refSize &&
  212. (L->refArray[ref].st == LOCK || L->refArray[ref].st == HOLD))
  213. *L->top = L->refArray[ref].o;
  214. else
  215. return 0;
  216. api_incr_top(L);
  217. return 1;
  218. }
  219. void lua_newtable (lua_State *L) {
  220. hvalue(L->top) = luaH_new(L, 0);
  221. ttype(L->top) = TAG_TABLE;
  222. api_incr_top(L);
  223. }
  224. /*
  225. ** set functions (stack -> Lua)
  226. */
  227. void lua_setglobal (lua_State *L, const char *name) {
  228. StkId top = L->top;
  229. luaV_setglobal(L, luaS_new(L, name));
  230. L->top = top-1; /* remove element from the top */
  231. }
  232. void lua_settable (lua_State *L, int index) {
  233. StkId t = Index(L, index);
  234. StkId top = L->top;
  235. luaV_settable(L, t, top-2);
  236. L->top = top-2; /* pop index and value */
  237. }
  238. void lua_rawset (lua_State *L, int index) {
  239. StkId t = Index(L, index);
  240. LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected");
  241. *luaH_set(L, hvalue(t), L->top-2) = *(L->top-1);
  242. L->top -= 2;
  243. }
  244. void lua_rawseti (lua_State *L, int index, int n) {
  245. StkId o = Index(L, index);
  246. LUA_ASSERT(ttype(o) == TAG_TABLE, "table expected");
  247. *luaH_setint(L, hvalue(o), n) = *(L->top-1);
  248. L->top--;
  249. }
  250. void lua_setglobals (lua_State *L) {
  251. StkId newtable = --L->top;
  252. LUA_ASSERT(ttype(newtable) == TAG_TABLE, "table expected");
  253. L->gt = hvalue(newtable);
  254. }
  255. int lua_ref (lua_State *L, int lock) {
  256. int ref;
  257. if (ttype(L->top-1) == TAG_NIL)
  258. ref = LUA_REFNIL;
  259. else {
  260. if (L->refFree != NONEXT) { /* is there a free place? */
  261. ref = L->refFree;
  262. L->refFree = L->refArray[ref].st;
  263. }
  264. else { /* no more free places */
  265. luaM_growvector(L, L->refArray, L->refSize, 1, struct Ref,
  266. "reference table overflow", MAX_INT);
  267. L->nblocks += sizeof(struct Ref);
  268. ref = L->refSize++;
  269. }
  270. L->refArray[ref].o = *(L->top-1);
  271. L->refArray[ref].st = lock ? LOCK : HOLD;
  272. }
  273. L->top--;
  274. return ref;
  275. }
  276. /*
  277. ** "do" functions (run Lua code)
  278. ** (most of them are in ldo.c)
  279. */
  280. void lua_rawcall (lua_State *L, int nargs, int nresults) {
  281. luaD_call(L, L->top-(nargs+1), nresults);
  282. }
  283. /*
  284. ** Garbage-collection functions
  285. */
  286. /* GC values are expressed in Kbytes: #bytes/2^10 */
  287. #define GCscale(x) ((int)((x)>>10))
  288. #define GCunscale(x) ((unsigned long)(x)<<10)
  289. int lua_getgcthreshold (lua_State *L) {
  290. return GCscale(L->GCthreshold);
  291. }
  292. int lua_getgccount (lua_State *L) {
  293. return GCscale(L->nblocks);
  294. }
  295. void lua_setgcthreshold (lua_State *L, int newthreshold) {
  296. if (newthreshold > GCscale(ULONG_MAX))
  297. L->GCthreshold = ULONG_MAX;
  298. else
  299. L->GCthreshold = GCunscale(newthreshold);
  300. luaC_checkGC(L);
  301. }
  302. /*
  303. ** miscellaneous functions
  304. */
  305. void lua_settag (lua_State *L, int tag) {
  306. luaT_realtag(L, tag);
  307. switch (ttype(L->top-1)) {
  308. case TAG_TABLE:
  309. hvalue(L->top-1)->htag = tag;
  310. break;
  311. case TAG_USERDATA:
  312. tsvalue(L->top-1)->u.d.tag = tag;
  313. break;
  314. default:
  315. luaO_verror(L, "cannot change the tag of a %.20s",
  316. luaO_typename(L, L->top-1));
  317. }
  318. L->top--;
  319. }
  320. void lua_unref (lua_State *L, int ref) {
  321. if (ref >= 0) {
  322. LUA_ASSERT(ref < L->refSize && L->refArray[ref].st < 0, "invalid ref");
  323. L->refArray[ref].st = L->refFree;
  324. L->refFree = ref;
  325. }
  326. }
  327. int lua_next (lua_State *L, int index) {
  328. StkId t = Index(L, index);
  329. Node *n;
  330. LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected");
  331. n = luaH_next(L, hvalue(t), Index(L, -1));
  332. if (n) {
  333. *(L->top-1) = *key(n);
  334. *L->top = *val(n);
  335. api_incr_top(L);
  336. return 1;
  337. }
  338. else { /* no more elements */
  339. L->top -= 1; /* remove key */
  340. return 0;
  341. }
  342. }
  343. int lua_getn (lua_State *L, int index) {
  344. Hash *h = hvalue(Index(L, index));
  345. const TObject *value = luaH_getstr(h, luaS_new(L, "n")); /* value = h.n */
  346. if (ttype(value) == TAG_NUMBER)
  347. return (int)nvalue(value);
  348. else {
  349. Number max = 0;
  350. int i = h->size;
  351. Node *n = h->node;
  352. while (i--) {
  353. if (ttype(key(n)) == TAG_NUMBER &&
  354. ttype(val(n)) != TAG_NIL &&
  355. nvalue(key(n)) > max)
  356. max = nvalue(key(n));
  357. n++;
  358. }
  359. return (int)max;
  360. }
  361. }
  362. void lua_concat (lua_State *L, int n) {
  363. StkId top = L->top;
  364. luaV_strconc(L, n, top);
  365. L->top = top-(n-1);
  366. luaC_checkGC(L);
  367. }