/
BOOTSTRAP.nqp
3054 lines (2860 loc) Β· 139 KB
/
BOOTSTRAP.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
use Perl6::Metamodel;
use QRegex;
# Here we start to piece together the top of the object model hierarchy.
# We can't just declare these bits in CORE.setting with normal Perl 6
# syntax due to circularity issues. Note that we don't compose any of
# these - which is equivalent to a { ... } body.
#
# One particular circularity we break here is that you can't have
# inheritance in Perl 6 without traits, but that needs multiple
# dispatch, which can't function without some a type hierarchy in
# place. It also needs us to be able to declare a signature with
# parameters and a code objects with dispatchees, which in turn need
# attributes. So, we set up quite a few bits in here, though the aim
# is to keep it "lagom". :-)
# Bootstrapping Attribute class that we eventually replace with the real
# one.
my class BOOTSTRAPATTR {
has $!name;
has $!type;
has $!box_target;
has $!package;
has $!inlined;
method name() { $!name }
method type() { $!type }
method box_target() { $!box_target }
method package() { $!package }
method inlined() { $!inlined }
method has_accessor() { 0 }
method has-accessor() { 0 }
method positional_delegate() { 0 }
method associative_delegate() { 0 }
method build() { }
method is_generic() { $!type.HOW.archetypes.generic }
method instantiate_generic($type_environment) {
my $ins := $!type.HOW.instantiate_generic($!type, $type_environment);
self.new(:name($!name), :box_target($!box_target), :type($ins))
}
method compose($obj) { }
}
# Stub all types.
my stub Mu metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Any metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Nil metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Cool metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Attribute metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Scalar metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Proxy metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Signature metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Parameter metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Code metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Block metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Routine metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Sub metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Method metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Submethod metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Regex metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Str metaclass Perl6::Metamodel::ClassHOW { ... };
my knowhow bigint is repr('P6bigint') { }
my stub Int metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Num metaclass Perl6::Metamodel::ClassHOW { ... };
my stub List metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Slip metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Array metaclass Perl6::Metamodel::ClassHOW { ... };
my stub EnumMap metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Hash metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Capture metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Bool metaclass Perl6::Metamodel::ClassHOW { ... };
my stub ObjAt metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Stash metaclass Perl6::Metamodel::ClassHOW { ... };
my stub PROCESS metaclass Perl6::Metamodel::ModuleHOW { ... };
my stub Grammar metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Junction metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Metamodel metaclass Perl6::Metamodel::PackageHOW { ... };
my stub ForeignCode metaclass Perl6::Metamodel::ClassHOW { ... };
my stub IntLexRef metaclass Perl6::Metamodel::NativeRefHOW { ... };
my stub NumLexRef metaclass Perl6::Metamodel::NativeRefHOW { ... };
my stub StrLexRef metaclass Perl6::Metamodel::NativeRefHOW { ... };
my stub IntAttrRef metaclass Perl6::Metamodel::NativeRefHOW { ... };
my stub NumAttrRef metaclass Perl6::Metamodel::NativeRefHOW { ... };
my stub StrAttrRef metaclass Perl6::Metamodel::NativeRefHOW { ... };
my stub IntPosRef metaclass Perl6::Metamodel::NativeRefHOW { ... };
my stub NumPosRef metaclass Perl6::Metamodel::NativeRefHOW { ... };
my stub StrPosRef metaclass Perl6::Metamodel::NativeRefHOW { ... };
#?if moar
# On MoarVM, implement the signature binder.
my class Binder {
# Flags that can be set on a signature element.
my int $SIG_ELEM_BIND_CAPTURE := 1;
my int $SIG_ELEM_BIND_PRIVATE_ATTR := 2;
my int $SIG_ELEM_BIND_PUBLIC_ATTR := 4;
my int $SIG_ELEM_BIND_ATTRIBUTIVE := ($SIG_ELEM_BIND_PRIVATE_ATTR +| $SIG_ELEM_BIND_PUBLIC_ATTR);
my int $SIG_ELEM_SLURPY_POS := 8;
my int $SIG_ELEM_SLURPY_NAMED := 16;
my int $SIG_ELEM_SLURPY_LOL := 32;
my int $SIG_ELEM_SLURPY := ($SIG_ELEM_SLURPY_POS +| SIG_ELEM_SLURPY_NAMED +| $SIG_ELEM_SLURPY_LOL);
my int $SIG_ELEM_INVOCANT := 64;
my int $SIG_ELEM_MULTI_INVOCANT := 128;
my int $SIG_ELEM_IS_RW := 256;
my int $SIG_ELEM_IS_COPY := 512;
my int $SIG_ELEM_IS_PARCEL := 1024;
my int $SIG_ELEM_IS_OPTIONAL := 2048;
my int $SIG_ELEM_ARRAY_SIGIL := 4096;
my int $SIG_ELEM_HASH_SIGIL := 8192;
my int $SIG_ELEM_DEFAULT_FROM_OUTER := 16384;
my int $SIG_ELEM_IS_CAPTURE := 32768;
my int $SIG_ELEM_UNDEFINED_ONLY := 65536;
my int $SIG_ELEM_DEFINED_ONLY := 131072;
my int $SIG_ELEM_DEFINEDNES_CHECK := ($SIG_ELEM_UNDEFINED_ONLY +| $SIG_ELEM_DEFINED_ONLY);
my int $SIG_ELEM_NOMINAL_GENERIC := 524288;
my int $SIG_ELEM_DEFAULT_IS_LITERAL := 1048576;
my int $SIG_ELEM_NATIVE_INT_VALUE := 2097152;
my int $SIG_ELEM_NATIVE_NUM_VALUE := 4194304;
my int $SIG_ELEM_NATIVE_STR_VALUE := 8388608;
my int $SIG_ELEM_NATIVE_VALUE := ($SIG_ELEM_NATIVE_INT_VALUE +| $SIG_ELEM_NATIVE_NUM_VALUE +| $SIG_ELEM_NATIVE_STR_VALUE);
# Binding reuslt flags.
my int $BIND_RESULT_OK := 0;
my int $BIND_RESULT_FAIL := 1;
my int $BIND_RESULT_JUNCTION := 2;
my $autothreader;
my $Positional;
my $PositionalBindFailover;
sub arity_fail($params, int $num_params, int $num_pos_args, int $too_many) {
my str $error_prefix := $too_many ?? "Too many" !! "Too few";
my int $count;
my int $arity;
my int $param_i := 0;
while $param_i < $num_params {
my $param := nqp::atpos($params, $param_i);
my int $flags := nqp::getattr_i($param, Parameter, '$!flags');
if !nqp::isnull(nqp::getattr($param, Parameter, '$!named_names')) {
}
elsif $flags +& $SIG_ELEM_SLURPY_NAMED {
}
elsif $flags +& $SIG_ELEM_SLURPY_POS {
$count--;
}
elsif $flags +& $SIG_ELEM_IS_OPTIONAL {
$count++
}
else {
$count++;
$arity++;
}
$param_i++;
}
my str $s := $arity == 1 ?? "" !! "s";
if $arity == $count {
return "$error_prefix positionals passed; expected $arity argument$s but got $num_pos_args";
} elsif $count == -1 {
return "$error_prefix positionals passed; expected at least $arity argument$s but got only $num_pos_args";
} else {
my str $conj := $count == $arity+1 ?? "or" !! "to";
return "$error_prefix positionals passed; expected $arity $conj $count arguments but got $num_pos_args";
}
}
method set_autothreader($callable) {
$autothreader := $callable;
}
method set_pos_bind_failover($pos, $pos_bind_failover) {
$Positional := $pos;
$PositionalBindFailover := $pos_bind_failover;
}
# Binds a single parameter.
sub bind_one_param($lexpad, $sig, $param, int $no_nom_type_check, $error,
int $got_native, $oval, int $ival, num $nval, str $sval) {
# Grab flags and variable name.
my int $flags := nqp::getattr_i($param, Parameter, '$!flags');
my str $varname := nqp::getattr_s($param, Parameter, '$!variable_name');
my int $has_varname := 1;
if nqp::isnull_s($varname) {
$varname := '<anon>';
$has_varname := 0;
}
# Check if boxed/unboxed expections are met.
my int $desired_native := $flags +& $SIG_ELEM_NATIVE_VALUE;
my int $is_rw := $flags +& $SIG_ELEM_IS_RW;
if $is_rw && $desired_native {
if $desired_native == $SIG_ELEM_NATIVE_INT_VALUE {
unless !$got_native && nqp::iscont_i($oval) {
if nqp::defined($error) {
$error[0] := "Expected a native int argument for '$varname'";
}
return $BIND_RESULT_FAIL;
}
}
elsif $desired_native == $SIG_ELEM_NATIVE_NUM_VALUE {
unless !$got_native && nqp::iscont_n($oval) {
if nqp::defined($error) {
$error[0] := "Expected a native num argument for '$varname'";
}
return $BIND_RESULT_FAIL;
}
}
elsif $desired_native == $SIG_ELEM_NATIVE_STR_VALUE {
unless !$got_native && nqp::iscont_s($oval) {
if nqp::defined($error) {
$error[0] := "Expected a native str argument for '$varname'";
}
return $BIND_RESULT_FAIL;
}
}
}
elsif $desired_native != $got_native {
# Maybe we need to box the native.
if $desired_native == 0 {
if $got_native == $SIG_ELEM_NATIVE_INT_VALUE {
$oval := nqp::box_i($ival, Int);
}
elsif $got_native == $SIG_ELEM_NATIVE_NUM_VALUE {
$oval := nqp::box_n($nval, Num);
}
else {
$oval := nqp::box_s($sval, Str);
}
$got_native := 0;
}
# Otherwise, maybe we need to unbox.
elsif !$got_native {
# XXX Probably want to do this a little differently to get a
# better error.
if $desired_native == $SIG_ELEM_NATIVE_INT_VALUE {
$ival := nqp::unbox_i($oval);
$got_native := $SIG_ELEM_NATIVE_INT_VALUE;
}
elsif $desired_native == $SIG_ELEM_NATIVE_NUM_VALUE {
$nval := nqp::unbox_n($oval);
$got_native := $SIG_ELEM_NATIVE_NUM_VALUE;
}
else {
$sval := nqp::unbox_s($oval);
$got_native := $SIG_ELEM_NATIVE_STR_VALUE;
}
}
# Otherwise, incompatible native types.
else {
if nqp::defined($error) {
$error[0] := "Incompatible native type passed for '$varname'";
}
return $BIND_RESULT_FAIL;
}
}
# By this point, we'll either have an object that we might be able to
# bind if it passes the type check, or a native value that needs no
# further checking.
my $nom_type;
unless $got_native || ($is_rw && $desired_native) {
# HLL-ize.
$oval := nqp::hllizefor($oval, 'perl6');
# Skip nominal type check if not needed.
unless $no_nom_type_check {
# Is the nominal type generic and in need of instantiation? (This
# can happen in (::T, T) where we didn't learn about the type until
# during the signature bind).
$nom_type := nqp::getattr($param, Parameter, '$!nominal_type');
if $flags +& $SIG_ELEM_NOMINAL_GENERIC {
$nom_type := $nom_type.HOW.instantiate_generic($nom_type, $lexpad);
}
# If the expected type is Positional, see if we need to do the
# positional bind failover.
if nqp::istype($nom_type, $Positional) && nqp::istype($oval, $PositionalBindFailover) {
$oval := $oval.list;
}
# If not, do the check. If the wanted nominal type is Mu, then
# anything goes.
unless $nom_type =:= Mu || nqp::istype($oval, $nom_type) {
# Type check failed; produce error if needed.
if nqp::defined($error) {
my %ex := nqp::gethllsym('perl6', 'P6EX');
if nqp::isnull(%ex) || !nqp::existskey(%ex, 'X::TypeCheck::Binding') {
$error[0] := "Nominal type check failed for parameter '" ~ $varname ~
"'; expected " ~ $nom_type.HOW.name($nom_type) ~
" but got " ~ $oval.HOW.name($oval);
} else {
$error[0] := { nqp::atkey(%ex, 'X::TypeCheck::Binding')($oval, $nom_type.WHAT, $varname) };
}
}
# Report junction failure mode if it's a junction.
return $oval.WHAT =:= Junction && nqp::isconcrete($oval)
?? $BIND_RESULT_JUNCTION
!! $BIND_RESULT_FAIL;
}
# Also enforce definedness constraints.
if $flags +& $SIG_ELEM_DEFINEDNES_CHECK {
if $flags +& $SIG_ELEM_UNDEFINED_ONLY && nqp::isconcrete($oval) {
if nqp::defined($error) {
my $class := $oval.HOW.name($oval);
my $what := $flags +& $SIG_ELEM_INVOCANT
?? "Invocant"
!! "Parameter '$varname'";
$error[0] := "$what requires a type object of type $class, but an object instance was passed. Did you forget a 'multi'?";
}
return $oval.WHAT =:= Junction && nqp::isconcrete($oval)
?? $BIND_RESULT_JUNCTION
!! $BIND_RESULT_FAIL;
}
if $flags +& $SIG_ELEM_DEFINED_ONLY && !nqp::isconcrete($oval) {
if nqp::defined($error) {
my $class := $oval.HOW.name($oval);
my $what := $flags +& $SIG_ELEM_INVOCANT
?? "Invocant"
!! "Parameter '$varname'";
$error[0] := "$what requires an instance of type $class, but a type object was passed. Did you forget a .new?";
}
return $oval.WHAT =:= Junction && nqp::isconcrete($oval)
?? $BIND_RESULT_JUNCTION
!! $BIND_RESULT_FAIL;
}
}
}
}
# Do we have any type captures to bind?
my $type_caps := nqp::getattr($param, Parameter, '$!type_captures');
unless nqp::isnull($type_caps) {
my int $num_type_caps := nqp::elems($type_caps);
my int $i := 0;
while $i < $num_type_caps {
nqp::bindkey($lexpad, nqp::atpos($type_caps, $i), $oval.WHAT);
$i++;
}
}
# Do a coercion, if one is needed.
my $coerce_type := nqp::getattr($param, Parameter, '$!coerce_type');
unless nqp::isnull($coerce_type) {
# Coercing natives not possible - nothing to call a method on.
if $got_native {
if nqp::defined($error) {
$error[0] := "Unable to coerce natively typed parameter '$varname'";
}
return $BIND_RESULT_FAIL;
}
# Only coerce if we don't already have the correct type.
unless nqp::istype($oval, $coerce_type) {
my $coerce_method := nqp::getattr($param, Parameter, '$!coerce_method');
if nqp::can($oval, $coerce_method) {
$oval := $oval."$coerce_method"();
}
else {
# No coercion method availale; whine and fail to bind.
if nqp::defined($error) {
$error[0] := "Unable to coerce value for '$varname' from " ~
$oval.HOW.name($oval) ~
" to $coerce_method; no coercion method defined";
}
return $BIND_RESULT_FAIL;
}
}
}
# If it's not got attributive binding, we'll go about binding it into the
# lex pad.
my int $is_attributive := $flags +& $SIG_ELEM_BIND_ATTRIBUTIVE;
unless $is_attributive || !$has_varname {
# Is it native? If so, just go ahead and bind it.
if $got_native {
if $got_native == $SIG_ELEM_NATIVE_INT_VALUE {
nqp::bindkey_i($lexpad, $varname, $ival);
}
elsif $got_native == $SIG_ELEM_NATIVE_NUM_VALUE {
nqp::bindkey_n($lexpad, $varname, $nval);
}
else {
nqp::bindkey_s($lexpad, $varname, $sval);
}
}
# Otherwise it's some objecty case.
elsif $is_rw {
# XXX TODO Check if rw flag is set; also need to have a
# wrapper container that carries extra constraints.
nqp::bindkey($lexpad, $varname, $oval);
}
elsif $flags +& $SIG_ELEM_IS_PARCEL {
# Just bind the thing as is into the lexpad.
nqp::bindkey($lexpad, $varname, $oval);
}
else {
# If it's an array, copy means make a new one and store,
# and a normal bind is a straightforward binding.
if $flags +& $SIG_ELEM_ARRAY_SIGIL {
if $flags +& $SIG_ELEM_IS_COPY {
# XXX GLR
nqp::die('replace this Array is copy logic');
# my $bindee := nqp::create(Array);
# $bindee.STORE(nqp::decont($oval));
# nqp::bindkey($lexpad, $varname, $bindee);
}
else {
nqp::bindkey($lexpad, $varname, nqp::decont($oval));
}
}
# If it's a hash, similar approach to array.
elsif $flags +& $SIG_ELEM_HASH_SIGIL {
if $flags +& $SIG_ELEM_IS_COPY {
my $bindee := nqp::create(Hash);
$bindee.STORE(nqp::decont($oval));
nqp::bindkey($lexpad, $varname, $bindee);
}
else {
nqp::bindkey($lexpad, $varname, nqp::decont($oval));
}
}
# If it's a scalar, we always need to wrap it into a new
# container and store it, for copy or ro case (the rw bit
# in the container descriptor takes care of the rest).
else {
my $new_cont := nqp::create(Scalar);
nqp::bindattr($new_cont, Scalar, '$!descriptor',
nqp::getattr($param, Parameter, '$!container_descriptor'));
nqp::bindattr($new_cont, Scalar, '$!value', nqp::decont($oval));
nqp::bindkey($lexpad, $varname, $new_cont);
}
}
}
# Is it the invocant? If so, also have to bind to self lexical.
if $flags +& $SIG_ELEM_INVOCANT {
nqp::bindkey($lexpad, 'self', nqp::decont($oval));
}
# Handle any constraint types (note that they may refer to the parameter by
# name, so we need to have bound it already).
my $post_cons := nqp::getattr($param, Parameter, '$!post_constraints');
unless nqp::isnull($post_cons) {
my int $n := nqp::elems($post_cons);
my int $i := 0;
while $i < $n {
# Check we meet the constraint.
my $cons_type := nqp::atpos($post_cons, $i);
if nqp::istype($cons_type, Code) {
$cons_type := nqp::p6capturelexwhere($cons_type.clone());
}
my $result;
if $got_native == 0 {
$result := $cons_type.ACCEPTS($oval);
}
elsif $got_native == $SIG_ELEM_NATIVE_INT_VALUE {
$result := $cons_type.ACCEPTS($ival);
}
elsif $got_native == $SIG_ELEM_NATIVE_NUM_VALUE {
$result := $cons_type.ACCEPTS($nval);
}
elsif $got_native == $SIG_ELEM_NATIVE_STR_VALUE {
$result := $cons_type.ACCEPTS($sval);
}
unless $result {
if nqp::defined($error) {
$error[0] := "Constraint type check failed for parameter '$varname'";
}
return $BIND_RESULT_FAIL;
}
$i++;
}
}
# If it's attributive, now we assign it.
if $is_attributive {
# Find self.
my $self;
if nqp::existskey($lexpad, 'self') {
$self := nqp::atkey($lexpad, 'self');
} else {
if nqp::defined($error) {
$error[0] := "Unable to bind attributive parameter '$varname'; could not find self";
}
return $BIND_RESULT_FAIL;
}
# Ensure it's not native; NYI.
if $got_native {
if nqp::defined($error) {
$error[0] := "Binding to natively typed attributive parameter '$varname' not supported";
}
return $BIND_RESULT_FAIL;
}
# If it's private, just need to fetch the attribute.
my $assignee;
if ($flags +& $SIG_ELEM_BIND_PRIVATE_ATTR) {
$assignee := nqp::getattr($self,
nqp::getattr($param, Parameter, '$!attr_package'),
$varname);
}
# Otherwise if it's public, do a method call to get the assignee.
else {
$assignee := $self."$varname"();
}
nqp::iscont($assignee)
?? nqp::assign($assignee, $oval)
!! $assignee.STORE(nqp::decont($oval));
}
# If it has a sub-signature, bind that.
my $subsig := nqp::getattr($param, Parameter, '$!sub_signature');
unless nqp::isnull($subsig) {
# Turn value into a capture, unless we already have one.
my $capture;
if $flags +& $SIG_ELEM_IS_CAPTURE {
$capture := $oval;
}
else {
if nqp::can($oval, 'Capture') {
$capture := $oval.Capture;
}
else {
if nqp::defined($error) {
$error[0] := "Could not turn argument into capture";
}
return $BIND_RESULT_FAIL;
}
}
# Recurse into signature binder.
my $result := bind(make_vm_capture($capture), $subsig, $lexpad,
$no_nom_type_check, $error);
unless $result == $BIND_RESULT_OK {
if $error {
# Note in the error message that we're in a sub-signature.
$error[0] := $error[0] ~ " in sub-signature";
if $has_varname {
$error[0] := $error[0] ~ " of parameter " ~ $varname;
}
}
return $result;
}
}
# Binding of this parameter was thus successful - we're done.
$BIND_RESULT_OK
}
# This takes a signature element and either runs the closure to get a default
# value if there is one, or creates an appropriate undefined-ish thingy.
sub handle_optional($param, int $flags, $lexpad) {
# Is the "get default from outer" flag set?
if $flags +& $SIG_ELEM_DEFAULT_FROM_OUTER {
nqp::atkey(
nqp::ctxouter($lexpad),
nqp::getattr_s($param, Parameter, '$!variable_name'))
}
# Do we have a default value or value closure?
elsif !nqp::isnull(my $default_value := nqp::getattr($param, Parameter, '$!default_value')) {
if $flags +& $SIG_ELEM_DEFAULT_IS_LITERAL {
$default_value;
}
else {
$default_value()
}
}
# Otherwise, go by sigil to pick the correct default type of value.
else {
if $flags +& $SIG_ELEM_ARRAY_SIGIL {
nqp::create(Array)
}
elsif $flags +& $SIG_ELEM_HASH_SIGIL {
nqp::create(Hash)
}
else {
nqp::getattr($param, Parameter, '$!nominal_type');
}
}
}
# Drives the overall binding process.
sub bind($capture, $sig, $lexpad, int $no_nom_type_check, $error) {
# Get params.
my @params := nqp::getattr($sig, Signature, '$!params');
# If we do have some named args, we get hold of a hash of them. We
# can safely delete from this as we go.
my $named_args;
if nqp::capturehasnameds($capture) {
$named_args := nqp::capturenamedshash($capture);
}
# Now we'll walk through the signature and go about binding things.
my $named_names;
my int $cur_pos_arg := 0;
my int $num_pos_args := nqp::captureposelems($capture);
my int $suppress_arity_fail := 0;
my int $num_params := nqp::elems(@params);
my int $i := 0;
while $i < $num_params {
# Get parameter object and its flags.
my $param := nqp::atpos(@params, $i);
my int $flags := nqp::getattr_i($param, Parameter, '$!flags');
my str $var_name := nqp::getattr_s($param, Parameter, '$!variable_name');
$i := $i + 1;
# Is it looking for us to bind a capture here?
my int $bind_fail;
my int $got_prim;
if $flags +& $SIG_ELEM_IS_CAPTURE {
# Capture the arguments from this point forwards into a Capture.
# Of course, if there's no variable name we can (cheaply) do pretty
# much nothing.
if nqp::isnull_s($var_name) {
$bind_fail := $BIND_RESULT_OK;
}
else {
my $capsnap := nqp::create(Capture);
my @pos_args;
my int $k := $cur_pos_arg;
while $k < $num_pos_args {
$got_prim := nqp::captureposprimspec($capture, $k);
if $got_prim == 0 {
nqp::push(@pos_args, nqp::captureposarg($capture, $k));
}
elsif $got_prim == 1 {
nqp::push(@pos_args, nqp::box_i(nqp::captureposarg_i($capture, $k), Int));
}
elsif $got_prim == 2 {
nqp::push(@pos_args, nqp::box_n(nqp::captureposarg_n($capture, $k), Num));
}
else {
nqp::push(@pos_args, nqp::box_s(nqp::captureposarg_s($capture, $k), Str));
}
$k++;
}
nqp::bindattr($capsnap, Capture, '$!list', @pos_args);
if $named_args {
nqp::bindattr($capsnap, Capture, '$!hash', nqp::clone($named_args));
}
else {
nqp::bindattr($capsnap, Capture, '$!hash', nqp::hash());
}
$bind_fail := bind_one_param($lexpad, $sig, $param, $no_nom_type_check, $error,
0, $capsnap, 0, 0.0, '');
}
if ($bind_fail) {
return $bind_fail;
}
elsif $i == $num_params {
# Since a capture acts as "the ultimate slurpy" in a sense, if
# this is the last parameter in the signature we can return
# success right off the bat.
return $BIND_RESULT_OK;
}
else {
my $next_param := nqp::atpos(@params, $i);
my int $next_flags := nqp::getattr_i($next_param, Parameter, '$!flags');
if $next_flags +& ($SIG_ELEM_SLURPY_POS +| $SIG_ELEM_SLURPY_NAMED) {
$suppress_arity_fail := 1;
}
}
}
# Could it be a named slurpy?
elsif $flags +& $SIG_ELEM_SLURPY_NAMED {
# We'll either take the current named arguments copy hash which
# will by definition contain all unbound named arguments and use
# that. Otherwise, putting Mu in there is fine; Hash is smart
# enough to know what to do.
my $hash := nqp::create(Hash);
nqp::bindattr($hash, EnumMap, '$!storage', $named_args || Mu);
$bind_fail := bind_one_param($lexpad, $sig, $param, $no_nom_type_check, $error,
0, $hash, 0, 0.0, '');
return $bind_fail if $bind_fail;
# Undefine named arguments hash now we've consumed it, to mark all
# is well.
$named_args := NQPMu;
}
# Otherwise, maybe it's a positional.
elsif nqp::isnull($named_names := nqp::getattr($param, Parameter, '$!named_names')) {
# Slurpy or LoL-slurpy?
if $flags +& ($SIG_ELEM_SLURPY_POS +| $SIG_ELEM_SLURPY_LOL) {
# Create Perl 6 array, create VM Array of all remaining things, then
# store it.
my $temp := nqp::list();
while $cur_pos_arg < $num_pos_args {
$got_prim := nqp::captureposprimspec($capture, $cur_pos_arg);
if $got_prim == 0 {
nqp::push($temp, nqp::captureposarg($capture, $cur_pos_arg));
}
elsif $got_prim == 1 {
nqp::push($temp, nqp::box_i(nqp::captureposarg_i($capture, $cur_pos_arg), Int));
}
elsif $got_prim == 2 {
nqp::push($temp, nqp::box_n(nqp::captureposarg_n($capture, $cur_pos_arg), Num));
}
else {
nqp::push($temp, nqp::box_s(nqp::captureposarg_s($capture, $cur_pos_arg), Str));
}
$cur_pos_arg++;
}
my $slurpy_type := $flags +& $SIG_ELEM_IS_RW ?? List !! Array;
my $bindee := $flags +& $SIG_ELEM_SLURPY_POS
?? $slurpy_type.from-slurpy-flat($temp)
!! $slurpy_type.from-slurpy($temp);
$bind_fail := bind_one_param($lexpad, $sig, $param, $no_nom_type_check, $error,
0, $bindee, 0, 0.0, '');
return $bind_fail if $bind_fail;
}
# Otherwise, a positional.
else {
# Do we have a value?
if $cur_pos_arg < $num_pos_args {
# Easy - just bind that.
$got_prim := nqp::captureposprimspec($capture, $cur_pos_arg);
if $got_prim == 0 {
$bind_fail := bind_one_param($lexpad, $sig, $param, $no_nom_type_check, $error,
0, nqp::captureposarg($capture, $cur_pos_arg), 0, 0.0, '');
}
elsif $got_prim == 1 {
$bind_fail := bind_one_param($lexpad, $sig, $param, $no_nom_type_check, $error,
$SIG_ELEM_NATIVE_INT_VALUE, nqp::null(), nqp::captureposarg_i($capture, $cur_pos_arg), 0.0, '');
}
elsif $got_prim == 2 {
$bind_fail := bind_one_param($lexpad, $sig, $param, $no_nom_type_check, $error,
$SIG_ELEM_NATIVE_NUM_VALUE, nqp::null(), 0, nqp::captureposarg_n($capture, $cur_pos_arg), '');
}
else {
$bind_fail := bind_one_param($lexpad, $sig, $param, $no_nom_type_check, $error,
$SIG_ELEM_NATIVE_STR_VALUE, nqp::null(), 0, 0.0, nqp::captureposarg_s($capture, $cur_pos_arg));
}
return $bind_fail if $bind_fail;
$cur_pos_arg++;
}
else {
# No value. If it's optional, fetch a default and bind that;
# if not, we're screwed. Note that we never nominal type check
# an optional with no value passed.
if $flags +& $SIG_ELEM_IS_OPTIONAL {
$bind_fail := bind_one_param($lexpad, $sig, $param, $no_nom_type_check, $error,
0, handle_optional($param, $flags, $lexpad), 0, 0.0, '');
return $bind_fail if $bind_fail;
}
else {
if nqp::defined($error) {
$error[0] := arity_fail(@params, $num_params, $num_pos_args, 0);
}
return $BIND_RESULT_FAIL;
}
}
}
}
# Else, it's a non-slurpy named.
else {
# Try and get hold of value.
my $value := nqp::null();
if $named_args {
my int $num_names := nqp::elems($named_names);
my int $j := 0;
my str $cur_name;
while $j < $num_names {
$cur_name := nqp::atpos($named_names, $j);
$value := nqp::atkey($named_args, $cur_name);
unless nqp::isnull($value) {
nqp::deletekey($named_args, $cur_name);
$j := $num_names;
}
++$j;
}
}
# Did we get one?
if nqp::isnull($value) {
# Nope. We'd better hope this param was optional...
if $flags +& $SIG_ELEM_IS_OPTIONAL {
$bind_fail := bind_one_param($lexpad, $sig, $param, $no_nom_type_check, $error,
0, handle_optional($param, $flags, $lexpad), 0, 0.0, '');
}
elsif !$suppress_arity_fail {
if nqp::defined($error) {
$error[0] := "Required named argument '" ~
$named_names[0] ~ "' not passed";
}
return $BIND_RESULT_FAIL;
}
}
else {
$bind_fail := bind_one_param($lexpad, $sig, $param, $no_nom_type_check, $error,
0, $value, 0, 0.0, '');
}
# If we got a binding failure, return it.
return $bind_fail if $bind_fail;
}
}
# Do we have any left-over args?
if $cur_pos_arg < $num_pos_args && !$suppress_arity_fail {
# Oh noes, too many positionals passed.
if nqp::defined($error) {
$error[0] := arity_fail(@params, $num_params, $num_pos_args, 1);
}
return $BIND_RESULT_FAIL;
}
if $named_args {
# Oh noes, unexpected named args.
if nqp::defined($error) {
my int $num_extra := nqp::elems($named_args);
my @names;
for $named_args {
nqp::push(@names, $_.key);
}
if $num_extra == 1 {
$error[0] := "Unexpected named argument '" ~ @names[0] ~ "' passed";
}
else {
$error[0] := $num_extra ~ " unexpected named arguments passed (" ~
nqp::join(",", @names) ~")";
}
}
return $BIND_RESULT_FAIL;
}
# If we get here, we're done.
return $BIND_RESULT_OK;
}
method bind($capture, $sig, $lexpad, int $no_nom_type_check, $error) {
bind($capture, $sig, $lexpad, $no_nom_type_check, $error);
}
method bind_sig($capture) {
# Get signature and lexpad.
my $caller := nqp::getcodeobj(nqp::callercode());
my $sig := nqp::getattr($caller, Code, '$!signature');
my $lexpad := nqp::ctxcaller(nqp::ctx());
# Call binder.
my @error;
my int $bind_res := bind($capture, $sig, $lexpad, 0, @error);
if $bind_res {
if $bind_res == $BIND_RESULT_JUNCTION {
my @pos_args;
my int $num_pos_args := nqp::captureposelems($capture);
my int $k := 0;
my int $got_prim;
while $k < $num_pos_args {
$got_prim := nqp::captureposprimspec($capture, $k);
if $got_prim == 0 {
nqp::push(@pos_args, nqp::captureposarg($capture, $k));
}
elsif $got_prim == 1 {
nqp::push(@pos_args, nqp::box_i(nqp::captureposarg_i($capture, $k), Int));
}
elsif $got_prim == 2 {
nqp::push(@pos_args, nqp::box_n(nqp::captureposarg_n($capture, $k), Num));
}
else {
nqp::push(@pos_args, nqp::box_s(nqp::captureposarg_s($capture, $k), Str));
}
$k++;
}
my %named_args := nqp::capturenamedshash($capture);
return Junction.AUTOTHREAD($caller,
|@pos_args,
|%named_args);
}
else {
nqp::isinvokable(@error[0]) ?? @error[0]() !! nqp::die(@error[0]);
}
}
nqp::null();
}
sub make_vm_capture($capture) {
sub vm_capture(*@pos, *%named) { nqp::savecapture() }
my @list := nqp::getattr($capture, Capture, '$!list');
@list := nqp::list() unless nqp::islist(@list);
my %hash := nqp::getattr($capture, Capture, '$!hash');
%hash := nqp::hash() unless nqp::ishash(%hash);
vm_capture(|@list, |%hash)
}
method is_bindable($sig, $capture) {
unless nqp::reprname($capture) eq 'MVMCallCapture' {
$capture := make_vm_capture($capture);
}
nqp::p6invokeunder(
nqp::getattr($sig, Signature, '$!code'),
-> { bind($capture, $sig, nqp::ctxcaller(nqp::ctx()), 0, NQPMu) != $BIND_RESULT_FAIL })
}
method bind_cap_to_sig($sig, $cap) {
my $capture := make_vm_capture($cap);
my $lexpad := nqp::ctxcaller(nqp::ctx());
my @error;
if bind($capture, $sig, $lexpad, 0, @error) != $BIND_RESULT_OK {
nqp::isinvokable(@error[0]) ?? @error[0]() !! nqp::die(@error[0]);
}
$sig
}
my int $TRIAL_BIND_NOT_SURE := 0; # Plausible, but need to check at runtime.
my int $TRIAL_BIND_OK := 1; # Bind will always work out.
my int $TRIAL_BIND_NO_WAY := -1; # Bind could never work out.
method trial_bind($sig, $args, $sigflags) {
my @params := nqp::getattr($sig, Signature, '$!params');
my int $num_params := nqp::elems(@params);
# If there's a single capture parameter, then we're OK. (Worth
# handling especially as it's the common case for protos).
if $num_params == 1 {
if nqp::getattr_i(@params[0], Parameter, '$!flags') +& $SIG_ELEM_IS_CAPTURE {
return $TRIAL_BIND_OK;
}
}
# Walk through the signature and consider the parameters.
my int $num_pos_args := nqp::elems($args);
my int $cur_pos_arg := 0;
my int $i := 0;
while $i < $num_params {
my $param := @params[$i];
$i++;
# If the parameter is anything other than a boring old
# positional parameter, we won't analyze it. */
my int $flags := nqp::getattr_i($param, Parameter, '$!flags');
if $flags +& nqp::bitneg_i(
$SIG_ELEM_MULTI_INVOCANT +| $SIG_ELEM_IS_PARCEL +|
$SIG_ELEM_IS_COPY +| $SIG_ELEM_ARRAY_SIGIL +|
$SIG_ELEM_HASH_SIGIL +| $SIG_ELEM_NATIVE_VALUE +|
$SIG_ELEM_IS_OPTIONAL) {
return $TRIAL_BIND_NOT_SURE;
}
unless nqp::isnull(nqp::getattr($param, Parameter, '$!named_names')) {
return $TRIAL_BIND_NOT_SURE;
}
unless nqp::isnull(nqp::getattr($param, Parameter, '$!post_constraints')) {
return $TRIAL_BIND_NOT_SURE;
}
unless nqp::isnull(nqp::getattr($param, Parameter, '$!type_captures')) {
return $TRIAL_BIND_NOT_SURE;
}
unless nqp::isnull(nqp::getattr($param, Parameter, '$!coerce_type')) {
return $TRIAL_BIND_NOT_SURE;
}
# Do we have an argument for this parameter?
if $cur_pos_arg >= $num_pos_args {
# No; if it's not optional, fail.
unless $flags +& $SIG_ELEM_IS_OPTIONAL {
return $TRIAL_BIND_NO_WAY;
}
}
else {
# Yes, need to consider type
my int $got_prim := $sigflags[$cur_pos_arg];
if $flags +& $SIG_ELEM_NATIVE_VALUE {
if $got_prim == 0 {
# We got an object; if we aren't sure we can unbox, we can't
# be sure about the dispatch.
if $flags +& $SIG_ELEM_NATIVE_INT_VALUE {
return $TRIAL_BIND_NOT_SURE unless nqp::isint($args[$cur_pos_arg]);
}
elsif $flags +& $SIG_ELEM_NATIVE_NUM_VALUE {
return $TRIAL_BIND_NOT_SURE unless nqp::isnum($args[$cur_pos_arg]);
}
elsif $flags +& $SIG_ELEM_NATIVE_STR_VALUE {
return $TRIAL_BIND_NOT_SURE unless nqp::isstr($args[$cur_pos_arg]);
}
else {
# WTF...
return $TRIAL_BIND_NOT_SURE;
}
}
else {
# If it's the wrong type of native, there's no way it