diff --git a/dev/tests/bytearray.lua b/dev/tests/bytearray.lua index 1a12bdeb..432dfbf5 100644 --- a/dev/tests/bytearray.lua +++ b/dev/tests/bytearray.lua @@ -25,3 +25,6 @@ end print(#arr, arr:get_capacity()) arr:trim() assert(#arr == arr:get_capacity()) + +arr = Bytearray({0, 2, 7, 1, 16, 75, 25}) +assert(arr[6] == 75) diff --git a/res/modules/internal/bytearray.lua b/res/modules/internal/bytearray.lua index ab93cfea..457587d5 100644 --- a/res/modules/internal/bytearray.lua +++ b/res/modules/internal/bytearray.lua @@ -179,7 +179,7 @@ local FFIBytearray = { return bytearray_type(buffer, #n, #n) elseif t == "table" then local capacity = math.max(#n, MIN_CAPACITY) - local buffer = malloc(capacity) + local buffer = FFI.cast("unsigned char*", malloc(capacity)) for i=1,#n do buffer[i - 1] = n[i] end diff --git a/src/coders/toml.cpp b/src/coders/toml.cpp index 1a249435..1c05dff4 100644 --- a/src/coders/toml.cpp +++ b/src/coders/toml.cpp @@ -239,13 +239,52 @@ dv::value toml::parse(std::string_view file, std::string_view source) { return TomlReader(file, source).read(); } +static void to_string(std::stringstream& ss, const dv::value& value); + +static void list_to_string(std::stringstream& ss, const dv::value& list) { + ss << "["; + int index = 0; + for (const auto& value : list) { + if (index > 0) { + ss << ", "; + } + to_string(ss, value); + index++; + } + ss << "]"; +} + +static void object_to_string(std::stringstream& ss, const dv::value& object) { + ss << "{"; + int index = 0; + for (const auto& [key, value] : object.asObject()) { + if (index > 0) { + ss << ", "; + } + ss << key << " = "; + to_string(ss, value); + index++; + } + ss << "}"; +} + +static void to_string(std::stringstream& ss, const dv::value& value) { + if (value.isObject()) { + object_to_string(ss, value); + } else if (value.isList()) { + list_to_string(ss, value); + } else { + ss << value; + } +} + std::string toml::stringify(const dv::value& root, const std::string& name) { std::stringstream ss; if (!name.empty()) { ss << "[" << name << "]\n"; } for (const auto& [key, value] : root.asObject()) { - if (!value.isObject()) { + if (!value.isObject() && !value.isList()) { ss << key << " = " << value << "\n"; } } @@ -253,6 +292,15 @@ std::string toml::stringify(const dv::value& root, const std::string& name) { if (value.isObject()) { ss << "\n" << toml::stringify(value, name.empty() ? key : name + "." + key); + } else if (value.isList()) { + ss << (name.empty() ? key : name + "." + key) << " = ["; + for (size_t i = 0; i < value.size(); i++) { + if (i > 0) { + ss << ", "; + } + to_string(ss, value[i]); + } + ss << "]"; } } return ss.str(); diff --git a/src/logic/scripting/lua/libs/libinventory.cpp b/src/logic/scripting/lua/libs/libinventory.cpp index 0536ff7b..629283a7 100644 --- a/src/logic/scripting/lua/libs/libinventory.cpp +++ b/src/logic/scripting/lua/libs/libinventory.cpp @@ -232,9 +232,9 @@ static int l_set_data(lua::State* L, ItemStack& stack) { } const luaL_Reg inventorylib[] = { - {"get", wrap_slot}, - {"set", wrap_slot}, - {"set_count", wrap_slot}, + {"get", lua::wrap>}, + {"set", lua::wrap>}, + {"set_count", lua::wrap>}, {"size", lua::wrap}, {"add", lua::wrap}, {"move", lua::wrap}, @@ -243,10 +243,10 @@ const luaL_Reg inventorylib[] = { {"get_block", lua::wrap}, {"bind_block", lua::wrap}, {"unbind_block", lua::wrap}, - {"get_data", wrap_slot}, - {"set_data", wrap_slot}, - {"get_all_data", wrap_slot}, - {"has_data", wrap_slot}, + {"get_data", lua::wrap>}, + {"set_data", lua::wrap>}, + {"get_all_data", lua::wrap>}, + {"has_data", lua::wrap>}, {"create", lua::wrap}, {"remove", lua::wrap}, {"clone", lua::wrap}, diff --git a/src/logic/scripting/lua/libs/libtoml.cpp b/src/logic/scripting/lua/libs/libtoml.cpp index d2f7b87e..bf5461c0 100644 --- a/src/logic/scripting/lua/libs/libtoml.cpp +++ b/src/logic/scripting/lua/libs/libtoml.cpp @@ -23,4 +23,5 @@ static int l_toml_parse(lua::State* L) { const luaL_Reg tomllib[] = { {"tostring", lua::wrap}, {"parse", lua::wrap}, - {NULL, NULL}}; + {NULL, NULL} +}; diff --git a/src/util/observer_handler.hpp b/src/util/observer_handler.hpp index 8dee7b05..79c4a488 100644 --- a/src/util/observer_handler.hpp +++ b/src/util/observer_handler.hpp @@ -14,6 +14,7 @@ public: ObserverHandler(ObserverHandler&& handler) noexcept : destructor(std::move(handler.destructor)) { + handler.destructor = nullptr; } ~ObserverHandler() { @@ -33,6 +34,7 @@ public: destructor(); } destructor = std::move(handler.destructor); + handler.destructor = nullptr; return *this; } private: diff --git a/src/window/detail/GLFWWindow.cpp b/src/window/detail/GLFWWindow.cpp index 2c648887..3d7ac26e 100644 --- a/src/window/detail/GLFWWindow.cpp +++ b/src/window/detail/GLFWWindow.cpp @@ -228,7 +228,7 @@ public: callbacks->second.notify(); } } - if (pressed) { + if (pressed && key < MOUSE_KEYS_OFFSET) { pressedKeys.push_back(static_cast(key)); } } diff --git a/test/coders/toml.cpp b/test/coders/toml.cpp index c520298f..2eccde07 100644 --- a/test/coders/toml.cpp +++ b/test/coders/toml.cpp @@ -26,6 +26,9 @@ TEST(TOML, EncodeDecode) { object["score"] = score; object["visible"] = visible; object["data"] = srcBytes; + object["values"] = dv::list({ + 5, 3, std::string("hello"), dv::object({{"number", 1234}}) + }); text = toml::stringify(object, ""); std::cout << text << std::endl;