lapi.c 35 KB

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