diff --git a/res/modules/bit_converter.lua b/res/modules/bit_converter.lua index 2686c2c2..86afa334 100644 --- a/res/modules/bit_converter.lua +++ b/res/modules/bit_converter.lua @@ -68,19 +68,19 @@ local function floatOrDoubleToBytes(val, opt) if opt == 'd' then val = mantissa for i = 1, 6 do - table.insert(bytes, math.floor(val) % (2 ^ 8)) + bytes[#bytes + 1] = math.floor(val) % (2 ^ 8) val = math.floor(val / (2 ^ 8)) end else - table.insert(bytes, math.floor(mantissa) % (2 ^ 8)) + bytes[#bytes + 1] = math.floor(mantissa) % (2 ^ 8) val = math.floor(mantissa / (2 ^ 8)) - table.insert(bytes, math.floor(val) % (2 ^ 8)) + bytes[#bytes + 1] = math.floor(val) % (2 ^ 8) val = math.floor(val / (2 ^ 8)) end - table.insert(bytes, math.floor(exponent * ((opt == 'd') and 16 or 128) + val) % (2 ^ 8)) + bytes[#bytes + 1] = math.floor(exponent * ((opt == 'd') and 16 or 128) + val) % (2 ^ 8) val = math.floor((exponent * ((opt == 'd') and 16 or 128) + val) / (2 ^ 8)) - table.insert(bytes, math.floor(sign * 128 + val) % (2 ^ 8)) + bytes[#bytes + 1] = math.floor(sign * 128 + val) % (2 ^ 8) val = math.floor((sign * 128 + val) / (2 ^ 8)) if not endianness then diff --git a/src/files/files.cpp b/src/files/files.cpp index 644e8716..8b66fa55 100644 --- a/src/files/files.cpp +++ b/src/files/files.cpp @@ -78,6 +78,21 @@ std::unique_ptr files::read_bytes(const fs::path& filename, size_t& len return data; } +std::vector files::read_bytes(const fs::path& filename) { + std::ifstream input(filename, std::ios::binary); + if (!input.is_open()) + return {}; + input.seekg(0, std::ios_base::end); + size_t length = input.tellg(); + input.seekg(0, std::ios_base::beg); + + std::vector data(length); + data.resize(length); + input.read((char*)data.data(), length); + input.close(); + return data; +} + std::string files::read_string(const fs::path& filename) { size_t size; std::unique_ptr bytes (read_bytes(filename, size)); diff --git a/src/files/files.hpp b/src/files/files.hpp index 4e6ad960..1a124b25 100644 --- a/src/files/files.hpp +++ b/src/files/files.hpp @@ -57,6 +57,7 @@ namespace files { bool read(const fs::path&, char* data, size_t size); std::unique_ptr read_bytes(const fs::path&, size_t& length); + std::vector read_bytes(const fs::path&); std::string read_string(const fs::path& filename); /// @brief Read JSON or BJSON file diff --git a/src/logic/scripting/lua/libfile.cpp b/src/logic/scripting/lua/libfile.cpp index 87e9af7a..3a00e0dd 100644 --- a/src/logic/scripting/lua/libfile.cpp +++ b/src/logic/scripting/lua/libfile.cpp @@ -109,18 +109,8 @@ static int l_file_mkdirs(lua::State* L) { static int l_file_read_bytes(lua::State* L) { fs::path path = resolve_path(lua::require_string(L, 1)); if (fs::is_regular_file(path)) { - size_t length = static_cast(fs::file_size(path)); - - auto bytes = files::read_bytes(path, length); - - lua::createtable(L, length, 0); - int newTable = lua::gettop(L); - - for(size_t i = 0; i < length; i++) { - lua::pushinteger(L, bytes[i]); - lua::rawseti(L, i+1, newTable); - } - return 1; + auto bytes = files::read_bytes(path); + return lua::newuserdata(L, std::move(bytes)); } throw std::runtime_error("file does not exists "+util::quote(path.u8string())); } @@ -151,10 +141,13 @@ static int l_file_write_bytes(lua::State* L) { fs::path path = resolve_path(lua::require_string(L, pathIndex)); + if (auto bytearray = lua::touserdata(L, -1)) { + auto& bytes = bytearray->data(); + return lua::pushboolean(L, files::write_bytes(path, bytes.data(), bytes.size())); + } + std::vector bytes; - int result = read_bytes_from_table(L, -1, bytes); - if(result != 1) { return result; } else { diff --git a/src/logic/scripting/lua/lua_custom_types.cpp b/src/logic/scripting/lua/lua_custom_types.cpp new file mode 100644 index 00000000..2983e1b1 --- /dev/null +++ b/src/logic/scripting/lua/lua_custom_types.cpp @@ -0,0 +1,183 @@ +#include "lua_custom_types.hpp" + +#include "lua_util.hpp" + +#include + +using namespace lua; + + +Bytearray::Bytearray(size_t capacity) + : buffer(capacity) { + buffer.resize(capacity); +} + +Bytearray::Bytearray(std::vector buffer) : buffer(std::move(buffer)) { +} + +Bytearray::~Bytearray() { +} + +static int l_bytearray_append(lua::State* L) { + if (auto buffer = touserdata(L, 1)) { + auto value = tointeger(L, 2); + 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"); + } + return newuserdata(L, static_cast(size)); +} + +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)) { + 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 (static_cast(index) > data.size()) { + return 0; + } + 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 = static_cast(tointeger(L, 2)-1); + auto value = tointeger(L, 3); + if (index >= data.size()) { + if (index == data.size()) { + data.push_back(static_cast(value)); + } + return 0; + } + 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->data().size()); + } + return 0; +} + +static int l_bytearray_meta_tostring(lua::State* L) { + 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(data.size()) << "]{"; + for (size_t i = 0; i < data.size(); i++) { + if (i > 0) { + ss << " "; + } + ss << static_cast(data[i]); + } + ss << "}"; + return pushstring(L, ss.str()); + } +} + +static int l_bytearray_meta_add(lua::State* L) { + auto bufferA = touserdata(L, 1); + auto bufferB = touserdata(L, 2); + if (bufferA == nullptr || bufferB == nullptr) { + return 0; + } + auto& dataA = bufferA->data(); + auto& dataB = bufferB->data(); + + std::vector ab; + ab.reserve(dataA.size() + dataB.size()); + ab.insert(ab.end(), dataA.begin(), dataA.end()); + ab.insert(ab.end(), dataB.begin(), dataB.end()); + return newuserdata(L, std::move(ab)); +} + +int Bytearray::createMetatable(lua::State* L) { + createtable(L, 0, 6); + pushcfunction(L, lua::wrap); + setfield(L, "__index"); + pushcfunction(L, lua::wrap); + setfield(L, "__newindex"); + pushcfunction(L, lua::wrap); + setfield(L, "__len"); + pushcfunction(L, lua::wrap); + setfield(L, "__tostring"); + pushcfunction(L, lua::wrap); + setfield(L, "__add"); + + createtable(L, 0, 1); + pushcfunction(L, lua::wrap); + setfield(L, "__call"); + setmetatable(L); + return 1; +} diff --git a/src/logic/scripting/lua/lua_custom_types.hpp b/src/logic/scripting/lua/lua_custom_types.hpp new file mode 100644 index 00000000..c69bd2b0 --- /dev/null +++ b/src/logic/scripting/lua/lua_custom_types.hpp @@ -0,0 +1,35 @@ +#ifndef LOGIC_SCRIPTING_LUA_LUA_CUSTOM_TYPES_HPP_ +#define LOGIC_SCRIPTING_LUA_LUA_CUSTOM_TYPES_HPP_ + +#include "lua_commons.hpp" + +#include +#include + +namespace lua { + class Userdata { + public: + virtual ~Userdata() {}; + virtual const std::string& getTypeName() const = 0; + }; + + class Bytearray : public Userdata { + std::vector buffer; + public: + Bytearray(size_t capacity); + Bytearray(std::vector buffer); + virtual ~Bytearray(); + + const std::string& getTypeName() const override { + return TYPENAME; + } + inline std::vector& data() { + return buffer; + } + + static int createMetatable(lua::State*); + inline static std::string TYPENAME = "bytearray"; + }; +} + +#endif // LOGIC_SCRIPTING_LUA_LUA_CUSTOM_TYPES_HPP_ diff --git a/src/logic/scripting/lua/lua_engine.cpp b/src/logic/scripting/lua/lua_engine.cpp index 096109df..75ef541f 100644 --- a/src/logic/scripting/lua/lua_engine.cpp +++ b/src/logic/scripting/lua/lua_engine.cpp @@ -1,6 +1,7 @@ #include "lua_engine.hpp" #include "api_lua.hpp" +#include "lua_custom_types.hpp" #include "../../../debug/Logger.hpp" #include "../../../util/stringutil.hpp" @@ -45,7 +46,6 @@ static void create_libs(lua::State* L) { addfunc(L, "print", lua::wrap); } -#include void lua::initialize() { logger.info() << LUA_VERSION; logger.info() << LUAJIT_VERSION; @@ -81,6 +81,8 @@ void lua::initialize() { createtable(L, 0, 0); setglobal(L, LAMBDAS_TABLE); + + newusertype(L, "bytearray"); } void lua::finalize() { diff --git a/src/logic/scripting/lua/lua_util.cpp b/src/logic/scripting/lua/lua_util.cpp index 198ba280..745889d2 100644 --- a/src/logic/scripting/lua/lua_util.cpp +++ b/src/logic/scripting/lua/lua_util.cpp @@ -9,6 +9,15 @@ using namespace lua; static int nextEnvironment = 1; +std::unordered_map lua::usertypeNames; + +int lua::userdata_destructor(lua::State* L) { + if (auto obj = touserdata(L, 1)) { + obj->~Userdata(); + } + return 0; +} + std::string lua::env_name(int env) { return "_ENV"+util::mangleid(env); } diff --git a/src/logic/scripting/lua/lua_util.hpp b/src/logic/scripting/lua/lua_util.hpp index 3c705eb3..af58ea11 100644 --- a/src/logic/scripting/lua/lua_util.hpp +++ b/src/logic/scripting/lua/lua_util.hpp @@ -2,9 +2,16 @@ #define LOGIC_SCRIPTING_LUA_UTIL_HPP_ #include "lua_commons.hpp" +#include "lua_custom_types.hpp" + +#include +#include +#include namespace lua { inline std::string LAMBDAS_TABLE = "$L"; + extern std::unordered_map usertypeNames; + int userdata_destructor(lua::State* L); std::string env_name(int env); @@ -42,6 +49,10 @@ namespace lua { inline const char* type_name(lua::State* L, int idx) { return lua_typename(L, idx); } + inline int rawget(lua::State* L, int idx=-2) { + lua_rawget(L, idx); + return 1; + } inline int rawgeti(lua::State* L, int n, int idx=-1) { lua_rawgeti(L, idx, n); return 1; @@ -230,6 +241,12 @@ namespace lua { inline bool isfunction(lua::State* L, int idx) { return lua_isfunction(L, idx); } + inline bool isuserdata(lua::State* L, int idx) { + return lua_isuserdata(L, idx); + } + inline void setfield(lua::State* L, const std::string& name, int idx=-2) { + lua_setfield(L, idx, name.c_str()); + } inline bool toboolean(lua::State* L, int idx) { return lua_toboolean(L, idx); } @@ -245,7 +262,42 @@ namespace lua { inline const void* topointer(lua::State* L, int idx) { return lua_topointer(L, idx); } - inline glm::vec2 tovec2(lua::State* L, int idx) { + inline void setglobal(lua::State* L, const std::string& name) { + lua_setglobal(L, name.c_str()); + } + template + inline T* touserdata(lua::State* L, int idx) { + if (void* rawptr = lua_touserdata(L, idx)) { + return static_cast(rawptr); + } + return nullptr; + } + template + inline int newuserdata(lua::State* L, Args&&... args) { + const auto& found = usertypeNames.find(typeid(T)); + void* ptr = lua_newuserdata(L, sizeof(T)); + new (ptr) T(args...); + + if (found == usertypeNames.end()) { + log_error("usertype is not registred: "+std::string(typeid(T).name())); + } else if (getglobal(L, found->second)) { + setmetatable(L); + } + return 1; + } + + template + inline void newusertype(lua::State* L, const std::string& name) { + usertypeNames[typeid(T)] = name; + func(L); + + pushcfunction(L, userdata_destructor); + setfield(L, "__gc"); + + setglobal(L, name); + } + + inline glm::vec2 tovec2(lua::State* L, int idx) { pushvalue(L, idx); if (!istable(L, idx) || objlen(L, idx) < 2) { throw std::runtime_error("value must be an array of two numbers"); @@ -287,14 +339,6 @@ namespace lua { return true; } - inline void setfield(lua::State* L, const std::string& name, int idx=-2) { - lua_setfield(L, idx, name.c_str()); - } - - inline void setglobal(lua::State* L, const std::string& name) { - lua_setglobal(L, name.c_str()); - } - inline const char* require_string(lua::State* L, int idx) { if (!isstring(L, idx)) { throw luaerror("string expected at "+std::to_string(idx));