~ubuntu-branches/ubuntu/intrepid/ruby1.9/intrepid-updates

« back to all changes in this revision

Viewing changes to eval_method.ci

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-09-04 16:01:17 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20070904160117-i15zckg2nhxe9fyw
Tags: 1.9.0+20070830-2ubuntu1
* Sync from Debian; remaining changes:
  - Add -g to CFLAGS.
* Fixes build failure on ia64.
* Fixes build failure with gcc-4.2 on lpia.
* Robustify check for target_os, fixing build failure on lpia.
* Set Ubuntu maintainer address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*-c-*- */
 
2
/*
 
3
 * This file is included by eval.c
 
4
 */
 
5
 
 
6
#define CACHE_SIZE 0x800
 
7
#define CACHE_MASK 0x7ff
 
8
#define EXPR1(c,m) ((((c)>>3)^(m))&CACHE_MASK)
 
9
 
 
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 */
 
14
    NODE *method;
 
15
};
 
16
 
 
17
static struct cache_entry cache[CACHE_SIZE];
 
18
static int ruby_running = 0;
 
19
 
 
20
void
 
21
rb_clear_cache(void)
 
22
{
 
23
    struct cache_entry *ent, *end;
 
24
 
 
25
    rb_vm_change_state();
 
26
 
 
27
    if (!ruby_running)
 
28
        return;
 
29
    ent = cache;
 
30
    end = ent + CACHE_SIZE;
 
31
    while (ent < end) {
 
32
        ent->mid = 0;
 
33
        ent++;
 
34
    }
 
35
}
 
36
 
 
37
static void
 
38
rb_clear_cache_for_undef(VALUE klass, ID id)
 
39
{
 
40
    struct cache_entry *ent, *end;
 
41
 
 
42
    rb_vm_change_state();
 
43
 
 
44
    if (!ruby_running)
 
45
        return;
 
46
    ent = cache;
 
47
    end = ent + CACHE_SIZE;
 
48
    while (ent < end) {
 
49
        if (ent->method && ent->method->nd_clss == klass && ent->mid == id) {
 
50
            ent->mid = 0;
 
51
        }
 
52
        ent++;
 
53
    }
 
54
}
 
55
 
 
56
static void
 
57
rb_clear_cache_by_id(ID id)
 
58
{
 
59
    struct cache_entry *ent, *end;
 
60
 
 
61
    rb_vm_change_state();
 
62
 
 
63
    if (!ruby_running)
 
64
        return;
 
65
    ent = cache;
 
66
    end = ent + CACHE_SIZE;
 
67
    while (ent < end) {
 
68
        if (ent->mid == id) {
 
69
            ent->mid = 0;
 
70
        }
 
71
        ent++;
 
72
    }
 
73
}
 
74
 
 
75
void
 
76
rb_clear_cache_by_class(VALUE klass)
 
77
{
 
78
    struct cache_entry *ent, *end;
 
79
 
 
80
    rb_vm_change_state();
 
81
 
 
82
    if (!ruby_running)
 
83
        return;
 
84
    ent = cache;
 
85
    end = ent + CACHE_SIZE;
 
86
    while (ent < end) {
 
87
        if ((ent->klass == klass) ||
 
88
            (ent->method && ent->method->nd_clss == klass)) {
 
89
            ent->mid = 0;
 
90
        }
 
91
        ent++;
 
92
    }
 
93
}
 
94
 
 
95
void
 
96
rb_add_method(VALUE klass, ID mid, NODE * node, int noex)
 
97
{
 
98
    NODE *body;
 
99
 
 
100
    if (NIL_P(klass)) {
 
101
        klass = rb_cObject;
 
102
    }
 
103
    if (rb_safe_level() >= 4 && (klass == rb_cObject || !OBJ_TAINTED(klass))) {
 
104
        rb_raise(rb_eSecurityError, "Insecure: can't define method");
 
105
    }
 
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;
 
110
    }
 
111
    else if (FL_TEST(klass, FL_SINGLETON) && node
 
112
             && nd_type(node) == NODE_CFUNC && mid == rb_intern("allocate")) {
 
113
        rb_warn
 
114
            ("defining %s.allocate is deprecated; use rb_define_alloc_func()",
 
115
             rb_class2name(rb_iv_get(klass, "__attached__")));
 
116
        mid = ID_ALLOCATOR;
 
117
    }
 
118
    if (OBJ_FROZEN(klass)) {
 
119
        rb_error_frozen("class/module");
 
120
    }
 
121
    rb_clear_cache_by_id(mid);
 
122
 
 
123
    /*
 
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)
 
128
     *
 
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)
 
133
     */
 
134
    if (node) {
 
135
        body = NEW_FBODY(NEW_METHOD(node, klass, NOEX_WITH_SAFE(noex)), 0);
 
136
    }
 
137
    else {
 
138
        body = 0;
 
139
    }
 
140
 
 
141
    {
 
142
        /* check re-definition */
 
143
        NODE *old_node;
 
144
 
 
145
        if (st_lookup(RCLASS(klass)->m_tbl, mid, (st_data_t *)&old_node)) {
 
146
            if (old_node) {
 
147
                if (nd_type(old_node->nd_body->nd_body) == NODE_CFUNC) {
 
148
                    rb_vm_check_redefinition_opt_method(old_node);
 
149
                }
 
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));
 
152
                }
 
153
            }
 
154
            if (klass == rb_cObject && node && node->nd_mid == init) {
 
155
                rb_warn("redefining Object#initialize may cause infinite loop");
 
156
            }
 
157
        }
 
158
 
 
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",
 
162
                        rb_id2name(mid));
 
163
            }
 
164
        }
 
165
    }
 
166
 
 
167
    st_insert(RCLASS(klass)->m_tbl, mid, (st_data_t) body);
 
168
 
 
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,
 
172
                       ID2SYM(mid));
 
173
        }
 
174
        else {
 
175
            rb_funcall(klass, added, 1, ID2SYM(mid));
 
176
        }
 
177
    }
 
178
}
 
179
 
 
180
void
 
181
rb_define_alloc_func(VALUE klass, VALUE (*func) _((VALUE)))
 
182
{
 
183
    Check_Type(klass, T_CLASS);
 
184
    rb_add_method(CLASS_OF(klass), ID_ALLOCATOR, NEW_CFUNC(func, 0),
 
185
                  NOEX_PRIVATE);
 
186
}
 
187
 
 
188
void
 
189
rb_undef_alloc_func(VALUE klass)
 
190
{
 
191
    Check_Type(klass, T_CLASS);
 
192
    rb_add_method(CLASS_OF(klass), ID_ALLOCATOR, 0, NOEX_UNDEF);
 
193
}
 
194
 
 
195
static NODE *
 
196
search_method(VALUE klass, ID id, VALUE *klassp)
 
197
{
 
198
    NODE *body;
 
199
 
 
200
    if (!klass) {
 
201
        return 0;
 
202
    }
 
203
 
 
204
    while (!st_lookup(RCLASS(klass)->m_tbl, id, (st_data_t *) & body)) {
 
205
        klass = RCLASS(klass)->super;
 
206
        if (!klass)
 
207
            return 0;
 
208
    }
 
209
 
 
210
    if (klassp) {
 
211
        *klassp = klass;
 
212
    }
 
213
 
 
214
    return body;
 
215
}
 
216
 
 
217
/*
 
218
 * search method body (NODE_METHOD)
 
219
 *   with    : klass and id
 
220
 *   without : method cache
 
221
 *
 
222
 * if you need method node with method cache, use
 
223
 * rb_method_node()
 
224
 */
 
225
NODE *
 
226
rb_get_method_body(VALUE klass, ID id, ID *idp)
 
227
{
 
228
    NODE *volatile fbody, *body;
 
229
    NODE *method;
 
230
 
 
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);
 
235
        ent->klass = klass;
 
236
        ent->mid = ent->mid0 = id;
 
237
        ent->method = 0;
 
238
        return 0;
 
239
    }
 
240
 
 
241
    method = fbody->nd_body;
 
242
 
 
243
    if (ruby_running) {
 
244
        /* store in cache */
 
245
        struct cache_entry *ent;
 
246
        ent = cache + EXPR1(klass, id);
 
247
        ent->klass = klass;
 
248
        ent->mid = id;
 
249
        ent->mid0 = fbody->nd_oid;
 
250
        ent->method = body = method;
 
251
    }
 
252
    else {
 
253
        body = method;
 
254
    }
 
255
 
 
256
    if (idp) {
 
257
        *idp = fbody->nd_oid;
 
258
    }
 
259
 
 
260
    return body;
 
261
}
 
262
 
 
263
NODE *
 
264
rb_method_node(VALUE klass, ID id)
 
265
{
 
266
    struct cache_entry *ent;
 
267
 
 
268
    ent = cache + EXPR1(klass, id);
 
269
    if (ent->mid == id && ent->klass == klass && ent->method) {
 
270
        return ent->method;
 
271
    }
 
272
 
 
273
    return rb_get_method_body(klass, id, 0);
 
274
}
 
275
 
 
276
static void
 
277
remove_method(VALUE klass, ID mid)
 
278
{
 
279
    NODE *body;
 
280
 
 
281
    if (klass == rb_cObject) {
 
282
        rb_secure(4);
 
283
    }
 
284
    if (rb_safe_level() >= 4 && !OBJ_TAINTED(klass)) {
 
285
        rb_raise(rb_eSecurityError, "Insecure: can't remove method");
 
286
    }
 
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));
 
291
    }
 
292
    if (!st_delete(RCLASS(klass)->m_tbl, &mid, (st_data_t *) & body) ||
 
293
        !body->nd_body) {
 
294
        rb_name_error(mid, "method `%s' not defined in %s",
 
295
                      rb_id2name(mid), rb_class2name(klass));
 
296
    }
 
297
 
 
298
    if (nd_type(body->nd_body->nd_body) == NODE_CFUNC) {
 
299
        rb_vm_check_redefinition_opt_method(body);
 
300
    }
 
301
 
 
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,
 
305
                   ID2SYM(mid));
 
306
    }
 
307
    else {
 
308
        rb_funcall(klass, removed, 1, ID2SYM(mid));
 
309
    }
 
310
}
 
311
 
 
312
void
 
313
rb_remove_method(VALUE klass, const char *name)
 
314
{
 
315
    remove_method(klass, rb_intern(name));
 
316
}
 
317
 
 
318
/*
 
319
 *  call-seq:
 
320
 *     remove_method(symbol)   => self
 
321
 *
 
322
 *  Removes the method identified by _symbol_ from the current
 
323
 *  class. For an example, see <code>Module.undef_method</code>.
 
324
 */
 
325
 
 
326
static VALUE
 
327
rb_mod_remove_method(int argc, VALUE *argv, VALUE mod)
 
328
{
 
329
    int i;
 
330
 
 
331
    for (i = 0; i < argc; i++) {
 
332
        remove_method(mod, rb_to_id(argv[i]));
 
333
    }
 
334
    return mod;
 
335
}
 
336
 
 
337
#undef rb_disable_super
 
338
#undef rb_enable_super
 
339
 
 
340
void
 
341
rb_disable_super(VALUE klass, const char *name)
 
342
{
 
343
    /* obsolete - no use */
 
344
}
 
345
 
 
346
void
 
347
rb_enable_super(VALUE klass, const char *name)
 
348
{
 
349
    rb_warning("rb_enable_super() is obsolete");
 
350
}
 
351
 
 
352
static void
 
353
rb_export_method(VALUE klass, ID name, ID noex)
 
354
{
 
355
    NODE *fbody;
 
356
    VALUE origin;
 
357
 
 
358
    if (klass == rb_cObject) {
 
359
        rb_secure(4);
 
360
    }
 
361
    fbody = search_method(klass, name, &origin);
 
362
    if (!fbody && TYPE(klass) == T_MODULE) {
 
363
        fbody = search_method(rb_cObject, name, &origin);
 
364
    }
 
365
    if (!fbody || !fbody->nd_body) {
 
366
        print_undef(klass, name);
 
367
    }
 
368
    if (fbody->nd_body->nd_noex != noex) {
 
369
        if (klass == origin) {
 
370
            fbody->nd_body->nd_noex = noex;
 
371
        }
 
372
        else {
 
373
            rb_add_method(klass, name, NEW_ZSUPER(), noex);
 
374
        }
 
375
    }
 
376
}
 
377
 
 
378
int
 
379
rb_method_boundp(VALUE klass, ID id, int ex)
 
380
{
 
381
    NODE *method;
 
382
 
 
383
    if ((method = rb_method_node(klass, id)) != 0) {
 
384
        if (ex && (method->nd_noex & NOEX_PRIVATE)) {
 
385
            return Qfalse;
 
386
        }
 
387
        return Qtrue;
 
388
    }
 
389
    return Qfalse;
 
390
}
 
391
 
 
392
void
 
393
rb_attr(VALUE klass, ID id, int read, int write, int ex)
 
394
{
 
395
    const char *name;
 
396
    char *buf;
 
397
    ID attriv;
 
398
    int noex;
 
399
    size_t len;
 
400
 
 
401
    if (!ex) {
 
402
        noex = NOEX_PUBLIC;
 
403
    }
 
404
    else {
 
405
        if (SCOPE_TEST(NOEX_PRIVATE)) {
 
406
            noex = NOEX_PRIVATE;
 
407
            rb_warning((SCOPE_CHECK(NOEX_MODFUNC)) ?
 
408
                       "attribute accessor as module_function" :
 
409
                       "private attribute?");
 
410
        }
 
411
        else if (SCOPE_TEST(NOEX_PROTECTED)) {
 
412
            noex = NOEX_PROTECTED;
 
413
        }
 
414
        else {
 
415
            noex = NOEX_PUBLIC;
 
416
        }
 
417
    }
 
418
 
 
419
    if (!rb_is_local_id(id) && !rb_is_const_id(id)) {
 
420
        rb_name_error(id, "invalid attribute name `%s'", rb_id2name(id));
 
421
    }
 
422
    name = rb_id2name(id);
 
423
    if (!name) {
 
424
        rb_raise(rb_eArgError, "argument needs to be symbol or string");
 
425
    }
 
426
    len = strlen(name) + 2;
 
427
    buf = ALLOCA_N(char, len);
 
428
    snprintf(buf, len, "@%s", name);
 
429
    attriv = rb_intern(buf);
 
430
    if (read) {
 
431
        rb_add_method(klass, id, NEW_IVAR(attriv), noex);
 
432
    }
 
433
    if (write) {
 
434
        rb_add_method(klass, rb_id_attrset(id), NEW_ATTRSET(attriv), noex);
 
435
    }
 
436
}
 
437
 
 
438
void
 
439
rb_undef(VALUE klass, ID id)
 
440
{
 
441
    VALUE origin;
 
442
    NODE *body;
 
443
 
 
444
    if (ruby_cbase() == rb_cObject && klass == rb_cObject) {
 
445
        rb_secure(4);
 
446
    }
 
447
    if (rb_safe_level() >= 4 && !OBJ_TAINTED(klass)) {
 
448
        rb_raise(rb_eSecurityError, "Insecure: can't undef `%s'",
 
449
                 rb_id2name(id));
 
450
    }
 
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));
 
454
    }
 
455
    body = search_method(klass, id, &origin);
 
456
    if (!body || !body->nd_body) {
 
457
        char *s0 = " class";
 
458
        VALUE c = klass;
 
459
 
 
460
        if (FL_TEST(c, FL_SINGLETON)) {
 
461
            VALUE obj = rb_iv_get(klass, "__attached__");
 
462
 
 
463
            switch (TYPE(obj)) {
 
464
              case T_MODULE:
 
465
              case T_CLASS:
 
466
                c = obj;
 
467
                s0 = "";
 
468
            }
 
469
        }
 
470
        else if (TYPE(c) == T_MODULE) {
 
471
            s0 = " module";
 
472
        }
 
473
        rb_name_error(id, "undefined method `%s' for%s `%s'",
 
474
                      rb_id2name(id), s0, rb_class2name(c));
 
475
    }
 
476
 
 
477
    rb_add_method(klass, id, 0, NOEX_PUBLIC);
 
478
 
 
479
    if (FL_TEST(klass, FL_SINGLETON)) {
 
480
        rb_funcall(rb_iv_get(klass, "__attached__"),
 
481
                   singleton_undefined, 1, ID2SYM(id));
 
482
    }
 
483
    else {
 
484
        rb_funcall(klass, undefined, 1, ID2SYM(id));
 
485
    }
 
486
}
 
487
 
 
488
/*
 
489
 *  call-seq:
 
490
 *     undef_method(symbol)    => self
 
491
 *
 
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.
 
496
 *
 
497
 *     class Parent
 
498
 *       def hello
 
499
 *         puts "In parent"
 
500
 *       end
 
501
 *     end
 
502
 *     class Child < Parent
 
503
 *       def hello
 
504
 *         puts "In child"
 
505
 *       end
 
506
 *     end
 
507
 *
 
508
 *
 
509
 *     c = Child.new
 
510
 *     c.hello
 
511
 *
 
512
 *
 
513
 *     class Child
 
514
 *       remove_method :hello  # remove from child, still in parent
 
515
 *     end
 
516
 *     c.hello
 
517
 *
 
518
 *
 
519
 *     class Child
 
520
 *       undef_method :hello   # prevent any calls to 'hello'
 
521
 *     end
 
522
 *     c.hello
 
523
 *
 
524
 *  <em>produces:</em>
 
525
 *
 
526
 *     In child
 
527
 *     In parent
 
528
 *     prog.rb:23: undefined method `hello' for #<Child:0x401b3bb4> (NoMethodError)
 
529
 */
 
530
 
 
531
static VALUE
 
532
rb_mod_undef_method(int argc, VALUE *argv, VALUE mod)
 
533
{
 
534
    int i;
 
535
    for (i = 0; i < argc; i++) {
 
536
        rb_undef(mod, rb_to_id(argv[i]));
 
537
    }
 
538
    return mod;
 
539
}
 
540
 
 
541
void
 
542
rb_alias(VALUE klass, ID name, ID def)
 
543
{
 
544
    NODE *orig_fbody, *node;
 
545
    VALUE singleton = 0;
 
546
 
 
547
    rb_frozen_class_p(klass);
 
548
    if (name == def)
 
549
        return;
 
550
    if (klass == rb_cObject) {
 
551
        rb_secure(4);
 
552
    }
 
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);
 
557
        }
 
558
    }
 
559
    if (!orig_fbody || !orig_fbody->nd_body) {
 
560
        print_undef(klass, def);
 
561
    }
 
562
    if (FL_TEST(klass, FL_SINGLETON)) {
 
563
        singleton = rb_iv_get(klass, "__attached__");
 
564
    }
 
565
 
 
566
    orig_fbody->nd_cnt++;
 
567
 
 
568
    if (st_lookup(RCLASS(klass)->m_tbl, name, (st_data_t *) & node)) {
 
569
        if (node) {
 
570
            if (RTEST(ruby_verbose) && node->nd_cnt == 0 && node->nd_body) {
 
571
                rb_warning("discarding old %s", rb_id2name(name));
 
572
            }
 
573
            if (nd_type(node->nd_body->nd_body) == NODE_CFUNC) {
 
574
                rb_vm_check_redefinition_opt_method(node);
 
575
            }
 
576
        }
 
577
    }
 
578
 
 
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));
 
584
 
 
585
    rb_clear_cache_by_id(name);
 
586
 
 
587
    if (singleton) {
 
588
        rb_funcall(singleton, singleton_added, 1, ID2SYM(name));
 
589
    }
 
590
    else {
 
591
        rb_funcall(klass, added, 1, ID2SYM(name));
 
592
    }
 
593
}
 
594
 
 
595
/*
 
596
 *  call-seq:
 
597
 *     alias_method(new_name, old_name)   => self
 
598
 *
 
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.
 
601
 *
 
602
 *     module Mod
 
603
 *       alias_method :orig_exit, :exit
 
604
 *       def exit(code=0)
 
605
 *         puts "Exiting with code #{code}"
 
606
 *         orig_exit(code)
 
607
 *       end
 
608
 *     end
 
609
 *     include Mod
 
610
 *     exit(99)
 
611
 *
 
612
 *  <em>produces:</em>
 
613
 *
 
614
 *     Exiting with code 99
 
615
 */
 
616
 
 
617
static VALUE
 
618
rb_mod_alias_method(VALUE mod, VALUE newname, VALUE oldname)
 
619
{
 
620
    rb_alias(mod, rb_to_id(newname), rb_to_id(oldname));
 
621
    return mod;
 
622
}