lapi.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. /*
  2. ** $Id: lapi.c,v 2.76 2009/04/17 22:00:01 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_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func))
  28. #define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject)
  29. static TValue *index2adr (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));
  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));
  39. return L->top + idx;
  40. }
  41. else switch (idx) { /* pseudo-indices */
  42. case LUA_REGISTRYINDEX: return registry(L);
  43. case LUA_ENVIRONINDEX: {
  44. Closure *func = curr_func(L);
  45. sethvalue(L, &L->env, func->c.env);
  46. return &L->env;
  47. }
  48. case LUA_GLOBALSINDEX: return gt(L);
  49. default: {
  50. Closure *func = curr_func(L);
  51. idx = LUA_GLOBALSINDEX - idx;
  52. api_check(L, idx <= UCHAR_MAX + 1);
  53. return (idx <= func->c.nupvalues)
  54. ? &func->c.upvalue[idx-1]
  55. : cast(TValue *, luaO_nilobject);
  56. }
  57. }
  58. }
  59. static Table *getcurrenv (lua_State *L) {
  60. if (L->ci->previous == NULL) /* no enclosing function? */
  61. return hvalue(gt(L)); /* use global table as environment */
  62. else {
  63. Closure *func = curr_func(L);
  64. return func->c.env;
  65. }
  66. }
  67. LUA_API int lua_checkstack (lua_State *L, int size) {
  68. int res = 1;
  69. CallInfo *ci = L->ci;
  70. lua_lock(L);
  71. if (size > LUAI_MAXCSTACK ||
  72. (L->top - (ci->func + 1) + size) > LUAI_MAXCSTACK)
  73. res = 0; /* stack overflow */
  74. else if (size > 0) {
  75. luaD_checkstack(L, size);
  76. if (ci->top < L->top + size)
  77. ci->top = L->top + size;
  78. }
  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));
  88. api_check(from, to->ci->top - to->top >= n);
  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 void lua_checkversion_ (lua_State *L, int version) {
  104. lua_lock(L);
  105. if (version != LUA_VERSION_NUM)
  106. luaG_runerror(L, "app./library not compiled with header " LUA_VERSION);
  107. if (G(L)->nilobjp != luaO_nilobject)
  108. luaG_runerror(L, "application using multiple Lua VMs");
  109. lua_unlock(L);
  110. }
  111. /*
  112. ** basic stack manipulation
  113. */
  114. LUA_API int lua_gettop (lua_State *L) {
  115. return cast_int(L->top - (L->ci->func + 1));
  116. }
  117. LUA_API void lua_settop (lua_State *L, int idx) {
  118. StkId func = L->ci->func;
  119. lua_lock(L);
  120. if (idx >= 0) {
  121. api_check(L, idx <= L->stack_last - (func + 1));
  122. while (L->top < (func + 1) + idx)
  123. setnilvalue(L->top++);
  124. L->top = (func + 1) + idx;
  125. }
  126. else {
  127. api_check(L, -(idx+1) <= (L->top - (func + 1)));
  128. L->top += idx+1; /* `subtract' index (index is negative) */
  129. }
  130. lua_unlock(L);
  131. }
  132. LUA_API void lua_remove (lua_State *L, int idx) {
  133. StkId p;
  134. lua_lock(L);
  135. p = index2adr(L, idx);
  136. api_checkvalidindex(L, p);
  137. while (++p < L->top) setobjs2s(L, p-1, p);
  138. L->top--;
  139. lua_unlock(L);
  140. }
  141. LUA_API void lua_insert (lua_State *L, int idx) {
  142. StkId p;
  143. StkId q;
  144. lua_lock(L);
  145. p = index2adr(L, idx);
  146. api_checkvalidindex(L, p);
  147. for (q = L->top; q>p; q--) setobjs2s(L, q, q-1);
  148. setobjs2s(L, p, L->top);
  149. lua_unlock(L);
  150. }
  151. LUA_API void lua_replace (lua_State *L, int idx) {
  152. StkId o;
  153. lua_lock(L);
  154. /* explicit test for incompatible code */
  155. if (idx == LUA_ENVIRONINDEX && L->ci->previous == NULL)
  156. luaG_runerror(L, "no calling environment");
  157. api_checknelems(L, 1);
  158. o = index2adr(L, idx);
  159. api_checkvalidindex(L, o);
  160. if (idx == LUA_ENVIRONINDEX) {
  161. Closure *func = curr_func(L);
  162. api_check(L, ttistable(L->top - 1));
  163. func->c.env = hvalue(L->top - 1);
  164. luaC_barrier(L, func, L->top - 1);
  165. }
  166. else {
  167. setobj(L, o, L->top - 1);
  168. if (idx < LUA_GLOBALSINDEX) /* function upvalue? */
  169. luaC_barrier(L, curr_func(L), L->top - 1);
  170. }
  171. /* LUA_GLOBALSINDEX does not need gc barrier (threads are never black) */
  172. L->top--;
  173. lua_unlock(L);
  174. }
  175. LUA_API void lua_pushvalue (lua_State *L, int idx) {
  176. lua_lock(L);
  177. setobj2s(L, L->top, index2adr(L, idx));
  178. api_incr_top(L);
  179. lua_unlock(L);
  180. }
  181. /*
  182. ** access functions (stack -> C)
  183. */
  184. LUA_API int lua_type (lua_State *L, int idx) {
  185. StkId o = index2adr(L, idx);
  186. return (o == luaO_nilobject) ? LUA_TNONE : ttype(o);
  187. }
  188. LUA_API const char *lua_typename (lua_State *L, int t) {
  189. UNUSED(L);
  190. return (t == LUA_TNONE) ? "no value" : luaT_typenames[t];
  191. }
  192. LUA_API int lua_iscfunction (lua_State *L, int idx) {
  193. StkId o = index2adr(L, idx);
  194. return iscfunction(o);
  195. }
  196. LUA_API int lua_isnumber (lua_State *L, int idx) {
  197. TValue n;
  198. const TValue *o = index2adr(L, idx);
  199. return tonumber(o, &n);
  200. }
  201. LUA_API int lua_isstring (lua_State *L, int idx) {
  202. int t = lua_type(L, idx);
  203. return (t == LUA_TSTRING || t == LUA_TNUMBER);
  204. }
  205. LUA_API int lua_isuserdata (lua_State *L, int idx) {
  206. const TValue *o = index2adr(L, idx);
  207. return (ttisuserdata(o) || ttislightuserdata(o));
  208. }
  209. LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
  210. StkId o1 = index2adr(L, index1);
  211. StkId o2 = index2adr(L, index2);
  212. return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
  213. : luaO_rawequalObj(o1, o2);
  214. }
  215. LUA_API int lua_equal (lua_State *L, int index1, int index2) {
  216. StkId o1, o2;
  217. int i;
  218. lua_lock(L); /* may call tag method */
  219. o1 = index2adr(L, index1);
  220. o2 = index2adr(L, index2);
  221. i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 : equalobj(L, o1, o2);
  222. lua_unlock(L);
  223. return i;
  224. }
  225. LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
  226. StkId o1, o2;
  227. int i;
  228. lua_lock(L); /* may call tag method */
  229. o1 = index2adr(L, index1);
  230. o2 = index2adr(L, index2);
  231. i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
  232. : luaV_lessthan(L, o1, o2);
  233. lua_unlock(L);
  234. return i;
  235. }
  236. LUA_API lua_Number lua_tonumber (lua_State *L, int idx) {
  237. TValue n;
  238. const TValue *o = index2adr(L, idx);
  239. if (tonumber(o, &n))
  240. return nvalue(o);
  241. else
  242. return 0;
  243. }
  244. LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) {
  245. TValue n;
  246. const TValue *o = index2adr(L, idx);
  247. if (tonumber(o, &n)) {
  248. lua_Integer res;
  249. lua_Number num = nvalue(o);
  250. lua_number2integer(res, num);
  251. return res;
  252. }
  253. else
  254. return 0;
  255. }
  256. LUA_API int lua_toboolean (lua_State *L, int idx) {
  257. const TValue *o = index2adr(L, idx);
  258. return !l_isfalse(o);
  259. }
  260. LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
  261. StkId o = index2adr(L, idx);
  262. if (!ttisstring(o)) {
  263. lua_lock(L); /* `luaV_tostring' may create a new string */
  264. if (!luaV_tostring(L, o)) { /* conversion failed? */
  265. if (len != NULL) *len = 0;
  266. lua_unlock(L);
  267. return NULL;
  268. }
  269. luaC_checkGC(L);
  270. o = index2adr(L, idx); /* previous call may reallocate the stack */
  271. lua_unlock(L);
  272. }
  273. if (len != NULL) *len = tsvalue(o)->len;
  274. return svalue(o);
  275. }
  276. LUA_API size_t lua_objlen (lua_State *L, int idx) {
  277. StkId o = index2adr(L, idx);
  278. switch (ttype(o)) {
  279. case LUA_TSTRING: return tsvalue(o)->len;
  280. case LUA_TUSERDATA: return uvalue(o)->len;
  281. case LUA_TTABLE: return luaH_getn(hvalue(o));
  282. case LUA_TNUMBER: {
  283. size_t l;
  284. lua_lock(L); /* `luaV_tostring' may create a new string */
  285. l = (luaV_tostring(L, o) ? tsvalue(o)->len : 0);
  286. lua_unlock(L);
  287. return l;
  288. }
  289. default: return 0;
  290. }
  291. }
  292. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
  293. StkId o = index2adr(L, idx);
  294. return (!iscfunction(o)) ? NULL : clvalue(o)->c.f;
  295. }
  296. LUA_API void *lua_touserdata (lua_State *L, int idx) {
  297. StkId o = index2adr(L, idx);
  298. switch (ttype(o)) {
  299. case LUA_TUSERDATA: return (rawuvalue(o) + 1);
  300. case LUA_TLIGHTUSERDATA: return pvalue(o);
  301. default: return NULL;
  302. }
  303. }
  304. LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
  305. StkId o = index2adr(L, idx);
  306. return (!ttisthread(o)) ? NULL : thvalue(o);
  307. }
  308. LUA_API const void *lua_topointer (lua_State *L, int idx) {
  309. StkId o = index2adr(L, idx);
  310. switch (ttype(o)) {
  311. case LUA_TTABLE: return hvalue(o);
  312. case LUA_TFUNCTION: return clvalue(o);
  313. case LUA_TTHREAD: return thvalue(o);
  314. case LUA_TUSERDATA:
  315. case LUA_TLIGHTUSERDATA:
  316. return lua_touserdata(L, idx);
  317. default: return NULL;
  318. }
  319. }
  320. /*
  321. ** push functions (C -> stack)
  322. */
  323. LUA_API void lua_pushnil (lua_State *L) {
  324. lua_lock(L);
  325. setnilvalue(L->top);
  326. api_incr_top(L);
  327. lua_unlock(L);
  328. }
  329. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  330. lua_lock(L);
  331. setnvalue(L->top, n);
  332. api_incr_top(L);
  333. lua_unlock(L);
  334. }
  335. LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
  336. lua_lock(L);
  337. setnvalue(L->top, cast_num(n));
  338. api_incr_top(L);
  339. lua_unlock(L);
  340. }
  341. LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
  342. TString *ts;
  343. lua_lock(L);
  344. luaC_checkGC(L);
  345. ts = luaS_newlstr(L, s, len);
  346. setsvalue2s(L, L->top, ts);
  347. api_incr_top(L);
  348. lua_unlock(L);
  349. return getstr(ts);
  350. }
  351. LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
  352. if (s == NULL) {
  353. lua_pushnil(L);
  354. return NULL;
  355. }
  356. else
  357. return lua_pushlstring(L, s, strlen(s));
  358. }
  359. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  360. va_list argp) {
  361. const char *ret;
  362. lua_lock(L);
  363. luaC_checkGC(L);
  364. ret = luaO_pushvfstring(L, fmt, argp);
  365. lua_unlock(L);
  366. return ret;
  367. }
  368. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  369. const char *ret;
  370. va_list argp;
  371. lua_lock(L);
  372. luaC_checkGC(L);
  373. va_start(argp, fmt);
  374. ret = luaO_pushvfstring(L, fmt, argp);
  375. va_end(argp);
  376. lua_unlock(L);
  377. return ret;
  378. }
  379. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  380. Closure *cl;
  381. lua_lock(L);
  382. api_checknelems(L, n);
  383. api_check(L, n <= UCHAR_MAX);
  384. luaC_checkGC(L);
  385. cl = luaF_newCclosure(L, n, getcurrenv(L));
  386. cl->c.f = fn;
  387. L->top -= n;
  388. while (n--)
  389. setobj2n(L, &cl->c.upvalue[n], L->top+n);
  390. setclvalue(L, L->top, cl);
  391. lua_assert(iswhite(obj2gco(cl)));
  392. api_incr_top(L);
  393. lua_unlock(L);
  394. }
  395. LUA_API void lua_pushboolean (lua_State *L, int b) {
  396. lua_lock(L);
  397. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  398. api_incr_top(L);
  399. lua_unlock(L);
  400. }
  401. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  402. lua_lock(L);
  403. setpvalue(L->top, p);
  404. api_incr_top(L);
  405. lua_unlock(L);
  406. }
  407. LUA_API int lua_pushthread (lua_State *L) {
  408. lua_lock(L);
  409. setthvalue(L, L->top, L);
  410. api_incr_top(L);
  411. lua_unlock(L);
  412. return (G(L)->mainthread == L);
  413. }
  414. /*
  415. ** get functions (Lua -> stack)
  416. */
  417. LUA_API void lua_gettable (lua_State *L, int idx) {
  418. StkId t;
  419. lua_lock(L);
  420. t = index2adr(L, idx);
  421. api_checkvalidindex(L, t);
  422. luaV_gettable(L, t, L->top - 1, L->top - 1);
  423. lua_unlock(L);
  424. }
  425. LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
  426. StkId t;
  427. lua_lock(L);
  428. t = index2adr(L, idx);
  429. api_checkvalidindex(L, t);
  430. setsvalue2s(L, L->top, luaS_new(L, k));
  431. api_incr_top(L);
  432. luaV_gettable(L, t, L->top - 1, L->top - 1);
  433. lua_unlock(L);
  434. }
  435. LUA_API void lua_rawget (lua_State *L, int idx) {
  436. StkId t;
  437. lua_lock(L);
  438. t = index2adr(L, idx);
  439. api_check(L, ttistable(t));
  440. setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
  441. lua_unlock(L);
  442. }
  443. LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
  444. StkId o;
  445. lua_lock(L);
  446. o = index2adr(L, idx);
  447. api_check(L, ttistable(o));
  448. setobj2s(L, L->top, luaH_getnum(hvalue(o), n));
  449. api_incr_top(L);
  450. lua_unlock(L);
  451. }
  452. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  453. Table *t;
  454. lua_lock(L);
  455. luaC_checkGC(L);
  456. t = luaH_new(L);
  457. sethvalue(L, L->top, t);
  458. api_incr_top(L);
  459. if (narray > 0 || nrec > 0)
  460. luaH_resize(L, t, narray, nrec);
  461. lua_unlock(L);
  462. }
  463. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  464. const TValue *obj;
  465. Table *mt = NULL;
  466. int res;
  467. lua_lock(L);
  468. obj = index2adr(L, objindex);
  469. switch (ttype(obj)) {
  470. case LUA_TTABLE:
  471. mt = hvalue(obj)->metatable;
  472. break;
  473. case LUA_TUSERDATA:
  474. mt = uvalue(obj)->metatable;
  475. break;
  476. default:
  477. mt = G(L)->mt[ttype(obj)];
  478. break;
  479. }
  480. if (mt == NULL)
  481. res = 0;
  482. else {
  483. sethvalue(L, L->top, mt);
  484. api_incr_top(L);
  485. res = 1;
  486. }
  487. lua_unlock(L);
  488. return res;
  489. }
  490. LUA_API void lua_getfenv (lua_State *L, int idx) {
  491. StkId o;
  492. lua_lock(L);
  493. o = index2adr(L, idx);
  494. api_checkvalidindex(L, o);
  495. switch (ttype(o)) {
  496. case LUA_TFUNCTION:
  497. sethvalue(L, L->top, clvalue(o)->c.env);
  498. break;
  499. case LUA_TUSERDATA:
  500. sethvalue(L, L->top, uvalue(o)->env);
  501. break;
  502. case LUA_TTHREAD:
  503. setobj2s(L, L->top, gt(thvalue(o)));
  504. break;
  505. default:
  506. setnilvalue(L->top);
  507. break;
  508. }
  509. api_incr_top(L);
  510. lua_unlock(L);
  511. }
  512. /*
  513. ** set functions (stack -> Lua)
  514. */
  515. LUA_API void lua_settable (lua_State *L, int idx) {
  516. StkId t;
  517. lua_lock(L);
  518. api_checknelems(L, 2);
  519. t = index2adr(L, idx);
  520. api_checkvalidindex(L, t);
  521. luaV_settable(L, t, L->top - 2, L->top - 1);
  522. L->top -= 2; /* pop index and value */
  523. lua_unlock(L);
  524. }
  525. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  526. StkId t;
  527. lua_lock(L);
  528. api_checknelems(L, 1);
  529. t = index2adr(L, idx);
  530. api_checkvalidindex(L, t);
  531. setsvalue2s(L, L->top++, luaS_new(L, k));
  532. luaV_settable(L, t, L->top - 1, L->top - 2);
  533. L->top -= 2; /* pop value and key */
  534. lua_unlock(L);
  535. }
  536. LUA_API void lua_rawset (lua_State *L, int idx) {
  537. StkId t;
  538. lua_lock(L);
  539. api_checknelems(L, 2);
  540. t = index2adr(L, idx);
  541. api_check(L, ttistable(t));
  542. setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
  543. luaC_barriert(L, hvalue(t), L->top-1);
  544. L->top -= 2;
  545. lua_unlock(L);
  546. }
  547. LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
  548. StkId o;
  549. lua_lock(L);
  550. api_checknelems(L, 1);
  551. o = index2adr(L, idx);
  552. api_check(L, ttistable(o));
  553. setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1);
  554. luaC_barriert(L, hvalue(o), L->top-1);
  555. L->top--;
  556. lua_unlock(L);
  557. }
  558. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  559. TValue *obj;
  560. Table *mt;
  561. lua_lock(L);
  562. api_checknelems(L, 1);
  563. obj = index2adr(L, objindex);
  564. api_checkvalidindex(L, obj);
  565. if (ttisnil(L->top - 1))
  566. mt = NULL;
  567. else {
  568. api_check(L, ttistable(L->top - 1));
  569. mt = hvalue(L->top - 1);
  570. }
  571. switch (ttype(obj)) {
  572. case LUA_TTABLE: {
  573. hvalue(obj)->metatable = mt;
  574. if (mt)
  575. luaC_objbarriert(L, hvalue(obj), mt);
  576. break;
  577. }
  578. case LUA_TUSERDATA: {
  579. uvalue(obj)->metatable = mt;
  580. if (mt) {
  581. luaC_objbarrier(L, rawuvalue(obj), mt);
  582. luaC_checkfinalizer(L, rawuvalue(obj));
  583. }
  584. break;
  585. }
  586. default: {
  587. G(L)->mt[ttype(obj)] = mt;
  588. break;
  589. }
  590. }
  591. L->top--;
  592. lua_unlock(L);
  593. return 1;
  594. }
  595. LUA_API int lua_setfenv (lua_State *L, int idx) {
  596. StkId o;
  597. int res = 1;
  598. lua_lock(L);
  599. api_checknelems(L, 1);
  600. o = index2adr(L, idx);
  601. api_checkvalidindex(L, o);
  602. api_check(L, ttistable(L->top - 1));
  603. switch (ttype(o)) {
  604. case LUA_TFUNCTION:
  605. clvalue(o)->c.env = hvalue(L->top - 1);
  606. break;
  607. case LUA_TUSERDATA:
  608. uvalue(o)->env = hvalue(L->top - 1);
  609. break;
  610. case LUA_TTHREAD:
  611. sethvalue(L, gt(thvalue(o)), hvalue(L->top - 1));
  612. break;
  613. default:
  614. res = 0;
  615. break;
  616. }
  617. if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
  618. L->top--;
  619. lua_unlock(L);
  620. return res;
  621. }
  622. /*
  623. ** `load' and `call' functions (run Lua code)
  624. */
  625. #define checkresults(L,na,nr) \
  626. api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)))
  627. LUA_API int lua_getctx (lua_State *L, int *ctx) {
  628. if (L->ci->callstatus & CIST_YIELDED) {
  629. if (ctx) *ctx = L->ci->u.c.ctx;
  630. return L->ci->u.c.status;
  631. }
  632. else return LUA_OK;
  633. }
  634. LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
  635. lua_CFunction k) {
  636. StkId func;
  637. lua_lock(L);
  638. /* cannot use continuations inside hooks */
  639. api_check(L, k == NULL || !isLua(L->ci));
  640. api_checknelems(L, nargs+1);
  641. checkresults(L, nargs, nresults);
  642. func = L->top - (nargs+1);
  643. if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
  644. L->ci->u.c.k = k; /* save continuation */
  645. L->ci->u.c.ctx = ctx; /* save context */
  646. luaD_call(L, func, nresults, 1); /* do the call */
  647. }
  648. else /* no continuation or no yieldable */
  649. luaD_call(L, func, nresults, 0); /* just do the call */
  650. adjustresults(L, nresults);
  651. lua_unlock(L);
  652. }
  653. /*
  654. ** Execute a protected call.
  655. */
  656. struct CallS { /* data to `f_call' */
  657. StkId func;
  658. int nresults;
  659. };
  660. static void f_call (lua_State *L, void *ud) {
  661. struct CallS *c = cast(struct CallS *, ud);
  662. luaD_call(L, c->func, c->nresults, 0);
  663. }
  664. LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
  665. int ctx, lua_CFunction k) {
  666. struct CallS c;
  667. int status;
  668. ptrdiff_t func;
  669. lua_lock(L);
  670. api_checknelems(L, nargs+1);
  671. checkresults(L, nargs, nresults);
  672. if (errfunc == 0)
  673. func = 0;
  674. else {
  675. StkId o = index2adr(L, errfunc);
  676. api_checkvalidindex(L, o);
  677. func = savestack(L, o);
  678. }
  679. c.func = L->top - (nargs+1); /* function to be called */
  680. if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */
  681. c.nresults = nresults; /* do a 'conventional' protected call */
  682. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  683. }
  684. else { /* prepare continuation (call is already protected by 'resume') */
  685. CallInfo *ci = L->ci;
  686. ci->u.c.k = k; /* save continuation */
  687. ci->u.c.ctx = ctx; /* save context */
  688. /* save information for error recovery */
  689. ci->u.c.oldtop = savestack(L, c.func);
  690. ci->u.c.old_allowhook = L->allowhook;
  691. ci->u.c.old_errfunc = L->errfunc;
  692. L->errfunc = func;
  693. /* mark that function may do error recovery */
  694. ci->callstatus |= CIST_YPCALL;
  695. luaD_call(L, c.func, nresults, 1); /* do the call */
  696. ci->callstatus &= ~CIST_YPCALL;
  697. L->errfunc = ci->u.c.old_errfunc;
  698. status = LUA_OK; /* if it is here, there were no errors */
  699. }
  700. adjustresults(L, nresults);
  701. lua_unlock(L);
  702. return status;
  703. }
  704. /*
  705. ** Execute a protected C call.
  706. */
  707. struct CCallS { /* data to `f_Ccall' */
  708. lua_CFunction func;
  709. void *ud;
  710. };
  711. static void f_Ccall (lua_State *L, void *ud) {
  712. struct CCallS *c = cast(struct CCallS *, ud);
  713. Closure *cl;
  714. cl = luaF_newCclosure(L, 0, getcurrenv(L));
  715. cl->c.f = c->func;
  716. setclvalue(L, L->top, cl); /* push function */
  717. api_incr_top(L);
  718. setpvalue(L->top, c->ud); /* push only argument */
  719. api_incr_top(L);
  720. luaD_call(L, L->top - 2, 0, 0);
  721. }
  722. LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) {
  723. struct CCallS c;
  724. int status;
  725. lua_lock(L);
  726. c.func = func;
  727. c.ud = ud;
  728. status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0);
  729. lua_unlock(L);
  730. return status;
  731. }
  732. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  733. const char *chunkname) {
  734. ZIO z;
  735. int status;
  736. lua_lock(L);
  737. if (!chunkname) chunkname = "?";
  738. luaZ_init(L, &z, reader, data);
  739. status = luaD_protectedparser(L, &z, chunkname);
  740. lua_unlock(L);
  741. return status;
  742. }
  743. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
  744. int status;
  745. TValue *o;
  746. lua_lock(L);
  747. api_checknelems(L, 1);
  748. o = L->top - 1;
  749. if (isLfunction(o))
  750. status = luaU_dump(L, clvalue(o)->l.p, writer, data, 0);
  751. else
  752. status = 1;
  753. lua_unlock(L);
  754. return status;
  755. }
  756. LUA_API int lua_status (lua_State *L) {
  757. return L->status;
  758. }
  759. /*
  760. ** Garbage-collection function
  761. */
  762. LUA_API int lua_gc (lua_State *L, int what, int data) {
  763. int res = 0;
  764. global_State *g;
  765. lua_lock(L);
  766. g = G(L);
  767. switch (what) {
  768. case LUA_GCSTOP: {
  769. g->GCthreshold = MAX_LUMEM;
  770. break;
  771. }
  772. case LUA_GCRESTART: {
  773. g->GCthreshold = g->totalbytes;
  774. break;
  775. }
  776. case LUA_GCCOLLECT: {
  777. luaC_fullgc(L, 0);
  778. break;
  779. }
  780. case LUA_GCCOUNT: {
  781. /* GC values are expressed in Kbytes: #bytes/2^10 */
  782. res = cast_int(g->totalbytes >> 10);
  783. break;
  784. }
  785. case LUA_GCCOUNTB: {
  786. res = cast_int(g->totalbytes & 0x3ff);
  787. break;
  788. }
  789. case LUA_GCSTEP: {
  790. lu_mem oldts = g->GCthreshold;
  791. lu_mem a = (cast(lu_mem, data) << 10);
  792. g->GCthreshold = (a <= g->totalbytes) ? g->totalbytes - a : 0;
  793. while (g->GCthreshold <= g->totalbytes) {
  794. luaC_step(L);
  795. if (g->gcstate == GCSpause) { /* end of cycle? */
  796. res = 1; /* signal it */
  797. break;
  798. }
  799. }
  800. if (oldts == MAX_LUMEM) /* collector was stopped? */
  801. g->GCthreshold = oldts; /* keep it that way */
  802. break;
  803. }
  804. case LUA_GCSETPAUSE: {
  805. res = g->gcpause;
  806. g->gcpause = data;
  807. break;
  808. }
  809. case LUA_GCSETSTEPMUL: {
  810. res = g->gcstepmul;
  811. g->gcstepmul = data;
  812. break;
  813. }
  814. default: res = -1; /* invalid option */
  815. }
  816. lua_unlock(L);
  817. return res;
  818. }
  819. /*
  820. ** miscellaneous functions
  821. */
  822. LUA_API int lua_error (lua_State *L) {
  823. lua_lock(L);
  824. api_checknelems(L, 1);
  825. luaG_errormsg(L);
  826. lua_unlock(L);
  827. return 0; /* to avoid warnings */
  828. }
  829. LUA_API int lua_next (lua_State *L, int idx) {
  830. StkId t;
  831. int more;
  832. lua_lock(L);
  833. t = index2adr(L, idx);
  834. api_check(L, ttistable(t));
  835. more = luaH_next(L, hvalue(t), L->top - 1);
  836. if (more) {
  837. api_incr_top(L);
  838. }
  839. else /* no more elements */
  840. L->top -= 1; /* remove key */
  841. lua_unlock(L);
  842. return more;
  843. }
  844. LUA_API void lua_concat (lua_State *L, int n) {
  845. lua_lock(L);
  846. api_checknelems(L, n);
  847. if (n >= 2) {
  848. luaC_checkGC(L);
  849. luaV_concat(L, n);
  850. }
  851. else if (n == 0) { /* push empty string */
  852. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  853. api_incr_top(L);
  854. }
  855. /* else n == 1; nothing to do */
  856. lua_unlock(L);
  857. }
  858. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  859. lua_Alloc f;
  860. lua_lock(L);
  861. if (ud) *ud = G(L)->ud;
  862. f = G(L)->frealloc;
  863. lua_unlock(L);
  864. return f;
  865. }
  866. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  867. lua_lock(L);
  868. G(L)->ud = ud;
  869. G(L)->frealloc = f;
  870. lua_unlock(L);
  871. }
  872. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  873. Udata *u;
  874. lua_lock(L);
  875. luaC_checkGC(L);
  876. u = luaS_newudata(L, size, getcurrenv(L));
  877. setuvalue(L, L->top, u);
  878. api_incr_top(L);
  879. lua_unlock(L);
  880. return u + 1;
  881. }
  882. static const char *aux_upvalue (StkId fi, int n, TValue **val) {
  883. Closure *f;
  884. if (!ttisfunction(fi)) return NULL;
  885. f = clvalue(fi);
  886. if (f->c.isC) {
  887. if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
  888. *val = &f->c.upvalue[n-1];
  889. return "";
  890. }
  891. else {
  892. Proto *p = f->l.p;
  893. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  894. *val = f->l.upvals[n-1]->v;
  895. return getstr(p->upvalues[n-1]);
  896. }
  897. }
  898. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  899. const char *name;
  900. TValue *val;
  901. lua_lock(L);
  902. name = aux_upvalue(index2adr(L, funcindex), n, &val);
  903. if (name) {
  904. setobj2s(L, L->top, val);
  905. api_incr_top(L);
  906. }
  907. lua_unlock(L);
  908. return name;
  909. }
  910. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  911. const char *name;
  912. TValue *val;
  913. StkId fi;
  914. lua_lock(L);
  915. fi = index2adr(L, funcindex);
  916. api_checknelems(L, 1);
  917. name = aux_upvalue(fi, n, &val);
  918. if (name) {
  919. L->top--;
  920. setobj(L, val, L->top);
  921. luaC_barrier(L, clvalue(fi), L->top);
  922. }
  923. lua_unlock(L);
  924. return name;
  925. }