lapi.c 33 KB

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