lapi.c 36 KB

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