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