Skip to content

Commit

Permalink
#1512 Fix issue in observer event forwarding code where source was in…
Browse files Browse the repository at this point in the history
…correctly set
  • Loading branch information
SanderMertens authored Jan 10, 2025
1 parent e0c2318 commit 54bcf3f
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 1 deletion.
4 changes: 4 additions & 0 deletions distr/flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -13818,6 +13818,8 @@ void flecs_emit_forward_id(
return;
}

ecs_entity_t old_src = it->sources[0];

it->ids[0] = id;
it->sources[0] = tgt;
it->event_id = id;
Expand Down Expand Up @@ -13884,6 +13886,8 @@ void flecs_emit_forward_id(
}
}


it->sources[0] = old_src;
it->up_fields = 0;
}

Expand Down
4 changes: 4 additions & 0 deletions src/observable.c
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,8 @@ void flecs_emit_forward_id(
return;
}

ecs_entity_t old_src = it->sources[0];

it->ids[0] = id;
it->sources[0] = tgt;
it->event_id = id;
Expand Down Expand Up @@ -674,6 +676,8 @@ void flecs_emit_forward_id(
}
}


it->sources[0] = old_src;
it->up_fields = 0;
}

Expand Down
1 change: 1 addition & 0 deletions test/core/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -1707,6 +1707,7 @@
"on_table_create",
"on_table_create_is_deferred",
"on_table_create_is_deferred_batched",
"2_children_w_deferred_set",
"cache_test_1",
"cache_test_2",
"cache_test_3",
Expand Down
62 changes: 62 additions & 0 deletions test/core/src/Observer.c
Original file line number Diff line number Diff line change
Expand Up @@ -9879,3 +9879,65 @@ void Observer_on_table_create_is_deferred_batched(void) {

ecs_fini(world);
}

void Observer_2_children_w_deferred_set(void) {
ecs_world_t *world = ecs_mini();

ECS_COMPONENT(world, Position);

Probe ctx = {0};

ecs_entity_t o = ecs_observer(world, {
.query.terms = {{ ecs_id(Position) }},
.events = { EcsOnAdd },
.callback = Observer_w_field,
.ctx = &ctx
});

ecs_entity_t p = ecs_new(world);
ecs_set(world, p, Position, {10, 20});

test_int(ctx.invoked, 1);
test_int(ctx.count, 1);
test_int(ctx.system, o);
test_int(ctx.event, EcsOnAdd);
test_uint(ctx.e[0], p);
test_uint(ctx.s[0][0], 0);
ecs_os_zeromem(&ctx);

{
ecs_defer_begin(world);
ecs_entity_t e = ecs_new(world);
ecs_add_pair(world, e, EcsChildOf, p);
ecs_set(world, e, Position, {20, 30});
test_int(ctx.invoked, 0);
ecs_defer_end(world);

test_int(ctx.invoked, 1);
test_int(ctx.count, 1);
test_int(ctx.system, o);
test_int(ctx.event, EcsOnAdd);
test_uint(ctx.e[0], e);
test_uint(ctx.s[0][0], 0);
ecs_os_zeromem(&ctx);
}

{
ecs_defer_begin(world);
ecs_entity_t e = ecs_new(world);
ecs_add_pair(world, e, EcsChildOf, p);
ecs_set(world, e, Position, {30, 40});
test_int(ctx.invoked, 0);
ecs_defer_end(world);

test_int(ctx.invoked, 1);
test_int(ctx.count, 1);
test_int(ctx.system, o);
test_int(ctx.event, EcsOnAdd);
test_uint(ctx.e[0], e);
test_uint(ctx.s[0][0], 0);
ecs_os_zeromem(&ctx);
}

ecs_fini(world);
}
7 changes: 6 additions & 1 deletion test/core/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,7 @@ void Observer_2_up_terms_w_batched_add(void);
void Observer_on_table_create(void);
void Observer_on_table_create_is_deferred(void);
void Observer_on_table_create_is_deferred_batched(void);
void Observer_2_children_w_deferred_set(void);
void Observer_cache_test_1(void);
void Observer_cache_test_2(void);
void Observer_cache_test_3(void);
Expand Down Expand Up @@ -8735,6 +8736,10 @@ bake_test_case Observer_testcases[] = {
"on_table_create_is_deferred_batched",
Observer_on_table_create_is_deferred_batched
},
{
"2_children_w_deferred_set",
Observer_2_children_w_deferred_set
},
{
"cache_test_1",
Observer_cache_test_1
Expand Down Expand Up @@ -11650,7 +11655,7 @@ static bake_test_suite suites[] = {
"Observer",
NULL,
NULL,
235,
236,
Observer_testcases
},
{
Expand Down

0 comments on commit 54bcf3f

Please sign in to comment.