mirror of
https://github.com/azerothcore/mod-ale
synced 2025-11-29 15:38:17 +08:00
Features(Lualib/Lua(Jit)): Lua Version and Static Linking Support (#209)
This commit is contained in:
88
src/LuaEngine/ElunaCompat.cpp
Normal file
88
src/LuaEngine/ElunaCompat.cpp
Normal file
@@ -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
src/LuaEngine/ElunaCompat.h
Normal file
43
src/LuaEngine/ElunaCompat.h
Normal file
@@ -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
|
||||
@@ -14,6 +14,7 @@ extern "C"
|
||||
#include "lauxlib.h"
|
||||
};
|
||||
#include "LuaEngine.h"
|
||||
#include "ElunaCompat.h"
|
||||
#include "ElunaUtility.h"
|
||||
#include "SharedDefines.h"
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <cstdint>
|
||||
#include "ElunaCompat.h"
|
||||
|
||||
extern "C" {
|
||||
#include "lua.h"
|
||||
|
||||
Reference in New Issue
Block a user