lapi.c 14 KB

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