Skip to content

Commit

Permalink
Features(Lualib/Lua(Jit)): Lua Version and Static Linking Support
Browse files Browse the repository at this point in the history
### Eluna Module Updates: Lua Version and Static Linking Support

Recent updates to the **Eluna** module in **AzerothCore** introduce new configuration options to manage Lua versions and static linking:

- **`-DLUA_VERSION=luajit/lua52/lua53/lua54`**: Select the Lua version for the module, supporting LuaJIT, Lua 5.2, 5.3, or 5.4.
- **`-DLUA_STATIC=on/off`**: Configure whether Lua should be linked statically or dynamically.

These options allow flexible Lua integration, building on Eluna's existing capabilities to handle different Lua versions. The changes also improve compatibility and performance by enabling static linking where needed.

#### Integration with Eluna

This functionality extends Eluna's existing support for Lua compatibility, mainly in **ElunaCompat.cpp**, which handles Lua version-specific interactions. These updates enhance Eluna’s adaptability for various Lua configurations in **AzerothCore**.
  • Loading branch information
iThorgrim committed Jan 9, 2025
1 parent 41c940e commit 9841fa1
Show file tree
Hide file tree
Showing 68 changed files with 468 additions and 20,321 deletions.
25 changes: 25 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
set(LUA_VERSION "lua52" CACHE STRING "Lua version to use")
set_property(CACHE LUA_VERSION PROPERTY STRINGS luajit lua51 lua52 lua53 lua54)
MESSAGE(STATUS "Lua version: ${LUA_VERSION}")

# Avoid warning about DOWNLOAD_EXTRACT_TIMESTAMP in CMake 3.24:
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
cmake_policy(SET CMP0135 NEW)
endif()

option(LUA_STATIC "link lua statically" OFF)
if (LUA_STATIC)
MESSAGE(STATUS "Lua linking: static")
else()
MESSAGE(STATUS "Lua linking: dynamic")
endif()

if (LUA_VERSION MATCHES "luajit")
add_subdirectory(src/lualib/luajit)
set(LUAJIT_VERSION true)
else()
add_subdirectory(src/lualib/lua)
set(LUAJIT_VERSION false)
endif()

set_target_properties(lualib PROPERTIES INTERFACE_COMPILE_DEFINITIONS LUAJIT_VERSION)
88 changes: 88 additions & 0 deletions src/LuaEngine/ElunaCompat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (C) 2010 - 2024 Eluna Lua Engine <https://elunaluaengine.github.io/>
* This program is free software licensed under GPL version 3
* Please see the included DOCS/LICENSE.md for more information
*/

#include "ElunaCompat.h"

#if LUA_VERSION_NUM == 501
const char* luaL_tolstring(lua_State* L, int idx, size_t* len) {
if (!luaL_callmeta(L, idx, "__tostring")) {
int t = lua_type(L, idx), tt = 0;
char const* name = NULL;
switch (t) {
case LUA_TNIL:
lua_pushliteral(L, "nil");
break;
case LUA_TSTRING:
case LUA_TNUMBER:
lua_pushvalue(L, idx);
break;
case LUA_TBOOLEAN:
if (lua_toboolean(L, idx))
lua_pushliteral(L, "true");
else
lua_pushliteral(L, "false");
break;
default:
tt = luaL_getmetafield(L, idx, "__name");
name = (tt == LUA_TSTRING) ? lua_tostring(L, -1) : lua_typename(L, t);
lua_pushfstring(L, "%s: %p", name, lua_topointer(L, idx));
if (tt != LUA_TNIL)
lua_replace(L, -2);
break;
}
}
else {
if (!lua_isstring(L, -1))
luaL_error(L, "'__tostring' must return a string");
}
return lua_tolstring(L, -1, len);
}

int luaL_getsubtable(lua_State* L, int i, const char* name) {
int abs_i = lua_absindex(L, i);
luaL_checkstack(L, 3, "not enough stack slots");
lua_pushstring(L, name);
lua_gettable(L, abs_i);
if (lua_istable(L, -1))
return 1;
lua_pop(L, 1);
lua_newtable(L);
lua_pushstring(L, name);
lua_pushvalue(L, -2);
lua_settable(L, abs_i);
return 0;
}

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

#if !defined LUAJIT_VERSION
void* luaL_testudata(lua_State* L, int index, const char* tname) {
void* ud = lua_touserdata(L, index);
if (ud)
{
if (lua_getmetatable(L, index))
{
luaL_getmetatable(L, tname);
if (!lua_rawequal(L, -1, -2))
ud = NULL;
lua_pop(L, 2);
return ud;
}
}
return NULL;
}

void luaL_setmetatable(lua_State* L, const char* tname) {
lua_pushstring(L, tname);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2);
}
#endif
#endif
43 changes: 43 additions & 0 deletions src/LuaEngine/ElunaCompat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2010 - 2024 Eluna Lua Engine <https://elunaluaengine.github.io/>
* This program is free software licensed under GPL version 3
* Please see the included DOCS/LICENSE.md for more information
*/

#ifndef ELUNACOMPAT_H
#define ELUNACOMPAT_H

extern "C"
{
#include "lua.h"
#include "lauxlib.h"
};

/* Compatibility layer for compiling with Lua 5.1 or LuaJIT */
#if LUA_VERSION_NUM == 501
int luaL_getsubtable(lua_State* L, int i, const char* name);
const char* luaL_tolstring(lua_State* L, int idx, size_t* len);
int lua_absindex(lua_State* L, int i);
#define lua_pushglobaltable(L) \
lua_pushvalue((L), LUA_GLOBALSINDEX)
#define lua_rawlen(L, idx) \
lua_objlen(L, idx)
#define lua_pushunsigned(L, u) \
lua_pushinteger(L, u)
#define lua_load(L, buf_read, dec_buf, str, NULL) \
lua_load(L, buf_read, dec_buf, str)

#if !defined LUAJIT_VERSION
void* luaL_testudata(lua_State* L, int index, const char* tname);
void luaL_setmetatable(lua_State* L, const char* tname);
#define luaL_setfuncs(L, l, n) luaL_register(L, NULL, l)
#endif
#endif

#if LUA_VERSION_NUM > 502
#define lua_dump(L, writer, data) \
lua_dump(L, writer, data, 0)
#define lua_pushunsigned(L, u) \
lua_pushinteger(L, u)
#endif
#endif
1 change: 1 addition & 0 deletions src/LuaEngine/ElunaTemplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extern "C"
#include "lauxlib.h"
};
#include "LuaEngine.h"
#include "ElunaCompat.h"
#include "ElunaUtility.h"
#include "SharedDefines.h"

Expand Down
1 change: 1 addition & 0 deletions src/LuaEngine/LuaEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "LuaEngine.h"
#include "BindingMap.h"
#include "Chat.h"
#include "ElunaCompat.h"
#include "ElunaEventMgr.h"
#include "ElunaIncludes.h"
#include "ElunaTemplate.h"
Expand Down
1 change: 1 addition & 0 deletions src/LuaEngine/lmarshal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <stdlib.h>
#include <string.h>
#include <cstdint>
#include "ElunaCompat.h"

extern "C" {
#include "lua.h"
Expand Down
34 changes: 0 additions & 34 deletions src/lualib/CMakeLists.txt

This file was deleted.

Loading

0 comments on commit 9841fa1

Please sign in to comment.