Skip to content

Commit

Permalink
Fix issue with pair components in script templates
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Jan 4, 2025
1 parent 3a4c120 commit d2ef197
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 4 deletions.
3 changes: 2 additions & 1 deletion distr/flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -61091,7 +61091,7 @@ int flecs_script_check_component(
}
}

ecs_entity_t expr_type = node->id.eval;
ecs_entity_t expr_type = ti->component;
if (flecs_script_check_expr(v, &node->expr, &expr_type)) {
return -1;
}
Expand Down Expand Up @@ -80488,6 +80488,7 @@ int flecs_expr_initializer_visit_type(
goto error;
}


ecs_expr_initializer_element_t *elems = ecs_vec_first(&node->elements);
int32_t i, count = ecs_vec_count(&node->elements);
for (i = 0; i < count; i ++) {
Expand Down
1 change: 1 addition & 0 deletions src/addons/script/expr/visit_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,7 @@ int flecs_expr_initializer_visit_type(
goto error;
}


ecs_expr_initializer_element_t *elems = ecs_vec_first(&node->elements);
int32_t i, count = ecs_vec_count(&node->elements);
for (i = 0; i < count; i ++) {
Expand Down
2 changes: 1 addition & 1 deletion src/addons/script/visit_check.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ int flecs_script_check_component(
}
}

ecs_entity_t expr_type = node->id.eval;
ecs_entity_t expr_type = ti->component;
if (flecs_script_check_expr(v, &node->expr, &expr_type)) {
return -1;
}
Expand Down
4 changes: 3 additions & 1 deletion test/script/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,9 @@
"const_from_prop",
"redefine_nested_template_w_prefab",
"redefine_nested_template_w_prefab_2",
"redefine_nested_template_w_prefab_3"
"redefine_nested_template_w_prefab_3",
"template_w_script_component",
"template_w_script_pair_component"
]
}, {
"id": "Error",
Expand Down
72 changes: 72 additions & 0 deletions test/script/src/Template.c
Original file line number Diff line number Diff line change
Expand Up @@ -3418,3 +3418,75 @@ void Template_redefine_nested_template_w_prefab_3(void) {

ecs_fini(world);
}

void Template_template_w_script_component(void) {
ecs_world_t *world = ecs_init();

const char *expr =
HEAD "struct Position {"
LINE " x = f32"
LINE " y = f32"
LINE "}"
LINE "template Foo {"
LINE " prop x = f32: 0"
LINE " prop y = f32: 0"
LINE " Position: {x, y}"
LINE "}"
LINE "Foo e(10, 20)";

test_assert(ecs_script_run(world, NULL, expr) == 0);

ECS_COMPONENT(world, Position);

ecs_entity_t foo = ecs_lookup(world, "Foo");
test_assert(foo != 0);

ecs_entity_t e = ecs_lookup(world, "e");
test_assert(e != 0);
test_assert(ecs_has_id(world, e, foo));
test_assert(ecs_has(world, e, Position));

const Position *p = ecs_get(world, e, Position);
test_assert(p != NULL);
test_int(p->x, 10);
test_int(p->y, 20);

ecs_fini(world);
}

void Template_template_w_script_pair_component(void) {
ecs_world_t *world = ecs_init();

const char *expr =
HEAD "struct Position {"
LINE " x = f32"
LINE " y = f32"
LINE "}"
LINE "Tgt {}"
LINE "template Foo {"
LINE " prop x = f32: 0"
LINE " prop y = f32: 0"
LINE " (Position, Tgt): {x, y}"
LINE "}"
LINE "Foo e(10, 20)";

test_assert(ecs_script_run(world, NULL, expr) == 0);

ECS_COMPONENT(world, Position);
ECS_TAG(world, Tgt);

ecs_entity_t foo = ecs_lookup(world, "Foo");
test_assert(foo != 0);

ecs_entity_t e = ecs_lookup(world, "e");
test_assert(e != 0);
test_assert(ecs_has_id(world, e, foo));
test_assert(ecs_has_pair(world, e, ecs_id(Position), Tgt));

const Position *p = ecs_get_pair(world, e, Position, Tgt);
test_assert(p != NULL);
test_int(p->x, 10);
test_int(p->y, 20);

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 @@ -396,6 +396,8 @@ void Template_const_from_prop(void);
void Template_redefine_nested_template_w_prefab(void);
void Template_redefine_nested_template_w_prefab_2(void);
void Template_redefine_nested_template_w_prefab_3(void);
void Template_template_w_script_component(void);
void Template_template_w_script_pair_component(void);

// Testsuite 'Error'
void Error_multi_line_comment_after_newline_before_newline_scope_open(void);
Expand Down Expand Up @@ -2494,6 +2496,14 @@ bake_test_case Template_testcases[] = {
{
"redefine_nested_template_w_prefab_3",
Template_redefine_nested_template_w_prefab_3
},
{
"template_w_script_component",
Template_template_w_script_component
},
{
"template_w_script_pair_component",
Template_template_w_script_pair_component
}
};

Expand Down Expand Up @@ -4691,7 +4701,7 @@ static bake_test_suite suites[] = {
"Template",
NULL,
NULL,
74,
76,
Template_testcases
},
{
Expand Down

0 comments on commit d2ef197

Please sign in to comment.