lapi.c 14 KB

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