Skip to content

Commit

Permalink
Fix bad any cast errors caused by hash collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Oct 7, 2024
1 parent 245ab01 commit 654f650
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
11 changes: 5 additions & 6 deletions include/rice/rice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1093,10 +1093,10 @@ namespace Rice::detail
Return_T lookup(VALUE klass, ID method_id);

private:
size_t key(VALUE klass, ID method_id);
std::unordered_map<size_t, std::any> natives_ = {};
std::pair<VALUE, ID> key(VALUE klass, ID method_id);
std::map<std::pair<VALUE, ID>, std::any> natives_ = {};
};
}
}

// --------- NativeRegistry.ipp ---------

Expand All @@ -1112,15 +1112,14 @@ namespace Rice::detail
{
// Effective Java (2nd edition)
// https://stackoverflow.com/a/2634715
inline size_t NativeRegistry::key(VALUE klass, ID id)
inline std::pair<VALUE, ID> NativeRegistry::key(VALUE klass, ID id)
{
if (rb_type(klass) == T_ICLASS)
{
klass = detail::protect(rb_class_of, klass);
}

uint32_t prime = 53;
return (prime + klass) * prime + id;
return std::make_pair(klass, id);
}

inline void NativeRegistry::add(VALUE klass, ID method_id, std::any callable)
Expand Down
8 changes: 4 additions & 4 deletions rice/detail/NativeRegistry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ namespace Rice::detail
Return_T lookup(VALUE klass, ID method_id);

private:
size_t key(VALUE klass, ID method_id);
std::unordered_map<size_t, std::any> natives_ = {};
std::pair<VALUE, ID> key(VALUE klass, ID method_id);
std::map<std::pair<VALUE, ID>, std::any> natives_ = {};
};
}
}
#include "NativeRegistry.ipp"

#endif // Rice__detail__NativeRegistry__hpp
#endif // Rice__detail__NativeRegistry__hpp
5 changes: 2 additions & 3 deletions rice/detail/NativeRegistry.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ namespace Rice::detail
{
// Effective Java (2nd edition)
// https://stackoverflow.com/a/2634715
inline size_t NativeRegistry::key(VALUE klass, ID id)
inline std::pair<VALUE, ID> NativeRegistry::key(VALUE klass, ID id)
{
if (rb_type(klass) == T_ICLASS)
{
klass = detail::protect(rb_class_of, klass);
}

uint32_t prime = 53;
return (prime + klass) * prime + id;
return std::make_pair(klass, id);
}

inline void NativeRegistry::add(VALUE klass, ID method_id, std::any callable)
Expand Down

0 comments on commit 654f650

Please sign in to comment.