-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathp5helper.c
1825 lines (1494 loc) · 46.1 KB
/
p5helper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <EXTERN.h>
#include <perl.h>
#include <XSUB.h>
#define declare_cbs perl6_callbacks *cbs = (perl6_callbacks*)SvIV(*hv_fetchs(PL_modglobal, "Inline::Perl5 callbacks", 0));
static void xs_init (pTHX);
EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
EXTERN_C void boot_Socket (pTHX_ CV* cv);
int p5_is_live_wrapped_p6_object(PerlInterpreter *my_perl, SV *obj);
typedef struct {
I32 key; /* to make sure it came from Inline */
IV index;
I32 is_wrapper;
} _perl6_magic;
typedef struct {
I32 key; /* to make sure it came from Inline */
IV index;
} _perl6_hash_magic;
typedef struct {
SV *(*call_p6_method)(IV, char *, I32, SV *, SV **);
SV *(*call_p6_package_method)(char *, char *, I32, SV *, SV **);
SV *(*call_p6_callable)(IV, SV *, SV **);
void (*free_p6_object)(IV);
SV *(*hash_at_key)(IV, char *);
SV *(*hash_assign_key)(IV, char *, SV *);
SV *(*compile_to_end)(char *, char *, U32 *pos);
} perl6_callbacks;
#ifndef wrap_keyword_plugin
void Perl_wrap_keyword_plugin(pTHX_ Perl_keyword_plugin_t new_plugin, Perl_keyword_plugin_t *old_plugin_p)
{
dVAR;
PERL_UNUSED_CONTEXT;
if (*old_plugin_p) return;
if (!*old_plugin_p) {
*old_plugin_p = PL_keyword_plugin;
PL_keyword_plugin = new_plugin;
}
}
#define wrap_keyword_plugin(new_plugin, old_plugin_p) Perl_wrap_keyword_plugin(aTHX_ new_plugin, old_plugin_p)
#endif
XS(p5_call_p6_method);
XS(p5_call_p6_extension_method);
XS(p5_destroy_p5_object);
XS(p5_call_p6_callable);
XS(p5_hash_at_key);
XS(p5_hash_assign_key);
XS(p5_load_module);
XS(p5_set_subname);
EXTERN_C void xs_init(pTHX) {
char *file = __FILE__;
/* DynaLoader is a special case */
newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
newXS("Perl6::Object::call_method", p5_call_p6_method, file);
newXS("Perl6::Object::call_extension_method", p5_call_p6_extension_method, file);
newXS("Perl6::Object::destroy", p5_destroy_p5_object, file);
newXS("Perl6::Hash::FETCH", p5_hash_at_key, file);
newXS("Perl6::Hash::STORE", p5_hash_assign_key, file);
newXS("Perl6::Callable::call", p5_call_p6_callable, file);
newXS("v6::load_module_impl", p5_load_module, file);
newXS("v6::set_subname", p5_set_subname, file);
}
size_t p5_size_of_iv() {
return IVSIZE;
}
#if NVSIZE > 8
# define MYNVSIZE 8
# define MYNV double
#else
# define MYNVSIZE NVSIZE
# define MYNV NV
#endif
size_t p5_size_of_nv() {
return MYNVSIZE;
}
static int inited = 0;
static Perl_keyword_plugin_t next_keyword_plugin;
static int raku_keyword_plugin(pTHX_ char *keyword_ptr, STRLEN keyword_len, OP **op_ptr) {
if (memEQs(keyword_ptr, keyword_len, "raku")) {
// read the whole source file
while (lex_next_chunk(0));
// move into the raku block
lex_read_space(0);
lex_read_to(PL_parser->bufptr + 1);
declare_cbs;
U32 pos;
char *bufptr = PL_parser->bufptr;
STRLEN len;
char *package_name = SvPV(PL_curstname, len);
SV *code = cbs->compile_to_end(package_name, PL_parser->bufptr, &pos);
lex_read_to(bufptr + pos + 1);
*op_ptr = code ? newUNOP(OP_ENTERSUB, 0, newSVOP(OP_CONST, 0, code)) : NULL;
return KEYWORD_PLUGIN_STMT;
}
else {
return next_keyword_plugin(aTHX_ keyword_ptr, keyword_len, op_ptr);
}
}
void p5_inline_perl6_xs_init(PerlInterpreter *my_perl) {
char *file = __FILE__;
newXS("Perl6::Object::call_method", p5_call_p6_method, file);
newXS("Perl6::Object::call_extension_method", p5_call_p6_extension_method, file);
newXS("Perl6::Hash::FETCH", p5_hash_at_key, file);
newXS("Perl6::Hash::STORE", p5_hash_assign_key, file);
newXS("Perl6::Callable::call", p5_call_p6_callable, file);
newXS("v6::load_module_impl", p5_load_module, file);
newXS("v6::set_subname", p5_set_subname, file);
inited = 1;
}
void p5_init_callbacks(
SV *(*call_p6_method)(IV, char * , I32, SV *, SV **),
SV *(*call_p6_package_method)(char *, char * , I32, SV *, SV **),
SV *(*call_p6_callable)(IV, SV *, SV **),
void (*free_p6_object)(IV),
SV *(*hash_at_key)(IV, char *),
SV *(*hash_assign_key)(IV, char *, SV *),
SV *(*compile_to_end)(char *, char *, U32 *)
) {
perl6_callbacks *cbs = malloc(sizeof(perl6_callbacks));
cbs->call_p6_method = call_p6_method;
cbs->call_p6_package_method = call_p6_package_method;
cbs->call_p6_callable = call_p6_callable;
cbs->free_p6_object = free_p6_object;
cbs->hash_at_key = hash_at_key;
cbs->hash_assign_key = hash_assign_key;
cbs->compile_to_end = compile_to_end;
hv_stores(PL_modglobal, "Inline::Perl5 callbacks", newSViv((IV)cbs));
}
static int interpreters = 0;
static int terminate = 0;
PerlInterpreter *p5_init_perl(
int argc,
char **argv,
SV *(*call_p6_method)(IV, char * , I32, SV *, SV **),
SV *(*call_p6_package_method)(char *, char * , I32, SV *, SV **),
SV *(*call_p6_callable)(IV, SV *, SV **),
void (*free_p6_object)(IV),
SV *(*hash_at_key)(IV, char *),
SV *(*hash_assign_key)(IV, char *, SV *),
SV *(*compile_to_end)(char *, char *, U32 *)
) {
if (inited) {
#ifndef MULTIPLICITY
return NULL;
#endif
}
else {
inited = 1;
PERL_SYS_INIT(&argc, &argv);
}
interpreters++;
PerlInterpreter *my_perl = perl_alloc();
PERL_SET_CONTEXT(my_perl);
PL_perl_destruct_level = 1;
perl_construct( my_perl );
perl_parse(my_perl, xs_init, argc, argv, NULL);
PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
perl_run(my_perl);
p5_init_callbacks(
call_p6_method,
call_p6_package_method,
call_p6_callable,
free_p6_object,
hash_at_key,
hash_assign_key,
compile_to_end
);
wrap_keyword_plugin(raku_keyword_plugin, &next_keyword_plugin);
return my_perl;
}
void p5_destruct_perl(PerlInterpreter *my_perl) {
PERL_SET_CONTEXT(my_perl);
SV **cbs_entry = hv_fetchs(PL_modglobal, "Inline::Perl5 callbacks", 0);
perl6_callbacks *cbs = NULL;
if (cbs_entry) {
cbs = (perl6_callbacks*)SvIV(*cbs_entry);
}
PL_perl_destruct_level = 1;
POPSTACK_TO(PL_mainstack);
dounwind(-1);
LEAVE_SCOPE(0);
perl_destruct(my_perl);
perl_free(my_perl);
if (cbs)
free(cbs);
if (--interpreters == 0 && terminate)
PERL_SYS_TERM();
}
void p5_terminate() {
terminate = 1;
}
U32 p5_SvIOK(PerlInterpreter *my_perl, SV* sv) {
return SvIOK(sv);
}
U32 p5_SvNOK(PerlInterpreter *my_perl, SV* sv) {
return SvNOK(sv);
}
U32 p5_SvPOK(PerlInterpreter *my_perl, SV* sv) {
return SvPOK(sv);
}
U32 p5_sv_utf8(PerlInterpreter *my_perl, SV* sv) {
PERL_SET_CONTEXT(my_perl);
if (SvUTF8(sv)) { // UTF-8 flag set -> can use string as-is
return 1;
}
else { // pure 7 bit ASCII is valid UTF-8 as well
STRLEN len;
char * const pv = SvPV(sv, len);
STRLEN i;
for (i = 0; i < len; i++)
if ((signed char) pv[i] < 0) // signed char!
return 0;
return 1;
}
}
IV p5_sv_iv(PerlInterpreter *my_perl, SV* sv) {
return SvIV(sv);
}
MYNV p5_sv_nv(PerlInterpreter *my_perl, SV* sv) {
return (MYNV) SvNV(sv);
}
SV *p5_sv_rv(PerlInterpreter *my_perl, SV* sv) {
return SvRV(sv);
}
int p5_is_object(PerlInterpreter *my_perl, SV* sv) {
PERL_SET_CONTEXT(my_perl);
return sv_isobject(sv);
}
int p5_is_sub_ref(PerlInterpreter *my_perl, SV* sv) {
return (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV);
}
int p5_is_array(PerlInterpreter *my_perl, SV* sv) {
return (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV);
}
int p5_is_hash(PerlInterpreter *my_perl, SV* sv) {
MAGIC *mg;
PERL_SET_CONTEXT(my_perl);
return (
(SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV)
? ((mg = mg_find(SvRV(sv), PERL_MAGIC_tied)) && sv_isa(mg->mg_obj, "Perl6::Hash"))
? 2
: 1
: 0
);
}
IV p5_unwrap_p6_hash(PerlInterpreter *my_perl, SV *obj) {
PERL_SET_CONTEXT(my_perl);
{
MAGIC * const tie_mg = mg_find(SvRV(obj), PERL_MAGIC_tied);
SV * const hash = tie_mg->mg_obj;
SV * const p6hashobj = *(av_fetch((AV *) SvRV(hash), 0, 0));
MAGIC * const mg = mg_find(SvRV(p6hashobj), '~');
return ((_perl6_hash_magic*)(mg->mg_ptr))->index;
}
}
int p5_is_scalar_ref(PerlInterpreter *my_perl, SV* sv) {
return (SvROK(sv) && SvTYPE(SvRV(sv)) < SVt_PVAV);
}
int p5_is_undef(PerlInterpreter *my_perl, SV* sv) {
return !SvOK(sv);
}
AV *p5_sv_to_av(PerlInterpreter *my_perl, SV* sv) {
return (AV *) SvRV(sv);
}
AV *p5_sv_to_av_inc(PerlInterpreter *my_perl, SV* sv) {
AV * const retval = (AV *) SvRV(sv);
SvREFCNT_inc((SV *)retval);
return retval;
}
HV *p5_sv_to_hv(PerlInterpreter *my_perl, SV* sv) {
return (HV *) SvRV(sv);
}
char *p5_sv_to_char_star(PerlInterpreter *my_perl, SV *sv) {
PERL_SET_CONTEXT(my_perl);
{
STRLEN len;
char * const pv = SvPV(sv, len);
return pv;
}
}
STRLEN p5_sv_to_buf(PerlInterpreter *my_perl, SV *sv, char **buf) {
PERL_SET_CONTEXT(my_perl);
{
STRLEN len;
*buf = SvPV(sv, len);
return len;
}
}
SV *p5_sv_to_ref(PerlInterpreter *my_perl, SV *sv) {
PERL_SET_CONTEXT(my_perl);
return newRV_noinc(sv);
}
int p5_sv_refcnt(PerlInterpreter *my_perl, SV *sv) {
PERL_SET_CONTEXT(my_perl);
return SvREFCNT(sv);
}
void p5_sv_refcnt_dec(PerlInterpreter *my_perl, SV *sv) {
PERL_SET_CONTEXT(my_perl);
SvREFCNT_dec(sv);
}
void p5_sv_refcnt_inc(PerlInterpreter *my_perl, SV *sv) {
SvREFCNT_inc(sv);
}
void p5_sv_destroy(PerlInterpreter *my_perl, SV *sv) {
PERL_SET_CONTEXT(my_perl);
if (!PL_in_clean_objs && !PL_in_clean_all)
SvREFCNT_dec(sv); // don't bother during global deconstruction
}
void p5_sv_2mortal(PerlInterpreter *my_perl, SV *sv) {
sv_2mortal(sv);
}
SV *p5_new_mortal_reference(PerlInterpreter *my_perl, SV *sv) {
return newRV_inc(SvRV(sv));
}
SV *p5_int_to_sv(PerlInterpreter *my_perl, IV value) {
PERL_SET_CONTEXT(my_perl);
return newSViv(value);
}
SV *p5_float_to_sv(PerlInterpreter *my_perl, MYNV value) {
PERL_SET_CONTEXT(my_perl);
return newSVnv((NV)value);
}
SV *p5_str_to_sv(PerlInterpreter *my_perl, STRLEN len, char* value) {
PERL_SET_CONTEXT(my_perl);
return newSVpvn_flags(value, len, SVf_UTF8);
}
SV *p5_buf_to_sv(PerlInterpreter *my_perl, STRLEN len, char* value) {
PERL_SET_CONTEXT(my_perl);
return newSVpvn_flags(value, len, 0);
}
I32 p5_av_top_index(PerlInterpreter *my_perl, AV *av) {
PERL_SET_CONTEXT(my_perl);
return av_top_index(av);
}
SV *p5_av_fetch(PerlInterpreter *my_perl, AV *av, I32 key) {
PERL_SET_CONTEXT(my_perl);
{
SV ** const item = av_fetch(av, key, 0);
if (item)
return *item;
return NULL;
}
}
void p5_av_store(PerlInterpreter *my_perl, AV *av, I32 key, SV *val) {
PERL_SET_CONTEXT(my_perl);
SvREFCNT_inc(val);
if (av_store(av, key, val) == NULL)
SvREFCNT_dec(val);
return;
}
SV *p5_av_pop(PerlInterpreter *my_perl, AV *av) {
PERL_SET_CONTEXT(my_perl);
return av_pop(av);
}
void p5_av_push(PerlInterpreter *my_perl, AV *av, SV *sv) {
PERL_SET_CONTEXT(my_perl);
av_push(av, sv);
}
SV *p5_av_shift(PerlInterpreter *my_perl, AV *av) {
PERL_SET_CONTEXT(my_perl);
return av_shift(av);
}
void p5_av_unshift(PerlInterpreter *my_perl, AV *av, SV *sv) {
PERL_SET_CONTEXT(my_perl);
av_unshift(av, 1);
SvREFCNT_inc(sv);
if (av_store(av, 0, sv) == NULL)
SvREFCNT_dec(sv);
}
void p5_av_delete(PerlInterpreter *my_perl, AV *av, I32 key) {
PERL_SET_CONTEXT(my_perl);
av_delete(av, key, G_DISCARD);
}
void p5_av_clear(PerlInterpreter *my_perl, AV *av) {
PERL_SET_CONTEXT(my_perl);
av_clear(av);
}
I32 p5_hv_iterinit(PerlInterpreter *my_perl, HV *hv) {
PERL_SET_CONTEXT(my_perl);
return hv_iterinit(hv);
}
HE *p5_hv_iternext(PerlInterpreter *my_perl, HV *hv) {
PERL_SET_CONTEXT(my_perl);
return hv_iternext(hv);
}
SV *p5_hv_iterkeysv(PerlInterpreter *my_perl, HE *entry) {
PERL_SET_CONTEXT(my_perl);
return hv_iterkeysv(entry);
}
SV *p5_hv_iterval(PerlInterpreter *my_perl, HV *hv, HE *entry) {
PERL_SET_CONTEXT(my_perl);
return hv_iterval(hv, entry);
}
SV *p5_hv_fetch(PerlInterpreter *my_perl, HV *hv, STRLEN len, const char *key) {
PERL_SET_CONTEXT(my_perl);
{
SV ** const item = hv_fetch(hv, key, len, 0);
if (item)
return *item;
return NULL;
}
}
void p5_hv_store(PerlInterpreter *my_perl, HV *hv, const char *key, SV *val) {
PERL_SET_CONTEXT(my_perl);
hv_store(hv, key, strlen(key), val, 0);
}
int p5_hv_exists(PerlInterpreter *my_perl, HV *hv, STRLEN len, const char *key) {
PERL_SET_CONTEXT(my_perl);
return hv_exists(hv, key, len);
}
SV *p5_undef(PerlInterpreter *my_perl) {
PERL_SET_CONTEXT(my_perl);
return &PL_sv_undef;
}
HV *p5_newHV(PerlInterpreter *my_perl) {
PERL_SET_CONTEXT(my_perl);
return newHV();
}
AV *p5_newAV(PerlInterpreter *my_perl) {
PERL_SET_CONTEXT(my_perl);
return newAV();
}
SV *p5_newRV_noinc(PerlInterpreter *my_perl, SV *sv) {
PERL_SET_CONTEXT(my_perl);
return newRV_noinc(sv);
}
SV *p5_newRV_inc(PerlInterpreter *my_perl, SV *sv) {
PERL_SET_CONTEXT(my_perl);
return newRV_inc(sv);
}
const char *p5_sv_reftype(PerlInterpreter *my_perl, SV *sv) {
PERL_SET_CONTEXT(my_perl);
return sv_reftype(SvRV(sv), 1);
}
SV *p5_new_blessed_hashref(PerlInterpreter *my_perl, char *package) {
PERL_SET_CONTEXT(my_perl);
HV * obj = newHV();
SV * const inst = newRV_noinc((SV*)obj);
HV *stash = gv_stashpv(package, GV_ADD);
(void)sv_bless(inst, stash);
return inst;
}
I32 p5_get_type(PerlInterpreter *my_perl, SV *sv) {
int is_hash;
PERL_SET_CONTEXT(my_perl);
if (p5_is_object(my_perl, sv)) {
return 1;
}
else if (p5_is_sub_ref(my_perl, sv)) {
return 2;
}
else if (p5_SvNOK(my_perl, sv)) {
return 3;
}
else if (p5_SvIOK(my_perl, sv)) {
return 4;
}
else if (p5_SvPOK(my_perl, sv)) {
return 5;
}
else if (p5_is_array(my_perl, sv)) {
return 6;
}
else if ((is_hash = p5_is_hash(my_perl, sv)) > 0) {
return 6 + is_hash;
}
else if (p5_is_undef(my_perl, sv)) {
return 9;
}
else if (p5_is_scalar_ref(my_perl, sv)) {
return 10;
}
else if (SvTYPE(sv) == SVt_PVGV) {
return 11;
}
else {
return 0;
}
}
SV *p5_get_global(PerlInterpreter *my_perl, const char* name) {
PERL_SET_CONTEXT(my_perl);
if (strlen(name) < 2)
return NULL;
if (name[0] == '$')
return get_sv(&name[1], 0);
if (name[0] == '@') {
AV *av = get_av(&name[1], 0);
return av ? sv_2mortal(newRV_inc((SV *)av)) : NULL;
}
if (name[0] == '%') {
HV *hv = get_hv(&name[1], 0);
return hv ? sv_2mortal(newRV_inc((SV *)hv)) : NULL;
}
return NULL;
}
void p5_set_global(PerlInterpreter *my_perl, const char* name, SV *value) {
PERL_SET_CONTEXT(my_perl);
if (strlen(name) < 2)
return;
if (name[0] == '$')
SvSetSV(get_sv(&name[1], 0), value);
else if (name[0] == '@')
croak("Setting global array variable NYI");
else if (name[0] == '%')
croak("Setting global hash variable NYI");
}
I32 p5_compile_sv(PerlInterpreter *my_perl, SV *line, CV **cv, SV **stash) {
PERL_SET_CONTEXT(my_perl);
char *start = NULL;
char start_orig;
I32 floor;
OP *op;
ENTER;
floor = start_subparse(FALSE, 0);
if (PL_parser) {
start = strstr(PL_parser->bufptr, SvPVX(line) + 1);
start_orig = start[-1];
start[-1] = '{';
lex_read_to(start - 1);
}
else {
lex_start(line, NULL, 0);
}
HV *new_stash = newHV();
HV *cur_stash = PL_curstash;
PL_curstash = new_stash;
op = parse_block(0);
PL_curstash = cur_stash;
HE *he;
while ((he = hv_iternext(new_stash))) {
SV *key = hv_iterkeysv(he);
SV *val = hv_iterval(new_stash, he);
/* Put the entry into the actual target stash */
hv_store_ent(PL_curstash, key, SvREFCNT_inc(val), HeHASH(he));
if ( 0 == strcmp(SvPV_nolen(key), "BEGIN")
|| 0 == strcmp(SvPV_nolen(key), "END")
|| 0 == strcmp(SvPV_nolen(key), "__ANON__"))
hv_delete_ent(new_stash, key, G_DISCARD, HeHASH(he));
}
*stash = sv_2mortal(newRV_noinc((SV*)new_stash));
I32 remainder = PL_parser->bufend - PL_parser->bufptr;
*cv = newATTRSUB(floor, NULL, NULL, NULL, op);
if (start)
start[-1] = start_orig;
else
remainder -= 2; /* not sure why but it fixes things */
LEAVE;
return remainder;
}
void p5_runops(PerlInterpreter *my_perl, CV *cv) {
dSP;
ENTER;
SAVETMPS;
PUSHMARK(sp);
call_sv((SV*)cv, G_DISCARD);
SPAGAIN;
FREETMPS;
LEAVE;
}
SV *p5_eval_pv(PerlInterpreter *my_perl, const char* p, I32 croak_on_error) {
PERL_SET_CONTEXT(my_perl);
{
dSP;
SV * retval;
ENTER;
SAVETMPS;
PUSHMARK(SP);
retval = eval_pv(p, croak_on_error);
SvREFCNT_inc(retval);
SPAGAIN;
PUTBACK;
FREETMPS;
LEAVE;
return retval;
}
}
SV *p5_err_sv(PerlInterpreter *my_perl) {
PERL_SET_CONTEXT(my_perl);
return sv_mortalcopy(ERRSV);
}
void handle_p5_error(I32 *err) {
SV *err_tmp = ERRSV;
*err = SvTRUE(err_tmp);
}
void push_arguments(SV **sp, int len, SV *args[]) {
int i;
for (i = 0; i < len; i++) {
if (args[i] != NULL) /* skip Nil which gets turned into NULL */
XPUSHs(sv_2mortal(args[i]));
}
PUTBACK;
}
SV *pop_return_values(PerlInterpreter *my_perl, SV **sp, I32 count, I32 *type) {
SV * retval = NULL;
I32 i;
if (count == 1) {
retval = POPs;
SvREFCNT_inc(retval);
*type = p5_get_type(my_perl, retval);
}
else {
if (count > 1) {
retval = (SV *)newAV();
av_extend((AV *)retval, count - 1);
}
for (i = count - 1; i >= 0; i--) {
SV * const next = POPs;
SvREFCNT_inc(next);
if (av_store((AV *)retval, i, next) == NULL)
SvREFCNT_dec(next); /* see perlguts Working with AVs */
}
}
PUTBACK;
return retval;
}
SV *p5_call_package_method(PerlInterpreter *my_perl, char *package, char *name, int len, SV *args[], I32 *count, I32 *err, I32 *type) {
PERL_SET_CONTEXT(my_perl);
{
dSP;
SV * retval = NULL;
int flags = G_ARRAY | G_EVAL;
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(package, 0)));
push_arguments(sp, len, args);
*count = call_method(name, flags);
SPAGAIN;
handle_p5_error(err);
retval = pop_return_values(my_perl, sp, *count, type);
FREETMPS;
LEAVE;
return retval;
}
}
GV *p5_look_up_package_method(PerlInterpreter *my_perl, char *module, char *name, I32 local) {
PERL_SET_CONTEXT(my_perl);
{
HV * const pkg = gv_stashpvn(module, strlen(module), 0);
GV * gv = gv_fetchmeth_pvn_autoload(pkg, name, strlen(name), -1, SVf_UTF8);
if (gv && local) {
GV * const super_gv = gv_fetchmeth_pvn_autoload(pkg, name, strlen(name), -1, SVf_UTF8 | GV_SUPER);
if (super_gv && (super_gv == gv || GvCV(super_gv) == GvCV(gv)))
gv = NULL;
}
if (gv && isGV(gv))
return gv;
return NULL;
}
}
SV *p5_call_inherited_package_method(PerlInterpreter *my_perl, char *package, char *base_package, char *name, int len, SV *args[], I32 *count, I32 *err, I32 *type) {
PERL_SET_CONTEXT(my_perl);
{
dSP;
SV * retval = NULL;
SV * package_sv = newSVpv(package, 0);
HV * stash = gv_stashsv(package_sv, SVf_UTF8);
int flags = G_ARRAY | G_EVAL;
if (stash == NULL) {
SvREFCNT_dec(package_sv);
*type = -1; /* signal that a wrapper package needs to be created */
return NULL;
}
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(package_sv));
push_arguments(sp, len, args);
GV * const gv = p5_look_up_package_method(my_perl, base_package, name, 0);
SV * const rv = GvCV(gv) ? sv_2mortal(newRV_inc((SV*)GvCV(gv))) : (SV*)gv;
*count = call_sv(rv, flags);
SPAGAIN;
handle_p5_error(err);
retval = pop_return_values(my_perl, sp, *count, type);
FREETMPS;
LEAVE;
return retval;
}
}
char *p5_stash_name(PerlInterpreter *my_perl, SV *obj) {
HV * const pkg = SvSTASH((SV*)SvRV(obj));
return HvNAME(pkg);
}
SV *p5_call_gv(PerlInterpreter *my_perl, GV *gv, int len, SV *args[], I32 *count, I32 *err, I32 *type) {
PERL_SET_CONTEXT(my_perl);
{
dSP;
int i;
SV * retval = NULL;
ENTER;
SAVETMPS;
PUSHMARK(SP);
if (len > 1) {
XPUSHs(args[0]);
for (i = 1; i < len; i++) {
if (args[i] != NULL) /* skip Nil which gets turned into NULL */
XPUSHs(sv_2mortal(args[i]));
}
}
else if (len > 0)
if (args != NULL) /* skip Nil which gets turned into NULL */
XPUSHs((SV*)args);
PUTBACK;
SV * const rv = GvCV(gv) ? sv_2mortal(newRV_inc((SV*)GvCV(gv))) : (SV*)gv;
*count = call_sv(rv, G_ARRAY | G_EVAL);
SPAGAIN;
handle_p5_error(err);
retval = pop_return_values(my_perl, sp, *count, type);
FREETMPS;
LEAVE;
return retval;
}
}
void reset_wrapped_object(PerlInterpreter *my_perl, SV *obj) {
if (SvREFCNT(obj) == 1 && SvREFCNT(SvRV(obj)) == 1) {
MAGIC * mg = mg_find(SvRV(obj), '~');
_perl6_magic* const p6mg = (_perl6_magic*) mg->mg_ptr;
SV **cbs_entry = hv_fetchs(PL_modglobal, "Inline::Perl5 callbacks", 0);
if (cbs_entry) {
perl6_callbacks *cbs = (perl6_callbacks*)SvIV(*cbs_entry);
cbs->free_p6_object(p6mg->index);
}
p6mg->index = -1;
SvREFCNT_inc(SvRV(obj)); // keep it from dying
}
}
SV *p5_call_parent_gv(PerlInterpreter *my_perl, GV *gv, int len, SV *args[], I32 *count, I32 *err, I32 *type) {
PERL_SET_CONTEXT(my_perl);
{
dSP;
int i;
SV * obj;
SV * retval = NULL;
ENTER;
SAVETMPS;
PUSHMARK(SP);
obj = len > 1 ? args[0] : (SV*) args;
if (len > 1) {
XPUSHs(obj);
for (i = 1; i < len; i++) {
if (args[i] != NULL) /* skip Nil which gets turned into NULL */
XPUSHs(sv_2mortal(args[i]));
}
}
else if (len > 0)
if (args != NULL) /* skip Nil which gets turned into NULL */
XPUSHs(obj);
PUTBACK;
SV * const rv = GvCV(gv) ? sv_2mortal(newRV((SV*)GvCV(gv))) : (SV*)gv; /* FIXME: can be done once */
*count = call_sv(rv, G_ARRAY | G_EVAL);
SPAGAIN;
handle_p5_error(err);
retval = pop_return_values(my_perl, sp, *count, type);
reset_wrapped_object(my_perl, obj);
SPAGAIN;
SvREFCNT_dec(obj);
FREETMPS;
LEAVE;
return retval;
}
}
SV *p5_scalar_call_gv(PerlInterpreter *my_perl, GV *gv, int len, SV *args[], I32 *count, I32 *err, I32 *type) {
PERL_SET_CONTEXT(my_perl);
{
dSP;
int i;
SV * retval = NULL;
ENTER;
SAVETMPS;
PUSHMARK(SP);
if (len > 1) {
XPUSHs(args[0]);
for (i = 1; i < len; i++) {
if (args[i] != NULL) /* skip Nil which gets turned into NULL */
XPUSHs(sv_2mortal(args[i]));
}
}
else if (len > 0)
if (args != NULL) /* skip Nil which gets turned into NULL */
XPUSHs((SV*)args);
PUTBACK;
SV * const rv = GvCV(gv) ? sv_2mortal(newRV_inc((SV*)GvCV(gv))) : (SV*)gv;
*count = call_sv(rv, G_SCALAR | G_EVAL);
SPAGAIN;
handle_p5_error(err);
retval = pop_return_values(my_perl, sp, *count, type);
PUTBACK;
FREETMPS;
LEAVE;
return retval;
}
}
SV *p5_scalar_call_parent_gv(PerlInterpreter *my_perl, GV *gv, int len, SV *args[], I32 *count, I32 *err, I32 *type) {
PERL_SET_CONTEXT(my_perl);
{
dSP;
int i;
SV * retval = NULL;
ENTER;
SAVETMPS;
PUSHMARK(SP);
if (len > 1) {
XPUSHs(args[0]);
for (i = 1; i < len; i++) {
if (args[i] != NULL) /* skip Nil which gets turned into NULL */
XPUSHs(sv_2mortal(args[i]));
}
}
else if (len > 0)
if (args != NULL) /* skip Nil which gets turned into NULL */
XPUSHs((SV*)args);
PUTBACK;
SV * const rv = GvCV(gv) ? sv_2mortal(newRV_inc((SV*)GvCV(gv))) : (SV*)gv;
*count = call_sv(rv, G_SCALAR | G_EVAL);
SPAGAIN;
handle_p5_error(err);