lapi.c 34 KB

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