summaryrefslogtreecommitdiff
path: root/methods.c
blob: d8613234444808477c3e0f35d511303698f08f67 (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
/************************************************

  methods.c -

  $Author: matz $
  $Date: 1994/06/17 14:23:50 $
  created at: Fri Oct  1 17:25:07 JST 1993

  Copyright (C) 1994 Yukihiro Matsumoto

************************************************/

#include "ruby.h"
#include "ident.h"
#include "env.h"
#include "node.h"

#define CACHE_SIZE 577
#if 0
#define EXPR1(c,m) (((int)(c)*(m))>>0)
#else
#define EXPR1(c,m) ((int)(c)^(m))
#endif

#define TRIAL 3

struct hash_entry {		/* method hash table. */
    ID mid;			/* method's id */
    struct RClass *class;	/* receiver's class */
    struct RClass *origin;	/* where method defined  */
    struct RMethod *method;
    enum mth_scope scope;
};

static struct hash_entry cache[CACHE_SIZE];

static struct RMethod*
search_method(class, id, origin)
    struct RClass *class, **origin;
    ID id;
{
    struct RMethod *body;
    NODE *list;

    while (!st_lookup(class->m_tbl, id, &body)) {
	class = class->super;
	if (class == Qnil) return Qnil;
    }

    if (body->origin)
	*origin = body->origin;
    else
	*origin = class;
    return body;
}

NODE*
rb_get_method_body(class, id, envset, scope)
    struct RClass *class;
    ID id;
    int envset;
    enum mth_scope scope;
{
    int pos, i;
    int cscope;
    struct RMethod *method;

    /* is it in the method cache? */
    pos = EXPR1(class, id) % CACHE_SIZE;
    if (cache[pos].class != class || cache[pos].mid != id) {
	/* not in the cache */
	struct RMethod *body;
	struct RClass *origin;

	if ((body = search_method(class, id, &origin)) == Qnil) {
	    return Qnil;
	}
	/* store in cache */
	cache[pos].mid = id;
	cache[pos].class = class;
	cache[pos].origin = origin;
	cache[pos].method = body;
	cache[pos].scope = body->scope;
    }

    cscope = cache[pos].scope;
    method = cache[pos].method;
    if (cscope == MTH_UNDEF) return Qnil;
    if (cscope == MTH_FUNC && scope == MTH_METHOD) return Qnil;
    if (envset) {
	the_env->last_func = method->id;
	the_env->last_class = cache[pos].origin;
    }
    return method->node;
}

void
rb_alias(class, name, def)
    struct RClass *class;
    ID name, def;
{
    struct RMethod *body;

    if (st_lookup(class->m_tbl, name, &body)) {
	if (verbose) {
	    Warning("redefine %s", rb_id2name(name));
	}
	unliteralize(body);
    }
    body = search_method(class, def, &body);
    st_insert(class->m_tbl, name, body);
}

void
rb_clear_cache(body)
    struct RMethod *body;
{
    int i;

    for (i = 0; i< CACHE_SIZE; i++ ) {
	if (cache[i].method == body) {
	    cache[i].class = Qnil;
	    cache[i].mid = Qnil;
	}
    }
}

void
rb_clear_cache2(class)
    struct RClass *class;
{

    class = class->super;
    while (class) {
	int i;

	for (i = 0; i< CACHE_SIZE; i++ ) {
	    if (cache[i].origin == class) {
		cache[i].class = Qnil;
		cache[i].mid = Qnil;
	    }
	}
	class = class->super;
    }
}