lapi.c 15 KB

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