lapi.c 15 KB

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