lapi.c 35 KB

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