lapi.c 14 KB

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