lapi.c 14 KB

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