lapi.c 33 KB

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