lapi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /*
  2. ** $Id: lapi.c,v 1.118 2001/01/19 13:20:30 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. setobj(L->top, o);
  36. incr_top;
  37. }
  38. LUA_API int lua_stackspace (lua_State *L) {
  39. int i;
  40. LUA_ENTRY;
  41. i = (L->stack_last - L->top);
  42. LUA_EXIT;
  43. return i;
  44. }
  45. /*
  46. ** basic stack manipulation
  47. */
  48. LUA_API int lua_gettop (lua_State *L) {
  49. int i;
  50. LUA_ENTRY;
  51. i = (L->top - L->Cbase);
  52. LUA_EXIT;
  53. return i;
  54. }
  55. LUA_API void lua_settop (lua_State *L, int index) {
  56. LUA_ENTRY;
  57. if (index >= 0)
  58. luaD_adjusttop(L, L->Cbase, index);
  59. else
  60. L->top = L->top+index+1; /* index is negative */
  61. LUA_EXIT;
  62. }
  63. LUA_API void lua_remove (lua_State *L, int index) {
  64. StkId p;
  65. LUA_ENTRY;
  66. p = luaA_index(L, index);
  67. while (++p < L->top) setobj(p-1, p);
  68. L->top--;
  69. LUA_EXIT;
  70. }
  71. LUA_API void lua_insert (lua_State *L, int index) {
  72. StkId p;
  73. StkId q;
  74. LUA_ENTRY;
  75. p = luaA_index(L, index);
  76. for (q = L->top; q>p; q--) setobj(q, q-1);
  77. setobj(p, L->top);
  78. LUA_EXIT;
  79. }
  80. LUA_API void lua_pushvalue (lua_State *L, int index) {
  81. LUA_ENTRY;
  82. setobj(L->top, luaA_index(L, index));
  83. api_incr_top(L);
  84. LUA_EXIT;
  85. }
  86. /*
  87. ** access functions (stack -> C)
  88. */
  89. LUA_API int lua_type (lua_State *L, int index) {
  90. StkId o;
  91. int i;
  92. LUA_ENTRY;
  93. o = luaA_indexAcceptable(L, index);
  94. i = (o == NULL) ? LUA_TNONE : ttype(o);
  95. LUA_EXIT;
  96. return i;
  97. }
  98. LUA_API const char *lua_typename (lua_State *L, int t) {
  99. const char *s;
  100. LUA_ENTRY;
  101. UNUSED(L);
  102. s = (t == LUA_TNONE) ? "no value" : luaO_typenames[t];
  103. LUA_EXIT;
  104. return s;
  105. }
  106. LUA_API int lua_iscfunction (lua_State *L, int index) {
  107. StkId o;
  108. int i;
  109. LUA_ENTRY;
  110. o = luaA_indexAcceptable(L, index);
  111. i = (o == NULL) ? 0 : iscfunction(o);
  112. LUA_EXIT;
  113. return i;
  114. }
  115. LUA_API int lua_isnumber (lua_State *L, int index) {
  116. TObject *o;
  117. int i;
  118. LUA_ENTRY;
  119. o = luaA_indexAcceptable(L, index);
  120. i = (o == NULL) ? 0 : (tonumber(o) == 0);
  121. LUA_EXIT;
  122. return i;
  123. }
  124. LUA_API int lua_isstring (lua_State *L, int index) {
  125. int t = lua_type(L, index);
  126. return (t == LUA_TSTRING || t == LUA_TNUMBER);
  127. }
  128. LUA_API int lua_tag (lua_State *L, int index) {
  129. StkId o;
  130. int i;
  131. LUA_ENTRY;
  132. o = luaA_indexAcceptable(L, index);
  133. i = (o == NULL) ? LUA_NOTAG : luaT_tag(o);
  134. LUA_EXIT;
  135. return i;
  136. }
  137. LUA_API int lua_equal (lua_State *L, int index1, int index2) {
  138. StkId o1, o2;
  139. int i;
  140. LUA_ENTRY;
  141. o1 = luaA_indexAcceptable(L, index1);
  142. o2 = luaA_indexAcceptable(L, index2);
  143. i = (o1 == NULL || o2 == NULL) ? 0 /* index out-of-range */
  144. : luaO_equalObj(o1, o2);
  145. LUA_EXIT;
  146. return i;
  147. }
  148. LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
  149. StkId o1, o2;
  150. int i;
  151. LUA_ENTRY;
  152. o1 = luaA_indexAcceptable(L, index1);
  153. o2 = luaA_indexAcceptable(L, index2);
  154. i = (o1 == NULL || o2 == NULL) ? 0 /* index out-of-range */
  155. : luaV_lessthan(L, o1, o2, L->top);
  156. LUA_EXIT;
  157. return i;
  158. }
  159. LUA_API lua_Number lua_tonumber (lua_State *L, int index) {
  160. StkId o;
  161. lua_Number n;
  162. LUA_ENTRY;
  163. o = luaA_indexAcceptable(L, index);
  164. n = (o == NULL || tonumber(o)) ? 0 : nvalue(o);
  165. LUA_EXIT;
  166. return n;
  167. }
  168. LUA_API const char *lua_tostring (lua_State *L, int index) {
  169. StkId o;
  170. const char *s;
  171. LUA_ENTRY;
  172. o = luaA_indexAcceptable(L, index);
  173. s = (o == NULL || tostring(L, o)) ? NULL : svalue(o);
  174. LUA_EXIT;
  175. return s;
  176. }
  177. LUA_API size_t lua_strlen (lua_State *L, int index) {
  178. StkId o;
  179. size_t l;
  180. LUA_ENTRY;
  181. o = luaA_indexAcceptable(L, index);
  182. l = (o == NULL || tostring(L, o)) ? 0 : tsvalue(o)->len;
  183. LUA_EXIT;
  184. return l;
  185. }
  186. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index) {
  187. StkId o;
  188. lua_CFunction f;
  189. LUA_ENTRY;
  190. o = luaA_indexAcceptable(L, index);
  191. f = (o == NULL || !iscfunction(o)) ? NULL : clvalue(o)->f.c;
  192. LUA_EXIT;
  193. return f;
  194. }
  195. LUA_API void *lua_touserdata (lua_State *L, int index) {
  196. StkId o;
  197. void *p;
  198. LUA_ENTRY;
  199. o = luaA_indexAcceptable(L, index);
  200. p = (o == NULL || ttype(o) != LUA_TUSERDATA) ? NULL :
  201. tsvalue(o)->u.d.value;
  202. LUA_EXIT;
  203. return p;
  204. }
  205. LUA_API const void *lua_topointer (lua_State *L, int index) {
  206. StkId o;
  207. const void *p;
  208. LUA_ENTRY;
  209. o = luaA_indexAcceptable(L, index);
  210. if (o == NULL) p = NULL;
  211. else {
  212. switch (ttype(o)) {
  213. case LUA_TTABLE:
  214. p = hvalue(o);
  215. break;
  216. case LUA_TFUNCTION:
  217. p = clvalue(o);
  218. break;
  219. default:
  220. p = NULL;
  221. break;
  222. }
  223. }
  224. LUA_EXIT;
  225. return p;
  226. }
  227. /*
  228. ** push functions (C -> stack)
  229. */
  230. LUA_API void lua_pushnil (lua_State *L) {
  231. LUA_ENTRY;
  232. setnilvalue(L->top);
  233. api_incr_top(L);
  234. LUA_EXIT;
  235. }
  236. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  237. LUA_ENTRY;
  238. setnvalue(L->top, n);
  239. api_incr_top(L);
  240. LUA_EXIT;
  241. }
  242. LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
  243. LUA_ENTRY;
  244. setsvalue(L->top, luaS_newlstr(L, s, len));
  245. api_incr_top(L);
  246. LUA_EXIT;
  247. }
  248. LUA_API void lua_pushstring (lua_State *L, const char *s) {
  249. if (s == NULL)
  250. lua_pushnil(L);
  251. else
  252. lua_pushlstring(L, s, strlen(s));
  253. }
  254. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  255. LUA_ENTRY;
  256. luaV_Cclosure(L, fn, n);
  257. LUA_EXIT;
  258. }
  259. LUA_API void lua_pushusertag (lua_State *L, void *u, int tag) {
  260. LUA_ENTRY;
  261. /* ORDER LUA_T */
  262. if (!(tag == LUA_ANYTAG || tag == LUA_TUSERDATA || validtag(G(L), tag)))
  263. luaO_verror(L, "invalid tag for a userdata (%d)", tag);
  264. setuvalue(L->top, luaS_createudata(L, u, tag));
  265. api_incr_top(L);
  266. LUA_EXIT;
  267. }
  268. /*
  269. ** get functions (Lua -> stack)
  270. */
  271. LUA_API void lua_getglobal (lua_State *L, const char *name) {
  272. StkId top;
  273. LUA_ENTRY;
  274. top = L->top;
  275. setobj(top, luaV_getglobal(L, luaS_new(L, name)));
  276. L->top = top;
  277. api_incr_top(L);
  278. LUA_EXIT;
  279. }
  280. LUA_API void lua_gettable (lua_State *L, int index) {
  281. StkId t, top;
  282. LUA_ENTRY;
  283. t = Index(L, index);
  284. top = L->top;
  285. setobj(top-1, luaV_gettable(L, t));
  286. L->top = top; /* tag method may change top */
  287. LUA_EXIT;
  288. }
  289. LUA_API void lua_rawget (lua_State *L, int index) {
  290. StkId t;
  291. LUA_ENTRY;
  292. t = Index(L, index);
  293. lua_assert(ttype(t) == LUA_TTABLE);
  294. setobj(L->top - 1, luaH_get(hvalue(t), L->top - 1));
  295. LUA_EXIT;
  296. }
  297. LUA_API void lua_rawgeti (lua_State *L, int index, int n) {
  298. StkId o;
  299. LUA_ENTRY;
  300. o = Index(L, index);
  301. lua_assert(ttype(o) == LUA_TTABLE);
  302. setobj(L->top, luaH_getnum(hvalue(o), n));
  303. api_incr_top(L);
  304. LUA_EXIT;
  305. }
  306. LUA_API void lua_getglobals (lua_State *L) {
  307. LUA_ENTRY;
  308. sethvalue(L->top, L->gt);
  309. api_incr_top(L);
  310. LUA_EXIT;
  311. }
  312. LUA_API int lua_getref (lua_State *L, int ref) {
  313. int status = 1;
  314. LUA_ENTRY;
  315. if (ref == LUA_REFNIL) {
  316. setnilvalue(L->top);
  317. api_incr_top(L);
  318. }
  319. else if (0 <= ref && ref < G(L)->nref &&
  320. (G(L)->refArray[ref].st == LOCK || G(L)->refArray[ref].st == HOLD)) {
  321. setobj(L->top, &G(L)->refArray[ref].o);
  322. api_incr_top(L);
  323. }
  324. else
  325. status = 0;
  326. LUA_EXIT;
  327. return status;
  328. }
  329. LUA_API void lua_newtable (lua_State *L) {
  330. LUA_ENTRY;
  331. sethvalue(L->top, luaH_new(L, 0));
  332. api_incr_top(L);
  333. LUA_EXIT;
  334. }
  335. /*
  336. ** set functions (stack -> Lua)
  337. */
  338. LUA_API void lua_setglobal (lua_State *L, const char *name) {
  339. StkId top;
  340. LUA_ENTRY;
  341. top = L->top;
  342. luaV_setglobal(L, luaS_new(L, name));
  343. L->top = top-1; /* remove element from the top */
  344. LUA_EXIT;
  345. }
  346. LUA_API void lua_settable (lua_State *L, int index) {
  347. StkId t, top;
  348. LUA_ENTRY;
  349. t = Index(L, index);
  350. top = L->top;
  351. luaV_settable(L, t, top-2);
  352. L->top = top-2; /* pop index and value */
  353. LUA_EXIT;
  354. }
  355. LUA_API void lua_rawset (lua_State *L, int index) {
  356. StkId t;
  357. LUA_ENTRY;
  358. t = Index(L, index);
  359. lua_assert(ttype(t) == LUA_TTABLE);
  360. setobj(luaH_set(L, hvalue(t), L->top-2), (L->top-1));
  361. L->top -= 2;
  362. LUA_EXIT;
  363. }
  364. LUA_API void lua_rawseti (lua_State *L, int index, int n) {
  365. StkId o;
  366. LUA_ENTRY;
  367. o = Index(L, index);
  368. lua_assert(ttype(o) == LUA_TTABLE);
  369. setobj(luaH_setnum(L, hvalue(o), n), (L->top-1));
  370. L->top--;
  371. LUA_EXIT;
  372. }
  373. LUA_API void lua_setglobals (lua_State *L) {
  374. StkId newtable;
  375. LUA_ENTRY;
  376. newtable = --L->top;
  377. lua_assert(ttype(newtable) == LUA_TTABLE);
  378. L->gt = hvalue(newtable);
  379. LUA_EXIT;
  380. }
  381. LUA_API int lua_ref (lua_State *L, int lock) {
  382. int ref;
  383. LUA_ENTRY;
  384. if (ttype(L->top-1) == LUA_TNIL)
  385. ref = LUA_REFNIL;
  386. else {
  387. if (G(L)->refFree != NONEXT) { /* is there a free place? */
  388. ref = G(L)->refFree;
  389. G(L)->refFree = G(L)->refArray[ref].st;
  390. }
  391. else { /* no more free places */
  392. luaM_growvector(L, G(L)->refArray, G(L)->nref, G(L)->sizeref, struct Ref,
  393. MAX_INT, "reference table overflow");
  394. ref = G(L)->nref++;
  395. }
  396. setobj(&G(L)->refArray[ref].o, L->top-1);
  397. G(L)->refArray[ref].st = lock ? LOCK : HOLD;
  398. }
  399. L->top--;
  400. LUA_EXIT;
  401. return ref;
  402. }
  403. /*
  404. ** "do" functions (run Lua code)
  405. ** (most of them are in ldo.c)
  406. */
  407. LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
  408. LUA_ENTRY;
  409. luaD_call(L, L->top-(nargs+1), nresults);
  410. LUA_EXIT;
  411. }
  412. /*
  413. ** Garbage-collection functions
  414. */
  415. /* GC values are expressed in Kbytes: #bytes/2^10 */
  416. #define GCscale(x) ((int)((x)>>10))
  417. #define GCunscale(x) ((mem_int)(x)<<10)
  418. LUA_API int lua_getgcthreshold (lua_State *L) {
  419. int threshold;
  420. LUA_ENTRY;
  421. threshold = GCscale(G(L)->GCthreshold);
  422. LUA_EXIT;
  423. return threshold;
  424. }
  425. LUA_API int lua_getgccount (lua_State *L) {
  426. int count;
  427. LUA_ENTRY;
  428. count = GCscale(G(L)->nblocks);
  429. LUA_EXIT;
  430. return count;
  431. }
  432. LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
  433. LUA_ENTRY;
  434. if (newthreshold > GCscale(ULONG_MAX))
  435. G(L)->GCthreshold = ULONG_MAX;
  436. else
  437. G(L)->GCthreshold = GCunscale(newthreshold);
  438. luaC_checkGC(L);
  439. LUA_EXIT;
  440. }
  441. /*
  442. ** miscellaneous functions
  443. */
  444. LUA_API void lua_settag (lua_State *L, int tag) {
  445. LUA_ENTRY;
  446. luaT_realtag(L, tag);
  447. switch (ttype(L->top-1)) {
  448. case LUA_TTABLE:
  449. hvalue(L->top-1)->htag = tag;
  450. break;
  451. case LUA_TUSERDATA:
  452. tsvalue(L->top-1)->u.d.tag = tag;
  453. break;
  454. default:
  455. luaO_verror(L, "cannot change the tag of a %.20s",
  456. luaO_typename(L->top-1));
  457. }
  458. LUA_EXIT;
  459. }
  460. LUA_API void lua_error (lua_State *L, const char *s) {
  461. LUA_ENTRY;
  462. luaD_error(L, s);
  463. LUA_EXIT;
  464. }
  465. LUA_API void lua_unref (lua_State *L, int ref) {
  466. LUA_ENTRY;
  467. if (ref >= 0) {
  468. lua_assert(ref < G(L)->nref && G(L)->refArray[ref].st < 0);
  469. G(L)->refArray[ref].st = G(L)->refFree;
  470. G(L)->refFree = ref;
  471. }
  472. LUA_EXIT;
  473. }
  474. LUA_API int lua_next (lua_State *L, int index) {
  475. StkId t;
  476. Node *n;
  477. int more;
  478. LUA_ENTRY;
  479. t = luaA_index(L, index);
  480. lua_assert(ttype(t) == LUA_TTABLE);
  481. n = luaH_next(L, hvalue(t), luaA_index(L, -1));
  482. if (n) {
  483. setobj(L->top-1, key(n));
  484. setobj(L->top, val(n));
  485. api_incr_top(L);
  486. more = 1;
  487. }
  488. else { /* no more elements */
  489. L->top -= 1; /* remove key */
  490. more = 0;
  491. }
  492. LUA_EXIT;
  493. return more;
  494. }
  495. LUA_API int lua_getn (lua_State *L, int index) {
  496. Hash *h;
  497. const TObject *value;
  498. int n;
  499. LUA_ENTRY;
  500. h = hvalue(luaA_index(L, index));
  501. value = luaH_getstr(h, luaS_newliteral(L, "n")); /* = h.n */
  502. if (ttype(value) == LUA_TNUMBER)
  503. n = (int)nvalue(value);
  504. else {
  505. lua_Number max = 0;
  506. int i = h->size;
  507. Node *nd = h->node;
  508. while (i--) {
  509. if (ttype(key(nd)) == LUA_TNUMBER &&
  510. ttype(val(nd)) != LUA_TNIL &&
  511. nvalue(key(nd)) > max)
  512. max = nvalue(key(nd));
  513. nd++;
  514. }
  515. n = (int)max;
  516. }
  517. LUA_EXIT;
  518. return n;
  519. }
  520. LUA_API void lua_concat (lua_State *L, int n) {
  521. StkId top;
  522. LUA_ENTRY;
  523. top = L->top;
  524. luaV_strconc(L, n, top);
  525. L->top = top-(n-1);
  526. luaC_checkGC(L);
  527. LUA_EXIT;
  528. }
  529. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  530. TString *ts;
  531. void *p;
  532. LUA_ENTRY;
  533. ts = luaS_newudata(L, size, NULL);
  534. setuvalue(L->top, ts);
  535. api_incr_top(L);
  536. p = ts->u.d.value;
  537. LUA_EXIT;
  538. return p;
  539. }