Skip to content

Commit

Permalink
#1328 Fix issue where script visitor function wasn't restored after t…
Browse files Browse the repository at this point in the history
…emplate
  • Loading branch information
SanderMertens committed Sep 2, 2024
1 parent fc4e296 commit b9eb30c
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
2 changes: 2 additions & 0 deletions distr/flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions src/addons/script/template.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion test/script/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
56 changes: 56 additions & 0 deletions test/script/src/Template.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
12 changes: 11 additions & 1 deletion test/script/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
}
};

Expand Down Expand Up @@ -3141,7 +3151,7 @@ static bake_test_suite suites[] = {
"Template",
NULL,
NULL,
34,
36,
Template_testcases
},
{
Expand Down

0 comments on commit b9eb30c

Please sign in to comment.