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
|
/************************************************
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"
#include "methods.h"
void method_free();
#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 SMethod *method;
int undef;
};
static struct hash_entry cache[CACHE_SIZE];
static struct SMethod*
search_method(class, id, origin)
struct RClass *class, **origin;
ID id;
{
struct SMethod *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)
struct RClass *class;
ID id;
int envset;
{
int pos, i;
struct SMethod *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 SMethod *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].undef = body->undef;
}
method = cache[pos].method;
if (cache[pos].undef) 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 SMethod *body;
if (st_lookup(class->m_tbl, name, &body)) {
if (verbose) {
Warning("redefine %s", rb_id2name(name));
}
rb_clear_cache(body);
method_free(body);
}
body = search_method(class, def, &body);
body->count++;
st_insert(class->m_tbl, name, body);
}
void
rb_clear_cache(body)
struct SMethod *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;
{
int i;
for (i = 0; i< CACHE_SIZE; i++ ) {
if (cache[i].origin == class) {
cache[i].class = Qnil;
cache[i].mid = Qnil;
}
}
}
void
method_free(body)
struct SMethod *body;
{
body->count--;
if (body->count == 0) {
freenode(body->node);
free(body);
}
}
|