fix lua::bytearray_as_string

This commit is contained in:
MihailRis 2026-05-24 14:48:00 +03:00
parent eb453d8170
commit 3f81494d4b
2 changed files with 30 additions and 7 deletions

View file

@ -17,6 +17,11 @@
#error LuaJIT required
#endif
#ifndef LUA_TCDATA
// unsafe, use for filtering only
#define LUA_TCDATA 10
#endif
namespace lua {
class luaerror : public std::runtime_error {
public:

View file

@ -6,6 +6,7 @@
#include "util/stringutil.hpp"
#include "engine/Engine.hpp"
#include "debug/Logger.hpp"
using namespace lua;
@ -13,6 +14,8 @@ static int next_environment = 1;
std::unordered_map<std::type_index, std::string> lua::usertypeNames;
static debug::Logger logger("lua-util");
int lua::userdata_destructor(lua::State* L) {
if (auto obj = touserdata<Userdata>(L, 1)) {
obj->~Userdata();
@ -371,10 +374,23 @@ void lua::remove_environment(State* L, int id) {
store_env(L, id);
}
static inline std::string_view bytearray_as_string_indirect(lua::State* L, int idx) {
requireglobal(L, "Bytearray_as_string");
pushvalue(L, -2);
call(L, 1, 1);
auto view = tolstring(L, -1);
pop(L, 2);
return view;
}
std::string_view lua::bytearray_as_string(lua::State* L, int idx) {
const auto& settings = scripting::engine->getSettings();
if (type(L, idx) == LUA_TSTRING) {
int luaType = type(L, idx);
if (luaType == LUA_TSTRING) {
return tolstring(L, idx);
} else if (luaType == LUA_TTABLE) {
return bytearray_as_string_indirect(L, idx);
}
pushvalue(L, idx);
@ -383,16 +399,18 @@ std::string_view lua::bytearray_as_string(lua::State* L, int idx) {
pushvalue(L, -2);
call(L, 1, 2);
auto view = tolstring(L, -2);
if (view == "0") {
if (luaType == LUA_TCDATA) {
logger.error() << "FFI-based Bytearray_as_ptr returned null-pointer";
}
pop(L, 2);
return bytearray_as_string_indirect(L, idx);
}
uint64_t size = touinteger(L, -1);
auto ptr = (const char*)std::stoull(std::string(view), nullptr, 16);
pop(L, 3);
return std::string_view(ptr, size);
} else {
requireglobal(L, "Bytearray_as_string");
pushvalue(L, -2);
call(L, 1, 1);
auto view = tolstring(L, -1);
pop(L, 2);
return view;
return bytearray_as_string_indirect(L, idx);
}
}