lapi.c 32 KB

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