-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvm_eval.c
2717 lines (2366 loc) · 79.3 KB
/
vm_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
/**********************************************************************
vm_eval.c -
$Author$
created at: Sat May 24 16:02:32 JST 2008
Copyright (C) 1993-2007 Yukihiro Matsumoto
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
Copyright (C) 2000 Information-technology Promotion Agency, Japan
**********************************************************************/
#include "internal/thread.h"
struct local_var_list {
VALUE tbl;
};
static inline VALUE method_missing(rb_execution_context_t *ec, VALUE obj, ID id, int argc, const VALUE *argv, enum method_missing_reason call_status, int kw_splat);
static inline VALUE vm_yield_with_cref(rb_execution_context_t *ec, int argc, const VALUE *argv, int kw_splat, const rb_cref_t *cref, int is_lambda);
static inline VALUE vm_yield(rb_execution_context_t *ec, int argc, const VALUE *argv, int kw_splat);
static inline VALUE vm_yield_with_block(rb_execution_context_t *ec, int argc, const VALUE *argv, VALUE block_handler, int kw_splat);
static inline VALUE vm_yield_force_blockarg(rb_execution_context_t *ec, VALUE args);
VALUE vm_exec(rb_execution_context_t *ec);
static void vm_set_eval_stack(rb_execution_context_t * th, const rb_iseq_t *iseq, const rb_cref_t *cref, const struct rb_block *base_block);
static int vm_collect_local_variables_in_heap(const VALUE *dfp, const struct local_var_list *vars);
static VALUE rb_eUncaughtThrow;
static ID id_result, id_tag, id_value;
#define id_mesg idMesg
static VALUE send_internal(int argc, const VALUE *argv, VALUE recv, call_type scope);
static VALUE vm_call0_body(rb_execution_context_t* ec, struct rb_calling_info *calling, const VALUE *argv);
static VALUE *
vm_argv_ruby_array(VALUE *av, const VALUE *argv, int *flags, int *argc, int kw_splat)
{
*flags |= VM_CALL_ARGS_SPLAT;
VALUE argv_ary = rb_ary_hidden_new(*argc);
rb_ary_cat(argv_ary, argv, *argc);
*argc = 2;
av[0] = argv_ary;
if (kw_splat) {
av[1] = rb_ary_pop(argv_ary);
}
else {
// Make sure flagged keyword hash passed as regular argument
// isn't treated as keywords
*flags |= VM_CALL_KW_SPLAT;
av[1] = rb_hash_new();
}
return av;
}
static inline VALUE vm_call0_cc(rb_execution_context_t *ec, VALUE recv, ID id, int argc, const VALUE *argv, const struct rb_callcache *cc, int kw_splat);
VALUE
rb_vm_call0(rb_execution_context_t *ec, VALUE recv, ID id, int argc, const VALUE *argv, const rb_callable_method_entry_t *cme, int kw_splat)
{
const struct rb_callcache cc = VM_CC_ON_STACK(Qfalse, vm_call_general, {{ 0 }}, cme);
return vm_call0_cc(ec, recv, id, argc, argv, &cc, kw_splat);
}
VALUE
rb_vm_call_with_refinements(rb_execution_context_t *ec, VALUE recv, ID id, int argc, const VALUE *argv, int kw_splat)
{
const rb_callable_method_entry_t *me =
rb_callable_method_entry_with_refinements(CLASS_OF(recv), id, NULL);
if (me) {
return rb_vm_call0(ec, recv, id, argc, argv, me, kw_splat);
}
else {
/* fallback to funcall (e.g. method_missing) */
return rb_funcallv(recv, id, argc, argv);
}
}
static inline VALUE
vm_call0_cc(rb_execution_context_t *ec, VALUE recv, ID id, int argc, const VALUE *argv, const struct rb_callcache *cc, int kw_splat)
{
int flags = kw_splat ? VM_CALL_KW_SPLAT : 0;
VALUE *use_argv = (VALUE *)argv;
VALUE av[2];
if (UNLIKELY(vm_cc_cme(cc)->def->type == VM_METHOD_TYPE_ISEQ && argc > VM_ARGC_STACK_MAX)) {
use_argv = vm_argv_ruby_array(av, argv, &flags, &argc, kw_splat);
}
struct rb_calling_info calling = {
.cd = &(struct rb_call_data) {
.ci = &VM_CI_ON_STACK(id, flags, argc, NULL),
.cc = NULL,
},
.cc = cc,
.block_handler = vm_passed_block_handler(ec),
.recv = recv,
.argc = argc,
.kw_splat = kw_splat,
};
return vm_call0_body(ec, &calling, use_argv);
}
static VALUE
vm_call0_cme(rb_execution_context_t *ec, struct rb_calling_info *calling, const VALUE *argv, const rb_callable_method_entry_t *cme)
{
calling->cc = &VM_CC_ON_STACK(Qfalse, vm_call_general, {{ 0 }}, cme);
return vm_call0_body(ec, calling, argv);
}
static VALUE
vm_call0_super(rb_execution_context_t *ec, struct rb_calling_info *calling, const VALUE *argv, VALUE klass, enum method_missing_reason ex)
{
ID mid = vm_ci_mid(calling->cd->ci);
klass = RCLASS_SUPER(klass);
if (klass) {
const rb_callable_method_entry_t *cme = rb_callable_method_entry(klass, mid);
if (cme) {
RUBY_VM_CHECK_INTS(ec);
return vm_call0_cme(ec, calling, argv, cme);
}
}
vm_passed_block_handler_set(ec, calling->block_handler);
return method_missing(ec, calling->recv, mid, calling->argc, argv, ex, calling->kw_splat);
}
static VALUE
vm_call0_cfunc_with_frame(rb_execution_context_t* ec, struct rb_calling_info *calling, const VALUE *argv)
{
const struct rb_callinfo *ci = calling->cd->ci;
VALUE val;
const rb_callable_method_entry_t *me = vm_cc_cme(calling->cc);
const rb_method_cfunc_t *cfunc = UNALIGNED_MEMBER_PTR(me->def, body.cfunc);
int len = cfunc->argc;
VALUE recv = calling->recv;
int argc = calling->argc;
ID mid = vm_ci_mid(ci);
VALUE block_handler = calling->block_handler;
int frame_flags = VM_FRAME_MAGIC_CFUNC | VM_FRAME_FLAG_CFRAME | VM_ENV_FLAG_LOCAL;
if (calling->kw_splat) {
if (argc > 0 && RB_TYPE_P(argv[argc-1], T_HASH) && RHASH_EMPTY_P(argv[argc-1])) {
argc--;
}
else {
frame_flags |= VM_FRAME_FLAG_CFRAME_KW;
}
}
RUBY_DTRACE_CMETHOD_ENTRY_HOOK(ec, me->owner, me->def->original_id);
EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_CALL, recv, me->def->original_id, mid, me->owner, Qnil);
{
rb_control_frame_t *reg_cfp = ec->cfp;
vm_push_frame(ec, 0, frame_flags, recv,
block_handler, (VALUE)me,
0, reg_cfp->sp, 0, 0);
if (len >= 0) rb_check_arity(argc, len, len);
val = (*cfunc->invoker)(recv, argc, argv, cfunc->func);
CHECK_CFP_CONSISTENCY("vm_call0_cfunc_with_frame");
rb_vm_pop_frame(ec);
}
EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_RETURN, recv, me->def->original_id, mid, me->owner, val);
RUBY_DTRACE_CMETHOD_RETURN_HOOK(ec, me->owner, me->def->original_id);
return val;
}
static VALUE
vm_call0_cfunc(rb_execution_context_t *ec, struct rb_calling_info *calling, const VALUE *argv)
{
return vm_call0_cfunc_with_frame(ec, calling, argv);
}
static void
vm_call_check_arity(struct rb_calling_info *calling, int argc, const VALUE *argv)
{
if (calling->kw_splat &&
calling->argc > 0 &&
RB_TYPE_P(argv[calling->argc-1], T_HASH) &&
RHASH_EMPTY_P(argv[calling->argc-1])) {
calling->argc--;
}
rb_check_arity(calling->argc, argc, argc);
}
/* `ci' should point temporal value (on stack value) */
static VALUE
vm_call0_body(rb_execution_context_t *ec, struct rb_calling_info *calling, const VALUE *argv)
{
const struct rb_callinfo *ci = calling->cd->ci;
const struct rb_callcache *cc = calling->cc;
VALUE ret;
retry:
switch (vm_cc_cme(cc)->def->type) {
case VM_METHOD_TYPE_ISEQ:
{
rb_control_frame_t *reg_cfp = ec->cfp;
int i;
CHECK_VM_STACK_OVERFLOW(reg_cfp, calling->argc + 1);
vm_check_canary(ec, reg_cfp->sp);
*reg_cfp->sp++ = calling->recv;
for (i = 0; i < calling->argc; i++) {
*reg_cfp->sp++ = argv[i];
}
if (ISEQ_BODY(def_iseq_ptr(vm_cc_cme(cc)->def))->param.flags.forwardable) {
vm_call_iseq_fwd_setup(ec, reg_cfp, calling);
}
else {
vm_call_iseq_setup(ec, reg_cfp, calling);
}
VM_ENV_FLAGS_SET(ec->cfp->ep, VM_FRAME_FLAG_FINISH);
return vm_exec(ec); // CHECK_INTS in this function
}
case VM_METHOD_TYPE_NOTIMPLEMENTED:
case VM_METHOD_TYPE_CFUNC:
ret = vm_call0_cfunc(ec, calling, argv);
goto success;
case VM_METHOD_TYPE_ATTRSET:
vm_call_check_arity(calling, 1, argv);
VM_CALL_METHOD_ATTR(ret,
rb_ivar_set(calling->recv, vm_cc_cme(cc)->def->body.attr.id, argv[0]),
(void)0);
goto success;
case VM_METHOD_TYPE_IVAR:
vm_call_check_arity(calling, 0, argv);
VM_CALL_METHOD_ATTR(ret,
rb_attr_get(calling->recv, vm_cc_cme(cc)->def->body.attr.id),
(void)0);
goto success;
case VM_METHOD_TYPE_BMETHOD:
ret = vm_call_bmethod_body(ec, calling, argv);
goto success;
case VM_METHOD_TYPE_ZSUPER:
{
VALUE klass = RCLASS_ORIGIN(vm_cc_cme(cc)->defined_class);
return vm_call0_super(ec, calling, argv, klass, MISSING_SUPER);
}
case VM_METHOD_TYPE_REFINED:
{
const rb_callable_method_entry_t *cme = vm_cc_cme(cc);
if (cme->def->body.refined.orig_me) {
const rb_callable_method_entry_t *orig_cme = refined_method_callable_without_refinement(cme);
return vm_call0_cme(ec, calling, argv, orig_cme);
}
VALUE klass = cme->defined_class;
return vm_call0_super(ec, calling, argv, klass, 0);
}
case VM_METHOD_TYPE_ALIAS:
{
const rb_callable_method_entry_t *cme = vm_cc_cme(cc);
const rb_callable_method_entry_t *orig_cme = aliased_callable_method_entry(cme);
if (cme == orig_cme) rb_bug("same!!");
if (vm_cc_markable(cc)) {
return vm_call0_cme(ec, calling, argv, orig_cme);
}
else {
*((const rb_callable_method_entry_t **)&cc->cme_) = orig_cme;
goto retry;
}
}
case VM_METHOD_TYPE_MISSING:
{
vm_passed_block_handler_set(ec, calling->block_handler);
return method_missing(ec, calling->recv, vm_ci_mid(ci), calling->argc,
argv, MISSING_NOENTRY, calling->kw_splat);
}
case VM_METHOD_TYPE_OPTIMIZED:
switch (vm_cc_cme(cc)->def->body.optimized.type) {
case OPTIMIZED_METHOD_TYPE_SEND:
ret = send_internal(calling->argc, argv, calling->recv, calling->kw_splat ? CALL_FCALL_KW : CALL_FCALL);
goto success;
case OPTIMIZED_METHOD_TYPE_CALL:
{
rb_proc_t *proc;
GetProcPtr(calling->recv, proc);
ret = rb_vm_invoke_proc(ec, proc, calling->argc, argv, calling->kw_splat, calling->block_handler);
goto success;
}
case OPTIMIZED_METHOD_TYPE_STRUCT_AREF:
vm_call_check_arity(calling, 0, argv);
VM_CALL_METHOD_ATTR(ret,
vm_call_opt_struct_aref0(ec, calling),
(void)0);
goto success;
case OPTIMIZED_METHOD_TYPE_STRUCT_ASET:
vm_call_check_arity(calling, 1, argv);
VM_CALL_METHOD_ATTR(ret,
vm_call_opt_struct_aset0(ec, calling, argv[0]),
(void)0);
goto success;
default:
rb_bug("vm_call0: unsupported optimized method type (%d)", vm_cc_cme(cc)->def->body.optimized.type);
}
break;
case VM_METHOD_TYPE_UNDEF:
break;
}
rb_bug("vm_call0: unsupported method type (%d)", vm_cc_cme(cc)->def->type);
return Qundef;
success:
RUBY_VM_CHECK_INTS(ec);
return ret;
}
VALUE
rb_vm_call_kw(rb_execution_context_t *ec, VALUE recv, VALUE id, int argc, const VALUE *argv, const rb_callable_method_entry_t *me, int kw_splat)
{
return rb_vm_call0(ec, recv, id, argc, argv, me, kw_splat);
}
static inline VALUE
vm_call_super(rb_execution_context_t *ec, int argc, const VALUE *argv, int kw_splat)
{
VALUE recv = ec->cfp->self;
VALUE klass;
ID id;
rb_control_frame_t *cfp = ec->cfp;
const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(cfp);
if (VM_FRAME_RUBYFRAME_P(cfp)) {
rb_bug("vm_call_super: should not be reached");
}
klass = RCLASS_ORIGIN(me->defined_class);
klass = RCLASS_SUPER(klass);
id = me->def->original_id;
me = rb_callable_method_entry(klass, id);
if (!me) {
return method_missing(ec, recv, id, argc, argv, MISSING_SUPER, kw_splat);
}
return rb_vm_call_kw(ec, recv, id, argc, argv, me, kw_splat);
}
VALUE
rb_call_super_kw(int argc, const VALUE *argv, int kw_splat)
{
rb_execution_context_t *ec = GET_EC();
PASS_PASSED_BLOCK_HANDLER_EC(ec);
return vm_call_super(ec, argc, argv, kw_splat);
}
VALUE
rb_call_super(int argc, const VALUE *argv)
{
return rb_call_super_kw(argc, argv, RB_NO_KEYWORDS);
}
VALUE
rb_current_receiver(void)
{
const rb_execution_context_t *ec = GET_EC();
rb_control_frame_t *cfp;
if (!ec || !(cfp = ec->cfp)) {
rb_raise(rb_eRuntimeError, "no self, no life");
}
return cfp->self;
}
static inline void
stack_check(rb_execution_context_t *ec)
{
if (!rb_ec_raised_p(ec, RAISED_STACKOVERFLOW) &&
rb_ec_stack_check(ec)) {
rb_ec_raised_set(ec, RAISED_STACKOVERFLOW);
rb_ec_stack_overflow(ec, FALSE);
}
}
void
rb_check_stack_overflow(void)
{
#ifndef RB_THREAD_LOCAL_SPECIFIER
if (!ruby_current_ec_key) return;
#endif
rb_execution_context_t *ec = GET_EC();
if (ec) stack_check(ec);
}
NORETURN(static void uncallable_object(VALUE recv, ID mid));
static inline const rb_callable_method_entry_t *rb_search_method_entry(VALUE recv, ID mid);
static inline enum method_missing_reason rb_method_call_status(rb_execution_context_t *ec, const rb_callable_method_entry_t *me, call_type scope, VALUE self);
static VALUE
gccct_hash(VALUE klass, ID mid)
{
return (klass >> 3) ^ (VALUE)mid;
}
NOINLINE(static const struct rb_callcache *gccct_method_search_slowpath(rb_vm_t *vm, VALUE klass, unsigned int index, const struct rb_callinfo * ci));
static const struct rb_callcache *
gccct_method_search_slowpath(rb_vm_t *vm, VALUE klass, unsigned int index, const struct rb_callinfo *ci)
{
struct rb_call_data cd = {
.ci = ci,
.cc = NULL
};
vm_search_method_slowpath0(vm->self, &cd, klass);
return vm->global_cc_cache_table[index] = cd.cc;
}
static void
scope_to_ci(call_type scope, ID mid, int argc, struct rb_callinfo *ci)
{
int flags = 0;
switch(scope) {
case CALL_PUBLIC:
break;
case CALL_FCALL:
flags |= VM_CALL_FCALL;
break;
case CALL_VCALL:
flags |= VM_CALL_VCALL;
break;
case CALL_PUBLIC_KW:
flags |= VM_CALL_KWARG;
break;
case CALL_FCALL_KW:
flags |= (VM_CALL_KWARG | VM_CALL_FCALL);
break;
}
*ci = VM_CI_ON_STACK(mid, flags, argc, NULL);
}
static inline const struct rb_callcache *
gccct_method_search(rb_execution_context_t *ec, VALUE recv, ID mid, const struct rb_callinfo *ci)
{
VALUE klass;
if (!SPECIAL_CONST_P(recv)) {
klass = RBASIC_CLASS(recv);
if (UNLIKELY(!klass)) uncallable_object(recv, mid);
}
else {
klass = CLASS_OF(recv);
}
// search global method cache
unsigned int index = (unsigned int)(gccct_hash(klass, mid) % VM_GLOBAL_CC_CACHE_TABLE_SIZE);
rb_vm_t *vm = rb_ec_vm_ptr(ec);
const struct rb_callcache *cc = vm->global_cc_cache_table[index];
if (LIKELY(cc)) {
if (LIKELY(vm_cc_class_check(cc, klass))) {
const rb_callable_method_entry_t *cme = vm_cc_cme(cc);
if (LIKELY(!METHOD_ENTRY_INVALIDATED(cme) &&
cme->called_id == mid)) {
VM_ASSERT(vm_cc_check_cme(cc, rb_callable_method_entry(klass, mid)));
RB_DEBUG_COUNTER_INC(gccct_hit);
return cc;
}
}
}
else {
RB_DEBUG_COUNTER_INC(gccct_null);
}
RB_DEBUG_COUNTER_INC(gccct_miss);
return gccct_method_search_slowpath(vm, klass, index, ci);
}
/**
* @internal
* calls the specified method.
*
* This function is called by functions in rb_call* family.
* @param ec current execution context
* @param recv receiver of the method
* @param mid an ID that represents the name of the method
* @param argc the number of method arguments
* @param argv a pointer to an array of method arguments
* @param scope
* @param self self in the caller. Qundef means no self is considered and
* protected methods cannot be called
*
* @note `self` is used in order to controlling access to protected methods.
*/
static inline VALUE
rb_call0(rb_execution_context_t *ec,
VALUE recv, ID mid, int argc, const VALUE *argv,
call_type call_scope, VALUE self)
{
enum method_missing_reason call_status;
call_type scope = call_scope;
int kw_splat = RB_NO_KEYWORDS;
switch (scope) {
case CALL_PUBLIC_KW:
scope = CALL_PUBLIC;
kw_splat = 1;
break;
case CALL_FCALL_KW:
scope = CALL_FCALL;
kw_splat = 1;
break;
default:
break;
}
struct rb_callinfo ci;
scope_to_ci(scope, mid, argc, &ci);
const struct rb_callcache *cc = gccct_method_search(ec, recv, mid, &ci);
if (scope == CALL_PUBLIC) {
RB_DEBUG_COUNTER_INC(call0_public);
const rb_callable_method_entry_t *cc_cme = cc ? vm_cc_cme(cc) : NULL;
const rb_callable_method_entry_t *cme = callable_method_entry_refinements0(CLASS_OF(recv), mid, NULL, true, cc_cme);
call_status = rb_method_call_status(ec, cme, scope, self);
if (UNLIKELY(call_status != MISSING_NONE)) {
return method_missing(ec, recv, mid, argc, argv, call_status, kw_splat);
}
else if (UNLIKELY(cc_cme != cme)) { // refinement is solved
stack_check(ec);
return rb_vm_call_kw(ec, recv, mid, argc, argv, cme, kw_splat);
}
}
else {
RB_DEBUG_COUNTER_INC(call0_other);
call_status = rb_method_call_status(ec, cc ? vm_cc_cme(cc) : NULL, scope, self);
if (UNLIKELY(call_status != MISSING_NONE)) {
return method_missing(ec, recv, mid, argc, argv, call_status, kw_splat);
}
}
stack_check(ec);
return vm_call0_cc(ec, recv, mid, argc, argv, cc, kw_splat);
}
struct rescue_funcall_args {
VALUE defined_class;
VALUE recv;
ID mid;
rb_execution_context_t *ec;
const rb_callable_method_entry_t *cme;
unsigned int respond: 1;
unsigned int respond_to_missing: 1;
int argc;
const VALUE *argv;
int kw_splat;
};
static VALUE
check_funcall_exec(VALUE v)
{
struct rescue_funcall_args *args = (void *)v;
return call_method_entry(args->ec, args->defined_class,
args->recv, idMethodMissing,
args->cme, args->argc, args->argv, args->kw_splat);
}
static VALUE
check_funcall_failed(VALUE v, VALUE e)
{
struct rescue_funcall_args *args = (void *)v;
int ret = args->respond;
if (!ret) {
switch (method_boundp(args->defined_class, args->mid,
BOUND_PRIVATE|BOUND_RESPONDS)) {
case 2:
ret = TRUE;
break;
case 0:
ret = args->respond_to_missing;
break;
default:
ret = FALSE;
break;
}
}
if (ret) {
rb_exc_raise(e);
}
return Qundef;
}
static int
check_funcall_respond_to(rb_execution_context_t *ec, VALUE klass, VALUE recv, ID mid)
{
return vm_respond_to(ec, klass, recv, mid, TRUE);
}
static int
check_funcall_callable(rb_execution_context_t *ec, const rb_callable_method_entry_t *me)
{
return rb_method_call_status(ec, me, CALL_FCALL, ec->cfp->self) == MISSING_NONE;
}
static VALUE
check_funcall_missing(rb_execution_context_t *ec, VALUE klass, VALUE recv, ID mid, int argc, const VALUE *argv, int respond, VALUE def, int kw_splat)
{
struct rescue_funcall_args args;
const rb_callable_method_entry_t *cme;
VALUE ret = Qundef;
ret = basic_obj_respond_to_missing(ec, klass, recv,
ID2SYM(mid), Qtrue);
if (!RTEST(ret)) return def;
args.respond = respond > 0;
args.respond_to_missing = !UNDEF_P(ret);
ret = def;
cme = callable_method_entry(klass, idMethodMissing, &args.defined_class);
if (cme && !METHOD_ENTRY_BASIC(cme)) {
VALUE argbuf, *new_args = ALLOCV_N(VALUE, argbuf, argc+1);
new_args[0] = ID2SYM(mid);
#ifdef __GLIBC__
if (!argv) {
static const VALUE buf = Qfalse;
VM_ASSERT(argc == 0);
argv = &buf;
}
#endif
MEMCPY(new_args+1, argv, VALUE, argc);
ec->method_missing_reason = MISSING_NOENTRY;
args.ec = ec;
args.recv = recv;
args.cme = cme;
args.mid = mid;
args.argc = argc + 1;
args.argv = new_args;
args.kw_splat = kw_splat;
ret = rb_rescue2(check_funcall_exec, (VALUE)&args,
check_funcall_failed, (VALUE)&args,
rb_eNoMethodError, (VALUE)0);
ALLOCV_END(argbuf);
}
return ret;
}
static VALUE rb_check_funcall_default_kw(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE def, int kw_splat);
VALUE
rb_check_funcall_kw(VALUE recv, ID mid, int argc, const VALUE *argv, int kw_splat)
{
return rb_check_funcall_default_kw(recv, mid, argc, argv, Qundef, kw_splat);
}
VALUE
rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv)
{
return rb_check_funcall_default_kw(recv, mid, argc, argv, Qundef, RB_NO_KEYWORDS);
}
static VALUE
rb_check_funcall_default_kw(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE def, int kw_splat)
{
VM_ASSERT(ruby_thread_has_gvl_p());
VALUE klass = CLASS_OF(recv);
const rb_callable_method_entry_t *me;
rb_execution_context_t *ec = GET_EC();
int respond = check_funcall_respond_to(ec, klass, recv, mid);
if (!respond)
return def;
me = rb_search_method_entry(recv, mid);
if (!check_funcall_callable(ec, me)) {
VALUE ret = check_funcall_missing(ec, klass, recv, mid, argc, argv,
respond, def, kw_splat);
if (UNDEF_P(ret)) ret = def;
return ret;
}
stack_check(ec);
return rb_vm_call_kw(ec, recv, mid, argc, argv, me, kw_splat);
}
VALUE
rb_check_funcall_default(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE def)
{
return rb_check_funcall_default_kw(recv, mid, argc, argv, def, RB_NO_KEYWORDS);
}
VALUE
rb_check_funcall_with_hook_kw(VALUE recv, ID mid, int argc, const VALUE *argv,
rb_check_funcall_hook *hook, VALUE arg, int kw_splat)
{
VALUE klass = CLASS_OF(recv);
const rb_callable_method_entry_t *me;
rb_execution_context_t *ec = GET_EC();
int respond = check_funcall_respond_to(ec, klass, recv, mid);
if (!respond) {
(*hook)(FALSE, recv, mid, argc, argv, arg);
return Qundef;
}
me = rb_search_method_entry(recv, mid);
if (!check_funcall_callable(ec, me)) {
VALUE ret = check_funcall_missing(ec, klass, recv, mid, argc, argv,
respond, Qundef, kw_splat);
(*hook)(!UNDEF_P(ret), recv, mid, argc, argv, arg);
return ret;
}
stack_check(ec);
(*hook)(TRUE, recv, mid, argc, argv, arg);
return rb_vm_call_kw(ec, recv, mid, argc, argv, me, kw_splat);
}
VALUE
rb_check_funcall_with_hook(VALUE recv, ID mid, int argc, const VALUE *argv,
rb_check_funcall_hook *hook, VALUE arg)
{
return rb_check_funcall_with_hook_kw(recv, mid, argc, argv, hook, arg, RB_NO_KEYWORDS);
}
const char *
rb_type_str(enum ruby_value_type type)
{
#define type_case(t) t: return #t
switch (type) {
case type_case(T_NONE);
case type_case(T_OBJECT);
case type_case(T_CLASS);
case type_case(T_MODULE);
case type_case(T_FLOAT);
case type_case(T_STRING);
case type_case(T_REGEXP);
case type_case(T_ARRAY);
case type_case(T_HASH);
case type_case(T_STRUCT);
case type_case(T_BIGNUM);
case type_case(T_FILE);
case type_case(T_DATA);
case type_case(T_MATCH);
case type_case(T_COMPLEX);
case type_case(T_RATIONAL);
case type_case(T_NIL);
case type_case(T_TRUE);
case type_case(T_FALSE);
case type_case(T_SYMBOL);
case type_case(T_FIXNUM);
case type_case(T_IMEMO);
case type_case(T_UNDEF);
case type_case(T_NODE);
case type_case(T_ICLASS);
case type_case(T_ZOMBIE);
case type_case(T_MOVED);
case T_MASK: break;
}
#undef type_case
return NULL;
}
static void
uncallable_object(VALUE recv, ID mid)
{
VALUE flags;
int type;
const char *typestr;
VALUE mname = rb_id2str(mid);
if (SPECIAL_CONST_P(recv)) {
rb_raise(rb_eNotImpError,
"method '%"PRIsVALUE"' called on unexpected immediate object (%p)",
mname, (void *)recv);
}
else if ((flags = RBASIC(recv)->flags) == 0) {
rb_raise(rb_eNotImpError,
"method '%"PRIsVALUE"' called on terminated object (%p)",
mname, (void *)recv);
}
else if (!(typestr = rb_type_str(type = BUILTIN_TYPE(recv)))) {
rb_raise(rb_eNotImpError,
"method '%"PRIsVALUE"' called on broken T_?""?""?(0x%02x) object"
" (%p flags=0x%"PRIxVALUE")",
mname, type, (void *)recv, flags);
}
else if (T_OBJECT <= type && type < T_NIL) {
rb_raise(rb_eNotImpError,
"method '%"PRIsVALUE"' called on hidden %s object"
" (%p flags=0x%"PRIxVALUE")",
mname, typestr, (void *)recv, flags);
}
else {
rb_raise(rb_eNotImpError,
"method '%"PRIsVALUE"' called on unexpected %s object"
" (%p flags=0x%"PRIxVALUE")",
mname, typestr, (void *)recv, flags);
}
}
static inline const rb_callable_method_entry_t *
rb_search_method_entry(VALUE recv, ID mid)
{
VALUE klass = CLASS_OF(recv);
if (!klass) uncallable_object(recv, mid);
return rb_callable_method_entry(klass, mid);
}
static inline enum method_missing_reason
rb_method_call_status(rb_execution_context_t *ec, const rb_callable_method_entry_t *me, call_type scope, VALUE self)
{
if (UNLIKELY(UNDEFINED_METHOD_ENTRY_P(me))) {
goto undefined;
}
else if (UNLIKELY(me->def->type == VM_METHOD_TYPE_REFINED)) {
me = rb_resolve_refined_method_callable(Qnil, me);
if (UNDEFINED_METHOD_ENTRY_P(me)) goto undefined;
}
rb_method_visibility_t visi = METHOD_ENTRY_VISI(me);
/* receiver specified form for private method */
if (UNLIKELY(visi != METHOD_VISI_PUBLIC)) {
if (me->def->original_id == idMethodMissing) {
return MISSING_NONE;
}
else if (visi == METHOD_VISI_PRIVATE &&
scope == CALL_PUBLIC) {
return MISSING_PRIVATE;
}
/* self must be kind of a specified form for protected method */
else if (visi == METHOD_VISI_PROTECTED &&
scope == CALL_PUBLIC) {
VALUE defined_class = me->owner;
if (RB_TYPE_P(defined_class, T_ICLASS)) {
defined_class = RBASIC(defined_class)->klass;
}
if (UNDEF_P(self) || !rb_obj_is_kind_of(self, defined_class)) {
return MISSING_PROTECTED;
}
}
}
return MISSING_NONE;
undefined:
return scope == CALL_VCALL ? MISSING_VCALL : MISSING_NOENTRY;
}
/**
* @internal
* calls the specified method.
*
* This function is called by functions in rb_call* family.
* @param recv receiver
* @param mid an ID that represents the name of the method
* @param argc the number of method arguments
* @param argv a pointer to an array of method arguments
* @param scope
*/
static inline VALUE
rb_call(VALUE recv, ID mid, int argc, const VALUE *argv, call_type scope)
{
rb_execution_context_t *ec = GET_EC();
return rb_call0(ec, recv, mid, argc, argv, scope, ec->cfp->self);
}
NORETURN(static void raise_method_missing(rb_execution_context_t *ec, int argc, const VALUE *argv,
VALUE obj, enum method_missing_reason call_status));
/*
* call-seq:
* obj.method_missing(symbol [, *args] ) -> result
*
* Invoked by Ruby when <i>obj</i> is sent a message it cannot handle.
* <i>symbol</i> is the symbol for the method called, and <i>args</i>
* are any arguments that were passed to it. By default, the interpreter
* raises an error when this method is called. However, it is possible
* to override the method to provide more dynamic behavior.
* If it is decided that a particular method should not be handled, then
* <i>super</i> should be called, so that ancestors can pick up the
* missing method.
* The example below creates
* a class <code>Roman</code>, which responds to methods with names
* consisting of roman numerals, returning the corresponding integer
* values.
*
* class Roman
* def roman_to_int(str)
* # ...
* end
*
* def method_missing(symbol, *args)
* str = symbol.id2name
* begin
* roman_to_int(str)
* rescue
* super(symbol, *args)
* end
* end
* end
*
* r = Roman.new
* r.iv #=> 4
* r.xxiii #=> 23
* r.mm #=> 2000
* r.foo #=> NoMethodError
*/
static VALUE
rb_method_missing(int argc, const VALUE *argv, VALUE obj)
{
rb_execution_context_t *ec = GET_EC();
raise_method_missing(ec, argc, argv, obj, ec->method_missing_reason);
UNREACHABLE_RETURN(Qnil);
}
VALUE
rb_make_no_method_exception(VALUE exc, VALUE format, VALUE obj,
int argc, const VALUE *argv, int priv)
{
VALUE name = argv[0];
if (!format) {
format = rb_fstring_lit("undefined method '%1$s' for %3$s%4$s");
}
if (exc == rb_eNoMethodError) {
VALUE args = rb_ary_new4(argc - 1, argv + 1);
return rb_nomethod_err_new(format, obj, name, args, priv);
}
else {
return rb_name_err_new(format, obj, name);
}
}
static void
raise_method_missing(rb_execution_context_t *ec, int argc, const VALUE *argv, VALUE obj,
enum method_missing_reason last_call_status)
{
VALUE exc = rb_eNoMethodError;
VALUE format = 0;
if (UNLIKELY(argc == 0)) {
rb_raise(rb_eArgError, "no method name given");
}
else if (UNLIKELY(!SYMBOL_P(argv[0]))) {
const VALUE e = rb_eArgError; /* TODO: TypeError? */
rb_raise(e, "method name must be a Symbol but %"PRIsVALUE" is given",
rb_obj_class(argv[0]));
}
stack_check(ec);
if (last_call_status & MISSING_PRIVATE) {
format = rb_fstring_lit("private method '%1$s' called for %3$s%4$s");
}
else if (last_call_status & MISSING_PROTECTED) {
format = rb_fstring_lit("protected method '%1$s' called for %3$s%4$s");
}
else if (last_call_status & MISSING_VCALL) {
format = rb_fstring_lit("undefined local variable or method '%1$s' for %3$s%4$s");
exc = rb_eNameError;
}
else if (last_call_status & MISSING_SUPER) {
format = rb_fstring_lit("super: no superclass method '%1$s' for %3$s%4$s");
}
{
exc = rb_make_no_method_exception(exc, format, obj, argc, argv,
last_call_status & (MISSING_FCALL|MISSING_VCALL));
if (!(last_call_status & MISSING_MISSING)) {
rb_vm_pop_cfunc_frame();
}
rb_exc_raise(exc);
}
}
static void
vm_raise_method_missing(rb_execution_context_t *ec, int argc, const VALUE *argv,
VALUE obj, int call_status)
{
vm_passed_block_handler_set(ec, VM_BLOCK_HANDLER_NONE);
raise_method_missing(ec, argc, argv, obj, call_status | MISSING_MISSING);
}