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