From 19a012f9c6c09ebb2bff19d9e9806263dd450e4b Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 16 Jun 2024 21:19:49 +0300 Subject: [PATCH] add bytearray.remove, .insert and constructor using array --- src/logic/scripting/lua/lua_custom_types.cpp | 88 ++++++++++++++++---- src/logic/scripting/lua/lua_custom_types.hpp | 15 +--- 2 files changed, 76 insertions(+), 27 deletions(-) diff --git a/src/logic/scripting/lua/lua_custom_types.cpp b/src/logic/scripting/lua/lua_custom_types.cpp index e3cdc643..f1e87dd1 100644 --- a/src/logic/scripting/lua/lua_custom_types.cpp +++ b/src/logic/scripting/lua/lua_custom_types.cpp @@ -21,12 +21,58 @@ Bytearray::~Bytearray() { static int l_bytearray_append(lua::State* L) { if (auto buffer = touserdata(L, 1)) { auto value = tointeger(L, 2); - buffer->append(static_cast(value)); + buffer->data().push_back(static_cast(value)); } return 0; } +static int l_bytearray_insert(lua::State* L) { + auto buffer = touserdata(L, 1); + if (buffer == nullptr) { + return 0; + } + auto& data = buffer->data(); + auto index = tointeger(L, 2)-1; + if (static_cast(index) > data.size()) { + return 0; + } + auto value = tointeger(L, 3); + data.insert(data.begin() + index, static_cast(value)); + return 0; +} + +static int l_bytearray_remove(lua::State* L) { + auto buffer = touserdata(L, 1); + if (buffer == nullptr) { + return 0; + } + auto& data = buffer->data(); + auto index = tointeger(L, 2)-1; + if (static_cast(index) > data.size()) { + return 0; + } + data.erase(data.begin()+index); + return 0; +} + +static std::unordered_map bytearray_methods { + {"append", lua::wrap}, + {"insert", lua::wrap}, + {"remove", lua::wrap}, +}; + static int l_bytearray_meta_meta_call(lua::State* L) { + if (lua_istable(L, 2)) { + size_t len = objlen(L, 2); + std::vector buffer(len); + buffer.resize(len); + for (size_t i = 0; i < len; i++) { + rawgeti(L, i+1); + buffer[i] = static_cast(tointeger(L, -1)); + pop(L); + } + return newuserdata(L, std::move(buffer)); + } auto size = tointeger(L, 2); if (size < 0) { throw std::runtime_error("size can not be less than 0"); @@ -36,49 +82,61 @@ static int l_bytearray_meta_meta_call(lua::State* L) { static int l_bytearray_meta_index(lua::State* L) { auto buffer = touserdata(L, 1); + if (buffer == nullptr) { + return 0; + } + auto& data = buffer->data(); if (isstring(L, 2)) { - std::string member = tostring(L, 2); - if (member == "append") { - return pushcfunction(L, l_bytearray_append); + auto found = bytearray_methods.find(tostring(L, 2)); + if (found != bytearray_methods.end()) { + return pushcfunction(L, found->second); } } auto index = tointeger(L, 2)-1; - if (buffer == nullptr || static_cast(index) > buffer->size()) { + if (static_cast(index) > data.size()) { return 0; } - return pushinteger(L, (*buffer)[index]); + return pushinteger(L, data[index]); } static int l_bytearray_meta_newindex(lua::State* L) { auto buffer = touserdata(L, 1); + if (buffer == nullptr) { + return 0; + } + auto& data = buffer->data(); auto index = tointeger(L, 2)-1; - if (buffer == nullptr || static_cast(index) > buffer->size()) { + if (static_cast(index) > data.size()) { return 0; } auto value = tointeger(L, 3); - (*buffer)[index] = static_cast(value); + data[index] = static_cast(value); return 0; } static int l_bytearray_meta_len(lua::State* L) { if (auto buffer = touserdata(L, 1)) { - return pushinteger(L, buffer->size()); + return pushinteger(L, buffer->data().size()); } return 0; } static int l_bytearray_meta_tostring(lua::State* L) { - auto& buffer = *touserdata(L, 1); - if (buffer.size() > 512) { - return pushstring(L, "bytearray["+std::to_string(buffer.size())+"]{...}"); + auto buffer = touserdata(L, 1); + if (buffer == nullptr) { + return 0; + } + auto& data = buffer->data(); + if (data.size() > 512) { + return pushstring(L, "bytearray["+std::to_string(data.size())+"]{...}"); } else { std::stringstream ss; - ss << "bytearray[" << std::to_string(buffer.size()) << "]{"; - for (size_t i = 0; i < buffer.size(); i++) { + ss << "bytearray[" << std::to_string(data.size()) << "]{"; + for (size_t i = 0; i < data.size(); i++) { if (i > 0) { ss << " "; } - ss << static_cast(buffer[i]); + ss << static_cast(data[i]); } ss << "}"; return pushstring(L, ss.str()); diff --git a/src/logic/scripting/lua/lua_custom_types.hpp b/src/logic/scripting/lua/lua_custom_types.hpp index 8f6d61e6..c69bd2b0 100644 --- a/src/logic/scripting/lua/lua_custom_types.hpp +++ b/src/logic/scripting/lua/lua_custom_types.hpp @@ -19,21 +19,12 @@ namespace lua { Bytearray(size_t capacity); Bytearray(std::vector buffer); virtual ~Bytearray(); - - inline ubyte& operator[](size_t index) { - return buffer[index]; - } - + const std::string& getTypeName() const override { return TYPENAME; } - - inline size_t size() const { - return buffer.size(); - } - - inline void append(ubyte b) { - buffer.push_back(b); + inline std::vector& data() { + return buffer; } static int createMetatable(lua::State*);