lapi.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  1. /*
  2. ** $Id: lapi.c,v 2.55.1.5 2008/07/04 18:41:18 roberto Exp $
  3. ** Lua API
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <assert.h>
  7. #include <math.h>
  8. #include <stdarg.h>
  9. #include <string.h>
  10. #define lapi_c
  11. #define LUA_CORE
  12. #include "lua.h"
  13. #include "lapi.h"
  14. #include "ldebug.h"
  15. #include "ldo.h"
  16. #include "lfunc.h"
  17. #include "lgc.h"
  18. #include "lmem.h"
  19. #include "lobject.h"
  20. #include "lstate.h"
  21. #include "lstring.h"
  22. #include "ltable.h"
  23. #include "ltm.h"
  24. #include "lundump.h"
  25. #include "lvm.h"
  26. #include "lrotable.h"
  27. const char lua_ident[] =
  28. "$Lua: " LUA_RELEASE " " LUA_COPYRIGHT " $\n"
  29. "$Authors: " LUA_AUTHORS " $\n"
  30. "$URL: www.lua.org $\n";
  31. #define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base))
  32. #define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject)
  33. #define api_incr_top(L) {api_check(L, L->top < L->ci->top); L->top++;}
  34. static TValue *index2adr (lua_State *L, int idx) {
  35. if (idx > 0) {
  36. TValue *o = L->base + (idx - 1);
  37. api_check(L, idx <= L->ci->top - L->base);
  38. if (o >= L->top) return cast(TValue *, luaO_nilobject);
  39. else return o;
  40. }
  41. else if (idx > LUA_REGISTRYINDEX) {
  42. api_check(L, idx != 0 && -idx <= L->top - L->base);
  43. return L->top + idx;
  44. }
  45. else switch (idx) { /* pseudo-indices */
  46. case LUA_REGISTRYINDEX: return registry(L);
  47. case LUA_ENVIRONINDEX: {
  48. Closure *func = curr_func(L);
  49. sethvalue(L, &L->env, func ? func->c.env : hvalue(gt(L)));
  50. return &L->env;
  51. }
  52. case LUA_GLOBALSINDEX: return gt(L);
  53. default: {
  54. Closure *func = curr_func(L);
  55. if (!func) return cast(TValue *, luaO_nilobject);
  56. idx = LUA_GLOBALSINDEX - idx;
  57. return (idx <= func->c.nupvalues)
  58. ? &func->c.upvalue[idx-1]
  59. : cast(TValue *, luaO_nilobject);
  60. }
  61. }
  62. }
  63. static Table *getcurrenv (lua_State *L) {
  64. if (L->ci == L->base_ci) /* no enclosing function? */
  65. return hvalue(gt(L)); /* use global table as environment */
  66. else {
  67. Closure *func = curr_func(L);
  68. return func ? func->c.env : hvalue(gt(L));
  69. }
  70. }
  71. void luaA_pushobject (lua_State *L, const TValue *o) {
  72. setobj2s(L, L->top, o);
  73. api_incr_top(L);
  74. }
  75. LUA_API int lua_checkstack (lua_State *L, int size) {
  76. int res = 1;
  77. lua_lock(L);
  78. if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK)
  79. res = 0; /* stack overflow */
  80. else if (size > 0) {
  81. luaD_checkstack(L, size);
  82. if (L->ci->top < L->top + size)
  83. L->ci->top = L->top + size;
  84. }
  85. lua_unlock(L);
  86. return res;
  87. }
  88. LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
  89. int i;
  90. if (from == to) return;
  91. lua_lock(to);
  92. api_checknelems(from, n);
  93. api_check(from, G(from) == G(to));
  94. api_check(from, to->ci->top - to->top >= n);
  95. from->top -= n;
  96. for (i = 0; i < n; i++) {
  97. setobj2s(to, to->top++, from->top + i);
  98. }
  99. lua_unlock(to);
  100. }
  101. LUA_API void lua_setlevel (lua_State *from, lua_State *to) {
  102. to->nCcalls = from->nCcalls;
  103. }
  104. LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
  105. lua_CFunction old;
  106. lua_lock(L);
  107. old = G(L)->panic;
  108. G(L)->panic = panicf;
  109. lua_unlock(L);
  110. return old;
  111. }
  112. LUA_API lua_State *lua_newthread (lua_State *L) {
  113. lua_State *L1;
  114. lua_lock(L);
  115. luaC_checkGC(L);
  116. L1 = luaE_newthread(L);
  117. setthvalue(L, L->top, L1);
  118. api_incr_top(L);
  119. lua_unlock(L);
  120. luai_userstatethread(L, L1);
  121. return L1;
  122. }
  123. /*
  124. ** basic stack manipulation
  125. */
  126. LUA_API int lua_gettop (lua_State *L) {
  127. return cast_int(L->top - L->base);
  128. }
  129. LUA_API void lua_settop (lua_State *L, int idx) {
  130. lua_lock(L);
  131. if (idx >= 0) {
  132. api_check(L, idx <= L->stack_last - L->base);
  133. while (L->top < L->base + idx)
  134. setnilvalue(L->top++);
  135. L->top = L->base + idx;
  136. }
  137. else {
  138. api_check(L, -(idx+1) <= (L->top - L->base));
  139. L->top += idx+1; /* `subtract' index (index is negative) */
  140. }
  141. lua_unlock(L);
  142. }
  143. LUA_API void lua_remove (lua_State *L, int idx) {
  144. StkId p;
  145. lua_lock(L);
  146. p = index2adr(L, idx);
  147. api_checkvalidindex(L, p);
  148. while (++p < L->top) setobjs2s(L, p-1, p);
  149. L->top--;
  150. lua_unlock(L);
  151. }
  152. LUA_API void lua_insert (lua_State *L, int idx) {
  153. StkId p;
  154. StkId q;
  155. lua_lock(L);
  156. p = index2adr(L, idx);
  157. api_checkvalidindex(L, p);
  158. for (q = L->top; q>p; q--) setobjs2s(L, q, q-1);
  159. setobjs2s(L, p, L->top);
  160. lua_unlock(L);
  161. }
  162. LUA_API void lua_replace (lua_State *L, int idx) {
  163. StkId o;
  164. lua_lock(L);
  165. /* explicit test for incompatible code */
  166. if (idx == LUA_ENVIRONINDEX && L->ci == L->base_ci)
  167. luaG_runerror(L, "no calling environment");
  168. api_checknelems(L, 1);
  169. o = index2adr(L, idx);
  170. api_checkvalidindex(L, o);
  171. if (idx == LUA_ENVIRONINDEX) {
  172. Closure *func = curr_func(L);
  173. if (!func)
  174. luaG_runerror(L, "attempt to set environment on lightfunction");
  175. else {
  176. api_check(L, ttistable(L->top - 1));
  177. func->c.env = hvalue(L->top - 1);
  178. luaC_barrier(L, func, L->top - 1);
  179. }
  180. }
  181. else {
  182. setobj(L, o, L->top - 1);
  183. if (curr_func(L) && idx < LUA_GLOBALSINDEX) /* function upvalue? */
  184. luaC_barrier(L, curr_func(L), L->top - 1);
  185. }
  186. L->top--;
  187. lua_unlock(L);
  188. }
  189. LUA_API void lua_pushvalue (lua_State *L, int idx) {
  190. lua_lock(L);
  191. setobj2s(L, L->top, index2adr(L, idx));
  192. api_incr_top(L);
  193. lua_unlock(L);
  194. }
  195. /*
  196. ** access functions (stack -> C)
  197. */
  198. LUA_API int lua_type (lua_State *L, int idx) {
  199. StkId o = index2adr(L, idx);
  200. return (o == luaO_nilobject) ? LUA_TNONE : ttype(o);
  201. }
  202. LUA_API const char *lua_typename (lua_State *L, int t) {
  203. UNUSED(L);
  204. return (t == LUA_TNONE) ? "no value" : luaT_typenames[t];
  205. }
  206. LUA_API int lua_iscfunction (lua_State *L, int idx) {
  207. StkId o = index2adr(L, idx);
  208. return iscfunction(o);
  209. }
  210. LUA_API int lua_isnumber (lua_State *L, int idx) {
  211. TValue n;
  212. const TValue *o = index2adr(L, idx);
  213. return tonumber(o, &n);
  214. }
  215. LUA_API int lua_isstring (lua_State *L, int idx) {
  216. int t = lua_type(L, idx);
  217. return (t == LUA_TSTRING || t == LUA_TNUMBER);
  218. }
  219. LUA_API int lua_isuserdata (lua_State *L, int idx) {
  220. const TValue *o = index2adr(L, idx);
  221. return (ttisuserdata(o) || ttislightuserdata(o));
  222. }
  223. LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
  224. StkId o1 = index2adr(L, index1);
  225. StkId o2 = index2adr(L, index2);
  226. return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
  227. : luaO_rawequalObj(o1, o2);
  228. }
  229. LUA_API int lua_equal (lua_State *L, int index1, int index2) {
  230. StkId o1, o2;
  231. int i;
  232. lua_lock(L); /* may call tag method */
  233. o1 = index2adr(L, index1);
  234. o2 = index2adr(L, index2);
  235. i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 : equalobj(L, o1, o2);
  236. lua_unlock(L);
  237. return i;
  238. }
  239. LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
  240. StkId o1, o2;
  241. int i;
  242. lua_lock(L); /* may call tag method */
  243. o1 = index2adr(L, index1);
  244. o2 = index2adr(L, index2);
  245. i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
  246. : luaV_lessthan(L, o1, o2);
  247. lua_unlock(L);
  248. return i;
  249. }
  250. LUA_API lua_Number lua_tonumber (lua_State *L, int idx) {
  251. TValue n;
  252. const TValue *o = index2adr(L, idx);
  253. if (tonumber(o, &n))
  254. return nvalue(o);
  255. else
  256. return 0;
  257. }
  258. LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) {
  259. TValue n;
  260. const TValue *o = index2adr(L, idx);
  261. if (tonumber(o, &n)) {
  262. lua_Integer res;
  263. lua_Number num = nvalue(o);
  264. lua_number2integer(res, num);
  265. return res;
  266. }
  267. else
  268. return 0;
  269. }
  270. LUA_API int lua_toboolean (lua_State *L, int idx) {
  271. const TValue *o = index2adr(L, idx);
  272. return !l_isfalse(o);
  273. }
  274. LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
  275. StkId o = index2adr(L, idx);
  276. if (!ttisstring(o)) {
  277. lua_lock(L); /* `luaV_tostring' may create a new string */
  278. if (!luaV_tostring(L, o)) { /* conversion failed? */
  279. if (len != NULL) *len = 0;
  280. lua_unlock(L);
  281. return NULL;
  282. }
  283. luaC_checkGC(L);
  284. o = index2adr(L, idx); /* previous call may reallocate the stack */
  285. lua_unlock(L);
  286. }
  287. if (len != NULL) *len = tsvalue(o)->len;
  288. return svalue(o);
  289. }
  290. LUA_API size_t lua_objlen (lua_State *L, int idx) {
  291. StkId o = index2adr(L, idx);
  292. switch (ttype(o)) {
  293. case LUA_TSTRING: return tsvalue(o)->len;
  294. case LUA_TUSERDATA: return uvalue(o)->len;
  295. case LUA_TTABLE: return luaH_getn(hvalue(o));
  296. case LUA_TROTABLE: return luaH_getn_ro(rvalue(o));
  297. case LUA_TNUMBER: {
  298. size_t l;
  299. lua_lock(L); /* `luaV_tostring' may create a new string */
  300. l = (luaV_tostring(L, o) ? tsvalue(o)->len : 0);
  301. lua_unlock(L);
  302. return l;
  303. }
  304. default: return 0;
  305. }
  306. }
  307. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
  308. StkId o = index2adr(L, idx);
  309. return (!iscfunction(o)) ? NULL : clvalue(o)->c.f;
  310. }
  311. LUA_API void *lua_touserdata (lua_State *L, int idx) {
  312. StkId o = index2adr(L, idx);
  313. switch (ttype(o)) {
  314. case LUA_TUSERDATA: return (rawuvalue(o) + 1);
  315. case LUA_TLIGHTUSERDATA: return pvalue(o);
  316. default: return NULL;
  317. }
  318. }
  319. LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
  320. StkId o = index2adr(L, idx);
  321. return (!ttisthread(o)) ? NULL : thvalue(o);
  322. }
  323. LUA_API const void *lua_topointer (lua_State *L, int idx) {
  324. StkId o = index2adr(L, idx);
  325. switch (ttype(o)) {
  326. case LUA_TTABLE: return hvalue(o);
  327. case LUA_TFUNCTION: return clvalue(o);
  328. case LUA_TTHREAD: return thvalue(o);
  329. case LUA_TUSERDATA:
  330. case LUA_TLIGHTUSERDATA:
  331. return lua_touserdata(L, idx);
  332. case LUA_TROTABLE:
  333. case LUA_TLIGHTFUNCTION:
  334. return pvalue(o);
  335. default: return NULL;
  336. }
  337. }
  338. /*
  339. ** push functions (C -> stack)
  340. */
  341. LUA_API void lua_pushnil (lua_State *L) {
  342. lua_lock(L);
  343. setnilvalue(L->top);
  344. api_incr_top(L);
  345. lua_unlock(L);
  346. }
  347. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  348. lua_lock(L);
  349. setnvalue(L->top, n);
  350. api_incr_top(L);
  351. lua_unlock(L);
  352. }
  353. LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
  354. lua_lock(L);
  355. setnvalue(L->top, cast_num(n));
  356. api_incr_top(L);
  357. lua_unlock(L);
  358. }
  359. LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
  360. lua_lock(L);
  361. luaC_checkGC(L);
  362. setsvalue2s(L, L->top, luaS_newlstr(L, s, len));
  363. api_incr_top(L);
  364. lua_unlock(L);
  365. }
  366. LUA_API void lua_pushrolstring (lua_State *L, const char *s, size_t len) {
  367. lua_lock(L);
  368. luaC_checkGC(L);
  369. setsvalue2s(L, L->top, luaS_newrolstr(L, s, len));
  370. api_incr_top(L);
  371. lua_unlock(L);
  372. }
  373. LUA_API void lua_pushstring (lua_State *L, const char *s) {
  374. if (s == NULL)
  375. lua_pushnil(L);
  376. else
  377. lua_pushlstring(L, s, strlen(s));
  378. }
  379. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  380. va_list argp) {
  381. const char *ret;
  382. lua_lock(L);
  383. luaC_checkGC(L);
  384. ret = luaO_pushvfstring(L, fmt, argp);
  385. lua_unlock(L);
  386. return ret;
  387. }
  388. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  389. const char *ret;
  390. va_list argp;
  391. lua_lock(L);
  392. luaC_checkGC(L);
  393. va_start(argp, fmt);
  394. ret = luaO_pushvfstring(L, fmt, argp);
  395. va_end(argp);
  396. lua_unlock(L);
  397. return ret;
  398. }
  399. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  400. Closure *cl;
  401. lua_lock(L);
  402. luaC_checkGC(L);
  403. api_checknelems(L, n);
  404. cl = luaF_newCclosure(L, n, getcurrenv(L));
  405. cl->c.f = fn;
  406. L->top -= n;
  407. while (n--)
  408. setobj2n(L, &cl->c.upvalue[n], L->top+n);
  409. setclvalue(L, L->top, cl);
  410. lua_assert(iswhite(obj2gco(cl)));
  411. api_incr_top(L);
  412. lua_unlock(L);
  413. }
  414. LUA_API void lua_pushboolean (lua_State *L, int b) {
  415. lua_lock(L);
  416. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  417. api_incr_top(L);
  418. lua_unlock(L);
  419. }
  420. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  421. lua_lock(L);
  422. setpvalue(L->top, p);
  423. api_incr_top(L);
  424. lua_unlock(L);
  425. }
  426. LUA_API void lua_pushrotable (lua_State *L, void *p) {
  427. lua_lock(L);
  428. setrvalue(L->top, p);
  429. api_incr_top(L);
  430. lua_unlock(L);
  431. }
  432. LUA_API void lua_pushlightfunction(lua_State *L, void *p) {
  433. lua_lock(L);
  434. setfvalue(L->top, p);
  435. api_incr_top(L);
  436. lua_unlock(L);
  437. }
  438. LUA_API int lua_pushthread (lua_State *L) {
  439. lua_lock(L);
  440. setthvalue(L, L->top, L);
  441. api_incr_top(L);
  442. lua_unlock(L);
  443. return (G(L)->mainthread == L);
  444. }
  445. /*
  446. ** get functions (Lua -> stack)
  447. */
  448. LUA_API void lua_gettable (lua_State *L, int idx) {
  449. StkId t;
  450. lua_lock(L);
  451. t = index2adr(L, idx);
  452. api_checkvalidindex(L, t);
  453. luaV_gettable(L, t, L->top - 1, L->top - 1);
  454. lua_unlock(L);
  455. }
  456. LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
  457. StkId t;
  458. TValue key;
  459. lua_lock(L);
  460. t = index2adr(L, idx);
  461. api_checkvalidindex(L, t);
  462. fixedstack(L);
  463. setsvalue(L, &key, luaS_new(L, k));
  464. unfixedstack(L);
  465. luaV_gettable(L, t, &key, L->top);
  466. api_incr_top(L);
  467. lua_unlock(L);
  468. }
  469. LUA_API void lua_rawget (lua_State *L, int idx) {
  470. StkId t;
  471. const TValue *res;
  472. lua_lock(L);
  473. t = index2adr(L, idx);
  474. api_check(L, ttistable(t) || ttisrotable(t));
  475. res = ttistable(t) ? luaH_get(hvalue(t), L->top - 1) : luaH_get_ro(rvalue(t), L->top - 1);
  476. setobj2s(L, L->top - 1, res);
  477. lua_unlock(L);
  478. }
  479. LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
  480. StkId o;
  481. lua_lock(L);
  482. o = index2adr(L, idx);
  483. api_check(L, ttistable(o) || ttisrotable(o));
  484. setobj2s(L, L->top, ttistable(o) ? luaH_getnum(hvalue(o), n) : luaH_getnum_ro(rvalue(o), n))
  485. api_incr_top(L);
  486. lua_unlock(L);
  487. }
  488. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  489. lua_lock(L);
  490. luaC_checkGC(L);
  491. sethvalue(L, L->top, luaH_new(L, narray, nrec));
  492. api_incr_top(L);
  493. lua_unlock(L);
  494. }
  495. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  496. const TValue *obj;
  497. Table *mt = NULL;
  498. int res;
  499. lua_lock(L);
  500. obj = index2adr(L, objindex);
  501. switch (ttype(obj)) {
  502. case LUA_TTABLE:
  503. mt = hvalue(obj)->metatable;
  504. break;
  505. case LUA_TUSERDATA:
  506. mt = uvalue(obj)->metatable;
  507. break;
  508. case LUA_TROTABLE:
  509. mt = (Table*)luaR_getmeta(rvalue(obj));
  510. break;
  511. default:
  512. mt = G(L)->mt[ttype(obj)];
  513. break;
  514. }
  515. if (mt == NULL)
  516. res = 0;
  517. else {
  518. if(luaR_isrotable(mt))
  519. setrvalue(L->top, mt)
  520. else
  521. sethvalue(L, L->top, mt)
  522. api_incr_top(L);
  523. res = 1;
  524. }
  525. lua_unlock(L);
  526. return res;
  527. }
  528. LUA_API void lua_getfenv (lua_State *L, int idx) {
  529. StkId o;
  530. lua_lock(L);
  531. o = index2adr(L, idx);
  532. api_checkvalidindex(L, o);
  533. switch (ttype(o)) {
  534. case LUA_TFUNCTION:
  535. sethvalue(L, L->top, clvalue(o)->c.env);
  536. break;
  537. case LUA_TUSERDATA:
  538. sethvalue(L, L->top, uvalue(o)->env);
  539. break;
  540. case LUA_TTHREAD:
  541. setobj2s(L, L->top, gt(thvalue(o)));
  542. break;
  543. default:
  544. setnilvalue(L->top);
  545. break;
  546. }
  547. api_incr_top(L);
  548. lua_unlock(L);
  549. }
  550. /*
  551. ** set functions (stack -> Lua)
  552. */
  553. LUA_API void lua_settable (lua_State *L, int idx) {
  554. StkId t;
  555. lua_lock(L);
  556. api_checknelems(L, 2);
  557. t = index2adr(L, idx);
  558. api_checkvalidindex(L, t);
  559. luaV_settable(L, t, L->top - 2, L->top - 1);
  560. L->top -= 2; /* pop index and value */
  561. lua_unlock(L);
  562. }
  563. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  564. StkId t;
  565. lua_lock(L);
  566. api_checknelems(L, 1);
  567. t = index2adr(L, idx);
  568. api_checkvalidindex(L, t);
  569. setsvalue2s(L, L->top, luaS_new(L, k));
  570. api_incr_top(L);
  571. luaV_settable(L, t, L->top - 1, L->top - 2);
  572. L->top -= 2; /* pop key and value */
  573. lua_unlock(L);
  574. }
  575. LUA_API void lua_rawset (lua_State *L, int idx) {
  576. StkId t;
  577. lua_lock(L);
  578. api_checknelems(L, 2);
  579. t = index2adr(L, idx);
  580. api_check(L, ttistable(t));
  581. fixedstack(L);
  582. setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
  583. unfixedstack(L);
  584. luaC_barriert(L, hvalue(t), L->top-1);
  585. L->top -= 2;
  586. lua_unlock(L);
  587. }
  588. LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
  589. StkId o;
  590. lua_lock(L);
  591. api_checknelems(L, 1);
  592. o = index2adr(L, idx);
  593. api_check(L, ttistable(o));
  594. fixedstack(L);
  595. setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1);
  596. unfixedstack(L);
  597. luaC_barriert(L, hvalue(o), L->top-1);
  598. L->top--;
  599. lua_unlock(L);
  600. }
  601. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  602. TValue *obj;
  603. Table *mt;
  604. int isrometa = 0;
  605. lua_lock(L);
  606. api_checknelems(L, 1);
  607. obj = index2adr(L, objindex);
  608. api_checkvalidindex(L, obj);
  609. if (ttisnil(L->top - 1))
  610. mt = NULL;
  611. else {
  612. api_check(L, ttistable(L->top - 1) || ttisrotable(L->top - 1));
  613. if (ttistable(L->top - 1))
  614. mt = hvalue(L->top - 1);
  615. else {
  616. mt = (Table*)rvalue(L->top - 1);
  617. isrometa = 1;
  618. }
  619. }
  620. switch (ttype(obj)) {
  621. case LUA_TTABLE: {
  622. hvalue(obj)->metatable = mt;
  623. if (mt && !isrometa)
  624. luaC_objbarriert(L, hvalue(obj), mt);
  625. break;
  626. }
  627. case LUA_TUSERDATA: {
  628. uvalue(obj)->metatable = mt;
  629. if (mt && !isrometa)
  630. luaC_objbarrier(L, rawuvalue(obj), mt);
  631. break;
  632. }
  633. default: {
  634. G(L)->mt[ttype(obj)] = mt;
  635. break;
  636. }
  637. }
  638. L->top--;
  639. lua_unlock(L);
  640. return 1;
  641. }
  642. LUA_API int lua_setfenv (lua_State *L, int idx) {
  643. StkId o;
  644. int res = 1;
  645. lua_lock(L);
  646. api_checknelems(L, 1);
  647. o = index2adr(L, idx);
  648. api_checkvalidindex(L, o);
  649. api_check(L, ttistable(L->top - 1));
  650. switch (ttype(o)) {
  651. case LUA_TFUNCTION:
  652. clvalue(o)->c.env = hvalue(L->top - 1);
  653. break;
  654. case LUA_TUSERDATA:
  655. uvalue(o)->env = hvalue(L->top - 1);
  656. break;
  657. case LUA_TTHREAD:
  658. sethvalue(L, gt(thvalue(o)), hvalue(L->top - 1));
  659. break;
  660. default:
  661. res = 0;
  662. break;
  663. }
  664. if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
  665. L->top--;
  666. lua_unlock(L);
  667. return res;
  668. }
  669. /*
  670. ** `load' and `call' functions (run Lua code)
  671. */
  672. #define adjustresults(L,nres) \
  673. { if (nres == LUA_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; }
  674. #define checkresults(L,na,nr) \
  675. api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)))
  676. LUA_API void lua_call (lua_State *L, int nargs, int nresults) {
  677. StkId func;
  678. lua_lock(L);
  679. api_checknelems(L, nargs+1);
  680. checkresults(L, nargs, nresults);
  681. func = L->top - (nargs+1);
  682. luaD_call(L, func, nresults);
  683. adjustresults(L, nresults);
  684. lua_unlock(L);
  685. }
  686. /*
  687. ** Execute a protected call.
  688. */
  689. struct CallS { /* data to `f_call' */
  690. StkId func;
  691. int nresults;
  692. };
  693. static void f_call (lua_State *L, void *ud) {
  694. struct CallS *c = cast(struct CallS *, ud);
  695. luaD_call(L, c->func, c->nresults);
  696. }
  697. LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) {
  698. struct CallS c;
  699. int status;
  700. ptrdiff_t func;
  701. lua_lock(L);
  702. api_checknelems(L, nargs+1);
  703. checkresults(L, nargs, nresults);
  704. if (errfunc == 0)
  705. func = 0;
  706. else {
  707. StkId o = index2adr(L, errfunc);
  708. api_checkvalidindex(L, o);
  709. func = savestack(L, o);
  710. }
  711. c.func = L->top - (nargs+1); /* function to be called */
  712. c.nresults = nresults;
  713. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  714. adjustresults(L, nresults);
  715. lua_unlock(L);
  716. return status;
  717. }
  718. /*
  719. ** Execute a protected C call.
  720. */
  721. struct CCallS { /* data to `f_Ccall' */
  722. lua_CFunction func;
  723. void *ud;
  724. };
  725. static void f_Ccall (lua_State *L, void *ud) {
  726. struct CCallS *c = cast(struct CCallS *, ud);
  727. Closure *cl;
  728. cl = luaF_newCclosure(L, 0, getcurrenv(L));
  729. cl->c.f = c->func;
  730. setclvalue(L, L->top, cl); /* push function */
  731. api_incr_top(L);
  732. setpvalue(L->top, c->ud); /* push only argument */
  733. api_incr_top(L);
  734. luaD_call(L, L->top - 2, 0);
  735. }
  736. LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) {
  737. struct CCallS c;
  738. int status;
  739. lua_lock(L);
  740. c.func = func;
  741. c.ud = ud;
  742. status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0);
  743. lua_unlock(L);
  744. return status;
  745. }
  746. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  747. const char *chunkname) {
  748. ZIO z;
  749. int status;
  750. lua_lock(L);
  751. if (!chunkname) chunkname = "?";
  752. luaZ_init(L, &z, reader, data);
  753. status = luaD_protectedparser(L, &z, chunkname);
  754. lua_unlock(L);
  755. return status;
  756. }
  757. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
  758. int status;
  759. TValue *o;
  760. lua_lock(L);
  761. api_checknelems(L, 1);
  762. o = L->top - 1;
  763. if (isLfunction(o))
  764. status = luaU_dump(L, clvalue(o)->l.p, writer, data, 0);
  765. else
  766. status = 1;
  767. lua_unlock(L);
  768. return status;
  769. }
  770. LUA_API int lua_status (lua_State *L) {
  771. return L->status;
  772. }
  773. /*
  774. ** Garbage-collection function
  775. */
  776. LUA_API int lua_gc (lua_State *L, int what, int data) {
  777. int res = 0;
  778. global_State *g;
  779. lua_lock(L);
  780. g = G(L);
  781. switch (what) {
  782. case LUA_GCSTOP: {
  783. set_block_gc(L);
  784. break;
  785. }
  786. case LUA_GCRESTART: {
  787. unset_block_gc(L);
  788. break;
  789. }
  790. case LUA_GCCOLLECT: {
  791. luaC_fullgc(L);
  792. break;
  793. }
  794. case LUA_GCCOUNT: {
  795. /* GC values are expressed in Kbytes: #bytes/2^10 */
  796. res = cast_int(g->totalbytes >> 10);
  797. break;
  798. }
  799. case LUA_GCCOUNTB: {
  800. res = cast_int(g->totalbytes & 0x3ff);
  801. break;
  802. }
  803. case LUA_GCSTEP: {
  804. if(is_block_gc(L)) {
  805. res = 1; /* gc is block so we need to pretend that the collection cycle finished. */
  806. break;
  807. }
  808. lu_mem a = (cast(lu_mem, data) << 10);
  809. if (a <= g->totalbytes)
  810. g->GCthreshold = g->totalbytes - a;
  811. else
  812. g->GCthreshold = 0;
  813. while (g->GCthreshold <= g->totalbytes) {
  814. luaC_step(L);
  815. if (g->gcstate == GCSpause) { /* end of cycle? */
  816. res = 1; /* signal it */
  817. break;
  818. }
  819. }
  820. break;
  821. }
  822. case LUA_GCSETPAUSE: {
  823. res = g->gcpause;
  824. g->gcpause = data;
  825. break;
  826. }
  827. case LUA_GCSETSTEPMUL: {
  828. res = g->gcstepmul;
  829. g->gcstepmul = data;
  830. break;
  831. }
  832. case LUA_GCSETMEMLIMIT: {
  833. /* GC values are expressed in Kbytes: #bytes/2^10 */
  834. lu_mem new_memlimit = (cast(lu_mem, data) << 10);
  835. if(new_memlimit > 0 && new_memlimit < g->totalbytes) {
  836. /* run a full GC to make totalbytes < the new limit. */
  837. luaC_fullgc(L);
  838. if(new_memlimit < g->totalbytes)
  839. new_memlimit = (g->totalbytes + 1024) & ~(1024-1); /* round up to next multiple of 1024 */
  840. }
  841. g->memlimit = new_memlimit;
  842. /* new memlimit might be > then requested memlimit. */
  843. res = cast_int(new_memlimit >> 10);
  844. break;
  845. }
  846. case LUA_GCGETMEMLIMIT: {
  847. res = cast_int(g->memlimit >> 10);
  848. break;
  849. }
  850. default: res = -1; /* invalid option */
  851. }
  852. lua_unlock(L);
  853. return res;
  854. }
  855. /*
  856. ** miscellaneous functions
  857. */
  858. LUA_API int lua_error (lua_State *L) {
  859. lua_lock(L);
  860. api_checknelems(L, 1);
  861. luaG_errormsg(L);
  862. lua_unlock(L);
  863. return 0; /* to avoid warnings */
  864. }
  865. LUA_API int lua_next (lua_State *L, int idx) {
  866. StkId t;
  867. int more;
  868. lua_lock(L);
  869. t = index2adr(L, idx);
  870. api_check(L, ttistable(t) || ttisrotable(t));
  871. more = ttistable(t) ? luaH_next(L, hvalue(t), L->top - 1) : luaH_next_ro(L, rvalue(t), L->top - 1);
  872. if (more) {
  873. api_incr_top(L);
  874. }
  875. else /* no more elements */
  876. L->top -= 1; /* remove key */
  877. lua_unlock(L);
  878. return more;
  879. }
  880. LUA_API void lua_concat (lua_State *L, int n) {
  881. lua_lock(L);
  882. api_checknelems(L, n);
  883. if (n >= 2) {
  884. luaC_checkGC(L);
  885. luaV_concat(L, n, cast_int(L->top - L->base) - 1);
  886. L->top -= (n-1);
  887. }
  888. else if (n == 0) { /* push empty string */
  889. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  890. api_incr_top(L);
  891. }
  892. /* else n == 1; nothing to do */
  893. lua_unlock(L);
  894. }
  895. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  896. lua_Alloc f;
  897. lua_lock(L);
  898. if (ud) *ud = G(L)->ud;
  899. f = G(L)->frealloc;
  900. lua_unlock(L);
  901. return f;
  902. }
  903. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  904. lua_lock(L);
  905. G(L)->ud = ud;
  906. G(L)->frealloc = f;
  907. lua_unlock(L);
  908. }
  909. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  910. Udata *u;
  911. lua_lock(L);
  912. luaC_checkGC(L);
  913. u = luaS_newudata(L, size, getcurrenv(L));
  914. setuvalue(L, L->top, u);
  915. api_incr_top(L);
  916. lua_unlock(L);
  917. return u + 1;
  918. }
  919. static const char *aux_upvalue (StkId fi, int n, TValue **val) {
  920. Closure *f;
  921. if (!ttisfunction(fi)) return NULL;
  922. f = clvalue(fi);
  923. if (f->c.isC) {
  924. if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
  925. *val = &f->c.upvalue[n-1];
  926. return "";
  927. }
  928. else {
  929. Proto *p = f->l.p;
  930. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  931. *val = f->l.upvals[n-1]->v;
  932. return getstr(p->upvalues[n-1]);
  933. }
  934. }
  935. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  936. const char *name;
  937. TValue *val;
  938. lua_lock(L);
  939. name = aux_upvalue(index2adr(L, funcindex), n, &val);
  940. if (name) {
  941. setobj2s(L, L->top, val);
  942. api_incr_top(L);
  943. }
  944. lua_unlock(L);
  945. return name;
  946. }
  947. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  948. const char *name;
  949. TValue *val;
  950. StkId fi;
  951. lua_lock(L);
  952. fi = index2adr(L, funcindex);
  953. api_checknelems(L, 1);
  954. name = aux_upvalue(fi, n, &val);
  955. if (name) {
  956. L->top--;
  957. setobj(L, val, L->top);
  958. luaC_barrier(L, clvalue(fi), L->top);
  959. }
  960. lua_unlock(L);
  961. return name;
  962. }