-
Notifications
You must be signed in to change notification settings - Fork 1
/
proc.c
2243 lines (2015 loc) · 54.5 KB
/
proc.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
/**********************************************************************
proc.c - Proc, Binding, Env
$Author$
created at: Wed Jan 17 12:13:14 2007
Copyright (C) 2004-2007 Koichi Sasada
**********************************************************************/
#include "eval_intern.h"
#include "gc.h"
struct METHOD {
VALUE recv;
VALUE rclass;
ID id;
rb_method_entry_t me;
};
VALUE rb_cUnboundMethod;
VALUE rb_cMethod;
VALUE rb_cBinding;
VALUE rb_cProc;
VALUE rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc);
static VALUE bmcall(VALUE, VALUE);
static int method_arity(VALUE);
rb_iseq_t *rb_method_get_iseq(VALUE method);
/* Proc */
#define IS_METHOD_PROC_NODE(node) (nd_type(node) == NODE_IFUNC && (node)->nd_cfnc == bmcall)
static void
proc_free(void *ptr)
{
RUBY_FREE_ENTER("proc");
if (ptr) {
ruby_xfree(ptr);
}
RUBY_FREE_LEAVE("proc");
}
static void
proc_mark(void *ptr)
{
rb_proc_t *proc;
RUBY_MARK_ENTER("proc");
if (ptr) {
proc = ptr;
RUBY_MARK_UNLESS_NULL(proc->envval);
RUBY_MARK_UNLESS_NULL(proc->blockprocval);
RUBY_MARK_UNLESS_NULL(proc->block.proc);
RUBY_MARK_UNLESS_NULL(proc->block.self);
if (proc->block.iseq && RUBY_VM_IFUNC_P(proc->block.iseq)) {
RUBY_MARK_UNLESS_NULL((VALUE)(proc->block.iseq));
}
}
RUBY_MARK_LEAVE("proc");
}
static size_t
proc_memsize(const void *ptr)
{
return ptr ? sizeof(rb_proc_t) : 0;
}
static const rb_data_type_t proc_data_type = {
"proc",
{
proc_mark,
proc_free,
proc_memsize,
},
};
VALUE
rb_proc_alloc(VALUE klass)
{
rb_proc_t *proc;
return TypedData_Make_Struct(klass, rb_proc_t, &proc_data_type, proc);
}
VALUE
rb_obj_is_proc(VALUE proc)
{
if (rb_typeddata_is_kind_of(proc, &proc_data_type)) {
return Qtrue;
}
else {
return Qfalse;
}
}
/* :nodoc: */
static VALUE
proc_dup(VALUE self)
{
VALUE procval = rb_proc_alloc(rb_cProc);
rb_proc_t *src, *dst;
GetProcPtr(self, src);
GetProcPtr(procval, dst);
dst->block = src->block;
dst->block.proc = procval;
dst->blockprocval = src->blockprocval;
dst->envval = src->envval;
dst->safe_level = src->safe_level;
dst->is_lambda = src->is_lambda;
return procval;
}
/* :nodoc: */
static VALUE
proc_clone(VALUE self)
{
VALUE procval = proc_dup(self);
CLONESETUP(procval, self);
return procval;
}
/*
* call-seq:
* prc.lambda? -> true or false
*
* Returns true for a Proc object which argument handling is rigid.
* Such procs are typically generated by lambda.
*
* A Proc object generated by proc ignore extra arguments.
*
* proc {|a,b| [a,b] }.call(1,2,3) #=> [1,2]
*
* It provides nil for lacked arguments.
*
* proc {|a,b| [a,b] }.call(1) #=> [1,nil]
*
* It expand single-array argument.
*
* proc {|a,b| [a,b] }.call([1,2]) #=> [1,2]
*
* A Proc object generated by lambda doesn't have such tricks.
*
* lambda {|a,b| [a,b] }.call(1,2,3) #=> ArgumentError
* lambda {|a,b| [a,b] }.call(1) #=> ArgumentError
* lambda {|a,b| [a,b] }.call([1,2]) #=> ArgumentError
*
* Proc#lambda? is a predicate for the tricks.
* It returns true if no tricks.
*
* lambda {}.lambda? #=> true
* proc {}.lambda? #=> false
*
* Proc.new is same as proc.
*
* Proc.new {}.lambda? #=> false
*
* lambda, proc and Proc.new preserves the tricks of
* a Proc object given by & argument.
*
* lambda(&lambda {}).lambda? #=> true
* proc(&lambda {}).lambda? #=> true
* Proc.new(&lambda {}).lambda? #=> true
*
* lambda(&proc {}).lambda? #=> false
* proc(&proc {}).lambda? #=> false
* Proc.new(&proc {}).lambda? #=> false
*
* A Proc object generated by & argument has the tricks
*
* def n(&b) b.lambda? end
* n {} #=> false
*
* The & argument preserves the tricks if a Proc object is given
* by & argument.
*
* n(&lambda {}) #=> true
* n(&proc {}) #=> false
* n(&Proc.new {}) #=> false
*
* A Proc object converted from a method has no tricks.
*
* def m() end
* method(:m).to_proc.lambda? #=> true
*
* n(&method(:m)) #=> true
* n(&method(:m).to_proc) #=> true
*
* define_method is treated same as method definition.
* The defined method has no tricks.
*
* class C
* define_method(:d) {}
* end
* C.new.e(1,2) #=> ArgumentError
* C.new.method(:d).to_proc.lambda? #=> true
*
* define_method always defines a method without the tricks,
* even if a non-lambda Proc object is given.
* This is the only exception which the tricks are not preserved.
*
* class C
* define_method(:e, &proc {})
* end
* C.new.e(1,2) #=> ArgumentError
* C.new.method(:e).to_proc.lambda? #=> true
*
* This exception is for a wrapper of define_method.
* It eases defining a method defining method which defines a usual method which has no tricks.
*
* class << C
* def def2(name, &body)
* define_method(name, &body)
* end
* end
* class C
* def2(:f) {}
* end
* C.new.f(1,2) #=> ArgumentError
*
* The wrapper, def2, defines a method which has no tricks.
*
*/
VALUE
rb_proc_lambda_p(VALUE procval)
{
rb_proc_t *proc;
GetProcPtr(procval, proc);
return proc->is_lambda ? Qtrue : Qfalse;
}
/* Binding */
static void
binding_free(void *ptr)
{
rb_binding_t *bind;
RUBY_FREE_ENTER("binding");
if (ptr) {
bind = ptr;
ruby_xfree(ptr);
}
RUBY_FREE_LEAVE("binding");
}
static void
binding_mark(void *ptr)
{
rb_binding_t *bind;
RUBY_MARK_ENTER("binding");
if (ptr) {
bind = ptr;
RUBY_MARK_UNLESS_NULL(bind->env);
RUBY_MARK_UNLESS_NULL(bind->filename);
}
RUBY_MARK_LEAVE("binding");
}
static size_t
binding_memsize(const void *ptr)
{
return ptr ? sizeof(rb_binding_t) : 0;
}
static const rb_data_type_t binding_data_type = {
"binding",
{
binding_mark,
binding_free,
binding_memsize,
},
};
static VALUE
binding_alloc(VALUE klass)
{
VALUE obj;
rb_binding_t *bind;
obj = TypedData_Make_Struct(klass, rb_binding_t, &binding_data_type, bind);
return obj;
}
/* :nodoc: */
static VALUE
binding_dup(VALUE self)
{
VALUE bindval = binding_alloc(rb_cBinding);
rb_binding_t *src, *dst;
GetBindingPtr(self, src);
GetBindingPtr(bindval, dst);
dst->env = src->env;
dst->filename = src->filename;
dst->line_no = src->line_no;
return bindval;
}
/* :nodoc: */
static VALUE
binding_clone(VALUE self)
{
VALUE bindval = binding_dup(self);
CLONESETUP(bindval, self);
return bindval;
}
VALUE
rb_binding_new(void)
{
rb_thread_t *th = GET_THREAD();
rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(th, th->cfp);
VALUE bindval = binding_alloc(rb_cBinding);
rb_binding_t *bind;
if (cfp == 0) {
rb_raise(rb_eRuntimeError, "Can't create Binding Object on top of Fiber.");
}
GetBindingPtr(bindval, bind);
bind->env = rb_vm_make_env_object(th, cfp);
bind->filename = cfp->iseq->filename;
bind->line_no = rb_vm_get_sourceline(cfp);
return bindval;
}
/*
* call-seq:
* binding -> a_binding
*
* Returns a +Binding+ object, describing the variable and
* method bindings at the point of call. This object can be used when
* calling +eval+ to execute the evaluated command in this
* environment. Also see the description of class +Binding+.
*
* def get_binding(param)
* return binding
* end
* b = get_binding("hello")
* eval("param", b) #=> "hello"
*/
static VALUE
rb_f_binding(VALUE self)
{
return rb_binding_new();
}
/*
* call-seq:
* binding.eval(string [, filename [,lineno]]) -> obj
*
* Evaluates the Ruby expression(s) in <em>string</em>, in the
* <em>binding</em>'s context. If the optional <em>filename</em> and
* <em>lineno</em> parameters are present, they will be used when
* reporting syntax errors.
*
* def get_binding(param)
* return binding
* end
* b = get_binding("hello")
* b.eval("param") #=> "hello"
*/
static VALUE
bind_eval(int argc, VALUE *argv, VALUE bindval)
{
VALUE args[4];
rb_scan_args(argc, argv, "12", &args[0], &args[2], &args[3]);
args[1] = bindval;
return rb_f_eval(argc+1, args, Qnil /* self will be searched in eval */);
}
static VALUE
proc_new(VALUE klass, int is_lambda)
{
VALUE procval = Qnil;
rb_thread_t *th = GET_THREAD();
rb_control_frame_t *cfp = th->cfp;
rb_block_t *block;
if ((GC_GUARDED_PTR_REF(cfp->lfp[0])) != 0) {
block = GC_GUARDED_PTR_REF(cfp->lfp[0]);
}
else {
cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
if ((GC_GUARDED_PTR_REF(cfp->lfp[0])) != 0) {
block = GC_GUARDED_PTR_REF(cfp->lfp[0]);
if (is_lambda) {
rb_warn("tried to create Proc object without a block");
}
}
else {
rb_raise(rb_eArgError,
"tried to create Proc object without a block");
}
}
procval = block->proc;
if (procval) {
if (RBASIC(procval)->klass == klass) {
return procval;
}
else {
VALUE newprocval = proc_dup(procval);
RBASIC(newprocval)->klass = klass;
return newprocval;
}
}
procval = rb_vm_make_proc(th, block, klass);
if (is_lambda) {
rb_proc_t *proc;
GetProcPtr(procval, proc);
proc->is_lambda = TRUE;
}
return procval;
}
/*
* call-seq:
* Proc.new {|...| block } -> a_proc
* Proc.new -> a_proc
*
* Creates a new <code>Proc</code> object, bound to the current
* context. <code>Proc::new</code> may be called without a block only
* within a method with an attached block, in which case that block is
* converted to the <code>Proc</code> object.
*
* def proc_from
* Proc.new
* end
* proc = proc_from { "hello" }
* proc.call #=> "hello"
*/
static VALUE
rb_proc_s_new(int argc, VALUE *argv, VALUE klass)
{
VALUE block = proc_new(klass, FALSE);
rb_obj_call_init(block, argc, argv);
return block;
}
/*
* call-seq:
* proc { |...| block } -> a_proc
*
* Equivalent to <code>Proc.new</code>.
*/
VALUE
rb_block_proc(void)
{
return proc_new(rb_cProc, FALSE);
}
VALUE
rb_block_lambda(void)
{
return proc_new(rb_cProc, TRUE);
}
VALUE
rb_f_lambda(void)
{
rb_warn("rb_f_lambda() is deprecated; use rb_block_proc() instead");
return rb_block_lambda();
}
/*
* call-seq:
* lambda { |...| block } -> a_proc
*
* Equivalent to <code>Proc.new</code>, except the resulting Proc objects
* check the number of parameters passed when called.
*/
static VALUE
proc_lambda(void)
{
return rb_block_lambda();
}
/* CHECKME: are the argument checking semantics correct? */
/*
* call-seq:
* prc.call(params,...) -> obj
* prc[params,...] -> obj
* prc.(params,...) -> obj
*
* Invokes the block, setting the block's parameters to the values in
* <i>params</i> using something close to method calling semantics.
* Generates a warning if multiple values are passed to a proc that
* expects just one (previously this silently converted the parameters
* to an array). Note that prc.() invokes prc.call() with the parameters
* given. It's a syntax sugar to hide "call".
*
* For procs created using <code>Kernel.proc</code>, generates an
* error if the wrong number of parameters
* are passed to a proc with multiple parameters. For procs created using
* <code>Proc.new</code>, extra parameters are silently discarded.
*
* Returns the value of the last expression evaluated in the block. See
* also <code>Proc#yield</code>.
*
* a_proc = Proc.new {|a, *b| b.collect {|i| i*a }}
* a_proc.call(9, 1, 2, 3) #=> [9, 18, 27]
* a_proc[9, 1, 2, 3] #=> [9, 18, 27]
* a_proc = Proc.new {|a,b| a}
* a_proc.call(1,2,3)
*
* <em>produces:</em>
*
* prog.rb:5: wrong number of arguments (3 for 2) (ArgumentError)
* from prog.rb:4:in `call'
* from prog.rb:5
*/
/*
* call-seq:
* prc === obj -> result_of_proc
*
* Invokes the block, with <i>obj</i> as the block's parameter. It is
* to allow a proc object to be a target of +when+ clause in the case statement.
*/
static VALUE
proc_call(int argc, VALUE *argv, VALUE procval)
{
rb_proc_t *proc;
rb_block_t *blockptr = 0;
rb_iseq_t *iseq;
VALUE passed_procval;
GetProcPtr(procval, proc);
iseq = proc->block.iseq;
if (BUILTIN_TYPE(iseq) == T_NODE || iseq->arg_block != -1) {
if (rb_block_given_p()) {
rb_proc_t *passed_proc;
RB_GC_GUARD(passed_procval) = rb_block_proc();
GetProcPtr(passed_procval, passed_proc);
blockptr = &passed_proc->block;
}
}
return rb_vm_invoke_proc(GET_THREAD(), proc, proc->block.self,
argc, argv, blockptr);
}
#if SIZEOF_LONG > SIZEOF_INT
static inline int
check_argc(long argc)
{
if (argc > INT_MAX || argc < 0) {
rb_raise(rb_eArgError, "too many arguments (%lu)",
(unsigned long)argc);
}
return (int)argc;
}
#else
#define check_argc(argc) (argc)
#endif
VALUE
rb_proc_call(VALUE self, VALUE args)
{
rb_proc_t *proc;
GetProcPtr(self, proc);
return rb_vm_invoke_proc(GET_THREAD(), proc, proc->block.self,
check_argc(RARRAY_LEN(args)), RARRAY_PTR(args), 0);
}
VALUE
rb_proc_call_with_block(VALUE self, int argc, VALUE *argv, VALUE pass_procval)
{
rb_proc_t *proc;
rb_block_t *block = 0;
GetProcPtr(self, proc);
if (!NIL_P(pass_procval)) {
rb_proc_t *pass_proc;
GetProcPtr(pass_procval, pass_proc);
block = &pass_proc->block;
}
return rb_vm_invoke_proc(GET_THREAD(), proc, proc->block.self,
argc, argv, block);
}
/*
* call-seq:
* prc.arity -> fixnum
*
* Returns the number of arguments that would not be ignored. If the block
* is declared to take no arguments, returns 0. If the block is known
* to take exactly n arguments, returns n. If the block has optional
* arguments, return -n-1, where n is the number of mandatory
* arguments. A <code>proc</code> with no argument declarations
* is the same a block declaring <code>||</code> as its arguments.
*
* Proc.new {}.arity #=> 0
* Proc.new {||}.arity #=> 0
* Proc.new {|a|}.arity #=> 1
* Proc.new {|a,b|}.arity #=> 2
* Proc.new {|a,b,c|}.arity #=> 3
* Proc.new {|*a|}.arity #=> -1
* Proc.new {|a,*b|}.arity #=> -2
* Proc.new {|a,*b, c|}.arity #=> -3
*/
static VALUE
proc_arity(VALUE self)
{
int arity = rb_proc_arity(self);
return INT2FIX(arity);
}
int
rb_proc_arity(VALUE self)
{
rb_proc_t *proc;
rb_iseq_t *iseq;
GetProcPtr(self, proc);
iseq = proc->block.iseq;
if (iseq) {
if (BUILTIN_TYPE(iseq) != T_NODE) {
if (iseq->arg_rest < 0) {
return iseq->argc;
}
else {
return -(iseq->argc + 1 + iseq->arg_post_len);
}
}
else {
NODE *node = (NODE *)iseq;
if (IS_METHOD_PROC_NODE(node)) {
/* method(:foo).to_proc.arity */
return method_arity(node->nd_tval);
}
}
}
return -1;
}
#define get_proc_iseq rb_proc_get_iseq
rb_iseq_t *
rb_proc_get_iseq(VALUE self, int *is_proc)
{
rb_proc_t *proc;
rb_iseq_t *iseq;
GetProcPtr(self, proc);
iseq = proc->block.iseq;
if (is_proc) *is_proc = !proc->is_lambda;
if (!RUBY_VM_NORMAL_ISEQ_P(iseq)) {
NODE *node = (NODE *)iseq;
iseq = 0;
if (IS_METHOD_PROC_NODE(node)) {
/* method(:foo).to_proc */
iseq = rb_method_get_iseq(node->nd_tval);
if (is_proc) *is_proc = 0;
}
}
return iseq;
}
static VALUE
iseq_location(rb_iseq_t *iseq)
{
VALUE loc[2];
if (!iseq) return Qnil;
loc[0] = iseq->filename;
if (iseq->insn_info_table) {
loc[1] = INT2FIX(rb_iseq_first_lineno(iseq));
}
else {
loc[1] = Qnil;
}
return rb_ary_new4(2, loc);
}
/*
* call-seq:
* prc.source_location -> [String, Fixnum]
*
* returns the ruby source filename and line number containing this proc
* or nil if this proc was not defined in ruby (i.e. native)
*/
VALUE
rb_proc_location(VALUE self)
{
return iseq_location(get_proc_iseq(self, 0));
}
static VALUE
unnamed_parameters(int arity)
{
VALUE a, param = rb_ary_new2((arity < 0) ? -arity : arity);
int n = (arity < 0) ? ~arity : arity;
ID req, rest;
CONST_ID(req, "req");
a = rb_ary_new3(1, ID2SYM(req));
OBJ_FREEZE(a);
for (; n; --n) {
rb_ary_push(param, a);
}
if (arity < 0) {
CONST_ID(rest, "rest");
rb_ary_store(param, ~arity, rb_ary_new3(1, ID2SYM(rest)));
}
return param;
}
/*
* call-seq:
* proc.parameters -> array
*
* returns the parameter information of this proc.
*
* prc = lambda{|x, y=42, *rest|}
* prc.parameters #=> [[:req, :x], [:opt, :y], [:rest, :rest]]
*/
static VALUE
rb_proc_parameters(VALUE self)
{
int is_proc;
rb_iseq_t *iseq = get_proc_iseq(self, &is_proc);
if (!iseq) {
return unnamed_parameters(rb_proc_arity(self));
}
return rb_iseq_parameters(iseq, is_proc);
}
/*
* call-seq:
* prc == other_proc -> true or false
*
* Return <code>true</code> if <i>prc</i> is the same object as
* <i>other_proc</i>, or if they are both procs with the same body.
*/
static VALUE
proc_eq(VALUE self, VALUE other)
{
if (self == other) {
return Qtrue;
}
else {
if (rb_obj_is_proc(other)) {
rb_proc_t *p1, *p2;
GetProcPtr(self, p1);
GetProcPtr(other, p2);
if (p1->envval == p2->envval &&
p1->block.iseq->iseq_size == p2->block.iseq->iseq_size &&
p1->block.iseq->local_size == p2->block.iseq->local_size &&
MEMCMP(p1->block.iseq->iseq, p2->block.iseq->iseq, VALUE,
p1->block.iseq->iseq_size) == 0) {
return Qtrue;
}
}
}
return Qfalse;
}
/*
* call-seq:
* prc.hash -> integer
*
* Return hash value corresponding to proc body.
*/
static VALUE
proc_hash(VALUE self)
{
st_index_t hash;
rb_proc_t *proc;
GetProcPtr(self, proc);
hash = rb_hash_start((st_index_t)proc->block.iseq);
hash = rb_hash_uint(hash, (st_index_t)proc->envval);
hash = rb_hash_uint(hash, (st_index_t)proc->block.lfp >> 16);
hash = rb_hash_end(hash);
return LONG2FIX(hash);
}
/*
* call-seq:
* prc.to_s -> string
*
* Shows the unique identifier for this proc, along with
* an indication of where the proc was defined.
*/
static VALUE
proc_to_s(VALUE self)
{
VALUE str = 0;
rb_proc_t *proc;
const char *cname = rb_obj_classname(self);
rb_iseq_t *iseq;
const char *is_lambda;
GetProcPtr(self, proc);
iseq = proc->block.iseq;
is_lambda = proc->is_lambda ? " (lambda)" : "";
if (RUBY_VM_NORMAL_ISEQ_P(iseq)) {
int line_no = 0;
if (iseq->insn_info_table) {
line_no = rb_iseq_first_lineno(iseq);
}
str = rb_sprintf("#<%s:%p@%s:%d%s>", cname, (void *)self,
RSTRING_PTR(iseq->filename),
line_no, is_lambda);
}
else {
str = rb_sprintf("#<%s:%p%s>", cname, (void *)proc->block.iseq,
is_lambda);
}
if (OBJ_TAINTED(self)) {
OBJ_TAINT(str);
}
return str;
}
/*
* call-seq:
* prc.to_proc -> prc
*
* Part of the protocol for converting objects to <code>Proc</code>
* objects. Instances of class <code>Proc</code> simply return
* themselves.
*/
static VALUE
proc_to_proc(VALUE self)
{
return self;
}
static void
bm_mark(void *ptr)
{
struct METHOD *data = ptr;
rb_gc_mark(data->rclass);
rb_gc_mark(data->recv);
rb_mark_method_entry(&data->me);
}
static void
bm_free(void *ptr)
{
struct METHOD *data = ptr;
rb_method_definition_t *def = data->me.def;
if (def->alias_count == 0)
xfree(def);
else if (def->alias_count > 0)
def->alias_count--;
xfree(ptr);
}
static size_t
bm_memsize(const void *ptr)
{
return ptr ? sizeof(struct METHOD) : 0;
}
static const rb_data_type_t method_data_type = {
"method",
{
bm_mark,
bm_free,
bm_memsize,
},
};
VALUE
rb_obj_is_method(VALUE m)
{
if (rb_typeddata_is_kind_of(m, &method_data_type)) {
return Qtrue;
}
else {
return Qfalse;
}
}
static VALUE
mnew(VALUE klass, VALUE obj, ID id, VALUE mclass, int scope)
{
VALUE method;
VALUE rclass = klass;
ID rid = id;
struct METHOD *data;
rb_method_entry_t *me, meb;
rb_method_definition_t *def = 0;
rb_method_flag_t flag = NOEX_UNDEF;
again:
me = rb_method_entry(klass, id);
if (UNDEFINED_METHOD_ENTRY_P(me)) {
ID rmiss = rb_intern("respond_to_missing?");
VALUE sym = ID2SYM(id);
if (obj != Qundef && !rb_method_basic_definition_p(klass, rmiss)) {
if (RTEST(rb_funcall(obj, rmiss, 2, sym, scope ? Qfalse : Qtrue))) {
def = ALLOC(rb_method_definition_t);
def->type = VM_METHOD_TYPE_MISSING;
def->original_id = id;
def->alias_count = 0;
meb.flag = 0;
meb.mark = 0;
meb.called_id = id;
meb.klass = klass;
meb.def = def;
me = &meb;
def = 0;
goto gen_method;
}
}
rb_print_undef(klass, id, 0);
}
def = me->def;
if (flag == NOEX_UNDEF) {
flag = me->flag;
if (scope && (flag & NOEX_MASK) != NOEX_PUBLIC) {
const char *v = "";
switch (flag & NOEX_MASK) {
case NOEX_PRIVATE: v = "private"; break;
case NOEX_PROTECTED: v = "protected"; break;
}
rb_name_error(id, "method `%s' for %s `%s' is %s",
rb_id2name(id),
(TYPE(klass) == T_MODULE) ? "module" : "class",
rb_class2name(klass),
v);
}
}
if (def && def->type == VM_METHOD_TYPE_ZSUPER) {
klass = RCLASS_SUPER(me->klass);
id = def->original_id;
goto again;
}
klass = me->klass;
while (rclass != klass &&
(FL_TEST(rclass, FL_SINGLETON) || TYPE(rclass) == T_ICLASS)) {
rclass = RCLASS_SUPER(rclass);
}
if (TYPE(klass) == T_ICLASS) {
klass = RBASIC(klass)->klass;
}
gen_method:
method = TypedData_Make_Struct(mclass, struct METHOD, &method_data_type, data);
data->recv = obj;
data->rclass = rclass;
data->id = rid;
data->me = *me;
if (def) def->alias_count++;
OBJ_INFECT(method, klass);
return method;
}
/**********************************************************************
*
* Document-class : Method
*
* Method objects are created by <code>Object#method</code>, and are
* associated with a particular object (not just with a class). They
* may be used to invoke the method within the object, and as a block
* associated with an iterator. They may also be unbound from one
* object (creating an <code>UnboundMethod</code>) and bound to
* another.