lapi.c 33 KB

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