This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
gv.c: Added gv_init_(sv|pv|pvn), renamed gv_init_sv as gv_init_svtype.
[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
b7b1e41b 469/* the real new*OP functions have a tendency to call fold_constants, and
5983a79d
BM
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
15103811
Z
590STATIC OP *
591THX_ck_entersub_pad_scalar(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
592{
593 OP *pushop, *argop;
594 PADOFFSET padoff = NOT_IN_PAD;
595 SV *a0, *a1;
596 ck_entersub_args_proto(entersubop, namegv, ckobj);
597 pushop = cUNOPx(entersubop)->op_first;
598 if(!pushop->op_sibling) pushop = cUNOPx(pushop)->op_first;
599 argop = pushop->op_sibling;
600 if(argop->op_type != OP_CONST || argop->op_sibling->op_type != OP_CONST)
601 croak("bad argument expression type for pad_scalar()");
602 a0 = cSVOPx_sv(argop);
603 a1 = cSVOPx_sv(argop->op_sibling);
604 switch(SvIV(a0)) {
605 case 1: {
606 SV *namesv = sv_2mortal(newSVpvs("$"));
607 sv_catsv(namesv, a1);
608 padoff = pad_findmy_sv(namesv, 0);
609 } break;
610 case 2: {
611 char *namepv;
612 STRLEN namelen;
613 SV *namesv = sv_2mortal(newSVpvs("$"));
614 sv_catsv(namesv, a1);
615 namepv = SvPV(namesv, namelen);
ab8f91e9 616 padoff = pad_findmy_pvn(namepv, namelen, SvUTF8(namesv));
15103811
Z
617 } break;
618 case 3: {
619 char *namepv;
620 SV *namesv = sv_2mortal(newSVpvs("$"));
621 sv_catsv(namesv, a1);
622 namepv = SvPV_nolen(namesv);
ab8f91e9 623 padoff = pad_findmy_pv(namepv, SvUTF8(namesv));
15103811
Z
624 } break;
625 case 4: {
626 padoff = pad_findmy_pvs("$foo", 0);
627 } break;
628 default: croak("bad type value for pad_scalar()");
629 }
630 op_free(entersubop);
631 if(padoff == NOT_IN_PAD) {
632 return newSVOP(OP_CONST, 0, newSVpvs("NOT_IN_PAD"));
633 } else if(PAD_COMPNAME_FLAGS_isOUR(padoff)) {
634 return newSVOP(OP_CONST, 0, newSVpvs("NOT_MY"));
635 } else {
636 OP *padop = newOP(OP_PADSV, 0);
637 padop->op_targ = padoff;
638 return padop;
639 }
640}
641
83f8bb56
Z
642/** RPN keyword parser **/
643
644#define sv_is_glob(sv) (SvTYPE(sv) == SVt_PVGV)
645#define sv_is_regexp(sv) (SvTYPE(sv) == SVt_REGEXP)
646#define sv_is_string(sv) \
647 (!sv_is_glob(sv) && !sv_is_regexp(sv) && \
648 (SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK|SVp_IOK|SVp_NOK|SVp_POK)))
649
650static SV *hintkey_rpn_sv, *hintkey_calcrpn_sv, *hintkey_stufftest_sv;
07ffcb73 651static SV *hintkey_swaptwostmts_sv, *hintkey_looprest_sv;
a7aaec61 652static SV *hintkey_scopelessblock_sv;
e53d8f76
Z
653static SV *hintkey_stmtasexpr_sv, *hintkey_stmtsasexpr_sv;
654static SV *hintkey_loopblock_sv, *hintkey_blockasexpr_sv;
361d9b55 655static SV *hintkey_swaplabel_sv, *hintkey_labelconst_sv;
78cdf107
Z
656static SV *hintkey_arrayfullexpr_sv, *hintkey_arraylistexpr_sv;
657static SV *hintkey_arraytermexpr_sv, *hintkey_arrayarithexpr_sv;
658static SV *hintkey_arrayexprflags_sv;
83f8bb56
Z
659static int (*next_keyword_plugin)(pTHX_ char *, STRLEN, OP **);
660
661/* low-level parser helpers */
662
663#define PL_bufptr (PL_parser->bufptr)
664#define PL_bufend (PL_parser->bufend)
665
666/* RPN parser */
667
668#define parse_var() THX_parse_var(aTHX)
669static OP *THX_parse_var(pTHX)
670{
671 char *s = PL_bufptr;
672 char *start = s;
673 PADOFFSET varpos;
674 OP *padop;
675 if(*s != '$') croak("RPN syntax error");
676 while(1) {
677 char c = *++s;
678 if(!isALNUM(c)) break;
679 }
680 if(s-start < 2) croak("RPN syntax error");
681 lex_read_to(s);
cc76b5cc 682 varpos = pad_findmy_pvn(start, s-start, 0);
83f8bb56
Z
683 if(varpos == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(varpos))
684 croak("RPN only supports \"my\" variables");
685 padop = newOP(OP_PADSV, 0);
686 padop->op_targ = varpos;
687 return padop;
688}
689
690#define push_rpn_item(o) \
691 (tmpop = (o), tmpop->op_sibling = stack, stack = tmpop)
692#define pop_rpn_item() \
693 (!stack ? (croak("RPN stack underflow"), (OP*)NULL) : \
694 (tmpop = stack, stack = stack->op_sibling, \
695 tmpop->op_sibling = NULL, tmpop))
696
697#define parse_rpn_expr() THX_parse_rpn_expr(aTHX)
698static OP *THX_parse_rpn_expr(pTHX)
699{
700 OP *stack = NULL, *tmpop;
701 while(1) {
702 I32 c;
703 lex_read_space(0);
704 c = lex_peek_unichar(0);
705 switch(c) {
706 case /*(*/')': case /*{*/'}': {
707 OP *result = pop_rpn_item();
708 if(stack) croak("RPN expression must return a single value");
709 return result;
710 } break;
711 case '0': case '1': case '2': case '3': case '4':
712 case '5': case '6': case '7': case '8': case '9': {
713 UV val = 0;
714 do {
715 lex_read_unichar(0);
716 val = 10*val + (c - '0');
717 c = lex_peek_unichar(0);
718 } while(c >= '0' && c <= '9');
719 push_rpn_item(newSVOP(OP_CONST, 0, newSVuv(val)));
720 } break;
721 case '$': {
722 push_rpn_item(parse_var());
723 } break;
724 case '+': {
725 OP *b = pop_rpn_item();
726 OP *a = pop_rpn_item();
727 lex_read_unichar(0);
728 push_rpn_item(newBINOP(OP_I_ADD, 0, a, b));
729 } break;
730 case '-': {
731 OP *b = pop_rpn_item();
732 OP *a = pop_rpn_item();
733 lex_read_unichar(0);
734 push_rpn_item(newBINOP(OP_I_SUBTRACT, 0, a, b));
735 } break;
736 case '*': {
737 OP *b = pop_rpn_item();
738 OP *a = pop_rpn_item();
739 lex_read_unichar(0);
740 push_rpn_item(newBINOP(OP_I_MULTIPLY, 0, a, b));
741 } break;
742 case '/': {
743 OP *b = pop_rpn_item();
744 OP *a = pop_rpn_item();
745 lex_read_unichar(0);
746 push_rpn_item(newBINOP(OP_I_DIVIDE, 0, a, b));
747 } break;
748 case '%': {
749 OP *b = pop_rpn_item();
750 OP *a = pop_rpn_item();
751 lex_read_unichar(0);
752 push_rpn_item(newBINOP(OP_I_MODULO, 0, a, b));
753 } break;
754 default: {
755 croak("RPN syntax error");
756 } break;
757 }
758 }
759}
760
761#define parse_keyword_rpn() THX_parse_keyword_rpn(aTHX)
762static OP *THX_parse_keyword_rpn(pTHX)
763{
764 OP *op;
765 lex_read_space(0);
766 if(lex_peek_unichar(0) != '('/*)*/)
767 croak("RPN expression must be parenthesised");
768 lex_read_unichar(0);
769 op = parse_rpn_expr();
770 if(lex_peek_unichar(0) != /*(*/')')
771 croak("RPN expression must be parenthesised");
772 lex_read_unichar(0);
773 return op;
774}
775
776#define parse_keyword_calcrpn() THX_parse_keyword_calcrpn(aTHX)
777static OP *THX_parse_keyword_calcrpn(pTHX)
778{
779 OP *varop, *exprop;
780 lex_read_space(0);
781 varop = parse_var();
782 lex_read_space(0);
783 if(lex_peek_unichar(0) != '{'/*}*/)
784 croak("RPN expression must be braced");
785 lex_read_unichar(0);
786 exprop = parse_rpn_expr();
787 if(lex_peek_unichar(0) != /*{*/'}')
788 croak("RPN expression must be braced");
789 lex_read_unichar(0);
790 return newASSIGNOP(OPf_STACKED, varop, 0, exprop);
791}
792
793#define parse_keyword_stufftest() THX_parse_keyword_stufftest(aTHX)
794static OP *THX_parse_keyword_stufftest(pTHX)
795{
796 I32 c;
797 bool do_stuff;
798 lex_read_space(0);
799 do_stuff = lex_peek_unichar(0) == '+';
800 if(do_stuff) {
801 lex_read_unichar(0);
802 lex_read_space(0);
803 }
804 c = lex_peek_unichar(0);
805 if(c == ';') {
806 lex_read_unichar(0);
807 } else if(c != /*{*/'}') {
808 croak("syntax error");
809 }
810 if(do_stuff) lex_stuff_pvs(" ", 0);
811 return newOP(OP_NULL, 0);
812}
813
814#define parse_keyword_swaptwostmts() THX_parse_keyword_swaptwostmts(aTHX)
815static OP *THX_parse_keyword_swaptwostmts(pTHX)
816{
817 OP *a, *b;
818 a = parse_fullstmt(0);
819 b = parse_fullstmt(0);
820 if(a && b)
821 PL_hints |= HINT_BLOCK_SCOPE;
2fcb4757 822 return op_append_list(OP_LINESEQ, b, a);
83f8bb56
Z
823}
824
07ffcb73
Z
825#define parse_keyword_looprest() THX_parse_keyword_looprest(aTHX)
826static OP *THX_parse_keyword_looprest(pTHX)
827{
94bf0465
Z
828 return newWHILEOP(0, 1, NULL, newSVOP(OP_CONST, 0, &PL_sv_yes),
829 parse_stmtseq(0), NULL, 1);
07ffcb73
Z
830}
831
a7aaec61
Z
832#define parse_keyword_scopelessblock() THX_parse_keyword_scopelessblock(aTHX)
833static OP *THX_parse_keyword_scopelessblock(pTHX)
834{
835 I32 c;
836 OP *body;
837 lex_read_space(0);
838 if(lex_peek_unichar(0) != '{'/*}*/) croak("syntax error");
839 lex_read_unichar(0);
840 body = parse_stmtseq(0);
841 c = lex_peek_unichar(0);
842 if(c != /*{*/'}' && c != /*[*/']' && c != /*(*/')') croak("syntax error");
843 lex_read_unichar(0);
844 return body;
845}
846
9eb5c532
Z
847#define parse_keyword_stmtasexpr() THX_parse_keyword_stmtasexpr(aTHX)
848static OP *THX_parse_keyword_stmtasexpr(pTHX)
849{
8359b381 850 OP *o = parse_barestmt(0);
3ad73efd
Z
851 if (!o) o = newOP(OP_STUB, 0);
852 if (PL_hints & HINT_BLOCK_SCOPE) o->op_flags |= OPf_PARENS;
853 return op_scope(o);
9eb5c532
Z
854}
855
856#define parse_keyword_stmtsasexpr() THX_parse_keyword_stmtsasexpr(aTHX)
857static OP *THX_parse_keyword_stmtsasexpr(pTHX)
858{
859 OP *o;
860 lex_read_space(0);
861 if(lex_peek_unichar(0) != '{'/*}*/) croak("syntax error");
862 lex_read_unichar(0);
863 o = parse_stmtseq(0);
864 lex_read_space(0);
865 if(lex_peek_unichar(0) != /*{*/'}') croak("syntax error");
866 lex_read_unichar(0);
3ad73efd
Z
867 if (!o) o = newOP(OP_STUB, 0);
868 if (PL_hints & HINT_BLOCK_SCOPE) o->op_flags |= OPf_PARENS;
869 return op_scope(o);
9eb5c532
Z
870}
871
e53d8f76
Z
872#define parse_keyword_loopblock() THX_parse_keyword_loopblock(aTHX)
873static OP *THX_parse_keyword_loopblock(pTHX)
874{
94bf0465
Z
875 return newWHILEOP(0, 1, NULL, newSVOP(OP_CONST, 0, &PL_sv_yes),
876 parse_block(0), NULL, 1);
e53d8f76
Z
877}
878
879#define parse_keyword_blockasexpr() THX_parse_keyword_blockasexpr(aTHX)
880static OP *THX_parse_keyword_blockasexpr(pTHX)
881{
882 OP *o = parse_block(0);
3ad73efd
Z
883 if (!o) o = newOP(OP_STUB, 0);
884 if (PL_hints & HINT_BLOCK_SCOPE) o->op_flags |= OPf_PARENS;
885 return op_scope(o);
e53d8f76
Z
886}
887
361d9b55
Z
888#define parse_keyword_swaplabel() THX_parse_keyword_swaplabel(aTHX)
889static OP *THX_parse_keyword_swaplabel(pTHX)
890{
891 OP *sop = parse_barestmt(0);
892 SV *label = parse_label(PARSE_OPTIONAL);
893 if (label) sv_2mortal(label);
894 return newSTATEOP(0, label ? savepv(SvPVX(label)) : NULL, sop);
895}
896
897#define parse_keyword_labelconst() THX_parse_keyword_labelconst(aTHX)
898static OP *THX_parse_keyword_labelconst(pTHX)
899{
900 return newSVOP(OP_CONST, 0, parse_label(0));
901}
902
78cdf107
Z
903#define parse_keyword_arrayfullexpr() THX_parse_keyword_arrayfullexpr(aTHX)
904static OP *THX_parse_keyword_arrayfullexpr(pTHX)
905{
906 return newANONLIST(parse_fullexpr(0));
907}
908
909#define parse_keyword_arraylistexpr() THX_parse_keyword_arraylistexpr(aTHX)
910static OP *THX_parse_keyword_arraylistexpr(pTHX)
911{
912 return newANONLIST(parse_listexpr(0));
913}
914
915#define parse_keyword_arraytermexpr() THX_parse_keyword_arraytermexpr(aTHX)
916static OP *THX_parse_keyword_arraytermexpr(pTHX)
917{
918 return newANONLIST(parse_termexpr(0));
919}
920
921#define parse_keyword_arrayarithexpr() THX_parse_keyword_arrayarithexpr(aTHX)
922static OP *THX_parse_keyword_arrayarithexpr(pTHX)
923{
924 return newANONLIST(parse_arithexpr(0));
925}
926
927#define parse_keyword_arrayexprflags() THX_parse_keyword_arrayexprflags(aTHX)
928static OP *THX_parse_keyword_arrayexprflags(pTHX)
929{
930 U32 flags = 0;
931 I32 c;
932 OP *o;
933 lex_read_space(0);
934 c = lex_peek_unichar(0);
935 if (c != '!' && c != '?') croak("syntax error");
936 lex_read_unichar(0);
937 if (c == '?') flags |= PARSE_OPTIONAL;
938 o = parse_listexpr(flags);
939 return o ? newANONLIST(o) : newANONHASH(newOP(OP_STUB, 0));
940}
941
83f8bb56
Z
942/* plugin glue */
943
944#define keyword_active(hintkey_sv) THX_keyword_active(aTHX_ hintkey_sv)
945static int THX_keyword_active(pTHX_ SV *hintkey_sv)
946{
947 HE *he;
948 if(!GvHV(PL_hintgv)) return 0;
949 he = hv_fetch_ent(GvHV(PL_hintgv), hintkey_sv, 0,
950 SvSHARED_HASH(hintkey_sv));
951 return he && SvTRUE(HeVAL(he));
952}
953
954static int my_keyword_plugin(pTHX_
955 char *keyword_ptr, STRLEN keyword_len, OP **op_ptr)
956{
957 if(keyword_len == 3 && strnEQ(keyword_ptr, "rpn", 3) &&
958 keyword_active(hintkey_rpn_sv)) {
959 *op_ptr = parse_keyword_rpn();
960 return KEYWORD_PLUGIN_EXPR;
961 } else if(keyword_len == 7 && strnEQ(keyword_ptr, "calcrpn", 7) &&
962 keyword_active(hintkey_calcrpn_sv)) {
963 *op_ptr = parse_keyword_calcrpn();
964 return KEYWORD_PLUGIN_STMT;
965 } else if(keyword_len == 9 && strnEQ(keyword_ptr, "stufftest", 9) &&
966 keyword_active(hintkey_stufftest_sv)) {
967 *op_ptr = parse_keyword_stufftest();
968 return KEYWORD_PLUGIN_STMT;
969 } else if(keyword_len == 12 &&
970 strnEQ(keyword_ptr, "swaptwostmts", 12) &&
971 keyword_active(hintkey_swaptwostmts_sv)) {
972 *op_ptr = parse_keyword_swaptwostmts();
973 return KEYWORD_PLUGIN_STMT;
07ffcb73
Z
974 } else if(keyword_len == 8 && strnEQ(keyword_ptr, "looprest", 8) &&
975 keyword_active(hintkey_looprest_sv)) {
976 *op_ptr = parse_keyword_looprest();
977 return KEYWORD_PLUGIN_STMT;
a7aaec61
Z
978 } else if(keyword_len == 14 && strnEQ(keyword_ptr, "scopelessblock", 14) &&
979 keyword_active(hintkey_scopelessblock_sv)) {
980 *op_ptr = parse_keyword_scopelessblock();
981 return KEYWORD_PLUGIN_STMT;
9eb5c532
Z
982 } else if(keyword_len == 10 && strnEQ(keyword_ptr, "stmtasexpr", 10) &&
983 keyword_active(hintkey_stmtasexpr_sv)) {
984 *op_ptr = parse_keyword_stmtasexpr();
985 return KEYWORD_PLUGIN_EXPR;
986 } else if(keyword_len == 11 && strnEQ(keyword_ptr, "stmtsasexpr", 11) &&
987 keyword_active(hintkey_stmtsasexpr_sv)) {
988 *op_ptr = parse_keyword_stmtsasexpr();
989 return KEYWORD_PLUGIN_EXPR;
e53d8f76
Z
990 } else if(keyword_len == 9 && strnEQ(keyword_ptr, "loopblock", 9) &&
991 keyword_active(hintkey_loopblock_sv)) {
992 *op_ptr = parse_keyword_loopblock();
993 return KEYWORD_PLUGIN_STMT;
994 } else if(keyword_len == 11 && strnEQ(keyword_ptr, "blockasexpr", 11) &&
995 keyword_active(hintkey_blockasexpr_sv)) {
996 *op_ptr = parse_keyword_blockasexpr();
997 return KEYWORD_PLUGIN_EXPR;
361d9b55
Z
998 } else if(keyword_len == 9 && strnEQ(keyword_ptr, "swaplabel", 9) &&
999 keyword_active(hintkey_swaplabel_sv)) {
1000 *op_ptr = parse_keyword_swaplabel();
1001 return KEYWORD_PLUGIN_STMT;
1002 } else if(keyword_len == 10 && strnEQ(keyword_ptr, "labelconst", 10) &&
1003 keyword_active(hintkey_labelconst_sv)) {
1004 *op_ptr = parse_keyword_labelconst();
1005 return KEYWORD_PLUGIN_EXPR;
78cdf107
Z
1006 } else if(keyword_len == 13 && strnEQ(keyword_ptr, "arrayfullexpr", 13) &&
1007 keyword_active(hintkey_arrayfullexpr_sv)) {
1008 *op_ptr = parse_keyword_arrayfullexpr();
1009 return KEYWORD_PLUGIN_EXPR;
1010 } else if(keyword_len == 13 && strnEQ(keyword_ptr, "arraylistexpr", 13) &&
1011 keyword_active(hintkey_arraylistexpr_sv)) {
1012 *op_ptr = parse_keyword_arraylistexpr();
1013 return KEYWORD_PLUGIN_EXPR;
1014 } else if(keyword_len == 13 && strnEQ(keyword_ptr, "arraytermexpr", 13) &&
1015 keyword_active(hintkey_arraytermexpr_sv)) {
1016 *op_ptr = parse_keyword_arraytermexpr();
1017 return KEYWORD_PLUGIN_EXPR;
1018 } else if(keyword_len == 14 && strnEQ(keyword_ptr, "arrayarithexpr", 14) &&
1019 keyword_active(hintkey_arrayarithexpr_sv)) {
1020 *op_ptr = parse_keyword_arrayarithexpr();
1021 return KEYWORD_PLUGIN_EXPR;
1022 } else if(keyword_len == 14 && strnEQ(keyword_ptr, "arrayexprflags", 14) &&
1023 keyword_active(hintkey_arrayexprflags_sv)) {
1024 *op_ptr = parse_keyword_arrayexprflags();
1025 return KEYWORD_PLUGIN_EXPR;
83f8bb56
Z
1026 } else {
1027 return next_keyword_plugin(aTHX_ keyword_ptr, keyword_len, op_ptr);
1028 }
1029}
1030
f568d64d
BM
1031static XOP my_xop;
1032
1033static OP *
1034pp_xop(pTHX)
1035{
1036 return PL_op->op_next;
1037}
1038
1039static void
1040peep_xop(pTHX_ OP *o, OP *oldop)
1041{
1042 dMY_CXT;
b58757d5
BM
1043 av_push(MY_CXT.xop_record, newSVpvf("peep:%"UVxf, PTR2UV(o)));
1044 av_push(MY_CXT.xop_record, newSVpvf("oldop:%"UVxf, PTR2UV(oldop)));
f568d64d
BM
1045}
1046
27fcb6ee
FC
1047static I32
1048filter_call(pTHX_ int idx, SV *buf_sv, int maxlen)
1049{
1050 SV *my_sv = FILTER_DATA(idx);
1051 char *p;
1052 char *end;
1053 int n = FILTER_READ(idx + 1, buf_sv, maxlen);
1054
1055 if (n<=0) return n;
1056
1057 p = SvPV_force_nolen(buf_sv);
1058 end = p + SvCUR(buf_sv);
1059 while (p < end) {
1060 if (*p == 'o') *p = 'e';
1061 p++;
1062 }
1063 return SvCUR(buf_sv);
1064}
1065
1066
9777c6aa
SM
1067XS_EXTERNAL(XS_XS__APItest__XSUB_XS_VERSION_undef);
1068XS_EXTERNAL(XS_XS__APItest__XSUB_XS_VERSION_empty);
1069XS_EXTERNAL(XS_XS__APItest__XSUB_XS_APIVERSION_invalid);
7b20c7cd 1070
55289a74
NC
1071#include "const-c.inc"
1072
ffe53d21 1073MODULE = XS::APItest PACKAGE = XS::APItest
0314122a 1074
55289a74
NC
1075INCLUDE: const-xs.inc
1076
ffe53d21
NC
1077INCLUDE: numeric.xs
1078
fed3ba5d
NC
1079MODULE = XS::APItest::utf8 PACKAGE = XS::APItest::utf8
1080
1081int
1082bytes_cmp_utf8(bytes, utf8)
1083 SV *bytes
1084 SV *utf8
1085 PREINIT:
1086 const U8 *b;
1087 STRLEN blen;
1088 const U8 *u;
1089 STRLEN ulen;
1090 CODE:
1091 b = (const U8 *)SvPVbyte(bytes, blen);
1092 u = (const U8 *)SvPVbyte(utf8, ulen);
1093 RETVAL = bytes_cmp_utf8(b, blen, u, ulen);
1094 OUTPUT:
1095 RETVAL
1096
7d255dc8
NC
1097MODULE = XS::APItest:Overload PACKAGE = XS::APItest::Overload
1098
c33e8be1 1099void
25a9ffce 1100amagic_deref_call(sv, what)
7d255dc8
NC
1101 SV *sv
1102 int what
1103 PPCODE:
7d255dc8 1104 /* The reference is owned by something else. */
25a9ffce 1105 PUSHs(amagic_deref_call(sv, what));
7d255dc8 1106
e89bfaa6
NC
1107# I'd certainly like to discourage the use of this macro, given that we now
1108# have amagic_deref_call
1109
c33e8be1 1110void
e89bfaa6
NC
1111tryAMAGICunDEREF_var(sv, what)
1112 SV *sv
1113 int what
1114 PPCODE:
1115 {
1116 SV **sp = &sv;
1117 switch(what) {
1118 case to_av_amg:
1119 tryAMAGICunDEREF(to_av);
1120 break;
1121 case to_cv_amg:
1122 tryAMAGICunDEREF(to_cv);
1123 break;
1124 case to_gv_amg:
1125 tryAMAGICunDEREF(to_gv);
1126 break;
1127 case to_hv_amg:
1128 tryAMAGICunDEREF(to_hv);
1129 break;
1130 case to_sv_amg:
1131 tryAMAGICunDEREF(to_sv);
1132 break;
1133 default:
1134 croak("Invalid value %d passed to tryAMAGICunDEREF_var", what);
1135 }
1136 }
1137 /* The reference is owned by something else. */
1138 PUSHs(sv);
1139
7b20c7cd
NC
1140MODULE = XS::APItest PACKAGE = XS::APItest::XSUB
1141
1142BOOT:
1143 newXS("XS::APItest::XSUB::XS_VERSION_undef", XS_XS__APItest__XSUB_XS_VERSION_undef, __FILE__);
f9cc56fa 1144 newXS("XS::APItest::XSUB::XS_VERSION_empty", XS_XS__APItest__XSUB_XS_VERSION_empty, __FILE__);
88c4b02d 1145 newXS("XS::APItest::XSUB::XS_APIVERSION_invalid", XS_XS__APItest__XSUB_XS_APIVERSION_invalid, __FILE__);
7b20c7cd
NC
1146
1147void
1148XS_VERSION_defined(...)
1149 PPCODE:
1150 XS_VERSION_BOOTCHECK;
1151 XSRETURN_EMPTY;
1152
88c4b02d
NC
1153void
1154XS_APIVERSION_valid(...)
1155 PPCODE:
1156 XS_APIVERSION_BOOTCHECK;
1157 XSRETURN_EMPTY;
1158
ffe53d21
NC
1159MODULE = XS::APItest:Hash PACKAGE = XS::APItest::Hash
1160
b54b4831
NC
1161void
1162rot13_hash(hash)
1163 HV *hash
1164 CODE:
1165 {
1166 struct ufuncs uf;
1167 uf.uf_val = rot13_key;
1168 uf.uf_set = 0;
1169 uf.uf_index = 0;
1170
1171 sv_magic((SV*)hash, NULL, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
1172 }
1173
53c40a8f
NC
1174void
1175bitflip_hash(hash)
1176 HV *hash
1177 CODE:
1178 {
1179 struct ufuncs uf;
1180 uf.uf_val = bitflip_key;
1181 uf.uf_set = 0;
1182 uf.uf_index = 0;
1183
1184 sv_magic((SV*)hash, NULL, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
1185 }
1186
028f8eaa
MHM
1187#define UTF8KLEN(sv, len) (SvUTF8(sv) ? -(I32)len : (I32)len)
1188
0314122a
NC
1189bool
1190exists(hash, key_sv)
1191 PREINIT:
1192 STRLEN len;
1193 const char *key;
1194 INPUT:
1195 HV *hash
1196 SV *key_sv
1197 CODE:
1198 key = SvPV(key_sv, len);
028f8eaa 1199 RETVAL = hv_exists(hash, key, UTF8KLEN(key_sv, len));
0314122a
NC
1200 OUTPUT:
1201 RETVAL
1202
bdee33e4
NC
1203bool
1204exists_ent(hash, key_sv)
1205 PREINIT:
1206 INPUT:
1207 HV *hash
1208 SV *key_sv
1209 CODE:
1210 RETVAL = hv_exists_ent(hash, key_sv, 0);
1211 OUTPUT:
1212 RETVAL
1213
b60cf05a 1214SV *
55289a74 1215delete(hash, key_sv, flags = 0)
b60cf05a
NC
1216 PREINIT:
1217 STRLEN len;
1218 const char *key;
1219 INPUT:
1220 HV *hash
1221 SV *key_sv
55289a74 1222 I32 flags;
b60cf05a
NC
1223 CODE:
1224 key = SvPV(key_sv, len);
1225 /* It's already mortal, so need to increase reference count. */
55289a74
NC
1226 RETVAL
1227 = SvREFCNT_inc(hv_delete(hash, key, UTF8KLEN(key_sv, len), flags));
1228 OUTPUT:
1229 RETVAL
1230
1231SV *
1232delete_ent(hash, key_sv, flags = 0)
1233 INPUT:
1234 HV *hash
1235 SV *key_sv
1236 I32 flags;
1237 CODE:
1238 /* It's already mortal, so need to increase reference count. */
1239 RETVAL = SvREFCNT_inc(hv_delete_ent(hash, key_sv, flags, 0));
b60cf05a
NC
1240 OUTPUT:
1241 RETVAL
1242
1243SV *
858117f8
NC
1244store_ent(hash, key, value)
1245 PREINIT:
1246 SV *copy;
1247 HE *result;
1248 INPUT:
1249 HV *hash
1250 SV *key
1251 SV *value
1252 CODE:
1253 copy = newSV(0);
1254 result = hv_store_ent(hash, key, copy, 0);
1255 SvSetMagicSV(copy, value);
1256 if (!result) {
1257 SvREFCNT_dec(copy);
1258 XSRETURN_EMPTY;
1259 }
1260 /* It's about to become mortal, so need to increase reference count.
1261 */
1262 RETVAL = SvREFCNT_inc(HeVAL(result));
1263 OUTPUT:
1264 RETVAL
1265
858117f8 1266SV *
b60cf05a
NC
1267store(hash, key_sv, value)
1268 PREINIT:
1269 STRLEN len;
1270 const char *key;
1271 SV *copy;
1272 SV **result;
1273 INPUT:
1274 HV *hash
1275 SV *key_sv
1276 SV *value
1277 CODE:
1278 key = SvPV(key_sv, len);
1279 copy = newSV(0);
028f8eaa 1280 result = hv_store(hash, key, UTF8KLEN(key_sv, len), copy, 0);
858117f8 1281 SvSetMagicSV(copy, value);
b60cf05a
NC
1282 if (!result) {
1283 SvREFCNT_dec(copy);
1284 XSRETURN_EMPTY;
1285 }
1286 /* It's about to become mortal, so need to increase reference count.
1287 */
1288 RETVAL = SvREFCNT_inc(*result);
1289 OUTPUT:
1290 RETVAL
1291
bdee33e4
NC
1292SV *
1293fetch_ent(hash, key_sv)
1294 PREINIT:
1295 HE *result;
1296 INPUT:
1297 HV *hash
1298 SV *key_sv
1299 CODE:
1300 result = hv_fetch_ent(hash, key_sv, 0, 0);
1301 if (!result) {
1302 XSRETURN_EMPTY;
1303 }
1304 /* Force mg_get */
1305 RETVAL = newSVsv(HeVAL(result));
1306 OUTPUT:
1307 RETVAL
b60cf05a
NC
1308
1309SV *
1310fetch(hash, key_sv)
1311 PREINIT:
1312 STRLEN len;
1313 const char *key;
1314 SV **result;
1315 INPUT:
1316 HV *hash
1317 SV *key_sv
1318 CODE:
1319 key = SvPV(key_sv, len);
028f8eaa 1320 result = hv_fetch(hash, key, UTF8KLEN(key_sv, len), 0);
b60cf05a
NC
1321 if (!result) {
1322 XSRETURN_EMPTY;
1323 }
1324 /* Force mg_get */
1325 RETVAL = newSVsv(*result);
1326 OUTPUT:
1327 RETVAL
2dc92170 1328
9568a123
NC
1329#if defined (hv_common)
1330
6b4de907
NC
1331SV *
1332common(params)
1333 INPUT:
1334 HV *params
1335 PREINIT:
1336 HE *result;
1337 HV *hv = NULL;
1338 SV *keysv = NULL;
1339 const char *key = NULL;
1340 STRLEN klen = 0;
1341 int flags = 0;
1342 int action = 0;
1343 SV *val = NULL;
1344 U32 hash = 0;
1345 SV **svp;
1346 CODE:
1347 if ((svp = hv_fetchs(params, "hv", 0))) {
1348 SV *const rv = *svp;
1349 if (!SvROK(rv))
1350 croak("common passed a non-reference for parameter hv");
1351 hv = (HV *)SvRV(rv);
1352 }
1353 if ((svp = hv_fetchs(params, "keysv", 0)))
1354 keysv = *svp;
1355 if ((svp = hv_fetchs(params, "keypv", 0))) {
1356 key = SvPV_const(*svp, klen);
1357 if (SvUTF8(*svp))
1358 flags = HVhek_UTF8;
1359 }
1360 if ((svp = hv_fetchs(params, "action", 0)))
1361 action = SvIV(*svp);
1362 if ((svp = hv_fetchs(params, "val", 0)))
527df579 1363 val = newSVsv(*svp);
6b4de907 1364 if ((svp = hv_fetchs(params, "hash", 0)))
a44093a9 1365 hash = SvUV(*svp);
6b4de907 1366
527df579
NC
1367 if ((svp = hv_fetchs(params, "hash_pv", 0))) {
1368 PERL_HASH(hash, key, klen);
1369 }
58ca560a
NC
1370 if ((svp = hv_fetchs(params, "hash_sv", 0))) {
1371 STRLEN len;
1372 const char *const p = SvPV(keysv, len);
1373 PERL_HASH(hash, p, len);
1374 }
527df579 1375
a75fcbca 1376 result = (HE *)hv_common(hv, keysv, key, klen, flags, action, val, hash);
6b4de907
NC
1377 if (!result) {
1378 XSRETURN_EMPTY;
1379 }
1380 /* Force mg_get */
1381 RETVAL = newSVsv(HeVAL(result));
1382 OUTPUT:
1383 RETVAL
1384
9568a123
NC
1385#endif
1386
439efdfe 1387void
2dc92170
NC
1388test_hv_free_ent()
1389 PPCODE:
1390 test_freeent(&Perl_hv_free_ent);
1391 XSRETURN(4);
1392
439efdfe 1393void
2dc92170
NC
1394test_hv_delayfree_ent()
1395 PPCODE:
1396 test_freeent(&Perl_hv_delayfree_ent);
1397 XSRETURN(4);
35ab5632
NC
1398
1399SV *
1400test_share_unshare_pvn(input)
1401 PREINIT:
35ab5632
NC
1402 STRLEN len;
1403 U32 hash;
1404 char *pvx;
1405 char *p;
1406 INPUT:
1407 SV *input
1408 CODE:
1409 pvx = SvPV(input, len);
1410 PERL_HASH(hash, pvx, len);
1411 p = sharepvn(pvx, len, hash);
1412 RETVAL = newSVpvn(p, len);
1413 unsharepvn(p, len, hash);
1414 OUTPUT:
1415 RETVAL
d8c5b3c5 1416
9568a123
NC
1417#if PERL_VERSION >= 9
1418
d8c5b3c5
NC
1419bool
1420refcounted_he_exists(key, level=0)
1421 SV *key
1422 IV level
1423 CODE:
1424 if (level) {
1425 croak("level must be zero, not %"IVdf, level);
1426 }
20439bc7 1427 RETVAL = (cop_hints_fetch_sv(PL_curcop, key, 0, 0) != &PL_sv_placeholder);
d8c5b3c5
NC
1428 OUTPUT:
1429 RETVAL
1430
d8c5b3c5
NC
1431SV *
1432refcounted_he_fetch(key, level=0)
1433 SV *key
1434 IV level
1435 CODE:
1436 if (level) {
1437 croak("level must be zero, not %"IVdf, level);
1438 }
20439bc7 1439 RETVAL = cop_hints_fetch_sv(PL_curcop, key, 0, 0);
d8c5b3c5
NC
1440 SvREFCNT_inc(RETVAL);
1441 OUTPUT:
1442 RETVAL
65bfe90c 1443
9568a123 1444#endif
65bfe90c 1445
0314122a
NC
1446=pod
1447
1448sub TIEHASH { bless {}, $_[0] }
1449sub STORE { $_[0]->{$_[1]} = $_[2] }
1450sub FETCH { $_[0]->{$_[1]} }
1451sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
1452sub NEXTKEY { each %{$_[0]} }
1453sub EXISTS { exists $_[0]->{$_[1]} }
1454sub DELETE { delete $_[0]->{$_[1]} }
1455sub CLEAR { %{$_[0]} = () }
1456
1457=cut
1458
e2fe06dd
EB
1459MODULE = XS::APItest:TempLv PACKAGE = XS::APItest::TempLv
1460
1461void
1462make_temp_mg_lv(sv)
1463SV* sv
1464 PREINIT:
1465 SV * const lv = newSV_type(SVt_PVLV);
1466 STRLEN len;
1467 PPCODE:
1468 SvPV(sv, len);
1469
1470 sv_magic(lv, NULL, PERL_MAGIC_substr, NULL, 0);
1471 LvTYPE(lv) = 'x';
1472 LvTARG(lv) = SvREFCNT_inc_simple(sv);
1473 LvTARGOFF(lv) = len == 0 ? 0 : 1;
1474 LvTARGLEN(lv) = len < 2 ? 0 : len-2;
1475
1476 EXTEND(SP, 1);
1477 ST(0) = sv_2mortal(lv);
1478 XSRETURN(1);
1479
1480
36c2b1d0
NC
1481MODULE = XS::APItest::PtrTable PACKAGE = XS::APItest::PtrTable PREFIX = ptr_table_
1482
1483void
1484ptr_table_new(classname)
1485const char * classname
1486 PPCODE:
1487 PUSHs(sv_setref_pv(sv_newmortal(), classname, (void*)ptr_table_new()));
1488
1489void
1490DESTROY(table)
1491XS::APItest::PtrTable table
1492 CODE:
1493 ptr_table_free(table);
1494
1495void
992b2363 1496ptr_table_store(table, from, to)
36c2b1d0 1497XS::APItest::PtrTable table
992b2363
NC
1498SVREF from
1499SVREF to
36c2b1d0 1500 CODE:
992b2363 1501 ptr_table_store(table, from, to);
36c2b1d0
NC
1502
1503UV
992b2363 1504ptr_table_fetch(table, from)
36c2b1d0 1505XS::APItest::PtrTable table
992b2363 1506SVREF from
36c2b1d0 1507 CODE:
992b2363 1508 RETVAL = PTR2UV(ptr_table_fetch(table, from));
36c2b1d0
NC
1509 OUTPUT:
1510 RETVAL
1511
1512void
1513ptr_table_split(table)
1514XS::APItest::PtrTable table
1515
1516void
1517ptr_table_clear(table)
1518XS::APItest::PtrTable table
1519
3e61d65a
JH
1520MODULE = XS::APItest PACKAGE = XS::APItest
1521
1522PROTOTYPES: DISABLE
1523
f568d64d
BM
1524HV *
1525xop_custom_ops ()
1526 CODE:
1527 RETVAL = PL_custom_ops;
1528 OUTPUT:
1529 RETVAL
1530
1531HV *
1532xop_custom_op_names ()
1533 CODE:
1534 PL_custom_op_names = newHV();
1535 RETVAL = PL_custom_op_names;
1536 OUTPUT:
1537 RETVAL
1538
1539HV *
1540xop_custom_op_descs ()
1541 CODE:
1542 PL_custom_op_descs = newHV();
1543 RETVAL = PL_custom_op_descs;
1544 OUTPUT:
1545 RETVAL
1546
1547void
1548xop_register ()
1549 CODE:
1550 XopENTRY_set(&my_xop, xop_name, "my_xop");
1551 XopENTRY_set(&my_xop, xop_desc, "XOP for testing");
1552 XopENTRY_set(&my_xop, xop_class, OA_UNOP);
1553 XopENTRY_set(&my_xop, xop_peep, peep_xop);
1554 Perl_custom_op_register(aTHX_ pp_xop, &my_xop);
1555
1556void
1557xop_clear ()
1558 CODE:
1559 XopDISABLE(&my_xop, xop_name);
1560 XopDISABLE(&my_xop, xop_desc);
1561 XopDISABLE(&my_xop, xop_class);
1562 XopDISABLE(&my_xop, xop_peep);
1563
1564IV
1565xop_my_xop ()
1566 CODE:
1567 RETVAL = PTR2IV(&my_xop);
1568 OUTPUT:
1569 RETVAL
1570
1571IV
1572xop_ppaddr ()
1573 CODE:
1574 RETVAL = PTR2IV(pp_xop);
1575 OUTPUT:
1576 RETVAL
1577
1578IV
1579xop_OA_UNOP ()
1580 CODE:
1581 RETVAL = OA_UNOP;
1582 OUTPUT:
1583 RETVAL
1584
1585AV *
1586xop_build_optree ()
1587 CODE:
1588 dMY_CXT;
1589 UNOP *unop;
1590 OP *kid;
1591
1592 MY_CXT.xop_record = newAV();
1593
1594 kid = newSVOP(OP_CONST, 0, newSViv(42));
1595
1596 NewOp(1102, unop, 1, UNOP);
1597 unop->op_type = OP_CUSTOM;
1598 unop->op_ppaddr = pp_xop;
1599 unop->op_flags = OPf_KIDS;
1600 unop->op_private = 0;
1601 unop->op_first = kid;
1602 unop->op_next = NULL;
1603 kid->op_next = (OP*)unop;
1604
b58757d5
BM
1605 av_push(MY_CXT.xop_record, newSVpvf("unop:%"UVxf, PTR2UV(unop)));
1606 av_push(MY_CXT.xop_record, newSVpvf("kid:%"UVxf, PTR2UV(kid)));
f568d64d
BM
1607
1608 av_push(MY_CXT.xop_record, newSVpvf("NAME:%s", OP_NAME((OP*)unop)));
1609 av_push(MY_CXT.xop_record, newSVpvf("DESC:%s", OP_DESC((OP*)unop)));
c33e8be1 1610 av_push(MY_CXT.xop_record, newSVpvf("CLASS:%d", (int)OP_CLASS((OP*)unop)));
f568d64d
BM
1611
1612 PL_rpeepp(aTHX_ kid);
1613
1614 FreeOp(kid);
1615 FreeOp(unop);
1616
1617 RETVAL = MY_CXT.xop_record;
1618 MY_CXT.xop_record = NULL;
1619 OUTPUT:
1620 RETVAL
1621
85ce96a1
DM
1622BOOT:
1623{
1624 MY_CXT_INIT;
03569ecf 1625
85ce96a1
DM
1626 MY_CXT.i = 99;
1627 MY_CXT.sv = newSVpv("initial",0);
13b6b3bc
BM
1628
1629 MY_CXT.bhkav = get_av("XS::APItest::bhkav", GV_ADDMULTI);
1630 MY_CXT.bhk_record = 0;
1631
a88d97bf
BM
1632 BhkENTRY_set(&bhk_test, bhk_start, blockhook_test_start);
1633 BhkENTRY_set(&bhk_test, bhk_pre_end, blockhook_test_pre_end);
1634 BhkENTRY_set(&bhk_test, bhk_post_end, blockhook_test_post_end);
1635 BhkENTRY_set(&bhk_test, bhk_eval, blockhook_test_eval);
13b6b3bc
BM
1636 Perl_blockhook_register(aTHX_ &bhk_test);
1637
65bfe90c 1638 MY_CXT.cscgv = gv_fetchpvs("XS::APItest::COMPILE_SCOPE_CONTAINER",
13b6b3bc 1639 GV_ADDMULTI, SVt_PVAV);
03569ecf
BM
1640 MY_CXT.cscav = GvAV(MY_CXT.cscgv);
1641
a88d97bf
BM
1642 BhkENTRY_set(&bhk_csc, bhk_start, blockhook_csc_start);
1643 BhkENTRY_set(&bhk_csc, bhk_pre_end, blockhook_csc_pre_end);
13b6b3bc 1644 Perl_blockhook_register(aTHX_ &bhk_csc);
201c7e1f
FR
1645
1646 MY_CXT.peep_recorder = newAV();
1647 MY_CXT.rpeep_recorder = newAV();
1648
1649 MY_CXT.orig_peep = PL_peepp;
1650 MY_CXT.orig_rpeep = PL_rpeepp;
1651 PL_peepp = my_peep;
1652 PL_rpeepp = my_rpeep;
65bfe90c 1653}
85ce96a1
DM
1654
1655void
1656CLONE(...)
1657 CODE:
1658 MY_CXT_CLONE;
c33e8be1 1659 PERL_UNUSED_VAR(items);
85ce96a1 1660 MY_CXT.sv = newSVpv("initial_clone",0);
65bfe90c 1661 MY_CXT.cscgv = gv_fetchpvs("XS::APItest::COMPILE_SCOPE_CONTAINER",
13b6b3bc 1662 GV_ADDMULTI, SVt_PVAV);
03569ecf 1663 MY_CXT.cscav = NULL;
13b6b3bc
BM
1664 MY_CXT.bhkav = get_av("XS::APItest::bhkav", GV_ADDMULTI);
1665 MY_CXT.bhk_record = 0;
201c7e1f
FR
1666 MY_CXT.peep_recorder = newAV();
1667 MY_CXT.rpeep_recorder = newAV();
85ce96a1 1668
3e61d65a
JH
1669void
1670print_double(val)
1671 double val
1672 CODE:
1673 printf("%5.3f\n",val);
1674
1675int
1676have_long_double()
1677 CODE:
1678#ifdef HAS_LONG_DOUBLE
1679 RETVAL = 1;
1680#else
1681 RETVAL = 0;
1682#endif
cabb36f0
CN
1683 OUTPUT:
1684 RETVAL
3e61d65a
JH
1685
1686void
1687print_long_double()
1688 CODE:
1689#ifdef HAS_LONG_DOUBLE
fc0bf671 1690# if defined(PERL_PRIfldbl) && (LONG_DOUBLESIZE > DOUBLESIZE)
3e61d65a
JH
1691 long double val = 7.0;
1692 printf("%5.3" PERL_PRIfldbl "\n",val);
1693# else
1694 double val = 7.0;
1695 printf("%5.3f\n",val);
1696# endif
1697#endif
1698
1699void
3e61d65a
JH
1700print_int(val)
1701 int val
1702 CODE:
1703 printf("%d\n",val);
1704
1705void
1706print_long(val)
1707 long val
1708 CODE:
1709 printf("%ld\n",val);
1710
1711void
1712print_float(val)
1713 float val
1714 CODE:
1715 printf("%5.3f\n",val);
9d911683
NIS
1716
1717void
1718print_flush()
1719 CODE:
1720 fflush(stdout);
d4b90eee
SH
1721
1722void
1723mpushp()
1724 PPCODE:
1725 EXTEND(SP, 3);
1726 mPUSHp("one", 3);
1727 mPUSHp("two", 3);
1728 mPUSHp("three", 5);
1729 XSRETURN(3);
1730
1731void
1732mpushn()
1733 PPCODE:
1734 EXTEND(SP, 3);
1735 mPUSHn(0.5);
1736 mPUSHn(-0.25);
1737 mPUSHn(0.125);
1738 XSRETURN(3);
1739
1740void
1741mpushi()
1742 PPCODE:
1743 EXTEND(SP, 3);
d75b63cf
MHM
1744 mPUSHi(-1);
1745 mPUSHi(2);
1746 mPUSHi(-3);
d4b90eee
SH
1747 XSRETURN(3);
1748
1749void
1750mpushu()
1751 PPCODE:
1752 EXTEND(SP, 3);
d75b63cf
MHM
1753 mPUSHu(1);
1754 mPUSHu(2);
1755 mPUSHu(3);
d4b90eee
SH
1756 XSRETURN(3);
1757
1758void
1759mxpushp()
1760 PPCODE:
1761 mXPUSHp("one", 3);
1762 mXPUSHp("two", 3);
1763 mXPUSHp("three", 5);
1764 XSRETURN(3);
1765
1766void
1767mxpushn()
1768 PPCODE:
1769 mXPUSHn(0.5);
1770 mXPUSHn(-0.25);
1771 mXPUSHn(0.125);
1772 XSRETURN(3);
1773
1774void
1775mxpushi()
1776 PPCODE:
d75b63cf
MHM
1777 mXPUSHi(-1);
1778 mXPUSHi(2);
1779 mXPUSHi(-3);
d4b90eee
SH
1780 XSRETURN(3);
1781
1782void
1783mxpushu()
1784 PPCODE:
d75b63cf
MHM
1785 mXPUSHu(1);
1786 mXPUSHu(2);
1787 mXPUSHu(3);
d4b90eee 1788 XSRETURN(3);
d1f347d7
DM
1789
1790
1791void
1792call_sv(sv, flags, ...)
1793 SV* sv
1794 I32 flags
1795 PREINIT:
1796 I32 i;
1797 PPCODE:
1798 for (i=0; i<items-2; i++)
1799 ST(i) = ST(i+2); /* pop first two args */
1800 PUSHMARK(SP);
1801 SP += items - 2;
1802 PUTBACK;
1803 i = call_sv(sv, flags);
1804 SPAGAIN;
1805 EXTEND(SP, 1);
1806 PUSHs(sv_2mortal(newSViv(i)));
1807
1808void
1809call_pv(subname, flags, ...)
1810 char* subname
1811 I32 flags
1812 PREINIT:
1813 I32 i;
1814 PPCODE:
1815 for (i=0; i<items-2; i++)
1816 ST(i) = ST(i+2); /* pop first two args */
1817 PUSHMARK(SP);
1818 SP += items - 2;
1819 PUTBACK;
1820 i = call_pv(subname, flags);
1821 SPAGAIN;
1822 EXTEND(SP, 1);
1823 PUSHs(sv_2mortal(newSViv(i)));
1824
1825void
1826call_method(methname, flags, ...)
1827 char* methname
1828 I32 flags
1829 PREINIT:
1830 I32 i;
1831 PPCODE:
1832 for (i=0; i<items-2; i++)
1833 ST(i) = ST(i+2); /* pop first two args */
1834 PUSHMARK(SP);
1835 SP += items - 2;
1836 PUTBACK;
1837 i = call_method(methname, flags);
1838 SPAGAIN;
1839 EXTEND(SP, 1);
1840 PUSHs(sv_2mortal(newSViv(i)));
1841
1842void
e6066781
BF
1843gv_init_type(namesv, multi, flags, type)
1844 SV* namesv
1845 int multi
1846 I32 flags
1847 int type
1848 PREINIT:
1849 STRLEN len;
1850 const char * const name = SvPV_const(namesv, len);
1851 GV *gv = *(GV**)hv_fetch(PL_defstash, name, len, TRUE);
1852 PPCODE:
1853 if (SvTYPE(gv) == SVt_PVGV)
1854 Perl_croak(aTHX_ "GV is already a PVGV");
1855 switch (type) {
1856 case 0:
1857 gv_init(gv, PL_defstash, name, len, multi);
1858 break;
1859 case 1:
1860 gv_init_sv(gv, PL_defstash, namesv, multi, flags);
1861 break;
1862 case 2:
1863 gv_init_pv(gv, PL_defstash, name, multi, flags | SvUTF8(namesv));
1864 break;
1865 case 3:
1866 gv_init_pvn(gv, PL_defstash, name, len, multi, flags | SvUTF8(namesv));
1867 break;
1868 }
1869 XPUSHs( gv ? (SV*)gv : &PL_sv_undef);
1870
1871void
d1f347d7
DM
1872eval_sv(sv, flags)
1873 SV* sv
1874 I32 flags
1875 PREINIT:
1876 I32 i;
1877 PPCODE:
1878 PUTBACK;
1879 i = eval_sv(sv, flags);
1880 SPAGAIN;
1881 EXTEND(SP, 1);
1882 PUSHs(sv_2mortal(newSViv(i)));
1883
b8e65a9b 1884void
d1f347d7
DM
1885eval_pv(p, croak_on_error)
1886 const char* p
1887 I32 croak_on_error
d1f347d7
DM
1888 PPCODE:
1889 PUTBACK;
1890 EXTEND(SP, 1);
1891 PUSHs(eval_pv(p, croak_on_error));
1892
1893void
1894require_pv(pv)
1895 const char* pv
d1f347d7
DM
1896 PPCODE:
1897 PUTBACK;
1898 require_pv(pv);
1899
0ca3a874 1900int
7a646707 1901apitest_exception(throw_e)
0ca3a874
MHM
1902 int throw_e
1903 OUTPUT:
1904 RETVAL
d1f347d7 1905
ef469b03 1906void
7e7a3dfc
GA
1907mycroak(sv)
1908 SV* sv
ef469b03 1909 CODE:
7e7a3dfc
GA
1910 if (SvOK(sv)) {
1911 Perl_croak(aTHX_ "%s", SvPV_nolen(sv));
1912 }
1913 else {
1914 Perl_croak(aTHX_ NULL);
1915 }
5d2b1485
NC
1916
1917SV*
1918strtab()
1919 CODE:
1920 RETVAL = newRV_inc((SV*)PL_strtab);
1921 OUTPUT:
1922 RETVAL
85ce96a1
DM
1923
1924int
1925my_cxt_getint()
1926 CODE:
1927 dMY_CXT;
1928 RETVAL = my_cxt_getint_p(aMY_CXT);
1929 OUTPUT:
1930 RETVAL
1931
1932void
1933my_cxt_setint(i)
1934 int i;
1935 CODE:
1936 dMY_CXT;
1937 my_cxt_setint_p(aMY_CXT_ i);
1938
1939void
9568a123
NC
1940my_cxt_getsv(how)
1941 bool how;
85ce96a1 1942 PPCODE:
85ce96a1 1943 EXTEND(SP, 1);
9568a123 1944 ST(0) = how ? my_cxt_getsv_interp_context() : my_cxt_getsv_interp();
85ce96a1
DM
1945 XSRETURN(1);
1946
1947void
1948my_cxt_setsv(sv)
1949 SV *sv;
1950 CODE:
1951 dMY_CXT;
1952 SvREFCNT_dec(MY_CXT.sv);
1953 my_cxt_setsv_p(sv _aMY_CXT);
1954 SvREFCNT_inc(sv);
34482cd6
NC
1955
1956bool
1957sv_setsv_cow_hashkey_core()
1958
1959bool
1960sv_setsv_cow_hashkey_notcore()
84ac5fd7
NC
1961
1962void
218787bd
VP
1963rmagical_cast(sv, type)
1964 SV *sv;
1965 SV *type;
1966 PREINIT:
1967 struct ufuncs uf;
1968 PPCODE:
1969 if (!SvOK(sv) || !SvROK(sv) || !SvOK(type)) { XSRETURN_UNDEF; }
1970 sv = SvRV(sv);
1971 if (SvTYPE(sv) != SVt_PVHV) { XSRETURN_UNDEF; }
1972 uf.uf_val = rmagical_a_dummy;
1973 uf.uf_set = NULL;
1974 uf.uf_index = 0;
1975 if (SvTRUE(type)) { /* b */
1976 sv_magicext(sv, NULL, PERL_MAGIC_ext, &rmagical_b, NULL, 0);
1977 } else { /* a */
1978 sv_magic(sv, NULL, PERL_MAGIC_uvar, (char *) &uf, sizeof(uf));
1979 }
1980 XSRETURN_YES;
1981
1982void
1983rmagical_flags(sv)
1984 SV *sv;
1985 PPCODE:
1986 if (!SvOK(sv) || !SvROK(sv)) { XSRETURN_UNDEF; }
1987 sv = SvRV(sv);
1988 EXTEND(SP, 3);
1989 mXPUSHu(SvFLAGS(sv) & SVs_GMG);
1990 mXPUSHu(SvFLAGS(sv) & SVs_SMG);
1991 mXPUSHu(SvFLAGS(sv) & SVs_RMG);
1992 XSRETURN(3);
1993
1994void
90d1f214
BM
1995my_caller(level)
1996 I32 level
1997 PREINIT:
1998 const PERL_CONTEXT *cx, *dbcx;
1999 const char *pv;
2000 const GV *gv;
2001 HV *hv;
2002 PPCODE:
2003 cx = caller_cx(level, &dbcx);
2004 EXTEND(SP, 8);
2005
2006 pv = CopSTASHPV(cx->blk_oldcop);
2007 ST(0) = pv ? sv_2mortal(newSVpv(pv, 0)) : &PL_sv_undef;
2008 gv = CvGV(cx->blk_sub.cv);
2009 ST(1) = isGV(gv) ? sv_2mortal(newSVpv(GvNAME(gv), 0)) : &PL_sv_undef;
2010
2011 pv = CopSTASHPV(dbcx->blk_oldcop);
2012 ST(2) = pv ? sv_2mortal(newSVpv(pv, 0)) : &PL_sv_undef;
2013 gv = CvGV(dbcx->blk_sub.cv);
2014 ST(3) = isGV(gv) ? sv_2mortal(newSVpv(GvNAME(gv), 0)) : &PL_sv_undef;
2015
20439bc7
Z
2016 ST(4) = cop_hints_fetch_pvs(cx->blk_oldcop, "foo", 0);
2017 ST(5) = cop_hints_fetch_pvn(cx->blk_oldcop, "foo", 3, 0, 0);
2018 ST(6) = cop_hints_fetch_sv(cx->blk_oldcop,
2019 sv_2mortal(newSVpvn("foo", 3)), 0, 0);
90d1f214 2020
20439bc7 2021 hv = cop_hints_2hv(cx->blk_oldcop, 0);
90d1f214
BM
2022 ST(7) = hv ? sv_2mortal(newRV_noinc((SV *)hv)) : &PL_sv_undef;
2023
2024 XSRETURN(8);
2025
2026void
f9c17636
MB
2027DPeek (sv)
2028 SV *sv
2029
2030 PPCODE:
5b1f7359 2031 ST (0) = newSVpv (Perl_sv_peek (aTHX_ sv), 0);
f9c17636
MB
2032 XSRETURN (1);
2033
2034void
84ac5fd7
NC
2035BEGIN()
2036 CODE:
2037 sv_inc(get_sv("XS::APItest::BEGIN_called", GV_ADD|GV_ADDMULTI));
2038
2039void
2040CHECK()
2041 CODE:
2042 sv_inc(get_sv("XS::APItest::CHECK_called", GV_ADD|GV_ADDMULTI));
2043
2044void
2045UNITCHECK()
2046 CODE:
0932863f 2047 sv_inc(get_sv("XS::APItest::UNITCHECK_called", GV_ADD|GV_ADDMULTI));
84ac5fd7
NC
2048
2049void
2050INIT()
2051 CODE:
2052 sv_inc(get_sv("XS::APItest::INIT_called", GV_ADD|GV_ADDMULTI));
2053
2054void
2055END()
2056 CODE:
2057 sv_inc(get_sv("XS::APItest::END_called", GV_ADD|GV_ADDMULTI));
30685b56
NC
2058
2059void
2060utf16_to_utf8 (sv, ...)
2061 SV* sv
2062 ALIAS:
2063 utf16_to_utf8_reversed = 1
2064 PREINIT:
2065 STRLEN len;
2066 U8 *source;
2067 SV *dest;
2068 I32 got; /* Gah, badly thought out APIs */
2069 CODE:
2070 source = (U8 *)SvPVbyte(sv, len);
2071 /* Optionally only convert part of the buffer. */
2072 if (items > 1) {
2073 len = SvUV(ST(1));
2074 }
2075 /* Mortalise this right now, as we'll be testing croak()s */
2076 dest = sv_2mortal(newSV(len * 3 / 2 + 1));
2077 if (ix) {
25f2e844 2078 utf16_to_utf8_reversed(source, (U8 *)SvPVX(dest), len, &got);
30685b56 2079 } else {
25f2e844 2080 utf16_to_utf8(source, (U8 *)SvPVX(dest), len, &got);
30685b56
NC
2081 }
2082 SvCUR_set(dest, got);
2083 SvPVX(dest)[got] = '\0';
2084 SvPOK_on(dest);
2085 ST(0) = dest;
2086 XSRETURN(1);
879d0c72 2087
6bd7445c
GG
2088void
2089my_exit(int exitcode)
2090 PPCODE:
2091 my_exit(exitcode);
d97c33b5 2092
209e41dc
NT
2093U8
2094first_byte(sv)
2095 SV *sv
2096 CODE:
2097 char *s;
2098 STRLEN len;
2099 s = SvPVbyte(sv, len);
2100 RETVAL = s[0];
2101 OUTPUT:
2102 RETVAL
2103
d97c33b5
DM
2104I32
2105sv_count()
2106 CODE:
2107 RETVAL = PL_sv_count;
2108 OUTPUT:
2109 RETVAL
13b6b3bc
BM
2110
2111void
2112bhk_record(bool on)
2113 CODE:
2114 dMY_CXT;
2115 MY_CXT.bhk_record = on;
2116 if (on)
2117 av_clear(MY_CXT.bhkav);
65bfe90c 2118
defdfed5 2119void
d9088386
Z
2120test_magic_chain()
2121 PREINIT:
2122 SV *sv;
2123 MAGIC *callmg, *uvarmg;
2124 CODE:
2125 sv = sv_2mortal(newSV(0));
11f9f0ed
NC
2126 if (SvTYPE(sv) >= SVt_PVMG) croak_fail();
2127 if (SvMAGICAL(sv)) croak_fail();
d9088386 2128 sv_magic(sv, &PL_sv_yes, PERL_MAGIC_checkcall, (char*)&callmg, 0);
11f9f0ed
NC
2129 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2130 if (!SvMAGICAL(sv)) croak_fail();
2131 if (mg_find(sv, PERL_MAGIC_uvar)) croak_fail();
d9088386 2132 callmg = mg_find(sv, PERL_MAGIC_checkcall);
11f9f0ed 2133 if (!callmg) croak_fail();
d9088386 2134 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 2135 croak_fail();
d9088386 2136 sv_magic(sv, &PL_sv_no, PERL_MAGIC_uvar, (char*)&uvarmg, 0);
11f9f0ed
NC
2137 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2138 if (!SvMAGICAL(sv)) croak_fail();
2139 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
d9088386 2140 uvarmg = mg_find(sv, PERL_MAGIC_uvar);
11f9f0ed 2141 if (!uvarmg) croak_fail();
d9088386 2142 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 2143 croak_fail();
d9088386 2144 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 2145 croak_fail();
d9088386 2146 mg_free_type(sv, PERL_MAGIC_vec);
11f9f0ed
NC
2147 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2148 if (!SvMAGICAL(sv)) croak_fail();
2149 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
2150 if (mg_find(sv, PERL_MAGIC_uvar) != uvarmg) croak_fail();
d9088386 2151 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 2152 croak_fail();
d9088386 2153 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 2154 croak_fail();
d9088386 2155 mg_free_type(sv, PERL_MAGIC_uvar);
11f9f0ed
NC
2156 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2157 if (!SvMAGICAL(sv)) croak_fail();
2158 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
2159 if (mg_find(sv, PERL_MAGIC_uvar)) croak_fail();
d9088386 2160 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 2161 croak_fail();
d9088386 2162 sv_magic(sv, &PL_sv_no, PERL_MAGIC_uvar, (char*)&uvarmg, 0);
11f9f0ed
NC
2163 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2164 if (!SvMAGICAL(sv)) croak_fail();
2165 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
d9088386 2166 uvarmg = mg_find(sv, PERL_MAGIC_uvar);
11f9f0ed 2167 if (!uvarmg) croak_fail();
d9088386 2168 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 2169 croak_fail();
d9088386 2170 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 2171 croak_fail();
d9088386 2172 mg_free_type(sv, PERL_MAGIC_checkcall);
11f9f0ed
NC
2173 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2174 if (!SvMAGICAL(sv)) croak_fail();
2175 if (mg_find(sv, PERL_MAGIC_uvar) != uvarmg) croak_fail();
2176 if (mg_find(sv, PERL_MAGIC_checkcall)) croak_fail();
d9088386 2177 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 2178 croak_fail();
d9088386 2179 mg_free_type(sv, PERL_MAGIC_uvar);
11f9f0ed
NC
2180 if (SvMAGICAL(sv)) croak_fail();
2181 if (mg_find(sv, PERL_MAGIC_checkcall)) croak_fail();
2182 if (mg_find(sv, PERL_MAGIC_uvar)) croak_fail();
d9088386
Z
2183
2184void
2185test_op_contextualize()
2186 PREINIT:
2187 OP *o;
2188 CODE:
2189 o = newSVOP(OP_CONST, 0, newSViv(0));
2190 o->op_flags &= ~OPf_WANT;
2191 o = op_contextualize(o, G_SCALAR);
2192 if (o->op_type != OP_CONST ||
2193 (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR)
11f9f0ed 2194 croak_fail();
d9088386
Z
2195 op_free(o);
2196 o = newSVOP(OP_CONST, 0, newSViv(0));
2197 o->op_flags &= ~OPf_WANT;
2198 o = op_contextualize(o, G_ARRAY);
2199 if (o->op_type != OP_CONST ||
2200 (o->op_flags & OPf_WANT) != OPf_WANT_LIST)
11f9f0ed 2201 croak_fail();
d9088386
Z
2202 op_free(o);
2203 o = newSVOP(OP_CONST, 0, newSViv(0));
2204 o->op_flags &= ~OPf_WANT;
2205 o = op_contextualize(o, G_VOID);
11f9f0ed 2206 if (o->op_type != OP_NULL) croak_fail();
d9088386
Z
2207 op_free(o);
2208
2209void
2210test_rv2cv_op_cv()
2211 PROTOTYPE:
2212 PREINIT:
2213 GV *troc_gv, *wibble_gv;
2214 CV *troc_cv;
2215 OP *o;
2216 CODE:
2217 troc_gv = gv_fetchpv("XS::APItest::test_rv2cv_op_cv", 0, SVt_PVGV);
2218 troc_cv = get_cv("XS::APItest::test_rv2cv_op_cv", 0);
2219 wibble_gv = gv_fetchpv("XS::APItest::wibble", 0, SVt_PVGV);
2220 o = newCVREF(0, newGVOP(OP_GV, 0, troc_gv));
11f9f0ed 2221 if (rv2cv_op_cv(o, 0) != troc_cv) croak_fail();
d9088386 2222 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV) != (CV*)troc_gv)
11f9f0ed 2223 croak_fail();
d9088386 2224 o->op_private |= OPpENTERSUB_AMPER;
11f9f0ed
NC
2225 if (rv2cv_op_cv(o, 0)) croak_fail();
2226 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386 2227 o->op_private &= ~OPpENTERSUB_AMPER;
11f9f0ed
NC
2228 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
2229 if (rv2cv_op_cv(o, RV2CVOPCV_MARK_EARLY) != troc_cv) croak_fail();
2230 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
d9088386
Z
2231 op_free(o);
2232 o = newSVOP(OP_CONST, 0, newSVpv("XS::APItest::test_rv2cv_op_cv", 0));
2233 o->op_private = OPpCONST_BARE;
2234 o = newCVREF(0, o);
11f9f0ed 2235 if (rv2cv_op_cv(o, 0) != troc_cv) croak_fail();
d9088386 2236 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV) != (CV*)troc_gv)
11f9f0ed 2237 croak_fail();
d9088386 2238 o->op_private |= OPpENTERSUB_AMPER;
11f9f0ed
NC
2239 if (rv2cv_op_cv(o, 0)) croak_fail();
2240 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386
Z
2241 op_free(o);
2242 o = newCVREF(0, newSVOP(OP_CONST, 0, newRV_inc((SV*)troc_cv)));
11f9f0ed 2243 if (rv2cv_op_cv(o, 0) != troc_cv) croak_fail();
d9088386 2244 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV) != (CV*)troc_gv)
11f9f0ed 2245 croak_fail();
d9088386 2246 o->op_private |= OPpENTERSUB_AMPER;
11f9f0ed
NC
2247 if (rv2cv_op_cv(o, 0)) croak_fail();
2248 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386 2249 o->op_private &= ~OPpENTERSUB_AMPER;
11f9f0ed
NC
2250 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
2251 if (rv2cv_op_cv(o, RV2CVOPCV_MARK_EARLY) != troc_cv) croak_fail();
2252 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
d9088386
Z
2253 op_free(o);
2254 o = newCVREF(0, newUNOP(OP_RAND, 0, newSVOP(OP_CONST, 0, newSViv(0))));
11f9f0ed
NC
2255 if (rv2cv_op_cv(o, 0)) croak_fail();
2256 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386 2257 o->op_private |= OPpENTERSUB_AMPER;
11f9f0ed
NC
2258 if (rv2cv_op_cv(o, 0)) croak_fail();
2259 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386 2260 o->op_private &= ~OPpENTERSUB_AMPER;
11f9f0ed
NC
2261 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
2262 if (rv2cv_op_cv(o, RV2CVOPCV_MARK_EARLY)) croak_fail();
2263 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
d9088386
Z
2264 op_free(o);
2265 o = newUNOP(OP_RAND, 0, newSVOP(OP_CONST, 0, newSViv(0)));
11f9f0ed
NC
2266 if (rv2cv_op_cv(o, 0)) croak_fail();
2267 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386
Z
2268 op_free(o);
2269
2270void
2271test_cv_getset_call_checker()
2272 PREINIT:
2273 CV *troc_cv, *tsh_cv;
2274 Perl_call_checker ckfun;
2275 SV *ckobj;
2276 CODE:
2277#define check_cc(cv, xckfun, xckobj) \
2278 do { \
2279 cv_get_call_checker((cv), &ckfun, &ckobj); \
779bc08a
NC
2280 if (ckfun != (xckfun)) croak_fail_ne(FPTR2DPTR(void *, ckfun), xckfun); \
2281 if (ckobj != (xckobj)) croak_fail_ne(FPTR2DPTR(void *, ckobj), xckobj); \
d9088386
Z
2282 } while(0)
2283 troc_cv = get_cv("XS::APItest::test_rv2cv_op_cv", 0);
2284 tsh_cv = get_cv("XS::APItest::test_savehints", 0);
2285 check_cc(troc_cv, Perl_ck_entersub_args_proto_or_list, (SV*)troc_cv);
2286 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv);
2287 cv_set_call_checker(tsh_cv, Perl_ck_entersub_args_proto_or_list,
2288 &PL_sv_yes);
2289 check_cc(troc_cv, Perl_ck_entersub_args_proto_or_list, (SV*)troc_cv);
2290 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, &PL_sv_yes);
2291 cv_set_call_checker(troc_cv, THX_ck_entersub_args_scalars, &PL_sv_no);
2292 check_cc(troc_cv, THX_ck_entersub_args_scalars, &PL_sv_no);
2293 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, &PL_sv_yes);
2294 cv_set_call_checker(tsh_cv, Perl_ck_entersub_args_proto_or_list,
2295 (SV*)tsh_cv);
2296 check_cc(troc_cv, THX_ck_entersub_args_scalars, &PL_sv_no);
2297 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv);
2298 cv_set_call_checker(troc_cv, Perl_ck_entersub_args_proto_or_list,
2299 (SV*)troc_cv);
2300 check_cc(troc_cv, Perl_ck_entersub_args_proto_or_list, (SV*)troc_cv);
2301 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv);
11f9f0ed
NC
2302 if (SvMAGICAL((SV*)troc_cv) || SvMAGIC((SV*)troc_cv)) croak_fail();
2303 if (SvMAGICAL((SV*)tsh_cv) || SvMAGIC((SV*)tsh_cv)) croak_fail();
d9088386
Z
2304#undef check_cc
2305
2306void
2307cv_set_call_checker_lists(CV *cv)
2308 CODE:
2309 cv_set_call_checker(cv, THX_ck_entersub_args_lists, &PL_sv_undef);
2310
2311void
2312cv_set_call_checker_scalars(CV *cv)
2313 CODE:
2314 cv_set_call_checker(cv, THX_ck_entersub_args_scalars, &PL_sv_undef);
2315
2316void
2317cv_set_call_checker_proto(CV *cv, SV *proto)
2318 CODE:
2319 if (SvROK(proto))
2320 proto = SvRV(proto);
2321 cv_set_call_checker(cv, Perl_ck_entersub_args_proto, proto);
2322
2323void
2324cv_set_call_checker_proto_or_list(CV *cv, SV *proto)
2325 CODE:
2326 if (SvROK(proto))
2327 proto = SvRV(proto);
2328 cv_set_call_checker(cv, Perl_ck_entersub_args_proto_or_list, proto);
2329
2330void
2331cv_set_call_checker_multi_sum(CV *cv)
2332 CODE:
2333 cv_set_call_checker(cv, THX_ck_entersub_multi_sum, &PL_sv_undef);
2334
2335void
20439bc7
Z
2336test_cophh()
2337 PREINIT:
2338 COPHH *a, *b;
2339 CODE:
2340#define check_ph(EXPR) \
2341 do { if((EXPR) != &PL_sv_placeholder) croak("fail"); } while(0)
2342#define check_iv(EXPR, EXPECT) \
2343 do { if(SvIV(EXPR) != (EXPECT)) croak("fail"); } while(0)
2344#define msvpvs(STR) sv_2mortal(newSVpvs(STR))
2345#define msviv(VALUE) sv_2mortal(newSViv(VALUE))
2346 a = cophh_new_empty();
2347 check_ph(cophh_fetch_pvn(a, "foo_1", 5, 0, 0));
2348 check_ph(cophh_fetch_pvs(a, "foo_1", 0));
2349 check_ph(cophh_fetch_pv(a, "foo_1", 0, 0));
2350 check_ph(cophh_fetch_sv(a, msvpvs("foo_1"), 0, 0));
2351 a = cophh_store_pvn(a, "foo_1abc", 5, 0, msviv(111), 0);
2352 a = cophh_store_pvs(a, "foo_2", msviv(222), 0);
2353 a = cophh_store_pv(a, "foo_3", 0, msviv(333), 0);
2354 a = cophh_store_sv(a, msvpvs("foo_4"), 0, msviv(444), 0);
2355 check_iv(cophh_fetch_pvn(a, "foo_1xyz", 5, 0, 0), 111);
2356 check_iv(cophh_fetch_pvs(a, "foo_1", 0), 111);
2357 check_iv(cophh_fetch_pv(a, "foo_1", 0, 0), 111);
2358 check_iv(cophh_fetch_sv(a, msvpvs("foo_1"), 0, 0), 111);
2359 check_iv(cophh_fetch_pvs(a, "foo_2", 0), 222);
2360 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
2361 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
2362 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
2363 b = cophh_copy(a);
2364 b = cophh_store_pvs(b, "foo_1", msviv(1111), 0);
2365 check_iv(cophh_fetch_pvs(a, "foo_1", 0), 111);
2366 check_iv(cophh_fetch_pvs(a, "foo_2", 0), 222);
2367 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
2368 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
2369 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
2370 check_iv(cophh_fetch_pvs(b, "foo_1", 0), 1111);
2371 check_iv(cophh_fetch_pvs(b, "foo_2", 0), 222);
2372 check_iv(cophh_fetch_pvs(b, "foo_3", 0), 333);
2373 check_iv(cophh_fetch_pvs(b, "foo_4", 0), 444);
2374 check_ph(cophh_fetch_pvs(b, "foo_5", 0));
2375 a = cophh_delete_pvn(a, "foo_1abc", 5, 0, 0);
2376 a = cophh_delete_pvs(a, "foo_2", 0);
2377 b = cophh_delete_pv(b, "foo_3", 0, 0);
2378 b = cophh_delete_sv(b, msvpvs("foo_4"), 0, 0);
2379 check_ph(cophh_fetch_pvs(a, "foo_1", 0));
2380 check_ph(cophh_fetch_pvs(a, "foo_2", 0));
2381 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
2382 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
2383 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
2384 check_iv(cophh_fetch_pvs(b, "foo_1", 0), 1111);
2385 check_iv(cophh_fetch_pvs(b, "foo_2", 0), 222);
2386 check_ph(cophh_fetch_pvs(b, "foo_3", 0));
2387 check_ph(cophh_fetch_pvs(b, "foo_4", 0));
2388 check_ph(cophh_fetch_pvs(b, "foo_5", 0));
2389 b = cophh_delete_pvs(b, "foo_3", 0);
2390 b = cophh_delete_pvs(b, "foo_5", 0);
2391 check_iv(cophh_fetch_pvs(b, "foo_1", 0), 1111);
2392 check_iv(cophh_fetch_pvs(b, "foo_2", 0), 222);
2393 check_ph(cophh_fetch_pvs(b, "foo_3", 0));
2394 check_ph(cophh_fetch_pvs(b, "foo_4", 0));
2395 check_ph(cophh_fetch_pvs(b, "foo_5", 0));
2396 cophh_free(b);
2397 check_ph(cophh_fetch_pvs(a, "foo_1", 0));
2398 check_ph(cophh_fetch_pvs(a, "foo_2", 0));
2399 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
2400 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
2401 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
2402 a = cophh_store_pvs(a, "foo_1", msviv(11111), COPHH_KEY_UTF8);
2403 a = cophh_store_pvs(a, "foo_\xaa", msviv(123), 0);
2404 a = cophh_store_pvs(a, "foo_\xc2\xbb", msviv(456), COPHH_KEY_UTF8);
2405 a = cophh_store_pvs(a, "foo_\xc3\x8c", msviv(789), COPHH_KEY_UTF8);
2406 a = cophh_store_pvs(a, "foo_\xd9\xa6", msviv(666), COPHH_KEY_UTF8);
2407 check_iv(cophh_fetch_pvs(a, "foo_1", 0), 11111);
2408 check_iv(cophh_fetch_pvs(a, "foo_1", COPHH_KEY_UTF8), 11111);
2409 check_iv(cophh_fetch_pvs(a, "foo_\xaa", 0), 123);
2410 check_iv(cophh_fetch_pvs(a, "foo_\xc2\xaa", COPHH_KEY_UTF8), 123);
2411 check_ph(cophh_fetch_pvs(a, "foo_\xc2\xaa", 0));
2412 check_iv(cophh_fetch_pvs(a, "foo_\xbb", 0), 456);
2413 check_iv(cophh_fetch_pvs(a, "foo_\xc2\xbb", COPHH_KEY_UTF8), 456);
2414 check_ph(cophh_fetch_pvs(a, "foo_\xc2\xbb", 0));
2415 check_iv(cophh_fetch_pvs(a, "foo_\xcc", 0), 789);
2416 check_iv(cophh_fetch_pvs(a, "foo_\xc3\x8c", COPHH_KEY_UTF8), 789);
2417 check_ph(cophh_fetch_pvs(a, "foo_\xc2\x8c", 0));
2418 check_iv(cophh_fetch_pvs(a, "foo_\xd9\xa6", COPHH_KEY_UTF8), 666);
2419 check_ph(cophh_fetch_pvs(a, "foo_\xd9\xa6", 0));
3987a177
Z
2420 ENTER;
2421 SAVEFREECOPHH(a);
2422 LEAVE;
20439bc7
Z
2423#undef check_ph
2424#undef check_iv
2425#undef msvpvs
2426#undef msviv
2427
8375c93e
RU
2428void
2429test_coplabel()
2430 PREINIT:
2431 COP *cop;
32708f0b
CB
2432 const char *label;
2433 STRLEN len;
2434 U32 utf8;
8375c93e
RU
2435 CODE:
2436 cop = &PL_compiling;
32708f0b
CB
2437 Perl_cop_store_label(aTHX_ cop, "foo", 3, 0);
2438 label = Perl_cop_fetch_label(aTHX_ cop, &len, &utf8);
2439 if (strcmp(label,"foo")) croak("fail # cop_fetch_label label");
2440 if (len != 3) croak("fail # cop_fetch_label len");
2441 if (utf8) croak("fail # cop_fetch_label utf8");
8375c93e 2442 /* SMALL GERMAN UMLAUT A */
32708f0b
CB
2443 Perl_cop_store_label(aTHX_ cop, "foä", 4, SVf_UTF8);
2444 label = Perl_cop_fetch_label(aTHX_ cop, &len, &utf8);
2445 if (strcmp(label,"foä")) croak("fail # cop_fetch_label label");
72024711 2446 if (len != 4) croak("fail # cop_fetch_label len");
32708f0b 2447 if (!utf8) croak("fail # cop_fetch_label utf8");
8375c93e
RU
2448
2449
20439bc7
Z
2450HV *
2451example_cophh_2hv()
2452 PREINIT:
2453 COPHH *a;
2454 CODE:
2455#define msviv(VALUE) sv_2mortal(newSViv(VALUE))
2456 a = cophh_new_empty();
2457 a = cophh_store_pvs(a, "foo_0", msviv(999), 0);
2458 a = cophh_store_pvs(a, "foo_1", msviv(111), 0);
2459 a = cophh_store_pvs(a, "foo_\xaa", msviv(123), 0);
2460 a = cophh_store_pvs(a, "foo_\xc2\xbb", msviv(456), COPHH_KEY_UTF8);
2461 a = cophh_store_pvs(a, "foo_\xc3\x8c", msviv(789), COPHH_KEY_UTF8);
2462 a = cophh_store_pvs(a, "foo_\xd9\xa6", msviv(666), COPHH_KEY_UTF8);
2463 a = cophh_delete_pvs(a, "foo_0", 0);
2464 a = cophh_delete_pvs(a, "foo_2", 0);
2465 RETVAL = cophh_2hv(a, 0);
2466 cophh_free(a);
2467#undef msviv
2468 OUTPUT:
2469 RETVAL
2470
2471void
defdfed5
Z
2472test_savehints()
2473 PREINIT:
2474 SV **svp, *sv;
2475 CODE:
2476#define store_hint(KEY, VALUE) \
2477 sv_setiv_mg(*hv_fetchs(GvHV(PL_hintgv), KEY, 1), (VALUE))
2478#define hint_ok(KEY, EXPECT) \
2479 ((svp = hv_fetchs(GvHV(PL_hintgv), KEY, 0)) && \
2480 (sv = *svp) && SvIV(sv) == (EXPECT) && \
20439bc7 2481 (sv = cop_hints_fetch_pvs(&PL_compiling, KEY, 0)) && \
defdfed5
Z
2482 SvIV(sv) == (EXPECT))
2483#define check_hint(KEY, EXPECT) \
11f9f0ed 2484 do { if (!hint_ok(KEY, EXPECT)) croak_fail(); } while(0)
defdfed5
Z
2485 PL_hints |= HINT_LOCALIZE_HH;
2486 ENTER;
2487 SAVEHINTS();
2488 PL_hints &= HINT_INTEGER;
2489 store_hint("t0", 123);
2490 store_hint("t1", 456);
11f9f0ed 2491 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2492 check_hint("t0", 123); check_hint("t1", 456);
2493 ENTER;
2494 SAVEHINTS();
11f9f0ed 2495 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2496 check_hint("t0", 123); check_hint("t1", 456);
2497 PL_hints |= HINT_INTEGER;
2498 store_hint("t0", 321);
11f9f0ed 2499 if (!(PL_hints & HINT_INTEGER)) croak_fail();
defdfed5
Z
2500 check_hint("t0", 321); check_hint("t1", 456);
2501 LEAVE;
11f9f0ed 2502 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2503 check_hint("t0", 123); check_hint("t1", 456);
2504 ENTER;
2505 SAVEHINTS();
11f9f0ed 2506 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2507 check_hint("t0", 123); check_hint("t1", 456);
2508 store_hint("t1", 654);
11f9f0ed 2509 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2510 check_hint("t0", 123); check_hint("t1", 654);
2511 LEAVE;
11f9f0ed 2512 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2513 check_hint("t0", 123); check_hint("t1", 456);
2514 LEAVE;
2515#undef store_hint
2516#undef hint_ok
2517#undef check_hint
2518
2519void
2520test_copyhints()
2521 PREINIT:
2522 HV *a, *b;
2523 CODE:
2524 PL_hints |= HINT_LOCALIZE_HH;
2525 ENTER;
2526 SAVEHINTS();
2527 sv_setiv_mg(*hv_fetchs(GvHV(PL_hintgv), "t0", 1), 123);
20439bc7
Z
2528 if (SvIV(cop_hints_fetch_pvs(&PL_compiling, "t0", 0)) != 123)
2529 croak_fail();
defdfed5
Z
2530 a = newHVhv(GvHV(PL_hintgv));
2531 sv_2mortal((SV*)a);
2532 sv_setiv_mg(*hv_fetchs(a, "t0", 1), 456);
20439bc7
Z
2533 if (SvIV(cop_hints_fetch_pvs(&PL_compiling, "t0", 0)) != 123)
2534 croak_fail();
defdfed5
Z
2535 b = hv_copy_hints_hv(a);
2536 sv_2mortal((SV*)b);
2537 sv_setiv_mg(*hv_fetchs(b, "t0", 1), 789);
20439bc7
Z
2538 if (SvIV(cop_hints_fetch_pvs(&PL_compiling, "t0", 0)) != 789)
2539 croak_fail();
defdfed5
Z
2540 LEAVE;
2541
201c7e1f 2542void
2fcb4757
Z
2543test_op_list()
2544 PREINIT:
2545 OP *a;
2546 CODE:
2547#define iv_op(iv) newSVOP(OP_CONST, 0, newSViv(iv))
2548#define check_op(o, expect) \
2549 do { \
2550 if (strcmp(test_op_list_describe(o), (expect))) \
2551 croak("fail %s %s", test_op_list_describe(o), (expect)); \
2552 } while(0)
2553 a = op_append_elem(OP_LIST, NULL, NULL);
2554 check_op(a, "");
2555 a = op_append_elem(OP_LIST, iv_op(1), a);
2556 check_op(a, "const(1).");
2557 a = op_append_elem(OP_LIST, NULL, a);
2558 check_op(a, "const(1).");
2559 a = op_append_elem(OP_LIST, a, iv_op(2));
2560 check_op(a, "list[pushmark.const(1).const(2).]");
2561 a = op_append_elem(OP_LIST, a, iv_op(3));
2562 check_op(a, "list[pushmark.const(1).const(2).const(3).]");
2563 a = op_append_elem(OP_LIST, a, NULL);
2564 check_op(a, "list[pushmark.const(1).const(2).const(3).]");
2565 a = op_append_elem(OP_LIST, NULL, a);
2566 check_op(a, "list[pushmark.const(1).const(2).const(3).]");
2567 a = op_append_elem(OP_LIST, iv_op(4), a);
2568 check_op(a, "list[pushmark.const(4)."
2569 "list[pushmark.const(1).const(2).const(3).]]");
2570 a = op_append_elem(OP_LIST, a, iv_op(5));
2571 check_op(a, "list[pushmark.const(4)."
2572 "list[pushmark.const(1).const(2).const(3).]const(5).]");
2573 a = op_append_elem(OP_LIST, a,
2574 op_append_elem(OP_LIST, iv_op(7), iv_op(6)));
2575 check_op(a, "list[pushmark.const(4)."
2576 "list[pushmark.const(1).const(2).const(3).]const(5)."
2577 "list[pushmark.const(7).const(6).]]");
2578 op_free(a);
2579 a = op_append_elem(OP_LINESEQ, iv_op(1), iv_op(2));
2580 check_op(a, "lineseq[const(1).const(2).]");
2581 a = op_append_elem(OP_LINESEQ, a, iv_op(3));
2582 check_op(a, "lineseq[const(1).const(2).const(3).]");
2583 op_free(a);
2584 a = op_append_elem(OP_LINESEQ,
2585 op_append_elem(OP_LIST, iv_op(1), iv_op(2)),
2586 iv_op(3));
2587 check_op(a, "lineseq[list[pushmark.const(1).const(2).]const(3).]");
2588 op_free(a);
2589 a = op_prepend_elem(OP_LIST, NULL, NULL);
2590 check_op(a, "");
2591 a = op_prepend_elem(OP_LIST, a, iv_op(1));
2592 check_op(a, "const(1).");
2593 a = op_prepend_elem(OP_LIST, a, NULL);
2594 check_op(a, "const(1).");
2595 a = op_prepend_elem(OP_LIST, iv_op(2), a);
2596 check_op(a, "list[pushmark.const(2).const(1).]");
2597 a = op_prepend_elem(OP_LIST, iv_op(3), a);
2598 check_op(a, "list[pushmark.const(3).const(2).const(1).]");
2599 a = op_prepend_elem(OP_LIST, NULL, a);
2600 check_op(a, "list[pushmark.const(3).const(2).const(1).]");
2601 a = op_prepend_elem(OP_LIST, a, NULL);
2602 check_op(a, "list[pushmark.const(3).const(2).const(1).]");
2603 a = op_prepend_elem(OP_LIST, a, iv_op(4));
2604 check_op(a, "list[pushmark."
2605 "list[pushmark.const(3).const(2).const(1).]const(4).]");
2606 a = op_prepend_elem(OP_LIST, iv_op(5), a);
2607 check_op(a, "list[pushmark.const(5)."
2608 "list[pushmark.const(3).const(2).const(1).]const(4).]");
2609 a = op_prepend_elem(OP_LIST,
2610 op_prepend_elem(OP_LIST, iv_op(6), iv_op(7)), a);
2611 check_op(a, "list[pushmark.list[pushmark.const(6).const(7).]const(5)."
2612 "list[pushmark.const(3).const(2).const(1).]const(4).]");
2613 op_free(a);
2614 a = op_prepend_elem(OP_LINESEQ, iv_op(2), iv_op(1));
2615 check_op(a, "lineseq[const(2).const(1).]");
2616 a = op_prepend_elem(OP_LINESEQ, iv_op(3), a);
2617 check_op(a, "lineseq[const(3).const(2).const(1).]");
2618 op_free(a);
2619 a = op_prepend_elem(OP_LINESEQ, iv_op(3),
2620 op_prepend_elem(OP_LIST, iv_op(2), iv_op(1)));
2621 check_op(a, "lineseq[const(3).list[pushmark.const(2).const(1).]]");
2622 op_free(a);
2623 a = op_append_list(OP_LINESEQ, NULL, NULL);
2624 check_op(a, "");
2625 a = op_append_list(OP_LINESEQ, iv_op(1), a);
2626 check_op(a, "const(1).");
2627 a = op_append_list(OP_LINESEQ, NULL, a);
2628 check_op(a, "const(1).");
2629 a = op_append_list(OP_LINESEQ, a, iv_op(2));
2630 check_op(a, "lineseq[const(1).const(2).]");
2631 a = op_append_list(OP_LINESEQ, a, iv_op(3));
2632 check_op(a, "lineseq[const(1).const(2).const(3).]");
2633 a = op_append_list(OP_LINESEQ, iv_op(4), a);
2634 check_op(a, "lineseq[const(4).const(1).const(2).const(3).]");
2635 a = op_append_list(OP_LINESEQ, a, NULL);
2636 check_op(a, "lineseq[const(4).const(1).const(2).const(3).]");
2637 a = op_append_list(OP_LINESEQ, NULL, a);
2638 check_op(a, "lineseq[const(4).const(1).const(2).const(3).]");
2639 a = op_append_list(OP_LINESEQ, a,
2640 op_append_list(OP_LINESEQ, iv_op(5), iv_op(6)));
2641 check_op(a, "lineseq[const(4).const(1).const(2).const(3)."
2642 "const(5).const(6).]");
2643 op_free(a);
2644 a = op_append_list(OP_LINESEQ,
2645 op_append_list(OP_LINESEQ, iv_op(1), iv_op(2)),
2646 op_append_list(OP_LIST, iv_op(3), iv_op(4)));
2647 check_op(a, "lineseq[const(1).const(2)."
2648 "list[pushmark.const(3).const(4).]]");
2649 op_free(a);
2650 a = op_append_list(OP_LINESEQ,
2651 op_append_list(OP_LIST, iv_op(1), iv_op(2)),
2652 op_append_list(OP_LINESEQ, iv_op(3), iv_op(4)));
2653 check_op(a, "lineseq[list[pushmark.const(1).const(2).]"
2654 "const(3).const(4).]");
2655 op_free(a);
2fcb4757
Z
2656#undef check_op
2657
2658void
5983a79d
BM
2659test_op_linklist ()
2660 PREINIT:
2661 OP *o;
2662 CODE:
2663#define check_ll(o, expect) \
2664 STMT_START { \
2665 if (strNE(test_op_linklist_describe(o), (expect))) \
2666 croak("fail %s %s", test_op_linklist_describe(o), (expect)); \
2667 } STMT_END
2668 o = iv_op(1);
2669 check_ll(o, ".const1");
2670 op_free(o);
2671
2672 o = mkUNOP(OP_NOT, iv_op(1));
2673 check_ll(o, ".const1.not");
2674 op_free(o);
2675
2676 o = mkUNOP(OP_NOT, mkUNOP(OP_NEGATE, iv_op(1)));
2677 check_ll(o, ".const1.negate.not");
2678 op_free(o);
2679
2680 o = mkBINOP(OP_ADD, iv_op(1), iv_op(2));
2681 check_ll(o, ".const1.const2.add");
2682 op_free(o);
2683
2684 o = mkBINOP(OP_ADD, mkUNOP(OP_NOT, iv_op(1)), iv_op(2));
2685 check_ll(o, ".const1.not.const2.add");
2686 op_free(o);
2687
2688 o = mkUNOP(OP_NOT, mkBINOP(OP_ADD, iv_op(1), iv_op(2)));
2689 check_ll(o, ".const1.const2.add.not");
2690 op_free(o);
2691
2692 o = mkLISTOP(OP_LINESEQ, iv_op(1), iv_op(2), iv_op(3));
2693 check_ll(o, ".const1.const2.const3.lineseq");
2694 op_free(o);
2695
2696 o = mkLISTOP(OP_LINESEQ,
2697 mkBINOP(OP_ADD, iv_op(1), iv_op(2)),
2698 mkUNOP(OP_NOT, iv_op(3)),
2699 mkLISTOP(OP_SUBSTR, iv_op(4), iv_op(5), iv_op(6)));
2700 check_ll(o, ".const1.const2.add.const3.not"
2701 ".const4.const5.const6.substr.lineseq");
2702 op_free(o);
2703
2704 o = mkBINOP(OP_ADD, iv_op(1), iv_op(2));
2705 LINKLIST(o);
2706 o = mkBINOP(OP_SUBTRACT, o, iv_op(3));
2707 check_ll(o, ".const1.const2.add.const3.subtract");
2708 op_free(o);
2709#undef check_ll
2710#undef iv_op
2711
2712void
201c7e1f
FR
2713peep_enable ()
2714 PREINIT:
2715 dMY_CXT;
2716 CODE:
2717 av_clear(MY_CXT.peep_recorder);
2718 av_clear(MY_CXT.rpeep_recorder);
2719 MY_CXT.peep_recording = 1;
2720
2721void
2722peep_disable ()
2723 PREINIT:
2724 dMY_CXT;
2725 CODE:
2726 MY_CXT.peep_recording = 0;
2727
2728SV *
2729peep_record ()
2730 PREINIT:
2731 dMY_CXT;
2732 CODE:
95d2461a 2733 RETVAL = newRV_inc((SV *)MY_CXT.peep_recorder);
201c7e1f
FR
2734 OUTPUT:
2735 RETVAL
2736
2737SV *
2738rpeep_record ()
2739 PREINIT:
2740 dMY_CXT;
2741 CODE:
95d2461a 2742 RETVAL = newRV_inc((SV *)MY_CXT.rpeep_recorder);
201c7e1f
FR
2743 OUTPUT:
2744 RETVAL
2745
9c540340
DM
2746=pod
2747
2748multicall_each: call a sub for each item in the list. Used to test MULTICALL
2749
2750=cut
2751
2752void
2753multicall_each(block,...)
2754 SV * block
2755PROTOTYPE: &@
2756CODE:
2757{
2758 dMULTICALL;
2759 int index;
2760 GV *gv;
2761 HV *stash;
2762 I32 gimme = G_SCALAR;
2763 SV **args = &PL_stack_base[ax];
2764 CV *cv;
2765
2766 if(items <= 1) {
2767 XSRETURN_UNDEF;
2768 }
2769 cv = sv_2cv(block, &stash, &gv, 0);
2770 if (cv == Nullcv) {
2771 croak("multicall_each: not a subroutine reference");
2772 }
2773 PUSH_MULTICALL(cv);
2774 SAVESPTR(GvSV(PL_defgv));
2775
2776 for(index = 1 ; index < items ; index++) {
2777 GvSV(PL_defgv) = args[index];
2778 MULTICALL;
2779 }
2780 POP_MULTICALL;
2781 XSRETURN_UNDEF;
2782}
2783
7b81e549
GG
2784#ifdef USE_ITHREADS
2785
2786void
2787clone_with_stack()
2788CODE:
2789{
2790 PerlInterpreter *interp = aTHX; /* The original interpreter */
2791 PerlInterpreter *interp_dup; /* The duplicate interpreter */
2792 int oldscope = 1; /* We are responsible for all scopes */
2793
2794 interp_dup = perl_clone(interp, CLONEf_COPY_STACKS | CLONEf_CLONE_HOST );
2795
2796 /* destroy old perl */
2797 PERL_SET_CONTEXT(interp);
2798
2799 POPSTACK_TO(PL_mainstack);
2800 dounwind(-1);
2801 LEAVE_SCOPE(0);
2802
2803 while (interp->Iscopestack_ix > 1)
2804 LEAVE;
2805 FREETMPS;
2806
2807 perl_destruct(interp);
2808 perl_free(interp);
2809
2810 /* switch to new perl */
2811 PERL_SET_CONTEXT(interp_dup);
2812
2813 /* continue after 'clone_with_stack' */
2814 interp_dup->Iop = interp_dup->Iop->op_next;
2815
2816 /* run with new perl */
2817 Perl_runops_standard(interp_dup);
2818
2819 /* We may have additional unclosed scopes if fork() was called
2820 * from within a BEGIN block. See perlfork.pod for more details.
2821 * We cannot clean up these other scopes because they belong to a
2822 * different interpreter, but we also cannot leave PL_scopestack_ix
2823 * dangling because that can trigger an assertion in perl_destruct().
2824 */
2825 if (PL_scopestack_ix > oldscope) {
2826 PL_scopestack[oldscope-1] = PL_scopestack[PL_scopestack_ix-1];
2827 PL_scopestack_ix = oldscope;
2828 }
2829
2830 perl_destruct(interp_dup);
2831 perl_free(interp_dup);
2832
2833 /* call the real 'exit' not PerlProc_exit */
2834#undef exit
2835 exit(0);
2836}
2837
2838#endif /* USE_ITHREDS */
9c540340 2839
88b5a879 2840SV*
2841take_svref(SVREF sv)
2842CODE:
2843 RETVAL = newRV_inc(sv);
2844OUTPUT:
2845 RETVAL
2846
2847SV*
2848take_avref(AV* av)
2849CODE:
2850 RETVAL = newRV_inc((SV*)av);
2851OUTPUT:
2852 RETVAL
2853
2854SV*
2855take_hvref(HV* hv)
2856CODE:
2857 RETVAL = newRV_inc((SV*)hv);
2858OUTPUT:
2859 RETVAL
2860
2861
2862SV*
2863take_cvref(CV* cv)
2864CODE:
2865 RETVAL = newRV_inc((SV*)cv);
2866OUTPUT:
2867 RETVAL
2868
2869
e2fe06dd
EB
2870BOOT:
2871 {
2872 HV* stash;
2873 SV** meth = NULL;
2874 CV* cv;
2875 stash = gv_stashpv("XS::APItest::TempLv", 0);
2876 if (stash)
2877 meth = hv_fetchs(stash, "make_temp_mg_lv", 0);
2878 if (!meth)
2879 croak("lost method 'make_temp_mg_lv'");
2880 cv = GvCV(*meth);
2881 CvLVALUE_on(cv);
2882 }
83f8bb56
Z
2883
2884BOOT:
2885{
2886 hintkey_rpn_sv = newSVpvs_share("XS::APItest/rpn");
2887 hintkey_calcrpn_sv = newSVpvs_share("XS::APItest/calcrpn");
2888 hintkey_stufftest_sv = newSVpvs_share("XS::APItest/stufftest");
2889 hintkey_swaptwostmts_sv = newSVpvs_share("XS::APItest/swaptwostmts");
07ffcb73 2890 hintkey_looprest_sv = newSVpvs_share("XS::APItest/looprest");
a7aaec61 2891 hintkey_scopelessblock_sv = newSVpvs_share("XS::APItest/scopelessblock");
9eb5c532
Z
2892 hintkey_stmtasexpr_sv = newSVpvs_share("XS::APItest/stmtasexpr");
2893 hintkey_stmtsasexpr_sv = newSVpvs_share("XS::APItest/stmtsasexpr");
e53d8f76
Z
2894 hintkey_loopblock_sv = newSVpvs_share("XS::APItest/loopblock");
2895 hintkey_blockasexpr_sv = newSVpvs_share("XS::APItest/blockasexpr");
361d9b55
Z
2896 hintkey_swaplabel_sv = newSVpvs_share("XS::APItest/swaplabel");
2897 hintkey_labelconst_sv = newSVpvs_share("XS::APItest/labelconst");
78cdf107
Z
2898 hintkey_arrayfullexpr_sv = newSVpvs_share("XS::APItest/arrayfullexpr");
2899 hintkey_arraylistexpr_sv = newSVpvs_share("XS::APItest/arraylistexpr");
2900 hintkey_arraytermexpr_sv = newSVpvs_share("XS::APItest/arraytermexpr");
2901 hintkey_arrayarithexpr_sv = newSVpvs_share("XS::APItest/arrayarithexpr");
2902 hintkey_arrayexprflags_sv = newSVpvs_share("XS::APItest/arrayexprflags");
83f8bb56
Z
2903 next_keyword_plugin = PL_keyword_plugin;
2904 PL_keyword_plugin = my_keyword_plugin;
2905}
8f89e5a9
Z
2906
2907void
2908establish_cleanup(...)
2909PROTOTYPE: $
2910CODE:
c33e8be1 2911 PERL_UNUSED_VAR(items);
8f89e5a9
Z
2912 croak("establish_cleanup called as a function");
2913
2914BOOT:
2915{
2916 CV *estcv = get_cv("XS::APItest::establish_cleanup", 0);
2917 cv_set_call_checker(estcv, THX_ck_entersub_establish_cleanup, (SV*)estcv);
2918}
3ad73efd
Z
2919
2920void
2921postinc(...)
2922PROTOTYPE: $
2923CODE:
c33e8be1 2924 PERL_UNUSED_VAR(items);
3ad73efd
Z
2925 croak("postinc called as a function");
2926
27fcb6ee
FC
2927void
2928filter()
2929CODE:
2930 filter_add(filter_call, NULL);
2931
3ad73efd
Z
2932BOOT:
2933{
2934 CV *asscv = get_cv("XS::APItest::postinc", 0);
2935 cv_set_call_checker(asscv, THX_ck_entersub_postinc, (SV*)asscv);
2936}
26ab20ee 2937
fba8e77b
FC
2938SV *
2939lv_temp_object()
2940CODE:
2941 RETVAL =
2942 sv_bless(
2943 newRV_noinc(newSV(0)),
2944 gv_stashpvs("XS::APItest::TempObj",GV_ADD)
2945 ); /* Package defined in test script */
2946OUTPUT:
2947 RETVAL
2948
7d6175ef
FC
2949void
2950fill_hash_with_nulls(HV *hv)
a2bf5ab1 2951PREINIT:
7d6175ef 2952 UV i = 0;
a2bf5ab1 2953CODE:
7d6175ef
FC
2954 for(; i < 1000; ++i) {
2955 HE *entry = hv_fetch_ent(hv, sv_2mortal(newSVuv(i)), 1, 0);
2956 SvREFCNT_dec(HeVAL(entry));
2957 HeVAL(entry) = NULL;
2958 }
2959
fa819c1c
FC
2960bool
2961SvIsCOW(SV *sv)
2962CODE:
2963 RETVAL = SvIsCOW(sv);
2964OUTPUT:
2965 RETVAL
2966
15103811
Z
2967void
2968pad_scalar(...)
2969PROTOTYPE: $$
2970CODE:
2971 PERL_UNUSED_VAR(items);
2972 croak("pad_scalar called as a function");
2973
2974BOOT:
2975{
2976 CV *pscv = get_cv("XS::APItest::pad_scalar", 0);
2977 cv_set_call_checker(pscv, THX_ck_entersub_pad_scalar, (SV*)pscv);
2978}
2979
ab8f91e9
BF
2980SV*
2981fetch_pad_names( cv )
2982CV* cv
2983 PREINIT:
2984 I32 i;
2985 AV *pad_namelist;
2986 AV *retav = newAV();
2987 CODE:
2988 pad_namelist = (AV*) *av_fetch(CvPADLIST(cv), 0, FALSE);
2989
2990 for ( i = av_len(pad_namelist); i >= 0; i-- ) {
2991 SV** name_ptr = av_fetch(pad_namelist, i, 0);
2992
2993 if (name_ptr && SvPOKp(*name_ptr)) {
2994 av_push(retav, newSVsv(*name_ptr));
2995 }
2996 }
2997 RETVAL = newRV_noinc((SV*)retav);
2998 OUTPUT:
2999 RETVAL
3000
c12735bb
Z
3001STRLEN
3002underscore_length()
3003PROTOTYPE:
3004PREINIT:
3005 SV *u;
3006 U8 *pv;
3007 STRLEN bytelen;
3008CODE:
3009 u = find_rundefsv();
3010 pv = (U8*)SvPV(u, bytelen);
3011 RETVAL = SvUTF8(u) ? utf8_length(pv, pv+bytelen) : bytelen;
3012OUTPUT:
3013 RETVAL
3014
e8ed61c5
FC
3015void
3016stringify(SV *sv)
3017PREINIT:
3018 const char *pv;
3019CODE:
3020 pv = SvPV_nolen(sv);
3021
3022
26ab20ee
FR
3023MODULE = XS::APItest PACKAGE = XS::APItest::Magic
3024
3025PROTOTYPES: DISABLE
3026
3027void
3028sv_magic_foo(SV *sv, SV *thingy)
3029ALIAS:
3030 sv_magic_bar = 1
3031CODE:
3032 sv_magicext(SvRV(sv), NULL, PERL_MAGIC_ext, ix ? &vtbl_bar : &vtbl_foo, (const char *)thingy, 0);
3033
3034SV *
3035mg_find_foo(SV *sv)
3036ALIAS:
3037 mg_find_bar = 1
3038CODE:
3039 MAGIC *mg = mg_findext(SvRV(sv), PERL_MAGIC_ext, ix ? &vtbl_bar : &vtbl_foo);
3040 RETVAL = mg ? SvREFCNT_inc((SV *)mg->mg_ptr) : &PL_sv_undef;
3041OUTPUT:
3042 RETVAL
3043
3044void
3045sv_unmagic_foo(SV *sv)
3046ALIAS:
3047 sv_unmagic_bar = 1
3048CODE:
3049 sv_unmagicext(SvRV(sv), PERL_MAGIC_ext, ix ? &vtbl_bar : &vtbl_foo);
406700cc
NC
3050
3051UV
3052test_get_vtbl()
3053 PREINIT:
3054 MGVTBL *have;
3055 MGVTBL *want;
3056 CODE:
3057#define test_get_this_vtable(name) \
3058 want = CAT2(&PL_vtbl_, name); \
3059 have = get_vtbl(CAT2(want_vtbl_, name)); \
3060 if (have != want) \
3061 croak("fail %p!=%p for get_vtbl(want_vtbl_" STRINGIFY(name) ") at " __FILE__ " line %d", have, want, __LINE__)
3062
3063 test_get_this_vtable(sv);
3064 test_get_this_vtable(env);
3065 test_get_this_vtable(envelem);
3066 test_get_this_vtable(sigelem);
3067 test_get_this_vtable(pack);
3068 test_get_this_vtable(packelem);
3069 test_get_this_vtable(dbline);
3070 test_get_this_vtable(isa);
3071 test_get_this_vtable(isaelem);
3072 test_get_this_vtable(arylen);
3073 test_get_this_vtable(mglob);
3074 test_get_this_vtable(nkeys);
3075 test_get_this_vtable(taint);
3076 test_get_this_vtable(substr);
3077 test_get_this_vtable(vec);
3078 test_get_this_vtable(pos);
3079 test_get_this_vtable(bm);
3080 test_get_this_vtable(fm);
3081 test_get_this_vtable(uvar);
3082 test_get_this_vtable(defelem);
3083 test_get_this_vtable(regexp);
3084 test_get_this_vtable(regdata);
3085 test_get_this_vtable(regdatum);
3086#ifdef USE_LOCALE_COLLATE
3087 test_get_this_vtable(collxfrm);
3088#endif
3089 test_get_this_vtable(amagic);
3090 test_get_this_vtable(amagicelem);
3091 test_get_this_vtable(backref);
3092 test_get_this_vtable(utf8);
3093
3094 RETVAL = PTR2UV(get_vtbl(-1));
3095 OUTPUT:
3096 RETVAL