From 1a76c4167eef6ea00e82c61dbdb39bb77a2f2a73 Mon Sep 17 00:00:00 2001 From: Nameless Date: Mon, 7 Oct 2024 08:55:11 -0500 Subject: [PATCH] enforce lua version compatibility --- CMakeLists.txt | 14 ++++++++++++++ src/luvi.h | 3 +++ src/main.c | 11 +++++++++++ 3 files changed, 28 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index b7f9661d..0b0007d3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -71,6 +71,20 @@ if (WithSharedLibluv) find_package(Libuv REQUIRED) include_directories(${LIBUV_INCLUDE_DIRS}) + include(CheckCSourceCompiles) + + set(CMAKE_REQUIRED_INCLUDES ${LUAJIT_INCLUDE_DIRS}) + set(CMAKE_REQUIRED_LIBRARIES ${LUAJIT_LIBRARIES}) + check_c_source_compiles(" + #include + int main() { + LUAJIT_VERSION_SYM(); + return 0; + }" LUAJIT_HAS_VERSION_SYM) + if (NOT LUAJIT_HAS_VERSION_SYM) + add_compile_definitions(LUAJIT_MISSING_VERSION_SYM) + endif () + list(APPEND LUVI_LIBRARIES ${LUV_LIBRARIES} ${LUAJIT_LIBRARIES} ${LIBUV_LIBRARIES}) else (WithSharedLibluv) # Build luv as static library instead of as module diff --git a/src/luvi.h b/src/luvi.h index 8f39a9f7..b5ab8136 100644 --- a/src/luvi.h +++ b/src/luvi.h @@ -22,6 +22,9 @@ #include "lauxlib.h" #include "uv.h" #include "luv.h" +#ifndef WITH_PLAIN_LUA +#include "luajit.h" +#endif #include #include diff --git a/src/main.c b/src/main.c index 43a0a50e..33297161 100644 --- a/src/main.c +++ b/src/main.c @@ -55,6 +55,17 @@ static lua_State* vm_acquire(){ if (L == NULL) return L; + // Ensure that the version of lua interpreter is compatible with the version luvi was compiled with. +#ifdef WITH_PLAIN_LUA +#if (LUA_VERSION_NUM >= 502) + luaL_checkversion(L); +#endif +#else +#ifndef LUAJIT_MISSING_VERSION_SYM // debian patches luajit to remove this symbol, so we can't check it. + LUAJIT_VERSION_SYM(); +#endif +#endif + // Add in the lua standard and compat libraries luvi_openlibs(L);