lapi.c 36 KB

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