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