lapi.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  1. /*
  2. ** $Id: lapi.c,v 2.285 2018/02/20 16:52:50 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. static int finishrawget (lua_State *L, const TValue *val) {
  532. if (isempty(val)) /* avoid copying empty items to the stack */
  533. setnilvalue(s2v(L->top));
  534. else
  535. setobj2s(L, L->top, val);
  536. api_incr_top(L);
  537. lua_unlock(L);
  538. return ttnov(s2v(L->top - 1));
  539. }
  540. LUA_API int lua_rawget (lua_State *L, int idx) {
  541. TValue *t;
  542. const TValue *val;
  543. lua_lock(L);
  544. t = index2value(L, idx);
  545. api_check(L, ttistable(t), "table expected");
  546. val = luaH_get(hvalue(t), s2v(L->top - 1));
  547. L->top--; /* remove key */
  548. return finishrawget(L, val);
  549. }
  550. LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
  551. TValue *t;
  552. lua_lock(L);
  553. t = index2value(L, idx);
  554. api_check(L, ttistable(t), "table expected");
  555. return finishrawget(L, luaH_getint(hvalue(t), n));
  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_voidp(p));
  564. return finishrawget(L, luaH_get(hvalue(t), &k));
  565. }
  566. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  567. Table *t;
  568. lua_lock(L);
  569. t = luaH_new(L);
  570. sethvalue2s(L, L->top, t);
  571. api_incr_top(L);
  572. if (narray > 0 || nrec > 0)
  573. luaH_resize(L, t, narray, nrec);
  574. luaC_checkGC(L);
  575. lua_unlock(L);
  576. }
  577. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  578. const TValue *obj;
  579. Table *mt;
  580. int res = 0;
  581. lua_lock(L);
  582. obj = index2value(L, objindex);
  583. switch (ttnov(obj)) {
  584. case LUA_TTABLE:
  585. mt = hvalue(obj)->metatable;
  586. break;
  587. case LUA_TUSERDATA:
  588. mt = uvalue(obj)->metatable;
  589. break;
  590. default:
  591. mt = G(L)->mt[ttnov(obj)];
  592. break;
  593. }
  594. if (mt != NULL) {
  595. sethvalue2s(L, L->top, mt);
  596. api_incr_top(L);
  597. res = 1;
  598. }
  599. lua_unlock(L);
  600. return res;
  601. }
  602. LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) {
  603. TValue *o;
  604. int t;
  605. lua_lock(L);
  606. o = index2value(L, idx);
  607. api_check(L, ttisfulluserdata(o), "full userdata expected");
  608. if (n <= 0 || n > uvalue(o)->nuvalue) {
  609. setnilvalue(s2v(L->top));
  610. t = LUA_TNONE;
  611. }
  612. else {
  613. setobj2s(L, L->top, &uvalue(o)->uv[n - 1].uv);
  614. t = ttnov(s2v(L->top));
  615. }
  616. api_incr_top(L);
  617. lua_unlock(L);
  618. return t;
  619. }
  620. /*
  621. ** set functions (stack -> Lua)
  622. */
  623. /*
  624. ** t[k] = value at the top of the stack (where 'k' is a string)
  625. */
  626. static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
  627. const TValue *slot;
  628. TString *str = luaS_new(L, k);
  629. api_checknelems(L, 1);
  630. if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
  631. luaV_finishfastset(L, t, slot, s2v(L->top - 1));
  632. L->top--; /* pop value */
  633. }
  634. else {
  635. setsvalue2s(L, L->top, str); /* push 'str' (to make it a TValue) */
  636. api_incr_top(L);
  637. luaV_finishset(L, t, s2v(L->top - 1), s2v(L->top - 2), slot);
  638. L->top -= 2; /* pop value and key */
  639. }
  640. lua_unlock(L); /* lock done by caller */
  641. }
  642. LUA_API void lua_setglobal (lua_State *L, const char *name) {
  643. Table *reg = hvalue(&G(L)->l_registry);
  644. lua_lock(L); /* unlock done in 'auxsetstr' */
  645. auxsetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name);
  646. }
  647. LUA_API void lua_settable (lua_State *L, int idx) {
  648. TValue *t;
  649. const TValue *slot;
  650. lua_lock(L);
  651. api_checknelems(L, 2);
  652. t = index2value(L, idx);
  653. if (luaV_fastget(L, t, s2v(L->top - 2), slot, luaH_get)) {
  654. luaV_finishfastset(L, t, slot, s2v(L->top - 1));
  655. }
  656. else
  657. luaV_finishset(L, t, s2v(L->top - 2), s2v(L->top - 1), slot);
  658. L->top -= 2; /* pop index and value */
  659. lua_unlock(L);
  660. }
  661. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  662. lua_lock(L); /* unlock done in 'auxsetstr' */
  663. auxsetstr(L, index2value(L, idx), k);
  664. }
  665. LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
  666. TValue *t;
  667. const TValue *slot;
  668. lua_lock(L);
  669. api_checknelems(L, 1);
  670. t = index2value(L, idx);
  671. if (luaV_fastgeti(L, t, n, slot)) {
  672. luaV_finishfastset(L, t, slot, s2v(L->top - 1));
  673. }
  674. else {
  675. TValue aux;
  676. setivalue(&aux, n);
  677. luaV_finishset(L, t, &aux, s2v(L->top - 1), slot);
  678. }
  679. L->top--; /* pop value */
  680. lua_unlock(L);
  681. }
  682. LUA_API void lua_rawset (lua_State *L, int idx) {
  683. TValue *o;
  684. TValue *slot;
  685. lua_lock(L);
  686. api_checknelems(L, 2);
  687. o = index2value(L, idx);
  688. api_check(L, ttistable(o), "table expected");
  689. slot = luaH_set(L, hvalue(o), s2v(L->top - 2));
  690. setobj2t(L, slot, s2v(L->top - 1));
  691. invalidateTMcache(hvalue(o));
  692. luaC_barrierback(L, gcvalue(o), s2v(L->top - 1));
  693. L->top -= 2;
  694. lua_unlock(L);
  695. }
  696. LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
  697. TValue *o;
  698. lua_lock(L);
  699. api_checknelems(L, 1);
  700. o = index2value(L, idx);
  701. api_check(L, ttistable(o), "table expected");
  702. luaH_setint(L, hvalue(o), n, s2v(L->top - 1));
  703. luaC_barrierback(L, gcvalue(o), s2v(L->top - 1));
  704. L->top--;
  705. lua_unlock(L);
  706. }
  707. LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
  708. TValue *o;
  709. TValue k, *slot;
  710. lua_lock(L);
  711. api_checknelems(L, 1);
  712. o = index2value(L, idx);
  713. api_check(L, ttistable(o), "table expected");
  714. setpvalue(&k, cast_voidp(p));
  715. slot = luaH_set(L, hvalue(o), &k);
  716. setobj2t(L, slot, s2v(L->top - 1));
  717. luaC_barrierback(L, gcvalue(o), s2v(L->top - 1));
  718. L->top--;
  719. lua_unlock(L);
  720. }
  721. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  722. TValue *obj;
  723. Table *mt;
  724. lua_lock(L);
  725. api_checknelems(L, 1);
  726. obj = index2value(L, objindex);
  727. if (ttisnil(s2v(L->top - 1)))
  728. mt = NULL;
  729. else {
  730. api_check(L, ttistable(s2v(L->top - 1)), "table expected");
  731. mt = hvalue(s2v(L->top - 1));
  732. }
  733. switch (ttnov(obj)) {
  734. case LUA_TTABLE: {
  735. hvalue(obj)->metatable = mt;
  736. if (mt) {
  737. luaC_objbarrier(L, gcvalue(obj), mt);
  738. luaC_checkfinalizer(L, gcvalue(obj), mt);
  739. }
  740. break;
  741. }
  742. case LUA_TUSERDATA: {
  743. uvalue(obj)->metatable = mt;
  744. if (mt) {
  745. luaC_objbarrier(L, uvalue(obj), mt);
  746. luaC_checkfinalizer(L, gcvalue(obj), mt);
  747. }
  748. break;
  749. }
  750. default: {
  751. G(L)->mt[ttnov(obj)] = mt;
  752. break;
  753. }
  754. }
  755. L->top--;
  756. lua_unlock(L);
  757. return 1;
  758. }
  759. LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) {
  760. TValue *o;
  761. int res;
  762. lua_lock(L);
  763. api_checknelems(L, 1);
  764. o = index2value(L, idx);
  765. api_check(L, ttisfulluserdata(o), "full userdata expected");
  766. if (!(0 < n && n <= uvalue(o)->nuvalue))
  767. res = 0;
  768. else {
  769. setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top - 1));
  770. luaC_barrierback(L, gcvalue(o), s2v(L->top - 1));
  771. L->top--;
  772. res = 1;
  773. }
  774. lua_unlock(L);
  775. return res;
  776. }
  777. /*
  778. ** 'load' and 'call' functions (run Lua code)
  779. */
  780. #define checkresults(L,na,nr) \
  781. api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
  782. "results from function overflow current stack size")
  783. LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
  784. lua_KContext ctx, lua_KFunction k) {
  785. StkId func;
  786. lua_lock(L);
  787. api_check(L, k == NULL || !isLua(L->ci),
  788. "cannot use continuations inside hooks");
  789. api_checknelems(L, nargs+1);
  790. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  791. checkresults(L, nargs, nresults);
  792. func = L->top - (nargs+1);
  793. if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
  794. L->ci->u.c.k = k; /* save continuation */
  795. L->ci->u.c.ctx = ctx; /* save context */
  796. luaD_call(L, func, nresults); /* do the call */
  797. }
  798. else /* no continuation or no yieldable */
  799. luaD_callnoyield(L, func, nresults); /* just do the call */
  800. adjustresults(L, nresults);
  801. lua_unlock(L);
  802. }
  803. /*
  804. ** Execute a protected call.
  805. */
  806. struct CallS { /* data to 'f_call' */
  807. StkId func;
  808. int nresults;
  809. };
  810. static void f_call (lua_State *L, void *ud) {
  811. struct CallS *c = cast(struct CallS *, ud);
  812. luaD_callnoyield(L, c->func, c->nresults);
  813. }
  814. LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
  815. lua_KContext ctx, lua_KFunction k) {
  816. struct CallS c;
  817. int status;
  818. ptrdiff_t func;
  819. lua_lock(L);
  820. api_check(L, k == NULL || !isLua(L->ci),
  821. "cannot use continuations inside hooks");
  822. api_checknelems(L, nargs+1);
  823. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  824. checkresults(L, nargs, nresults);
  825. if (errfunc == 0)
  826. func = 0;
  827. else {
  828. StkId o = index2stack(L, errfunc);
  829. api_check(L, ttisfunction(s2v(o)), "error handler must be a function");
  830. func = savestack(L, o);
  831. }
  832. c.func = L->top - (nargs+1); /* function to be called */
  833. if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */
  834. c.nresults = nresults; /* do a 'conventional' protected call */
  835. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  836. }
  837. else { /* prepare continuation (call is already protected by 'resume') */
  838. CallInfo *ci = L->ci;
  839. ci->u.c.k = k; /* save continuation */
  840. ci->u.c.ctx = ctx; /* save context */
  841. /* save information for error recovery */
  842. ci->u2.funcidx = cast_int(savestack(L, c.func));
  843. ci->u.c.old_errfunc = L->errfunc;
  844. L->errfunc = func;
  845. setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */
  846. ci->callstatus |= CIST_YPCALL; /* function can do error recovery */
  847. luaD_call(L, c.func, nresults); /* do the call */
  848. ci->callstatus &= ~CIST_YPCALL;
  849. L->errfunc = ci->u.c.old_errfunc;
  850. status = LUA_OK; /* if it is here, there were no errors */
  851. }
  852. adjustresults(L, nresults);
  853. lua_unlock(L);
  854. return status;
  855. }
  856. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  857. const char *chunkname, const char *mode) {
  858. ZIO z;
  859. int status;
  860. lua_lock(L);
  861. if (!chunkname) chunkname = "?";
  862. luaZ_init(L, &z, reader, data);
  863. status = luaD_protectedparser(L, &z, chunkname, mode);
  864. if (status == LUA_OK) { /* no errors? */
  865. LClosure *f = clLvalue(s2v(L->top - 1)); /* get newly created function */
  866. if (f->nupvalues >= 1) { /* does it have an upvalue? */
  867. /* get global table from registry */
  868. Table *reg = hvalue(&G(L)->l_registry);
  869. const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  870. /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
  871. setobj(L, f->upvals[0]->v, gt);
  872. luaC_barrier(L, f->upvals[0], gt);
  873. }
  874. }
  875. lua_unlock(L);
  876. return status;
  877. }
  878. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
  879. int status;
  880. TValue *o;
  881. lua_lock(L);
  882. api_checknelems(L, 1);
  883. o = s2v(L->top - 1);
  884. if (isLfunction(o))
  885. status = luaU_dump(L, getproto(o), writer, data, strip);
  886. else
  887. status = 1;
  888. lua_unlock(L);
  889. return status;
  890. }
  891. LUA_API int lua_status (lua_State *L) {
  892. return L->status;
  893. }
  894. /*
  895. ** Garbage-collection function
  896. */
  897. LUA_API int lua_gc (lua_State *L, int what, ...) {
  898. va_list argp;
  899. int res = 0;
  900. global_State *g = G(L);
  901. lua_lock(L);
  902. va_start(argp, what);
  903. switch (what) {
  904. case LUA_GCSTOP: {
  905. g->gcrunning = 0;
  906. break;
  907. }
  908. case LUA_GCRESTART: {
  909. luaE_setdebt(g, 0);
  910. g->gcrunning = 1;
  911. break;
  912. }
  913. case LUA_GCCOLLECT: {
  914. luaC_fullgc(L, 0);
  915. break;
  916. }
  917. case LUA_GCCOUNT: {
  918. /* GC values are expressed in Kbytes: #bytes/2^10 */
  919. res = cast_int(gettotalbytes(g) >> 10);
  920. break;
  921. }
  922. case LUA_GCCOUNTB: {
  923. res = cast_int(gettotalbytes(g) & 0x3ff);
  924. break;
  925. }
  926. case LUA_GCSTEP: {
  927. int data = va_arg(argp, int);
  928. l_mem debt = 1; /* =1 to signal that it did an actual step */
  929. lu_byte oldrunning = g->gcrunning;
  930. g->gcrunning = 1; /* allow GC to run */
  931. if (data == 0) {
  932. luaE_setdebt(g, 0); /* do a basic step */
  933. luaC_step(L);
  934. }
  935. else { /* add 'data' to total debt */
  936. debt = cast(l_mem, data) * 1024 + g->GCdebt;
  937. luaE_setdebt(g, debt);
  938. luaC_checkGC(L);
  939. }
  940. g->gcrunning = oldrunning; /* restore previous state */
  941. if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */
  942. res = 1; /* signal it */
  943. break;
  944. }
  945. case LUA_GCSETPAUSE: {
  946. int data = va_arg(argp, int);
  947. res = getgcparam(g->gcpause);
  948. setgcparam(g->gcpause, data);
  949. break;
  950. }
  951. case LUA_GCSETSTEPMUL: {
  952. int data = va_arg(argp, int);
  953. res = getgcparam(g->gcstepmul);
  954. setgcparam(g->gcstepmul, data);
  955. break;
  956. }
  957. case LUA_GCISRUNNING: {
  958. res = g->gcrunning;
  959. break;
  960. }
  961. case LUA_GCGEN: {
  962. int oldmode = g->gckind;
  963. int minormul = va_arg(argp, int);
  964. int majormul = va_arg(argp, int);
  965. if (minormul != 0)
  966. g->genminormul = minormul;
  967. if (majormul != 0)
  968. setgcparam(g->genmajormul, majormul);
  969. luaC_changemode(L, KGC_GEN);
  970. res = (oldmode == KGC_GEN) ? LUA_GCGEN : LUA_GCINC;
  971. break;
  972. }
  973. case LUA_GCINC: {
  974. int oldmode = g->gckind;
  975. int pause = va_arg(argp, int);
  976. int stepmul = va_arg(argp, int);
  977. int stepsize = va_arg(argp, int);
  978. if (pause != 0)
  979. setgcparam(g->gcpause, pause);
  980. if (stepmul != 0)
  981. setgcparam(g->gcstepmul, stepmul);
  982. if (stepsize != 0)
  983. g->gcstepsize = stepsize;
  984. luaC_changemode(L, KGC_INC);
  985. res = (oldmode == KGC_GEN) ? LUA_GCGEN : LUA_GCINC;
  986. break;
  987. }
  988. default: res = -1; /* invalid option */
  989. }
  990. va_end(argp);
  991. lua_unlock(L);
  992. return res;
  993. }
  994. /*
  995. ** miscellaneous functions
  996. */
  997. LUA_API int lua_error (lua_State *L) {
  998. lua_lock(L);
  999. api_checknelems(L, 1);
  1000. luaG_errormsg(L);
  1001. /* code unreachable; will unlock when control actually leaves the kernel */
  1002. return 0; /* to avoid warnings */
  1003. }
  1004. LUA_API int lua_next (lua_State *L, int idx) {
  1005. TValue *t;
  1006. int more;
  1007. lua_lock(L);
  1008. t = index2value(L, idx);
  1009. api_check(L, ttistable(t), "table expected");
  1010. more = luaH_next(L, hvalue(t), L->top - 1);
  1011. if (more) {
  1012. api_incr_top(L);
  1013. }
  1014. else /* no more elements */
  1015. L->top -= 1; /* remove key */
  1016. lua_unlock(L);
  1017. return more;
  1018. }
  1019. LUA_API void lua_concat (lua_State *L, int n) {
  1020. lua_lock(L);
  1021. api_checknelems(L, n);
  1022. if (n >= 2) {
  1023. luaV_concat(L, n);
  1024. }
  1025. else if (n == 0) { /* push empty string */
  1026. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  1027. api_incr_top(L);
  1028. }
  1029. /* else n == 1; nothing to do */
  1030. luaC_checkGC(L);
  1031. lua_unlock(L);
  1032. }
  1033. LUA_API void lua_len (lua_State *L, int idx) {
  1034. TValue *t;
  1035. lua_lock(L);
  1036. t = index2value(L, idx);
  1037. luaV_objlen(L, L->top, t);
  1038. api_incr_top(L);
  1039. lua_unlock(L);
  1040. }
  1041. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  1042. lua_Alloc f;
  1043. lua_lock(L);
  1044. if (ud) *ud = G(L)->ud;
  1045. f = G(L)->frealloc;
  1046. lua_unlock(L);
  1047. return f;
  1048. }
  1049. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  1050. lua_lock(L);
  1051. G(L)->ud = ud;
  1052. G(L)->frealloc = f;
  1053. lua_unlock(L);
  1054. }
  1055. LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) {
  1056. Udata *u;
  1057. lua_lock(L);
  1058. api_check(L, 0 <= nuvalue && nuvalue < USHRT_MAX, "invalid value");
  1059. u = luaS_newudata(L, size, nuvalue);
  1060. setuvalue(L, s2v(L->top), u);
  1061. api_incr_top(L);
  1062. luaC_checkGC(L);
  1063. lua_unlock(L);
  1064. return getudatamem(u);
  1065. }
  1066. static const char *aux_upvalue (TValue *fi, int n, TValue **val,
  1067. GCObject **owner) {
  1068. switch (ttype(fi)) {
  1069. case LUA_TCCL: { /* C closure */
  1070. CClosure *f = clCvalue(fi);
  1071. if (!(1 <= n && n <= f->nupvalues)) return NULL;
  1072. *val = &f->upvalue[n-1];
  1073. if (owner) *owner = obj2gco(f);
  1074. return "";
  1075. }
  1076. case LUA_TLCL: { /* Lua closure */
  1077. LClosure *f = clLvalue(fi);
  1078. TString *name;
  1079. Proto *p = f->p;
  1080. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  1081. *val = f->upvals[n-1]->v;
  1082. if (owner) *owner = obj2gco(f->upvals[n - 1]);
  1083. name = p->upvalues[n-1].name;
  1084. return (name == NULL) ? "(*no name)" : getstr(name);
  1085. }
  1086. default: return NULL; /* not a closure */
  1087. }
  1088. }
  1089. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  1090. const char *name;
  1091. TValue *val = NULL; /* to avoid warnings */
  1092. lua_lock(L);
  1093. name = aux_upvalue(index2value(L, funcindex), n, &val, NULL);
  1094. if (name) {
  1095. setobj2s(L, L->top, val);
  1096. api_incr_top(L);
  1097. }
  1098. lua_unlock(L);
  1099. return name;
  1100. }
  1101. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  1102. const char *name;
  1103. TValue *val = NULL; /* to avoid warnings */
  1104. GCObject *owner = NULL; /* to avoid warnings */
  1105. TValue *fi;
  1106. lua_lock(L);
  1107. fi = index2value(L, funcindex);
  1108. api_checknelems(L, 1);
  1109. name = aux_upvalue(fi, n, &val, &owner);
  1110. if (name) {
  1111. L->top--;
  1112. setobj(L, val, s2v(L->top));
  1113. luaC_barrier(L, owner, val);
  1114. }
  1115. lua_unlock(L);
  1116. return name;
  1117. }
  1118. static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
  1119. LClosure *f;
  1120. TValue *fi = index2value(L, fidx);
  1121. api_check(L, ttisLclosure(fi), "Lua function expected");
  1122. f = clLvalue(fi);
  1123. api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
  1124. if (pf) *pf = f;
  1125. return &f->upvals[n - 1]; /* get its upvalue pointer */
  1126. }
  1127. LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
  1128. TValue *fi = index2value(L, fidx);
  1129. switch (ttype(fi)) {
  1130. case LUA_TLCL: { /* lua closure */
  1131. return *getupvalref(L, fidx, n, NULL);
  1132. }
  1133. case LUA_TCCL: { /* C closure */
  1134. CClosure *f = clCvalue(fi);
  1135. api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
  1136. return &f->upvalue[n - 1];
  1137. }
  1138. default: {
  1139. api_check(L, 0, "closure expected");
  1140. return NULL;
  1141. }
  1142. }
  1143. }
  1144. LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
  1145. int fidx2, int n2) {
  1146. LClosure *f1;
  1147. UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
  1148. UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
  1149. *up1 = *up2;
  1150. luaC_objbarrier(L, f1, *up1);
  1151. }