Skip to content

Commit

Permalink
Fix json implementations with unstable pointers (`INJA_DATA_TYPE=nloh…
Browse files Browse the repository at this point in the history
…mann::ordered_json`)

* Closes pantor#289
  • Loading branch information
Naios committed Mar 27, 2024
1 parent 0066e60 commit 87fb892
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 30 deletions.
45 changes: 30 additions & 15 deletions include/inja/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ class Renderer : public NodeVisitor {
std::ostream* output_stream;

json additional_data;
json* current_loop_data = &additional_data["loop"];

json* get_current_loop_data() {
return &additional_data["loop"];
}

std::vector<std::shared_ptr<json>> data_tmp_stack;
std::stack<const json*> data_eval_stack;
Expand Down Expand Up @@ -485,17 +488,24 @@ class Renderer : public NodeVisitor {
throw_renderer_error("object must be an array", node);
}

if (!current_loop_data->empty()) {
auto tmp = *current_loop_data; // Because of clang-3
(*current_loop_data)["parent"] = std::move(tmp);
{
json* const current_loop_data = get_current_loop_data();
if (!current_loop_data->empty()) {
auto tmp = *current_loop_data; // Because of clang-3
(*current_loop_data)["parent"] = std::move(tmp);
}

(*current_loop_data)["is_first"] = true;
(*current_loop_data)["is_last"] = (result->size() <= 1);
}

size_t index = 0;
(*current_loop_data)["is_first"] = true;
(*current_loop_data)["is_last"] = (result->size() <= 1);

for (auto it = result->begin(); it != result->end(); ++it) {
additional_data[static_cast<std::string>(node.value)] = *it;

json* const current_loop_data = get_current_loop_data();

(*current_loop_data)["index"] = index;
(*current_loop_data)["index1"] = index + 1;
if (index == 1) {
Expand All @@ -510,11 +520,11 @@ class Renderer : public NodeVisitor {
}

additional_data[static_cast<std::string>(node.value)].clear();

json* const current_loop_data = get_current_loop_data();
if (!(*current_loop_data)["parent"].empty()) {
const auto tmp = (*current_loop_data)["parent"];
*current_loop_data = std::move(tmp);
} else {
current_loop_data = &additional_data["loop"];
}
}

Expand All @@ -524,17 +534,23 @@ class Renderer : public NodeVisitor {
throw_renderer_error("object must be an object", node);
}

if (!current_loop_data->empty()) {
(*current_loop_data)["parent"] = std::move(*current_loop_data);
{
json* const current_loop_data = get_current_loop_data();
if (!current_loop_data->empty()) {
(*current_loop_data)["parent"] = std::move(*current_loop_data);
}

(*current_loop_data)["is_first"] = true;
(*current_loop_data)["is_last"] = (result->size() <= 1);
}

size_t index = 0;
(*current_loop_data)["is_first"] = true;
(*current_loop_data)["is_last"] = (result->size() <= 1);

for (auto it = result->begin(); it != result->end(); ++it) {
additional_data[static_cast<std::string>(node.key)] = it.key();
additional_data[static_cast<std::string>(node.value)] = it.value();

json* const current_loop_data = get_current_loop_data();
(*current_loop_data)["index"] = index;
(*current_loop_data)["index1"] = index + 1;
if (index == 1) {
Expand All @@ -550,10 +566,10 @@ class Renderer : public NodeVisitor {

additional_data[static_cast<std::string>(node.key)].clear();
additional_data[static_cast<std::string>(node.value)].clear();

json* const current_loop_data = get_current_loop_data();
if (!(*current_loop_data)["parent"].empty()) {
*current_loop_data = std::move((*current_loop_data)["parent"]);
} else {
current_loop_data = &additional_data["loop"];
}
}

Expand Down Expand Up @@ -618,7 +634,6 @@ class Renderer : public NodeVisitor {
data_input = &data;
if (loop_data) {
additional_data = *loop_data;
current_loop_data = &additional_data["loop"];
}

template_stack.emplace_back(current_template);
Expand Down
45 changes: 30 additions & 15 deletions single_include/inja/inja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2103,7 +2103,10 @@ class Renderer : public NodeVisitor {
std::ostream* output_stream;

json additional_data;
json* current_loop_data = &additional_data["loop"];

json* get_current_loop_data() {
return &additional_data["loop"];
}

std::vector<std::shared_ptr<json>> data_tmp_stack;
std::stack<const json*> data_eval_stack;
Expand Down Expand Up @@ -2554,17 +2557,24 @@ class Renderer : public NodeVisitor {
throw_renderer_error("object must be an array", node);
}

if (!current_loop_data->empty()) {
auto tmp = *current_loop_data; // Because of clang-3
(*current_loop_data)["parent"] = std::move(tmp);
{
json* const current_loop_data = get_current_loop_data();
if (!current_loop_data->empty()) {
auto tmp = *current_loop_data; // Because of clang-3
(*current_loop_data)["parent"] = std::move(tmp);
}

(*current_loop_data)["is_first"] = true;
(*current_loop_data)["is_last"] = (result->size() <= 1);
}

size_t index = 0;
(*current_loop_data)["is_first"] = true;
(*current_loop_data)["is_last"] = (result->size() <= 1);

for (auto it = result->begin(); it != result->end(); ++it) {
additional_data[static_cast<std::string>(node.value)] = *it;

json* const current_loop_data = get_current_loop_data();

(*current_loop_data)["index"] = index;
(*current_loop_data)["index1"] = index + 1;
if (index == 1) {
Expand All @@ -2579,11 +2589,11 @@ class Renderer : public NodeVisitor {
}

additional_data[static_cast<std::string>(node.value)].clear();

json* const current_loop_data = get_current_loop_data();
if (!(*current_loop_data)["parent"].empty()) {
const auto tmp = (*current_loop_data)["parent"];
*current_loop_data = std::move(tmp);
} else {
current_loop_data = &additional_data["loop"];
}
}

Expand All @@ -2593,17 +2603,23 @@ class Renderer : public NodeVisitor {
throw_renderer_error("object must be an object", node);
}

if (!current_loop_data->empty()) {
(*current_loop_data)["parent"] = std::move(*current_loop_data);
{
json* const current_loop_data = get_current_loop_data();
if (!current_loop_data->empty()) {
(*current_loop_data)["parent"] = std::move(*current_loop_data);
}

(*current_loop_data)["is_first"] = true;
(*current_loop_data)["is_last"] = (result->size() <= 1);
}

size_t index = 0;
(*current_loop_data)["is_first"] = true;
(*current_loop_data)["is_last"] = (result->size() <= 1);

for (auto it = result->begin(); it != result->end(); ++it) {
additional_data[static_cast<std::string>(node.key)] = it.key();
additional_data[static_cast<std::string>(node.value)] = it.value();

json* const current_loop_data = get_current_loop_data();
(*current_loop_data)["index"] = index;
(*current_loop_data)["index1"] = index + 1;
if (index == 1) {
Expand All @@ -2619,10 +2635,10 @@ class Renderer : public NodeVisitor {

additional_data[static_cast<std::string>(node.key)].clear();
additional_data[static_cast<std::string>(node.value)].clear();

json* const current_loop_data = get_current_loop_data();
if (!(*current_loop_data)["parent"].empty()) {
*current_loop_data = std::move((*current_loop_data)["parent"]);
} else {
current_loop_data = &additional_data["loop"];
}
}

Expand Down Expand Up @@ -2687,7 +2703,6 @@ class Renderer : public NodeVisitor {
data_input = &data;
if (loop_data) {
additional_data = *loop_data;
current_loop_data = &additional_data["loop"];
}

template_stack.emplace_back(current_template);
Expand Down

0 comments on commit 87fb892

Please sign in to comment.