lapi.c 35 KB

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