lapi.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  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. static 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_checkpop(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. newtop = L->top.p + diff;
  162. if (diff < 0 && L->tbclist.p >= newtop) {
  163. lua_assert(ci->callstatus & CIST_CLSRET);
  164. newtop = luaF_close(L, newtop, CLOSEKTOP, 0);
  165. }
  166. L->top.p = newtop; /* correct top only after closing any upvalue */
  167. lua_unlock(L);
  168. }
  169. LUA_API void lua_closeslot (lua_State *L, int idx) {
  170. StkId level;
  171. lua_lock(L);
  172. level = index2stack(L, idx);
  173. api_check(L, (L->ci->callstatus & CIST_CLSRET) && L->tbclist.p == level,
  174. "no variable to close at given level");
  175. level = luaF_close(L, level, CLOSEKTOP, 0);
  176. setnilvalue(s2v(level));
  177. lua_unlock(L);
  178. }
  179. /*
  180. ** Reverse the stack segment from 'from' to 'to'
  181. ** (auxiliary to 'lua_rotate')
  182. ** Note that we move(copy) only the value inside the stack.
  183. ** (We do not move additional fields that may exist.)
  184. */
  185. static void reverse (lua_State *L, StkId from, StkId to) {
  186. for (; from < to; from++, to--) {
  187. TValue temp;
  188. setobj(L, &temp, s2v(from));
  189. setobjs2s(L, from, to);
  190. setobj2s(L, to, &temp);
  191. }
  192. }
  193. /*
  194. ** Let x = AB, where A is a prefix of length 'n'. Then,
  195. ** rotate x n == BA. But BA == (A^r . B^r)^r.
  196. */
  197. LUA_API void lua_rotate (lua_State *L, int idx, int n) {
  198. StkId p, t, m;
  199. lua_lock(L);
  200. t = L->top.p - 1; /* end of stack segment being rotated */
  201. p = index2stack(L, idx); /* start of segment */
  202. api_check(L, L->tbclist.p < p, "moving a to-be-closed slot");
  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_checkpop(L, 2); /* all other operations expect two operands */
  271. else { /* for unary operations, add fake 2nd operand */
  272. api_checkpop(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--; /* pop 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. lua_unlock(L);
  340. if (len != NULL)
  341. return getlstr(tsvalue(o), *len);
  342. else
  343. return getstr(tsvalue(o));
  344. }
  345. LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) {
  346. const TValue *o = index2value(L, idx);
  347. switch (ttypetag(o)) {
  348. case LUA_VSHRSTR: return cast(lua_Unsigned, tsvalue(o)->shrlen);
  349. case LUA_VLNGSTR: return cast(lua_Unsigned, tsvalue(o)->u.lnglen);
  350. case LUA_VUSERDATA: return cast(lua_Unsigned, uvalue(o)->len);
  351. case LUA_VTABLE: return luaH_getn(hvalue(o));
  352. default: return 0;
  353. }
  354. }
  355. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
  356. const TValue *o = index2value(L, idx);
  357. if (ttislcf(o)) return fvalue(o);
  358. else if (ttisCclosure(o))
  359. return clCvalue(o)->f;
  360. else return NULL; /* not a C function */
  361. }
  362. l_sinline void *touserdata (const TValue *o) {
  363. switch (ttype(o)) {
  364. case LUA_TUSERDATA: return getudatamem(uvalue(o));
  365. case LUA_TLIGHTUSERDATA: return pvalue(o);
  366. default: return NULL;
  367. }
  368. }
  369. LUA_API void *lua_touserdata (lua_State *L, int idx) {
  370. const TValue *o = index2value(L, idx);
  371. return touserdata(o);
  372. }
  373. LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
  374. const TValue *o = index2value(L, idx);
  375. return (!ttisthread(o)) ? NULL : thvalue(o);
  376. }
  377. /*
  378. ** Returns a pointer to the internal representation of an object.
  379. ** Note that ANSI C does not allow the conversion of a pointer to
  380. ** function to a 'void*', so the conversion here goes through
  381. ** a 'size_t'. (As the returned pointer is only informative, this
  382. ** conversion should not be a problem.)
  383. */
  384. LUA_API const void *lua_topointer (lua_State *L, int idx) {
  385. const TValue *o = index2value(L, idx);
  386. switch (ttypetag(o)) {
  387. case LUA_VLCF: return cast_voidp(cast_sizet(fvalue(o)));
  388. case LUA_VUSERDATA: case LUA_VLIGHTUSERDATA:
  389. return touserdata(o);
  390. default: {
  391. if (iscollectable(o))
  392. return gcvalue(o);
  393. else
  394. return NULL;
  395. }
  396. }
  397. }
  398. /*
  399. ** push functions (C -> stack)
  400. */
  401. LUA_API void lua_pushnil (lua_State *L) {
  402. lua_lock(L);
  403. setnilvalue(s2v(L->top.p));
  404. api_incr_top(L);
  405. lua_unlock(L);
  406. }
  407. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  408. lua_lock(L);
  409. setfltvalue(s2v(L->top.p), n);
  410. api_incr_top(L);
  411. lua_unlock(L);
  412. }
  413. LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
  414. lua_lock(L);
  415. setivalue(s2v(L->top.p), n);
  416. api_incr_top(L);
  417. lua_unlock(L);
  418. }
  419. /*
  420. ** Pushes on the stack a string with given length. Avoid using 's' when
  421. ** 'len' == 0 (as 's' can be NULL in that case), due to later use of
  422. ** 'memcmp' and 'memcpy'.
  423. */
  424. LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
  425. TString *ts;
  426. lua_lock(L);
  427. ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len);
  428. setsvalue2s(L, L->top.p, ts);
  429. api_incr_top(L);
  430. luaC_checkGC(L);
  431. lua_unlock(L);
  432. return getstr(ts);
  433. }
  434. LUA_API const char *lua_pushextlstring (lua_State *L,
  435. const char *s, size_t len, lua_Alloc falloc, void *ud) {
  436. TString *ts;
  437. lua_lock(L);
  438. api_check(L, len <= MAX_SIZE, "string too large");
  439. api_check(L, s[len] == '\0', "string not ending with zero");
  440. ts = luaS_newextlstr (L, s, len, falloc, ud);
  441. setsvalue2s(L, L->top.p, ts);
  442. api_incr_top(L);
  443. luaC_checkGC(L);
  444. lua_unlock(L);
  445. return getstr(ts);
  446. }
  447. LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
  448. lua_lock(L);
  449. if (s == NULL)
  450. setnilvalue(s2v(L->top.p));
  451. else {
  452. TString *ts;
  453. ts = luaS_new(L, s);
  454. setsvalue2s(L, L->top.p, ts);
  455. s = getstr(ts); /* internal copy's address */
  456. }
  457. api_incr_top(L);
  458. luaC_checkGC(L);
  459. lua_unlock(L);
  460. return s;
  461. }
  462. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  463. va_list argp) {
  464. const char *ret;
  465. lua_lock(L);
  466. ret = luaO_pushvfstring(L, fmt, argp);
  467. luaC_checkGC(L);
  468. lua_unlock(L);
  469. return ret;
  470. }
  471. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  472. const char *ret;
  473. va_list argp;
  474. lua_lock(L);
  475. va_start(argp, fmt);
  476. ret = luaO_pushvfstring(L, fmt, argp);
  477. va_end(argp);
  478. luaC_checkGC(L);
  479. if (ret == NULL) /* error? */
  480. luaD_throw(L, LUA_ERRMEM);
  481. lua_unlock(L);
  482. return ret;
  483. }
  484. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  485. lua_lock(L);
  486. if (n == 0) {
  487. setfvalue(s2v(L->top.p), fn);
  488. api_incr_top(L);
  489. }
  490. else {
  491. int i;
  492. CClosure *cl;
  493. api_checkpop(L, n);
  494. api_check(L, n <= MAXUPVAL, "upvalue index too large");
  495. cl = luaF_newCclosure(L, n);
  496. cl->f = fn;
  497. for (i = 0; i < n; i++) {
  498. setobj2n(L, &cl->upvalue[i], s2v(L->top.p - n + i));
  499. /* does not need barrier because closure is white */
  500. lua_assert(iswhite(cl));
  501. }
  502. L->top.p -= n;
  503. setclCvalue(L, s2v(L->top.p), cl);
  504. api_incr_top(L);
  505. luaC_checkGC(L);
  506. }
  507. lua_unlock(L);
  508. }
  509. LUA_API void lua_pushboolean (lua_State *L, int b) {
  510. lua_lock(L);
  511. if (b)
  512. setbtvalue(s2v(L->top.p));
  513. else
  514. setbfvalue(s2v(L->top.p));
  515. api_incr_top(L);
  516. lua_unlock(L);
  517. }
  518. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  519. lua_lock(L);
  520. setpvalue(s2v(L->top.p), p);
  521. api_incr_top(L);
  522. lua_unlock(L);
  523. }
  524. LUA_API int lua_pushthread (lua_State *L) {
  525. lua_lock(L);
  526. setthvalue(L, s2v(L->top.p), L);
  527. api_incr_top(L);
  528. lua_unlock(L);
  529. return (G(L)->mainthread == L);
  530. }
  531. /*
  532. ** get functions (Lua -> stack)
  533. */
  534. static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
  535. lu_byte tag;
  536. TString *str = luaS_new(L, k);
  537. luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, tag);
  538. if (!tagisempty(tag)) {
  539. api_incr_top(L);
  540. }
  541. else {
  542. setsvalue2s(L, L->top.p, str);
  543. api_incr_top(L);
  544. tag = luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, tag);
  545. }
  546. lua_unlock(L);
  547. return novariant(tag);
  548. }
  549. static void getGlobalTable (lua_State *L, TValue *gt) {
  550. Table *registry = hvalue(&G(L)->l_registry);
  551. lu_byte tag = luaH_getint(registry, LUA_RIDX_GLOBALS, gt);
  552. (void)tag; /* avoid not-used warnings when checks are off */
  553. api_check(L, novariant(tag) == LUA_TTABLE, "global table must exist");
  554. }
  555. LUA_API int lua_getglobal (lua_State *L, const char *name) {
  556. TValue gt;
  557. lua_lock(L);
  558. getGlobalTable(L, &gt);
  559. return auxgetstr(L, &gt, name);
  560. }
  561. LUA_API int lua_gettable (lua_State *L, int idx) {
  562. lu_byte tag;
  563. TValue *t;
  564. lua_lock(L);
  565. api_checkpop(L, 1);
  566. t = index2value(L, idx);
  567. luaV_fastget(t, s2v(L->top.p - 1), s2v(L->top.p - 1), luaH_get, tag);
  568. if (tagisempty(tag))
  569. tag = luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, tag);
  570. lua_unlock(L);
  571. return novariant(tag);
  572. }
  573. LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
  574. lua_lock(L);
  575. return auxgetstr(L, index2value(L, idx), k);
  576. }
  577. LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
  578. TValue *t;
  579. lu_byte tag;
  580. lua_lock(L);
  581. t = index2value(L, idx);
  582. luaV_fastgeti(t, n, s2v(L->top.p), tag);
  583. if (tagisempty(tag)) {
  584. TValue key;
  585. setivalue(&key, n);
  586. tag = luaV_finishget(L, t, &key, L->top.p, tag);
  587. }
  588. api_incr_top(L);
  589. lua_unlock(L);
  590. return novariant(tag);
  591. }
  592. static int finishrawget (lua_State *L, lu_byte tag) {
  593. if (tagisempty(tag)) /* avoid copying empty items to the stack */
  594. setnilvalue(s2v(L->top.p));
  595. api_incr_top(L);
  596. lua_unlock(L);
  597. return novariant(tag);
  598. }
  599. l_sinline Table *gettable (lua_State *L, int idx) {
  600. TValue *t = index2value(L, idx);
  601. api_check(L, ttistable(t), "table expected");
  602. return hvalue(t);
  603. }
  604. LUA_API int lua_rawget (lua_State *L, int idx) {
  605. Table *t;
  606. lu_byte tag;
  607. lua_lock(L);
  608. api_checkpop(L, 1);
  609. t = gettable(L, idx);
  610. tag = luaH_get(t, s2v(L->top.p - 1), s2v(L->top.p - 1));
  611. L->top.p--; /* pop key */
  612. return finishrawget(L, tag);
  613. }
  614. LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
  615. Table *t;
  616. lu_byte tag;
  617. lua_lock(L);
  618. t = gettable(L, idx);
  619. luaH_fastgeti(t, n, s2v(L->top.p), tag);
  620. return finishrawget(L, tag);
  621. }
  622. LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
  623. Table *t;
  624. TValue k;
  625. lua_lock(L);
  626. t = gettable(L, idx);
  627. setpvalue(&k, cast_voidp(p));
  628. return finishrawget(L, luaH_get(t, &k, s2v(L->top.p)));
  629. }
  630. LUA_API void lua_createtable (lua_State *L, unsigned narray, unsigned nrec) {
  631. Table *t;
  632. lua_lock(L);
  633. t = luaH_new(L);
  634. sethvalue2s(L, L->top.p, t);
  635. api_incr_top(L);
  636. if (narray > 0 || nrec > 0)
  637. luaH_resize(L, t, narray, nrec);
  638. luaC_checkGC(L);
  639. lua_unlock(L);
  640. }
  641. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  642. const TValue *obj;
  643. Table *mt;
  644. int res = 0;
  645. lua_lock(L);
  646. obj = index2value(L, objindex);
  647. switch (ttype(obj)) {
  648. case LUA_TTABLE:
  649. mt = hvalue(obj)->metatable;
  650. break;
  651. case LUA_TUSERDATA:
  652. mt = uvalue(obj)->metatable;
  653. break;
  654. default:
  655. mt = G(L)->mt[ttype(obj)];
  656. break;
  657. }
  658. if (mt != NULL) {
  659. sethvalue2s(L, L->top.p, mt);
  660. api_incr_top(L);
  661. res = 1;
  662. }
  663. lua_unlock(L);
  664. return res;
  665. }
  666. LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) {
  667. TValue *o;
  668. int t;
  669. lua_lock(L);
  670. o = index2value(L, idx);
  671. api_check(L, ttisfulluserdata(o), "full userdata expected");
  672. if (n <= 0 || n > uvalue(o)->nuvalue) {
  673. setnilvalue(s2v(L->top.p));
  674. t = LUA_TNONE;
  675. }
  676. else {
  677. setobj2s(L, L->top.p, &uvalue(o)->uv[n - 1].uv);
  678. t = ttype(s2v(L->top.p));
  679. }
  680. api_incr_top(L);
  681. lua_unlock(L);
  682. return t;
  683. }
  684. /*
  685. ** set functions (stack -> Lua)
  686. */
  687. /*
  688. ** t[k] = value at the top of the stack (where 'k' is a string)
  689. */
  690. static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
  691. int hres;
  692. TString *str = luaS_new(L, k);
  693. api_checkpop(L, 1);
  694. luaV_fastset(t, str, s2v(L->top.p - 1), hres, luaH_psetstr);
  695. if (hres == HOK) {
  696. luaV_finishfastset(L, t, s2v(L->top.p - 1));
  697. L->top.p--; /* pop value */
  698. }
  699. else {
  700. setsvalue2s(L, L->top.p, str); /* push 'str' (to make it a TValue) */
  701. api_incr_top(L);
  702. luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), hres);
  703. L->top.p -= 2; /* pop value and key */
  704. }
  705. lua_unlock(L); /* lock done by caller */
  706. }
  707. LUA_API void lua_setglobal (lua_State *L, const char *name) {
  708. TValue gt;
  709. lua_lock(L); /* unlock done in 'auxsetstr' */
  710. getGlobalTable(L, &gt);
  711. auxsetstr(L, &gt, name);
  712. }
  713. LUA_API void lua_settable (lua_State *L, int idx) {
  714. TValue *t;
  715. int hres;
  716. lua_lock(L);
  717. api_checkpop(L, 2);
  718. t = index2value(L, idx);
  719. luaV_fastset(t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres, luaH_pset);
  720. if (hres == HOK) {
  721. luaV_finishfastset(L, t, s2v(L->top.p - 1));
  722. }
  723. else
  724. luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres);
  725. L->top.p -= 2; /* pop index and value */
  726. lua_unlock(L);
  727. }
  728. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  729. lua_lock(L); /* unlock done in 'auxsetstr' */
  730. auxsetstr(L, index2value(L, idx), k);
  731. }
  732. LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
  733. TValue *t;
  734. int hres;
  735. lua_lock(L);
  736. api_checkpop(L, 1);
  737. t = index2value(L, idx);
  738. luaV_fastseti(t, n, s2v(L->top.p - 1), hres);
  739. if (hres == HOK)
  740. luaV_finishfastset(L, t, s2v(L->top.p - 1));
  741. else {
  742. TValue temp;
  743. setivalue(&temp, n);
  744. luaV_finishset(L, t, &temp, s2v(L->top.p - 1), hres);
  745. }
  746. L->top.p--; /* pop value */
  747. lua_unlock(L);
  748. }
  749. static void aux_rawset (lua_State *L, int idx, TValue *key, int n) {
  750. Table *t;
  751. lua_lock(L);
  752. api_checkpop(L, n);
  753. t = gettable(L, idx);
  754. luaH_set(L, t, key, s2v(L->top.p - 1));
  755. invalidateTMcache(t);
  756. luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
  757. L->top.p -= n;
  758. lua_unlock(L);
  759. }
  760. LUA_API void lua_rawset (lua_State *L, int idx) {
  761. aux_rawset(L, idx, s2v(L->top.p - 2), 2);
  762. }
  763. LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
  764. TValue k;
  765. setpvalue(&k, cast_voidp(p));
  766. aux_rawset(L, idx, &k, 1);
  767. }
  768. LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
  769. Table *t;
  770. lua_lock(L);
  771. api_checkpop(L, 1);
  772. t = gettable(L, idx);
  773. luaH_setint(L, t, n, s2v(L->top.p - 1));
  774. luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
  775. L->top.p--;
  776. lua_unlock(L);
  777. }
  778. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  779. TValue *obj;
  780. Table *mt;
  781. lua_lock(L);
  782. api_checkpop(L, 1);
  783. obj = index2value(L, objindex);
  784. if (ttisnil(s2v(L->top.p - 1)))
  785. mt = NULL;
  786. else {
  787. api_check(L, ttistable(s2v(L->top.p - 1)), "table expected");
  788. mt = hvalue(s2v(L->top.p - 1));
  789. }
  790. switch (ttype(obj)) {
  791. case LUA_TTABLE: {
  792. hvalue(obj)->metatable = mt;
  793. if (mt) {
  794. luaC_objbarrier(L, gcvalue(obj), mt);
  795. luaC_checkfinalizer(L, gcvalue(obj), mt);
  796. }
  797. break;
  798. }
  799. case LUA_TUSERDATA: {
  800. uvalue(obj)->metatable = mt;
  801. if (mt) {
  802. luaC_objbarrier(L, uvalue(obj), mt);
  803. luaC_checkfinalizer(L, gcvalue(obj), mt);
  804. }
  805. break;
  806. }
  807. default: {
  808. G(L)->mt[ttype(obj)] = mt;
  809. break;
  810. }
  811. }
  812. L->top.p--;
  813. lua_unlock(L);
  814. return 1;
  815. }
  816. LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) {
  817. TValue *o;
  818. int res;
  819. lua_lock(L);
  820. api_checkpop(L, 1);
  821. o = index2value(L, idx);
  822. api_check(L, ttisfulluserdata(o), "full userdata expected");
  823. if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue)))
  824. res = 0; /* 'n' not in [1, uvalue(o)->nuvalue] */
  825. else {
  826. setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top.p - 1));
  827. luaC_barrierback(L, gcvalue(o), s2v(L->top.p - 1));
  828. res = 1;
  829. }
  830. L->top.p--;
  831. lua_unlock(L);
  832. return res;
  833. }
  834. /*
  835. ** 'load' and 'call' functions (run Lua code)
  836. */
  837. #define MAXRESULTS 250
  838. #define checkresults(L,na,nr) \
  839. (api_check(L, (nr) == LUA_MULTRET \
  840. || (L->ci->top.p - L->top.p >= (nr) - (na)), \
  841. "results from function overflow current stack size"), \
  842. api_check(L, LUA_MULTRET <= (nr) && (nr) <= MAXRESULTS, \
  843. "invalid number of results"))
  844. LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
  845. lua_KContext ctx, lua_KFunction k) {
  846. StkId func;
  847. lua_lock(L);
  848. api_check(L, k == NULL || !isLua(L->ci),
  849. "cannot use continuations inside hooks");
  850. api_checkpop(L, nargs + 1);
  851. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  852. checkresults(L, nargs, nresults);
  853. func = L->top.p - (nargs+1);
  854. if (k != NULL && yieldable(L)) { /* need to prepare continuation? */
  855. L->ci->u.c.k = k; /* save continuation */
  856. L->ci->u.c.ctx = ctx; /* save context */
  857. luaD_call(L, func, nresults); /* do the call */
  858. }
  859. else /* no continuation or no yieldable */
  860. luaD_callnoyield(L, func, nresults); /* just do the call */
  861. adjustresults(L, nresults);
  862. lua_unlock(L);
  863. }
  864. /*
  865. ** Execute a protected call.
  866. */
  867. struct CallS { /* data to 'f_call' */
  868. StkId func;
  869. int nresults;
  870. };
  871. static void f_call (lua_State *L, void *ud) {
  872. struct CallS *c = cast(struct CallS *, ud);
  873. luaD_callnoyield(L, c->func, c->nresults);
  874. }
  875. LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
  876. lua_KContext ctx, lua_KFunction k) {
  877. struct CallS c;
  878. int status;
  879. ptrdiff_t func;
  880. lua_lock(L);
  881. api_check(L, k == NULL || !isLua(L->ci),
  882. "cannot use continuations inside hooks");
  883. api_checkpop(L, nargs + 1);
  884. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  885. checkresults(L, nargs, nresults);
  886. if (errfunc == 0)
  887. func = 0;
  888. else {
  889. StkId o = index2stack(L, errfunc);
  890. api_check(L, ttisfunction(s2v(o)), "error handler must be a function");
  891. func = savestack(L, o);
  892. }
  893. c.func = L->top.p - (nargs+1); /* function to be called */
  894. if (k == NULL || !yieldable(L)) { /* no continuation or no yieldable? */
  895. c.nresults = nresults; /* do a 'conventional' protected call */
  896. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  897. }
  898. else { /* prepare continuation (call is already protected by 'resume') */
  899. CallInfo *ci = L->ci;
  900. ci->u.c.k = k; /* save continuation */
  901. ci->u.c.ctx = ctx; /* save context */
  902. /* save information for error recovery */
  903. ci->u2.funcidx = cast_int(savestack(L, c.func));
  904. ci->u.c.old_errfunc = L->errfunc;
  905. L->errfunc = func;
  906. setoah(ci, L->allowhook); /* save value of 'allowhook' */
  907. ci->callstatus |= CIST_YPCALL; /* function can do error recovery */
  908. luaD_call(L, c.func, nresults); /* do the call */
  909. ci->callstatus &= ~CIST_YPCALL;
  910. L->errfunc = ci->u.c.old_errfunc;
  911. status = LUA_OK; /* if it is here, there were no errors */
  912. }
  913. adjustresults(L, nresults);
  914. lua_unlock(L);
  915. return status;
  916. }
  917. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  918. const char *chunkname, const char *mode) {
  919. ZIO z;
  920. int status;
  921. lua_lock(L);
  922. if (!chunkname) chunkname = "?";
  923. luaZ_init(L, &z, reader, data);
  924. status = luaD_protectedparser(L, &z, chunkname, mode);
  925. if (status == LUA_OK) { /* no errors? */
  926. LClosure *f = clLvalue(s2v(L->top.p - 1)); /* get new function */
  927. if (f->nupvalues >= 1) { /* does it have an upvalue? */
  928. /* get global table from registry */
  929. TValue gt;
  930. getGlobalTable(L, &gt);
  931. /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
  932. setobj(L, f->upvals[0]->v.p, &gt);
  933. luaC_barrier(L, f->upvals[0], &gt);
  934. }
  935. }
  936. lua_unlock(L);
  937. return status;
  938. }
  939. /*
  940. ** Dump a Lua function, calling 'writer' to write its parts. Ensure
  941. ** the stack returns with its original size.
  942. */
  943. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
  944. int status;
  945. ptrdiff_t otop = savestack(L, L->top.p); /* original top */
  946. TValue *f = s2v(L->top.p - 1); /* function to be dumped */
  947. lua_lock(L);
  948. api_checkpop(L, 1);
  949. api_check(L, isLfunction(f), "Lua function expected");
  950. status = luaU_dump(L, clLvalue(f)->p, writer, data, strip);
  951. L->top.p = restorestack(L, otop); /* restore top */
  952. lua_unlock(L);
  953. return status;
  954. }
  955. LUA_API int lua_status (lua_State *L) {
  956. return L->status;
  957. }
  958. /*
  959. ** Garbage-collection function
  960. */
  961. LUA_API int lua_gc (lua_State *L, int what, ...) {
  962. va_list argp;
  963. int res = 0;
  964. global_State *g = G(L);
  965. if (g->gcstp & (GCSTPGC | GCSTPCLS)) /* internal stop? */
  966. return -1; /* all options are invalid when stopped */
  967. lua_lock(L);
  968. va_start(argp, what);
  969. switch (what) {
  970. case LUA_GCSTOP: {
  971. g->gcstp = GCSTPUSR; /* stopped by the user */
  972. break;
  973. }
  974. case LUA_GCRESTART: {
  975. luaE_setdebt(g, 0);
  976. g->gcstp = 0; /* (other bits must be zero here) */
  977. break;
  978. }
  979. case LUA_GCCOLLECT: {
  980. luaC_fullgc(L, 0);
  981. break;
  982. }
  983. case LUA_GCCOUNT: {
  984. /* GC values are expressed in Kbytes: #bytes/2^10 */
  985. res = cast_int(gettotalbytes(g) >> 10);
  986. break;
  987. }
  988. case LUA_GCCOUNTB: {
  989. res = cast_int(gettotalbytes(g) & 0x3ff);
  990. break;
  991. }
  992. case LUA_GCSTEP: {
  993. lu_byte oldstp = g->gcstp;
  994. l_mem n = cast(l_mem, va_arg(argp, size_t));
  995. int work = 0; /* true if GC did some work */
  996. g->gcstp = 0; /* allow GC to run (other bits must be zero here) */
  997. if (n <= 0)
  998. n = g->GCdebt; /* force to run one basic step */
  999. luaE_setdebt(g, g->GCdebt - n);
  1000. luaC_condGC(L, (void)0, work = 1);
  1001. if (work && g->gcstate == GCSpause) /* end of cycle? */
  1002. res = 1; /* signal it */
  1003. g->gcstp = oldstp; /* restore previous state */
  1004. break;
  1005. }
  1006. case LUA_GCISRUNNING: {
  1007. res = gcrunning(g);
  1008. break;
  1009. }
  1010. case LUA_GCGEN: {
  1011. res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN;
  1012. luaC_changemode(L, KGC_GENMINOR);
  1013. break;
  1014. }
  1015. case LUA_GCINC: {
  1016. res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN;
  1017. luaC_changemode(L, KGC_INC);
  1018. break;
  1019. }
  1020. case LUA_GCPARAM: {
  1021. int param = va_arg(argp, int);
  1022. int value = va_arg(argp, int);
  1023. api_check(L, 0 <= param && param < LUA_GCPN, "invalid parameter");
  1024. res = cast_int(luaO_applyparam(g->gcparams[param], 100));
  1025. if (value >= 0)
  1026. g->gcparams[param] = luaO_codeparam(cast_uint(value));
  1027. break;
  1028. }
  1029. default: res = -1; /* invalid option */
  1030. }
  1031. va_end(argp);
  1032. lua_unlock(L);
  1033. return res;
  1034. }
  1035. /*
  1036. ** miscellaneous functions
  1037. */
  1038. LUA_API int lua_error (lua_State *L) {
  1039. TValue *errobj;
  1040. lua_lock(L);
  1041. errobj = s2v(L->top.p - 1);
  1042. api_checkpop(L, 1);
  1043. /* error object is the memory error message? */
  1044. if (ttisshrstring(errobj) && eqshrstr(tsvalue(errobj), G(L)->memerrmsg))
  1045. luaM_error(L); /* raise a memory error */
  1046. else
  1047. luaG_errormsg(L); /* raise a regular error */
  1048. /* code unreachable; will unlock when control actually leaves the kernel */
  1049. return 0; /* to avoid warnings */
  1050. }
  1051. LUA_API int lua_next (lua_State *L, int idx) {
  1052. Table *t;
  1053. int more;
  1054. lua_lock(L);
  1055. api_checkpop(L, 1);
  1056. t = gettable(L, idx);
  1057. more = luaH_next(L, t, L->top.p - 1);
  1058. if (more)
  1059. api_incr_top(L);
  1060. else /* no more elements */
  1061. L->top.p--; /* pop key */
  1062. lua_unlock(L);
  1063. return more;
  1064. }
  1065. LUA_API void lua_toclose (lua_State *L, int idx) {
  1066. StkId o;
  1067. lua_lock(L);
  1068. o = index2stack(L, idx);
  1069. api_check(L, L->tbclist.p < o, "given index below or equal a marked one");
  1070. luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */
  1071. L->ci->callstatus |= CIST_CLSRET; /* mark that function has TBC slots */
  1072. lua_unlock(L);
  1073. }
  1074. LUA_API void lua_concat (lua_State *L, int n) {
  1075. lua_lock(L);
  1076. api_checknelems(L, n);
  1077. if (n > 0) {
  1078. luaV_concat(L, n);
  1079. luaC_checkGC(L);
  1080. }
  1081. else { /* nothing to concatenate */
  1082. setsvalue2s(L, L->top.p, luaS_newlstr(L, "", 0)); /* push empty string */
  1083. api_incr_top(L);
  1084. }
  1085. lua_unlock(L);
  1086. }
  1087. LUA_API void lua_len (lua_State *L, int idx) {
  1088. TValue *t;
  1089. lua_lock(L);
  1090. t = index2value(L, idx);
  1091. luaV_objlen(L, L->top.p, t);
  1092. api_incr_top(L);
  1093. lua_unlock(L);
  1094. }
  1095. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  1096. lua_Alloc f;
  1097. lua_lock(L);
  1098. if (ud) *ud = G(L)->ud;
  1099. f = G(L)->frealloc;
  1100. lua_unlock(L);
  1101. return f;
  1102. }
  1103. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  1104. lua_lock(L);
  1105. G(L)->ud = ud;
  1106. G(L)->frealloc = f;
  1107. lua_unlock(L);
  1108. }
  1109. void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud) {
  1110. lua_lock(L);
  1111. G(L)->ud_warn = ud;
  1112. G(L)->warnf = f;
  1113. lua_unlock(L);
  1114. }
  1115. void lua_warning (lua_State *L, const char *msg, int tocont) {
  1116. lua_lock(L);
  1117. luaE_warning(L, msg, tocont);
  1118. lua_unlock(L);
  1119. }
  1120. LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) {
  1121. Udata *u;
  1122. lua_lock(L);
  1123. api_check(L, 0 <= nuvalue && nuvalue < SHRT_MAX, "invalid value");
  1124. u = luaS_newudata(L, size, cast(unsigned short, nuvalue));
  1125. setuvalue(L, s2v(L->top.p), u);
  1126. api_incr_top(L);
  1127. luaC_checkGC(L);
  1128. lua_unlock(L);
  1129. return getudatamem(u);
  1130. }
  1131. static const char *aux_upvalue (TValue *fi, int n, TValue **val,
  1132. GCObject **owner) {
  1133. switch (ttypetag(fi)) {
  1134. case LUA_VCCL: { /* C closure */
  1135. CClosure *f = clCvalue(fi);
  1136. if (!(cast_uint(n) - 1u < cast_uint(f->nupvalues)))
  1137. return NULL; /* 'n' not in [1, f->nupvalues] */
  1138. *val = &f->upvalue[n-1];
  1139. if (owner) *owner = obj2gco(f);
  1140. return "";
  1141. }
  1142. case LUA_VLCL: { /* Lua closure */
  1143. LClosure *f = clLvalue(fi);
  1144. TString *name;
  1145. Proto *p = f->p;
  1146. if (!(cast_uint(n) - 1u < cast_uint(p->sizeupvalues)))
  1147. return NULL; /* 'n' not in [1, p->sizeupvalues] */
  1148. *val = f->upvals[n-1]->v.p;
  1149. if (owner) *owner = obj2gco(f->upvals[n - 1]);
  1150. name = p->upvalues[n-1].name;
  1151. return (name == NULL) ? "(no name)" : getstr(name);
  1152. }
  1153. default: return NULL; /* not a closure */
  1154. }
  1155. }
  1156. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  1157. const char *name;
  1158. TValue *val = NULL; /* to avoid warnings */
  1159. lua_lock(L);
  1160. name = aux_upvalue(index2value(L, funcindex), n, &val, NULL);
  1161. if (name) {
  1162. setobj2s(L, L->top.p, val);
  1163. api_incr_top(L);
  1164. }
  1165. lua_unlock(L);
  1166. return name;
  1167. }
  1168. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  1169. const char *name;
  1170. TValue *val = NULL; /* to avoid warnings */
  1171. GCObject *owner = NULL; /* to avoid warnings */
  1172. TValue *fi;
  1173. lua_lock(L);
  1174. fi = index2value(L, funcindex);
  1175. api_checknelems(L, 1);
  1176. name = aux_upvalue(fi, n, &val, &owner);
  1177. if (name) {
  1178. L->top.p--;
  1179. setobj(L, val, s2v(L->top.p));
  1180. luaC_barrier(L, owner, val);
  1181. }
  1182. lua_unlock(L);
  1183. return name;
  1184. }
  1185. static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
  1186. static const UpVal *const nullup = NULL;
  1187. LClosure *f;
  1188. TValue *fi = index2value(L, fidx);
  1189. api_check(L, ttisLclosure(fi), "Lua function expected");
  1190. f = clLvalue(fi);
  1191. if (pf) *pf = f;
  1192. if (1 <= n && n <= f->p->sizeupvalues)
  1193. return &f->upvals[n - 1]; /* get its upvalue pointer */
  1194. else
  1195. return (UpVal**)&nullup;
  1196. }
  1197. LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
  1198. TValue *fi = index2value(L, fidx);
  1199. switch (ttypetag(fi)) {
  1200. case LUA_VLCL: { /* lua closure */
  1201. return *getupvalref(L, fidx, n, NULL);
  1202. }
  1203. case LUA_VCCL: { /* C closure */
  1204. CClosure *f = clCvalue(fi);
  1205. if (1 <= n && n <= f->nupvalues)
  1206. return &f->upvalue[n - 1];
  1207. /* else */
  1208. } /* FALLTHROUGH */
  1209. case LUA_VLCF:
  1210. return NULL; /* light C functions have no upvalues */
  1211. default: {
  1212. api_check(L, 0, "function expected");
  1213. return NULL;
  1214. }
  1215. }
  1216. }
  1217. LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
  1218. int fidx2, int n2) {
  1219. LClosure *f1;
  1220. UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
  1221. UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
  1222. api_check(L, *up1 != NULL && *up2 != NULL, "invalid upvalue index");
  1223. *up1 = *up2;
  1224. luaC_objbarrier(L, f1, *up1);
  1225. }