summaryrefslogtreecommitdiff
path: root/ext/-test-/symbol/type.c
blob: c6dbdd1f8eb26e9725e826201ca8831352112a5a (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "ruby.h"

#ifdef HAVE_RB_IS_CONST_NAME
# define get_symbol_type(type, t, name) do { \
	ID id = rb_check_id(&name); \
	t = (id ? rb_is_##type##_id(id) : rb_is_##type##_name(name)); \
    } while (0)
#else
# define get_symbol_type(type, t, name) do { \
	t = rb_is_##type##_id(rb_to_id(name)); \
    } while (0)
#endif

#define define_symbol_type_p(type) \
static VALUE \
bug_sym_##type##_p(VALUE self, VALUE name) \
{ \
    int t; \
    get_symbol_type(type, t, name); \
    return (t ? Qtrue : Qfalse); \
}

#define declare_symbol_type_p(type) \
    rb_define_singleton_method(klass, #type"?", bug_sym_##type##_p, 1);

#define FOREACH_ID_TYPES(x) x(const) x(class) x(global) x(instance) x(attrset) x(local) x(junk)

FOREACH_ID_TYPES(define_symbol_type_p)

static VALUE
bug_sym_attrset(VALUE self, VALUE name)
{
    ID id = rb_to_id(name);
    id = rb_id_attrset(id);
    return ID2SYM(id);
}

static VALUE
bug_id2str(VALUE self, VALUE sym)
{
    return rb_id2str(SYM2ID(sym));
}

static VALUE
bug_static_p(VALUE self, VALUE sym)
{
    return STATIC_SYM_P(sym) ? Qtrue : Qfalse;
}

static VALUE
bug_dynamic_p(VALUE self, VALUE sym)
{
    return DYNAMIC_SYM_P(sym) ? Qtrue : Qfalse;
}

void
Init_type(VALUE klass)
{
    FOREACH_ID_TYPES(declare_symbol_type_p);
    rb_define_singleton_method(klass, "attrset", bug_sym_attrset, 1);
    rb_define_singleton_method(klass, "id2str", bug_id2str, 1);
    rb_define_singleton_method(klass, "static?", bug_static_p, 1);
    rb_define_singleton_method(klass, "dynamic?", bug_dynamic_p, 1);
}