lj_api.c 32 KB

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