This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
test various types of SVs with call_sv
[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
5aaab254 81typedef void (freeent_function)(pTHX_ HV *, HE *);
2dc92170
NC
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;
bd70aaaf
KW
151 UV chr = utf8_to_uvchr_buf((U8 *)p, (U8 *) end, &len);
152 new_p = (char *)uvchr_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);
5db1eb8d
BF
894 return newSTATEOP(label ? SvUTF8(label) : 0,
895 label ? savepv(SvPVX(label)) : NULL,
896 sop);
361d9b55
Z
897}
898
899#define parse_keyword_labelconst() THX_parse_keyword_labelconst(aTHX)
900static OP *THX_parse_keyword_labelconst(pTHX)
901{
902 return newSVOP(OP_CONST, 0, parse_label(0));
903}
904
78cdf107
Z
905#define parse_keyword_arrayfullexpr() THX_parse_keyword_arrayfullexpr(aTHX)
906static OP *THX_parse_keyword_arrayfullexpr(pTHX)
907{
908 return newANONLIST(parse_fullexpr(0));
909}
910
911#define parse_keyword_arraylistexpr() THX_parse_keyword_arraylistexpr(aTHX)
912static OP *THX_parse_keyword_arraylistexpr(pTHX)
913{
914 return newANONLIST(parse_listexpr(0));
915}
916
917#define parse_keyword_arraytermexpr() THX_parse_keyword_arraytermexpr(aTHX)
918static OP *THX_parse_keyword_arraytermexpr(pTHX)
919{
920 return newANONLIST(parse_termexpr(0));
921}
922
923#define parse_keyword_arrayarithexpr() THX_parse_keyword_arrayarithexpr(aTHX)
924static OP *THX_parse_keyword_arrayarithexpr(pTHX)
925{
926 return newANONLIST(parse_arithexpr(0));
927}
928
929#define parse_keyword_arrayexprflags() THX_parse_keyword_arrayexprflags(aTHX)
930static OP *THX_parse_keyword_arrayexprflags(pTHX)
931{
932 U32 flags = 0;
933 I32 c;
934 OP *o;
935 lex_read_space(0);
936 c = lex_peek_unichar(0);
937 if (c != '!' && c != '?') croak("syntax error");
938 lex_read_unichar(0);
939 if (c == '?') flags |= PARSE_OPTIONAL;
940 o = parse_listexpr(flags);
941 return o ? newANONLIST(o) : newANONHASH(newOP(OP_STUB, 0));
942}
943
83f8bb56
Z
944/* plugin glue */
945
946#define keyword_active(hintkey_sv) THX_keyword_active(aTHX_ hintkey_sv)
947static int THX_keyword_active(pTHX_ SV *hintkey_sv)
948{
949 HE *he;
950 if(!GvHV(PL_hintgv)) return 0;
951 he = hv_fetch_ent(GvHV(PL_hintgv), hintkey_sv, 0,
952 SvSHARED_HASH(hintkey_sv));
953 return he && SvTRUE(HeVAL(he));
954}
955
956static int my_keyword_plugin(pTHX_
957 char *keyword_ptr, STRLEN keyword_len, OP **op_ptr)
958{
959 if(keyword_len == 3 && strnEQ(keyword_ptr, "rpn", 3) &&
960 keyword_active(hintkey_rpn_sv)) {
961 *op_ptr = parse_keyword_rpn();
962 return KEYWORD_PLUGIN_EXPR;
963 } else if(keyword_len == 7 && strnEQ(keyword_ptr, "calcrpn", 7) &&
964 keyword_active(hintkey_calcrpn_sv)) {
965 *op_ptr = parse_keyword_calcrpn();
966 return KEYWORD_PLUGIN_STMT;
967 } else if(keyword_len == 9 && strnEQ(keyword_ptr, "stufftest", 9) &&
968 keyword_active(hintkey_stufftest_sv)) {
969 *op_ptr = parse_keyword_stufftest();
970 return KEYWORD_PLUGIN_STMT;
971 } else if(keyword_len == 12 &&
972 strnEQ(keyword_ptr, "swaptwostmts", 12) &&
973 keyword_active(hintkey_swaptwostmts_sv)) {
974 *op_ptr = parse_keyword_swaptwostmts();
975 return KEYWORD_PLUGIN_STMT;
07ffcb73
Z
976 } else if(keyword_len == 8 && strnEQ(keyword_ptr, "looprest", 8) &&
977 keyword_active(hintkey_looprest_sv)) {
978 *op_ptr = parse_keyword_looprest();
979 return KEYWORD_PLUGIN_STMT;
a7aaec61
Z
980 } else if(keyword_len == 14 && strnEQ(keyword_ptr, "scopelessblock", 14) &&
981 keyword_active(hintkey_scopelessblock_sv)) {
982 *op_ptr = parse_keyword_scopelessblock();
983 return KEYWORD_PLUGIN_STMT;
9eb5c532
Z
984 } else if(keyword_len == 10 && strnEQ(keyword_ptr, "stmtasexpr", 10) &&
985 keyword_active(hintkey_stmtasexpr_sv)) {
986 *op_ptr = parse_keyword_stmtasexpr();
987 return KEYWORD_PLUGIN_EXPR;
988 } else if(keyword_len == 11 && strnEQ(keyword_ptr, "stmtsasexpr", 11) &&
989 keyword_active(hintkey_stmtsasexpr_sv)) {
990 *op_ptr = parse_keyword_stmtsasexpr();
991 return KEYWORD_PLUGIN_EXPR;
e53d8f76
Z
992 } else if(keyword_len == 9 && strnEQ(keyword_ptr, "loopblock", 9) &&
993 keyword_active(hintkey_loopblock_sv)) {
994 *op_ptr = parse_keyword_loopblock();
995 return KEYWORD_PLUGIN_STMT;
996 } else if(keyword_len == 11 && strnEQ(keyword_ptr, "blockasexpr", 11) &&
997 keyword_active(hintkey_blockasexpr_sv)) {
998 *op_ptr = parse_keyword_blockasexpr();
999 return KEYWORD_PLUGIN_EXPR;
361d9b55
Z
1000 } else if(keyword_len == 9 && strnEQ(keyword_ptr, "swaplabel", 9) &&
1001 keyword_active(hintkey_swaplabel_sv)) {
1002 *op_ptr = parse_keyword_swaplabel();
1003 return KEYWORD_PLUGIN_STMT;
1004 } else if(keyword_len == 10 && strnEQ(keyword_ptr, "labelconst", 10) &&
1005 keyword_active(hintkey_labelconst_sv)) {
1006 *op_ptr = parse_keyword_labelconst();
1007 return KEYWORD_PLUGIN_EXPR;
78cdf107
Z
1008 } else if(keyword_len == 13 && strnEQ(keyword_ptr, "arrayfullexpr", 13) &&
1009 keyword_active(hintkey_arrayfullexpr_sv)) {
1010 *op_ptr = parse_keyword_arrayfullexpr();
1011 return KEYWORD_PLUGIN_EXPR;
1012 } else if(keyword_len == 13 && strnEQ(keyword_ptr, "arraylistexpr", 13) &&
1013 keyword_active(hintkey_arraylistexpr_sv)) {
1014 *op_ptr = parse_keyword_arraylistexpr();
1015 return KEYWORD_PLUGIN_EXPR;
1016 } else if(keyword_len == 13 && strnEQ(keyword_ptr, "arraytermexpr", 13) &&
1017 keyword_active(hintkey_arraytermexpr_sv)) {
1018 *op_ptr = parse_keyword_arraytermexpr();
1019 return KEYWORD_PLUGIN_EXPR;
1020 } else if(keyword_len == 14 && strnEQ(keyword_ptr, "arrayarithexpr", 14) &&
1021 keyword_active(hintkey_arrayarithexpr_sv)) {
1022 *op_ptr = parse_keyword_arrayarithexpr();
1023 return KEYWORD_PLUGIN_EXPR;
1024 } else if(keyword_len == 14 && strnEQ(keyword_ptr, "arrayexprflags", 14) &&
1025 keyword_active(hintkey_arrayexprflags_sv)) {
1026 *op_ptr = parse_keyword_arrayexprflags();
1027 return KEYWORD_PLUGIN_EXPR;
83f8bb56
Z
1028 } else {
1029 return next_keyword_plugin(aTHX_ keyword_ptr, keyword_len, op_ptr);
1030 }
1031}
1032
f568d64d
BM
1033static XOP my_xop;
1034
1035static OP *
1036pp_xop(pTHX)
1037{
1038 return PL_op->op_next;
1039}
1040
1041static void
1042peep_xop(pTHX_ OP *o, OP *oldop)
1043{
1044 dMY_CXT;
b58757d5
BM
1045 av_push(MY_CXT.xop_record, newSVpvf("peep:%"UVxf, PTR2UV(o)));
1046 av_push(MY_CXT.xop_record, newSVpvf("oldop:%"UVxf, PTR2UV(oldop)));
f568d64d
BM
1047}
1048
27fcb6ee
FC
1049static I32
1050filter_call(pTHX_ int idx, SV *buf_sv, int maxlen)
1051{
27fcb6ee
FC
1052 char *p;
1053 char *end;
1054 int n = FILTER_READ(idx + 1, buf_sv, maxlen);
1055
1056 if (n<=0) return n;
1057
1058 p = SvPV_force_nolen(buf_sv);
1059 end = p + SvCUR(buf_sv);
1060 while (p < end) {
1061 if (*p == 'o') *p = 'e';
1062 p++;
1063 }
1064 return SvCUR(buf_sv);
1065}
1066
02cab674
FC
1067static AV *
1068myget_linear_isa(pTHX_ HV *stash, U32 level) {
02cab674 1069 GV **gvp = (GV **)hv_fetchs(stash, "ISA", 0);
6dbddf4a 1070 PERL_UNUSED_ARG(level);
02cab674
FC
1071 return gvp && *gvp && GvAV(*gvp)
1072 ? GvAV(*gvp)
1073 : (AV *)sv_2mortal((SV *)newAV());
1074}
1075
27fcb6ee 1076
9777c6aa
S
1077XS_EXTERNAL(XS_XS__APItest__XSUB_XS_VERSION_undef);
1078XS_EXTERNAL(XS_XS__APItest__XSUB_XS_VERSION_empty);
1079XS_EXTERNAL(XS_XS__APItest__XSUB_XS_APIVERSION_invalid);
7b20c7cd 1080
02cab674
FC
1081static struct mro_alg mymro;
1082
e8570548
Z
1083static Perl_check_t addissub_nxck_add;
1084
1085static OP *
1086addissub_myck_add(pTHX_ OP *op)
1087{
1088 SV **flag_svp = hv_fetchs(GvHV(PL_hintgv), "XS::APItest/addissub", 0);
1089 OP *aop, *bop;
1090 U8 flags;
1091 if (!(flag_svp && SvTRUE(*flag_svp) && (op->op_flags & OPf_KIDS) &&
1092 (aop = cBINOPx(op)->op_first) && (bop = aop->op_sibling) &&
1093 !bop->op_sibling))
1094 return addissub_nxck_add(aTHX_ op);
1095 aop->op_sibling = NULL;
1096 cBINOPx(op)->op_first = NULL;
1097 op->op_flags &= ~OPf_KIDS;
1098 flags = op->op_flags;
1099 op_free(op);
1100 return newBINOP(OP_SUBTRACT, flags, aop, bop);
1101}
02cab674 1102
39c012bc
FC
1103static Perl_check_t old_ck_rv2cv;
1104
1105static OP *
1106my_ck_rv2cv(pTHX_ OP *o)
1107{
1108 SV *ref;
1109 SV **flag_svp = hv_fetchs(GvHV(PL_hintgv), "XS::APItest/addunder", 0);
1110 OP *aop;
1111
1112 if (flag_svp && SvTRUE(*flag_svp) && (o->op_flags & OPf_KIDS)
1113 && (aop = cUNOPx(o)->op_first) && aop->op_type == OP_CONST
1114 && aop->op_private & (OPpCONST_ENTERED|OPpCONST_BARE)
1115 && (ref = cSVOPx(aop)->op_sv) && SvPOK(ref) && SvCUR(ref)
1116 && *(SvEND(ref)-1) == 'o')
1117 {
1118 SvGROW(ref, SvCUR(ref)+2);
1119 *SvEND(ref) = '_';
1120 SvCUR(ref)++;
1121 *SvEND(ref) = '\0';
1122 }
1123 return old_ck_rv2cv(aTHX_ o);
1124}
1125
55289a74
NC
1126#include "const-c.inc"
1127
ffe53d21 1128MODULE = XS::APItest PACKAGE = XS::APItest
0314122a 1129
55289a74
NC
1130INCLUDE: const-xs.inc
1131
ffe53d21
NC
1132INCLUDE: numeric.xs
1133
fed3ba5d
NC
1134MODULE = XS::APItest::utf8 PACKAGE = XS::APItest::utf8
1135
1136int
1137bytes_cmp_utf8(bytes, utf8)
1138 SV *bytes
1139 SV *utf8
1140 PREINIT:
1141 const U8 *b;
1142 STRLEN blen;
1143 const U8 *u;
1144 STRLEN ulen;
1145 CODE:
1146 b = (const U8 *)SvPVbyte(bytes, blen);
1147 u = (const U8 *)SvPVbyte(utf8, ulen);
1148 RETVAL = bytes_cmp_utf8(b, blen, u, ulen);
1149 OUTPUT:
1150 RETVAL
1151
eb83ed87 1152AV *
bd70aaaf 1153test_utf8n_to_uvchr(s, len, flags)
eb83ed87
KW
1154
1155 SV *s
1156 SV *len
1157 SV *flags
1158 PREINIT:
1159 STRLEN retlen;
1160 UV ret;
1161 STRLEN slen;
1162
1163 CODE:
bd70aaaf 1164 /* Call utf8n_to_uvchr() with the inputs. It always asks for the
eb83ed87
KW
1165 * actual length to be returned
1166 *
1167 * Length to assume <s> is; not checked, so could have buffer overflow
1168 */
1169 RETVAL = newAV();
1170 sv_2mortal((SV*)RETVAL);
1171
1172 ret
bd70aaaf 1173 = utf8n_to_uvchr((U8*) SvPV(s, slen), SvUV(len), &retlen, SvUV(flags));
eb83ed87
KW
1174
1175 /* Returns the return value in [0]; <retlen> in [1] */
1176 av_push(RETVAL, newSVuv(ret));
1177 if (retlen == (STRLEN) -1) {
1178 av_push(RETVAL, newSViv(-1));
1179 }
1180 else {
1181 av_push(RETVAL, newSVuv(retlen));
1182 }
1183
1184 OUTPUT:
1185 RETVAL
1186
7d255dc8
NC
1187MODULE = XS::APItest:Overload PACKAGE = XS::APItest::Overload
1188
c33e8be1 1189void
25a9ffce 1190amagic_deref_call(sv, what)
7d255dc8
NC
1191 SV *sv
1192 int what
1193 PPCODE:
7d255dc8 1194 /* The reference is owned by something else. */
25a9ffce 1195 PUSHs(amagic_deref_call(sv, what));
7d255dc8 1196
e89bfaa6
NC
1197# I'd certainly like to discourage the use of this macro, given that we now
1198# have amagic_deref_call
1199
c33e8be1 1200void
e89bfaa6
NC
1201tryAMAGICunDEREF_var(sv, what)
1202 SV *sv
1203 int what
1204 PPCODE:
1205 {
1206 SV **sp = &sv;
1207 switch(what) {
1208 case to_av_amg:
1209 tryAMAGICunDEREF(to_av);
1210 break;
1211 case to_cv_amg:
1212 tryAMAGICunDEREF(to_cv);
1213 break;
1214 case to_gv_amg:
1215 tryAMAGICunDEREF(to_gv);
1216 break;
1217 case to_hv_amg:
1218 tryAMAGICunDEREF(to_hv);
1219 break;
1220 case to_sv_amg:
1221 tryAMAGICunDEREF(to_sv);
1222 break;
1223 default:
1224 croak("Invalid value %d passed to tryAMAGICunDEREF_var", what);
1225 }
1226 }
1227 /* The reference is owned by something else. */
1228 PUSHs(sv);
1229
7b20c7cd
NC
1230MODULE = XS::APItest PACKAGE = XS::APItest::XSUB
1231
1232BOOT:
1233 newXS("XS::APItest::XSUB::XS_VERSION_undef", XS_XS__APItest__XSUB_XS_VERSION_undef, __FILE__);
f9cc56fa 1234 newXS("XS::APItest::XSUB::XS_VERSION_empty", XS_XS__APItest__XSUB_XS_VERSION_empty, __FILE__);
88c4b02d 1235 newXS("XS::APItest::XSUB::XS_APIVERSION_invalid", XS_XS__APItest__XSUB_XS_APIVERSION_invalid, __FILE__);
7b20c7cd
NC
1236
1237void
1238XS_VERSION_defined(...)
1239 PPCODE:
1240 XS_VERSION_BOOTCHECK;
1241 XSRETURN_EMPTY;
1242
88c4b02d
NC
1243void
1244XS_APIVERSION_valid(...)
1245 PPCODE:
1246 XS_APIVERSION_BOOTCHECK;
1247 XSRETURN_EMPTY;
1248
ffe53d21
NC
1249MODULE = XS::APItest:Hash PACKAGE = XS::APItest::Hash
1250
b54b4831
NC
1251void
1252rot13_hash(hash)
1253 HV *hash
1254 CODE:
1255 {
1256 struct ufuncs uf;
1257 uf.uf_val = rot13_key;
1258 uf.uf_set = 0;
1259 uf.uf_index = 0;
1260
1261 sv_magic((SV*)hash, NULL, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
1262 }
1263
53c40a8f
NC
1264void
1265bitflip_hash(hash)
1266 HV *hash
1267 CODE:
1268 {
1269 struct ufuncs uf;
1270 uf.uf_val = bitflip_key;
1271 uf.uf_set = 0;
1272 uf.uf_index = 0;
1273
1274 sv_magic((SV*)hash, NULL, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
1275 }
1276
028f8eaa
MHM
1277#define UTF8KLEN(sv, len) (SvUTF8(sv) ? -(I32)len : (I32)len)
1278
0314122a
NC
1279bool
1280exists(hash, key_sv)
1281 PREINIT:
1282 STRLEN len;
1283 const char *key;
1284 INPUT:
1285 HV *hash
1286 SV *key_sv
1287 CODE:
1288 key = SvPV(key_sv, len);
028f8eaa 1289 RETVAL = hv_exists(hash, key, UTF8KLEN(key_sv, len));
0314122a
NC
1290 OUTPUT:
1291 RETVAL
1292
bdee33e4
NC
1293bool
1294exists_ent(hash, key_sv)
1295 PREINIT:
1296 INPUT:
1297 HV *hash
1298 SV *key_sv
1299 CODE:
1300 RETVAL = hv_exists_ent(hash, key_sv, 0);
1301 OUTPUT:
1302 RETVAL
1303
b60cf05a 1304SV *
55289a74 1305delete(hash, key_sv, flags = 0)
b60cf05a
NC
1306 PREINIT:
1307 STRLEN len;
1308 const char *key;
1309 INPUT:
1310 HV *hash
1311 SV *key_sv
55289a74 1312 I32 flags;
b60cf05a
NC
1313 CODE:
1314 key = SvPV(key_sv, len);
1315 /* It's already mortal, so need to increase reference count. */
55289a74
NC
1316 RETVAL
1317 = SvREFCNT_inc(hv_delete(hash, key, UTF8KLEN(key_sv, len), flags));
1318 OUTPUT:
1319 RETVAL
1320
1321SV *
1322delete_ent(hash, key_sv, flags = 0)
1323 INPUT:
1324 HV *hash
1325 SV *key_sv
1326 I32 flags;
1327 CODE:
1328 /* It's already mortal, so need to increase reference count. */
1329 RETVAL = SvREFCNT_inc(hv_delete_ent(hash, key_sv, flags, 0));
b60cf05a
NC
1330 OUTPUT:
1331 RETVAL
1332
1333SV *
858117f8
NC
1334store_ent(hash, key, value)
1335 PREINIT:
1336 SV *copy;
1337 HE *result;
1338 INPUT:
1339 HV *hash
1340 SV *key
1341 SV *value
1342 CODE:
1343 copy = newSV(0);
1344 result = hv_store_ent(hash, key, copy, 0);
1345 SvSetMagicSV(copy, value);
1346 if (!result) {
1347 SvREFCNT_dec(copy);
1348 XSRETURN_EMPTY;
1349 }
1350 /* It's about to become mortal, so need to increase reference count.
1351 */
1352 RETVAL = SvREFCNT_inc(HeVAL(result));
1353 OUTPUT:
1354 RETVAL
1355
858117f8 1356SV *
b60cf05a
NC
1357store(hash, key_sv, value)
1358 PREINIT:
1359 STRLEN len;
1360 const char *key;
1361 SV *copy;
1362 SV **result;
1363 INPUT:
1364 HV *hash
1365 SV *key_sv
1366 SV *value
1367 CODE:
1368 key = SvPV(key_sv, len);
1369 copy = newSV(0);
028f8eaa 1370 result = hv_store(hash, key, UTF8KLEN(key_sv, len), copy, 0);
858117f8 1371 SvSetMagicSV(copy, value);
b60cf05a
NC
1372 if (!result) {
1373 SvREFCNT_dec(copy);
1374 XSRETURN_EMPTY;
1375 }
1376 /* It's about to become mortal, so need to increase reference count.
1377 */
1378 RETVAL = SvREFCNT_inc(*result);
1379 OUTPUT:
1380 RETVAL
1381
bdee33e4
NC
1382SV *
1383fetch_ent(hash, key_sv)
1384 PREINIT:
1385 HE *result;
1386 INPUT:
1387 HV *hash
1388 SV *key_sv
1389 CODE:
1390 result = hv_fetch_ent(hash, key_sv, 0, 0);
1391 if (!result) {
1392 XSRETURN_EMPTY;
1393 }
1394 /* Force mg_get */
1395 RETVAL = newSVsv(HeVAL(result));
1396 OUTPUT:
1397 RETVAL
b60cf05a
NC
1398
1399SV *
1400fetch(hash, key_sv)
1401 PREINIT:
1402 STRLEN len;
1403 const char *key;
1404 SV **result;
1405 INPUT:
1406 HV *hash
1407 SV *key_sv
1408 CODE:
1409 key = SvPV(key_sv, len);
028f8eaa 1410 result = hv_fetch(hash, key, UTF8KLEN(key_sv, len), 0);
b60cf05a
NC
1411 if (!result) {
1412 XSRETURN_EMPTY;
1413 }
1414 /* Force mg_get */
1415 RETVAL = newSVsv(*result);
1416 OUTPUT:
1417 RETVAL
2dc92170 1418
9568a123
NC
1419#if defined (hv_common)
1420
6b4de907
NC
1421SV *
1422common(params)
1423 INPUT:
1424 HV *params
1425 PREINIT:
1426 HE *result;
1427 HV *hv = NULL;
1428 SV *keysv = NULL;
1429 const char *key = NULL;
1430 STRLEN klen = 0;
1431 int flags = 0;
1432 int action = 0;
1433 SV *val = NULL;
1434 U32 hash = 0;
1435 SV **svp;
1436 CODE:
1437 if ((svp = hv_fetchs(params, "hv", 0))) {
1438 SV *const rv = *svp;
1439 if (!SvROK(rv))
1440 croak("common passed a non-reference for parameter hv");
1441 hv = (HV *)SvRV(rv);
1442 }
1443 if ((svp = hv_fetchs(params, "keysv", 0)))
1444 keysv = *svp;
1445 if ((svp = hv_fetchs(params, "keypv", 0))) {
1446 key = SvPV_const(*svp, klen);
1447 if (SvUTF8(*svp))
1448 flags = HVhek_UTF8;
1449 }
1450 if ((svp = hv_fetchs(params, "action", 0)))
1451 action = SvIV(*svp);
1452 if ((svp = hv_fetchs(params, "val", 0)))
527df579 1453 val = newSVsv(*svp);
6b4de907 1454 if ((svp = hv_fetchs(params, "hash", 0)))
a44093a9 1455 hash = SvUV(*svp);
6b4de907 1456
527df579
NC
1457 if ((svp = hv_fetchs(params, "hash_pv", 0))) {
1458 PERL_HASH(hash, key, klen);
1459 }
58ca560a
NC
1460 if ((svp = hv_fetchs(params, "hash_sv", 0))) {
1461 STRLEN len;
1462 const char *const p = SvPV(keysv, len);
1463 PERL_HASH(hash, p, len);
1464 }
527df579 1465
a75fcbca 1466 result = (HE *)hv_common(hv, keysv, key, klen, flags, action, val, hash);
6b4de907
NC
1467 if (!result) {
1468 XSRETURN_EMPTY;
1469 }
1470 /* Force mg_get */
1471 RETVAL = newSVsv(HeVAL(result));
1472 OUTPUT:
1473 RETVAL
1474
9568a123
NC
1475#endif
1476
439efdfe 1477void
2dc92170
NC
1478test_hv_free_ent()
1479 PPCODE:
1480 test_freeent(&Perl_hv_free_ent);
1481 XSRETURN(4);
1482
439efdfe 1483void
2dc92170
NC
1484test_hv_delayfree_ent()
1485 PPCODE:
1486 test_freeent(&Perl_hv_delayfree_ent);
1487 XSRETURN(4);
35ab5632
NC
1488
1489SV *
1490test_share_unshare_pvn(input)
1491 PREINIT:
35ab5632
NC
1492 STRLEN len;
1493 U32 hash;
1494 char *pvx;
1495 char *p;
1496 INPUT:
1497 SV *input
1498 CODE:
1499 pvx = SvPV(input, len);
1500 PERL_HASH(hash, pvx, len);
1501 p = sharepvn(pvx, len, hash);
1502 RETVAL = newSVpvn(p, len);
1503 unsharepvn(p, len, hash);
1504 OUTPUT:
1505 RETVAL
d8c5b3c5 1506
9568a123
NC
1507#if PERL_VERSION >= 9
1508
d8c5b3c5
NC
1509bool
1510refcounted_he_exists(key, level=0)
1511 SV *key
1512 IV level
1513 CODE:
1514 if (level) {
1515 croak("level must be zero, not %"IVdf, level);
1516 }
20439bc7 1517 RETVAL = (cop_hints_fetch_sv(PL_curcop, key, 0, 0) != &PL_sv_placeholder);
d8c5b3c5
NC
1518 OUTPUT:
1519 RETVAL
1520
d8c5b3c5
NC
1521SV *
1522refcounted_he_fetch(key, level=0)
1523 SV *key
1524 IV level
1525 CODE:
1526 if (level) {
1527 croak("level must be zero, not %"IVdf, level);
1528 }
20439bc7 1529 RETVAL = cop_hints_fetch_sv(PL_curcop, key, 0, 0);
d8c5b3c5
NC
1530 SvREFCNT_inc(RETVAL);
1531 OUTPUT:
1532 RETVAL
65bfe90c 1533
9568a123 1534#endif
65bfe90c 1535
5f39160d
TC
1536void
1537test_force_keys(HV *hv)
1538 PREINIT:
1539 HE *he;
1540 STRLEN count = 0;
1541 PPCODE:
1542 hv_iterinit(hv);
1543 he = hv_iternext(hv);
1544 while (he) {
1545 SV *sv = HeSVKEY_force(he);
1546 ++count;
1547 EXTEND(SP, count);
1548 PUSHs(sv_mortalcopy(sv));
1549 he = hv_iternext(hv);
1550 }
1551
0314122a
NC
1552=pod
1553
1554sub TIEHASH { bless {}, $_[0] }
1555sub STORE { $_[0]->{$_[1]} = $_[2] }
1556sub FETCH { $_[0]->{$_[1]} }
1557sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
1558sub NEXTKEY { each %{$_[0]} }
1559sub EXISTS { exists $_[0]->{$_[1]} }
1560sub DELETE { delete $_[0]->{$_[1]} }
1561sub CLEAR { %{$_[0]} = () }
1562
1563=cut
1564
e2fe06dd
EB
1565MODULE = XS::APItest:TempLv PACKAGE = XS::APItest::TempLv
1566
1567void
1568make_temp_mg_lv(sv)
1569SV* sv
1570 PREINIT:
1571 SV * const lv = newSV_type(SVt_PVLV);
1572 STRLEN len;
1573 PPCODE:
1574 SvPV(sv, len);
1575
1576 sv_magic(lv, NULL, PERL_MAGIC_substr, NULL, 0);
1577 LvTYPE(lv) = 'x';
1578 LvTARG(lv) = SvREFCNT_inc_simple(sv);
1579 LvTARGOFF(lv) = len == 0 ? 0 : 1;
1580 LvTARGLEN(lv) = len < 2 ? 0 : len-2;
1581
1582 EXTEND(SP, 1);
1583 ST(0) = sv_2mortal(lv);
1584 XSRETURN(1);
1585
1586
36c2b1d0
NC
1587MODULE = XS::APItest::PtrTable PACKAGE = XS::APItest::PtrTable PREFIX = ptr_table_
1588
1589void
1590ptr_table_new(classname)
1591const char * classname
1592 PPCODE:
1593 PUSHs(sv_setref_pv(sv_newmortal(), classname, (void*)ptr_table_new()));
1594
1595void
1596DESTROY(table)
1597XS::APItest::PtrTable table
1598 CODE:
1599 ptr_table_free(table);
1600
1601void
992b2363 1602ptr_table_store(table, from, to)
36c2b1d0 1603XS::APItest::PtrTable table
992b2363
NC
1604SVREF from
1605SVREF to
36c2b1d0 1606 CODE:
992b2363 1607 ptr_table_store(table, from, to);
36c2b1d0
NC
1608
1609UV
992b2363 1610ptr_table_fetch(table, from)
36c2b1d0 1611XS::APItest::PtrTable table
992b2363 1612SVREF from
36c2b1d0 1613 CODE:
992b2363 1614 RETVAL = PTR2UV(ptr_table_fetch(table, from));
36c2b1d0
NC
1615 OUTPUT:
1616 RETVAL
1617
1618void
1619ptr_table_split(table)
1620XS::APItest::PtrTable table
1621
1622void
1623ptr_table_clear(table)
1624XS::APItest::PtrTable table
1625
6911735f
FC
1626MODULE = XS::APItest::AutoLoader PACKAGE = XS::APItest::AutoLoader
1627
1628SV *
1629AUTOLOAD()
1630 CODE:
1631 RETVAL = newSVpvn_flags(SvPVX(cv), SvCUR(cv), SvUTF8(cv));
1632 OUTPUT:
1633 RETVAL
1634
8fa6a409
FC
1635SV *
1636AUTOLOADp(...)
1637 PROTOTYPE: *$
1638 CODE:
5f74c55e 1639 PERL_UNUSED_ARG(items);
8fa6a409
FC
1640 RETVAL = newSVpvn_flags(SvPVX(cv), SvCUR(cv), SvUTF8(cv));
1641 OUTPUT:
1642 RETVAL
1643
6911735f 1644
3e61d65a
JH
1645MODULE = XS::APItest PACKAGE = XS::APItest
1646
1647PROTOTYPES: DISABLE
1648
67ecba2f
FC
1649BOOT:
1650 mymro.resolve = myget_linear_isa;
1651 mymro.name = "justisa";
1652 mymro.length = 7;
1653 mymro.kflags = 0;
1654 mymro.hash = 0;
1655 Perl_mro_register(aTHX_ &mymro);
1656
f568d64d
BM
1657HV *
1658xop_custom_ops ()
1659 CODE:
1660 RETVAL = PL_custom_ops;
1661 OUTPUT:
1662 RETVAL
1663
1664HV *
1665xop_custom_op_names ()
1666 CODE:
1667 PL_custom_op_names = newHV();
1668 RETVAL = PL_custom_op_names;
1669 OUTPUT:
1670 RETVAL
1671
1672HV *
1673xop_custom_op_descs ()
1674 CODE:
1675 PL_custom_op_descs = newHV();
1676 RETVAL = PL_custom_op_descs;
1677 OUTPUT:
1678 RETVAL
1679
1680void
1681xop_register ()
1682 CODE:
1683 XopENTRY_set(&my_xop, xop_name, "my_xop");
1684 XopENTRY_set(&my_xop, xop_desc, "XOP for testing");
1685 XopENTRY_set(&my_xop, xop_class, OA_UNOP);
1686 XopENTRY_set(&my_xop, xop_peep, peep_xop);
1687 Perl_custom_op_register(aTHX_ pp_xop, &my_xop);
1688
1689void
1690xop_clear ()
1691 CODE:
1692 XopDISABLE(&my_xop, xop_name);
1693 XopDISABLE(&my_xop, xop_desc);
1694 XopDISABLE(&my_xop, xop_class);
1695 XopDISABLE(&my_xop, xop_peep);
1696
1697IV
1698xop_my_xop ()
1699 CODE:
1700 RETVAL = PTR2IV(&my_xop);
1701 OUTPUT:
1702 RETVAL
1703
1704IV
1705xop_ppaddr ()
1706 CODE:
1707 RETVAL = PTR2IV(pp_xop);
1708 OUTPUT:
1709 RETVAL
1710
1711IV
1712xop_OA_UNOP ()
1713 CODE:
1714 RETVAL = OA_UNOP;
1715 OUTPUT:
1716 RETVAL
1717
1718AV *
1719xop_build_optree ()
1720 CODE:
1721 dMY_CXT;
1722 UNOP *unop;
1723 OP *kid;
1724
1725 MY_CXT.xop_record = newAV();
1726
1727 kid = newSVOP(OP_CONST, 0, newSViv(42));
1728
1729 NewOp(1102, unop, 1, UNOP);
1730 unop->op_type = OP_CUSTOM;
1731 unop->op_ppaddr = pp_xop;
1732 unop->op_flags = OPf_KIDS;
1733 unop->op_private = 0;
1734 unop->op_first = kid;
1735 unop->op_next = NULL;
1736 kid->op_next = (OP*)unop;
1737
b58757d5
BM
1738 av_push(MY_CXT.xop_record, newSVpvf("unop:%"UVxf, PTR2UV(unop)));
1739 av_push(MY_CXT.xop_record, newSVpvf("kid:%"UVxf, PTR2UV(kid)));
f568d64d
BM
1740
1741 av_push(MY_CXT.xop_record, newSVpvf("NAME:%s", OP_NAME((OP*)unop)));
1742 av_push(MY_CXT.xop_record, newSVpvf("DESC:%s", OP_DESC((OP*)unop)));
c33e8be1 1743 av_push(MY_CXT.xop_record, newSVpvf("CLASS:%d", (int)OP_CLASS((OP*)unop)));
f568d64d
BM
1744
1745 PL_rpeepp(aTHX_ kid);
1746
1747 FreeOp(kid);
1748 FreeOp(unop);
1749
1750 RETVAL = MY_CXT.xop_record;
1751 MY_CXT.xop_record = NULL;
1752 OUTPUT:
1753 RETVAL
1754
ae103e09
DD
1755IV
1756xop_from_custom_op ()
1757 CODE:
1758/* author note: this test doesn't imply Perl_custom_op_xop is or isn't public
1759 API or that Perl_custom_op_xop is known to be used outside the core */
1760 UNOP *unop;
1761 XOP *xop;
1762
1763 NewOp(1102, unop, 1, UNOP);
1764 unop->op_type = OP_CUSTOM;
1765 unop->op_ppaddr = pp_xop;
1766 unop->op_flags = OPf_KIDS;
1767 unop->op_private = 0;
1768 unop->op_first = NULL;
1769 unop->op_next = NULL;
1770
1771 xop = Perl_custom_op_xop(aTHX_ (OP *)unop);
1772 FreeOp(unop);
1773 RETVAL = PTR2IV(xop);
1774 OUTPUT:
1775 RETVAL
1776
85ce96a1
DM
1777BOOT:
1778{
1779 MY_CXT_INIT;
03569ecf 1780
85ce96a1
DM
1781 MY_CXT.i = 99;
1782 MY_CXT.sv = newSVpv("initial",0);
13b6b3bc
BM
1783
1784 MY_CXT.bhkav = get_av("XS::APItest::bhkav", GV_ADDMULTI);
1785 MY_CXT.bhk_record = 0;
1786
a88d97bf
BM
1787 BhkENTRY_set(&bhk_test, bhk_start, blockhook_test_start);
1788 BhkENTRY_set(&bhk_test, bhk_pre_end, blockhook_test_pre_end);
1789 BhkENTRY_set(&bhk_test, bhk_post_end, blockhook_test_post_end);
1790 BhkENTRY_set(&bhk_test, bhk_eval, blockhook_test_eval);
13b6b3bc
BM
1791 Perl_blockhook_register(aTHX_ &bhk_test);
1792
65bfe90c 1793 MY_CXT.cscgv = gv_fetchpvs("XS::APItest::COMPILE_SCOPE_CONTAINER",
13b6b3bc 1794 GV_ADDMULTI, SVt_PVAV);
03569ecf
BM
1795 MY_CXT.cscav = GvAV(MY_CXT.cscgv);
1796
a88d97bf
BM
1797 BhkENTRY_set(&bhk_csc, bhk_start, blockhook_csc_start);
1798 BhkENTRY_set(&bhk_csc, bhk_pre_end, blockhook_csc_pre_end);
13b6b3bc 1799 Perl_blockhook_register(aTHX_ &bhk_csc);
201c7e1f
FR
1800
1801 MY_CXT.peep_recorder = newAV();
1802 MY_CXT.rpeep_recorder = newAV();
1803
1804 MY_CXT.orig_peep = PL_peepp;
1805 MY_CXT.orig_rpeep = PL_rpeepp;
1806 PL_peepp = my_peep;
1807 PL_rpeepp = my_rpeep;
65bfe90c 1808}
85ce96a1
DM
1809
1810void
1811CLONE(...)
1812 CODE:
1813 MY_CXT_CLONE;
c33e8be1 1814 PERL_UNUSED_VAR(items);
85ce96a1 1815 MY_CXT.sv = newSVpv("initial_clone",0);
65bfe90c 1816 MY_CXT.cscgv = gv_fetchpvs("XS::APItest::COMPILE_SCOPE_CONTAINER",
13b6b3bc 1817 GV_ADDMULTI, SVt_PVAV);
03569ecf 1818 MY_CXT.cscav = NULL;
13b6b3bc
BM
1819 MY_CXT.bhkav = get_av("XS::APItest::bhkav", GV_ADDMULTI);
1820 MY_CXT.bhk_record = 0;
201c7e1f
FR
1821 MY_CXT.peep_recorder = newAV();
1822 MY_CXT.rpeep_recorder = newAV();
85ce96a1 1823
3e61d65a
JH
1824void
1825print_double(val)
1826 double val
1827 CODE:
1828 printf("%5.3f\n",val);
1829
1830int
1831have_long_double()
1832 CODE:
1833#ifdef HAS_LONG_DOUBLE
1834 RETVAL = 1;
1835#else
1836 RETVAL = 0;
1837#endif
cabb36f0
CN
1838 OUTPUT:
1839 RETVAL
3e61d65a
JH
1840
1841void
1842print_long_double()
1843 CODE:
1844#ifdef HAS_LONG_DOUBLE
fc0bf671 1845# if defined(PERL_PRIfldbl) && (LONG_DOUBLESIZE > DOUBLESIZE)
3e61d65a
JH
1846 long double val = 7.0;
1847 printf("%5.3" PERL_PRIfldbl "\n",val);
1848# else
1849 double val = 7.0;
1850 printf("%5.3f\n",val);
1851# endif
1852#endif
1853
1854void
3e61d65a
JH
1855print_int(val)
1856 int val
1857 CODE:
1858 printf("%d\n",val);
1859
1860void
1861print_long(val)
1862 long val
1863 CODE:
1864 printf("%ld\n",val);
1865
1866void
1867print_float(val)
1868 float val
1869 CODE:
1870 printf("%5.3f\n",val);
9d911683
NIS
1871
1872void
1873print_flush()
1874 CODE:
1875 fflush(stdout);
d4b90eee
SH
1876
1877void
1878mpushp()
1879 PPCODE:
1880 EXTEND(SP, 3);
1881 mPUSHp("one", 3);
1882 mPUSHp("two", 3);
1883 mPUSHp("three", 5);
1884 XSRETURN(3);
1885
1886void
1887mpushn()
1888 PPCODE:
1889 EXTEND(SP, 3);
1890 mPUSHn(0.5);
1891 mPUSHn(-0.25);
1892 mPUSHn(0.125);
1893 XSRETURN(3);
1894
1895void
1896mpushi()
1897 PPCODE:
1898 EXTEND(SP, 3);
d75b63cf
MHM
1899 mPUSHi(-1);
1900 mPUSHi(2);
1901 mPUSHi(-3);
d4b90eee
SH
1902 XSRETURN(3);
1903
1904void
1905mpushu()
1906 PPCODE:
1907 EXTEND(SP, 3);
d75b63cf
MHM
1908 mPUSHu(1);
1909 mPUSHu(2);
1910 mPUSHu(3);
d4b90eee
SH
1911 XSRETURN(3);
1912
1913void
1914mxpushp()
1915 PPCODE:
1916 mXPUSHp("one", 3);
1917 mXPUSHp("two", 3);
1918 mXPUSHp("three", 5);
1919 XSRETURN(3);
1920
1921void
1922mxpushn()
1923 PPCODE:
1924 mXPUSHn(0.5);
1925 mXPUSHn(-0.25);
1926 mXPUSHn(0.125);
1927 XSRETURN(3);
1928
1929void
1930mxpushi()
1931 PPCODE:
d75b63cf
MHM
1932 mXPUSHi(-1);
1933 mXPUSHi(2);
1934 mXPUSHi(-3);
d4b90eee
SH
1935 XSRETURN(3);
1936
1937void
1938mxpushu()
1939 PPCODE:
d75b63cf
MHM
1940 mXPUSHu(1);
1941 mXPUSHu(2);
1942 mXPUSHu(3);
d4b90eee 1943 XSRETURN(3);
d1f347d7 1944
a85ce6f0
DD
1945void
1946call_sv_C()
1947PREINIT:
1948 CV * i_sub;
1949 GV * i_gv;
1950 I32 retcnt;
1951 SV * errsv;
1952 char * errstr;
1953 SV * miscsv = sv_newmortal();
1954 HV * hv = (HV*)sv_2mortal((SV*)newHV());
1955CODE:
1956 i_sub = get_cv("i", 0);
1957 PUSHMARK(SP);
1958 /* PUTBACK not needed since this sub was called with 0 args, and is calling
1959 0 args, so global SP doesn't need to be moved before a call_* */
1960 retcnt = call_sv((SV*)i_sub, 0); /* try a CV* */
1961 SPAGAIN;
1962 SP -= retcnt; /* dont care about return count, wipe everything off */
1963 sv_setpvs(miscsv, "i");
1964 PUSHMARK(SP);
1965 retcnt = call_sv(miscsv, 0); /* try a PV */
1966 SPAGAIN;
1967 SP -= retcnt;
1968 /* no add and SVt_NULL are intentional, sub i should be defined already */
1969 i_gv = gv_fetchpvn_flags("i", sizeof("i")-1, 0, SVt_NULL);
1970 PUSHMARK(SP);
1971 retcnt = call_sv((SV*)i_gv, 0); /* try a GV* */
1972 SPAGAIN;
1973 SP -= retcnt;
1974 /* the tests below are not declaring this being public API behavior,
1975 only current internal behavior, these tests can be changed in the
1976 future if necessery */
1977 PUSHMARK(SP);
1978 retcnt = call_sv(&PL_sv_yes, 0); /* does nothing */
1979 SPAGAIN;
1980 SP -= retcnt;
1981 PUSHMARK(SP);
1982 retcnt = call_sv(&PL_sv_no, G_EVAL);
1983 SPAGAIN;
1984 SP -= retcnt;
1985 errsv = ERRSV;
1986 errstr = SvPV_nolen(errsv);
1987 if(strnEQ(errstr, "Undefined subroutine &main:: called at",
1988 sizeof("Undefined subroutine &main:: called at") - 1)) {
1989 PUSHMARK(SP);
1990 retcnt = call_sv((SV*)i_sub, 0); /* call again to increase counter */
1991 SPAGAIN;
1992 SP -= retcnt;
1993 }
1994 PUSHMARK(SP);
1995 retcnt = call_sv(&PL_sv_undef, G_EVAL);
1996 SPAGAIN;
1997 SP -= retcnt;
1998 errsv = ERRSV;
1999 errstr = SvPV_nolen(errsv);
2000 if(strnEQ(errstr, "Can't use an undefined value as a subroutine reference at",
2001 sizeof("Can't use an undefined value as a subroutine reference at") - 1)) {
2002 PUSHMARK(SP);
2003 retcnt = call_sv((SV*)i_sub, 0); /* call again to increase counter */
2004 SPAGAIN;
2005 SP -= retcnt;
2006 }
2007 PUSHMARK(SP);
2008 retcnt = call_sv((SV*)hv, G_EVAL);
2009 SPAGAIN;
2010 SP -= retcnt;
2011 errsv = ERRSV;
2012 errstr = SvPV_nolen(errsv);
2013 if(strnEQ(errstr, "Not a CODE reference at",
2014 sizeof("Not a CODE reference at") - 1)) {
2015 PUSHMARK(SP);
2016 retcnt = call_sv((SV*)i_sub, 0); /* call again to increase counter */
2017 SPAGAIN;
2018 SP -= retcnt;
2019 }
d1f347d7
DM
2020
2021void
2022call_sv(sv, flags, ...)
2023 SV* sv
2024 I32 flags
2025 PREINIT:
2026 I32 i;
2027 PPCODE:
2028 for (i=0; i<items-2; i++)
2029 ST(i) = ST(i+2); /* pop first two args */
2030 PUSHMARK(SP);
2031 SP += items - 2;
2032 PUTBACK;
2033 i = call_sv(sv, flags);
2034 SPAGAIN;
2035 EXTEND(SP, 1);
2036 PUSHs(sv_2mortal(newSViv(i)));
2037
2038void
2039call_pv(subname, flags, ...)
2040 char* subname
2041 I32 flags
2042 PREINIT:
2043 I32 i;
2044 PPCODE:
2045 for (i=0; i<items-2; i++)
2046 ST(i) = ST(i+2); /* pop first two args */
2047 PUSHMARK(SP);
2048 SP += items - 2;
2049 PUTBACK;
2050 i = call_pv(subname, flags);
2051 SPAGAIN;
2052 EXTEND(SP, 1);
2053 PUSHs(sv_2mortal(newSViv(i)));
2054
2055void
2056call_method(methname, flags, ...)
2057 char* methname
2058 I32 flags
2059 PREINIT:
2060 I32 i;
2061 PPCODE:
2062 for (i=0; i<items-2; i++)
2063 ST(i) = ST(i+2); /* pop first two args */
2064 PUSHMARK(SP);
2065 SP += items - 2;
2066 PUTBACK;
2067 i = call_method(methname, flags);
2068 SPAGAIN;
2069 EXTEND(SP, 1);
2070 PUSHs(sv_2mortal(newSViv(i)));
2071
2072void
c0810f8e 2073newCONSTSUB(stash, name, flags, sv)
3453414d
BF
2074 HV* stash
2075 SV* name
2076 I32 flags
8f82b567 2077 SV* sv
c0810f8e
NC
2078 ALIAS:
2079 newCONSTSUB_flags = 1
3453414d 2080 PREINIT:
5f74c55e 2081 CV* mycv = NULL;
9c0a6090
FC
2082 STRLEN len;
2083 const char *pv = SvPV(name, len);
3453414d 2084 PPCODE:
c0810f8e 2085 switch (ix) {
3453414d 2086 case 0:
dc4b20bd 2087 mycv = newCONSTSUB(stash, pv, SvOK(sv) ? SvREFCNT_inc(sv) : NULL);
3453414d
BF
2088 break;
2089 case 1:
dc4b20bd 2090 mycv = newCONSTSUB_flags(
0fc4714a 2091 stash, pv, len, flags | SvUTF8(name), SvOK(sv) ? SvREFCNT_inc(sv) : NULL
8f82b567 2092 );
3453414d
BF
2093 break;
2094 }
2095 EXTEND(SP, 2);
dc4b20bd
CB
2096 PUSHs( CvCONST(mycv) ? &PL_sv_yes : &PL_sv_no );
2097 PUSHs((SV*)CvGV(mycv));
3453414d
BF
2098
2099void
e6066781
BF
2100gv_init_type(namesv, multi, flags, type)
2101 SV* namesv
2102 int multi
2103 I32 flags
2104 int type
2105 PREINIT:
2106 STRLEN len;
2107 const char * const name = SvPV_const(namesv, len);
2108 GV *gv = *(GV**)hv_fetch(PL_defstash, name, len, TRUE);
2109 PPCODE:
2110 if (SvTYPE(gv) == SVt_PVGV)
2111 Perl_croak(aTHX_ "GV is already a PVGV");
04ec7e59 2112 if (multi) flags |= GV_ADDMULTI;
e6066781
BF
2113 switch (type) {
2114 case 0:
2115 gv_init(gv, PL_defstash, name, len, multi);
2116 break;
2117 case 1:
04ec7e59 2118 gv_init_sv(gv, PL_defstash, namesv, flags);
e6066781
BF
2119 break;
2120 case 2:
04ec7e59 2121 gv_init_pv(gv, PL_defstash, name, flags | SvUTF8(namesv));
e6066781
BF
2122 break;
2123 case 3:
04ec7e59 2124 gv_init_pvn(gv, PL_defstash, name, len, flags | SvUTF8(namesv));
e6066781
BF
2125 break;
2126 }
2127 XPUSHs( gv ? (SV*)gv : &PL_sv_undef);
2128
2129void
e6919483
BF
2130gv_fetchmeth_type(stash, methname, type, level, flags)
2131 HV* stash
2132 SV* methname
2133 int type
2134 I32 level
2135 I32 flags
2136 PREINIT:
2137 STRLEN len;
2138 const char * const name = SvPV_const(methname, len);
5f74c55e 2139 GV* gv = NULL;
e6919483
BF
2140 PPCODE:
2141 switch (type) {
2142 case 0:
2143 gv = gv_fetchmeth(stash, name, len, level);
2144 break;
2145 case 1:
2146 gv = gv_fetchmeth_sv(stash, methname, level, flags);
2147 break;
2148 case 2:
2149 gv = gv_fetchmeth_pv(stash, name, level, flags | SvUTF8(methname));
2150 break;
2151 case 3:
2152 gv = gv_fetchmeth_pvn(stash, name, len, level, flags | SvUTF8(methname));
2153 break;
2154 }
2155 XPUSHs( gv ? MUTABLE_SV(gv) : &PL_sv_undef );
2156
2157void
d21989ed
BF
2158gv_fetchmeth_autoload_type(stash, methname, type, level, flags)
2159 HV* stash
2160 SV* methname
2161 int type
2162 I32 level
2163 I32 flags
2164 PREINIT:
2165 STRLEN len;
2166 const char * const name = SvPV_const(methname, len);
5f74c55e 2167 GV* gv = NULL;
d21989ed
BF
2168 PPCODE:
2169 switch (type) {
2170 case 0:
2171 gv = gv_fetchmeth_autoload(stash, name, len, level);
2172 break;
2173 case 1:
2174 gv = gv_fetchmeth_sv_autoload(stash, methname, level, flags);
2175 break;
2176 case 2:
2177 gv = gv_fetchmeth_pv_autoload(stash, name, level, flags | SvUTF8(methname));
2178 break;
2179 case 3:
2180 gv = gv_fetchmeth_pvn_autoload(stash, name, len, level, flags | SvUTF8(methname));
2181 break;
2182 }
2183 XPUSHs( gv ? MUTABLE_SV(gv) : &PL_sv_undef );
2184
2185void
44130a26
BF
2186gv_fetchmethod_flags_type(stash, methname, type, flags)
2187 HV* stash
2188 SV* methname
2189 int type
2190 I32 flags
2191 PREINIT:
5f74c55e 2192 GV* gv = NULL;
44130a26
BF
2193 PPCODE:
2194 switch (type) {
2195 case 0:
2196 gv = gv_fetchmethod_flags(stash, SvPVX_const(methname), flags);
2197 break;
2198 case 1:
2199 gv = gv_fetchmethod_sv_flags(stash, methname, flags);
2200 break;
2201 case 2:
2202 gv = gv_fetchmethod_pv_flags(stash, SvPV_nolen(methname), flags | SvUTF8(methname));
2203 break;
2204 case 3: {
2205 STRLEN len;
2206 const char * const name = SvPV_const(methname, len);
2207 gv = gv_fetchmethod_pvn_flags(stash, name, len, flags | SvUTF8(methname));
2208 break;
2209 }
2210 }
5fba3c91 2211 XPUSHs( gv ? (SV*)gv : &PL_sv_undef);
44130a26 2212
5fba3c91 2213void
0eeb01b9 2214gv_autoload_type(stash, methname, type, method)
5fba3c91
BF
2215 HV* stash
2216 SV* methname
2217 int type
2218 I32 method
5fba3c91
BF
2219 PREINIT:
2220 STRLEN len;
2221 const char * const name = SvPV_const(methname, len);
5f74c55e 2222 GV* gv = NULL;
0eeb01b9 2223 I32 flags = method ? GV_AUTOLOAD_ISMETHOD : 0;
5fba3c91
BF
2224 PPCODE:
2225 switch (type) {
2226 case 0:
2227 gv = gv_autoload4(stash, name, len, method);
2228 break;
2229 case 1:
0eeb01b9 2230 gv = gv_autoload_sv(stash, methname, flags);
5fba3c91
BF
2231 break;
2232 case 2:
0eeb01b9 2233 gv = gv_autoload_pv(stash, name, flags | SvUTF8(methname));
5fba3c91
BF
2234 break;
2235 case 3:
0eeb01b9 2236 gv = gv_autoload_pvn(stash, name, len, flags | SvUTF8(methname));
5fba3c91
BF
2237 break;
2238 }
44130a26
BF
2239 XPUSHs( gv ? (SV*)gv : &PL_sv_undef);
2240
2241void
84c7b88c
BF
2242whichsig_type(namesv, type)
2243 SV* namesv
2244 int type
2245 PREINIT:
2246 STRLEN len;
2247 const char * const name = SvPV_const(namesv, len);
5f74c55e 2248 I32 i = 0;
84c7b88c
BF
2249 PPCODE:
2250 switch (type) {
2251 case 0:
2252 i = whichsig(name);
2253 break;
2254 case 1:
2255 i = whichsig_sv(namesv);
2256 break;
2257 case 2:
2258 i = whichsig_pv(name);
2259 break;
2260 case 3:
2261 i = whichsig_pvn(name, len);
2262 break;
2263 }
2264 XPUSHs(sv_2mortal(newSViv(i)));
2265
2266void
d1f347d7
DM
2267eval_sv(sv, flags)
2268 SV* sv
2269 I32 flags
2270 PREINIT:
2271 I32 i;
2272 PPCODE:
2273 PUTBACK;
2274 i = eval_sv(sv, flags);
2275 SPAGAIN;
2276 EXTEND(SP, 1);
2277 PUSHs(sv_2mortal(newSViv(i)));
2278
b8e65a9b 2279void
d1f347d7
DM
2280eval_pv(p, croak_on_error)
2281 const char* p
2282 I32 croak_on_error
d1f347d7
DM
2283 PPCODE:
2284 PUTBACK;
2285 EXTEND(SP, 1);
2286 PUSHs(eval_pv(p, croak_on_error));
2287
2288void
2289require_pv(pv)
2290 const char* pv
d1f347d7
DM
2291 PPCODE:
2292 PUTBACK;
2293 require_pv(pv);
2294
0ca3a874 2295int
7a646707 2296apitest_exception(throw_e)
0ca3a874
MHM
2297 int throw_e
2298 OUTPUT:
2299 RETVAL
d1f347d7 2300
ef469b03 2301void
7e7a3dfc
GA
2302mycroak(sv)
2303 SV* sv
ef469b03 2304 CODE:
7e7a3dfc
GA
2305 if (SvOK(sv)) {
2306 Perl_croak(aTHX_ "%s", SvPV_nolen(sv));
2307 }
2308 else {
2309 Perl_croak(aTHX_ NULL);
2310 }
5d2b1485
NC
2311
2312SV*
2313strtab()
2314 CODE:
2315 RETVAL = newRV_inc((SV*)PL_strtab);
2316 OUTPUT:
2317 RETVAL
85ce96a1
DM
2318
2319int
2320my_cxt_getint()
2321 CODE:
2322 dMY_CXT;
2323 RETVAL = my_cxt_getint_p(aMY_CXT);
2324 OUTPUT:
2325 RETVAL
2326
2327void
2328my_cxt_setint(i)
2329 int i;
2330 CODE:
2331 dMY_CXT;
2332 my_cxt_setint_p(aMY_CXT_ i);
2333
2334void
9568a123
NC
2335my_cxt_getsv(how)
2336 bool how;
85ce96a1 2337 PPCODE:
85ce96a1 2338 EXTEND(SP, 1);
9568a123 2339 ST(0) = how ? my_cxt_getsv_interp_context() : my_cxt_getsv_interp();
85ce96a1
DM
2340 XSRETURN(1);
2341
2342void
2343my_cxt_setsv(sv)
2344 SV *sv;
2345 CODE:
2346 dMY_CXT;
2347 SvREFCNT_dec(MY_CXT.sv);
2348 my_cxt_setsv_p(sv _aMY_CXT);
2349 SvREFCNT_inc(sv);
34482cd6
NC
2350
2351bool
2352sv_setsv_cow_hashkey_core()
2353
2354bool
2355sv_setsv_cow_hashkey_notcore()
84ac5fd7
NC
2356
2357void
74ee33f2
FC
2358sv_set_deref(SV *sv, SV *sv2, int which)
2359 CODE:
2360 {
2361 STRLEN len;
2362 const char *pv = SvPV(sv2,len);
2363 if (!SvROK(sv)) croak("Not a ref");
2364 sv = SvRV(sv);
2365 switch (which) {
2366 case 0: sv_setsv(sv,sv2); break;
2367 case 1: sv_setpv(sv,pv); break;
2368 case 2: sv_setpvn(sv,pv,len); break;
2369 }
2370 }
2371
2372void
218787bd
VP
2373rmagical_cast(sv, type)
2374 SV *sv;
2375 SV *type;
2376 PREINIT:
2377 struct ufuncs uf;
2378 PPCODE:
2379 if (!SvOK(sv) || !SvROK(sv) || !SvOK(type)) { XSRETURN_UNDEF; }
2380 sv = SvRV(sv);
2381 if (SvTYPE(sv) != SVt_PVHV) { XSRETURN_UNDEF; }
2382 uf.uf_val = rmagical_a_dummy;
2383 uf.uf_set = NULL;
2384 uf.uf_index = 0;
2385 if (SvTRUE(type)) { /* b */
2386 sv_magicext(sv, NULL, PERL_MAGIC_ext, &rmagical_b, NULL, 0);
2387 } else { /* a */
2388 sv_magic(sv, NULL, PERL_MAGIC_uvar, (char *) &uf, sizeof(uf));
2389 }
2390 XSRETURN_YES;
2391
2392void
2393rmagical_flags(sv)
2394 SV *sv;
2395 PPCODE:
2396 if (!SvOK(sv) || !SvROK(sv)) { XSRETURN_UNDEF; }
2397 sv = SvRV(sv);
2398 EXTEND(SP, 3);
2399 mXPUSHu(SvFLAGS(sv) & SVs_GMG);
2400 mXPUSHu(SvFLAGS(sv) & SVs_SMG);
2401 mXPUSHu(SvFLAGS(sv) & SVs_RMG);
2402 XSRETURN(3);
2403
2404void
90d1f214
BM
2405my_caller(level)
2406 I32 level
2407 PREINIT:
2408 const PERL_CONTEXT *cx, *dbcx;
2409 const char *pv;
2410 const GV *gv;
2411 HV *hv;
2412 PPCODE:
2413 cx = caller_cx(level, &dbcx);
2414 EXTEND(SP, 8);
2415
2416 pv = CopSTASHPV(cx->blk_oldcop);
2417 ST(0) = pv ? sv_2mortal(newSVpv(pv, 0)) : &PL_sv_undef;
2418 gv = CvGV(cx->blk_sub.cv);
2419 ST(1) = isGV(gv) ? sv_2mortal(newSVpv(GvNAME(gv), 0)) : &PL_sv_undef;
2420
2421 pv = CopSTASHPV(dbcx->blk_oldcop);
2422 ST(2) = pv ? sv_2mortal(newSVpv(pv, 0)) : &PL_sv_undef;
2423 gv = CvGV(dbcx->blk_sub.cv);
2424 ST(3) = isGV(gv) ? sv_2mortal(newSVpv(GvNAME(gv), 0)) : &PL_sv_undef;
2425
20439bc7
Z
2426 ST(4) = cop_hints_fetch_pvs(cx->blk_oldcop, "foo", 0);
2427 ST(5) = cop_hints_fetch_pvn(cx->blk_oldcop, "foo", 3, 0, 0);
2428 ST(6) = cop_hints_fetch_sv(cx->blk_oldcop,
2429 sv_2mortal(newSVpvn("foo", 3)), 0, 0);
90d1f214 2430
20439bc7 2431 hv = cop_hints_2hv(cx->blk_oldcop, 0);
90d1f214
BM
2432 ST(7) = hv ? sv_2mortal(newRV_noinc((SV *)hv)) : &PL_sv_undef;
2433
2434 XSRETURN(8);
2435
2436void
f9c17636
MB
2437DPeek (sv)
2438 SV *sv
2439
2440 PPCODE:
5b1f7359 2441 ST (0) = newSVpv (Perl_sv_peek (aTHX_ sv), 0);
f9c17636
MB
2442 XSRETURN (1);
2443
2444void
84ac5fd7
NC
2445BEGIN()
2446 CODE:
2447 sv_inc(get_sv("XS::APItest::BEGIN_called", GV_ADD|GV_ADDMULTI));
2448
2449void
2450CHECK()
2451 CODE:
2452 sv_inc(get_sv("XS::APItest::CHECK_called", GV_ADD|GV_ADDMULTI));
2453
2454void
2455UNITCHECK()
2456 CODE:
0932863f 2457 sv_inc(get_sv("XS::APItest::UNITCHECK_called", GV_ADD|GV_ADDMULTI));
84ac5fd7
NC
2458
2459void
2460INIT()
2461 CODE:
2462 sv_inc(get_sv("XS::APItest::INIT_called", GV_ADD|GV_ADDMULTI));
2463
2464void
2465END()
2466 CODE:
2467 sv_inc(get_sv("XS::APItest::END_called", GV_ADD|GV_ADDMULTI));
30685b56
NC
2468
2469void
2470utf16_to_utf8 (sv, ...)
2471 SV* sv
2472 ALIAS:
2473 utf16_to_utf8_reversed = 1
2474 PREINIT:
2475 STRLEN len;
2476 U8 *source;
2477 SV *dest;
2478 I32 got; /* Gah, badly thought out APIs */
2479 CODE:
2480 source = (U8 *)SvPVbyte(sv, len);
2481 /* Optionally only convert part of the buffer. */
2482 if (items > 1) {
2483 len = SvUV(ST(1));
2484 }
2485 /* Mortalise this right now, as we'll be testing croak()s */
2486 dest = sv_2mortal(newSV(len * 3 / 2 + 1));
2487 if (ix) {
25f2e844 2488 utf16_to_utf8_reversed(source, (U8 *)SvPVX(dest), len, &got);
30685b56 2489 } else {
25f2e844 2490 utf16_to_utf8(source, (U8 *)SvPVX(dest), len, &got);
30685b56
NC
2491 }
2492 SvCUR_set(dest, got);
2493 SvPVX(dest)[got] = '\0';
2494 SvPOK_on(dest);
2495 ST(0) = dest;
2496 XSRETURN(1);
879d0c72 2497
6bd7445c
GG
2498void
2499my_exit(int exitcode)
2500 PPCODE:
2501 my_exit(exitcode);
d97c33b5 2502
209e41dc
NT
2503U8
2504first_byte(sv)
2505 SV *sv
2506 CODE:
2507 char *s;
2508 STRLEN len;
2509 s = SvPVbyte(sv, len);
2510 RETVAL = s[0];
2511 OUTPUT:
2512 RETVAL
2513
d97c33b5
DM
2514I32
2515sv_count()
2516 CODE:
2517 RETVAL = PL_sv_count;
2518 OUTPUT:
2519 RETVAL
13b6b3bc
BM
2520
2521void
2522bhk_record(bool on)
2523 CODE:
2524 dMY_CXT;
2525 MY_CXT.bhk_record = on;
2526 if (on)
2527 av_clear(MY_CXT.bhkav);
65bfe90c 2528
defdfed5 2529void
d9088386
Z
2530test_magic_chain()
2531 PREINIT:
2532 SV *sv;
2533 MAGIC *callmg, *uvarmg;
2534 CODE:
2535 sv = sv_2mortal(newSV(0));
11f9f0ed
NC
2536 if (SvTYPE(sv) >= SVt_PVMG) croak_fail();
2537 if (SvMAGICAL(sv)) croak_fail();
d9088386 2538 sv_magic(sv, &PL_sv_yes, PERL_MAGIC_checkcall, (char*)&callmg, 0);
11f9f0ed
NC
2539 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2540 if (!SvMAGICAL(sv)) croak_fail();
2541 if (mg_find(sv, PERL_MAGIC_uvar)) croak_fail();
d9088386 2542 callmg = mg_find(sv, PERL_MAGIC_checkcall);
11f9f0ed 2543 if (!callmg) croak_fail();
d9088386 2544 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 2545 croak_fail();
d9088386 2546 sv_magic(sv, &PL_sv_no, PERL_MAGIC_uvar, (char*)&uvarmg, 0);
11f9f0ed
NC
2547 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2548 if (!SvMAGICAL(sv)) croak_fail();
2549 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
d9088386 2550 uvarmg = mg_find(sv, PERL_MAGIC_uvar);
11f9f0ed 2551 if (!uvarmg) croak_fail();
d9088386 2552 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 2553 croak_fail();
d9088386 2554 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 2555 croak_fail();
d9088386 2556 mg_free_type(sv, PERL_MAGIC_vec);
11f9f0ed
NC
2557 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2558 if (!SvMAGICAL(sv)) croak_fail();
2559 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
2560 if (mg_find(sv, PERL_MAGIC_uvar) != uvarmg) croak_fail();
d9088386 2561 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 2562 croak_fail();
d9088386 2563 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 2564 croak_fail();
d9088386 2565 mg_free_type(sv, PERL_MAGIC_uvar);
11f9f0ed
NC
2566 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2567 if (!SvMAGICAL(sv)) croak_fail();
2568 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
2569 if (mg_find(sv, PERL_MAGIC_uvar)) croak_fail();
d9088386 2570 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 2571 croak_fail();
d9088386 2572 sv_magic(sv, &PL_sv_no, PERL_MAGIC_uvar, (char*)&uvarmg, 0);
11f9f0ed
NC
2573 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2574 if (!SvMAGICAL(sv)) croak_fail();
2575 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
d9088386 2576 uvarmg = mg_find(sv, PERL_MAGIC_uvar);
11f9f0ed 2577 if (!uvarmg) croak_fail();
d9088386 2578 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 2579 croak_fail();
d9088386 2580 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 2581 croak_fail();
d9088386 2582 mg_free_type(sv, PERL_MAGIC_checkcall);
11f9f0ed
NC
2583 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2584 if (!SvMAGICAL(sv)) croak_fail();
2585 if (mg_find(sv, PERL_MAGIC_uvar) != uvarmg) croak_fail();
2586 if (mg_find(sv, PERL_MAGIC_checkcall)) croak_fail();
d9088386 2587 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 2588 croak_fail();
d9088386 2589 mg_free_type(sv, PERL_MAGIC_uvar);
11f9f0ed
NC
2590 if (SvMAGICAL(sv)) croak_fail();
2591 if (mg_find(sv, PERL_MAGIC_checkcall)) croak_fail();
2592 if (mg_find(sv, PERL_MAGIC_uvar)) croak_fail();
d9088386
Z
2593
2594void
2595test_op_contextualize()
2596 PREINIT:
2597 OP *o;
2598 CODE:
2599 o = newSVOP(OP_CONST, 0, newSViv(0));
2600 o->op_flags &= ~OPf_WANT;
2601 o = op_contextualize(o, G_SCALAR);
2602 if (o->op_type != OP_CONST ||
2603 (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR)
11f9f0ed 2604 croak_fail();
d9088386
Z
2605 op_free(o);
2606 o = newSVOP(OP_CONST, 0, newSViv(0));
2607 o->op_flags &= ~OPf_WANT;
2608 o = op_contextualize(o, G_ARRAY);
2609 if (o->op_type != OP_CONST ||
2610 (o->op_flags & OPf_WANT) != OPf_WANT_LIST)
11f9f0ed 2611 croak_fail();
d9088386
Z
2612 op_free(o);
2613 o = newSVOP(OP_CONST, 0, newSViv(0));
2614 o->op_flags &= ~OPf_WANT;
2615 o = op_contextualize(o, G_VOID);
11f9f0ed 2616 if (o->op_type != OP_NULL) croak_fail();
d9088386
Z
2617 op_free(o);
2618
2619void
2620test_rv2cv_op_cv()
2621 PROTOTYPE:
2622 PREINIT:
7616a0c2 2623 GV *troc_gv;
d9088386
Z
2624 CV *troc_cv;
2625 OP *o;
2626 CODE:
2627 troc_gv = gv_fetchpv("XS::APItest::test_rv2cv_op_cv", 0, SVt_PVGV);
2628 troc_cv = get_cv("XS::APItest::test_rv2cv_op_cv", 0);
d9088386 2629 o = newCVREF(0, newGVOP(OP_GV, 0, troc_gv));
11f9f0ed 2630 if (rv2cv_op_cv(o, 0) != troc_cv) croak_fail();
d9088386 2631 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV) != (CV*)troc_gv)
11f9f0ed 2632 croak_fail();
d9088386 2633 o->op_private |= OPpENTERSUB_AMPER;
11f9f0ed
NC
2634 if (rv2cv_op_cv(o, 0)) croak_fail();
2635 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386 2636 o->op_private &= ~OPpENTERSUB_AMPER;
11f9f0ed
NC
2637 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
2638 if (rv2cv_op_cv(o, RV2CVOPCV_MARK_EARLY) != troc_cv) croak_fail();
2639 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
d9088386
Z
2640 op_free(o);
2641 o = newSVOP(OP_CONST, 0, newSVpv("XS::APItest::test_rv2cv_op_cv", 0));
2642 o->op_private = OPpCONST_BARE;
2643 o = newCVREF(0, o);
11f9f0ed 2644 if (rv2cv_op_cv(o, 0) != troc_cv) croak_fail();
d9088386 2645 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV) != (CV*)troc_gv)
11f9f0ed 2646 croak_fail();
d9088386 2647 o->op_private |= OPpENTERSUB_AMPER;
11f9f0ed
NC
2648 if (rv2cv_op_cv(o, 0)) croak_fail();
2649 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386
Z
2650 op_free(o);
2651 o = newCVREF(0, newSVOP(OP_CONST, 0, newRV_inc((SV*)troc_cv)));
11f9f0ed 2652 if (rv2cv_op_cv(o, 0) != troc_cv) croak_fail();
d9088386 2653 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV) != (CV*)troc_gv)
11f9f0ed 2654 croak_fail();
d9088386 2655 o->op_private |= OPpENTERSUB_AMPER;
11f9f0ed
NC
2656 if (rv2cv_op_cv(o, 0)) croak_fail();
2657 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386 2658 o->op_private &= ~OPpENTERSUB_AMPER;
11f9f0ed
NC
2659 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
2660 if (rv2cv_op_cv(o, RV2CVOPCV_MARK_EARLY) != troc_cv) croak_fail();
2661 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
d9088386
Z
2662 op_free(o);
2663 o = newCVREF(0, newUNOP(OP_RAND, 0, newSVOP(OP_CONST, 0, newSViv(0))));
11f9f0ed
NC
2664 if (rv2cv_op_cv(o, 0)) croak_fail();
2665 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386 2666 o->op_private |= OPpENTERSUB_AMPER;
11f9f0ed
NC
2667 if (rv2cv_op_cv(o, 0)) croak_fail();
2668 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386 2669 o->op_private &= ~OPpENTERSUB_AMPER;
11f9f0ed
NC
2670 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
2671 if (rv2cv_op_cv(o, RV2CVOPCV_MARK_EARLY)) croak_fail();
2672 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
d9088386
Z
2673 op_free(o);
2674 o = newUNOP(OP_RAND, 0, newSVOP(OP_CONST, 0, newSViv(0)));
11f9f0ed
NC
2675 if (rv2cv_op_cv(o, 0)) croak_fail();
2676 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386
Z
2677 op_free(o);
2678
2679void
2680test_cv_getset_call_checker()
2681 PREINIT:
2682 CV *troc_cv, *tsh_cv;
2683 Perl_call_checker ckfun;
2684 SV *ckobj;
2685 CODE:
2686#define check_cc(cv, xckfun, xckobj) \
2687 do { \
2688 cv_get_call_checker((cv), &ckfun, &ckobj); \
779bc08a
NC
2689 if (ckfun != (xckfun)) croak_fail_ne(FPTR2DPTR(void *, ckfun), xckfun); \
2690 if (ckobj != (xckobj)) croak_fail_ne(FPTR2DPTR(void *, ckobj), xckobj); \
d9088386
Z
2691 } while(0)
2692 troc_cv = get_cv("XS::APItest::test_rv2cv_op_cv", 0);
2693 tsh_cv = get_cv("XS::APItest::test_savehints", 0);
2694 check_cc(troc_cv, Perl_ck_entersub_args_proto_or_list, (SV*)troc_cv);
2695 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv);
2696 cv_set_call_checker(tsh_cv, Perl_ck_entersub_args_proto_or_list,
2697 &PL_sv_yes);
2698 check_cc(troc_cv, Perl_ck_entersub_args_proto_or_list, (SV*)troc_cv);
2699 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, &PL_sv_yes);
2700 cv_set_call_checker(troc_cv, THX_ck_entersub_args_scalars, &PL_sv_no);
2701 check_cc(troc_cv, THX_ck_entersub_args_scalars, &PL_sv_no);
2702 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, &PL_sv_yes);
2703 cv_set_call_checker(tsh_cv, Perl_ck_entersub_args_proto_or_list,
2704 (SV*)tsh_cv);
2705 check_cc(troc_cv, THX_ck_entersub_args_scalars, &PL_sv_no);
2706 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv);
2707 cv_set_call_checker(troc_cv, Perl_ck_entersub_args_proto_or_list,
2708 (SV*)troc_cv);
2709 check_cc(troc_cv, Perl_ck_entersub_args_proto_or_list, (SV*)troc_cv);
2710 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv);
11f9f0ed
NC
2711 if (SvMAGICAL((SV*)troc_cv) || SvMAGIC((SV*)troc_cv)) croak_fail();
2712 if (SvMAGICAL((SV*)tsh_cv) || SvMAGIC((SV*)tsh_cv)) croak_fail();
d9088386
Z
2713#undef check_cc
2714
2715void
2716cv_set_call_checker_lists(CV *cv)
2717 CODE:
2718 cv_set_call_checker(cv, THX_ck_entersub_args_lists, &PL_sv_undef);
2719
2720void
2721cv_set_call_checker_scalars(CV *cv)
2722 CODE:
2723 cv_set_call_checker(cv, THX_ck_entersub_args_scalars, &PL_sv_undef);
2724
2725void
2726cv_set_call_checker_proto(CV *cv, SV *proto)
2727 CODE:
2728 if (SvROK(proto))
2729 proto = SvRV(proto);
2730 cv_set_call_checker(cv, Perl_ck_entersub_args_proto, proto);
2731
2732void
2733cv_set_call_checker_proto_or_list(CV *cv, SV *proto)
2734 CODE:
2735 if (SvROK(proto))
2736 proto = SvRV(proto);
2737 cv_set_call_checker(cv, Perl_ck_entersub_args_proto_or_list, proto);
2738
2739void
2740cv_set_call_checker_multi_sum(CV *cv)
2741 CODE:
2742 cv_set_call_checker(cv, THX_ck_entersub_multi_sum, &PL_sv_undef);
2743
2744void
20439bc7
Z
2745test_cophh()
2746 PREINIT:
2747 COPHH *a, *b;
2748 CODE:
2749#define check_ph(EXPR) \
2750 do { if((EXPR) != &PL_sv_placeholder) croak("fail"); } while(0)
2751#define check_iv(EXPR, EXPECT) \
2752 do { if(SvIV(EXPR) != (EXPECT)) croak("fail"); } while(0)
2753#define msvpvs(STR) sv_2mortal(newSVpvs(STR))
2754#define msviv(VALUE) sv_2mortal(newSViv(VALUE))
2755 a = cophh_new_empty();
2756 check_ph(cophh_fetch_pvn(a, "foo_1", 5, 0, 0));
2757 check_ph(cophh_fetch_pvs(a, "foo_1", 0));
2758 check_ph(cophh_fetch_pv(a, "foo_1", 0, 0));
2759 check_ph(cophh_fetch_sv(a, msvpvs("foo_1"), 0, 0));
2760 a = cophh_store_pvn(a, "foo_1abc", 5, 0, msviv(111), 0);
2761 a = cophh_store_pvs(a, "foo_2", msviv(222), 0);
2762 a = cophh_store_pv(a, "foo_3", 0, msviv(333), 0);
2763 a = cophh_store_sv(a, msvpvs("foo_4"), 0, msviv(444), 0);
2764 check_iv(cophh_fetch_pvn(a, "foo_1xyz", 5, 0, 0), 111);
2765 check_iv(cophh_fetch_pvs(a, "foo_1", 0), 111);
2766 check_iv(cophh_fetch_pv(a, "foo_1", 0, 0), 111);
2767 check_iv(cophh_fetch_sv(a, msvpvs("foo_1"), 0, 0), 111);
2768 check_iv(cophh_fetch_pvs(a, "foo_2", 0), 222);
2769 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
2770 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
2771 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
2772 b = cophh_copy(a);
2773 b = cophh_store_pvs(b, "foo_1", msviv(1111), 0);
2774 check_iv(cophh_fetch_pvs(a, "foo_1", 0), 111);
2775 check_iv(cophh_fetch_pvs(a, "foo_2", 0), 222);
2776 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
2777 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
2778 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
2779 check_iv(cophh_fetch_pvs(b, "foo_1", 0), 1111);
2780 check_iv(cophh_fetch_pvs(b, "foo_2", 0), 222);
2781 check_iv(cophh_fetch_pvs(b, "foo_3", 0), 333);
2782 check_iv(cophh_fetch_pvs(b, "foo_4", 0), 444);
2783 check_ph(cophh_fetch_pvs(b, "foo_5", 0));
2784 a = cophh_delete_pvn(a, "foo_1abc", 5, 0, 0);
2785 a = cophh_delete_pvs(a, "foo_2", 0);
2786 b = cophh_delete_pv(b, "foo_3", 0, 0);
2787 b = cophh_delete_sv(b, msvpvs("foo_4"), 0, 0);
2788 check_ph(cophh_fetch_pvs(a, "foo_1", 0));
2789 check_ph(cophh_fetch_pvs(a, "foo_2", 0));
2790 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
2791 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
2792 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
2793 check_iv(cophh_fetch_pvs(b, "foo_1", 0), 1111);
2794 check_iv(cophh_fetch_pvs(b, "foo_2", 0), 222);
2795 check_ph(cophh_fetch_pvs(b, "foo_3", 0));
2796 check_ph(cophh_fetch_pvs(b, "foo_4", 0));
2797 check_ph(cophh_fetch_pvs(b, "foo_5", 0));
2798 b = cophh_delete_pvs(b, "foo_3", 0);
2799 b = cophh_delete_pvs(b, "foo_5", 0);
2800 check_iv(cophh_fetch_pvs(b, "foo_1", 0), 1111);
2801 check_iv(cophh_fetch_pvs(b, "foo_2", 0), 222);
2802 check_ph(cophh_fetch_pvs(b, "foo_3", 0));
2803 check_ph(cophh_fetch_pvs(b, "foo_4", 0));
2804 check_ph(cophh_fetch_pvs(b, "foo_5", 0));
2805 cophh_free(b);
2806 check_ph(cophh_fetch_pvs(a, "foo_1", 0));
2807 check_ph(cophh_fetch_pvs(a, "foo_2", 0));
2808 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
2809 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
2810 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
2811 a = cophh_store_pvs(a, "foo_1", msviv(11111), COPHH_KEY_UTF8);
2812 a = cophh_store_pvs(a, "foo_\xaa", msviv(123), 0);
2813 a = cophh_store_pvs(a, "foo_\xc2\xbb", msviv(456), COPHH_KEY_UTF8);
2814 a = cophh_store_pvs(a, "foo_\xc3\x8c", msviv(789), COPHH_KEY_UTF8);
2815 a = cophh_store_pvs(a, "foo_\xd9\xa6", msviv(666), COPHH_KEY_UTF8);
2816 check_iv(cophh_fetch_pvs(a, "foo_1", 0), 11111);
2817 check_iv(cophh_fetch_pvs(a, "foo_1", COPHH_KEY_UTF8), 11111);
2818 check_iv(cophh_fetch_pvs(a, "foo_\xaa", 0), 123);
2819 check_iv(cophh_fetch_pvs(a, "foo_\xc2\xaa", COPHH_KEY_UTF8), 123);
2820 check_ph(cophh_fetch_pvs(a, "foo_\xc2\xaa", 0));
2821 check_iv(cophh_fetch_pvs(a, "foo_\xbb", 0), 456);
2822 check_iv(cophh_fetch_pvs(a, "foo_\xc2\xbb", COPHH_KEY_UTF8), 456);
2823 check_ph(cophh_fetch_pvs(a, "foo_\xc2\xbb", 0));
2824 check_iv(cophh_fetch_pvs(a, "foo_\xcc", 0), 789);
2825 check_iv(cophh_fetch_pvs(a, "foo_\xc3\x8c", COPHH_KEY_UTF8), 789);
2826 check_ph(cophh_fetch_pvs(a, "foo_\xc2\x8c", 0));
2827 check_iv(cophh_fetch_pvs(a, "foo_\xd9\xa6", COPHH_KEY_UTF8), 666);
2828 check_ph(cophh_fetch_pvs(a, "foo_\xd9\xa6", 0));
3987a177
Z
2829 ENTER;
2830 SAVEFREECOPHH(a);
2831 LEAVE;
20439bc7
Z
2832#undef check_ph
2833#undef check_iv
2834#undef msvpvs
2835#undef msviv
2836
8375c93e
RU
2837void
2838test_coplabel()
2839 PREINIT:
2840 COP *cop;
32708f0b
CB
2841 const char *label;
2842 STRLEN len;
2843 U32 utf8;
8375c93e
RU
2844 CODE:
2845 cop = &PL_compiling;
32708f0b
CB
2846 Perl_cop_store_label(aTHX_ cop, "foo", 3, 0);
2847 label = Perl_cop_fetch_label(aTHX_ cop, &len, &utf8);
2848 if (strcmp(label,"foo")) croak("fail # cop_fetch_label label");
2849 if (len != 3) croak("fail # cop_fetch_label len");
2850 if (utf8) croak("fail # cop_fetch_label utf8");
8375c93e 2851 /* SMALL GERMAN UMLAUT A */
a1cd495a 2852 Perl_cop_store_label(aTHX_ cop, "fo\xc3\xa4", 4, SVf_UTF8);
32708f0b 2853 label = Perl_cop_fetch_label(aTHX_ cop, &len, &utf8);
a1cd495a 2854 if (strcmp(label,"fo\xc3\xa4")) croak("fail # cop_fetch_label label");
72024711 2855 if (len != 4) croak("fail # cop_fetch_label len");
32708f0b 2856 if (!utf8) croak("fail # cop_fetch_label utf8");
8375c93e
RU
2857
2858
20439bc7
Z
2859HV *
2860example_cophh_2hv()
2861 PREINIT:
2862 COPHH *a;
2863 CODE:
2864#define msviv(VALUE) sv_2mortal(newSViv(VALUE))
2865 a = cophh_new_empty();
2866 a = cophh_store_pvs(a, "foo_0", msviv(999), 0);
2867 a = cophh_store_pvs(a, "foo_1", msviv(111), 0);
2868 a = cophh_store_pvs(a, "foo_\xaa", msviv(123), 0);
2869 a = cophh_store_pvs(a, "foo_\xc2\xbb", msviv(456), COPHH_KEY_UTF8);
2870 a = cophh_store_pvs(a, "foo_\xc3\x8c", msviv(789), COPHH_KEY_UTF8);
2871 a = cophh_store_pvs(a, "foo_\xd9\xa6", msviv(666), COPHH_KEY_UTF8);
2872 a = cophh_delete_pvs(a, "foo_0", 0);
2873 a = cophh_delete_pvs(a, "foo_2", 0);
2874 RETVAL = cophh_2hv(a, 0);
2875 cophh_free(a);
2876#undef msviv
2877 OUTPUT:
2878 RETVAL
2879
2880void
defdfed5
Z
2881test_savehints()
2882 PREINIT:
2883 SV **svp, *sv;
2884 CODE:
2885#define store_hint(KEY, VALUE) \
2886 sv_setiv_mg(*hv_fetchs(GvHV(PL_hintgv), KEY, 1), (VALUE))
2887#define hint_ok(KEY, EXPECT) \
2888 ((svp = hv_fetchs(GvHV(PL_hintgv), KEY, 0)) && \
2889 (sv = *svp) && SvIV(sv) == (EXPECT) && \
20439bc7 2890 (sv = cop_hints_fetch_pvs(&PL_compiling, KEY, 0)) && \
defdfed5
Z
2891 SvIV(sv) == (EXPECT))
2892#define check_hint(KEY, EXPECT) \
11f9f0ed 2893 do { if (!hint_ok(KEY, EXPECT)) croak_fail(); } while(0)
defdfed5
Z
2894 PL_hints |= HINT_LOCALIZE_HH;
2895 ENTER;
2896 SAVEHINTS();
2897 PL_hints &= HINT_INTEGER;
2898 store_hint("t0", 123);
2899 store_hint("t1", 456);
11f9f0ed 2900 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2901 check_hint("t0", 123); check_hint("t1", 456);
2902 ENTER;
2903 SAVEHINTS();
11f9f0ed 2904 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2905 check_hint("t0", 123); check_hint("t1", 456);
2906 PL_hints |= HINT_INTEGER;
2907 store_hint("t0", 321);
11f9f0ed 2908 if (!(PL_hints & HINT_INTEGER)) croak_fail();
defdfed5
Z
2909 check_hint("t0", 321); check_hint("t1", 456);
2910 LEAVE;
11f9f0ed 2911 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2912 check_hint("t0", 123); check_hint("t1", 456);
2913 ENTER;
2914 SAVEHINTS();
11f9f0ed 2915 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2916 check_hint("t0", 123); check_hint("t1", 456);
2917 store_hint("t1", 654);
11f9f0ed 2918 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2919 check_hint("t0", 123); check_hint("t1", 654);
2920 LEAVE;
11f9f0ed 2921 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2922 check_hint("t0", 123); check_hint("t1", 456);
2923 LEAVE;
2924#undef store_hint
2925#undef hint_ok
2926#undef check_hint
2927
2928void
2929test_copyhints()
2930 PREINIT:
2931 HV *a, *b;
2932 CODE:
2933 PL_hints |= HINT_LOCALIZE_HH;
2934 ENTER;
2935 SAVEHINTS();
2936 sv_setiv_mg(*hv_fetchs(GvHV(PL_hintgv), "t0", 1), 123);
20439bc7
Z
2937 if (SvIV(cop_hints_fetch_pvs(&PL_compiling, "t0", 0)) != 123)
2938 croak_fail();
defdfed5
Z
2939 a = newHVhv(GvHV(PL_hintgv));
2940 sv_2mortal((SV*)a);
2941 sv_setiv_mg(*hv_fetchs(a, "t0", 1), 456);
20439bc7
Z
2942 if (SvIV(cop_hints_fetch_pvs(&PL_compiling, "t0", 0)) != 123)
2943 croak_fail();
defdfed5
Z
2944 b = hv_copy_hints_hv(a);
2945 sv_2mortal((SV*)b);
2946 sv_setiv_mg(*hv_fetchs(b, "t0", 1), 789);
20439bc7
Z
2947 if (SvIV(cop_hints_fetch_pvs(&PL_compiling, "t0", 0)) != 789)
2948 croak_fail();
defdfed5
Z
2949 LEAVE;
2950
201c7e1f 2951void
2fcb4757
Z
2952test_op_list()
2953 PREINIT:
2954 OP *a;
2955 CODE:
2956#define iv_op(iv) newSVOP(OP_CONST, 0, newSViv(iv))
2957#define check_op(o, expect) \
2958 do { \
2959 if (strcmp(test_op_list_describe(o), (expect))) \
2960 croak("fail %s %s", test_op_list_describe(o), (expect)); \
2961 } while(0)
2962 a = op_append_elem(OP_LIST, NULL, NULL);
2963 check_op(a, "");
2964 a = op_append_elem(OP_LIST, iv_op(1), a);
2965 check_op(a, "const(1).");
2966 a = op_append_elem(OP_LIST, NULL, a);
2967 check_op(a, "const(1).");
2968 a = op_append_elem(OP_LIST, a, iv_op(2));
2969 check_op(a, "list[pushmark.const(1).const(2).]");
2970 a = op_append_elem(OP_LIST, a, iv_op(3));
2971 check_op(a, "list[pushmark.const(1).const(2).const(3).]");
2972 a = op_append_elem(OP_LIST, a, NULL);
2973 check_op(a, "list[pushmark.const(1).const(2).const(3).]");
2974 a = op_append_elem(OP_LIST, NULL, a);
2975 check_op(a, "list[pushmark.const(1).const(2).const(3).]");
2976 a = op_append_elem(OP_LIST, iv_op(4), a);
2977 check_op(a, "list[pushmark.const(4)."
2978 "list[pushmark.const(1).const(2).const(3).]]");
2979 a = op_append_elem(OP_LIST, a, iv_op(5));
2980 check_op(a, "list[pushmark.const(4)."
2981 "list[pushmark.const(1).const(2).const(3).]const(5).]");
2982 a = op_append_elem(OP_LIST, a,
2983 op_append_elem(OP_LIST, iv_op(7), iv_op(6)));
2984 check_op(a, "list[pushmark.const(4)."
2985 "list[pushmark.const(1).const(2).const(3).]const(5)."
2986 "list[pushmark.const(7).const(6).]]");
2987 op_free(a);
2988 a = op_append_elem(OP_LINESEQ, iv_op(1), iv_op(2));
2989 check_op(a, "lineseq[const(1).const(2).]");
2990 a = op_append_elem(OP_LINESEQ, a, iv_op(3));
2991 check_op(a, "lineseq[const(1).const(2).const(3).]");
2992 op_free(a);
2993 a = op_append_elem(OP_LINESEQ,
2994 op_append_elem(OP_LIST, iv_op(1), iv_op(2)),
2995 iv_op(3));
2996 check_op(a, "lineseq[list[pushmark.const(1).const(2).]const(3).]");
2997 op_free(a);
2998 a = op_prepend_elem(OP_LIST, NULL, NULL);
2999 check_op(a, "");
3000 a = op_prepend_elem(OP_LIST, a, iv_op(1));
3001 check_op(a, "const(1).");
3002 a = op_prepend_elem(OP_LIST, a, NULL);
3003 check_op(a, "const(1).");
3004 a = op_prepend_elem(OP_LIST, iv_op(2), a);
3005 check_op(a, "list[pushmark.const(2).const(1).]");
3006 a = op_prepend_elem(OP_LIST, iv_op(3), a);
3007 check_op(a, "list[pushmark.const(3).const(2).const(1).]");
3008 a = op_prepend_elem(OP_LIST, NULL, a);
3009 check_op(a, "list[pushmark.const(3).const(2).const(1).]");
3010 a = op_prepend_elem(OP_LIST, a, NULL);
3011 check_op(a, "list[pushmark.const(3).const(2).const(1).]");
3012 a = op_prepend_elem(OP_LIST, a, iv_op(4));
3013 check_op(a, "list[pushmark."
3014 "list[pushmark.const(3).const(2).const(1).]const(4).]");
3015 a = op_prepend_elem(OP_LIST, iv_op(5), a);
3016 check_op(a, "list[pushmark.const(5)."
3017 "list[pushmark.const(3).const(2).const(1).]const(4).]");
3018 a = op_prepend_elem(OP_LIST,
3019 op_prepend_elem(OP_LIST, iv_op(6), iv_op(7)), a);
3020 check_op(a, "list[pushmark.list[pushmark.const(6).const(7).]const(5)."
3021 "list[pushmark.const(3).const(2).const(1).]const(4).]");
3022 op_free(a);
3023 a = op_prepend_elem(OP_LINESEQ, iv_op(2), iv_op(1));
3024 check_op(a, "lineseq[const(2).const(1).]");
3025 a = op_prepend_elem(OP_LINESEQ, iv_op(3), a);
3026 check_op(a, "lineseq[const(3).const(2).const(1).]");
3027 op_free(a);
3028 a = op_prepend_elem(OP_LINESEQ, iv_op(3),
3029 op_prepend_elem(OP_LIST, iv_op(2), iv_op(1)));
3030 check_op(a, "lineseq[const(3).list[pushmark.const(2).const(1).]]");
3031 op_free(a);
3032 a = op_append_list(OP_LINESEQ, NULL, NULL);
3033 check_op(a, "");
3034 a = op_append_list(OP_LINESEQ, iv_op(1), a);
3035 check_op(a, "const(1).");
3036 a = op_append_list(OP_LINESEQ, NULL, a);
3037 check_op(a, "const(1).");
3038 a = op_append_list(OP_LINESEQ, a, iv_op(2));
3039 check_op(a, "lineseq[const(1).const(2).]");
3040 a = op_append_list(OP_LINESEQ, a, iv_op(3));
3041 check_op(a, "lineseq[const(1).const(2).const(3).]");
3042 a = op_append_list(OP_LINESEQ, iv_op(4), a);
3043 check_op(a, "lineseq[const(4).const(1).const(2).const(3).]");
3044 a = op_append_list(OP_LINESEQ, a, NULL);
3045 check_op(a, "lineseq[const(4).const(1).const(2).const(3).]");
3046 a = op_append_list(OP_LINESEQ, NULL, a);
3047 check_op(a, "lineseq[const(4).const(1).const(2).const(3).]");
3048 a = op_append_list(OP_LINESEQ, a,
3049 op_append_list(OP_LINESEQ, iv_op(5), iv_op(6)));
3050 check_op(a, "lineseq[const(4).const(1).const(2).const(3)."
3051 "const(5).const(6).]");
3052 op_free(a);
3053 a = op_append_list(OP_LINESEQ,
3054 op_append_list(OP_LINESEQ, iv_op(1), iv_op(2)),
3055 op_append_list(OP_LIST, iv_op(3), iv_op(4)));
3056 check_op(a, "lineseq[const(1).const(2)."
3057 "list[pushmark.const(3).const(4).]]");
3058 op_free(a);
3059 a = op_append_list(OP_LINESEQ,
3060 op_append_list(OP_LIST, iv_op(1), iv_op(2)),
3061 op_append_list(OP_LINESEQ, iv_op(3), iv_op(4)));
3062 check_op(a, "lineseq[list[pushmark.const(1).const(2).]"
3063 "const(3).const(4).]");
3064 op_free(a);
2fcb4757
Z
3065#undef check_op
3066
3067void
5983a79d
BM
3068test_op_linklist ()
3069 PREINIT:
3070 OP *o;
3071 CODE:
3072#define check_ll(o, expect) \
3073 STMT_START { \
3074 if (strNE(test_op_linklist_describe(o), (expect))) \
3075 croak("fail %s %s", test_op_linklist_describe(o), (expect)); \
3076 } STMT_END
3077 o = iv_op(1);
3078 check_ll(o, ".const1");
3079 op_free(o);
3080
3081 o = mkUNOP(OP_NOT, iv_op(1));
3082 check_ll(o, ".const1.not");
3083 op_free(o);
3084
3085 o = mkUNOP(OP_NOT, mkUNOP(OP_NEGATE, iv_op(1)));
3086 check_ll(o, ".const1.negate.not");
3087 op_free(o);
3088
3089 o = mkBINOP(OP_ADD, iv_op(1), iv_op(2));
3090 check_ll(o, ".const1.const2.add");
3091 op_free(o);
3092
3093 o = mkBINOP(OP_ADD, mkUNOP(OP_NOT, iv_op(1)), iv_op(2));
3094 check_ll(o, ".const1.not.const2.add");
3095 op_free(o);
3096
3097 o = mkUNOP(OP_NOT, mkBINOP(OP_ADD, iv_op(1), iv_op(2)));
3098 check_ll(o, ".const1.const2.add.not");
3099 op_free(o);
3100
3101 o = mkLISTOP(OP_LINESEQ, iv_op(1), iv_op(2), iv_op(3));
3102 check_ll(o, ".const1.const2.const3.lineseq");
3103 op_free(o);
3104
3105 o = mkLISTOP(OP_LINESEQ,
3106 mkBINOP(OP_ADD, iv_op(1), iv_op(2)),
3107 mkUNOP(OP_NOT, iv_op(3)),
3108 mkLISTOP(OP_SUBSTR, iv_op(4), iv_op(5), iv_op(6)));
3109 check_ll(o, ".const1.const2.add.const3.not"
3110 ".const4.const5.const6.substr.lineseq");
3111 op_free(o);
3112
3113 o = mkBINOP(OP_ADD, iv_op(1), iv_op(2));
3114 LINKLIST(o);
3115 o = mkBINOP(OP_SUBTRACT, o, iv_op(3));
3116 check_ll(o, ".const1.const2.add.const3.subtract");
3117 op_free(o);
3118#undef check_ll
3119#undef iv_op
3120
3121void
201c7e1f
FR
3122peep_enable ()
3123 PREINIT:
3124 dMY_CXT;
3125 CODE:
3126 av_clear(MY_CXT.peep_recorder);
3127 av_clear(MY_CXT.rpeep_recorder);
3128 MY_CXT.peep_recording = 1;
3129
3130void
3131peep_disable ()
3132 PREINIT:
3133 dMY_CXT;
3134 CODE:
3135 MY_CXT.peep_recording = 0;
3136
3137SV *
3138peep_record ()
3139 PREINIT:
3140 dMY_CXT;
3141 CODE:
95d2461a 3142 RETVAL = newRV_inc((SV *)MY_CXT.peep_recorder);
201c7e1f
FR
3143 OUTPUT:
3144 RETVAL
3145
3146SV *
3147rpeep_record ()
3148 PREINIT:
3149 dMY_CXT;
3150 CODE:
95d2461a 3151 RETVAL = newRV_inc((SV *)MY_CXT.rpeep_recorder);
201c7e1f
FR
3152 OUTPUT:
3153 RETVAL
3154
9c540340
DM
3155=pod
3156
3157multicall_each: call a sub for each item in the list. Used to test MULTICALL
3158
3159=cut
3160
3161void
3162multicall_each(block,...)
3163 SV * block
3164PROTOTYPE: &@
3165CODE:
3166{
3167 dMULTICALL;
3168 int index;
3169 GV *gv;
3170 HV *stash;
3171 I32 gimme = G_SCALAR;
3172 SV **args = &PL_stack_base[ax];
3173 CV *cv;
3174
3175 if(items <= 1) {
3176 XSRETURN_UNDEF;
3177 }
3178 cv = sv_2cv(block, &stash, &gv, 0);
3179 if (cv == Nullcv) {
3180 croak("multicall_each: not a subroutine reference");
3181 }
3182 PUSH_MULTICALL(cv);
3183 SAVESPTR(GvSV(PL_defgv));
3184
3185 for(index = 1 ; index < items ; index++) {
3186 GvSV(PL_defgv) = args[index];
3187 MULTICALL;
3188 }
3189 POP_MULTICALL;
5f74c55e 3190 PERL_UNUSED_VAR(newsp);
9c540340
DM
3191 XSRETURN_UNDEF;
3192}
3193
7b81e549
GG
3194#ifdef USE_ITHREADS
3195
3196void
3197clone_with_stack()
3198CODE:
3199{
3200 PerlInterpreter *interp = aTHX; /* The original interpreter */
3201 PerlInterpreter *interp_dup; /* The duplicate interpreter */
3202 int oldscope = 1; /* We are responsible for all scopes */
3203
3204 interp_dup = perl_clone(interp, CLONEf_COPY_STACKS | CLONEf_CLONE_HOST );
3205
3206 /* destroy old perl */
3207 PERL_SET_CONTEXT(interp);
3208
3209 POPSTACK_TO(PL_mainstack);
3210 dounwind(-1);
3211 LEAVE_SCOPE(0);
3212
3213 while (interp->Iscopestack_ix > 1)
3214 LEAVE;
3215 FREETMPS;
3216
3217 perl_destruct(interp);
3218 perl_free(interp);
3219
3220 /* switch to new perl */
3221 PERL_SET_CONTEXT(interp_dup);
3222
3223 /* continue after 'clone_with_stack' */
4a808ed1
MS
3224 if (interp_dup->Iop)
3225 interp_dup->Iop = interp_dup->Iop->op_next;
7b81e549
GG
3226
3227 /* run with new perl */
3228 Perl_runops_standard(interp_dup);
3229
3230 /* We may have additional unclosed scopes if fork() was called
3231 * from within a BEGIN block. See perlfork.pod for more details.
3232 * We cannot clean up these other scopes because they belong to a
3233 * different interpreter, but we also cannot leave PL_scopestack_ix
3234 * dangling because that can trigger an assertion in perl_destruct().
3235 */
3236 if (PL_scopestack_ix > oldscope) {
3237 PL_scopestack[oldscope-1] = PL_scopestack[PL_scopestack_ix-1];
3238 PL_scopestack_ix = oldscope;
3239 }
3240
3241 perl_destruct(interp_dup);
3242 perl_free(interp_dup);
3243
3244 /* call the real 'exit' not PerlProc_exit */
3245#undef exit
3246 exit(0);
3247}
3248
3249#endif /* USE_ITHREDS */
9c540340 3250
88b5a879
GF
3251SV*
3252take_svref(SVREF sv)
3253CODE:
3254 RETVAL = newRV_inc(sv);
3255OUTPUT:
3256 RETVAL
3257
3258SV*
3259take_avref(AV* av)
3260CODE:
3261 RETVAL = newRV_inc((SV*)av);
3262OUTPUT:
3263 RETVAL
3264
3265SV*
3266take_hvref(HV* hv)
3267CODE:
3268 RETVAL = newRV_inc((SV*)hv);
3269OUTPUT:
3270 RETVAL
3271
3272
3273SV*
3274take_cvref(CV* cv)
3275CODE:
3276 RETVAL = newRV_inc((SV*)cv);
3277OUTPUT:
3278 RETVAL
3279
3280
e2fe06dd
EB
3281BOOT:
3282 {
3283 HV* stash;
3284 SV** meth = NULL;
3285 CV* cv;
3286 stash = gv_stashpv("XS::APItest::TempLv", 0);
3287 if (stash)
3288 meth = hv_fetchs(stash, "make_temp_mg_lv", 0);
3289 if (!meth)
3290 croak("lost method 'make_temp_mg_lv'");
3291 cv = GvCV(*meth);
3292 CvLVALUE_on(cv);
3293 }
83f8bb56
Z
3294
3295BOOT:
3296{
3297 hintkey_rpn_sv = newSVpvs_share("XS::APItest/rpn");
3298 hintkey_calcrpn_sv = newSVpvs_share("XS::APItest/calcrpn");
3299 hintkey_stufftest_sv = newSVpvs_share("XS::APItest/stufftest");
3300 hintkey_swaptwostmts_sv = newSVpvs_share("XS::APItest/swaptwostmts");
07ffcb73 3301 hintkey_looprest_sv = newSVpvs_share("XS::APItest/looprest");
a7aaec61 3302 hintkey_scopelessblock_sv = newSVpvs_share("XS::APItest/scopelessblock");
9eb5c532
Z
3303 hintkey_stmtasexpr_sv = newSVpvs_share("XS::APItest/stmtasexpr");
3304 hintkey_stmtsasexpr_sv = newSVpvs_share("XS::APItest/stmtsasexpr");
e53d8f76
Z
3305 hintkey_loopblock_sv = newSVpvs_share("XS::APItest/loopblock");
3306 hintkey_blockasexpr_sv = newSVpvs_share("XS::APItest/blockasexpr");
361d9b55
Z
3307 hintkey_swaplabel_sv = newSVpvs_share("XS::APItest/swaplabel");
3308 hintkey_labelconst_sv = newSVpvs_share("XS::APItest/labelconst");
78cdf107
Z
3309 hintkey_arrayfullexpr_sv = newSVpvs_share("XS::APItest/arrayfullexpr");
3310 hintkey_arraylistexpr_sv = newSVpvs_share("XS::APItest/arraylistexpr");
3311 hintkey_arraytermexpr_sv = newSVpvs_share("XS::APItest/arraytermexpr");
3312 hintkey_arrayarithexpr_sv = newSVpvs_share("XS::APItest/arrayarithexpr");
3313 hintkey_arrayexprflags_sv = newSVpvs_share("XS::APItest/arrayexprflags");
83f8bb56
Z
3314 next_keyword_plugin = PL_keyword_plugin;
3315 PL_keyword_plugin = my_keyword_plugin;
3316}
8f89e5a9
Z
3317
3318void
3319establish_cleanup(...)
3320PROTOTYPE: $
3321CODE:
c33e8be1 3322 PERL_UNUSED_VAR(items);
8f89e5a9
Z
3323 croak("establish_cleanup called as a function");
3324
3325BOOT:
3326{
3327 CV *estcv = get_cv("XS::APItest::establish_cleanup", 0);
3328 cv_set_call_checker(estcv, THX_ck_entersub_establish_cleanup, (SV*)estcv);
3329}
3ad73efd
Z
3330
3331void
3332postinc(...)
3333PROTOTYPE: $
3334CODE:
c33e8be1 3335 PERL_UNUSED_VAR(items);
3ad73efd
Z
3336 croak("postinc called as a function");
3337
27fcb6ee
FC
3338void
3339filter()
3340CODE:
3341 filter_add(filter_call, NULL);
3342
3ad73efd
Z
3343BOOT:
3344{
3345 CV *asscv = get_cv("XS::APItest::postinc", 0);
3346 cv_set_call_checker(asscv, THX_ck_entersub_postinc, (SV*)asscv);
3347}
26ab20ee 3348
fba8e77b
FC
3349SV *
3350lv_temp_object()
3351CODE:
3352 RETVAL =
3353 sv_bless(
3354 newRV_noinc(newSV(0)),
3355 gv_stashpvs("XS::APItest::TempObj",GV_ADD)
3356 ); /* Package defined in test script */
3357OUTPUT:
3358 RETVAL
3359
7d6175ef 3360void
65b22876 3361fill_hash_with_nulls(HV *hv)
a2bf5ab1 3362PREINIT:
7d6175ef 3363 UV i = 0;
a2bf5ab1 3364CODE:
7d6175ef
FC
3365 for(; i < 1000; ++i) {
3366 HE *entry = hv_fetch_ent(hv, sv_2mortal(newSVuv(i)), 1, 0);
3367 SvREFCNT_dec(HeVAL(entry));
3368 HeVAL(entry) = NULL;
3369 }
3370
3f4d1d78
FC
3371HV *
3372newHVhv(HV *hv)
3373CODE:
3374 RETVAL = newHVhv(hv);
3375OUTPUT:
3376 RETVAL
3377
dd82de44 3378U32
fa819c1c
FC
3379SvIsCOW(SV *sv)
3380CODE:
3381 RETVAL = SvIsCOW(sv);
3382OUTPUT:
3383 RETVAL
3384
15103811
Z
3385void
3386pad_scalar(...)
3387PROTOTYPE: $$
3388CODE:
3389 PERL_UNUSED_VAR(items);
3390 croak("pad_scalar called as a function");
3391
3392BOOT:
3393{
3394 CV *pscv = get_cv("XS::APItest::pad_scalar", 0);
3395 cv_set_call_checker(pscv, THX_ck_entersub_pad_scalar, (SV*)pscv);
3396}
3397
ab8f91e9
BF
3398SV*
3399fetch_pad_names( cv )
3400CV* cv
3401 PREINIT:
3402 I32 i;
35e035cc 3403 PADNAMELIST *pad_namelist;
ab8f91e9
BF
3404 AV *retav = newAV();
3405 CODE:
86d2498c 3406 pad_namelist = PadlistNAMES(CvPADLIST(cv));
ab8f91e9 3407
86d2498c
FC
3408 for ( i = PadnamelistMAX(pad_namelist); i >= 0; i-- ) {
3409 PADNAME* name = PadnamelistARRAY(pad_namelist)[i];
ab8f91e9 3410
325e1816 3411 if (PadnameLEN(name)) {
35e035cc 3412 av_push(retav, newSVpadname(name));
ab8f91e9
BF
3413 }
3414 }
3415 RETVAL = newRV_noinc((SV*)retav);
3416 OUTPUT:
3417 RETVAL
3418
c12735bb
Z
3419STRLEN
3420underscore_length()
3421PROTOTYPE:
3422PREINIT:
3423 SV *u;
3424 U8 *pv;
3425 STRLEN bytelen;
3426CODE:
3427 u = find_rundefsv();
3428 pv = (U8*)SvPV(u, bytelen);
3429 RETVAL = SvUTF8(u) ? utf8_length(pv, pv+bytelen) : bytelen;
3430OUTPUT:
3431 RETVAL
3432
e8ed61c5
FC
3433void
3434stringify(SV *sv)
e8ed61c5 3435CODE:
5f74c55e 3436 (void)SvPV_nolen(sv);
e8ed61c5 3437
c682ebef
FC
3438SV *
3439HvENAME(HV *hv)
3440CODE:
3441 RETVAL = hv && HvENAME(hv)
3442 ? newSVpvn_flags(
3443 HvENAME(hv),HvENAMELEN(hv),
3444 (HvENAMEUTF8(hv) ? SVf_UTF8 : 0)
3445 )
3446 : NULL;
3447OUTPUT:
3448 RETVAL
3449
2fc49ef1
FC
3450int
3451xs_cmp(int a, int b)
3452CODE:
3453 /* Odd sorting (odd numbers first), to make sure we are actually
3454 being called */
3455 RETVAL = a % 2 != b % 2
3456 ? a % 2 ? -1 : 1
3457 : a < b ? -1 : a == b ? 0 : 1;
3458OUTPUT:
3459 RETVAL
3460
c19fd8b4
FC
3461SV *
3462xs_cmp_undef(SV *a, SV *b)
3463CODE:
5f74c55e
DM
3464 PERL_UNUSED_ARG(a);
3465 PERL_UNUSED_ARG(b);
c19fd8b4
FC
3466 RETVAL = &PL_sv_undef;
3467OUTPUT:
3468 RETVAL
3469
fe46cbda
FC
3470char *
3471SvPVbyte(SV *sv)
3472CODE:
3473 RETVAL = SvPVbyte_nolen(sv);
3474OUTPUT:
3475 RETVAL
3476
3477char *
3478SvPVutf8(SV *sv)
3479CODE:
3480 RETVAL = SvPVutf8_nolen(sv);
3481OUTPUT:
3482 RETVAL
3483
e8570548
Z
3484void
3485setup_addissub()
3486CODE:
3487 wrap_op_checker(OP_ADD, addissub_myck_add, &addissub_nxck_add);
e8ed61c5 3488
39c012bc
FC
3489void
3490setup_rv2cv_addunderbar()
3491CODE:
3492 wrap_op_checker(OP_RV2CV, my_ck_rv2cv, &old_ck_rv2cv);
3493
c0b8aebd
FC
3494#ifdef USE_ITHREADS
3495
3496bool
3497test_alloccopstash()
3498CODE:
3499 RETVAL = PL_stashpad[alloccopstash(PL_defstash)] == PL_defstash;
3500OUTPUT:
3501 RETVAL
3502
3503#endif
3504
b448305b
FC
3505bool
3506test_newFOROP_without_slab()
3507CODE:
3508 {
3509 const I32 floor = start_subparse(0,0);
b448305b
FC
3510 /* The slab allocator does not like CvROOT being set. */
3511 CvROOT(PL_compcv) = (OP *)1;
3512 op_free(newFOROP(0, 0, newOP(OP_PUSHMARK, 0), 0, 0));
3513 CvROOT(PL_compcv) = NULL;
3514 SvREFCNT_dec(PL_compcv);
3515 LEAVE_SCOPE(floor);
3516 /* If we have not crashed yet, then the test passes. */
3517 RETVAL = TRUE;
3518 }
3519OUTPUT:
3520 RETVAL
3521
7016d6eb
DM
3522 # provide access to CALLREGEXEC, except replace pointers within the
3523 # string with offsets from the start of the string
3524
3525I32
3526callregexec(SV *prog, STRLEN stringarg, STRLEN strend, I32 minend, SV *sv, U32 nosave)
3527CODE:
3528 {
3529 STRLEN len;
3530 char *strbeg;
3531 if (SvROK(prog))
3532 prog = SvRV(prog);
3533 strbeg = SvPV_force(sv, len);
3534 RETVAL = CALLREGEXEC((REGEXP *)prog,
3535 strbeg + stringarg,
3536 strbeg + strend,
3537 strbeg,
3538 minend,
3539 sv,
3540 NULL, /* data */
3541 nosave);
3542 }
3543OUTPUT:
3544 RETVAL
3545
0be5d18d
FC
3546void
3547lexical_import(SV *name, CV *cv)
3548 CODE:
3549 {
3550 PADLIST *pl;
3551 PADOFFSET off;
3552 if (!PL_compcv)
3553 Perl_croak(aTHX_
3554 "lexical_import can only be called at compile time");
3555 pl = CvPADLIST(PL_compcv);
3556 ENTER;
3557 SAVESPTR(PL_comppad_name); PL_comppad_name = PadlistNAMES(pl);
3558 SAVESPTR(PL_comppad); PL_comppad = PadlistARRAY(pl)[1];
3559 SAVESPTR(PL_curpad); PL_curpad = PadARRAY(PL_comppad);
1af585b3
FC
3560 off = pad_add_name_sv(sv_2mortal(newSVpvf("&%"SVf,name)),
3561 padadd_STATE, 0, 0);
0be5d18d
FC
3562 SvREFCNT_dec(PL_curpad[off]);
3563 PL_curpad[off] = SvREFCNT_inc(cv);
3564 LEAVE;
3565 }
3566
108cb980
FC
3567SV *
3568sv_mortalcopy(SV *sv)
3569 CODE:
3570 RETVAL = SvREFCNT_inc(sv_mortalcopy(sv));
3571 OUTPUT:
3572 RETVAL
b448305b 3573
3455055f
FC
3574SV *
3575newRV(SV *sv)
3576
031c6d82
FC
3577void
3578alias_av(AV *av, IV ix, SV *sv)
3579 CODE:
3580 av_store(av, ix, SvREFCNT_inc(sv));
3581
120b7a08
S
3582MODULE = XS::APItest PACKAGE = XS::APItest::AUTOLOADtest
3583
3584int
3585AUTOLOAD(...)
3586 INIT:
3587 SV* comms;
120b7a08 3588 SV* class_and_method;
120b7a08 3589 CODE:
5f74c55e 3590 PERL_UNUSED_ARG(items);
bb619f37 3591 class_and_method = GvSV(CvGV(cv));
120b7a08
S
3592 comms = get_sv("main::the_method", 1);
3593 if (class_and_method == NULL) {
3594 RETVAL = 1;
3595 } else if (!SvOK(class_and_method)) {
3596 RETVAL = 2;
3597 } else if (!SvPOK(class_and_method)) {
3598 RETVAL = 3;
3599 } else {
bb619f37 3600 sv_setsv(comms, class_and_method);
120b7a08
S
3601 RETVAL = 0;
3602 }
3603 OUTPUT: RETVAL
3604
3605
26ab20ee
FR
3606MODULE = XS::APItest PACKAGE = XS::APItest::Magic
3607
3608PROTOTYPES: DISABLE
3609
3610void
3611sv_magic_foo(SV *sv, SV *thingy)
3612ALIAS:
3613 sv_magic_bar = 1
3614CODE:
3615 sv_magicext(SvRV(sv), NULL, PERL_MAGIC_ext, ix ? &vtbl_bar : &vtbl_foo, (const char *)thingy, 0);
3616
3617SV *
3618mg_find_foo(SV *sv)
3619ALIAS:
3620 mg_find_bar = 1
3621CODE:
3622 MAGIC *mg = mg_findext(SvRV(sv), PERL_MAGIC_ext, ix ? &vtbl_bar : &vtbl_foo);
3623 RETVAL = mg ? SvREFCNT_inc((SV *)mg->mg_ptr) : &PL_sv_undef;
3624OUTPUT:
3625 RETVAL
3626
3627void
3628sv_unmagic_foo(SV *sv)
3629ALIAS:
3630 sv_unmagic_bar = 1
3631CODE:
3632 sv_unmagicext(SvRV(sv), PERL_MAGIC_ext, ix ? &vtbl_bar : &vtbl_foo);
406700cc
NC
3633
3634UV
3635test_get_vtbl()
3636 PREINIT:
3637 MGVTBL *have;
3638 MGVTBL *want;
3639 CODE:
3640#define test_get_this_vtable(name) \
3641 want = CAT2(&PL_vtbl_, name); \
3642 have = get_vtbl(CAT2(want_vtbl_, name)); \
3643 if (have != want) \
3644 croak("fail %p!=%p for get_vtbl(want_vtbl_" STRINGIFY(name) ") at " __FILE__ " line %d", have, want, __LINE__)
3645
3646 test_get_this_vtable(sv);
3647 test_get_this_vtable(env);
3648 test_get_this_vtable(envelem);
3649 test_get_this_vtable(sigelem);
3650 test_get_this_vtable(pack);
3651 test_get_this_vtable(packelem);
3652 test_get_this_vtable(dbline);
3653 test_get_this_vtable(isa);
3654 test_get_this_vtable(isaelem);
3655 test_get_this_vtable(arylen);
3656 test_get_this_vtable(mglob);
3657 test_get_this_vtable(nkeys);
3658 test_get_this_vtable(taint);
3659 test_get_this_vtable(substr);
3660 test_get_this_vtable(vec);
3661 test_get_this_vtable(pos);
3662 test_get_this_vtable(bm);
3663 test_get_this_vtable(fm);
3664 test_get_this_vtable(uvar);
3665 test_get_this_vtable(defelem);
3666 test_get_this_vtable(regexp);
3667 test_get_this_vtable(regdata);
3668 test_get_this_vtable(regdatum);
3669#ifdef USE_LOCALE_COLLATE
3670 test_get_this_vtable(collxfrm);
3671#endif
406700cc
NC
3672 test_get_this_vtable(backref);
3673 test_get_this_vtable(utf8);
3674
3675 RETVAL = PTR2UV(get_vtbl(-1));
3676 OUTPUT:
3677 RETVAL
bdd8600f
KW
3678
3679bool
3680test_isBLANK_uni(UV ord)
3681 CODE:
3682 RETVAL = isBLANK_uni(ord);
3683 OUTPUT:
3684 RETVAL
3685
3686bool
981746b9
KW
3687test_isBLANK_LC_uvchr(UV ord)
3688 CODE:
3689 RETVAL = isBLANK_LC_uvchr(ord);
3690 OUTPUT:
3691 RETVAL
3692
3693bool
c9c05358 3694test_isBLANK_A(UV ord)
bdd8600f 3695 CODE:
c9c05358
KW
3696 RETVAL = isBLANK_A(ord);
3697 OUTPUT:
3698 RETVAL
3699
3700bool
3701test_isBLANK_L1(UV ord)
3702 CODE:
3703 RETVAL = isBLANK_L1(ord);
3704 OUTPUT:
3705 RETVAL
3706
3707bool
981746b9
KW
3708test_isBLANK_LC(UV ord)
3709 CODE:
3710 RETVAL = isBLANK_LC(ord);
3711 OUTPUT:
3712 RETVAL
3713
3714bool
c9c05358
KW
3715test_isBLANK_utf8(unsigned char * p)
3716 CODE:
3717 RETVAL = isBLANK_utf8(p);
3718 OUTPUT:
3719 RETVAL
3720
3721bool
981746b9
KW
3722test_isBLANK_LC_utf8(unsigned char * p)
3723 CODE:
3724 RETVAL = isBLANK_LC_utf8(p);
3725 OUTPUT:
3726 RETVAL
3727
3728bool
840f8e92
KW
3729test_isVERTWS_uni(UV ord)
3730 CODE:
3731 RETVAL = isVERTWS_uni(ord);
3732 OUTPUT:
3733 RETVAL
3734
3735bool
3736test_isVERTWS_utf8(unsigned char * p)
3737 CODE:
3738 RETVAL = isVERTWS_utf8(p);
3739 OUTPUT:
3740 RETVAL
3741
3742bool
c9c05358
KW
3743test_isUPPER_uni(UV ord)
3744 CODE:
3745 RETVAL = isUPPER_uni(ord);
3746 OUTPUT:
3747 RETVAL
3748
3749bool
981746b9
KW
3750test_isUPPER_LC_uvchr(UV ord)
3751 CODE:
3752 RETVAL = isUPPER_LC_uvchr(ord);
3753 OUTPUT:
3754 RETVAL
3755
3756bool
c9c05358
KW
3757test_isUPPER_A(UV ord)
3758 CODE:
3759 RETVAL = isUPPER_A(ord);
3760 OUTPUT:
3761 RETVAL
3762
3763bool
3764test_isUPPER_L1(UV ord)
3765 CODE:
3766 RETVAL = isUPPER_L1(ord);
3767 OUTPUT:
3768 RETVAL
3769
3770bool
981746b9
KW
3771test_isUPPER_LC(UV ord)
3772 CODE:
3773 RETVAL = isUPPER_LC(ord);
3774 OUTPUT:
3775 RETVAL
3776
3777bool
c9c05358
KW
3778test_isUPPER_utf8(unsigned char * p)
3779 CODE:
3780 RETVAL = isUPPER_utf8( p);
3781 OUTPUT:
3782 RETVAL
3783
3784bool
981746b9
KW
3785test_isUPPER_LC_utf8(unsigned char * p)
3786 CODE:
3787 RETVAL = isUPPER_LC_utf8( p);
3788 OUTPUT:
3789 RETVAL
3790
3791bool
c9c05358
KW
3792test_isLOWER_uni(UV ord)
3793 CODE:
3794 RETVAL = isLOWER_uni(ord);
3795 OUTPUT:
3796 RETVAL
3797
3798bool
981746b9
KW
3799test_isLOWER_LC_uvchr(UV ord)
3800 CODE:
3801 RETVAL = isLOWER_LC_uvchr(ord);
3802 OUTPUT:
3803 RETVAL
3804
3805bool
c9c05358
KW
3806test_isLOWER_A(UV ord)
3807 CODE:
3808 RETVAL = isLOWER_A(ord);
3809 OUTPUT:
3810 RETVAL
3811
3812bool
3813test_isLOWER_L1(UV ord)
3814 CODE:
3815 RETVAL = isLOWER_L1(ord);
3816 OUTPUT:
3817 RETVAL
3818
3819bool
981746b9
KW
3820test_isLOWER_LC(UV ord)
3821 CODE:
3822 RETVAL = isLOWER_LC(ord);
3823 OUTPUT:
3824 RETVAL
3825
3826bool
c9c05358
KW
3827test_isLOWER_utf8(unsigned char * p)
3828 CODE:
3829 RETVAL = isLOWER_utf8( p);
3830 OUTPUT:
3831 RETVAL
3832
3833bool
981746b9
KW
3834test_isLOWER_LC_utf8(unsigned char * p)
3835 CODE:
3836 RETVAL = isLOWER_LC_utf8( p);
3837 OUTPUT:
3838 RETVAL
3839
3840bool
c9c05358
KW
3841test_isALPHA_uni(UV ord)
3842 CODE:
3843 RETVAL = isALPHA_uni(ord);
3844 OUTPUT:
3845 RETVAL
3846
3847bool
981746b9
KW
3848test_isALPHA_LC_uvchr(UV ord)
3849 CODE:
3850 RETVAL = isALPHA_LC_uvchr(ord);
3851 OUTPUT:
3852 RETVAL
3853
3854bool
c9c05358
KW
3855test_isALPHA_A(UV ord)
3856 CODE:
3857 RETVAL = isALPHA_A(ord);
3858 OUTPUT:
3859 RETVAL
3860
3861bool
3862test_isALPHA_L1(UV ord)
3863 CODE:
3864 RETVAL = isALPHA_L1(ord);
3865 OUTPUT:
3866 RETVAL
3867
3868bool
981746b9
KW
3869test_isALPHA_LC(UV ord)
3870 CODE:
3871 RETVAL = isALPHA_LC(ord);
3872 OUTPUT:
3873 RETVAL
3874
3875bool
c9c05358
KW
3876test_isALPHA_utf8(unsigned char * p)
3877 CODE:
3878 RETVAL = isALPHA_utf8( p);
3879 OUTPUT:
3880 RETVAL
3881
3882bool
981746b9 3883test_isALPHA_LC_utf8(unsigned char * p)
c9c05358 3884 CODE:
981746b9
KW
3885 RETVAL = isALPHA_LC_utf8( p);
3886 OUTPUT:
3887 RETVAL
3888
3889bool
3890test_isWORDCHAR_uni(UV ord)
3891 CODE:
3892 RETVAL = isWORDCHAR_uni(ord);
3893 OUTPUT:
3894 RETVAL
3895
3896bool
3897test_isWORDCHAR_LC_uvchr(UV ord)
3898 CODE:
3899 RETVAL = isWORDCHAR_LC_uvchr(ord);
c9c05358
KW
3900 OUTPUT:
3901 RETVAL
3902
3903bool
981746b9 3904test_isWORDCHAR_A(UV ord)
c9c05358
KW
3905 CODE:
3906 RETVAL = isWORDCHAR_A(ord);
3907 OUTPUT:
3908 RETVAL
3909
3910bool
981746b9 3911test_isWORDCHAR_L1(UV ord)
c9c05358
KW
3912 CODE:
3913 RETVAL = isWORDCHAR_L1(ord);
3914 OUTPUT:
3915 RETVAL
3916
3917bool
981746b9
KW
3918test_isWORDCHAR_LC(UV ord)
3919 CODE:
3920 RETVAL = isWORDCHAR_LC(ord);
3921 OUTPUT:
3922 RETVAL
3923
3924bool
3925test_isWORDCHAR_utf8(unsigned char * p)
3926 CODE:
3927 RETVAL = isWORDCHAR_utf8( p);
3928 OUTPUT:
3929 RETVAL
3930
3931bool
3932test_isWORDCHAR_LC_utf8(unsigned char * p)
3933 CODE:
3934 RETVAL = isWORDCHAR_LC_utf8( p);
3935 OUTPUT:
3936 RETVAL
3937
3938bool
15861f94 3939test_isALPHANUMERIC_uni(UV ord)
981746b9 3940 CODE:
15861f94 3941 RETVAL = isALPHANUMERIC_uni(ord);
981746b9
KW
3942 OUTPUT:
3943 RETVAL
3944
3945bool
15861f94 3946test_isALPHANUMERIC_LC_uvchr(UV ord)
981746b9 3947 CODE:
15861f94 3948 RETVAL = isALPHANUMERIC_LC_uvchr(ord);
981746b9
KW
3949 OUTPUT:
3950 RETVAL
3951
3952bool
15861f94 3953test_isALPHANUMERIC_A(UV ord)
981746b9 3954 CODE:
15861f94 3955 RETVAL = isALPHANUMERIC_A(ord);
981746b9
KW
3956 OUTPUT:
3957 RETVAL
3958
3959bool
15861f94 3960test_isALPHANUMERIC_L1(UV ord)
981746b9 3961 CODE:
15861f94 3962 RETVAL = isALPHANUMERIC_L1(ord);
981746b9
KW
3963 OUTPUT:
3964 RETVAL
3965
3966bool
15861f94 3967test_isALPHANUMERIC_LC(UV ord)
981746b9 3968 CODE:
15861f94 3969 RETVAL = isALPHANUMERIC_LC(ord);
981746b9
KW
3970 OUTPUT:
3971 RETVAL
3972
3973bool
15861f94 3974test_isALPHANUMERIC_utf8(unsigned char * p)
981746b9 3975 CODE: