mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-13 06:51:51 +00:00
fix lua::bytearray_as_string
This commit is contained in:
parent
eb453d8170
commit
3f81494d4b
2 changed files with 30 additions and 7 deletions
|
|
@ -17,6 +17,11 @@
|
||||||
#error LuaJIT required
|
#error LuaJIT required
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef LUA_TCDATA
|
||||||
|
// unsafe, use for filtering only
|
||||||
|
#define LUA_TCDATA 10
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace lua {
|
namespace lua {
|
||||||
class luaerror : public std::runtime_error {
|
class luaerror : public std::runtime_error {
|
||||||
public:
|
public:
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
#include "util/stringutil.hpp"
|
#include "util/stringutil.hpp"
|
||||||
#include "engine/Engine.hpp"
|
#include "engine/Engine.hpp"
|
||||||
|
#include "debug/Logger.hpp"
|
||||||
|
|
||||||
using namespace lua;
|
using namespace lua;
|
||||||
|
|
||||||
|
|
@ -13,6 +14,8 @@ static int next_environment = 1;
|
||||||
|
|
||||||
std::unordered_map<std::type_index, std::string> lua::usertypeNames;
|
std::unordered_map<std::type_index, std::string> lua::usertypeNames;
|
||||||
|
|
||||||
|
static debug::Logger logger("lua-util");
|
||||||
|
|
||||||
int lua::userdata_destructor(lua::State* L) {
|
int lua::userdata_destructor(lua::State* L) {
|
||||||
if (auto obj = touserdata<Userdata>(L, 1)) {
|
if (auto obj = touserdata<Userdata>(L, 1)) {
|
||||||
obj->~Userdata();
|
obj->~Userdata();
|
||||||
|
|
@ -371,10 +374,23 @@ void lua::remove_environment(State* L, int id) {
|
||||||
store_env(L, 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) {
|
std::string_view lua::bytearray_as_string(lua::State* L, int idx) {
|
||||||
const auto& settings = scripting::engine->getSettings();
|
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);
|
return tolstring(L, idx);
|
||||||
|
} else if (luaType == LUA_TTABLE) {
|
||||||
|
return bytearray_as_string_indirect(L, idx);
|
||||||
}
|
}
|
||||||
pushvalue(L, idx);
|
pushvalue(L, idx);
|
||||||
|
|
||||||
|
|
@ -383,16 +399,18 @@ std::string_view lua::bytearray_as_string(lua::State* L, int idx) {
|
||||||
pushvalue(L, -2);
|
pushvalue(L, -2);
|
||||||
call(L, 1, 2);
|
call(L, 1, 2);
|
||||||
auto view = tolstring(L, -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);
|
uint64_t size = touinteger(L, -1);
|
||||||
auto ptr = (const char*)std::stoull(std::string(view), nullptr, 16);
|
auto ptr = (const char*)std::stoull(std::string(view), nullptr, 16);
|
||||||
pop(L, 3);
|
pop(L, 3);
|
||||||
return std::string_view(ptr, size);
|
return std::string_view(ptr, size);
|
||||||
} else {
|
} else {
|
||||||
requireglobal(L, "Bytearray_as_string");
|
return bytearray_as_string_indirect(L, idx);
|
||||||
pushvalue(L, -2);
|
|
||||||
call(L, 1, 1);
|
|
||||||
auto view = tolstring(L, -1);
|
|
||||||
pop(L, 2);
|
|
||||||
return view;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue