lapi.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  1. /*
  2. ** $Id: lapi.c,v 2.274 2017/11/03 12:12:30 roberto Exp roberto $
  3. ** Lua API
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lapi_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <limits.h>
  10. #include <stdarg.h>
  11. #include <string.h>
  12. #include "lua.h"
  13. #include "lapi.h"
  14. #include "ldebug.h"
  15. #include "ldo.h"
  16. #include "lfunc.h"
  17. #include "lgc.h"
  18. #include "lmem.h"
  19. #include "lobject.h"
  20. #include "lstate.h"
  21. #include "lstring.h"
  22. #include "ltable.h"
  23. #include "ltm.h"
  24. #include "lundump.h"
  25. #include "lvm.h"
  26. const char lua_ident[] =
  27. "$LuaVersion: " LUA_COPYRIGHT " $"
  28. "$LuaAuthors: " LUA_AUTHORS " $";
  29. /* value at a non-valid index */
  30. #define NONVALIDVALUE cast(TValue *, luaO_nilobject)
  31. /* corresponding test */
  32. #define isvalid(o) ((o) != luaO_nilobject)
  33. /* test for pseudo index */
  34. #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX)
  35. /* test for upvalue */
  36. #define isupvalue(i) ((i) < LUA_REGISTRYINDEX)
  37. /* test for valid but not pseudo index */
  38. #define isstackindex(i, o) (isvalid(o) && !ispseudo(i))
  39. #define api_checkvalidindex(l,o) api_check(l, isvalid(o), "invalid index")
  40. #define api_checkstackindex(l, i, o) \
  41. api_check(l, isstackindex(i, o), "index not in the stack")
  42. static TValue *index2value (lua_State *L, int idx) {
  43. if (idx > 0) {
  44. StkId o = L->func + idx;
  45. api_check(L, idx < L->func->stkci.framesize, "unacceptable index");
  46. if (o >= L->top) return NONVALIDVALUE;
  47. else return s2v(o);
  48. }
  49. else if (!ispseudo(idx)) { /* negative index */
  50. api_check(L, idx != 0 && -idx < L->func->stkci.framesize, "invalid index");
  51. return s2v(L->top + idx);
  52. }
  53. else if (idx == LUA_REGISTRYINDEX)
  54. return &G(L)->l_registry;
  55. else { /* upvalues */
  56. idx = LUA_REGISTRYINDEX - idx;
  57. api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
  58. if (ttislcf(s2v(L->func))) /* light C function? */
  59. return NONVALIDVALUE; /* it has no upvalues */
  60. else {
  61. CClosure *func = clCvalue(s2v(L->func));
  62. return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE;
  63. }
  64. }
  65. }
  66. static StkId index2stack (lua_State *L, int idx) {
  67. if (idx > 0) {
  68. StkId o = L->func + idx;
  69. api_check(L, o < L->top, "unacceptable index");
  70. return o;
  71. }
  72. else { /* non-positive index */
  73. api_check(L, idx != 0 && -idx <= L->top - (L->func + 1), "invalid index");
  74. api_check(L, !ispseudo(idx), "invalid index");
  75. return L->top + idx;
  76. }
  77. }
  78. /*
  79. ** to be called by 'lua_checkstack' in protected mode, to grow stack
  80. ** capturing memory errors
  81. */
  82. static void growstack (lua_State *L, void *ud) {
  83. int size = *(int *)ud;
  84. luaD_growstack(L, size);
  85. }
  86. LUA_API int lua_checkstack (lua_State *L, int n) {
  87. int res;
  88. int frameuse = L->top - L->func;
  89. lua_lock(L);
  90. api_check(L, n >= 0, "negative 'n'");
  91. if (n >= USHRT_MAX - frameuse)
  92. res = 0; /* frame size overflow */
  93. else if (L->stack_last - L->top > n) /* stack large enough? */
  94. res = 1; /* yes; check is OK */
  95. else { /* no; need to grow stack */
  96. int inuse = cast_int(L->top - L->stack) + EXTRA_STACK;
  97. if (inuse > LUAI_MAXSTACK - n) /* can grow without overflow? */
  98. res = 0; /* no */
  99. else /* try to grow stack */
  100. res = (luaD_rawrunprotected(L, &growstack, &n) == LUA_OK);
  101. }
  102. if (res && L->func->stkci.framesize < frameuse + n)
  103. L->func->stkci.framesize = frameuse + n; /* adjust frame size */
  104. lua_unlock(L);
  105. return res;
  106. }
  107. LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
  108. int i;
  109. if (from == to) return;
  110. lua_lock(to);
  111. api_checknelems(from, n);
  112. api_check(from, G(from) == G(to), "moving among independent states");
  113. api_check(from, functop(to->func) - to->top >= n, "stack overflow");
  114. from->top -= n;
  115. for (i = 0; i < n; i++) {
  116. setobjs2s(to, to->top, from->top + i);
  117. to->top++; /* stack already checked by previous 'api_check' */
  118. }
  119. lua_unlock(to);
  120. }
  121. LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
  122. lua_CFunction old;
  123. lua_lock(L);
  124. old = G(L)->panic;
  125. G(L)->panic = panicf;
  126. lua_unlock(L);
  127. return old;
  128. }
  129. LUA_API const lua_Number *lua_version (lua_State *L) {
  130. static const lua_Number version = LUA_VERSION_NUM;
  131. if (L == NULL) return &version;
  132. else return G(L)->version;
  133. }
  134. /*
  135. ** basic stack manipulation
  136. */
  137. /*
  138. ** convert an acceptable stack index into an absolute index
  139. */
  140. LUA_API int lua_absindex (lua_State *L, int idx) {
  141. return (idx > 0 || ispseudo(idx))
  142. ? idx
  143. : cast_int(L->top - L->func) + idx;
  144. }
  145. LUA_API int lua_gettop (lua_State *L) {
  146. return cast_int(L->top - (L->func + 1));
  147. }
  148. LUA_API void lua_settop (lua_State *L, int idx) {
  149. StkId func = L->func;
  150. lua_lock(L);
  151. if (idx >= 0) {
  152. api_check(L, idx <= L->stack_last - (func + 1), "new top too large");
  153. while (L->top < (func + 1) + idx)
  154. setnilvalue(s2v(L->top++));
  155. L->top = (func + 1) + idx;
  156. }
  157. else {
  158. api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
  159. L->top += idx+1; /* 'subtract' index (index is negative) */
  160. }
  161. lua_unlock(L);
  162. }
  163. /*
  164. ** Reverse the stack segment from 'from' to 'to'
  165. ** (auxiliary to 'lua_rotate')
  166. ** Note that we move(copy) only the value inside the stack.
  167. ** (We do not move addicional fields that may exist.)
  168. */
  169. static void reverse (lua_State *L, StkId from, StkId to) {
  170. for (; from < to; from++, to--) {
  171. TValue temp;
  172. setobj(L, &temp, s2v(from));
  173. setobjs2s(L, from, to);
  174. setobj2s(L, to, &temp);
  175. }
  176. }
  177. /*
  178. ** Let x = AB, where A is a prefix of length 'n'. Then,
  179. ** rotate x n == BA. But BA == (A^r . B^r)^r.
  180. */
  181. LUA_API void lua_rotate (lua_State *L, int idx, int n) {
  182. StkId p, t, m;
  183. lua_lock(L);
  184. t = L->top - 1; /* end of stack segment being rotated */
  185. p = index2stack(L, idx); /* start of segment */
  186. api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
  187. m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */
  188. reverse(L, p, m); /* reverse the prefix with length 'n' */
  189. reverse(L, m + 1, t); /* reverse the suffix */
  190. reverse(L, p, t); /* reverse the entire segment */
  191. lua_unlock(L);
  192. }
  193. LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
  194. TValue *fr, *to;
  195. lua_lock(L);
  196. fr = index2value(L, fromidx);
  197. to = index2value(L, toidx);
  198. api_checkvalidindex(L, to);
  199. setobj(L, to, fr);
  200. if (isupvalue(toidx)) /* function upvalue? */
  201. luaC_barrier(L, clCvalue(s2v(L->func)), fr);
  202. /* LUA_REGISTRYINDEX does not need gc barrier
  203. (collector revisits it before finishing collection) */
  204. lua_unlock(L);
  205. }
  206. LUA_API void lua_pushvalue (lua_State *L, int idx) {
  207. lua_lock(L);
  208. setobj2s(L, L->top, index2value(L, idx));
  209. api_incr_top(L);
  210. lua_unlock(L);
  211. }
  212. /*
  213. ** access functions (stack -> C)
  214. */
  215. LUA_API int lua_type (lua_State *L, int idx) {
  216. const TValue *o = index2value(L, idx);
  217. return (isvalid(o) ? ttnov(o) : LUA_TNONE);
  218. }
  219. LUA_API const char *lua_typename (lua_State *L, int t) {
  220. UNUSED(L);
  221. api_check(L, LUA_TNONE <= t && t < LUA_NUMTAGS, "invalid tag");
  222. return ttypename(t);
  223. }
  224. LUA_API int lua_iscfunction (lua_State *L, int idx) {
  225. const TValue *o = index2value(L, idx);
  226. return (ttislcf(o) || (ttisCclosure(o)));
  227. }
  228. LUA_API int lua_isinteger (lua_State *L, int idx) {
  229. const TValue *o = index2value(L, idx);
  230. return ttisinteger(o);
  231. }
  232. LUA_API int lua_isnumber (lua_State *L, int idx) {
  233. lua_Number n;
  234. const TValue *o = index2value(L, idx);
  235. return tonumber(o, &n);
  236. }
  237. LUA_API int lua_isstring (lua_State *L, int idx) {
  238. const TValue *o = index2value(L, idx);
  239. return (ttisstring(o) || cvt2str(o));
  240. }
  241. LUA_API int lua_isuserdata (lua_State *L, int idx) {
  242. const TValue *o = index2value(L, idx);
  243. return (ttisfulluserdata(o) || ttislightuserdata(o));
  244. }
  245. LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
  246. const TValue *o1 = index2value(L, index1);
  247. const TValue *o2 = index2value(L, index2);
  248. return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0;
  249. }
  250. LUA_API void lua_arith (lua_State *L, int op) {
  251. lua_lock(L);
  252. if (op != LUA_OPUNM && op != LUA_OPBNOT)
  253. api_checknelems(L, 2); /* all other operations expect two operands */
  254. else { /* for unary operations, add fake 2nd operand */
  255. api_checknelems(L, 1);
  256. setobjs2s(L, L->top, L->top - 1);
  257. api_incr_top(L);
  258. }
  259. /* first operand at top - 2, second at top - 1; result go to top - 2 */
  260. luaO_arith(L, op, s2v(L->top - 2), s2v(L->top - 1), L->top - 2);
  261. L->top--; /* remove second operand */
  262. lua_unlock(L);
  263. }
  264. LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
  265. const TValue *o1;
  266. const TValue *o2;
  267. int i = 0;
  268. lua_lock(L); /* may call tag method */
  269. o1 = index2value(L, index1);
  270. o2 = index2value(L, index2);
  271. if (isvalid(o1) && isvalid(o2)) {
  272. switch (op) {
  273. case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break;
  274. case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
  275. case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
  276. default: api_check(L, 0, "invalid option");
  277. }
  278. }
  279. lua_unlock(L);
  280. return i;
  281. }
  282. LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) {
  283. size_t sz = luaO_str2num(s, s2v(L->top));
  284. if (sz != 0)
  285. api_incr_top(L);
  286. return sz;
  287. }
  288. LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) {
  289. lua_Number n;
  290. const TValue *o = index2value(L, idx);
  291. int isnum = tonumber(o, &n);
  292. if (!isnum)
  293. n = 0; /* call to 'tonumber' may change 'n' even if it fails */
  294. if (pisnum) *pisnum = isnum;
  295. return n;
  296. }
  297. LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) {
  298. lua_Integer res;
  299. const TValue *o = index2value(L, idx);
  300. int isnum = tointeger(o, &res);
  301. if (!isnum)
  302. res = 0; /* call to 'tointeger' may change 'n' even if it fails */
  303. if (pisnum) *pisnum = isnum;
  304. return res;
  305. }
  306. LUA_API int lua_toboolean (lua_State *L, int idx) {
  307. const TValue *o = index2value(L, idx);
  308. return !l_isfalse(o);
  309. }
  310. LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
  311. TValue *o = index2value(L, idx);
  312. if (!ttisstring(o)) {
  313. if (!cvt2str(o)) { /* not convertible? */
  314. if (len != NULL) *len = 0;
  315. return NULL;
  316. }
  317. lua_lock(L); /* 'luaO_tostring' may create a new string */
  318. luaO_tostring(L, o);
  319. luaC_checkGC(L);
  320. o = index2value(L, idx); /* previous call may reallocate the stack */
  321. lua_unlock(L);
  322. }
  323. if (len != NULL)
  324. *len = vslen(o);
  325. return svalue(o);
  326. }
  327. LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) {
  328. const TValue *o = index2value(L, idx);
  329. switch (ttype(o)) {
  330. case LUA_TSHRSTR: return tsvalue(o)->shrlen;
  331. case LUA_TLNGSTR: return tsvalue(o)->u.lnglen;
  332. case LUA_TUSERDATA: return uvalue(o)->len;
  333. case LUA_TTABLE: return luaH_getn(hvalue(o));
  334. default: return 0;
  335. }
  336. }
  337. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
  338. const TValue *o = index2value(L, idx);
  339. if (ttislcf(o)) return fvalue(o);
  340. else if (ttisCclosure(o))
  341. return clCvalue(o)->f;
  342. else return NULL; /* not a C function */
  343. }
  344. LUA_API void *lua_touserdata (lua_State *L, int idx) {
  345. const TValue *o = index2value(L, idx);
  346. switch (ttnov(o)) {
  347. case LUA_TUSERDATA: return getudatamem(uvalue(o));
  348. case LUA_TLIGHTUSERDATA: return pvalue(o);
  349. default: return NULL;
  350. }
  351. }
  352. LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
  353. const TValue *o = index2value(L, idx);
  354. return (!ttisthread(o)) ? NULL : thvalue(o);
  355. }
  356. LUA_API const void *lua_topointer (lua_State *L, int idx) {
  357. const TValue *o = index2value(L, idx);
  358. switch (ttype(o)) {
  359. case LUA_TTABLE: return hvalue(o);
  360. case LUA_TLCL: return clLvalue(o);
  361. case LUA_TCCL: return clCvalue(o);
  362. case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o)));
  363. case LUA_TTHREAD: return thvalue(o);
  364. case LUA_TUSERDATA: return getudatamem(uvalue(o));
  365. case LUA_TLIGHTUSERDATA: return pvalue(o);
  366. default: return NULL;
  367. }
  368. }
  369. /*
  370. ** push functions (C -> stack)
  371. */
  372. LUA_API void lua_pushnil (lua_State *L) {
  373. lua_lock(L);
  374. setnilvalue(s2v(L->top));
  375. api_incr_top(L);
  376. lua_unlock(L);
  377. }
  378. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  379. lua_lock(L);
  380. setfltvalue(s2v(L->top), n);
  381. api_incr_top(L);
  382. lua_unlock(L);
  383. }
  384. LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
  385. lua_lock(L);
  386. setivalue(s2v(L->top), n);
  387. api_incr_top(L);
  388. lua_unlock(L);
  389. }
  390. /*
  391. ** Pushes on the stack a string with given length. Avoid using 's' when
  392. ** 'len' == 0 (as 's' can be NULL in that case), due to later use of
  393. ** 'memcmp' and 'memcpy'.
  394. */
  395. LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
  396. TString *ts;
  397. lua_lock(L);
  398. ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len);
  399. setsvalue2s(L, L->top, ts);
  400. api_incr_top(L);
  401. luaC_checkGC(L);
  402. lua_unlock(L);
  403. return getstr(ts);
  404. }
  405. LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
  406. lua_lock(L);
  407. if (s == NULL)
  408. setnilvalue(s2v(L->top));
  409. else {
  410. TString *ts;
  411. ts = luaS_new(L, s);
  412. setsvalue2s(L, L->top, ts);
  413. s = getstr(ts); /* internal copy's address */
  414. }
  415. api_incr_top(L);
  416. luaC_checkGC(L);
  417. lua_unlock(L);
  418. return s;
  419. }
  420. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  421. va_list argp) {
  422. const char *ret;
  423. lua_lock(L);
  424. ret = luaO_pushvfstring(L, fmt, argp);
  425. luaC_checkGC(L);
  426. lua_unlock(L);
  427. return ret;
  428. }
  429. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  430. const char *ret;
  431. va_list argp;
  432. lua_lock(L);
  433. va_start(argp, fmt);
  434. ret = luaO_pushvfstring(L, fmt, argp);
  435. va_end(argp);
  436. luaC_checkGC(L);
  437. lua_unlock(L);
  438. return ret;
  439. }
  440. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  441. lua_lock(L);
  442. if (n == 0) {
  443. setfvalue(s2v(L->top), fn);
  444. }
  445. else {
  446. CClosure *cl;
  447. api_checknelems(L, n);
  448. api_check(L, n <= MAXUPVAL, "upvalue index too large");
  449. cl = luaF_newCclosure(L, n);
  450. cl->f = fn;
  451. L->top -= n;
  452. while (n--) {
  453. setobj2n(L, &cl->upvalue[n], s2v(L->top + n));
  454. /* does not need barrier because closure is white */
  455. }
  456. setclCvalue(L, s2v(L->top), cl);
  457. }
  458. api_incr_top(L);
  459. luaC_checkGC(L);
  460. lua_unlock(L);
  461. }
  462. LUA_API void lua_pushboolean (lua_State *L, int b) {
  463. lua_lock(L);
  464. setbvalue(s2v(L->top), (b != 0)); /* ensure that true is 1 */
  465. api_incr_top(L);
  466. lua_unlock(L);
  467. }
  468. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  469. lua_lock(L);
  470. setpvalue(s2v(L->top), p);
  471. api_incr_top(L);
  472. lua_unlock(L);
  473. }
  474. LUA_API int lua_pushthread (lua_State *L) {
  475. lua_lock(L);
  476. setthvalue(L, s2v(L->top), L);
  477. api_incr_top(L);
  478. lua_unlock(L);
  479. return (G(L)->mainthread == L);
  480. }
  481. /*
  482. ** get functions (Lua -> stack)
  483. */
  484. static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
  485. const TValue *slot;
  486. TString *str = luaS_new(L, k);
  487. if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
  488. setobj2s(L, L->top, slot);
  489. api_incr_top(L);
  490. }
  491. else {
  492. setsvalue2s(L, L->top, str);
  493. api_incr_top(L);
  494. luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot);
  495. }
  496. lua_unlock(L);
  497. return ttnov(s2v(L->top - 1));
  498. }
  499. LUA_API int lua_getglobal (lua_State *L, const char *name) {
  500. Table *reg = hvalue(&G(L)->l_registry);
  501. lua_lock(L);
  502. return auxgetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name);
  503. }
  504. LUA_API int lua_gettable (lua_State *L, int idx) {
  505. const TValue *slot;
  506. TValue *t;
  507. lua_lock(L);
  508. t = index2value(L, idx);
  509. if (luaV_fastget(L, t, s2v(L->top - 1), slot, luaH_get)) {
  510. setobj2s(L, L->top - 1, slot);
  511. }
  512. else
  513. luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot);
  514. lua_unlock(L);
  515. return ttnov(s2v(L->top - 1));
  516. }
  517. LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
  518. lua_lock(L);
  519. return auxgetstr(L, index2value(L, idx), k);
  520. }
  521. LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
  522. TValue *t;
  523. const TValue *slot;
  524. lua_lock(L);
  525. t = index2value(L, idx);
  526. if (luaV_fastgeti(L, t, n, slot)) {
  527. setobj2s(L, L->top, slot);
  528. }
  529. else {
  530. TValue aux;
  531. setivalue(&aux, n);
  532. luaV_finishget(L, t, &aux, L->top, slot);
  533. }
  534. api_incr_top(L);
  535. lua_unlock(L);
  536. return ttnov(s2v(L->top - 1));
  537. }
  538. LUA_API int lua_rawget (lua_State *L, int idx) {
  539. TValue *t;
  540. lua_lock(L);
  541. t = index2value(L, idx);
  542. api_check(L, ttistable(t), "table expected");
  543. setobj2s(L, L->top - 1, luaH_get(hvalue(t), s2v(L->top - 1)));
  544. lua_unlock(L);
  545. return ttnov(s2v(L->top - 1));
  546. }
  547. LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
  548. TValue *t;
  549. lua_lock(L);
  550. t = index2value(L, idx);
  551. api_check(L, ttistable(t), "table expected");
  552. setobj2s(L, L->top, luaH_getint(hvalue(t), n));
  553. api_incr_top(L);
  554. lua_unlock(L);
  555. return ttnov(s2v(L->top - 1));
  556. }
  557. LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
  558. TValue *t;
  559. TValue k;
  560. lua_lock(L);
  561. t = index2value(L, idx);
  562. api_check(L, ttistable(t), "table expected");
  563. setpvalue(&k, cast(void *, p));
  564. setobj2s(L, L->top, luaH_get(hvalue(t), &k));
  565. api_incr_top(L);
  566. lua_unlock(L);
  567. return ttnov(s2v(L->top - 1));
  568. }
  569. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  570. Table *t;
  571. lua_lock(L);
  572. t = luaH_new(L);
  573. sethvalue2s(L, L->top, t);
  574. api_incr_top(L);
  575. if (narray > 0 || nrec > 0)
  576. luaH_resize(L, t, narray, nrec);
  577. luaC_checkGC(L);
  578. lua_unlock(L);
  579. }
  580. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  581. const TValue *obj;
  582. Table *mt;
  583. int res = 0;
  584. lua_lock(L);
  585. obj = index2value(L, objindex);
  586. switch (ttnov(obj)) {
  587. case LUA_TTABLE:
  588. mt = hvalue(obj)->metatable;
  589. break;
  590. case LUA_TUSERDATA:
  591. mt = uvalue(obj)->metatable;
  592. break;
  593. default:
  594. mt = G(L)->mt[ttnov(obj)];
  595. break;
  596. }
  597. if (mt != NULL) {
  598. sethvalue2s(L, L->top, mt);
  599. api_incr_top(L);
  600. res = 1;
  601. }
  602. lua_unlock(L);
  603. return res;
  604. }
  605. LUA_API int lua_getuservalue (lua_State *L, int idx) {
  606. TValue *o;
  607. lua_lock(L);
  608. o = index2value(L, idx);
  609. api_check(L, ttisfulluserdata(o), "full userdata expected");
  610. getuservalue(L, uvalue(o), s2v(L->top));
  611. api_incr_top(L);
  612. lua_unlock(L);
  613. return ttnov(s2v(L->top - 1));
  614. }
  615. /*
  616. ** set functions (stack -> Lua)
  617. */
  618. /*
  619. ** t[k] = value at the top of the stack (where 'k' is a string)
  620. */
  621. static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
  622. const TValue *slot;
  623. TString *str = luaS_new(L, k);
  624. api_checknelems(L, 1);
  625. if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
  626. luaV_finishfastset(L, t, slot, s2v(L->top - 1));
  627. L->top--; /* pop value */
  628. }
  629. else {
  630. setsvalue2s(L, L->top, str); /* push 'str' (to make it a TValue) */
  631. api_incr_top(L);
  632. luaV_finishset(L, t, s2v(L->top - 1), s2v(L->top - 2), slot);
  633. L->top -= 2; /* pop value and key */
  634. }
  635. lua_unlock(L); /* lock done by caller */
  636. }
  637. LUA_API void lua_setglobal (lua_State *L, const char *name) {
  638. Table *reg = hvalue(&G(L)->l_registry);
  639. lua_lock(L); /* unlock done in 'auxsetstr' */
  640. auxsetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name);
  641. }
  642. LUA_API void lua_settable (lua_State *L, int idx) {
  643. TValue *t;
  644. const TValue *slot;
  645. lua_lock(L);
  646. api_checknelems(L, 2);
  647. t = index2value(L, idx);
  648. if (luaV_fastget(L, t, s2v(L->top - 2), slot, luaH_get)) {
  649. luaV_finishfastset(L, t, slot, s2v(L->top - 1));
  650. }
  651. else
  652. luaV_finishset(L, t, s2v(L->top - 2), s2v(L->top - 1), slot);
  653. L->top -= 2; /* pop index and value */
  654. lua_unlock(L);
  655. }
  656. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  657. lua_lock(L); /* unlock done in 'auxsetstr' */
  658. auxsetstr(L, index2value(L, idx), k);
  659. }
  660. LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
  661. TValue *t;
  662. const TValue *slot;
  663. lua_lock(L);
  664. api_checknelems(L, 1);
  665. t = index2value(L, idx);
  666. if (luaV_fastgeti(L, t, n, slot)) {
  667. luaV_finishfastset(L, t, slot, s2v(L->top - 1));
  668. }
  669. else {
  670. TValue aux;
  671. setivalue(&aux, n);
  672. luaV_finishset(L, t, &aux, s2v(L->top - 1), slot);
  673. }
  674. L->top--; /* pop value */
  675. lua_unlock(L);
  676. }
  677. LUA_API void lua_rawset (lua_State *L, int idx) {
  678. TValue *o;
  679. TValue *slot;
  680. lua_lock(L);
  681. api_checknelems(L, 2);
  682. o = index2value(L, idx);
  683. api_check(L, ttistable(o), "table expected");
  684. slot = luaH_set(L, hvalue(o), s2v(L->top - 2));
  685. setobj2t(L, slot, s2v(L->top - 1));
  686. invalidateTMcache(hvalue(o));
  687. luaC_barrierback(L, hvalue(o), s2v(L->top - 1));
  688. L->top -= 2;
  689. lua_unlock(L);
  690. }
  691. LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
  692. TValue *o;
  693. lua_lock(L);
  694. api_checknelems(L, 1);
  695. o = index2value(L, idx);
  696. api_check(L, ttistable(o), "table expected");
  697. luaH_setint(L, hvalue(o), n, s2v(L->top - 1));
  698. luaC_barrierback(L, hvalue(o), s2v(L->top - 1));
  699. L->top--;
  700. lua_unlock(L);
  701. }
  702. LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
  703. TValue *o;
  704. TValue k, *slot;
  705. lua_lock(L);
  706. api_checknelems(L, 1);
  707. o = index2value(L, idx);
  708. api_check(L, ttistable(o), "table expected");
  709. setpvalue(&k, cast(void *, p));
  710. slot = luaH_set(L, hvalue(o), &k);
  711. setobj2t(L, slot, s2v(L->top - 1));
  712. luaC_barrierback(L, hvalue(o), s2v(L->top - 1));
  713. L->top--;
  714. lua_unlock(L);
  715. }
  716. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  717. TValue *obj;
  718. Table *mt;
  719. lua_lock(L);
  720. api_checknelems(L, 1);
  721. obj = index2value(L, objindex);
  722. if (ttisnil(s2v(L->top - 1)))
  723. mt = NULL;
  724. else {
  725. api_check(L, ttistable(s2v(L->top - 1)), "table expected");
  726. mt = hvalue(s2v(L->top - 1));
  727. }
  728. switch (ttnov(obj)) {
  729. case LUA_TTABLE: {
  730. hvalue(obj)->metatable = mt;
  731. if (mt) {
  732. luaC_objbarrier(L, gcvalue(obj), mt);
  733. luaC_checkfinalizer(L, gcvalue(obj), mt);
  734. }
  735. break;
  736. }
  737. case LUA_TUSERDATA: {
  738. uvalue(obj)->metatable = mt;
  739. if (mt) {
  740. luaC_objbarrier(L, uvalue(obj), mt);
  741. luaC_checkfinalizer(L, gcvalue(obj), mt);
  742. }
  743. break;
  744. }
  745. default: {
  746. G(L)->mt[ttnov(obj)] = mt;
  747. break;
  748. }
  749. }
  750. L->top--;
  751. lua_unlock(L);
  752. return 1;
  753. }
  754. LUA_API void lua_setuservalue (lua_State *L, int idx) {
  755. TValue *o;
  756. lua_lock(L);
  757. api_checknelems(L, 1);
  758. o = index2value(L, idx);
  759. api_check(L, ttisfulluserdata(o), "full userdata expected");
  760. setuservalue(L, uvalue(o), s2v(L->top - 1));
  761. luaC_barrier(L, gcvalue(o), s2v(L->top - 1));
  762. L->top--;
  763. lua_unlock(L);
  764. }
  765. /*
  766. ** 'load' and 'call' functions (run Lua code)
  767. */
  768. #define checkresults(L,na,nr) \
  769. api_check(L, (nr) == LUA_MULTRET || \
  770. (functop(L->func) - L->top >= (nr) - (na)), \
  771. "results from function overflow current frame size")
  772. LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
  773. lua_KContext ctx, lua_KFunction k) {
  774. StkId func;
  775. lua_lock(L);
  776. api_check(L, k == NULL || !isLua(L->func),
  777. "cannot use continuations inside hooks");
  778. api_checknelems(L, nargs+1);
  779. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  780. checkresults(L, nargs, nresults);
  781. func = L->top - (nargs+1);
  782. if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
  783. L->func->stkci.u.c.k = k; /* save continuation */
  784. L->func->stkci.u.c.ctx = ctx; /* save context */
  785. luaD_call(L, func, nresults); /* do the call */
  786. }
  787. else /* no continuation or no yieldable */
  788. luaD_callnoyield(L, func, nresults); /* just do the call */
  789. adjustresults(L, nresults);
  790. lua_unlock(L);
  791. }
  792. /*
  793. ** Execute a protected call.
  794. */
  795. struct CallS { /* data to 'f_call' */
  796. StkId func;
  797. int nresults;
  798. };
  799. static void f_call (lua_State *L, void *ud) {
  800. struct CallS *c = cast(struct CallS *, ud);
  801. luaD_callnoyield(L, c->func, c->nresults);
  802. }
  803. LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
  804. lua_KContext ctx, lua_KFunction k) {
  805. struct CallS c;
  806. int status;
  807. ptrdiff_t efunc;
  808. lua_lock(L);
  809. api_check(L, k == NULL || !isLua(L->func),
  810. "cannot use continuations inside hooks");
  811. api_checknelems(L, nargs+1);
  812. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  813. checkresults(L, nargs, nresults);
  814. if (errfunc == 0)
  815. efunc = 0;
  816. else {
  817. StkId o = index2stack(L, errfunc);
  818. efunc = savestack(L, o);
  819. }
  820. c.func = L->top - (nargs+1); /* function to be called */
  821. if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */
  822. c.nresults = nresults; /* do a 'conventional' protected call */
  823. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), efunc);
  824. }
  825. else { /* prepare continuation (call is already protected by 'resume') */
  826. StkId func = L->func;
  827. func->stkci.u.c.k = k; /* save continuation */
  828. func->stkci.u.c.ctx = ctx; /* save context */
  829. /* save information for error recovery */
  830. func->stkci.u2.funcidx = c.func - func;
  831. func->stkci.u.c.old_errfunc = L->errfunc;
  832. L->errfunc = efunc;
  833. setoah(callstatus(func), L->allowhook); /* save value of 'allowhook' */
  834. callstatus(func) |= CIST_YPCALL; /* function can do error recovery */
  835. luaD_call(L, c.func, nresults); /* do the call */
  836. func = L->func; /* previous call can reallocate stack */
  837. callstatus(func) &= ~CIST_YPCALL;
  838. L->errfunc = func->stkci.u.c.old_errfunc;
  839. status = LUA_OK; /* if it is here, there were no errors */
  840. }
  841. adjustresults(L, nresults);
  842. lua_unlock(L);
  843. return status;
  844. }
  845. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  846. const char *chunkname, const char *mode) {
  847. ZIO z;
  848. int status;
  849. lua_lock(L);
  850. if (!chunkname) chunkname = "?";
  851. luaZ_init(L, &z, reader, data);
  852. status = luaD_protectedparser(L, &z, chunkname, mode);
  853. if (status == LUA_OK) { /* no errors? */
  854. LClosure *f = clLvalue(s2v(L->top - 1)); /* get newly created function */
  855. if (f->nupvalues >= 1) { /* does it have an upvalue? */
  856. /* get global table from registry */
  857. Table *reg = hvalue(&G(L)->l_registry);
  858. const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  859. /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
  860. setobj(L, f->upvals[0]->v, gt);
  861. luaC_barrier(L, f->upvals[0], gt);
  862. }
  863. }
  864. lua_unlock(L);
  865. return status;
  866. }
  867. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
  868. int status;
  869. TValue *o;
  870. lua_lock(L);
  871. api_checknelems(L, 1);
  872. o = s2v(L->top - 1);
  873. if (isLfunction(o))
  874. status = luaU_dump(L, getproto(o), writer, data, strip);
  875. else
  876. status = 1;
  877. lua_unlock(L);
  878. return status;
  879. }
  880. LUA_API int lua_status (lua_State *L) {
  881. return L->status;
  882. }
  883. /*
  884. ** Garbage-collection function
  885. */
  886. LUA_API int lua_gc (lua_State *L, int what, ...) {
  887. va_list argp;
  888. int res = 0;
  889. global_State *g = G(L);
  890. lua_lock(L);
  891. va_start(argp, what);
  892. switch (what) {
  893. case LUA_GCSTOP: {
  894. g->gcrunning = 0;
  895. break;
  896. }
  897. case LUA_GCRESTART: {
  898. luaE_setdebt(g, 0);
  899. g->gcrunning = 1;
  900. break;
  901. }
  902. case LUA_GCCOLLECT: {
  903. luaC_fullgc(L, 0);
  904. break;
  905. }
  906. case LUA_GCCOUNT: {
  907. /* GC values are expressed in Kbytes: #bytes/2^10 */
  908. res = cast_int(gettotalbytes(g) >> 10);
  909. break;
  910. }
  911. case LUA_GCCOUNTB: {
  912. res = cast_int(gettotalbytes(g) & 0x3ff);
  913. break;
  914. }
  915. case LUA_GCSTEP: {
  916. int data = va_arg(argp, int);
  917. l_mem debt = 1; /* =1 to signal that it did an actual step */
  918. lu_byte oldrunning = g->gcrunning;
  919. g->gcrunning = 1; /* allow GC to run */
  920. if (data == 0) {
  921. luaE_setdebt(g, 0); /* do a basic step */
  922. luaC_step(L);
  923. }
  924. else { /* add 'data' to total debt */
  925. debt = cast(l_mem, data) * 1024 + g->GCdebt;
  926. luaE_setdebt(g, debt);
  927. luaC_checkGC(L);
  928. }
  929. g->gcrunning = oldrunning; /* restore previous state */
  930. if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */
  931. res = 1; /* signal it */
  932. break;
  933. }
  934. case LUA_GCSETPAUSE: {
  935. int data = va_arg(argp, int);
  936. res = getgcparam(g->gcpause);
  937. setgcparam(g->gcpause, data);
  938. break;
  939. }
  940. case LUA_GCSETSTEPMUL: {
  941. int data = va_arg(argp, int);
  942. res = getgcparam(g->gcstepmul);
  943. setgcparam(g->gcstepmul, data);
  944. break;
  945. }
  946. case LUA_GCISRUNNING: {
  947. res = g->gcrunning;
  948. break;
  949. }
  950. case LUA_GCGEN: {
  951. int minormul = va_arg(argp, int);
  952. int majormul = va_arg(argp, int);
  953. if (minormul != 0)
  954. g->genminormul = minormul;
  955. if (majormul != 0)
  956. setgcparam(g->genmajormul, majormul);
  957. luaC_changemode(L, KGC_GEN);
  958. break;
  959. }
  960. case LUA_GCINC: {
  961. int pause = va_arg(argp, int);
  962. int stepmul = va_arg(argp, int);
  963. int stepsize = va_arg(argp, int);
  964. if (pause != 0)
  965. setgcparam(g->gcpause, pause);
  966. if (stepmul != 0)
  967. setgcparam(g->gcstepmul, stepmul);
  968. if (stepsize != 0)
  969. g->gcstepsize = stepsize;
  970. luaC_changemode(L, KGC_INC);
  971. break;
  972. }
  973. default: res = -1; /* invalid option */
  974. }
  975. va_end(argp);
  976. lua_unlock(L);
  977. return res;
  978. }
  979. /*
  980. ** miscellaneous functions
  981. */
  982. LUA_API int lua_error (lua_State *L) {
  983. lua_lock(L);
  984. api_checknelems(L, 1);
  985. luaG_errormsg(L);
  986. /* code unreachable; will unlock when control actually leaves the kernel */
  987. return 0; /* to avoid warnings */
  988. }
  989. LUA_API int lua_next (lua_State *L, int idx) {
  990. TValue *t;
  991. int more;
  992. lua_lock(L);
  993. t = index2value(L, idx);
  994. api_check(L, ttistable(t), "table expected");
  995. more = luaH_next(L, hvalue(t), L->top - 1);
  996. if (more) {
  997. api_incr_top(L);
  998. }
  999. else /* no more elements */
  1000. L->top -= 1; /* remove key */
  1001. lua_unlock(L);
  1002. return more;
  1003. }
  1004. LUA_API void lua_concat (lua_State *L, int n) {
  1005. lua_lock(L);
  1006. api_checknelems(L, n);
  1007. if (n >= 2) {
  1008. luaV_concat(L, n);
  1009. }
  1010. else if (n == 0) { /* push empty string */
  1011. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  1012. api_incr_top(L);
  1013. }
  1014. /* else n == 1; nothing to do */
  1015. luaC_checkGC(L);
  1016. lua_unlock(L);
  1017. }
  1018. LUA_API void lua_len (lua_State *L, int idx) {
  1019. TValue *t;
  1020. lua_lock(L);
  1021. t = index2value(L, idx);
  1022. luaV_objlen(L, L->top, t);
  1023. api_incr_top(L);
  1024. lua_unlock(L);
  1025. }
  1026. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  1027. lua_Alloc f;
  1028. lua_lock(L);
  1029. if (ud) *ud = G(L)->ud;
  1030. f = G(L)->frealloc;
  1031. lua_unlock(L);
  1032. return f;
  1033. }
  1034. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  1035. lua_lock(L);
  1036. G(L)->ud = ud;
  1037. G(L)->frealloc = f;
  1038. lua_unlock(L);
  1039. }
  1040. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  1041. Udata *u;
  1042. lua_lock(L);
  1043. u = luaS_newudata(L, size);
  1044. setuvalue(L, s2v(L->top), u);
  1045. api_incr_top(L);
  1046. luaC_checkGC(L);
  1047. lua_unlock(L);
  1048. return getudatamem(u);
  1049. }
  1050. static const char *aux_upvalue (TValue *fi, int n, TValue **val,
  1051. GCObject **owner) {
  1052. switch (ttype(fi)) {
  1053. case LUA_TCCL: { /* C closure */
  1054. CClosure *f = clCvalue(fi);
  1055. if (!(1 <= n && n <= f->nupvalues)) return NULL;
  1056. *val = &f->upvalue[n-1];
  1057. if (owner) *owner = obj2gco(f);
  1058. return "";
  1059. }
  1060. case LUA_TLCL: { /* Lua closure */
  1061. LClosure *f = clLvalue(fi);
  1062. TString *name;
  1063. Proto *p = f->p;
  1064. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  1065. *val = f->upvals[n-1]->v;
  1066. if (owner) *owner = obj2gco(f->upvals[n - 1]);
  1067. name = p->upvalues[n-1].name;
  1068. return (name == NULL) ? "(*no name)" : getstr(name);
  1069. }
  1070. default: return NULL; /* not a closure */
  1071. }
  1072. }
  1073. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  1074. const char *name;
  1075. TValue *val = NULL; /* to avoid warnings */
  1076. lua_lock(L);
  1077. name = aux_upvalue(index2value(L, funcindex), n, &val, NULL);
  1078. if (name) {
  1079. setobj2s(L, L->top, val);
  1080. api_incr_top(L);
  1081. }
  1082. lua_unlock(L);
  1083. return name;
  1084. }
  1085. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  1086. const char *name;
  1087. TValue *val = NULL; /* to avoid warnings */
  1088. GCObject *owner = NULL; /* to avoid warnings */
  1089. TValue *fi;
  1090. lua_lock(L);
  1091. fi = index2value(L, funcindex);
  1092. api_checknelems(L, 1);
  1093. name = aux_upvalue(fi, n, &val, &owner);
  1094. if (name) {
  1095. L->top--;
  1096. setobj(L, val, s2v(L->top));
  1097. luaC_barrier(L, owner, val);
  1098. }
  1099. lua_unlock(L);
  1100. return name;
  1101. }
  1102. static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
  1103. LClosure *f;
  1104. TValue *fi = index2value(L, fidx);
  1105. api_check(L, ttisLclosure(fi), "Lua function expected");
  1106. f = clLvalue(fi);
  1107. api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
  1108. if (pf) *pf = f;
  1109. return &f->upvals[n - 1]; /* get its upvalue pointer */
  1110. }
  1111. LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
  1112. TValue *fi = index2value(L, fidx);
  1113. switch (ttype(fi)) {
  1114. case LUA_TLCL: { /* lua closure */
  1115. return *getupvalref(L, fidx, n, NULL);
  1116. }
  1117. case LUA_TCCL: { /* C closure */
  1118. CClosure *f = clCvalue(fi);
  1119. api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
  1120. return &f->upvalue[n - 1];
  1121. }
  1122. default: {
  1123. api_check(L, 0, "closure expected");
  1124. return NULL;
  1125. }
  1126. }
  1127. }
  1128. LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
  1129. int fidx2, int n2) {
  1130. LClosure *f1;
  1131. UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
  1132. UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
  1133. *up1 = *up2;
  1134. luaC_objbarrier(L, f1, *up1);
  1135. }