Skip to content

Commit

Permalink
Fix issue where value of component defined in script was freed after …
Browse files Browse the repository at this point in the history
…component was deleted
  • Loading branch information
SanderMertens committed Dec 9, 2024
1 parent ddc5a69 commit af3fd0d
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 2 deletions.
3 changes: 3 additions & 0 deletions distr/flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -57789,6 +57789,9 @@ int ecs_script_update(
ecs_entity_t prev = ecs_set_with(world, flecs_script_tag(e, instance));

if (ecs_script_eval(s->script, NULL)) {
ecs_script_free(s->script);
s->script = NULL;

ecs_delete_with(world, ecs_pair_t(EcsScript, e));
result = -1;
}
Expand Down
3 changes: 3 additions & 0 deletions src/addons/script/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ int ecs_script_update(
ecs_entity_t prev = ecs_set_with(world, flecs_script_tag(e, instance));

if (ecs_script_eval(s->script, NULL)) {
ecs_script_free(s->script);
s->script = NULL;

ecs_delete_with(world, ecs_pair_t(EcsScript, e));
result = -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 @@ -387,7 +387,9 @@
"update_template_after_error",
"template_in_template",
"unterminated_binary",
"component_in_with_scope"
"component_in_with_scope",
"reload_script_w_component_w_error",
"reload_script_w_component_w_error_again"
]
}, {
"id": "Expr",
Expand Down
83 changes: 83 additions & 0 deletions test/script/src/Error.c
Original file line number Diff line number Diff line change
Expand Up @@ -1239,3 +1239,86 @@ void Error_unterminated_binary(void) {

ecs_fini(world);
}

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

ecs_entity_t s = ecs_script(world, {
.code =
"struct Position {\n"
" x = f32\n"
" y = f32\n"
"}\n"
"\n"
"e {\n"
" Position: {10, 20}\n"
"}\n"
});

test_assert(s != 0);

ecs_log_set_level(-4);

test_assert(0 != ecs_script_update(world, s, 0,
"struct Position {\n"
" x = f32\n"
" y = f32\n"
"}\n"
"\n"
"e {\n"
" Position: {10, 20}\n"
"}\n"
"\n"
"f\n"
));

ecs_fini(world);
}

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

ecs_entity_t s = ecs_script(world, {
.code =
"struct Position {\n"
" x = f32\n"
" y = f32\n"
"}\n"
"\n"
"e {\n"
" Position: {10, 20}\n"
"}\n"
});

test_assert(s != 0);

ecs_log_set_level(-4);

test_assert(0 != ecs_script_update(world, s, 0,
"struct Position {\n"
" x = f32\n"
" y = f32\n"
"}\n"
"\n"
"e {\n"
" Position: {10, 20}\n"
"}\n"
"\n"
"f\n"
));

ecs_log_set_level(-1);

test_assert(0 == ecs_script_update(world, s, 0,
"struct Position {\n"
" x = f32\n"
" y = f32\n"
"}\n"
"\n"
"e {\n"
" Position: {10, 20}\n"
"}\n"
));

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 @@ -380,6 +380,8 @@ void Error_update_template_after_error(void);
void Error_template_in_template(void);
void Error_unterminated_binary(void);
void Error_component_in_with_scope(void);
void Error_reload_script_w_component_w_error(void);
void Error_reload_script_w_component_w_error_again(void);

// Testsuite 'Expr'
void Expr_setup(void);
Expand Down Expand Up @@ -2210,6 +2212,14 @@ bake_test_case Error_testcases[] = {
{
"component_in_with_scope",
Error_component_in_with_scope
},
{
"reload_script_w_component_w_error",
Error_reload_script_w_component_w_error
},
{
"reload_script_w_component_w_error_again",
Error_reload_script_w_component_w_error_again
}
};

Expand Down Expand Up @@ -3633,7 +3643,7 @@ static bake_test_suite suites[] = {
"Error",
NULL,
NULL,
65,
67,
Error_testcases
},
{
Expand Down

0 comments on commit af3fd0d

Please sign in to comment.