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

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

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;