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