lapi.c 32 KB

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