lapi.c 35 KB

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