summaryrefslogtreecommitdiff
path: root/ext/-test-/load/stringify_symbols/stringify_symbols.c
blob: 11a5ee3bc5514c73334a2f3da9de5a149b80314a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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);
}