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