lapi.c 11 KB

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