2
* This file is included by eval.c
5
#define CACHE_SIZE 0x800
6
#define CACHE_MASK 0x7ff
7
#define EXPR1(c,m) ((((c)>>3)^(m))&CACHE_MASK)
9
struct cache_entry { /* method hash table. */
10
ID mid; /* method's id */
11
ID mid0; /* method's original id */
12
VALUE klass; /* receiver's class */
16
static struct cache_entry cache[CACHE_SIZE];
17
static int ruby_running = 0;
22
struct cache_entry *ent, *end;
29
end = ent + CACHE_SIZE;
37
rb_clear_cache_for_undef(VALUE klass, ID id)
39
struct cache_entry *ent, *end;
46
end = ent + CACHE_SIZE;
48
if (ent->method && ent->method->nd_clss == klass && ent->mid == id) {
56
rb_clear_cache_by_id(ID id)
58
struct cache_entry *ent, *end;
65
end = ent + CACHE_SIZE;
75
rb_clear_cache_by_class(VALUE klass)
77
struct cache_entry *ent, *end;
84
end = ent + CACHE_SIZE;
86
if ((ent->klass == klass) ||
87
(ent->method && ent->method->nd_clss == klass)) {
95
rb_add_method(VALUE klass, ID mid, NODE * node, int noex)
102
if (rb_safe_level() >= 4 && (klass == rb_cObject || !OBJ_TAINTED(klass))) {
103
rb_raise(rb_eSecurityError, "Insecure: can't define method");
105
if (!FL_TEST(klass, FL_SINGLETON) &&
106
node && nd_type(node) != NODE_ZSUPER &&
107
(mid == rb_intern("initialize") || mid == rb_intern("initialize_copy"))) {
108
noex = NOEX_PRIVATE | noex;
110
else if (FL_TEST(klass, FL_SINGLETON) && node
111
&& nd_type(node) == NODE_CFUNC && mid == rb_intern("allocate")) {
113
("defining %s.allocate is deprecated; use rb_define_alloc_func()",
114
rb_class2name(rb_iv_get(klass, "__attached__")));
117
if (OBJ_FROZEN(klass)) {
118
rb_error_frozen("class/module");
120
rb_clear_cache_by_id(mid);
123
* NODE_METHOD (NEW_METHOD(body, klass, vis)):
124
* nd_body : method body // (2) // mark
125
* nd_clss : klass // (1) // mark
126
* nd_noex : visibility // (3)
128
* NODE_FBODY (NEW_FBODY(method, alias)):
129
* nd_body : method (NODE_METHOD) // (2) // mark
130
* nd_oid : original id // (1)
131
* nd_cnt : alias count // (3)
134
body = NEW_FBODY(NEW_METHOD(node, klass, noex), 0);
141
/* check re-definition */
144
if (st_lookup(RCLASS(klass)->m_tbl, mid, (st_data_t *)&old_node)) {
146
if (nd_type(old_node->nd_body->nd_body) == NODE_CFUNC) {
147
rb_vm_check_redefinition_opt_method(old_node);
149
if (RTEST(ruby_verbose) && old_node->nd_cnt == 0 && old_node->nd_body) {
150
rb_warning("method redefined; discarding old %s", rb_id2name(mid));
153
if (klass == rb_cObject && node->nd_mid == init) {
154
rb_warn("redefining Object#initialize may cause infinite loop");
158
if (mid == object_id || mid == __send || mid == __send_bang) {
159
if (node && nd_type(node) == RUBY_VM_METHOD_NODE) {
160
rb_warn("redefining `%s' may cause serious problem",
166
st_insert(RCLASS(klass)->m_tbl, mid, (st_data_t) body);
168
if (node && mid != ID_ALLOCATOR && ruby_running) {
169
if (FL_TEST(klass, FL_SINGLETON)) {
170
rb_funcall(rb_iv_get(klass, "__attached__"), singleton_added, 1,
174
rb_funcall(klass, added, 1, ID2SYM(mid));
180
rb_define_alloc_func(VALUE klass, VALUE (*func) _((VALUE)))
182
Check_Type(klass, T_CLASS);
183
rb_add_method(CLASS_OF(klass), ID_ALLOCATOR, NEW_CFUNC(func, 0),
188
rb_undef_alloc_func(VALUE klass)
190
Check_Type(klass, T_CLASS);
191
rb_add_method(CLASS_OF(klass), ID_ALLOCATOR, 0, NOEX_UNDEF);
195
search_method(VALUE klass, ID id, VALUE *klassp)
203
while (!st_lookup(RCLASS(klass)->m_tbl, id, (st_data_t *) & body)) {
204
klass = RCLASS(klass)->super;
217
* search method body (NODE_METHOD)
218
* with : klass and id
219
* without : method cache
221
* if you need method node with method cache, use
225
rb_get_method_body(VALUE klass, ID id, ID *idp)
227
NODE *volatile fbody, *body;
230
if ((fbody = search_method(klass, id, 0)) == 0 || !fbody->nd_body) {
231
/* store empty info in cache */
232
struct cache_entry *ent;
233
ent = cache + EXPR1(klass, id);
235
ent->mid = ent->mid0 = id;
240
method = fbody->nd_body;
244
struct cache_entry *ent;
245
ent = cache + EXPR1(klass, id);
248
ent->mid0 = fbody->nd_oid;
249
ent->method = body = method;
256
*idp = fbody->nd_oid;
263
rb_method_node(VALUE klass, ID id)
265
struct cache_entry *ent;
267
ent = cache + EXPR1(klass, id);
268
if (ent->mid == id && ent->klass == klass && ent->method) {
272
return rb_get_method_body(klass, id, 0);
276
remove_method(VALUE klass, ID mid)
280
if (klass == rb_cObject) {
283
if (rb_safe_level() >= 4 && !OBJ_TAINTED(klass)) {
284
rb_raise(rb_eSecurityError, "Insecure: can't remove method");
286
if (OBJ_FROZEN(klass))
287
rb_error_frozen("class/module");
288
if (mid == object_id || mid == __send || mid == __send_bang || mid == init) {
289
rb_warn("removing `%s' may cause serious problem", rb_id2name(mid));
291
if (!st_delete(RCLASS(klass)->m_tbl, &mid, (st_data_t *) & body) ||
293
rb_name_error(mid, "method `%s' not defined in %s",
294
rb_id2name(mid), rb_class2name(klass));
297
if (nd_type(body->nd_body->nd_body) == NODE_CFUNC) {
298
rb_vm_check_redefinition_opt_method(body);
301
rb_clear_cache_for_undef(klass, mid);
302
if (FL_TEST(klass, FL_SINGLETON)) {
303
rb_funcall(rb_iv_get(klass, "__attached__"), singleton_removed, 1,
307
rb_funcall(klass, removed, 1, ID2SYM(mid));
312
rb_remove_method(VALUE klass, const char *name)
314
remove_method(klass, rb_intern(name));
319
* remove_method(symbol) => self
321
* Removes the method identified by _symbol_ from the current
322
* class. For an example, see <code>Module.undef_method</code>.
326
rb_mod_remove_method(int argc, VALUE *argv, VALUE mod)
330
for (i = 0; i < argc; i++) {
331
remove_method(mod, rb_to_id(argv[i]));
336
#undef rb_disable_super
337
#undef rb_enable_super
340
rb_disable_super(VALUE klass, const char *name)
342
/* obsolete - no use */
346
rb_enable_super(VALUE klass, const char *name)
348
rb_warning("rb_enable_super() is obsolete");
352
rb_export_method(VALUE klass, ID name, ID noex)
357
if (klass == rb_cObject) {
360
fbody = search_method(klass, name, &origin);
361
if (!fbody && TYPE(klass) == T_MODULE) {
362
fbody = search_method(rb_cObject, name, &origin);
364
if (!fbody || !fbody->nd_body) {
365
print_undef(klass, name);
367
if (fbody->nd_body->nd_noex != noex) {
368
if (klass == origin) {
369
fbody->nd_body->nd_noex = noex;
372
rb_add_method(klass, name, NEW_ZSUPER(), noex);
378
rb_method_boundp(VALUE klass, ID id, int ex)
382
if ((method = rb_method_node(klass, id)) != 0) {
383
if (ex && (method->nd_noex & NOEX_PRIVATE)) {
392
rb_attr(VALUE klass, ID id, int read, int write, int ex)
404
if (SCOPE_TEST(NOEX_PRIVATE)) {
406
rb_warning((SCOPE_CHECK(NOEX_MODFUNC)) ?
407
"attribute accessor as module_function" :
408
"private attribute?");
410
else if (SCOPE_TEST(NOEX_PROTECTED)) {
411
noex = NOEX_PROTECTED;
418
if (!rb_is_local_id(id) && !rb_is_const_id(id)) {
419
rb_name_error(id, "invalid attribute name `%s'", rb_id2name(id));
421
name = rb_id2name(id);
423
rb_raise(rb_eArgError, "argument needs to be symbol or string");
425
len = strlen(name) + 2;
426
buf = ALLOCA_N(char, len);
427
snprintf(buf, len, "@%s", name);
428
attriv = rb_intern(buf);
430
rb_add_method(klass, id, NEW_IVAR(attriv), noex);
433
rb_add_method(klass, rb_id_attrset(id), NEW_ATTRSET(attriv), noex);
438
rb_undef(VALUE klass, ID id)
443
if (ruby_cbase() == rb_cObject && klass == rb_cObject) {
446
if (rb_safe_level() >= 4 && !OBJ_TAINTED(klass)) {
447
rb_raise(rb_eSecurityError, "Insecure: can't undef `%s'",
450
rb_frozen_class_p(klass);
451
if (id == object_id || id == __send || id == __send_bang || id == init) {
452
rb_warn("undefining `%s' may cause serious problem", rb_id2name(id));
454
body = search_method(klass, id, &origin);
455
if (!body || !body->nd_body) {
459
if (FL_TEST(c, FL_SINGLETON)) {
460
VALUE obj = rb_iv_get(klass, "__attached__");
469
else if (TYPE(c) == T_MODULE) {
472
rb_name_error(id, "undefined method `%s' for%s `%s'",
473
rb_id2name(id), s0, rb_class2name(c));
476
rb_add_method(klass, id, 0, NOEX_PUBLIC);
478
if (FL_TEST(klass, FL_SINGLETON)) {
479
rb_funcall(rb_iv_get(klass, "__attached__"),
480
singleton_undefined, 1, ID2SYM(id));
483
rb_funcall(klass, undefined, 1, ID2SYM(id));
489
* undef_method(symbol) => self
491
* Prevents the current class from responding to calls to the named
492
* method. Contrast this with <code>remove_method</code>, which deletes
493
* the method from the particular class; Ruby will still search
494
* superclasses and mixed-in modules for a possible receiver.
501
* class Child < Parent
513
* remove_method :hello # remove from child, still in parent
519
* undef_method :hello # prevent any calls to 'hello'
527
* prog.rb:23: undefined method `hello' for #<Child:0x401b3bb4> (NoMethodError)
531
rb_mod_undef_method(int argc, VALUE *argv, VALUE mod)
534
for (i = 0; i < argc; i++) {
535
rb_undef(mod, rb_to_id(argv[i]));
541
rb_alias(VALUE klass, ID name, ID def)
543
NODE *orig_fbody, *node;
546
rb_frozen_class_p(klass);
549
if (klass == rb_cObject) {
552
orig_fbody = search_method(klass, def, 0);
553
if (!orig_fbody || !orig_fbody->nd_body) {
554
if (TYPE(klass) == T_MODULE) {
555
orig_fbody = search_method(rb_cObject, def, 0);
558
if (!orig_fbody || !orig_fbody->nd_body) {
559
print_undef(klass, def);
561
if (FL_TEST(klass, FL_SINGLETON)) {
562
singleton = rb_iv_get(klass, "__attached__");
565
orig_fbody->nd_cnt++;
567
if (st_lookup(RCLASS(klass)->m_tbl, name, (st_data_t *) & node)) {
569
if (RTEST(ruby_verbose) && node->nd_cnt == 0 && node->nd_body) {
570
rb_warning("discarding old %s", rb_id2name(name));
572
if (nd_type(node->nd_body->nd_body) == NODE_CFUNC) {
573
rb_vm_check_redefinition_opt_method(node);
578
st_insert(RCLASS(klass)->m_tbl, name,
579
(st_data_t) NEW_FBODY(
580
NEW_METHOD(orig_fbody->nd_body->nd_body,
581
orig_fbody->nd_body->nd_clss,
582
orig_fbody->nd_body->nd_noex), def));
584
rb_clear_cache_by_id(name);
587
rb_funcall(singleton, singleton_added, 1, ID2SYM(name));
590
rb_funcall(klass, added, 1, ID2SYM(name));
596
* alias_method(new_name, old_name) => self
598
* Makes <i>new_name</i> a new copy of the method <i>old_name</i>. This can
599
* be used to retain access to methods that are overridden.
602
* alias_method :orig_exit, :exit
604
* puts "Exiting with code #{code}"
613
* Exiting with code 99
617
rb_mod_alias_method(VALUE mod, VALUE newname, VALUE oldname)
619
rb_alias(mod, rb_to_id(newname), rb_to_id(oldname));