@@ -366,13 +366,11 @@ class Value : public std::enable_shared_from_this<Value> {
366
366
throw std::runtime_error (" contains can only be called on arrays and objects: " + dump ());
367
367
}
368
368
}
369
- void erase (size_t index) {
370
- if (array_) throw std::runtime_error (" Value is not an array: " + dump ());
369
+ Value pop (size_t index) {
370
+ if (!array_) throw std::runtime_error (" Value is not an array: " + dump ());
371
+ auto value = array_->at (index );
371
372
array_->erase (array_->begin () + index );
372
- }
373
- void erase (const std::string & key) {
374
- if (object_) throw std::runtime_error (" Value is not an object: " + dump ());
375
- object_->erase (key);
373
+ return value;
376
374
}
377
375
const Value& at (const Value & index) const {
378
376
return const_cast <Value*>(this )->at (index );
@@ -1353,6 +1351,15 @@ class MethodCallExpr : public Expression {
1353
1351
if (index < 0 || index > (int64_t ) obj.size ()) throw std::runtime_error (" Index out of range for insert method" );
1354
1352
obj.insert (index , vargs.args [1 ]);
1355
1353
return Value ();
1354
+ } else if (method->get_name () == " pop" ) {
1355
+ vargs.expectArgs (" pop method" , {0 , 1 }, {0 , 0 });
1356
+ if (vargs.args .empty ()) {
1357
+ return obj.pop (obj.size () - 1 );
1358
+ } else {
1359
+ auto index = vargs.args [0 ].get <int64_t >();
1360
+ if (index < 0 || index >= (int64_t ) obj.size ()) throw std::runtime_error (" Index out of range for pop method" );
1361
+ return obj.pop (index );
1362
+ }
1356
1363
}
1357
1364
} else if (obj.is_object ()) {
1358
1365
if (method->get_name () == " items" ) {
@@ -2539,7 +2546,7 @@ inline std::shared_ptr<Context> Context::builtins() {
2539
2546
}));
2540
2547
globals.set (" namespace" , Value::callable ([=](const std::shared_ptr<Context> &, ArgumentsValue & args) {
2541
2548
auto ns = Value::object ();
2542
- args.expectArgs (" namespace" , {0 , 0 }, {0 , ( std::numeric_limits<size_t >::max) ()});
2549
+ args.expectArgs (" namespace" , {0 , 0 }, {0 , std::numeric_limits<size_t >::max ()});
2543
2550
for (auto & [name, value] : args.kwargs ) {
2544
2551
ns.set (name, value);
2545
2552
}
@@ -2594,7 +2601,7 @@ inline std::shared_ptr<Context> Context::builtins() {
2594
2601
};
2595
2602
// https://jinja.palletsprojects.com/en/3.0.x/templates/#jinja-filters.reject
2596
2603
globals.set (" reject" , Value::callable ([=](const std::shared_ptr<Context> & context, ArgumentsValue & args) {
2597
- args.expectArgs (" reject" , {2 , ( std::numeric_limits<size_t >::max) ()}, {0 , 0 });
2604
+ args.expectArgs (" reject" , {2 , std::numeric_limits<size_t >::max ()}, {0 , 0 });
2598
2605
auto & items = args.args [0 ];
2599
2606
auto filter_fn = context->get (args.args [1 ]);
2600
2607
if (filter_fn.is_null ()) throw std::runtime_error (" Undefined filter: " + args.args [1 ].dump ());
@@ -2665,7 +2672,7 @@ inline std::shared_ptr<Context> Context::builtins() {
2665
2672
return out;
2666
2673
}));
2667
2674
globals.set (" selectattr" , Value::callable ([=](const std::shared_ptr<Context> & context, ArgumentsValue & args) {
2668
- args.expectArgs (" selectattr" , {2 , ( std::numeric_limits<size_t >::max) ()}, {0 , 0 });
2675
+ args.expectArgs (" selectattr" , {2 , std::numeric_limits<size_t >::max ()}, {0 , 0 });
2669
2676
auto & items = args.args [0 ];
2670
2677
if (items.is_null ())
2671
2678
return Value::array ();
0 commit comments