lapi.c 35 KB

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