This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
3bad3286f21a8f9ed50da1afb08e28ff9edea8db
[perl5.git] / ext / XS-APItest / APItest.xs
1 #define PERL_IN_XS_APITEST
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5
6 typedef SV *SVREF;
7 typedef PTR_TBL_t *XS__APItest__PtrTable;
8
9 #define croak_fail() croak("fail at " __FILE__ " line %d", __LINE__)
10 #define croak_fail_ne(h, w) croak("fail %p!=%p at " __FILE__ " line %d", (h), (w), __LINE__)
11
12 /* for my_cxt tests */
13
14 #define MY_CXT_KEY "XS::APItest::_guts" XS_VERSION
15
16 typedef struct {
17     int i;
18     SV *sv;
19     GV *cscgv;
20     AV *cscav;
21     AV *bhkav;
22     bool bhk_record;
23     peep_t orig_peep;
24     peep_t orig_rpeep;
25     int peep_recording;
26     AV *peep_recorder;
27     AV *rpeep_recorder;
28 } my_cxt_t;
29
30 START_MY_CXT
31
32 /* indirect functions to test the [pa]MY_CXT macros */
33
34 int
35 my_cxt_getint_p(pMY_CXT)
36 {
37     return MY_CXT.i;
38 }
39
40 void
41 my_cxt_setint_p(pMY_CXT_ int i)
42 {
43     MY_CXT.i = i;
44 }
45
46 SV*
47 my_cxt_getsv_interp_context(void)
48 {
49     dTHX;
50     dMY_CXT_INTERP(my_perl);
51     return MY_CXT.sv;
52 }
53
54 SV*
55 my_cxt_getsv_interp(void)
56 {
57     dMY_CXT;
58     return MY_CXT.sv;
59 }
60
61 void
62 my_cxt_setsv_p(SV* sv _pMY_CXT)
63 {
64     MY_CXT.sv = sv;
65 }
66
67
68 /* from exception.c */
69 int apitest_exception(int);
70
71 /* from core_or_not.inc */
72 bool sv_setsv_cow_hashkey_core(void);
73 bool sv_setsv_cow_hashkey_notcore(void);
74
75 /* A routine to test hv_delayfree_ent
76    (which itself is tested by testing on hv_free_ent  */
77
78 typedef void (freeent_function)(pTHX_ HV *, register HE *);
79
80 void
81 test_freeent(freeent_function *f) {
82     dTHX;
83     dSP;
84     HV *test_hash = newHV();
85     HE *victim;
86     SV *test_scalar;
87     U32 results[4];
88     int i;
89
90 #ifdef PURIFY
91     victim = (HE*)safemalloc(sizeof(HE));
92 #else
93     /* Storing then deleting something should ensure that a hash entry is
94        available.  */
95     hv_store(test_hash, "", 0, &PL_sv_yes, 0);
96     hv_delete(test_hash, "", 0, 0);
97
98     /* We need to "inline" new_he here as it's static, and the functions we
99        test expect to be able to call del_HE on the HE  */
100     if (!PL_body_roots[HE_SVSLOT])
101         croak("PL_he_root is 0");
102     victim = (HE*) PL_body_roots[HE_SVSLOT];
103     PL_body_roots[HE_SVSLOT] = HeNEXT(victim);
104 #endif
105
106     victim->hent_hek = Perl_share_hek(aTHX_ "", 0, 0);
107
108     test_scalar = newSV(0);
109     SvREFCNT_inc(test_scalar);
110     HeVAL(victim) = test_scalar;
111
112     /* Need this little game else we free the temps on the return stack.  */
113     results[0] = SvREFCNT(test_scalar);
114     SAVETMPS;
115     results[1] = SvREFCNT(test_scalar);
116     f(aTHX_ test_hash, victim);
117     results[2] = SvREFCNT(test_scalar);
118     FREETMPS;
119     results[3] = SvREFCNT(test_scalar);
120
121     i = 0;
122     do {
123         mPUSHu(results[i]);
124     } while (++i < sizeof(results)/sizeof(results[0]));
125
126     /* Goodbye to our extra reference.  */
127     SvREFCNT_dec(test_scalar);
128 }
129
130
131 static I32
132 bitflip_key(pTHX_ IV action, SV *field) {
133     MAGIC *mg = mg_find(field, PERL_MAGIC_uvar);
134     SV *keysv;
135     if (mg && (keysv = mg->mg_obj)) {
136         STRLEN len;
137         const char *p = SvPV(keysv, len);
138
139         if (len) {
140             SV *newkey = newSV(len);
141             char *new_p = SvPVX(newkey);
142
143             if (SvUTF8(keysv)) {
144                 const char *const end = p + len;
145                 while (p < end) {
146                     STRLEN len;
147                     UV chr = utf8_to_uvuni((U8 *)p, &len);
148                     new_p = (char *)uvuni_to_utf8((U8 *)new_p, chr ^ 32);
149                     p += len;
150                 }
151                 SvUTF8_on(newkey);
152             } else {
153                 while (len--)
154                     *new_p++ = *p++ ^ 32;
155             }
156             *new_p = '\0';
157             SvCUR_set(newkey, SvCUR(keysv));
158             SvPOK_on(newkey);
159
160             mg->mg_obj = newkey;
161         }
162     }
163     return 0;
164 }
165
166 static I32
167 rot13_key(pTHX_ IV action, SV *field) {
168     MAGIC *mg = mg_find(field, PERL_MAGIC_uvar);
169     SV *keysv;
170     if (mg && (keysv = mg->mg_obj)) {
171         STRLEN len;
172         const char *p = SvPV(keysv, len);
173
174         if (len) {
175             SV *newkey = newSV(len);
176             char *new_p = SvPVX(newkey);
177
178             /* There's a deliberate fencepost error here to loop len + 1 times
179                to copy the trailing \0  */
180             do {
181                 char new_c = *p++;
182                 /* Try doing this cleanly and clearly in EBCDIC another way: */
183                 switch (new_c) {
184                 case 'A': new_c = 'N'; break;
185                 case 'B': new_c = 'O'; break;
186                 case 'C': new_c = 'P'; break;
187                 case 'D': new_c = 'Q'; break;
188                 case 'E': new_c = 'R'; break;
189                 case 'F': new_c = 'S'; break;
190                 case 'G': new_c = 'T'; break;
191                 case 'H': new_c = 'U'; break;
192                 case 'I': new_c = 'V'; break;
193                 case 'J': new_c = 'W'; break;
194                 case 'K': new_c = 'X'; break;
195                 case 'L': new_c = 'Y'; break;
196                 case 'M': new_c = 'Z'; break;
197                 case 'N': new_c = 'A'; break;
198                 case 'O': new_c = 'B'; break;
199                 case 'P': new_c = 'C'; break;
200                 case 'Q': new_c = 'D'; break;
201                 case 'R': new_c = 'E'; break;
202                 case 'S': new_c = 'F'; break;
203                 case 'T': new_c = 'G'; break;
204                 case 'U': new_c = 'H'; break;
205                 case 'V': new_c = 'I'; break;
206                 case 'W': new_c = 'J'; break;
207                 case 'X': new_c = 'K'; break;
208                 case 'Y': new_c = 'L'; break;
209                 case 'Z': new_c = 'M'; break;
210                 case 'a': new_c = 'n'; break;
211                 case 'b': new_c = 'o'; break;
212                 case 'c': new_c = 'p'; break;
213                 case 'd': new_c = 'q'; break;
214                 case 'e': new_c = 'r'; break;
215                 case 'f': new_c = 's'; break;
216                 case 'g': new_c = 't'; break;
217                 case 'h': new_c = 'u'; break;
218                 case 'i': new_c = 'v'; break;
219                 case 'j': new_c = 'w'; break;
220                 case 'k': new_c = 'x'; break;
221                 case 'l': new_c = 'y'; break;
222                 case 'm': new_c = 'z'; break;
223                 case 'n': new_c = 'a'; break;
224                 case 'o': new_c = 'b'; break;
225                 case 'p': new_c = 'c'; break;
226                 case 'q': new_c = 'd'; break;
227                 case 'r': new_c = 'e'; break;
228                 case 's': new_c = 'f'; break;
229                 case 't': new_c = 'g'; break;
230                 case 'u': new_c = 'h'; break;
231                 case 'v': new_c = 'i'; break;
232                 case 'w': new_c = 'j'; break;
233                 case 'x': new_c = 'k'; break;
234                 case 'y': new_c = 'l'; break;
235                 case 'z': new_c = 'm'; break;
236                 }
237                 *new_p++ = new_c;
238             } while (len--);
239             SvCUR_set(newkey, SvCUR(keysv));
240             SvPOK_on(newkey);
241             if (SvUTF8(keysv))
242                 SvUTF8_on(newkey);
243
244             mg->mg_obj = newkey;
245         }
246     }
247     return 0;
248 }
249
250 STATIC I32
251 rmagical_a_dummy(pTHX_ IV idx, SV *sv) {
252     return 0;
253 }
254
255 STATIC MGVTBL rmagical_b = { 0 };
256
257 STATIC void
258 blockhook_csc_start(pTHX_ int full)
259 {
260     dMY_CXT;
261     AV *const cur = GvAV(MY_CXT.cscgv);
262
263     SAVEGENERICSV(GvAV(MY_CXT.cscgv));
264
265     if (cur) {
266         I32 i;
267         AV *const new_av = newAV();
268
269         for (i = 0; i <= av_len(cur); i++) {
270             av_store(new_av, i, newSVsv(*av_fetch(cur, i, 0)));
271         }
272
273         GvAV(MY_CXT.cscgv) = new_av;
274     }
275 }
276
277 STATIC void
278 blockhook_csc_pre_end(pTHX_ OP **o)
279 {
280     dMY_CXT;
281
282     /* if we hit the end of a scope we missed the start of, we need to
283      * unconditionally clear @CSC */
284     if (GvAV(MY_CXT.cscgv) == MY_CXT.cscav && MY_CXT.cscav) {
285         av_clear(MY_CXT.cscav);
286     }
287
288 }
289
290 STATIC void
291 blockhook_test_start(pTHX_ int full)
292 {
293     dMY_CXT;
294     AV *av;
295     
296     if (MY_CXT.bhk_record) {
297         av = newAV();
298         av_push(av, newSVpvs("start"));
299         av_push(av, newSViv(full));
300         av_push(MY_CXT.bhkav, newRV_noinc(MUTABLE_SV(av)));
301     }
302 }
303
304 STATIC void
305 blockhook_test_pre_end(pTHX_ OP **o)
306 {
307     dMY_CXT;
308
309     if (MY_CXT.bhk_record)
310         av_push(MY_CXT.bhkav, newSVpvs("pre_end"));
311 }
312
313 STATIC void
314 blockhook_test_post_end(pTHX_ OP **o)
315 {
316     dMY_CXT;
317
318     if (MY_CXT.bhk_record)
319         av_push(MY_CXT.bhkav, newSVpvs("post_end"));
320 }
321
322 STATIC void
323 blockhook_test_eval(pTHX_ OP *const o)
324 {
325     dMY_CXT;
326     AV *av;
327
328     if (MY_CXT.bhk_record) {
329         av = newAV();
330         av_push(av, newSVpvs("eval"));
331         av_push(av, newSVpv(OP_NAME(o), 0));
332         av_push(MY_CXT.bhkav, newRV_noinc(MUTABLE_SV(av)));
333     }
334 }
335
336 STATIC BHK bhk_csc, bhk_test;
337
338 STATIC void
339 my_peep (pTHX_ OP *o)
340 {
341     dMY_CXT;
342
343     if (!o)
344         return;
345
346     MY_CXT.orig_peep(aTHX_ o);
347
348     if (!MY_CXT.peep_recording)
349         return;
350
351     for (; o; o = o->op_next) {
352         if (o->op_type == OP_CONST && cSVOPx_sv(o) && SvPOK(cSVOPx_sv(o))) {
353             av_push(MY_CXT.peep_recorder, newSVsv(cSVOPx_sv(o)));
354         }
355     }
356 }
357
358 STATIC void
359 my_rpeep (pTHX_ OP *o)
360 {
361     dMY_CXT;
362
363     if (!o)
364         return;
365
366     MY_CXT.orig_rpeep(aTHX_ o);
367
368     if (!MY_CXT.peep_recording)
369         return;
370
371     for (; o; o = o->op_next) {
372         if (o->op_type == OP_CONST && cSVOPx_sv(o) && SvPOK(cSVOPx_sv(o))) {
373             av_push(MY_CXT.rpeep_recorder, newSVsv(cSVOPx_sv(o)));
374         }
375     }
376 }
377
378 STATIC OP *
379 THX_ck_entersub_args_lists(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
380 {
381     return ck_entersub_args_list(entersubop);
382 }
383
384 STATIC OP *
385 THX_ck_entersub_args_scalars(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
386 {
387     OP *aop = cUNOPx(entersubop)->op_first;
388     if (!aop->op_sibling)
389         aop = cUNOPx(aop)->op_first;
390     for (aop = aop->op_sibling; aop->op_sibling; aop = aop->op_sibling) {
391         op_contextualize(aop, G_SCALAR);
392     }
393     return entersubop;
394 }
395
396 STATIC OP *
397 THX_ck_entersub_multi_sum(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
398 {
399     OP *sumop = NULL;
400     OP *pushop = cUNOPx(entersubop)->op_first;
401     if (!pushop->op_sibling)
402         pushop = cUNOPx(pushop)->op_first;
403     while (1) {
404         OP *aop = pushop->op_sibling;
405         if (!aop->op_sibling)
406             break;
407         pushop->op_sibling = aop->op_sibling;
408         aop->op_sibling = NULL;
409         op_contextualize(aop, G_SCALAR);
410         if (sumop) {
411             sumop = newBINOP(OP_ADD, 0, sumop, aop);
412         } else {
413             sumop = aop;
414         }
415     }
416     if (!sumop)
417         sumop = newSVOP(OP_CONST, 0, newSViv(0));
418     op_free(entersubop);
419     return sumop;
420 }
421
422 STATIC void test_op_list_describe_part(SV *res, OP *o);
423 STATIC void
424 test_op_list_describe_part(SV *res, OP *o)
425 {
426     sv_catpv(res, PL_op_name[o->op_type]);
427     switch (o->op_type) {
428         case OP_CONST: {
429             sv_catpvf(res, "(%d)", (int)SvIV(cSVOPx(o)->op_sv));
430         } break;
431     }
432     if (o->op_flags & OPf_KIDS) {
433         OP *k;
434         sv_catpvs(res, "[");
435         for (k = cUNOPx(o)->op_first; k; k = k->op_sibling)
436             test_op_list_describe_part(res, k);
437         sv_catpvs(res, "]");
438     } else {
439         sv_catpvs(res, ".");
440     }
441 }
442
443 STATIC char *
444 test_op_list_describe(OP *o)
445 {
446     SV *res = sv_2mortal(newSVpvs(""));
447     if (o)
448         test_op_list_describe_part(res, o);
449     return SvPVX(res);
450 }
451
452 /* the real new*OP functions have a tendancy to call fold_constants, and
453  * other such unhelpful things, so we need our own versions for testing */
454
455 #define mkUNOP(t, f) THX_mkUNOP(aTHX_ (t), (f))
456 static OP *
457 THX_mkUNOP(pTHX_ U32 type, OP *first)
458 {
459     UNOP *unop;
460     NewOp(1103, unop, 1, UNOP);
461     unop->op_type   = (OPCODE)type;
462     unop->op_first  = first;
463     unop->op_flags  = OPf_KIDS;
464     return (OP *)unop;
465 }
466
467 #define mkBINOP(t, f, l) THX_mkBINOP(aTHX_ (t), (f), (l))
468 static OP *
469 THX_mkBINOP(pTHX_ U32 type, OP *first, OP *last)
470 {
471     BINOP *binop;
472     NewOp(1103, binop, 1, BINOP);
473     binop->op_type      = (OPCODE)type;
474     binop->op_first     = first;
475     binop->op_flags     = OPf_KIDS;
476     binop->op_last      = last;
477     first->op_sibling   = last;
478     return (OP *)binop;
479 }
480
481 #define mkLISTOP(t, f, s, l) THX_mkLISTOP(aTHX_ (t), (f), (s), (l))
482 static OP *
483 THX_mkLISTOP(pTHX_ U32 type, OP *first, OP *sib, OP *last)
484 {
485     LISTOP *listop;
486     NewOp(1103, listop, 1, LISTOP);
487     listop->op_type     = (OPCODE)type;
488     listop->op_flags    = OPf_KIDS;
489     listop->op_first    = first;
490     first->op_sibling   = sib;
491     sib->op_sibling     = last;
492     listop->op_last     = last;
493     return (OP *)listop;
494 }
495
496 static char *
497 test_op_linklist_describe(OP *start)
498 {
499     SV *rv = sv_2mortal(newSVpvs(""));
500     OP *o;
501     o = start = LINKLIST(start);
502     do {
503         sv_catpvs(rv, ".");
504         sv_catpv(rv, OP_NAME(o));
505         if (o->op_type == OP_CONST)
506             sv_catsv(rv, cSVOPo->op_sv);
507         o = o->op_next;
508     } while (o && o != start);
509     return SvPVX(rv);
510 }
511
512 /** establish_cleanup operator, ripped off from Scope::Cleanup **/
513
514 STATIC void
515 THX_run_cleanup(pTHX_ void *cleanup_code_ref)
516 {
517     dSP;
518     ENTER;
519     SAVETMPS;
520     PUSHMARK(SP);
521     call_sv((SV*)cleanup_code_ref, G_VOID|G_DISCARD);
522     FREETMPS;
523     LEAVE;
524 }
525
526 STATIC OP *
527 THX_pp_establish_cleanup(pTHX)
528 {
529     dSP;
530     SV *cleanup_code_ref;
531     cleanup_code_ref = newSVsv(POPs);
532     SAVEFREESV(cleanup_code_ref);
533     SAVEDESTRUCTOR_X(THX_run_cleanup, cleanup_code_ref);
534     if(GIMME_V != G_VOID) PUSHs(&PL_sv_undef);
535     RETURN;
536 }
537
538 STATIC OP *
539 THX_ck_entersub_establish_cleanup(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
540 {
541     OP *pushop, *argop, *estop;
542     ck_entersub_args_proto(entersubop, namegv, ckobj);
543     pushop = cUNOPx(entersubop)->op_first;
544     if(!pushop->op_sibling) pushop = cUNOPx(pushop)->op_first;
545     argop = pushop->op_sibling;
546     pushop->op_sibling = argop->op_sibling;
547     argop->op_sibling = NULL;
548     op_free(entersubop);
549     NewOpSz(0, estop, sizeof(UNOP));
550     estop->op_type = OP_RAND;
551     estop->op_ppaddr = THX_pp_establish_cleanup;
552     cUNOPx(estop)->op_flags = OPf_KIDS;
553     cUNOPx(estop)->op_first = argop;
554     PL_hints |= HINT_BLOCK_SCOPE;
555     return estop;
556 }
557
558 STATIC OP *
559 THX_ck_entersub_postinc(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
560 {
561     OP *pushop, *argop, *estop;
562     ck_entersub_args_proto(entersubop, namegv, ckobj);
563     pushop = cUNOPx(entersubop)->op_first;
564     if(!pushop->op_sibling) pushop = cUNOPx(pushop)->op_first;
565     argop = pushop->op_sibling;
566     pushop->op_sibling = argop->op_sibling;
567     argop->op_sibling = NULL;
568     op_free(entersubop);
569     return newUNOP(OP_POSTINC, 0,
570         op_lvalue(op_contextualize(argop, G_SCALAR), OP_POSTINC));
571 }
572
573 /** RPN keyword parser **/
574
575 #define sv_is_glob(sv) (SvTYPE(sv) == SVt_PVGV)
576 #define sv_is_regexp(sv) (SvTYPE(sv) == SVt_REGEXP)
577 #define sv_is_string(sv) \
578     (!sv_is_glob(sv) && !sv_is_regexp(sv) && \
579      (SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK|SVp_IOK|SVp_NOK|SVp_POK)))
580
581 static SV *hintkey_rpn_sv, *hintkey_calcrpn_sv, *hintkey_stufftest_sv;
582 static SV *hintkey_swaptwostmts_sv, *hintkey_looprest_sv;
583 static SV *hintkey_scopelessblock_sv;
584 static SV *hintkey_stmtasexpr_sv, *hintkey_stmtsasexpr_sv;
585 static SV *hintkey_loopblock_sv, *hintkey_blockasexpr_sv;
586 static SV *hintkey_swaplabel_sv, *hintkey_labelconst_sv;
587 static int (*next_keyword_plugin)(pTHX_ char *, STRLEN, OP **);
588
589 /* low-level parser helpers */
590
591 #define PL_bufptr (PL_parser->bufptr)
592 #define PL_bufend (PL_parser->bufend)
593
594 /* RPN parser */
595
596 #define parse_var() THX_parse_var(aTHX)
597 static OP *THX_parse_var(pTHX)
598 {
599     char *s = PL_bufptr;
600     char *start = s;
601     PADOFFSET varpos;
602     OP *padop;
603     if(*s != '$') croak("RPN syntax error");
604     while(1) {
605         char c = *++s;
606         if(!isALNUM(c)) break;
607     }
608     if(s-start < 2) croak("RPN syntax error");
609     lex_read_to(s);
610     {
611         /* because pad_findmy() doesn't really use length yet */
612         SV *namesv = sv_2mortal(newSVpvn(start, s-start));
613         varpos = pad_findmy(SvPVX(namesv), s-start, 0);
614     }
615     if(varpos == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(varpos))
616         croak("RPN only supports \"my\" variables");
617     padop = newOP(OP_PADSV, 0);
618     padop->op_targ = varpos;
619     return padop;
620 }
621
622 #define push_rpn_item(o) \
623     (tmpop = (o), tmpop->op_sibling = stack, stack = tmpop)
624 #define pop_rpn_item() \
625     (!stack ? (croak("RPN stack underflow"), (OP*)NULL) : \
626      (tmpop = stack, stack = stack->op_sibling, \
627       tmpop->op_sibling = NULL, tmpop))
628
629 #define parse_rpn_expr() THX_parse_rpn_expr(aTHX)
630 static OP *THX_parse_rpn_expr(pTHX)
631 {
632     OP *stack = NULL, *tmpop;
633     while(1) {
634         I32 c;
635         lex_read_space(0);
636         c = lex_peek_unichar(0);
637         switch(c) {
638             case /*(*/')': case /*{*/'}': {
639                 OP *result = pop_rpn_item();
640                 if(stack) croak("RPN expression must return a single value");
641                 return result;
642             } break;
643             case '0': case '1': case '2': case '3': case '4':
644             case '5': case '6': case '7': case '8': case '9': {
645                 UV val = 0;
646                 do {
647                     lex_read_unichar(0);
648                     val = 10*val + (c - '0');
649                     c = lex_peek_unichar(0);
650                 } while(c >= '0' && c <= '9');
651                 push_rpn_item(newSVOP(OP_CONST, 0, newSVuv(val)));
652             } break;
653             case '$': {
654                 push_rpn_item(parse_var());
655             } break;
656             case '+': {
657                 OP *b = pop_rpn_item();
658                 OP *a = pop_rpn_item();
659                 lex_read_unichar(0);
660                 push_rpn_item(newBINOP(OP_I_ADD, 0, a, b));
661             } break;
662             case '-': {
663                 OP *b = pop_rpn_item();
664                 OP *a = pop_rpn_item();
665                 lex_read_unichar(0);
666                 push_rpn_item(newBINOP(OP_I_SUBTRACT, 0, a, b));
667             } break;
668             case '*': {
669                 OP *b = pop_rpn_item();
670                 OP *a = pop_rpn_item();
671                 lex_read_unichar(0);
672                 push_rpn_item(newBINOP(OP_I_MULTIPLY, 0, a, b));
673             } break;
674             case '/': {
675                 OP *b = pop_rpn_item();
676                 OP *a = pop_rpn_item();
677                 lex_read_unichar(0);
678                 push_rpn_item(newBINOP(OP_I_DIVIDE, 0, a, b));
679             } break;
680             case '%': {
681                 OP *b = pop_rpn_item();
682                 OP *a = pop_rpn_item();
683                 lex_read_unichar(0);
684                 push_rpn_item(newBINOP(OP_I_MODULO, 0, a, b));
685             } break;
686             default: {
687                 croak("RPN syntax error");
688             } break;
689         }
690     }
691 }
692
693 #define parse_keyword_rpn() THX_parse_keyword_rpn(aTHX)
694 static OP *THX_parse_keyword_rpn(pTHX)
695 {
696     OP *op;
697     lex_read_space(0);
698     if(lex_peek_unichar(0) != '('/*)*/)
699         croak("RPN expression must be parenthesised");
700     lex_read_unichar(0);
701     op = parse_rpn_expr();
702     if(lex_peek_unichar(0) != /*(*/')')
703         croak("RPN expression must be parenthesised");
704     lex_read_unichar(0);
705     return op;
706 }
707
708 #define parse_keyword_calcrpn() THX_parse_keyword_calcrpn(aTHX)
709 static OP *THX_parse_keyword_calcrpn(pTHX)
710 {
711     OP *varop, *exprop;
712     lex_read_space(0);
713     varop = parse_var();
714     lex_read_space(0);
715     if(lex_peek_unichar(0) != '{'/*}*/)
716         croak("RPN expression must be braced");
717     lex_read_unichar(0);
718     exprop = parse_rpn_expr();
719     if(lex_peek_unichar(0) != /*{*/'}')
720         croak("RPN expression must be braced");
721     lex_read_unichar(0);
722     return newASSIGNOP(OPf_STACKED, varop, 0, exprop);
723 }
724
725 #define parse_keyword_stufftest() THX_parse_keyword_stufftest(aTHX)
726 static OP *THX_parse_keyword_stufftest(pTHX)
727 {
728     I32 c;
729     bool do_stuff;
730     lex_read_space(0);
731     do_stuff = lex_peek_unichar(0) == '+';
732     if(do_stuff) {
733         lex_read_unichar(0);
734         lex_read_space(0);
735     }
736     c = lex_peek_unichar(0);
737     if(c == ';') {
738         lex_read_unichar(0);
739     } else if(c != /*{*/'}') {
740         croak("syntax error");
741     }
742     if(do_stuff) lex_stuff_pvs(" ", 0);
743     return newOP(OP_NULL, 0);
744 }
745
746 #define parse_keyword_swaptwostmts() THX_parse_keyword_swaptwostmts(aTHX)
747 static OP *THX_parse_keyword_swaptwostmts(pTHX)
748 {
749     OP *a, *b;
750     a = parse_fullstmt(0);
751     b = parse_fullstmt(0);
752     if(a && b)
753         PL_hints |= HINT_BLOCK_SCOPE;
754     return op_append_list(OP_LINESEQ, b, a);
755 }
756
757 #define parse_keyword_looprest() THX_parse_keyword_looprest(aTHX)
758 static OP *THX_parse_keyword_looprest(pTHX)
759 {
760     return newWHILEOP(0, 1, NULL, newSVOP(OP_CONST, 0, &PL_sv_yes),
761                         parse_stmtseq(0), NULL, 1);
762 }
763
764 #define parse_keyword_scopelessblock() THX_parse_keyword_scopelessblock(aTHX)
765 static OP *THX_parse_keyword_scopelessblock(pTHX)
766 {
767     I32 c;
768     OP *body;
769     lex_read_space(0);
770     if(lex_peek_unichar(0) != '{'/*}*/) croak("syntax error");
771     lex_read_unichar(0);
772     body = parse_stmtseq(0);
773     c = lex_peek_unichar(0);
774     if(c != /*{*/'}' && c != /*[*/']' && c != /*(*/')') croak("syntax error");
775     lex_read_unichar(0);
776     return body;
777 }
778
779 #define parse_keyword_stmtasexpr() THX_parse_keyword_stmtasexpr(aTHX)
780 static OP *THX_parse_keyword_stmtasexpr(pTHX)
781 {
782     OP *o = parse_barestmt(0);
783     if (!o) o = newOP(OP_STUB, 0);
784     if (PL_hints & HINT_BLOCK_SCOPE) o->op_flags |= OPf_PARENS;
785     return op_scope(o);
786 }
787
788 #define parse_keyword_stmtsasexpr() THX_parse_keyword_stmtsasexpr(aTHX)
789 static OP *THX_parse_keyword_stmtsasexpr(pTHX)
790 {
791     OP *o;
792     lex_read_space(0);
793     if(lex_peek_unichar(0) != '{'/*}*/) croak("syntax error");
794     lex_read_unichar(0);
795     o = parse_stmtseq(0);
796     lex_read_space(0);
797     if(lex_peek_unichar(0) != /*{*/'}') croak("syntax error");
798     lex_read_unichar(0);
799     if (!o) o = newOP(OP_STUB, 0);
800     if (PL_hints & HINT_BLOCK_SCOPE) o->op_flags |= OPf_PARENS;
801     return op_scope(o);
802 }
803
804 #define parse_keyword_loopblock() THX_parse_keyword_loopblock(aTHX)
805 static OP *THX_parse_keyword_loopblock(pTHX)
806 {
807     return newWHILEOP(0, 1, NULL, newSVOP(OP_CONST, 0, &PL_sv_yes),
808                         parse_block(0), NULL, 1);
809 }
810
811 #define parse_keyword_blockasexpr() THX_parse_keyword_blockasexpr(aTHX)
812 static OP *THX_parse_keyword_blockasexpr(pTHX)
813 {
814     OP *o = parse_block(0);
815     if (!o) o = newOP(OP_STUB, 0);
816     if (PL_hints & HINT_BLOCK_SCOPE) o->op_flags |= OPf_PARENS;
817     return op_scope(o);
818 }
819
820 #define parse_keyword_swaplabel() THX_parse_keyword_swaplabel(aTHX)
821 static OP *THX_parse_keyword_swaplabel(pTHX)
822 {
823     OP *sop = parse_barestmt(0);
824     SV *label = parse_label(PARSE_OPTIONAL);
825     if (label) sv_2mortal(label);
826     return newSTATEOP(0, label ? savepv(SvPVX(label)) : NULL, sop);
827 }
828
829 #define parse_keyword_labelconst() THX_parse_keyword_labelconst(aTHX)
830 static OP *THX_parse_keyword_labelconst(pTHX)
831 {
832     return newSVOP(OP_CONST, 0, parse_label(0));
833 }
834
835 /* plugin glue */
836
837 #define keyword_active(hintkey_sv) THX_keyword_active(aTHX_ hintkey_sv)
838 static int THX_keyword_active(pTHX_ SV *hintkey_sv)
839 {
840     HE *he;
841     if(!GvHV(PL_hintgv)) return 0;
842     he = hv_fetch_ent(GvHV(PL_hintgv), hintkey_sv, 0,
843                 SvSHARED_HASH(hintkey_sv));
844     return he && SvTRUE(HeVAL(he));
845 }
846
847 static int my_keyword_plugin(pTHX_
848     char *keyword_ptr, STRLEN keyword_len, OP **op_ptr)
849 {
850     if(keyword_len == 3 && strnEQ(keyword_ptr, "rpn", 3) &&
851                     keyword_active(hintkey_rpn_sv)) {
852         *op_ptr = parse_keyword_rpn();
853         return KEYWORD_PLUGIN_EXPR;
854     } else if(keyword_len == 7 && strnEQ(keyword_ptr, "calcrpn", 7) &&
855                     keyword_active(hintkey_calcrpn_sv)) {
856         *op_ptr = parse_keyword_calcrpn();
857         return KEYWORD_PLUGIN_STMT;
858     } else if(keyword_len == 9 && strnEQ(keyword_ptr, "stufftest", 9) &&
859                     keyword_active(hintkey_stufftest_sv)) {
860         *op_ptr = parse_keyword_stufftest();
861         return KEYWORD_PLUGIN_STMT;
862     } else if(keyword_len == 12 &&
863                     strnEQ(keyword_ptr, "swaptwostmts", 12) &&
864                     keyword_active(hintkey_swaptwostmts_sv)) {
865         *op_ptr = parse_keyword_swaptwostmts();
866         return KEYWORD_PLUGIN_STMT;
867     } else if(keyword_len == 8 && strnEQ(keyword_ptr, "looprest", 8) &&
868                     keyword_active(hintkey_looprest_sv)) {
869         *op_ptr = parse_keyword_looprest();
870         return KEYWORD_PLUGIN_STMT;
871     } else if(keyword_len == 14 && strnEQ(keyword_ptr, "scopelessblock", 14) &&
872                     keyword_active(hintkey_scopelessblock_sv)) {
873         *op_ptr = parse_keyword_scopelessblock();
874         return KEYWORD_PLUGIN_STMT;
875     } else if(keyword_len == 10 && strnEQ(keyword_ptr, "stmtasexpr", 10) &&
876                     keyword_active(hintkey_stmtasexpr_sv)) {
877         *op_ptr = parse_keyword_stmtasexpr();
878         return KEYWORD_PLUGIN_EXPR;
879     } else if(keyword_len == 11 && strnEQ(keyword_ptr, "stmtsasexpr", 11) &&
880                     keyword_active(hintkey_stmtsasexpr_sv)) {
881         *op_ptr = parse_keyword_stmtsasexpr();
882         return KEYWORD_PLUGIN_EXPR;
883     } else if(keyword_len == 9 && strnEQ(keyword_ptr, "loopblock", 9) &&
884                     keyword_active(hintkey_loopblock_sv)) {
885         *op_ptr = parse_keyword_loopblock();
886         return KEYWORD_PLUGIN_STMT;
887     } else if(keyword_len == 11 && strnEQ(keyword_ptr, "blockasexpr", 11) &&
888                     keyword_active(hintkey_blockasexpr_sv)) {
889         *op_ptr = parse_keyword_blockasexpr();
890         return KEYWORD_PLUGIN_EXPR;
891     } else if(keyword_len == 9 && strnEQ(keyword_ptr, "swaplabel", 9) &&
892                     keyword_active(hintkey_swaplabel_sv)) {
893         *op_ptr = parse_keyword_swaplabel();
894         return KEYWORD_PLUGIN_STMT;
895     } else if(keyword_len == 10 && strnEQ(keyword_ptr, "labelconst", 10) &&
896                     keyword_active(hintkey_labelconst_sv)) {
897         *op_ptr = parse_keyword_labelconst();
898         return KEYWORD_PLUGIN_EXPR;
899     } else {
900         return next_keyword_plugin(aTHX_ keyword_ptr, keyword_len, op_ptr);
901     }
902 }
903
904 XS(XS_XS__APItest__XSUB_XS_VERSION_undef);
905 XS(XS_XS__APItest__XSUB_XS_VERSION_empty);
906 XS(XS_XS__APItest__XSUB_XS_APIVERSION_invalid);
907
908 #include "const-c.inc"
909
910 MODULE = XS::APItest            PACKAGE = XS::APItest
911
912 INCLUDE: const-xs.inc
913
914 INCLUDE: numeric.xs
915
916 MODULE = XS::APItest:Overload   PACKAGE = XS::APItest::Overload
917
918 SV *
919 amagic_deref_call(sv, what)
920         SV *sv
921         int what
922     PPCODE:
923         /* The reference is owned by something else.  */
924         PUSHs(amagic_deref_call(sv, what));
925
926 MODULE = XS::APItest            PACKAGE = XS::APItest::XSUB
927
928 BOOT:
929     newXS("XS::APItest::XSUB::XS_VERSION_undef", XS_XS__APItest__XSUB_XS_VERSION_undef, __FILE__);
930     newXS("XS::APItest::XSUB::XS_VERSION_empty", XS_XS__APItest__XSUB_XS_VERSION_empty, __FILE__);
931     newXS("XS::APItest::XSUB::XS_APIVERSION_invalid", XS_XS__APItest__XSUB_XS_APIVERSION_invalid, __FILE__);
932
933 void
934 XS_VERSION_defined(...)
935     PPCODE:
936         XS_VERSION_BOOTCHECK;
937         XSRETURN_EMPTY;
938
939 void
940 XS_APIVERSION_valid(...)
941     PPCODE:
942         XS_APIVERSION_BOOTCHECK;
943         XSRETURN_EMPTY;
944
945 MODULE = XS::APItest:Hash               PACKAGE = XS::APItest::Hash
946
947 void
948 rot13_hash(hash)
949         HV *hash
950         CODE:
951         {
952             struct ufuncs uf;
953             uf.uf_val = rot13_key;
954             uf.uf_set = 0;
955             uf.uf_index = 0;
956
957             sv_magic((SV*)hash, NULL, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
958         }
959
960 void
961 bitflip_hash(hash)
962         HV *hash
963         CODE:
964         {
965             struct ufuncs uf;
966             uf.uf_val = bitflip_key;
967             uf.uf_set = 0;
968             uf.uf_index = 0;
969
970             sv_magic((SV*)hash, NULL, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
971         }
972
973 #define UTF8KLEN(sv, len)   (SvUTF8(sv) ? -(I32)len : (I32)len)
974
975 bool
976 exists(hash, key_sv)
977         PREINIT:
978         STRLEN len;
979         const char *key;
980         INPUT:
981         HV *hash
982         SV *key_sv
983         CODE:
984         key = SvPV(key_sv, len);
985         RETVAL = hv_exists(hash, key, UTF8KLEN(key_sv, len));
986         OUTPUT:
987         RETVAL
988
989 bool
990 exists_ent(hash, key_sv)
991         PREINIT:
992         INPUT:
993         HV *hash
994         SV *key_sv
995         CODE:
996         RETVAL = hv_exists_ent(hash, key_sv, 0);
997         OUTPUT:
998         RETVAL
999
1000 SV *
1001 delete(hash, key_sv, flags = 0)
1002         PREINIT:
1003         STRLEN len;
1004         const char *key;
1005         INPUT:
1006         HV *hash
1007         SV *key_sv
1008         I32 flags;
1009         CODE:
1010         key = SvPV(key_sv, len);
1011         /* It's already mortal, so need to increase reference count.  */
1012         RETVAL
1013             = SvREFCNT_inc(hv_delete(hash, key, UTF8KLEN(key_sv, len), flags));
1014         OUTPUT:
1015         RETVAL
1016
1017 SV *
1018 delete_ent(hash, key_sv, flags = 0)
1019         INPUT:
1020         HV *hash
1021         SV *key_sv
1022         I32 flags;
1023         CODE:
1024         /* It's already mortal, so need to increase reference count.  */
1025         RETVAL = SvREFCNT_inc(hv_delete_ent(hash, key_sv, flags, 0));
1026         OUTPUT:
1027         RETVAL
1028
1029 SV *
1030 store_ent(hash, key, value)
1031         PREINIT:
1032         SV *copy;
1033         HE *result;
1034         INPUT:
1035         HV *hash
1036         SV *key
1037         SV *value
1038         CODE:
1039         copy = newSV(0);
1040         result = hv_store_ent(hash, key, copy, 0);
1041         SvSetMagicSV(copy, value);
1042         if (!result) {
1043             SvREFCNT_dec(copy);
1044             XSRETURN_EMPTY;
1045         }
1046         /* It's about to become mortal, so need to increase reference count.
1047          */
1048         RETVAL = SvREFCNT_inc(HeVAL(result));
1049         OUTPUT:
1050         RETVAL
1051
1052 SV *
1053 store(hash, key_sv, value)
1054         PREINIT:
1055         STRLEN len;
1056         const char *key;
1057         SV *copy;
1058         SV **result;
1059         INPUT:
1060         HV *hash
1061         SV *key_sv
1062         SV *value
1063         CODE:
1064         key = SvPV(key_sv, len);
1065         copy = newSV(0);
1066         result = hv_store(hash, key, UTF8KLEN(key_sv, len), copy, 0);
1067         SvSetMagicSV(copy, value);
1068         if (!result) {
1069             SvREFCNT_dec(copy);
1070             XSRETURN_EMPTY;
1071         }
1072         /* It's about to become mortal, so need to increase reference count.
1073          */
1074         RETVAL = SvREFCNT_inc(*result);
1075         OUTPUT:
1076         RETVAL
1077
1078 SV *
1079 fetch_ent(hash, key_sv)
1080         PREINIT:
1081         HE *result;
1082         INPUT:
1083         HV *hash
1084         SV *key_sv
1085         CODE:
1086         result = hv_fetch_ent(hash, key_sv, 0, 0);
1087         if (!result) {
1088             XSRETURN_EMPTY;
1089         }
1090         /* Force mg_get  */
1091         RETVAL = newSVsv(HeVAL(result));
1092         OUTPUT:
1093         RETVAL
1094
1095 SV *
1096 fetch(hash, key_sv)
1097         PREINIT:
1098         STRLEN len;
1099         const char *key;
1100         SV **result;
1101         INPUT:
1102         HV *hash
1103         SV *key_sv
1104         CODE:
1105         key = SvPV(key_sv, len);
1106         result = hv_fetch(hash, key, UTF8KLEN(key_sv, len), 0);
1107         if (!result) {
1108             XSRETURN_EMPTY;
1109         }
1110         /* Force mg_get  */
1111         RETVAL = newSVsv(*result);
1112         OUTPUT:
1113         RETVAL
1114
1115 #if defined (hv_common)
1116
1117 SV *
1118 common(params)
1119         INPUT:
1120         HV *params
1121         PREINIT:
1122         HE *result;
1123         HV *hv = NULL;
1124         SV *keysv = NULL;
1125         const char *key = NULL;
1126         STRLEN klen = 0;
1127         int flags = 0;
1128         int action = 0;
1129         SV *val = NULL;
1130         U32 hash = 0;
1131         SV **svp;
1132         CODE:
1133         if ((svp = hv_fetchs(params, "hv", 0))) {
1134             SV *const rv = *svp;
1135             if (!SvROK(rv))
1136                 croak("common passed a non-reference for parameter hv");
1137             hv = (HV *)SvRV(rv);
1138         }
1139         if ((svp = hv_fetchs(params, "keysv", 0)))
1140             keysv = *svp;
1141         if ((svp = hv_fetchs(params, "keypv", 0))) {
1142             key = SvPV_const(*svp, klen);
1143             if (SvUTF8(*svp))
1144                 flags = HVhek_UTF8;
1145         }
1146         if ((svp = hv_fetchs(params, "action", 0)))
1147             action = SvIV(*svp);
1148         if ((svp = hv_fetchs(params, "val", 0)))
1149             val = newSVsv(*svp);
1150         if ((svp = hv_fetchs(params, "hash", 0)))
1151             hash = SvUV(*svp);
1152
1153         if ((svp = hv_fetchs(params, "hash_pv", 0))) {
1154             PERL_HASH(hash, key, klen);
1155         }
1156         if ((svp = hv_fetchs(params, "hash_sv", 0))) {
1157             STRLEN len;
1158             const char *const p = SvPV(keysv, len);
1159             PERL_HASH(hash, p, len);
1160         }
1161
1162         result = (HE *)hv_common(hv, keysv, key, klen, flags, action, val, hash);
1163         if (!result) {
1164             XSRETURN_EMPTY;
1165         }
1166         /* Force mg_get  */
1167         RETVAL = newSVsv(HeVAL(result));
1168         OUTPUT:
1169         RETVAL
1170
1171 #endif
1172
1173 void
1174 test_hv_free_ent()
1175         PPCODE:
1176         test_freeent(&Perl_hv_free_ent);
1177         XSRETURN(4);
1178
1179 void
1180 test_hv_delayfree_ent()
1181         PPCODE:
1182         test_freeent(&Perl_hv_delayfree_ent);
1183         XSRETURN(4);
1184
1185 SV *
1186 test_share_unshare_pvn(input)
1187         PREINIT:
1188         STRLEN len;
1189         U32 hash;
1190         char *pvx;
1191         char *p;
1192         INPUT:
1193         SV *input
1194         CODE:
1195         pvx = SvPV(input, len);
1196         PERL_HASH(hash, pvx, len);
1197         p = sharepvn(pvx, len, hash);
1198         RETVAL = newSVpvn(p, len);
1199         unsharepvn(p, len, hash);
1200         OUTPUT:
1201         RETVAL
1202
1203 #if PERL_VERSION >= 9
1204
1205 bool
1206 refcounted_he_exists(key, level=0)
1207         SV *key
1208         IV level
1209         CODE:
1210         if (level) {
1211             croak("level must be zero, not %"IVdf, level);
1212         }
1213         RETVAL = (cop_hints_fetch_sv(PL_curcop, key, 0, 0) != &PL_sv_placeholder);
1214         OUTPUT:
1215         RETVAL
1216
1217 SV *
1218 refcounted_he_fetch(key, level=0)
1219         SV *key
1220         IV level
1221         CODE:
1222         if (level) {
1223             croak("level must be zero, not %"IVdf, level);
1224         }
1225         RETVAL = cop_hints_fetch_sv(PL_curcop, key, 0, 0);
1226         SvREFCNT_inc(RETVAL);
1227         OUTPUT:
1228         RETVAL
1229
1230 #endif
1231
1232 =pod
1233
1234 sub TIEHASH  { bless {}, $_[0] }
1235 sub STORE    { $_[0]->{$_[1]} = $_[2] }
1236 sub FETCH    { $_[0]->{$_[1]} }
1237 sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
1238 sub NEXTKEY  { each %{$_[0]} }
1239 sub EXISTS   { exists $_[0]->{$_[1]} }
1240 sub DELETE   { delete $_[0]->{$_[1]} }
1241 sub CLEAR    { %{$_[0]} = () }
1242
1243 =cut
1244
1245 MODULE = XS::APItest:TempLv             PACKAGE = XS::APItest::TempLv
1246
1247 void
1248 make_temp_mg_lv(sv)
1249 SV* sv
1250     PREINIT:
1251         SV * const lv = newSV_type(SVt_PVLV);
1252         STRLEN len;
1253     PPCODE:
1254         SvPV(sv, len);
1255
1256         sv_magic(lv, NULL, PERL_MAGIC_substr, NULL, 0);
1257         LvTYPE(lv) = 'x';
1258         LvTARG(lv) = SvREFCNT_inc_simple(sv);
1259         LvTARGOFF(lv) = len == 0 ? 0 : 1;
1260         LvTARGLEN(lv) = len < 2 ? 0 : len-2;
1261
1262         EXTEND(SP, 1);
1263         ST(0) = sv_2mortal(lv);
1264         XSRETURN(1);
1265
1266
1267 MODULE = XS::APItest::PtrTable  PACKAGE = XS::APItest::PtrTable PREFIX = ptr_table_
1268
1269 void
1270 ptr_table_new(classname)
1271 const char * classname
1272     PPCODE:
1273     PUSHs(sv_setref_pv(sv_newmortal(), classname, (void*)ptr_table_new()));
1274
1275 void
1276 DESTROY(table)
1277 XS::APItest::PtrTable table
1278     CODE:
1279     ptr_table_free(table);
1280
1281 void
1282 ptr_table_store(table, from, to)
1283 XS::APItest::PtrTable table
1284 SVREF from
1285 SVREF to
1286    CODE:
1287    ptr_table_store(table, from, to);
1288
1289 UV
1290 ptr_table_fetch(table, from)
1291 XS::APItest::PtrTable table
1292 SVREF from
1293    CODE:
1294    RETVAL = PTR2UV(ptr_table_fetch(table, from));
1295    OUTPUT:
1296    RETVAL
1297
1298 void
1299 ptr_table_split(table)
1300 XS::APItest::PtrTable table
1301
1302 void
1303 ptr_table_clear(table)
1304 XS::APItest::PtrTable table
1305
1306 MODULE = XS::APItest            PACKAGE = XS::APItest
1307
1308 PROTOTYPES: DISABLE
1309
1310 BOOT:
1311 {
1312     MY_CXT_INIT;
1313
1314     MY_CXT.i  = 99;
1315     MY_CXT.sv = newSVpv("initial",0);
1316
1317     MY_CXT.bhkav = get_av("XS::APItest::bhkav", GV_ADDMULTI);
1318     MY_CXT.bhk_record = 0;
1319
1320     BhkENTRY_set(&bhk_test, bhk_start, blockhook_test_start);
1321     BhkENTRY_set(&bhk_test, bhk_pre_end, blockhook_test_pre_end);
1322     BhkENTRY_set(&bhk_test, bhk_post_end, blockhook_test_post_end);
1323     BhkENTRY_set(&bhk_test, bhk_eval, blockhook_test_eval);
1324     Perl_blockhook_register(aTHX_ &bhk_test);
1325
1326     MY_CXT.cscgv = gv_fetchpvs("XS::APItest::COMPILE_SCOPE_CONTAINER",
1327         GV_ADDMULTI, SVt_PVAV);
1328     MY_CXT.cscav = GvAV(MY_CXT.cscgv);
1329
1330     BhkENTRY_set(&bhk_csc, bhk_start, blockhook_csc_start);
1331     BhkENTRY_set(&bhk_csc, bhk_pre_end, blockhook_csc_pre_end);
1332     Perl_blockhook_register(aTHX_ &bhk_csc);
1333
1334     MY_CXT.peep_recorder = newAV();
1335     MY_CXT.rpeep_recorder = newAV();
1336
1337     MY_CXT.orig_peep = PL_peepp;
1338     MY_CXT.orig_rpeep = PL_rpeepp;
1339     PL_peepp = my_peep;
1340     PL_rpeepp = my_rpeep;
1341 }
1342
1343 void
1344 CLONE(...)
1345     CODE:
1346     MY_CXT_CLONE;
1347     MY_CXT.sv = newSVpv("initial_clone",0);
1348     MY_CXT.cscgv = gv_fetchpvs("XS::APItest::COMPILE_SCOPE_CONTAINER",
1349         GV_ADDMULTI, SVt_PVAV);
1350     MY_CXT.cscav = NULL;
1351     MY_CXT.bhkav = get_av("XS::APItest::bhkav", GV_ADDMULTI);
1352     MY_CXT.bhk_record = 0;
1353     MY_CXT.peep_recorder = newAV();
1354     MY_CXT.rpeep_recorder = newAV();
1355
1356 void
1357 print_double(val)
1358         double val
1359         CODE:
1360         printf("%5.3f\n",val);
1361
1362 int
1363 have_long_double()
1364         CODE:
1365 #ifdef HAS_LONG_DOUBLE
1366         RETVAL = 1;
1367 #else
1368         RETVAL = 0;
1369 #endif
1370         OUTPUT:
1371         RETVAL
1372
1373 void
1374 print_long_double()
1375         CODE:
1376 #ifdef HAS_LONG_DOUBLE
1377 #   if defined(PERL_PRIfldbl) && (LONG_DOUBLESIZE > DOUBLESIZE)
1378         long double val = 7.0;
1379         printf("%5.3" PERL_PRIfldbl "\n",val);
1380 #   else
1381         double val = 7.0;
1382         printf("%5.3f\n",val);
1383 #   endif
1384 #endif
1385
1386 void
1387 print_int(val)
1388         int val
1389         CODE:
1390         printf("%d\n",val);
1391
1392 void
1393 print_long(val)
1394         long val
1395         CODE:
1396         printf("%ld\n",val);
1397
1398 void
1399 print_float(val)
1400         float val
1401         CODE:
1402         printf("%5.3f\n",val);
1403         
1404 void
1405 print_flush()
1406         CODE:
1407         fflush(stdout);
1408
1409 void
1410 mpushp()
1411         PPCODE:
1412         EXTEND(SP, 3);
1413         mPUSHp("one", 3);
1414         mPUSHp("two", 3);
1415         mPUSHp("three", 5);
1416         XSRETURN(3);
1417
1418 void
1419 mpushn()
1420         PPCODE:
1421         EXTEND(SP, 3);
1422         mPUSHn(0.5);
1423         mPUSHn(-0.25);
1424         mPUSHn(0.125);
1425         XSRETURN(3);
1426
1427 void
1428 mpushi()
1429         PPCODE:
1430         EXTEND(SP, 3);
1431         mPUSHi(-1);
1432         mPUSHi(2);
1433         mPUSHi(-3);
1434         XSRETURN(3);
1435
1436 void
1437 mpushu()
1438         PPCODE:
1439         EXTEND(SP, 3);
1440         mPUSHu(1);
1441         mPUSHu(2);
1442         mPUSHu(3);
1443         XSRETURN(3);
1444
1445 void
1446 mxpushp()
1447         PPCODE:
1448         mXPUSHp("one", 3);
1449         mXPUSHp("two", 3);
1450         mXPUSHp("three", 5);
1451         XSRETURN(3);
1452
1453 void
1454 mxpushn()
1455         PPCODE:
1456         mXPUSHn(0.5);
1457         mXPUSHn(-0.25);
1458         mXPUSHn(0.125);
1459         XSRETURN(3);
1460
1461 void
1462 mxpushi()
1463         PPCODE:
1464         mXPUSHi(-1);
1465         mXPUSHi(2);
1466         mXPUSHi(-3);
1467         XSRETURN(3);
1468
1469 void
1470 mxpushu()
1471         PPCODE:
1472         mXPUSHu(1);
1473         mXPUSHu(2);
1474         mXPUSHu(3);
1475         XSRETURN(3);
1476
1477
1478 void
1479 call_sv(sv, flags, ...)
1480     SV* sv
1481     I32 flags
1482     PREINIT:
1483         I32 i;
1484     PPCODE:
1485         for (i=0; i<items-2; i++)
1486             ST(i) = ST(i+2); /* pop first two args */
1487         PUSHMARK(SP);
1488         SP += items - 2;
1489         PUTBACK;
1490         i = call_sv(sv, flags);
1491         SPAGAIN;
1492         EXTEND(SP, 1);
1493         PUSHs(sv_2mortal(newSViv(i)));
1494
1495 void
1496 call_pv(subname, flags, ...)
1497     char* subname
1498     I32 flags
1499     PREINIT:
1500         I32 i;
1501     PPCODE:
1502         for (i=0; i<items-2; i++)
1503             ST(i) = ST(i+2); /* pop first two args */
1504         PUSHMARK(SP);
1505         SP += items - 2;
1506         PUTBACK;
1507         i = call_pv(subname, flags);
1508         SPAGAIN;
1509         EXTEND(SP, 1);
1510         PUSHs(sv_2mortal(newSViv(i)));
1511
1512 void
1513 call_method(methname, flags, ...)
1514     char* methname
1515     I32 flags
1516     PREINIT:
1517         I32 i;
1518     PPCODE:
1519         for (i=0; i<items-2; i++)
1520             ST(i) = ST(i+2); /* pop first two args */
1521         PUSHMARK(SP);
1522         SP += items - 2;
1523         PUTBACK;
1524         i = call_method(methname, flags);
1525         SPAGAIN;
1526         EXTEND(SP, 1);
1527         PUSHs(sv_2mortal(newSViv(i)));
1528
1529 void
1530 eval_sv(sv, flags)
1531     SV* sv
1532     I32 flags
1533     PREINIT:
1534         I32 i;
1535     PPCODE:
1536         PUTBACK;
1537         i = eval_sv(sv, flags);
1538         SPAGAIN;
1539         EXTEND(SP, 1);
1540         PUSHs(sv_2mortal(newSViv(i)));
1541
1542 void
1543 eval_pv(p, croak_on_error)
1544     const char* p
1545     I32 croak_on_error
1546     PPCODE:
1547         PUTBACK;
1548         EXTEND(SP, 1);
1549         PUSHs(eval_pv(p, croak_on_error));
1550
1551 void
1552 require_pv(pv)
1553     const char* pv
1554     PPCODE:
1555         PUTBACK;
1556         require_pv(pv);
1557
1558 int
1559 apitest_exception(throw_e)
1560     int throw_e
1561     OUTPUT:
1562         RETVAL
1563
1564 void
1565 mycroak(sv)
1566     SV* sv
1567     CODE:
1568     if (SvOK(sv)) {
1569         Perl_croak(aTHX_ "%s", SvPV_nolen(sv));
1570     }
1571     else {
1572         Perl_croak(aTHX_ NULL);
1573     }
1574
1575 SV*
1576 strtab()
1577    CODE:
1578    RETVAL = newRV_inc((SV*)PL_strtab);
1579    OUTPUT:
1580    RETVAL
1581
1582 int
1583 my_cxt_getint()
1584     CODE:
1585         dMY_CXT;
1586         RETVAL = my_cxt_getint_p(aMY_CXT);
1587     OUTPUT:
1588         RETVAL
1589
1590 void
1591 my_cxt_setint(i)
1592     int i;
1593     CODE:
1594         dMY_CXT;
1595         my_cxt_setint_p(aMY_CXT_ i);
1596
1597 void
1598 my_cxt_getsv(how)
1599     bool how;
1600     PPCODE:
1601         EXTEND(SP, 1);
1602         ST(0) = how ? my_cxt_getsv_interp_context() : my_cxt_getsv_interp();
1603         XSRETURN(1);
1604
1605 void
1606 my_cxt_setsv(sv)
1607     SV *sv;
1608     CODE:
1609         dMY_CXT;
1610         SvREFCNT_dec(MY_CXT.sv);
1611         my_cxt_setsv_p(sv _aMY_CXT);
1612         SvREFCNT_inc(sv);
1613
1614 bool
1615 sv_setsv_cow_hashkey_core()
1616
1617 bool
1618 sv_setsv_cow_hashkey_notcore()
1619
1620 void
1621 rmagical_cast(sv, type)
1622     SV *sv;
1623     SV *type;
1624     PREINIT:
1625         struct ufuncs uf;
1626     PPCODE:
1627         if (!SvOK(sv) || !SvROK(sv) || !SvOK(type)) { XSRETURN_UNDEF; }
1628         sv = SvRV(sv);
1629         if (SvTYPE(sv) != SVt_PVHV) { XSRETURN_UNDEF; }
1630         uf.uf_val = rmagical_a_dummy;
1631         uf.uf_set = NULL;
1632         uf.uf_index = 0;
1633         if (SvTRUE(type)) { /* b */
1634             sv_magicext(sv, NULL, PERL_MAGIC_ext, &rmagical_b, NULL, 0);
1635         } else { /* a */
1636             sv_magic(sv, NULL, PERL_MAGIC_uvar, (char *) &uf, sizeof(uf));
1637         }
1638         XSRETURN_YES;
1639
1640 void
1641 rmagical_flags(sv)
1642     SV *sv;
1643     PPCODE:
1644         if (!SvOK(sv) || !SvROK(sv)) { XSRETURN_UNDEF; }
1645         sv = SvRV(sv);
1646         EXTEND(SP, 3); 
1647         mXPUSHu(SvFLAGS(sv) & SVs_GMG);
1648         mXPUSHu(SvFLAGS(sv) & SVs_SMG);
1649         mXPUSHu(SvFLAGS(sv) & SVs_RMG);
1650         XSRETURN(3);
1651
1652 void
1653 my_caller(level)
1654         I32 level
1655     PREINIT:
1656         const PERL_CONTEXT *cx, *dbcx;
1657         const char *pv;
1658         const GV *gv;
1659         HV *hv;
1660     PPCODE:
1661         cx = caller_cx(level, &dbcx);
1662         EXTEND(SP, 8);
1663
1664         pv = CopSTASHPV(cx->blk_oldcop);
1665         ST(0) = pv ? sv_2mortal(newSVpv(pv, 0)) : &PL_sv_undef;
1666         gv = CvGV(cx->blk_sub.cv);
1667         ST(1) = isGV(gv) ? sv_2mortal(newSVpv(GvNAME(gv), 0)) : &PL_sv_undef;
1668
1669         pv = CopSTASHPV(dbcx->blk_oldcop);
1670         ST(2) = pv ? sv_2mortal(newSVpv(pv, 0)) : &PL_sv_undef;
1671         gv = CvGV(dbcx->blk_sub.cv);
1672         ST(3) = isGV(gv) ? sv_2mortal(newSVpv(GvNAME(gv), 0)) : &PL_sv_undef;
1673
1674         ST(4) = cop_hints_fetch_pvs(cx->blk_oldcop, "foo", 0);
1675         ST(5) = cop_hints_fetch_pvn(cx->blk_oldcop, "foo", 3, 0, 0);
1676         ST(6) = cop_hints_fetch_sv(cx->blk_oldcop, 
1677                 sv_2mortal(newSVpvn("foo", 3)), 0, 0);
1678
1679         hv = cop_hints_2hv(cx->blk_oldcop, 0);
1680         ST(7) = hv ? sv_2mortal(newRV_noinc((SV *)hv)) : &PL_sv_undef;
1681
1682         XSRETURN(8);
1683
1684 void
1685 DPeek (sv)
1686     SV   *sv
1687
1688   PPCODE:
1689     ST (0) = newSVpv (Perl_sv_peek (aTHX_ sv), 0);
1690     XSRETURN (1);
1691
1692 void
1693 BEGIN()
1694     CODE:
1695         sv_inc(get_sv("XS::APItest::BEGIN_called", GV_ADD|GV_ADDMULTI));
1696
1697 void
1698 CHECK()
1699     CODE:
1700         sv_inc(get_sv("XS::APItest::CHECK_called", GV_ADD|GV_ADDMULTI));
1701
1702 void
1703 UNITCHECK()
1704     CODE:
1705         sv_inc(get_sv("XS::APItest::UNITCHECK_called", GV_ADD|GV_ADDMULTI));
1706
1707 void
1708 INIT()
1709     CODE:
1710         sv_inc(get_sv("XS::APItest::INIT_called", GV_ADD|GV_ADDMULTI));
1711
1712 void
1713 END()
1714     CODE:
1715         sv_inc(get_sv("XS::APItest::END_called", GV_ADD|GV_ADDMULTI));
1716
1717 void
1718 utf16_to_utf8 (sv, ...)
1719     SV* sv
1720         ALIAS:
1721             utf16_to_utf8_reversed = 1
1722     PREINIT:
1723         STRLEN len;
1724         U8 *source;
1725         SV *dest;
1726         I32 got; /* Gah, badly thought out APIs */
1727     CODE:
1728         source = (U8 *)SvPVbyte(sv, len);
1729         /* Optionally only convert part of the buffer.  */      
1730         if (items > 1) {
1731             len = SvUV(ST(1));
1732         }
1733         /* Mortalise this right now, as we'll be testing croak()s  */
1734         dest = sv_2mortal(newSV(len * 3 / 2 + 1));
1735         if (ix) {
1736             utf16_to_utf8_reversed(source, (U8 *)SvPVX(dest), len, &got);
1737         } else {
1738             utf16_to_utf8(source, (U8 *)SvPVX(dest), len, &got);
1739         }
1740         SvCUR_set(dest, got);
1741         SvPVX(dest)[got] = '\0';
1742         SvPOK_on(dest);
1743         ST(0) = dest;
1744         XSRETURN(1);
1745
1746 void
1747 my_exit(int exitcode)
1748         PPCODE:
1749         my_exit(exitcode);
1750
1751 U8
1752 first_byte(sv)
1753         SV *sv
1754    CODE:
1755     char *s;
1756     STRLEN len;
1757         s = SvPVbyte(sv, len);
1758         RETVAL = s[0];
1759    OUTPUT:
1760     RETVAL
1761
1762 I32
1763 sv_count()
1764         CODE:
1765             RETVAL = PL_sv_count;
1766         OUTPUT:
1767             RETVAL
1768
1769 void
1770 bhk_record(bool on)
1771     CODE:
1772         dMY_CXT;
1773         MY_CXT.bhk_record = on;
1774         if (on)
1775             av_clear(MY_CXT.bhkav);
1776
1777 void
1778 test_magic_chain()
1779     PREINIT:
1780         SV *sv;
1781         MAGIC *callmg, *uvarmg;
1782     CODE:
1783         sv = sv_2mortal(newSV(0));
1784         if (SvTYPE(sv) >= SVt_PVMG) croak_fail();
1785         if (SvMAGICAL(sv)) croak_fail();
1786         sv_magic(sv, &PL_sv_yes, PERL_MAGIC_checkcall, (char*)&callmg, 0);
1787         if (SvTYPE(sv) < SVt_PVMG) croak_fail();
1788         if (!SvMAGICAL(sv)) croak_fail();
1789         if (mg_find(sv, PERL_MAGIC_uvar)) croak_fail();
1790         callmg = mg_find(sv, PERL_MAGIC_checkcall);
1791         if (!callmg) croak_fail();
1792         if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
1793             croak_fail();
1794         sv_magic(sv, &PL_sv_no, PERL_MAGIC_uvar, (char*)&uvarmg, 0);
1795         if (SvTYPE(sv) < SVt_PVMG) croak_fail();
1796         if (!SvMAGICAL(sv)) croak_fail();
1797         if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
1798         uvarmg = mg_find(sv, PERL_MAGIC_uvar);
1799         if (!uvarmg) croak_fail();
1800         if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
1801             croak_fail();
1802         if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
1803             croak_fail();
1804         mg_free_type(sv, PERL_MAGIC_vec);
1805         if (SvTYPE(sv) < SVt_PVMG) croak_fail();
1806         if (!SvMAGICAL(sv)) croak_fail();
1807         if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
1808         if (mg_find(sv, PERL_MAGIC_uvar) != uvarmg) croak_fail();
1809         if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
1810             croak_fail();
1811         if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
1812             croak_fail();
1813         mg_free_type(sv, PERL_MAGIC_uvar);
1814         if (SvTYPE(sv) < SVt_PVMG) croak_fail();
1815         if (!SvMAGICAL(sv)) croak_fail();
1816         if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
1817         if (mg_find(sv, PERL_MAGIC_uvar)) croak_fail();
1818         if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
1819             croak_fail();
1820         sv_magic(sv, &PL_sv_no, PERL_MAGIC_uvar, (char*)&uvarmg, 0);
1821         if (SvTYPE(sv) < SVt_PVMG) croak_fail();
1822         if (!SvMAGICAL(sv)) croak_fail();
1823         if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
1824         uvarmg = mg_find(sv, PERL_MAGIC_uvar);
1825         if (!uvarmg) croak_fail();
1826         if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
1827             croak_fail();
1828         if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
1829             croak_fail();
1830         mg_free_type(sv, PERL_MAGIC_checkcall);
1831         if (SvTYPE(sv) < SVt_PVMG) croak_fail();
1832         if (!SvMAGICAL(sv)) croak_fail();
1833         if (mg_find(sv, PERL_MAGIC_uvar) != uvarmg) croak_fail();
1834         if (mg_find(sv, PERL_MAGIC_checkcall)) croak_fail();
1835         if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
1836             croak_fail();
1837         mg_free_type(sv, PERL_MAGIC_uvar);
1838         if (SvMAGICAL(sv)) croak_fail();
1839         if (mg_find(sv, PERL_MAGIC_checkcall)) croak_fail();
1840         if (mg_find(sv, PERL_MAGIC_uvar)) croak_fail();
1841
1842 void
1843 test_op_contextualize()
1844     PREINIT:
1845         OP *o;
1846     CODE:
1847         o = newSVOP(OP_CONST, 0, newSViv(0));
1848         o->op_flags &= ~OPf_WANT;
1849         o = op_contextualize(o, G_SCALAR);
1850         if (o->op_type != OP_CONST ||
1851                 (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR)
1852             croak_fail();
1853         op_free(o);
1854         o = newSVOP(OP_CONST, 0, newSViv(0));
1855         o->op_flags &= ~OPf_WANT;
1856         o = op_contextualize(o, G_ARRAY);
1857         if (o->op_type != OP_CONST ||
1858                 (o->op_flags & OPf_WANT) != OPf_WANT_LIST)
1859             croak_fail();
1860         op_free(o);
1861         o = newSVOP(OP_CONST, 0, newSViv(0));
1862         o->op_flags &= ~OPf_WANT;
1863         o = op_contextualize(o, G_VOID);
1864         if (o->op_type != OP_NULL) croak_fail();
1865         op_free(o);
1866
1867 void
1868 test_rv2cv_op_cv()
1869     PROTOTYPE:
1870     PREINIT:
1871         GV *troc_gv, *wibble_gv;
1872         CV *troc_cv;
1873         OP *o;
1874     CODE:
1875         troc_gv = gv_fetchpv("XS::APItest::test_rv2cv_op_cv", 0, SVt_PVGV);
1876         troc_cv = get_cv("XS::APItest::test_rv2cv_op_cv", 0);
1877         wibble_gv = gv_fetchpv("XS::APItest::wibble", 0, SVt_PVGV);
1878         o = newCVREF(0, newGVOP(OP_GV, 0, troc_gv));
1879         if (rv2cv_op_cv(o, 0) != troc_cv) croak_fail();
1880         if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV) != (CV*)troc_gv)
1881             croak_fail();
1882         o->op_private |= OPpENTERSUB_AMPER;
1883         if (rv2cv_op_cv(o, 0)) croak_fail();
1884         if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
1885         o->op_private &= ~OPpENTERSUB_AMPER;
1886         if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
1887         if (rv2cv_op_cv(o, RV2CVOPCV_MARK_EARLY) != troc_cv) croak_fail();
1888         if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
1889         op_free(o);
1890         o = newSVOP(OP_CONST, 0, newSVpv("XS::APItest::test_rv2cv_op_cv", 0));
1891         o->op_private = OPpCONST_BARE;
1892         o = newCVREF(0, o);
1893         if (rv2cv_op_cv(o, 0) != troc_cv) croak_fail();
1894         if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV) != (CV*)troc_gv)
1895             croak_fail();
1896         o->op_private |= OPpENTERSUB_AMPER;
1897         if (rv2cv_op_cv(o, 0)) croak_fail();
1898         if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
1899         op_free(o);
1900         o = newCVREF(0, newSVOP(OP_CONST, 0, newRV_inc((SV*)troc_cv)));
1901         if (rv2cv_op_cv(o, 0) != troc_cv) croak_fail();
1902         if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV) != (CV*)troc_gv)
1903             croak_fail();
1904         o->op_private |= OPpENTERSUB_AMPER;
1905         if (rv2cv_op_cv(o, 0)) croak_fail();
1906         if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
1907         o->op_private &= ~OPpENTERSUB_AMPER;
1908         if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
1909         if (rv2cv_op_cv(o, RV2CVOPCV_MARK_EARLY) != troc_cv) croak_fail();
1910         if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
1911         op_free(o);
1912         o = newCVREF(0, newUNOP(OP_RAND, 0, newSVOP(OP_CONST, 0, newSViv(0))));
1913         if (rv2cv_op_cv(o, 0)) croak_fail();
1914         if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
1915         o->op_private |= OPpENTERSUB_AMPER;
1916         if (rv2cv_op_cv(o, 0)) croak_fail();
1917         if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
1918         o->op_private &= ~OPpENTERSUB_AMPER;
1919         if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
1920         if (rv2cv_op_cv(o, RV2CVOPCV_MARK_EARLY)) croak_fail();
1921         if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
1922         op_free(o);
1923         o = newUNOP(OP_RAND, 0, newSVOP(OP_CONST, 0, newSViv(0)));
1924         if (rv2cv_op_cv(o, 0)) croak_fail();
1925         if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
1926         op_free(o);
1927
1928 void
1929 test_cv_getset_call_checker()
1930     PREINIT:
1931         CV *troc_cv, *tsh_cv;
1932         Perl_call_checker ckfun;
1933         SV *ckobj;
1934     CODE:
1935 #define check_cc(cv, xckfun, xckobj) \
1936     do { \
1937         cv_get_call_checker((cv), &ckfun, &ckobj); \
1938         if (ckfun != (xckfun)) croak_fail_ne(FPTR2DPTR(void *, ckfun), xckfun); \
1939         if (ckobj != (xckobj)) croak_fail_ne(FPTR2DPTR(void *, ckobj), xckobj); \
1940     } while(0)
1941         troc_cv = get_cv("XS::APItest::test_rv2cv_op_cv", 0);
1942         tsh_cv = get_cv("XS::APItest::test_savehints", 0);
1943         check_cc(troc_cv, Perl_ck_entersub_args_proto_or_list, (SV*)troc_cv);
1944         check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv);
1945         cv_set_call_checker(tsh_cv, Perl_ck_entersub_args_proto_or_list,
1946                                     &PL_sv_yes);
1947         check_cc(troc_cv, Perl_ck_entersub_args_proto_or_list, (SV*)troc_cv);
1948         check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, &PL_sv_yes);
1949         cv_set_call_checker(troc_cv, THX_ck_entersub_args_scalars, &PL_sv_no);
1950         check_cc(troc_cv, THX_ck_entersub_args_scalars, &PL_sv_no);
1951         check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, &PL_sv_yes);
1952         cv_set_call_checker(tsh_cv, Perl_ck_entersub_args_proto_or_list,
1953                                     (SV*)tsh_cv);
1954         check_cc(troc_cv, THX_ck_entersub_args_scalars, &PL_sv_no);
1955         check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv);
1956         cv_set_call_checker(troc_cv, Perl_ck_entersub_args_proto_or_list,
1957                                     (SV*)troc_cv);
1958         check_cc(troc_cv, Perl_ck_entersub_args_proto_or_list, (SV*)troc_cv);
1959         check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv);
1960         if (SvMAGICAL((SV*)troc_cv) || SvMAGIC((SV*)troc_cv)) croak_fail();
1961         if (SvMAGICAL((SV*)tsh_cv) || SvMAGIC((SV*)tsh_cv)) croak_fail();
1962 #undef check_cc
1963
1964 void
1965 cv_set_call_checker_lists(CV *cv)
1966     CODE:
1967         cv_set_call_checker(cv, THX_ck_entersub_args_lists, &PL_sv_undef);
1968
1969 void
1970 cv_set_call_checker_scalars(CV *cv)
1971     CODE:
1972         cv_set_call_checker(cv, THX_ck_entersub_args_scalars, &PL_sv_undef);
1973
1974 void
1975 cv_set_call_checker_proto(CV *cv, SV *proto)
1976     CODE:
1977         if (SvROK(proto))
1978             proto = SvRV(proto);
1979         cv_set_call_checker(cv, Perl_ck_entersub_args_proto, proto);
1980
1981 void
1982 cv_set_call_checker_proto_or_list(CV *cv, SV *proto)
1983     CODE:
1984         if (SvROK(proto))
1985             proto = SvRV(proto);
1986         cv_set_call_checker(cv, Perl_ck_entersub_args_proto_or_list, proto);
1987
1988 void
1989 cv_set_call_checker_multi_sum(CV *cv)
1990     CODE:
1991         cv_set_call_checker(cv, THX_ck_entersub_multi_sum, &PL_sv_undef);
1992
1993 void
1994 test_cophh()
1995     PREINIT:
1996         COPHH *a, *b;
1997     CODE:
1998 #define check_ph(EXPR) \
1999             do { if((EXPR) != &PL_sv_placeholder) croak("fail"); } while(0)
2000 #define check_iv(EXPR, EXPECT) \
2001             do { if(SvIV(EXPR) != (EXPECT)) croak("fail"); } while(0)
2002 #define msvpvs(STR) sv_2mortal(newSVpvs(STR))
2003 #define msviv(VALUE) sv_2mortal(newSViv(VALUE))
2004         a = cophh_new_empty();
2005         check_ph(cophh_fetch_pvn(a, "foo_1", 5, 0, 0));
2006         check_ph(cophh_fetch_pvs(a, "foo_1", 0));
2007         check_ph(cophh_fetch_pv(a, "foo_1", 0, 0));
2008         check_ph(cophh_fetch_sv(a, msvpvs("foo_1"), 0, 0));
2009         a = cophh_store_pvn(a, "foo_1abc", 5, 0, msviv(111), 0);
2010         a = cophh_store_pvs(a, "foo_2", msviv(222), 0);
2011         a = cophh_store_pv(a, "foo_3", 0, msviv(333), 0);
2012         a = cophh_store_sv(a, msvpvs("foo_4"), 0, msviv(444), 0);
2013         check_iv(cophh_fetch_pvn(a, "foo_1xyz", 5, 0, 0), 111);
2014         check_iv(cophh_fetch_pvs(a, "foo_1", 0), 111);
2015         check_iv(cophh_fetch_pv(a, "foo_1", 0, 0), 111);
2016         check_iv(cophh_fetch_sv(a, msvpvs("foo_1"), 0, 0), 111);
2017         check_iv(cophh_fetch_pvs(a, "foo_2", 0), 222);
2018         check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
2019         check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
2020         check_ph(cophh_fetch_pvs(a, "foo_5", 0));
2021         b = cophh_copy(a);
2022         b = cophh_store_pvs(b, "foo_1", msviv(1111), 0);
2023         check_iv(cophh_fetch_pvs(a, "foo_1", 0), 111);
2024         check_iv(cophh_fetch_pvs(a, "foo_2", 0), 222);
2025         check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
2026         check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
2027         check_ph(cophh_fetch_pvs(a, "foo_5", 0));
2028         check_iv(cophh_fetch_pvs(b, "foo_1", 0), 1111);
2029         check_iv(cophh_fetch_pvs(b, "foo_2", 0), 222);
2030         check_iv(cophh_fetch_pvs(b, "foo_3", 0), 333);
2031         check_iv(cophh_fetch_pvs(b, "foo_4", 0), 444);
2032         check_ph(cophh_fetch_pvs(b, "foo_5", 0));
2033         a = cophh_delete_pvn(a, "foo_1abc", 5, 0, 0);
2034         a = cophh_delete_pvs(a, "foo_2", 0);
2035         b = cophh_delete_pv(b, "foo_3", 0, 0);
2036         b = cophh_delete_sv(b, msvpvs("foo_4"), 0, 0);
2037         check_ph(cophh_fetch_pvs(a, "foo_1", 0));
2038         check_ph(cophh_fetch_pvs(a, "foo_2", 0));
2039         check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
2040         check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
2041         check_ph(cophh_fetch_pvs(a, "foo_5", 0));
2042         check_iv(cophh_fetch_pvs(b, "foo_1", 0), 1111);
2043         check_iv(cophh_fetch_pvs(b, "foo_2", 0), 222);
2044         check_ph(cophh_fetch_pvs(b, "foo_3", 0));
2045         check_ph(cophh_fetch_pvs(b, "foo_4", 0));
2046         check_ph(cophh_fetch_pvs(b, "foo_5", 0));
2047         b = cophh_delete_pvs(b, "foo_3", 0);
2048         b = cophh_delete_pvs(b, "foo_5", 0);
2049         check_iv(cophh_fetch_pvs(b, "foo_1", 0), 1111);
2050         check_iv(cophh_fetch_pvs(b, "foo_2", 0), 222);
2051         check_ph(cophh_fetch_pvs(b, "foo_3", 0));
2052         check_ph(cophh_fetch_pvs(b, "foo_4", 0));
2053         check_ph(cophh_fetch_pvs(b, "foo_5", 0));
2054         cophh_free(b);
2055         check_ph(cophh_fetch_pvs(a, "foo_1", 0));
2056         check_ph(cophh_fetch_pvs(a, "foo_2", 0));
2057         check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
2058         check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
2059         check_ph(cophh_fetch_pvs(a, "foo_5", 0));
2060         a = cophh_store_pvs(a, "foo_1", msviv(11111), COPHH_KEY_UTF8);
2061         a = cophh_store_pvs(a, "foo_\xaa", msviv(123), 0);
2062         a = cophh_store_pvs(a, "foo_\xc2\xbb", msviv(456), COPHH_KEY_UTF8);
2063         a = cophh_store_pvs(a, "foo_\xc3\x8c", msviv(789), COPHH_KEY_UTF8);
2064         a = cophh_store_pvs(a, "foo_\xd9\xa6", msviv(666), COPHH_KEY_UTF8);
2065         check_iv(cophh_fetch_pvs(a, "foo_1", 0), 11111);
2066         check_iv(cophh_fetch_pvs(a, "foo_1", COPHH_KEY_UTF8), 11111);
2067         check_iv(cophh_fetch_pvs(a, "foo_\xaa", 0), 123);
2068         check_iv(cophh_fetch_pvs(a, "foo_\xc2\xaa", COPHH_KEY_UTF8), 123);
2069         check_ph(cophh_fetch_pvs(a, "foo_\xc2\xaa", 0));
2070         check_iv(cophh_fetch_pvs(a, "foo_\xbb", 0), 456);
2071         check_iv(cophh_fetch_pvs(a, "foo_\xc2\xbb", COPHH_KEY_UTF8), 456);
2072         check_ph(cophh_fetch_pvs(a, "foo_\xc2\xbb", 0));
2073         check_iv(cophh_fetch_pvs(a, "foo_\xcc", 0), 789);
2074         check_iv(cophh_fetch_pvs(a, "foo_\xc3\x8c", COPHH_KEY_UTF8), 789);
2075         check_ph(cophh_fetch_pvs(a, "foo_\xc2\x8c", 0));
2076         check_iv(cophh_fetch_pvs(a, "foo_\xd9\xa6", COPHH_KEY_UTF8), 666);
2077         check_ph(cophh_fetch_pvs(a, "foo_\xd9\xa6", 0));
2078         ENTER;
2079         SAVEFREECOPHH(a);
2080         LEAVE;
2081 #undef check_ph
2082 #undef check_iv
2083 #undef msvpvs
2084 #undef msviv
2085
2086 HV *
2087 example_cophh_2hv()
2088     PREINIT:
2089         COPHH *a;
2090     CODE:
2091 #define msviv(VALUE) sv_2mortal(newSViv(VALUE))
2092         a = cophh_new_empty();
2093         a = cophh_store_pvs(a, "foo_0", msviv(999), 0);
2094         a = cophh_store_pvs(a, "foo_1", msviv(111), 0);
2095         a = cophh_store_pvs(a, "foo_\xaa", msviv(123), 0);
2096         a = cophh_store_pvs(a, "foo_\xc2\xbb", msviv(456), COPHH_KEY_UTF8);
2097         a = cophh_store_pvs(a, "foo_\xc3\x8c", msviv(789), COPHH_KEY_UTF8);
2098         a = cophh_store_pvs(a, "foo_\xd9\xa6", msviv(666), COPHH_KEY_UTF8);
2099         a = cophh_delete_pvs(a, "foo_0", 0);
2100         a = cophh_delete_pvs(a, "foo_2", 0);
2101         RETVAL = cophh_2hv(a, 0);
2102         cophh_free(a);
2103 #undef msviv
2104     OUTPUT:
2105         RETVAL
2106
2107 void
2108 test_savehints()
2109     PREINIT:
2110         SV **svp, *sv;
2111     CODE:
2112 #define store_hint(KEY, VALUE) \
2113                 sv_setiv_mg(*hv_fetchs(GvHV(PL_hintgv), KEY, 1), (VALUE))
2114 #define hint_ok(KEY, EXPECT) \
2115                 ((svp = hv_fetchs(GvHV(PL_hintgv), KEY, 0)) && \
2116                     (sv = *svp) && SvIV(sv) == (EXPECT) && \
2117                     (sv = cop_hints_fetch_pvs(&PL_compiling, KEY, 0)) && \
2118                     SvIV(sv) == (EXPECT))
2119 #define check_hint(KEY, EXPECT) \
2120                 do { if (!hint_ok(KEY, EXPECT)) croak_fail(); } while(0)
2121         PL_hints |= HINT_LOCALIZE_HH;
2122         ENTER;
2123         SAVEHINTS();
2124         PL_hints &= HINT_INTEGER;
2125         store_hint("t0", 123);
2126         store_hint("t1", 456);
2127         if (PL_hints & HINT_INTEGER) croak_fail();
2128         check_hint("t0", 123); check_hint("t1", 456);
2129         ENTER;
2130         SAVEHINTS();
2131         if (PL_hints & HINT_INTEGER) croak_fail();
2132         check_hint("t0", 123); check_hint("t1", 456);
2133         PL_hints |= HINT_INTEGER;
2134         store_hint("t0", 321);
2135         if (!(PL_hints & HINT_INTEGER)) croak_fail();
2136         check_hint("t0", 321); check_hint("t1", 456);
2137         LEAVE;
2138         if (PL_hints & HINT_INTEGER) croak_fail();
2139         check_hint("t0", 123); check_hint("t1", 456);
2140         ENTER;
2141         SAVEHINTS();
2142         if (PL_hints & HINT_INTEGER) croak_fail();
2143         check_hint("t0", 123); check_hint("t1", 456);
2144         store_hint("t1", 654);
2145         if (PL_hints & HINT_INTEGER) croak_fail();
2146         check_hint("t0", 123); check_hint("t1", 654);
2147         LEAVE;
2148         if (PL_hints & HINT_INTEGER) croak_fail();
2149         check_hint("t0", 123); check_hint("t1", 456);
2150         LEAVE;
2151 #undef store_hint
2152 #undef hint_ok
2153 #undef check_hint
2154
2155 void
2156 test_copyhints()
2157     PREINIT:
2158         HV *a, *b;
2159     CODE:
2160         PL_hints |= HINT_LOCALIZE_HH;
2161         ENTER;
2162         SAVEHINTS();
2163         sv_setiv_mg(*hv_fetchs(GvHV(PL_hintgv), "t0", 1), 123);
2164         if (SvIV(cop_hints_fetch_pvs(&PL_compiling, "t0", 0)) != 123)
2165             croak_fail();
2166         a = newHVhv(GvHV(PL_hintgv));
2167         sv_2mortal((SV*)a);
2168         sv_setiv_mg(*hv_fetchs(a, "t0", 1), 456);
2169         if (SvIV(cop_hints_fetch_pvs(&PL_compiling, "t0", 0)) != 123)
2170             croak_fail();
2171         b = hv_copy_hints_hv(a);
2172         sv_2mortal((SV*)b);
2173         sv_setiv_mg(*hv_fetchs(b, "t0", 1), 789);
2174         if (SvIV(cop_hints_fetch_pvs(&PL_compiling, "t0", 0)) != 789)
2175             croak_fail();
2176         LEAVE;
2177
2178 void
2179 test_op_list()
2180     PREINIT:
2181         OP *a;
2182     CODE:
2183 #define iv_op(iv) newSVOP(OP_CONST, 0, newSViv(iv))
2184 #define check_op(o, expect) \
2185     do { \
2186         if (strcmp(test_op_list_describe(o), (expect))) \
2187             croak("fail %s %s", test_op_list_describe(o), (expect)); \
2188     } while(0)
2189         a = op_append_elem(OP_LIST, NULL, NULL);
2190         check_op(a, "");
2191         a = op_append_elem(OP_LIST, iv_op(1), a);
2192         check_op(a, "const(1).");
2193         a = op_append_elem(OP_LIST, NULL, a);
2194         check_op(a, "const(1).");
2195         a = op_append_elem(OP_LIST, a, iv_op(2));
2196         check_op(a, "list[pushmark.const(1).const(2).]");
2197         a = op_append_elem(OP_LIST, a, iv_op(3));
2198         check_op(a, "list[pushmark.const(1).const(2).const(3).]");
2199         a = op_append_elem(OP_LIST, a, NULL);
2200         check_op(a, "list[pushmark.const(1).const(2).const(3).]");
2201         a = op_append_elem(OP_LIST, NULL, a);
2202         check_op(a, "list[pushmark.const(1).const(2).const(3).]");
2203         a = op_append_elem(OP_LIST, iv_op(4), a);
2204         check_op(a, "list[pushmark.const(4)."
2205                 "list[pushmark.const(1).const(2).const(3).]]");
2206         a = op_append_elem(OP_LIST, a, iv_op(5));
2207         check_op(a, "list[pushmark.const(4)."
2208                 "list[pushmark.const(1).const(2).const(3).]const(5).]");
2209         a = op_append_elem(OP_LIST, a, 
2210                 op_append_elem(OP_LIST, iv_op(7), iv_op(6)));
2211         check_op(a, "list[pushmark.const(4)."
2212                 "list[pushmark.const(1).const(2).const(3).]const(5)."
2213                 "list[pushmark.const(7).const(6).]]");
2214         op_free(a);
2215         a = op_append_elem(OP_LINESEQ, iv_op(1), iv_op(2));
2216         check_op(a, "lineseq[const(1).const(2).]");
2217         a = op_append_elem(OP_LINESEQ, a, iv_op(3));
2218         check_op(a, "lineseq[const(1).const(2).const(3).]");
2219         op_free(a);
2220         a = op_append_elem(OP_LINESEQ,
2221                 op_append_elem(OP_LIST, iv_op(1), iv_op(2)),
2222                 iv_op(3));
2223         check_op(a, "lineseq[list[pushmark.const(1).const(2).]const(3).]");
2224         op_free(a);
2225         a = op_prepend_elem(OP_LIST, NULL, NULL);
2226         check_op(a, "");
2227         a = op_prepend_elem(OP_LIST, a, iv_op(1));
2228         check_op(a, "const(1).");
2229         a = op_prepend_elem(OP_LIST, a, NULL);
2230         check_op(a, "const(1).");
2231         a = op_prepend_elem(OP_LIST, iv_op(2), a);
2232         check_op(a, "list[pushmark.const(2).const(1).]");
2233         a = op_prepend_elem(OP_LIST, iv_op(3), a);
2234         check_op(a, "list[pushmark.const(3).const(2).const(1).]");
2235         a = op_prepend_elem(OP_LIST, NULL, a);
2236         check_op(a, "list[pushmark.const(3).const(2).const(1).]");
2237         a = op_prepend_elem(OP_LIST, a, NULL);
2238         check_op(a, "list[pushmark.const(3).const(2).const(1).]");
2239         a = op_prepend_elem(OP_LIST, a, iv_op(4));
2240         check_op(a, "list[pushmark."
2241                 "list[pushmark.const(3).const(2).const(1).]const(4).]");
2242         a = op_prepend_elem(OP_LIST, iv_op(5), a);
2243         check_op(a, "list[pushmark.const(5)."
2244                 "list[pushmark.const(3).const(2).const(1).]const(4).]");
2245         a = op_prepend_elem(OP_LIST,
2246                 op_prepend_elem(OP_LIST, iv_op(6), iv_op(7)), a);
2247         check_op(a, "list[pushmark.list[pushmark.const(6).const(7).]const(5)."
2248                 "list[pushmark.const(3).const(2).const(1).]const(4).]");
2249         op_free(a);
2250         a = op_prepend_elem(OP_LINESEQ, iv_op(2), iv_op(1));
2251         check_op(a, "lineseq[const(2).const(1).]");
2252         a = op_prepend_elem(OP_LINESEQ, iv_op(3), a);
2253         check_op(a, "lineseq[const(3).const(2).const(1).]");
2254         op_free(a);
2255         a = op_prepend_elem(OP_LINESEQ, iv_op(3),
2256                 op_prepend_elem(OP_LIST, iv_op(2), iv_op(1)));
2257         check_op(a, "lineseq[const(3).list[pushmark.const(2).const(1).]]");
2258         op_free(a);
2259         a = op_append_list(OP_LINESEQ, NULL, NULL);
2260         check_op(a, "");
2261         a = op_append_list(OP_LINESEQ, iv_op(1), a);
2262         check_op(a, "const(1).");
2263         a = op_append_list(OP_LINESEQ, NULL, a);
2264         check_op(a, "const(1).");
2265         a = op_append_list(OP_LINESEQ, a, iv_op(2));
2266         check_op(a, "lineseq[const(1).const(2).]");
2267         a = op_append_list(OP_LINESEQ, a, iv_op(3));
2268         check_op(a, "lineseq[const(1).const(2).const(3).]");
2269         a = op_append_list(OP_LINESEQ, iv_op(4), a);
2270         check_op(a, "lineseq[const(4).const(1).const(2).const(3).]");
2271         a = op_append_list(OP_LINESEQ, a, NULL);
2272         check_op(a, "lineseq[const(4).const(1).const(2).const(3).]");
2273         a = op_append_list(OP_LINESEQ, NULL, a);
2274         check_op(a, "lineseq[const(4).const(1).const(2).const(3).]");
2275         a = op_append_list(OP_LINESEQ, a,
2276                 op_append_list(OP_LINESEQ, iv_op(5), iv_op(6)));
2277         check_op(a, "lineseq[const(4).const(1).const(2).const(3)."
2278                 "const(5).const(6).]");
2279         op_free(a);
2280         a = op_append_list(OP_LINESEQ,
2281                 op_append_list(OP_LINESEQ, iv_op(1), iv_op(2)),
2282                 op_append_list(OP_LIST, iv_op(3), iv_op(4)));
2283         check_op(a, "lineseq[const(1).const(2)."
2284                 "list[pushmark.const(3).const(4).]]");
2285         op_free(a);
2286         a = op_append_list(OP_LINESEQ,
2287                 op_append_list(OP_LIST, iv_op(1), iv_op(2)),
2288                 op_append_list(OP_LINESEQ, iv_op(3), iv_op(4)));
2289         check_op(a, "lineseq[list[pushmark.const(1).const(2).]"
2290                 "const(3).const(4).]");
2291         op_free(a);
2292 #undef check_op
2293
2294 void
2295 test_op_linklist ()
2296     PREINIT:
2297         OP *o;
2298     CODE:
2299 #define check_ll(o, expect) \
2300     STMT_START { \
2301         if (strNE(test_op_linklist_describe(o), (expect))) \
2302             croak("fail %s %s", test_op_linklist_describe(o), (expect)); \
2303     } STMT_END
2304         o = iv_op(1);
2305         check_ll(o, ".const1");
2306         op_free(o);
2307
2308         o = mkUNOP(OP_NOT, iv_op(1));
2309         check_ll(o, ".const1.not");
2310         op_free(o);
2311
2312         o = mkUNOP(OP_NOT, mkUNOP(OP_NEGATE, iv_op(1)));
2313         check_ll(o, ".const1.negate.not");
2314         op_free(o);
2315
2316         o = mkBINOP(OP_ADD, iv_op(1), iv_op(2));
2317         check_ll(o, ".const1.const2.add");
2318         op_free(o);
2319
2320         o = mkBINOP(OP_ADD, mkUNOP(OP_NOT, iv_op(1)), iv_op(2));
2321         check_ll(o, ".const1.not.const2.add");
2322         op_free(o);
2323
2324         o = mkUNOP(OP_NOT, mkBINOP(OP_ADD, iv_op(1), iv_op(2)));
2325         check_ll(o, ".const1.const2.add.not");
2326         op_free(o);
2327
2328         o = mkLISTOP(OP_LINESEQ, iv_op(1), iv_op(2), iv_op(3));
2329         check_ll(o, ".const1.const2.const3.lineseq");
2330         op_free(o);
2331
2332         o = mkLISTOP(OP_LINESEQ,
2333                 mkBINOP(OP_ADD, iv_op(1), iv_op(2)),
2334                 mkUNOP(OP_NOT, iv_op(3)),
2335                 mkLISTOP(OP_SUBSTR, iv_op(4), iv_op(5), iv_op(6)));
2336         check_ll(o, ".const1.const2.add.const3.not"
2337                     ".const4.const5.const6.substr.lineseq");
2338         op_free(o);
2339
2340         o = mkBINOP(OP_ADD, iv_op(1), iv_op(2));
2341         LINKLIST(o);
2342         o = mkBINOP(OP_SUBTRACT, o, iv_op(3));
2343         check_ll(o, ".const1.const2.add.const3.subtract");
2344         op_free(o);
2345 #undef check_ll
2346 #undef iv_op
2347
2348 void
2349 peep_enable ()
2350     PREINIT:
2351         dMY_CXT;
2352     CODE:
2353         av_clear(MY_CXT.peep_recorder);
2354         av_clear(MY_CXT.rpeep_recorder);
2355         MY_CXT.peep_recording = 1;
2356
2357 void
2358 peep_disable ()
2359     PREINIT:
2360         dMY_CXT;
2361     CODE:
2362         MY_CXT.peep_recording = 0;
2363
2364 SV *
2365 peep_record ()
2366     PREINIT:
2367         dMY_CXT;
2368     CODE:
2369         RETVAL = newRV_inc((SV *)MY_CXT.peep_recorder);
2370     OUTPUT:
2371         RETVAL
2372
2373 SV *
2374 rpeep_record ()
2375     PREINIT:
2376         dMY_CXT;
2377     CODE:
2378         RETVAL = newRV_inc((SV *)MY_CXT.rpeep_recorder);
2379     OUTPUT:
2380         RETVAL
2381
2382 =pod
2383
2384 multicall_each: call a sub for each item in the list. Used to test MULTICALL
2385
2386 =cut
2387
2388 void
2389 multicall_each(block,...)
2390     SV * block
2391 PROTOTYPE: &@
2392 CODE:
2393 {
2394     dMULTICALL;
2395     int index;
2396     GV *gv;
2397     HV *stash;
2398     I32 gimme = G_SCALAR;
2399     SV **args = &PL_stack_base[ax];
2400     CV *cv;
2401
2402     if(items <= 1) {
2403         XSRETURN_UNDEF;
2404     }
2405     cv = sv_2cv(block, &stash, &gv, 0);
2406     if (cv == Nullcv) {
2407        croak("multicall_each: not a subroutine reference");
2408     }
2409     PUSH_MULTICALL(cv);
2410     SAVESPTR(GvSV(PL_defgv));
2411
2412     for(index = 1 ; index < items ; index++) {
2413         GvSV(PL_defgv) = args[index];
2414         MULTICALL;
2415     }
2416     POP_MULTICALL;
2417     XSRETURN_UNDEF;
2418 }
2419
2420
2421 BOOT:
2422         {
2423         HV* stash;
2424         SV** meth = NULL;
2425         CV* cv;
2426         stash = gv_stashpv("XS::APItest::TempLv", 0);
2427         if (stash)
2428             meth = hv_fetchs(stash, "make_temp_mg_lv", 0);
2429         if (!meth)
2430             croak("lost method 'make_temp_mg_lv'");
2431         cv = GvCV(*meth);
2432         CvLVALUE_on(cv);
2433         }
2434
2435 BOOT:
2436 {
2437     hintkey_rpn_sv = newSVpvs_share("XS::APItest/rpn");
2438     hintkey_calcrpn_sv = newSVpvs_share("XS::APItest/calcrpn");
2439     hintkey_stufftest_sv = newSVpvs_share("XS::APItest/stufftest");
2440     hintkey_swaptwostmts_sv = newSVpvs_share("XS::APItest/swaptwostmts");
2441     hintkey_looprest_sv = newSVpvs_share("XS::APItest/looprest");
2442     hintkey_scopelessblock_sv = newSVpvs_share("XS::APItest/scopelessblock");
2443     hintkey_stmtasexpr_sv = newSVpvs_share("XS::APItest/stmtasexpr");
2444     hintkey_stmtsasexpr_sv = newSVpvs_share("XS::APItest/stmtsasexpr");
2445     hintkey_loopblock_sv = newSVpvs_share("XS::APItest/loopblock");
2446     hintkey_blockasexpr_sv = newSVpvs_share("XS::APItest/blockasexpr");
2447     hintkey_swaplabel_sv = newSVpvs_share("XS::APItest/swaplabel");
2448     hintkey_labelconst_sv = newSVpvs_share("XS::APItest/labelconst");
2449     next_keyword_plugin = PL_keyword_plugin;
2450     PL_keyword_plugin = my_keyword_plugin;
2451 }
2452
2453 void
2454 establish_cleanup(...)
2455 PROTOTYPE: $
2456 CODE:
2457     croak("establish_cleanup called as a function");
2458
2459 BOOT:
2460 {
2461     CV *estcv = get_cv("XS::APItest::establish_cleanup", 0);
2462     cv_set_call_checker(estcv, THX_ck_entersub_establish_cleanup, (SV*)estcv);
2463 }
2464
2465 void
2466 postinc(...)
2467 PROTOTYPE: $
2468 CODE:
2469     croak("postinc called as a function");
2470
2471 BOOT:
2472 {
2473     CV *asscv = get_cv("XS::APItest::postinc", 0);
2474     cv_set_call_checker(asscv, THX_ck_entersub_postinc, (SV*)asscv);
2475 }