Skip to content

Commit 498a48a

Browse files
[script] Fix template preprocessing of component pairs and entity names with vars
1 parent f31e8a5 commit 498a48a

8 files changed

Lines changed: 131 additions & 8 deletions

File tree

distr/flecs.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69372,10 +69372,12 @@ int flecs_script_template_eval_prop(
6937269372
}
6937369373

6937469374
var->value.type = type;
69375-
var->value.ptr = flecs_stack_alloc(
69375+
var->value.ptr = flecs_stack_calloc(
6937669376
&v->r->stack, ti->size, ti->alignment);
6937769377
var->type_info = ti;
6937869378

69379+
flecs_type_info_ctor(var->value.ptr, 1, ti);
69380+
6937969381
if (flecs_script_eval_expr(v, &node->expr, &var->value)) {
6938069382
return -1;
6938169383
}
@@ -70332,6 +70334,13 @@ int flecs_script_check_entity(
7033270334
}
7033370335
}
7033470336

70337+
if (node->name_expr && !node->name_expr->type_info) {
70338+
ecs_entity_t type = ecs_id(ecs_string_t);
70339+
if (flecs_script_check_expr(v, &node->name_expr, &type)) {
70340+
return -1;
70341+
}
70342+
}
70343+
7033570344
ecs_script_entity_t *old_entity = v->entity;
7033670345
v->entity = node;
7033770346

@@ -70403,6 +70412,10 @@ int flecs_script_check_component(
7040370412
}
7040470413

7040570414
if (node->expr) {
70415+
if (!node->id.eval) {
70416+
return 0;
70417+
}
70418+
7040670419
const ecs_type_info_t *ti = ecs_get_type_info(v->world, node->id.eval);
7040770420
if (!ti) {
7040870421
return 0;
@@ -70922,7 +70935,8 @@ ecs_entity_t flecs_script_eval_name_expr(
7092270935
.lookup_ctx = v,
7092370936
.vars = v->vars,
7092470937
.type = ecs_id(ecs_string_t),
70925-
.runtime = v->r
70938+
.runtime = v->r,
70939+
.disable_dynamic_variable_binding = !v->dynamic_variable_binding
7092670940
};
7092770941

7092870942
ecs_expr_node_t *expr = *expr_ptr;

src/addons/script/template.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,12 @@ int flecs_script_template_eval_prop(
358358
}
359359

360360
var->value.type = type;
361-
var->value.ptr = flecs_stack_alloc(
361+
var->value.ptr = flecs_stack_calloc(
362362
&v->r->stack, ti->size, ti->alignment);
363363
var->type_info = ti;
364364

365+
flecs_type_info_ctor(var->value.ptr, 1, ti);
366+
365367
if (flecs_script_eval_expr(v, &node->expr, &var->value)) {
366368
return -1;
367369
}

src/addons/script/visit_check.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ int flecs_script_check_entity(
145145
}
146146
}
147147

148+
if (node->name_expr && !node->name_expr->type_info) {
149+
ecs_entity_t type = ecs_id(ecs_string_t);
150+
if (flecs_script_check_expr(v, &node->name_expr, &type)) {
151+
return -1;
152+
}
153+
}
154+
148155
ecs_script_entity_t *old_entity = v->entity;
149156
v->entity = node;
150157

@@ -216,6 +223,10 @@ int flecs_script_check_component(
216223
}
217224

218225
if (node->expr) {
226+
if (!node->id.eval) {
227+
return 0;
228+
}
229+
219230
const ecs_type_info_t *ti = ecs_get_type_info(v->world, node->id.eval);
220231
if (!ti) {
221232
return 0;

src/addons/script/visit_eval.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ ecs_entity_t flecs_script_eval_name_expr(
181181
.lookup_ctx = v,
182182
.vars = v->vars,
183183
.type = ecs_id(ecs_string_t),
184-
.runtime = v->r
184+
.runtime = v->r,
185+
.disable_dynamic_variable_binding = !v->dynamic_variable_binding
185186
};
186187

187188
ecs_expr_node_t *expr = *expr_ptr;

test/script/project.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,9 @@
649649
"template_w_tree_parent",
650650
"template_w_tree_parent_change_value",
651651
"template_w_nested_template_w_with",
652-
"template_w_nested_template_w_with_kind_value"
652+
"template_w_nested_template_w_with_kind_value",
653+
"pair_component_w_entity_prop_target",
654+
"child_name_from_string_prop"
653655
]
654656
}, {
655657
"id": "Error",
@@ -755,7 +757,8 @@
755757
"annotation_to_unresolved_identifier_managed_parse_twice_2",
756758
"annotation_to_tag",
757759
"parse_error_logged",
758-
"eval_error_logged"
760+
"eval_error_logged",
761+
"template_unresolved_var_in_entity_name"
759762
]
760763
}, {
761764
"id": "Expr",

test/script/src/Error.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,3 +2145,18 @@ void Error_eval_error_logged(void) {
21452145

21462146
ecs_fini(world);
21472147
}
2148+
2149+
void Error_template_unresolved_var_in_entity_name(void) {
2150+
ecs_world_t *world = ecs_init();
2151+
2152+
const char *expr =
2153+
HEAD "template Foo {"
2154+
LINE " \"child_$suffix\" {}"
2155+
LINE "}"
2156+
LINE "ent { Foo: {} }";
2157+
2158+
ecs_log_set_level(-4);
2159+
test_assert(ecs_script_run(world, NULL, expr, NULL) != 0);
2160+
2161+
ecs_fini(world);
2162+
}

test/script/src/Template.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3844,3 +3844,65 @@ void Template_template_w_nested_template_w_with_kind_value(void) {
38443844

38453845
ecs_fini(world);
38463846
}
3847+
3848+
void Template_pair_component_w_entity_prop_target(void) {
3849+
ecs_world_t *world = ecs_init();
3850+
3851+
ECS_COMPONENT(world, Position);
3852+
3853+
ecs_struct(world, {
3854+
.entity = ecs_id(Position),
3855+
.members = {
3856+
{"x", ecs_id(ecs_f32_t)},
3857+
{"y", ecs_id(ecs_f32_t)}
3858+
}
3859+
});
3860+
3861+
const char *expr =
3862+
LINE "template Foo {\n"
3863+
LINE " prop tgt = flecs.meta.entity: flecs\n"
3864+
LINE " (Position, $tgt): {5, 6}\n"
3865+
LINE "}\n"
3866+
LINE "ent { Foo: {flecs.core} }\n"
3867+
LINE "\n";
3868+
3869+
test_assert(ecs_script_run(world, NULL, expr, NULL) == 0);
3870+
3871+
ecs_entity_t foo = ecs_lookup(world, "Foo");
3872+
ecs_entity_t ent = ecs_lookup(world, "ent");
3873+
3874+
test_assert(foo != 0);
3875+
test_assert(ent != 0);
3876+
3877+
test_assert(ecs_has_id(world, ent, foo));
3878+
test_assert(ecs_has_pair(world, ent, ecs_id(Position), EcsFlecsCore));
3879+
3880+
const Position *p = ecs_get_pair(world, ent, Position, EcsFlecsCore);
3881+
test_assert(p != NULL);
3882+
test_int(p->x, 5);
3883+
test_int(p->y, 6);
3884+
3885+
ecs_fini(world);
3886+
}
3887+
3888+
void Template_child_name_from_string_prop(void) {
3889+
ecs_world_t *world = ecs_init();
3890+
3891+
const char *expr =
3892+
LINE "template Foo {"
3893+
LINE " prop suffix = flecs.meta.string: \"a\""
3894+
LINE " \"child_$suffix\" {}"
3895+
LINE "}"
3896+
LINE "ent { Foo: {suffix: \"hello\"} }"
3897+
LINE "";
3898+
3899+
test_assert(ecs_script_run(world, NULL, expr, NULL) == 0);
3900+
3901+
ecs_entity_t ent = ecs_lookup(world, "ent");
3902+
test_assert(ent != 0);
3903+
3904+
ecs_entity_t child = ecs_lookup(world, "ent.child_hello");
3905+
test_assert(child != 0);
3906+
3907+
ecs_fini(world);
3908+
}

test/script/src/main.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,8 @@ void Template_template_w_tree_parent(void);
642642
void Template_template_w_tree_parent_change_value(void);
643643
void Template_template_w_nested_template_w_with(void);
644644
void Template_template_w_nested_template_w_with_kind_value(void);
645+
void Template_pair_component_w_entity_prop_target(void);
646+
void Template_child_name_from_string_prop(void);
645647

646648
// Testsuite 'Error'
647649
void Error_multi_line_comment_after_newline_before_newline_scope_open(void);
@@ -746,6 +748,7 @@ void Error_annotation_to_unresolved_identifier_managed_parse_twice_2(void);
746748
void Error_annotation_to_tag(void);
747749
void Error_parse_error_logged(void);
748750
void Error_eval_error_logged(void);
751+
void Error_template_unresolved_var_in_entity_name(void);
749752

750753
// Testsuite 'Expr'
751754
void Expr_setup(void);
@@ -4017,6 +4020,14 @@ bake_test_case Template_testcases[] = {
40174020
{
40184021
"template_w_nested_template_w_with_kind_value",
40194022
Template_template_w_nested_template_w_with_kind_value
4023+
},
4024+
{
4025+
"pair_component_w_entity_prop_target",
4026+
Template_pair_component_w_entity_prop_target
4027+
},
4028+
{
4029+
"child_name_from_string_prop",
4030+
Template_child_name_from_string_prop
40204031
}
40214032
};
40224033

@@ -4428,6 +4439,10 @@ bake_test_case Error_testcases[] = {
44284439
{
44294440
"eval_error_logged",
44304441
Error_eval_error_logged
4442+
},
4443+
{
4444+
"template_unresolved_var_in_entity_name",
4445+
Error_template_unresolved_var_in_entity_name
44314446
}
44324447
};
44334448

@@ -7409,14 +7424,14 @@ static bake_test_suite suites[] = {
74097424
"Template",
74107425
NULL,
74117426
NULL,
7412-
83,
7427+
85,
74137428
Template_testcases
74147429
},
74157430
{
74167431
"Error",
74177432
NULL,
74187433
NULL,
7419-
102,
7434+
103,
74207435
Error_testcases
74217436
},
74227437
{

0 commit comments

Comments
 (0)