lapi.c 35 KB

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