diff --git a/distr/flecs.c b/distr/flecs.c index 3603b2861..1499d9fe9 100644 --- a/distr/flecs.c +++ b/distr/flecs.c @@ -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; diff --git a/src/addons/script/expr/visit_type.c b/src/addons/script/expr/visit_type.c index 09ba1150f..e2100dea7 100644 --- a/src/addons/script/expr/visit_type.c +++ b/src/addons/script/expr/visit_type.c @@ -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; diff --git a/test/script/project.json b/test/script/project.json index 4755fd829..38c8e972c 100644 --- a/test/script/project.json +++ b/test/script/project.json @@ -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", diff --git a/test/script/src/Eval.c b/test/script/src/Eval.c index 76b0a38e1..1d8cd7bfd 100644 --- a/test/script/src/Eval.c +++ b/test/script/src/Eval.c @@ -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); +} diff --git a/test/script/src/main.c b/test/script/src/main.c index 013c986b5..1365dbb0a 100644 --- a/test/script/src/main.c +++ b/test/script/src/main.c @@ -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); @@ -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 } }; @@ -4304,7 +4334,7 @@ static bake_test_suite suites[] = { "Eval", NULL, NULL, - 286, + 292, Eval_testcases }, {