ruby/ext/-test-/load/stringify_symbols/stringify_symbols.c
Satoshi Tagomori e51f9e9f75 rb_ext_resolve_symbol: C API to resolve and return externed symbols [Feature #20005]
This is a C API for extensions to resolve and get function symbols of other extensions.
Extensions can check the expected symbol is correctly loaded and accessible, and
use it if it is available.
Otherwise, extensions can raise their own error to guide users to setup their
environments correctly and what's missing.
2023-12-14 17:39:42 +09:00

29 lines
722 B
C

#include <ruby.h>
#include "ruby/internal/intern/load.h"
#include "ruby/util.h"
#if SIZEOF_INTPTR_T == SIZEOF_LONG_LONG
# define UINTPTR2NUM ULL2NUM
#elif SIZEOF_INTPTR_T == SIZEOF_LONG
# define UINTPTR2NUM ULONG2NUM
#else
# define UINTPTR2NUM UINT2NUM
#endif
static VALUE
stringify_symbol(VALUE klass, VALUE fname, VALUE sname)
{
void *ptr = rb_ext_resolve_symbol(StringValueCStr(fname), StringValueCStr(sname));
if (ptr == NULL) {
return Qnil;
}
uintptr_t uintptr = (uintptr_t)ptr;
return UINTPTR2NUM(uintptr);
}
void
Init_stringify_symbols(void)
{
VALUE mod = rb_define_module("StringifySymbols");
rb_define_singleton_method(mod, "stringify_symbol", stringify_symbol, 2);
}