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