From 344fa9e69da43ecdc4d8f7768d85d42639352405 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Mon, 29 Aug 2022 15:02:31 +0200 Subject: lib: extend render() to support function values Extend the `render()` function to accept a function value as first argument, which allows running arbitrary ucode functions and capturing their output. This is especially useful in conjunction with `loadfile()` or `loadstring()` to dynamically compile templates and rendering their output into a string. Signed-off-by: Jo-Philipp Wich --- lib.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'lib.c') diff --git a/lib.c b/lib.c index d5e02a5..5d76cc8 100644 --- a/lib.c +++ b/lib.c @@ -2484,8 +2484,13 @@ uc_render(uc_vm_t *vm, size_t nargs) prev = vm->output; vm->output = mem; + /* execute function */ + if (ucv_is_callable(uc_fn_arg(0))) + (void) uc_vm_call(vm, false, nargs - 1); + /* execute include */ - (void) uc_include_common(vm, nargs, false); + else + (void) uc_include_common(vm, nargs, false); /* restore previous VM output */ vm->output = prev; -- cgit v1.2.3