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