lapi.c 22 KB

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