lapi.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463
  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 = vslen(o);
  341. lua_unlock(L);
  342. return svalue(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. const TValue *slot;
  519. TString *str = luaS_new(L, k);
  520. if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
  521. setobj2s(L, L->top.p, slot);
  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, slot);
  528. }
  529. lua_unlock(L);
  530. return ttype(s2v(L->top.p - 1));
  531. }
  532. /*
  533. ** Get the global table in the registry. Since all predefined
  534. ** indices in the registry were inserted right when the registry
  535. ** was created and never removed, they must always be in the array
  536. ** part of the registry.
  537. */
  538. #define getGtable(L) \
  539. (&hvalue(&G(L)->l_registry)->array[LUA_RIDX_GLOBALS - 1])
  540. LUA_API int lua_getglobal (lua_State *L, const char *name) {
  541. const TValue *G;
  542. lua_lock(L);
  543. G = getGtable(L);
  544. return auxgetstr(L, G, name);
  545. }
  546. LUA_API int lua_gettable (lua_State *L, int idx) {
  547. const TValue *slot;
  548. TValue *t;
  549. lua_lock(L);
  550. t = index2value(L, idx);
  551. if (luaV_fastget(L, t, s2v(L->top.p - 1), slot, luaH_get)) {
  552. setobj2s(L, L->top.p - 1, slot);
  553. }
  554. else
  555. luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot);
  556. lua_unlock(L);
  557. return ttype(s2v(L->top.p - 1));
  558. }
  559. LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
  560. lua_lock(L);
  561. return auxgetstr(L, index2value(L, idx), k);
  562. }
  563. LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
  564. TValue *t;
  565. const TValue *slot;
  566. lua_lock(L);
  567. t = index2value(L, idx);
  568. if (luaV_fastgeti(L, t, n, slot)) {
  569. setobj2s(L, L->top.p, slot);
  570. }
  571. else {
  572. TValue aux;
  573. setivalue(&aux, n);
  574. luaV_finishget(L, t, &aux, L->top.p, slot);
  575. }
  576. api_incr_top(L);
  577. lua_unlock(L);
  578. return ttype(s2v(L->top.p - 1));
  579. }
  580. l_sinline int finishrawget (lua_State *L, const TValue *val) {
  581. if (isempty(val)) /* avoid copying empty items to the stack */
  582. setnilvalue(s2v(L->top.p));
  583. else
  584. setobj2s(L, L->top.p, val);
  585. api_incr_top(L);
  586. lua_unlock(L);
  587. return ttype(s2v(L->top.p - 1));
  588. }
  589. static Table *gettable (lua_State *L, int idx) {
  590. TValue *t = index2value(L, idx);
  591. api_check(L, ttistable(t), "table expected");
  592. return hvalue(t);
  593. }
  594. LUA_API int lua_rawget (lua_State *L, int idx) {
  595. Table *t;
  596. const TValue *val;
  597. lua_lock(L);
  598. api_checknelems(L, 1);
  599. t = gettable(L, idx);
  600. val = luaH_get(t, s2v(L->top.p - 1));
  601. L->top.p--; /* remove key */
  602. return finishrawget(L, val);
  603. }
  604. LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
  605. Table *t;
  606. lua_lock(L);
  607. t = gettable(L, idx);
  608. return finishrawget(L, luaH_getint(t, n));
  609. }
  610. LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
  611. Table *t;
  612. TValue k;
  613. lua_lock(L);
  614. t = gettable(L, idx);
  615. setpvalue(&k, cast_voidp(p));
  616. return finishrawget(L, luaH_get(t, &k));
  617. }
  618. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  619. Table *t;
  620. lua_lock(L);
  621. t = luaH_new(L);
  622. sethvalue2s(L, L->top.p, t);
  623. api_incr_top(L);
  624. if (narray > 0 || nrec > 0)
  625. luaH_resize(L, t, narray, nrec);
  626. luaC_checkGC(L);
  627. lua_unlock(L);
  628. }
  629. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  630. const TValue *obj;
  631. Table *mt;
  632. int res = 0;
  633. lua_lock(L);
  634. obj = index2value(L, objindex);
  635. switch (ttype(obj)) {
  636. case LUA_TTABLE:
  637. mt = hvalue(obj)->metatable;
  638. break;
  639. case LUA_TUSERDATA:
  640. mt = uvalue(obj)->metatable;
  641. break;
  642. default:
  643. mt = G(L)->mt[ttype(obj)];
  644. break;
  645. }
  646. if (mt != NULL) {
  647. sethvalue2s(L, L->top.p, mt);
  648. api_incr_top(L);
  649. res = 1;
  650. }
  651. lua_unlock(L);
  652. return res;
  653. }
  654. LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) {
  655. TValue *o;
  656. int t;
  657. lua_lock(L);
  658. o = index2value(L, idx);
  659. api_check(L, ttisfulluserdata(o), "full userdata expected");
  660. if (n <= 0 || n > uvalue(o)->nuvalue) {
  661. setnilvalue(s2v(L->top.p));
  662. t = LUA_TNONE;
  663. }
  664. else {
  665. setobj2s(L, L->top.p, &uvalue(o)->uv[n - 1].uv);
  666. t = ttype(s2v(L->top.p));
  667. }
  668. api_incr_top(L);
  669. lua_unlock(L);
  670. return t;
  671. }
  672. /*
  673. ** set functions (stack -> Lua)
  674. */
  675. /*
  676. ** t[k] = value at the top of the stack (where 'k' is a string)
  677. */
  678. static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
  679. const TValue *slot;
  680. TString *str = luaS_new(L, k);
  681. api_checknelems(L, 1);
  682. if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
  683. luaV_finishfastset(L, t, slot, s2v(L->top.p - 1));
  684. L->top.p--; /* pop value */
  685. }
  686. else {
  687. setsvalue2s(L, L->top.p, str); /* push 'str' (to make it a TValue) */
  688. api_incr_top(L);
  689. luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), slot);
  690. L->top.p -= 2; /* pop value and key */
  691. }
  692. lua_unlock(L); /* lock done by caller */
  693. }
  694. LUA_API void lua_setglobal (lua_State *L, const char *name) {
  695. const TValue *G;
  696. lua_lock(L); /* unlock done in 'auxsetstr' */
  697. G = getGtable(L);
  698. auxsetstr(L, G, name);
  699. }
  700. LUA_API void lua_settable (lua_State *L, int idx) {
  701. TValue *t;
  702. const TValue *slot;
  703. lua_lock(L);
  704. api_checknelems(L, 2);
  705. t = index2value(L, idx);
  706. if (luaV_fastget(L, t, s2v(L->top.p - 2), slot, luaH_get)) {
  707. luaV_finishfastset(L, t, slot, s2v(L->top.p - 1));
  708. }
  709. else
  710. luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), slot);
  711. L->top.p -= 2; /* pop index and value */
  712. lua_unlock(L);
  713. }
  714. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  715. lua_lock(L); /* unlock done in 'auxsetstr' */
  716. auxsetstr(L, index2value(L, idx), k);
  717. }
  718. LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
  719. TValue *t;
  720. const TValue *slot;
  721. lua_lock(L);
  722. api_checknelems(L, 1);
  723. t = index2value(L, idx);
  724. if (luaV_fastgeti(L, t, n, slot)) {
  725. luaV_finishfastset(L, t, slot, s2v(L->top.p - 1));
  726. }
  727. else {
  728. TValue aux;
  729. setivalue(&aux, n);
  730. luaV_finishset(L, t, &aux, s2v(L->top.p - 1), slot);
  731. }
  732. L->top.p--; /* pop value */
  733. lua_unlock(L);
  734. }
  735. static void aux_rawset (lua_State *L, int idx, TValue *key, int n) {
  736. Table *t;
  737. lua_lock(L);
  738. api_checknelems(L, n);
  739. t = gettable(L, idx);
  740. luaH_set(L, t, key, s2v(L->top.p - 1));
  741. invalidateTMcache(t);
  742. luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
  743. L->top.p -= n;
  744. lua_unlock(L);
  745. }
  746. LUA_API void lua_rawset (lua_State *L, int idx) {
  747. aux_rawset(L, idx, s2v(L->top.p - 2), 2);
  748. }
  749. LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
  750. TValue k;
  751. setpvalue(&k, cast_voidp(p));
  752. aux_rawset(L, idx, &k, 1);
  753. }
  754. LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
  755. Table *t;
  756. lua_lock(L);
  757. api_checknelems(L, 1);
  758. t = gettable(L, idx);
  759. luaH_setint(L, t, n, s2v(L->top.p - 1));
  760. luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
  761. L->top.p--;
  762. lua_unlock(L);
  763. }
  764. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  765. TValue *obj;
  766. Table *mt;
  767. lua_lock(L);
  768. api_checknelems(L, 1);
  769. obj = index2value(L, objindex);
  770. if (ttisnil(s2v(L->top.p - 1)))
  771. mt = NULL;
  772. else {
  773. api_check(L, ttistable(s2v(L->top.p - 1)), "table expected");
  774. mt = hvalue(s2v(L->top.p - 1));
  775. }
  776. switch (ttype(obj)) {
  777. case LUA_TTABLE: {
  778. hvalue(obj)->metatable = mt;
  779. if (mt) {
  780. luaC_objbarrier(L, gcvalue(obj), mt);
  781. luaC_checkfinalizer(L, gcvalue(obj), mt);
  782. }
  783. break;
  784. }
  785. case LUA_TUSERDATA: {
  786. uvalue(obj)->metatable = mt;
  787. if (mt) {
  788. luaC_objbarrier(L, uvalue(obj), mt);
  789. luaC_checkfinalizer(L, gcvalue(obj), mt);
  790. }
  791. break;
  792. }
  793. default: {
  794. G(L)->mt[ttype(obj)] = mt;
  795. break;
  796. }
  797. }
  798. L->top.p--;
  799. lua_unlock(L);
  800. return 1;
  801. }
  802. LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) {
  803. TValue *o;
  804. int res;
  805. lua_lock(L);
  806. api_checknelems(L, 1);
  807. o = index2value(L, idx);
  808. api_check(L, ttisfulluserdata(o), "full userdata expected");
  809. if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue)))
  810. res = 0; /* 'n' not in [1, uvalue(o)->nuvalue] */
  811. else {
  812. setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top.p - 1));
  813. luaC_barrierback(L, gcvalue(o), s2v(L->top.p - 1));
  814. res = 1;
  815. }
  816. L->top.p--;
  817. lua_unlock(L);
  818. return res;
  819. }
  820. /*
  821. ** 'load' and 'call' functions (run Lua code)
  822. */
  823. #define checkresults(L,na,nr) \
  824. api_check(L, (nr) == LUA_MULTRET \
  825. || (L->ci->top.p - L->top.p >= (nr) - (na)), \
  826. "results from function overflow current stack size")
  827. LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
  828. lua_KContext ctx, lua_KFunction k) {
  829. StkId func;
  830. lua_lock(L);
  831. api_check(L, k == NULL || !isLua(L->ci),
  832. "cannot use continuations inside hooks");
  833. api_checknelems(L, nargs+1);
  834. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  835. checkresults(L, nargs, nresults);
  836. func = L->top.p - (nargs+1);
  837. if (k != NULL && yieldable(L)) { /* need to prepare continuation? */
  838. L->ci->u.c.k = k; /* save continuation */
  839. L->ci->u.c.ctx = ctx; /* save context */
  840. luaD_call(L, func, nresults); /* do the call */
  841. }
  842. else /* no continuation or no yieldable */
  843. luaD_callnoyield(L, func, nresults); /* just do the call */
  844. adjustresults(L, nresults);
  845. lua_unlock(L);
  846. }
  847. /*
  848. ** Execute a protected call.
  849. */
  850. struct CallS { /* data to 'f_call' */
  851. StkId func;
  852. int nresults;
  853. };
  854. static void f_call (lua_State *L, void *ud) {
  855. struct CallS *c = cast(struct CallS *, ud);
  856. luaD_callnoyield(L, c->func, c->nresults);
  857. }
  858. LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
  859. lua_KContext ctx, lua_KFunction k) {
  860. struct CallS c;
  861. int status;
  862. ptrdiff_t func;
  863. lua_lock(L);
  864. api_check(L, k == NULL || !isLua(L->ci),
  865. "cannot use continuations inside hooks");
  866. api_checknelems(L, nargs+1);
  867. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  868. checkresults(L, nargs, nresults);
  869. if (errfunc == 0)
  870. func = 0;
  871. else {
  872. StkId o = index2stack(L, errfunc);
  873. api_check(L, ttisfunction(s2v(o)), "error handler must be a function");
  874. func = savestack(L, o);
  875. }
  876. c.func = L->top.p - (nargs+1); /* function to be called */
  877. if (k == NULL || !yieldable(L)) { /* no continuation or no yieldable? */
  878. c.nresults = nresults; /* do a 'conventional' protected call */
  879. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  880. }
  881. else { /* prepare continuation (call is already protected by 'resume') */
  882. CallInfo *ci = L->ci;
  883. ci->u.c.k = k; /* save continuation */
  884. ci->u.c.ctx = ctx; /* save context */
  885. /* save information for error recovery */
  886. ci->u2.funcidx = cast_int(savestack(L, c.func));
  887. ci->u.c.old_errfunc = L->errfunc;
  888. L->errfunc = func;
  889. setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */
  890. ci->callstatus |= CIST_YPCALL; /* function can do error recovery */
  891. luaD_call(L, c.func, nresults); /* do the call */
  892. ci->callstatus &= ~CIST_YPCALL;
  893. L->errfunc = ci->u.c.old_errfunc;
  894. status = LUA_OK; /* if it is here, there were no errors */
  895. }
  896. adjustresults(L, nresults);
  897. lua_unlock(L);
  898. return status;
  899. }
  900. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  901. const char *chunkname, const char *mode) {
  902. ZIO z;
  903. int status;
  904. lua_lock(L);
  905. if (!chunkname) chunkname = "?";
  906. luaZ_init(L, &z, reader, data);
  907. status = luaD_protectedparser(L, &z, chunkname, mode);
  908. if (status == LUA_OK) { /* no errors? */
  909. LClosure *f = clLvalue(s2v(L->top.p - 1)); /* get new function */
  910. if (f->nupvalues >= 1) { /* does it have an upvalue? */
  911. /* get global table from registry */
  912. const TValue *gt = getGtable(L);
  913. /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
  914. setobj(L, f->upvals[0]->v.p, gt);
  915. luaC_barrier(L, f->upvals[0], gt);
  916. }
  917. }
  918. lua_unlock(L);
  919. return status;
  920. }
  921. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
  922. int status;
  923. TValue *o;
  924. lua_lock(L);
  925. api_checknelems(L, 1);
  926. o = s2v(L->top.p - 1);
  927. if (isLfunction(o))
  928. status = luaU_dump(L, getproto(o), writer, data, strip);
  929. else
  930. status = 1;
  931. lua_unlock(L);
  932. return status;
  933. }
  934. LUA_API int lua_status (lua_State *L) {
  935. return L->status;
  936. }
  937. /*
  938. ** Garbage-collection function
  939. */
  940. LUA_API int lua_gc (lua_State *L, int what, ...) {
  941. va_list argp;
  942. int res = 0;
  943. global_State *g = G(L);
  944. if (g->gcstp & GCSTPGC) /* internal stop? */
  945. return -1; /* all options are invalid when stopped */
  946. lua_lock(L);
  947. va_start(argp, what);
  948. switch (what) {
  949. case LUA_GCSTOP: {
  950. g->gcstp = GCSTPUSR; /* stopped by the user */
  951. break;
  952. }
  953. case LUA_GCRESTART: {
  954. luaE_setdebt(g, 0);
  955. g->gcstp = 0; /* (GCSTPGC must be already zero here) */
  956. break;
  957. }
  958. case LUA_GCCOLLECT: {
  959. luaC_fullgc(L, 0);
  960. break;
  961. }
  962. case LUA_GCCOUNT: {
  963. /* GC values are expressed in Kbytes: #bytes/2^10 */
  964. res = cast_int(gettotalbytes(g) >> 10);
  965. break;
  966. }
  967. case LUA_GCCOUNTB: {
  968. res = cast_int(gettotalbytes(g) & 0x3ff);
  969. break;
  970. }
  971. case LUA_GCSTEP: {
  972. int data = va_arg(argp, int);
  973. l_mem debt = 1; /* =1 to signal that it did an actual step */
  974. lu_byte oldstp = g->gcstp;
  975. g->gcstp = 0; /* allow GC to run (GCSTPGC must be zero here) */
  976. if (data == 0) {
  977. luaE_setdebt(g, 0); /* do a basic step */
  978. luaC_step(L);
  979. }
  980. else { /* add 'data' to total debt */
  981. debt = cast(l_mem, data) * 1024 + g->GCdebt;
  982. luaE_setdebt(g, debt);
  983. luaC_checkGC(L);
  984. }
  985. g->gcstp = oldstp; /* restore previous state */
  986. if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */
  987. res = 1; /* signal it */
  988. break;
  989. }
  990. case LUA_GCSETPAUSE: {
  991. int data = va_arg(argp, int);
  992. res = getgcparam(g->gcpause);
  993. setgcparam(g->gcpause, data);
  994. break;
  995. }
  996. case LUA_GCSETSTEPMUL: {
  997. int data = va_arg(argp, int);
  998. res = getgcparam(g->gcstepmul);
  999. setgcparam(g->gcstepmul, data);
  1000. break;
  1001. }
  1002. case LUA_GCISRUNNING: {
  1003. res = gcrunning(g);
  1004. break;
  1005. }
  1006. case LUA_GCGEN: {
  1007. int minormul = va_arg(argp, int);
  1008. int majormul = va_arg(argp, int);
  1009. res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC;
  1010. if (minormul != 0)
  1011. g->genminormul = minormul;
  1012. if (majormul != 0)
  1013. setgcparam(g->genmajormul, majormul);
  1014. luaC_changemode(L, KGC_GEN);
  1015. break;
  1016. }
  1017. case LUA_GCINC: {
  1018. int pause = va_arg(argp, int);
  1019. int stepmul = va_arg(argp, int);
  1020. int stepsize = va_arg(argp, int);
  1021. res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC;
  1022. if (pause != 0)
  1023. setgcparam(g->gcpause, pause);
  1024. if (stepmul != 0)
  1025. setgcparam(g->gcstepmul, stepmul);
  1026. if (stepsize != 0)
  1027. g->gcstepsize = stepsize;
  1028. luaC_changemode(L, KGC_INC);
  1029. break;
  1030. }
  1031. default: res = -1; /* invalid option */
  1032. }
  1033. va_end(argp);
  1034. lua_unlock(L);
  1035. return res;
  1036. }
  1037. /*
  1038. ** miscellaneous functions
  1039. */
  1040. LUA_API int lua_error (lua_State *L) {
  1041. TValue *errobj;
  1042. lua_lock(L);
  1043. errobj = s2v(L->top.p - 1);
  1044. api_checknelems(L, 1);
  1045. /* error object is the memory error message? */
  1046. if (ttisshrstring(errobj) && eqshrstr(tsvalue(errobj), G(L)->memerrmsg))
  1047. luaM_error(L); /* raise a memory error */
  1048. else
  1049. luaG_errormsg(L); /* raise a regular error */
  1050. /* code unreachable; will unlock when control actually leaves the kernel */
  1051. return 0; /* to avoid warnings */
  1052. }
  1053. LUA_API int lua_next (lua_State *L, int idx) {
  1054. Table *t;
  1055. int more;
  1056. lua_lock(L);
  1057. api_checknelems(L, 1);
  1058. t = gettable(L, idx);
  1059. more = luaH_next(L, t, L->top.p - 1);
  1060. if (more) {
  1061. api_incr_top(L);
  1062. }
  1063. else /* no more elements */
  1064. L->top.p -= 1; /* remove key */
  1065. lua_unlock(L);
  1066. return more;
  1067. }
  1068. LUA_API void lua_toclose (lua_State *L, int idx) {
  1069. int nresults;
  1070. StkId o;
  1071. lua_lock(L);
  1072. o = index2stack(L, idx);
  1073. nresults = L->ci->nresults;
  1074. api_check(L, L->tbclist.p < o, "given index below or equal a marked one");
  1075. luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */
  1076. if (!hastocloseCfunc(nresults)) /* function not marked yet? */
  1077. L->ci->nresults = codeNresults(nresults); /* mark it */
  1078. lua_assert(hastocloseCfunc(L->ci->nresults));
  1079. lua_unlock(L);
  1080. }
  1081. LUA_API void lua_concat (lua_State *L, int n) {
  1082. lua_lock(L);
  1083. api_checknelems(L, n);
  1084. if (n > 0)
  1085. luaV_concat(L, n);
  1086. else { /* nothing to concatenate */
  1087. setsvalue2s(L, L->top.p, luaS_newlstr(L, "", 0)); /* push empty string */
  1088. api_incr_top(L);
  1089. }
  1090. luaC_checkGC(L);
  1091. lua_unlock(L);
  1092. }
  1093. LUA_API void lua_len (lua_State *L, int idx) {
  1094. TValue *t;
  1095. lua_lock(L);
  1096. t = index2value(L, idx);
  1097. luaV_objlen(L, L->top.p, t);
  1098. api_incr_top(L);
  1099. lua_unlock(L);
  1100. }
  1101. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  1102. lua_Alloc f;
  1103. lua_lock(L);
  1104. if (ud) *ud = G(L)->ud;
  1105. f = G(L)->frealloc;
  1106. lua_unlock(L);
  1107. return f;
  1108. }
  1109. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  1110. lua_lock(L);
  1111. G(L)->ud = ud;
  1112. G(L)->frealloc = f;
  1113. lua_unlock(L);
  1114. }
  1115. void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud) {
  1116. lua_lock(L);
  1117. G(L)->ud_warn = ud;
  1118. G(L)->warnf = f;
  1119. lua_unlock(L);
  1120. }
  1121. void lua_warning (lua_State *L, const char *msg, int tocont) {
  1122. lua_lock(L);
  1123. luaE_warning(L, msg, tocont);
  1124. lua_unlock(L);
  1125. }
  1126. LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) {
  1127. Udata *u;
  1128. lua_lock(L);
  1129. api_check(L, 0 <= nuvalue && nuvalue < USHRT_MAX, "invalid value");
  1130. u = luaS_newudata(L, size, nuvalue);
  1131. setuvalue(L, s2v(L->top.p), u);
  1132. api_incr_top(L);
  1133. luaC_checkGC(L);
  1134. lua_unlock(L);
  1135. return getudatamem(u);
  1136. }
  1137. static const char *aux_upvalue (TValue *fi, int n, TValue **val,
  1138. GCObject **owner) {
  1139. switch (ttypetag(fi)) {
  1140. case LUA_VCCL: { /* C closure */
  1141. CClosure *f = clCvalue(fi);
  1142. if (!(cast_uint(n) - 1u < cast_uint(f->nupvalues)))
  1143. return NULL; /* 'n' not in [1, f->nupvalues] */
  1144. *val = &f->upvalue[n-1];
  1145. if (owner) *owner = obj2gco(f);
  1146. return "";
  1147. }
  1148. case LUA_VLCL: { /* Lua closure */
  1149. LClosure *f = clLvalue(fi);
  1150. TString *name;
  1151. Proto *p = f->p;
  1152. if (!(cast_uint(n) - 1u < cast_uint(p->sizeupvalues)))
  1153. return NULL; /* 'n' not in [1, p->sizeupvalues] */
  1154. *val = f->upvals[n-1]->v.p;
  1155. if (owner) *owner = obj2gco(f->upvals[n - 1]);
  1156. name = p->upvalues[n-1].name;
  1157. return (name == NULL) ? "(no name)" : getstr(name);
  1158. }
  1159. default: return NULL; /* not a closure */
  1160. }
  1161. }
  1162. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  1163. const char *name;
  1164. TValue *val = NULL; /* to avoid warnings */
  1165. lua_lock(L);
  1166. name = aux_upvalue(index2value(L, funcindex), n, &val, NULL);
  1167. if (name) {
  1168. setobj2s(L, L->top.p, val);
  1169. api_incr_top(L);
  1170. }
  1171. lua_unlock(L);
  1172. return name;
  1173. }
  1174. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  1175. const char *name;
  1176. TValue *val = NULL; /* to avoid warnings */
  1177. GCObject *owner = NULL; /* to avoid warnings */
  1178. TValue *fi;
  1179. lua_lock(L);
  1180. fi = index2value(L, funcindex);
  1181. api_checknelems(L, 1);
  1182. name = aux_upvalue(fi, n, &val, &owner);
  1183. if (name) {
  1184. L->top.p--;
  1185. setobj(L, val, s2v(L->top.p));
  1186. luaC_barrier(L, owner, val);
  1187. }
  1188. lua_unlock(L);
  1189. return name;
  1190. }
  1191. static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
  1192. static const UpVal *const nullup = NULL;
  1193. LClosure *f;
  1194. TValue *fi = index2value(L, fidx);
  1195. api_check(L, ttisLclosure(fi), "Lua function expected");
  1196. f = clLvalue(fi);
  1197. if (pf) *pf = f;
  1198. if (1 <= n && n <= f->p->sizeupvalues)
  1199. return &f->upvals[n - 1]; /* get its upvalue pointer */
  1200. else
  1201. return (UpVal**)&nullup;
  1202. }
  1203. LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
  1204. TValue *fi = index2value(L, fidx);
  1205. switch (ttypetag(fi)) {
  1206. case LUA_VLCL: { /* lua closure */
  1207. return *getupvalref(L, fidx, n, NULL);
  1208. }
  1209. case LUA_VCCL: { /* C closure */
  1210. CClosure *f = clCvalue(fi);
  1211. if (1 <= n && n <= f->nupvalues)
  1212. return &f->upvalue[n - 1];
  1213. /* else */
  1214. } /* FALLTHROUGH */
  1215. case LUA_VLCF:
  1216. return NULL; /* light C functions have no upvalues */
  1217. default: {
  1218. api_check(L, 0, "function expected");
  1219. return NULL;
  1220. }
  1221. }
  1222. }
  1223. LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
  1224. int fidx2, int n2) {
  1225. LClosure *f1;
  1226. UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
  1227. UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
  1228. api_check(L, *up1 != NULL && *up2 != NULL, "invalid upvalue index");
  1229. *up1 = *up2;
  1230. luaC_objbarrier(L, f1, *up1);
  1231. }