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. */
  32. #define isvalid(L, o) ((o) != &G(L)->nilvalue)
  33. /* test for pseudo index */
  34. #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX)
  35. /* test for upvalue */
  36. #define isupvalue(i) ((i) < LUA_REGISTRYINDEX)
  37. /*
  38. ** Convert an acceptable index to a pointer to its respective value.
  39. ** Non-valid indices return the special nil value 'G(L)->nilvalue'.
  40. */
  41. static TValue *index2value (lua_State *L, int idx) {
  42. CallInfo *ci = L->ci;
  43. if (idx > 0) {
  44. StkId o = ci->func.p + idx;
  45. api_check(L, idx <= ci->top.p - (ci->func.p + 1), "unacceptable index");
  46. if (o >= L->top.p) return &G(L)->nilvalue;
  47. else return s2v(o);
  48. }
  49. else if (!ispseudo(idx)) { /* negative index */
  50. api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1),
  51. "invalid index");
  52. return s2v(L->top.p + idx);
  53. }
  54. else if (idx == LUA_REGISTRYINDEX)
  55. return &G(L)->l_registry;
  56. else { /* upvalues */
  57. idx = LUA_REGISTRYINDEX - idx;
  58. api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
  59. if (ttisCclosure(s2v(ci->func.p))) { /* C closure? */
  60. CClosure *func = clCvalue(s2v(ci->func.p));
  61. return (idx <= func->nupvalues) ? &func->upvalue[idx-1]
  62. : &G(L)->nilvalue;
  63. }
  64. else { /* light C function or Lua function (through a hook)?) */
  65. api_check(L, ttislcf(s2v(ci->func.p)), "caller not a C function");
  66. return &G(L)->nilvalue; /* no upvalues */
  67. }
  68. }
  69. }
  70. /*
  71. ** Convert a valid actual index (not a pseudo-index) to its address.
  72. */
  73. static StkId index2stack (lua_State *L, int idx) {
  74. CallInfo *ci = L->ci;
  75. if (idx > 0) {
  76. StkId o = ci->func.p + idx;
  77. api_check(L, o < L->top.p, "invalid index");
  78. return o;
  79. }
  80. else { /* non-positive index */
  81. api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1),
  82. "invalid index");
  83. api_check(L, !ispseudo(idx), "invalid index");
  84. return L->top.p + idx;
  85. }
  86. }
  87. LUA_API int lua_checkstack (lua_State *L, int n) {
  88. int res;
  89. CallInfo *ci;
  90. lua_lock(L);
  91. ci = L->ci;
  92. api_check(L, n >= 0, "negative 'n'");
  93. if (L->stack_last.p - L->top.p > n) /* stack large enough? */
  94. res = 1; /* yes; check is OK */
  95. else /* need to grow stack */
  96. res = luaD_growstack(L, n, 0);
  97. if (res && ci->top.p < L->top.p + n)
  98. ci->top.p = L->top.p + n; /* adjust frame top */
  99. lua_unlock(L);
  100. return res;
  101. }
  102. LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
  103. int i;
  104. if (from == to) return;
  105. lua_lock(to);
  106. api_checkpop(from, n);
  107. api_check(from, G(from) == G(to), "moving among independent states");
  108. api_check(from, to->ci->top.p - to->top.p >= n, "stack overflow");
  109. from->top.p -= n;
  110. for (i = 0; i < n; i++) {
  111. setobjs2s(to, to->top.p, from->top.p + i);
  112. to->top.p++; /* stack already checked by previous 'api_check' */
  113. }
  114. lua_unlock(to);
  115. }
  116. LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
  117. lua_CFunction old;
  118. lua_lock(L);
  119. old = G(L)->panic;
  120. G(L)->panic = panicf;
  121. lua_unlock(L);
  122. return old;
  123. }
  124. LUA_API lua_Number lua_version (lua_State *L) {
  125. UNUSED(L);
  126. return LUA_VERSION_NUM;
  127. }
  128. /*
  129. ** basic stack manipulation
  130. */
  131. /*
  132. ** convert an acceptable stack index into an absolute index
  133. */
  134. LUA_API int lua_absindex (lua_State *L, int idx) {
  135. return (idx > 0 || ispseudo(idx))
  136. ? idx
  137. : cast_int(L->top.p - L->ci->func.p) + idx;
  138. }
  139. LUA_API int lua_gettop (lua_State *L) {
  140. return cast_int(L->top.p - (L->ci->func.p + 1));
  141. }
  142. LUA_API void lua_settop (lua_State *L, int idx) {
  143. CallInfo *ci;
  144. StkId func, newtop;
  145. ptrdiff_t diff; /* difference for new top */
  146. lua_lock(L);
  147. ci = L->ci;
  148. func = ci->func.p;
  149. if (idx >= 0) {
  150. api_check(L, idx <= ci->top.p - (func + 1), "new top too large");
  151. diff = ((func + 1) + idx) - L->top.p;
  152. for (; diff > 0; diff--)
  153. setnilvalue(s2v(L->top.p++)); /* clear new slots */
  154. }
  155. else {
  156. api_check(L, -(idx+1) <= (L->top.p - (func + 1)), "invalid new top");
  157. diff = idx + 1; /* will "subtract" index (as it is negative) */
  158. }
  159. newtop = L->top.p + diff;
  160. if (diff < 0 && L->tbclist.p >= newtop) {
  161. lua_assert(ci->callstatus & CIST_TBC);
  162. newtop = luaF_close(L, newtop, CLOSEKTOP, 0);
  163. }
  164. L->top.p = newtop; /* correct top only after closing any upvalue */
  165. lua_unlock(L);
  166. }
  167. LUA_API void lua_closeslot (lua_State *L, int idx) {
  168. StkId level;
  169. lua_lock(L);
  170. level = index2stack(L, idx);
  171. api_check(L, (L->ci->callstatus & CIST_TBC) && (L->tbclist.p == level),
  172. "no variable to close at given level");
  173. level = luaF_close(L, level, CLOSEKTOP, 0);
  174. setnilvalue(s2v(level));
  175. lua_unlock(L);
  176. }
  177. /*
  178. ** Reverse the stack segment from 'from' to 'to'
  179. ** (auxiliary to 'lua_rotate')
  180. ** Note that we move(copy) only the value inside the stack.
  181. ** (We do not move additional fields that may exist.)
  182. */
  183. static void reverse (lua_State *L, StkId from, StkId to) {
  184. for (; from < to; from++, to--) {
  185. TValue temp;
  186. setobj(L, &temp, s2v(from));
  187. setobjs2s(L, from, to);
  188. setobj2s(L, to, &temp);
  189. }
  190. }
  191. /*
  192. ** Let x = AB, where A is a prefix of length 'n'. Then,
  193. ** rotate x n == BA. But BA == (A^r . B^r)^r.
  194. */
  195. LUA_API void lua_rotate (lua_State *L, int idx, int n) {
  196. StkId p, t, m;
  197. lua_lock(L);
  198. t = L->top.p - 1; /* end of stack segment being rotated */
  199. p = index2stack(L, idx); /* start of segment */
  200. api_check(L, L->tbclist.p < p, "moving a to-be-closed slot");
  201. api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
  202. m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */
  203. reverse(L, p, m); /* reverse the prefix with length 'n' */
  204. reverse(L, m + 1, t); /* reverse the suffix */
  205. reverse(L, p, t); /* reverse the entire segment */
  206. lua_unlock(L);
  207. }
  208. LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
  209. TValue *fr, *to;
  210. lua_lock(L);
  211. fr = index2value(L, fromidx);
  212. to = index2value(L, toidx);
  213. api_check(L, isvalid(L, to), "invalid index");
  214. setobj(L, to, fr);
  215. if (isupvalue(toidx)) /* function upvalue? */
  216. luaC_barrier(L, clCvalue(s2v(L->ci->func.p)), fr);
  217. /* LUA_REGISTRYINDEX does not need gc barrier
  218. (collector revisits it before finishing collection) */
  219. lua_unlock(L);
  220. }
  221. LUA_API void lua_pushvalue (lua_State *L, int idx) {
  222. lua_lock(L);
  223. setobj2s(L, L->top.p, index2value(L, idx));
  224. api_incr_top(L);
  225. lua_unlock(L);
  226. }
  227. /*
  228. ** access functions (stack -> C)
  229. */
  230. LUA_API int lua_type (lua_State *L, int idx) {
  231. const TValue *o = index2value(L, idx);
  232. return (isvalid(L, o) ? ttype(o) : LUA_TNONE);
  233. }
  234. LUA_API const char *lua_typename (lua_State *L, int t) {
  235. UNUSED(L);
  236. api_check(L, LUA_TNONE <= t && t < LUA_NUMTYPES, "invalid type");
  237. return ttypename(t);
  238. }
  239. LUA_API int lua_iscfunction (lua_State *L, int idx) {
  240. const TValue *o = index2value(L, idx);
  241. return (ttislcf(o) || (ttisCclosure(o)));
  242. }
  243. LUA_API int lua_isinteger (lua_State *L, int idx) {
  244. const TValue *o = index2value(L, idx);
  245. return ttisinteger(o);
  246. }
  247. LUA_API int lua_isnumber (lua_State *L, int idx) {
  248. lua_Number n;
  249. const TValue *o = index2value(L, idx);
  250. return tonumber(o, &n);
  251. }
  252. LUA_API int lua_isstring (lua_State *L, int idx) {
  253. const TValue *o = index2value(L, idx);
  254. return (ttisstring(o) || cvt2str(o));
  255. }
  256. LUA_API int lua_isuserdata (lua_State *L, int idx) {
  257. const TValue *o = index2value(L, idx);
  258. return (ttisfulluserdata(o) || ttislightuserdata(o));
  259. }
  260. LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
  261. const TValue *o1 = index2value(L, index1);
  262. const TValue *o2 = index2value(L, index2);
  263. return (isvalid(L, o1) && isvalid(L, o2)) ? luaV_rawequalobj(o1, o2) : 0;
  264. }
  265. LUA_API void lua_arith (lua_State *L, int op) {
  266. lua_lock(L);
  267. if (op != LUA_OPUNM && op != LUA_OPBNOT)
  268. api_checkpop(L, 2); /* all other operations expect two operands */
  269. else { /* for unary operations, add fake 2nd operand */
  270. api_checkpop(L, 1);
  271. setobjs2s(L, L->top.p, L->top.p - 1);
  272. api_incr_top(L);
  273. }
  274. /* first operand at top - 2, second at top - 1; result go to top - 2 */
  275. luaO_arith(L, op, s2v(L->top.p - 2), s2v(L->top.p - 1), L->top.p - 2);
  276. L->top.p--; /* pop second operand */
  277. lua_unlock(L);
  278. }
  279. LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
  280. const TValue *o1;
  281. const TValue *o2;
  282. int i = 0;
  283. lua_lock(L); /* may call tag method */
  284. o1 = index2value(L, index1);
  285. o2 = index2value(L, index2);
  286. if (isvalid(L, o1) && isvalid(L, o2)) {
  287. switch (op) {
  288. case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break;
  289. case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
  290. case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
  291. default: api_check(L, 0, "invalid option");
  292. }
  293. }
  294. lua_unlock(L);
  295. return i;
  296. }
  297. LUA_API unsigned (lua_numbertocstring) (lua_State *L, int idx, char *buff) {
  298. const TValue *o = index2value(L, idx);
  299. if (ttisnumber(o)) {
  300. unsigned len = luaO_tostringbuff(o, buff);
  301. buff[len++] = '\0'; /* add final zero */
  302. return len;
  303. }
  304. else
  305. return 0;
  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 cast(lua_Unsigned, tsvalue(o)->shrlen);
  357. case LUA_VLNGSTR: return cast(lua_Unsigned, tsvalue(o)->u.lnglen);
  358. case LUA_VUSERDATA: return cast(lua_Unsigned, 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. luaC_checkGC(L);
  439. lua_unlock(L);
  440. return getstr(ts);
  441. }
  442. LUA_API const char *lua_pushexternalstring (lua_State *L,
  443. const char *s, size_t len, lua_Alloc falloc, void *ud) {
  444. TString *ts;
  445. lua_lock(L);
  446. api_check(L, len <= MAX_SIZE, "string too large");
  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. luaC_checkGC(L);
  452. lua_unlock(L);
  453. return getstr(ts);
  454. }
  455. LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
  456. lua_lock(L);
  457. if (s == NULL)
  458. setnilvalue(s2v(L->top.p));
  459. else {
  460. TString *ts;
  461. ts = luaS_new(L, s);
  462. setsvalue2s(L, L->top.p, ts);
  463. s = getstr(ts); /* internal copy's address */
  464. }
  465. api_incr_top(L);
  466. luaC_checkGC(L);
  467. lua_unlock(L);
  468. return s;
  469. }
  470. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  471. va_list argp) {
  472. const char *ret;
  473. lua_lock(L);
  474. ret = luaO_pushvfstring(L, fmt, argp);
  475. luaC_checkGC(L);
  476. lua_unlock(L);
  477. return ret;
  478. }
  479. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  480. const char *ret;
  481. va_list argp;
  482. lua_lock(L);
  483. va_start(argp, fmt);
  484. ret = luaO_pushvfstring(L, fmt, argp);
  485. va_end(argp);
  486. luaC_checkGC(L);
  487. if (ret == NULL) /* error? */
  488. luaD_throw(L, LUA_ERRMEM);
  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 (mainthread(G(L)) == L);
  538. }
  539. /*
  540. ** get functions (Lua -> stack)
  541. */
  542. static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
  543. lu_byte tag;
  544. TString *str = luaS_new(L, k);
  545. luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, tag);
  546. if (!tagisempty(tag))
  547. api_incr_top(L);
  548. else {
  549. setsvalue2s(L, L->top.p, str);
  550. api_incr_top(L);
  551. tag = luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, tag);
  552. }
  553. lua_unlock(L);
  554. return novariant(tag);
  555. }
  556. /*
  557. ** The following function assumes that the registry cannot be a weak
  558. ** table, so that en mergency collection while using the global table
  559. ** cannot collect it.
  560. */
  561. static void getGlobalTable (lua_State *L, TValue *gt) {
  562. Table *registry = hvalue(&G(L)->l_registry);
  563. lu_byte tag = luaH_getint(registry, LUA_RIDX_GLOBALS, gt);
  564. (void)tag; /* avoid not-used warnings when checks are off */
  565. api_check(L, novariant(tag) == LUA_TTABLE, "global table must exist");
  566. }
  567. LUA_API int lua_getglobal (lua_State *L, const char *name) {
  568. TValue gt;
  569. lua_lock(L);
  570. getGlobalTable(L, &gt);
  571. return auxgetstr(L, &gt, name);
  572. }
  573. LUA_API int lua_gettable (lua_State *L, int idx) {
  574. lu_byte tag;
  575. TValue *t;
  576. lua_lock(L);
  577. api_checkpop(L, 1);
  578. t = index2value(L, idx);
  579. luaV_fastget(t, s2v(L->top.p - 1), s2v(L->top.p - 1), luaH_get, tag);
  580. if (tagisempty(tag))
  581. tag = luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, tag);
  582. lua_unlock(L);
  583. return novariant(tag);
  584. }
  585. LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
  586. lua_lock(L);
  587. return auxgetstr(L, index2value(L, idx), k);
  588. }
  589. LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
  590. TValue *t;
  591. lu_byte tag;
  592. lua_lock(L);
  593. t = index2value(L, idx);
  594. luaV_fastgeti(t, n, s2v(L->top.p), tag);
  595. if (tagisempty(tag)) {
  596. TValue key;
  597. setivalue(&key, n);
  598. tag = luaV_finishget(L, t, &key, L->top.p, tag);
  599. }
  600. api_incr_top(L);
  601. lua_unlock(L);
  602. return novariant(tag);
  603. }
  604. static int finishrawget (lua_State *L, lu_byte tag) {
  605. if (tagisempty(tag)) /* avoid copying empty items to the stack */
  606. setnilvalue(s2v(L->top.p));
  607. api_incr_top(L);
  608. lua_unlock(L);
  609. return novariant(tag);
  610. }
  611. l_sinline Table *gettable (lua_State *L, int idx) {
  612. TValue *t = index2value(L, idx);
  613. api_check(L, ttistable(t), "table expected");
  614. return hvalue(t);
  615. }
  616. LUA_API int lua_rawget (lua_State *L, int idx) {
  617. Table *t;
  618. lu_byte tag;
  619. lua_lock(L);
  620. api_checkpop(L, 1);
  621. t = gettable(L, idx);
  622. tag = luaH_get(t, s2v(L->top.p - 1), s2v(L->top.p - 1));
  623. L->top.p--; /* pop key */
  624. return finishrawget(L, tag);
  625. }
  626. LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
  627. Table *t;
  628. lu_byte tag;
  629. lua_lock(L);
  630. t = gettable(L, idx);
  631. luaH_fastgeti(t, n, s2v(L->top.p), tag);
  632. return finishrawget(L, tag);
  633. }
  634. LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
  635. Table *t;
  636. TValue k;
  637. lua_lock(L);
  638. t = gettable(L, idx);
  639. setpvalue(&k, cast_voidp(p));
  640. return finishrawget(L, luaH_get(t, &k, s2v(L->top.p)));
  641. }
  642. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  643. Table *t;
  644. lua_lock(L);
  645. t = luaH_new(L);
  646. sethvalue2s(L, L->top.p, t);
  647. api_incr_top(L);
  648. if (narray > 0 || nrec > 0)
  649. luaH_resize(L, t, cast_uint(narray), cast_uint(nrec));
  650. luaC_checkGC(L);
  651. lua_unlock(L);
  652. }
  653. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  654. const TValue *obj;
  655. Table *mt;
  656. int res = 0;
  657. lua_lock(L);
  658. obj = index2value(L, objindex);
  659. switch (ttype(obj)) {
  660. case LUA_TTABLE:
  661. mt = hvalue(obj)->metatable;
  662. break;
  663. case LUA_TUSERDATA:
  664. mt = uvalue(obj)->metatable;
  665. break;
  666. default:
  667. mt = G(L)->mt[ttype(obj)];
  668. break;
  669. }
  670. if (mt != NULL) {
  671. sethvalue2s(L, L->top.p, mt);
  672. api_incr_top(L);
  673. res = 1;
  674. }
  675. lua_unlock(L);
  676. return res;
  677. }
  678. LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) {
  679. TValue *o;
  680. int t;
  681. lua_lock(L);
  682. o = index2value(L, idx);
  683. api_check(L, ttisfulluserdata(o), "full userdata expected");
  684. if (n <= 0 || n > uvalue(o)->nuvalue) {
  685. setnilvalue(s2v(L->top.p));
  686. t = LUA_TNONE;
  687. }
  688. else {
  689. setobj2s(L, L->top.p, &uvalue(o)->uv[n - 1].uv);
  690. t = ttype(s2v(L->top.p));
  691. }
  692. api_incr_top(L);
  693. lua_unlock(L);
  694. return t;
  695. }
  696. /*
  697. ** set functions (stack -> Lua)
  698. */
  699. /*
  700. ** t[k] = value at the top of the stack (where 'k' is a string)
  701. */
  702. static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
  703. int hres;
  704. TString *str = luaS_new(L, k);
  705. api_checkpop(L, 1);
  706. luaV_fastset(t, str, s2v(L->top.p - 1), hres, luaH_psetstr);
  707. if (hres == HOK) {
  708. luaV_finishfastset(L, t, s2v(L->top.p - 1));
  709. L->top.p--; /* pop value */
  710. }
  711. else {
  712. setsvalue2s(L, L->top.p, str); /* push 'str' (to make it a TValue) */
  713. api_incr_top(L);
  714. luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), hres);
  715. L->top.p -= 2; /* pop value and key */
  716. }
  717. lua_unlock(L); /* lock done by caller */
  718. }
  719. LUA_API void lua_setglobal (lua_State *L, const char *name) {
  720. TValue gt;
  721. lua_lock(L); /* unlock done in 'auxsetstr' */
  722. getGlobalTable(L, &gt);
  723. auxsetstr(L, &gt, name);
  724. }
  725. LUA_API void lua_settable (lua_State *L, int idx) {
  726. TValue *t;
  727. int hres;
  728. lua_lock(L);
  729. api_checkpop(L, 2);
  730. t = index2value(L, idx);
  731. luaV_fastset(t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres, luaH_pset);
  732. if (hres == HOK)
  733. luaV_finishfastset(L, t, s2v(L->top.p - 1));
  734. else
  735. luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres);
  736. L->top.p -= 2; /* pop index and value */
  737. lua_unlock(L);
  738. }
  739. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  740. lua_lock(L); /* unlock done in 'auxsetstr' */
  741. auxsetstr(L, index2value(L, idx), k);
  742. }
  743. LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
  744. TValue *t;
  745. int hres;
  746. lua_lock(L);
  747. api_checkpop(L, 1);
  748. t = index2value(L, idx);
  749. luaV_fastseti(t, n, s2v(L->top.p - 1), hres);
  750. if (hres == HOK)
  751. luaV_finishfastset(L, t, s2v(L->top.p - 1));
  752. else {
  753. TValue temp;
  754. setivalue(&temp, n);
  755. luaV_finishset(L, t, &temp, s2v(L->top.p - 1), hres);
  756. }
  757. L->top.p--; /* pop value */
  758. lua_unlock(L);
  759. }
  760. static void aux_rawset (lua_State *L, int idx, TValue *key, int n) {
  761. Table *t;
  762. lua_lock(L);
  763. api_checkpop(L, n);
  764. t = gettable(L, idx);
  765. luaH_set(L, t, key, s2v(L->top.p - 1));
  766. invalidateTMcache(t);
  767. luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
  768. L->top.p -= n;
  769. lua_unlock(L);
  770. }
  771. LUA_API void lua_rawset (lua_State *L, int idx) {
  772. aux_rawset(L, idx, s2v(L->top.p - 2), 2);
  773. }
  774. LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
  775. TValue k;
  776. setpvalue(&k, cast_voidp(p));
  777. aux_rawset(L, idx, &k, 1);
  778. }
  779. LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
  780. Table *t;
  781. lua_lock(L);
  782. api_checkpop(L, 1);
  783. t = gettable(L, idx);
  784. luaH_setint(L, t, n, s2v(L->top.p - 1));
  785. luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
  786. L->top.p--;
  787. lua_unlock(L);
  788. }
  789. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  790. TValue *obj;
  791. Table *mt;
  792. lua_lock(L);
  793. api_checkpop(L, 1);
  794. obj = index2value(L, objindex);
  795. if (ttisnil(s2v(L->top.p - 1)))
  796. mt = NULL;
  797. else {
  798. api_check(L, ttistable(s2v(L->top.p - 1)), "table expected");
  799. mt = hvalue(s2v(L->top.p - 1));
  800. }
  801. switch (ttype(obj)) {
  802. case LUA_TTABLE: {
  803. hvalue(obj)->metatable = mt;
  804. if (mt) {
  805. luaC_objbarrier(L, gcvalue(obj), mt);
  806. luaC_checkfinalizer(L, gcvalue(obj), mt);
  807. }
  808. break;
  809. }
  810. case LUA_TUSERDATA: {
  811. uvalue(obj)->metatable = mt;
  812. if (mt) {
  813. luaC_objbarrier(L, uvalue(obj), mt);
  814. luaC_checkfinalizer(L, gcvalue(obj), mt);
  815. }
  816. break;
  817. }
  818. default: {
  819. G(L)->mt[ttype(obj)] = mt;
  820. break;
  821. }
  822. }
  823. L->top.p--;
  824. lua_unlock(L);
  825. return 1;
  826. }
  827. LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) {
  828. TValue *o;
  829. int res;
  830. lua_lock(L);
  831. api_checkpop(L, 1);
  832. o = index2value(L, idx);
  833. api_check(L, ttisfulluserdata(o), "full userdata expected");
  834. if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue)))
  835. res = 0; /* 'n' not in [1, uvalue(o)->nuvalue] */
  836. else {
  837. setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top.p - 1));
  838. luaC_barrierback(L, gcvalue(o), s2v(L->top.p - 1));
  839. res = 1;
  840. }
  841. L->top.p--;
  842. lua_unlock(L);
  843. return res;
  844. }
  845. /*
  846. ** 'load' and 'call' functions (run Lua code)
  847. */
  848. #define checkresults(L,na,nr) \
  849. (api_check(L, (nr) == LUA_MULTRET \
  850. || (L->ci->top.p - L->top.p >= (nr) - (na)), \
  851. "results from function overflow current stack size"), \
  852. api_check(L, LUA_MULTRET <= (nr) && (nr) <= MAXRESULTS, \
  853. "invalid number of results"))
  854. LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
  855. lua_KContext ctx, lua_KFunction k) {
  856. StkId func;
  857. lua_lock(L);
  858. api_check(L, k == NULL || !isLua(L->ci),
  859. "cannot use continuations inside hooks");
  860. api_checkpop(L, nargs + 1);
  861. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  862. checkresults(L, nargs, nresults);
  863. func = L->top.p - (nargs+1);
  864. if (k != NULL && yieldable(L)) { /* need to prepare continuation? */
  865. L->ci->u.c.k = k; /* save continuation */
  866. L->ci->u.c.ctx = ctx; /* save context */
  867. luaD_call(L, func, nresults); /* do the call */
  868. }
  869. else /* no continuation or no yieldable */
  870. luaD_callnoyield(L, func, nresults); /* just do the call */
  871. adjustresults(L, nresults);
  872. lua_unlock(L);
  873. }
  874. /*
  875. ** Execute a protected call.
  876. */
  877. struct CallS { /* data to 'f_call' */
  878. StkId func;
  879. int nresults;
  880. };
  881. static void f_call (lua_State *L, void *ud) {
  882. struct CallS *c = cast(struct CallS *, ud);
  883. luaD_callnoyield(L, c->func, c->nresults);
  884. }
  885. LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
  886. lua_KContext ctx, lua_KFunction k) {
  887. struct CallS c;
  888. TStatus status;
  889. ptrdiff_t func;
  890. lua_lock(L);
  891. api_check(L, k == NULL || !isLua(L->ci),
  892. "cannot use continuations inside hooks");
  893. api_checkpop(L, nargs + 1);
  894. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  895. checkresults(L, nargs, nresults);
  896. if (errfunc == 0)
  897. func = 0;
  898. else {
  899. StkId o = index2stack(L, errfunc);
  900. api_check(L, ttisfunction(s2v(o)), "error handler must be a function");
  901. func = savestack(L, o);
  902. }
  903. c.func = L->top.p - (nargs+1); /* function to be called */
  904. if (k == NULL || !yieldable(L)) { /* no continuation or no yieldable? */
  905. c.nresults = nresults; /* do a 'conventional' protected call */
  906. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  907. }
  908. else { /* prepare continuation (call is already protected by 'resume') */
  909. CallInfo *ci = L->ci;
  910. ci->u.c.k = k; /* save continuation */
  911. ci->u.c.ctx = ctx; /* save context */
  912. /* save information for error recovery */
  913. ci->u2.funcidx = cast_int(savestack(L, c.func));
  914. ci->u.c.old_errfunc = L->errfunc;
  915. L->errfunc = func;
  916. setoah(ci, L->allowhook); /* save value of 'allowhook' */
  917. ci->callstatus |= CIST_YPCALL; /* function can do error recovery */
  918. luaD_call(L, c.func, nresults); /* do the call */
  919. ci->callstatus &= ~CIST_YPCALL;
  920. L->errfunc = ci->u.c.old_errfunc;
  921. status = LUA_OK; /* if it is here, there were no errors */
  922. }
  923. adjustresults(L, nresults);
  924. lua_unlock(L);
  925. return APIstatus(status);
  926. }
  927. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  928. const char *chunkname, const char *mode) {
  929. ZIO z;
  930. TStatus status;
  931. lua_lock(L);
  932. if (!chunkname) chunkname = "?";
  933. luaZ_init(L, &z, reader, data);
  934. status = luaD_protectedparser(L, &z, chunkname, mode);
  935. if (status == LUA_OK) { /* no errors? */
  936. LClosure *f = clLvalue(s2v(L->top.p - 1)); /* get new function */
  937. if (f->nupvalues >= 1) { /* does it have an upvalue? */
  938. /* get global table from registry */
  939. TValue gt;
  940. getGlobalTable(L, &gt);
  941. /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
  942. setobj(L, f->upvals[0]->v.p, &gt);
  943. luaC_barrier(L, f->upvals[0], &gt);
  944. }
  945. }
  946. lua_unlock(L);
  947. return APIstatus(status);
  948. }
  949. /*
  950. ** Dump a Lua function, calling 'writer' to write its parts. Ensure
  951. ** the stack returns with its original size.
  952. */
  953. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
  954. int status;
  955. ptrdiff_t otop = savestack(L, L->top.p); /* original top */
  956. TValue *f = s2v(L->top.p - 1); /* function to be dumped */
  957. lua_lock(L);
  958. api_checkpop(L, 1);
  959. api_check(L, isLfunction(f), "Lua function expected");
  960. status = luaU_dump(L, clLvalue(f)->p, writer, data, strip);
  961. L->top.p = restorestack(L, otop); /* restore top */
  962. lua_unlock(L);
  963. return status;
  964. }
  965. LUA_API int lua_status (lua_State *L) {
  966. return APIstatus(L->status);
  967. }
  968. /*
  969. ** Garbage-collection function
  970. */
  971. LUA_API int lua_gc (lua_State *L, int what, ...) {
  972. va_list argp;
  973. int res = 0;
  974. global_State *g = G(L);
  975. if (g->gcstp & (GCSTPGC | GCSTPCLS)) /* internal stop? */
  976. return -1; /* all options are invalid when stopped */
  977. lua_lock(L);
  978. va_start(argp, what);
  979. switch (what) {
  980. case LUA_GCSTOP: {
  981. g->gcstp = GCSTPUSR; /* stopped by the user */
  982. break;
  983. }
  984. case LUA_GCRESTART: {
  985. luaE_setdebt(g, 0);
  986. g->gcstp = 0; /* (other bits must be zero here) */
  987. break;
  988. }
  989. case LUA_GCCOLLECT: {
  990. luaC_fullgc(L, 0);
  991. break;
  992. }
  993. case LUA_GCCOUNT: {
  994. /* GC values are expressed in Kbytes: #bytes/2^10 */
  995. res = cast_int(gettotalbytes(g) >> 10);
  996. break;
  997. }
  998. case LUA_GCCOUNTB: {
  999. res = cast_int(gettotalbytes(g) & 0x3ff);
  1000. break;
  1001. }
  1002. case LUA_GCSTEP: {
  1003. lu_byte oldstp = g->gcstp;
  1004. l_mem n = cast(l_mem, va_arg(argp, size_t));
  1005. int work = 0; /* true if GC did some work */
  1006. g->gcstp = 0; /* allow GC to run (other bits must be zero here) */
  1007. if (n <= 0)
  1008. n = g->GCdebt; /* force to run one basic step */
  1009. luaE_setdebt(g, g->GCdebt - n);
  1010. luaC_condGC(L, (void)0, work = 1);
  1011. if (work && g->gcstate == GCSpause) /* end of cycle? */
  1012. res = 1; /* signal it */
  1013. g->gcstp = oldstp; /* restore previous state */
  1014. break;
  1015. }
  1016. case LUA_GCISRUNNING: {
  1017. res = gcrunning(g);
  1018. break;
  1019. }
  1020. case LUA_GCGEN: {
  1021. res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN;
  1022. luaC_changemode(L, KGC_GENMINOR);
  1023. break;
  1024. }
  1025. case LUA_GCINC: {
  1026. res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN;
  1027. luaC_changemode(L, KGC_INC);
  1028. break;
  1029. }
  1030. case LUA_GCPARAM: {
  1031. int param = va_arg(argp, int);
  1032. int value = va_arg(argp, int);
  1033. api_check(L, 0 <= param && param < LUA_GCPN, "invalid parameter");
  1034. res = cast_int(luaO_applyparam(g->gcparams[param], 100));
  1035. if (value >= 0)
  1036. g->gcparams[param] = luaO_codeparam(cast_uint(value));
  1037. break;
  1038. }
  1039. default: res = -1; /* invalid option */
  1040. }
  1041. va_end(argp);
  1042. lua_unlock(L);
  1043. return res;
  1044. }
  1045. /*
  1046. ** miscellaneous functions
  1047. */
  1048. LUA_API int lua_error (lua_State *L) {
  1049. TValue *errobj;
  1050. lua_lock(L);
  1051. errobj = s2v(L->top.p - 1);
  1052. api_checkpop(L, 1);
  1053. /* error object is the memory error message? */
  1054. if (ttisshrstring(errobj) && eqshrstr(tsvalue(errobj), G(L)->memerrmsg))
  1055. luaM_error(L); /* raise a memory error */
  1056. else
  1057. luaG_errormsg(L); /* raise a regular error */
  1058. /* code unreachable; will unlock when control actually leaves the kernel */
  1059. return 0; /* to avoid warnings */
  1060. }
  1061. LUA_API int lua_next (lua_State *L, int idx) {
  1062. Table *t;
  1063. int more;
  1064. lua_lock(L);
  1065. api_checkpop(L, 1);
  1066. t = gettable(L, idx);
  1067. more = luaH_next(L, t, L->top.p - 1);
  1068. if (more)
  1069. api_incr_top(L);
  1070. else /* no more elements */
  1071. L->top.p--; /* pop key */
  1072. lua_unlock(L);
  1073. return more;
  1074. }
  1075. LUA_API void lua_toclose (lua_State *L, int idx) {
  1076. StkId o;
  1077. lua_lock(L);
  1078. o = index2stack(L, idx);
  1079. api_check(L, L->tbclist.p < o, "given index below or equal a marked one");
  1080. luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */
  1081. L->ci->callstatus |= CIST_TBC; /* mark that function has TBC slots */
  1082. lua_unlock(L);
  1083. }
  1084. LUA_API void lua_concat (lua_State *L, int n) {
  1085. lua_lock(L);
  1086. api_checknelems(L, n);
  1087. if (n > 0) {
  1088. luaV_concat(L, n);
  1089. luaC_checkGC(L);
  1090. }
  1091. else { /* nothing to concatenate */
  1092. setsvalue2s(L, L->top.p, luaS_newlstr(L, "", 0)); /* push empty string */
  1093. api_incr_top(L);
  1094. }
  1095. lua_unlock(L);
  1096. }
  1097. LUA_API void lua_len (lua_State *L, int idx) {
  1098. TValue *t;
  1099. lua_lock(L);
  1100. t = index2value(L, idx);
  1101. luaV_objlen(L, L->top.p, t);
  1102. api_incr_top(L);
  1103. lua_unlock(L);
  1104. }
  1105. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  1106. lua_Alloc f;
  1107. lua_lock(L);
  1108. if (ud) *ud = G(L)->ud;
  1109. f = G(L)->frealloc;
  1110. lua_unlock(L);
  1111. return f;
  1112. }
  1113. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  1114. lua_lock(L);
  1115. G(L)->ud = ud;
  1116. G(L)->frealloc = f;
  1117. lua_unlock(L);
  1118. }
  1119. void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud) {
  1120. lua_lock(L);
  1121. G(L)->ud_warn = ud;
  1122. G(L)->warnf = f;
  1123. lua_unlock(L);
  1124. }
  1125. void lua_warning (lua_State *L, const char *msg, int tocont) {
  1126. lua_lock(L);
  1127. luaE_warning(L, msg, tocont);
  1128. lua_unlock(L);
  1129. }
  1130. LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) {
  1131. Udata *u;
  1132. lua_lock(L);
  1133. api_check(L, 0 <= nuvalue && nuvalue < SHRT_MAX, "invalid value");
  1134. u = luaS_newudata(L, size, cast(unsigned short, nuvalue));
  1135. setuvalue(L, s2v(L->top.p), u);
  1136. api_incr_top(L);
  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. }