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