-
Notifications
You must be signed in to change notification settings - Fork 7
/
proc.c
2713 lines (2439 loc) · 66.9 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 "internal.h"
#include "gc.h"
#include "iseq.h"
struct METHOD {
VALUE recv;
VALUE rclass;
VALUE defined_class;
ID id;
rb_method_entry_t *me;
struct unlinked_method_entry_list_entry *ume;
};
VALUE rb_cUnboundMethod;
VALUE rb_cMethod;
VALUE rb_cBinding;
VALUE rb_cProc;
static VALUE bmcall(VALUE, VALUE, int, VALUE *, VALUE);
static int method_arity(VALUE);
static int method_min_max_arity(VALUE, int *max);
#define attached id__attached__
/* 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 for which argument handling is rigid.
* Such procs are typically generated by +lambda+.
*
* A Proc object generated by +proc+ ignores extra arguments.
*
* proc {|a,b| [a,b] }.call(1,2,3) #=> [1,2]
*
* It provides +nil+ for missing arguments.
*
* proc {|a,b| [a,b] }.call(1) #=> [1,nil]
*
* It expands a 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 apply.
*
* lambda {}.lambda? #=> true
* proc {}.lambda? #=> false
*
* Proc.new is the same as +proc+.
*
* Proc.new {}.lambda? #=> false
*
* +lambda+, +proc+ and Proc.new preserve the tricks of
* a Proc object given by <code>&</code> 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 <code>&</code> argument has the tricks
*
* def n(&b) b.lambda? end
* n {} #=> false
*
* The <code>&</code> argument preserves the tricks if a Proc object
* is given by <code>&</code> 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 the same as method definition.
* The defined method has no tricks.
*
* class C
* define_method(:d) {}
* end
* C.new.d(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 for 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 insures that methods never have tricks
* and makes it easy to have wrappers to define methods that behave as usual.
*
* class C
* def self.def2(name, &body)
* define_method(name, &body)
* end
*
* def2(:f) {}
* end
* C.new.f(1,2) #=> ArgumentError
*
* The wrapper <i>def2</i> 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(bind);
}
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->path);
}
RUBY_MARK_LEAVE("binding");
}
static size_t
binding_memsize(const void *ptr)
{
return ptr ? sizeof(rb_binding_t) : 0;
}
const rb_data_type_t ruby_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, &ruby_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->path = src->path;
dst->first_lineno = src->first_lineno;
return bindval;
}
/* :nodoc: */
static VALUE
binding_clone(VALUE self)
{
VALUE bindval = binding_dup(self);
CLONESETUP(bindval, self);
return bindval;
}
VALUE
rb_binding_new_with_cfp(rb_thread_t *th, const rb_control_frame_t *src_cfp)
{
rb_control_frame_t *cfp = rb_vm_get_binding_creatable_next_cfp(th, src_cfp);
rb_control_frame_t *ruby_level_cfp = rb_vm_get_ruby_level_next_cfp(th, src_cfp);
VALUE bindval, envval;
rb_binding_t *bind;
if (cfp == 0 || ruby_level_cfp == 0) {
rb_raise(rb_eRuntimeError, "Can't create Binding Object on top of Fiber.");
}
while (1) {
envval = rb_vm_make_env_object(th, cfp);
if (cfp == ruby_level_cfp) {
break;
}
cfp = rb_vm_get_binding_creatable_next_cfp(th, RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp));
}
bindval = binding_alloc(rb_cBinding);
GetBindingPtr(bindval, bind);
bind->env = envval;
bind->path = ruby_level_cfp->iseq->location.path;
bind->first_lineno = rb_vm_get_sourceline(ruby_level_cfp);
return bindval;
}
VALUE
rb_binding_new(void)
{
rb_thread_t *th = GET_THREAD();
return rb_binding_new_with_cfp(th, th->cfp);
}
/*
* 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. See also 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 *
get_local_variable_ptr(VALUE envval, ID lid)
{
const rb_env_t *env;
do {
const rb_iseq_t *iseq;
int i;
GetEnvPtr(envval, env);
iseq = env->block.iseq;
for (i=0; i<iseq->local_table_size; i++) {
if (iseq->local_table[i] == lid) {
return &env->env[i];
}
}
} while ((envval = env->prev_envval) != 0);
return 0;
}
/*
* check local variable name.
* returns ID if it's an already interned symbol, or 0 with setting
* local name in String to *namep.
*/
static ID
check_local_id(VALUE bindval, volatile VALUE *pname)
{
ID lid = rb_check_id(pname);
VALUE name = *pname, sym = name;
if (lid) {
if (!rb_is_local_id(lid)) {
name = rb_id2str(lid);
wrong:
rb_name_error_str(sym, "wrong local variable name `% "PRIsVALUE"' for %"PRIsVALUE,
name, bindval);
}
}
else {
if (!rb_is_local_name(sym)) goto wrong;
return 0;
}
return lid;
}
/*
* call-seq:
* binding.local_variable_get(symbol) -> obj
*
* Returns a +value+ of local variable +symbol+.
*
* def foo
* a = 1
* binding.local_variable_get(:a) #=> 1
* binding.local_variable_get(:b) #=> NameError
* end
*
* This method is short version of the following code.
*
* binding.eval("#{symbol}")
*
*/
static VALUE
bind_local_variable_get(VALUE bindval, VALUE sym)
{
ID lid = check_local_id(bindval, &sym);
const rb_binding_t *bind;
const VALUE *ptr;
if (!lid) goto undefined;
GetBindingPtr(bindval, bind);
if ((ptr = get_local_variable_ptr(bind->env, lid)) == NULL) {
undefined:
rb_name_error_str(sym, "local variable `%"PRIsVALUE"' not defined for %"PRIsVALUE,
sym, bindval);
}
return *ptr;
}
/*
* call-seq:
* binding.local_variable_set(symbol, obj) -> obj
*
* Set local variable named +symbol+ as +obj+.
*
* def foo
* a = 1
* b = binding
* b.local_variable_set(:a, 2) # set existing local variable `a'
* b.local_variable_set(:b, 3) # create new local variable `b'
* # `b' exists only in binding.
* b.local_variable_get(:a) #=> 2
* b.local_variable_get(:b) #=> 3
* p a #=> 2
* p b #=> NameError
* end
*
* This method is a similar behavior of the following code
*
* binding.eval("#{symbol} = #{obj}")
*
* if obj can be dumped in Ruby code.
*/
static VALUE
bind_local_variable_set(VALUE bindval, VALUE sym, VALUE val)
{
ID lid = check_local_id(bindval, &sym);
rb_binding_t *bind;
VALUE *ptr;
if (!lid) lid = rb_intern_str(sym);
GetBindingPtr(bindval, bind);
if ((ptr = get_local_variable_ptr(bind->env, lid)) == NULL) {
/* not found. create new env */
ptr = rb_binding_add_dynavars(bind, 1, &lid);
}
*ptr = val;
return val;
}
/*
* call-seq:
* binding.local_variable_defined?(symbol) -> obj
*
* Returns a +true+ if a local variable +symbol+ exists.
*
* def foo
* a = 1
* binding.local_variable_defined?(:a) #=> true
* binding.local_variable_defined?(:b) #=> false
* end
*
* This method is short version of the following code.
*
* binding.eval("defined?(#{symbol}) == 'local-variable'")
*
*/
static VALUE
bind_local_variable_defined_p(VALUE bindval, VALUE sym)
{
ID lid = check_local_id(bindval, &sym);
const rb_binding_t *bind;
if (!lid) return Qfalse;
GetBindingPtr(bindval, bind);
return get_local_variable_ptr(bind->env, lid) ? Qtrue : Qfalse;
}
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 ((block = rb_vm_control_frame_block_ptr(cfp)) != 0) {
/* block found */
}
else {
cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
if ((block = rb_vm_control_frame_block_ptr(cfp)) != 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_SET_CLASS(newprocval, 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);
}
/*
* 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.
*/
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();
}
/* Document-method: ===
*
* call-seq:
* proc === obj -> result_of_proc
*
* Invokes the block with +obj+ as the proc's parameter like Proc#call. It
* is to allow a proc object to be a target of +when+ clause in a case
* statement.
*/
/* 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>lambda</code> or <code>->()</code> an error
* is generated if the wrong number of parameters are passed to a Proc with
* multiple parameters. For procs created using <code>Proc.new</code> or
* <code>Kernel.proc</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 = lambda {|a,b| a}
* a_proc.call(1,2,3)
*
* <em>produces:</em>
*
* prog.rb:4:in `block in <main>': wrong number of arguments (3 for 2) (ArgumentError)
* from prog.rb:5:in `call'
* from prog.rb:5:in `<main>'
*
*/
static VALUE
proc_call(int argc, VALUE *argv, VALUE procval)
{
VALUE vret;
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;
}
}
vret = rb_vm_invoke_proc(GET_THREAD(), proc, argc, argv, blockptr);
RB_GC_GUARD(procval);
return vret;
}
#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)
{
VALUE vret;
rb_proc_t *proc;
GetProcPtr(self, proc);
vret = rb_vm_invoke_proc(GET_THREAD(), proc, check_argc(RARRAY_LEN(args)), RARRAY_CONST_PTR(args), 0);
RB_GC_GUARD(self);
RB_GC_GUARD(args);
return vret;
}
VALUE
rb_proc_call_with_block(VALUE self, int argc, const VALUE *argv, VALUE pass_procval)
{
VALUE vret;
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;
}
vret = rb_vm_invoke_proc(GET_THREAD(), proc, argc, argv, block);
RB_GC_GUARD(self);
RB_GC_GUARD(pass_procval);
return vret;
}
/*
* 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 {}.arity #=> 0
* proc {||}.arity #=> 0
* proc {|a|}.arity #=> 1
* proc {|a,b|}.arity #=> 2
* proc {|a,b,c|}.arity #=> 3
* proc {|*a|}.arity #=> -1
* proc {|a,*b|}.arity #=> -2
* proc {|a,*b, c|}.arity #=> -3
*
* proc { |x = 0| }.arity #=> 0
* lambda { |a = 0| }.arity #=> -1
* proc { |x=0, y| }.arity #=> 1
* lambda { |x=0, y| }.arity #=> -2
* proc { |x=0, y=0| }.arity #=> 0
* lambda { |x=0, y=0| }.arity #=> -1
* proc { |x, y=0| }.arity #=> 1
* lambda { |x, y=0| }.arity #=> -2
* proc { |(x, y), z=0| }.arity #=> 1
* lambda { |(x, y), z=0| }.arity #=> -2
*/
static VALUE
proc_arity(VALUE self)
{
int arity = rb_proc_arity(self);
return INT2FIX(arity);
}
static inline int
rb_iseq_min_max_arity(const rb_iseq_t *iseq, int *max)
{
*max = iseq->arg_rest == -1 ?
iseq->argc + iseq->arg_post_len + iseq->arg_opts - (iseq->arg_opts > 0)
: UNLIMITED_ARGUMENTS;
return iseq->argc + iseq->arg_post_len;
}
static int
rb_block_min_max_arity(rb_block_t *block, int *max)
{
rb_iseq_t *iseq = block->iseq;
if (iseq) {
if (BUILTIN_TYPE(iseq) != T_NODE) {
return rb_iseq_min_max_arity(iseq, max);
}
else {
NODE *node = (NODE *)iseq;
if (IS_METHOD_PROC_NODE(node)) {
/* e.g. method(:foo).to_proc.arity */
return method_min_max_arity(node->nd_tval, max);
}
}
}
*max = UNLIMITED_ARGUMENTS;
return 0;
}
/*
* Returns the number of required parameters and stores the maximum
* number of parameters in max, or UNLIMITED_ARGUMENTS if no max.
* For non-lambda procs, the maximum is the number of non-ignored
* parameters even though there is no actual limit to the number of parameters
*/
static int
rb_proc_min_max_arity(VALUE self, int *max)
{
rb_proc_t *proc;
rb_block_t *block;
GetProcPtr(self, proc);
block = &proc->block;
return rb_block_min_max_arity(block, max);
}
int
rb_proc_arity(VALUE self)
{
rb_proc_t *proc;
int max, min = rb_proc_min_max_arity(self, &max);
GetProcPtr(self, proc);
return (proc->is_lambda ? min == max : max != UNLIMITED_ARGUMENTS) ? min : -min-1;
}
int
rb_block_arity(void)
{
int min, max;
rb_thread_t *th = GET_THREAD();
rb_control_frame_t *cfp = th->cfp;
rb_block_t *block = rb_vm_control_frame_block_ptr(cfp);
VALUE proc_value;
if (!block) rb_raise(rb_eArgError, "no block given");
min = rb_block_min_max_arity(block, &max);
proc_value = block->proc;
if (proc_value) {
rb_proc_t *proc;
GetProcPtr(proc_value, proc);
if (proc)
return (proc->is_lambda ? min == max : max != UNLIMITED_ARGUMENTS) ? min : -min-1;
}
return max != UNLIMITED_ARGUMENTS ? min : -min-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->location.path;
if (iseq->line_info_table) {
loc[1] = rb_iseq_first_lineno(iseq->self);
}
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:
* prc.parameters -> array
*
* Returns the parameter information of this proc.
*
* prc = lambda{|x, y=42, *other|}
* prc.parameters #=> [[:req, :x], [:opt, :y], [:rest, :other]]
*/
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);
}
st_index_t
rb_hash_proc(st_index_t hash, VALUE prc)
{
rb_proc_t *proc;
GetProcPtr(prc, proc);
hash = rb_hash_uint(hash, (st_index_t)proc->block.iseq);
hash = rb_hash_uint(hash, (st_index_t)proc->envval);
return rb_hash_uint(hash, (st_index_t)proc->block.ep >> 16);
}
/*
* call-seq: