Skip to content

Commit

Permalink
Fix issue where assigning id value in script would not take into acco…
Browse files Browse the repository at this point in the history
…unt using
  • Loading branch information
SanderMertens committed Dec 27, 2024
1 parent 3ea8c87 commit d49297b
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 4 deletions.
2 changes: 1 addition & 1 deletion distr/flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -79858,7 +79858,7 @@ int flecs_expr_identifier_visit_type(
ecs_expr_value_node_t *result = flecs_expr_value_from(
script, (ecs_expr_node_t*)node, type);

if (type == ecs_id(ecs_entity_t)) {
if (type == ecs_id(ecs_entity_t) || type == ecs_id(ecs_id_t)) {
result->storage.entity = desc->lookup_action(
script->world, node->value, desc->lookup_ctx);
result->ptr = &result->storage.entity;
Expand Down
2 changes: 1 addition & 1 deletion src/addons/script/expr/visit_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ int flecs_expr_identifier_visit_type(
ecs_expr_value_node_t *result = flecs_expr_value_from(
script, (ecs_expr_node_t*)node, type);

if (type == ecs_id(ecs_entity_t)) {
if (type == ecs_id(ecs_entity_t) || type == ecs_id(ecs_id_t)) {
result->storage.entity = desc->lookup_action(
script->world, node->value, desc->lookup_ctx);
result->ptr = &result->storage.entity;
Expand Down
8 changes: 7 additions & 1 deletion test/script/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,13 @@
"for_range_vars",
"for_range_1_4",
"for_range_min_1_2",
"variable_assign_self"
"variable_assign_self",
"func_w_entity_arg",
"func_w_entity_arg_w_using",
"method_w_entity_arg",
"method_w_entity_arg_w_using",
"assign_id",
"assign_id_w_using"
]
}, {
"id": "Template",
Expand Down
219 changes: 219 additions & 0 deletions test/script/src/Eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -9668,3 +9668,222 @@ void Eval_variable_assign_self(void) {

ecs_fini(world);
}

static
void func_is_component(
const ecs_function_ctx_t *ctx,
int32_t argc,
const ecs_value_t *argv,
ecs_value_t *result)
{
test_assert(result != NULL);
test_int(argc, 1);
test_assert(argv != NULL);

*(bool*)result->ptr = (*(ecs_entity_t*)argv[0].ptr) == ecs_id(EcsComponent);
}

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

ECS_TAG(world, True);
ECS_TAG(world, False);

ecs_function(world, {
.name = "is_component",
.return_type = ecs_id(ecs_bool_t),
.params = {
{ "a", ecs_id(ecs_entity_t) }
},
.callback = func_is_component
});

const char *expr =
HEAD "e {"
LINE " (bool, True): {is_component(flecs.core.Component)}"
LINE " (bool, False): {is_component(flecs.core.Relationship)}"
LINE "}";

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

ecs_entity_t e = ecs_lookup(world, "e");
test_assert(e != 0);
{
const bool *v = ecs_get_pair(world, e, ecs_bool_t, True);
test_assert(v != NULL);
test_bool(*v, true);
}
{
const bool *v = ecs_get_pair(world, e, ecs_bool_t, False);
test_assert(v != NULL);
test_bool(*v, false);
}

ecs_fini(world);
}

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

ECS_TAG(world, True);
ECS_TAG(world, False);

ecs_function(world, {
.name = "is_component",
.return_type = ecs_id(ecs_bool_t),
.params = {
{ "a", ecs_id(ecs_entity_t) }
},
.callback = func_is_component
});

const char *expr =
HEAD "using flecs"
LINE "e {"
LINE " (bool, True): {is_component(core.Component)}"
LINE " (bool, False): {is_component(core.Relationship)}"
LINE "}";

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

ecs_entity_t e = ecs_lookup(world, "e");
test_assert(e != 0);
{
const bool *v = ecs_get_pair(world, e, ecs_bool_t, True);
test_assert(v != NULL);
test_bool(*v, true);
}
{
const bool *v = ecs_get_pair(world, e, ecs_bool_t, False);
test_assert(v != NULL);
test_bool(*v, false);
}

ecs_fini(world);
}

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

ECS_TAG(world, True);
ECS_TAG(world, False);

const char *expr =
HEAD "e {"
LINE " (bool, True): {flecs.core.Component.has(flecs.core.Component)}"
LINE " (bool, False): {flecs.core.Component.has(flecs.core.Relationship)}"
LINE "}";

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

ecs_entity_t e = ecs_lookup(world, "e");
test_assert(e != 0);
{
const bool *v = ecs_get_pair(world, e, ecs_bool_t, True);
test_assert(v != NULL);
test_bool(*v, true);
}
{
const bool *v = ecs_get_pair(world, e, ecs_bool_t, False);
test_assert(v != NULL);
test_bool(*v, false);
}

ecs_fini(world);
}

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

ECS_TAG(world, True);
ECS_TAG(world, False);

const char *expr =
HEAD "using flecs"
LINE "e {"
LINE " (bool, True): {core.Component.has(core.Component)}"
LINE " (bool, False): {core.Component.has(core.Relationship)}"
LINE "}";

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

ecs_entity_t e = ecs_lookup(world, "e");
test_assert(e != 0);
{
const bool *v = ecs_get_pair(world, e, ecs_bool_t, True);
test_assert(v != NULL);
test_bool(*v, true);
}
{
const bool *v = ecs_get_pair(world, e, ecs_bool_t, False);
test_assert(v != NULL);
test_bool(*v, false);
}

ecs_fini(world);
}

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

typedef struct {
ecs_id_t value;
} Id;

ecs_entity_t ecs_id(Id) = ecs_struct(world, {
.entity = ecs_entity(world, {.name = "Id"}),
.members = {
{"value", ecs_id(ecs_id_t)}
}
});

const char *expr =
HEAD "Foo = Id: {flecs.core.Component}";

test_assert(ecs_script_run(world, NULL, expr) == 0);
ecs_entity_t foo = ecs_lookup(world, "Foo");

test_assert(foo != 0);

test_assert(ecs_has(world, foo, Id));

const Id *ptr = ecs_get(world, foo, Id);
test_assert(ptr != NULL);

test_int(ptr->value, ecs_id(EcsComponent));

ecs_fini(world);
}

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

typedef struct {
ecs_id_t value;
} Id;

ecs_entity_t ecs_id(Id) = ecs_struct(world, {
.entity = ecs_entity(world, {.name = "Id"}),
.members = {
{"value", ecs_id(ecs_id_t)}
}
});

const char *expr =
HEAD "using flecs"
LINE "Foo = Id: {core.Component}";

test_assert(ecs_script_run(world, NULL, expr) == 0);
ecs_entity_t foo = ecs_lookup(world, "Foo");

test_assert(foo != 0);

test_assert(ecs_has(world, foo, Id));

const Id *ptr = ecs_get(world, foo, Id);
test_assert(ptr != NULL);

test_int(ptr->value, ecs_id(EcsComponent));

ecs_fini(world);
}
32 changes: 31 additions & 1 deletion test/script/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,12 @@ void Eval_for_range_vars(void);
void Eval_for_range_1_4(void);
void Eval_for_range_min_1_2(void);
void Eval_variable_assign_self(void);
void Eval_func_w_entity_arg(void);
void Eval_func_w_entity_arg_w_using(void);
void Eval_method_w_entity_arg(void);
void Eval_method_w_entity_arg_w_using(void);
void Eval_assign_id(void);
void Eval_assign_id_w_using(void);

// Testsuite 'Template'
void Template_template_no_scope(void);
Expand Down Expand Up @@ -2019,6 +2025,30 @@ bake_test_case Eval_testcases[] = {
{
"variable_assign_self",
Eval_variable_assign_self
},
{
"func_w_entity_arg",
Eval_func_w_entity_arg
},
{
"func_w_entity_arg_w_using",
Eval_func_w_entity_arg_w_using
},
{
"method_w_entity_arg",
Eval_method_w_entity_arg
},
{
"method_w_entity_arg_w_using",
Eval_method_w_entity_arg_w_using
},
{
"assign_id",
Eval_assign_id
},
{
"assign_id_w_using",
Eval_assign_id_w_using
}
};

Expand Down Expand Up @@ -4304,7 +4334,7 @@ static bake_test_suite suites[] = {
"Eval",
NULL,
NULL,
286,
292,
Eval_testcases
},
{
Expand Down

0 comments on commit d49297b

Please sign in to comment.