forked from ruby/ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proc.c
4440 lines (4007 loc) · 123 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"
#include "internal.h"
#include "internal/class.h"
#include "internal/error.h"
#include "internal/eval.h"
#include "internal/object.h"
#include "internal/proc.h"
#include "internal/symbol.h"
#include "method.h"
#include "iseq.h"
#include "vm_core.h"
#include "yjit.h"
#if !defined(__GNUC__) || __GNUC__ < 5 || defined(__MINGW32__)
# define NO_CLOBBERED(v) (*(volatile VALUE *)&(v))
#else
# define NO_CLOBBERED(v) (v)
#endif
#define UPDATE_TYPED_REFERENCE(_type, _ref) *(_type*)&_ref = (_type)rb_gc_location((VALUE)_ref)
#define UPDATE_REFERENCE(_ref) UPDATE_TYPED_REFERENCE(VALUE, _ref)
const rb_cref_t *rb_vm_cref_in_context(VALUE self, VALUE cbase);
struct METHOD {
const VALUE recv;
const VALUE klass;
const VALUE iclass;
const rb_method_entry_t * const me;
/* for bound methods, `me' should be rb_callable_method_entry_t * */
rb_method_visibility_t visibility;
};
VALUE rb_cUnboundMethod;
VALUE rb_cMethod;
VALUE rb_cBinding;
VALUE rb_cProc;
static rb_block_call_func bmcall;
static int method_arity(VALUE);
static int method_min_max_arity(VALUE, int *max);
static VALUE proc_binding(VALUE self);
#define attached id__attached__
/* Proc */
#define IS_METHOD_PROC_IFUNC(ifunc) ((ifunc)->func == bmcall)
/* :FIXME: The way procs are cloned has been historically different from the
* way everything else are. @shyouhei is not sure for the intention though.
*/
#undef CLONESETUP
static inline void
CLONESETUP(VALUE clone, VALUE obj)
{
RBIMPL_ASSERT_OR_ASSUME(! RB_SPECIAL_CONST_P(obj));
RBIMPL_ASSERT_OR_ASSUME(! RB_SPECIAL_CONST_P(clone));
const VALUE flags = RUBY_FL_PROMOTED0 | RUBY_FL_PROMOTED1 | RUBY_FL_FINALIZE;
rb_obj_setup(clone, rb_singleton_class_clone(obj),
RB_FL_TEST_RAW(obj, ~flags));
rb_singleton_class_attached(RBASIC_CLASS(clone), clone);
if (RB_FL_TEST(obj, RUBY_FL_EXIVAR)) rb_copy_generic_ivar(clone, obj);
}
static void
block_mark(const struct rb_block *block)
{
switch (vm_block_type(block)) {
case block_type_iseq:
case block_type_ifunc:
{
const struct rb_captured_block *captured = &block->as.captured;
RUBY_MARK_MOVABLE_UNLESS_NULL(captured->self);
RUBY_MARK_MOVABLE_UNLESS_NULL((VALUE)captured->code.val);
if (captured->ep && captured->ep[VM_ENV_DATA_INDEX_ENV] != Qundef /* cfunc_proc_t */) {
rb_gc_mark(VM_ENV_ENVVAL(captured->ep));
}
}
break;
case block_type_symbol:
RUBY_MARK_MOVABLE_UNLESS_NULL(block->as.symbol);
break;
case block_type_proc:
RUBY_MARK_MOVABLE_UNLESS_NULL(block->as.proc);
break;
}
}
static void
block_compact(struct rb_block *block)
{
switch (block->type) {
case block_type_iseq:
case block_type_ifunc:
{
struct rb_captured_block *captured = &block->as.captured;
captured->self = rb_gc_location(captured->self);
captured->code.val = rb_gc_location(captured->code.val);
}
break;
case block_type_symbol:
block->as.symbol = rb_gc_location(block->as.symbol);
break;
case block_type_proc:
block->as.proc = rb_gc_location(block->as.proc);
break;
}
}
static void
proc_compact(void *ptr)
{
rb_proc_t *proc = ptr;
block_compact((struct rb_block *)&proc->block);
}
static void
proc_mark(void *ptr)
{
rb_proc_t *proc = ptr;
block_mark(&proc->block);
RUBY_MARK_LEAVE("proc");
}
typedef struct {
rb_proc_t basic;
VALUE env[VM_ENV_DATA_SIZE + 1]; /* ..., envval */
} cfunc_proc_t;
static size_t
proc_memsize(const void *ptr)
{
const rb_proc_t *proc = ptr;
if (proc->block.as.captured.ep == ((const cfunc_proc_t *)ptr)->env+1)
return sizeof(cfunc_proc_t);
return sizeof(rb_proc_t);
}
static const rb_data_type_t proc_data_type = {
"proc",
{
proc_mark,
RUBY_TYPED_DEFAULT_FREE,
proc_memsize,
proc_compact,
},
0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
};
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)
{
return RBOOL(rb_typeddata_is_kind_of(proc, &proc_data_type));
}
/* :nodoc: */
static VALUE
proc_clone(VALUE self)
{
VALUE procval = rb_proc_dup(self);
CLONESETUP(procval, self);
return procval;
}
/*
* call-seq:
* prc.lambda? -> true or false
*
* Returns +true+ if a Proc object is lambda.
* +false+ if non-lambda.
*
* The lambda-ness affects argument handling and the behavior of +return+ and +break+.
*
* 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 ensures 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 RBOOL(proc->is_lambda);
}
/* Binding */
static void
binding_free(void *ptr)
{
RUBY_FREE_ENTER("binding");
ruby_xfree(ptr);
RUBY_FREE_LEAVE("binding");
}
static void
binding_mark(void *ptr)
{
rb_binding_t *bind = ptr;
RUBY_MARK_ENTER("binding");
block_mark(&bind->block);
rb_gc_mark_movable(bind->pathobj);
RUBY_MARK_LEAVE("binding");
}
static void
binding_compact(void *ptr)
{
rb_binding_t *bind = ptr;
block_compact((struct rb_block *)&bind->block);
UPDATE_REFERENCE(bind->pathobj);
}
static size_t
binding_memsize(const void *ptr)
{
return sizeof(rb_binding_t);
}
const rb_data_type_t ruby_binding_data_type = {
"binding",
{
binding_mark,
binding_free,
binding_memsize,
binding_compact,
},
0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY
};
VALUE
rb_binding_alloc(VALUE klass)
{
VALUE obj;
rb_binding_t *bind;
obj = TypedData_Make_Struct(klass, rb_binding_t, &ruby_binding_data_type, bind);
#if YJIT_STATS
rb_yjit_collect_binding_alloc();
#endif
return obj;
}
/* :nodoc: */
static VALUE
binding_dup(VALUE self)
{
VALUE bindval = rb_binding_alloc(rb_cBinding);
rb_binding_t *src, *dst;
GetBindingPtr(self, src);
GetBindingPtr(bindval, dst);
rb_vm_block_copy(bindval, &dst->block, &src->block);
RB_OBJ_WRITE(bindval, &dst->pathobj, src->pathobj);
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(void)
{
rb_execution_context_t *ec = GET_EC();
return rb_vm_make_binding(ec, ec->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)
* 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)
* 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 const VALUE *
get_local_variable_ptr(const rb_env_t **envp, ID lid)
{
const rb_env_t *env = *envp;
do {
if (!VM_ENV_FLAGS(env->ep, VM_FRAME_FLAG_CFRAME)) {
if (VM_ENV_FLAGS(env->ep, VM_ENV_FLAG_ISOLATED)) {
return NULL;
}
const rb_iseq_t *iseq = env->iseq;
unsigned int i;
VM_ASSERT(rb_obj_is_iseq((VALUE)iseq));
for (i=0; i<ISEQ_BODY(iseq)->local_table_size; i++) {
if (ISEQ_BODY(iseq)->local_table[i] == lid) {
if (ISEQ_BODY(iseq)->local_iseq == iseq &&
ISEQ_BODY(iseq)->param.flags.has_block &&
(unsigned int)ISEQ_BODY(iseq)->param.block_start == i) {
const VALUE *ep = env->ep;
if (!VM_ENV_FLAGS(ep, VM_FRAME_FLAG_MODIFIED_BLOCK_PARAM)) {
RB_OBJ_WRITE(env, &env->env[i], rb_vm_bh_to_procval(GET_EC(), VM_ENV_BLOCK_HANDLER(ep)));
VM_ENV_FLAGS_SET(ep, VM_FRAME_FLAG_MODIFIED_BLOCK_PARAM);
}
}
*envp = env;
return &env->env[i];
}
}
}
else {
*envp = NULL;
return NULL;
}
} while ((env = rb_vm_env_prev_env(env)) != NULL);
*envp = NULL;
return NULL;
}
/*
* 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;
if (lid) {
if (!rb_is_local_id(lid)) {
rb_name_err_raise("wrong local variable name `%1$s' for %2$s",
bindval, ID2SYM(lid));
}
}
else {
if (!rb_is_local_name(name)) {
rb_name_err_raise("wrong local variable name `%1$s' for %2$s",
bindval, name);
}
return 0;
}
return lid;
}
/*
* call-seq:
* binding.local_variables -> Array
*
* Returns the names of the binding's local variables as symbols.
*
* def foo
* a = 1
* 2.times do |n|
* binding.local_variables #=> [:a, :n]
* end
* end
*
* This method is the short version of the following code:
*
* binding.eval("local_variables")
*
*/
static VALUE
bind_local_variables(VALUE bindval)
{
const rb_binding_t *bind;
const rb_env_t *env;
GetBindingPtr(bindval, bind);
env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
return rb_vm_env_local_variables(env);
}
/*
* call-seq:
* binding.local_variable_get(symbol) -> obj
*
* Returns the value of the local variable +symbol+.
*
* def foo
* a = 1
* binding.local_variable_get(:a) #=> 1
* binding.local_variable_get(:b) #=> NameError
* end
*
* This method is the 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;
const rb_env_t *env;
if (!lid) goto undefined;
GetBindingPtr(bindval, bind);
env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
if ((ptr = get_local_variable_ptr(&env, lid)) != NULL) {
return *ptr;
}
sym = ID2SYM(lid);
undefined:
rb_name_err_raise("local variable `%1$s' is not defined for %2$s",
bindval, sym);
UNREACHABLE_RETURN(Qundef);
}
/*
* call-seq:
* binding.local_variable_set(symbol, obj) -> obj
*
* Set local variable named +symbol+ as +obj+.
*
* def foo
* a = 1
* bind = binding
* bind.local_variable_set(:a, 2) # set existing local variable `a'
* bind.local_variable_set(:b, 3) # create new local variable `b'
* # `b' exists only in binding
*
* p bind.local_variable_get(:a) #=> 2
* p bind.local_variable_get(:b) #=> 3
* p a #=> 2
* p b #=> NameError
* end
*
* This method behaves similarly to 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;
const VALUE *ptr;
const rb_env_t *env;
if (!lid) lid = rb_intern_str(sym);
GetBindingPtr(bindval, bind);
env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
if ((ptr = get_local_variable_ptr(&env, lid)) == NULL) {
/* not found. create new env */
ptr = rb_binding_add_dynavars(bindval, bind, 1, &lid);
env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
}
#if YJIT_STATS
rb_yjit_collect_binding_set();
#endif
RB_OBJ_WRITE(env, ptr, val);
return val;
}
/*
* call-seq:
* binding.local_variable_defined?(symbol) -> obj
*
* Returns +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 the 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;
const rb_env_t *env;
if (!lid) return Qfalse;
GetBindingPtr(bindval, bind);
env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
return RBOOL(get_local_variable_ptr(&env, lid));
}
/*
* call-seq:
* binding.receiver -> object
*
* Returns the bound receiver of the binding object.
*/
static VALUE
bind_receiver(VALUE bindval)
{
const rb_binding_t *bind;
GetBindingPtr(bindval, bind);
return vm_block_self(&bind->block);
}
/*
* call-seq:
* binding.source_location -> [String, Integer]
*
* Returns the Ruby source filename and line number of the binding object.
*/
static VALUE
bind_location(VALUE bindval)
{
VALUE loc[2];
const rb_binding_t *bind;
GetBindingPtr(bindval, bind);
loc[0] = pathobj_path(bind->pathobj);
loc[1] = INT2FIX(bind->first_lineno);
return rb_ary_new4(2, loc);
}
static VALUE
cfunc_proc_new(VALUE klass, VALUE ifunc)
{
rb_proc_t *proc;
cfunc_proc_t *sproc;
VALUE procval = TypedData_Make_Struct(klass, cfunc_proc_t, &proc_data_type, sproc);
VALUE *ep;
proc = &sproc->basic;
vm_block_type_set(&proc->block, block_type_ifunc);
*(VALUE **)&proc->block.as.captured.ep = ep = sproc->env + VM_ENV_DATA_SIZE-1;
ep[VM_ENV_DATA_INDEX_FLAGS] = VM_FRAME_MAGIC_IFUNC | VM_FRAME_FLAG_CFRAME | VM_ENV_FLAG_LOCAL | VM_ENV_FLAG_ESCAPED;
ep[VM_ENV_DATA_INDEX_ME_CREF] = Qfalse;
ep[VM_ENV_DATA_INDEX_SPECVAL] = VM_BLOCK_HANDLER_NONE;
ep[VM_ENV_DATA_INDEX_ENV] = Qundef; /* envval */
/* self? */
RB_OBJ_WRITE(procval, &proc->block.as.captured.code.ifunc, ifunc);
proc->is_lambda = TRUE;
return procval;
}
static VALUE
sym_proc_new(VALUE klass, VALUE sym)
{
VALUE procval = rb_proc_alloc(klass);
rb_proc_t *proc;
GetProcPtr(procval, proc);
vm_block_type_set(&proc->block, block_type_symbol);
proc->is_lambda = TRUE;
RB_OBJ_WRITE(procval, &proc->block.as.symbol, sym);
return procval;
}
struct vm_ifunc *
rb_vm_ifunc_new(rb_block_call_func_t func, const void *data, int min_argc, int max_argc)
{
union {
struct vm_ifunc_argc argc;
VALUE packed;
} arity;
if (min_argc < UNLIMITED_ARGUMENTS ||
#if SIZEOF_INT * 2 > SIZEOF_VALUE
min_argc >= (int)(1U << (SIZEOF_VALUE * CHAR_BIT) / 2) ||
#endif
0) {
rb_raise(rb_eRangeError, "minimum argument number out of range: %d",
min_argc);
}
if (max_argc < UNLIMITED_ARGUMENTS ||
#if SIZEOF_INT * 2 > SIZEOF_VALUE
max_argc >= (int)(1U << (SIZEOF_VALUE * CHAR_BIT) / 2) ||
#endif
0) {
rb_raise(rb_eRangeError, "maximum argument number out of range: %d",
max_argc);
}
arity.argc.min = min_argc;
arity.argc.max = max_argc;
VALUE ret = rb_imemo_new(imemo_ifunc, (VALUE)func, (VALUE)data, arity.packed, 0);
return (struct vm_ifunc *)ret;
}
MJIT_FUNC_EXPORTED VALUE
rb_func_proc_new(rb_block_call_func_t func, VALUE val)
{
struct vm_ifunc *ifunc = rb_vm_ifunc_proc_new(func, (void *)val);
return cfunc_proc_new(rb_cProc, (VALUE)ifunc);
}
MJIT_FUNC_EXPORTED VALUE
rb_func_lambda_new(rb_block_call_func_t func, VALUE val, int min_argc, int max_argc)
{
struct vm_ifunc *ifunc = rb_vm_ifunc_new(func, (void *)val, min_argc, max_argc);
return cfunc_proc_new(rb_cProc, (VALUE)ifunc);
}
static const char proc_without_block[] = "tried to create Proc object without a block";
static VALUE
proc_new(VALUE klass, int8_t is_lambda, int8_t kernel)
{
VALUE procval;
const rb_execution_context_t *ec = GET_EC();
rb_control_frame_t *cfp = ec->cfp;
VALUE block_handler;
if ((block_handler = rb_vm_frame_block_handler(cfp)) == VM_BLOCK_HANDLER_NONE) {
rb_raise(rb_eArgError, proc_without_block);
}
/* block is in cf */
switch (vm_block_handler_type(block_handler)) {
case block_handler_type_proc:
procval = VM_BH_TO_PROC(block_handler);
if (RBASIC_CLASS(procval) == klass) {
return procval;
}
else {
VALUE newprocval = rb_proc_dup(procval);
RBASIC_SET_CLASS(newprocval, klass);
return newprocval;
}
break;
case block_handler_type_symbol:
return (klass != rb_cProc) ?
sym_proc_new(klass, VM_BH_TO_SYMBOL(block_handler)) :
rb_sym_to_proc(VM_BH_TO_SYMBOL(block_handler));
break;
case block_handler_type_ifunc:
return rb_vm_make_proc_lambda(ec, VM_BH_TO_CAPT_BLOCK(block_handler), klass, is_lambda);
case block_handler_type_iseq:
{
const struct rb_captured_block *captured = VM_BH_TO_CAPT_BLOCK(block_handler);
rb_control_frame_t *last_ruby_cfp = rb_vm_get_ruby_level_next_cfp(ec, cfp);
if (is_lambda && last_ruby_cfp && vm_cfp_forwarded_bh_p(last_ruby_cfp, block_handler)) {
is_lambda = false;
}
return rb_vm_make_proc_lambda(ec, captured, klass, is_lambda);
}
}
VM_UNREACHABLE(proc_new);
return Qnil;
}
/*
* call-seq:
* Proc.new {|...| block } -> a_proc
*
* Creates a new Proc object, bound to the current context.
*
* proc = Proc.new { "hello" }
* proc.call #=> "hello"
*
* Raises ArgumentError if called without a block.
*
* Proc.new #=> ArgumentError
*/
static VALUE
rb_proc_s_new(int argc, VALUE *argv, VALUE klass)
{
VALUE block = proc_new(klass, FALSE, FALSE);
rb_obj_call_init_kw(block, argc, argv, RB_PASS_CALLED_KEYWORDS);
return block;
}
VALUE
rb_block_proc(void)
{
return proc_new(rb_cProc, FALSE, FALSE);
}
/*
* call-seq:
* proc { |...| block } -> a_proc
*
* Equivalent to Proc.new.
*/
static VALUE
f_proc(VALUE _)
{
return proc_new(rb_cProc, FALSE, TRUE);
}
VALUE
rb_block_lambda(void)
{
return proc_new(rb_cProc, TRUE, FALSE);
}
static void
f_lambda_warn(void)
{
rb_control_frame_t *cfp = GET_EC()->cfp;
VALUE block_handler = rb_vm_frame_block_handler(cfp);
if (block_handler != VM_BLOCK_HANDLER_NONE) {
switch (vm_block_handler_type(block_handler)) {
case block_handler_type_iseq:
if (RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp)->ep == VM_BH_TO_ISEQ_BLOCK(block_handler)->ep) {
return;
}
break;
case block_handler_type_symbol:
return;
case block_handler_type_proc:
if (rb_proc_lambda_p(VM_BH_TO_PROC(block_handler))) {
return;
}
break;
case block_handler_type_ifunc:
break;
}
}
rb_warn_deprecated("lambda without a literal block", "the proc without lambda");
}
/*
* call-seq:
* lambda { |...| block } -> a_proc
*
* Equivalent to Proc.new, except the resulting Proc objects check the
* number of parameters passed when called.
*/
static VALUE
f_lambda(VALUE _)
{
f_lambda_warn();
return rb_block_lambda();
}
/* Document-method: Proc#===
*
* call-seq:
* proc === obj -> result_of_proc
*
* Invokes the block with +obj+ as the proc's parameter like Proc#call.
* This allows a proc object to be the target of a +when+ clause
* in a case statement.
*/
/* CHECKME: are the argument checking semantics correct? */
/*
* Document-method: Proc#[]
* Document-method: Proc#call
* Document-method: Proc#yield
*
* call-seq:
* prc.call(params,...) -> obj
* prc[params,...] -> obj
* prc.(params,...) -> obj
* prc.yield(params,...) -> obj
*
* Invokes the block, setting the block's parameters to the values in
* <i>params</i> using something close to method calling semantics.
* Returns the value of the last expression evaluated in the block.
*
* a_proc = Proc.new {|scalar, *values| values.map {|value| value*scalar } }
* a_proc.call(9, 1, 2, 3) #=> [9, 18, 27]
* a_proc[9, 1, 2, 3] #=> [9, 18, 27]
* a_proc.(9, 1, 2, 3) #=> [9, 18, 27]
* a_proc.yield(9, 1, 2, 3) #=> [9, 18, 27]
*
* Note that <code>prc.()</code> invokes <code>prc.call()</code> with
* the parameters given. It's syntactic sugar to hide "call".
*
* For procs created using #lambda or <code>->()</code> an error is
* generated if the wrong number of parameters are passed to the
* proc. For procs created using Proc.new or Kernel.proc, extra
* parameters are silently discarded and missing parameters are set
* to +nil+.
*
* a_proc = proc {|a,b| [a,b] }
* a_proc.call(1) #=> [1, nil]
*
* a_proc = lambda {|a,b| [a,b] }
* a_proc.call(1) # ArgumentError: wrong number of arguments (given 1, expected 2)
*
* See also Proc#lambda?.
*/
#if 0
static VALUE
proc_call(int argc, VALUE *argv, VALUE procval)
{
/* removed */
}
#endif
#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_kw(VALUE self, VALUE args, int kw_splat)
{
VALUE vret;
rb_proc_t *proc;
int argc = check_argc(RARRAY_LEN(args));
const VALUE *argv = RARRAY_CONST_PTR(args);
GetProcPtr(self, proc);
vret = rb_vm_invoke_proc(GET_EC(), proc, argc, argv,
kw_splat, VM_BLOCK_HANDLER_NONE);
RB_GC_GUARD(self);
RB_GC_GUARD(args);
return vret;
}
VALUE
rb_proc_call(VALUE self, VALUE args)