[Tarantool-patches] [PATCH luajit 1/6] Abstract out on-demand loading of FFI library.

Sergey Kaplun skaplun at tarantool.org
Mon Oct 23 12:22:01 MSK 2023


From: Mike Pall <mike>

(cherry-picked from commit 50d6883e6027c4c2f9a5e495fee6b7fff1bd73c9)

This patch introduces the macro `ctype_loadffi(L)` to avoid code
duplication while loading the FFI library.

Sergey Kaplun:
* added the description

Part of tarantool/tarantool#9145
---
 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 b3c1c93c..f705f334 100644
--- a/src/lib_jit.c
+++ b/src/lib_jit.c
@@ -339,11 +339,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 cddf6ff1..4915240f 100644
--- a/src/lj_bcread.c
+++ b/src/lj_bcread.c
@@ -414,11 +414,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 c4f3bdde..fce29409 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 cef3c683..7c6319cc 100644
--- a/src/lj_lex.c
+++ b/src/lj_lex.c
@@ -112,11 +112,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;
-- 
2.42.0



More information about the Tarantool-patches mailing list