lapi.c 24 KB

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