Skip to content

Commit

Permalink
Fix issue where C++ component with name of core entity would be incor…
Browse files Browse the repository at this point in the history
…rectly registered
  • Loading branch information
SanderMertens committed Jul 19, 2023
1 parent e2d7648 commit 1e35ea1
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 34 deletions.
18 changes: 10 additions & 8 deletions flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -19151,7 +19151,7 @@ ecs_entity_t ecs_cpp_component_register(
/* If no entity is found, lookup symbol to check if the component was
* registered under a different name. */
} else if (!implicit_name) {
ent = ecs_lookup_symbol(world, symbol, false);
ent = ecs_lookup_symbol(world, symbol, false, false);
ecs_assert(ent == 0 || (ent == id), ECS_INCONSISTENT_COMPONENT_ID, symbol);
}

Expand Down Expand Up @@ -19184,7 +19184,7 @@ ecs_entity_t ecs_cpp_component_register_explicit(
if (!name) {
// If no name was provided first check if a type with the provided
// symbol was already registered.
id = ecs_lookup_symbol(world, symbol, false);
id = ecs_lookup_symbol(world, symbol, false, false);
if (id) {
existing_name = ecs_get_path_w_sep(world, 0, id, "::", "::");
name = existing_name;
Expand Down Expand Up @@ -46466,7 +46466,8 @@ ecs_entity_t meta_lookup_array(
goto error;
}

ecs_entity_t element_type = ecs_lookup_symbol(world, params.type.type, true);
ecs_entity_t element_type = ecs_lookup_symbol(
world, params.type.type, true, true);
if (!element_type) {
ecs_meta_error(ctx, params_decl, "unknown element type '%s'",
params.type.type);
Expand Down Expand Up @@ -46634,7 +46635,7 @@ ecs_entity_t meta_lookup(
} else if (!ecs_os_strcmp(typename, "char*")) {
type = ecs_id(ecs_string_t);
} else {
type = ecs_lookup_symbol(world, typename, true);
type = ecs_lookup_symbol(world, typename, true, true);
}
} else {
if (!ecs_os_strcmp(typename, "char")) {
Expand All @@ -46649,7 +46650,7 @@ ecs_entity_t meta_lookup(
typename = "flecs.meta.string";
}

type = ecs_lookup_symbol(world, typename, true);
type = ecs_lookup_symbol(world, typename, true, true);
}

if (count != 1) {
Expand Down Expand Up @@ -50616,7 +50617,7 @@ int flecs_term_id_lookup(
return 0;
}

ecs_entity_t e = ecs_lookup_symbol(world, name, true);
ecs_entity_t e = ecs_lookup_symbol(world, name, true, true);
if (scope && !e) {
e = ecs_lookup_child(world, scope, name);
}
Expand Down Expand Up @@ -62398,7 +62399,8 @@ ecs_entity_t ecs_lookup(
ecs_entity_t ecs_lookup_symbol(
const ecs_world_t *world,
const char *name,
bool lookup_as_path)
bool lookup_as_path,
bool recursive)
{
if (!name) {
return 0;
Expand All @@ -62413,7 +62415,7 @@ ecs_entity_t ecs_lookup_symbol(
}

if (lookup_as_path) {
return ecs_lookup_fullpath(world, name);
return ecs_lookup_path_w_sep(world, 0, name, ".", NULL, recursive);
}

error:
Expand Down
13 changes: 10 additions & 3 deletions flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -5960,12 +5960,19 @@ ecs_entity_t ecs_lookup_path_w_sep(
*
* This operation can be useful to resolve, for example, a type by its C
* identifier, which does not include the Flecs namespacing.
*
* @param world The world.
* @param symbol The symbol.
* @param lookup_as_path If not found as a symbol, lookup as path.
* @param recursive If looking up as path, recursively traverse up the tree.
* @return The entity if found, else 0.
*/
FLECS_API
ecs_entity_t ecs_lookup_symbol(
const ecs_world_t *world,
const char *symbol,
bool lookup_as_path);
bool lookup_as_path,
bool recursive);

/** Get a path identifier for an entity.
* This operation creates a path that contains the names of the entities from
Expand Down Expand Up @@ -27900,7 +27907,7 @@ ecs_entity_t do_import(world& world, const char *symbol) {
ecs_set_scope(world, scope);

// It should now be possible to lookup the module
ecs_entity_t m = ecs_lookup_symbol(world, symbol, true);
ecs_entity_t m = ecs_lookup_symbol(world, symbol, true, false);
ecs_assert(m != 0, ECS_MODULE_UNDEFINED, symbol);
ecs_assert(m == m_c, ECS_INTERNAL_ERROR, NULL);

Expand All @@ -27913,7 +27920,7 @@ template <typename T>
flecs::entity import(world& world) {
const char *symbol = _::symbol_name<T>();

ecs_entity_t m = ecs_lookup_symbol(world, symbol, true);
ecs_entity_t m = ecs_lookup_symbol(world, symbol, true, false);

if (!_::cpp_type<T>::registered(world)) {

Expand Down
9 changes: 8 additions & 1 deletion include/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -3207,12 +3207,19 @@ ecs_entity_t ecs_lookup_path_w_sep(
*
* This operation can be useful to resolve, for example, a type by its C
* identifier, which does not include the Flecs namespacing.
*
* @param world The world.
* @param symbol The symbol.
* @param lookup_as_path If not found as a symbol, lookup as path.
* @param recursive If looking up as path, recursively traverse up the tree.
* @return The entity if found, else 0.
*/
FLECS_API
ecs_entity_t ecs_lookup_symbol(
const ecs_world_t *world,
const char *symbol,
bool lookup_as_path);
bool lookup_as_path,
bool recursive);

/** Get a path identifier for an entity.
* This operation creates a path that contains the names of the entities from
Expand Down
4 changes: 2 additions & 2 deletions include/flecs/addons/cpp/mixins/module/impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ecs_entity_t do_import(world& world, const char *symbol) {
ecs_set_scope(world, scope);

// It should now be possible to lookup the module
ecs_entity_t m = ecs_lookup_symbol(world, symbol, true);
ecs_entity_t m = ecs_lookup_symbol(world, symbol, true, false);
ecs_assert(m != 0, ECS_MODULE_UNDEFINED, symbol);
ecs_assert(m == m_c, ECS_INTERNAL_ERROR, NULL);

Expand All @@ -39,7 +39,7 @@ template <typename T>
flecs::entity import(world& world) {
const char *symbol = _::symbol_name<T>();

ecs_entity_t m = ecs_lookup_symbol(world, symbol, true);
ecs_entity_t m = ecs_lookup_symbol(world, symbol, true, false);

if (!_::cpp_type<T>::registered(world)) {

Expand Down
4 changes: 2 additions & 2 deletions src/addons/flecs_cpp.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ ecs_entity_t ecs_cpp_component_register(
/* If no entity is found, lookup symbol to check if the component was
* registered under a different name. */
} else if (!implicit_name) {
ent = ecs_lookup_symbol(world, symbol, false);
ent = ecs_lookup_symbol(world, symbol, false, false);
ecs_assert(ent == 0 || (ent == id), ECS_INCONSISTENT_COMPONENT_ID, symbol);
}

Expand Down Expand Up @@ -367,7 +367,7 @@ ecs_entity_t ecs_cpp_component_register_explicit(
if (!name) {
// If no name was provided first check if a type with the provided
// symbol was already registered.
id = ecs_lookup_symbol(world, symbol, false);
id = ecs_lookup_symbol(world, symbol, false, false);
if (id) {
existing_name = ecs_get_path_w_sep(world, 0, id, "::", "::");
name = existing_name;
Expand Down
7 changes: 4 additions & 3 deletions src/addons/meta_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,8 @@ ecs_entity_t meta_lookup_array(
goto error;
}

ecs_entity_t element_type = ecs_lookup_symbol(world, params.type.type, true);
ecs_entity_t element_type = ecs_lookup_symbol(
world, params.type.type, true, true);
if (!element_type) {
ecs_meta_error(ctx, params_decl, "unknown element type '%s'",
params.type.type);
Expand Down Expand Up @@ -640,7 +641,7 @@ ecs_entity_t meta_lookup(
} else if (!ecs_os_strcmp(typename, "char*")) {
type = ecs_id(ecs_string_t);
} else {
type = ecs_lookup_symbol(world, typename, true);
type = ecs_lookup_symbol(world, typename, true, true);
}
} else {
if (!ecs_os_strcmp(typename, "char")) {
Expand All @@ -655,7 +656,7 @@ ecs_entity_t meta_lookup(
typename = "flecs.meta.string";
}

type = ecs_lookup_symbol(world, typename, true);
type = ecs_lookup_symbol(world, typename, true, true);
}

if (count != 1) {
Expand Down
2 changes: 1 addition & 1 deletion src/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ int flecs_term_id_lookup(
return 0;
}

ecs_entity_t e = ecs_lookup_symbol(world, name, true);
ecs_entity_t e = ecs_lookup_symbol(world, name, true, true);
if (scope && !e) {
e = ecs_lookup_child(world, scope, name);
}
Expand Down
5 changes: 3 additions & 2 deletions src/hierarchy.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,8 @@ ecs_entity_t ecs_lookup(
ecs_entity_t ecs_lookup_symbol(
const ecs_world_t *world,
const char *name,
bool lookup_as_path)
bool lookup_as_path,
bool recursive)
{
if (!name) {
return 0;
Expand All @@ -364,7 +365,7 @@ ecs_entity_t ecs_lookup_symbol(
}

if (lookup_as_path) {
return ecs_lookup_fullpath(world, name);
return ecs_lookup_path_w_sep(world, 0, name, ".", NULL, recursive);
}

error:
Expand Down
4 changes: 2 additions & 2 deletions test/addons/src/Modules.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,11 @@ void Modules_lookup_by_symbol() {

ECS_IMPORT(world, SimpleModule);

ecs_entity_t e = ecs_lookup_symbol(world, "Position", true);
ecs_entity_t e = ecs_lookup_symbol(world, "Position", true, true);
test_assert(e != 0);
test_assert(e == ecs_id(Position));

e = ecs_lookup_symbol(world, "SimpleFooComponent", true);
e = ecs_lookup_symbol(world, "SimpleFooComponent", true, true);
test_assert(e != 0);
test_assert(e == ecs_id(SimpleFooComponent));

Expand Down
14 changes: 7 additions & 7 deletions test/api/src/Lookup.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ void Lookup_lookup_symbol_by_id() {
ecs_world_t *world = ecs_mini();

ecs_ensure(world, 1000);
ecs_entity_t e = ecs_lookup_symbol(world, "1000", true);
ecs_entity_t e = ecs_lookup_symbol(world, "1000", true, true);
test_int(e, 1000);

ecs_fini(world);
Expand All @@ -188,7 +188,7 @@ void Lookup_lookup_symbol_w_digit() {
ecs_world_t *world = ecs_mini();

ecs_entity_t e = ecs_set_symbol(world, 0, "10_id");
ecs_entity_t e2 = ecs_lookup_symbol(world, "10_id", true);
ecs_entity_t e2 = ecs_lookup_symbol(world, "10_id", true, true);
test_assert(e == e2);

ecs_fini(world);
Expand Down Expand Up @@ -297,7 +297,7 @@ void Lookup_lookup_null() {
void Lookup_lookup_symbol_null() {
ecs_world_t *world = ecs_mini();

test_assert(ecs_lookup_symbol(world, NULL, true) == 0);
test_assert(ecs_lookup_symbol(world, NULL, true, true) == 0);

ecs_fini(world);
}
Expand Down Expand Up @@ -389,11 +389,11 @@ void Lookup_lookup_path_wildcard_from_scope() {
void Lookup_resolve_builtin_symbols() {
ecs_world_t *world = ecs_mini();

test_assert(ecs_lookup_symbol(world, "EcsComponent", false) == ecs_id(EcsComponent));
test_assert(ecs_lookup_symbol(world, "EcsIdentifier", false) == ecs_id(EcsIdentifier));
test_assert(ecs_lookup_symbol(world, "EcsComponent", false, true) == ecs_id(EcsComponent));
test_assert(ecs_lookup_symbol(world, "EcsIdentifier", false, true) == ecs_id(EcsIdentifier));

test_assert(ecs_lookup_symbol(world, "EcsName", false) == EcsName);
test_assert(ecs_lookup_symbol(world, "EcsSymbol", false) == EcsSymbol);
test_assert(ecs_lookup_symbol(world, "EcsName", false, true) == EcsName);
test_assert(ecs_lookup_symbol(world, "EcsSymbol", false, true) == EcsSymbol);

ecs_fini(world);
}
Expand Down
4 changes: 3 additions & 1 deletion test/cpp_api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,8 @@
"implicit_module",
"module_in_namespace_w_root_name",
"module_as_entity",
"module_as_component"
"module_as_component",
"module_with_core_name"
]
}, {
"id": "ImplicitComponents",
Expand Down Expand Up @@ -1073,6 +1074,7 @@
"reregister_after_reset_w_hooks_and_in_use",
"reregister_after_reset_w_hooks_and_in_use_implicit",
"register_component_w_reset_in_multithreaded",
"register_component_w_core_name",
"count",
"count_id",
"count_pair",
Expand Down
20 changes: 20 additions & 0 deletions test/cpp_api/src/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ class NamedModuleInRoot {

}

struct Module {
Module(flecs::world& world) {
world.module<Module>();
world.component<Position>();
}
};

void Module_import() {
flecs::world world;
auto m = world.import<ns::SimpleModule>();
Expand Down Expand Up @@ -256,3 +263,16 @@ void Module_module_as_component() {
auto e = world.component<ns::SimpleModule>();
test_assert(m == e);
}

void Module_module_with_core_name() {
flecs::world world;

flecs::entity m = world.import<Module>();
test_assert(m != 0);
test_str(m.path().c_str(), "::Module");

flecs::entity pos = m.lookup("Position");
test_assert(pos != 0);
test_str(pos.path().c_str(), "::Module::Position");
test_assert(pos == world.id<Position>());
}
11 changes: 11 additions & 0 deletions test/cpp_api/src/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,16 @@ void World_register_component_w_reset_in_multithreaded() {
test_int(p->y, 20);
}

struct Module { };

void World_register_component_w_core_name() {
flecs::world ecs;

flecs::entity c = ecs.component<Module>();
test_assert(c != 0);
test_str(c.path().c_str(), "::Module");
}

template <typename T>
struct Tmp { int32_t v; };
struct Test { };
Expand Down Expand Up @@ -1704,3 +1714,4 @@ void World_id_from_pair_type() {
test_assert(id.first() == ecs.id<Position>());
test_assert(id.second() == ecs.id<Velocity>());
}

Loading

0 comments on commit 1e35ea1

Please sign in to comment.