@fugood/llama.node 1.4.15 → 1.6.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/binding.ts +1 -5
- package/lib/index.js +2 -2
- package/lib/index.ts +2 -2
- package/package.json +15 -15
- package/scripts/llama.cpp.patch +76 -61
- package/src/LlamaContext.cpp +20 -32
- package/src/llama.cpp/common/CMakeLists.txt +12 -0
- package/src/llama.cpp/common/arg.cpp +20 -0
- package/src/llama.cpp/common/chat-parser.cpp +3 -3
- package/src/llama.cpp/common/chat-parser.h +4 -4
- package/src/llama.cpp/common/chat.cpp +289 -34
- package/src/llama.cpp/common/chat.h +32 -20
- package/src/llama.cpp/common/common.cpp +0 -1
- package/src/llama.cpp/common/common.h +31 -25
- package/src/llama.cpp/common/download.cpp +19 -14
- package/src/llama.cpp/common/jinja/caps.cpp +237 -0
- package/src/llama.cpp/common/jinja/caps.h +24 -0
- package/src/llama.cpp/common/jinja/lexer.cpp +341 -0
- package/src/llama.cpp/common/jinja/lexer.h +157 -0
- package/src/llama.cpp/common/jinja/parser.cpp +591 -0
- package/src/llama.cpp/common/jinja/parser.h +21 -0
- package/src/llama.cpp/common/jinja/runtime.cpp +865 -0
- package/src/llama.cpp/common/jinja/runtime.h +628 -0
- package/src/llama.cpp/common/jinja/string.cpp +207 -0
- package/src/llama.cpp/common/jinja/string.h +58 -0
- package/src/llama.cpp/common/jinja/utils.h +49 -0
- package/src/llama.cpp/common/jinja/value.cpp +1221 -0
- package/src/llama.cpp/common/jinja/value.h +464 -0
- package/src/llama.cpp/common/json-partial.h +1 -0
- package/src/llama.cpp/common/sampling.cpp +52 -19
- package/src/llama.cpp/ggml/include/ggml.h +39 -7
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +4 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/ops.cpp +63 -37
- package/src/llama.cpp/ggml/src/ggml-cpu/simd-mappings.h +31 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/vec.cpp +18 -0
- package/src/llama.cpp/include/llama-cpp.h +3 -1
- package/src/llama.cpp/include/llama.h +29 -2
- package/src/llama.cpp/src/llama-adapter.cpp +7 -13
- package/src/llama.cpp/src/llama-adapter.h +1 -3
- package/src/llama.cpp/src/llama-context.cpp +232 -144
- package/src/llama.cpp/src/llama-context.h +10 -0
- package/src/llama.cpp/src/llama-cparams.h +2 -0
- package/src/llama.cpp/src/llama-hparams.cpp +0 -36
- package/src/llama.cpp/src/llama-hparams.h +38 -1
- package/src/llama.cpp/src/llama-kv-cache.cpp +201 -59
- package/src/llama.cpp/src/llama-kv-cache.h +0 -2
- package/src/llama.cpp/src/llama-mmap.cpp +5 -1
- package/src/llama.cpp/src/llama-model-loader.cpp +21 -7
- package/src/llama.cpp/src/llama-model.cpp +5 -1
- package/src/llama.cpp/src/llama-model.h +3 -2
- package/src/llama.cpp/src/llama-sampling.cpp +170 -13
|
@@ -0,0 +1,1221 @@
|
|
|
1
|
+
#include "runtime.h"
|
|
2
|
+
#include "value.h"
|
|
3
|
+
|
|
4
|
+
// for converting from JSON to jinja values
|
|
5
|
+
#include <nlohmann/json.hpp>
|
|
6
|
+
|
|
7
|
+
#include <string>
|
|
8
|
+
#include <cctype>
|
|
9
|
+
#include <vector>
|
|
10
|
+
#include <optional>
|
|
11
|
+
#include <algorithm>
|
|
12
|
+
|
|
13
|
+
#define FILENAME "jinja-value"
|
|
14
|
+
|
|
15
|
+
namespace jinja {
|
|
16
|
+
|
|
17
|
+
// func_args method implementations
|
|
18
|
+
|
|
19
|
+
value func_args::get_kwarg(const std::string & key, value default_val) const {
|
|
20
|
+
for (const auto & arg : args) {
|
|
21
|
+
if (is_val<value_kwarg>(arg)) {
|
|
22
|
+
auto * kwarg = cast_val<value_kwarg>(arg);
|
|
23
|
+
if (kwarg->key == key) {
|
|
24
|
+
return kwarg->val;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return default_val;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
value func_args::get_kwarg_or_pos(const std::string & key, size_t pos) const {
|
|
32
|
+
value val = get_kwarg(key, mk_val<value_undefined>());
|
|
33
|
+
|
|
34
|
+
if (val->is_undefined() && pos < count() && !is_val<value_kwarg>(args[pos])) {
|
|
35
|
+
return args[pos];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return val;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
value func_args::get_pos(size_t pos) const {
|
|
42
|
+
if (count() > pos) {
|
|
43
|
+
return args[pos];
|
|
44
|
+
}
|
|
45
|
+
throw raised_exception("Function '" + func_name + "' expected at least " + std::to_string(pos + 1) + " arguments, got " + std::to_string(count()));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
value func_args::get_pos(size_t pos, value default_val) const {
|
|
49
|
+
if (count() > pos) {
|
|
50
|
+
return args[pos];
|
|
51
|
+
}
|
|
52
|
+
return default_val;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
void func_args::push_back(const value & val) {
|
|
56
|
+
args.push_back(val);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
void func_args::push_front(const value & val) {
|
|
60
|
+
args.insert(args.begin(), val);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const std::vector<value> & func_args::get_args() const {
|
|
64
|
+
return args;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Function that mimics Python's array slicing.
|
|
69
|
+
*/
|
|
70
|
+
template<typename T>
|
|
71
|
+
static T slice(const T & array, int64_t start, int64_t stop, int64_t step = 1) {
|
|
72
|
+
int64_t len = static_cast<int64_t>(array.size());
|
|
73
|
+
int64_t direction = (step > 0) ? 1 : ((step < 0) ? -1 : 0);
|
|
74
|
+
int64_t start_val = 0;
|
|
75
|
+
int64_t stop_val = 0;
|
|
76
|
+
if (direction >= 0) {
|
|
77
|
+
start_val = start;
|
|
78
|
+
if (start_val < 0) {
|
|
79
|
+
start_val = std::max(len + start_val, (int64_t)0);
|
|
80
|
+
} else {
|
|
81
|
+
start_val = std::min(start_val, len);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
stop_val = stop;
|
|
85
|
+
if (stop_val < 0) {
|
|
86
|
+
stop_val = std::max(len + stop_val, (int64_t)0);
|
|
87
|
+
} else {
|
|
88
|
+
stop_val = std::min(stop_val, len);
|
|
89
|
+
}
|
|
90
|
+
} else {
|
|
91
|
+
start_val = len - 1;
|
|
92
|
+
if (start_val < 0) {
|
|
93
|
+
start_val = std::max(len + start_val, (int64_t)-1);
|
|
94
|
+
} else {
|
|
95
|
+
start_val = std::min(start_val, len - 1);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
stop_val = -1;
|
|
99
|
+
if (stop_val < -1) {
|
|
100
|
+
stop_val = std::max(len + stop_val, (int64_t)-1);
|
|
101
|
+
} else {
|
|
102
|
+
stop_val = std::min(stop_val, len - 1);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
T result;
|
|
106
|
+
if (direction == 0) {
|
|
107
|
+
return result;
|
|
108
|
+
}
|
|
109
|
+
for (int64_t i = start_val; direction * i < direction * stop_val; i += step) {
|
|
110
|
+
if (i >= 0 && i < len) {
|
|
111
|
+
result.push_back(array[static_cast<size_t>(i)]);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return result;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
template<typename T>
|
|
118
|
+
static value test_type_fn(const func_args & args) {
|
|
119
|
+
args.ensure_count(1);
|
|
120
|
+
bool is_type = is_val<T>(args.get_pos(0));
|
|
121
|
+
JJ_DEBUG("test_type_fn: type=%s result=%d", typeid(T).name(), is_type ? 1 : 0);
|
|
122
|
+
return mk_val<value_bool>(is_type);
|
|
123
|
+
}
|
|
124
|
+
template<typename T, typename U>
|
|
125
|
+
static value test_type_fn(const func_args & args) {
|
|
126
|
+
args.ensure_count(1);
|
|
127
|
+
bool is_type = is_val<T>(args.get_pos(0)) || is_val<U>(args.get_pos(0));
|
|
128
|
+
JJ_DEBUG("test_type_fn: type=%s or %s result=%d", typeid(T).name(), typeid(U).name(), is_type ? 1 : 0);
|
|
129
|
+
return mk_val<value_bool>(is_type);
|
|
130
|
+
}
|
|
131
|
+
template<value_compare_op op>
|
|
132
|
+
static value test_compare_fn(const func_args & args) {
|
|
133
|
+
args.ensure_count(2, 2);
|
|
134
|
+
return mk_val<value_bool>(value_compare(args.get_pos(0), args.get_pos(1), op));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
static value tojson(const func_args & args) {
|
|
138
|
+
args.ensure_count(1, 5);
|
|
139
|
+
value val_ascii = args.get_kwarg_or_pos("ensure_ascii", 1);
|
|
140
|
+
value val_indent = args.get_kwarg_or_pos("indent", 2);
|
|
141
|
+
value val_separators = args.get_kwarg_or_pos("separators", 3);
|
|
142
|
+
value val_sort = args.get_kwarg_or_pos("sort_keys", 4);
|
|
143
|
+
int indent = -1;
|
|
144
|
+
if (is_val<value_int>(val_indent)) {
|
|
145
|
+
indent = static_cast<int>(val_indent->as_int());
|
|
146
|
+
}
|
|
147
|
+
if (val_ascii->as_bool()) { // undefined == false
|
|
148
|
+
throw not_implemented_exception("tojson ensure_ascii=true not implemented");
|
|
149
|
+
}
|
|
150
|
+
if (val_sort->as_bool()) { // undefined == false
|
|
151
|
+
throw not_implemented_exception("tojson sort_keys=true not implemented");
|
|
152
|
+
}
|
|
153
|
+
auto separators = (is_val<value_array>(val_separators) ? val_separators : mk_val<value_array>())->as_array();
|
|
154
|
+
std::string item_sep = separators.size() > 0 ? separators[0]->as_string().str() : (indent < 0 ? ", " : ",");
|
|
155
|
+
std::string key_sep = separators.size() > 1 ? separators[1]->as_string().str() : ": ";
|
|
156
|
+
std::string json_str = value_to_json(args.get_pos(0), indent, item_sep, key_sep);
|
|
157
|
+
return mk_val<value_string>(json_str);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
template<bool is_reject>
|
|
161
|
+
static value selectattr(const func_args & args) {
|
|
162
|
+
args.ensure_count(2, 4);
|
|
163
|
+
args.ensure_vals<value_array, value_string, value_string, value_string>(true, true, false, false);
|
|
164
|
+
|
|
165
|
+
auto arr = args.get_pos(0)->as_array();
|
|
166
|
+
auto attr_name = args.get_pos(1)->as_string().str();
|
|
167
|
+
auto out = mk_val<value_array>();
|
|
168
|
+
value val_default = mk_val<value_undefined>();
|
|
169
|
+
|
|
170
|
+
if (args.count() == 2) {
|
|
171
|
+
// example: array | selectattr("active")
|
|
172
|
+
for (const auto & item : arr) {
|
|
173
|
+
if (!is_val<value_object>(item)) {
|
|
174
|
+
throw raised_exception("selectattr: item is not an object");
|
|
175
|
+
}
|
|
176
|
+
value attr_val = item->at(attr_name, val_default);
|
|
177
|
+
bool is_selected = attr_val->as_bool();
|
|
178
|
+
if constexpr (is_reject) is_selected = !is_selected;
|
|
179
|
+
if (is_selected) out->push_back(item);
|
|
180
|
+
}
|
|
181
|
+
return out;
|
|
182
|
+
|
|
183
|
+
} else if (args.count() == 3) {
|
|
184
|
+
// example: array | selectattr("equalto", "text")
|
|
185
|
+
// translated to: test_is_equalto(item, "text")
|
|
186
|
+
std::string test_name = args.get_pos(1)->as_string().str();
|
|
187
|
+
value test_val = args.get_pos(2);
|
|
188
|
+
auto & builtins = global_builtins();
|
|
189
|
+
auto it = builtins.find("test_is_" + test_name);
|
|
190
|
+
if (it == builtins.end()) {
|
|
191
|
+
throw raised_exception("selectattr: unknown test '" + test_name + "'");
|
|
192
|
+
}
|
|
193
|
+
auto test_fn = it->second;
|
|
194
|
+
for (const auto & item : arr) {
|
|
195
|
+
func_args test_args(args.ctx);
|
|
196
|
+
test_args.push_back(item); // current object
|
|
197
|
+
test_args.push_back(test_val); // extra argument
|
|
198
|
+
value test_result = test_fn(test_args);
|
|
199
|
+
bool is_selected = test_result->as_bool();
|
|
200
|
+
if constexpr (is_reject) is_selected = !is_selected;
|
|
201
|
+
if (is_selected) out->push_back(item);
|
|
202
|
+
}
|
|
203
|
+
return out;
|
|
204
|
+
|
|
205
|
+
} else if (args.count() == 4) {
|
|
206
|
+
// example: array | selectattr("status", "equalto", "active")
|
|
207
|
+
// translated to: test_is_equalto(item.status, "active")
|
|
208
|
+
std::string test_name = args.get_pos(2)->as_string().str();
|
|
209
|
+
auto extra_arg = args.get_pos(3);
|
|
210
|
+
auto & builtins = global_builtins();
|
|
211
|
+
auto it = builtins.find("test_is_" + test_name);
|
|
212
|
+
if (it == builtins.end()) {
|
|
213
|
+
throw raised_exception("selectattr: unknown test '" + test_name + "'");
|
|
214
|
+
}
|
|
215
|
+
auto test_fn = it->second;
|
|
216
|
+
for (const auto & item : arr) {
|
|
217
|
+
if (!is_val<value_object>(item)) {
|
|
218
|
+
throw raised_exception("selectattr: item is not an object");
|
|
219
|
+
}
|
|
220
|
+
value attr_val = item->at(attr_name, val_default);
|
|
221
|
+
func_args test_args(args.ctx);
|
|
222
|
+
test_args.push_back(attr_val); // attribute value
|
|
223
|
+
test_args.push_back(extra_arg); // extra argument
|
|
224
|
+
value test_result = test_fn(test_args);
|
|
225
|
+
bool is_selected = test_result->as_bool();
|
|
226
|
+
if constexpr (is_reject) is_selected = !is_selected;
|
|
227
|
+
if (is_selected) out->push_back(item);
|
|
228
|
+
}
|
|
229
|
+
return out;
|
|
230
|
+
} else {
|
|
231
|
+
throw raised_exception("selectattr: invalid number of arguments");
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return out;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
static value default_value(const func_args & args) {
|
|
238
|
+
args.ensure_count(2, 3);
|
|
239
|
+
value val_check = args.get_kwarg_or_pos("boolean", 2);
|
|
240
|
+
bool check_bool = val_check->as_bool(); // undefined == false
|
|
241
|
+
bool no_value = check_bool
|
|
242
|
+
? (!args.get_pos(0)->as_bool())
|
|
243
|
+
: (args.get_pos(0)->is_undefined() || args.get_pos(0)->is_none());
|
|
244
|
+
return no_value ? args.get_pos(1) : args.get_pos(0);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const func_builtins & global_builtins() {
|
|
248
|
+
static const func_builtins builtins = {
|
|
249
|
+
{"raise_exception", [](const func_args & args) -> value {
|
|
250
|
+
args.ensure_vals<value_string>();
|
|
251
|
+
std::string msg = args.get_pos(0)->as_string().str();
|
|
252
|
+
throw raised_exception("Jinja Exception: " + msg);
|
|
253
|
+
}},
|
|
254
|
+
{"namespace", [](const func_args & args) -> value {
|
|
255
|
+
auto out = mk_val<value_object>();
|
|
256
|
+
for (const auto & arg : args.get_args()) {
|
|
257
|
+
if (!is_val<value_kwarg>(arg)) {
|
|
258
|
+
throw raised_exception("namespace() arguments must be kwargs");
|
|
259
|
+
}
|
|
260
|
+
auto kwarg = cast_val<value_kwarg>(arg);
|
|
261
|
+
JJ_DEBUG("namespace: adding key '%s'", kwarg->key.c_str());
|
|
262
|
+
out->insert(kwarg->key, kwarg->val);
|
|
263
|
+
}
|
|
264
|
+
return out;
|
|
265
|
+
}},
|
|
266
|
+
{"strftime_now", [](const func_args & args) -> value {
|
|
267
|
+
args.ensure_vals<value_string>();
|
|
268
|
+
std::string format = args.get_pos(0)->as_string().str();
|
|
269
|
+
// get current time
|
|
270
|
+
// TODO: make sure this is the same behavior as Python's strftime
|
|
271
|
+
char buf[100];
|
|
272
|
+
if (std::strftime(buf, sizeof(buf), format.c_str(), std::localtime(&args.ctx.current_time))) {
|
|
273
|
+
return mk_val<value_string>(std::string(buf));
|
|
274
|
+
} else {
|
|
275
|
+
throw raised_exception("strftime_now: failed to format time");
|
|
276
|
+
}
|
|
277
|
+
}},
|
|
278
|
+
{"range", [](const func_args & args) -> value {
|
|
279
|
+
args.ensure_count(1, 3);
|
|
280
|
+
args.ensure_vals<value_int, value_int, value_int>(true, false, false);
|
|
281
|
+
|
|
282
|
+
auto arg0 = args.get_pos(0);
|
|
283
|
+
auto arg1 = args.get_pos(1, mk_val<value_undefined>());
|
|
284
|
+
auto arg2 = args.get_pos(2, mk_val<value_undefined>());
|
|
285
|
+
|
|
286
|
+
int64_t start, stop, step;
|
|
287
|
+
if (args.count() == 1) {
|
|
288
|
+
start = 0;
|
|
289
|
+
stop = arg0->as_int();
|
|
290
|
+
step = 1;
|
|
291
|
+
} else if (args.count() == 2) {
|
|
292
|
+
start = arg0->as_int();
|
|
293
|
+
stop = arg1->as_int();
|
|
294
|
+
step = 1;
|
|
295
|
+
} else {
|
|
296
|
+
start = arg0->as_int();
|
|
297
|
+
stop = arg1->as_int();
|
|
298
|
+
step = arg2->as_int();
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
auto out = mk_val<value_array>();
|
|
302
|
+
if (step == 0) {
|
|
303
|
+
throw raised_exception("range() step argument must not be zero");
|
|
304
|
+
}
|
|
305
|
+
if (step > 0) {
|
|
306
|
+
for (int64_t i = start; i < stop; i += step) {
|
|
307
|
+
out->push_back(mk_val<value_int>(i));
|
|
308
|
+
}
|
|
309
|
+
} else {
|
|
310
|
+
for (int64_t i = start; i > stop; i += step) {
|
|
311
|
+
out->push_back(mk_val<value_int>(i));
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return out;
|
|
315
|
+
}},
|
|
316
|
+
{"tojson", tojson},
|
|
317
|
+
|
|
318
|
+
// tests
|
|
319
|
+
{"test_is_boolean", test_type_fn<value_bool>},
|
|
320
|
+
{"test_is_callable", test_type_fn<value_func>},
|
|
321
|
+
{"test_is_odd", [](const func_args & args) -> value {
|
|
322
|
+
args.ensure_vals<value_int>();
|
|
323
|
+
int64_t val = args.get_pos(0)->as_int();
|
|
324
|
+
return mk_val<value_bool>(val % 2 != 0);
|
|
325
|
+
}},
|
|
326
|
+
{"test_is_even", [](const func_args & args) -> value {
|
|
327
|
+
args.ensure_vals<value_int>();
|
|
328
|
+
int64_t val = args.get_pos(0)->as_int();
|
|
329
|
+
return mk_val<value_bool>(val % 2 == 0);
|
|
330
|
+
}},
|
|
331
|
+
{"test_is_false", [](const func_args & args) -> value {
|
|
332
|
+
args.ensure_count(1);
|
|
333
|
+
bool val = is_val<value_bool>(args.get_pos(0)) && !args.get_pos(0)->as_bool();
|
|
334
|
+
return mk_val<value_bool>(val);
|
|
335
|
+
}},
|
|
336
|
+
{"test_is_true", [](const func_args & args) -> value {
|
|
337
|
+
args.ensure_count(1);
|
|
338
|
+
bool val = is_val<value_bool>(args.get_pos(0)) && args.get_pos(0)->as_bool();
|
|
339
|
+
return mk_val<value_bool>(val);
|
|
340
|
+
}},
|
|
341
|
+
{"test_is_divisibleby", [](const func_args & args) -> value {
|
|
342
|
+
args.ensure_vals<value_int, value_int>();
|
|
343
|
+
bool res = args.get_pos(0)->val_int % args.get_pos(1)->val_int == 0;
|
|
344
|
+
return mk_val<value_bool>(res);
|
|
345
|
+
}},
|
|
346
|
+
{"test_is_string", test_type_fn<value_string>},
|
|
347
|
+
{"test_is_integer", test_type_fn<value_int>},
|
|
348
|
+
{"test_is_float", test_type_fn<value_float>},
|
|
349
|
+
{"test_is_number", test_type_fn<value_int, value_float>},
|
|
350
|
+
{"test_is_iterable", test_type_fn<value_array, value_string>},
|
|
351
|
+
{"test_is_sequence", test_type_fn<value_array, value_string>},
|
|
352
|
+
{"test_is_mapping", test_type_fn<value_object>},
|
|
353
|
+
{"test_is_lower", [](const func_args & args) -> value {
|
|
354
|
+
args.ensure_vals<value_string>();
|
|
355
|
+
return mk_val<value_bool>(args.get_pos(0)->val_str.is_lowercase());
|
|
356
|
+
}},
|
|
357
|
+
{"test_is_upper", [](const func_args & args) -> value {
|
|
358
|
+
args.ensure_vals<value_string>();
|
|
359
|
+
return mk_val<value_bool>(args.get_pos(0)->val_str.is_uppercase());
|
|
360
|
+
}},
|
|
361
|
+
{"test_is_none", test_type_fn<value_none>},
|
|
362
|
+
{"test_is_defined", [](const func_args & args) -> value {
|
|
363
|
+
args.ensure_count(1);
|
|
364
|
+
bool res = !args.get_pos(0)->is_undefined();
|
|
365
|
+
JJ_DEBUG("test_is_defined: result=%d", res ? 1 : 0);
|
|
366
|
+
return mk_val<value_bool>(res);
|
|
367
|
+
}},
|
|
368
|
+
{"test_is_undefined", test_type_fn<value_undefined>},
|
|
369
|
+
{"test_is_eq", test_compare_fn<value_compare_op::eq>},
|
|
370
|
+
{"test_is_equalto", test_compare_fn<value_compare_op::eq>},
|
|
371
|
+
{"test_is_ge", test_compare_fn<value_compare_op::ge>},
|
|
372
|
+
{"test_is_gt", test_compare_fn<value_compare_op::gt>},
|
|
373
|
+
{"test_is_greaterthan", test_compare_fn<value_compare_op::gt>},
|
|
374
|
+
{"test_is_lt", test_compare_fn<value_compare_op::lt>},
|
|
375
|
+
{"test_is_lessthan", test_compare_fn<value_compare_op::lt>},
|
|
376
|
+
{"test_is_ne", test_compare_fn<value_compare_op::ne>},
|
|
377
|
+
{"test_is_test", [](const func_args & args) -> value {
|
|
378
|
+
args.ensure_vals<value_string>();
|
|
379
|
+
auto & builtins = global_builtins();
|
|
380
|
+
std::string test_name = args.get_pos(0)->val_str.str();
|
|
381
|
+
auto it = builtins.find("test_is_" + test_name);
|
|
382
|
+
bool res = it != builtins.end();
|
|
383
|
+
return mk_val<value_bool>(res);
|
|
384
|
+
}},
|
|
385
|
+
{"test_is_sameas", [](const func_args & args) -> value {
|
|
386
|
+
// Check if an object points to the same memory address as another object
|
|
387
|
+
(void)args;
|
|
388
|
+
throw not_implemented_exception("sameas test not implemented");
|
|
389
|
+
}},
|
|
390
|
+
{"test_is_escaped", [](const func_args & args) -> value {
|
|
391
|
+
(void)args;
|
|
392
|
+
throw not_implemented_exception("escaped test not implemented");
|
|
393
|
+
}},
|
|
394
|
+
{"test_is_filter", [](const func_args & args) -> value {
|
|
395
|
+
(void)args;
|
|
396
|
+
throw not_implemented_exception("filter test not implemented");
|
|
397
|
+
}},
|
|
398
|
+
};
|
|
399
|
+
return builtins;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
const func_builtins & value_int_t::get_builtins() const {
|
|
404
|
+
static const func_builtins builtins = {
|
|
405
|
+
{"default", default_value},
|
|
406
|
+
{"abs", [](const func_args & args) -> value {
|
|
407
|
+
args.ensure_vals<value_int>();
|
|
408
|
+
int64_t val = args.get_pos(0)->as_int();
|
|
409
|
+
return mk_val<value_int>(val < 0 ? -val : val);
|
|
410
|
+
}},
|
|
411
|
+
{"float", [](const func_args & args) -> value {
|
|
412
|
+
args.ensure_vals<value_int>();
|
|
413
|
+
double val = static_cast<double>(args.get_pos(0)->as_int());
|
|
414
|
+
return mk_val<value_float>(val);
|
|
415
|
+
}},
|
|
416
|
+
{"tojson", tojson},
|
|
417
|
+
{"string", tojson},
|
|
418
|
+
};
|
|
419
|
+
return builtins;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
const func_builtins & value_float_t::get_builtins() const {
|
|
424
|
+
static const func_builtins builtins = {
|
|
425
|
+
{"default", default_value},
|
|
426
|
+
{"abs", [](const func_args & args) -> value {
|
|
427
|
+
args.ensure_vals<value_float>();
|
|
428
|
+
double val = args.get_pos(0)->as_float();
|
|
429
|
+
return mk_val<value_float>(val < 0.0 ? -val : val);
|
|
430
|
+
}},
|
|
431
|
+
{"int", [](const func_args & args) -> value {
|
|
432
|
+
args.ensure_vals<value_float>();
|
|
433
|
+
int64_t val = static_cast<int64_t>(args.get_pos(0)->as_float());
|
|
434
|
+
return mk_val<value_int>(val);
|
|
435
|
+
}},
|
|
436
|
+
{"tojson", tojson},
|
|
437
|
+
{"string", tojson},
|
|
438
|
+
};
|
|
439
|
+
return builtins;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
static bool string_startswith(const std::string & str, const std::string & prefix) {
|
|
443
|
+
if (str.length() < prefix.length()) return false;
|
|
444
|
+
return str.compare(0, prefix.length(), prefix) == 0;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
static bool string_endswith(const std::string & str, const std::string & suffix) {
|
|
448
|
+
if (str.length() < suffix.length()) return false;
|
|
449
|
+
return str.compare(str.length() - suffix.length(), suffix.length(), suffix) == 0;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
const func_builtins & value_string_t::get_builtins() const {
|
|
453
|
+
static const func_builtins builtins = {
|
|
454
|
+
{"default", default_value},
|
|
455
|
+
{"upper", [](const func_args & args) -> value {
|
|
456
|
+
args.ensure_vals<value_string>();
|
|
457
|
+
jinja::string str = args.get_pos(0)->as_string().uppercase();
|
|
458
|
+
return mk_val<value_string>(str);
|
|
459
|
+
}},
|
|
460
|
+
{"lower", [](const func_args & args) -> value {
|
|
461
|
+
args.ensure_vals<value_string>();
|
|
462
|
+
jinja::string str = args.get_pos(0)->as_string().lowercase();
|
|
463
|
+
return mk_val<value_string>(str);
|
|
464
|
+
}},
|
|
465
|
+
{"strip", [](const func_args & args) -> value {
|
|
466
|
+
value val_input = args.get_pos(0);
|
|
467
|
+
if (!is_val<value_string>(val_input)) {
|
|
468
|
+
throw raised_exception("strip() first argument must be a string");
|
|
469
|
+
}
|
|
470
|
+
value val_chars = args.get_kwarg_or_pos("chars", 1);
|
|
471
|
+
if (val_chars->is_undefined()) {
|
|
472
|
+
return mk_val<value_string>(args.get_pos(0)->as_string().strip(true, true));
|
|
473
|
+
} else {
|
|
474
|
+
return mk_val<value_string>(args.get_pos(0)->as_string().strip(true, true, val_chars->as_string().str()));
|
|
475
|
+
}
|
|
476
|
+
}},
|
|
477
|
+
{"rstrip", [](const func_args & args) -> value {
|
|
478
|
+
args.ensure_vals<value_string>();
|
|
479
|
+
value val_chars = args.get_kwarg_or_pos("chars", 1);
|
|
480
|
+
if (val_chars->is_undefined()) {
|
|
481
|
+
return mk_val<value_string>(args.get_pos(0)->as_string().strip(false, true));
|
|
482
|
+
} else {
|
|
483
|
+
return mk_val<value_string>(args.get_pos(0)->as_string().strip(false, true, val_chars->as_string().str()));
|
|
484
|
+
}
|
|
485
|
+
}},
|
|
486
|
+
{"lstrip", [](const func_args & args) -> value {
|
|
487
|
+
args.ensure_vals<value_string>();
|
|
488
|
+
value val_chars = args.get_kwarg_or_pos("chars", 1);
|
|
489
|
+
if (val_chars->is_undefined()) {
|
|
490
|
+
return mk_val<value_string>(args.get_pos(0)->as_string().strip(true, false));
|
|
491
|
+
} else {
|
|
492
|
+
return mk_val<value_string>(args.get_pos(0)->as_string().strip(true, false, val_chars->as_string().str()));
|
|
493
|
+
}
|
|
494
|
+
}},
|
|
495
|
+
{"title", [](const func_args & args) -> value {
|
|
496
|
+
args.ensure_vals<value_string>();
|
|
497
|
+
jinja::string str = args.get_pos(0)->as_string().titlecase();
|
|
498
|
+
return mk_val<value_string>(str);
|
|
499
|
+
}},
|
|
500
|
+
{"capitalize", [](const func_args & args) -> value {
|
|
501
|
+
args.ensure_vals<value_string>();
|
|
502
|
+
jinja::string str = args.get_pos(0)->as_string().capitalize();
|
|
503
|
+
return mk_val<value_string>(str);
|
|
504
|
+
}},
|
|
505
|
+
{"length", [](const func_args & args) -> value {
|
|
506
|
+
args.ensure_vals<value_string>();
|
|
507
|
+
jinja::string str = args.get_pos(0)->as_string();
|
|
508
|
+
return mk_val<value_int>(str.length());
|
|
509
|
+
}},
|
|
510
|
+
{"startswith", [](const func_args & args) -> value {
|
|
511
|
+
args.ensure_vals<value_string, value_string>();
|
|
512
|
+
std::string str = args.get_pos(0)->as_string().str();
|
|
513
|
+
std::string prefix = args.get_pos(1)->as_string().str();
|
|
514
|
+
return mk_val<value_bool>(string_startswith(str, prefix));
|
|
515
|
+
}},
|
|
516
|
+
{"endswith", [](const func_args & args) -> value {
|
|
517
|
+
args.ensure_vals<value_string, value_string>();
|
|
518
|
+
std::string str = args.get_pos(0)->as_string().str();
|
|
519
|
+
std::string suffix = args.get_pos(1)->as_string().str();
|
|
520
|
+
return mk_val<value_bool>(string_endswith(str, suffix));
|
|
521
|
+
}},
|
|
522
|
+
{"split", [](const func_args & args) -> value {
|
|
523
|
+
args.ensure_count(1, 3);
|
|
524
|
+
value val_input = args.get_pos(0);
|
|
525
|
+
if (!is_val<value_string>(val_input)) {
|
|
526
|
+
throw raised_exception("split() first argument must be a string");
|
|
527
|
+
}
|
|
528
|
+
std::string str = val_input->as_string().str();
|
|
529
|
+
// FIXME: Support non-specified delimiter (split on consecutive (no leading or trailing) whitespace)
|
|
530
|
+
std::string delim = (args.count() > 1) ? args.get_pos(1)->as_string().str() : " ";
|
|
531
|
+
int64_t maxsplit = (args.count() > 2) ? args.get_pos(2)->as_int() : -1;
|
|
532
|
+
auto result = mk_val<value_array>();
|
|
533
|
+
size_t pos = 0;
|
|
534
|
+
std::string token;
|
|
535
|
+
while ((pos = str.find(delim)) != std::string::npos && maxsplit != 0) {
|
|
536
|
+
token = str.substr(0, pos);
|
|
537
|
+
result->push_back(mk_val<value_string>(token));
|
|
538
|
+
str.erase(0, pos + delim.length());
|
|
539
|
+
--maxsplit;
|
|
540
|
+
}
|
|
541
|
+
auto res = mk_val<value_string>(str);
|
|
542
|
+
res->val_str.mark_input_based_on(args.get_pos(0)->val_str);
|
|
543
|
+
result->push_back(std::move(res));
|
|
544
|
+
return result;
|
|
545
|
+
}},
|
|
546
|
+
{"rsplit", [](const func_args & args) -> value {
|
|
547
|
+
args.ensure_count(1, 3);
|
|
548
|
+
value val_input = args.get_pos(0);
|
|
549
|
+
if (!is_val<value_string>(val_input)) {
|
|
550
|
+
throw raised_exception("rsplit() first argument must be a string");
|
|
551
|
+
}
|
|
552
|
+
std::string str = val_input->as_string().str();
|
|
553
|
+
// FIXME: Support non-specified delimiter (split on consecutive (no leading or trailing) whitespace)
|
|
554
|
+
std::string delim = (args.count() > 1) ? args.get_pos(1)->as_string().str() : " ";
|
|
555
|
+
int64_t maxsplit = (args.count() > 2) ? args.get_pos(2)->as_int() : -1;
|
|
556
|
+
auto result = mk_val<value_array>();
|
|
557
|
+
size_t pos = 0;
|
|
558
|
+
std::string token;
|
|
559
|
+
while ((pos = str.rfind(delim)) != std::string::npos && maxsplit != 0) {
|
|
560
|
+
token = str.substr(pos + delim.length());
|
|
561
|
+
result->push_back(mk_val<value_string>(token));
|
|
562
|
+
str.erase(pos);
|
|
563
|
+
--maxsplit;
|
|
564
|
+
}
|
|
565
|
+
auto res = mk_val<value_string>(str);
|
|
566
|
+
res->val_str.mark_input_based_on(args.get_pos(0)->val_str);
|
|
567
|
+
result->push_back(std::move(res));
|
|
568
|
+
result->reverse();
|
|
569
|
+
return result;
|
|
570
|
+
}},
|
|
571
|
+
{"replace", [](const func_args & args) -> value {
|
|
572
|
+
args.ensure_vals<value_string, value_string, value_string, value_int>(true, true, true, false);
|
|
573
|
+
std::string str = args.get_pos(0)->as_string().str();
|
|
574
|
+
std::string old_str = args.get_pos(1)->as_string().str();
|
|
575
|
+
std::string new_str = args.get_pos(2)->as_string().str();
|
|
576
|
+
int64_t count = args.count() > 3 ? args.get_pos(3)->as_int() : -1;
|
|
577
|
+
if (count > 0) {
|
|
578
|
+
throw not_implemented_exception("String replace with count argument not implemented");
|
|
579
|
+
}
|
|
580
|
+
size_t pos = 0;
|
|
581
|
+
while ((pos = str.find(old_str, pos)) != std::string::npos) {
|
|
582
|
+
str.replace(pos, old_str.length(), new_str);
|
|
583
|
+
pos += new_str.length();
|
|
584
|
+
}
|
|
585
|
+
auto res = mk_val<value_string>(str);
|
|
586
|
+
res->val_str.mark_input_based_on(args.get_pos(0)->val_str);
|
|
587
|
+
return res;
|
|
588
|
+
}},
|
|
589
|
+
{"int", [](const func_args & args) -> value {
|
|
590
|
+
value val_input = args.get_pos(0);
|
|
591
|
+
value val_default = args.get_kwarg_or_pos("default", 1);
|
|
592
|
+
value val_base = args.get_kwarg_or_pos("base", 2);
|
|
593
|
+
const int base = val_base->is_undefined() ? 10 : val_base->as_int();
|
|
594
|
+
if (is_val<value_string>(val_input) == false) {
|
|
595
|
+
throw raised_exception("int() first argument must be a string");
|
|
596
|
+
}
|
|
597
|
+
std::string str = val_input->as_string().str();
|
|
598
|
+
try {
|
|
599
|
+
return mk_val<value_int>(std::stoi(str, nullptr, base));
|
|
600
|
+
} catch (...) {
|
|
601
|
+
return mk_val<value_int>(val_default->is_undefined() ? 0 : val_default->as_int());
|
|
602
|
+
}
|
|
603
|
+
}},
|
|
604
|
+
{"float", [](const func_args & args) -> value {
|
|
605
|
+
args.ensure_vals<value_string>();
|
|
606
|
+
value val_default = args.get_kwarg_or_pos("default", 1);
|
|
607
|
+
std::string str = args.get_pos(0)->as_string().str();
|
|
608
|
+
try {
|
|
609
|
+
return mk_val<value_float>(std::stod(str));
|
|
610
|
+
} catch (...) {
|
|
611
|
+
return mk_val<value_float>(val_default->is_undefined() ? 0.0 : val_default->as_float());
|
|
612
|
+
}
|
|
613
|
+
}},
|
|
614
|
+
{"string", [](const func_args & args) -> value {
|
|
615
|
+
// no-op
|
|
616
|
+
args.ensure_vals<value_string>();
|
|
617
|
+
return mk_val<value_string>(args.get_pos(0)->as_string());
|
|
618
|
+
}},
|
|
619
|
+
{"default", [](const func_args & args) -> value {
|
|
620
|
+
value input = args.get_pos(0);
|
|
621
|
+
if (!is_val<value_string>(input)) {
|
|
622
|
+
throw raised_exception("default() first argument must be a string");
|
|
623
|
+
}
|
|
624
|
+
value default_val = mk_val<value_string>("");
|
|
625
|
+
if (args.count() > 1 && !args.get_pos(1)->is_undefined()) {
|
|
626
|
+
default_val = args.get_pos(1);
|
|
627
|
+
}
|
|
628
|
+
value boolean_val = args.get_kwarg_or_pos("boolean", 2); // undefined == false
|
|
629
|
+
if (input->is_undefined() || (boolean_val->as_bool() && !input->as_bool())) {
|
|
630
|
+
return default_val;
|
|
631
|
+
} else {
|
|
632
|
+
return input;
|
|
633
|
+
}
|
|
634
|
+
}},
|
|
635
|
+
{"slice", [](const func_args & args) -> value {
|
|
636
|
+
args.ensure_count(1, 4);
|
|
637
|
+
args.ensure_vals<value_string, value_int, value_int, value_int>(true, true, false, false);
|
|
638
|
+
|
|
639
|
+
auto arg0 = args.get_pos(1);
|
|
640
|
+
auto arg1 = args.get_pos(2, mk_val<value_undefined>());
|
|
641
|
+
auto arg2 = args.get_pos(3, mk_val<value_undefined>());
|
|
642
|
+
|
|
643
|
+
int64_t start, stop, step;
|
|
644
|
+
if (args.count() == 1) {
|
|
645
|
+
start = 0;
|
|
646
|
+
stop = arg0->as_int();
|
|
647
|
+
step = 1;
|
|
648
|
+
} else if (args.count() == 2) {
|
|
649
|
+
start = arg0->as_int();
|
|
650
|
+
stop = arg1->as_int();
|
|
651
|
+
step = 1;
|
|
652
|
+
} else {
|
|
653
|
+
start = arg0->as_int();
|
|
654
|
+
stop = arg1->as_int();
|
|
655
|
+
step = arg2->as_int();
|
|
656
|
+
}
|
|
657
|
+
if (step == 0) {
|
|
658
|
+
throw raised_exception("slice step cannot be zero");
|
|
659
|
+
}
|
|
660
|
+
auto input = args.get_pos(0);
|
|
661
|
+
auto sliced = slice(input->as_string().str(), start, stop, step);
|
|
662
|
+
auto res = mk_val<value_string>(sliced);
|
|
663
|
+
res->val_str.mark_input_based_on(input->as_string());
|
|
664
|
+
return res;
|
|
665
|
+
}},
|
|
666
|
+
{"safe", [](const func_args & args) -> value {
|
|
667
|
+
// no-op for now
|
|
668
|
+
args.ensure_vals<value_string>();
|
|
669
|
+
return args.get_pos(0);
|
|
670
|
+
}},
|
|
671
|
+
{"tojson", tojson},
|
|
672
|
+
{"indent", [](const func_args &) -> value {
|
|
673
|
+
throw not_implemented_exception("String indent builtin not implemented");
|
|
674
|
+
}},
|
|
675
|
+
{"join", [](const func_args &) -> value {
|
|
676
|
+
throw not_implemented_exception("String join builtin not implemented");
|
|
677
|
+
}},
|
|
678
|
+
};
|
|
679
|
+
return builtins;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
const func_builtins & value_bool_t::get_builtins() const {
|
|
684
|
+
static const func_builtins builtins = {
|
|
685
|
+
{"default", default_value},
|
|
686
|
+
{"int", [](const func_args & args) -> value {
|
|
687
|
+
args.ensure_vals<value_bool>();
|
|
688
|
+
bool val = args.get_pos(0)->as_bool();
|
|
689
|
+
return mk_val<value_int>(val ? 1 : 0);
|
|
690
|
+
}},
|
|
691
|
+
{"float", [](const func_args & args) -> value {
|
|
692
|
+
args.ensure_vals<value_bool>();
|
|
693
|
+
bool val = args.get_pos(0)->as_bool();
|
|
694
|
+
return mk_val<value_float>(val ? 1.0 : 0.0);
|
|
695
|
+
}},
|
|
696
|
+
{"string", [](const func_args & args) -> value {
|
|
697
|
+
args.ensure_vals<value_bool>();
|
|
698
|
+
bool val = args.get_pos(0)->as_bool();
|
|
699
|
+
return mk_val<value_string>(val ? "True" : "False");
|
|
700
|
+
}},
|
|
701
|
+
{"tojson", tojson},
|
|
702
|
+
};
|
|
703
|
+
return builtins;
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
|
|
707
|
+
const func_builtins & value_array_t::get_builtins() const {
|
|
708
|
+
static const func_builtins builtins = {
|
|
709
|
+
{"default", default_value},
|
|
710
|
+
{"list", [](const func_args & args) -> value {
|
|
711
|
+
args.ensure_vals<value_array>();
|
|
712
|
+
const auto & arr = args.get_pos(0)->as_array();
|
|
713
|
+
auto result = mk_val<value_array>();
|
|
714
|
+
for (const auto& v : arr) {
|
|
715
|
+
result->push_back(v);
|
|
716
|
+
}
|
|
717
|
+
return result;
|
|
718
|
+
}},
|
|
719
|
+
{"first", [](const func_args & args) -> value {
|
|
720
|
+
args.ensure_vals<value_array>();
|
|
721
|
+
const auto & arr = args.get_pos(0)->as_array();
|
|
722
|
+
if (arr.empty()) {
|
|
723
|
+
return mk_val<value_undefined>();
|
|
724
|
+
}
|
|
725
|
+
return arr[0];
|
|
726
|
+
}},
|
|
727
|
+
{"last", [](const func_args & args) -> value {
|
|
728
|
+
args.ensure_vals<value_array>();
|
|
729
|
+
const auto & arr = args.get_pos(0)->as_array();
|
|
730
|
+
if (arr.empty()) {
|
|
731
|
+
return mk_val<value_undefined>();
|
|
732
|
+
}
|
|
733
|
+
return arr[arr.size() - 1];
|
|
734
|
+
}},
|
|
735
|
+
{"length", [](const func_args & args) -> value {
|
|
736
|
+
args.ensure_vals<value_array>();
|
|
737
|
+
const auto & arr = args.get_pos(0)->as_array();
|
|
738
|
+
return mk_val<value_int>(static_cast<int64_t>(arr.size()));
|
|
739
|
+
}},
|
|
740
|
+
{"slice", [](const func_args & args) -> value {
|
|
741
|
+
args.ensure_count(1, 4);
|
|
742
|
+
args.ensure_vals<value_array, value_int, value_int, value_int>(true, true, false, false);
|
|
743
|
+
|
|
744
|
+
auto arg0 = args.get_pos(1);
|
|
745
|
+
auto arg1 = args.get_pos(2, mk_val<value_undefined>());
|
|
746
|
+
auto arg2 = args.get_pos(3, mk_val<value_undefined>());
|
|
747
|
+
|
|
748
|
+
int64_t start, stop, step;
|
|
749
|
+
if (args.count() == 1) {
|
|
750
|
+
start = 0;
|
|
751
|
+
stop = arg0->as_int();
|
|
752
|
+
step = 1;
|
|
753
|
+
} else if (args.count() == 2) {
|
|
754
|
+
start = arg0->as_int();
|
|
755
|
+
stop = arg1->as_int();
|
|
756
|
+
step = 1;
|
|
757
|
+
} else {
|
|
758
|
+
start = arg0->as_int();
|
|
759
|
+
stop = arg1->as_int();
|
|
760
|
+
step = arg2->as_int();
|
|
761
|
+
}
|
|
762
|
+
if (step == 0) {
|
|
763
|
+
throw raised_exception("slice step cannot be zero");
|
|
764
|
+
}
|
|
765
|
+
auto arr = slice(args.get_pos(0)->as_array(), start, stop, step);
|
|
766
|
+
auto res = mk_val<value_array>();
|
|
767
|
+
res->val_arr = std::move(arr);
|
|
768
|
+
return res;
|
|
769
|
+
}},
|
|
770
|
+
{"selectattr", selectattr<false>},
|
|
771
|
+
{"select", selectattr<false>},
|
|
772
|
+
{"rejectattr", selectattr<true>},
|
|
773
|
+
{"reject", selectattr<true>},
|
|
774
|
+
{"join", [](const func_args & args) -> value {
|
|
775
|
+
args.ensure_count(1, 3);
|
|
776
|
+
if (!is_val<value_array>(args.get_pos(0))) {
|
|
777
|
+
throw raised_exception("join() first argument must be an array");
|
|
778
|
+
}
|
|
779
|
+
value val_delim = args.get_kwarg_or_pos("d", 1);
|
|
780
|
+
value attribute = args.get_kwarg_or_pos("attribute", 2);
|
|
781
|
+
const auto & arr = args.get_pos(0)->as_array();
|
|
782
|
+
const bool attr_is_int = is_val<value_int>(attribute);
|
|
783
|
+
if (!attribute->is_undefined() && !is_val<value_string>(attribute) && !attr_is_int) {
|
|
784
|
+
throw raised_exception("join() attribute must be string or integer");
|
|
785
|
+
}
|
|
786
|
+
const int64_t attr_int = attr_is_int ? attribute->as_int() : 0;
|
|
787
|
+
const std::string delim = val_delim->is_undefined() ? "" : val_delim->as_string().str();
|
|
788
|
+
const std::string attr_name = attribute->is_undefined() ? "" : attribute->as_string().str();
|
|
789
|
+
std::string result;
|
|
790
|
+
for (size_t i = 0; i < arr.size(); ++i) {
|
|
791
|
+
value val_arr = arr[i];
|
|
792
|
+
if (!attribute->is_undefined()) {
|
|
793
|
+
if (attr_is_int && is_val<value_array>(val_arr)) {
|
|
794
|
+
val_arr = val_arr->at(attr_int);
|
|
795
|
+
} else if (!attr_is_int && !attr_name.empty() && is_val<value_object>(val_arr)) {
|
|
796
|
+
val_arr = val_arr->at(attr_name);
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
if (!is_val<value_string>(val_arr) && !is_val<value_int>(val_arr) && !is_val<value_float>(val_arr)) {
|
|
800
|
+
throw raised_exception("join() can only join arrays of strings or numerics");
|
|
801
|
+
}
|
|
802
|
+
result += val_arr->as_string().str();
|
|
803
|
+
if (i < arr.size() - 1) {
|
|
804
|
+
result += delim;
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
return mk_val<value_string>(result);
|
|
808
|
+
}},
|
|
809
|
+
{"string", [](const func_args & args) -> value {
|
|
810
|
+
args.ensure_vals<value_array>();
|
|
811
|
+
auto str = mk_val<value_string>();
|
|
812
|
+
gather_string_parts_recursive(args.get_pos(0), str);
|
|
813
|
+
return str;
|
|
814
|
+
}},
|
|
815
|
+
{"tojson", tojson},
|
|
816
|
+
{"map", [](const func_args & args) -> value {
|
|
817
|
+
args.ensure_count(2);
|
|
818
|
+
if (!is_val<value_array>(args.get_pos(0))) {
|
|
819
|
+
throw raised_exception("map: first argument must be an array");
|
|
820
|
+
}
|
|
821
|
+
if (!is_val<value_kwarg>(args.get_args().at(1))) {
|
|
822
|
+
throw not_implemented_exception("map: filter-mapping not implemented");
|
|
823
|
+
}
|
|
824
|
+
value attribute = args.get_kwarg_or_pos("attribute", 1);
|
|
825
|
+
const bool attr_is_int = is_val<value_int>(attribute);
|
|
826
|
+
if (!is_val<value_string>(attribute) && !attr_is_int) {
|
|
827
|
+
throw raised_exception("map: attribute must be string or integer");
|
|
828
|
+
}
|
|
829
|
+
const int64_t attr_int = attr_is_int ? attribute->as_int() : 0;
|
|
830
|
+
const std::string attr_name = attribute->as_string().str();
|
|
831
|
+
value default_val = args.get_kwarg("default", mk_val<value_undefined>());
|
|
832
|
+
auto out = mk_val<value_array>();
|
|
833
|
+
auto arr = args.get_pos(0)->as_array();
|
|
834
|
+
for (const auto & item : arr) {
|
|
835
|
+
value attr_val;
|
|
836
|
+
if (attr_is_int) {
|
|
837
|
+
attr_val = is_val<value_array>(item) ? item->at(attr_int, default_val) : default_val;
|
|
838
|
+
} else {
|
|
839
|
+
attr_val = is_val<value_object>(item) ? item->at(attr_name, default_val) : default_val;
|
|
840
|
+
}
|
|
841
|
+
out->push_back(attr_val);
|
|
842
|
+
}
|
|
843
|
+
return out;
|
|
844
|
+
}},
|
|
845
|
+
{"append", [](const func_args & args) -> value {
|
|
846
|
+
args.ensure_count(2);
|
|
847
|
+
if (!is_val<value_array>(args.get_pos(0))) {
|
|
848
|
+
throw raised_exception("append: first argument must be an array");
|
|
849
|
+
}
|
|
850
|
+
const value_array_t * arr = cast_val<value_array>(args.get_pos(0));
|
|
851
|
+
// need to use const_cast here to modify the array
|
|
852
|
+
value_array_t * arr_editable = const_cast<value_array_t *>(arr);
|
|
853
|
+
arr_editable->push_back(args.get_pos(1));
|
|
854
|
+
return args.get_pos(0);
|
|
855
|
+
}},
|
|
856
|
+
{"pop", [](const func_args & args) -> value {
|
|
857
|
+
args.ensure_count(1, 2);
|
|
858
|
+
args.ensure_vals<value_array, value_int>(true, false);
|
|
859
|
+
int64_t index = args.count() == 2 ? args.get_pos(1)->as_int() : -1;
|
|
860
|
+
const value_array_t * arr = cast_val<value_array>(args.get_pos(0));
|
|
861
|
+
// need to use const_cast here to modify the array
|
|
862
|
+
value_array_t * arr_editable = const_cast<value_array_t *>(arr);
|
|
863
|
+
return arr_editable->pop_at(index);
|
|
864
|
+
}},
|
|
865
|
+
{"sort", [](const func_args & args) -> value {
|
|
866
|
+
args.ensure_count(1, 4);
|
|
867
|
+
if (!is_val<value_array>(args.get_pos(0))) {
|
|
868
|
+
throw raised_exception("sort: first argument must be an array");
|
|
869
|
+
}
|
|
870
|
+
value val_reverse = args.get_kwarg_or_pos("reverse", 1);
|
|
871
|
+
value val_case = args.get_kwarg_or_pos("case_sensitive", 2);
|
|
872
|
+
value attribute = args.get_kwarg_or_pos("attribute", 3);
|
|
873
|
+
// FIXME: sorting is currently always case sensitive
|
|
874
|
+
//const bool case_sensitive = val_case->as_bool(); // undefined == false
|
|
875
|
+
const bool reverse = val_reverse->as_bool(); // undefined == false
|
|
876
|
+
const bool attr_is_int = is_val<value_int>(attribute);
|
|
877
|
+
const int64_t attr_int = attr_is_int ? attribute->as_int() : 0;
|
|
878
|
+
const std::string attr_name = attribute->is_undefined() ? "" : attribute->as_string().str();
|
|
879
|
+
std::vector<value> arr = cast_val<value_array>(args.get_pos(0))->as_array(); // copy
|
|
880
|
+
std::sort(arr.begin(), arr.end(),[&](const value & a, const value & b) {
|
|
881
|
+
value val_a = a;
|
|
882
|
+
value val_b = b;
|
|
883
|
+
if (!attribute->is_undefined()) {
|
|
884
|
+
if (attr_is_int && is_val<value_array>(a) && is_val<value_array>(b)) {
|
|
885
|
+
val_a = a->at(attr_int);
|
|
886
|
+
val_b = b->at(attr_int);
|
|
887
|
+
} else if (!attr_is_int && !attr_name.empty() && is_val<value_object>(a) && is_val<value_object>(b)) {
|
|
888
|
+
val_a = a->at(attr_name);
|
|
889
|
+
val_b = b->at(attr_name);
|
|
890
|
+
} else {
|
|
891
|
+
throw raised_exception("sort: unsupported object attribute comparison");
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
return value_compare(val_a, val_b, reverse ? value_compare_op::gt : value_compare_op::lt);
|
|
895
|
+
});
|
|
896
|
+
return mk_val<value_array>(arr);
|
|
897
|
+
}},
|
|
898
|
+
{"reverse", [](const func_args & args) -> value {
|
|
899
|
+
args.ensure_vals<value_array>();
|
|
900
|
+
std::vector<value> arr = cast_val<value_array>(args.get_pos(0))->as_array(); // copy
|
|
901
|
+
std::reverse(arr.begin(), arr.end());
|
|
902
|
+
return mk_val<value_array>(arr);
|
|
903
|
+
}},
|
|
904
|
+
{"unique", [](const func_args &) -> value {
|
|
905
|
+
throw not_implemented_exception("Array unique builtin not implemented");
|
|
906
|
+
}},
|
|
907
|
+
};
|
|
908
|
+
return builtins;
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
|
|
912
|
+
const func_builtins & value_object_t::get_builtins() const {
|
|
913
|
+
if (!has_builtins) {
|
|
914
|
+
static const func_builtins no_builtins = {};
|
|
915
|
+
return no_builtins;
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
static const func_builtins builtins = {
|
|
919
|
+
// {"default", default_value}, // cause issue with gpt-oss
|
|
920
|
+
{"get", [](const func_args & args) -> value {
|
|
921
|
+
args.ensure_count(2, 3);
|
|
922
|
+
if (!is_val<value_object>(args.get_pos(0))) {
|
|
923
|
+
throw raised_exception("get: first argument must be an object");
|
|
924
|
+
}
|
|
925
|
+
if (!is_val<value_string>(args.get_pos(1))) {
|
|
926
|
+
throw raised_exception("get: second argument must be a string (key)");
|
|
927
|
+
}
|
|
928
|
+
value default_val = mk_val<value_none>();
|
|
929
|
+
if (args.count() == 3) {
|
|
930
|
+
default_val = args.get_pos(2);
|
|
931
|
+
}
|
|
932
|
+
const value obj = args.get_pos(0);
|
|
933
|
+
std::string key = args.get_pos(1)->as_string().str();
|
|
934
|
+
return obj->at(key, default_val);
|
|
935
|
+
}},
|
|
936
|
+
{"keys", [](const func_args & args) -> value {
|
|
937
|
+
args.ensure_vals<value_object>();
|
|
938
|
+
const auto & obj = args.get_pos(0)->as_ordered_object();
|
|
939
|
+
auto result = mk_val<value_array>();
|
|
940
|
+
for (const auto & pair : obj) {
|
|
941
|
+
result->push_back(mk_val<value_string>(pair.first));
|
|
942
|
+
}
|
|
943
|
+
return result;
|
|
944
|
+
}},
|
|
945
|
+
{"values", [](const func_args & args) -> value {
|
|
946
|
+
args.ensure_vals<value_object>();
|
|
947
|
+
const auto & obj = args.get_pos(0)->as_ordered_object();
|
|
948
|
+
auto result = mk_val<value_array>();
|
|
949
|
+
for (const auto & pair : obj) {
|
|
950
|
+
result->push_back(pair.second);
|
|
951
|
+
}
|
|
952
|
+
return result;
|
|
953
|
+
}},
|
|
954
|
+
{"items", [](const func_args & args) -> value {
|
|
955
|
+
args.ensure_vals<value_object>();
|
|
956
|
+
const auto & obj = args.get_pos(0)->as_ordered_object();
|
|
957
|
+
auto result = mk_val<value_array>();
|
|
958
|
+
for (const auto & pair : obj) {
|
|
959
|
+
auto item = mk_val<value_array>();
|
|
960
|
+
item->push_back(mk_val<value_string>(pair.first));
|
|
961
|
+
item->push_back(pair.second);
|
|
962
|
+
result->push_back(std::move(item));
|
|
963
|
+
}
|
|
964
|
+
return result;
|
|
965
|
+
}},
|
|
966
|
+
{"tojson", tojson},
|
|
967
|
+
{"string", tojson},
|
|
968
|
+
{"length", [](const func_args & args) -> value {
|
|
969
|
+
args.ensure_vals<value_object>();
|
|
970
|
+
const auto & obj = args.get_pos(0)->as_ordered_object();
|
|
971
|
+
return mk_val<value_int>(static_cast<int64_t>(obj.size()));
|
|
972
|
+
}},
|
|
973
|
+
{"tojson", [](const func_args & args) -> value {
|
|
974
|
+
args.ensure_vals<value_object>();
|
|
975
|
+
// use global to_json
|
|
976
|
+
return global_builtins().at("tojson")(args);
|
|
977
|
+
}},
|
|
978
|
+
{"dictsort", [](const func_args & args) -> value {
|
|
979
|
+
value val_input = args.get_pos(0);
|
|
980
|
+
value val_case = args.get_kwarg_or_pos("case_sensitive", 1);
|
|
981
|
+
value val_by = args.get_kwarg_or_pos("by", 2);
|
|
982
|
+
value val_reverse = args.get_kwarg_or_pos("reverse", 3);
|
|
983
|
+
// FIXME: sorting is currently always case sensitive
|
|
984
|
+
//const bool case_sensitive = val_case->as_bool(); // undefined == false
|
|
985
|
+
const bool reverse = val_reverse->as_bool(); // undefined == false
|
|
986
|
+
const bool by_value = is_val<value_string>(val_by) && val_by->as_string().str() == "value" ? true : false;
|
|
987
|
+
auto result = mk_val<value_object>(val_input); // copy
|
|
988
|
+
std::sort(result->val_obj.ordered.begin(), result->val_obj.ordered.end(), [&](const auto & a, const auto & b) {
|
|
989
|
+
if (by_value) {
|
|
990
|
+
return value_compare(a.second, b.second, reverse ? value_compare_op::gt : value_compare_op::lt);
|
|
991
|
+
} else {
|
|
992
|
+
return reverse ? a.first > b.first : a.first < b.first;
|
|
993
|
+
}
|
|
994
|
+
});
|
|
995
|
+
return result;
|
|
996
|
+
}},
|
|
997
|
+
{"join", [](const func_args &) -> value {
|
|
998
|
+
throw not_implemented_exception("object join not implemented");
|
|
999
|
+
}},
|
|
1000
|
+
};
|
|
1001
|
+
return builtins;
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
const func_builtins & value_none_t::get_builtins() const {
|
|
1005
|
+
static const func_builtins builtins = {
|
|
1006
|
+
{"default", default_value},
|
|
1007
|
+
{"tojson", tojson},
|
|
1008
|
+
};
|
|
1009
|
+
return builtins;
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
|
|
1013
|
+
const func_builtins & value_undefined_t::get_builtins() const {
|
|
1014
|
+
static const func_builtins builtins = {
|
|
1015
|
+
{"default", default_value},
|
|
1016
|
+
{"tojson", [](const func_args & args) -> value {
|
|
1017
|
+
args.ensure_vals<value_undefined>();
|
|
1018
|
+
return mk_val<value_string>("null");
|
|
1019
|
+
}},
|
|
1020
|
+
};
|
|
1021
|
+
return builtins;
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
|
|
1025
|
+
//////////////////////////////////
|
|
1026
|
+
|
|
1027
|
+
|
|
1028
|
+
static value from_json(const nlohmann::ordered_json & j, bool mark_input) {
|
|
1029
|
+
if (j.is_null()) {
|
|
1030
|
+
return mk_val<value_none>();
|
|
1031
|
+
} else if (j.is_boolean()) {
|
|
1032
|
+
return mk_val<value_bool>(j.get<bool>());
|
|
1033
|
+
} else if (j.is_number_integer()) {
|
|
1034
|
+
return mk_val<value_int>(j.get<int64_t>());
|
|
1035
|
+
} else if (j.is_number_float()) {
|
|
1036
|
+
return mk_val<value_float>(j.get<double>());
|
|
1037
|
+
} else if (j.is_string()) {
|
|
1038
|
+
auto str = mk_val<value_string>(j.get<std::string>());
|
|
1039
|
+
if (mark_input) {
|
|
1040
|
+
str->mark_input();
|
|
1041
|
+
}
|
|
1042
|
+
return str;
|
|
1043
|
+
} else if (j.is_array()) {
|
|
1044
|
+
auto arr = mk_val<value_array>();
|
|
1045
|
+
for (const auto & item : j) {
|
|
1046
|
+
arr->push_back(from_json(item, mark_input));
|
|
1047
|
+
}
|
|
1048
|
+
return arr;
|
|
1049
|
+
} else if (j.is_object()) {
|
|
1050
|
+
auto obj = mk_val<value_object>();
|
|
1051
|
+
for (auto it = j.begin(); it != j.end(); ++it) {
|
|
1052
|
+
obj->insert(it.key(), from_json(it.value(), mark_input));
|
|
1053
|
+
}
|
|
1054
|
+
return obj;
|
|
1055
|
+
} else {
|
|
1056
|
+
throw std::runtime_error("Unsupported JSON value type");
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
// compare operator for value_t
|
|
1061
|
+
bool value_compare(const value & a, const value & b, value_compare_op op) {
|
|
1062
|
+
auto cmp = [&]() {
|
|
1063
|
+
// compare numeric types
|
|
1064
|
+
if ((is_val<value_int>(a) || is_val<value_float>(a)) &&
|
|
1065
|
+
(is_val<value_int>(b) || is_val<value_float>(b))){
|
|
1066
|
+
try {
|
|
1067
|
+
if (op == value_compare_op::eq) {
|
|
1068
|
+
return a->as_float() == b->as_float();
|
|
1069
|
+
} else if (op == value_compare_op::ge) {
|
|
1070
|
+
return a->as_float() >= b->as_float();
|
|
1071
|
+
} else if (op == value_compare_op::gt) {
|
|
1072
|
+
return a->as_float() > b->as_float();
|
|
1073
|
+
} else if (op == value_compare_op::lt) {
|
|
1074
|
+
return a->as_float() < b->as_float();
|
|
1075
|
+
} else if (op == value_compare_op::ne) {
|
|
1076
|
+
return a->as_float() != b->as_float();
|
|
1077
|
+
} else {
|
|
1078
|
+
throw std::runtime_error("Unsupported comparison operator for numeric types");
|
|
1079
|
+
}
|
|
1080
|
+
} catch (...) {}
|
|
1081
|
+
}
|
|
1082
|
+
// compare string and number
|
|
1083
|
+
// TODO: not sure if this is the right behavior
|
|
1084
|
+
if ((is_val<value_string>(b) && (is_val<value_int>(a) || is_val<value_float>(a))) ||
|
|
1085
|
+
(is_val<value_string>(a) && (is_val<value_int>(b) || is_val<value_float>(b))) ||
|
|
1086
|
+
(is_val<value_string>(a) && is_val<value_string>(b))) {
|
|
1087
|
+
try {
|
|
1088
|
+
if (op == value_compare_op::eq) {
|
|
1089
|
+
return a->as_string().str() == b->as_string().str();
|
|
1090
|
+
} else if (op == value_compare_op::ge) {
|
|
1091
|
+
return a->as_string().str() >= b->as_string().str();
|
|
1092
|
+
} else if (op == value_compare_op::gt) {
|
|
1093
|
+
return a->as_string().str() > b->as_string().str();
|
|
1094
|
+
} else if (op == value_compare_op::lt) {
|
|
1095
|
+
return a->as_string().str() < b->as_string().str();
|
|
1096
|
+
} else if (op == value_compare_op::ne) {
|
|
1097
|
+
return a->as_string().str() != b->as_string().str();
|
|
1098
|
+
} else {
|
|
1099
|
+
throw std::runtime_error("Unsupported comparison operator for string/number types");
|
|
1100
|
+
}
|
|
1101
|
+
} catch (...) {}
|
|
1102
|
+
}
|
|
1103
|
+
// compare boolean simple
|
|
1104
|
+
if (is_val<value_bool>(a) && is_val<value_bool>(b)) {
|
|
1105
|
+
if (op == value_compare_op::eq) {
|
|
1106
|
+
return a->as_bool() == b->as_bool();
|
|
1107
|
+
} else if (op == value_compare_op::ne) {
|
|
1108
|
+
return a->as_bool() != b->as_bool();
|
|
1109
|
+
} else {
|
|
1110
|
+
throw std::runtime_error("Unsupported comparison operator for bool type");
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
// compare by type
|
|
1114
|
+
if (a->type() != b->type()) {
|
|
1115
|
+
return false;
|
|
1116
|
+
}
|
|
1117
|
+
return false;
|
|
1118
|
+
};
|
|
1119
|
+
auto result = cmp();
|
|
1120
|
+
JJ_DEBUG("Comparing types: %s and %s result=%d", a->type().c_str(), b->type().c_str(), result);
|
|
1121
|
+
return result;
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
template<>
|
|
1125
|
+
void global_from_json(context & ctx, const nlohmann::ordered_json & json_obj, bool mark_input) {
|
|
1126
|
+
// printf("global_from_json: %s\n" , json_obj.dump(2).c_str());
|
|
1127
|
+
if (json_obj.is_null() || !json_obj.is_object()) {
|
|
1128
|
+
throw std::runtime_error("global_from_json: input JSON value must be an object");
|
|
1129
|
+
}
|
|
1130
|
+
for (auto it = json_obj.begin(); it != json_obj.end(); ++it) {
|
|
1131
|
+
JJ_DEBUG("global_from_json: setting key '%s'", it.key().c_str());
|
|
1132
|
+
ctx.set_val(it.key(), from_json(it.value(), mark_input));
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
static void value_to_json_internal(std::ostringstream & oss, const value & val, int curr_lvl, int indent, const std::string_view item_sep, const std::string_view key_sep) {
|
|
1137
|
+
auto indent_str = [indent, curr_lvl]() -> std::string {
|
|
1138
|
+
return (indent > 0) ? std::string(curr_lvl * indent, ' ') : "";
|
|
1139
|
+
};
|
|
1140
|
+
auto newline = [indent]() -> std::string {
|
|
1141
|
+
return (indent >= 0) ? "\n" : "";
|
|
1142
|
+
};
|
|
1143
|
+
|
|
1144
|
+
if (is_val<value_none>(val) || val->is_undefined()) {
|
|
1145
|
+
oss << "null";
|
|
1146
|
+
} else if (is_val<value_bool>(val)) {
|
|
1147
|
+
oss << (val->as_bool() ? "true" : "false");
|
|
1148
|
+
} else if (is_val<value_int>(val)) {
|
|
1149
|
+
oss << val->as_int();
|
|
1150
|
+
} else if (is_val<value_float>(val)) {
|
|
1151
|
+
oss << val->as_float();
|
|
1152
|
+
} else if (is_val<value_string>(val)) {
|
|
1153
|
+
oss << "\"";
|
|
1154
|
+
for (char c : val->as_string().str()) {
|
|
1155
|
+
switch (c) {
|
|
1156
|
+
case '"': oss << "\\\""; break;
|
|
1157
|
+
case '\\': oss << "\\\\"; break;
|
|
1158
|
+
case '\b': oss << "\\b"; break;
|
|
1159
|
+
case '\f': oss << "\\f"; break;
|
|
1160
|
+
case '\n': oss << "\\n"; break;
|
|
1161
|
+
case '\r': oss << "\\r"; break;
|
|
1162
|
+
case '\t': oss << "\\t"; break;
|
|
1163
|
+
default:
|
|
1164
|
+
if (static_cast<unsigned char>(c) < 0x20) {
|
|
1165
|
+
char buf[7];
|
|
1166
|
+
snprintf(buf, sizeof(buf), "\\u%04x", static_cast<unsigned char>(c));
|
|
1167
|
+
oss << buf;
|
|
1168
|
+
} else {
|
|
1169
|
+
oss << c;
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
oss << "\"";
|
|
1174
|
+
} else if (is_val<value_array>(val)) {
|
|
1175
|
+
const auto & arr = val->as_array();
|
|
1176
|
+
oss << "[";
|
|
1177
|
+
if (!arr.empty()) {
|
|
1178
|
+
oss << newline();
|
|
1179
|
+
for (size_t i = 0; i < arr.size(); ++i) {
|
|
1180
|
+
oss << indent_str() << (indent > 0 ? std::string(indent, ' ') : "");
|
|
1181
|
+
value_to_json_internal(oss, arr[i], curr_lvl + 1, indent, item_sep, key_sep);
|
|
1182
|
+
if (i < arr.size() - 1) {
|
|
1183
|
+
oss << item_sep;
|
|
1184
|
+
}
|
|
1185
|
+
oss << newline();
|
|
1186
|
+
}
|
|
1187
|
+
oss << indent_str();
|
|
1188
|
+
}
|
|
1189
|
+
oss << "]";
|
|
1190
|
+
} else if (is_val<value_object>(val)) {
|
|
1191
|
+
const auto & obj = val->as_ordered_object(); // IMPORTANT: need to keep exact order
|
|
1192
|
+
oss << "{";
|
|
1193
|
+
if (!obj.empty()) {
|
|
1194
|
+
oss << newline();
|
|
1195
|
+
size_t i = 0;
|
|
1196
|
+
for (const auto & pair : obj) {
|
|
1197
|
+
oss << indent_str() << (indent > 0 ? std::string(indent, ' ') : "");
|
|
1198
|
+
oss << "\"" << pair.first << "\"" << key_sep;
|
|
1199
|
+
value_to_json_internal(oss, pair.second, curr_lvl + 1, indent, item_sep, key_sep);
|
|
1200
|
+
if (i < obj.size() - 1) {
|
|
1201
|
+
oss << item_sep;
|
|
1202
|
+
}
|
|
1203
|
+
oss << newline();
|
|
1204
|
+
++i;
|
|
1205
|
+
}
|
|
1206
|
+
oss << indent_str();
|
|
1207
|
+
}
|
|
1208
|
+
oss << "}";
|
|
1209
|
+
} else {
|
|
1210
|
+
oss << "null";
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
std::string value_to_json(const value & val, int indent, const std::string_view item_sep, const std::string_view key_sep) {
|
|
1215
|
+
std::ostringstream oss;
|
|
1216
|
+
value_to_json_internal(oss, val, 0, indent, item_sep, key_sep);
|
|
1217
|
+
JJ_DEBUG("value_to_json: result=%s", oss.str().c_str());
|
|
1218
|
+
return oss.str();
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
} // namespace jinja
|