lapi.c 15 KB

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