lapi.c 35 KB

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