From 50d6883e6027c4c2f9a5e495fee6b7fff1bd73c9 Mon Sep 17 00:00:00 2001 From: Mike Pall Date: Tue, 1 Jun 2021 01:01:15 +0200 Subject: [PATCH] Abstract out on-demand loading of FFI library. --- src/lib_jit.c | 6 +----- src/lj_bcread.c | 6 +----- src/lj_ctype.h | 10 ++++++++++ src/lj_lex.c | 6 +----- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/lib_jit.c b/src/lib_jit.c index 12f51d75..817c2967 100644 --- a/src/lib_jit.c +++ b/src/lib_jit.c @@ -346,11 +346,7 @@ LJLIB_CF(jit_util_tracek) ir = &T->ir[ir->op1]; } #if LJ_HASFFI - if (ir->o == IR_KINT64 && !ctype_ctsG(G(L))) { - ptrdiff_t oldtop = savestack(L, L->top); - luaopen_ffi(L); /* Load FFI library on-demand. */ - L->top = restorestack(L, oldtop); - } + if (ir->o == IR_KINT64) ctype_loadffi(L); #endif lj_ir_kvalue(L, L->top-2, ir); setintV(L->top-1, (int32_t)irt_type(ir->t)); diff --git a/src/lj_bcread.c b/src/lj_bcread.c index 28795e3c..298e6c45 100644 --- a/src/lj_bcread.c +++ b/src/lj_bcread.c @@ -399,11 +399,7 @@ static int bcread_header(LexState *ls) if ((flags & BCDUMP_F_FFI)) { #if LJ_HASFFI lua_State *L = ls->L; - if (!ctype_ctsG(G(L))) { - ptrdiff_t oldtop = savestack(L, L->top); - luaopen_ffi(L); /* Load FFI library on-demand. */ - L->top = restorestack(L, oldtop); - } + ctype_loadffi(L); #else return 0; #endif diff --git a/src/lj_ctype.h b/src/lj_ctype.h index 49ec71da..9589ef2a 100644 --- a/src/lj_ctype.h +++ b/src/lj_ctype.h @@ -389,6 +389,16 @@ static LJ_AINLINE CTState *ctype_cts(lua_State *L) return cts; } +/* Load FFI library on-demand. */ +#define ctype_loadffi(L) \ + do { \ + if (!ctype_ctsG(G(L))) { \ + ptrdiff_t oldtop = (char *)L->top - mref(L->stack, char); \ + luaopen_ffi(L); \ + L->top = (TValue *)(mref(L->stack, char) + oldtop); \ + } \ + } while (0) + /* Save and restore state of C type table. */ #define LJ_CTYPE_SAVE(cts) CTState savects_ = *(cts) #define LJ_CTYPE_RESTORE(cts) \ diff --git a/src/lj_lex.c b/src/lj_lex.c index f954b428..cc6fa533 100644 --- a/src/lj_lex.c +++ b/src/lj_lex.c @@ -118,11 +118,7 @@ static void lex_number(LexState *ls, TValue *tv) GCcdata *cd; lj_assertLS(fmt == STRSCAN_I64 || fmt == STRSCAN_U64 || fmt == STRSCAN_IMAG, "unexpected number format %d", fmt); - if (!ctype_ctsG(G(L))) { - ptrdiff_t oldtop = savestack(L, L->top); - luaopen_ffi(L); /* Load FFI library on-demand. */ - L->top = restorestack(L, oldtop); - } + ctype_loadffi(L); if (fmt == STRSCAN_IMAG) { cd = lj_cdata_new_(L, CTID_COMPLEX_DOUBLE, 2*sizeof(double)); ((double *)cdataptr(cd))[0] = 0;