/
module.cpp
281 lines (207 loc) · 8.62 KB
/
module.cpp
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include "builtin/object.hpp"
#include "builtin/module.hpp"
#include "builtin/symbol.hpp"
#include "builtin/autoload.hpp"
#include "vm/configuration.hpp"
#include "helpers.hpp"
#include "call_frame.hpp"
#include "exception_point.hpp"
#include "on_stack.hpp"
#include "capi/capi.hpp"
#include "capi/18/include/ruby.h"
using namespace rubinius;
using namespace rubinius::capi;
extern "C" {
int rb_const_defined(VALUE module_handle, ID const_id) {
VALUE ret = rb_funcall(module_handle,
rb_intern("const_defined?"), 1, ID2SYM(const_id));
// This version also checks Object. le sigh.
if(!RTEST(ret)) {
ret = rb_funcall(rb_cObject,
rb_intern("const_defined?"), 1, ID2SYM(const_id));
}
return ret;
}
int rb_const_defined_at(VALUE module_handle, ID const_id) {
if(LANGUAGE_18_ENABLED(NativeMethodEnvironment::get()->state())) {
return rb_funcall(module_handle,
rb_intern("const_defined?"), 1, ID2SYM(const_id));
} else {
return rb_funcall(module_handle,
rb_intern("const_defined?"), 2, ID2SYM(const_id), Qfalse);
}
}
ID rb_frame_last_func() {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
CallFrame* rcf = env->current_call_frame()->previous->top_ruby_frame();
return env->get_handle(rcf->name());
}
ID rb_frame_this_func() {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
CallFrame* rcf = env->current_call_frame()->top_ruby_frame();
return env->get_handle(rcf->name());
}
static VALUE const_missing(VALUE klass, ID id) {
return rb_funcall(klass, rb_intern("const_missing"), 1, ID2SYM(id));
}
VALUE rb_const_get_at(VALUE module_handle, ID id_name) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Symbol* name = reinterpret_cast<Symbol*>(id_name);
Module* module = c_as<Module>(env->get_object(module_handle));
bool found = false;
Object* val = module->get_const(env->state(), name, &found);
if(!found) return const_missing(module_handle, id_name);
if(Autoload* autoload = try_as<Autoload>(val)) {
return capi_fast_call(env->get_handle(autoload), rb_intern("call"), 0);
}
return env->get_handle(val);
}
VALUE rb_const_get_from(VALUE module_handle, ID id_name) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Symbol* name = reinterpret_cast<Symbol*>(id_name);
Module* module = c_as<Module>(env->get_object(module_handle));
bool found = false;
while(!module->nil_p()) {
Object* val = module->get_const(env->state(), name, &found);
if(found) {
if(Autoload* autoload = try_as<Autoload>(val)) {
return capi_fast_call(env->get_handle(autoload), rb_intern("call"), 0);
}
return env->get_handle(val);
}
module = module->superclass();
}
return const_missing(module_handle, id_name);
}
VALUE rb_const_get(VALUE module_handle, ID id_name) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Symbol* name = reinterpret_cast<Symbol*>(id_name);
Module* module = c_as<Module>(env->get_object(module_handle));
bool found = false;
while(!module->nil_p()) {
Object* val = module->get_const(env->state(), name, &found);
if(found) {
if(Autoload* autoload = try_as<Autoload>(val)) {
return capi_fast_call(env->get_handle(autoload), rb_intern("call"), 0);
}
return env->get_handle(val);
}
module = module->superclass();
}
// Try from Object as well.
module = env->state()->globals().object.get();
while(!module->nil_p()) {
Object* val = module->get_const(env->state(), name, &found);
if(found) {
if(Autoload* autoload = try_as<Autoload>(val)) {
return capi_fast_call(env->get_handle(autoload), rb_intern("call"), 0);
}
return env->get_handle(val);
}
module = module->superclass();
}
return const_missing(module_handle, id_name);
}
void rb_const_set(VALUE module_handle, ID name, VALUE obj_handle) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Module* module = c_as<Module>(env->get_object(module_handle));
Object* object = env->get_object(obj_handle);
module->set_const(env->state(), reinterpret_cast<Symbol*>(name), object);
}
void rb_define_alias(VALUE module_handle, const char* new_name, const char* old_name) {
ID id_new = rb_intern(new_name);
ID id_old = rb_intern(old_name);
rb_funcall(module_handle, rb_intern("alias_method"), 2, id_new, id_old);
}
void rb_alias(VALUE module_handle, ID id_new, ID id_old) {
rb_funcall(module_handle, rb_intern("alias_method"), 2, id_new, id_old);
}
void rb_define_alloc_func(VALUE class_handle, CApiAllocFunction allocator) {
rb_define_singleton_method(class_handle, "allocate", allocator, 0);
}
void rb_undef_alloc_func(VALUE class_handle) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
rb_undef_method(
env->get_handle(env->get_object(class_handle)->singleton_class(env->state())),
"allocate");
}
void rb_define_attr(VALUE module_handle, const char* attr_name, int readable, int writable) {
if(readable) {
rb_funcall(module_handle, rb_intern("attr_reader"), 1, rb_intern(attr_name));
}
if(writable) {
rb_funcall(module_handle, rb_intern("attr_writer"), 1, rb_intern(attr_name));
}
}
void rb_define_const(VALUE module_handle, const char* name, VALUE obj_handle) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Module* module = c_as<Module>(env->get_object(module_handle));
Object* object = env->get_object(obj_handle);
module->set_const(env->state(), name, object);
}
void rb_define_global_const(const char* name, VALUE obj) {
rb_define_const(rb_cObject, name, obj);
}
void rb_define_global_function(const char* name, CApiGenericFunction func, int argc) {
rb_define_module_function(rb_mKernel, name, func, argc);
}
VALUE rb_define_module(const char* name) {
return rb_define_module_under(rb_cObject, name);
}
void rb_define_module_function(VALUE module_handle, const char* name,
CApiGenericFunction func, int args) {
rb_define_private_method(module_handle, name, func, args);
rb_define_singleton_method(module_handle, name, func, args);
}
/** @note Shares code with rb_define_class_under, change there too. --rue */
VALUE rb_define_module_under(VALUE parent_handle, const char* name) {
GCTokenImpl gct;
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Module* parent = c_as<Module>(env->get_object(parent_handle));
Symbol* constant = env->state()->symbol(name);
LEAVE_CAPI(env->state());
OnStack<2> os(env->state(), parent, constant);
Module* module = rubinius::Helpers::open_module(env->state(), gct,
env->current_call_frame(), parent, constant);
// The call above could have triggered an Autoload resolve, which may
// raise an exception, so we have to check the value returned.
if(!module) env->current_ep()->return_to(env);
// Grab the module handle before grabbing the lock
// so the Module isn't accidentally GC'ed.
VALUE module_handle = env->get_handle(module);
ENTER_CAPI(env->state());
return module_handle;
}
void rb_include_module(VALUE includer_handle, VALUE includee_handle) {
rb_funcall(includer_handle, rb_intern("include"), 1, includee_handle);
}
void rb_undef_method(VALUE module_handle, const char* name) {
rb_funcall(module_handle, rb_intern("undef_method!"), 1, ID2SYM(rb_intern(name)));
}
void rb_undef(VALUE handle, ID name) {
Symbol* sym = reinterpret_cast<Symbol*>(name);
rb_funcall(handle, rb_intern("undef_method!"), 1, sym);
// In MRI, rb_undef also calls the undef_method hooks, maybe we should?
}
VALUE rb_mod_ancestors(VALUE mod) {
return rb_funcall(mod, rb_intern("ancestors"), 0);
}
VALUE rb_mod_name(VALUE mod) {
return rb_funcall(mod, rb_intern("name"), 0);
}
VALUE rb_module_new(void) {
return rb_funcall(rb_cModule, rb_intern("new"), 0);
}
VALUE rb_mod_remove_const(VALUE mod, VALUE name) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Module* module = c_as<Module>(env->get_object(mod));
module->del_const(env->state(), reinterpret_cast<Symbol*>(name));
return Qnil;
}
const char* rb_class2name(VALUE module_handle) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Module* module_object = c_as<Module>(env->get_object(module_handle));
String* str = module_object->get_name(env->state());
return RSTRING_PTR(env->get_handle(str));
}
}