replace Bytearray with FFI implementation

This commit is contained in:
MihailRis
2025-04-07 20:48:12 +03:00
parent 794aa5b9cd
commit 303e861fbb
16 changed files with 125 additions and 333 deletions
+9 -12
View File
@@ -16,14 +16,14 @@ static int l_encode(lua::State* L) {
return lua::pushstring(L, util::base64_encode(
reinterpret_cast<const ubyte*>(buffer.data()), buffer.size()
));
} else if (auto bytes = lua::touserdata<lua::LuaBytearray>(L, 1)) {
return lua::pushstring(
L,
util::base64_encode(
bytes->data().data(),
bytes->data().size()
)
} else {
auto string = lua::bytearray_as_string(L, 1);
auto out = util::base64_encode(
reinterpret_cast<const ubyte*>(string.data()),
string.size()
);
lua::pop(L);
return lua::pushstring(L, std::move(out));
}
throw std::runtime_error("array or ByteArray expected");
}
@@ -36,13 +36,10 @@ static int l_decode(lua::State* L) {
lua::pushinteger(L, buffer[i] & 0xFF);
lua::rawseti(L, i+1);
}
return 1;
} else {
lua::newuserdata<lua::LuaBytearray>(L, buffer.size());
auto bytearray = lua::touserdata<lua::LuaBytearray>(L, -1);
bytearray->data().reserve(buffer.size());
std::memcpy(bytearray->data().data(), buffer.data(), buffer.size());
return lua::create_bytearray(L, buffer.data(), buffer.size());
}
return 1;
}
const luaL_Reg base64lib[] = {
+9 -10
View File
@@ -9,9 +9,7 @@ static int l_tobytes(lua::State* L) {
if (lua::gettop(L) >= 2) {
compress = lua::toboolean(L, 2);
}
return lua::newuserdata<lua::LuaBytearray>(
L, json::to_binary(value, compress)
);
return lua::create_bytearray(L, json::to_binary(value, compress));
}
static int l_frombytes(lua::State* L) {
@@ -24,17 +22,18 @@ static int l_frombytes(lua::State* L) {
lua::pop(L);
}
return lua::pushvalue(L, json::from_binary(buffer.data(), len));
} else if (auto bytes = lua::touserdata<lua::LuaBytearray>(L, -1)) {
const auto& buffer = bytes->data();
return lua::pushvalue(
L, json::from_binary(buffer.data(), buffer.size())
);
} else {
throw std::runtime_error("table or Bytearray expected");
auto string = lua::bytearray_as_string(L, 1);
auto out = json::from_binary(
reinterpret_cast<const ubyte*>(string.data()), string.size()
);
lua::pop(L);
return lua::pushvalue(L, std::move(out));
}
}
const luaL_Reg bjsonlib[] = {
{"tobytes", lua::wrap<l_tobytes>},
{"frombytes", lua::wrap<l_frombytes>},
{NULL, NULL}};
{NULL, NULL}
};
+3 -3
View File
@@ -99,7 +99,7 @@ static int pack(lua::State* L, const char* format, bool usetable) {
}
return 1;
} else {
return lua::newuserdata<lua::LuaBytearray>(L, builder.build());
return lua::create_bytearray(L, builder.build());
}
}
@@ -130,8 +130,8 @@ static int count_elements(const char* format) {
static int l_unpack(lua::State* L) {
const char* format = lua::require_string(L, 1);
int count = count_elements(format);
auto bytes = lua::require_bytearray(L, 2);
ByteReader reader(bytes);
auto bytes = lua::bytearray_as_string(L, 2);
ByteReader reader(reinterpret_cast<const ubyte*>(bytes.data()), bytes.size());
bool bigEndian = false;
for (size_t i = 0; format[i]; i++) {
+5 -3
View File
@@ -248,12 +248,14 @@ static int l_load_texture(lua::State* L) {
}
lua::pop(L);
load_texture(buffer.data(), buffer.size(), lua::require_string(L, 2));
} else if (auto bytes = lua::touserdata<lua::LuaBytearray>(L, 1)) {
} else {
auto string = lua::bytearray_as_string(L, 1);
load_texture(
bytes->data().data(),
bytes->data().size(),
reinterpret_cast<const ubyte*>(string.data()),
string.size(),
lua::require_string(L, 2)
);
lua::pop(L);
}
return 0;
}
+6 -12
View File
@@ -128,7 +128,7 @@ static int l_read_bytes(lua::State* L) {
auto bytes = io::read_bytes(path);
if (lua::gettop(L) < 2 || !lua::toboolean(L, 2)) {
lua::newuserdata<lua::LuaBytearray>(L, std::move(bytes));
lua::create_bytearray(L, std::move(bytes));
} else {
lua::createtable(L, length, 0);
int newTable = lua::gettop(L);
@@ -148,18 +148,12 @@ static int l_read_bytes(lua::State* L) {
static int l_write_bytes(lua::State* L) {
io::path path = get_writeable_path(L);
if (auto bytearray = lua::touserdata<lua::LuaBytearray>(L, 2)) {
auto& bytes = bytearray->data();
return lua::pushboolean(
L, io::write_bytes(path, bytes.data(), bytes.size())
);
}
std::vector<ubyte> bytes;
lua::read_bytes_from_table(L, 2, bytes);
return lua::pushboolean(
L, io::write_bytes(path, bytes.data(), bytes.size())
auto string = lua::bytearray_as_string(L, 2);
bool res = io::write_bytes(
path, reinterpret_cast<const ubyte*>(string.data()), string.size()
);
lua::pop(L);
return lua::pushboolean(L, res);
}
static int l_list_all_res(lua::State* L, const std::string& path) {
+6 -9
View File
@@ -109,13 +109,13 @@ static int l_send(lua::State* L, network::Network& network) {
}
lua::pop(L);
connection->send(buffer.data(), size);
} else if (auto bytes = lua::touserdata<lua::LuaBytearray>(L, 2)) {
connection->send(
reinterpret_cast<char*>(bytes->data().data()), bytes->data().size()
);
} else if (lua::isstring(L, 2)) {
auto string = lua::tolstring(L, 2);
connection->send(string.data(), string.length());
} else {
auto string = lua::bytearray_as_string(L, 2);
connection->send(string.data(), string.length());
lua::pop(L);
}
return 0;
}
@@ -140,13 +140,10 @@ static int l_recv(lua::State* L, network::Network& network) {
lua::pushinteger(L, buffer[i] & 0xFF);
lua::rawseti(L, i+1);
}
return 1;
} else {
lua::newuserdata<lua::LuaBytearray>(L, size);
auto bytearray = lua::touserdata<lua::LuaBytearray>(L, -1);
bytearray->data().reserve(size);
std::memcpy(bytearray->data().data(), buffer.data(), size);
return lua::create_bytearray(L, buffer.data(), size);
}
return 1;
}
static int l_available(lua::State* L, network::Network& network) {
+5 -14
View File
@@ -14,13 +14,10 @@ static int l_tobytes(lua::State* L) {
lua::pushinteger(L, string[i] & 0xFF);
lua::rawseti(L, i+1);
}
return 1;
} else {
lua::newuserdata<lua::LuaBytearray>(L, string.length());
auto bytearray = lua::touserdata<lua::LuaBytearray>(L, -1);
bytearray->data().reserve(string.length());
std::memcpy(bytearray->data().data(), string.data(), string.length());
return lua::create_bytearray(L, string.data(), string.length());
}
return 1;
}
static int l_tostring(lua::State* L) {
@@ -35,16 +32,10 @@ static int l_tostring(lua::State* L) {
}
lua::pop(L);
return lua::pushlstring(L, buffer.data(), size);
} else if (auto bytes = lua::touserdata<lua::LuaBytearray>(L, 1)) {
return lua::pushstring(
L,
std::string(
reinterpret_cast<char*>(bytes->data().data()),
bytes->data().size()
)
);
} else {
lua::bytearray_as_string(L, 1);
return 1;
}
return 1;
}
static int l_length(lua::State* L) {
+14 -5
View File
@@ -144,7 +144,7 @@ static int l_get_chunk_data(lua::State* L) {
} else {
chunkData = compressed_chunks::encode(*chunk);
}
return lua::newuserdata<lua::LuaBytearray>(L, std::move(chunkData));
return lua::create_bytearray(L, std::move(chunkData));
}
static void integrate_chunk_client(Chunk& chunk) {
@@ -175,14 +175,17 @@ static int l_set_chunk_data(lua::State* L) {
int x = static_cast<int>(lua::tointeger(L, 1));
int z = static_cast<int>(lua::tointeger(L, 2));
auto buffer = lua::require_bytearray(L, 3);
auto buffer = lua::bytearray_as_string(L, 3);
auto chunk = level->chunks->getChunk(x, z);
if (chunk == nullptr) {
return lua::pushboolean(L, false);
}
compressed_chunks::decode(
*chunk, buffer.data(), buffer.size(), *content->getIndices()
*chunk,
reinterpret_cast<const ubyte*>(buffer.data()),
buffer.size(),
*content->getIndices()
);
if (controller->getChunksController()->lighting == nullptr) {
return lua::pushboolean(L, true);
@@ -198,10 +201,16 @@ static int l_save_chunk_data(lua::State* L) {
int x = static_cast<int>(lua::tointeger(L, 1));
int z = static_cast<int>(lua::tointeger(L, 2));
auto buffer = lua::require_bytearray(L, 3);
auto buffer = lua::bytearray_as_string(L, 3);
compressed_chunks::save(
x, z, std::move(buffer), level->getWorld()->wfile->getRegions()
x,
z,
std::vector(
reinterpret_cast<const ubyte*>(buffer.data()),
reinterpret_cast<const ubyte*>(buffer.data()) + buffer.size()
),
level->getWorld()->wfile->getRegions()
);
return 0;
}