[ox/mc] Fix MC read alloca in a loop

This commit is contained in:
Gary Talent 2023-11-13 23:42:09 -06:00
parent 3eec0a149d
commit d386bc8c91
3 changed files with 19 additions and 5 deletions

View File

@ -6,6 +6,14 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <ox/model/modelhandleradaptor.hpp>
#include <ox/std/buffer.hpp>
#include <ox/std/reader.hpp>
#include "read.hpp"
namespace ox {
template class ModelHandlerInterface<MetalClawReaderTemplate<BufferReader>>;
}

View File

@ -549,4 +549,6 @@ Result<T> readMC(const Buffer &buff) noexcept {
return readMC<T>(buff.data(), buff.size());
}
extern template class ModelHandlerInterface<MetalClawReaderTemplate<BufferReader>>;
}

View File

@ -359,15 +359,19 @@ constexpr Error MetalClawWriter<Writer>::field(const char*, const HashMap<String
ModelHandlerInterface handler{&writer};
// double len for both key and value
oxReturnError(handler.setTypeInfo("Map", 0, {}, len * 2));
// write the array
for (std::size_t i = 0; i < len; i++) {
const auto &key = keys[i];
// this loop body needs to be in a lambda because of the potential alloca call
constexpr auto loopBody = [](auto &handler, auto const&key, auto const&val) -> ox::Error {
const auto keyLen = ox_strlen(key);
auto wkey = ox_malloca(keyLen + 1, char, 0);
memcpy(wkey, key.c_str(), keyLen + 1);
oxReturnError(handler.fieldCString("", wkey.get(), keyLen));
oxRequireM(value, val->at(key));
oxReturnError(handler.field("", value));
oxRequireM(value, val.at(key));
return handler.field("", value);
};
// write the array
for (std::size_t i = 0; i < len; i++) {
auto const&key = keys[i];
oxReturnError(loopBody(handler, key, *val));
}
oxReturnError(writer.finalize());
fieldSet = true;