[ox/mc] Fix MC read alloca in a loop
This commit is contained in:
14
deps/ox/src/ox/mc/write.hpp
vendored
14
deps/ox/src/ox/mc/write.hpp
vendored
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user