lapi.c 32 KB

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