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