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