lapi.c 15 KB

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