summaryrefslogtreecommitdiff
path: root/variable.c
blob: ae3163de19c427bde2fd9e3bd27cadc014379594 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/************************************************

  variable.c -

  $Author: matz $
  $Date: 1994/11/01 08:28:42 $
  created at: Tue Apr 19 23:55:15 JST 1994

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

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

st_table *rb_global_tbl;
st_table *rb_class_tbl;
#define global_tbl rb_global_tbl
#define class_tbl rb_class_tbl
#define instance_tbl (RBASIC(Qself)->iv_tbl)

st_table *new_idhash()
{
    return st_init_table(ST_NUMCMP, ST_NUMHASH);
}

void
Init_var_tables()
{
    global_tbl = new_idhash();
    class_tbl = new_idhash();
}

void
rb_name_class(class, id)
    VALUE class;
    ID id;
{
    VALUE body;

    if (st_lookup(class_tbl, id, &body)) {
	Bug("%s %s already exists",
	    TYPE(body)==T_CLASS?"class":"module", rb_id2name(id));
    }
    st_add_direct(class_tbl, id, class);
}

struct global_entry {
    enum { GLOBAL_VAL, GLOBAL_VAR, GLOBAL_UNDEF } mode;
    ID id;
    union {
	VALUE val;
	VALUE *var;
    } v;
    VALUE (*get_hook)();
    VALUE (*set_hook)();
};

static 
mark_global_entry(key, entry)
    ID key;
    struct global_entry *entry;
{
    switch (entry->mode) {
	case GLOBAL_VAL:
	gc_mark(entry->v.val);	/* normal global value */
	break;
      case GLOBAL_VAR:
	if (entry->v.var)
	    gc_mark(*entry->v.var); /* c variable pointer */
	break;
      default:
	break;
    }
    return ST_CONTINUE;
}

gc_mark_global_tbl()
{
    st_foreach(global_tbl, mark_global_entry, 0);
}

struct global_entry*
rb_global_entry(id)
    ID id;
{
    struct global_entry *entry;

    if (!st_lookup(global_tbl, id, &entry)) {
	entry = ALLOC(struct global_entry);
	st_insert(global_tbl, id, entry);
	entry->id = id;
	entry->mode = GLOBAL_UNDEF;
	entry->v.var = Qnil;
	entry->get_hook = entry->set_hook = Qnil;
    }
    return entry;
}

void
rb_define_variable(name, var, get_hook, set_hook)
    char  *name;
    VALUE *var;
    VALUE (*get_hook)();
    VALUE (*set_hook)();
{
    struct global_entry *entry;
    ID id;

    if (name[0] == '$') id = rb_intern(name);
    else {
	char *buf = (char*)alloca(strlen(name)+2);
	buf[0] = '$';
	strcpy(buf+1, name);
	id = rb_intern(buf);
    }

    if (!st_lookup(global_tbl, id, &entry)) {
	entry = rb_global_entry(id);
    }
    entry->mode = GLOBAL_VAR;
    entry->v.var = var;
    entry->get_hook = get_hook;
    entry->set_hook = set_hook;
}

void
rb_define_varhook(name, get_hook, set_hook)
    char  *name;
    VALUE (*get_hook)();
    VALUE (*set_hook)();
{
    struct global_entry *entry;
    ID id;

    if (name[0] == '$') id = rb_intern(name);
    else {
	char *buf = (char*)alloca(strlen(name)+2);
	buf[0] = '$';
	strcpy(buf+1, name);
	id = rb_intern(buf);
    }

    if (!st_lookup(global_tbl, id, &entry)) {
	entry = ALLOC(struct global_entry);
	entry->id = id;
	entry->mode = GLOBAL_VAL;
	st_insert(global_tbl, id, entry);
    }
    else if (entry->mode == GLOBAL_UNDEF) {
	entry->mode = GLOBAL_VAL;
    }
    entry->v.val = Qnil;
    entry->get_hook = get_hook;
    entry->set_hook = set_hook;
}

VALUE
rb_readonly_hook(val, id)
    VALUE val;
    ID id;
{
    Fail("Can't set variable %s", rb_id2name(id));
    /* not reached */
}

VALUE
rb_id2class(id)
    ID id;
{
    VALUE class;

    if (st_lookup(class_tbl, id, &class))
	return class;
    return Qnil;
}

VALUE
rb_gvar_get(entry)
    struct global_entry *entry;
{
    VALUE val;

    if (entry->get_hook)
	val = (*entry->get_hook)(entry->id);
    switch (entry->mode) {
      case GLOBAL_VAL:
	return entry->v.val;

      case GLOBAL_VAR:
	if (entry->v.var == Qnil) return val;
	return *entry->v.var;

      default:
	break;
    }
    if (verbose)
	Warning("global var %s not initialized", rb_id2name(entry->id));
    return Qnil;
}

VALUE
rb_ivar_get_1(obj, id)
    struct RBasic *obj;
    ID id;
{
    VALUE val;

    if (obj->iv_tbl == Qnil)
	return Qnil;
    if (st_lookup(obj->iv_tbl, id, &val))
	return val;
    if (verbose)
	Warning("instance var %s not initialized", rb_id2name(id));
    return Qnil;
}

VALUE
rb_ivar_get(id)
    ID id;
{
    return rb_ivar_get_1(Qself, id);
}

VALUE
rb_mvar_get(id)
    ID id;
{
    VALUE val;

    if (st_lookup(class_tbl, id, &val)) return val;
    if (verbose)
	Warning("local var %s not initialized", rb_id2name(id));
    return Qnil;
}

VALUE
rb_const_get(id)
    ID id;
{
    struct RClass *class = (struct RClass*)CLASS_OF(Qself);
    VALUE value;

    while (class) {
	if (class->c_tbl && st_lookup(class->c_tbl, id, &value)) {
	    return value;
	}
	class = class->super;
    }
    Fail("Uninitialized constant %s", rb_id2name(id));
    /* not reached */
}

VALUE
rb_gvar_set(entry, val)
    struct global_entry *entry;
    VALUE val;
{
    if (entry->set_hook)
	(*entry->set_hook)(val, entry->id);

    if (entry->mode == GLOBAL_VAR) {
	if (entry->v.var == Qnil) {
	    rb_readonly_hook(val, entry->id);
	}
	return *entry->v.var = val;
    }
    else {
	if (entry->mode == GLOBAL_UNDEF)
	    entry->mode = GLOBAL_VAL;
	return entry->v.val = val;
    }
}

VALUE
rb_gvar_set2(name, val)
    char *name;
    VALUE val;
{
    struct global_entry *entry;
    ID id;

    id = rb_intern(name);
    if (!st_lookup(global_tbl, id, &entry)) {
	entry = rb_global_entry(id);
    }
    rb_gvar_set(entry, val);

    return val;
}

VALUE
rb_ivar_set_1(obj, id, val)
    struct RBasic *obj;
    ID id;
    VALUE val;
{
    if (obj->iv_tbl == Qnil) obj->iv_tbl = new_idhash();
    st_insert(obj->iv_tbl, id, val);
    return val;
}

VALUE
rb_ivar_set(id, val)
    ID id;
    VALUE val;
{
    return rb_ivar_set_1(Qself, id, val);
}

static VALUE
const_bound(class, id)
    struct RClass *class;
    ID id;
{
    while (class) {
	if (class->c_tbl && st_lookup(class->c_tbl, id, Qnil)) {
	    return TRUE;
	}
	class = class->super;
    }
    return FALSE;
}

void
rb_const_set(class, id, val)
    struct RClass *class;
    ID id;
    VALUE val;
{
    if (const_bound(class, id))
	Fail("already initialized constnant");

    if (class->c_tbl == Qnil)
	class->c_tbl = new_idhash();

    st_insert(class->c_tbl, id, val);
}

void
rb_define_const(class, name, val)
    struct RClass *class;
    char *name;
    VALUE val;
{
    rb_const_set(class, rb_intern(name), val);
}

VALUE
rb_iv_get(obj, name)
    VALUE obj;
    char *name;
{
    ID id = rb_intern(name);

    return rb_ivar_get_1(obj, id);
}

VALUE
rb_iv_set(obj, name, val)
    VALUE obj;
    char *name;
    VALUE val;
{
    ID id = rb_intern(name);

    return rb_ivar_set_1(obj, id, val);
}

VALUE
Fdefined(obj, name)
    VALUE obj;
    struct RString *name;
{
    ID id;
    struct global_entry *entry;

    if (FIXNUM_P(name)) {
	id = FIX2INT(name);
    }
    else {
	Check_Type(name, T_STRING);
	id = rb_intern(name->ptr);
    }

    if (id == rb_intern("nil") || id == rb_intern("self")) return TRUE;

    switch (id & ID_SCOPE_MASK) {
      case ID_GLOBAL:
	if (st_lookup(global_tbl, id, &entry) && entry->mode != GLOBAL_UNDEF)
	    return TRUE;
	break;

      case ID_INSTANCE:
	if (TYPE(Qself) != T_OBJECT || instance_tbl == Qnil) break;
	if (st_lookup(instance_tbl, id, Qnil)) return TRUE;
	break;

      case ID_CONST:
	return const_bound(CLASS_OF(Qself), id);
	break;

      default:
	{
	    int i, max;

	    if (the_scope->local_tbl) {
		for (i=1, max=the_scope->local_tbl[0]+1; i<max; i++) {
		    if (the_scope->local_tbl[i] == id) return TRUE;
		}
	    }
	}
	if (st_lookup(class_tbl, id, Qnil)) return TRUE;
	break;
    }
    return FALSE;
}