lapi.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  1. /*
  2. ** $Id: lapi.c,v 2.148 2011/06/02 19:31:40 roberto Exp roberto $
  3. ** Lua API
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdarg.h>
  7. #include <string.h>
  8. #define lapi_c
  9. #define LUA_CORE
  10. #include "lua.h"
  11. #include "lapi.h"
  12. #include "ldebug.h"
  13. #include "ldo.h"
  14. #include "lfunc.h"
  15. #include "lgc.h"
  16. #include "lmem.h"
  17. #include "lobject.h"
  18. #include "lstate.h"
  19. #include "lstring.h"
  20. #include "ltable.h"
  21. #include "ltm.h"
  22. #include "lundump.h"
  23. #include "lvm.h"
  24. const char lua_ident[] =
  25. "$LuaVersion: " LUA_COPYRIGHT " $"
  26. "$LuaAuthors: " LUA_AUTHORS " $";
  27. #define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject, \
  28. "invalid index")
  29. static TValue *index2addr (lua_State *L, int idx) {
  30. CallInfo *ci = L->ci;
  31. if (idx > 0) {
  32. TValue *o = ci->func + idx;
  33. api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index");
  34. if (o >= L->top) return cast(TValue *, luaO_nilobject);
  35. else return o;
  36. }
  37. else if (idx > LUA_REGISTRYINDEX) {
  38. api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
  39. return L->top + idx;
  40. }
  41. else if (idx == LUA_REGISTRYINDEX)
  42. return &G(L)->l_registry;
  43. else { /* upvalues */
  44. idx = LUA_REGISTRYINDEX - idx;
  45. api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
  46. if (ttislcf(ci->func)) /* light C function? */
  47. return cast(TValue *, luaO_nilobject); /* it has no upvalues */
  48. else {
  49. CClosure *func = clCvalue(ci->func);
  50. return (idx <= func->nupvalues)
  51. ? &func->upvalue[idx-1]
  52. : cast(TValue *, luaO_nilobject);
  53. }
  54. }
  55. }
  56. /*
  57. ** to be called by 'lua_checkstack' in protected mode, to grow stack
  58. ** capturing memory errors
  59. */
  60. static void growstack (lua_State *L, void *ud) {
  61. int size = *(int *)ud;
  62. luaD_growstack(L, size);
  63. }
  64. LUA_API int lua_checkstack (lua_State *L, int size) {
  65. int res;
  66. CallInfo *ci = L->ci;
  67. lua_lock(L);
  68. if (L->stack_last - L->top > size) /* stack large enough? */
  69. res = 1; /* yes; check is OK */
  70. else { /* no; need to grow stack */
  71. int inuse = cast_int(L->top - L->stack) + EXTRA_STACK;
  72. if (inuse > LUAI_MAXSTACK - size) /* can grow without overflow? */
  73. res = 0; /* no */
  74. else /* try to grow stack */
  75. res = (luaD_rawrunprotected(L, &growstack, &size) == LUA_OK);
  76. }
  77. if (res && ci->top < L->top + size)
  78. ci->top = L->top + size; /* adjust frame top */
  79. lua_unlock(L);
  80. return res;
  81. }
  82. LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
  83. int i;
  84. if (from == to) return;
  85. lua_lock(to);
  86. api_checknelems(from, n);
  87. api_check(from, G(from) == G(to), "moving among independent states");
  88. api_check(from, to->ci->top - to->top >= n, "not enough elements to move");
  89. from->top -= n;
  90. for (i = 0; i < n; i++) {
  91. setobj2s(to, to->top++, from->top + i);
  92. }
  93. lua_unlock(to);
  94. }
  95. LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
  96. lua_CFunction old;
  97. lua_lock(L);
  98. old = G(L)->panic;
  99. G(L)->panic = panicf;
  100. lua_unlock(L);
  101. return old;
  102. }
  103. LUA_API const lua_Number *lua_version (lua_State *L) {
  104. static const lua_Number version = LUA_VERSION_NUM;
  105. if (L == NULL) return &version;
  106. else return G(L)->version;
  107. }
  108. /*
  109. ** basic stack manipulation
  110. */
  111. /*
  112. ** convert an acceptable stack index into an absolute index
  113. */
  114. LUA_API int lua_absindex (lua_State *L, int idx) {
  115. return (idx > 0 || idx <= LUA_REGISTRYINDEX)
  116. ? idx
  117. : cast_int(L->top - L->ci->func + idx);
  118. }
  119. LUA_API int lua_gettop (lua_State *L) {
  120. return cast_int(L->top - (L->ci->func + 1));
  121. }
  122. LUA_API void lua_settop (lua_State *L, int idx) {
  123. StkId func = L->ci->func;
  124. lua_lock(L);
  125. if (idx >= 0) {
  126. api_check(L, idx <= L->stack_last - (func + 1), "new top too large");
  127. while (L->top < (func + 1) + idx)
  128. setnilvalue(L->top++);
  129. L->top = (func + 1) + idx;
  130. }
  131. else {
  132. api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
  133. L->top += idx+1; /* `subtract' index (index is negative) */
  134. }
  135. lua_unlock(L);
  136. }
  137. LUA_API void lua_remove (lua_State *L, int idx) {
  138. StkId p;
  139. lua_lock(L);
  140. p = index2addr(L, idx);
  141. api_checkvalidindex(L, p);
  142. while (++p < L->top) setobjs2s(L, p-1, p);
  143. L->top--;
  144. lua_unlock(L);
  145. }
  146. LUA_API void lua_insert (lua_State *L, int idx) {
  147. StkId p;
  148. StkId q;
  149. lua_lock(L);
  150. p = index2addr(L, idx);
  151. api_checkvalidindex(L, p);
  152. for (q = L->top; q>p; q--) setobjs2s(L, q, q-1);
  153. setobjs2s(L, p, L->top);
  154. lua_unlock(L);
  155. }
  156. static void moveto (lua_State *L, TValue *fr, int idx) {
  157. TValue *to = index2addr(L, idx);
  158. api_checkvalidindex(L, to);
  159. setobj(L, to, fr);
  160. if (idx < LUA_REGISTRYINDEX) /* function upvalue? */
  161. luaC_barrier(L, clCvalue(L->ci->func), fr);
  162. /* LUA_REGISTRYINDEX does not need gc barrier
  163. (collector revisits it before finishing collection) */
  164. }
  165. LUA_API void lua_replace (lua_State *L, int idx) {
  166. lua_lock(L);
  167. api_checknelems(L, 1);
  168. moveto(L, L->top - 1, idx);
  169. L->top--;
  170. lua_unlock(L);
  171. }
  172. LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
  173. TValue *fr;
  174. lua_lock(L);
  175. fr = index2addr(L, fromidx);
  176. api_checkvalidindex(L, fr);
  177. moveto(L, fr, toidx);
  178. lua_unlock(L);
  179. }
  180. LUA_API void lua_pushvalue (lua_State *L, int idx) {
  181. lua_lock(L);
  182. setobj2s(L, L->top, index2addr(L, idx));
  183. api_incr_top(L);
  184. lua_unlock(L);
  185. }
  186. /*
  187. ** access functions (stack -> C)
  188. */
  189. LUA_API int lua_type (lua_State *L, int idx) {
  190. StkId o = index2addr(L, idx);
  191. return (o == luaO_nilobject) ? LUA_TNONE : ttypenv(o);
  192. }
  193. LUA_API const char *lua_typename (lua_State *L, int t) {
  194. UNUSED(L);
  195. return ttypename(t);
  196. }
  197. LUA_API int lua_iscfunction (lua_State *L, int idx) {
  198. StkId o = index2addr(L, idx);
  199. return (ttislcf(o) || (ttisCclosure(o)));
  200. }
  201. LUA_API int lua_isnumber (lua_State *L, int idx) {
  202. TValue n;
  203. const TValue *o = index2addr(L, idx);
  204. return tonumber(o, &n);
  205. }
  206. LUA_API int lua_isstring (lua_State *L, int idx) {
  207. int t = lua_type(L, idx);
  208. return (t == LUA_TSTRING || t == LUA_TNUMBER);
  209. }
  210. LUA_API int lua_isuserdata (lua_State *L, int idx) {
  211. const TValue *o = index2addr(L, idx);
  212. return (ttisuserdata(o) || ttislightuserdata(o));
  213. }
  214. LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
  215. StkId o1 = index2addr(L, index1);
  216. StkId o2 = index2addr(L, index2);
  217. return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
  218. : luaV_rawequalobj(o1, o2);
  219. }
  220. LUA_API void lua_arith (lua_State *L, int op) {
  221. StkId o1; /* 1st operand */
  222. StkId o2; /* 2nd operand */
  223. lua_lock(L);
  224. if (op != LUA_OPUNM) /* all other operations expect two operands */
  225. api_checknelems(L, 2);
  226. else { /* for unary minus, add fake 2nd operand */
  227. api_checknelems(L, 1);
  228. setobjs2s(L, L->top, L->top - 1);
  229. L->top++;
  230. }
  231. o1 = L->top - 2;
  232. o2 = L->top - 1;
  233. if (ttisnumber(o1) && ttisnumber(o2)) {
  234. changenvalue(o1, luaO_arith(op, nvalue(o1), nvalue(o2)));
  235. }
  236. else
  237. luaV_arith(L, o1, o1, o2, cast(TMS, op - LUA_OPADD + TM_ADD));
  238. L->top--;
  239. lua_unlock(L);
  240. }
  241. LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
  242. StkId o1, o2;
  243. int i;
  244. lua_lock(L); /* may call tag method */
  245. o1 = index2addr(L, index1);
  246. o2 = index2addr(L, index2);
  247. if (o1 == luaO_nilobject || o2 == luaO_nilobject)
  248. i = 0;
  249. else switch (op) {
  250. case LUA_OPEQ: i = equalobj(L, o1, o2); break;
  251. case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
  252. case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
  253. default: api_check(L, 0, "invalid option"); i = 0;
  254. }
  255. lua_unlock(L);
  256. return i;
  257. }
  258. LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *isnum) {
  259. TValue n;
  260. const TValue *o = index2addr(L, idx);
  261. if (tonumber(o, &n)) {
  262. if (isnum) *isnum = 1;
  263. return nvalue(o);
  264. }
  265. else {
  266. if (isnum) *isnum = 0;
  267. return 0;
  268. }
  269. }
  270. LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum) {
  271. TValue n;
  272. const TValue *o = index2addr(L, idx);
  273. if (tonumber(o, &n)) {
  274. lua_Integer res;
  275. lua_Number num = nvalue(o);
  276. lua_number2integer(res, num);
  277. if (isnum) *isnum = 1;
  278. return res;
  279. }
  280. else {
  281. if (isnum) *isnum = 0;
  282. return 0;
  283. }
  284. }
  285. LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *isnum) {
  286. TValue n;
  287. const TValue *o = index2addr(L, idx);
  288. if (tonumber(o, &n)) {
  289. lua_Unsigned res;
  290. lua_Number num = nvalue(o);
  291. lua_number2unsigned(res, num);
  292. if (isnum) *isnum = 1;
  293. return res;
  294. }
  295. else {
  296. if (isnum) *isnum = 0;
  297. return 0;
  298. }
  299. }
  300. LUA_API int lua_toboolean (lua_State *L, int idx) {
  301. const TValue *o = index2addr(L, idx);
  302. return !l_isfalse(o);
  303. }
  304. LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
  305. StkId o = index2addr(L, idx);
  306. if (!ttisstring(o)) {
  307. lua_lock(L); /* `luaV_tostring' may create a new string */
  308. if (!luaV_tostring(L, o)) { /* conversion failed? */
  309. if (len != NULL) *len = 0;
  310. lua_unlock(L);
  311. return NULL;
  312. }
  313. luaC_checkGC(L);
  314. o = index2addr(L, idx); /* previous call may reallocate the stack */
  315. lua_unlock(L);
  316. }
  317. if (len != NULL) *len = tsvalue(o)->len;
  318. return svalue(o);
  319. }
  320. LUA_API size_t lua_rawlen (lua_State *L, int idx) {
  321. StkId o = index2addr(L, idx);
  322. switch (ttypenv(o)) {
  323. case LUA_TSTRING: return tsvalue(o)->len;
  324. case LUA_TUSERDATA: return uvalue(o)->len;
  325. case LUA_TTABLE: return luaH_getn(hvalue(o));
  326. default: return 0;
  327. }
  328. }
  329. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
  330. StkId o = index2addr(L, idx);
  331. if (ttislcf(o)) return fvalue(o);
  332. else if (ttisCclosure(o))
  333. return clCvalue(o)->f;
  334. else return NULL; /* not a C function */
  335. }
  336. LUA_API void *lua_touserdata (lua_State *L, int idx) {
  337. StkId o = index2addr(L, idx);
  338. switch (ttypenv(o)) {
  339. case LUA_TUSERDATA: return (rawuvalue(o) + 1);
  340. case LUA_TLIGHTUSERDATA: return pvalue(o);
  341. default: return NULL;
  342. }
  343. }
  344. LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
  345. StkId o = index2addr(L, idx);
  346. return (!ttisthread(o)) ? NULL : thvalue(o);
  347. }
  348. LUA_API const void *lua_topointer (lua_State *L, int idx) {
  349. StkId o = index2addr(L, idx);
  350. switch (ttype(o)) {
  351. case LUA_TTABLE: return hvalue(o);
  352. case LUA_TLCL: return clLvalue(o);
  353. case LUA_TCCL: return clCvalue(o);
  354. case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o)));
  355. case LUA_TTHREAD: return thvalue(o);
  356. case LUA_TUSERDATA:
  357. case LUA_TLIGHTUSERDATA:
  358. return lua_touserdata(L, idx);
  359. default: return NULL;
  360. }
  361. }
  362. /*
  363. ** push functions (C -> stack)
  364. */
  365. LUA_API void lua_pushnil (lua_State *L) {
  366. lua_lock(L);
  367. setnilvalue(L->top);
  368. api_incr_top(L);
  369. lua_unlock(L);
  370. }
  371. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  372. lua_lock(L);
  373. setnvalue(L->top, n);
  374. luai_checknum(L, L->top,
  375. luaG_runerror(L, "C API - attempt to push a signaling NaN"));
  376. api_incr_top(L);
  377. lua_unlock(L);
  378. }
  379. LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
  380. lua_lock(L);
  381. setnvalue(L->top, cast_num(n));
  382. api_incr_top(L);
  383. lua_unlock(L);
  384. }
  385. LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) {
  386. lua_Number n;
  387. lua_lock(L);
  388. n = lua_unsigned2number(u);
  389. setnvalue(L->top, n);
  390. api_incr_top(L);
  391. lua_unlock(L);
  392. }
  393. LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
  394. TString *ts;
  395. lua_lock(L);
  396. luaC_checkGC(L);
  397. ts = luaS_newlstr(L, s, len);
  398. setsvalue2s(L, L->top, ts);
  399. api_incr_top(L);
  400. lua_unlock(L);
  401. return getstr(ts);
  402. }
  403. LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
  404. if (s == NULL) {
  405. lua_pushnil(L);
  406. return NULL;
  407. }
  408. else {
  409. TString *ts;
  410. lua_lock(L);
  411. luaC_checkGC(L);
  412. ts = luaS_new(L, s);
  413. setsvalue2s(L, L->top, ts);
  414. api_incr_top(L);
  415. lua_unlock(L);
  416. return getstr(ts);
  417. }
  418. }
  419. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  420. va_list argp) {
  421. const char *ret;
  422. lua_lock(L);
  423. luaC_checkGC(L);
  424. ret = luaO_pushvfstring(L, fmt, argp);
  425. lua_unlock(L);
  426. return ret;
  427. }
  428. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  429. const char *ret;
  430. va_list argp;
  431. lua_lock(L);
  432. luaC_checkGC(L);
  433. va_start(argp, fmt);
  434. ret = luaO_pushvfstring(L, fmt, argp);
  435. va_end(argp);
  436. lua_unlock(L);
  437. return ret;
  438. }
  439. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  440. lua_lock(L);
  441. if (n == 0) {
  442. setfvalue(L->top, fn);
  443. }
  444. else {
  445. Closure *cl;
  446. api_checknelems(L, n);
  447. api_check(L, n <= MAXUPVAL, "upvalue index too large");
  448. luaC_checkGC(L);
  449. cl = luaF_newCclosure(L, n);
  450. cl->c.f = fn;
  451. L->top -= n;
  452. while (n--)
  453. setobj2n(L, &cl->c.upvalue[n], L->top + n);
  454. setclCvalue(L, L->top, cl);
  455. }
  456. api_incr_top(L);
  457. lua_unlock(L);
  458. }
  459. LUA_API void lua_pushboolean (lua_State *L, int b) {
  460. lua_lock(L);
  461. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  462. api_incr_top(L);
  463. lua_unlock(L);
  464. }
  465. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  466. lua_lock(L);
  467. setpvalue(L->top, p);
  468. api_incr_top(L);
  469. lua_unlock(L);
  470. }
  471. LUA_API int lua_pushthread (lua_State *L) {
  472. lua_lock(L);
  473. setthvalue(L, L->top, L);
  474. api_incr_top(L);
  475. lua_unlock(L);
  476. return (G(L)->mainthread == L);
  477. }
  478. /*
  479. ** get functions (Lua -> stack)
  480. */
  481. LUA_API void lua_gettable (lua_State *L, int idx) {
  482. StkId t;
  483. lua_lock(L);
  484. t = index2addr(L, idx);
  485. api_checkvalidindex(L, t);
  486. luaV_gettable(L, t, L->top - 1, L->top - 1);
  487. lua_unlock(L);
  488. }
  489. LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
  490. StkId t;
  491. lua_lock(L);
  492. t = index2addr(L, idx);
  493. api_checkvalidindex(L, t);
  494. setsvalue2s(L, L->top, luaS_new(L, k));
  495. api_incr_top(L);
  496. luaV_gettable(L, t, L->top - 1, L->top - 1);
  497. lua_unlock(L);
  498. }
  499. LUA_API void lua_rawget (lua_State *L, int idx) {
  500. StkId t;
  501. lua_lock(L);
  502. t = index2addr(L, idx);
  503. api_check(L, ttistable(t), "table expected");
  504. setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
  505. lua_unlock(L);
  506. }
  507. LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
  508. StkId o;
  509. lua_lock(L);
  510. o = index2addr(L, idx);
  511. api_check(L, ttistable(o), "table expected");
  512. setobj2s(L, L->top, luaH_getint(hvalue(o), n));
  513. api_incr_top(L);
  514. lua_unlock(L);
  515. }
  516. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  517. Table *t;
  518. lua_lock(L);
  519. luaC_checkGC(L);
  520. t = luaH_new(L);
  521. sethvalue(L, L->top, t);
  522. api_incr_top(L);
  523. if (narray > 0 || nrec > 0)
  524. luaH_resize(L, t, narray, nrec);
  525. lua_unlock(L);
  526. }
  527. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  528. const TValue *obj;
  529. Table *mt = NULL;
  530. int res;
  531. lua_lock(L);
  532. obj = index2addr(L, objindex);
  533. switch (ttypenv(obj)) {
  534. case LUA_TTABLE:
  535. mt = hvalue(obj)->metatable;
  536. break;
  537. case LUA_TUSERDATA:
  538. mt = uvalue(obj)->metatable;
  539. break;
  540. default:
  541. mt = G(L)->mt[ttypenv(obj)];
  542. break;
  543. }
  544. if (mt == NULL)
  545. res = 0;
  546. else {
  547. sethvalue(L, L->top, mt);
  548. api_incr_top(L);
  549. res = 1;
  550. }
  551. lua_unlock(L);
  552. return res;
  553. }
  554. LUA_API void lua_getuservalue (lua_State *L, int idx) {
  555. StkId o;
  556. lua_lock(L);
  557. o = index2addr(L, idx);
  558. api_checkvalidindex(L, o);
  559. api_check(L, ttisuserdata(o), "userdata expected");
  560. if (uvalue(o)->env) {
  561. sethvalue(L, L->top, uvalue(o)->env);
  562. } else
  563. setnilvalue(L->top);
  564. api_incr_top(L);
  565. lua_unlock(L);
  566. }
  567. /*
  568. ** set functions (stack -> Lua)
  569. */
  570. LUA_API void lua_settable (lua_State *L, int idx) {
  571. StkId t;
  572. lua_lock(L);
  573. api_checknelems(L, 2);
  574. t = index2addr(L, idx);
  575. api_checkvalidindex(L, t);
  576. luaV_settable(L, t, L->top - 2, L->top - 1);
  577. L->top -= 2; /* pop index and value */
  578. lua_unlock(L);
  579. }
  580. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  581. StkId t;
  582. lua_lock(L);
  583. api_checknelems(L, 1);
  584. t = index2addr(L, idx);
  585. api_checkvalidindex(L, t);
  586. setsvalue2s(L, L->top++, luaS_new(L, k));
  587. luaV_settable(L, t, L->top - 1, L->top - 2);
  588. L->top -= 2; /* pop value and key */
  589. lua_unlock(L);
  590. }
  591. LUA_API void lua_rawset (lua_State *L, int idx) {
  592. StkId t;
  593. lua_lock(L);
  594. api_checknelems(L, 2);
  595. t = index2addr(L, idx);
  596. api_check(L, ttistable(t), "table expected");
  597. setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
  598. luaC_barrierback(L, gcvalue(t), L->top-1);
  599. L->top -= 2;
  600. lua_unlock(L);
  601. }
  602. LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
  603. StkId o;
  604. lua_lock(L);
  605. api_checknelems(L, 1);
  606. o = index2addr(L, idx);
  607. api_check(L, ttistable(o), "table expected");
  608. setobj2t(L, luaH_setint(L, hvalue(o), n), L->top-1);
  609. luaC_barrierback(L, gcvalue(o), L->top-1);
  610. L->top--;
  611. lua_unlock(L);
  612. }
  613. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  614. TValue *obj;
  615. Table *mt;
  616. lua_lock(L);
  617. api_checknelems(L, 1);
  618. obj = index2addr(L, objindex);
  619. api_checkvalidindex(L, obj);
  620. if (ttisnil(L->top - 1))
  621. mt = NULL;
  622. else {
  623. api_check(L, ttistable(L->top - 1), "table expected");
  624. mt = hvalue(L->top - 1);
  625. }
  626. switch (ttypenv(obj)) {
  627. case LUA_TTABLE: {
  628. hvalue(obj)->metatable = mt;
  629. if (mt)
  630. luaC_objbarrierback(L, gcvalue(obj), mt);
  631. luaC_checkfinalizer(L, gcvalue(obj), mt);
  632. break;
  633. }
  634. case LUA_TUSERDATA: {
  635. uvalue(obj)->metatable = mt;
  636. if (mt) {
  637. luaC_objbarrier(L, rawuvalue(obj), mt);
  638. luaC_checkfinalizer(L, gcvalue(obj), mt);
  639. }
  640. break;
  641. }
  642. default: {
  643. G(L)->mt[ttypenv(obj)] = mt;
  644. break;
  645. }
  646. }
  647. L->top--;
  648. lua_unlock(L);
  649. return 1;
  650. }
  651. LUA_API void lua_setuservalue (lua_State *L, int idx) {
  652. StkId o;
  653. lua_lock(L);
  654. api_checknelems(L, 1);
  655. o = index2addr(L, idx);
  656. api_checkvalidindex(L, o);
  657. api_check(L, ttisuserdata(o), "userdata expected");
  658. if (ttisnil(L->top - 1))
  659. uvalue(o)->env = NULL;
  660. else {
  661. api_check(L, ttistable(L->top - 1), "table expected");
  662. uvalue(o)->env = hvalue(L->top - 1);
  663. luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
  664. }
  665. L->top--;
  666. lua_unlock(L);
  667. }
  668. /*
  669. ** `load' and `call' functions (run Lua code)
  670. */
  671. #define checkresults(L,na,nr) \
  672. api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
  673. "results from function overflow current stack size")
  674. LUA_API int lua_getctx (lua_State *L, int *ctx) {
  675. if (L->ci->callstatus & CIST_YIELDED) {
  676. if (ctx) *ctx = L->ci->u.c.ctx;
  677. return L->ci->u.c.status;
  678. }
  679. else return LUA_OK;
  680. }
  681. LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
  682. lua_CFunction k) {
  683. StkId func;
  684. lua_lock(L);
  685. api_check(L, k == NULL || !isLua(L->ci),
  686. "cannot use continuations inside hooks");
  687. api_checknelems(L, nargs+1);
  688. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  689. checkresults(L, nargs, nresults);
  690. func = L->top - (nargs+1);
  691. if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
  692. L->ci->u.c.k = k; /* save continuation */
  693. L->ci->u.c.ctx = ctx; /* save context */
  694. luaD_call(L, func, nresults, 1); /* do the call */
  695. }
  696. else /* no continuation or no yieldable */
  697. luaD_call(L, func, nresults, 0); /* just do the call */
  698. adjustresults(L, nresults);
  699. lua_unlock(L);
  700. }
  701. /*
  702. ** Execute a protected call.
  703. */
  704. struct CallS { /* data to `f_call' */
  705. StkId func;
  706. int nresults;
  707. };
  708. static void f_call (lua_State *L, void *ud) {
  709. struct CallS *c = cast(struct CallS *, ud);
  710. luaD_call(L, c->func, c->nresults, 0);
  711. }
  712. LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
  713. int ctx, lua_CFunction k) {
  714. struct CallS c;
  715. int status;
  716. ptrdiff_t func;
  717. lua_lock(L);
  718. api_check(L, k == NULL || !isLua(L->ci),
  719. "cannot use continuations inside hooks");
  720. api_checknelems(L, nargs+1);
  721. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  722. checkresults(L, nargs, nresults);
  723. if (errfunc == 0)
  724. func = 0;
  725. else {
  726. StkId o = index2addr(L, errfunc);
  727. api_checkvalidindex(L, o);
  728. func = savestack(L, o);
  729. }
  730. c.func = L->top - (nargs+1); /* function to be called */
  731. if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */
  732. c.nresults = nresults; /* do a 'conventional' protected call */
  733. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  734. }
  735. else { /* prepare continuation (call is already protected by 'resume') */
  736. CallInfo *ci = L->ci;
  737. ci->u.c.k = k; /* save continuation */
  738. ci->u.c.ctx = ctx; /* save context */
  739. /* save information for error recovery */
  740. ci->u.c.extra = savestack(L, c.func);
  741. ci->u.c.old_allowhook = L->allowhook;
  742. ci->u.c.old_errfunc = L->errfunc;
  743. L->errfunc = func;
  744. /* mark that function may do error recovery */
  745. ci->callstatus |= CIST_YPCALL;
  746. luaD_call(L, c.func, nresults, 1); /* do the call */
  747. ci->callstatus &= ~CIST_YPCALL;
  748. L->errfunc = ci->u.c.old_errfunc;
  749. status = LUA_OK; /* if it is here, there were no errors */
  750. }
  751. adjustresults(L, nresults);
  752. lua_unlock(L);
  753. return status;
  754. }
  755. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  756. const char *chunkname) {
  757. ZIO z;
  758. int status;
  759. lua_lock(L);
  760. if (!chunkname) chunkname = "?";
  761. luaZ_init(L, &z, reader, data);
  762. status = luaD_protectedparser(L, &z, chunkname);
  763. if (status == LUA_OK) { /* no errors? */
  764. LClosure *f = clLvalue(L->top - 1); /* get newly created function */
  765. if (f->nupvalues == 1) { /* does it have one upvalue? */
  766. /* get global table from registry */
  767. Table *reg = hvalue(&G(L)->l_registry);
  768. const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  769. /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
  770. setobj(L, f->upvals[0]->v, gt);
  771. luaC_barrier(L, f->upvals[0], gt);
  772. }
  773. }
  774. lua_unlock(L);
  775. return status;
  776. }
  777. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
  778. int status;
  779. TValue *o;
  780. lua_lock(L);
  781. api_checknelems(L, 1);
  782. o = L->top - 1;
  783. if (isLfunction(o))
  784. status = luaU_dump(L, getproto(o), writer, data, 0);
  785. else
  786. status = 1;
  787. lua_unlock(L);
  788. return status;
  789. }
  790. LUA_API int lua_status (lua_State *L) {
  791. return L->status;
  792. }
  793. /*
  794. ** Garbage-collection function
  795. */
  796. LUA_API int lua_gc (lua_State *L, int what, int data) {
  797. int res = 0;
  798. global_State *g;
  799. lua_lock(L);
  800. g = G(L);
  801. switch (what) {
  802. case LUA_GCSTOP: {
  803. g->gcrunning = 0;
  804. break;
  805. }
  806. case LUA_GCRESTART: {
  807. luaE_setdebt(g, 0);
  808. g->gcrunning = 1;
  809. break;
  810. }
  811. case LUA_GCCOLLECT: {
  812. luaC_fullgc(L, 0);
  813. break;
  814. }
  815. case LUA_GCCOUNT: {
  816. /* GC values are expressed in Kbytes: #bytes/2^10 */
  817. res = cast_int(gettotalbytes(g) >> 10);
  818. break;
  819. }
  820. case LUA_GCCOUNTB: {
  821. res = cast_int(gettotalbytes(g) & 0x3ff);
  822. break;
  823. }
  824. case LUA_GCSTEP: {
  825. if (g->gckind == KGC_GEN) { /* generational mode? */
  826. res = (g->lastmajormem == 0); /* 1 if will do major collection */
  827. luaC_forcestep(L); /* do a single step */
  828. }
  829. else {
  830. while (data-- >= 0) {
  831. luaC_forcestep(L);
  832. if (g->gcstate == GCSpause) { /* end of cycle? */
  833. res = 1; /* signal it */
  834. break;
  835. }
  836. }
  837. }
  838. break;
  839. }
  840. case LUA_GCSETPAUSE: {
  841. res = g->gcpause;
  842. g->gcpause = data;
  843. break;
  844. }
  845. case LUA_GCSETMAJORINC: {
  846. res = g->gcmajorinc;
  847. g->gcmajorinc = data;
  848. break;
  849. }
  850. case LUA_GCSETSTEPMUL: {
  851. res = g->gcstepmul;
  852. g->gcstepmul = data;
  853. break;
  854. }
  855. case LUA_GCISRUNNING: {
  856. res = g->gcrunning;
  857. break;
  858. }
  859. case LUA_GCGEN: { /* change collector to generational mode */
  860. luaC_changemode(L, KGC_GEN);
  861. break;
  862. }
  863. case LUA_GCINC: { /* change collector to incremental mode */
  864. luaC_changemode(L, KGC_NORMAL);
  865. break;
  866. }
  867. default: res = -1; /* invalid option */
  868. }
  869. lua_unlock(L);
  870. return res;
  871. }
  872. /*
  873. ** miscellaneous functions
  874. */
  875. LUA_API int lua_error (lua_State *L) {
  876. lua_lock(L);
  877. api_checknelems(L, 1);
  878. luaG_errormsg(L);
  879. lua_unlock(L);
  880. return 0; /* to avoid warnings */
  881. }
  882. LUA_API int lua_next (lua_State *L, int idx) {
  883. StkId t;
  884. int more;
  885. lua_lock(L);
  886. t = index2addr(L, idx);
  887. api_check(L, ttistable(t), "table expected");
  888. more = luaH_next(L, hvalue(t), L->top - 1);
  889. if (more) {
  890. api_incr_top(L);
  891. }
  892. else /* no more elements */
  893. L->top -= 1; /* remove key */
  894. lua_unlock(L);
  895. return more;
  896. }
  897. LUA_API void lua_concat (lua_State *L, int n) {
  898. lua_lock(L);
  899. api_checknelems(L, n);
  900. if (n >= 2) {
  901. luaC_checkGC(L);
  902. luaV_concat(L, n);
  903. }
  904. else if (n == 0) { /* push empty string */
  905. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  906. api_incr_top(L);
  907. }
  908. /* else n == 1; nothing to do */
  909. lua_unlock(L);
  910. }
  911. LUA_API void lua_len (lua_State *L, int idx) {
  912. StkId t;
  913. lua_lock(L);
  914. t = index2addr(L, idx);
  915. luaV_objlen(L, L->top, t);
  916. api_incr_top(L);
  917. lua_unlock(L);
  918. }
  919. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  920. lua_Alloc f;
  921. lua_lock(L);
  922. if (ud) *ud = G(L)->ud;
  923. f = G(L)->frealloc;
  924. lua_unlock(L);
  925. return f;
  926. }
  927. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  928. lua_lock(L);
  929. G(L)->ud = ud;
  930. G(L)->frealloc = f;
  931. lua_unlock(L);
  932. }
  933. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  934. Udata *u;
  935. lua_lock(L);
  936. luaC_checkGC(L);
  937. u = luaS_newudata(L, size, NULL);
  938. setuvalue(L, L->top, u);
  939. api_incr_top(L);
  940. lua_unlock(L);
  941. return u + 1;
  942. }
  943. static const char *aux_upvalue (StkId fi, int n, TValue **val,
  944. GCObject **owner) {
  945. switch (ttype(fi)) {
  946. case LUA_TCCL: { /* C closure */
  947. CClosure *f = clCvalue(fi);
  948. if (!(1 <= n && n <= f->nupvalues)) return NULL;
  949. *val = &f->upvalue[n-1];
  950. if (owner) *owner = obj2gco(f);
  951. return "";
  952. }
  953. case LUA_TLCL: { /* Lua closure */
  954. LClosure *f = clLvalue(fi);
  955. const char *name;
  956. Proto *p = f->p;
  957. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  958. *val = f->upvals[n-1]->v;
  959. if (owner) *owner = obj2gco(f->upvals[n - 1]);
  960. name = getstr(p->upvalues[n-1].name);
  961. if (name == NULL) /* no debug information? */
  962. name = "";
  963. return name;
  964. }
  965. default: return NULL; /* not a closure */
  966. }
  967. }
  968. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  969. const char *name;
  970. TValue *val;
  971. lua_lock(L);
  972. name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL);
  973. if (name) {
  974. setobj2s(L, L->top, val);
  975. api_incr_top(L);
  976. }
  977. lua_unlock(L);
  978. return name;
  979. }
  980. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  981. const char *name;
  982. TValue *val;
  983. GCObject *owner;
  984. StkId fi;
  985. lua_lock(L);
  986. fi = index2addr(L, funcindex);
  987. api_checknelems(L, 1);
  988. name = aux_upvalue(fi, n, &val, &owner);
  989. if (name) {
  990. L->top--;
  991. setobj(L, val, L->top);
  992. luaC_barrier(L, owner, L->top);
  993. }
  994. lua_unlock(L);
  995. return name;
  996. }
  997. static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
  998. LClosure *f;
  999. StkId fi = index2addr(L, fidx);
  1000. api_check(L, ttisLclosure(fi), "Lua function expected");
  1001. f = clLvalue(fi);
  1002. api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
  1003. if (pf) *pf = f;
  1004. return &f->upvals[n - 1]; /* get its upvalue pointer */
  1005. }
  1006. LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
  1007. StkId fi = index2addr(L, fidx);
  1008. switch (ttype(fi)) {
  1009. case LUA_TLCL: { /* lua closure */
  1010. return *getupvalref(L, fidx, n, NULL);
  1011. }
  1012. case LUA_TCCL: { /* C closure */
  1013. CClosure *f = clCvalue(fi);
  1014. api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
  1015. return &f->upvalue[n - 1];
  1016. }
  1017. default: {
  1018. api_check(L, 0, "closure expected");
  1019. return NULL;
  1020. }
  1021. }
  1022. }
  1023. LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
  1024. int fidx2, int n2) {
  1025. LClosure *f1;
  1026. UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
  1027. UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
  1028. *up1 = *up2;
  1029. luaC_objbarrier(L, f1, *up2);
  1030. }