lapi.c 35 KB

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