lapi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /*
  2. ** $Id: lapi.c,v 1.182 2002/04/02 20:43:18 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[] =
  20. "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
  21. "$Authors: " LUA_AUTHORS " $\n"
  22. "$URL: www.lua.org $\n";
  23. #ifndef api_check
  24. #define api_check(L, o) ((void)1)
  25. #endif
  26. #define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->ci->base))
  27. #define api_incr_top(L) (api_check(L, L->top<L->ci->top), L->top++)
  28. static TObject *negindex (lua_State *L, int index) {
  29. if (index > LUA_REGISTRYINDEX) {
  30. api_check(L, index != 0 && -index <= L->top - L->ci->base);
  31. return L->top+index;
  32. }
  33. else switch (index) { /* pseudo-indices */
  34. case LUA_REGISTRYINDEX: return registry(L);
  35. case LUA_GLOBALSINDEX: return gt(L);
  36. default: {
  37. TObject *func = (L->ci->base - 1);
  38. index = LUA_GLOBALSINDEX - index;
  39. api_check(L, iscfunction(func) && index <= clvalue(func)->c.nupvalues);
  40. return &clvalue(func)->c.upvalue[index-1];
  41. }
  42. }
  43. }
  44. #define luaA_index(L, index) \
  45. ( (index > 0) ? \
  46. (api_check(L, index <= L->top - L->ci->base), L->ci->base+index-1) : \
  47. negindex(L, index))
  48. static TObject *luaA_indexAcceptable (lua_State *L, int index) {
  49. if (index > 0) {
  50. TObject *o = L->ci->base+(index-1);
  51. api_check(L, index <= L->stack_last - L->ci->base);
  52. if (o >= L->top) return NULL;
  53. else return o;
  54. }
  55. else
  56. return negindex(L, index);
  57. }
  58. void luaA_pushobject (lua_State *L, const TObject *o) {
  59. setobj(L->top, o);
  60. incr_top(L);
  61. }
  62. LUA_API int lua_checkstack (lua_State *L, int size) {
  63. int res;
  64. lua_lock(L);
  65. if ((L->top - L->ci->base + size) > LUA_MAXCSTACK)
  66. res = 0; /* stack overflow */
  67. else {
  68. luaD_checkstack(L, size);
  69. if (L->ci->top < L->top + size)
  70. L->ci->top = L->top + size;
  71. res = 1;
  72. }
  73. lua_unlock(L);
  74. return res;
  75. }
  76. /*
  77. ** basic stack manipulation
  78. */
  79. LUA_API int lua_gettop (lua_State *L) {
  80. return (L->top - L->ci->base);
  81. }
  82. LUA_API void lua_settop (lua_State *L, int index) {
  83. lua_lock(L);
  84. if (index >= 0) {
  85. api_check(L, index <= L->stack_last - L->ci->base);
  86. while (L->top < L->ci->base + index)
  87. setnilvalue(L->top++);
  88. L->top = L->ci->base + index;
  89. }
  90. else {
  91. api_check(L, -(index+1) <= (L->top - L->ci->base));
  92. L->top += index+1; /* `subtract' index (index is negative) */
  93. }
  94. lua_unlock(L);
  95. }
  96. LUA_API void lua_remove (lua_State *L, int index) {
  97. StkId p;
  98. lua_lock(L);
  99. p = luaA_index(L, index);
  100. while (++p < L->top) setobj(p-1, p);
  101. L->top--;
  102. lua_unlock(L);
  103. }
  104. LUA_API void lua_insert (lua_State *L, int index) {
  105. StkId p;
  106. StkId q;
  107. lua_lock(L);
  108. p = luaA_index(L, index);
  109. for (q = L->top; q>p; q--) setobj(q, q-1);
  110. setobj(p, L->top);
  111. lua_unlock(L);
  112. }
  113. LUA_API void lua_replace (lua_State *L, int index) {
  114. lua_lock(L);
  115. api_checknelems(L, 1);
  116. setobj(luaA_index(L, index), L->top - 1);
  117. L->top--;
  118. lua_unlock(L);
  119. }
  120. LUA_API void lua_pushvalue (lua_State *L, int index) {
  121. lua_lock(L);
  122. setobj(L->top, luaA_index(L, index));
  123. api_incr_top(L);
  124. lua_unlock(L);
  125. }
  126. /*
  127. ** access functions (stack -> C)
  128. */
  129. LUA_API int lua_type (lua_State *L, int index) {
  130. StkId o = luaA_indexAcceptable(L, index);
  131. return (o == NULL) ? LUA_TNONE : ttype(o);
  132. }
  133. LUA_API const char *lua_typename (lua_State *L, int t) {
  134. UNUSED(L);
  135. return (t == LUA_TNONE) ? "no value" : luaT_typenames[t];
  136. }
  137. LUA_API int lua_iscfunction (lua_State *L, int index) {
  138. StkId o = luaA_indexAcceptable(L, index);
  139. return (o == NULL) ? 0 : iscfunction(o);
  140. }
  141. LUA_API int lua_isnumber (lua_State *L, int index) {
  142. TObject n;
  143. const TObject *o = luaA_indexAcceptable(L, index);
  144. return (o != NULL && tonumber(o, &n));
  145. }
  146. LUA_API int lua_isstring (lua_State *L, int index) {
  147. int t = lua_type(L, index);
  148. return (t == LUA_TSTRING || t == LUA_TNUMBER);
  149. }
  150. LUA_API int lua_equal (lua_State *L, int index1, int index2) {
  151. StkId o1 = luaA_indexAcceptable(L, index1);
  152. StkId o2 = luaA_indexAcceptable(L, index2);
  153. return (o1 == NULL || o2 == NULL) ? 0 /* index out of range */
  154. : luaO_equalObj(o1, o2);
  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); /* may call tag method */
  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. TObject n;
  169. const TObject *o = luaA_indexAcceptable(L, index);
  170. if (o != NULL && tonumber(o, &n))
  171. return nvalue(o);
  172. else
  173. return 0;
  174. }
  175. LUA_API int lua_toboolean (lua_State *L, int index) {
  176. const TObject *o = luaA_indexAcceptable(L, index);
  177. return (o != NULL) && !l_isfalse(o);
  178. }
  179. LUA_API const char *lua_tostring (lua_State *L, int index) {
  180. StkId o = luaA_indexAcceptable(L, index);
  181. if (o == NULL)
  182. return NULL;
  183. else if (ttype(o) == LUA_TSTRING)
  184. return svalue(o);
  185. else {
  186. const char *s;
  187. lua_lock(L); /* `luaV_tostring' may create a new string */
  188. s = (luaV_tostring(L, o) ? svalue(o) : NULL);
  189. lua_unlock(L);
  190. return s;
  191. }
  192. }
  193. LUA_API size_t lua_strlen (lua_State *L, int index) {
  194. StkId o = luaA_indexAcceptable(L, index);
  195. if (o == NULL)
  196. return 0;
  197. else if (ttype(o) == LUA_TSTRING)
  198. return tsvalue(o)->tsv.len;
  199. else {
  200. size_t l;
  201. lua_lock(L); /* `luaV_tostring' may create a new string */
  202. l = (luaV_tostring(L, o) ? tsvalue(o)->tsv.len : 0);
  203. lua_unlock(L);
  204. return l;
  205. }
  206. }
  207. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index) {
  208. StkId o = luaA_indexAcceptable(L, index);
  209. return (o == NULL || !iscfunction(o)) ? NULL : clvalue(o)->c.f;
  210. }
  211. LUA_API void *lua_touserdata (lua_State *L, int index) {
  212. StkId o = luaA_indexAcceptable(L, index);
  213. if (o == NULL) return NULL;
  214. switch (ttype(o)) {
  215. case LUA_TUSERDATA: return (uvalue(o) + 1);
  216. case LUA_TUDATAVAL: return pvalue(o);
  217. default: return NULL;
  218. }
  219. }
  220. LUA_API const void *lua_topointer (lua_State *L, int index) {
  221. StkId o = luaA_indexAcceptable(L, index);
  222. if (o == NULL) return NULL;
  223. else {
  224. switch (ttype(o)) {
  225. case LUA_TTABLE: return hvalue(o);
  226. case LUA_TFUNCTION: return clvalue(o);
  227. default: return NULL;
  228. }
  229. }
  230. }
  231. /*
  232. ** push functions (C -> stack)
  233. */
  234. LUA_API void lua_pushnil (lua_State *L) {
  235. lua_lock(L);
  236. setnilvalue(L->top);
  237. api_incr_top(L);
  238. lua_unlock(L);
  239. }
  240. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  241. lua_lock(L);
  242. setnvalue(L->top, n);
  243. api_incr_top(L);
  244. lua_unlock(L);
  245. }
  246. LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
  247. lua_lock(L);
  248. setsvalue(L->top, luaS_newlstr(L, s, len));
  249. api_incr_top(L);
  250. lua_unlock(L);
  251. }
  252. LUA_API void lua_pushstring (lua_State *L, const char *s) {
  253. if (s == NULL)
  254. lua_pushnil(L);
  255. else
  256. lua_pushlstring(L, s, strlen(s));
  257. }
  258. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  259. Closure *cl;
  260. lua_lock(L);
  261. api_checknelems(L, n);
  262. cl = luaF_newCclosure(L, n);
  263. cl->c.f = fn;
  264. L->top -= n;
  265. while (n--)
  266. setobj(&cl->c.upvalue[n], L->top+n);
  267. setclvalue(L->top, cl);
  268. api_incr_top(L);
  269. lua_unlock(L);
  270. }
  271. LUA_API void lua_pushboolean (lua_State *L, int b) {
  272. lua_lock(L);
  273. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  274. api_incr_top(L);
  275. lua_unlock(L);
  276. }
  277. LUA_API void lua_pushudataval (lua_State *L, void *p) {
  278. lua_lock(L);
  279. setpvalue(L->top, p);
  280. api_incr_top(L);
  281. lua_unlock(L);
  282. }
  283. /*
  284. ** get functions (Lua -> stack)
  285. */
  286. LUA_API void lua_gettable (lua_State *L, int index) {
  287. StkId t;
  288. lua_lock(L);
  289. t = luaA_index(L, index);
  290. luaV_gettable(L, t, L->top-1, L->top-1);
  291. lua_unlock(L);
  292. }
  293. LUA_API void lua_rawget (lua_State *L, int index) {
  294. StkId t;
  295. lua_lock(L);
  296. t = luaA_index(L, index);
  297. api_check(L, ttype(t) == LUA_TTABLE);
  298. setobj(L->top - 1, luaH_get(hvalue(t), L->top - 1));
  299. lua_unlock(L);
  300. }
  301. LUA_API void lua_rawgeti (lua_State *L, int index, int n) {
  302. StkId o;
  303. lua_lock(L);
  304. o = luaA_index(L, index);
  305. api_check(L, ttype(o) == LUA_TTABLE);
  306. setobj(L->top, luaH_getnum(hvalue(o), n));
  307. api_incr_top(L);
  308. lua_unlock(L);
  309. }
  310. LUA_API void lua_newtable (lua_State *L) {
  311. lua_lock(L);
  312. sethvalue(L->top, luaH_new(L, 0, 0));
  313. api_incr_top(L);
  314. lua_unlock(L);
  315. }
  316. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  317. StkId obj;
  318. Table *mt;
  319. int res;
  320. lua_lock(L);
  321. obj = luaA_indexAcceptable(L, objindex);
  322. switch (ttype(obj)) {
  323. case LUA_TTABLE:
  324. mt = hvalue(obj)->metatable;
  325. break;
  326. case LUA_TUSERDATA:
  327. mt = uvalue(obj)->uv.metatable;
  328. break;
  329. default:
  330. mt = hvalue(defaultmeta(L));
  331. }
  332. if (mt == hvalue(defaultmeta(L)))
  333. res = 0;
  334. else {
  335. sethvalue(L->top, mt);
  336. api_incr_top(L);
  337. res = 1;
  338. }
  339. lua_unlock(L);
  340. return res;
  341. }
  342. /*
  343. ** set functions (stack -> Lua)
  344. */
  345. LUA_API void lua_settable (lua_State *L, int index) {
  346. StkId t;
  347. lua_lock(L);
  348. api_checknelems(L, 2);
  349. t = luaA_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. api_checknelems(L, 2);
  358. t = luaA_index(L, index);
  359. api_check(L, ttype(t) == LUA_TTABLE);
  360. 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. api_checknelems(L, 1);
  368. o = luaA_index(L, index);
  369. api_check(L, ttype(o) == LUA_TTABLE);
  370. luaH_setnum(L, hvalue(o), n, L->top-1);
  371. L->top--;
  372. lua_unlock(L);
  373. }
  374. LUA_API void lua_setmetatable (lua_State *L, int objindex) {
  375. StkId obj, mt;
  376. lua_lock(L);
  377. api_checknelems(L, 1);
  378. obj = luaA_index(L, objindex);
  379. mt = --L->top;
  380. if (ttype(mt) == LUA_TNIL)
  381. mt = defaultmeta(L);
  382. api_check(L, ttype(mt) == LUA_TTABLE);
  383. switch (ttype(obj)) {
  384. case LUA_TTABLE:
  385. hvalue(obj)->metatable = hvalue(mt);
  386. break;
  387. case LUA_TUSERDATA:
  388. uvalue(obj)->uv.metatable = hvalue(mt);
  389. break;
  390. default:
  391. luaO_verror(L, "cannot change the meta table of a %.20s",
  392. luaT_typenames[ttype(obj)]);
  393. }
  394. lua_unlock(L);
  395. }
  396. /*
  397. ** `do' functions (run Lua code)
  398. */
  399. LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
  400. StkId func;
  401. lua_lock(L);
  402. api_checknelems(L, nargs+1);
  403. func = L->top - (nargs+1);
  404. luaD_call(L, func, nresults);
  405. lua_unlock(L);
  406. }
  407. LUA_API int lua_dofile (lua_State *L, const char *filename) {
  408. int status;
  409. status = lua_loadfile(L, filename);
  410. if (status == 0) /* parse OK? */
  411. status = lua_call(L, 0, LUA_MULTRET); /* call main */
  412. return status;
  413. }
  414. LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size,
  415. const char *name) {
  416. int status;
  417. status = lua_loadbuffer(L, buff, size, name);
  418. if (status == 0) /* parse OK? */
  419. status = lua_call(L, 0, LUA_MULTRET); /* call main */
  420. return status;
  421. }
  422. LUA_API int lua_dostring (lua_State *L, const char *str) {
  423. return lua_dobuffer(L, str, strlen(str), str);
  424. }
  425. /*
  426. ** Garbage-collection functions
  427. */
  428. /* GC values are expressed in Kbytes: #bytes/2^10 */
  429. #define GCscale(x) (cast(int, (x)>>10))
  430. #define GCunscale(x) (cast(lu_mem, (x)<<10))
  431. LUA_API int lua_getgcthreshold (lua_State *L) {
  432. int threshold;
  433. lua_lock(L);
  434. threshold = GCscale(G(L)->GCthreshold);
  435. lua_unlock(L);
  436. return threshold;
  437. }
  438. LUA_API int lua_getgccount (lua_State *L) {
  439. int count;
  440. lua_lock(L);
  441. count = GCscale(G(L)->nblocks);
  442. lua_unlock(L);
  443. return count;
  444. }
  445. LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
  446. lua_lock(L);
  447. if (newthreshold > GCscale(ULONG_MAX))
  448. G(L)->GCthreshold = ULONG_MAX;
  449. else
  450. G(L)->GCthreshold = GCunscale(newthreshold);
  451. luaC_checkGC(L);
  452. lua_unlock(L);
  453. }
  454. /*
  455. ** miscellaneous functions
  456. */
  457. LUA_API void lua_error (lua_State *L, const char *s) {
  458. lua_lock(L);
  459. luaD_error(L, s);
  460. lua_unlock(L);
  461. }
  462. LUA_API int lua_next (lua_State *L, int index) {
  463. StkId t;
  464. int more;
  465. lua_lock(L);
  466. t = luaA_index(L, index);
  467. api_check(L, ttype(t) == LUA_TTABLE);
  468. more = luaH_next(L, hvalue(t), L->top - 1);
  469. if (more) {
  470. api_incr_top(L);
  471. }
  472. else /* no more elements */
  473. L->top -= 1; /* remove key */
  474. lua_unlock(L);
  475. return more;
  476. }
  477. LUA_API int lua_getn (lua_State *L, int index) {
  478. StkId t;
  479. const TObject *value;
  480. int n;
  481. lua_lock(L);
  482. t = luaA_index(L, index);
  483. api_check(L, ttype(t) == LUA_TTABLE);
  484. value = luaH_getstr(hvalue(t), luaS_newliteral(L, "n")); /* = t.n */
  485. if (ttype(value) == LUA_TNUMBER)
  486. lua_number2int(n, nvalue(value));
  487. else {
  488. Node *nd;
  489. Table *a = hvalue(t);
  490. lua_Number max = 0;
  491. int i;
  492. i = sizearray(a);
  493. while (i--) {
  494. if (ttype(&a->array[i]) != LUA_TNIL)
  495. break;
  496. }
  497. max = i+1;
  498. i = sizenode(a);
  499. nd = a->node;
  500. while (i--) {
  501. if (ttype(key(nd)) == LUA_TNUMBER &&
  502. ttype(val(nd)) != LUA_TNIL &&
  503. nvalue(key(nd)) > max)
  504. max = nvalue(key(nd));
  505. nd++;
  506. }
  507. lua_number2int(n, max);
  508. }
  509. lua_unlock(L);
  510. return n;
  511. }
  512. LUA_API void lua_concat (lua_State *L, int n) {
  513. lua_lock(L);
  514. api_checknelems(L, n);
  515. if (n >= 2) {
  516. luaV_strconc(L, n, L->top - L->ci->base - 1);
  517. L->top -= (n-1);
  518. luaC_checkGC(L);
  519. }
  520. else if (n == 0) { /* push null string */
  521. setsvalue(L->top, luaS_newlstr(L, NULL, 0));
  522. api_incr_top(L);
  523. }
  524. /* else n == 1; nothing to do */
  525. lua_unlock(L);
  526. }
  527. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  528. Udata *u;
  529. lua_lock(L);
  530. u = luaS_newudata(L, size);
  531. setuvalue(L->top, u);
  532. api_incr_top(L);
  533. lua_unlock(L);
  534. return u + 1;
  535. }
  536. LUA_API int lua_pushupvalues (lua_State *L) {
  537. TObject *func;
  538. int n, i;
  539. lua_lock(L);
  540. func = (L->ci->base - 1);
  541. api_check(L, iscfunction(func));
  542. n = clvalue(func)->c.nupvalues;
  543. luaD_checkstack(L, n + LUA_MINSTACK);
  544. for (i=0; i<n; i++) {
  545. setobj(L->top, &clvalue(func)->c.upvalue[i]);
  546. L->top++;
  547. }
  548. lua_unlock(L);
  549. return n;
  550. }