runtime.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. #include "lexer.h"
  2. #include "runtime.h"
  3. #include "value.h"
  4. #include "utils.h"
  5. #include <string>
  6. #include <vector>
  7. #include <memory>
  8. #include <cmath>
  9. #define FILENAME "jinja-runtime"
  10. bool g_jinja_debug = false;
  11. namespace jinja {
  12. void enable_debug(bool enable) {
  13. g_jinja_debug = enable;
  14. }
  15. static value_string exec_statements(const statements & stmts, context & ctx) {
  16. auto result = mk_val<value_array>();
  17. for (const auto & stmt : stmts) {
  18. JJ_DEBUG("Executing statement of type %s", stmt->type().c_str());
  19. result->push_back(stmt->execute(ctx));
  20. }
  21. // convert to string parts
  22. value_string str = mk_val<value_string>();
  23. gather_string_parts_recursive(result, str);
  24. return str;
  25. }
  26. static std::string get_line_col(const std::string & source, size_t pos) {
  27. size_t line = 1;
  28. size_t col = 1;
  29. for (size_t i = 0; i < pos && i < source.size(); i++) {
  30. if (source[i] == '\n') {
  31. line++;
  32. col = 1;
  33. } else {
  34. col++;
  35. }
  36. }
  37. return "line " + std::to_string(line) + ", column " + std::to_string(col);
  38. }
  39. // execute with error handling
  40. value statement::execute(context & ctx) {
  41. try {
  42. return execute_impl(ctx);
  43. } catch (const continue_statement::signal & /* ex */) {
  44. throw;
  45. } catch (const break_statement::signal & /* ex */) {
  46. throw;
  47. } catch (const rethrown_exception & /* ex */) {
  48. throw;
  49. } catch (const not_implemented_exception & /* ex */) {
  50. throw;
  51. } catch (const std::exception & e) {
  52. const std::string & source = *ctx.src;
  53. if (source.empty()) {
  54. std::ostringstream oss;
  55. oss << "\nError executing " << type() << " at position " << pos << ": " << e.what();
  56. throw rethrown_exception(oss.str());
  57. } else {
  58. std::ostringstream oss;
  59. oss << "\n------------\n";
  60. oss << "While executing " << type() << " at " << get_line_col(source, pos) << " in source:\n";
  61. oss << peak_source(source, pos) << "\n";
  62. oss << "Error: " << e.what();
  63. // throw as another exception to avoid repeated formatting
  64. throw rethrown_exception(oss.str());
  65. }
  66. }
  67. }
  68. value identifier::execute_impl(context & ctx) {
  69. auto it = ctx.get_val(val);
  70. auto builtins = global_builtins();
  71. if (!it->is_undefined()) {
  72. if (ctx.is_get_stats) {
  73. it->stats.used = true;
  74. }
  75. JJ_DEBUG("Identifier '%s' found, type = %s", val.c_str(), it->type().c_str());
  76. return it;
  77. } else if (builtins.find(val) != builtins.end()) {
  78. JJ_DEBUG("Identifier '%s' found in builtins", val.c_str());
  79. return mk_val<value_func>(val, builtins.at(val));
  80. } else {
  81. JJ_DEBUG("Identifier '%s' not found, returning undefined", val.c_str());
  82. return mk_val<value_undefined>(val);
  83. }
  84. }
  85. value object_literal::execute_impl(context & ctx) {
  86. auto obj = mk_val<value_object>();
  87. for (const auto & pair : val) {
  88. value key_val = pair.first->execute(ctx);
  89. if (!is_val<value_string>(key_val) && !is_val<value_int>(key_val)) {
  90. throw std::runtime_error("Object literal: keys must be string or int values, got " + key_val->type());
  91. }
  92. std::string key = key_val->as_string().str();
  93. value val = pair.second->execute(ctx);
  94. JJ_DEBUG("Object literal: setting key '%s' with value type %s", key.c_str(), val->type().c_str());
  95. obj->insert(key, val);
  96. if (is_val<value_int>(key_val)) {
  97. obj->val_obj.is_key_numeric = true;
  98. } else if (obj->val_obj.is_key_numeric) {
  99. throw std::runtime_error("Object literal: cannot mix numeric and non-numeric keys");
  100. }
  101. }
  102. return obj;
  103. }
  104. value binary_expression::execute_impl(context & ctx) {
  105. value left_val = left->execute(ctx);
  106. // Logical operators
  107. if (op.value == "and") {
  108. return left_val->as_bool() ? right->execute(ctx) : std::move(left_val);
  109. } else if (op.value == "or") {
  110. return left_val->as_bool() ? std::move(left_val) : right->execute(ctx);
  111. }
  112. // Equality operators
  113. value right_val = right->execute(ctx);
  114. JJ_DEBUG("Executing binary expression %s '%s' %s", left_val->type().c_str(), op.value.c_str(), right_val->type().c_str());
  115. if (op.value == "==") {
  116. return mk_val<value_bool>(value_compare(left_val, right_val, value_compare_op::eq));
  117. } else if (op.value == "!=") {
  118. return mk_val<value_bool>(!value_compare(left_val, right_val, value_compare_op::eq));
  119. }
  120. auto workaround_concat_null_with_str = [&](value & res) -> bool {
  121. bool is_left_null = left_val->is_none() || left_val->is_undefined();
  122. bool is_right_null = right_val->is_none() || right_val->is_undefined();
  123. bool is_left_str = is_val<value_string>(left_val);
  124. bool is_right_str = is_val<value_string>(right_val);
  125. if ((is_left_null && is_right_str) || (is_right_null && is_left_str)) {
  126. JJ_DEBUG("%s", "Workaround: treating null/undefined as empty string for string concatenation");
  127. string left_str = is_left_null ? string() : left_val->as_string();
  128. string right_str = is_right_null ? string() : right_val->as_string();
  129. auto output = left_str.append(right_str);
  130. res = mk_val<value_string>(std::move(output));
  131. return true;
  132. }
  133. return false;
  134. };
  135. // Handle undefined and null values
  136. if (is_val<value_undefined>(left_val) || is_val<value_undefined>(right_val)) {
  137. if (is_val<value_undefined>(right_val) && (op.value == "in" || op.value == "not in")) {
  138. // Special case: `anything in undefined` is `false` and `anything not in undefined` is `true`
  139. return mk_val<value_bool>(op.value == "not in");
  140. }
  141. if (op.value == "+" || op.value == "~") {
  142. value res = mk_val<value_undefined>();
  143. if (workaround_concat_null_with_str(res)) {
  144. return res;
  145. }
  146. }
  147. throw std::runtime_error("Cannot perform operation " + op.value + " on undefined values");
  148. } else if (is_val<value_none>(left_val) || is_val<value_none>(right_val)) {
  149. if (op.value == "+" || op.value == "~") {
  150. value res = mk_val<value_undefined>();
  151. if (workaround_concat_null_with_str(res)) {
  152. return res;
  153. }
  154. }
  155. throw std::runtime_error("Cannot perform operation on null values");
  156. }
  157. // Float operations
  158. if ((is_val<value_int>(left_val) || is_val<value_float>(left_val)) &&
  159. (is_val<value_int>(right_val) || is_val<value_float>(right_val))) {
  160. double a = left_val->as_float();
  161. double b = right_val->as_float();
  162. if (op.value == "+" || op.value == "-" || op.value == "*") {
  163. double res = (op.value == "+") ? a + b : (op.value == "-") ? a - b : a * b;
  164. JJ_DEBUG("Arithmetic operation: %f %s %f = %f", a, op.value.c_str(), b, res);
  165. bool is_float = is_val<value_float>(left_val) || is_val<value_float>(right_val);
  166. if (is_float) {
  167. return mk_val<value_float>(res);
  168. } else {
  169. return mk_val<value_int>(static_cast<int64_t>(res));
  170. }
  171. } else if (op.value == "/") {
  172. JJ_DEBUG("Division operation: %f / %f", a, b);
  173. return mk_val<value_float>(a / b);
  174. } else if (op.value == "%") {
  175. double rem = std::fmod(a, b);
  176. JJ_DEBUG("Modulo operation: %f %% %f = %f", a, b, rem);
  177. bool is_float = is_val<value_float>(left_val) || is_val<value_float>(right_val);
  178. if (is_float) {
  179. return mk_val<value_float>(rem);
  180. } else {
  181. return mk_val<value_int>(static_cast<int64_t>(rem));
  182. }
  183. } else if (op.value == "<") {
  184. JJ_DEBUG("Comparison operation: %f < %f is %d", a, b, a < b);
  185. return mk_val<value_bool>(a < b);
  186. } else if (op.value == ">") {
  187. JJ_DEBUG("Comparison operation: %f > %f is %d", a, b, a > b);
  188. return mk_val<value_bool>(a > b);
  189. } else if (op.value == ">=") {
  190. JJ_DEBUG("Comparison operation: %f >= %f is %d", a, b, a >= b);
  191. return mk_val<value_bool>(a >= b);
  192. } else if (op.value == "<=") {
  193. JJ_DEBUG("Comparison operation: %f <= %f is %d", a, b, a <= b);
  194. return mk_val<value_bool>(a <= b);
  195. }
  196. }
  197. // Array operations
  198. if (is_val<value_array>(left_val) && is_val<value_array>(right_val)) {
  199. if (op.value == "+") {
  200. auto & left_arr = left_val->as_array();
  201. auto & right_arr = right_val->as_array();
  202. auto result = mk_val<value_array>();
  203. for (const auto & item : left_arr) {
  204. result->push_back(item);
  205. }
  206. for (const auto & item : right_arr) {
  207. result->push_back(item);
  208. }
  209. return result;
  210. }
  211. } else if (is_val<value_array>(right_val)) {
  212. auto & arr = right_val->as_array();
  213. bool member = false;
  214. for (const auto & item : arr) {
  215. if (value_compare(left_val, item, value_compare_op::eq)) {
  216. member = true;
  217. break;
  218. }
  219. }
  220. if (op.value == "in") {
  221. JJ_DEBUG("Checking membership: %s in Array is %d", left_val->type().c_str(), member);
  222. return mk_val<value_bool>(member);
  223. } else if (op.value == "not in") {
  224. JJ_DEBUG("Checking non-membership: %s not in Array is %d", left_val->type().c_str(), !member);
  225. return mk_val<value_bool>(!member);
  226. }
  227. }
  228. // String concatenation with ~ and +
  229. if ((is_val<value_string>(left_val) || is_val<value_string>(right_val)) &&
  230. (op.value == "~" || op.value == "+")) {
  231. JJ_DEBUG("String concatenation with %s operator", op.value.c_str());
  232. auto output = left_val->as_string().append(right_val->as_string());
  233. auto res = mk_val<value_string>();
  234. res->val_str = std::move(output);
  235. return res;
  236. }
  237. // String membership
  238. if (is_val<value_string>(left_val) && is_val<value_string>(right_val)) {
  239. auto left_str = left_val->as_string().str();
  240. auto right_str = right_val->as_string().str();
  241. if (op.value == "in") {
  242. return mk_val<value_bool>(right_str.find(left_str) != std::string::npos);
  243. } else if (op.value == "not in") {
  244. return mk_val<value_bool>(right_str.find(left_str) == std::string::npos);
  245. }
  246. }
  247. // String in object
  248. if (is_val<value_string>(left_val) && is_val<value_object>(right_val)) {
  249. auto key = left_val->as_string().str();
  250. auto & obj = right_val->as_object();
  251. bool has_key = obj.find(key) != obj.end();
  252. if (op.value == "in") {
  253. return mk_val<value_bool>(has_key);
  254. } else if (op.value == "not in") {
  255. return mk_val<value_bool>(!has_key);
  256. }
  257. }
  258. throw std::runtime_error("Unknown operator \"" + op.value + "\" between " + left_val->type() + " and " + right_val->type());
  259. }
  260. static value try_builtin_func(context & ctx, const std::string & name, value & input, bool undef_on_missing = false) {
  261. JJ_DEBUG("Trying built-in function '%s' for type %s", name.c_str(), input->type().c_str());
  262. if (ctx.is_get_stats) {
  263. input->stats.used = true;
  264. input->stats.ops.insert(name);
  265. }
  266. auto builtins = input->get_builtins();
  267. auto it = builtins.find(name);
  268. if (it != builtins.end()) {
  269. JJ_DEBUG("Binding built-in '%s'", name.c_str());
  270. return mk_val<value_func>(name, it->second, input);
  271. }
  272. if (undef_on_missing) {
  273. return mk_val<value_undefined>(name);
  274. }
  275. throw std::runtime_error("Unknown (built-in) filter '" + name + "' for type " + input->type());
  276. }
  277. value filter_expression::execute_impl(context & ctx) {
  278. value input = operand ? operand->execute(ctx) : val;
  279. JJ_DEBUG("Applying filter to %s", input->type().c_str());
  280. if (is_stmt<identifier>(filter)) {
  281. auto filter_id = cast_stmt<identifier>(filter)->val;
  282. if (filter_id == "trim") {
  283. filter_id = "strip"; // alias
  284. }
  285. JJ_DEBUG("Applying filter '%s' to %s", filter_id.c_str(), input->type().c_str());
  286. return try_builtin_func(ctx, filter_id, input)->invoke(func_args(ctx));
  287. } else if (is_stmt<call_expression>(filter)) {
  288. auto call = cast_stmt<call_expression>(filter);
  289. if (!is_stmt<identifier>(call->callee)) {
  290. throw std::runtime_error("Filter callee must be an identifier");
  291. }
  292. auto filter_id = cast_stmt<identifier>(call->callee)->val;
  293. if (filter_id == "trim") {
  294. filter_id = "strip"; // alias
  295. }
  296. JJ_DEBUG("Applying filter '%s' with arguments to %s", filter_id.c_str(), input->type().c_str());
  297. func_args args(ctx);
  298. for (const auto & arg_expr : call->args) {
  299. args.push_back(arg_expr->execute(ctx));
  300. }
  301. return try_builtin_func(ctx, filter_id, input)->invoke(args);
  302. } else {
  303. throw std::runtime_error("Invalid filter expression");
  304. }
  305. }
  306. value filter_statement::execute_impl(context & ctx) {
  307. // eval body as string, then apply filter
  308. auto body_val = exec_statements(body, ctx);
  309. value_string parts = mk_val<value_string>();
  310. gather_string_parts_recursive(body_val, parts);
  311. JJ_DEBUG("FilterStatement: applying filter to body string of length %zu", parts->val_str.length());
  312. filter_expression filter_expr(std::move(parts), std::move(filter));
  313. value out = filter_expr.execute(ctx);
  314. // this node can be reused later, make sure filter is preserved
  315. this->filter = std::move(filter_expr.filter);
  316. return out;
  317. }
  318. value test_expression::execute_impl(context & ctx) {
  319. // NOTE: "value is something" translates to function call "test_is_something(value)"
  320. const auto & builtins = global_builtins();
  321. std::string test_id;
  322. value input = operand->execute(ctx);
  323. func_args args(ctx);
  324. args.push_back(input);
  325. if (is_stmt<identifier>(test)) {
  326. test_id = cast_stmt<identifier>(test)->val;
  327. } else if (is_stmt<call_expression>(test)) {
  328. auto call = cast_stmt<call_expression>(test);
  329. if (!is_stmt<identifier>(call->callee)) {
  330. throw std::runtime_error("Test callee must be an identifier");
  331. }
  332. test_id = cast_stmt<identifier>(call->callee)->val;
  333. JJ_DEBUG("Applying test '%s' with arguments to %s", test_id.c_str(), input->type().c_str());
  334. for (const auto & arg_expr : call->args) {
  335. args.push_back(arg_expr->execute(ctx));
  336. }
  337. } else {
  338. throw std::runtime_error("Invalid test expression");
  339. }
  340. auto it = builtins.find("test_is_" + test_id);
  341. JJ_DEBUG("Test expression %s '%s' %s (using function 'test_is_%s')", operand->type().c_str(), test_id.c_str(), negate ? "(negate)" : "", test_id.c_str());
  342. if (it == builtins.end()) {
  343. throw std::runtime_error("Unknown test '" + test_id + "'");
  344. }
  345. auto res = it->second(args);
  346. if (negate) {
  347. return mk_val<value_bool>(!res->as_bool());
  348. } else {
  349. return res;
  350. }
  351. }
  352. value unary_expression::execute_impl(context & ctx) {
  353. value operand_val = argument->execute(ctx);
  354. JJ_DEBUG("Executing unary expression with operator '%s'", op.value.c_str());
  355. if (op.value == "not") {
  356. return mk_val<value_bool>(!operand_val->as_bool());
  357. } else if (op.value == "-") {
  358. if (is_val<value_int>(operand_val)) {
  359. return mk_val<value_int>(-operand_val->as_int());
  360. } else if (is_val<value_float>(operand_val)) {
  361. return mk_val<value_float>(-operand_val->as_float());
  362. } else {
  363. throw std::runtime_error("Unary - operator requires numeric operand");
  364. }
  365. }
  366. throw std::runtime_error("Unknown unary operator '" + op.value + "'");
  367. }
  368. value if_statement::execute_impl(context & ctx) {
  369. value test_val = test->execute(ctx);
  370. auto out = mk_val<value_array>();
  371. if (test_val->as_bool()) {
  372. for (auto & stmt : body) {
  373. JJ_DEBUG("IF --> Executing THEN body, current block: %s", stmt->type().c_str());
  374. out->push_back(stmt->execute(ctx));
  375. }
  376. } else {
  377. for (auto & stmt : alternate) {
  378. JJ_DEBUG("IF --> Executing ELSE body, current block: %s", stmt->type().c_str());
  379. out->push_back(stmt->execute(ctx));
  380. }
  381. }
  382. // convert to string parts
  383. value_string str = mk_val<value_string>();
  384. gather_string_parts_recursive(out, str);
  385. return str;
  386. }
  387. value for_statement::execute_impl(context & ctx) {
  388. context scope(ctx); // new scope for loop variables
  389. jinja::select_expression * select_expr = cast_stmt<select_expression>(iterable);
  390. statement_ptr test_expr_nullptr;
  391. statement_ptr & iter_expr = [&]() -> statement_ptr & {
  392. auto tmp = cast_stmt<select_expression>(iterable);
  393. return tmp ? tmp->lhs : iterable;
  394. }();
  395. statement_ptr & test_expr = [&]() -> statement_ptr & {
  396. auto tmp = cast_stmt<select_expression>(iterable);
  397. return tmp ? tmp->test : test_expr_nullptr;
  398. }();
  399. JJ_DEBUG("Executing for statement, iterable type: %s", iter_expr->type().c_str());
  400. value iterable_val = iter_expr->execute(scope);
  401. if (iterable_val->is_undefined()) {
  402. JJ_DEBUG("%s", "For loop iterable is undefined, skipping loop");
  403. iterable_val = mk_val<value_array>();
  404. }
  405. if (!is_val<value_array>(iterable_val) && !is_val<value_object>(iterable_val)) {
  406. throw std::runtime_error("Expected iterable or object type in for loop: got " + iterable_val->type());
  407. }
  408. std::vector<value> items;
  409. if (is_val<value_object>(iterable_val)) {
  410. JJ_DEBUG("%s", "For loop over object keys");
  411. auto & obj = iterable_val->as_object();
  412. for (auto & p : obj) {
  413. auto tuple = mk_val<value_array>();
  414. if (iterable_val->val_obj.is_key_numeric) {
  415. tuple->push_back(mk_val<value_int>(std::stoll(p.first)));
  416. } else {
  417. tuple->push_back(mk_val<value_string>(p.first));
  418. }
  419. tuple->push_back(p.second);
  420. items.push_back(tuple);
  421. }
  422. if (ctx.is_get_stats) {
  423. iterable_val->stats.used = true;
  424. iterable_val->stats.ops.insert("object_access");
  425. }
  426. } else {
  427. JJ_DEBUG("%s", "For loop over array items");
  428. auto & arr = iterable_val->as_array();
  429. for (const auto & item : arr) {
  430. items.push_back(item);
  431. }
  432. if (ctx.is_get_stats) {
  433. iterable_val->stats.used = true;
  434. iterable_val->stats.ops.insert("array_access");
  435. }
  436. }
  437. std::vector<std::function<void(context &)>> scope_update_fns;
  438. std::vector<value> filtered_items;
  439. for (size_t i = 0; i < items.size(); ++i) {
  440. context loop_scope(scope);
  441. value current = items[i];
  442. std::function<void(context&)> scope_update_fn = [](context &) { /* no-op */};
  443. if (is_stmt<identifier>(loopvar)) {
  444. auto id = cast_stmt<identifier>(loopvar)->val;
  445. if (is_val<value_object>(iterable_val)) {
  446. // case example: {% for key in dict %}
  447. current = items[i]->as_array()[0];
  448. scope_update_fn = [id, &items, i](context & ctx) {
  449. ctx.set_val(id, items[i]->as_array()[0]);
  450. };
  451. } else {
  452. // case example: {% for item in list %}
  453. scope_update_fn = [id, &items, i](context & ctx) {
  454. ctx.set_val(id, items[i]);
  455. };
  456. }
  457. } else if (is_stmt<tuple_literal>(loopvar)) {
  458. // case example: {% for key, value in dict %}
  459. auto tuple = cast_stmt<tuple_literal>(loopvar);
  460. if (!is_val<value_array>(current)) {
  461. throw std::runtime_error("Cannot unpack non-iterable type: " + current->type());
  462. }
  463. auto & c_arr = current->as_array();
  464. if (tuple->val.size() != c_arr.size()) {
  465. throw std::runtime_error(std::string("Too ") + (tuple->val.size() > c_arr.size() ? "few" : "many") + " items to unpack");
  466. }
  467. scope_update_fn = [tuple, &items, i](context & ctx) {
  468. auto & c_arr = items[i]->as_array();
  469. for (size_t j = 0; j < tuple->val.size(); ++j) {
  470. if (!is_stmt<identifier>(tuple->val[j])) {
  471. throw std::runtime_error("Cannot unpack non-identifier type: " + tuple->val[j]->type());
  472. }
  473. auto id = cast_stmt<identifier>(tuple->val[j])->val;
  474. ctx.set_val(id, c_arr[j]);
  475. }
  476. };
  477. } else {
  478. throw std::runtime_error("Invalid loop variable(s): " + loopvar->type());
  479. }
  480. if (select_expr && test_expr) {
  481. scope_update_fn(loop_scope);
  482. value test_val = test_expr->execute(loop_scope);
  483. if (!test_val->as_bool()) {
  484. continue;
  485. }
  486. }
  487. JJ_DEBUG("For loop: adding item type %s at index %zu", current->type().c_str(), i);
  488. filtered_items.push_back(current);
  489. scope_update_fns.push_back(scope_update_fn);
  490. }
  491. JJ_DEBUG("For loop: %zu items after filtering", filtered_items.size());
  492. auto result = mk_val<value_array>();
  493. bool noIteration = true;
  494. for (size_t i = 0; i < filtered_items.size(); i++) {
  495. JJ_DEBUG("For loop iteration %zu/%zu", i + 1, filtered_items.size());
  496. value_object loop_obj = mk_val<value_object>();
  497. loop_obj->insert("index", mk_val<value_int>(i + 1));
  498. loop_obj->insert("index0", mk_val<value_int>(i));
  499. loop_obj->insert("revindex", mk_val<value_int>(filtered_items.size() - i));
  500. loop_obj->insert("revindex0", mk_val<value_int>(filtered_items.size() - i - 1));
  501. loop_obj->insert("first", mk_val<value_bool>(i == 0));
  502. loop_obj->insert("last", mk_val<value_bool>(i == filtered_items.size() - 1));
  503. loop_obj->insert("length", mk_val<value_int>(filtered_items.size()));
  504. loop_obj->insert("previtem", i > 0 ? filtered_items[i - 1] : mk_val<value_undefined>("previtem"));
  505. loop_obj->insert("nextitem", i < filtered_items.size() - 1 ? filtered_items[i + 1] : mk_val<value_undefined>("nextitem"));
  506. scope.set_val("loop", loop_obj);
  507. scope_update_fns[i](scope);
  508. try {
  509. for (auto & stmt : body) {
  510. value val = stmt->execute(scope);
  511. result->push_back(val);
  512. }
  513. } catch (const continue_statement::signal &) {
  514. continue;
  515. } catch (const break_statement::signal &) {
  516. break;
  517. }
  518. noIteration = false;
  519. }
  520. JJ_DEBUG("For loop complete, total iterations: %zu", filtered_items.size());
  521. if (noIteration) {
  522. for (auto & stmt : default_block) {
  523. value val = stmt->execute(ctx);
  524. result->push_back(val);
  525. }
  526. }
  527. // convert to string parts
  528. value_string str = mk_val<value_string>();
  529. gather_string_parts_recursive(result, str);
  530. return str;
  531. }
  532. value set_statement::execute_impl(context & ctx) {
  533. auto rhs = val ? val->execute(ctx) : exec_statements(body, ctx);
  534. if (is_stmt<identifier>(assignee)) {
  535. auto var_name = cast_stmt<identifier>(assignee)->val;
  536. JJ_DEBUG("Setting global variable '%s' with value type %s", var_name.c_str(), rhs->type().c_str());
  537. ctx.set_val(var_name, rhs);
  538. } else if (is_stmt<tuple_literal>(assignee)) {
  539. auto tuple = cast_stmt<tuple_literal>(assignee);
  540. if (!is_val<value_array>(rhs)) {
  541. throw std::runtime_error("Cannot unpack non-iterable type in set: " + rhs->type());
  542. }
  543. auto & arr = rhs->as_array();
  544. if (arr.size() != tuple->val.size()) {
  545. throw std::runtime_error(std::string("Too ") + (tuple->val.size() > arr.size() ? "few" : "many") + " items to unpack in set");
  546. }
  547. for (size_t i = 0; i < tuple->val.size(); ++i) {
  548. auto & elem = tuple->val[i];
  549. if (!is_stmt<identifier>(elem)) {
  550. throw std::runtime_error("Cannot unpack to non-identifier in set: " + elem->type());
  551. }
  552. auto var_name = cast_stmt<identifier>(elem)->val;
  553. ctx.set_val(var_name, arr[i]);
  554. }
  555. } else if (is_stmt<member_expression>(assignee)) {
  556. auto member = cast_stmt<member_expression>(assignee);
  557. if (member->computed) {
  558. throw std::runtime_error("Cannot assign to computed member");
  559. }
  560. if (!is_stmt<identifier>(member->property)) {
  561. throw std::runtime_error("Cannot assign to member with non-identifier property");
  562. }
  563. auto prop_name = cast_stmt<identifier>(member->property)->val;
  564. value object = member->object->execute(ctx);
  565. if (!is_val<value_object>(object)) {
  566. throw std::runtime_error("Cannot assign to member of non-object");
  567. }
  568. auto obj_ptr = cast_val<value_object>(object);
  569. JJ_DEBUG("Setting object property '%s' with value type %s", prop_name.c_str(), rhs->type().c_str());
  570. obj_ptr->insert(prop_name, rhs);
  571. } else {
  572. throw std::runtime_error("Invalid LHS inside assignment expression: " + assignee->type());
  573. }
  574. return mk_val<value_undefined>();
  575. }
  576. value macro_statement::execute_impl(context & ctx) {
  577. if (!is_stmt<identifier>(this->name)) {
  578. throw std::runtime_error("Macro name must be an identifier");
  579. }
  580. std::string name = cast_stmt<identifier>(this->name)->val;
  581. const func_handler func = [this, name, &ctx](const func_args & args) -> value {
  582. size_t expected_count = this->args.size();
  583. size_t input_count = args.count();
  584. JJ_DEBUG("Invoking macro '%s' with %zu input arguments (expected %zu)", name.c_str(), input_count, expected_count);
  585. context macro_ctx(ctx); // new scope for macro execution
  586. // bind parameters
  587. for (size_t i = 0; i < expected_count; ++i) {
  588. if (i < input_count) {
  589. if (is_stmt<identifier>(this->args[i])) {
  590. // normal parameter
  591. std::string param_name = cast_stmt<identifier>(this->args[i])->val;
  592. JJ_DEBUG(" Binding parameter '%s' to argument of type %s", param_name.c_str(), args.get_pos(i)->type().c_str());
  593. macro_ctx.set_val(param_name, args.get_pos(i));
  594. } else if (is_stmt<keyword_argument_expression>(this->args[i])) {
  595. // default argument used as normal parameter
  596. auto kwarg = cast_stmt<keyword_argument_expression>(this->args[i]);
  597. if (!is_stmt<identifier>(kwarg->key)) {
  598. throw std::runtime_error("Keyword argument key must be an identifier in macro '" + name + "'");
  599. }
  600. std::string param_name = cast_stmt<identifier>(kwarg->key)->val;
  601. JJ_DEBUG(" Binding parameter '%s' to argument of type %s", param_name.c_str(), args.get_pos(i)->type().c_str());
  602. macro_ctx.set_val(param_name, args.get_pos(i));
  603. } else {
  604. throw std::runtime_error("Invalid parameter type in macro '" + name + "'");
  605. }
  606. } else {
  607. auto & default_arg = this->args[i];
  608. if (is_stmt<keyword_argument_expression>(default_arg)) {
  609. auto kwarg = cast_stmt<keyword_argument_expression>(default_arg);
  610. if (!is_stmt<identifier>(kwarg->key)) {
  611. throw std::runtime_error("Keyword argument key must be an identifier in macro '" + name + "'");
  612. }
  613. std::string param_name = cast_stmt<identifier>(kwarg->key)->val;
  614. JJ_DEBUG(" Binding parameter '%s' to default argument of type %s", param_name.c_str(), kwarg->val->type().c_str());
  615. macro_ctx.set_val(param_name, kwarg->val->execute(ctx));
  616. } else {
  617. throw std::runtime_error("Not enough arguments provided to macro '" + name + "'");
  618. }
  619. //std::string param_name = cast_stmt<identifier>(default_args[i])->val;
  620. //JJ_DEBUG(" Binding parameter '%s' to default", param_name.c_str());
  621. //macro_ctx.var[param_name] = default_args[i]->execute(ctx);
  622. }
  623. }
  624. // execute macro body
  625. JJ_DEBUG("Executing macro '%s' body with %zu statements", name.c_str(), this->body.size());
  626. auto res = exec_statements(this->body, macro_ctx);
  627. JJ_DEBUG("Macro '%s' execution complete, result: %s", name.c_str(), res->val_str.str().c_str());
  628. return res;
  629. };
  630. JJ_DEBUG("Defining macro '%s' with %zu parameters", name.c_str(), args.size());
  631. ctx.set_val(name, mk_val<value_func>(name, func));
  632. return mk_val<value_undefined>();
  633. }
  634. value member_expression::execute_impl(context & ctx) {
  635. value object = this->object->execute(ctx);
  636. value property;
  637. if (this->computed) {
  638. JJ_DEBUG("Member expression, computing property type %s", this->property->type().c_str());
  639. int64_t arr_size = 0;
  640. if (is_val<value_array>(object)) {
  641. arr_size = object->as_array().size();
  642. }
  643. if (is_stmt<slice_expression>(this->property)) {
  644. auto s = cast_stmt<slice_expression>(this->property);
  645. value start_val = s->start_expr ? s->start_expr->execute(ctx) : mk_val<value_int>(0);
  646. value stop_val = s->stop_expr ? s->stop_expr->execute(ctx) : mk_val<value_int>(arr_size);
  647. value step_val = s->step_expr ? s->step_expr->execute(ctx) : mk_val<value_int>(1);
  648. // translate to function call: obj.slice(start, stop, step)
  649. JJ_DEBUG("Member expression is a slice: start %s, stop %s, step %s",
  650. start_val->as_repr().c_str(),
  651. stop_val->as_repr().c_str(),
  652. step_val->as_repr().c_str());
  653. auto slice_func = try_builtin_func(ctx, "slice", object);
  654. func_args args(ctx);
  655. args.push_back(start_val);
  656. args.push_back(stop_val);
  657. args.push_back(step_val);
  658. return slice_func->invoke(args);
  659. } else {
  660. property = this->property->execute(ctx);
  661. }
  662. } else {
  663. if (!is_stmt<identifier>(this->property)) {
  664. throw std::runtime_error("Non-computed member property must be an identifier");
  665. }
  666. property = mk_val<value_string>(cast_stmt<identifier>(this->property)->val);
  667. }
  668. JJ_DEBUG("Member expression on object type %s, property type %s", object->type().c_str(), property->type().c_str());
  669. value val = mk_val<value_undefined>("object_property");
  670. if (is_val<value_undefined>(object)) {
  671. JJ_DEBUG("%s", "Accessing property on undefined object, returning undefined");
  672. return val;
  673. } else if (is_val<value_object>(object)) {
  674. if (!is_val<value_string>(property)) {
  675. throw std::runtime_error("Cannot access object with non-string: got " + property->type());
  676. }
  677. auto key = property->as_string().str();
  678. auto & obj = object->as_object();
  679. auto it = obj.find(key);
  680. if (it != obj.end()) {
  681. val = it->second;
  682. } else {
  683. val = try_builtin_func(ctx, key, object, true);
  684. }
  685. JJ_DEBUG("Accessed property '%s' value, got type: %s", key.c_str(), val->type().c_str());
  686. } else if (is_val<value_array>(object) || is_val<value_string>(object)) {
  687. if (is_val<value_int>(property)) {
  688. int64_t index = property->as_int();
  689. JJ_DEBUG("Accessing %s index %d", object->type().c_str(), (int)index);
  690. if (is_val<value_array>(object)) {
  691. auto & arr = object->as_array();
  692. if (index < 0) {
  693. index += static_cast<int64_t>(arr.size());
  694. }
  695. if (index >= 0 && index < static_cast<int64_t>(arr.size())) {
  696. val = arr[index];
  697. }
  698. } else { // value_string
  699. auto str = object->as_string().str();
  700. if (index >= 0 && index < static_cast<int64_t>(str.size())) {
  701. val = mk_val<value_string>(std::string(1, str[index]));
  702. }
  703. }
  704. } else if (is_val<value_string>(property)) {
  705. auto key = property->as_string().str();
  706. JJ_DEBUG("Accessing %s built-in '%s'", is_val<value_array>(object) ? "array" : "string", key.c_str());
  707. val = try_builtin_func(ctx, key, object);
  708. } else {
  709. throw std::runtime_error("Cannot access property with non-string/non-number: got " + property->type());
  710. }
  711. } else {
  712. if (!is_val<value_string>(property)) {
  713. throw std::runtime_error("Cannot access property with non-string: got " + property->type());
  714. }
  715. auto key = property->as_string().str();
  716. val = try_builtin_func(ctx, key, object);
  717. }
  718. if (ctx.is_get_stats && val && object && property) {
  719. val->stats.used = true;
  720. object->stats.used = true;
  721. if (is_val<value_int>(property)) {
  722. object->stats.ops.insert("array_access");
  723. } else if (is_val<value_string>(property)) {
  724. object->stats.ops.insert("object_access");
  725. }
  726. }
  727. return val;
  728. }
  729. value call_expression::execute_impl(context & ctx) {
  730. // gather arguments
  731. func_args args(ctx);
  732. for (auto & arg_stmt : this->args) {
  733. auto arg_val = arg_stmt->execute(ctx);
  734. JJ_DEBUG(" Argument type: %s", arg_val->type().c_str());
  735. args.push_back(std::move(arg_val));
  736. }
  737. // execute callee
  738. value callee_val = callee->execute(ctx);
  739. if (!is_val<value_func>(callee_val)) {
  740. throw std::runtime_error("Callee is not a function: got " + callee_val->type());
  741. }
  742. auto * callee_func = cast_val<value_func>(callee_val);
  743. JJ_DEBUG("Calling function '%s' with %zu arguments", callee_func->name.c_str(), args.count());
  744. return callee_func->invoke(args);
  745. }
  746. value keyword_argument_expression::execute_impl(context & ctx) {
  747. if (!is_stmt<identifier>(key)) {
  748. throw std::runtime_error("Keyword argument key must be identifiers");
  749. }
  750. std::string k = cast_stmt<identifier>(key)->val;
  751. JJ_DEBUG("Keyword argument expression key: %s, value: %s", k.c_str(), val->type().c_str());
  752. value v = val->execute(ctx);
  753. JJ_DEBUG("Keyword argument value executed, type: %s", v->type().c_str());
  754. return mk_val<value_kwarg>(k, v);
  755. }
  756. } // namespace jinja