Skip to content

Commit

Permalink
LuaPlugin: LuaJit support
Browse files Browse the repository at this point in the history
  • Loading branch information
uccold authored and mrianura committed Feb 8, 2023
1 parent fc0b04b commit 5443715
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 4 deletions.
13 changes: 12 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ jobs:
- uses: actions/checkout@v3

- name: Install Dependencies
run: C:\vcpkg\vcpkg install freetype[core] sdl2[core,vulkan] lua[core]
run: C:\vcpkg\vcpkg install freetype[core] sdl2[core,vulkan] lua[core] luajit

- name: Create Build Environment
run: cmake -E make_directory ${{github.workspace}}/Build
Expand All @@ -118,10 +118,21 @@ jobs:
cmake $env:GITHUB_WORKSPACE -A x64 -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=$env:BUILD_TYPE -DBUILD_LUA_BINDINGS=ON -DBUILD_SAMPLES=ON -DWARNINGS_AS_ERRORS=ON
-DCMAKE_TOOLCHAIN_FILE="C:/vcpkg/scripts/buildsystems/vcpkg.cmake"
${{ matrix.cmake_options }}
- name: Configure luajit CMake
working-directory: ${{github.workspace}}/BuildLuajit
run: >-
cmake $env:GITHUB_WORKSPACE -A x64 -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=$env:BUILD_TYPE -DBUILD_LUA_BINDINGS=ON -DBUILD_SAMPLES=ON -DWARNINGS_AS_ERRORS=ON -DBUILD_USE_LUAJITBUILD_LUA_BINDINGS_FOR_LUAJIT=ON
-DCMAKE_TOOLCHAIN_FILE="C:/vcpkg/scripts/buildsystems/vcpkg.cmake"
${{ matrix.cmake_options }}
- name: Build
working-directory: ${{github.workspace}}/Build
run: cmake --build . --config $env:BUILD_TYPE

- name: BuildLuajit
working-directory: ${{github.workspace}}/BuildLuajit
run: cmake --build . --config $env:BUILD_TYPE


Emscripten:
Expand Down
16 changes: 16 additions & 0 deletions CMake/FindLuaJIT.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Try to find the lua library
# LUAJIT_FOUND - system has lua
# LUAJIT_INCLUDE_DIR - the lua include directory
# LUAJIT_LIBRARY - the lua library

FIND_PATH(LUAJIT_INCLUDE_DIR NAMES luajit.h PATH_SUFFIXES luajit luajit-2.0 luajit-2.1)
SET(_LUAJIT_STATIC_LIBS libluajit-5.1.a libluajit.a liblua51.a)
SET(_LUAJIT_SHARED_LIBS luajit-5.1 luajit lua51)
IF(USE_STATIC_LIBS)
FIND_LIBRARY(LUAJIT_LIBRARY NAMES ${_LUAJIT_STATIC_LIBS} ${_LUAJIT_SHARED_LIBS})
ELSE()
FIND_LIBRARY(LUAJIT_LIBRARY NAMES ${_LUAJIT_SHARED_LIBS} ${_LUAJIT_STATIC_LIBS})
ENDIF()
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(LuaJIT DEFAULT_MSG LUAJIT_LIBRARY LUAJIT_INCLUDE_DIR)
MARK_AS_ADVANCED(LUAJIT_LIBRARY LUAJIT_INCLUDE_DIR)
16 changes: 13 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ endif(NOT IOS)

option(BUILD_LUA_BINDINGS "Build Lua bindings" OFF)

if (BUILD_LUA_BINDINGS)
option(BUILD_LUA_BINDINGS_FOR_LUAJIT "Build Lua bindings using luajit" OFF)
endif()

if(APPLE)
option(BUILD_FRAMEWORK "Build Framework bundle for OSX" OFF)
endif()
Expand Down Expand Up @@ -356,9 +360,15 @@ endif()

# Lua
if(BUILD_LUA_BINDINGS)
find_package(Lua REQUIRED)
list(APPEND LUA_BINDINGS_INCLUDE_DIRS ${LUA_INCLUDE_DIR})
list(APPEND LUA_BINDINGS_LINK_LIBS ${LUA_LIBRARIES})
if(BUILD_USE_LUAJIT)
find_package(LuaJIT REQUIRED)
list(APPEND LUA_BINDINGS_INCLUDE_DIRS ${LUAJIT_INCLUDE_DIR})
list(APPEND LUA_BINDINGS_LINK_LIBS ${LUAJIT_LIBRARY})
else()
find_package(Lua REQUIRED)
list(APPEND LUA_BINDINGS_INCLUDE_DIRS ${LUA_INCLUDE_DIR})
list(APPEND LUA_BINDINGS_LINK_LIBS ${LUA_LIBRARIES})
endif()
endif()

# rlottie
Expand Down
16 changes: 16 additions & 0 deletions Include/RmlUi/Lua/Utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@
namespace Rml {
namespace Lua {

#if LUA_VERSION_NUM < 502
#define lua_setuservalue(L, i) \
(luaL_checktype((L), -1, LUA_TTABLE), lua_setfenv((L), (i)))

inline int lua_absindex(lua_State* L, int idx)
{
if (idx > LUA_REGISTRYINDEX && idx < 0)
return lua_gettop(L) + idx + 1;
else
return idx;
}

void lua_len (lua_State *L, int i);
lua_Integer luaL_len(lua_State *L, int i);
#endif

/** casts the variant to its specific type before pushing it to the stack
@relates LuaType */
void RMLUILUA_API PushVariant(lua_State* L, const Variant* var);
Expand Down
33 changes: 33 additions & 0 deletions Source/Lua/Utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,39 @@
namespace Rml {
namespace Lua {

#if LUA_VERSION_NUM < 502
void lua_len (lua_State *L, int i) {
switch (lua_type(L, i)) {
case LUA_TSTRING:
lua_pushnumber(L, (lua_Number)lua_objlen(L, i));
break;
case LUA_TTABLE:
if (!luaL_callmeta(L, i, "__len"))
lua_pushnumber(L, (lua_Number)lua_objlen(L, i));
break;
case LUA_TUSERDATA:
if (luaL_callmeta(L, i, "__len"))
break;
/* FALLTHROUGH */
default:
luaL_error(L, "attempt to get length of a %s value",
lua_typename(L, lua_type(L, i)));
}
}

lua_Integer luaL_len(lua_State *L, int i) {
lua_Integer res = 0;
int isnum = 0;
luaL_checkstack(L, 1, "not enough stack slots");
lua_len(L, i);
res = lua_tointegerx(L, -1, &isnum);
lua_pop(L, 1);
if (!isnum)
luaL_error(L, "object length is not an integer");
return res;
}
#endif

void PushVariant(lua_State* L, const Variant* var)
{
if (!var)
Expand Down

0 comments on commit 5443715

Please sign in to comment.