lapi.c 36 KB

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