Skip to content

Commit

Permalink
#940 Fix component registration issue with binaries compiled by diffe…
Browse files Browse the repository at this point in the history
…rent compilers
  • Loading branch information
SanderMertens authored Mar 26, 2023
1 parent 57c77e1 commit 3c705ed
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 30 deletions.
29 changes: 18 additions & 11 deletions flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -18602,9 +18602,10 @@ void ecs_cpp_trim_type_name(
char* ecs_cpp_get_type_name(
char *type_name,
const char *func_name,
size_t len)
size_t len,
size_t front_len)
{
memcpy(type_name, func_name + ECS_FUNC_NAME_FRONT(const char*, type_name), len);
memcpy(type_name, func_name + front_len, len);
type_name[len] = '\0';
ecs_cpp_trim_type_name(type_name);
return type_name;
Expand Down Expand Up @@ -18635,20 +18636,21 @@ char* ecs_cpp_get_symbol_name(
}

static
const char* cpp_func_rchr(
const char* flecs_cpp_func_rchr(
const char *func_name,
ecs_size_t func_name_len,
ecs_size_t func_back_len,
char ch)
{
const char *r = strrchr(func_name, ch);
if ((r - func_name) >= (func_name_len - flecs_uto(ecs_size_t, ECS_FUNC_NAME_BACK))) {
if ((r - func_name) >= (func_name_len - flecs_uto(ecs_size_t, func_back_len))) {
return NULL;
}
return r;
}

static
const char* cpp_func_max(
const char* flecs_cpp_func_max(
const char *a,
const char *b)
{
Expand All @@ -18659,18 +18661,23 @@ const char* cpp_func_max(
char* ecs_cpp_get_constant_name(
char *constant_name,
const char *func_name,
size_t func_name_len)
size_t func_name_len,
size_t func_back_len)
{
ecs_size_t f_len = flecs_uto(ecs_size_t, func_name_len);
const char *start = cpp_func_rchr(func_name, f_len, ' ');
start = cpp_func_max(start, cpp_func_rchr(func_name, f_len, ')'));
start = cpp_func_max(start, cpp_func_rchr(func_name, f_len, ':'));
start = cpp_func_max(start, cpp_func_rchr(func_name, f_len, ','));
ecs_size_t fb_len = flecs_uto(ecs_size_t, func_back_len);
const char *start = flecs_cpp_func_rchr(func_name, f_len, fb_len, ' ');
start = flecs_cpp_func_max(start, flecs_cpp_func_rchr(
func_name, f_len, fb_len, ')'));
start = flecs_cpp_func_max(start, flecs_cpp_func_rchr(
func_name, f_len, fb_len, ':'));
start = flecs_cpp_func_max(start, flecs_cpp_func_rchr(
func_name, f_len, fb_len, ','));
ecs_assert(start != NULL, ECS_INVALID_PARAMETER, func_name);
start ++;

ecs_size_t len = flecs_uto(ecs_size_t,
(f_len - (start - func_name) - flecs_uto(ecs_size_t, ECS_FUNC_NAME_BACK)));
(f_len - (start - func_name) - fb_len));
ecs_os_memcpy_n(constant_name, start, char, len);
constant_name[len] = '\0';
return constant_name;
Expand Down
12 changes: 8 additions & 4 deletions flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -14449,7 +14449,8 @@ FLECS_API
char* ecs_cpp_get_type_name(
char *type_name,
const char *func_name,
size_t len);
size_t len,
size_t front_len);

FLECS_API
char* ecs_cpp_get_symbol_name(
Expand All @@ -14461,7 +14462,8 @@ FLECS_API
char* ecs_cpp_get_constant_name(
char *constant_name,
const char *func_name,
size_t len);
size_t len,
size_t back_len);

FLECS_API
const char* ecs_cpp_trim_module(
Expand Down Expand Up @@ -15257,7 +15259,8 @@ static const char* enum_constant_to_name() {
static const size_t len = ECS_FUNC_TYPE_LEN(const char*, enum_constant_to_name, ECS_FUNC_NAME);
static char result[len + 1] = {};
return ecs_cpp_get_constant_name(
result, ECS_FUNC_NAME, string::length(ECS_FUNC_NAME));
result, ECS_FUNC_NAME, string::length(ECS_FUNC_NAME),
ECS_FUNC_NAME_BACK);
}

/** Enumeration constant data */
Expand Down Expand Up @@ -23282,7 +23285,8 @@ template <typename T>
inline static const char* type_name() {
static const size_t len = ECS_FUNC_TYPE_LEN(const char*, type_name, ECS_FUNC_NAME);
static char result[len + 1] = {};
return ecs_cpp_get_type_name(result, ECS_FUNC_NAME, len);
static const size_t front_len = ECS_FUNC_NAME_FRONT(const char*, type_name);
return ecs_cpp_get_type_name(result, ECS_FUNC_NAME, len, front_len);
}
#else
#error "implicit component registration not supported"
Expand Down
3 changes: 2 additions & 1 deletion include/flecs/addons/cpp/component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ template <typename T>
inline static const char* type_name() {
static const size_t len = ECS_FUNC_TYPE_LEN(const char*, type_name, ECS_FUNC_NAME);
static char result[len + 1] = {};
return ecs_cpp_get_type_name(result, ECS_FUNC_NAME, len);
static const size_t front_len = ECS_FUNC_NAME_FRONT(const char*, type_name);
return ecs_cpp_get_type_name(result, ECS_FUNC_NAME, len, front_len);
}
#else
#error "implicit component registration not supported"
Expand Down
3 changes: 2 additions & 1 deletion include/flecs/addons/cpp/utils/enum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ static const char* enum_constant_to_name() {
static const size_t len = ECS_FUNC_TYPE_LEN(const char*, enum_constant_to_name, ECS_FUNC_NAME);
static char result[len + 1] = {};
return ecs_cpp_get_constant_name(
result, ECS_FUNC_NAME, string::length(ECS_FUNC_NAME));
result, ECS_FUNC_NAME, string::length(ECS_FUNC_NAME),
ECS_FUNC_NAME_BACK);
}

/** Enumeration constant data */
Expand Down
6 changes: 4 additions & 2 deletions include/flecs/addons/flecs_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ FLECS_API
char* ecs_cpp_get_type_name(
char *type_name,
const char *func_name,
size_t len);
size_t len,
size_t front_len);

FLECS_API
char* ecs_cpp_get_symbol_name(
Expand All @@ -51,7 +52,8 @@ FLECS_API
char* ecs_cpp_get_constant_name(
char *constant_name,
const char *func_name,
size_t len);
size_t len,
size_t back_len);

FLECS_API
const char* ecs_cpp_trim_module(
Expand Down
29 changes: 18 additions & 11 deletions src/addons/flecs_cpp.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ void ecs_cpp_trim_type_name(
char* ecs_cpp_get_type_name(
char *type_name,
const char *func_name,
size_t len)
size_t len,
size_t front_len)
{
memcpy(type_name, func_name + ECS_FUNC_NAME_FRONT(const char*, type_name), len);
memcpy(type_name, func_name + front_len, len);
type_name[len] = '\0';
ecs_cpp_trim_type_name(type_name);
return type_name;
Expand Down Expand Up @@ -117,20 +118,21 @@ char* ecs_cpp_get_symbol_name(
}

static
const char* cpp_func_rchr(
const char* flecs_cpp_func_rchr(
const char *func_name,
ecs_size_t func_name_len,
ecs_size_t func_back_len,
char ch)
{
const char *r = strrchr(func_name, ch);
if ((r - func_name) >= (func_name_len - flecs_uto(ecs_size_t, ECS_FUNC_NAME_BACK))) {
if ((r - func_name) >= (func_name_len - flecs_uto(ecs_size_t, func_back_len))) {
return NULL;
}
return r;
}

static
const char* cpp_func_max(
const char* flecs_cpp_func_max(
const char *a,
const char *b)
{
Expand All @@ -141,18 +143,23 @@ const char* cpp_func_max(
char* ecs_cpp_get_constant_name(
char *constant_name,
const char *func_name,
size_t func_name_len)
size_t func_name_len,
size_t func_back_len)
{
ecs_size_t f_len = flecs_uto(ecs_size_t, func_name_len);
const char *start = cpp_func_rchr(func_name, f_len, ' ');
start = cpp_func_max(start, cpp_func_rchr(func_name, f_len, ')'));
start = cpp_func_max(start, cpp_func_rchr(func_name, f_len, ':'));
start = cpp_func_max(start, cpp_func_rchr(func_name, f_len, ','));
ecs_size_t fb_len = flecs_uto(ecs_size_t, func_back_len);
const char *start = flecs_cpp_func_rchr(func_name, f_len, fb_len, ' ');
start = flecs_cpp_func_max(start, flecs_cpp_func_rchr(
func_name, f_len, fb_len, ')'));
start = flecs_cpp_func_max(start, flecs_cpp_func_rchr(
func_name, f_len, fb_len, ':'));
start = flecs_cpp_func_max(start, flecs_cpp_func_rchr(
func_name, f_len, fb_len, ','));
ecs_assert(start != NULL, ECS_INVALID_PARAMETER, func_name);
start ++;

ecs_size_t len = flecs_uto(ecs_size_t,
(f_len - (start - func_name) - flecs_uto(ecs_size_t, ECS_FUNC_NAME_BACK)));
(f_len - (start - func_name) - fb_len));
ecs_os_memcpy_n(constant_name, start, char, len);
constant_name[len] = '\0';
return constant_name;
Expand Down

0 comments on commit 3c705ed

Please sign in to comment.