lapi.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460
  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 size_t lua_stringtonumber (lua_State *L, const char *s) {
  300. size_t sz = luaO_str2num(s, s2v(L->top.p));
  301. if (sz != 0)
  302. api_incr_top(L);
  303. return sz;
  304. }
  305. LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) {
  306. lua_Number n = 0;
  307. const TValue *o = index2value(L, idx);
  308. int isnum = tonumber(o, &n);
  309. if (pisnum)
  310. *pisnum = isnum;
  311. return n;
  312. }
  313. LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) {
  314. lua_Integer res = 0;
  315. const TValue *o = index2value(L, idx);
  316. int isnum = tointeger(o, &res);
  317. if (pisnum)
  318. *pisnum = isnum;
  319. return res;
  320. }
  321. LUA_API int lua_toboolean (lua_State *L, int idx) {
  322. const TValue *o = index2value(L, idx);
  323. return !l_isfalse(o);
  324. }
  325. LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
  326. TValue *o;
  327. lua_lock(L);
  328. o = index2value(L, idx);
  329. if (!ttisstring(o)) {
  330. if (!cvt2str(o)) { /* not convertible? */
  331. if (len != NULL) *len = 0;
  332. lua_unlock(L);
  333. return NULL;
  334. }
  335. luaO_tostring(L, o);
  336. luaC_checkGC(L);
  337. o = index2value(L, idx); /* previous call may reallocate the stack */
  338. }
  339. lua_unlock(L);
  340. if (len != NULL)
  341. return getlstr(tsvalue(o), *len);
  342. else
  343. return getstr(tsvalue(o));
  344. }
  345. LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) {
  346. const TValue *o = index2value(L, idx);
  347. switch (ttypetag(o)) {
  348. case LUA_VSHRSTR: return cast(lua_Unsigned, tsvalue(o)->shrlen);
  349. case LUA_VLNGSTR: return cast(lua_Unsigned, tsvalue(o)->u.lnglen);
  350. case LUA_VUSERDATA: return cast(lua_Unsigned, uvalue(o)->len);
  351. case LUA_VTABLE: return luaH_getn(hvalue(o));
  352. default: return 0;
  353. }
  354. }
  355. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
  356. const TValue *o = index2value(L, idx);
  357. if (ttislcf(o)) return fvalue(o);
  358. else if (ttisCclosure(o))
  359. return clCvalue(o)->f;
  360. else return NULL; /* not a C function */
  361. }
  362. l_sinline void *touserdata (const TValue *o) {
  363. switch (ttype(o)) {
  364. case LUA_TUSERDATA: return getudatamem(uvalue(o));
  365. case LUA_TLIGHTUSERDATA: return pvalue(o);
  366. default: return NULL;
  367. }
  368. }
  369. LUA_API void *lua_touserdata (lua_State *L, int idx) {
  370. const TValue *o = index2value(L, idx);
  371. return touserdata(o);
  372. }
  373. LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
  374. const TValue *o = index2value(L, idx);
  375. return (!ttisthread(o)) ? NULL : thvalue(o);
  376. }
  377. /*
  378. ** Returns a pointer to the internal representation of an object.
  379. ** Note that ANSI C does not allow the conversion of a pointer to
  380. ** function to a 'void*', so the conversion here goes through
  381. ** a 'size_t'. (As the returned pointer is only informative, this
  382. ** conversion should not be a problem.)
  383. */
  384. LUA_API const void *lua_topointer (lua_State *L, int idx) {
  385. const TValue *o = index2value(L, idx);
  386. switch (ttypetag(o)) {
  387. case LUA_VLCF: return cast_voidp(cast_sizet(fvalue(o)));
  388. case LUA_VUSERDATA: case LUA_VLIGHTUSERDATA:
  389. return touserdata(o);
  390. default: {
  391. if (iscollectable(o))
  392. return gcvalue(o);
  393. else
  394. return NULL;
  395. }
  396. }
  397. }
  398. /*
  399. ** push functions (C -> stack)
  400. */
  401. LUA_API void lua_pushnil (lua_State *L) {
  402. lua_lock(L);
  403. setnilvalue(s2v(L->top.p));
  404. api_incr_top(L);
  405. lua_unlock(L);
  406. }
  407. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  408. lua_lock(L);
  409. setfltvalue(s2v(L->top.p), n);
  410. api_incr_top(L);
  411. lua_unlock(L);
  412. }
  413. LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
  414. lua_lock(L);
  415. setivalue(s2v(L->top.p), n);
  416. api_incr_top(L);
  417. lua_unlock(L);
  418. }
  419. /*
  420. ** Pushes on the stack a string with given length. Avoid using 's' when
  421. ** 'len' == 0 (as 's' can be NULL in that case), due to later use of
  422. ** 'memcmp' and 'memcpy'.
  423. */
  424. LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
  425. TString *ts;
  426. lua_lock(L);
  427. ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len);
  428. setsvalue2s(L, L->top.p, ts);
  429. api_incr_top(L);
  430. luaC_checkGC(L);
  431. lua_unlock(L);
  432. return getstr(ts);
  433. }
  434. LUA_API const char *lua_pushextlstring (lua_State *L,
  435. const char *s, size_t len, lua_Alloc falloc, void *ud) {
  436. TString *ts;
  437. lua_lock(L);
  438. api_check(L, len <= MAX_SIZE, "string too large");
  439. api_check(L, s[len] == '\0', "string not ending with zero");
  440. ts = luaS_newextlstr (L, s, len, falloc, ud);
  441. setsvalue2s(L, L->top.p, ts);
  442. api_incr_top(L);
  443. if (falloc != NULL) /* non-static string? */
  444. luaC_checkGC(L);
  445. lua_unlock(L);
  446. return getstr(ts);
  447. }
  448. LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
  449. lua_lock(L);
  450. if (s == NULL)
  451. setnilvalue(s2v(L->top.p));
  452. else {
  453. TString *ts;
  454. ts = luaS_new(L, s);
  455. setsvalue2s(L, L->top.p, ts);
  456. s = getstr(ts); /* internal copy's address */
  457. }
  458. api_incr_top(L);
  459. luaC_checkGC(L);
  460. lua_unlock(L);
  461. return s;
  462. }
  463. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  464. va_list argp) {
  465. const char *ret;
  466. lua_lock(L);
  467. ret = luaO_pushvfstring(L, fmt, argp);
  468. luaC_checkGC(L);
  469. lua_unlock(L);
  470. return ret;
  471. }
  472. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  473. const char *ret;
  474. va_list argp;
  475. lua_lock(L);
  476. va_start(argp, fmt);
  477. ret = luaO_pushvfstring(L, fmt, argp);
  478. va_end(argp);
  479. luaC_checkGC(L);
  480. lua_unlock(L);
  481. return ret;
  482. }
  483. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  484. lua_lock(L);
  485. if (n == 0) {
  486. setfvalue(s2v(L->top.p), fn);
  487. api_incr_top(L);
  488. }
  489. else {
  490. int i;
  491. CClosure *cl;
  492. api_checkpop(L, n);
  493. api_check(L, n <= MAXUPVAL, "upvalue index too large");
  494. cl = luaF_newCclosure(L, n);
  495. cl->f = fn;
  496. for (i = 0; i < n; i++) {
  497. setobj2n(L, &cl->upvalue[i], s2v(L->top.p - n + i));
  498. /* does not need barrier because closure is white */
  499. lua_assert(iswhite(cl));
  500. }
  501. L->top.p -= n;
  502. setclCvalue(L, s2v(L->top.p), cl);
  503. api_incr_top(L);
  504. luaC_checkGC(L);
  505. }
  506. lua_unlock(L);
  507. }
  508. LUA_API void lua_pushboolean (lua_State *L, int b) {
  509. lua_lock(L);
  510. if (b)
  511. setbtvalue(s2v(L->top.p));
  512. else
  513. setbfvalue(s2v(L->top.p));
  514. api_incr_top(L);
  515. lua_unlock(L);
  516. }
  517. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  518. lua_lock(L);
  519. setpvalue(s2v(L->top.p), p);
  520. api_incr_top(L);
  521. lua_unlock(L);
  522. }
  523. LUA_API int lua_pushthread (lua_State *L) {
  524. lua_lock(L);
  525. setthvalue(L, s2v(L->top.p), L);
  526. api_incr_top(L);
  527. lua_unlock(L);
  528. return (G(L)->mainthread == L);
  529. }
  530. /*
  531. ** get functions (Lua -> stack)
  532. */
  533. static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
  534. lu_byte tag;
  535. TString *str = luaS_new(L, k);
  536. luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, tag);
  537. if (!tagisempty(tag)) {
  538. api_incr_top(L);
  539. }
  540. else {
  541. setsvalue2s(L, L->top.p, str);
  542. api_incr_top(L);
  543. tag = luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, tag);
  544. }
  545. lua_unlock(L);
  546. return novariant(tag);
  547. }
  548. static void getGlobalTable (lua_State *L, TValue *gt) {
  549. Table *registry = hvalue(&G(L)->l_registry);
  550. lu_byte tag = luaH_getint(registry, LUA_RIDX_GLOBALS, gt);
  551. (void)tag; /* avoid not-used warnings when checks are off */
  552. api_check(L, novariant(tag) == LUA_TTABLE, "global table must exist");
  553. }
  554. LUA_API int lua_getglobal (lua_State *L, const char *name) {
  555. TValue gt;
  556. lua_lock(L);
  557. getGlobalTable(L, &gt);
  558. return auxgetstr(L, &gt, name);
  559. }
  560. LUA_API int lua_gettable (lua_State *L, int idx) {
  561. lu_byte tag;
  562. TValue *t;
  563. lua_lock(L);
  564. api_checkpop(L, 1);
  565. t = index2value(L, idx);
  566. luaV_fastget(t, s2v(L->top.p - 1), s2v(L->top.p - 1), luaH_get, tag);
  567. if (tagisempty(tag))
  568. tag = luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, tag);
  569. lua_unlock(L);
  570. return novariant(tag);
  571. }
  572. LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
  573. lua_lock(L);
  574. return auxgetstr(L, index2value(L, idx), k);
  575. }
  576. LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
  577. TValue *t;
  578. lu_byte tag;
  579. lua_lock(L);
  580. t = index2value(L, idx);
  581. luaV_fastgeti(t, n, s2v(L->top.p), tag);
  582. if (tagisempty(tag)) {
  583. TValue key;
  584. setivalue(&key, n);
  585. tag = luaV_finishget(L, t, &key, L->top.p, tag);
  586. }
  587. api_incr_top(L);
  588. lua_unlock(L);
  589. return novariant(tag);
  590. }
  591. static int finishrawget (lua_State *L, lu_byte tag) {
  592. if (tagisempty(tag)) /* avoid copying empty items to the stack */
  593. setnilvalue(s2v(L->top.p));
  594. api_incr_top(L);
  595. lua_unlock(L);
  596. return novariant(tag);
  597. }
  598. l_sinline Table *gettable (lua_State *L, int idx) {
  599. TValue *t = index2value(L, idx);
  600. api_check(L, ttistable(t), "table expected");
  601. return hvalue(t);
  602. }
  603. LUA_API int lua_rawget (lua_State *L, int idx) {
  604. Table *t;
  605. lu_byte tag;
  606. lua_lock(L);
  607. api_checkpop(L, 1);
  608. t = gettable(L, idx);
  609. tag = luaH_get(t, s2v(L->top.p - 1), s2v(L->top.p - 1));
  610. L->top.p--; /* pop key */
  611. return finishrawget(L, tag);
  612. }
  613. LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
  614. Table *t;
  615. lu_byte tag;
  616. lua_lock(L);
  617. t = gettable(L, idx);
  618. luaH_fastgeti(t, n, s2v(L->top.p), tag);
  619. return finishrawget(L, tag);
  620. }
  621. LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
  622. Table *t;
  623. TValue k;
  624. lua_lock(L);
  625. t = gettable(L, idx);
  626. setpvalue(&k, cast_voidp(p));
  627. return finishrawget(L, luaH_get(t, &k, s2v(L->top.p)));
  628. }
  629. LUA_API void lua_createtable (lua_State *L, unsigned narray, unsigned nrec) {
  630. Table *t;
  631. lua_lock(L);
  632. t = luaH_new(L);
  633. sethvalue2s(L, L->top.p, t);
  634. api_incr_top(L);
  635. if (narray > 0 || nrec > 0)
  636. luaH_resize(L, t, narray, nrec);
  637. luaC_checkGC(L);
  638. lua_unlock(L);
  639. }
  640. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  641. const TValue *obj;
  642. Table *mt;
  643. int res = 0;
  644. lua_lock(L);
  645. obj = index2value(L, objindex);
  646. switch (ttype(obj)) {
  647. case LUA_TTABLE:
  648. mt = hvalue(obj)->metatable;
  649. break;
  650. case LUA_TUSERDATA:
  651. mt = uvalue(obj)->metatable;
  652. break;
  653. default:
  654. mt = G(L)->mt[ttype(obj)];
  655. break;
  656. }
  657. if (mt != NULL) {
  658. sethvalue2s(L, L->top.p, mt);
  659. api_incr_top(L);
  660. res = 1;
  661. }
  662. lua_unlock(L);
  663. return res;
  664. }
  665. LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) {
  666. TValue *o;
  667. int t;
  668. lua_lock(L);
  669. o = index2value(L, idx);
  670. api_check(L, ttisfulluserdata(o), "full userdata expected");
  671. if (n <= 0 || n > uvalue(o)->nuvalue) {
  672. setnilvalue(s2v(L->top.p));
  673. t = LUA_TNONE;
  674. }
  675. else {
  676. setobj2s(L, L->top.p, &uvalue(o)->uv[n - 1].uv);
  677. t = ttype(s2v(L->top.p));
  678. }
  679. api_incr_top(L);
  680. lua_unlock(L);
  681. return t;
  682. }
  683. /*
  684. ** set functions (stack -> Lua)
  685. */
  686. /*
  687. ** t[k] = value at the top of the stack (where 'k' is a string)
  688. */
  689. static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
  690. int hres;
  691. TString *str = luaS_new(L, k);
  692. api_checkpop(L, 1);
  693. luaV_fastset(t, str, s2v(L->top.p - 1), hres, luaH_psetstr);
  694. if (hres == HOK) {
  695. luaV_finishfastset(L, t, s2v(L->top.p - 1));
  696. L->top.p--; /* pop value */
  697. }
  698. else {
  699. setsvalue2s(L, L->top.p, str); /* push 'str' (to make it a TValue) */
  700. api_incr_top(L);
  701. luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), hres);
  702. L->top.p -= 2; /* pop value and key */
  703. }
  704. lua_unlock(L); /* lock done by caller */
  705. }
  706. LUA_API void lua_setglobal (lua_State *L, const char *name) {
  707. TValue gt;
  708. lua_lock(L); /* unlock done in 'auxsetstr' */
  709. getGlobalTable(L, &gt);
  710. auxsetstr(L, &gt, name);
  711. }
  712. LUA_API void lua_settable (lua_State *L, int idx) {
  713. TValue *t;
  714. int hres;
  715. lua_lock(L);
  716. api_checkpop(L, 2);
  717. t = index2value(L, idx);
  718. luaV_fastset(t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres, luaH_pset);
  719. if (hres == HOK) {
  720. luaV_finishfastset(L, t, s2v(L->top.p - 1));
  721. }
  722. else
  723. luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres);
  724. L->top.p -= 2; /* pop index and value */
  725. lua_unlock(L);
  726. }
  727. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  728. lua_lock(L); /* unlock done in 'auxsetstr' */
  729. auxsetstr(L, index2value(L, idx), k);
  730. }
  731. LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
  732. TValue *t;
  733. int hres;
  734. lua_lock(L);
  735. api_checkpop(L, 1);
  736. t = index2value(L, idx);
  737. luaV_fastseti(t, n, s2v(L->top.p - 1), hres);
  738. if (hres == HOK)
  739. luaV_finishfastset(L, t, s2v(L->top.p - 1));
  740. else {
  741. TValue temp;
  742. setivalue(&temp, n);
  743. luaV_finishset(L, t, &temp, s2v(L->top.p - 1), hres);
  744. }
  745. L->top.p--; /* pop value */
  746. lua_unlock(L);
  747. }
  748. static void aux_rawset (lua_State *L, int idx, TValue *key, int n) {
  749. Table *t;
  750. lua_lock(L);
  751. api_checkpop(L, n);
  752. t = gettable(L, idx);
  753. luaH_set(L, t, key, s2v(L->top.p - 1));
  754. invalidateTMcache(t);
  755. luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
  756. L->top.p -= n;
  757. lua_unlock(L);
  758. }
  759. LUA_API void lua_rawset (lua_State *L, int idx) {
  760. aux_rawset(L, idx, s2v(L->top.p - 2), 2);
  761. }
  762. LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
  763. TValue k;
  764. setpvalue(&k, cast_voidp(p));
  765. aux_rawset(L, idx, &k, 1);
  766. }
  767. LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
  768. Table *t;
  769. lua_lock(L);
  770. api_checkpop(L, 1);
  771. t = gettable(L, idx);
  772. luaH_setint(L, t, n, s2v(L->top.p - 1));
  773. luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
  774. L->top.p--;
  775. lua_unlock(L);
  776. }
  777. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  778. TValue *obj;
  779. Table *mt;
  780. lua_lock(L);
  781. api_checkpop(L, 1);
  782. obj = index2value(L, objindex);
  783. if (ttisnil(s2v(L->top.p - 1)))
  784. mt = NULL;
  785. else {
  786. api_check(L, ttistable(s2v(L->top.p - 1)), "table expected");
  787. mt = hvalue(s2v(L->top.p - 1));
  788. }
  789. switch (ttype(obj)) {
  790. case LUA_TTABLE: {
  791. hvalue(obj)->metatable = mt;
  792. if (mt) {
  793. luaC_objbarrier(L, gcvalue(obj), mt);
  794. luaC_checkfinalizer(L, gcvalue(obj), mt);
  795. }
  796. break;
  797. }
  798. case LUA_TUSERDATA: {
  799. uvalue(obj)->metatable = mt;
  800. if (mt) {
  801. luaC_objbarrier(L, uvalue(obj), mt);
  802. luaC_checkfinalizer(L, gcvalue(obj), mt);
  803. }
  804. break;
  805. }
  806. default: {
  807. G(L)->mt[ttype(obj)] = mt;
  808. break;
  809. }
  810. }
  811. L->top.p--;
  812. lua_unlock(L);
  813. return 1;
  814. }
  815. LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) {
  816. TValue *o;
  817. int res;
  818. lua_lock(L);
  819. api_checkpop(L, 1);
  820. o = index2value(L, idx);
  821. api_check(L, ttisfulluserdata(o), "full userdata expected");
  822. if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue)))
  823. res = 0; /* 'n' not in [1, uvalue(o)->nuvalue] */
  824. else {
  825. setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top.p - 1));
  826. luaC_barrierback(L, gcvalue(o), s2v(L->top.p - 1));
  827. res = 1;
  828. }
  829. L->top.p--;
  830. lua_unlock(L);
  831. return res;
  832. }
  833. /*
  834. ** 'load' and 'call' functions (run Lua code)
  835. */
  836. #define MAXRESULTS 250
  837. #define checkresults(L,na,nr) \
  838. (api_check(L, (nr) == LUA_MULTRET \
  839. || (L->ci->top.p - L->top.p >= (nr) - (na)), \
  840. "results from function overflow current stack size"), \
  841. api_check(L, LUA_MULTRET <= (nr) && (nr) <= MAXRESULTS, \
  842. "invalid number of results"))
  843. LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
  844. lua_KContext ctx, lua_KFunction k) {
  845. StkId func;
  846. lua_lock(L);
  847. api_check(L, k == NULL || !isLua(L->ci),
  848. "cannot use continuations inside hooks");
  849. api_checkpop(L, nargs + 1);
  850. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  851. checkresults(L, nargs, nresults);
  852. func = L->top.p - (nargs+1);
  853. if (k != NULL && yieldable(L)) { /* need to prepare continuation? */
  854. L->ci->u.c.k = k; /* save continuation */
  855. L->ci->u.c.ctx = ctx; /* save context */
  856. luaD_call(L, func, nresults); /* do the call */
  857. }
  858. else /* no continuation or no yieldable */
  859. luaD_callnoyield(L, func, nresults); /* just do the call */
  860. adjustresults(L, nresults);
  861. lua_unlock(L);
  862. }
  863. /*
  864. ** Execute a protected call.
  865. */
  866. struct CallS { /* data to 'f_call' */
  867. StkId func;
  868. int nresults;
  869. };
  870. static void f_call (lua_State *L, void *ud) {
  871. struct CallS *c = cast(struct CallS *, ud);
  872. luaD_callnoyield(L, c->func, c->nresults);
  873. }
  874. LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
  875. lua_KContext ctx, lua_KFunction k) {
  876. struct CallS c;
  877. int status;
  878. ptrdiff_t func;
  879. lua_lock(L);
  880. api_check(L, k == NULL || !isLua(L->ci),
  881. "cannot use continuations inside hooks");
  882. api_checkpop(L, nargs + 1);
  883. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  884. checkresults(L, nargs, nresults);
  885. if (errfunc == 0)
  886. func = 0;
  887. else {
  888. StkId o = index2stack(L, errfunc);
  889. api_check(L, ttisfunction(s2v(o)), "error handler must be a function");
  890. func = savestack(L, o);
  891. }
  892. c.func = L->top.p - (nargs+1); /* function to be called */
  893. if (k == NULL || !yieldable(L)) { /* no continuation or no yieldable? */
  894. c.nresults = nresults; /* do a 'conventional' protected call */
  895. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  896. }
  897. else { /* prepare continuation (call is already protected by 'resume') */
  898. CallInfo *ci = L->ci;
  899. ci->u.c.k = k; /* save continuation */
  900. ci->u.c.ctx = ctx; /* save context */
  901. /* save information for error recovery */
  902. ci->u2.funcidx = cast_int(savestack(L, c.func));
  903. ci->u.c.old_errfunc = L->errfunc;
  904. L->errfunc = func;
  905. setoah(ci, L->allowhook); /* save value of 'allowhook' */
  906. ci->callstatus |= CIST_YPCALL; /* function can do error recovery */
  907. luaD_call(L, c.func, nresults); /* do the call */
  908. ci->callstatus &= ~CIST_YPCALL;
  909. L->errfunc = ci->u.c.old_errfunc;
  910. status = LUA_OK; /* if it is here, there were no errors */
  911. }
  912. adjustresults(L, nresults);
  913. lua_unlock(L);
  914. return status;
  915. }
  916. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  917. const char *chunkname, const char *mode) {
  918. ZIO z;
  919. int status;
  920. lua_lock(L);
  921. if (!chunkname) chunkname = "?";
  922. luaZ_init(L, &z, reader, data);
  923. status = luaD_protectedparser(L, &z, chunkname, mode);
  924. if (status == LUA_OK) { /* no errors? */
  925. LClosure *f = clLvalue(s2v(L->top.p - 1)); /* get new function */
  926. if (f->nupvalues >= 1) { /* does it have an upvalue? */
  927. /* get global table from registry */
  928. TValue gt;
  929. getGlobalTable(L, &gt);
  930. /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
  931. setobj(L, f->upvals[0]->v.p, &gt);
  932. luaC_barrier(L, f->upvals[0], &gt);
  933. }
  934. }
  935. lua_unlock(L);
  936. return status;
  937. }
  938. /*
  939. ** Dump a Lua function, calling 'writer' to write its parts. Ensure
  940. ** the stack returns with its original size.
  941. */
  942. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
  943. int status;
  944. ptrdiff_t otop = savestack(L, L->top.p); /* original top */
  945. TValue *f = s2v(L->top.p - 1); /* function to be dumped */
  946. lua_lock(L);
  947. api_checkpop(L, 1);
  948. api_check(L, isLfunction(f), "Lua function expected");
  949. status = luaU_dump(L, clLvalue(f)->p, writer, data, strip);
  950. L->top.p = restorestack(L, otop); /* restore top */
  951. lua_unlock(L);
  952. return status;
  953. }
  954. LUA_API int lua_status (lua_State *L) {
  955. return L->status;
  956. }
  957. /*
  958. ** Garbage-collection function
  959. */
  960. LUA_API int lua_gc (lua_State *L, int what, ...) {
  961. va_list argp;
  962. int res = 0;
  963. global_State *g = G(L);
  964. if (g->gcstp & (GCSTPGC | GCSTPCLS)) /* internal stop? */
  965. return -1; /* all options are invalid when stopped */
  966. lua_lock(L);
  967. va_start(argp, what);
  968. switch (what) {
  969. case LUA_GCSTOP: {
  970. g->gcstp = GCSTPUSR; /* stopped by the user */
  971. break;
  972. }
  973. case LUA_GCRESTART: {
  974. luaE_setdebt(g, 0);
  975. g->gcstp = 0; /* (other bits must be zero here) */
  976. break;
  977. }
  978. case LUA_GCCOLLECT: {
  979. luaC_fullgc(L, 0);
  980. break;
  981. }
  982. case LUA_GCCOUNT: {
  983. /* GC values are expressed in Kbytes: #bytes/2^10 */
  984. res = cast_int(gettotalbytes(g) >> 10);
  985. break;
  986. }
  987. case LUA_GCCOUNTB: {
  988. res = cast_int(gettotalbytes(g) & 0x3ff);
  989. break;
  990. }
  991. case LUA_GCSTEP: {
  992. lu_byte oldstp = g->gcstp;
  993. l_mem n = cast(l_mem, va_arg(argp, size_t));
  994. int work = 0; /* true if GC did some work */
  995. g->gcstp = 0; /* allow GC to run (other bits must be zero here) */
  996. if (n <= 0)
  997. n = g->GCdebt; /* force to run one basic step */
  998. luaE_setdebt(g, g->GCdebt - n);
  999. luaC_condGC(L, (void)0, work = 1);
  1000. if (work && g->gcstate == GCSpause) /* end of cycle? */
  1001. res = 1; /* signal it */
  1002. g->gcstp = oldstp; /* restore previous state */
  1003. break;
  1004. }
  1005. case LUA_GCISRUNNING: {
  1006. res = gcrunning(g);
  1007. break;
  1008. }
  1009. case LUA_GCGEN: {
  1010. res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN;
  1011. luaC_changemode(L, KGC_GENMINOR);
  1012. break;
  1013. }
  1014. case LUA_GCINC: {
  1015. res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN;
  1016. luaC_changemode(L, KGC_INC);
  1017. break;
  1018. }
  1019. case LUA_GCPARAM: {
  1020. int param = va_arg(argp, int);
  1021. int value = va_arg(argp, int);
  1022. api_check(L, 0 <= param && param < LUA_GCPN, "invalid parameter");
  1023. res = cast_int(luaO_applyparam(g->gcparams[param], 100));
  1024. if (value >= 0)
  1025. g->gcparams[param] = luaO_codeparam(cast_uint(value));
  1026. break;
  1027. }
  1028. default: res = -1; /* invalid option */
  1029. }
  1030. va_end(argp);
  1031. lua_unlock(L);
  1032. return res;
  1033. }
  1034. /*
  1035. ** miscellaneous functions
  1036. */
  1037. LUA_API int lua_error (lua_State *L) {
  1038. TValue *errobj;
  1039. lua_lock(L);
  1040. errobj = s2v(L->top.p - 1);
  1041. api_checkpop(L, 1);
  1042. /* error object is the memory error message? */
  1043. if (ttisshrstring(errobj) && eqshrstr(tsvalue(errobj), G(L)->memerrmsg))
  1044. luaM_error(L); /* raise a memory error */
  1045. else
  1046. luaG_errormsg(L); /* raise a regular error */
  1047. /* code unreachable; will unlock when control actually leaves the kernel */
  1048. return 0; /* to avoid warnings */
  1049. }
  1050. LUA_API int lua_next (lua_State *L, int idx) {
  1051. Table *t;
  1052. int more;
  1053. lua_lock(L);
  1054. api_checkpop(L, 1);
  1055. t = gettable(L, idx);
  1056. more = luaH_next(L, t, L->top.p - 1);
  1057. if (more)
  1058. api_incr_top(L);
  1059. else /* no more elements */
  1060. L->top.p--; /* pop key */
  1061. lua_unlock(L);
  1062. return more;
  1063. }
  1064. LUA_API void lua_toclose (lua_State *L, int idx) {
  1065. StkId o;
  1066. lua_lock(L);
  1067. o = index2stack(L, idx);
  1068. api_check(L, L->tbclist.p < o, "given index below or equal a marked one");
  1069. luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */
  1070. L->ci->callstatus |= CIST_CLSRET; /* mark that function has TBC slots */
  1071. lua_unlock(L);
  1072. }
  1073. LUA_API void lua_concat (lua_State *L, int n) {
  1074. lua_lock(L);
  1075. api_checknelems(L, n);
  1076. if (n > 0) {
  1077. luaV_concat(L, n);
  1078. luaC_checkGC(L);
  1079. }
  1080. else { /* nothing to concatenate */
  1081. setsvalue2s(L, L->top.p, luaS_newlstr(L, "", 0)); /* push empty string */
  1082. api_incr_top(L);
  1083. }
  1084. lua_unlock(L);
  1085. }
  1086. LUA_API void lua_len (lua_State *L, int idx) {
  1087. TValue *t;
  1088. lua_lock(L);
  1089. t = index2value(L, idx);
  1090. luaV_objlen(L, L->top.p, t);
  1091. api_incr_top(L);
  1092. lua_unlock(L);
  1093. }
  1094. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  1095. lua_Alloc f;
  1096. lua_lock(L);
  1097. if (ud) *ud = G(L)->ud;
  1098. f = G(L)->frealloc;
  1099. lua_unlock(L);
  1100. return f;
  1101. }
  1102. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  1103. lua_lock(L);
  1104. G(L)->ud = ud;
  1105. G(L)->frealloc = f;
  1106. lua_unlock(L);
  1107. }
  1108. void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud) {
  1109. lua_lock(L);
  1110. G(L)->ud_warn = ud;
  1111. G(L)->warnf = f;
  1112. lua_unlock(L);
  1113. }
  1114. void lua_warning (lua_State *L, const char *msg, int tocont) {
  1115. lua_lock(L);
  1116. luaE_warning(L, msg, tocont);
  1117. lua_unlock(L);
  1118. }
  1119. LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) {
  1120. Udata *u;
  1121. lua_lock(L);
  1122. api_check(L, 0 <= nuvalue && nuvalue < SHRT_MAX, "invalid value");
  1123. u = luaS_newudata(L, size, cast(unsigned short, nuvalue));
  1124. setuvalue(L, s2v(L->top.p), u);
  1125. api_incr_top(L);
  1126. luaC_checkGC(L);
  1127. lua_unlock(L);
  1128. return getudatamem(u);
  1129. }
  1130. static const char *aux_upvalue (TValue *fi, int n, TValue **val,
  1131. GCObject **owner) {
  1132. switch (ttypetag(fi)) {
  1133. case LUA_VCCL: { /* C closure */
  1134. CClosure *f = clCvalue(fi);
  1135. if (!(cast_uint(n) - 1u < cast_uint(f->nupvalues)))
  1136. return NULL; /* 'n' not in [1, f->nupvalues] */
  1137. *val = &f->upvalue[n-1];
  1138. if (owner) *owner = obj2gco(f);
  1139. return "";
  1140. }
  1141. case LUA_VLCL: { /* Lua closure */
  1142. LClosure *f = clLvalue(fi);
  1143. TString *name;
  1144. Proto *p = f->p;
  1145. if (!(cast_uint(n) - 1u < cast_uint(p->sizeupvalues)))
  1146. return NULL; /* 'n' not in [1, p->sizeupvalues] */
  1147. *val = f->upvals[n-1]->v.p;
  1148. if (owner) *owner = obj2gco(f->upvals[n - 1]);
  1149. name = p->upvalues[n-1].name;
  1150. return (name == NULL) ? "(no name)" : getstr(name);
  1151. }
  1152. default: return NULL; /* not a closure */
  1153. }
  1154. }
  1155. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  1156. const char *name;
  1157. TValue *val = NULL; /* to avoid warnings */
  1158. lua_lock(L);
  1159. name = aux_upvalue(index2value(L, funcindex), n, &val, NULL);
  1160. if (name) {
  1161. setobj2s(L, L->top.p, val);
  1162. api_incr_top(L);
  1163. }
  1164. lua_unlock(L);
  1165. return name;
  1166. }
  1167. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  1168. const char *name;
  1169. TValue *val = NULL; /* to avoid warnings */
  1170. GCObject *owner = NULL; /* to avoid warnings */
  1171. TValue *fi;
  1172. lua_lock(L);
  1173. fi = index2value(L, funcindex);
  1174. api_checknelems(L, 1);
  1175. name = aux_upvalue(fi, n, &val, &owner);
  1176. if (name) {
  1177. L->top.p--;
  1178. setobj(L, val, s2v(L->top.p));
  1179. luaC_barrier(L, owner, val);
  1180. }
  1181. lua_unlock(L);
  1182. return name;
  1183. }
  1184. static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
  1185. static const UpVal *const nullup = NULL;
  1186. LClosure *f;
  1187. TValue *fi = index2value(L, fidx);
  1188. api_check(L, ttisLclosure(fi), "Lua function expected");
  1189. f = clLvalue(fi);
  1190. if (pf) *pf = f;
  1191. if (1 <= n && n <= f->p->sizeupvalues)
  1192. return &f->upvals[n - 1]; /* get its upvalue pointer */
  1193. else
  1194. return (UpVal**)&nullup;
  1195. }
  1196. LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
  1197. TValue *fi = index2value(L, fidx);
  1198. switch (ttypetag(fi)) {
  1199. case LUA_VLCL: { /* lua closure */
  1200. return *getupvalref(L, fidx, n, NULL);
  1201. }
  1202. case LUA_VCCL: { /* C closure */
  1203. CClosure *f = clCvalue(fi);
  1204. if (1 <= n && n <= f->nupvalues)
  1205. return &f->upvalue[n - 1];
  1206. /* else */
  1207. } /* FALLTHROUGH */
  1208. case LUA_VLCF:
  1209. return NULL; /* light C functions have no upvalues */
  1210. default: {
  1211. api_check(L, 0, "function expected");
  1212. return NULL;
  1213. }
  1214. }
  1215. }
  1216. LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
  1217. int fidx2, int n2) {
  1218. LClosure *f1;
  1219. UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
  1220. UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
  1221. api_check(L, *up1 != NULL && *up2 != NULL, "invalid upvalue index");
  1222. *up1 = *up2;
  1223. luaC_objbarrier(L, f1, *up1);
  1224. }