diff options
-rw-r--r-- | lib.c | 28 | ||||
-rw-r--r-- | tests/custom/02_runtime/07_raw_template_mode_switching | 45 | ||||
-rw-r--r-- | tests/custom/03_stdlib/29_require | 26 | ||||
-rw-r--r-- | tests/custom/03_stdlib/35_include | 32 |
4 files changed, 111 insertions, 20 deletions
@@ -1505,14 +1505,16 @@ uc_require_so(uc_vm_t *vm, const char *path, uc_value_t **res) } static bool -uc_require_ucode(uc_vm_t *vm, const char *path, uc_value_t *scope, uc_value_t **res) +uc_require_ucode(uc_vm_t *vm, const char *path, uc_value_t *scope, uc_value_t **res, bool raw_mode) { + uc_parse_config_t config = { 0 }; uc_exception_type_t extype; uc_program_t *program; uc_value_t *prev_scope; uc_value_t *closure; uc_source_t *source; struct stat st; + bool prev_mode; char *err; if (stat(path, &st)) @@ -1527,6 +1529,12 @@ uc_require_ucode(uc_vm_t *vm, const char *path, uc_value_t *scope, uc_value_t ** return true; } + if (!vm->config) + vm->config = &config; + + prev_mode = vm->config->raw_mode; + vm->config->raw_mode = raw_mode; + program = uc_compile(vm->config, source, &err); uc_source_put(source); @@ -1537,6 +1545,8 @@ uc_require_ucode(uc_vm_t *vm, const char *path, uc_value_t *scope, uc_value_t ** free(err); + vm->config->raw_mode = prev_mode; + return true; } @@ -1558,6 +1568,8 @@ uc_require_ucode(uc_vm_t *vm, const char *path, uc_value_t *scope, uc_value_t ** if (extype == EXCEPTION_NONE) *res = uc_vm_stack_pop(vm); + vm->config->raw_mode = prev_mode; + return true; } @@ -1604,7 +1616,7 @@ uc_require_path(uc_vm_t *vm, const char *path_template, const char *name, uc_val if (!strcmp(p + 1, ".so")) rv = uc_require_so(vm, buf->buf, res); else if (!strcmp(p + 1, ".uc")) - rv = uc_require_ucode(vm, buf->buf, NULL, res); + rv = uc_require_ucode(vm, buf->buf, NULL, res, true); if (rv) ucv_object_add(modtable, name, ucv_get(*res)); @@ -2124,7 +2136,7 @@ include_path(const char *curpath, const char *incpath) } static uc_value_t * -uc_include(uc_vm_t *vm, size_t nargs) +uc_include_common(uc_vm_t *vm, size_t nargs, bool raw_mode) { uc_value_t *path = uc_fn_arg(0); uc_value_t *scope = uc_fn_arg(1); @@ -2182,7 +2194,7 @@ uc_include(uc_vm_t *vm, size_t nargs) sc = ucv_get(uc_vm_scope_get(vm)); } - if (uc_require_ucode(vm, p, sc, &rv)) + if (uc_require_ucode(vm, p, sc, &rv, raw_mode)) ucv_put(rv); ucv_put(sc); @@ -2192,6 +2204,12 @@ uc_include(uc_vm_t *vm, size_t nargs) } static uc_value_t * +uc_include(uc_vm_t *vm, size_t nargs) +{ + return uc_include_common(vm, nargs, vm->config && vm->config->raw_mode); +} + +static uc_value_t * uc_render(uc_vm_t *vm, size_t nargs) { uc_string_t hdr = { .header = { .type = UC_STRING, .refcount = 1 } }; @@ -2213,7 +2231,7 @@ uc_render(uc_vm_t *vm, size_t nargs) vm->output = mem; /* execute include */ - (void) uc_include(vm, nargs); + (void) uc_include_common(vm, nargs, false); /* restore previous VM output */ vm->output = prev; diff --git a/tests/custom/02_runtime/07_raw_template_mode_switching b/tests/custom/02_runtime/07_raw_template_mode_switching new file mode 100644 index 0000000..5ae53e8 --- /dev/null +++ b/tests/custom/02_runtime/07_raw_template_mode_switching @@ -0,0 +1,45 @@ +Testing that require(), render() and include() properly toggle between +raw- and template parse mode. + + +1. Testing recursive invocation. + +-- Testcase -- +require("files.requiretest"); +print(render("files/render-test.uc")); +include("files/include-test.uc"); +-- End -- + +-- Args -- +-R +-- End -- + +-- File requiretest.uc -- +print("This is a raw mode file loaded by require()\n"); +print(render("require-render-test.uc")); +include("require-include-test.uc"); +-- End -- + +-- File require-include-test.uc -- +print("This is a raw mode file included by a required file\n"); +-- End -- + +-- File require-render-test.uc -- +This is a {{ "template mode" }} file rendered by a required file +-- End -- + +-- File render-test.uc -- +This is a {{ "template mode" }} file loaded by render() from a raw mode file +-- End -- + +-- File include-test.uc -- +print("This is a raw mode file loaded by include() from a raw mode file\n"); +-- End -- + +-- Expect stdout -- +This is a raw mode file loaded by require() +This is a template mode file rendered by a required file +This is a raw mode file included by a required file +This is a template mode file loaded by render() from a raw mode file +This is a raw mode file loaded by include() from a raw mode file +-- End -- diff --git a/tests/custom/03_stdlib/29_require b/tests/custom/03_stdlib/29_require index 681f3f7..4fb4216 100644 --- a/tests/custom/03_stdlib/29_require +++ b/tests/custom/03_stdlib/29_require @@ -42,15 +42,13 @@ Returns the value returned by the invoked module code (typically an object). -- End -- -- File require/test/module.uc -- -{% - print("This is require.test.module running!\n\n"); +print("This is require.test.module running!\n\n"); - return { - greeting: function(name) { - printf("Hello, %s!\n", name); - } - }; -%} +return { + greeting: function(name) { + printf("Hello, %s!\n", name); + } +}; -- End -- -- Expect stdout -- @@ -139,19 +137,17 @@ A compilation error in the module triggers an exception. -- End -- -- File require/test/broken.uc -- -{% - // Unclosed object to force syntax error - return { -%} +// Unclosed object to force syntax error +return { -- End -- -- Expect stderr -- Unable to compile module '.../require/test/broken.uc': Syntax error: Expecting label -In line 3, byte 11: +In line 2, byte 10: - ` return {` - Near here --^ + `return {` + ^-- Near here diff --git a/tests/custom/03_stdlib/35_include b/tests/custom/03_stdlib/35_include index 6d808f2..1d428f1 100644 --- a/tests/custom/03_stdlib/35_include +++ b/tests/custom/03_stdlib/35_include @@ -171,3 +171,35 @@ In line 12, byte 8: -- End -- + + +Ensure that included files inherit the parse mode of their calling file. + +-- Testcase -- +{% include("files/inctest.uc"); %} +-- End -- + +-- File inctest.uc -- +print("Test\n"); +-- End -- + +-- Expect stdout -- +print("Test\n"); +-- End -- + + +-- Testcase -- +include("files/inctest.uc"); +-- End -- + +-- Args -- +-R +-- End -- + +-- File inctest.uc -- +print("Test\n"); +-- End -- + +-- Expect stdout -- +Test +-- End -- |