Skip to content

Commit 192b86c

Browse files
committed
update(jinja): update jinja to new.
1 parent 8dd9c6d commit 192b86c

File tree

3 files changed

+644
-17
lines changed

3 files changed

+644
-17
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
88
include_directories(include)
99
include_directories(third_party)
1010

11+
option(UJSON_USE_RAPIDJSON "Use RapidJSON backend" OFF)
12+
if(UJSON_USE_RAPIDJSON)
13+
add_definitions(-DUJSON_USE_RAPIDJSON)
14+
include_directories(third_party/rapidjson/include)
15+
endif()
16+
1117
# Oniguruma
1218
add_subdirectory(third_party/oniguruma)
1319
include_directories(third_party/oniguruma/src)

third_party/jinja.hpp

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,23 @@
1515
limitations under the License.
1616
*/
1717

18-
#pragma once
19-
2018
#include <string>
21-
#include <memory>
2219
#include <vector>
2320
#include <map>
2421
#include <functional>
25-
#include <algorithm>
22+
#include <memory>
2623
#include <sstream>
24+
#include <algorithm>
2725
#include <iostream>
26+
#include <fstream>
27+
#include <regex>
28+
#include <initializer_list>
29+
#include <ctime>
30+
#include <iomanip>
31+
#include <chrono>
2832

2933
// External dependency: nlohmann/json
30-
#include <nlohmann/json.hpp>
34+
#include "ujson.hpp"
3135

3236
#define JINJA_VERSION_MAJOR 0
3337
#define JINJA_VERSION_MINOR 0
@@ -42,8 +46,9 @@
4246

4347
namespace jinja {
4448

45-
using json = nlohmann::json;
46-
using json = nlohmann::json;
49+
using json = ujson::json;
50+
51+
4752
using Argument = std::pair<std::string, json>;
4853
using UserFunction = std::function<json(const std::vector<Argument>&)>;
4954

@@ -558,8 +563,6 @@ class Lexer {
558563

559564

560565
// --- Helpers ---
561-
using json = nlohmann::json;
562-
563566
class Context; // Forward declaration
564567

565568
struct Node {
@@ -571,7 +574,12 @@ struct Macro;
571574

572575
// Forward declarations
573576
static bool is_truthy(const json& val);
574-
static const json UNDEFINED = {{"__jinja_undefined__", true}};
577+
static json undefined_init() {
578+
json j = json::object();
579+
j["__jinja_undefined__"] = true;
580+
return j;
581+
}
582+
static const json UNDEFINED = undefined_init();
575583

576584
inline bool is_undefined(const json& val) {
577585
return val.is_object() && val.contains("__jinja_undefined__");
@@ -619,27 +627,25 @@ class Context {
619627
return nullptr;
620628
}
621629

622-
json& get(const std::string& name) {
630+
json get(const std::string& name) {
623631
// Search from top to bottom
624632
for (auto it = scopes.rbegin(); it != scopes.rend(); ++it) {
625633
if (it->contains(name)) {
626634
return (*it)[name];
627635
}
628636
}
629637
JINJA_LOG("Context: Variable '" << name << "' not found, returning UNDEFINED");
630-
static json undefined_val = UNDEFINED;
631-
return undefined_val;
638+
return UNDEFINED;
632639
}
633640

634-
const json& get(const std::string& name) const {
641+
json get(const std::string& name) const {
635642
// Const version
636643
for (auto it = scopes.rbegin(); it != scopes.rend(); ++it) {
637644
if (it->contains(name)) {
638645
return (*it)[name];
639646
}
640647
}
641-
static const json undefined_val = UNDEFINED;
642-
return undefined_val;
648+
return UNDEFINED;
643649
}
644650

645651
void set(const std::string& name, json val) {
@@ -1284,7 +1290,7 @@ struct SetNode : Node {
12841290
// But Expr::evaluate returns value. We need reference.
12851291
// Context needs to support getting reference.
12861292
if (auto* var = dynamic_cast<VarExpr*>(attr->object.get())) {
1287-
json& obj = context.get(var->name);
1293+
json obj = context.get(var->name);
12881294
if (!obj.is_null()) {
12891295
obj[attr->name] = val;
12901296
}

0 commit comments

Comments
 (0)