lapi.c 33 KB

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