lj_api.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  1. /*
  2. ** Public Lua/C API.
  3. ** Copyright (C) 2005-2021 Mike Pall. See Copyright Notice in luajit.h
  4. **
  5. ** Major portions taken verbatim or adapted from the Lua interpreter.
  6. ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
  7. */
  8. #define lj_api_c
  9. #define LUA_CORE
  10. #include "lj_obj.h"
  11. #include "lj_gc.h"
  12. #include "lj_err.h"
  13. #include "lj_debug.h"
  14. #include "lj_str.h"
  15. #include "lj_tab.h"
  16. #include "lj_func.h"
  17. #include "lj_udata.h"
  18. #include "lj_meta.h"
  19. #include "lj_state.h"
  20. #include "lj_bc.h"
  21. #include "lj_frame.h"
  22. #include "lj_trace.h"
  23. #include "lj_vm.h"
  24. #include "lj_strscan.h"
  25. #include "lj_strfmt.h"
  26. /* -- Common helper functions --------------------------------------------- */
  27. #define lj_checkapi_slot(idx) \
  28. lj_checkapi((idx) <= (L->top - L->base), "stack slot %d out of range", (idx))
  29. static TValue *index2adr(lua_State *L, int idx)
  30. {
  31. if (idx > 0) {
  32. TValue *o = L->base + (idx - 1);
  33. return o < L->top ? o : niltv(L);
  34. } else if (idx > LUA_REGISTRYINDEX) {
  35. lj_checkapi(idx != 0 && -idx <= L->top - L->base,
  36. "bad stack slot %d", idx);
  37. return L->top + idx;
  38. } else if (idx == LUA_GLOBALSINDEX) {
  39. TValue *o = &G(L)->tmptv;
  40. settabV(L, o, tabref(L->env));
  41. return o;
  42. } else if (idx == LUA_REGISTRYINDEX) {
  43. return registry(L);
  44. } else {
  45. GCfunc *fn = curr_func(L);
  46. lj_checkapi(fn->c.gct == ~LJ_TFUNC && !isluafunc(fn),
  47. "calling frame is not a C function");
  48. if (idx == LUA_ENVIRONINDEX) {
  49. TValue *o = &G(L)->tmptv;
  50. settabV(L, o, tabref(fn->c.env));
  51. return o;
  52. } else {
  53. idx = LUA_GLOBALSINDEX - idx;
  54. return idx <= fn->c.nupvalues ? &fn->c.upvalue[idx-1] : niltv(L);
  55. }
  56. }
  57. }
  58. static LJ_AINLINE TValue *index2adr_check(lua_State *L, int idx)
  59. {
  60. TValue *o = index2adr(L, idx);
  61. lj_checkapi(o != niltv(L), "invalid stack slot %d", idx);
  62. return o;
  63. }
  64. static TValue *index2adr_stack(lua_State *L, int idx)
  65. {
  66. if (idx > 0) {
  67. TValue *o = L->base + (idx - 1);
  68. if (o < L->top) {
  69. return o;
  70. } else {
  71. lj_checkapi(0, "invalid stack slot %d", idx);
  72. return niltv(L);
  73. }
  74. return o < L->top ? o : niltv(L);
  75. } else {
  76. lj_checkapi(idx != 0 && -idx <= L->top - L->base,
  77. "invalid stack slot %d", idx);
  78. return L->top + idx;
  79. }
  80. }
  81. static GCtab *getcurrenv(lua_State *L)
  82. {
  83. GCfunc *fn = curr_func(L);
  84. return fn->c.gct == ~LJ_TFUNC ? tabref(fn->c.env) : tabref(L->env);
  85. }
  86. /* -- Miscellaneous API functions ----------------------------------------- */
  87. LUA_API int lua_status(lua_State *L)
  88. {
  89. return L->status;
  90. }
  91. LUA_API int lua_checkstack(lua_State *L, int size)
  92. {
  93. if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK) {
  94. return 0; /* Stack overflow. */
  95. } else if (size > 0) {
  96. lj_state_checkstack(L, (MSize)size);
  97. }
  98. return 1;
  99. }
  100. LUALIB_API void luaL_checkstack(lua_State *L, int size, const char *msg)
  101. {
  102. if (!lua_checkstack(L, size))
  103. lj_err_callerv(L, LJ_ERR_STKOVM, msg);
  104. }
  105. LUA_API void lua_xmove(lua_State *L, lua_State *to, int n)
  106. {
  107. TValue *f, *t;
  108. if (L == to) return;
  109. lj_checkapi_slot(n);
  110. lj_checkapi(G(L) == G(to), "move across global states");
  111. lj_state_checkstack(to, (MSize)n);
  112. f = L->top;
  113. t = to->top = to->top + n;
  114. while (--n >= 0) copyTV(to, --t, --f);
  115. L->top = f;
  116. }
  117. LUA_API const lua_Number *lua_version(lua_State *L)
  118. {
  119. static const lua_Number version = LUA_VERSION_NUM;
  120. UNUSED(L);
  121. return &version;
  122. }
  123. /* -- Stack manipulation -------------------------------------------------- */
  124. LUA_API int lua_gettop(lua_State *L)
  125. {
  126. return (int)(L->top - L->base);
  127. }
  128. LUA_API void lua_settop(lua_State *L, int idx)
  129. {
  130. if (idx >= 0) {
  131. lj_checkapi(idx <= tvref(L->maxstack) - L->base, "bad stack slot %d", idx);
  132. if (L->base + idx > L->top) {
  133. if (L->base + idx >= tvref(L->maxstack))
  134. lj_state_growstack(L, (MSize)idx - (MSize)(L->top - L->base));
  135. do { setnilV(L->top++); } while (L->top < L->base + idx);
  136. } else {
  137. L->top = L->base + idx;
  138. }
  139. } else {
  140. lj_checkapi(-(idx+1) <= (L->top - L->base), "bad stack slot %d", idx);
  141. L->top += idx+1; /* Shrinks top (idx < 0). */
  142. }
  143. }
  144. LUA_API void lua_remove(lua_State *L, int idx)
  145. {
  146. TValue *p = index2adr_stack(L, idx);
  147. while (++p < L->top) copyTV(L, p-1, p);
  148. L->top--;
  149. }
  150. LUA_API void lua_insert(lua_State *L, int idx)
  151. {
  152. TValue *q, *p = index2adr_stack(L, idx);
  153. for (q = L->top; q > p; q--) copyTV(L, q, q-1);
  154. copyTV(L, p, L->top);
  155. }
  156. static void copy_slot(lua_State *L, TValue *f, int idx)
  157. {
  158. if (idx == LUA_GLOBALSINDEX) {
  159. lj_checkapi(tvistab(f), "stack slot %d is not a table", idx);
  160. /* NOBARRIER: A thread (i.e. L) is never black. */
  161. setgcref(L->env, obj2gco(tabV(f)));
  162. } else if (idx == LUA_ENVIRONINDEX) {
  163. GCfunc *fn = curr_func(L);
  164. if (fn->c.gct != ~LJ_TFUNC)
  165. lj_err_msg(L, LJ_ERR_NOENV);
  166. lj_checkapi(tvistab(f), "stack slot %d is not a table", idx);
  167. setgcref(fn->c.env, obj2gco(tabV(f)));
  168. lj_gc_barrier(L, fn, f);
  169. } else {
  170. TValue *o = index2adr_check(L, idx);
  171. copyTV(L, o, f);
  172. if (idx < LUA_GLOBALSINDEX) /* Need a barrier for upvalues. */
  173. lj_gc_barrier(L, curr_func(L), f);
  174. }
  175. }
  176. LUA_API void lua_replace(lua_State *L, int idx)
  177. {
  178. lj_checkapi_slot(1);
  179. copy_slot(L, L->top - 1, idx);
  180. L->top--;
  181. }
  182. LUA_API void lua_copy(lua_State *L, int fromidx, int toidx)
  183. {
  184. copy_slot(L, index2adr(L, fromidx), toidx);
  185. }
  186. LUA_API void lua_pushvalue(lua_State *L, int idx)
  187. {
  188. copyTV(L, L->top, index2adr(L, idx));
  189. incr_top(L);
  190. }
  191. /* -- Stack getters ------------------------------------------------------- */
  192. LUA_API int lua_type(lua_State *L, int idx)
  193. {
  194. cTValue *o = index2adr(L, idx);
  195. if (tvisnumber(o)) {
  196. return LUA_TNUMBER;
  197. #if LJ_64 && !LJ_GC64
  198. } else if (tvislightud(o)) {
  199. return LUA_TLIGHTUSERDATA;
  200. #endif
  201. } else if (o == niltv(L)) {
  202. return LUA_TNONE;
  203. } else { /* Magic internal/external tag conversion. ORDER LJ_T */
  204. uint32_t t = ~itype(o);
  205. #if LJ_64
  206. int tt = (int)((U64x(75a06,98042110) >> 4*t) & 15u);
  207. #else
  208. int tt = (int)(((t < 8 ? 0x98042110u : 0x75a06u) >> 4*(t&7)) & 15u);
  209. #endif
  210. lj_assertL(tt != LUA_TNIL || tvisnil(o), "bad tag conversion");
  211. return tt;
  212. }
  213. }
  214. LUALIB_API void luaL_checktype(lua_State *L, int idx, int tt)
  215. {
  216. if (lua_type(L, idx) != tt)
  217. lj_err_argt(L, idx, tt);
  218. }
  219. LUALIB_API void luaL_checkany(lua_State *L, int idx)
  220. {
  221. if (index2adr(L, idx) == niltv(L))
  222. lj_err_arg(L, idx, LJ_ERR_NOVAL);
  223. }
  224. LUA_API const char *lua_typename(lua_State *L, int t)
  225. {
  226. UNUSED(L);
  227. return lj_obj_typename[t+1];
  228. }
  229. LUA_API int lua_iscfunction(lua_State *L, int idx)
  230. {
  231. cTValue *o = index2adr(L, idx);
  232. return tvisfunc(o) && !isluafunc(funcV(o));
  233. }
  234. LUA_API int lua_isnumber(lua_State *L, int idx)
  235. {
  236. cTValue *o = index2adr(L, idx);
  237. TValue tmp;
  238. return (tvisnumber(o) || (tvisstr(o) && lj_strscan_number(strV(o), &tmp)));
  239. }
  240. LUA_API int lua_isstring(lua_State *L, int idx)
  241. {
  242. cTValue *o = index2adr(L, idx);
  243. return (tvisstr(o) || tvisnumber(o));
  244. }
  245. LUA_API int lua_isuserdata(lua_State *L, int idx)
  246. {
  247. cTValue *o = index2adr(L, idx);
  248. return (tvisudata(o) || tvislightud(o));
  249. }
  250. LUA_API int lua_rawequal(lua_State *L, int idx1, int idx2)
  251. {
  252. cTValue *o1 = index2adr(L, idx1);
  253. cTValue *o2 = index2adr(L, idx2);
  254. return (o1 == niltv(L) || o2 == niltv(L)) ? 0 : lj_obj_equal(o1, o2);
  255. }
  256. LUA_API int lua_equal(lua_State *L, int idx1, int idx2)
  257. {
  258. cTValue *o1 = index2adr(L, idx1);
  259. cTValue *o2 = index2adr(L, idx2);
  260. if (tvisint(o1) && tvisint(o2)) {
  261. return intV(o1) == intV(o2);
  262. } else if (tvisnumber(o1) && tvisnumber(o2)) {
  263. return numberVnum(o1) == numberVnum(o2);
  264. } else if (itype(o1) != itype(o2)) {
  265. return 0;
  266. } else if (tvispri(o1)) {
  267. return o1 != niltv(L) && o2 != niltv(L);
  268. #if LJ_64 && !LJ_GC64
  269. } else if (tvislightud(o1)) {
  270. return o1->u64 == o2->u64;
  271. #endif
  272. } else if (gcrefeq(o1->gcr, o2->gcr)) {
  273. return 1;
  274. } else if (!tvistabud(o1)) {
  275. return 0;
  276. } else {
  277. TValue *base = lj_meta_equal(L, gcV(o1), gcV(o2), 0);
  278. if ((uintptr_t)base <= 1) {
  279. return (int)(uintptr_t)base;
  280. } else {
  281. L->top = base+2;
  282. lj_vm_call(L, base, 1+1);
  283. L->top -= 2+LJ_FR2;
  284. return tvistruecond(L->top+1+LJ_FR2);
  285. }
  286. }
  287. }
  288. LUA_API int lua_lessthan(lua_State *L, int idx1, int idx2)
  289. {
  290. cTValue *o1 = index2adr(L, idx1);
  291. cTValue *o2 = index2adr(L, idx2);
  292. if (o1 == niltv(L) || o2 == niltv(L)) {
  293. return 0;
  294. } else if (tvisint(o1) && tvisint(o2)) {
  295. return intV(o1) < intV(o2);
  296. } else if (tvisnumber(o1) && tvisnumber(o2)) {
  297. return numberVnum(o1) < numberVnum(o2);
  298. } else {
  299. TValue *base = lj_meta_comp(L, o1, o2, 0);
  300. if ((uintptr_t)base <= 1) {
  301. return (int)(uintptr_t)base;
  302. } else {
  303. L->top = base+2;
  304. lj_vm_call(L, base, 1+1);
  305. L->top -= 2+LJ_FR2;
  306. return tvistruecond(L->top+1+LJ_FR2);
  307. }
  308. }
  309. }
  310. LUA_API lua_Number lua_tonumber(lua_State *L, int idx)
  311. {
  312. cTValue *o = index2adr(L, idx);
  313. TValue tmp;
  314. if (LJ_LIKELY(tvisnumber(o)))
  315. return numberVnum(o);
  316. else if (tvisstr(o) && lj_strscan_num(strV(o), &tmp))
  317. return numV(&tmp);
  318. else
  319. return 0;
  320. }
  321. LUA_API lua_Number lua_tonumberx(lua_State *L, int idx, int *ok)
  322. {
  323. cTValue *o = index2adr(L, idx);
  324. TValue tmp;
  325. if (LJ_LIKELY(tvisnumber(o))) {
  326. if (ok) *ok = 1;
  327. return numberVnum(o);
  328. } else if (tvisstr(o) && lj_strscan_num(strV(o), &tmp)) {
  329. if (ok) *ok = 1;
  330. return numV(&tmp);
  331. } else {
  332. if (ok) *ok = 0;
  333. return 0;
  334. }
  335. }
  336. LUALIB_API lua_Number luaL_checknumber(lua_State *L, int idx)
  337. {
  338. cTValue *o = index2adr(L, idx);
  339. TValue tmp;
  340. if (LJ_LIKELY(tvisnumber(o)))
  341. return numberVnum(o);
  342. else if (!(tvisstr(o) && lj_strscan_num(strV(o), &tmp)))
  343. lj_err_argt(L, idx, LUA_TNUMBER);
  344. return numV(&tmp);
  345. }
  346. LUALIB_API lua_Number luaL_optnumber(lua_State *L, int idx, lua_Number def)
  347. {
  348. cTValue *o = index2adr(L, idx);
  349. TValue tmp;
  350. if (LJ_LIKELY(tvisnumber(o)))
  351. return numberVnum(o);
  352. else if (tvisnil(o))
  353. return def;
  354. else if (!(tvisstr(o) && lj_strscan_num(strV(o), &tmp)))
  355. lj_err_argt(L, idx, LUA_TNUMBER);
  356. return numV(&tmp);
  357. }
  358. LUA_API lua_Integer lua_tointeger(lua_State *L, int idx)
  359. {
  360. cTValue *o = index2adr(L, idx);
  361. TValue tmp;
  362. lua_Number n;
  363. if (LJ_LIKELY(tvisint(o))) {
  364. return intV(o);
  365. } else if (LJ_LIKELY(tvisnum(o))) {
  366. n = numV(o);
  367. } else {
  368. if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp)))
  369. return 0;
  370. if (tvisint(&tmp))
  371. return intV(&tmp);
  372. n = numV(&tmp);
  373. }
  374. #if LJ_64
  375. return (lua_Integer)n;
  376. #else
  377. return lj_num2int(n);
  378. #endif
  379. }
  380. LUA_API lua_Integer lua_tointegerx(lua_State *L, int idx, int *ok)
  381. {
  382. cTValue *o = index2adr(L, idx);
  383. TValue tmp;
  384. lua_Number n;
  385. if (LJ_LIKELY(tvisint(o))) {
  386. if (ok) *ok = 1;
  387. return intV(o);
  388. } else if (LJ_LIKELY(tvisnum(o))) {
  389. n = numV(o);
  390. } else {
  391. if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp))) {
  392. if (ok) *ok = 0;
  393. return 0;
  394. }
  395. if (tvisint(&tmp)) {
  396. if (ok) *ok = 1;
  397. return intV(&tmp);
  398. }
  399. n = numV(&tmp);
  400. }
  401. if (ok) *ok = 1;
  402. #if LJ_64
  403. return (lua_Integer)n;
  404. #else
  405. return lj_num2int(n);
  406. #endif
  407. }
  408. LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int idx)
  409. {
  410. cTValue *o = index2adr(L, idx);
  411. TValue tmp;
  412. lua_Number n;
  413. if (LJ_LIKELY(tvisint(o))) {
  414. return intV(o);
  415. } else if (LJ_LIKELY(tvisnum(o))) {
  416. n = numV(o);
  417. } else {
  418. if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp)))
  419. lj_err_argt(L, idx, LUA_TNUMBER);
  420. if (tvisint(&tmp))
  421. return (lua_Integer)intV(&tmp);
  422. n = numV(&tmp);
  423. }
  424. #if LJ_64
  425. return (lua_Integer)n;
  426. #else
  427. return lj_num2int(n);
  428. #endif
  429. }
  430. LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int idx, lua_Integer def)
  431. {
  432. cTValue *o = index2adr(L, idx);
  433. TValue tmp;
  434. lua_Number n;
  435. if (LJ_LIKELY(tvisint(o))) {
  436. return intV(o);
  437. } else if (LJ_LIKELY(tvisnum(o))) {
  438. n = numV(o);
  439. } else if (tvisnil(o)) {
  440. return def;
  441. } else {
  442. if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp)))
  443. lj_err_argt(L, idx, LUA_TNUMBER);
  444. if (tvisint(&tmp))
  445. return (lua_Integer)intV(&tmp);
  446. n = numV(&tmp);
  447. }
  448. #if LJ_64
  449. return (lua_Integer)n;
  450. #else
  451. return lj_num2int(n);
  452. #endif
  453. }
  454. LUA_API int lua_toboolean(lua_State *L, int idx)
  455. {
  456. cTValue *o = index2adr(L, idx);
  457. return tvistruecond(o);
  458. }
  459. LUA_API const char *lua_tolstring(lua_State *L, int idx, size_t *len)
  460. {
  461. TValue *o = index2adr(L, idx);
  462. GCstr *s;
  463. if (LJ_LIKELY(tvisstr(o))) {
  464. s = strV(o);
  465. } else if (tvisnumber(o)) {
  466. lj_gc_check(L);
  467. o = index2adr(L, idx); /* GC may move the stack. */
  468. s = lj_strfmt_number(L, o);
  469. setstrV(L, o, s);
  470. } else {
  471. if (len != NULL) *len = 0;
  472. return NULL;
  473. }
  474. if (len != NULL) *len = s->len;
  475. return strdata(s);
  476. }
  477. LUALIB_API const char *luaL_checklstring(lua_State *L, int idx, size_t *len)
  478. {
  479. TValue *o = index2adr(L, idx);
  480. GCstr *s;
  481. if (LJ_LIKELY(tvisstr(o))) {
  482. s = strV(o);
  483. } else if (tvisnumber(o)) {
  484. lj_gc_check(L);
  485. o = index2adr(L, idx); /* GC may move the stack. */
  486. s = lj_strfmt_number(L, o);
  487. setstrV(L, o, s);
  488. } else {
  489. lj_err_argt(L, idx, LUA_TSTRING);
  490. }
  491. if (len != NULL) *len = s->len;
  492. return strdata(s);
  493. }
  494. LUALIB_API const char *luaL_optlstring(lua_State *L, int idx,
  495. const char *def, size_t *len)
  496. {
  497. TValue *o = index2adr(L, idx);
  498. GCstr *s;
  499. if (LJ_LIKELY(tvisstr(o))) {
  500. s = strV(o);
  501. } else if (tvisnil(o)) {
  502. if (len != NULL) *len = def ? strlen(def) : 0;
  503. return def;
  504. } else if (tvisnumber(o)) {
  505. lj_gc_check(L);
  506. o = index2adr(L, idx); /* GC may move the stack. */
  507. s = lj_strfmt_number(L, o);
  508. setstrV(L, o, s);
  509. } else {
  510. lj_err_argt(L, idx, LUA_TSTRING);
  511. }
  512. if (len != NULL) *len = s->len;
  513. return strdata(s);
  514. }
  515. LUALIB_API int luaL_checkoption(lua_State *L, int idx, const char *def,
  516. const char *const lst[])
  517. {
  518. ptrdiff_t i;
  519. const char *s = lua_tolstring(L, idx, NULL);
  520. if (s == NULL && (s = def) == NULL)
  521. lj_err_argt(L, idx, LUA_TSTRING);
  522. for (i = 0; lst[i]; i++)
  523. if (strcmp(lst[i], s) == 0)
  524. return (int)i;
  525. lj_err_argv(L, idx, LJ_ERR_INVOPTM, s);
  526. }
  527. LUA_API size_t lua_objlen(lua_State *L, int idx)
  528. {
  529. TValue *o = index2adr(L, idx);
  530. if (tvisstr(o)) {
  531. return strV(o)->len;
  532. } else if (tvistab(o)) {
  533. return (size_t)lj_tab_len(tabV(o));
  534. } else if (tvisudata(o)) {
  535. return udataV(o)->len;
  536. } else if (tvisnumber(o)) {
  537. GCstr *s = lj_strfmt_number(L, o);
  538. setstrV(L, o, s);
  539. return s->len;
  540. } else {
  541. return 0;
  542. }
  543. }
  544. LUA_API lua_CFunction lua_tocfunction(lua_State *L, int idx)
  545. {
  546. cTValue *o = index2adr(L, idx);
  547. if (tvisfunc(o)) {
  548. BCOp op = bc_op(*mref(funcV(o)->c.pc, BCIns));
  549. if (op == BC_FUNCC || op == BC_FUNCCW)
  550. return funcV(o)->c.f;
  551. }
  552. return NULL;
  553. }
  554. LUA_API void *lua_touserdata(lua_State *L, int idx)
  555. {
  556. cTValue *o = index2adr(L, idx);
  557. if (tvisudata(o))
  558. return uddata(udataV(o));
  559. else if (tvislightud(o))
  560. return lightudV(G(L), o);
  561. else
  562. return NULL;
  563. }
  564. LUA_API lua_State *lua_tothread(lua_State *L, int idx)
  565. {
  566. cTValue *o = index2adr(L, idx);
  567. return (!tvisthread(o)) ? NULL : threadV(o);
  568. }
  569. LUA_API const void *lua_topointer(lua_State *L, int idx)
  570. {
  571. return lj_obj_ptr(G(L), index2adr(L, idx));
  572. }
  573. /* -- Stack setters (object creation) ------------------------------------- */
  574. LUA_API void lua_pushnil(lua_State *L)
  575. {
  576. setnilV(L->top);
  577. incr_top(L);
  578. }
  579. LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
  580. {
  581. setnumV(L->top, n);
  582. if (LJ_UNLIKELY(tvisnan(L->top)))
  583. setnanV(L->top); /* Canonicalize injected NaNs. */
  584. incr_top(L);
  585. }
  586. LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
  587. {
  588. setintptrV(L->top, n);
  589. incr_top(L);
  590. }
  591. LUA_API void lua_pushlstring(lua_State *L, const char *str, size_t len)
  592. {
  593. GCstr *s;
  594. lj_gc_check(L);
  595. s = lj_str_new(L, str, len);
  596. setstrV(L, L->top, s);
  597. incr_top(L);
  598. }
  599. LUA_API void lua_pushstring(lua_State *L, const char *str)
  600. {
  601. if (str == NULL) {
  602. setnilV(L->top);
  603. } else {
  604. GCstr *s;
  605. lj_gc_check(L);
  606. s = lj_str_newz(L, str);
  607. setstrV(L, L->top, s);
  608. }
  609. incr_top(L);
  610. }
  611. LUA_API const char *lua_pushvfstring(lua_State *L, const char *fmt,
  612. va_list argp)
  613. {
  614. lj_gc_check(L);
  615. return lj_strfmt_pushvf(L, fmt, argp);
  616. }
  617. LUA_API const char *lua_pushfstring(lua_State *L, const char *fmt, ...)
  618. {
  619. const char *ret;
  620. va_list argp;
  621. lj_gc_check(L);
  622. va_start(argp, fmt);
  623. ret = lj_strfmt_pushvf(L, fmt, argp);
  624. va_end(argp);
  625. return ret;
  626. }
  627. LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction f, int n)
  628. {
  629. GCfunc *fn;
  630. lj_gc_check(L);
  631. lj_checkapi_slot(n);
  632. fn = lj_func_newC(L, (MSize)n, getcurrenv(L));
  633. fn->c.f = f;
  634. L->top -= n;
  635. while (n--)
  636. copyTV(L, &fn->c.upvalue[n], L->top+n);
  637. setfuncV(L, L->top, fn);
  638. lj_assertL(iswhite(obj2gco(fn)), "new GC object is not white");
  639. incr_top(L);
  640. }
  641. LUA_API void lua_pushboolean(lua_State *L, int b)
  642. {
  643. setboolV(L->top, (b != 0));
  644. incr_top(L);
  645. }
  646. LUA_API void lua_pushlightuserdata(lua_State *L, void *p)
  647. {
  648. #if LJ_64
  649. p = lj_lightud_intern(L, p);
  650. #endif
  651. setrawlightudV(L->top, p);
  652. incr_top(L);
  653. }
  654. LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
  655. {
  656. lj_gc_check(L);
  657. settabV(L, L->top, lj_tab_new_ah(L, narray, nrec));
  658. incr_top(L);
  659. }
  660. LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname)
  661. {
  662. GCtab *regt = tabV(registry(L));
  663. TValue *tv = lj_tab_setstr(L, regt, lj_str_newz(L, tname));
  664. if (tvisnil(tv)) {
  665. GCtab *mt = lj_tab_new(L, 0, 1);
  666. settabV(L, tv, mt);
  667. settabV(L, L->top++, mt);
  668. lj_gc_anybarriert(L, regt);
  669. return 1;
  670. } else {
  671. copyTV(L, L->top++, tv);
  672. return 0;
  673. }
  674. }
  675. LUA_API int lua_pushthread(lua_State *L)
  676. {
  677. setthreadV(L, L->top, L);
  678. incr_top(L);
  679. return (mainthread(G(L)) == L);
  680. }
  681. LUA_API lua_State *lua_newthread(lua_State *L)
  682. {
  683. lua_State *L1;
  684. lj_gc_check(L);
  685. L1 = lj_state_new(L);
  686. setthreadV(L, L->top, L1);
  687. incr_top(L);
  688. return L1;
  689. }
  690. LUA_API void *lua_newuserdata(lua_State *L, size_t size)
  691. {
  692. GCudata *ud;
  693. lj_gc_check(L);
  694. if (size > LJ_MAX_UDATA)
  695. lj_err_msg(L, LJ_ERR_UDATAOV);
  696. ud = lj_udata_new(L, (MSize)size, getcurrenv(L));
  697. setudataV(L, L->top, ud);
  698. incr_top(L);
  699. return uddata(ud);
  700. }
  701. LUA_API void lua_concat(lua_State *L, int n)
  702. {
  703. lj_checkapi_slot(n);
  704. if (n >= 2) {
  705. n--;
  706. do {
  707. TValue *top = lj_meta_cat(L, L->top-1, -n);
  708. if (top == NULL) {
  709. L->top -= n;
  710. break;
  711. }
  712. n -= (int)(L->top - top);
  713. L->top = top+2;
  714. lj_vm_call(L, top, 1+1);
  715. L->top -= 1+LJ_FR2;
  716. copyTV(L, L->top-1, L->top+LJ_FR2);
  717. } while (--n > 0);
  718. } else if (n == 0) { /* Push empty string. */
  719. setstrV(L, L->top, &G(L)->strempty);
  720. incr_top(L);
  721. }
  722. /* else n == 1: nothing to do. */
  723. }
  724. /* -- Object getters ------------------------------------------------------ */
  725. LUA_API void lua_gettable(lua_State *L, int idx)
  726. {
  727. cTValue *t = index2adr_check(L, idx);
  728. cTValue *v = lj_meta_tget(L, t, L->top-1);
  729. if (v == NULL) {
  730. L->top += 2;
  731. lj_vm_call(L, L->top-2, 1+1);
  732. L->top -= 2+LJ_FR2;
  733. v = L->top+1+LJ_FR2;
  734. }
  735. copyTV(L, L->top-1, v);
  736. }
  737. LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
  738. {
  739. cTValue *v, *t = index2adr_check(L, idx);
  740. TValue key;
  741. setstrV(L, &key, lj_str_newz(L, k));
  742. v = lj_meta_tget(L, t, &key);
  743. if (v == NULL) {
  744. L->top += 2;
  745. lj_vm_call(L, L->top-2, 1+1);
  746. L->top -= 2+LJ_FR2;
  747. v = L->top+1+LJ_FR2;
  748. }
  749. copyTV(L, L->top, v);
  750. incr_top(L);
  751. }
  752. LUA_API void lua_rawget(lua_State *L, int idx)
  753. {
  754. cTValue *t = index2adr(L, idx);
  755. lj_checkapi(tvistab(t), "stack slot %d is not a table", idx);
  756. copyTV(L, L->top-1, lj_tab_get(L, tabV(t), L->top-1));
  757. }
  758. LUA_API void lua_rawgeti(lua_State *L, int idx, int n)
  759. {
  760. cTValue *v, *t = index2adr(L, idx);
  761. lj_checkapi(tvistab(t), "stack slot %d is not a table", idx);
  762. v = lj_tab_getint(tabV(t), n);
  763. if (v) {
  764. copyTV(L, L->top, v);
  765. } else {
  766. setnilV(L->top);
  767. }
  768. incr_top(L);
  769. }
  770. LUA_API int lua_getmetatable(lua_State *L, int idx)
  771. {
  772. cTValue *o = index2adr(L, idx);
  773. GCtab *mt = NULL;
  774. if (tvistab(o))
  775. mt = tabref(tabV(o)->metatable);
  776. else if (tvisudata(o))
  777. mt = tabref(udataV(o)->metatable);
  778. else
  779. mt = tabref(basemt_obj(G(L), o));
  780. if (mt == NULL)
  781. return 0;
  782. settabV(L, L->top, mt);
  783. incr_top(L);
  784. return 1;
  785. }
  786. LUALIB_API int luaL_getmetafield(lua_State *L, int idx, const char *field)
  787. {
  788. if (lua_getmetatable(L, idx)) {
  789. cTValue *tv = lj_tab_getstr(tabV(L->top-1), lj_str_newz(L, field));
  790. if (tv && !tvisnil(tv)) {
  791. copyTV(L, L->top-1, tv);
  792. return 1;
  793. }
  794. L->top--;
  795. }
  796. return 0;
  797. }
  798. LUA_API void lua_getfenv(lua_State *L, int idx)
  799. {
  800. cTValue *o = index2adr_check(L, idx);
  801. if (tvisfunc(o)) {
  802. settabV(L, L->top, tabref(funcV(o)->c.env));
  803. } else if (tvisudata(o)) {
  804. settabV(L, L->top, tabref(udataV(o)->env));
  805. } else if (tvisthread(o)) {
  806. settabV(L, L->top, tabref(threadV(o)->env));
  807. } else {
  808. setnilV(L->top);
  809. }
  810. incr_top(L);
  811. }
  812. LUA_API int lua_next(lua_State *L, int idx)
  813. {
  814. cTValue *t = index2adr(L, idx);
  815. int more;
  816. lj_checkapi(tvistab(t), "stack slot %d is not a table", idx);
  817. more = lj_tab_next(tabV(t), L->top-1, L->top-1);
  818. if (more > 0) {
  819. incr_top(L); /* Return new key and value slot. */
  820. } else if (!more) { /* End of traversal. */
  821. L->top--; /* Remove key slot. */
  822. } else {
  823. lj_err_msg(L, LJ_ERR_NEXTIDX);
  824. }
  825. return more;
  826. }
  827. LUA_API const char *lua_getupvalue(lua_State *L, int idx, int n)
  828. {
  829. TValue *val;
  830. GCobj *o;
  831. const char *name = lj_debug_uvnamev(index2adr(L, idx), (uint32_t)(n-1), &val, &o);
  832. if (name) {
  833. copyTV(L, L->top, val);
  834. incr_top(L);
  835. }
  836. return name;
  837. }
  838. LUA_API void *lua_upvalueid(lua_State *L, int idx, int n)
  839. {
  840. GCfunc *fn = funcV(index2adr(L, idx));
  841. n--;
  842. lj_checkapi((uint32_t)n < fn->l.nupvalues, "bad upvalue %d", n);
  843. return isluafunc(fn) ? (void *)gcref(fn->l.uvptr[n]) :
  844. (void *)&fn->c.upvalue[n];
  845. }
  846. LUA_API void lua_upvaluejoin(lua_State *L, int idx1, int n1, int idx2, int n2)
  847. {
  848. GCfunc *fn1 = funcV(index2adr(L, idx1));
  849. GCfunc *fn2 = funcV(index2adr(L, idx2));
  850. n1--; n2--;
  851. lj_checkapi(isluafunc(fn1), "stack slot %d is not a Lua function", idx1);
  852. lj_checkapi(isluafunc(fn2), "stack slot %d is not a Lua function", idx2);
  853. lj_checkapi((uint32_t)n1 < fn1->l.nupvalues, "bad upvalue %d", n1+1);
  854. lj_checkapi((uint32_t)n2 < fn2->l.nupvalues, "bad upvalue %d", n2+1);
  855. setgcrefr(fn1->l.uvptr[n1], fn2->l.uvptr[n2]);
  856. lj_gc_objbarrier(L, fn1, gcref(fn1->l.uvptr[n1]));
  857. }
  858. LUALIB_API void *luaL_testudata(lua_State *L, int idx, const char *tname)
  859. {
  860. cTValue *o = index2adr(L, idx);
  861. if (tvisudata(o)) {
  862. GCudata *ud = udataV(o);
  863. cTValue *tv = lj_tab_getstr(tabV(registry(L)), lj_str_newz(L, tname));
  864. if (tv && tvistab(tv) && tabV(tv) == tabref(ud->metatable))
  865. return uddata(ud);
  866. }
  867. return NULL; /* value is not a userdata with a metatable */
  868. }
  869. LUALIB_API void *luaL_checkudata(lua_State *L, int idx, const char *tname)
  870. {
  871. void *p = luaL_testudata(L, idx, tname);
  872. if (!p) lj_err_argtype(L, idx, tname);
  873. return p;
  874. }
  875. /* -- Object setters ------------------------------------------------------ */
  876. LUA_API void lua_settable(lua_State *L, int idx)
  877. {
  878. TValue *o;
  879. cTValue *t = index2adr_check(L, idx);
  880. lj_checkapi_slot(2);
  881. o = lj_meta_tset(L, t, L->top-2);
  882. if (o) {
  883. /* NOBARRIER: lj_meta_tset ensures the table is not black. */
  884. L->top -= 2;
  885. copyTV(L, o, L->top+1);
  886. } else {
  887. TValue *base = L->top;
  888. copyTV(L, base+2, base-3-2*LJ_FR2);
  889. L->top = base+3;
  890. lj_vm_call(L, base, 0+1);
  891. L->top -= 3+LJ_FR2;
  892. }
  893. }
  894. LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
  895. {
  896. TValue *o;
  897. TValue key;
  898. cTValue *t = index2adr_check(L, idx);
  899. lj_checkapi_slot(1);
  900. setstrV(L, &key, lj_str_newz(L, k));
  901. o = lj_meta_tset(L, t, &key);
  902. if (o) {
  903. /* NOBARRIER: lj_meta_tset ensures the table is not black. */
  904. copyTV(L, o, --L->top);
  905. } else {
  906. TValue *base = L->top;
  907. copyTV(L, base+2, base-3-2*LJ_FR2);
  908. L->top = base+3;
  909. lj_vm_call(L, base, 0+1);
  910. L->top -= 2+LJ_FR2;
  911. }
  912. }
  913. LUA_API void lua_rawset(lua_State *L, int idx)
  914. {
  915. GCtab *t = tabV(index2adr(L, idx));
  916. TValue *dst, *key;
  917. lj_checkapi_slot(2);
  918. key = L->top-2;
  919. dst = lj_tab_set(L, t, key);
  920. copyTV(L, dst, key+1);
  921. lj_gc_anybarriert(L, t);
  922. L->top = key;
  923. }
  924. LUA_API void lua_rawseti(lua_State *L, int idx, int n)
  925. {
  926. GCtab *t = tabV(index2adr(L, idx));
  927. TValue *dst, *src;
  928. lj_checkapi_slot(1);
  929. dst = lj_tab_setint(L, t, n);
  930. src = L->top-1;
  931. copyTV(L, dst, src);
  932. lj_gc_barriert(L, t, dst);
  933. L->top = src;
  934. }
  935. LUA_API int lua_setmetatable(lua_State *L, int idx)
  936. {
  937. global_State *g;
  938. GCtab *mt;
  939. cTValue *o = index2adr_check(L, idx);
  940. lj_checkapi_slot(1);
  941. if (tvisnil(L->top-1)) {
  942. mt = NULL;
  943. } else {
  944. lj_checkapi(tvistab(L->top-1), "top stack slot is not a table");
  945. mt = tabV(L->top-1);
  946. }
  947. g = G(L);
  948. if (tvistab(o)) {
  949. setgcref(tabV(o)->metatable, obj2gco(mt));
  950. if (mt)
  951. lj_gc_objbarriert(L, tabV(o), mt);
  952. } else if (tvisudata(o)) {
  953. setgcref(udataV(o)->metatable, obj2gco(mt));
  954. if (mt)
  955. lj_gc_objbarrier(L, udataV(o), mt);
  956. } else {
  957. /* Flush cache, since traces specialize to basemt. But not during __gc. */
  958. if (lj_trace_flushall(L))
  959. lj_err_caller(L, LJ_ERR_NOGCMM);
  960. if (tvisbool(o)) {
  961. /* NOBARRIER: basemt is a GC root. */
  962. setgcref(basemt_it(g, LJ_TTRUE), obj2gco(mt));
  963. setgcref(basemt_it(g, LJ_TFALSE), obj2gco(mt));
  964. } else {
  965. /* NOBARRIER: basemt is a GC root. */
  966. setgcref(basemt_obj(g, o), obj2gco(mt));
  967. }
  968. }
  969. L->top--;
  970. return 1;
  971. }
  972. LUALIB_API void luaL_setmetatable(lua_State *L, const char *tname)
  973. {
  974. lua_getfield(L, LUA_REGISTRYINDEX, tname);
  975. lua_setmetatable(L, -2);
  976. }
  977. LUA_API int lua_setfenv(lua_State *L, int idx)
  978. {
  979. cTValue *o = index2adr_check(L, idx);
  980. GCtab *t;
  981. lj_checkapi_slot(1);
  982. lj_checkapi(tvistab(L->top-1), "top stack slot is not a table");
  983. t = tabV(L->top-1);
  984. if (tvisfunc(o)) {
  985. setgcref(funcV(o)->c.env, obj2gco(t));
  986. } else if (tvisudata(o)) {
  987. setgcref(udataV(o)->env, obj2gco(t));
  988. } else if (tvisthread(o)) {
  989. setgcref(threadV(o)->env, obj2gco(t));
  990. } else {
  991. L->top--;
  992. return 0;
  993. }
  994. lj_gc_objbarrier(L, gcV(o), t);
  995. L->top--;
  996. return 1;
  997. }
  998. LUA_API const char *lua_setupvalue(lua_State *L, int idx, int n)
  999. {
  1000. cTValue *f = index2adr(L, idx);
  1001. TValue *val;
  1002. GCobj *o;
  1003. const char *name;
  1004. lj_checkapi_slot(1);
  1005. name = lj_debug_uvnamev(f, (uint32_t)(n-1), &val, &o);
  1006. if (name) {
  1007. L->top--;
  1008. copyTV(L, val, L->top);
  1009. lj_gc_barrier(L, o, L->top);
  1010. }
  1011. return name;
  1012. }
  1013. /* -- Calls --------------------------------------------------------------- */
  1014. #if LJ_FR2
  1015. static TValue *api_call_base(lua_State *L, int nargs)
  1016. {
  1017. TValue *o = L->top, *base = o - nargs;
  1018. L->top = o+1;
  1019. for (; o > base; o--) copyTV(L, o, o-1);
  1020. setnilV(o);
  1021. return o+1;
  1022. }
  1023. #else
  1024. #define api_call_base(L, nargs) (L->top - (nargs))
  1025. #endif
  1026. LUA_API void lua_call(lua_State *L, int nargs, int nresults)
  1027. {
  1028. lj_checkapi(L->status == LUA_OK || L->status == LUA_ERRERR,
  1029. "thread called in wrong state %d", L->status);
  1030. lj_checkapi_slot(nargs+1);
  1031. lj_vm_call(L, api_call_base(L, nargs), nresults+1);
  1032. }
  1033. LUA_API int lua_pcall(lua_State *L, int nargs, int nresults, int errfunc)
  1034. {
  1035. global_State *g = G(L);
  1036. uint8_t oldh = hook_save(g);
  1037. ptrdiff_t ef;
  1038. int status;
  1039. lj_checkapi(L->status == LUA_OK || L->status == LUA_ERRERR,
  1040. "thread called in wrong state %d", L->status);
  1041. lj_checkapi_slot(nargs+1);
  1042. if (errfunc == 0) {
  1043. ef = 0;
  1044. } else {
  1045. cTValue *o = index2adr_stack(L, errfunc);
  1046. ef = savestack(L, o);
  1047. }
  1048. status = lj_vm_pcall(L, api_call_base(L, nargs), nresults+1, ef);
  1049. if (status) hook_restore(g, oldh);
  1050. return status;
  1051. }
  1052. static TValue *cpcall(lua_State *L, lua_CFunction func, void *ud)
  1053. {
  1054. GCfunc *fn = lj_func_newC(L, 0, getcurrenv(L));
  1055. TValue *top = L->top;
  1056. fn->c.f = func;
  1057. setfuncV(L, top++, fn);
  1058. if (LJ_FR2) setnilV(top++);
  1059. #if LJ_64
  1060. ud = lj_lightud_intern(L, ud);
  1061. #endif
  1062. setrawlightudV(top++, ud);
  1063. cframe_nres(L->cframe) = 1+0; /* Zero results. */
  1064. L->top = top;
  1065. return top-1; /* Now call the newly allocated C function. */
  1066. }
  1067. LUA_API int lua_cpcall(lua_State *L, lua_CFunction func, void *ud)
  1068. {
  1069. global_State *g = G(L);
  1070. uint8_t oldh = hook_save(g);
  1071. int status;
  1072. lj_checkapi(L->status == LUA_OK || L->status == LUA_ERRERR,
  1073. "thread called in wrong state %d", L->status);
  1074. status = lj_vm_cpcall(L, func, ud, cpcall);
  1075. if (status) hook_restore(g, oldh);
  1076. return status;
  1077. }
  1078. LUALIB_API int luaL_callmeta(lua_State *L, int idx, const char *field)
  1079. {
  1080. if (luaL_getmetafield(L, idx, field)) {
  1081. TValue *top = L->top--;
  1082. if (LJ_FR2) setnilV(top++);
  1083. copyTV(L, top++, index2adr(L, idx));
  1084. L->top = top;
  1085. lj_vm_call(L, top-1, 1+1);
  1086. return 1;
  1087. }
  1088. return 0;
  1089. }
  1090. /* -- Coroutine yield and resume ------------------------------------------ */
  1091. LUA_API int lua_isyieldable(lua_State *L)
  1092. {
  1093. return cframe_canyield(L->cframe);
  1094. }
  1095. LUA_API int lua_yield(lua_State *L, int nresults)
  1096. {
  1097. void *cf = L->cframe;
  1098. global_State *g = G(L);
  1099. if (cframe_canyield(cf)) {
  1100. cf = cframe_raw(cf);
  1101. if (!hook_active(g)) { /* Regular yield: move results down if needed. */
  1102. cTValue *f = L->top - nresults;
  1103. if (f > L->base) {
  1104. TValue *t = L->base;
  1105. while (--nresults >= 0) copyTV(L, t++, f++);
  1106. L->top = t;
  1107. }
  1108. L->cframe = NULL;
  1109. L->status = LUA_YIELD;
  1110. return -1;
  1111. } else { /* Yield from hook: add a pseudo-frame. */
  1112. TValue *top = L->top;
  1113. hook_leave(g);
  1114. (top++)->u64 = cframe_multres(cf);
  1115. setcont(top, lj_cont_hook);
  1116. if (LJ_FR2) top++;
  1117. setframe_pc(top, cframe_pc(cf)-1);
  1118. top++;
  1119. setframe_gc(top, obj2gco(L), LJ_TTHREAD);
  1120. if (LJ_FR2) top++;
  1121. setframe_ftsz(top, ((char *)(top+1)-(char *)L->base)+FRAME_CONT);
  1122. L->top = L->base = top+1;
  1123. #if ((defined(__GNUC__) || defined(__clang__)) && (LJ_TARGET_X64 || defined(LUAJIT_UNWIND_EXTERNAL)) && !LJ_NO_UNWIND) || LJ_TARGET_WINDOWS
  1124. lj_err_throw(L, LUA_YIELD);
  1125. #else
  1126. L->cframe = NULL;
  1127. L->status = LUA_YIELD;
  1128. lj_vm_unwind_c(cf, LUA_YIELD);
  1129. #endif
  1130. }
  1131. }
  1132. lj_err_msg(L, LJ_ERR_CYIELD);
  1133. return 0; /* unreachable */
  1134. }
  1135. LUA_API int lua_resume(lua_State *L, int nargs)
  1136. {
  1137. if (L->cframe == NULL && L->status <= LUA_YIELD)
  1138. return lj_vm_resume(L,
  1139. L->status == LUA_OK ? api_call_base(L, nargs) : L->top - nargs,
  1140. 0, 0);
  1141. L->top = L->base;
  1142. setstrV(L, L->top, lj_err_str(L, LJ_ERR_COSUSP));
  1143. incr_top(L);
  1144. return LUA_ERRRUN;
  1145. }
  1146. /* -- GC and memory management -------------------------------------------- */
  1147. LUA_API int lua_gc(lua_State *L, int what, int data)
  1148. {
  1149. global_State *g = G(L);
  1150. int res = 0;
  1151. switch (what) {
  1152. case LUA_GCSTOP:
  1153. g->gc.threshold = LJ_MAX_MEM;
  1154. break;
  1155. case LUA_GCRESTART:
  1156. g->gc.threshold = data == -1 ? (g->gc.total/100)*g->gc.pause : g->gc.total;
  1157. break;
  1158. case LUA_GCCOLLECT:
  1159. lj_gc_fullgc(L);
  1160. break;
  1161. case LUA_GCCOUNT:
  1162. res = (int)(g->gc.total >> 10);
  1163. break;
  1164. case LUA_GCCOUNTB:
  1165. res = (int)(g->gc.total & 0x3ff);
  1166. break;
  1167. case LUA_GCSTEP: {
  1168. GCSize a = (GCSize)data << 10;
  1169. g->gc.threshold = (a <= g->gc.total) ? (g->gc.total - a) : 0;
  1170. while (g->gc.total >= g->gc.threshold)
  1171. if (lj_gc_step(L) > 0) {
  1172. res = 1;
  1173. break;
  1174. }
  1175. break;
  1176. }
  1177. case LUA_GCSETPAUSE:
  1178. res = (int)(g->gc.pause);
  1179. g->gc.pause = (MSize)data;
  1180. break;
  1181. case LUA_GCSETSTEPMUL:
  1182. res = (int)(g->gc.stepmul);
  1183. g->gc.stepmul = (MSize)data;
  1184. break;
  1185. case LUA_GCISRUNNING:
  1186. res = (g->gc.threshold != LJ_MAX_MEM);
  1187. break;
  1188. default:
  1189. res = -1; /* Invalid option. */
  1190. }
  1191. return res;
  1192. }
  1193. LUA_API lua_Alloc lua_getallocf(lua_State *L, void **ud)
  1194. {
  1195. global_State *g = G(L);
  1196. if (ud) *ud = g->allocd;
  1197. return g->allocf;
  1198. }
  1199. LUA_API void lua_setallocf(lua_State *L, lua_Alloc f, void *ud)
  1200. {
  1201. global_State *g = G(L);
  1202. g->allocd = ud;
  1203. g->allocf = f;
  1204. }