lapi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /*
  2. ** $Id: lapi.c,v 1.124 2001/02/01 16:03:38 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, L->top);
  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 void lua_pushusertag (lua_State *L, void *u, int tag) {
  268. LUA_LOCK(L);
  269. /* ORDER LUA_T */
  270. if (!(tag == LUA_ANYTAG || tag == LUA_TUSERDATA || validtag(G(L), tag)))
  271. luaO_verror(L, "invalid tag for a userdata (%d)", tag);
  272. setuvalue(L->top, luaS_createudata(L, u, tag));
  273. api_incr_top(L);
  274. LUA_UNLOCK(L);
  275. }
  276. /*
  277. ** get functions (Lua -> stack)
  278. */
  279. LUA_API void lua_getglobal (lua_State *L, const char *name) {
  280. LUA_LOCK(L);
  281. luaV_getglobal(L, luaS_new(L, name), L->top);
  282. api_incr_top(L);
  283. LUA_UNLOCK(L);
  284. }
  285. LUA_API void lua_gettable (lua_State *L, int index) {
  286. StkId t;
  287. LUA_LOCK(L);
  288. t = Index(L, index);
  289. luaV_gettable(L, t, L->top, L->top-1);
  290. LUA_UNLOCK(L);
  291. }
  292. LUA_API void lua_rawget (lua_State *L, int index) {
  293. StkId t;
  294. LUA_LOCK(L);
  295. t = Index(L, index);
  296. lua_assert(ttype(t) == LUA_TTABLE);
  297. setobj(L->top - 1, luaH_get(hvalue(t), L->top - 1));
  298. LUA_UNLOCK(L);
  299. }
  300. LUA_API void lua_rawgeti (lua_State *L, int index, int n) {
  301. StkId o;
  302. LUA_LOCK(L);
  303. o = Index(L, index);
  304. lua_assert(ttype(o) == LUA_TTABLE);
  305. setobj(L->top, luaH_getnum(hvalue(o), n));
  306. api_incr_top(L);
  307. LUA_UNLOCK(L);
  308. }
  309. LUA_API void lua_getglobals (lua_State *L) {
  310. LUA_LOCK(L);
  311. sethvalue(L->top, L->gt);
  312. api_incr_top(L);
  313. LUA_UNLOCK(L);
  314. }
  315. LUA_API int lua_getref (lua_State *L, int ref) {
  316. int status = 1;
  317. LUA_LOCK(L);
  318. if (ref == LUA_REFNIL) {
  319. setnilvalue(L->top);
  320. api_incr_top(L);
  321. }
  322. else if (0 <= ref && ref < G(L)->nref &&
  323. (G(L)->refArray[ref].st == LOCK || G(L)->refArray[ref].st == HOLD)) {
  324. setobj(L->top, &G(L)->refArray[ref].o);
  325. api_incr_top(L);
  326. }
  327. else
  328. status = 0;
  329. LUA_UNLOCK(L);
  330. return status;
  331. }
  332. LUA_API void lua_newtable (lua_State *L) {
  333. LUA_LOCK(L);
  334. sethvalue(L->top, luaH_new(L, 0));
  335. api_incr_top(L);
  336. LUA_UNLOCK(L);
  337. }
  338. /*
  339. ** set functions (stack -> Lua)
  340. */
  341. LUA_API void lua_setglobal (lua_State *L, const char *name) {
  342. LUA_LOCK(L);
  343. luaV_setglobal(L, luaS_new(L, name), L->top);
  344. L->top--; /* remove element from the top */
  345. LUA_UNLOCK(L);
  346. }
  347. LUA_API void lua_settable (lua_State *L, int index) {
  348. StkId t;
  349. LUA_LOCK(L);
  350. t = Index(L, index);
  351. luaV_settable(L, t, L->top - 2, L->top);
  352. L->top -= 2; /* pop index and value */
  353. LUA_UNLOCK(L);
  354. }
  355. LUA_API void lua_rawset (lua_State *L, int index) {
  356. StkId t;
  357. LUA_LOCK(L);
  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_UNLOCK(L);
  363. }
  364. LUA_API void lua_rawseti (lua_State *L, int index, int n) {
  365. StkId o;
  366. LUA_LOCK(L);
  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_UNLOCK(L);
  372. }
  373. LUA_API void lua_setglobals (lua_State *L) {
  374. StkId newtable;
  375. LUA_LOCK(L);
  376. newtable = --L->top;
  377. lua_assert(ttype(newtable) == LUA_TTABLE);
  378. L->gt = hvalue(newtable);
  379. LUA_UNLOCK(L);
  380. }
  381. LUA_API int lua_ref (lua_State *L, int lock) {
  382. int ref;
  383. LUA_LOCK(L);
  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_UNLOCK(L);
  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_LOCK(L);
  409. luaD_call(L, L->top-(nargs+1), nresults);
  410. LUA_UNLOCK(L);
  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_LOCK(L);
  421. threshold = GCscale(G(L)->GCthreshold);
  422. LUA_UNLOCK(L);
  423. return threshold;
  424. }
  425. LUA_API int lua_getgccount (lua_State *L) {
  426. int count;
  427. LUA_LOCK(L);
  428. count = GCscale(G(L)->nblocks);
  429. LUA_UNLOCK(L);
  430. return count;
  431. }
  432. LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
  433. LUA_LOCK(L);
  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_UNLOCK(L);
  440. }
  441. /*
  442. ** miscellaneous functions
  443. */
  444. LUA_API int lua_newtype (lua_State *L, const char *name, int basictype) {
  445. int tag;
  446. LUA_LOCK(L);
  447. if (basictype != LUA_TNONE &&
  448. basictype != LUA_TTABLE &&
  449. basictype != LUA_TUSERDATA)
  450. luaO_verror(L, "invalid basic type (%d) for new type", basictype);
  451. tag = luaT_newtag(L, name, basictype);
  452. if (tag == LUA_TNONE)
  453. luaO_verror(L, "type name '%.30s' already exists", name);
  454. LUA_UNLOCK(L);
  455. return tag;
  456. }
  457. LUA_API int lua_type2tag (lua_State *L, const char *name) {
  458. int tag;
  459. const TObject *v;
  460. LUA_LOCK(L);
  461. v = luaH_getstr(G(L)->type2tag, luaS_new(L, name));
  462. if (ttype(v) == LUA_TNIL)
  463. tag = LUA_TNONE;
  464. else {
  465. lua_assert(ttype(v) == LUA_TNUMBER);
  466. tag = (int)nvalue(v);
  467. }
  468. LUA_UNLOCK(L);
  469. return tag;
  470. }
  471. LUA_API void lua_settag (lua_State *L, int tag) {
  472. int basictype;
  473. LUA_LOCK(L);
  474. if (tag < 0 || tag >= G(L)->ntag)
  475. luaO_verror(L, "%d is not a valid tag", tag);
  476. basictype = G(L)->TMtable[tag].basictype;
  477. if (basictype != LUA_TNONE && basictype != ttype(L->top-1))
  478. luaO_verror(L, "tag %d can only be used for type '%.20s'", tag,
  479. basictypename(G(L), basictype));
  480. switch (ttype(L->top-1)) {
  481. case LUA_TTABLE:
  482. hvalue(L->top-1)->htag = tag;
  483. break;
  484. case LUA_TUSERDATA:
  485. tsvalue(L->top-1)->u.d.tag = tag;
  486. break;
  487. default:
  488. luaO_verror(L, "cannot change the tag of a %.20s",
  489. luaT_typename(G(L), L->top-1));
  490. }
  491. LUA_UNLOCK(L);
  492. }
  493. LUA_API void lua_error (lua_State *L, const char *s) {
  494. LUA_LOCK(L);
  495. luaD_error(L, s);
  496. LUA_UNLOCK(L);
  497. }
  498. LUA_API void lua_unref (lua_State *L, int ref) {
  499. LUA_LOCK(L);
  500. if (ref >= 0) {
  501. lua_assert(ref < G(L)->nref && G(L)->refArray[ref].st < 0);
  502. G(L)->refArray[ref].st = G(L)->refFree;
  503. G(L)->refFree = ref;
  504. }
  505. LUA_UNLOCK(L);
  506. }
  507. LUA_API int lua_next (lua_State *L, int index) {
  508. StkId t;
  509. Node *n;
  510. int more;
  511. LUA_LOCK(L);
  512. t = luaA_index(L, index);
  513. lua_assert(ttype(t) == LUA_TTABLE);
  514. n = luaH_next(L, hvalue(t), luaA_index(L, -1));
  515. if (n) {
  516. setkey2obj(L->top-1, n);
  517. setobj(L->top, val(n));
  518. api_incr_top(L);
  519. more = 1;
  520. }
  521. else { /* no more elements */
  522. L->top -= 1; /* remove key */
  523. more = 0;
  524. }
  525. LUA_UNLOCK(L);
  526. return more;
  527. }
  528. LUA_API int lua_getn (lua_State *L, int index) {
  529. Hash *h;
  530. const TObject *value;
  531. int n;
  532. LUA_LOCK(L);
  533. h = hvalue(luaA_index(L, index));
  534. value = luaH_getstr(h, luaS_newliteral(L, "n")); /* = h.n */
  535. if (ttype(value) == LUA_TNUMBER)
  536. n = (int)nvalue(value);
  537. else {
  538. lua_Number max = 0;
  539. int i = h->size;
  540. Node *nd = h->node;
  541. while (i--) {
  542. if (ttype_key(nd) == LUA_TNUMBER &&
  543. ttype(val(nd)) != LUA_TNIL &&
  544. nvalue_key(nd) > max)
  545. max = nvalue_key(nd);
  546. nd++;
  547. }
  548. n = (int)max;
  549. }
  550. LUA_UNLOCK(L);
  551. return n;
  552. }
  553. LUA_API void lua_concat (lua_State *L, int n) {
  554. StkId top;
  555. LUA_LOCK(L);
  556. top = L->top;
  557. luaV_strconc(L, n, top);
  558. L->top = top-(n-1);
  559. luaC_checkGC(L);
  560. LUA_UNLOCK(L);
  561. }
  562. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  563. TString *ts;
  564. void *p;
  565. LUA_LOCK(L);
  566. if (size == 0) size = 1;
  567. ts = luaS_newudata(L, size, NULL);
  568. setuvalue(L->top, ts);
  569. api_incr_top(L);
  570. p = ts->u.d.value;
  571. LUA_UNLOCK(L);
  572. return p;
  573. }