-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
dispatchers.nqp
4944 lines (4420 loc) Β· 201 KB
/
dispatchers.nqp
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
# Some maximum cache sizes we allow at a callsite before we switch to a
# megamorphic strategy.
my int $MEGA-TYPE-CALLSITE-SIZE := 16;
my int $MEGA-METH-CALLSITE-SIZE := 16;
#- General Helper Subs ---------------------------------------------------------
# Helper sub to delegate to return given constant
sub delegate-constant($capture, $value) {
# Insert the literal value at the start (boot-constant ignores
# the rest of the args) and delegate
nqp::delegate('boot-constant',
nqp::syscall('dispatcher-insert-arg-literal-obj',
$capture, 0, $value
)
);
}
# Helper sub to delegate to return Nil
sub delegate-constant-Nil($capture) { delegate-constant($capture, Nil) }
# Helper sub to delegate to return given value
sub delegate-value($capture, $value) {
# Insert the value at the start (boot-value ignores the rest
# of the args) and delegate
nqp::delegate('boot-value',
nqp::syscall('dispatcher-insert-arg', $capture, 0, $value)
);
}
# Helper sub to delegate to a syscall for a code object as the first
# argument in a capture
sub delegate-code-syscall($capture, str $dispatcher) {
my $Tcode := nqp::track('arg', $capture, 0);
nqp::guard('type', $Tcode);
nqp::istype(nqp::captureposarg($capture, 0), Code)
# Delegate to calling try-capture-lex-callers function
?? nqp::delegate('boot-syscall',
nqp::syscall('dispatcher-insert-arg-literal-str',
nqp::syscall('dispatcher-insert-arg',
nqp::syscall('dispatcher-drop-arg', $capture, 0),
0, nqp::track('attr', $Tcode, Code, '$!do')
),
0, $dispatcher
)
)
# Produce whatever we were given
!! nqp::delegate('boot-value', $capture);
}
# Guard the given tracker on type and concreteness
sub guard-type-concreteness($tracker) {
nqp::guard('type', $tracker);
nqp::guard('concreteness', $tracker);
}
#- raku-rv-decont --------------------------------------------------------------
# Return value decontainerization dispatcher. Often we have nothing at all
# to do, in which case we can make it identity. Other times, we need a
# decont. In a few, we need to re-wrap it.
;{
# Simple re-containerization logic
my $recont := -> $obj {
my $rc := nqp::create(Scalar);
nqp::bindattr($rc, Scalar, '$!value', nqp::decont($obj));
$rc
}
# Default decontainerize logic, with check for Iterables
my $container-fallback := -> $Iterable, $cont {
if nqp::isrwcont($cont) {
# It's an RW container, so we really need to decont it.
my $rv := nqp::decont($cont);
if nqp::istype($rv, $Iterable) {
my $rc := nqp::create(Scalar);
nqp::bindattr($rc, Scalar, '$!value', $rv);
$rc
}
else {
$rv
}
}
else {
# A read-only container, so just return it.
$cont
}
}
# Dispatcher for decontainerization of return values in Raku.
# Expects a capture with the value as its first argument.
nqp::register('raku-rv-decont', -> $capture {
# If it's heading megamorphic, then we'll install the fallback,
# without any conditions, which is faster than over-filling the
# cache and running this dispatch logic every time.
my int $cache-size := nqp::syscall('dispatcher-inline-cache-size');
if $cache-size >= $MEGA-TYPE-CALLSITE-SIZE {
nqp::delegate('boot-code-constant',
nqp::syscall('dispatcher-insert-arg-literal-obj',
nqp::syscall('dispatcher-insert-arg-literal-obj',
$capture, 0, nqp::gethllsym('Raku', 'Iterable')
),
0, $container-fallback
)
);
}
else {
# We always need to guard on type and concreteness.
my $rv := nqp::captureposarg($capture, 0);
my $Trv := nqp::track('arg', $capture, 0);
guard-type-concreteness($Trv);
# Is it a container?
if nqp::isconcrete_nd($rv) && nqp::iscont($rv) {
# It's a container. We have special cases for Scalar.
if nqp::istype_nd($rv, Scalar) {
# Check if the descriptor is undefined, in which case
# it's read-only.
my $desc := nqp::getattr($rv, Scalar, '$!descriptor');
my $Tdesc :=
nqp::track('attr', $Trv, Scalar, '$!descriptor');
nqp::guard('concreteness', $Tdesc);
if nqp::isconcrete($desc) {
# Writeable, so we may need to recontainerize the
# value if the type is iterable, otherwise we can
# decont it.
my $value := nqp::getattr($rv, Scalar, '$!value');
my $Tvalue :=
nqp::track('attr', $Trv, Scalar, '$!value');
nqp::guard('type', $Tvalue);
if nqp::istype_nd(
$value, nqp::gethllsym('Raku', 'Iterable')
) {
# Need to recont in order to preserve item nature.
# Shuffle in the recont code to invoke. We already
# read the deconted value, so we insert that as the
# arg so it needn't be dereferenced again.
nqp::delegate('boot-code-constant',
nqp::syscall('dispatcher-insert-arg-literal-obj',
nqp::syscall('dispatcher-replace-arg',
$capture, 0, $Tvalue
),
0, $recont
)
);
}
# Not an Iterable
else {
# Decont, so just evaluate to the read attr
# (boot-value ignores all but the first argument)
delegate-value($capture, $Tvalue);
}
}
# No descriptor, so read-only: identity will do
else {
nqp::delegate('boot-value', $capture);
}
}
else {
# Delegate to non-Scalar container fallback.
nqp::delegate('boot-code-constant',
nqp::syscall('dispatcher-insert-arg-literal-obj',
nqp::syscall('dispatcher-insert-arg-literal-obj',
$capture, 0, nqp::gethllsym('Raku', 'Iterable')
),
0, $container-fallback
)
);
}
}
else {
# Not containerized, so identity shall do.
# Unless it is null, then we map it to Mu.
nqp::isnull($rv)
?? delegate-constant($capture, Mu)
!! nqp::delegate('boot-value', $capture);
}
}
});
# This emulates a bug where Proxy was never decontainerized no
# matter what. The ecosystem came to depend on that, so we will
# accept it for now. We need to revisit this in the future.
# Expects the value to be in the first argument like raku-rv-decont.
nqp::register('raku-rv-decont-6c', -> $capture {
my $rv := nqp::captureposarg($capture, 0);
if nqp::eqaddr(nqp::what_nd($rv), Proxy) && nqp::isconcrete_nd($rv) {
guard-type-concreteness(nqp::track('arg', $capture, 0));
nqp::delegate('boot-value', $capture);
}
else {
nqp::delegate('raku-rv-decont', $capture);
}
});
}
#- raku-assign -----------------------------------------------------------------
# Assignment dispatcher, which case-analyzes assignments and provides
# optimized paths for a range of common situations, and typically lifting
# type checks to be guards.
{
# Shortcut for throwing type errors on assignment
sub assign-type-error($desc, $got) {
Perl6::Metamodel::Configuration.throw_or_die(
'X::TypeCheck::Assignment', "Type check failed in assignment",
:symbol($desc.name), :$desc, :$got, :expected($desc.of)
);
}
# Handler for when there is no optimization possible, moving all
# type checks to runtime
my $assign-fallback := -> $cont, $value {
nqp::assign($cont, $value)
}
# Handler for simplest container assignment that is not part of
# an array or hash (just bind value)
my $assign-scalar-no-whence-no-typecheck := -> $cont, $value {
nqp::bindattr($cont, Scalar, '$!value', $value)
}
# Handler for assigning Nil to a container that is not part of
# an array or hash, which will set the default value from the
# descriptor
my $assign-scalar-nil-no-whence := -> $cont, $value {
nqp::bindattr($cont, Scalar, '$!value',
nqp::getattr(
nqp::getattr($cont, Scalar, '$!descriptor'),
ContainerDescriptor,
'$!default'
)
)
}
# Handler for assigning a value to a container that is not part
# of an array or hash, with type check done at runtime.
my $assign-scalar-no-whence := -> $cont, $value {
my $desc := nqp::getattr($cont, Scalar, '$!descriptor');
my $type := nqp::getattr($desc, ContainerDescriptor, '$!of');
nqp::istype($value, $type)
?? nqp::bindattr($cont, Scalar, '$!value',
nqp::how_nd($type).archetypes($type).coercive
?? nqp::dispatch('raku-coercion', $type, $value)
!! $value
)
!! assign-type-error($desc, $value);
}
# Handler for assigning a value to a container that is to be
# part of an array. Binds the container to the array, normalizes
# the descriptor and sets the value without any typechecking.
my $assign-scalar-bindpos-no-typecheck := -> $cont, $value {
my $desc := nqp::getattr($cont, Scalar, '$!descriptor');
# Install the container in the array
nqp::bindpos(
nqp::getattr( $desc, ContainerDescriptor::BindArrayPos, '$!target'),
nqp::getattr_i($desc, ContainerDescriptor::BindArrayPos, '$!pos'),
$cont
);
# Initialization done, install "normal" descriptor
nqp::bindattr($cont, Scalar, '$!descriptor',
nqp::getattr(
$desc, ContainerDescriptor::BindArrayPos, '$!next-descriptor'
)
);
# Set the value
nqp::bindattr($cont, Scalar, '$!value', $value);
}
# Handler for assigning a value to a container that is to be
# part of an array with typechecking. Binds the container to
# the array, normalizes the descriptor and sets the value.
my $assign-scalar-bindpos := -> $cont, $value {
my $desc := nqp::getattr($cont, Scalar, '$!descriptor');
my $next := nqp::getattr(
$desc, ContainerDescriptor::BindArrayPos, '$!next-descriptor'
);
# Quit if typecheck failed
my $type := nqp::getattr($next, ContainerDescriptor, '$!of');
assign-type-error($next, $value) unless nqp::istype($value, $type);
# Install the container in the array
nqp::bindpos(
nqp::getattr($desc, ContainerDescriptor::BindArrayPos, '$!target'),
nqp::getattr_i($desc, ContainerDescriptor::BindArrayPos, '$!pos'),
$cont
);
# Install "normal" descriptor
nqp::bindattr($cont, Scalar, '$!descriptor', $next);
# Set the value
nqp::bindattr($cont, Scalar, '$!value',
nqp::how_nd($type).archetypes($type).coercive
?? nqp::dispatch('raku-coercion', $type, $value)
!! $value
);
}
# Handler for assigning a value to a container that is to be
# part of a two-dimensional array. Binds the container to the
# array, normalizes the descriptor and sets the value without
# any typechecking.
my $assign-scalar-bindpos2d-no-typecheck := -> $cont, $value {
my $desc := nqp::getattr($cont, Scalar, '$!descriptor');
# Install the container in the array
nqp::bindpos2d(
nqp::getattr( $desc, ContainerDescriptor::BindArrayPos2D, '$!target'),
nqp::getattr_i($desc, ContainerDescriptor::BindArrayPos2D, '$!one'),
nqp::getattr_i($desc, ContainerDescriptor::BindArrayPos2D, '$!two'),
$cont
);
# Initialization done, install "normal" descriptor
nqp::bindattr($cont, Scalar, '$!descriptor',
nqp::getattr(
$desc, ContainerDescriptor::BindArrayPos2D, '$!next-descriptor'
)
);
# Set the value
nqp::bindattr($cont, Scalar, '$!value', $value);
}
# Handler for assigning a value to a container that is to be
# part of a two-dimensional array with typechecking. Binds the
# container to the array, normalizes the descriptor and sets the
# value.
my $assign-scalar-bindpos2d := -> $cont, $value {
my $desc := nqp::getattr($cont, Scalar, '$!descriptor');
my $next := nqp::getattr(
$desc, ContainerDescriptor::BindArrayPos2D, '$!next-descriptor'
);
# Quit if typecheck failed
my $type := nqp::getattr($next, ContainerDescriptor, '$!of');
assign-type-error($next, $value) unless nqp::istype($value, $type);
# Install the container in the array
nqp::bindpos2d(
nqp::getattr($desc, ContainerDescriptor::BindArrayPos2D, '$!target'),
nqp::getattr_i($desc, ContainerDescriptor::BindArrayPos2D, '$!one'),
nqp::getattr_i($desc, ContainerDescriptor::BindArrayPos2D, '$!two'),
$cont
);
# Install "normal" descriptor
nqp::bindattr($cont, Scalar, '$!descriptor', $next);
# Set the value
nqp::bindattr($cont, Scalar, '$!value',
nqp::how_nd($type).archetypes($type).coercive
?? nqp::dispatch('raku-coercion', $type, $value)
!! $value
);
}
# Handler for assigning a value to a container that is to be
# part of a three-dimensional array. Binds the container to the
# array, normalizes the descriptor and sets the value without
# any typechecking.
my $assign-scalar-bindpos3d-no-typecheck := -> $cont, $value {
my $desc := nqp::getattr($cont, Scalar, '$!descriptor');
# Install the container in the array
nqp::bindpos3d(
nqp::getattr( $desc, ContainerDescriptor::BindArrayPos3D, '$!target'),
nqp::getattr_i($desc, ContainerDescriptor::BindArrayPos3D, '$!one'),
nqp::getattr_i($desc, ContainerDescriptor::BindArrayPos3D, '$!two'),
nqp::getattr_i($desc, ContainerDescriptor::BindArrayPos3D, '$!three'),
$cont
);
# Initialization done, install "normal" descriptor
nqp::bindattr($cont, Scalar, '$!descriptor',
nqp::getattr(
$desc, ContainerDescriptor::BindArrayPos3D, '$!next-descriptor'
)
);
# Set the value
nqp::bindattr($cont, Scalar, '$!value', $value);
}
# Handler for assigning a value to a container that is to be
# part of a three-dimensional array with typechecking. Binds the
# container to the array, normalizes the descriptor and sets the
# value.
my $assign-scalar-bindpos3d := -> $cont, $value {
my $desc := nqp::getattr($cont, Scalar, '$!descriptor');
my $next := nqp::getattr(
$desc, ContainerDescriptor::BindArrayPos3D, '$!next-descriptor'
);
# Quit if typecheck failed
my $type := nqp::getattr($next, ContainerDescriptor, '$!of');
assign-type-error($next, $value) unless nqp::istype($value, $type);
# Install the container in the array
nqp::bindpos3d(
nqp::getattr($desc, ContainerDescriptor::BindArrayPos3D, '$!target'),
nqp::getattr_i($desc, ContainerDescriptor::BindArrayPos3D, '$!one'),
nqp::getattr_i($desc, ContainerDescriptor::BindArrayPos3D, '$!two'),
nqp::getattr_i($desc, ContainerDescriptor::BindArrayPos3D, '$!three'),
$cont
);
# Install "normal" descriptor
nqp::bindattr($cont, Scalar, '$!descriptor', $next);
# Set the value
nqp::bindattr($cont, Scalar, '$!value',
nqp::how_nd($type).archetypes($type).coercive
?? nqp::dispatch('raku-coercion', $type, $value)
!! $value
);
}
# Handler for assigning a value to a container that is to be
# part of an N-dimensional array. Binds the container to the
# array, normalizes the descriptor and sets the value without
# any typechecking.
my $assign-scalar-bindposnd-no-typecheck := -> $cont, $value {
my $desc := nqp::getattr($cont, Scalar, '$!descriptor');
# Install the container in the array
nqp::bindposnd(
nqp::getattr( $desc, ContainerDescriptor::BindArrayPosND, '$!target'),
nqp::getattr($desc, ContainerDescriptor::BindArrayPosND, '$!idxs'),
$cont
);
# Initialization done, install "normal" descriptor
nqp::bindattr($cont, Scalar, '$!descriptor',
nqp::getattr(
$desc, ContainerDescriptor::BindArrayPosND, '$!next-descriptor'
)
);
# Set the value
nqp::bindattr($cont, Scalar, '$!value', $value);
}
# Handler for assigning a value to a container that is to be
# part of an N-dimensional array with typechecking. Binds the
# container to the array, normalizes the descriptor and sets the
# value.
my $assign-scalar-bindposnd := -> $cont, $value {
my $desc := nqp::getattr($cont, Scalar, '$!descriptor');
my $next := nqp::getattr(
$desc, ContainerDescriptor::BindArrayPosND, '$!next-descriptor'
);
# Quit if typecheck failed
my $type := nqp::getattr($next, ContainerDescriptor, '$!of');
assign-type-error($next, $value) unless nqp::istype($value, $type);
# Install the container in the array
nqp::bindposnd(
nqp::getattr($desc, ContainerDescriptor::BindArrayPosND, '$!target'),
nqp::getattr($desc, ContainerDescriptor::BindArrayPosND, '$!idxs'),
$cont
);
# Install "normal" descriptor
nqp::bindattr($cont, Scalar, '$!descriptor', $next);
# Set the value
nqp::bindattr($cont, Scalar, '$!value',
nqp::how_nd($type).archetypes($type).coercive
?? nqp::dispatch('raku-coercion', $type, $value)
!! $value
);
}
# Handler for assigning a value to a container that is to be
# part of a hash. Binds the container to the hash, normalizes
# the descriptor and sets the value without any typechecking.
my $assign-scalar-bindkey-no-typecheck := -> $cont, $value {
my $desc := nqp::getattr($cont, Scalar, '$!descriptor');
# Install the container in the hash
nqp::bindkey(
nqp::getattr($desc, ContainerDescriptor::BindHashKey, '$!target'),
nqp::getattr($desc, ContainerDescriptor::BindHashKey, '$!key'),
$cont
);
# Initialization done, install "normal" descriptor
nqp::bindattr($cont, Scalar, '$!descriptor',
nqp::getattr(
$desc, ContainerDescriptor::BindHashKey, '$!next-descriptor'
)
);
# Set the value
nqp::bindattr($cont, Scalar, '$!value', $value);
}
# Handler for assigning a value to a container that is to be
# part of a hash with typechecking. Binds the container to
# the hash, normalizes the descriptor and sets the value.
my $assign-scalar-bindkey := -> $cont, $value {
my $desc := nqp::getattr($cont, Scalar, '$!descriptor');
my $next := nqp::getattr(
$desc, ContainerDescriptor::BindHashKey, '$!next-descriptor'
);
# Quit if typecheck failed
my $type := nqp::getattr($next, ContainerDescriptor, '$!of');
assign-type-error($next, $value) unless nqp::istype($value, $type);
# Install the container in the hash
nqp::bindkey(
nqp::getattr($desc, ContainerDescriptor::BindHashKey, '$!target'),
nqp::getattr($desc, ContainerDescriptor::BindHashKey, '$!key'),
$cont
);
# Install "normal" descriptor
nqp::bindattr($cont, Scalar, '$!descriptor', $next);
# Set the value
nqp::bindattr($cont, Scalar, '$!value',
nqp::how_nd($type).archetypes($type).coercive
?? nqp::dispatch('raku-coercion', $type, $value)
!! $value
);
}
# Handler for assigning a value to a container that is to be
# part of an object hash. Binds the container to the hash,
# normalizes the descriptor and sets the value without any
# typechecking.
my $assign-scalar-obj-bindkey-no-typecheck := -> $cont, $value {
my $desc := nqp::getattr($cont, Scalar, '$!descriptor');
# Install the container in the object hash
nqp::bindkey(
nqp::getattr($desc, ContainerDescriptor::BindObjHashKey, '$!target'),
nqp::getattr($desc, ContainerDescriptor::BindObjHashKey, '$!which'),
nqp::getattr(
$desc, ContainerDescriptor::BindObjHashKey, '$!pair'
).new(
nqp::getattr($desc, ContainerDescriptor::BindObjHashKey, '$!key'),
$cont
)
);
# Initialization done, install "normal" descriptor
nqp::bindattr($cont, Scalar, '$!descriptor',
nqp::getattr(
$desc, ContainerDescriptor::BindObjHashKey, '$!next-descriptor'
)
);
# Set the value
nqp::bindattr($cont, Scalar, '$!value', $value);
}
# Handler for assigning a value to a container that is to be part
# of an object hash with typechecking. Binds the container to
# the object hash, normalizes the descriptor and sets the value.
my $assign-scalar-obj-bindkey := -> $cont, $value {
my $desc := nqp::getattr($cont, Scalar, '$!descriptor');
my $next := nqp::getattr(
$desc, ContainerDescriptor::BindObjHashKey, '$!next-descriptor'
);
# Quit if typecheck failed
my $type := nqp::getattr($next, ContainerDescriptor, '$!of');
assign-type-error($next, $value) unless nqp::istype($value, $type);
# Install the container in the object hash
nqp::bindkey(
nqp::getattr($desc, ContainerDescriptor::BindObjHashKey, '$!target'),
nqp::getattr($desc, ContainerDescriptor::BindObjHashKey, '$!which'),
nqp::getattr(
$desc, ContainerDescriptor::BindObjHashKey, '$!pair'
).new(
nqp::getattr($desc, ContainerDescriptor::BindObjHashKey, '$!key'),
$cont
)
);
# Install "normal" descriptor
nqp::bindattr($cont, Scalar, '$!descriptor', $next);
# Set the value
nqp::bindattr($cont, Scalar, '$!value',
nqp::how_nd($type).archetypes($type).coercive
?? nqp::dispatch('raku-coercion', $type, $value)
!! $value
);
}
# Handler for initializing an attribute. The container is
# assumed to have been bound to its object already.
my $assign-scalar-uninit-no-typecheck := -> $cont, $value {
# Install "normal" descriptor
nqp::bindattr($cont, Scalar, '$!descriptor',
nqp::getattr(
nqp::getattr($cont, Scalar, '$!descriptor'),
ContainerDescriptor::UninitializedAttribute,
'$!next-descriptor'
)
);
# Set the value
nqp::bindattr($cont, Scalar, '$!value', $value);
}
# Handler for initializing an attribute with typecheck. The
# container is assumed to have been bound to its object already.
my $assign-scalar-uninit := -> $cont, $value {
my $desc := nqp::getattr($cont, Scalar, '$!descriptor');
my $next := nqp::getattr(
$desc,
ContainerDescriptor::UninitializedAttribute,
'$!next-descriptor'
);
# Quit if typecheck failed
my $type := nqp::getattr($next, ContainerDescriptor, '$!of');
assign-type-error($next, $value) unless nqp::istype($value, $type);
# Install "normal" descriptor
nqp::bindattr($cont, Scalar, '$!descriptor', $next);
# Set the value
nqp::bindattr($cont, Scalar, '$!value',
nqp::how_nd($type).archetypes($type).coercive
?? nqp::dispatch('raku-coercion', $type, $value)
!! $value
);
}
# Dispatcher for assignment in Raku. Expects a capture with the
# container as its first argument, and the value as the second
# argument.
nqp::register('raku-assign', -> $capture {
# Whatever we do, we'll guard on the type of the container
# and its concreteness
my $cont := nqp::captureposarg($capture, 0);
my $Tcont := nqp::track('arg', $capture, 0);
guard-type-concreteness($Tcont);
# Final handler to be delegated to
my $handler := $assign-fallback;
if nqp::eqaddr(nqp::what_nd($cont), Scalar) # it's a container
&& nqp::isconcrete_nd($cont) # and it's concrete
&& nqp::isconcrete( # and descriptor ok
my $desc := nqp::getattr($cont, Scalar, '$!descriptor')
) {
my $desc-WHAT := $desc.WHAT;
my $Tdesc := nqp::track('attr', $Tcont, Scalar, '$!descriptor');
my $value := nqp::captureposarg($capture, 1);
my $Tvalue := nqp::track('arg', $capture, 1);
# Helper sub for handling container descriptors that have a
# whence, aka that handle containers in an array or a hash
sub delegate-whence($no-typecheck, $typecheck) {
nqp::guard('type', $Tdesc);
my $next := nqp::getattr($desc, $desc-WHAT, '$!next-descriptor');
if nqp::eqaddr($next.WHAT, ContainerDescriptor) ||
nqp::eqaddr($next.WHAT, ContainerDescriptor::Untyped) {
# Ensure we're not assigning Nil. (This would be very odd,
# as a Scalar starts off with its default value, and if we
# are vivifying we'll likely have a new container).
unless nqp::eqaddr($value.WHAT, Nil) {
# Go by whether we can type check the target.
nqp::guard('literal', nqp::track(
'attr', $Tdesc, $desc-WHAT, '$!next-descriptor'
));
nqp::guard('type', $Tvalue);
my $of := $next.of;
$handler := $of.HOW.archetypes.nominal
&& (nqp::eqaddr($of, Mu) || nqp::istype($value, $of))
?? $no-typecheck
!! $typecheck;
}
}
}
# A simple container assignment that contains type information
if nqp::eqaddr($desc-WHAT, ContainerDescriptor) {
my $of := nqp::getattr($desc, ContainerDescriptor, '$!of');
my $nominal := $of.HOW.archetypes.nominal;
# Need to set default value
if nqp::eqaddr($value, Nil) {
# Copy in the default, provided we've a simple type.
if $nominal {
nqp::guard('type', $Tvalue);
nqp::guard('type', $Tdesc);
nqp::guard('literal',
nqp::track('attr',$Tdesc,ContainerDescriptor,'$!of')
);
$handler := $assign-scalar-nil-no-whence;
}
}
# No whence, no Nil. Is it a nominal type? If yes, we can
# check it here.
else {
nqp::guard('type', $Tdesc);
if $nominal && nqp::istype($value, $of) {
# Nominal and passes type check; stack up guards and
# delegate to simple bind.
nqp::guard('literal',
nqp::track('attr',$Tdesc,ContainerDescriptor,'$!of')
);
nqp::guard('type', $Tvalue);
$handler := $assign-scalar-no-whence-no-typecheck;
}
else {
# Non-nominal or type check error.
nqp::guard('not-literal-obj', $Tvalue, Nil);
$handler := $assign-scalar-no-whence;
}
}
}
# A container assignment without type information
elsif nqp::eqaddr($desc-WHAT, ContainerDescriptor::Untyped) {
if nqp::eqaddr($value, Nil) {
# Nil case is NYI.
}
else {
# Assignment to an untyped container descriptor;
# no type check is required, just bind the value
# into place.
nqp::guard('type', $Tdesc);
nqp::guard('not-literal-obj', $Tvalue, Nil);
$handler := $assign-scalar-no-whence-no-typecheck;
}
}
# An attribute initialization
elsif nqp::eqaddr(
$desc-WHAT,
ContainerDescriptor::UninitializedAttribute
) {
delegate-whence(
$assign-scalar-uninit-no-typecheck,
$assign-scalar-uninit
);
}
# An array element initialization
elsif nqp::eqaddr($desc-WHAT, ContainerDescriptor::BindArrayPos) {
delegate-whence(
$assign-scalar-bindpos-no-typecheck,
$assign-scalar-bindpos
);
}
# A hash element initialization
elsif nqp::eqaddr($desc-WHAT, ContainerDescriptor::BindHashKey) {
delegate-whence(
$assign-scalar-bindkey-no-typecheck,
$assign-scalar-bindkey
);
}
# An object hash element initialization
elsif nqp::eqaddr($desc-WHAT, ContainerDescriptor::BindObjHashKey) {
delegate-whence(
$assign-scalar-obj-bindkey-no-typecheck,
$assign-scalar-obj-bindkey
);
}
# A two-dimensional array element initialization
elsif nqp::eqaddr($desc-WHAT, ContainerDescriptor::BindArrayPos2D) {
delegate-whence(
$assign-scalar-bindpos2d-no-typecheck,
$assign-scalar-bindpos2d
);
}
# A three-dimensional array element initialization
elsif nqp::eqaddr($desc-WHAT, ContainerDescriptor::BindArrayPos3D) {
delegate-whence(
$assign-scalar-bindpos3d-no-typecheck,
$assign-scalar-bindpos3d
);
}
# An N-dimensional array element initialization
elsif nqp::eqaddr($desc-WHAT, ContainerDescriptor::BindArrayPosND) {
delegate-whence(
$assign-scalar-bindposnd-no-typecheck,
$assign-scalar-bindposnd
);
}
}
# Do the delegation with the indicated handler
nqp::delegate('boot-code-constant',
nqp::syscall('dispatcher-insert-arg-literal-obj',
$capture, 0, $handler
)
);
});
}
#- raku-bind-assert ------------------------------------------------------------
# Binding type assertion dispatcher, used to do type checks of binds.
# Evaluates to the value itself when the type check passes, installing
# a guard along the way. Otherwise, throws.
{
# Helper sub to throw type check error
sub bind-error($got, $expected) {
Perl6::Metamodel::Configuration.throw_or_die(
'X::TypeCheck::Binding',
"Type check failed in binding; expected '"
~ nqp::how_nd($expected).name($expected)
~ "' but got '"
~ nqp::how_nd($got).name($got)
~ "'",
:$got, :$expected
);
}
# The run-time checker
my $bind-check := -> $value, $value-decont, $type {
nqp::istype_nd($value-decont, $type)
?? $value
!! bind-error($value, $type)
}
# Actual dispatcher Expects the original value as the first argument,
# the deconted value as the second, and the type to be checked against
# as the third argument.
nqp::register('raku-bind-assert', -> $capture {
my $value-decont := nqp::captureposarg($capture, 1);
my $type := nqp::captureposarg($capture, 2);
# Nominal, so a type guard on the decont'd value will suffice,
# then produce the original value.
if nqp::how_nd($type).archetypes.nominal {
# Type is ok
if nqp::istype_nd($value-decont, $type) {
nqp::guard('type', nqp::track('arg', $capture, 1));
nqp::guard('type', nqp::track('arg', $capture, 2));
nqp::delegate('boot-value', $capture);
}
# Not ok
else {
bind-error(nqp::captureposarg($capture, 0), $type);
}
}
# Not a nominal type, can't guard it, so set up a call to do the
# check late-bound.
else {
nqp::guard('type', nqp::track('arg', $capture, 2));
nqp::delegate('boot-code-constant',
nqp::syscall('dispatcher-insert-arg-literal-obj',
$capture, 0, $bind-check
)
);
}
});
}
#- raku-is-attr-inited ---------------------------------------------------------
# Object construction time checking of if a container is initialized. Done as
# a dispatcher primarily to intern .^mixin_type, but also for more compact
# bytecode size in generated BUILDALL.
# Array initialization checker
my $array-init-check := -> $arr {
my $storage := nqp::getattr($arr, List, '$!reified');
nqp::isconcrete($storage) && nqp::elems($storage)
}
# Hash initialization checker
my $hash-init-check := -> $hash {
my $storage := nqp::getattr($hash, Map, '$!storage');
nqp::isconcrete($storage) && nqp::elems($storage)
}
# Actual dispatcher taking the attribute and returning 1 if initialized,
# and 0 if not.
nqp::register('raku-is-attr-inited', -> $capture {
# If there's a non-concrete object observed, then we bound a non-container
# in place, so trivially initialized.
my $attr := nqp::captureposarg($capture, 0);
my $Tattr := nqp::track('arg', $capture, 0);
my int $inited;
my $elem-check;
# Might be a container that was assigned.
if nqp::isconcrete_nd($attr) {
# Just try and read a descriptor. Also see if we have an array or
# hash, which needs an additional element check to handle an
# assignment to an individual element during BUILD.
my $base;
my $desc;
my $Tdesc;
try {
$base := nqp::how_nd($attr).mixin_base($attr);
$desc := nqp::getattr($attr, $base, '$!descriptor');
$Tdesc := nqp::track('attr', $Tattr, $base, '$!descriptor');
}
# If we managed to track a descriptor, then we have a container to
# see if was uninitialized. The attribute tracking above will have
# established type/concreteness guards on the attribute, so don't
# repeat them.
if $Tdesc {
# Guard on the descriptor type, then outcome depends on if
# it's an uninitialized attribute descriptor. If it is, then
# for arrays and hashes we also need an extra check.
nqp::guard('type', $Tdesc);
$inited := !nqp::eqaddr(
$desc.WHAT, ContainerDescriptor::UninitializedAttribute
);
if nqp::istype($base, Array) {
$elem-check := $array-init-check;
}
elsif nqp::istype($base, Hash) {
$elem-check := $hash-init-check;
}
}
# Otherwise, bound concrete value. Guard on type and concreteness,
# outcome is that it's initialized.
else {
guard-type-concreteness($Tattr);
$inited := 1;
}
}
# Nothing concrete, so no container yet
else {
nqp::guard('concreteness', $Tattr);
$inited := 1;
}
$inited || !nqp::isconcrete($elem-check)
# Initialized per the descriptor, or no additional check needed
?? nqp::delegate('boot-constant',
nqp::syscall('dispatcher-insert-arg-literal-int',
$capture, 0, $inited
)
)
# The descriptor suggests it's not initialized by assignment to
# the entire array/hash, but individual elements may have been.
!! nqp::delegate('boot-code-constant',
nqp::syscall('dispatcher-insert-arg-literal-obj',
$capture, 0, $elem-check
)
);
});
#- raku-sink -------------------------------------------------------------------
# Sink dispatcher. Called in void context with the value to sink, possibly
# inside a container.
nqp::register('raku-sink', -> $capture {
# Guard on the type and concreteness.
my $sinkee := nqp::captureposarg($capture, 0);
guard-type-concreteness(nqp::track('arg', $capture, 0));
# Now consider what we're sinking.
if nqp::isconcrete_nd($sinkee) {
# Concrete. See if it has a `sink` method, and then also if it's not
# Mu.sink, which is a no-op
my $sink := nqp::decont(
nqp::how_nd($sinkee).find_method($sinkee, 'sink')
);
# A non-standard .sink method
if nqp::isconcrete($sink)
&& !nqp::eqaddr($sink, Mu.HOW.find_method(Mu, 'sink')) {
# Need to actually do a call to the sink method. Since sink