/
perl6.ops
2156 lines (1648 loc) Β· 61.6 KB
/
perl6.ops
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
/*
* Copyright (C) 2008-2012, The Perl Foundation.
*/
BEGIN_OPS_PREAMBLE
#include "parrot/parrot.h"
#include "parrot/events.h"
#include "parrot/extend.h"
#include "parrot/dynext.h"
#include "pmc_object.h"
#include "pmc_class.h"
#include "pmc_callcontext.h"
#include "pmc_sub.h"
#include "pmc_continuation.h"
#include "pmc_exception.h"
#include "../binder/bind.h"
#include "../binder/multidispatch.h"
#include "../binder/container.h"
#include "../binder/types.h"
#include "../binder/sixmodelobject.h"
#include "../binder/exceptions.h"
#if PARROT_HAS_ICU
# include <unicode/uchar.h>
#endif
/* Cache some stuff for fast access. */
static INTVAL smo_id = 0;
static INTVAL qrpa_id = 0;
/* The current dispatcher, for the next thing that wants one to take. */
static PMC *current_dispatcher = NULL;
static PMC *build_sig_object(PARROT_INTERP, ARGIN_NULLOK(PMC *signature), ARGIN(const char *sig), ...)
{
PMC *sig_obj;
va_list args;
va_start(args, sig);
/* sigh, Parrot_pcc_build_sig_object_from_varargs does not have a signature arg */
sig_obj = Parrot_pcc_build_sig_object_from_varargs(interp, PMCNULL, sig, args);
va_end(args);
return sig_obj;
}
static INTVAL should_run_phaser(PARROT_INTERP, PMC *phaser, PMC *all_phasers, PMC *result) {
PMC *keep_list, *undo_list;
INTVAL i, elems;
/* Check if the phaser is in the keep or undo list. */
INTVAL in_keep_list = 0;
INTVAL in_undo_list = 0;
phaser = Rakudo_cont_decontainerize(interp, phaser);
keep_list = VTABLE_get_pmc_keyed_str(interp, all_phasers, Parrot_str_new_constant(interp, "KEEP"));
if (!PMC_IS_NULL(keep_list)) {
elems = VTABLE_elements(interp, keep_list);
for (i = 0; i < elems; i++)
if (Rakudo_cont_decontainerize(interp, VTABLE_get_pmc_keyed_int(interp, keep_list, i)) == phaser)
{
in_keep_list = 1;
break;
}
}
if (!in_keep_list) {
undo_list = VTABLE_get_pmc_keyed_str(interp, all_phasers, Parrot_str_new_constant(interp, "UNDO"));
if (!PMC_IS_NULL(undo_list)) {
elems = VTABLE_elements(interp, undo_list);
for (i = 0; i < elems; i++)
if (Rakudo_cont_decontainerize(interp, VTABLE_get_pmc_keyed_int(interp, undo_list, i)) == phaser)
{
in_undo_list = 1;
break;
}
}
}
/* If it's in neither list, it's just a plain old LEAVE. */
if (!in_keep_list && !in_undo_list)
return 1;
/* If it's NULL, we're unwinding, which means we only UNDO. */
if (PMC_IS_NULL(result))
return in_undo_list;
/* Otherwise, need to consider the definedness of the return value. */
if (IS_CONCRETE(result)) {
PMC *meth = VTABLE_find_method(interp, result, Parrot_str_new_constant(interp, "defined"));
PMC *definedness;
Parrot_ext_call(interp, meth, "Pi->P", result, &definedness);
if (VTABLE_get_bool(interp, definedness))
return in_keep_list;
else
return in_undo_list;
}
else {
return in_undo_list;
}
}
static PMC *run_leave_phasers(PARROT_INTERP, PMC *ctx, PMC *perl6_code, PMC *result, PMC *exceptions)
{
Rakudo_Code *code;
PMC *phasers, *leave_phasers, *post_phasers;
PMC *oldctx;
int i, n;
Parrot_runloop jump_point;
/* Don't run any phasers if we failed while checking pre-conditions. */
if (PObj_flag_TEST(P6_CHECKING_PRE, ctx))
return PMCNULL;
if (PMC_IS_NULL(perl6_code))
return PMCNULL;
code = (Rakudo_Code *)PMC_data(perl6_code);
phasers = code->phasers;
if (PMC_IS_NULL(phasers))
return PMCNULL;
leave_phasers = VTABLE_get_pmc_keyed_str(interp, phasers, Parrot_str_new_constant(interp, "!LEAVE-ORDER"));
post_phasers = VTABLE_get_pmc_keyed_str(interp, phasers, Parrot_str_new_constant(interp, "POST"));
if (PMC_IS_NULL(leave_phasers) && PMC_IS_NULL(post_phasers))
return PMCNULL;
oldctx = CURRENT_CONTEXT(interp);
Parrot_pcc_set_context(interp, ctx);
if (!PMC_IS_NULL(leave_phasers)) {
n = VTABLE_elements(interp, leave_phasers);
for (i = 0; i < n; i++) {
int runloop_id = interp->current_runloop_id;
if (setjmp(jump_point.resume)) {
if (PMC_IS_NULL(exceptions)) {
exceptions = Parrot_pmc_new(interp, enum_class_ResizablePMCArray);
}
VTABLE_push_pmc(interp, exceptions, jump_point.exception);
/* grrr */
while (interp->current_runloop && interp->current_runloop_id != runloop_id)
free_runloop_jump_point(interp);
} else {
PMC *phaser = VTABLE_get_pmc_keyed_int(interp, leave_phasers, i);
Parrot_ex_add_c_handler(interp, &jump_point);
if (should_run_phaser(interp, phaser, phasers, result))
Parrot_pcc_invoke_sub_from_c_args(interp, phaser, "->");
}
Parrot_cx_delete_handler_local(interp);
}
}
if (!PMC_IS_NULL(post_phasers)) {
INTVAL failed_a_post = 0;
n = VTABLE_elements(interp, post_phasers);
for (i = 0; i < n; i++) {
int runloop_id = interp->current_runloop_id;
if (setjmp(jump_point.resume)) {
if (PMC_IS_NULL(exceptions)) {
exceptions = Parrot_pmc_new(interp, enum_class_ResizablePMCArray);
}
VTABLE_push_pmc(interp, exceptions, jump_point.exception);
/* grrrrrrr */
while (interp->current_runloop && interp->current_runloop_id != runloop_id)
free_runloop_jump_point(interp);
failed_a_post = 1;
} else {
PMC *phaser = VTABLE_get_pmc_keyed_int(interp, post_phasers, i);
Parrot_ex_add_c_handler(interp, &jump_point);
Parrot_pcc_invoke_sub_from_c_args(interp, phaser, "P->", result);
}
Parrot_cx_delete_handler_local(interp);
if (failed_a_post)
break;
}
}
Parrot_pcc_set_context(interp, oldctx);
return exceptions;
}
static void rethrow_phaser_exceptions(PARROT_INTERP, PMC *exceptions)
{
int i, n;
if (PMC_IS_NULL(exceptions))
return;
n = VTABLE_elements(interp, exceptions);
if (!n)
return;
for (i = 0; i < n; i++) {
Parrot_ex_rethrow_from_c(interp, VTABLE_get_pmc_keyed_int(interp, exceptions, i));
}
}
static void rewind_to_ctx(PARROT_INTERP, ARGIN_NULLOK(PMC *ctx), ARGIN_NULLOK(PMC *basectx), ARGIN_NULLOK(PMC *result))
{
PMC *parrot_sub;
PMC *perl6_code;
PMC *exceptions = PMCNULL;
while (!PMC_IS_NULL(ctx) && ctx != basectx) {
parrot_sub = Parrot_pcc_get_sub(interp, ctx);
if (!PMC_IS_NULL(parrot_sub)) {
GETATTR_Sub_multi_signature(interp, parrot_sub, perl6_code);
if (!PMC_IS_NULL(perl6_code) && perl6_code->vtable->base_type == smo_id &&
STABLE(perl6_code)->WHAT != Rakudo_types_code_get()) {
exceptions = run_leave_phasers(interp, ctx, perl6_code, result, exceptions);
}
}
ctx = Parrot_pcc_get_caller_ctx(interp, ctx);
}
if (!PMC_IS_NULL(exceptions)) {
Parrot_pcc_set_context(interp, basectx);
rethrow_phaser_exceptions(interp, exceptions);
}
}
static PMC *find_common_ctx(PARROT_INTERP, ARGIN_NULLOK(PMC *ctx1), ARGIN_NULLOK(PMC *ctx2))
{
int depth1 = 0;
int depth2 = 0;
PMC *ctx;
for (ctx = ctx1; !PMC_IS_NULL(ctx); ctx = Parrot_pcc_get_caller_ctx(interp, ctx), depth1++)
if (ctx == ctx2)
return ctx;
for (ctx = ctx2; !PMC_IS_NULL(ctx); ctx = Parrot_pcc_get_caller_ctx(interp, ctx), depth2++)
if (ctx == ctx1)
return ctx;
for (; depth1 > depth2; depth2++)
ctx1 = Parrot_pcc_get_caller_ctx(interp, ctx1);
for (; depth2 > depth1; depth1++)
ctx2 = Parrot_pcc_get_caller_ctx(interp, ctx2);
while (ctx1 != ctx2) {
ctx1 = Parrot_pcc_get_caller_ctx(interp, ctx1);
ctx2 = Parrot_pcc_get_caller_ctx(interp, ctx2);
}
return ctx1;
}
PARROT_CAN_RETURN_NULL
PARROT_WARN_UNUSED_RESULT
static PMC* sub_find_pad(PARROT_INTERP, ARGIN(STRING *lex_name), ARGIN(PMC *ctx))
{
ASSERT_ARGS(Parrot_sub_find_pad)
while (1) {
PMC * const lex_pad = Parrot_pcc_get_lex_pad(interp, ctx);
PMC * outer = Parrot_pcc_get_outer_ctx(interp, ctx);
if (PMC_IS_NULL(outer))
return lex_pad;
PARROT_ASSERT(outer->vtable->base_type == enum_class_CallContext);
if (!PMC_IS_NULL(lex_pad))
if (VTABLE_exists_keyed_str(interp, lex_pad, lex_name))
return lex_pad;
ctx = outer;
}
}
END_OPS_PREAMBLE
/*
=item rakudo_dynop_setup()
Does various setup tasks on behalf of all of the other dynops.
=cut
*/
inline op rakudo_dynop_setup() :base_core {
/* Get 6model object type ID. */
smo_id = Parrot_pmc_get_type_str(interp, Parrot_str_new_constant(interp, "SixModelObject"));
qrpa_id = Parrot_pmc_get_type_str(interp, Parrot_str_new_constant(interp, "QRPA"));
}
/*
=item find_lex_skip_current(out PMC, in STR)
Finds the lexical named $2 and returns it. However, unlike find_lex this op
skips the current sub and starts looking immediately at its outers.
=cut
*/
inline op find_lex_skip_current(out PMC, in STR) :base_core {
PMC *ctx = CURRENT_CONTEXT(interp);
$1 = PMCNULL;
while (Parrot_pcc_get_outer_ctx(interp, ctx)) {
PMC * const outer = Parrot_pcc_get_outer_ctx(interp, ctx);
PMC * const lex_pad = Parrot_pcc_get_lex_pad(interp, outer);
if (!PMC_IS_NULL(lex_pad) && VTABLE_exists_keyed_str(interp, lex_pad, $2)) {
$1 = VTABLE_get_pmc_keyed_str(interp, lex_pad, $2);
break;
}
ctx = outer;
}
}
/*
=item inline op x_is_uprop(out INT, in STR, in STR, in INT)
Sets a true value in $1 if character $4 in string $3 has the unicode property
named $2.
=cut
*/
inline op x_is_uprop(out INT, in STR, in STR, in INT) :base_core {
#if PARROT_HAS_ICU
char *cstr;
INTVAL ord;
int32_t strwhich, ordwhich;
UProperty strprop;
opcode_t *handler;
if ($4 > 0 && (UINTVAL)$4 == ($3->strlen)) {
$1 = 0;
goto NEXT();
}
ord = Parrot_str_indexed(interp, $3, $4);
cstr = Parrot_str_to_cstring(interp, $2);
/* try block tests */
if (strncmp(cstr, "In", 2) == 0) {
strwhich = u_getPropertyValueEnum(UCHAR_BLOCK, cstr+2);
ordwhich = u_getIntPropertyValue(ord, UCHAR_BLOCK);
if (strwhich != UCHAR_INVALID_CODE) {
$1 = (strwhich == ordwhich);
Parrot_str_free_cstring(cstr);
goto NEXT();
}
}
/* try bidi tests */
if (strncmp(cstr, "Bidi", 4) == 0) {
strwhich = u_getPropertyValueEnum(UCHAR_BIDI_CLASS, cstr+4);
ordwhich = u_getIntPropertyValue(ord, UCHAR_BIDI_CLASS);
if (strwhich != UCHAR_INVALID_CODE) {
$1 = (strwhich == ordwhich);
Parrot_str_free_cstring(cstr);
goto NEXT();
}
}
/* try property value aliases */
strwhich = u_getPropertyValueEnum(UCHAR_GENERAL_CATEGORY_MASK, cstr);
if (strwhich != UCHAR_INVALID_CODE) {
ordwhich = u_getIntPropertyValue(ord, UCHAR_GENERAL_CATEGORY_MASK);
$1 = ((strwhich & ordwhich) != 0);
Parrot_str_free_cstring(cstr);
goto NEXT();
}
/* try property */
strprop = u_getPropertyEnum(cstr);
if (strprop != UCHAR_INVALID_CODE) {
$1 = (u_hasBinaryProperty(ord, strprop) != 0);
Parrot_str_free_cstring(cstr);
goto NEXT();
}
/* try script aliases */
strwhich = u_getPropertyValueEnum(UCHAR_SCRIPT, cstr);
if (strwhich != UCHAR_INVALID_CODE) {
ordwhich = u_getIntPropertyValue(ord, UCHAR_SCRIPT);
$1 = (strwhich == ordwhich);
Parrot_str_free_cstring(cstr);
goto NEXT();
}
/* unrecognized property name */
Parrot_str_free_cstring(cstr);
handler = Parrot_ex_throw_from_op_args(interp, NULL,
EXCEPTION_ICU_ERROR,
"Unicode property '%Ss' not found", $2);
goto ADDRESS(handler);
#else
opcode_t * const handler = Parrot_ex_throw_from_op_args(interp, NULL,
EXCEPTION_ICU_ERROR,
"ICU not loaded", $2);
goto ADDRESS(handler);
#endif
}
/*
=item bind_signature()
This is emitted into a sub to cause it's Perl 6 signature to be bound.
=cut
*/
inline op bind_signature() :base_core {
/* Need to make sure some stuff doesn't get destroyed. */
PMC * const ctx = CURRENT_CONTEXT(interp);
PMC * const saved_ccont = interp->current_cont;
PMC * const saved_sig = Parrot_pcc_get_signature(interp, ctx);
opcode_t * const current_pc = Parrot_pcc_get_pc(interp, ctx);
/* Obtain lexpad and other settings. */
PMC * const lexpad = Parrot_pcc_get_lex_pad(interp, ctx);
const INTVAL noms_checked = PObj_flag_TEST(P6BINDER_ALREADY_CHECKED, ctx);
STRING * error = STRINGNULL;
INTVAL bind_error;
/* Look up signature to bind. */
PMC * const parrot_sub = Parrot_pcc_get_sub(interp, ctx);
PMC *perl6_code, *signature;
GETATTR_Sub_multi_signature(interp, parrot_sub, perl6_code);
if (PMC_IS_NULL(perl6_code))
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Could not locate Perl 6 code object");
signature = ((Rakudo_Code *)PMC_data(perl6_code))->signature;
/* Call signature binder. */
bind_error = Rakudo_binding_bind(interp, lexpad, signature, ctx,
noms_checked, &error);
/* Bind ok? */
if (!bind_error) {
/* Re-instate anything we may have damaged. */
CURRENT_CONTEXT(interp) = ctx;
interp->current_cont = saved_ccont;
Parrot_pcc_set_signature(interp, ctx, saved_sig);
Parrot_pcc_set_pc(interp, ctx, current_pc);
goto NEXT();
}
else {
/* Maybe we need to auto-thread... */
if (bind_error == BIND_RESULT_JUNCTION) {
/* Find dispatcher and call it. */
PMC * const dispatcher = Rakudo_types_junction_threader_get();
PMC * const sub = Parrot_pcc_get_sub(interp, ctx);
PMC * call_ctx = VTABLE_clone(interp, ctx);
PMC * ret_cont = Parrot_pcc_get_continuation(interp, ctx);
PMC * p6sub;
opcode_t *next;
GETATTR_Sub_multi_signature(interp, sub, p6sub);
VTABLE_unshift_pmc(interp, call_ctx, p6sub);
Parrot_pcc_invoke_from_sig_object(interp, dispatcher, call_ctx);
/* Invoke the original return continuation, to return junction result. */
next = VTABLE_invoke(interp, ret_cont, expr NEXT());
goto ADDRESS(next);
}
else {
/* Nope, just normal fail... */
opcode_t * const handler = Parrot_ex_throw_from_op_args(interp, NULL,
EXCEPTION_INVALID_OPERATION, "%Ss", error);
goto ADDRESS(handler);
}
}
}
/*
=item perl6_bind_sig_to_cap()
Takes a signature and capture. Binds it in the current lexical context.
=cut
*/
inline op perl6_bind_sig_to_cap(in PMC, in PMC) :base_core {
PMC *signature = $1;
PMC *capture = $2;
/* Need to make sure some stuff doesn't get destroyed. */
PMC * const ctx = CURRENT_CONTEXT(interp);
PMC * const saved_ccont = interp->current_cont;
PMC * const saved_sig = Parrot_pcc_get_signature(interp, ctx);
opcode_t * const current_pc = Parrot_pcc_get_pc(interp, ctx);
/* Obtain lexpad and other settings. */
PMC * const lexpad = Parrot_pcc_get_lex_pad(interp, ctx);
STRING * error = STRINGNULL;
INTVAL bind_error;
/* Call signature binder. */
bind_error = Rakudo_binding_bind(interp, lexpad, signature,
capture, 0, &error);
/* Bind ok? */
if (!bind_error) {
/* Re-instate anything we may have damaged. */
CURRENT_CONTEXT(interp) = ctx;
interp->current_cont = saved_ccont;
Parrot_pcc_set_signature(interp, ctx, saved_sig);
Parrot_pcc_set_pc(interp, ctx, current_pc);
goto NEXT();
}
else {
opcode_t * const handler = Parrot_ex_throw_from_op_args(interp, NULL,
EXCEPTION_INVALID_OPERATION, "%Ss", error);
goto ADDRESS(handler);
}
}
/*
=item perl6_trial_bind_ct()
Sees if we could potentially bind a signature.
$0 is a flag indicating the outcome. 0 means could not decide, 1 means
decided that we will be able to bind, -1 means that it'd never work
$1 is the signature object
$2 is the argument array
$3 is a set of flags for native types. 0 = object, 1 = native int,
2 = native num, 3 = native str.
=cut
*/
inline op perl6_trial_bind_ct(out INT, in PMC, in PMC, in PMC) :base_core {
/* Build up a capture with sample arguments. */
PMC *capture = Parrot_pmc_new(interp, enum_class_CallContext);
INTVAL num_args = VTABLE_elements(interp, $3);
INTVAL args_ok = 1;
INTVAL i;
for (i = 0; i < num_args; i++) {
INTVAL native = VTABLE_get_integer_keyed_int(interp, $4, i);
PMC *obj;
switch (native) {
case BIND_VAL_INT:
VTABLE_push_integer(interp, capture, 0);
break;
case BIND_VAL_NUM:
VTABLE_push_float(interp, capture, 0.0);
break;
case BIND_VAL_STR:
VTABLE_push_string(interp, capture, STRINGNULL);
break;
default:
obj = VTABLE_get_pmc_keyed_int(interp, $3, i);
if (obj->vtable->base_type == smo_id) {
VTABLE_push_pmc(interp, capture, obj);
}
else {
args_ok = 0;
break;
}
}
}
/* Do trial bind. */
$1 = Rakudo_binding_trial_bind(interp, $2, capture);
}
/*
=item perl6_is_sig_bindable()
Checks if a capture can be bound to a signature.
=cut
*/
inline op perl6_is_sig_bindable(out INT, in PMC, in PMC) :base_core {
PMC *signature = $2;
PMC *capture = $3;
STRING *error = STRINGNULL;
PMC *lexpad, *ctx, *_do, *ret_cont, *call_object;
opcode_t *next;
INTVAL result;
/* Need to make sure some stuff doesn't get destroyed. */
PMC * const saved_ctx = CURRENT_CONTEXT(interp);
PMC * const saved_ccont = interp->current_cont;
PMC * const saved_sig = Parrot_pcc_get_signature(interp, saved_ctx);
opcode_t * const saved_pc = Parrot_pcc_get_pc(interp, saved_ctx);
/* Obtain code object from signature. */
PMC *code = ((Rakudo_Signature *)PMC_data(signature))->code;
if (PMC_IS_NULL(code)) {
$1 = 0;
goto NEXT();
}
_do = ((Rakudo_Code *)PMC_data(code))->_do;
/* Invoke the code that the signature belongs to. */
ret_cont = Parrot_pmc_new(interp, enum_class_Continuation);
call_object = Parrot_pmc_new(interp, enum_class_CallContext);
Parrot_pcc_set_signature(interp, CURRENT_CONTEXT(interp), call_object);
PARROT_CONTINUATION(ret_cont)->from_ctx = call_object;
Parrot_pcc_set_continuation(interp, call_object, ret_cont);
interp->current_cont = ret_cont;
next = VTABLE_invoke(interp, _do, saved_pc);
ctx = CURRENT_CONTEXT(interp);
/* Obtain lexpad. */
lexpad = Parrot_pcc_get_lex_pad(interp, ctx);
/* Call signature binder and stash outcome. */
result = Rakudo_binding_bind(interp, lexpad, signature, capture, 0, &error)
== BIND_RESULT_OK;
/* Invoke the return continuation. */
VTABLE_invoke(interp, ret_cont, next);
$1 = result;
}
/*
=item perl6_set_types_mu_any(in PMC, in PMC)
Sets the top type.
=cut
*/
inline op perl6_set_types_mu_any(in PMC, in PMC) :base_core {
Rakudo_types_mu_set($1);
Rakudo_types_any_set($2);
}
/*
=item perl6_set_type_routine(in PMC)
Sets the routine type.
=cut
*/
inline op perl6_set_type_routine(in PMC) :base_core {
Rakudo_types_routine_set($1);
}
/*
=item perl6_setup_junction_autothreading(in PMC)
Sets the junction type and auto-threader.
=cut
*/
inline op perl6_setup_junction_autothreading(in PMC, in PMC) :base_core {
Rakudo_types_junction_set($1);
Rakudo_types_junction_threader_set($2);
}
/*
=item perl6_set_types_ins(in PMC, in PMC, in PMC)
Sets the Int/Num/Str types.
=cut
*/
inline op perl6_set_types_ins(in PMC, in PMC, in PMC) :base_core {
Rakudo_types_int_set($1);
Rakudo_types_num_set($2);
Rakudo_types_str_set($3);
}
/*
=item perl6_set_types_list_array_lol(in PMC, in PMC, in PMC, in PMC, in PMC)
Sets the List, ListIter, Array and LoL types.
=cut
*/
inline op perl6_set_types_list_array_lol(in PMC, in PMC, in PMC, in PMC, in PMC) :base_core {
Rakudo_types_list_set($1);
Rakudo_types_listiter_set($2);
Rakudo_types_array_set($3);
Rakudo_types_lol_set($4);
Rakudo_types_parcel_set($5);
}
/*
=item perl6_set_types_enummap_hash(in PMC, in PMC)
Sets the EnumMap and Hash types.
=cut
*/
inline op perl6_set_types_enummap_hash(in PMC, in PMC) :base_core {
Rakudo_types_enummap_set($1);
Rakudo_types_hash_set($2);
}
/*
=item perl6_set_type_capture(in PMC)
Sets the Capture type.
=cut
*/
inline op perl6_set_type_capture(in PMC) :base_core {
Rakudo_types_capture_set($1);
}
/*
=item perl6_set_type_code(in PMC)
Sets the Code type.
=cut
*/
inline op perl6_set_type_code(in PMC) :base_core {
Rakudo_types_code_set($1);
}
/*
=item perl6_set_type_packagehow(in PMC)
Sets the package type.
=cut
*/
inline op perl6_set_type_packagehow(in PMC) :base_core {
Rakudo_types_packagehow_set($1);
}
/*
=item perl6_booleanize(out PMC, in INT)
If $2 is non-zero, puts Bool::True in $1. Otherwise puts Bool::False
in.
=cut
*/
inline op perl6_booleanize(out PMC, in INT) :base_core {
$1 = $2 == 0 ? Rakudo_types_bool_false_get() : Rakudo_types_bool_true_get();
}
/*
=item perl6_set_bools(in PMC, in PMC)
Sets and caches the False ($1) and True ($2) values to booleanize to.
=cut
*/
inline op perl6_set_bools(in PMC, in PMC) :base_core {
Rakudo_types_bool_false_set($1);
Rakudo_types_bool_true_set($2);
}
/*
=item perl6_box_str()
Box a native string to a Perl 6 Str.
=cut
*/
inline op perl6_box_str(out PMC, in STR) :base_core {
PMC *type = Rakudo_types_str_get();
$1 = REPR(type)->allocate(interp, STABLE(type));
REPR($1)->box_funcs->set_str(interp, STABLE($1), OBJECT_BODY($1), $2);
PARROT_GC_WRITE_BARRIER(interp, $1);
}
/*
=item perl6_box_int()
Box a native int to a Perl 6 Int.
=cut
*/
inline op perl6_box_int(out PMC, in INT) :base_core {
PMC *type = Rakudo_types_int_get();
$1 = REPR(type)->allocate(interp, STABLE(type));
REPR($1)->box_funcs->set_int(interp, STABLE($1), OBJECT_BODY($1), $2);
}
/*
=item perl6_box_num()
Box a native floating point number to a Perl 6 Num.
=cut
*/
inline op perl6_box_num(out PMC, in NUM) :base_core {
PMC *type = Rakudo_types_num_get();
$1 = REPR(type)->allocate(interp, STABLE(type));
REPR($1)->box_funcs->set_num(interp, STABLE($1), OBJECT_BODY($1), $2);
}
/*
=item perl6_box_bigint(out PMC, in NUM)
Return a Perl 6 Int if $2 will fit, otherwise return a Perl 6 Num.
=cut
*/
inline op perl6_box_bigint(out PMC, in NUM) :base_core {
if ((INTVAL)$2 == $2) {
PMC *type = Rakudo_types_int_get();
$1 = REPR(type)->allocate(interp, STABLE(type));
REPR($1)->box_funcs->set_int(interp, STABLE($1), OBJECT_BODY($1), $2);
}
else {
PMC *type = Rakudo_types_num_get();
$1 = REPR(type)->allocate(interp, STABLE(type));
REPR($1)->box_funcs->set_num(interp, STABLE($1), OBJECT_BODY($1), $2);
}
}
/*
=item perl6ize_type(out PMC, in PMC)
Looks for Parrot-y types sneaking into Perl 6 land and maps them
into Perl 6 types.
=cut
*/
inline op perl6ize_type(out PMC, in PMC) :base_core {
if ($2->vtable->base_type == smo_id)
$1 = $2;
else
$1 = Rakudo_types_parrot_map(interp, $2);
}
/*
=item set_scalar_container_type(in PMC)
Sets the scalar container type.
=cut
*/
inline op set_scalar_container_type(in PMC) :base_core {
Rakudo_cont_set_scalar_type($1);
}
/*
=item perl6_decontainerize(out PMC, in PMC)
Strips away any outer container, if one exists. Otherwise, no-op.
=cut
*/
inline op perl6_decontainerize(out PMC, in PMC) :base_core {
$1 = $2->vtable->base_type == smo_id ?
Rakudo_cont_decontainerize(interp, $2) :
$2;
}
/*
=item perl6_recontainerize_to_ro()
If the passed value is an rw scalar, re-wrap it. Otherwise, just
hand it on back.
=cut
*/
inline op perl6_recontainerize_to_ro(out PMC, in PMC) :base_core {
if ($2->vtable->base_type == smo_id && Rakudo_cont_is_rw_scalar(interp, $2))
$1 = Rakudo_cont_scalar_with_value_no_descriptor(interp,
Rakudo_cont_decontainerize(interp, $2));
else
$1 = $2;
}
/*
=item perl6_container_store(in PMC, in PMC)
Stores a value in a container. If it's Scalar, there's a fast path;
otherwise, calls the .STORE method.
=cut
*/
inline op perl6_container_store(in PMC, in PMC) :base_core {
Rakudo_cont_store(interp, $1, $2, 1, 1);
}
/*
=item perl6_container_store_unchecked(in PMC, in PMC)
Stores a value in a container. If it's Scalar, there's a fast path;
otherwise, calls the .STORE method. In the fast path case, with this
op no rw or type checking is done (assumes that the compiler has
already decided that it's safe).
=cut
*/
inline op perl6_container_store_unchecked(in PMC, in PMC) :base_core {
Rakudo_cont_store(interp, $1, $2, 0, 0);
}
/*
=item perl6_create_container_descriptor
Creates a container descriptor and puts in in $1. $2 is the type of the
descriptor to create. $3 is the 'of' type, $4 is the rw flag and $5 is
the name.
=cut
*/
inline op perl6_create_container_descriptor(out PMC, in PMC, in PMC, in INT, in STR) :base_core {
$1 = Rakudo_create_container_descriptor(interp, $2, $3, $4, $5);
}
/*
=item perl6_assert_bind_ok(in PMC, in PMC)
Takes a potential value to bind in $1 and a container descriptor in $2
and asserts that the bind is allowed to take place.
=cut
*/
inline op perl6_assert_bind_ok(in PMC, in PMC) :base_core {
if ($2->vtable->base_type == smo_id) {
PMC *type = $2;
if (type != Rakudo_types_mu_get()) {
INTVAL ok = 0;
if ($1->vtable->base_type == smo_id) {
PMC *value = Rakudo_cont_decontainerize(interp, $1);
ok = STABLE(value)->type_check(interp, value, type);
}
if (!ok) {
PMC * const thrower = Rakudo_get_thrower(interp, "X::TypeCheck::Binding");
if (PMC_IS_NULL(thrower))
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Type check failed in binding");
else
Parrot_pcc_invoke_sub_from_c_args(interp, thrower, "PP->",
$1, type);
}
}