lapi.c 15 KB

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