lapi.c 36 KB

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