lapi.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510
  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. const TValue *slot;
  519. TString *str = luaS_new(L, k);
  520. if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
  521. setobj2s(L, L->top.p, slot);
  522. api_incr_top(L);
  523. }
  524. else {
  525. setsvalue2s(L, L->top.p, str);
  526. api_incr_top(L);
  527. luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot);
  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. const TValue *slot;
  548. TValue *t;
  549. lua_lock(L);
  550. t = index2value(L, idx);
  551. if (luaV_fastget(L, t, s2v(L->top.p - 1), slot, luaH_get)) {
  552. setobj2s(L, L->top.p - 1, slot);
  553. }
  554. else
  555. luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot);
  556. lua_unlock(L);
  557. return ttype(s2v(L->top.p - 1));
  558. }
  559. LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
  560. lua_lock(L);
  561. return auxgetstr(L, index2value(L, idx), k);
  562. }
  563. LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
  564. TValue *t;
  565. const TValue *slot;
  566. lua_lock(L);
  567. t = index2value(L, idx);
  568. if (luaV_fastgeti(L, t, n, slot)) {
  569. setobj2s(L, L->top.p, slot);
  570. }
  571. else {
  572. TValue aux;
  573. setivalue(&aux, n);
  574. luaV_finishget(L, t, &aux, L->top.p, slot);
  575. }
  576. api_incr_top(L);
  577. lua_unlock(L);
  578. return ttype(s2v(L->top.p - 1));
  579. }
  580. l_sinline int finishrawget (lua_State *L, const TValue *val) {
  581. if (isempty(val)) /* avoid copying empty items to the stack */
  582. setnilvalue(s2v(L->top.p));
  583. else
  584. setobj2s(L, L->top.p, val);
  585. api_incr_top(L);
  586. lua_unlock(L);
  587. return ttype(s2v(L->top.p - 1));
  588. }
  589. static Table *gettable (lua_State *L, int idx) {
  590. TValue *t = index2value(L, idx);
  591. api_check(L, ttistable(t), "table expected");
  592. return hvalue(t);
  593. }
  594. LUA_API int lua_rawget (lua_State *L, int idx) {
  595. Table *t;
  596. const TValue *val;
  597. lua_lock(L);
  598. api_checknelems(L, 1);
  599. t = gettable(L, idx);
  600. val = luaH_get(t, s2v(L->top.p - 1));
  601. L->top.p--; /* remove key */
  602. return finishrawget(L, val);
  603. }
  604. LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
  605. Table *t;
  606. lua_lock(L);
  607. t = gettable(L, idx);
  608. return finishrawget(L, luaH_getint(t, n));
  609. }
  610. LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
  611. Table *t;
  612. TValue k;
  613. lua_lock(L);
  614. t = gettable(L, idx);
  615. setpvalue(&k, cast_voidp(p));
  616. return finishrawget(L, luaH_get(t, &k));
  617. }
  618. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  619. Table *t;
  620. lua_lock(L);
  621. t = luaH_new(L);
  622. sethvalue2s(L, L->top.p, t);
  623. api_incr_top(L);
  624. if (narray > 0 || nrec > 0)
  625. luaH_resize(L, t, narray, nrec);
  626. luaC_checkGC(L);
  627. lua_unlock(L);
  628. }
  629. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  630. const TValue *obj;
  631. Table *mt;
  632. int res = 0;
  633. lua_lock(L);
  634. obj = index2value(L, objindex);
  635. switch (ttype(obj)) {
  636. case LUA_TTABLE:
  637. mt = hvalue(obj)->metatable;
  638. break;
  639. case LUA_TUSERDATA:
  640. mt = uvalue(obj)->metatable;
  641. break;
  642. default:
  643. mt = G(L)->mt[ttype(obj)];
  644. break;
  645. }
  646. if (mt != NULL) {
  647. sethvalue2s(L, L->top.p, mt);
  648. api_incr_top(L);
  649. res = 1;
  650. }
  651. lua_unlock(L);
  652. return res;
  653. }
  654. LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) {
  655. TValue *o;
  656. int t;
  657. lua_lock(L);
  658. o = index2value(L, idx);
  659. api_check(L, ttisfulluserdata(o), "full userdata expected");
  660. if (n <= 0 || n > uvalue(o)->nuvalue) {
  661. setnilvalue(s2v(L->top.p));
  662. t = LUA_TNONE;
  663. }
  664. else {
  665. setobj2s(L, L->top.p, &uvalue(o)->uv[n - 1].uv);
  666. t = ttype(s2v(L->top.p));
  667. }
  668. api_incr_top(L);
  669. lua_unlock(L);
  670. return t;
  671. }
  672. /*
  673. ** set functions (stack -> Lua)
  674. */
  675. /*
  676. ** t[k] = value at the top of the stack (where 'k' is a string)
  677. */
  678. static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
  679. const TValue *slot;
  680. TString *str = luaS_new(L, k);
  681. api_checknelems(L, 1);
  682. if (luaV_fastset(L, t, str, slot, luaH_getstr)) {
  683. luaV_finishfastset(L, t, slot, s2v(L->top.p - 1));
  684. L->top.p--; /* pop value */
  685. }
  686. else {
  687. setsvalue2s(L, L->top.p, str); /* push 'str' (to make it a TValue) */
  688. api_incr_top(L);
  689. luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), slot);
  690. L->top.p -= 2; /* pop value and key */
  691. }
  692. lua_unlock(L); /* lock done by caller */
  693. }
  694. LUA_API void lua_setglobal (lua_State *L, const char *name) {
  695. const TValue *G;
  696. lua_lock(L); /* unlock done in 'auxsetstr' */
  697. G = getGtable(L);
  698. auxsetstr(L, G, name);
  699. }
  700. LUA_API void lua_settable (lua_State *L, int idx) {
  701. TValue *t;
  702. const TValue *slot;
  703. lua_lock(L);
  704. api_checknelems(L, 2);
  705. t = index2value(L, idx);
  706. if (luaV_fastset(L, t, s2v(L->top.p - 2), slot, luaH_get)) {
  707. luaV_finishfastset(L, t, slot, s2v(L->top.p - 1));
  708. }
  709. else
  710. luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), slot);
  711. L->top.p -= 2; /* pop index and value */
  712. lua_unlock(L);
  713. }
  714. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  715. lua_lock(L); /* unlock done in 'auxsetstr' */
  716. auxsetstr(L, index2value(L, idx), k);
  717. }
  718. LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
  719. TValue *t;
  720. const TValue *slot;
  721. lua_lock(L);
  722. api_checknelems(L, 1);
  723. t = index2value(L, idx);
  724. if (luaV_fastseti(L, t, n, slot)) {
  725. luaV_finishfastset(L, t, slot, s2v(L->top.p - 1));
  726. }
  727. else {
  728. TValue aux;
  729. setivalue(&aux, n);
  730. luaV_finishset(L, t, &aux, s2v(L->top.p - 1), slot);
  731. }
  732. L->top.p--; /* pop value */
  733. lua_unlock(L);
  734. }
  735. static void aux_rawset (lua_State *L, int idx, TValue *key, int n) {
  736. Table *t;
  737. lua_lock(L);
  738. api_checknelems(L, n);
  739. t = gettable(L, idx);
  740. luaH_set(L, t, key, s2v(L->top.p - 1));
  741. invalidateTMcache(t);
  742. luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
  743. L->top.p -= n;
  744. lua_unlock(L);
  745. }
  746. LUA_API void lua_rawset (lua_State *L, int idx) {
  747. aux_rawset(L, idx, s2v(L->top.p - 2), 2);
  748. }
  749. LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
  750. TValue k;
  751. setpvalue(&k, cast_voidp(p));
  752. aux_rawset(L, idx, &k, 1);
  753. }
  754. LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
  755. Table *t;
  756. lua_lock(L);
  757. api_checknelems(L, 1);
  758. t = gettable(L, idx);
  759. luaH_setint(L, t, n, s2v(L->top.p - 1));
  760. luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
  761. L->top.p--;
  762. lua_unlock(L);
  763. }
  764. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  765. TValue *obj;
  766. Table *mt;
  767. lua_lock(L);
  768. api_checknelems(L, 1);
  769. obj = index2value(L, objindex);
  770. if (ttisnil(s2v(L->top.p - 1)))
  771. mt = NULL;
  772. else {
  773. api_check(L, ttistable(s2v(L->top.p - 1)), "table expected");
  774. mt = hvalue(s2v(L->top.p - 1));
  775. }
  776. switch (ttype(obj)) {
  777. case LUA_TTABLE: {
  778. if (l_unlikely(isshared(hvalue(obj))))
  779. luaG_runerror(L, "can't setmetatable to shared table");
  780. hvalue(obj)->metatable = mt;
  781. if (mt) {
  782. luaC_objbarrier(L, gcvalue(obj), mt);
  783. luaC_checkfinalizer(L, gcvalue(obj), mt);
  784. }
  785. break;
  786. }
  787. case LUA_TUSERDATA: {
  788. uvalue(obj)->metatable = mt;
  789. if (mt) {
  790. luaC_objbarrier(L, uvalue(obj), mt);
  791. luaC_checkfinalizer(L, gcvalue(obj), mt);
  792. }
  793. break;
  794. }
  795. default: {
  796. G(L)->mt[ttype(obj)] = mt;
  797. break;
  798. }
  799. }
  800. L->top.p--;
  801. lua_unlock(L);
  802. return 1;
  803. }
  804. LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) {
  805. TValue *o;
  806. int res;
  807. lua_lock(L);
  808. api_checknelems(L, 1);
  809. o = index2value(L, idx);
  810. api_check(L, ttisfulluserdata(o), "full userdata expected");
  811. if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue)))
  812. res = 0; /* 'n' not in [1, uvalue(o)->nuvalue] */
  813. else {
  814. setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top.p - 1));
  815. luaC_barrierback(L, gcvalue(o), s2v(L->top.p - 1));
  816. res = 1;
  817. }
  818. L->top.p--;
  819. lua_unlock(L);
  820. return res;
  821. }
  822. /*
  823. ** 'load' and 'call' functions (run Lua code)
  824. */
  825. #define checkresults(L,na,nr) \
  826. api_check(L, (nr) == LUA_MULTRET \
  827. || (L->ci->top.p - L->top.p >= (nr) - (na)), \
  828. "results from function overflow current stack size")
  829. LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
  830. lua_KContext ctx, lua_KFunction k) {
  831. StkId func;
  832. lua_lock(L);
  833. api_check(L, k == NULL || !isLua(L->ci),
  834. "cannot use continuations inside hooks");
  835. api_checknelems(L, nargs+1);
  836. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  837. checkresults(L, nargs, nresults);
  838. func = L->top.p - (nargs+1);
  839. if (k != NULL && yieldable(L)) { /* need to prepare continuation? */
  840. L->ci->u.c.k = k; /* save continuation */
  841. L->ci->u.c.ctx = ctx; /* save context */
  842. luaD_call(L, func, nresults); /* do the call */
  843. }
  844. else /* no continuation or no yieldable */
  845. luaD_callnoyield(L, func, nresults); /* just do the call */
  846. adjustresults(L, nresults);
  847. lua_unlock(L);
  848. }
  849. /*
  850. ** Execute a protected call.
  851. */
  852. struct CallS { /* data to 'f_call' */
  853. StkId func;
  854. int nresults;
  855. };
  856. static void f_call (lua_State *L, void *ud) {
  857. struct CallS *c = cast(struct CallS *, ud);
  858. luaD_callnoyield(L, c->func, c->nresults);
  859. }
  860. LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
  861. lua_KContext ctx, lua_KFunction k) {
  862. struct CallS c;
  863. int status;
  864. ptrdiff_t func;
  865. lua_lock(L);
  866. api_check(L, k == NULL || !isLua(L->ci),
  867. "cannot use continuations inside hooks");
  868. api_checknelems(L, nargs+1);
  869. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  870. checkresults(L, nargs, nresults);
  871. if (errfunc == 0)
  872. func = 0;
  873. else {
  874. StkId o = index2stack(L, errfunc);
  875. api_check(L, ttisfunction(s2v(o)), "error handler must be a function");
  876. func = savestack(L, o);
  877. }
  878. c.func = L->top.p - (nargs+1); /* function to be called */
  879. if (k == NULL || !yieldable(L)) { /* no continuation or no yieldable? */
  880. c.nresults = nresults; /* do a 'conventional' protected call */
  881. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  882. }
  883. else { /* prepare continuation (call is already protected by 'resume') */
  884. CallInfo *ci = L->ci;
  885. ci->u.c.k = k; /* save continuation */
  886. ci->u.c.ctx = ctx; /* save context */
  887. /* save information for error recovery */
  888. ci->u2.funcidx = cast_int(savestack(L, c.func));
  889. ci->u.c.old_errfunc = L->errfunc;
  890. L->errfunc = func;
  891. setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */
  892. ci->callstatus |= CIST_YPCALL; /* function can do error recovery */
  893. luaD_call(L, c.func, nresults); /* do the call */
  894. ci->callstatus &= ~CIST_YPCALL;
  895. L->errfunc = ci->u.c.old_errfunc;
  896. status = LUA_OK; /* if it is here, there were no errors */
  897. }
  898. adjustresults(L, nresults);
  899. lua_unlock(L);
  900. return status;
  901. }
  902. static void set_env (lua_State *L, LClosure *f) {
  903. if (f->nupvalues >= 1) { /* does it have an upvalue? */
  904. /* get global table from registry */
  905. const TValue *gt = getGtable(L);
  906. /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
  907. setobj(L, f->upvals[0]->v.p, gt);
  908. luaC_barrier(L, f->upvals[0], gt);
  909. }
  910. }
  911. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  912. const char *chunkname, const char *mode) {
  913. ZIO z;
  914. int status;
  915. lua_lock(L);
  916. if (!chunkname) chunkname = "?";
  917. luaZ_init(L, &z, reader, data);
  918. status = luaD_protectedparser(L, &z, chunkname, mode);
  919. if (status == LUA_OK) { /* no errors? */
  920. LClosure *f = clLvalue(s2v(L->top.p - 1)); /* get new function */
  921. set_env(L,f);
  922. }
  923. lua_unlock(L);
  924. return status;
  925. }
  926. LUA_API void lua_clonefunction (lua_State *L, const void * fp) {
  927. LClosure *cl;
  928. LClosure *f = cast(LClosure *, fp);
  929. api_check(L, isshared(f->p), "Not a shared proto");
  930. lua_lock(L);
  931. cl = luaF_newLclosure(L,f->nupvalues);
  932. setclLvalue2s(L,L->top.p,cl);
  933. api_incr_top(L);
  934. cl->p = f->p;
  935. luaF_initupvals(L, cl);
  936. set_env(L,cl);
  937. lua_unlock(L);
  938. }
  939. LUA_API void lua_sharefunction (lua_State *L, int index) {
  940. LClosure *f;
  941. if (!lua_isfunction(L,index) || lua_iscfunction(L,index))
  942. luaG_runerror(L, "Only Lua function can share");
  943. f = cast(LClosure *, lua_topointer(L, index));
  944. luaF_shareproto(f->p);
  945. }
  946. LUA_API void lua_sharestring (lua_State *L, int index) {
  947. TString *ts;
  948. if (lua_type(L,index) != LUA_TSTRING)
  949. luaG_runerror(L, "need a string to share");
  950. ts = tsvalue(index2value(L,index));
  951. luaS_share(ts);
  952. }
  953. LUA_API void lua_clonetable(lua_State *L, const void * tp) {
  954. Table *t = cast(Table *, tp);
  955. if (l_unlikely(!isshared(t)))
  956. luaG_runerror(L, "Not a shared table");
  957. lua_lock(L);
  958. sethvalue2s(L, L->top.p, t);
  959. api_incr_top(L);
  960. lua_unlock(L);
  961. }
  962. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
  963. int status;
  964. TValue *o;
  965. lua_lock(L);
  966. api_checknelems(L, 1);
  967. o = s2v(L->top.p - 1);
  968. if (isLfunction(o))
  969. status = luaU_dump(L, getproto(o), writer, data, strip);
  970. else
  971. status = 1;
  972. lua_unlock(L);
  973. return status;
  974. }
  975. LUA_API int lua_status (lua_State *L) {
  976. return L->status;
  977. }
  978. /*
  979. ** Garbage-collection function
  980. */
  981. LUA_API int lua_gc (lua_State *L, int what, ...) {
  982. va_list argp;
  983. int res = 0;
  984. global_State *g = G(L);
  985. if (g->gcstp & GCSTPGC) /* internal stop? */
  986. return -1; /* all options are invalid when stopped */
  987. lua_lock(L);
  988. va_start(argp, what);
  989. switch (what) {
  990. case LUA_GCSTOP: {
  991. g->gcstp = GCSTPUSR; /* stopped by the user */
  992. break;
  993. }
  994. case LUA_GCRESTART: {
  995. luaE_setdebt(g, 0);
  996. g->gcstp = 0; /* (GCSTPGC must be already zero here) */
  997. break;
  998. }
  999. case LUA_GCCOLLECT: {
  1000. luaC_fullgc(L, 0);
  1001. break;
  1002. }
  1003. case LUA_GCCOUNT: {
  1004. /* GC values are expressed in Kbytes: #bytes/2^10 */
  1005. res = cast_int(gettotalbytes(g) >> 10);
  1006. break;
  1007. }
  1008. case LUA_GCCOUNTB: {
  1009. res = cast_int(gettotalbytes(g) & 0x3ff);
  1010. break;
  1011. }
  1012. case LUA_GCSTEP: {
  1013. int data = va_arg(argp, int);
  1014. l_mem debt = 1; /* =1 to signal that it did an actual step */
  1015. lu_byte oldstp = g->gcstp;
  1016. g->gcstp = 0; /* allow GC to run (GCSTPGC must be zero here) */
  1017. if (data == 0) {
  1018. luaE_setdebt(g, 0); /* do a basic step */
  1019. luaC_step(L);
  1020. }
  1021. else { /* add 'data' to total debt */
  1022. debt = cast(l_mem, data) * 1024 + g->GCdebt;
  1023. luaE_setdebt(g, debt);
  1024. luaC_checkGC(L);
  1025. }
  1026. g->gcstp = oldstp; /* restore previous state */
  1027. if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */
  1028. res = 1; /* signal it */
  1029. break;
  1030. }
  1031. case LUA_GCSETPAUSE: {
  1032. int data = va_arg(argp, int);
  1033. res = getgcparam(g->gcpause);
  1034. setgcparam(g->gcpause, data);
  1035. break;
  1036. }
  1037. case LUA_GCSETSTEPMUL: {
  1038. int data = va_arg(argp, int);
  1039. res = getgcparam(g->gcstepmul);
  1040. setgcparam(g->gcstepmul, data);
  1041. break;
  1042. }
  1043. case LUA_GCISRUNNING: {
  1044. res = gcrunning(g);
  1045. break;
  1046. }
  1047. case LUA_GCGEN: {
  1048. int minormul = va_arg(argp, int);
  1049. int majormul = va_arg(argp, int);
  1050. res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC;
  1051. if (minormul != 0)
  1052. g->genminormul = minormul;
  1053. if (majormul != 0)
  1054. setgcparam(g->genmajormul, majormul);
  1055. luaC_changemode(L, KGC_GEN);
  1056. break;
  1057. }
  1058. case LUA_GCINC: {
  1059. int pause = va_arg(argp, int);
  1060. int stepmul = va_arg(argp, int);
  1061. int stepsize = va_arg(argp, int);
  1062. res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC;
  1063. if (pause != 0)
  1064. setgcparam(g->gcpause, pause);
  1065. if (stepmul != 0)
  1066. setgcparam(g->gcstepmul, stepmul);
  1067. if (stepsize != 0)
  1068. g->gcstepsize = stepsize;
  1069. luaC_changemode(L, KGC_INC);
  1070. break;
  1071. }
  1072. default: res = -1; /* invalid option */
  1073. }
  1074. va_end(argp);
  1075. lua_unlock(L);
  1076. return res;
  1077. }
  1078. /*
  1079. ** miscellaneous functions
  1080. */
  1081. LUA_API int lua_error (lua_State *L) {
  1082. TValue *errobj;
  1083. lua_lock(L);
  1084. errobj = s2v(L->top.p - 1);
  1085. api_checknelems(L, 1);
  1086. /* error object is the memory error message? */
  1087. if (ttisshrstring(errobj) && eqshrstr(tsvalue(errobj), G(L)->memerrmsg))
  1088. luaM_error(L); /* raise a memory error */
  1089. else
  1090. luaG_errormsg(L); /* raise a regular error */
  1091. /* code unreachable; will unlock when control actually leaves the kernel */
  1092. return 0; /* to avoid warnings */
  1093. }
  1094. LUA_API int lua_next (lua_State *L, int idx) {
  1095. Table *t;
  1096. int more;
  1097. lua_lock(L);
  1098. api_checknelems(L, 1);
  1099. t = gettable(L, idx);
  1100. more = luaH_next(L, t, L->top.p - 1);
  1101. if (more) {
  1102. api_incr_top(L);
  1103. }
  1104. else /* no more elements */
  1105. L->top.p -= 1; /* remove key */
  1106. lua_unlock(L);
  1107. return more;
  1108. }
  1109. LUA_API void lua_toclose (lua_State *L, int idx) {
  1110. int nresults;
  1111. StkId o;
  1112. lua_lock(L);
  1113. o = index2stack(L, idx);
  1114. nresults = L->ci->nresults;
  1115. api_check(L, L->tbclist.p < o, "given index below or equal a marked one");
  1116. luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */
  1117. if (!hastocloseCfunc(nresults)) /* function not marked yet? */
  1118. L->ci->nresults = codeNresults(nresults); /* mark it */
  1119. lua_assert(hastocloseCfunc(L->ci->nresults));
  1120. lua_unlock(L);
  1121. }
  1122. LUA_API void lua_concat (lua_State *L, int n) {
  1123. lua_lock(L);
  1124. api_checknelems(L, n);
  1125. if (n > 0)
  1126. luaV_concat(L, n);
  1127. else { /* nothing to concatenate */
  1128. setsvalue2s(L, L->top.p, luaS_newlstr(L, "", 0)); /* push empty string */
  1129. api_incr_top(L);
  1130. }
  1131. luaC_checkGC(L);
  1132. lua_unlock(L);
  1133. }
  1134. LUA_API void lua_len (lua_State *L, int idx) {
  1135. TValue *t;
  1136. lua_lock(L);
  1137. t = index2value(L, idx);
  1138. luaV_objlen(L, L->top.p, t);
  1139. api_incr_top(L);
  1140. lua_unlock(L);
  1141. }
  1142. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  1143. lua_Alloc f;
  1144. lua_lock(L);
  1145. if (ud) *ud = G(L)->ud;
  1146. f = G(L)->frealloc;
  1147. lua_unlock(L);
  1148. return f;
  1149. }
  1150. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  1151. lua_lock(L);
  1152. G(L)->ud = ud;
  1153. G(L)->frealloc = f;
  1154. lua_unlock(L);
  1155. }
  1156. void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud) {
  1157. lua_lock(L);
  1158. G(L)->ud_warn = ud;
  1159. G(L)->warnf = f;
  1160. lua_unlock(L);
  1161. }
  1162. void lua_warning (lua_State *L, const char *msg, int tocont) {
  1163. lua_lock(L);
  1164. luaE_warning(L, msg, tocont);
  1165. lua_unlock(L);
  1166. }
  1167. LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) {
  1168. Udata *u;
  1169. lua_lock(L);
  1170. api_check(L, 0 <= nuvalue && nuvalue < USHRT_MAX, "invalid value");
  1171. u = luaS_newudata(L, size, nuvalue);
  1172. setuvalue(L, s2v(L->top.p), u);
  1173. api_incr_top(L);
  1174. luaC_checkGC(L);
  1175. lua_unlock(L);
  1176. return getudatamem(u);
  1177. }
  1178. static const char *aux_upvalue (TValue *fi, int n, TValue **val,
  1179. GCObject **owner) {
  1180. switch (ttypetag(fi)) {
  1181. case LUA_VCCL: { /* C closure */
  1182. CClosure *f = clCvalue(fi);
  1183. if (!(cast_uint(n) - 1u < cast_uint(f->nupvalues)))
  1184. return NULL; /* 'n' not in [1, f->nupvalues] */
  1185. *val = &f->upvalue[n-1];
  1186. if (owner) *owner = obj2gco(f);
  1187. return "";
  1188. }
  1189. case LUA_VLCL: { /* Lua closure */
  1190. LClosure *f = clLvalue(fi);
  1191. TString *name;
  1192. Proto *p = f->p;
  1193. if (!(cast_uint(n) - 1u < cast_uint(p->sizeupvalues)))
  1194. return NULL; /* 'n' not in [1, p->sizeupvalues] */
  1195. *val = f->upvals[n-1]->v.p;
  1196. if (owner) *owner = obj2gco(f->upvals[n - 1]);
  1197. name = p->upvalues[n-1].name;
  1198. return (name == NULL) ? "(no name)" : getstr(name);
  1199. }
  1200. default: return NULL; /* not a closure */
  1201. }
  1202. }
  1203. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  1204. const char *name;
  1205. TValue *val = NULL; /* to avoid warnings */
  1206. lua_lock(L);
  1207. name = aux_upvalue(index2value(L, funcindex), n, &val, NULL);
  1208. if (name) {
  1209. setobj2s(L, L->top.p, val);
  1210. api_incr_top(L);
  1211. }
  1212. lua_unlock(L);
  1213. return name;
  1214. }
  1215. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  1216. const char *name;
  1217. TValue *val = NULL; /* to avoid warnings */
  1218. GCObject *owner = NULL; /* to avoid warnings */
  1219. TValue *fi;
  1220. lua_lock(L);
  1221. fi = index2value(L, funcindex);
  1222. api_checknelems(L, 1);
  1223. name = aux_upvalue(fi, n, &val, &owner);
  1224. if (name) {
  1225. L->top.p--;
  1226. setobj(L, val, s2v(L->top.p));
  1227. luaC_barrier(L, owner, val);
  1228. }
  1229. lua_unlock(L);
  1230. return name;
  1231. }
  1232. static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
  1233. static const UpVal *const nullup = NULL;
  1234. LClosure *f;
  1235. TValue *fi = index2value(L, fidx);
  1236. api_check(L, ttisLclosure(fi), "Lua function expected");
  1237. f = clLvalue(fi);
  1238. if (pf) *pf = f;
  1239. if (1 <= n && n <= f->p->sizeupvalues)
  1240. return &f->upvals[n - 1]; /* get its upvalue pointer */
  1241. else
  1242. return (UpVal**)&nullup;
  1243. }
  1244. LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
  1245. TValue *fi = index2value(L, fidx);
  1246. switch (ttypetag(fi)) {
  1247. case LUA_VLCL: { /* lua closure */
  1248. return *getupvalref(L, fidx, n, NULL);
  1249. }
  1250. case LUA_VCCL: { /* C closure */
  1251. CClosure *f = clCvalue(fi);
  1252. if (1 <= n && n <= f->nupvalues)
  1253. return &f->upvalue[n - 1];
  1254. /* else */
  1255. } /* FALLTHROUGH */
  1256. case LUA_VLCF:
  1257. return NULL; /* light C functions have no upvalues */
  1258. default: {
  1259. api_check(L, 0, "function expected");
  1260. return NULL;
  1261. }
  1262. }
  1263. }
  1264. LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
  1265. int fidx2, int n2) {
  1266. LClosure *f1;
  1267. UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
  1268. UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
  1269. api_check(L, *up1 != NULL && *up2 != NULL, "invalid upvalue index");
  1270. *up1 = *up2;
  1271. luaC_objbarrier(L, f1, *up1);
  1272. }