-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.c
13287 lines (11852 loc) · 308 KB
/
eval.c
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
eval.c -
$Author$
$Date$
created at: Thu Jun 10 14:22:17 JST 1993
Copyright (C) 1993-2003 Yukihiro Matsumoto
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
Copyright (C) 2000 Information-technology Promotion Agency, Japan
**********************************************************************/
#include "ruby.h"
#include "node.h"
#include "env.h"
#include "util.h"
#include "rubysig.h"
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifndef EXIT_SUCCESS
#define EXIT_SUCCESS 0
#endif
#ifndef EXIT_FAILURE
#define EXIT_FAILURE 1
#endif
#include <stdio.h>
#include "st.h"
#include "dln.h"
#ifdef __APPLE__
#include <crt_externs.h>
#endif
/* Make alloca work the best possible way. */
#ifdef __GNUC__
# ifndef atarist
# ifndef alloca
# define alloca __builtin_alloca
# endif
# endif /* atarist */
#else
# ifdef HAVE_ALLOCA_H
# include <alloca.h>
# else
# ifndef _AIX
# ifndef alloca /* predefined by HP cc +Olibcalls */
void *alloca ();
# endif
# endif /* AIX */
# endif /* HAVE_ALLOCA_H */
#endif /* __GNUC__ */
#include <stdarg.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef __BEOS__
#include <net/socket.h>
#endif
#ifdef __MACOS__
#include "macruby_private.h"
#endif
#ifdef __VMS
#include "vmsruby_private.h"
#endif
#ifdef USE_CONTEXT
NORETURN(static void rb_jump_context(rb_jmpbuf_t, int));
static inline void
rb_jump_context(rb_jmpbuf_t env, int val)
{
env->status = val;
setcontext(&env->context);
abort(); /* ensure noreturn */
}
/*
* PRE_GETCONTEXT and POST_GETCONTEXT is a magic for getcontext, gcc,
* IA64 register stack and SPARC register window combination problem.
*
* Assume following code sequence.
*
* 1. set a register in the register stack/window such as r32/l0.
* 2. call getcontext.
* 3. use the register.
* 4. update the register for other use.
* 5. call setcontext indirectly (or directly).
*
* This code should be run as 1->2->3->4->5->3->4.
* But after second getcontext return (second 3),
* the register is broken (updated).
* It's because getcontext/setcontext doesn't preserve the content of the
* register stack/window.
*
* setjmp also doesn't preserve the content of the register stack/window.
* But it has not the problem because gcc knows setjmp may return twice.
* gcc detects setjmp and generates setjmp safe code.
*
* So setjmp calls before and after getcontext call makes the code
* somewhat safe.
* It fix the problem on IA64.
* It is not required that setjmp is called at run time, since the problem is
* register usage.
*
* Since the magic setjmp is not enough for SPARC,
* inline asm is used to prohibit registers in register windows.
*
* Since the problem is fixed at gcc 4.0.3, the magic is applied only for
* prior versions of gcc.
* http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21957
* http://gcc.gnu.org/bugzilla/show_bug.cgi?id=22127
*/
# define GCC_VERSION_BEFORE(major, minor, patchlevel) \
(defined(__GNUC__) && !defined(__INTEL_COMPILER) && \
((__GNUC__ < (major)) || \
(__GNUC__ == (major) && __GNUC_MINOR__ < (minor)) || \
(__GNUC__ == (major) && __GNUC_MINOR__ == (minor) && __GNUC_PATCHLEVEL__ < (patchlevel))))
# if GCC_VERSION_BEFORE(4,0,3) && (defined(sparc) || defined(__sparc__))
# ifdef __pic__
/*
* %l7 is excluded for PIC because it is PIC register.
* http://lists.freebsd.org/pipermail/freebsd-sparc64/2006-January/003739.html
*/
# define PRE_GETCONTEXT \
({ __asm__ volatile ("" : : : \
"%o0", "%o1", "%o2", "%o3", "%o4", "%o5", "%o7", \
"%l0", "%l1", "%l2", "%l3", "%l4", "%l5", "%l6", \
"%i0", "%i1", "%i2", "%i3", "%i4", "%i5", "%i7"); }),
# else
# define PRE_GETCONTEXT \
({ __asm__ volatile ("" : : : \
"%o0", "%o1", "%o2", "%o3", "%o4", "%o5", "%o7", \
"%l0", "%l1", "%l2", "%l3", "%l4", "%l5", "%l6", "%l7", \
"%i0", "%i1", "%i2", "%i3", "%i4", "%i5", "%i7"); }),
# endif
# define POST_GETCONTEXT PRE_GETCONTEXT
# elif GCC_VERSION_BEFORE(4,0,3) && defined(__ia64)
static jmp_buf function_call_may_return_twice_jmp_buf;
int function_call_may_return_twice_false_1 = 0;
int function_call_may_return_twice_false_2 = 0;
# define PRE_GETCONTEXT \
(function_call_may_return_twice_false_1 ? \
setjmp(function_call_may_return_twice_jmp_buf) : \
0),
# define POST_GETCONTEXT \
(function_call_may_return_twice_false_2 ? \
setjmp(function_call_may_return_twice_jmp_buf) : \
0),
# elif defined(__FreeBSD__) && __FreeBSD__ < 7
/*
* workaround for FreeBSD/i386 getcontext/setcontext bug.
* clear the carry flag by (0 ? ... : ...).
* FreeBSD PR 92110 http://www.freebsd.org/cgi/query-pr.cgi?pr=92110
* [ruby-dev:28263]
*/
static int volatile freebsd_clear_carry_flag = 0;
# define PRE_GETCONTEXT \
(freebsd_clear_carry_flag ? (freebsd_clear_carry_flag = 0) : 0),
# endif
# ifndef PRE_GETCONTEXT
# define PRE_GETCONTEXT
# endif
# ifndef POST_GETCONTEXT
# define POST_GETCONTEXT
# endif
# define ruby_longjmp(env, val) rb_jump_context(env, val)
# define ruby_setjmp(just_before_setjmp, j) ((j)->status = 0, \
(just_before_setjmp), \
PRE_GETCONTEXT \
getcontext(&(j)->context), \
POST_GETCONTEXT \
(j)->status)
#else
# if !defined(setjmp) && defined(HAVE__SETJMP)
# define ruby_setjmp(just_before_setjmp, env) \
((just_before_setjmp), _setjmp(env))
# define ruby_longjmp(env,val) _longjmp(env,val)
# else
# define ruby_setjmp(just_before_setjmp, env) \
((just_before_setjmp), setjmp(env))
# define ruby_longjmp(env,val) longjmp(env,val)
# endif
#endif
#include <sys/types.h>
#include <signal.h>
#include <errno.h>
#if defined(__VMS)
#pragma nostandard
#endif
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
/*
Solaris sys/select.h switches select to select_large_fdset to support larger
file descriptors if FD_SETSIZE is larger than 1024 on 32bit environment.
But Ruby doesn't change FD_SETSIZE because fd_set is allocated dynamically.
So following definition is required to use select_large_fdset.
*/
#ifdef HAVE_SELECT_LARGE_FDSET
#define select(n, r, w, e, t) select_large_fdset(n, r, w, e, t)
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#include <sys/stat.h>
VALUE rb_cProc;
VALUE rb_cBinding;
static VALUE proc_alloc(VALUE,struct BLOCK*,int);
static VALUE proc_invoke(VALUE,VALUE,VALUE,VALUE,int);
#define INVOKE_CALL (YIELD_CALL|YIELD_VALUES)
#define INVOKE_VALUES YIELD_VALUES
static VALUE proc_lambda(void);
static VALUE rb_f_binding(VALUE);
static void rb_f_END(void);
static struct BLOCK *passing_block(VALUE,struct BLOCK*);
static int block_orphan(struct BLOCK *data);
VALUE rb_cMethod;
VALUE rb_cUnboundMethod;
static VALUE umethod_bind(VALUE, VALUE);
static VALUE rb_mod_define_method(int, VALUE*, VALUE);
static VALUE rb_obj_define_method(int, VALUE*, VALUE);
NORETURN(static void rb_raise_jump(VALUE));
static VALUE rb_make_exception(int argc, VALUE *argv);
static int vis_mode;
#define VIS_PUBLIC 0
#define VIS_PRIVATE 1
#define VIS_PROTECTED 2
#define VIS_MODFUNC 5
#define VIS_LOCAL 8
#define VIS_MASK 15
#define VIS_SET(f) (vis_mode=(f))
#define VIS_TEST(f) (vis_mode&(f))
#define VIS_MODE() (vis_mode)
VALUE (*ruby_sandbox_save)(struct thread *) = NULL;
VALUE (*ruby_sandbox_restore)(struct thread *) = NULL;
NODE* ruby_current_node;
int ruby_safe_level = 0;
/* safe-level:
0 - strings from streams/environment/ARGV are tainted (default)
1 - no dangerous operation by tainted value
2 - process/file operations prohibited
3 - all generated objects are tainted
4 - no global (non-tainted) variable modification/no direct output
*/
static VALUE safe_getter(void);
static void safe_setter(VALUE val);
void
rb_secure(int level)
{
if (level <= ruby_safe_level) {
if (ruby_frame->callee) {
rb_raise(rb_eSecurityError, "Insecure operation `%s' at level %d",
rb_id2name(ruby_frame->callee), ruby_safe_level);
}
else {
rb_raise(rb_eSecurityError, "Insecure operation at level %d", ruby_safe_level);
}
}
}
void
rb_secure_update(VALUE obj)
{
if (!OBJ_TAINTED(obj)) rb_secure(4);
}
void
rb_check_safe_obj(VALUE x)
{
if (ruby_safe_level > 0 && OBJ_TAINTED(x)){
if (ruby_frame->callee) {
rb_raise(rb_eSecurityError, "Insecure operation - %s",
rb_id2name(ruby_frame->callee));
}
else {
rb_raise(rb_eSecurityError, "Insecure operation: -r");
}
}
rb_secure(4);
}
void
rb_check_safe_str(VALUE x)
{
rb_check_safe_obj(x);
if (TYPE(x)!= T_STRING) {
rb_raise(rb_eTypeError, "wrong argument type %s (expected String)",
rb_obj_classname(x));
}
}
NORETURN(static void raise_undef(VALUE, ID));
static void
raise_undef(VALUE klass, ID id)
{
rb_name_error(id, "undefined method `%s' for %s `%s'",
rb_id2name(id),
(TYPE(klass) == T_MODULE) ? "module" : "class",
rb_class2name(klass));
}
static ID removed, singleton_removed, undefined, singleton_undefined;
#define CACHE_SIZE 0x800
#define CACHE_MASK 0x7ff
#define EXPR1(c,m) ((((c)>>3)^(m))&CACHE_MASK)
struct cache_entry { /* method hash table. */
ID mid; /* method's id */
ID mid0; /* method's original id */
VALUE klass; /* receiver's class */
VALUE origin; /* where method defined */
NODE *method;
int noex;
};
static struct cache_entry cache[2][CACHE_SIZE];
static int ruby_running = 0;
void
rb_clear_cache()
{
int i;
if (!ruby_running) return;
for (i=0; i<CACHE_SIZE; i++) {
cache[0][i].mid = cache[1][i].mid = 0;
}
}
static void
rb_clear_cache_for_remove(VALUE klass, ID id)
{
int i, j;
if (!ruby_running) return;
for (i=0; i<CACHE_SIZE; i++) {
for (j=0; j<2; j++) {
struct cache_entry *ent = cache[j]+i;
if (ent->mid == id &&
RCLASS(ent->origin)->m_tbl == RCLASS(klass)->m_tbl) {
ent->mid = 0;
}
}
}
}
static void
rb_clear_cache_by_id(ID id)
{
int i, j;
if (!ruby_running) return;
for (i=0; i<CACHE_SIZE; i++) {
for (j=0; j<2; j++) {
struct cache_entry *ent = cache[j]+i;
if (ent->mid == id) {
ent->mid = 0;
}
}
}
}
void
rb_clear_cache_by_class(VALUE klass)
{
int i, j;
if (!ruby_running) return;
for (i=0; i<CACHE_SIZE; i++) {
for (j=0; j<2; j++) {
struct cache_entry *ent = cache[j]+i;
if (ent->klass == klass || ent->origin == klass) {
ent->mid = 0;
}
}
}
}
static ID init, eqq, each, aref, aset, match, missing;
static ID added, singleton_added;
static ID object_id, __send, __send_bang, respond_to;
#define NOEX_SAFE(n) ((n) >> 5)
#define NOEX_WITH(n, v) ((n) | (v) << 5)
#define NOEX_WITH_SAFE(n) NOEX_WITH(n, ruby_safe_level)
void
rb_add_method(VALUE klass, ID mid, NODE *node, int noex)
{
NODE *body;
if (NIL_P(klass)) klass = rb_cObject;
if (ruby_safe_level >= 4 && (klass == rb_cObject || !OBJ_TAINTED(klass))) {
rb_raise(rb_eSecurityError, "Insecure: can't define method");
}
if (!FL_TEST(klass, FL_SINGLETON) &&
node && nd_type(node) != NODE_ZSUPER &&
(mid == rb_intern("initialize" )|| mid == rb_intern("initialize_copy"))) {
noex = NOEX_PRIVATE | noex;
}
else if (FL_TEST(klass, FL_SINGLETON) && node && nd_type(node) == NODE_CFUNC &&
mid == rb_intern("allocate")) {
rb_warn("defining %s.allocate is deprecated; use rb_define_alloc_func()",
rb_class2name(rb_iv_get(klass, "__attached__")));
mid = ID_ALLOCATOR;
}
if (OBJ_FROZEN(klass)) rb_error_frozen("class/module");
rb_clear_cache_by_id(mid);
body = NEW_METHOD(node, NOEX_WITH_SAFE(noex));
st_insert(RCLASS(klass)->m_tbl, mid, (st_data_t)body);
if (node && mid != ID_ALLOCATOR && ruby_running) {
if (FL_TEST(klass, FL_SINGLETON)) {
rb_funcall(rb_iv_get(klass, "__attached__"), singleton_added, 1, ID2SYM(mid));
}
else {
rb_funcall(klass, added, 1, ID2SYM(mid));
}
}
}
void
rb_define_alloc_func(VALUE klass, VALUE (*func) (VALUE))
{
Check_Type(klass, T_CLASS);
rb_add_method(CLASS_OF(klass), ID_ALLOCATOR, NEW_CFUNC(func, 0), NOEX_PRIVATE);
}
void
rb_undef_alloc_func(VALUE klass)
{
Check_Type(klass, T_CLASS);
rb_add_method(CLASS_OF(klass), ID_ALLOCATOR, 0, NOEX_UNDEF);
}
#define LOOKUP_NORMAL 0
#define LOOKUP_FCALL 1
#define LOOKUP_NOSKIP 2
#define LOOKUP_LOCAL 1
static NODE*
search_method(VALUE klass, ID id, VALUE *origin, int flag, int *out)
{
NODE *body;
if (flag == LOOKUP_FCALL && ruby_frame->this_class) {
if (st_lookup(RCLASS(ruby_frame->this_class)->m_tbl, id, (st_data_t *)&body) &&
body->nd_noex == NOEX_LOCAL) {
if (origin) *origin = ruby_frame->this_class;
if (out) *out = LOOKUP_LOCAL;
return body;
}
}
for (;klass; klass = RCLASS(klass)->super) {
if (st_lookup(RCLASS(klass)->m_tbl, id, (st_data_t *)&body) &&
(flag == LOOKUP_NOSKIP || body->nd_noex != NOEX_LOCAL)) {
if (origin) *origin = klass;
if (out) *out = LOOKUP_NORMAL;
return body;
}
}
return 0;
}
static NODE*
rb_get_method_body(VALUE *klassp, ID *idp, int *noexp)
{
ID id = *idp;
VALUE klass = *klassp;
VALUE origin;
NODE * volatile body;
struct cache_entry *ent;
int noex = *noexp;
int lc;
if ((body = search_method(klass, id, &origin, noex, &lc)) == 0 || !body->nd_body) {
/* store empty info in cache */
ent = cache[noex] + EXPR1(klass, id);
ent->klass = klass;
ent->origin = klass;
ent->mid = ent->mid0 = id;
ent->noex = 0;
ent->method = 0;
return 0;
}
if (ruby_running) {
VALUE c = (lc == LOOKUP_LOCAL) ? origin : klass;
/* store in cache */
ent = cache[lc] + EXPR1(c, id);
ent->klass = c;
ent->noex = body->nd_noex;
if (noexp) *noexp = body->nd_noex;
body = body->nd_body;
if (nd_type(body) == NODE_FBODY) {
ent->mid = id;
*klassp = body->nd_orig;
ent->origin = body->nd_orig;
*idp = ent->mid0 = body->nd_mid;
body = ent->method = body->nd_head;
}
else {
*klassp = origin;
ent->origin = origin;
ent->mid = ent->mid0 = id;
ent->method = body;
}
}
else {
if (noexp) *noexp = body->nd_noex;
body = body->nd_body;
if (nd_type(body) == NODE_FBODY) {
*klassp = body->nd_orig;
*idp = body->nd_mid;
body = body->nd_head;
}
else {
*klassp = origin;
}
}
return body;
}
NODE*
rb_method_node(VALUE klass, ID id)
{
int noex = LOOKUP_NORMAL;
struct cache_entry *ent;
ent = cache[0] + EXPR1(klass, id);
if (ent->mid == id && ent->klass == klass && ent->method){
return ent->method;
}
return rb_get_method_body(&klass, &id, &noex);
}
static void
remove_method(VALUE klass, ID mid)
{
NODE *body;
if (klass == rb_cObject) {
rb_secure(4);
}
if (ruby_safe_level >= 4 && !OBJ_TAINTED(klass)) {
rb_raise(rb_eSecurityError, "Insecure: can't remove method");
}
if (OBJ_FROZEN(klass)) rb_error_frozen("class/module");
if (mid == object_id || mid == __send || mid == __send_bang || mid == init) {
rb_warn("removing `%s' may cause serious problem", rb_id2name(mid));
}
if (!st_delete(RCLASS(klass)->m_tbl, &mid, (st_data_t *)&body) ||
!body->nd_body) {
rb_name_error(mid, "method `%s' not defined in %s",
rb_id2name(mid), rb_class2name(klass));
}
rb_clear_cache_for_remove(klass, mid);
if (FL_TEST(klass, FL_SINGLETON)) {
rb_funcall(rb_iv_get(klass, "__attached__"), singleton_removed, 1, ID2SYM(mid));
}
else {
rb_funcall(klass, removed, 1, ID2SYM(mid));
}
}
void
rb_remove_method(VALUE klass, const char *name)
{
remove_method(klass, rb_intern(name));
}
/*
* call-seq:
* remove_method(symbol) => self
*
* Removes the method identified by _symbol_ from the current
* class. For an example, see <code>Module.undef_method</code>.
*/
static VALUE
rb_mod_remove_method(int argc, VALUE *argv, VALUE mod)
{
int i;
for (i=0; i<argc; i++) {
remove_method(mod, rb_to_id(argv[i]));
}
return mod;
}
#undef rb_disable_super
#undef rb_enable_super
void
rb_disable_super(VALUE klass, const char *name)
{
/* obsolete - no use */
}
void
rb_enable_super(VALUE klass, const char *name)
{
rb_warning("rb_enable_super() is obsolete");
}
static void
rb_export_method(VALUE klass, ID name, ID noex)
{
NODE *body;
VALUE origin;
if (klass == rb_cObject) {
rb_secure(4);
}
body = search_method(klass, name, &origin, LOOKUP_NOSKIP, 0);
if (!body && TYPE(klass) == T_MODULE) {
body = search_method(rb_cObject, name, &origin, LOOKUP_NOSKIP, 0);
}
if (!body || !body->nd_body) {
raise_undef(klass, name);
}
if (body->nd_noex != noex) {
if (klass == origin) {
body->nd_noex = noex;
}
else {
rb_add_method(klass, name, NEW_ZSUPER(), noex);
}
}
}
static int
method_exists(VALUE klass, ID id, int noex)
{
struct cache_entry *ent;
int nx = noex;
switch (noex) {
case LOOKUP_NORMAL:
case LOOKUP_FCALL:
/* is it in the method cache? */
ent = cache[noex] + EXPR1(klass, id);
if (ent->mid == id && ent->klass == klass) {
if (nx == LOOKUP_NORMAL) {
if (ent->noex == NOEX_PRIVATE) return Qfalse;
}
else if (ent->noex != NOEX_LOCAL) {
if (!ent->method) return Qfalse;
return Qtrue;
}
}
/* fall through */
default:
if (rb_get_method_body(&klass, &id, &noex)) {
if (nx == LOOKUP_NORMAL && noex == NOEX_PRIVATE)
return Qfalse;
return Qtrue;
}
return Qfalse;
}
}
int
rb_method_boundp(VALUE klass, ID id, int pub)
{
return method_exists(klass, id, pub ? LOOKUP_NORMAL : LOOKUP_FCALL);
}
void
rb_attr(VALUE klass, ID id, int read, int write, int noex)
{
const char *name;
char *buf;
ID attriv;
size_t len;
if (!noex) noex = NOEX_PUBLIC;
else {
if (VIS_TEST(VIS_PRIVATE)) {
noex = NOEX_PRIVATE;
rb_warning((VIS_MODE() == VIS_MODFUNC) ?
"attribute accessor as module_function" :
"private attribute?");
}
else if (VIS_TEST(VIS_PROTECTED)) {
noex = NOEX_PROTECTED;
}
else {
noex = NOEX_PUBLIC;
}
}
if (!rb_is_local_id(id) && !rb_is_const_id(id)) {
rb_name_error(id, "invalid attribute name `%s'", rb_id2name(id));
}
name = rb_id2name(id);
if (!name) {
rb_raise(rb_eArgError, "argument needs to be symbol or string");
}
len = strlen(name)+2;
buf = ALLOCA_N(char,len);
snprintf(buf, len, "@%s", name);
attriv = rb_intern(buf);
if (read) {
rb_add_method(klass, id, NEW_IVAR(attriv), noex);
}
if (write) {
rb_add_method(klass, rb_id_attrset(id), NEW_ATTRSET(attriv), noex);
}
}
VALUE ruby_errinfo = Qnil;
extern int ruby_nerrs;
VALUE rb_eLocalJumpError;
VALUE rb_eSysStackError;
extern VALUE ruby_top_self;
struct FRAME *ruby_frame;
struct SCOPE *ruby_scope;
static struct FRAME *top_frame;
static struct SCOPE *top_scope;
static unsigned long frame_unique = 1;
#define PUSH_FRAME(link) do { \
struct FRAME _frame; \
_frame.prev = ruby_frame; \
_frame.tmp = 0; \
_frame.node = ruby_current_node; \
_frame.argc = 0; \
_frame.self = (link)?ruby_frame->self:0;\
_frame.block = (link)?ruby_frame->block:0;\
_frame.flags = 0; \
_frame.uniq = frame_unique++; \
_frame.callee = 0; \
_frame.this_func = 0; \
_frame.this_class = 0; \
ruby_frame = &_frame
#define POP_FRAME() \
ruby_current_node = _frame.node; \
ruby_frame = _frame.prev; \
} while (0)
static unsigned long block_unique = 1;
#define PUSH_BLOCK(v,iv,b) do { \
struct BLOCK _block; \
_block.var = (iv); \
_block.body = (b); \
_block.self = self; \
_block.frame = *ruby_frame; \
_block.cref = ruby_cref; \
_block.frame.node = ruby_current_node;\
_block.scope = ruby_scope; \
_block.vmode = vis_mode; \
_block.flags = BLOCK_D_SCOPE; \
_block.dyna_vars = ruby_dyna_vars; \
_block.wrapper = ruby_wrapper; \
_block.block_obj = 0; \
if (b) { \
_block.uniq = block_unique++; \
prot_tag->blkid = _block.uniq; \
} \
else { \
_block.uniq = 0; \
prot_tag->blkid = 0; \
} \
(v) = &_block
#define POP_BLOCK() } while (0)
struct RVarmap *ruby_dyna_vars;
#define PUSH_VARS() do { \
struct RVarmap * volatile _old; \
_old = ruby_dyna_vars; \
ruby_dyna_vars = 0
#define POP_VARS() \
if (_old && (ruby_scope->flags & SCOPE_DONT_RECYCLE)) {\
if (RBASIC(_old)->flags) /* unless it's already recycled */ \
FL_SET(_old, DVAR_DONT_RECYCLE); \
}\
ruby_dyna_vars = _old; \
} while (0)
#define DVAR_DONT_RECYCLE FL_USER2
#define DMETHOD_P() (ruby_frame->flags & FRAME_DMETH)
static struct RVarmap*
new_dvar(ID id, VALUE value, struct RVarmap *prev)
{
NEWOBJ(vars, struct RVarmap);
OBJSETUP(vars, 0, T_VARMAP);
vars->id = id;
vars->val = value;
vars->next = prev;
return vars;
}
VALUE
rb_dvar_defined(ID id)
{
struct RVarmap *vars = ruby_dyna_vars;
while (vars) {
if (vars->id == id) return Qtrue;
vars = vars->next;
}
return Qfalse;
}
VALUE
rb_dvar_curr(ID id)
{
struct RVarmap *vars = ruby_dyna_vars;
while (vars) {
if (vars->id == 0) break;
if (vars->id == id) return Qtrue;
vars = vars->next;
}
return Qfalse;
}
VALUE
rb_dvar_ref(ID id)
{
struct RVarmap *vars = ruby_dyna_vars;
while (vars) {
if (vars->id == id) {
return vars->val;
}
vars = vars->next;
}
return Qnil;
}
void
rb_dvar_push(ID id, VALUE value)
{
ruby_dyna_vars = new_dvar(id, value, ruby_dyna_vars);
}
static void
dvar_asgn_internal(ID id, VALUE value, int curr)
{
int n = 0;
struct RVarmap *vars = ruby_dyna_vars;
while (vars) {
if (curr && vars->id == 0) {
/* first null is a dvar header */
n++;
if (n == 2) break;
}
if (vars->id == id) {
vars->val = value;
return;
}
vars = vars->next;
}
if (!ruby_dyna_vars) {
ruby_dyna_vars = new_dvar(id, value, 0);
}
else {
vars = new_dvar(id, value, ruby_dyna_vars->next);
ruby_dyna_vars->next = vars;
}
}
static inline void
dvar_asgn(ID id, VALUE value)
{
dvar_asgn_internal(id, value, 0);
}
static inline void
dvar_asgn_curr(ID id, VALUE value)
{
dvar_asgn_internal(id, value, 1);
}
VALUE *
rb_svar(int cnt)
{
struct RVarmap *vars = ruby_dyna_vars;
ID id;
if (!ruby_scope->local_tbl) return NULL;
if (cnt >= ruby_scope->local_tbl[0]) return NULL;
id = ruby_scope->local_tbl[cnt+1];
while (vars) {
if (vars->id == id) return &vars->val;
vars = vars->next;
}
if (ruby_scope->local_vars == 0) return NULL;
return &ruby_scope->local_vars[cnt];
}
struct tag {
rb_jmpbuf_t buf;
struct FRAME *frame;
VALUE tag;
VALUE retval;
struct SCOPE *scope;
VALUE dst;
struct tag *prev;
int blkid;
};
static struct tag *prot_tag;
#define PUSH_TAG(ptag) do { \
struct tag _tag; \
_tag.retval = Qnil; \
_tag.frame = ruby_frame; \
_tag.prev = prot_tag; \
_tag.scope = ruby_scope; \
_tag.tag = ptag; \
_tag.dst = -1; \
_tag.blkid = 0; \
prot_tag = &_tag
#define PROT_NONE Qfalse /* 0 */
#define PROT_THREAD Qtrue /* 2 */
#define PROT_FUNC INT2FIX(0) /* 1 */
#define PROT_LOOP INT2FIX(1) /* 3 */
#define PROT_LAMBDA INT2FIX(2) /* 5 */
#define PROT_YIELD INT2FIX(3) /* 7 */
#define PROT_TOP INT2FIX(4) /* 9 */
#define EXEC_TAG() (FLUSH_REGISTER_WINDOWS, ruby_setjmp(((void)0), prot_tag->buf))
#define JUMP_TAG(st) do { \
ruby_frame = prot_tag->frame; \
ruby_longjmp(prot_tag->buf,(st)); \
} while (0)
#define POP_TAG() \
prot_tag = _tag.prev; \
} while (0)
#define TAG_DST() (_tag.dst == (VALUE)ruby_frame->uniq)
#define TAG_RETURN 0x1
#define TAG_BREAK 0x2
#define TAG_NEXT 0x3
#define TAG_RETRY 0x4
#define TAG_REDO 0x5
#define TAG_RAISE 0x6
#define TAG_THROW 0x7
#define TAG_FATAL 0x8
#define TAG_CONTCALL 0x9
#define TAG_THREAD 0xa
#define TAG_MASK 0xf
VALUE ruby_wrapper; /* security wrapper */
NODE *ruby_cref = 0;
NODE *ruby_top_cref;
#define PUSH_CREF(c) ruby_cref = NEW_NODE(NODE_CREF,(c),0,ruby_cref)
#define POP_CREF() ruby_cref = ruby_cref->nd_next
#define PUSH_SCOPE() do { \
volatile int _vmode = vis_mode; \
struct SCOPE * volatile _old; \
NEWOBJ(_scope, struct SCOPE); \
OBJSETUP(_scope, 0, T_SCOPE); \
_scope->local_tbl = 0; \