diff --git a/distr/flecs.c b/distr/flecs.c index 5ea554ff31..e6b7977ff1 100644 --- a/distr/flecs.c +++ b/distr/flecs.c @@ -57848,11 +57848,13 @@ int flecs_script_template_preprocess( ecs_script_eval_visitor_t *v, ecs_script_template_t *template) { + ecs_visit_action_t prev_visit = v->base.visit; v->template = template; v->base.visit = (ecs_visit_action_t)flecs_script_template_eval; v->vars = flecs_script_vars_push(v->vars, &v->stack, v->allocator); int result = ecs_script_visit_scope(v, template->node->scope); v->vars = ecs_script_vars_pop(v->vars); + v->base.visit = prev_visit; v->template = NULL; return result; } diff --git a/src/addons/script/template.c b/src/addons/script/template.c index 8a71aa2be6..db0f8903d5 100644 --- a/src/addons/script/template.c +++ b/src/addons/script/template.c @@ -270,11 +270,13 @@ int flecs_script_template_preprocess( ecs_script_eval_visitor_t *v, ecs_script_template_t *template) { + ecs_visit_action_t prev_visit = v->base.visit; v->template = template; v->base.visit = (ecs_visit_action_t)flecs_script_template_eval; v->vars = flecs_script_vars_push(v->vars, &v->stack, v->allocator); int result = ecs_script_visit_scope(v, template->node->scope); v->vars = ecs_script_vars_pop(v->vars); + v->base.visit = prev_visit; v->template = NULL; return result; } diff --git a/test/script/project.json b/test/script/project.json index 3cb84d9bf1..16740dd034 100644 --- a/test/script/project.json +++ b/test/script/project.json @@ -301,7 +301,9 @@ "anonymous_template_instance", "anonymous_template_instance_no_scope", "anonymous_template_instance_w_prop", - "anonymous_template_instance_w_prop_no_scope" + "anonymous_template_instance_w_prop_no_scope", + "with_after_template", + "with_in_scope_after_template" ] }, { "id": "Error", diff --git a/test/script/src/Template.c b/test/script/src/Template.c index 3a302063ec..e44ae3dba3 100644 --- a/test/script/src/Template.c +++ b/test/script/src/Template.c @@ -1879,3 +1879,59 @@ void Template_anonymous_template_instance_w_prop_no_scope(void) { ecs_fini(world); } + +void Template_with_after_template(void) { + ecs_world_t *world = ecs_init(); + + ECS_TAG(world, Bar); + + const char *expr = + HEAD "template Foo {}" + LINE "with Bar {" + LINE " withBar {}" + LINE "}" + LINE "withoutBar {}"; + + test_assert(ecs_script_run(world, NULL, expr) == 0); + + ecs_entity_t withBar = ecs_lookup(world, "withBar"); + test_assert(withBar != 0); + ecs_entity_t withoutBar = ecs_lookup(world, "withoutBar"); + test_assert(withoutBar != 0); + + test_assert(ecs_has(world, withBar, Bar)); + test_assert(!ecs_has(world, withoutBar, Bar)); + + ecs_fini(world); +} + +void Template_with_in_scope_after_template(void) { + ecs_world_t *world = ecs_init(); + + ECS_TAG(world, Bar); + + const char *expr = + HEAD "template Foo {}" + LINE "parent {" + LINE " with Bar {" + LINE " withBar {}" + LINE " }" + LINE " withoutBar {}" + LINE "}" + LINE "defWithoutBar {}"; + + test_assert(ecs_script_run(world, NULL, expr) == 0); + + ecs_entity_t withBar = ecs_lookup(world, "parent.withBar"); + test_assert(withBar != 0); + ecs_entity_t withoutBar = ecs_lookup(world, "parent.withoutBar"); + test_assert(withoutBar != 0); + ecs_entity_t defWithoutBar = ecs_lookup(world, "defWithoutBar"); + test_assert(defWithoutBar != 0); + + test_assert(ecs_has(world, withBar, Bar)); + test_assert(!ecs_has(world, withoutBar, Bar)); + test_assert(!ecs_has(world, defWithoutBar, Bar)); + + ecs_fini(world); +} diff --git a/test/script/src/main.c b/test/script/src/main.c index 19414f071d..be8af1a9ce 100644 --- a/test/script/src/main.c +++ b/test/script/src/main.c @@ -296,6 +296,8 @@ void Template_anonymous_template_instance(void); void Template_anonymous_template_instance_no_scope(void); void Template_anonymous_template_instance_w_prop(void); void Template_anonymous_template_instance_w_prop_no_scope(void); +void Template_with_after_template(void); +void Template_with_in_scope_after_template(void); // Testsuite 'Error' void Error_multi_line_comment_after_newline_before_newline_scope_open(void); @@ -1782,6 +1784,14 @@ bake_test_case Template_testcases[] = { { "anonymous_template_instance_w_prop_no_scope", Template_anonymous_template_instance_w_prop_no_scope + }, + { + "with_after_template", + Template_with_after_template + }, + { + "with_in_scope_after_template", + Template_with_in_scope_after_template } }; @@ -3141,7 +3151,7 @@ static bake_test_suite suites[] = { "Template", NULL, NULL, - 34, + 36, Template_testcases }, {