lapi.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  1. /*
  2. ** $Id: lapi.c,v 2.290 2018/02/27 20:01:55 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) ? ttype(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 (ttypetag(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 (ttype(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 (ttypetag(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 ttype(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 ttype(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 ttype(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 ttype(s2v(L->top - 1));
  539. }
  540. static Table *gettable (lua_State *L, int idx) {
  541. TValue *t = index2value(L, idx);
  542. api_check(L, ttistable(t), "table expected");
  543. return hvalue(t);
  544. }
  545. LUA_API int lua_rawget (lua_State *L, int idx) {
  546. Table *t;
  547. const TValue *val;
  548. lua_lock(L);
  549. api_checknelems(L, 1);
  550. t = gettable(L, idx);
  551. val = luaH_get(t, s2v(L->top - 1));
  552. L->top--; /* remove key */
  553. return finishrawget(L, val);
  554. }
  555. LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
  556. Table *t;
  557. lua_lock(L);
  558. t = gettable(L, idx);
  559. return finishrawget(L, luaH_getint(t, n));
  560. }
  561. LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
  562. Table *t;
  563. TValue k;
  564. lua_lock(L);
  565. t = gettable(L, idx);
  566. setpvalue(&k, cast_voidp(p));
  567. return finishrawget(L, luaH_get(t, &k));
  568. }
  569. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  570. Table *t;
  571. lua_lock(L);
  572. t = luaH_new(L);
  573. sethvalue2s(L, L->top, t);
  574. api_incr_top(L);
  575. if (narray > 0 || nrec > 0)
  576. luaH_resize(L, t, narray, nrec);
  577. luaC_checkGC(L);
  578. lua_unlock(L);
  579. }
  580. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  581. const TValue *obj;
  582. Table *mt;
  583. int res = 0;
  584. lua_lock(L);
  585. obj = index2value(L, objindex);
  586. switch (ttype(obj)) {
  587. case LUA_TTABLE:
  588. mt = hvalue(obj)->metatable;
  589. break;
  590. case LUA_TUSERDATA:
  591. mt = uvalue(obj)->metatable;
  592. break;
  593. default:
  594. mt = G(L)->mt[ttype(obj)];
  595. break;
  596. }
  597. if (mt != NULL) {
  598. sethvalue2s(L, L->top, mt);
  599. api_incr_top(L);
  600. res = 1;
  601. }
  602. lua_unlock(L);
  603. return res;
  604. }
  605. LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) {
  606. TValue *o;
  607. int t;
  608. lua_lock(L);
  609. o = index2value(L, idx);
  610. api_check(L, ttisfulluserdata(o), "full userdata expected");
  611. if (n <= 0 || n > uvalue(o)->nuvalue) {
  612. setnilvalue(s2v(L->top));
  613. t = LUA_TNONE;
  614. }
  615. else {
  616. setobj2s(L, L->top, &uvalue(o)->uv[n - 1].uv);
  617. t = ttype(s2v(L->top));
  618. }
  619. api_incr_top(L);
  620. lua_unlock(L);
  621. return t;
  622. }
  623. /*
  624. ** set functions (stack -> Lua)
  625. */
  626. /*
  627. ** t[k] = value at the top of the stack (where 'k' is a string)
  628. */
  629. static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
  630. const TValue *slot;
  631. TString *str = luaS_new(L, k);
  632. api_checknelems(L, 1);
  633. if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
  634. luaV_finishfastset(L, t, slot, s2v(L->top - 1));
  635. L->top--; /* pop value */
  636. }
  637. else {
  638. setsvalue2s(L, L->top, str); /* push 'str' (to make it a TValue) */
  639. api_incr_top(L);
  640. luaV_finishset(L, t, s2v(L->top - 1), s2v(L->top - 2), slot);
  641. L->top -= 2; /* pop value and key */
  642. }
  643. lua_unlock(L); /* lock done by caller */
  644. }
  645. LUA_API void lua_setglobal (lua_State *L, const char *name) {
  646. Table *reg = hvalue(&G(L)->l_registry);
  647. lua_lock(L); /* unlock done in 'auxsetstr' */
  648. auxsetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name);
  649. }
  650. LUA_API void lua_settable (lua_State *L, int idx) {
  651. TValue *t;
  652. const TValue *slot;
  653. lua_lock(L);
  654. api_checknelems(L, 2);
  655. t = index2value(L, idx);
  656. if (luaV_fastget(L, t, s2v(L->top - 2), slot, luaH_get)) {
  657. luaV_finishfastset(L, t, slot, s2v(L->top - 1));
  658. }
  659. else
  660. luaV_finishset(L, t, s2v(L->top - 2), s2v(L->top - 1), slot);
  661. L->top -= 2; /* pop index and value */
  662. lua_unlock(L);
  663. }
  664. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  665. lua_lock(L); /* unlock done in 'auxsetstr' */
  666. auxsetstr(L, index2value(L, idx), k);
  667. }
  668. LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
  669. TValue *t;
  670. const TValue *slot;
  671. lua_lock(L);
  672. api_checknelems(L, 1);
  673. t = index2value(L, idx);
  674. if (luaV_fastgeti(L, t, n, slot)) {
  675. luaV_finishfastset(L, t, slot, s2v(L->top - 1));
  676. }
  677. else {
  678. TValue aux;
  679. setivalue(&aux, n);
  680. luaV_finishset(L, t, &aux, s2v(L->top - 1), slot);
  681. }
  682. L->top--; /* pop value */
  683. lua_unlock(L);
  684. }
  685. LUA_API void lua_rawset (lua_State *L, int idx) {
  686. Table *t;
  687. TValue *slot;
  688. lua_lock(L);
  689. api_checknelems(L, 2);
  690. t = gettable(L, idx);
  691. slot = luaH_set(L, t, s2v(L->top - 2));
  692. setobj2t(L, slot, s2v(L->top - 1));
  693. invalidateTMcache(t);
  694. luaC_barrierback(L, obj2gco(t), s2v(L->top - 1));
  695. L->top -= 2;
  696. lua_unlock(L);
  697. }
  698. LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
  699. Table *t;
  700. lua_lock(L);
  701. api_checknelems(L, 1);
  702. t = gettable(L, idx);
  703. luaH_setint(L, t, n, s2v(L->top - 1));
  704. luaC_barrierback(L, obj2gco(t), s2v(L->top - 1));
  705. L->top--;
  706. lua_unlock(L);
  707. }
  708. LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
  709. Table *t;
  710. TValue k, *slot;
  711. lua_lock(L);
  712. api_checknelems(L, 1);
  713. t = gettable(L, idx);
  714. setpvalue(&k, cast_voidp(p));
  715. slot = luaH_set(L, t, &k);
  716. setobj2t(L, slot, s2v(L->top - 1));
  717. luaC_barrierback(L, obj2gco(t), 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 (ttype(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[ttype(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. res = 1;
  772. }
  773. L->top--;
  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. Table *t;
  1006. int more;
  1007. lua_lock(L);
  1008. api_checknelems(L, 1);
  1009. t = gettable(L, idx);
  1010. more = luaH_next(L, 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 (ttypetag(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 (ttypetag(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. }