lapi.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  1. /*
  2. ** $Id: lapi.c,v 2.277 2017/11/23 19:29:04 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. /*
  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 additional 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. api_incr_top(L);
  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. api_incr_top(L);
  458. luaC_checkGC(L);
  459. }
  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 || (L->ci->top - L->top >= (nr) - (na)), \
  770. "results from function overflow current stack size")
  771. LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
  772. lua_KContext ctx, lua_KFunction k) {
  773. StkId func;
  774. lua_lock(L);
  775. api_check(L, k == NULL || !isLua(L->ci),
  776. "cannot use continuations inside hooks");
  777. api_checknelems(L, nargs+1);
  778. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  779. checkresults(L, nargs, nresults);
  780. func = L->top - (nargs+1);
  781. if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
  782. L->ci->u.c.k = k; /* save continuation */
  783. L->ci->u.c.ctx = ctx; /* save context */
  784. luaD_call(L, func, nresults); /* do the call */
  785. }
  786. else /* no continuation or no yieldable */
  787. luaD_callnoyield(L, func, nresults); /* just do the call */
  788. adjustresults(L, nresults);
  789. lua_unlock(L);
  790. }
  791. /*
  792. ** Execute a protected call.
  793. */
  794. struct CallS { /* data to 'f_call' */
  795. StkId func;
  796. int nresults;
  797. };
  798. static void f_call (lua_State *L, void *ud) {
  799. struct CallS *c = cast(struct CallS *, ud);
  800. luaD_callnoyield(L, c->func, c->nresults);
  801. }
  802. LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
  803. lua_KContext ctx, lua_KFunction k) {
  804. struct CallS c;
  805. int status;
  806. ptrdiff_t func;
  807. lua_lock(L);
  808. api_check(L, k == NULL || !isLua(L->ci),
  809. "cannot use continuations inside hooks");
  810. api_checknelems(L, nargs+1);
  811. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  812. checkresults(L, nargs, nresults);
  813. if (errfunc == 0)
  814. func = 0;
  815. else {
  816. StkId o = index2stack(L, errfunc);
  817. func = savestack(L, o);
  818. }
  819. c.func = L->top - (nargs+1); /* function to be called */
  820. if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */
  821. c.nresults = nresults; /* do a 'conventional' protected call */
  822. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  823. }
  824. else { /* prepare continuation (call is already protected by 'resume') */
  825. CallInfo *ci = L->ci;
  826. ci->u.c.k = k; /* save continuation */
  827. ci->u.c.ctx = ctx; /* save context */
  828. /* save information for error recovery */
  829. ci->u2.funcidx = savestack(L, c.func);
  830. ci->u.c.old_errfunc = L->errfunc;
  831. L->errfunc = func;
  832. setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */
  833. ci->callstatus |= CIST_YPCALL; /* function can do error recovery */
  834. luaD_call(L, c.func, nresults); /* do the call */
  835. ci->callstatus &= ~CIST_YPCALL;
  836. L->errfunc = ci->u.c.old_errfunc;
  837. status = LUA_OK; /* if it is here, there were no errors */
  838. }
  839. adjustresults(L, nresults);
  840. lua_unlock(L);
  841. return status;
  842. }
  843. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  844. const char *chunkname, const char *mode) {
  845. ZIO z;
  846. int status;
  847. lua_lock(L);
  848. if (!chunkname) chunkname = "?";
  849. luaZ_init(L, &z, reader, data);
  850. status = luaD_protectedparser(L, &z, chunkname, mode);
  851. if (status == LUA_OK) { /* no errors? */
  852. LClosure *f = clLvalue(s2v(L->top - 1)); /* get newly created function */
  853. if (f->nupvalues >= 1) { /* does it have an upvalue? */
  854. /* get global table from registry */
  855. Table *reg = hvalue(&G(L)->l_registry);
  856. const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  857. /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
  858. setobj(L, f->upvals[0]->v, gt);
  859. luaC_barrier(L, f->upvals[0], gt);
  860. }
  861. }
  862. lua_unlock(L);
  863. return status;
  864. }
  865. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
  866. int status;
  867. TValue *o;
  868. lua_lock(L);
  869. api_checknelems(L, 1);
  870. o = s2v(L->top - 1);
  871. if (isLfunction(o))
  872. status = luaU_dump(L, getproto(o), writer, data, strip);
  873. else
  874. status = 1;
  875. lua_unlock(L);
  876. return status;
  877. }
  878. LUA_API int lua_status (lua_State *L) {
  879. return L->status;
  880. }
  881. /*
  882. ** Garbage-collection function
  883. */
  884. LUA_API int lua_gc (lua_State *L, int what, ...) {
  885. va_list argp;
  886. int res = 0;
  887. global_State *g = G(L);
  888. lua_lock(L);
  889. va_start(argp, what);
  890. switch (what) {
  891. case LUA_GCSTOP: {
  892. g->gcrunning = 0;
  893. break;
  894. }
  895. case LUA_GCRESTART: {
  896. luaE_setdebt(g, 0);
  897. g->gcrunning = 1;
  898. break;
  899. }
  900. case LUA_GCCOLLECT: {
  901. luaC_fullgc(L, 0);
  902. break;
  903. }
  904. case LUA_GCCOUNT: {
  905. /* GC values are expressed in Kbytes: #bytes/2^10 */
  906. res = cast_int(gettotalbytes(g) >> 10);
  907. break;
  908. }
  909. case LUA_GCCOUNTB: {
  910. res = cast_int(gettotalbytes(g) & 0x3ff);
  911. break;
  912. }
  913. case LUA_GCSTEP: {
  914. int data = va_arg(argp, int);
  915. l_mem debt = 1; /* =1 to signal that it did an actual step */
  916. lu_byte oldrunning = g->gcrunning;
  917. g->gcrunning = 1; /* allow GC to run */
  918. if (data == 0) {
  919. luaE_setdebt(g, 0); /* do a basic step */
  920. luaC_step(L);
  921. }
  922. else { /* add 'data' to total debt */
  923. debt = cast(l_mem, data) * 1024 + g->GCdebt;
  924. luaE_setdebt(g, debt);
  925. luaC_checkGC(L);
  926. }
  927. g->gcrunning = oldrunning; /* restore previous state */
  928. if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */
  929. res = 1; /* signal it */
  930. break;
  931. }
  932. case LUA_GCSETPAUSE: {
  933. int data = va_arg(argp, int);
  934. res = getgcparam(g->gcpause);
  935. setgcparam(g->gcpause, data);
  936. break;
  937. }
  938. case LUA_GCSETSTEPMUL: {
  939. int data = va_arg(argp, int);
  940. res = getgcparam(g->gcstepmul);
  941. setgcparam(g->gcstepmul, data);
  942. break;
  943. }
  944. case LUA_GCISRUNNING: {
  945. res = g->gcrunning;
  946. break;
  947. }
  948. case LUA_GCGEN: {
  949. int minormul = va_arg(argp, int);
  950. int majormul = va_arg(argp, int);
  951. if (minormul != 0)
  952. g->genminormul = minormul;
  953. if (majormul != 0)
  954. setgcparam(g->genmajormul, majormul);
  955. luaC_changemode(L, KGC_GEN);
  956. break;
  957. }
  958. case LUA_GCINC: {
  959. int pause = va_arg(argp, int);
  960. int stepmul = va_arg(argp, int);
  961. int stepsize = va_arg(argp, int);
  962. if (pause != 0)
  963. setgcparam(g->gcpause, pause);
  964. if (stepmul != 0)
  965. setgcparam(g->gcstepmul, stepmul);
  966. if (stepsize != 0)
  967. g->gcstepsize = stepsize;
  968. luaC_changemode(L, KGC_INC);
  969. break;
  970. }
  971. default: res = -1; /* invalid option */
  972. }
  973. va_end(argp);
  974. lua_unlock(L);
  975. return res;
  976. }
  977. /*
  978. ** miscellaneous functions
  979. */
  980. LUA_API int lua_error (lua_State *L) {
  981. lua_lock(L);
  982. api_checknelems(L, 1);
  983. luaG_errormsg(L);
  984. /* code unreachable; will unlock when control actually leaves the kernel */
  985. return 0; /* to avoid warnings */
  986. }
  987. LUA_API int lua_next (lua_State *L, int idx) {
  988. TValue *t;
  989. int more;
  990. lua_lock(L);
  991. t = index2value(L, idx);
  992. api_check(L, ttistable(t), "table expected");
  993. more = luaH_next(L, hvalue(t), L->top - 1);
  994. if (more) {
  995. api_incr_top(L);
  996. }
  997. else /* no more elements */
  998. L->top -= 1; /* remove key */
  999. lua_unlock(L);
  1000. return more;
  1001. }
  1002. LUA_API void lua_concat (lua_State *L, int n) {
  1003. lua_lock(L);
  1004. api_checknelems(L, n);
  1005. if (n >= 2) {
  1006. luaV_concat(L, n);
  1007. }
  1008. else if (n == 0) { /* push empty string */
  1009. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  1010. api_incr_top(L);
  1011. }
  1012. /* else n == 1; nothing to do */
  1013. luaC_checkGC(L);
  1014. lua_unlock(L);
  1015. }
  1016. LUA_API void lua_len (lua_State *L, int idx) {
  1017. TValue *t;
  1018. lua_lock(L);
  1019. t = index2value(L, idx);
  1020. luaV_objlen(L, L->top, t);
  1021. api_incr_top(L);
  1022. lua_unlock(L);
  1023. }
  1024. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  1025. lua_Alloc f;
  1026. lua_lock(L);
  1027. if (ud) *ud = G(L)->ud;
  1028. f = G(L)->frealloc;
  1029. lua_unlock(L);
  1030. return f;
  1031. }
  1032. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  1033. lua_lock(L);
  1034. G(L)->ud = ud;
  1035. G(L)->frealloc = f;
  1036. lua_unlock(L);
  1037. }
  1038. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  1039. Udata *u;
  1040. lua_lock(L);
  1041. u = luaS_newudata(L, size);
  1042. setuvalue(L, s2v(L->top), u);
  1043. api_incr_top(L);
  1044. luaC_checkGC(L);
  1045. lua_unlock(L);
  1046. return getudatamem(u);
  1047. }
  1048. static const char *aux_upvalue (TValue *fi, int n, TValue **val,
  1049. GCObject **owner) {
  1050. switch (ttype(fi)) {
  1051. case LUA_TCCL: { /* C closure */
  1052. CClosure *f = clCvalue(fi);
  1053. if (!(1 <= n && n <= f->nupvalues)) return NULL;
  1054. *val = &f->upvalue[n-1];
  1055. if (owner) *owner = obj2gco(f);
  1056. return "";
  1057. }
  1058. case LUA_TLCL: { /* Lua closure */
  1059. LClosure *f = clLvalue(fi);
  1060. TString *name;
  1061. Proto *p = f->p;
  1062. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  1063. *val = f->upvals[n-1]->v;
  1064. if (owner) *owner = obj2gco(f->upvals[n - 1]);
  1065. name = p->upvalues[n-1].name;
  1066. return (name == NULL) ? "(*no name)" : getstr(name);
  1067. }
  1068. default: return NULL; /* not a closure */
  1069. }
  1070. }
  1071. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  1072. const char *name;
  1073. TValue *val = NULL; /* to avoid warnings */
  1074. lua_lock(L);
  1075. name = aux_upvalue(index2value(L, funcindex), n, &val, NULL);
  1076. if (name) {
  1077. setobj2s(L, L->top, val);
  1078. api_incr_top(L);
  1079. }
  1080. lua_unlock(L);
  1081. return name;
  1082. }
  1083. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  1084. const char *name;
  1085. TValue *val = NULL; /* to avoid warnings */
  1086. GCObject *owner = NULL; /* to avoid warnings */
  1087. TValue *fi;
  1088. lua_lock(L);
  1089. fi = index2value(L, funcindex);
  1090. api_checknelems(L, 1);
  1091. name = aux_upvalue(fi, n, &val, &owner);
  1092. if (name) {
  1093. L->top--;
  1094. setobj(L, val, s2v(L->top));
  1095. luaC_barrier(L, owner, val);
  1096. }
  1097. lua_unlock(L);
  1098. return name;
  1099. }
  1100. static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
  1101. LClosure *f;
  1102. TValue *fi = index2value(L, fidx);
  1103. api_check(L, ttisLclosure(fi), "Lua function expected");
  1104. f = clLvalue(fi);
  1105. api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
  1106. if (pf) *pf = f;
  1107. return &f->upvals[n - 1]; /* get its upvalue pointer */
  1108. }
  1109. LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
  1110. TValue *fi = index2value(L, fidx);
  1111. switch (ttype(fi)) {
  1112. case LUA_TLCL: { /* lua closure */
  1113. return *getupvalref(L, fidx, n, NULL);
  1114. }
  1115. case LUA_TCCL: { /* C closure */
  1116. CClosure *f = clCvalue(fi);
  1117. api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
  1118. return &f->upvalue[n - 1];
  1119. }
  1120. default: {
  1121. api_check(L, 0, "closure expected");
  1122. return NULL;
  1123. }
  1124. }
  1125. }
  1126. LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
  1127. int fidx2, int n2) {
  1128. LClosure *f1;
  1129. UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
  1130. UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
  1131. *up1 = *up2;
  1132. luaC_objbarrier(L, f1, *up1);
  1133. }