lapi.c 24 KB

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