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