lapi.c 32 KB

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