summaryrefslogtreecommitdiff
path: root/spec/ruby/optional/capi/ext/module_spec.c
blob: 12bcf999835ee8c5ba9c4b1f3dd91c589477316c (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "ruby.h"
#include "rubyspec.h"

#ifdef __cplusplus
extern "C" {
#endif

static VALUE module_specs_test_method(VALUE self) {
  return ID2SYM(rb_intern("test_method"));
}

static VALUE module_specs_const_defined(VALUE self, VALUE klass, VALUE id) {
  return rb_const_defined(klass, SYM2ID(id)) ? Qtrue : Qfalse;
}

static VALUE module_specs_const_defined_at(VALUE self, VALUE klass, VALUE id) {
  return rb_const_defined_at(klass, SYM2ID(id)) ? Qtrue : Qfalse;
}

static VALUE module_specs_const_get(VALUE self, VALUE klass, VALUE val) {
  return rb_const_get(klass, SYM2ID(val));
}

static VALUE module_specs_const_get_at(VALUE self, VALUE klass, VALUE val) {
  return rb_const_get_at(klass, SYM2ID(val));
}

static VALUE module_specs_const_get_from(VALUE self, VALUE klass, VALUE val) {
  return rb_const_get_from(klass, SYM2ID(val));
}

static VALUE module_specs_const_set(VALUE self, VALUE klass, VALUE name, VALUE val) {
  rb_const_set(klass, SYM2ID(name), val);
  return Qnil;
}

static VALUE module_specs_rb_define_alias(VALUE self, VALUE obj,
  VALUE new_name, VALUE old_name) {

  rb_define_alias(obj, RSTRING_PTR(new_name), RSTRING_PTR(old_name));
  return Qnil;
}

static VALUE module_specs_rb_alias(VALUE self, VALUE obj,
  VALUE new_name, VALUE old_name) {

  rb_alias(obj, SYM2ID(new_name), SYM2ID(old_name));
  return Qnil;
}

static VALUE module_specs_rb_define_module(VALUE self, VALUE name) {
  return rb_define_module(RSTRING_PTR(name));
}

static VALUE module_specs_rb_define_module_under(VALUE self, VALUE outer, VALUE name) {
  return rb_define_module_under(outer, RSTRING_PTR(name));
}

static VALUE module_specs_define_const(VALUE self, VALUE klass, VALUE str_name, VALUE val) {
  rb_define_const(klass, RSTRING_PTR(str_name), val);
  return Qnil;
}

static VALUE module_specs_define_global_const(VALUE self, VALUE str_name, VALUE obj) {
  rb_define_global_const(RSTRING_PTR(str_name), obj);
  return Qnil;
}

static VALUE module_specs_rb_define_global_function(VALUE self, VALUE str_name) {
  rb_define_global_function(RSTRING_PTR(str_name), module_specs_test_method, 0);
  return Qnil;
}

static VALUE module_specs_rb_define_method(VALUE self, VALUE cls, VALUE str_name) {
  rb_define_method(cls, RSTRING_PTR(str_name), module_specs_test_method, 0);
  return Qnil;
}

static VALUE module_specs_method_var_args_1(int argc, VALUE *argv, VALUE self) {
  VALUE ary = rb_ary_new();
  int i;
  for (i = 0; i < argc; i++) {
    rb_ary_push(ary, argv[i]);
  }
  return ary;
}

static VALUE module_specs_method_var_args_2(VALUE self, VALUE argv) {
  return argv;
}

static VALUE module_specs_rb_define_method_1required(VALUE self, VALUE arg1) {
    return arg1;
}

static VALUE module_specs_rb_define_method_2required(VALUE self, VALUE arg1, VALUE arg2) {
    return arg2;
}

static VALUE module_specs_rb_define_module_function(VALUE self, VALUE cls, VALUE str_name) {
  rb_define_module_function(cls, RSTRING_PTR(str_name), module_specs_test_method, 0);
  return Qnil;
}

static VALUE module_specs_rb_define_private_method(VALUE self, VALUE cls, VALUE str_name) {
  rb_define_private_method(cls, RSTRING_PTR(str_name), module_specs_test_method, 0);
  return Qnil;
}

static VALUE module_specs_rb_define_protected_method(VALUE self, VALUE cls, VALUE str_name) {
  rb_define_protected_method(cls, RSTRING_PTR(str_name), module_specs_test_method, 0);
  return Qnil;
}

static VALUE module_specs_rb_define_singleton_method(VALUE self, VALUE cls, VALUE str_name) {
  rb_define_singleton_method(cls, RSTRING_PTR(str_name), module_specs_test_method, 0);
  return Qnil;
}

static VALUE module_specs_rb_undef_method(VALUE self, VALUE cls, VALUE str_name) {
  rb_undef_method(cls, RSTRING_PTR(str_name));
  return Qnil;
}

static VALUE module_specs_rb_undef(VALUE self, VALUE cls, VALUE symbol_name) {
  rb_undef(cls, SYM2ID(symbol_name));
  return Qnil;
}

static VALUE module_specs_rbclass2name(VALUE self, VALUE klass) {
  return rb_str_new2(rb_class2name(klass));
}

static VALUE module_specs_rb_mod_ancestors(VALUE self, VALUE klass) {
  return rb_mod_ancestors(klass);
}

void Init_module_spec(void) {
  VALUE cls = rb_define_class("CApiModuleSpecs", rb_cObject);
  rb_define_method(cls, "rb_const_defined", module_specs_const_defined, 2);
  rb_define_method(cls, "rb_const_defined_at", module_specs_const_defined_at, 2);
  rb_define_method(cls, "rb_const_get", module_specs_const_get, 2);
  rb_define_method(cls, "rb_const_get_at", module_specs_const_get_at, 2);
  rb_define_method(cls, "rb_const_get_from", module_specs_const_get_from, 2);
  rb_define_method(cls, "rb_const_set", module_specs_const_set, 3);
  rb_define_method(cls, "rb_define_alias", module_specs_rb_define_alias, 3);
  rb_define_method(cls, "rb_alias", module_specs_rb_alias, 3);
  rb_define_method(cls, "rb_define_module", module_specs_rb_define_module, 1);
  rb_define_method(cls, "rb_define_module_under", module_specs_rb_define_module_under, 2);
  rb_define_method(cls, "rb_define_const", module_specs_define_const, 3);
  rb_define_method(cls, "rb_define_global_const", module_specs_define_global_const, 2);
  rb_define_method(cls, "rb_define_global_function", module_specs_rb_define_global_function, 1);

  rb_define_method(cls, "rb_define_method", module_specs_rb_define_method, 2);
  rb_define_method(cls, "rb_define_method_varargs_1", module_specs_method_var_args_1, -1);
  rb_define_method(cls, "rb_define_method_varargs_2", module_specs_method_var_args_2, -2);
  rb_define_method(cls, "rb_define_method_1required", module_specs_rb_define_method_1required, 1);
  rb_define_method(cls, "rb_define_method_2required", module_specs_rb_define_method_2required, 2);

  rb_define_method(cls, "rb_define_module_function", module_specs_rb_define_module_function, 2);

  rb_define_method(cls, "rb_define_private_method", module_specs_rb_define_private_method, 2);

  rb_define_method(cls, "rb_define_protected_method", module_specs_rb_define_protected_method, 2);

  rb_define_method(cls, "rb_define_singleton_method", module_specs_rb_define_singleton_method, 2);

  rb_define_method(cls, "rb_undef_method", module_specs_rb_undef_method, 2);
  rb_define_method(cls, "rb_undef", module_specs_rb_undef, 2);
  rb_define_method(cls, "rb_class2name", module_specs_rbclass2name, 1);
  rb_define_method(cls, "rb_mod_ancestors", module_specs_rb_mod_ancestors, 1);
}

#ifdef __cplusplus
}
#endif