add new Bytearray:append overloads

This commit is contained in:
MihailRis 2024-12-22 02:14:04 +03:00
parent e5b533f65f
commit 4ce2f3edca
3 changed files with 36 additions and 26 deletions

View File

@ -149,27 +149,6 @@ static int l_read_bytes(lua::State* L) {
);
}
static void read_bytes_from_table(
lua::State* L, int tableIndex, std::vector<ubyte>& bytes
) {
if (!lua::istable(L, tableIndex)) {
throw std::runtime_error("table expected");
} else {
size_t size = lua::objlen(L, tableIndex);
for (size_t i = 0; i < size; i++) {
lua::rawgeti(L, i + 1, tableIndex);
const int byte = lua::tointeger(L, -1);
lua::pop(L);
if (byte < 0 || byte > 255) {
throw std::runtime_error(
"invalid byte '" + std::to_string(byte) + "'"
);
}
bytes.push_back(byte);
}
}
}
static int l_write_bytes(lua::State* L) {
fs::path path = get_writeable_path(L);
@ -181,7 +160,7 @@ static int l_write_bytes(lua::State* L) {
}
std::vector<ubyte> bytes;
read_bytes_from_table(L, 2, bytes);
lua::read_bytes_from_table(L, 2, bytes);
return lua::pushboolean(
L, files::write_bytes(path, bytes.data(), bytes.size())
);
@ -223,7 +202,7 @@ static int l_list(lua::State* L) {
static int l_gzip_compress(lua::State* L) {
std::vector<ubyte> bytes;
read_bytes_from_table(L, 1, bytes);
lua::read_bytes_from_table(L, 1, bytes);
auto compressed_bytes = gzip::compress(bytes.data(), bytes.size());
int newTable = lua::gettop(L);
@ -237,7 +216,7 @@ static int l_gzip_compress(lua::State* L) {
static int l_gzip_decompress(lua::State* L) {
std::vector<ubyte> bytes;
read_bytes_from_table(L, 1, bytes);
lua::read_bytes_from_table(L, 1, bytes);
auto decompressed_bytes = gzip::decompress(bytes.data(), bytes.size());
int newTable = lua::gettop(L);

View File

@ -2,6 +2,7 @@
#include <typeindex>
#include <typeinfo>
#include <stdexcept>
#include <unordered_map>
#include "data/dv.hpp"
@ -698,4 +699,25 @@ namespace lua {
}
return def;
}
inline void read_bytes_from_table(
lua::State* L, int tableIndex, std::vector<ubyte>& bytes
) {
if (!lua::istable(L, tableIndex)) {
throw std::runtime_error("table expected");
} else {
size_t size = lua::objlen(L, tableIndex);
for (size_t i = 0; i < size; i++) {
lua::rawgeti(L, i + 1, tableIndex);
const int byte = lua::tointeger(L, -1);
lua::pop(L);
if (byte < 0 || byte > 255) {
throw std::runtime_error(
"invalid byte '" + std::to_string(byte) + "'"
);
}
bytes.push_back(byte);
}
}
}
}

View File

@ -2,6 +2,7 @@
#include <sstream>
#include "util/listutil.hpp"
#include "../lua_util.hpp"
using namespace lua;
@ -18,8 +19,16 @@ LuaBytearray::~LuaBytearray() {
static int l_append(lua::State* L) {
if (auto buffer = touserdata<LuaBytearray>(L, 1)) {
auto value = tointeger(L, 2);
buffer->data().push_back(static_cast<ubyte>(value));
if (lua::isnumber(L, 2)) {
auto value = tointeger(L, 2);
buffer->data().push_back(static_cast<ubyte>(value));
} else if (lua::istable(L, 2)) {
lua::read_bytes_from_table(L, 2, buffer->data());
} else if (auto extension = lua::touserdata<LuaBytearray>(L, 2)) {
util::concat(buffer->data(), extension->data());
} else {
throw std::runtime_error("integer/table/Bytearray expected");
}
}
return 0;
}