lapi.c 25 KB

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