execute method

  1. @override
JinjaValue execute(
  1. Context ctx
)
override

Executes the statement within the given ctx and returns the result as a JinjaValue.

Implementation

@override
JinjaValue execute(Context ctx) {
  final func = callee.execute(ctx);
  if (func is! JinjaFunction) {
    throw Exception('Call to non-function: ${func.typeName}');
  }

  // Evaluate args
  List<JinjaValue> positionals = [];
  Map<String, JinjaValue> kwargs = {};

  for (final arg in args) {
    if (arg is KeywordArgumentExpression) {
      final key = arg.key;
      if (key is! Identifier) {
        throw Exception('Keyword arg key must be identifier');
      }
      kwargs[key.name] = arg.val.execute(ctx);
    } else if (arg is Expression) {
      positionals.add(arg.execute(ctx));
    }
  }

  return deriveRendered(called(func, func.handler(positionals, kwargs)), [
    ...positionals,
    ...kwargs.values,
  ]);
}