3
* This file is included by eval.c
6
#define CACHE_SIZE 0x800
7
#define CACHE_MASK 0x7ff
8
#define EXPR1(c,m) ((((c)>>3)^(m))&CACHE_MASK)
10
struct cache_entry { /* method hash table. */
11
ID mid; /* method's id */
12
ID mid0; /* method's original id */
13
VALUE klass; /* receiver's class */
17
static struct cache_entry cache[CACHE_SIZE];
18
static int ruby_running = 0;
23
struct cache_entry *ent, *end;
30
end = ent + CACHE_SIZE;
38
rb_clear_cache_for_undef(VALUE klass, ID id)
40
struct cache_entry *ent, *end;
47
end = ent + CACHE_SIZE;
49
if (ent->method && ent->method->nd_clss == klass && ent->mid == id) {
57
rb_clear_cache_by_id(ID id)
59
struct cache_entry *ent, *end;
66
end = ent + CACHE_SIZE;
76
rb_clear_cache_by_class(VALUE klass)
78
struct cache_entry *ent, *end;
85
end = ent + CACHE_SIZE;
87
if ((ent->klass == klass) ||
88
(ent->method && ent->method->nd_clss == klass)) {
96
rb_add_method(VALUE klass, ID mid, NODE * node, int noex)
103
if (rb_safe_level() >= 4 && (klass == rb_cObject || !OBJ_TAINTED(klass))) {
104
rb_raise(rb_eSecurityError, "Insecure: can't define method");
106
if (!FL_TEST(klass, FL_SINGLETON) &&
107
node && nd_type(node) != NODE_ZSUPER &&
108
(mid == rb_intern("initialize") || mid == rb_intern("initialize_copy"))) {
109
noex = NOEX_PRIVATE | noex;
111
else if (FL_TEST(klass, FL_SINGLETON) && node
112
&& nd_type(node) == NODE_CFUNC && mid == rb_intern("allocate")) {
114
("defining %s.allocate is deprecated; use rb_define_alloc_func()",
115
rb_class2name(rb_iv_get(klass, "__attached__")));
118
if (OBJ_FROZEN(klass)) {
119
rb_error_frozen("class/module");
121
rb_clear_cache_by_id(mid);
124
* NODE_METHOD (NEW_METHOD(body, klass, vis)):
125
* nd_body : method body // (2) // mark
126
* nd_clss : klass // (1) // mark
127
* nd_noex : visibility // (3)
129
* NODE_FBODY (NEW_FBODY(method, alias)):
130
* nd_body : method (NODE_METHOD) // (2) // mark
131
* nd_oid : original id // (1)
132
* nd_cnt : alias count // (3)
135
body = NEW_FBODY(NEW_METHOD(node, klass, NOEX_WITH_SAFE(noex)), 0);
142
/* check re-definition */
145
if (st_lookup(RCLASS(klass)->m_tbl, mid, (st_data_t *)&old_node)) {
147
if (nd_type(old_node->nd_body->nd_body) == NODE_CFUNC) {
148
rb_vm_check_redefinition_opt_method(old_node);
150
if (RTEST(ruby_verbose) && old_node->nd_cnt == 0 && old_node->nd_body) {
151
rb_warning("method redefined; discarding old %s", rb_id2name(mid));
154
if (klass == rb_cObject && node && node->nd_mid == init) {
155
rb_warn("redefining Object#initialize may cause infinite loop");
159
if (mid == object_id || mid == __send || mid == __send_bang) {
160
if (node && nd_type(node) == RUBY_VM_METHOD_NODE) {
161
rb_warn("redefining `%s' may cause serious problem",
167
st_insert(RCLASS(klass)->m_tbl, mid, (st_data_t) body);
169
if (node && mid != ID_ALLOCATOR && ruby_running) {
170
if (FL_TEST(klass, FL_SINGLETON)) {
171
rb_funcall(rb_iv_get(klass, "__attached__"), singleton_added, 1,
175
rb_funcall(klass, added, 1, ID2SYM(mid));
181
rb_define_alloc_func(VALUE klass, VALUE (*func) _((VALUE)))
183
Check_Type(klass, T_CLASS);
184
rb_add_method(CLASS_OF(klass), ID_ALLOCATOR, NEW_CFUNC(func, 0),
189
rb_undef_alloc_func(VALUE klass)
191
Check_Type(klass, T_CLASS);
192
rb_add_method(CLASS_OF(klass), ID_ALLOCATOR, 0, NOEX_UNDEF);
196
search_method(VALUE klass, ID id, VALUE *klassp)
204
while (!st_lookup(RCLASS(klass)->m_tbl, id, (st_data_t *) & body)) {
205
klass = RCLASS(klass)->super;
218
* search method body (NODE_METHOD)
219
* with : klass and id
220
* without : method cache
222
* if you need method node with method cache, use
226
rb_get_method_body(VALUE klass, ID id, ID *idp)
228
NODE *volatile fbody, *body;
231
if ((fbody = search_method(klass, id, 0)) == 0 || !fbody->nd_body) {
232
/* store empty info in cache */
233
struct cache_entry *ent;
234
ent = cache + EXPR1(klass, id);
236
ent->mid = ent->mid0 = id;
241
method = fbody->nd_body;
245
struct cache_entry *ent;
246
ent = cache + EXPR1(klass, id);
249
ent->mid0 = fbody->nd_oid;
250
ent->method = body = method;
257
*idp = fbody->nd_oid;
264
rb_method_node(VALUE klass, ID id)
266
struct cache_entry *ent;
268
ent = cache + EXPR1(klass, id);
269
if (ent->mid == id && ent->klass == klass && ent->method) {
273
return rb_get_method_body(klass, id, 0);
277
remove_method(VALUE klass, ID mid)
281
if (klass == rb_cObject) {
284
if (rb_safe_level() >= 4 && !OBJ_TAINTED(klass)) {
285
rb_raise(rb_eSecurityError, "Insecure: can't remove method");
287
if (OBJ_FROZEN(klass))
288
rb_error_frozen("class/module");
289
if (mid == object_id || mid == __send || mid == __send_bang || mid == init) {
290
rb_warn("removing `%s' may cause serious problem", rb_id2name(mid));
292
if (!st_delete(RCLASS(klass)->m_tbl, &mid, (st_data_t *) & body) ||
294
rb_name_error(mid, "method `%s' not defined in %s",
295
rb_id2name(mid), rb_class2name(klass));
298
if (nd_type(body->nd_body->nd_body) == NODE_CFUNC) {
299
rb_vm_check_redefinition_opt_method(body);
302
rb_clear_cache_for_undef(klass, mid);
303
if (FL_TEST(klass, FL_SINGLETON)) {
304
rb_funcall(rb_iv_get(klass, "__attached__"), singleton_removed, 1,
308
rb_funcall(klass, removed, 1, ID2SYM(mid));
313
rb_remove_method(VALUE klass, const char *name)
315
remove_method(klass, rb_intern(name));
320
* remove_method(symbol) => self
322
* Removes the method identified by _symbol_ from the current
323
* class. For an example, see <code>Module.undef_method</code>.
327
rb_mod_remove_method(int argc, VALUE *argv, VALUE mod)
331
for (i = 0; i < argc; i++) {
332
remove_method(mod, rb_to_id(argv[i]));
337
#undef rb_disable_super
338
#undef rb_enable_super
341
rb_disable_super(VALUE klass, const char *name)
343
/* obsolete - no use */
347
rb_enable_super(VALUE klass, const char *name)
349
rb_warning("rb_enable_super() is obsolete");
353
rb_export_method(VALUE klass, ID name, ID noex)
358
if (klass == rb_cObject) {
361
fbody = search_method(klass, name, &origin);
362
if (!fbody && TYPE(klass) == T_MODULE) {
363
fbody = search_method(rb_cObject, name, &origin);
365
if (!fbody || !fbody->nd_body) {
366
print_undef(klass, name);
368
if (fbody->nd_body->nd_noex != noex) {
369
if (klass == origin) {
370
fbody->nd_body->nd_noex = noex;
373
rb_add_method(klass, name, NEW_ZSUPER(), noex);
379
rb_method_boundp(VALUE klass, ID id, int ex)
383
if ((method = rb_method_node(klass, id)) != 0) {
384
if (ex && (method->nd_noex & NOEX_PRIVATE)) {
393
rb_attr(VALUE klass, ID id, int read, int write, int ex)
405
if (SCOPE_TEST(NOEX_PRIVATE)) {
407
rb_warning((SCOPE_CHECK(NOEX_MODFUNC)) ?
408
"attribute accessor as module_function" :
409
"private attribute?");
411
else if (SCOPE_TEST(NOEX_PROTECTED)) {
412
noex = NOEX_PROTECTED;
419
if (!rb_is_local_id(id) && !rb_is_const_id(id)) {
420
rb_name_error(id, "invalid attribute name `%s'", rb_id2name(id));
422
name = rb_id2name(id);
424
rb_raise(rb_eArgError, "argument needs to be symbol or string");
426
len = strlen(name) + 2;
427
buf = ALLOCA_N(char, len);
428
snprintf(buf, len, "@%s", name);
429
attriv = rb_intern(buf);
431
rb_add_method(klass, id, NEW_IVAR(attriv), noex);
434
rb_add_method(klass, rb_id_attrset(id), NEW_ATTRSET(attriv), noex);
439
rb_undef(VALUE klass, ID id)
444
if (ruby_cbase() == rb_cObject && klass == rb_cObject) {
447
if (rb_safe_level() >= 4 && !OBJ_TAINTED(klass)) {
448
rb_raise(rb_eSecurityError, "Insecure: can't undef `%s'",
451
rb_frozen_class_p(klass);
452
if (id == object_id || id == __send || id == __send_bang || id == init) {
453
rb_warn("undefining `%s' may cause serious problem", rb_id2name(id));
455
body = search_method(klass, id, &origin);
456
if (!body || !body->nd_body) {
460
if (FL_TEST(c, FL_SINGLETON)) {
461
VALUE obj = rb_iv_get(klass, "__attached__");
470
else if (TYPE(c) == T_MODULE) {
473
rb_name_error(id, "undefined method `%s' for%s `%s'",
474
rb_id2name(id), s0, rb_class2name(c));
477
rb_add_method(klass, id, 0, NOEX_PUBLIC);
479
if (FL_TEST(klass, FL_SINGLETON)) {
480
rb_funcall(rb_iv_get(klass, "__attached__"),
481
singleton_undefined, 1, ID2SYM(id));
484
rb_funcall(klass, undefined, 1, ID2SYM(id));
490
* undef_method(symbol) => self
492
* Prevents the current class from responding to calls to the named
493
* method. Contrast this with <code>remove_method</code>, which deletes
494
* the method from the particular class; Ruby will still search
495
* superclasses and mixed-in modules for a possible receiver.
502
* class Child < Parent
514
* remove_method :hello # remove from child, still in parent
520
* undef_method :hello # prevent any calls to 'hello'
528
* prog.rb:23: undefined method `hello' for #<Child:0x401b3bb4> (NoMethodError)
532
rb_mod_undef_method(int argc, VALUE *argv, VALUE mod)
535
for (i = 0; i < argc; i++) {
536
rb_undef(mod, rb_to_id(argv[i]));
542
rb_alias(VALUE klass, ID name, ID def)
544
NODE *orig_fbody, *node;
547
rb_frozen_class_p(klass);
550
if (klass == rb_cObject) {
553
orig_fbody = search_method(klass, def, 0);
554
if (!orig_fbody || !orig_fbody->nd_body) {
555
if (TYPE(klass) == T_MODULE) {
556
orig_fbody = search_method(rb_cObject, def, 0);
559
if (!orig_fbody || !orig_fbody->nd_body) {
560
print_undef(klass, def);
562
if (FL_TEST(klass, FL_SINGLETON)) {
563
singleton = rb_iv_get(klass, "__attached__");
566
orig_fbody->nd_cnt++;
568
if (st_lookup(RCLASS(klass)->m_tbl, name, (st_data_t *) & node)) {
570
if (RTEST(ruby_verbose) && node->nd_cnt == 0 && node->nd_body) {
571
rb_warning("discarding old %s", rb_id2name(name));
573
if (nd_type(node->nd_body->nd_body) == NODE_CFUNC) {
574
rb_vm_check_redefinition_opt_method(node);
579
st_insert(RCLASS(klass)->m_tbl, name,
580
(st_data_t) NEW_FBODY(
581
NEW_METHOD(orig_fbody->nd_body->nd_body,
582
orig_fbody->nd_body->nd_clss,
583
NOEX_WITH_SAFE(orig_fbody->nd_body->nd_noex)), def));
585
rb_clear_cache_by_id(name);
588
rb_funcall(singleton, singleton_added, 1, ID2SYM(name));
591
rb_funcall(klass, added, 1, ID2SYM(name));
597
* alias_method(new_name, old_name) => self
599
* Makes <i>new_name</i> a new copy of the method <i>old_name</i>. This can
600
* be used to retain access to methods that are overridden.
603
* alias_method :orig_exit, :exit
605
* puts "Exiting with code #{code}"
614
* Exiting with code 99
618
rb_mod_alias_method(VALUE mod, VALUE newname, VALUE oldname)
620
rb_alias(mod, rb_to_id(newname), rb_to_id(oldname));