This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
restore ExtUtils-ParseXS portability to Perl 5.6
[perl5.git] / ext / XS-APItest / APItest.xs
CommitLineData
6a93a7e5 1#define PERL_IN_XS_APITEST
3e61d65a
JH
2#include "EXTERN.h"
3#include "perl.h"
4#include "XSUB.h"
5
36c2b1d0
NC
6typedef SV *SVREF;
7typedef PTR_TBL_t *XS__APItest__PtrTable;
85ce96a1 8
11f9f0ed 9#define croak_fail() croak("fail at " __FILE__ " line %d", __LINE__)
779bc08a 10#define croak_fail_ne(h, w) croak("fail %p!=%p at " __FILE__ " line %d", (h), (w), __LINE__)
11f9f0ed 11
85ce96a1
DM
12/* for my_cxt tests */
13
14#define MY_CXT_KEY "XS::APItest::_guts" XS_VERSION
15
16typedef struct {
17 int i;
18 SV *sv;
03569ecf
BM
19 GV *cscgv;
20 AV *cscav;
13b6b3bc
BM
21 AV *bhkav;
22 bool bhk_record;
201c7e1f
FR
23 peep_t orig_peep;
24 peep_t orig_rpeep;
25 int peep_recording;
26 AV *peep_recorder;
27 AV *rpeep_recorder;
f568d64d 28 AV *xop_record;
85ce96a1
DM
29} my_cxt_t;
30
31START_MY_CXT
32
26ab20ee
FR
33MGVTBL vtbl_foo, vtbl_bar;
34
85ce96a1 35/* indirect functions to test the [pa]MY_CXT macros */
f16dd614 36
85ce96a1
DM
37int
38my_cxt_getint_p(pMY_CXT)
39{
40 return MY_CXT.i;
41}
f16dd614 42
85ce96a1
DM
43void
44my_cxt_setint_p(pMY_CXT_ int i)
45{
46 MY_CXT.i = i;
47}
f16dd614
DM
48
49SV*
9568a123 50my_cxt_getsv_interp_context(void)
f16dd614 51{
f16dd614
DM
52 dTHX;
53 dMY_CXT_INTERP(my_perl);
9568a123
NC
54 return MY_CXT.sv;
55}
56
57SV*
58my_cxt_getsv_interp(void)
59{
f16dd614 60 dMY_CXT;
f16dd614
DM
61 return MY_CXT.sv;
62}
63
85ce96a1
DM
64void
65my_cxt_setsv_p(SV* sv _pMY_CXT)
66{
67 MY_CXT.sv = sv;
68}
69
70
9b5c3821 71/* from exception.c */
7a646707 72int apitest_exception(int);
0314122a 73
ff66e713
SH
74/* from core_or_not.inc */
75bool sv_setsv_cow_hashkey_core(void);
76bool sv_setsv_cow_hashkey_notcore(void);
77
2dc92170
NC
78/* A routine to test hv_delayfree_ent
79 (which itself is tested by testing on hv_free_ent */
80
81typedef void (freeent_function)(pTHX_ HV *, register HE *);
82
83void
84test_freeent(freeent_function *f) {
85 dTHX;
86 dSP;
87 HV *test_hash = newHV();
88 HE *victim;
89 SV *test_scalar;
90 U32 results[4];
91 int i;
92
8afd2d2e
NC
93#ifdef PURIFY
94 victim = (HE*)safemalloc(sizeof(HE));
95#else
2dc92170
NC
96 /* Storing then deleting something should ensure that a hash entry is
97 available. */
c33e8be1
Z
98 (void) hv_store(test_hash, "", 0, &PL_sv_yes, 0);
99 (void) hv_delete(test_hash, "", 0, 0);
2dc92170
NC
100
101 /* We need to "inline" new_he here as it's static, and the functions we
102 test expect to be able to call del_HE on the HE */
6a93a7e5 103 if (!PL_body_roots[HE_SVSLOT])
2dc92170 104 croak("PL_he_root is 0");
8a722a80 105 victim = (HE*) PL_body_roots[HE_SVSLOT];
6a93a7e5 106 PL_body_roots[HE_SVSLOT] = HeNEXT(victim);
8afd2d2e 107#endif
2dc92170
NC
108
109 victim->hent_hek = Perl_share_hek(aTHX_ "", 0, 0);
110
111 test_scalar = newSV(0);
112 SvREFCNT_inc(test_scalar);
de616631 113 HeVAL(victim) = test_scalar;
2dc92170
NC
114
115 /* Need this little game else we free the temps on the return stack. */
116 results[0] = SvREFCNT(test_scalar);
117 SAVETMPS;
118 results[1] = SvREFCNT(test_scalar);
119 f(aTHX_ test_hash, victim);
120 results[2] = SvREFCNT(test_scalar);
121 FREETMPS;
122 results[3] = SvREFCNT(test_scalar);
123
124 i = 0;
125 do {
126 mPUSHu(results[i]);
c33e8be1 127 } while (++i < (int)(sizeof(results)/sizeof(results[0])));
2dc92170
NC
128
129 /* Goodbye to our extra reference. */
130 SvREFCNT_dec(test_scalar);
131}
132
b54b4831
NC
133
134static I32
53c40a8f
NC
135bitflip_key(pTHX_ IV action, SV *field) {
136 MAGIC *mg = mg_find(field, PERL_MAGIC_uvar);
137 SV *keysv;
c33e8be1 138 PERL_UNUSED_ARG(action);
53c40a8f
NC
139 if (mg && (keysv = mg->mg_obj)) {
140 STRLEN len;
141 const char *p = SvPV(keysv, len);
142
143 if (len) {
144 SV *newkey = newSV(len);
145 char *new_p = SvPVX(newkey);
146
147 if (SvUTF8(keysv)) {
148 const char *const end = p + len;
149 while (p < end) {
150 STRLEN len;
a75fcbca
SP
151 UV chr = utf8_to_uvuni((U8 *)p, &len);
152 new_p = (char *)uvuni_to_utf8((U8 *)new_p, chr ^ 32);
53c40a8f
NC
153 p += len;
154 }
155 SvUTF8_on(newkey);
156 } else {
157 while (len--)
158 *new_p++ = *p++ ^ 32;
159 }
160 *new_p = '\0';
161 SvCUR_set(newkey, SvCUR(keysv));
162 SvPOK_on(newkey);
163
164 mg->mg_obj = newkey;
165 }
166 }
167 return 0;
168}
169
170static I32
b54b4831
NC
171rot13_key(pTHX_ IV action, SV *field) {
172 MAGIC *mg = mg_find(field, PERL_MAGIC_uvar);
173 SV *keysv;
c33e8be1 174 PERL_UNUSED_ARG(action);
b54b4831
NC
175 if (mg && (keysv = mg->mg_obj)) {
176 STRLEN len;
177 const char *p = SvPV(keysv, len);
178
179 if (len) {
180 SV *newkey = newSV(len);
181 char *new_p = SvPVX(newkey);
182
183 /* There's a deliberate fencepost error here to loop len + 1 times
184 to copy the trailing \0 */
185 do {
186 char new_c = *p++;
187 /* Try doing this cleanly and clearly in EBCDIC another way: */
188 switch (new_c) {
189 case 'A': new_c = 'N'; break;
190 case 'B': new_c = 'O'; break;
191 case 'C': new_c = 'P'; break;
192 case 'D': new_c = 'Q'; break;
193 case 'E': new_c = 'R'; break;
194 case 'F': new_c = 'S'; break;
195 case 'G': new_c = 'T'; break;
196 case 'H': new_c = 'U'; break;
197 case 'I': new_c = 'V'; break;
198 case 'J': new_c = 'W'; break;
199 case 'K': new_c = 'X'; break;
200 case 'L': new_c = 'Y'; break;
201 case 'M': new_c = 'Z'; break;
202 case 'N': new_c = 'A'; break;
203 case 'O': new_c = 'B'; break;
204 case 'P': new_c = 'C'; break;
205 case 'Q': new_c = 'D'; break;
206 case 'R': new_c = 'E'; break;
207 case 'S': new_c = 'F'; break;
208 case 'T': new_c = 'G'; break;
209 case 'U': new_c = 'H'; break;
210 case 'V': new_c = 'I'; break;
211 case 'W': new_c = 'J'; break;
212 case 'X': new_c = 'K'; break;
213 case 'Y': new_c = 'L'; break;
214 case 'Z': new_c = 'M'; break;
215 case 'a': new_c = 'n'; break;
216 case 'b': new_c = 'o'; break;
217 case 'c': new_c = 'p'; break;
218 case 'd': new_c = 'q'; break;
219 case 'e': new_c = 'r'; break;
220 case 'f': new_c = 's'; break;
221 case 'g': new_c = 't'; break;
222 case 'h': new_c = 'u'; break;
223 case 'i': new_c = 'v'; break;
224 case 'j': new_c = 'w'; break;
225 case 'k': new_c = 'x'; break;
226 case 'l': new_c = 'y'; break;
227 case 'm': new_c = 'z'; break;
228 case 'n': new_c = 'a'; break;
229 case 'o': new_c = 'b'; break;
230 case 'p': new_c = 'c'; break;
231 case 'q': new_c = 'd'; break;
232 case 'r': new_c = 'e'; break;
233 case 's': new_c = 'f'; break;
234 case 't': new_c = 'g'; break;
235 case 'u': new_c = 'h'; break;
236 case 'v': new_c = 'i'; break;
237 case 'w': new_c = 'j'; break;
238 case 'x': new_c = 'k'; break;
239 case 'y': new_c = 'l'; break;
240 case 'z': new_c = 'm'; break;
241 }
242 *new_p++ = new_c;
243 } while (len--);
244 SvCUR_set(newkey, SvCUR(keysv));
245 SvPOK_on(newkey);
246 if (SvUTF8(keysv))
247 SvUTF8_on(newkey);
248
249 mg->mg_obj = newkey;
250 }
251 }
252 return 0;
253}
254
218787bd
VP
255STATIC I32
256rmagical_a_dummy(pTHX_ IV idx, SV *sv) {
c33e8be1
Z
257 PERL_UNUSED_ARG(idx);
258 PERL_UNUSED_ARG(sv);
218787bd
VP
259 return 0;
260}
261
262STATIC MGVTBL rmagical_b = { 0 };
263
03569ecf 264STATIC void
13b6b3bc 265blockhook_csc_start(pTHX_ int full)
03569ecf
BM
266{
267 dMY_CXT;
268 AV *const cur = GvAV(MY_CXT.cscgv);
269
c33e8be1 270 PERL_UNUSED_ARG(full);
03569ecf
BM
271 SAVEGENERICSV(GvAV(MY_CXT.cscgv));
272
273 if (cur) {
274 I32 i;
d024465f 275 AV *const new_av = newAV();
03569ecf
BM
276
277 for (i = 0; i <= av_len(cur); i++) {
d024465f 278 av_store(new_av, i, newSVsv(*av_fetch(cur, i, 0)));
03569ecf
BM
279 }
280
d024465f 281 GvAV(MY_CXT.cscgv) = new_av;
03569ecf
BM
282 }
283}
284
285STATIC void
13b6b3bc 286blockhook_csc_pre_end(pTHX_ OP **o)
03569ecf
BM
287{
288 dMY_CXT;
289
c33e8be1 290 PERL_UNUSED_ARG(o);
03569ecf
BM
291 /* if we hit the end of a scope we missed the start of, we need to
292 * unconditionally clear @CSC */
52db365a 293 if (GvAV(MY_CXT.cscgv) == MY_CXT.cscav && MY_CXT.cscav) {
03569ecf 294 av_clear(MY_CXT.cscav);
52db365a 295 }
03569ecf
BM
296
297}
298
13b6b3bc
BM
299STATIC void
300blockhook_test_start(pTHX_ int full)
301{
302 dMY_CXT;
303 AV *av;
304
305 if (MY_CXT.bhk_record) {
306 av = newAV();
307 av_push(av, newSVpvs("start"));
308 av_push(av, newSViv(full));
309 av_push(MY_CXT.bhkav, newRV_noinc(MUTABLE_SV(av)));
310 }
311}
312
313STATIC void
314blockhook_test_pre_end(pTHX_ OP **o)
315{
316 dMY_CXT;
317
c33e8be1 318 PERL_UNUSED_ARG(o);
13b6b3bc
BM
319 if (MY_CXT.bhk_record)
320 av_push(MY_CXT.bhkav, newSVpvs("pre_end"));
321}
322
323STATIC void
324blockhook_test_post_end(pTHX_ OP **o)
325{
326 dMY_CXT;
327
c33e8be1 328 PERL_UNUSED_ARG(o);
13b6b3bc
BM
329 if (MY_CXT.bhk_record)
330 av_push(MY_CXT.bhkav, newSVpvs("post_end"));
331}
332
333STATIC void
334blockhook_test_eval(pTHX_ OP *const o)
335{
336 dMY_CXT;
337 AV *av;
338
339 if (MY_CXT.bhk_record) {
340 av = newAV();
341 av_push(av, newSVpvs("eval"));
342 av_push(av, newSVpv(OP_NAME(o), 0));
343 av_push(MY_CXT.bhkav, newRV_noinc(MUTABLE_SV(av)));
344 }
345}
346
347STATIC BHK bhk_csc, bhk_test;
348
201c7e1f
FR
349STATIC void
350my_peep (pTHX_ OP *o)
351{
352 dMY_CXT;
353
354 if (!o)
355 return;
356
357 MY_CXT.orig_peep(aTHX_ o);
358
359 if (!MY_CXT.peep_recording)
360 return;
361
362 for (; o; o = o->op_next) {
363 if (o->op_type == OP_CONST && cSVOPx_sv(o) && SvPOK(cSVOPx_sv(o))) {
364 av_push(MY_CXT.peep_recorder, newSVsv(cSVOPx_sv(o)));
365 }
366 }
367}
368
369STATIC void
370my_rpeep (pTHX_ OP *o)
371{
372 dMY_CXT;
373
374 if (!o)
375 return;
376
377 MY_CXT.orig_rpeep(aTHX_ o);
378
379 if (!MY_CXT.peep_recording)
380 return;
381
382 for (; o; o = o->op_next) {
383 if (o->op_type == OP_CONST && cSVOPx_sv(o) && SvPOK(cSVOPx_sv(o))) {
384 av_push(MY_CXT.rpeep_recorder, newSVsv(cSVOPx_sv(o)));
385 }
386 }
387}
388
d9088386
Z
389STATIC OP *
390THX_ck_entersub_args_lists(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
391{
c33e8be1
Z
392 PERL_UNUSED_ARG(namegv);
393 PERL_UNUSED_ARG(ckobj);
d9088386
Z
394 return ck_entersub_args_list(entersubop);
395}
396
397STATIC OP *
398THX_ck_entersub_args_scalars(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
399{
400 OP *aop = cUNOPx(entersubop)->op_first;
c33e8be1
Z
401 PERL_UNUSED_ARG(namegv);
402 PERL_UNUSED_ARG(ckobj);
d9088386
Z
403 if (!aop->op_sibling)
404 aop = cUNOPx(aop)->op_first;
405 for (aop = aop->op_sibling; aop->op_sibling; aop = aop->op_sibling) {
406 op_contextualize(aop, G_SCALAR);
407 }
408 return entersubop;
409}
410
411STATIC OP *
412THX_ck_entersub_multi_sum(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
413{
414 OP *sumop = NULL;
415 OP *pushop = cUNOPx(entersubop)->op_first;
c33e8be1
Z
416 PERL_UNUSED_ARG(namegv);
417 PERL_UNUSED_ARG(ckobj);
d9088386
Z
418 if (!pushop->op_sibling)
419 pushop = cUNOPx(pushop)->op_first;
420 while (1) {
421 OP *aop = pushop->op_sibling;
422 if (!aop->op_sibling)
423 break;
424 pushop->op_sibling = aop->op_sibling;
425 aop->op_sibling = NULL;
426 op_contextualize(aop, G_SCALAR);
427 if (sumop) {
428 sumop = newBINOP(OP_ADD, 0, sumop, aop);
429 } else {
430 sumop = aop;
431 }
432 }
433 if (!sumop)
434 sumop = newSVOP(OP_CONST, 0, newSViv(0));
435 op_free(entersubop);
436 return sumop;
437}
438
2fcb4757
Z
439STATIC void test_op_list_describe_part(SV *res, OP *o);
440STATIC void
441test_op_list_describe_part(SV *res, OP *o)
442{
443 sv_catpv(res, PL_op_name[o->op_type]);
444 switch (o->op_type) {
445 case OP_CONST: {
446 sv_catpvf(res, "(%d)", (int)SvIV(cSVOPx(o)->op_sv));
447 } break;
448 }
449 if (o->op_flags & OPf_KIDS) {
450 OP *k;
451 sv_catpvs(res, "[");
452 for (k = cUNOPx(o)->op_first; k; k = k->op_sibling)
453 test_op_list_describe_part(res, k);
454 sv_catpvs(res, "]");
455 } else {
456 sv_catpvs(res, ".");
457 }
458}
459
460STATIC char *
461test_op_list_describe(OP *o)
462{
463 SV *res = sv_2mortal(newSVpvs(""));
464 if (o)
465 test_op_list_describe_part(res, o);
466 return SvPVX(res);
467}
468
b7b1e41b 469/* the real new*OP functions have a tendency to call fold_constants, and
5983a79d
BM
470 * other such unhelpful things, so we need our own versions for testing */
471
472#define mkUNOP(t, f) THX_mkUNOP(aTHX_ (t), (f))
473static OP *
474THX_mkUNOP(pTHX_ U32 type, OP *first)
475{
476 UNOP *unop;
477 NewOp(1103, unop, 1, UNOP);
478 unop->op_type = (OPCODE)type;
479 unop->op_first = first;
480 unop->op_flags = OPf_KIDS;
481 return (OP *)unop;
482}
483
484#define mkBINOP(t, f, l) THX_mkBINOP(aTHX_ (t), (f), (l))
485static OP *
486THX_mkBINOP(pTHX_ U32 type, OP *first, OP *last)
487{
488 BINOP *binop;
489 NewOp(1103, binop, 1, BINOP);
490 binop->op_type = (OPCODE)type;
491 binop->op_first = first;
492 binop->op_flags = OPf_KIDS;
493 binop->op_last = last;
494 first->op_sibling = last;
495 return (OP *)binop;
496}
497
498#define mkLISTOP(t, f, s, l) THX_mkLISTOP(aTHX_ (t), (f), (s), (l))
499static OP *
500THX_mkLISTOP(pTHX_ U32 type, OP *first, OP *sib, OP *last)
501{
502 LISTOP *listop;
503 NewOp(1103, listop, 1, LISTOP);
504 listop->op_type = (OPCODE)type;
505 listop->op_flags = OPf_KIDS;
506 listop->op_first = first;
507 first->op_sibling = sib;
508 sib->op_sibling = last;
509 listop->op_last = last;
510 return (OP *)listop;
511}
512
513static char *
514test_op_linklist_describe(OP *start)
515{
516 SV *rv = sv_2mortal(newSVpvs(""));
517 OP *o;
518 o = start = LINKLIST(start);
519 do {
520 sv_catpvs(rv, ".");
521 sv_catpv(rv, OP_NAME(o));
522 if (o->op_type == OP_CONST)
523 sv_catsv(rv, cSVOPo->op_sv);
524 o = o->op_next;
525 } while (o && o != start);
526 return SvPVX(rv);
527}
528
8f89e5a9
Z
529/** establish_cleanup operator, ripped off from Scope::Cleanup **/
530
531STATIC void
532THX_run_cleanup(pTHX_ void *cleanup_code_ref)
533{
534 dSP;
535 ENTER;
536 SAVETMPS;
537 PUSHMARK(SP);
538 call_sv((SV*)cleanup_code_ref, G_VOID|G_DISCARD);
539 FREETMPS;
540 LEAVE;
541}
542
543STATIC OP *
544THX_pp_establish_cleanup(pTHX)
545{
546 dSP;
547 SV *cleanup_code_ref;
548 cleanup_code_ref = newSVsv(POPs);
549 SAVEFREESV(cleanup_code_ref);
550 SAVEDESTRUCTOR_X(THX_run_cleanup, cleanup_code_ref);
551 if(GIMME_V != G_VOID) PUSHs(&PL_sv_undef);
552 RETURN;
553}
554
555STATIC OP *
556THX_ck_entersub_establish_cleanup(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
557{
558 OP *pushop, *argop, *estop;
559 ck_entersub_args_proto(entersubop, namegv, ckobj);
560 pushop = cUNOPx(entersubop)->op_first;
561 if(!pushop->op_sibling) pushop = cUNOPx(pushop)->op_first;
562 argop = pushop->op_sibling;
563 pushop->op_sibling = argop->op_sibling;
564 argop->op_sibling = NULL;
565 op_free(entersubop);
566 NewOpSz(0, estop, sizeof(UNOP));
567 estop->op_type = OP_RAND;
568 estop->op_ppaddr = THX_pp_establish_cleanup;
569 cUNOPx(estop)->op_flags = OPf_KIDS;
570 cUNOPx(estop)->op_first = argop;
571 PL_hints |= HINT_BLOCK_SCOPE;
572 return estop;
573}
574
3ad73efd
Z
575STATIC OP *
576THX_ck_entersub_postinc(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
577{
c33e8be1 578 OP *pushop, *argop;
3ad73efd
Z
579 ck_entersub_args_proto(entersubop, namegv, ckobj);
580 pushop = cUNOPx(entersubop)->op_first;
581 if(!pushop->op_sibling) pushop = cUNOPx(pushop)->op_first;
582 argop = pushop->op_sibling;
583 pushop->op_sibling = argop->op_sibling;
584 argop->op_sibling = NULL;
585 op_free(entersubop);
586 return newUNOP(OP_POSTINC, 0,
587 op_lvalue(op_contextualize(argop, G_SCALAR), OP_POSTINC));
588}
589
15103811
Z
590STATIC OP *
591THX_ck_entersub_pad_scalar(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
592{
593 OP *pushop, *argop;
594 PADOFFSET padoff = NOT_IN_PAD;
595 SV *a0, *a1;
596 ck_entersub_args_proto(entersubop, namegv, ckobj);
597 pushop = cUNOPx(entersubop)->op_first;
598 if(!pushop->op_sibling) pushop = cUNOPx(pushop)->op_first;
599 argop = pushop->op_sibling;
600 if(argop->op_type != OP_CONST || argop->op_sibling->op_type != OP_CONST)
601 croak("bad argument expression type for pad_scalar()");
602 a0 = cSVOPx_sv(argop);
603 a1 = cSVOPx_sv(argop->op_sibling);
604 switch(SvIV(a0)) {
605 case 1: {
606 SV *namesv = sv_2mortal(newSVpvs("$"));
607 sv_catsv(namesv, a1);
608 padoff = pad_findmy_sv(namesv, 0);
609 } break;
610 case 2: {
611 char *namepv;
612 STRLEN namelen;
613 SV *namesv = sv_2mortal(newSVpvs("$"));
614 sv_catsv(namesv, a1);
615 namepv = SvPV(namesv, namelen);
ab8f91e9 616 padoff = pad_findmy_pvn(namepv, namelen, SvUTF8(namesv));
15103811
Z
617 } break;
618 case 3: {
619 char *namepv;
620 SV *namesv = sv_2mortal(newSVpvs("$"));
621 sv_catsv(namesv, a1);
622 namepv = SvPV_nolen(namesv);
ab8f91e9 623 padoff = pad_findmy_pv(namepv, SvUTF8(namesv));
15103811
Z
624 } break;
625 case 4: {
626 padoff = pad_findmy_pvs("$foo", 0);
627 } break;
628 default: croak("bad type value for pad_scalar()");
629 }
630 op_free(entersubop);
631 if(padoff == NOT_IN_PAD) {
632 return newSVOP(OP_CONST, 0, newSVpvs("NOT_IN_PAD"));
633 } else if(PAD_COMPNAME_FLAGS_isOUR(padoff)) {
634 return newSVOP(OP_CONST, 0, newSVpvs("NOT_MY"));
635 } else {
636 OP *padop = newOP(OP_PADSV, 0);
637 padop->op_targ = padoff;
638 return padop;
639 }
640}
641
83f8bb56
Z
642/** RPN keyword parser **/
643
644#define sv_is_glob(sv) (SvTYPE(sv) == SVt_PVGV)
645#define sv_is_regexp(sv) (SvTYPE(sv) == SVt_REGEXP)
646#define sv_is_string(sv) \
647 (!sv_is_glob(sv) && !sv_is_regexp(sv) && \
648 (SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK|SVp_IOK|SVp_NOK|SVp_POK)))
649
650static SV *hintkey_rpn_sv, *hintkey_calcrpn_sv, *hintkey_stufftest_sv;
07ffcb73 651static SV *hintkey_swaptwostmts_sv, *hintkey_looprest_sv;
a7aaec61 652static SV *hintkey_scopelessblock_sv;
e53d8f76
Z
653static SV *hintkey_stmtasexpr_sv, *hintkey_stmtsasexpr_sv;
654static SV *hintkey_loopblock_sv, *hintkey_blockasexpr_sv;
361d9b55 655static SV *hintkey_swaplabel_sv, *hintkey_labelconst_sv;
78cdf107
Z
656static SV *hintkey_arrayfullexpr_sv, *hintkey_arraylistexpr_sv;
657static SV *hintkey_arraytermexpr_sv, *hintkey_arrayarithexpr_sv;
658static SV *hintkey_arrayexprflags_sv;
83f8bb56
Z
659static int (*next_keyword_plugin)(pTHX_ char *, STRLEN, OP **);
660
661/* low-level parser helpers */
662
663#define PL_bufptr (PL_parser->bufptr)
664#define PL_bufend (PL_parser->bufend)
665
666/* RPN parser */
667
668#define parse_var() THX_parse_var(aTHX)
669static OP *THX_parse_var(pTHX)
670{
671 char *s = PL_bufptr;
672 char *start = s;
673 PADOFFSET varpos;
674 OP *padop;
675 if(*s != '$') croak("RPN syntax error");
676 while(1) {
677 char c = *++s;
678 if(!isALNUM(c)) break;
679 }
680 if(s-start < 2) croak("RPN syntax error");
681 lex_read_to(s);
cc76b5cc 682 varpos = pad_findmy_pvn(start, s-start, 0);
83f8bb56
Z
683 if(varpos == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(varpos))
684 croak("RPN only supports \"my\" variables");
685 padop = newOP(OP_PADSV, 0);
686 padop->op_targ = varpos;
687 return padop;
688}
689
690#define push_rpn_item(o) \
691 (tmpop = (o), tmpop->op_sibling = stack, stack = tmpop)
692#define pop_rpn_item() \
693 (!stack ? (croak("RPN stack underflow"), (OP*)NULL) : \
694 (tmpop = stack, stack = stack->op_sibling, \
695 tmpop->op_sibling = NULL, tmpop))
696
697#define parse_rpn_expr() THX_parse_rpn_expr(aTHX)
698static OP *THX_parse_rpn_expr(pTHX)
699{
700 OP *stack = NULL, *tmpop;
701 while(1) {
702 I32 c;
703 lex_read_space(0);
704 c = lex_peek_unichar(0);
705 switch(c) {
706 case /*(*/')': case /*{*/'}': {
707 OP *result = pop_rpn_item();
708 if(stack) croak("RPN expression must return a single value");
709 return result;
710 } break;
711 case '0': case '1': case '2': case '3': case '4':
712 case '5': case '6': case '7': case '8': case '9': {
713 UV val = 0;
714 do {
715 lex_read_unichar(0);
716 val = 10*val + (c - '0');
717 c = lex_peek_unichar(0);
718 } while(c >= '0' && c <= '9');
719 push_rpn_item(newSVOP(OP_CONST, 0, newSVuv(val)));
720 } break;
721 case '$': {
722 push_rpn_item(parse_var());
723 } break;
724 case '+': {
725 OP *b = pop_rpn_item();
726 OP *a = pop_rpn_item();
727 lex_read_unichar(0);
728 push_rpn_item(newBINOP(OP_I_ADD, 0, a, b));
729 } break;
730 case '-': {
731 OP *b = pop_rpn_item();
732 OP *a = pop_rpn_item();
733 lex_read_unichar(0);
734 push_rpn_item(newBINOP(OP_I_SUBTRACT, 0, a, b));
735 } break;
736 case '*': {
737 OP *b = pop_rpn_item();
738 OP *a = pop_rpn_item();
739 lex_read_unichar(0);
740 push_rpn_item(newBINOP(OP_I_MULTIPLY, 0, a, b));
741 } break;
742 case '/': {
743 OP *b = pop_rpn_item();
744 OP *a = pop_rpn_item();
745 lex_read_unichar(0);
746 push_rpn_item(newBINOP(OP_I_DIVIDE, 0, a, b));
747 } break;
748 case '%': {
749 OP *b = pop_rpn_item();
750 OP *a = pop_rpn_item();
751 lex_read_unichar(0);
752 push_rpn_item(newBINOP(OP_I_MODULO, 0, a, b));
753 } break;
754 default: {
755 croak("RPN syntax error");
756 } break;
757 }
758 }
759}
760
761#define parse_keyword_rpn() THX_parse_keyword_rpn(aTHX)
762static OP *THX_parse_keyword_rpn(pTHX)
763{
764 OP *op;
765 lex_read_space(0);
766 if(lex_peek_unichar(0) != '('/*)*/)
767 croak("RPN expression must be parenthesised");
768 lex_read_unichar(0);
769 op = parse_rpn_expr();
770 if(lex_peek_unichar(0) != /*(*/')')
771 croak("RPN expression must be parenthesised");
772 lex_read_unichar(0);
773 return op;
774}
775
776#define parse_keyword_calcrpn() THX_parse_keyword_calcrpn(aTHX)
777static OP *THX_parse_keyword_calcrpn(pTHX)
778{
779 OP *varop, *exprop;
780 lex_read_space(0);
781 varop = parse_var();
782 lex_read_space(0);
783 if(lex_peek_unichar(0) != '{'/*}*/)
784 croak("RPN expression must be braced");
785 lex_read_unichar(0);
786 exprop = parse_rpn_expr();
787 if(lex_peek_unichar(0) != /*{*/'}')
788 croak("RPN expression must be braced");
789 lex_read_unichar(0);
790 return newASSIGNOP(OPf_STACKED, varop, 0, exprop);
791}
792
793#define parse_keyword_stufftest() THX_parse_keyword_stufftest(aTHX)
794static OP *THX_parse_keyword_stufftest(pTHX)
795{
796 I32 c;
797 bool do_stuff;
798 lex_read_space(0);
799 do_stuff = lex_peek_unichar(0) == '+';
800 if(do_stuff) {
801 lex_read_unichar(0);
802 lex_read_space(0);
803 }
804 c = lex_peek_unichar(0);
805 if(c == ';') {
806 lex_read_unichar(0);
807 } else if(c != /*{*/'}') {
808 croak("syntax error");
809 }
810 if(do_stuff) lex_stuff_pvs(" ", 0);
811 return newOP(OP_NULL, 0);
812}
813
814#define parse_keyword_swaptwostmts() THX_parse_keyword_swaptwostmts(aTHX)
815static OP *THX_parse_keyword_swaptwostmts(pTHX)
816{
817 OP *a, *b;
818 a = parse_fullstmt(0);
819 b = parse_fullstmt(0);
820 if(a && b)
821 PL_hints |= HINT_BLOCK_SCOPE;
2fcb4757 822 return op_append_list(OP_LINESEQ, b, a);
83f8bb56
Z
823}
824
07ffcb73
Z
825#define parse_keyword_looprest() THX_parse_keyword_looprest(aTHX)
826static OP *THX_parse_keyword_looprest(pTHX)
827{
94bf0465
Z
828 return newWHILEOP(0, 1, NULL, newSVOP(OP_CONST, 0, &PL_sv_yes),
829 parse_stmtseq(0), NULL, 1);
07ffcb73
Z
830}
831
a7aaec61
Z
832#define parse_keyword_scopelessblock() THX_parse_keyword_scopelessblock(aTHX)
833static OP *THX_parse_keyword_scopelessblock(pTHX)
834{
835 I32 c;
836 OP *body;
837 lex_read_space(0);
838 if(lex_peek_unichar(0) != '{'/*}*/) croak("syntax error");
839 lex_read_unichar(0);
840 body = parse_stmtseq(0);
841 c = lex_peek_unichar(0);
842 if(c != /*{*/'}' && c != /*[*/']' && c != /*(*/')') croak("syntax error");
843 lex_read_unichar(0);
844 return body;
845}
846
9eb5c532
Z
847#define parse_keyword_stmtasexpr() THX_parse_keyword_stmtasexpr(aTHX)
848static OP *THX_parse_keyword_stmtasexpr(pTHX)
849{
8359b381 850 OP *o = parse_barestmt(0);
3ad73efd
Z
851 if (!o) o = newOP(OP_STUB, 0);
852 if (PL_hints & HINT_BLOCK_SCOPE) o->op_flags |= OPf_PARENS;
853 return op_scope(o);
9eb5c532
Z
854}
855
856#define parse_keyword_stmtsasexpr() THX_parse_keyword_stmtsasexpr(aTHX)
857static OP *THX_parse_keyword_stmtsasexpr(pTHX)
858{
859 OP *o;
860 lex_read_space(0);
861 if(lex_peek_unichar(0) != '{'/*}*/) croak("syntax error");
862 lex_read_unichar(0);
863 o = parse_stmtseq(0);
864 lex_read_space(0);
865 if(lex_peek_unichar(0) != /*{*/'}') croak("syntax error");
866 lex_read_unichar(0);
3ad73efd
Z
867 if (!o) o = newOP(OP_STUB, 0);
868 if (PL_hints & HINT_BLOCK_SCOPE) o->op_flags |= OPf_PARENS;
869 return op_scope(o);
9eb5c532
Z
870}
871
e53d8f76
Z
872#define parse_keyword_loopblock() THX_parse_keyword_loopblock(aTHX)
873static OP *THX_parse_keyword_loopblock(pTHX)
874{
94bf0465
Z
875 return newWHILEOP(0, 1, NULL, newSVOP(OP_CONST, 0, &PL_sv_yes),
876 parse_block(0), NULL, 1);
e53d8f76
Z
877}
878
879#define parse_keyword_blockasexpr() THX_parse_keyword_blockasexpr(aTHX)
880static OP *THX_parse_keyword_blockasexpr(pTHX)
881{
882 OP *o = parse_block(0);
3ad73efd
Z
883 if (!o) o = newOP(OP_STUB, 0);
884 if (PL_hints & HINT_BLOCK_SCOPE) o->op_flags |= OPf_PARENS;
885 return op_scope(o);
e53d8f76
Z
886}
887
361d9b55
Z
888#define parse_keyword_swaplabel() THX_parse_keyword_swaplabel(aTHX)
889static OP *THX_parse_keyword_swaplabel(pTHX)
890{
891 OP *sop = parse_barestmt(0);
892 SV *label = parse_label(PARSE_OPTIONAL);
893 if (label) sv_2mortal(label);
894 return newSTATEOP(0, label ? savepv(SvPVX(label)) : NULL, sop);
895}
896
897#define parse_keyword_labelconst() THX_parse_keyword_labelconst(aTHX)
898static OP *THX_parse_keyword_labelconst(pTHX)
899{
900 return newSVOP(OP_CONST, 0, parse_label(0));
901}
902
78cdf107
Z
903#define parse_keyword_arrayfullexpr() THX_parse_keyword_arrayfullexpr(aTHX)
904static OP *THX_parse_keyword_arrayfullexpr(pTHX)
905{
906 return newANONLIST(parse_fullexpr(0));
907}
908
909#define parse_keyword_arraylistexpr() THX_parse_keyword_arraylistexpr(aTHX)
910static OP *THX_parse_keyword_arraylistexpr(pTHX)
911{
912 return newANONLIST(parse_listexpr(0));
913}
914
915#define parse_keyword_arraytermexpr() THX_parse_keyword_arraytermexpr(aTHX)
916static OP *THX_parse_keyword_arraytermexpr(pTHX)
917{
918 return newANONLIST(parse_termexpr(0));
919}
920
921#define parse_keyword_arrayarithexpr() THX_parse_keyword_arrayarithexpr(aTHX)
922static OP *THX_parse_keyword_arrayarithexpr(pTHX)
923{
924 return newANONLIST(parse_arithexpr(0));
925}
926
927#define parse_keyword_arrayexprflags() THX_parse_keyword_arrayexprflags(aTHX)
928static OP *THX_parse_keyword_arrayexprflags(pTHX)
929{
930 U32 flags = 0;
931 I32 c;
932 OP *o;
933 lex_read_space(0);
934 c = lex_peek_unichar(0);
935 if (c != '!' && c != '?') croak("syntax error");
936 lex_read_unichar(0);
937 if (c == '?') flags |= PARSE_OPTIONAL;
938 o = parse_listexpr(flags);
939 return o ? newANONLIST(o) : newANONHASH(newOP(OP_STUB, 0));
940}
941
83f8bb56
Z
942/* plugin glue */
943
944#define keyword_active(hintkey_sv) THX_keyword_active(aTHX_ hintkey_sv)
945static int THX_keyword_active(pTHX_ SV *hintkey_sv)
946{
947 HE *he;
948 if(!GvHV(PL_hintgv)) return 0;
949 he = hv_fetch_ent(GvHV(PL_hintgv), hintkey_sv, 0,
950 SvSHARED_HASH(hintkey_sv));
951 return he && SvTRUE(HeVAL(he));
952}
953
954static int my_keyword_plugin(pTHX_
955 char *keyword_ptr, STRLEN keyword_len, OP **op_ptr)
956{
957 if(keyword_len == 3 && strnEQ(keyword_ptr, "rpn", 3) &&
958 keyword_active(hintkey_rpn_sv)) {
959 *op_ptr = parse_keyword_rpn();
960 return KEYWORD_PLUGIN_EXPR;
961 } else if(keyword_len == 7 && strnEQ(keyword_ptr, "calcrpn", 7) &&
962 keyword_active(hintkey_calcrpn_sv)) {
963 *op_ptr = parse_keyword_calcrpn();
964 return KEYWORD_PLUGIN_STMT;
965 } else if(keyword_len == 9 && strnEQ(keyword_ptr, "stufftest", 9) &&
966 keyword_active(hintkey_stufftest_sv)) {
967 *op_ptr = parse_keyword_stufftest();
968 return KEYWORD_PLUGIN_STMT;
969 } else if(keyword_len == 12 &&
970 strnEQ(keyword_ptr, "swaptwostmts", 12) &&
971 keyword_active(hintkey_swaptwostmts_sv)) {
972 *op_ptr = parse_keyword_swaptwostmts();
973 return KEYWORD_PLUGIN_STMT;
07ffcb73
Z
974 } else if(keyword_len == 8 && strnEQ(keyword_ptr, "looprest", 8) &&
975 keyword_active(hintkey_looprest_sv)) {
976 *op_ptr = parse_keyword_looprest();
977 return KEYWORD_PLUGIN_STMT;
a7aaec61
Z
978 } else if(keyword_len == 14 && strnEQ(keyword_ptr, "scopelessblock", 14) &&
979 keyword_active(hintkey_scopelessblock_sv)) {
980 *op_ptr = parse_keyword_scopelessblock();
981 return KEYWORD_PLUGIN_STMT;
9eb5c532
Z
982 } else if(keyword_len == 10 && strnEQ(keyword_ptr, "stmtasexpr", 10) &&
983 keyword_active(hintkey_stmtasexpr_sv)) {
984 *op_ptr = parse_keyword_stmtasexpr();
985 return KEYWORD_PLUGIN_EXPR;
986 } else if(keyword_len == 11 && strnEQ(keyword_ptr, "stmtsasexpr", 11) &&
987 keyword_active(hintkey_stmtsasexpr_sv)) {
988 *op_ptr = parse_keyword_stmtsasexpr();
989 return KEYWORD_PLUGIN_EXPR;
e53d8f76
Z
990 } else if(keyword_len == 9 && strnEQ(keyword_ptr, "loopblock", 9) &&
991 keyword_active(hintkey_loopblock_sv)) {
992 *op_ptr = parse_keyword_loopblock();
993 return KEYWORD_PLUGIN_STMT;
994 } else if(keyword_len == 11 && strnEQ(keyword_ptr, "blockasexpr", 11) &&
995 keyword_active(hintkey_blockasexpr_sv)) {
996 *op_ptr = parse_keyword_blockasexpr();
997 return KEYWORD_PLUGIN_EXPR;
361d9b55
Z
998 } else if(keyword_len == 9 && strnEQ(keyword_ptr, "swaplabel", 9) &&
999 keyword_active(hintkey_swaplabel_sv)) {
1000 *op_ptr = parse_keyword_swaplabel();
1001 return KEYWORD_PLUGIN_STMT;
1002 } else if(keyword_len == 10 && strnEQ(keyword_ptr, "labelconst", 10) &&
1003 keyword_active(hintkey_labelconst_sv)) {
1004 *op_ptr = parse_keyword_labelconst();
1005 return KEYWORD_PLUGIN_EXPR;
78cdf107
Z
1006 } else if(keyword_len == 13 && strnEQ(keyword_ptr, "arrayfullexpr", 13) &&
1007 keyword_active(hintkey_arrayfullexpr_sv)) {
1008 *op_ptr = parse_keyword_arrayfullexpr();
1009 return KEYWORD_PLUGIN_EXPR;
1010 } else if(keyword_len == 13 && strnEQ(keyword_ptr, "arraylistexpr", 13) &&
1011 keyword_active(hintkey_arraylistexpr_sv)) {
1012 *op_ptr = parse_keyword_arraylistexpr();
1013 return KEYWORD_PLUGIN_EXPR;
1014 } else if(keyword_len == 13 && strnEQ(keyword_ptr, "arraytermexpr", 13) &&
1015 keyword_active(hintkey_arraytermexpr_sv)) {
1016 *op_ptr = parse_keyword_arraytermexpr();
1017 return KEYWORD_PLUGIN_EXPR;
1018 } else if(keyword_len == 14 && strnEQ(keyword_ptr, "arrayarithexpr", 14) &&
1019 keyword_active(hintkey_arrayarithexpr_sv)) {
1020 *op_ptr = parse_keyword_arrayarithexpr();
1021 return KEYWORD_PLUGIN_EXPR;
1022 } else if(keyword_len == 14 && strnEQ(keyword_ptr, "arrayexprflags", 14) &&
1023 keyword_active(hintkey_arrayexprflags_sv)) {
1024 *op_ptr = parse_keyword_arrayexprflags();
1025 return KEYWORD_PLUGIN_EXPR;
83f8bb56
Z
1026 } else {
1027 return next_keyword_plugin(aTHX_ keyword_ptr, keyword_len, op_ptr);
1028 }
1029}
1030
f568d64d
BM
1031static XOP my_xop;
1032
1033static OP *
1034pp_xop(pTHX)
1035{
1036 return PL_op->op_next;
1037}
1038
1039static void
1040peep_xop(pTHX_ OP *o, OP *oldop)
1041{
1042 dMY_CXT;
b58757d5
BM
1043 av_push(MY_CXT.xop_record, newSVpvf("peep:%"UVxf, PTR2UV(o)));
1044 av_push(MY_CXT.xop_record, newSVpvf("oldop:%"UVxf, PTR2UV(oldop)));
f568d64d
BM
1045}
1046
27fcb6ee
FC
1047static I32
1048filter_call(pTHX_ int idx, SV *buf_sv, int maxlen)
1049{
1050 SV *my_sv = FILTER_DATA(idx);
1051 char *p;
1052 char *end;
1053 int n = FILTER_READ(idx + 1, buf_sv, maxlen);
1054
1055 if (n<=0) return n;
1056
1057 p = SvPV_force_nolen(buf_sv);
1058 end = p + SvCUR(buf_sv);
1059 while (p < end) {
1060 if (*p == 'o') *p = 'e';
1061 p++;
1062 }
1063 return SvCUR(buf_sv);
1064}
1065
02cab674
FC
1066static AV *
1067myget_linear_isa(pTHX_ HV *stash, U32 level) {
02cab674 1068 GV **gvp = (GV **)hv_fetchs(stash, "ISA", 0);
6dbddf4a 1069 PERL_UNUSED_ARG(level);
02cab674
FC
1070 return gvp && *gvp && GvAV(*gvp)
1071 ? GvAV(*gvp)
1072 : (AV *)sv_2mortal((SV *)newAV());
1073}
1074
27fcb6ee 1075
9777c6aa
S
1076XS_EXTERNAL(XS_XS__APItest__XSUB_XS_VERSION_undef);
1077XS_EXTERNAL(XS_XS__APItest__XSUB_XS_VERSION_empty);
1078XS_EXTERNAL(XS_XS__APItest__XSUB_XS_APIVERSION_invalid);
7b20c7cd 1079
02cab674
FC
1080static struct mro_alg mymro;
1081
1082
55289a74
NC
1083#include "const-c.inc"
1084
ffe53d21 1085MODULE = XS::APItest PACKAGE = XS::APItest
0314122a 1086
55289a74
NC
1087INCLUDE: const-xs.inc
1088
ffe53d21
NC
1089INCLUDE: numeric.xs
1090
fed3ba5d
NC
1091MODULE = XS::APItest::utf8 PACKAGE = XS::APItest::utf8
1092
1093int
1094bytes_cmp_utf8(bytes, utf8)
1095 SV *bytes
1096 SV *utf8
1097 PREINIT:
1098 const U8 *b;
1099 STRLEN blen;
1100 const U8 *u;
1101 STRLEN ulen;
1102 CODE:
1103 b = (const U8 *)SvPVbyte(bytes, blen);
1104 u = (const U8 *)SvPVbyte(utf8, ulen);
1105 RETVAL = bytes_cmp_utf8(b, blen, u, ulen);
1106 OUTPUT:
1107 RETVAL
1108
7d255dc8
NC
1109MODULE = XS::APItest:Overload PACKAGE = XS::APItest::Overload
1110
c33e8be1 1111void
25a9ffce 1112amagic_deref_call(sv, what)
7d255dc8
NC
1113 SV *sv
1114 int what
1115 PPCODE:
7d255dc8 1116 /* The reference is owned by something else. */
25a9ffce 1117 PUSHs(amagic_deref_call(sv, what));
7d255dc8 1118
e89bfaa6
NC
1119# I'd certainly like to discourage the use of this macro, given that we now
1120# have amagic_deref_call
1121
c33e8be1 1122void
e89bfaa6
NC
1123tryAMAGICunDEREF_var(sv, what)
1124 SV *sv
1125 int what
1126 PPCODE:
1127 {
1128 SV **sp = &sv;
1129 switch(what) {
1130 case to_av_amg:
1131 tryAMAGICunDEREF(to_av);
1132 break;
1133 case to_cv_amg:
1134 tryAMAGICunDEREF(to_cv);
1135 break;
1136 case to_gv_amg:
1137 tryAMAGICunDEREF(to_gv);
1138 break;
1139 case to_hv_amg:
1140 tryAMAGICunDEREF(to_hv);
1141 break;
1142 case to_sv_amg:
1143 tryAMAGICunDEREF(to_sv);
1144 break;
1145 default:
1146 croak("Invalid value %d passed to tryAMAGICunDEREF_var", what);
1147 }
1148 }
1149 /* The reference is owned by something else. */
1150 PUSHs(sv);
1151
7b20c7cd
NC
1152MODULE = XS::APItest PACKAGE = XS::APItest::XSUB
1153
1154BOOT:
1155 newXS("XS::APItest::XSUB::XS_VERSION_undef", XS_XS__APItest__XSUB_XS_VERSION_undef, __FILE__);
f9cc56fa 1156 newXS("XS::APItest::XSUB::XS_VERSION_empty", XS_XS__APItest__XSUB_XS_VERSION_empty, __FILE__);
88c4b02d 1157 newXS("XS::APItest::XSUB::XS_APIVERSION_invalid", XS_XS__APItest__XSUB_XS_APIVERSION_invalid, __FILE__);
7b20c7cd
NC
1158
1159void
1160XS_VERSION_defined(...)
1161 PPCODE:
1162 XS_VERSION_BOOTCHECK;
1163 XSRETURN_EMPTY;
1164
88c4b02d
NC
1165void
1166XS_APIVERSION_valid(...)
1167 PPCODE:
1168 XS_APIVERSION_BOOTCHECK;
1169 XSRETURN_EMPTY;
1170
ffe53d21
NC
1171MODULE = XS::APItest:Hash PACKAGE = XS::APItest::Hash
1172
b54b4831
NC
1173void
1174rot13_hash(hash)
1175 HV *hash
1176 CODE:
1177 {
1178 struct ufuncs uf;
1179 uf.uf_val = rot13_key;
1180 uf.uf_set = 0;
1181 uf.uf_index = 0;
1182
1183 sv_magic((SV*)hash, NULL, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
1184 }
1185
53c40a8f
NC
1186void
1187bitflip_hash(hash)
1188 HV *hash
1189 CODE:
1190 {
1191 struct ufuncs uf;
1192 uf.uf_val = bitflip_key;
1193 uf.uf_set = 0;
1194 uf.uf_index = 0;
1195
1196 sv_magic((SV*)hash, NULL, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
1197 }
1198
028f8eaa
MHM
1199#define UTF8KLEN(sv, len) (SvUTF8(sv) ? -(I32)len : (I32)len)
1200
0314122a
NC
1201bool
1202exists(hash, key_sv)
1203 PREINIT:
1204 STRLEN len;
1205 const char *key;
1206 INPUT:
1207 HV *hash
1208 SV *key_sv
1209 CODE:
1210 key = SvPV(key_sv, len);
028f8eaa 1211 RETVAL = hv_exists(hash, key, UTF8KLEN(key_sv, len));
0314122a
NC
1212 OUTPUT:
1213 RETVAL
1214
bdee33e4
NC
1215bool
1216exists_ent(hash, key_sv)
1217 PREINIT:
1218 INPUT:
1219 HV *hash
1220 SV *key_sv
1221 CODE:
1222 RETVAL = hv_exists_ent(hash, key_sv, 0);
1223 OUTPUT:
1224 RETVAL
1225
b60cf05a 1226SV *
55289a74 1227delete(hash, key_sv, flags = 0)
b60cf05a
NC
1228 PREINIT:
1229 STRLEN len;
1230 const char *key;
1231 INPUT:
1232 HV *hash
1233 SV *key_sv
55289a74 1234 I32 flags;
b60cf05a
NC
1235 CODE:
1236 key = SvPV(key_sv, len);
1237 /* It's already mortal, so need to increase reference count. */
55289a74
NC
1238 RETVAL
1239 = SvREFCNT_inc(hv_delete(hash, key, UTF8KLEN(key_sv, len), flags));
1240 OUTPUT:
1241 RETVAL
1242
1243SV *
1244delete_ent(hash, key_sv, flags = 0)
1245 INPUT:
1246 HV *hash
1247 SV *key_sv
1248 I32 flags;
1249 CODE:
1250 /* It's already mortal, so need to increase reference count. */
1251 RETVAL = SvREFCNT_inc(hv_delete_ent(hash, key_sv, flags, 0));
b60cf05a
NC
1252 OUTPUT:
1253 RETVAL
1254
1255SV *
858117f8
NC
1256store_ent(hash, key, value)
1257 PREINIT:
1258 SV *copy;
1259 HE *result;
1260 INPUT:
1261 HV *hash
1262 SV *key
1263 SV *value
1264 CODE:
1265 copy = newSV(0);
1266 result = hv_store_ent(hash, key, copy, 0);
1267 SvSetMagicSV(copy, value);
1268 if (!result) {
1269 SvREFCNT_dec(copy);
1270 XSRETURN_EMPTY;
1271 }
1272 /* It's about to become mortal, so need to increase reference count.
1273 */
1274 RETVAL = SvREFCNT_inc(HeVAL(result));
1275 OUTPUT:
1276 RETVAL
1277
858117f8 1278SV *
b60cf05a
NC
1279store(hash, key_sv, value)
1280 PREINIT:
1281 STRLEN len;
1282 const char *key;
1283 SV *copy;
1284 SV **result;
1285 INPUT:
1286 HV *hash
1287 SV *key_sv
1288 SV *value
1289 CODE:
1290 key = SvPV(key_sv, len);
1291 copy = newSV(0);
028f8eaa 1292 result = hv_store(hash, key, UTF8KLEN(key_sv, len), copy, 0);
858117f8 1293 SvSetMagicSV(copy, value);
b60cf05a
NC
1294 if (!result) {
1295 SvREFCNT_dec(copy);
1296 XSRETURN_EMPTY;
1297 }
1298 /* It's about to become mortal, so need to increase reference count.
1299 */
1300 RETVAL = SvREFCNT_inc(*result);
1301 OUTPUT:
1302 RETVAL
1303
bdee33e4
NC
1304SV *
1305fetch_ent(hash, key_sv)
1306 PREINIT:
1307 HE *result;
1308 INPUT:
1309 HV *hash
1310 SV *key_sv
1311 CODE:
1312 result = hv_fetch_ent(hash, key_sv, 0, 0);
1313 if (!result) {
1314 XSRETURN_EMPTY;
1315 }
1316 /* Force mg_get */
1317 RETVAL = newSVsv(HeVAL(result));
1318 OUTPUT:
1319 RETVAL
b60cf05a
NC
1320
1321SV *
1322fetch(hash, key_sv)
1323 PREINIT:
1324 STRLEN len;
1325 const char *key;
1326 SV **result;
1327 INPUT:
1328 HV *hash
1329 SV *key_sv
1330 CODE:
1331 key = SvPV(key_sv, len);
028f8eaa 1332 result = hv_fetch(hash, key, UTF8KLEN(key_sv, len), 0);
b60cf05a
NC
1333 if (!result) {
1334 XSRETURN_EMPTY;
1335 }
1336 /* Force mg_get */
1337 RETVAL = newSVsv(*result);
1338 OUTPUT:
1339 RETVAL
2dc92170 1340
9568a123
NC
1341#if defined (hv_common)
1342
6b4de907
NC
1343SV *
1344common(params)
1345 INPUT:
1346 HV *params
1347 PREINIT:
1348 HE *result;
1349 HV *hv = NULL;
1350 SV *keysv = NULL;
1351 const char *key = NULL;
1352 STRLEN klen = 0;
1353 int flags = 0;
1354 int action = 0;
1355 SV *val = NULL;
1356 U32 hash = 0;
1357 SV **svp;
1358 CODE:
1359 if ((svp = hv_fetchs(params, "hv", 0))) {
1360 SV *const rv = *svp;
1361 if (!SvROK(rv))
1362 croak("common passed a non-reference for parameter hv");
1363 hv = (HV *)SvRV(rv);
1364 }
1365 if ((svp = hv_fetchs(params, "keysv", 0)))
1366 keysv = *svp;
1367 if ((svp = hv_fetchs(params, "keypv", 0))) {
1368 key = SvPV_const(*svp, klen);
1369 if (SvUTF8(*svp))
1370 flags = HVhek_UTF8;
1371 }
1372 if ((svp = hv_fetchs(params, "action", 0)))
1373 action = SvIV(*svp);
1374 if ((svp = hv_fetchs(params, "val", 0)))
527df579 1375 val = newSVsv(*svp);
6b4de907 1376 if ((svp = hv_fetchs(params, "hash", 0)))
a44093a9 1377 hash = SvUV(*svp);
6b4de907 1378
527df579
NC
1379 if ((svp = hv_fetchs(params, "hash_pv", 0))) {
1380 PERL_HASH(hash, key, klen);
1381 }
58ca560a
NC
1382 if ((svp = hv_fetchs(params, "hash_sv", 0))) {
1383 STRLEN len;
1384 const char *const p = SvPV(keysv, len);
1385 PERL_HASH(hash, p, len);
1386 }
527df579 1387
a75fcbca 1388 result = (HE *)hv_common(hv, keysv, key, klen, flags, action, val, hash);
6b4de907
NC
1389 if (!result) {
1390 XSRETURN_EMPTY;
1391 }
1392 /* Force mg_get */
1393 RETVAL = newSVsv(HeVAL(result));
1394 OUTPUT:
1395 RETVAL
1396
9568a123
NC
1397#endif
1398
439efdfe 1399void
2dc92170
NC
1400test_hv_free_ent()
1401 PPCODE:
1402 test_freeent(&Perl_hv_free_ent);
1403 XSRETURN(4);
1404
439efdfe 1405void
2dc92170
NC
1406test_hv_delayfree_ent()
1407 PPCODE:
1408 test_freeent(&Perl_hv_delayfree_ent);
1409 XSRETURN(4);
35ab5632
NC
1410
1411SV *
1412test_share_unshare_pvn(input)
1413 PREINIT:
35ab5632
NC
1414 STRLEN len;
1415 U32 hash;
1416 char *pvx;
1417 char *p;
1418 INPUT:
1419 SV *input
1420 CODE:
1421 pvx = SvPV(input, len);
1422 PERL_HASH(hash, pvx, len);
1423 p = sharepvn(pvx, len, hash);
1424 RETVAL = newSVpvn(p, len);
1425 unsharepvn(p, len, hash);
1426 OUTPUT:
1427 RETVAL
d8c5b3c5 1428
9568a123
NC
1429#if PERL_VERSION >= 9
1430
d8c5b3c5
NC
1431bool
1432refcounted_he_exists(key, level=0)
1433 SV *key
1434 IV level
1435 CODE:
1436 if (level) {
1437 croak("level must be zero, not %"IVdf, level);
1438 }
20439bc7 1439 RETVAL = (cop_hints_fetch_sv(PL_curcop, key, 0, 0) != &PL_sv_placeholder);
d8c5b3c5
NC
1440 OUTPUT:
1441 RETVAL
1442
d8c5b3c5
NC
1443SV *
1444refcounted_he_fetch(key, level=0)
1445 SV *key
1446 IV level
1447 CODE:
1448 if (level) {
1449 croak("level must be zero, not %"IVdf, level);
1450 }
20439bc7 1451 RETVAL = cop_hints_fetch_sv(PL_curcop, key, 0, 0);
d8c5b3c5
NC
1452 SvREFCNT_inc(RETVAL);
1453 OUTPUT:
1454 RETVAL
65bfe90c 1455
9568a123 1456#endif
65bfe90c 1457
0314122a
NC
1458=pod
1459
1460sub TIEHASH { bless {}, $_[0] }
1461sub STORE { $_[0]->{$_[1]} = $_[2] }
1462sub FETCH { $_[0]->{$_[1]} }
1463sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
1464sub NEXTKEY { each %{$_[0]} }
1465sub EXISTS { exists $_[0]->{$_[1]} }
1466sub DELETE { delete $_[0]->{$_[1]} }
1467sub CLEAR { %{$_[0]} = () }
1468
1469=cut
1470
e2fe06dd
EB
1471MODULE = XS::APItest:TempLv PACKAGE = XS::APItest::TempLv
1472
1473void
1474make_temp_mg_lv(sv)
1475SV* sv
1476 PREINIT:
1477 SV * const lv = newSV_type(SVt_PVLV);
1478 STRLEN len;
1479 PPCODE:
1480 SvPV(sv, len);
1481
1482 sv_magic(lv, NULL, PERL_MAGIC_substr, NULL, 0);
1483 LvTYPE(lv) = 'x';
1484 LvTARG(lv) = SvREFCNT_inc_simple(sv);
1485 LvTARGOFF(lv) = len == 0 ? 0 : 1;
1486 LvTARGLEN(lv) = len < 2 ? 0 : len-2;
1487
1488 EXTEND(SP, 1);
1489 ST(0) = sv_2mortal(lv);
1490 XSRETURN(1);
1491
1492
36c2b1d0
NC
1493MODULE = XS::APItest::PtrTable PACKAGE = XS::APItest::PtrTable PREFIX = ptr_table_
1494
1495void
1496ptr_table_new(classname)
1497const char * classname
1498 PPCODE:
1499 PUSHs(sv_setref_pv(sv_newmortal(), classname, (void*)ptr_table_new()));
1500
1501void
1502DESTROY(table)
1503XS::APItest::PtrTable table
1504 CODE:
1505 ptr_table_free(table);
1506
1507void
992b2363 1508ptr_table_store(table, from, to)
36c2b1d0 1509XS::APItest::PtrTable table
992b2363
NC
1510SVREF from
1511SVREF to
36c2b1d0 1512 CODE:
992b2363 1513 ptr_table_store(table, from, to);
36c2b1d0
NC
1514
1515UV
992b2363 1516ptr_table_fetch(table, from)
36c2b1d0 1517XS::APItest::PtrTable table
992b2363 1518SVREF from
36c2b1d0 1519 CODE:
992b2363 1520 RETVAL = PTR2UV(ptr_table_fetch(table, from));
36c2b1d0
NC
1521 OUTPUT:
1522 RETVAL
1523
1524void
1525ptr_table_split(table)
1526XS::APItest::PtrTable table
1527
1528void
1529ptr_table_clear(table)
1530XS::APItest::PtrTable table
1531
6911735f
FC
1532MODULE = XS::APItest::AutoLoader PACKAGE = XS::APItest::AutoLoader
1533
1534SV *
1535AUTOLOAD()
1536 CODE:
1537 RETVAL = newSVpvn_flags(SvPVX(cv), SvCUR(cv), SvUTF8(cv));
1538 OUTPUT:
1539 RETVAL
1540
8fa6a409
FC
1541SV *
1542AUTOLOADp(...)
1543 PROTOTYPE: *$
1544 CODE:
1545 RETVAL = newSVpvn_flags(SvPVX(cv), SvCUR(cv), SvUTF8(cv));
1546 OUTPUT:
1547 RETVAL
1548
6911735f 1549
3e61d65a
JH
1550MODULE = XS::APItest PACKAGE = XS::APItest
1551
1552PROTOTYPES: DISABLE
1553
67ecba2f
FC
1554BOOT:
1555 mymro.resolve = myget_linear_isa;
1556 mymro.name = "justisa";
1557 mymro.length = 7;
1558 mymro.kflags = 0;
1559 mymro.hash = 0;
1560 Perl_mro_register(aTHX_ &mymro);
1561
f568d64d
BM
1562HV *
1563xop_custom_ops ()
1564 CODE:
1565 RETVAL = PL_custom_ops;
1566 OUTPUT:
1567 RETVAL
1568
1569HV *
1570xop_custom_op_names ()
1571 CODE:
1572 PL_custom_op_names = newHV();
1573 RETVAL = PL_custom_op_names;
1574 OUTPUT:
1575 RETVAL
1576
1577HV *
1578xop_custom_op_descs ()
1579 CODE:
1580 PL_custom_op_descs = newHV();
1581 RETVAL = PL_custom_op_descs;
1582 OUTPUT:
1583 RETVAL
1584
1585void
1586xop_register ()
1587 CODE:
1588 XopENTRY_set(&my_xop, xop_name, "my_xop");
1589 XopENTRY_set(&my_xop, xop_desc, "XOP for testing");
1590 XopENTRY_set(&my_xop, xop_class, OA_UNOP);
1591 XopENTRY_set(&my_xop, xop_peep, peep_xop);
1592 Perl_custom_op_register(aTHX_ pp_xop, &my_xop);
1593
1594void
1595xop_clear ()
1596 CODE:
1597 XopDISABLE(&my_xop, xop_name);
1598 XopDISABLE(&my_xop, xop_desc);
1599 XopDISABLE(&my_xop, xop_class);
1600 XopDISABLE(&my_xop, xop_peep);
1601
1602IV
1603xop_my_xop ()
1604 CODE:
1605 RETVAL = PTR2IV(&my_xop);
1606 OUTPUT:
1607 RETVAL
1608
1609IV
1610xop_ppaddr ()
1611 CODE:
1612 RETVAL = PTR2IV(pp_xop);
1613 OUTPUT:
1614 RETVAL
1615
1616IV
1617xop_OA_UNOP ()
1618 CODE:
1619 RETVAL = OA_UNOP;
1620 OUTPUT:
1621 RETVAL
1622
1623AV *
1624xop_build_optree ()
1625 CODE:
1626 dMY_CXT;
1627 UNOP *unop;
1628 OP *kid;
1629
1630 MY_CXT.xop_record = newAV();
1631
1632 kid = newSVOP(OP_CONST, 0, newSViv(42));
1633
1634 NewOp(1102, unop, 1, UNOP);
1635 unop->op_type = OP_CUSTOM;
1636 unop->op_ppaddr = pp_xop;
1637 unop->op_flags = OPf_KIDS;
1638 unop->op_private = 0;
1639 unop->op_first = kid;
1640 unop->op_next = NULL;
1641 kid->op_next = (OP*)unop;
1642
b58757d5
BM
1643 av_push(MY_CXT.xop_record, newSVpvf("unop:%"UVxf, PTR2UV(unop)));
1644 av_push(MY_CXT.xop_record, newSVpvf("kid:%"UVxf, PTR2UV(kid)));
f568d64d
BM
1645
1646 av_push(MY_CXT.xop_record, newSVpvf("NAME:%s", OP_NAME((OP*)unop)));
1647 av_push(MY_CXT.xop_record, newSVpvf("DESC:%s", OP_DESC((OP*)unop)));
c33e8be1 1648 av_push(MY_CXT.xop_record, newSVpvf("CLASS:%d", (int)OP_CLASS((OP*)unop)));
f568d64d
BM
1649
1650 PL_rpeepp(aTHX_ kid);
1651
1652 FreeOp(kid);
1653 FreeOp(unop);
1654
1655 RETVAL = MY_CXT.xop_record;
1656 MY_CXT.xop_record = NULL;
1657 OUTPUT:
1658 RETVAL
1659
85ce96a1
DM
1660BOOT:
1661{
1662 MY_CXT_INIT;
03569ecf 1663
85ce96a1
DM
1664 MY_CXT.i = 99;
1665 MY_CXT.sv = newSVpv("initial",0);
13b6b3bc
BM
1666
1667 MY_CXT.bhkav = get_av("XS::APItest::bhkav", GV_ADDMULTI);
1668 MY_CXT.bhk_record = 0;
1669
a88d97bf
BM
1670 BhkENTRY_set(&bhk_test, bhk_start, blockhook_test_start);
1671 BhkENTRY_set(&bhk_test, bhk_pre_end, blockhook_test_pre_end);
1672 BhkENTRY_set(&bhk_test, bhk_post_end, blockhook_test_post_end);
1673 BhkENTRY_set(&bhk_test, bhk_eval, blockhook_test_eval);
13b6b3bc
BM
1674 Perl_blockhook_register(aTHX_ &bhk_test);
1675
65bfe90c 1676 MY_CXT.cscgv = gv_fetchpvs("XS::APItest::COMPILE_SCOPE_CONTAINER",
13b6b3bc 1677 GV_ADDMULTI, SVt_PVAV);
03569ecf
BM
1678 MY_CXT.cscav = GvAV(MY_CXT.cscgv);
1679
a88d97bf
BM
1680 BhkENTRY_set(&bhk_csc, bhk_start, blockhook_csc_start);
1681 BhkENTRY_set(&bhk_csc, bhk_pre_end, blockhook_csc_pre_end);
13b6b3bc 1682 Perl_blockhook_register(aTHX_ &bhk_csc);
201c7e1f
FR
1683
1684 MY_CXT.peep_recorder = newAV();
1685 MY_CXT.rpeep_recorder = newAV();
1686
1687 MY_CXT.orig_peep = PL_peepp;
1688 MY_CXT.orig_rpeep = PL_rpeepp;
1689 PL_peepp = my_peep;
1690 PL_rpeepp = my_rpeep;
65bfe90c 1691}
85ce96a1
DM
1692
1693void
1694CLONE(...)
1695 CODE:
1696 MY_CXT_CLONE;
c33e8be1 1697 PERL_UNUSED_VAR(items);
85ce96a1 1698 MY_CXT.sv = newSVpv("initial_clone",0);
65bfe90c 1699 MY_CXT.cscgv = gv_fetchpvs("XS::APItest::COMPILE_SCOPE_CONTAINER",
13b6b3bc 1700 GV_ADDMULTI, SVt_PVAV);
03569ecf 1701 MY_CXT.cscav = NULL;
13b6b3bc
BM
1702 MY_CXT.bhkav = get_av("XS::APItest::bhkav", GV_ADDMULTI);
1703 MY_CXT.bhk_record = 0;
201c7e1f
FR
1704 MY_CXT.peep_recorder = newAV();
1705 MY_CXT.rpeep_recorder = newAV();
85ce96a1 1706
3e61d65a
JH
1707void
1708print_double(val)
1709 double val
1710 CODE:
1711 printf("%5.3f\n",val);
1712
1713int
1714have_long_double()
1715 CODE:
1716#ifdef HAS_LONG_DOUBLE
1717 RETVAL = 1;
1718#else
1719 RETVAL = 0;
1720#endif
cabb36f0
CN
1721 OUTPUT:
1722 RETVAL
3e61d65a
JH
1723
1724void
1725print_long_double()
1726 CODE:
1727#ifdef HAS_LONG_DOUBLE
fc0bf671 1728# if defined(PERL_PRIfldbl) && (LONG_DOUBLESIZE > DOUBLESIZE)
3e61d65a
JH
1729 long double val = 7.0;
1730 printf("%5.3" PERL_PRIfldbl "\n",val);
1731# else
1732 double val = 7.0;
1733 printf("%5.3f\n",val);
1734# endif
1735#endif
1736
1737void
3e61d65a
JH
1738print_int(val)
1739 int val
1740 CODE:
1741 printf("%d\n",val);
1742
1743void
1744print_long(val)
1745 long val
1746 CODE:
1747 printf("%ld\n",val);
1748
1749void
1750print_float(val)
1751 float val
1752 CODE:
1753 printf("%5.3f\n",val);
9d911683
NIS
1754
1755void
1756print_flush()
1757 CODE:
1758 fflush(stdout);
d4b90eee
SH
1759
1760void
1761mpushp()
1762 PPCODE:
1763 EXTEND(SP, 3);
1764 mPUSHp("one", 3);
1765 mPUSHp("two", 3);
1766 mPUSHp("three", 5);
1767 XSRETURN(3);
1768
1769void
1770mpushn()
1771 PPCODE:
1772 EXTEND(SP, 3);
1773 mPUSHn(0.5);
1774 mPUSHn(-0.25);
1775 mPUSHn(0.125);
1776 XSRETURN(3);
1777
1778void
1779mpushi()
1780 PPCODE:
1781 EXTEND(SP, 3);
d75b63cf
MHM
1782 mPUSHi(-1);
1783 mPUSHi(2);
1784 mPUSHi(-3);
d4b90eee
SH
1785 XSRETURN(3);
1786
1787void
1788mpushu()
1789 PPCODE:
1790 EXTEND(SP, 3);
d75b63cf
MHM
1791 mPUSHu(1);
1792 mPUSHu(2);
1793 mPUSHu(3);
d4b90eee
SH
1794 XSRETURN(3);
1795
1796void
1797mxpushp()
1798 PPCODE:
1799 mXPUSHp("one", 3);
1800 mXPUSHp("two", 3);
1801 mXPUSHp("three", 5);
1802 XSRETURN(3);
1803
1804void
1805mxpushn()
1806 PPCODE:
1807 mXPUSHn(0.5);
1808 mXPUSHn(-0.25);
1809 mXPUSHn(0.125);
1810 XSRETURN(3);
1811
1812void
1813mxpushi()
1814 PPCODE:
d75b63cf
MHM
1815 mXPUSHi(-1);
1816 mXPUSHi(2);
1817 mXPUSHi(-3);
d4b90eee
SH
1818 XSRETURN(3);
1819
1820void
1821mxpushu()
1822 PPCODE:
d75b63cf
MHM
1823 mXPUSHu(1);
1824 mXPUSHu(2);
1825 mXPUSHu(3);
d4b90eee 1826 XSRETURN(3);
d1f347d7
DM
1827
1828
1829void
1830call_sv(sv, flags, ...)
1831 SV* sv
1832 I32 flags
1833 PREINIT:
1834 I32 i;
1835 PPCODE:
1836 for (i=0; i<items-2; i++)
1837 ST(i) = ST(i+2); /* pop first two args */
1838 PUSHMARK(SP);
1839 SP += items - 2;
1840 PUTBACK;
1841 i = call_sv(sv, flags);
1842 SPAGAIN;
1843 EXTEND(SP, 1);
1844 PUSHs(sv_2mortal(newSViv(i)));
1845
1846void
1847call_pv(subname, flags, ...)
1848 char* subname
1849 I32 flags
1850 PREINIT:
1851 I32 i;
1852 PPCODE:
1853 for (i=0; i<items-2; i++)
1854 ST(i) = ST(i+2); /* pop first two args */
1855 PUSHMARK(SP);
1856 SP += items - 2;
1857 PUTBACK;
1858 i = call_pv(subname, flags);
1859 SPAGAIN;
1860 EXTEND(SP, 1);
1861 PUSHs(sv_2mortal(newSViv(i)));
1862
1863void
1864call_method(methname, flags, ...)
1865 char* methname
1866 I32 flags
1867 PREINIT:
1868 I32 i;
1869 PPCODE:
1870 for (i=0; i<items-2; i++)
1871 ST(i) = ST(i+2); /* pop first two args */
1872 PUSHMARK(SP);
1873 SP += items - 2;
1874 PUTBACK;
1875 i = call_method(methname, flags);
1876 SPAGAIN;
1877 EXTEND(SP, 1);
1878 PUSHs(sv_2mortal(newSViv(i)));
1879
1880void
8f82b567 1881newCONSTSUB_type(stash, name, flags, type, sv)
3453414d
BF
1882 HV* stash
1883 SV* name
1884 I32 flags
1885 int type
8f82b567 1886 SV* sv
3453414d
BF
1887 PREINIT:
1888 CV* cv;
9c0a6090
FC
1889 STRLEN len;
1890 const char *pv = SvPV(name, len);
3453414d
BF
1891 PPCODE:
1892 switch (type) {
1893 case 0:
8f82b567 1894 cv = newCONSTSUB(stash, pv, SvOK(sv) ? sv : NULL);
3453414d
BF
1895 break;
1896 case 1:
8f82b567
FC
1897 cv = newCONSTSUB_flags(
1898 stash, pv, len, flags | SvUTF8(name), SvOK(sv) ? sv : NULL
1899 );
3453414d
BF
1900 break;
1901 }
1902 EXTEND(SP, 2);
1903 PUSHs( CvCONST(cv) ? &PL_sv_yes : &PL_sv_no );
1904 PUSHs((SV*)CvGV(cv));
1905
1906void
e6066781
BF
1907gv_init_type(namesv, multi, flags, type)
1908 SV* namesv
1909 int multi
1910 I32 flags
1911 int type
1912 PREINIT:
1913 STRLEN len;
1914 const char * const name = SvPV_const(namesv, len);
1915 GV *gv = *(GV**)hv_fetch(PL_defstash, name, len, TRUE);
1916 PPCODE:
1917 if (SvTYPE(gv) == SVt_PVGV)
1918 Perl_croak(aTHX_ "GV is already a PVGV");
04ec7e59 1919 if (multi) flags |= GV_ADDMULTI;
e6066781
BF
1920 switch (type) {
1921 case 0:
1922 gv_init(gv, PL_defstash, name, len, multi);
1923 break;
1924 case 1:
04ec7e59 1925 gv_init_sv(gv, PL_defstash, namesv, flags);
e6066781
BF
1926 break;
1927 case 2:
04ec7e59 1928 gv_init_pv(gv, PL_defstash, name, flags | SvUTF8(namesv));
e6066781
BF
1929 break;
1930 case 3:
04ec7e59 1931 gv_init_pvn(gv, PL_defstash, name, len, flags | SvUTF8(namesv));
e6066781
BF
1932 break;
1933 }
1934 XPUSHs( gv ? (SV*)gv : &PL_sv_undef);
1935
1936void
e6919483
BF
1937gv_fetchmeth_type(stash, methname, type, level, flags)
1938 HV* stash
1939 SV* methname
1940 int type
1941 I32 level
1942 I32 flags
1943 PREINIT:
1944 STRLEN len;
1945 const char * const name = SvPV_const(methname, len);
1946 GV* gv;
1947 PPCODE:
1948 switch (type) {
1949 case 0:
1950 gv = gv_fetchmeth(stash, name, len, level);
1951 break;
1952 case 1:
1953 gv = gv_fetchmeth_sv(stash, methname, level, flags);
1954 break;
1955 case 2:
1956 gv = gv_fetchmeth_pv(stash, name, level, flags | SvUTF8(methname));
1957 break;
1958 case 3:
1959 gv = gv_fetchmeth_pvn(stash, name, len, level, flags | SvUTF8(methname));
1960 break;
1961 }
1962 XPUSHs( gv ? MUTABLE_SV(gv) : &PL_sv_undef );
1963
1964void
d21989ed
BF
1965gv_fetchmeth_autoload_type(stash, methname, type, level, flags)
1966 HV* stash
1967 SV* methname
1968 int type
1969 I32 level
1970 I32 flags
1971 PREINIT:
1972 STRLEN len;
1973 const char * const name = SvPV_const(methname, len);
1974 GV* gv;
1975 PPCODE:
1976 switch (type) {
1977 case 0:
1978 gv = gv_fetchmeth_autoload(stash, name, len, level);
1979 break;
1980 case 1:
1981 gv = gv_fetchmeth_sv_autoload(stash, methname, level, flags);
1982 break;
1983 case 2:
1984 gv = gv_fetchmeth_pv_autoload(stash, name, level, flags | SvUTF8(methname));
1985 break;
1986 case 3:
1987 gv = gv_fetchmeth_pvn_autoload(stash, name, len, level, flags | SvUTF8(methname));
1988 break;
1989 }
1990 XPUSHs( gv ? MUTABLE_SV(gv) : &PL_sv_undef );
1991
1992void
44130a26
BF
1993gv_fetchmethod_flags_type(stash, methname, type, flags)
1994 HV* stash
1995 SV* methname
1996 int type
1997 I32 flags
1998 PREINIT:
1999 GV* gv;
2000 PPCODE:
2001 switch (type) {
2002 case 0:
2003 gv = gv_fetchmethod_flags(stash, SvPVX_const(methname), flags);
2004 break;
2005 case 1:
2006 gv = gv_fetchmethod_sv_flags(stash, methname, flags);
2007 break;
2008 case 2:
2009 gv = gv_fetchmethod_pv_flags(stash, SvPV_nolen(methname), flags | SvUTF8(methname));
2010 break;
2011 case 3: {
2012 STRLEN len;
2013 const char * const name = SvPV_const(methname, len);
2014 gv = gv_fetchmethod_pvn_flags(stash, name, len, flags | SvUTF8(methname));
2015 break;
2016 }
2017 }
5fba3c91 2018 XPUSHs( gv ? (SV*)gv : &PL_sv_undef);
44130a26 2019
5fba3c91 2020void
0eeb01b9 2021gv_autoload_type(stash, methname, type, method)
5fba3c91
BF
2022 HV* stash
2023 SV* methname
2024 int type
2025 I32 method
5fba3c91
BF
2026 PREINIT:
2027 STRLEN len;
2028 const char * const name = SvPV_const(methname, len);
2029 GV* gv;
0eeb01b9 2030 I32 flags = method ? GV_AUTOLOAD_ISMETHOD : 0;
5fba3c91
BF
2031 PPCODE:
2032 switch (type) {
2033 case 0:
2034 gv = gv_autoload4(stash, name, len, method);
2035 break;
2036 case 1:
0eeb01b9 2037 gv = gv_autoload_sv(stash, methname, flags);
5fba3c91
BF
2038 break;
2039 case 2:
0eeb01b9 2040 gv = gv_autoload_pv(stash, name, flags | SvUTF8(methname));
5fba3c91
BF
2041 break;
2042 case 3:
0eeb01b9 2043 gv = gv_autoload_pvn(stash, name, len, flags | SvUTF8(methname));
5fba3c91
BF
2044 break;
2045 }
44130a26
BF
2046 XPUSHs( gv ? (SV*)gv : &PL_sv_undef);
2047
2048void
84c7b88c
BF
2049whichsig_type(namesv, type)
2050 SV* namesv
2051 int type
2052 PREINIT:
2053 STRLEN len;
2054 const char * const name = SvPV_const(namesv, len);
2055 I32 i;
2056 PPCODE:
2057 switch (type) {
2058 case 0:
2059 i = whichsig(name);
2060 break;
2061 case 1:
2062 i = whichsig_sv(namesv);
2063 break;
2064 case 2:
2065 i = whichsig_pv(name);
2066 break;
2067 case 3:
2068 i = whichsig_pvn(name, len);
2069 break;
2070 }
2071 XPUSHs(sv_2mortal(newSViv(i)));
2072
2073void
d1f347d7
DM
2074eval_sv(sv, flags)
2075 SV* sv
2076 I32 flags
2077 PREINIT:
2078 I32 i;
2079 PPCODE:
2080 PUTBACK;
2081 i = eval_sv(sv, flags);
2082 SPAGAIN;
2083 EXTEND(SP, 1);
2084 PUSHs(sv_2mortal(newSViv(i)));
2085
b8e65a9b 2086void
d1f347d7
DM
2087eval_pv(p, croak_on_error)
2088 const char* p
2089 I32 croak_on_error
d1f347d7
DM
2090 PPCODE:
2091 PUTBACK;
2092 EXTEND(SP, 1);
2093 PUSHs(eval_pv(p, croak_on_error));
2094
2095void
2096require_pv(pv)
2097 const char* pv
d1f347d7
DM
2098 PPCODE:
2099 PUTBACK;
2100 require_pv(pv);
2101
0ca3a874 2102int
7a646707 2103apitest_exception(throw_e)
0ca3a874
MHM
2104 int throw_e
2105 OUTPUT:
2106 RETVAL
d1f347d7 2107
ef469b03 2108void
7e7a3dfc
GA
2109mycroak(sv)
2110 SV* sv
ef469b03 2111 CODE:
7e7a3dfc
GA
2112 if (SvOK(sv)) {
2113 Perl_croak(aTHX_ "%s", SvPV_nolen(sv));
2114 }
2115 else {
2116 Perl_croak(aTHX_ NULL);
2117 }
5d2b1485
NC
2118
2119SV*
2120strtab()
2121 CODE:
2122 RETVAL = newRV_inc((SV*)PL_strtab);
2123 OUTPUT:
2124 RETVAL
85ce96a1
DM
2125
2126int
2127my_cxt_getint()
2128 CODE:
2129 dMY_CXT;
2130 RETVAL = my_cxt_getint_p(aMY_CXT);
2131 OUTPUT:
2132 RETVAL
2133
2134void
2135my_cxt_setint(i)
2136 int i;
2137 CODE:
2138 dMY_CXT;
2139 my_cxt_setint_p(aMY_CXT_ i);
2140
2141void
9568a123
NC
2142my_cxt_getsv(how)
2143 bool how;
85ce96a1 2144 PPCODE:
85ce96a1 2145 EXTEND(SP, 1);
9568a123 2146 ST(0) = how ? my_cxt_getsv_interp_context() : my_cxt_getsv_interp();
85ce96a1
DM
2147 XSRETURN(1);
2148
2149void
2150my_cxt_setsv(sv)
2151 SV *sv;
2152 CODE:
2153 dMY_CXT;
2154 SvREFCNT_dec(MY_CXT.sv);
2155 my_cxt_setsv_p(sv _aMY_CXT);
2156 SvREFCNT_inc(sv);
34482cd6
NC
2157
2158bool
2159sv_setsv_cow_hashkey_core()
2160
2161bool
2162sv_setsv_cow_hashkey_notcore()
84ac5fd7
NC
2163
2164void
74ee33f2
FC
2165sv_set_deref(SV *sv, SV *sv2, int which)
2166 CODE:
2167 {
2168 STRLEN len;
2169 const char *pv = SvPV(sv2,len);
2170 if (!SvROK(sv)) croak("Not a ref");
2171 sv = SvRV(sv);
2172 switch (which) {
2173 case 0: sv_setsv(sv,sv2); break;
2174 case 1: sv_setpv(sv,pv); break;
2175 case 2: sv_setpvn(sv,pv,len); break;
2176 }
2177 }
2178
2179void
218787bd
VP
2180rmagical_cast(sv, type)
2181 SV *sv;
2182 SV *type;
2183 PREINIT:
2184 struct ufuncs uf;
2185 PPCODE:
2186 if (!SvOK(sv) || !SvROK(sv) || !SvOK(type)) { XSRETURN_UNDEF; }
2187 sv = SvRV(sv);
2188 if (SvTYPE(sv) != SVt_PVHV) { XSRETURN_UNDEF; }
2189 uf.uf_val = rmagical_a_dummy;
2190 uf.uf_set = NULL;
2191 uf.uf_index = 0;
2192 if (SvTRUE(type)) { /* b */
2193 sv_magicext(sv, NULL, PERL_MAGIC_ext, &rmagical_b, NULL, 0);
2194 } else { /* a */
2195 sv_magic(sv, NULL, PERL_MAGIC_uvar, (char *) &uf, sizeof(uf));
2196 }
2197 XSRETURN_YES;
2198
2199void
2200rmagical_flags(sv)
2201 SV *sv;
2202 PPCODE:
2203 if (!SvOK(sv) || !SvROK(sv)) { XSRETURN_UNDEF; }
2204 sv = SvRV(sv);
2205 EXTEND(SP, 3);
2206 mXPUSHu(SvFLAGS(sv) & SVs_GMG);
2207 mXPUSHu(SvFLAGS(sv) & SVs_SMG);
2208 mXPUSHu(SvFLAGS(sv) & SVs_RMG);
2209 XSRETURN(3);
2210
2211void
90d1f214
BM
2212my_caller(level)
2213 I32 level
2214 PREINIT:
2215 const PERL_CONTEXT *cx, *dbcx;
2216 const char *pv;
2217 const GV *gv;
2218 HV *hv;
2219 PPCODE:
2220 cx = caller_cx(level, &dbcx);
2221 EXTEND(SP, 8);
2222
2223 pv = CopSTASHPV(cx->blk_oldcop);
2224 ST(0) = pv ? sv_2mortal(newSVpv(pv, 0)) : &PL_sv_undef;
2225 gv = CvGV(cx->blk_sub.cv);
2226 ST(1) = isGV(gv) ? sv_2mortal(newSVpv(GvNAME(gv), 0)) : &PL_sv_undef;
2227
2228 pv = CopSTASHPV(dbcx->blk_oldcop);
2229 ST(2) = pv ? sv_2mortal(newSVpv(pv, 0)) : &PL_sv_undef;
2230 gv = CvGV(dbcx->blk_sub.cv);
2231 ST(3) = isGV(gv) ? sv_2mortal(newSVpv(GvNAME(gv), 0)) : &PL_sv_undef;
2232
20439bc7
Z
2233 ST(4) = cop_hints_fetch_pvs(cx->blk_oldcop, "foo", 0);
2234 ST(5) = cop_hints_fetch_pvn(cx->blk_oldcop, "foo", 3, 0, 0);
2235 ST(6) = cop_hints_fetch_sv(cx->blk_oldcop,
2236 sv_2mortal(newSVpvn("foo", 3)), 0, 0);
90d1f214 2237
20439bc7 2238 hv = cop_hints_2hv(cx->blk_oldcop, 0);
90d1f214
BM
2239 ST(7) = hv ? sv_2mortal(newRV_noinc((SV *)hv)) : &PL_sv_undef;
2240
2241 XSRETURN(8);
2242
2243void
f9c17636
MB
2244DPeek (sv)
2245 SV *sv
2246
2247 PPCODE:
5b1f7359 2248 ST (0) = newSVpv (Perl_sv_peek (aTHX_ sv), 0);
f9c17636
MB
2249 XSRETURN (1);
2250
2251void
84ac5fd7
NC
2252BEGIN()
2253 CODE:
2254 sv_inc(get_sv("XS::APItest::BEGIN_called", GV_ADD|GV_ADDMULTI));
2255
2256void
2257CHECK()
2258 CODE:
2259 sv_inc(get_sv("XS::APItest::CHECK_called", GV_ADD|GV_ADDMULTI));
2260
2261void
2262UNITCHECK()
2263 CODE:
0932863f 2264 sv_inc(get_sv("XS::APItest::UNITCHECK_called", GV_ADD|GV_ADDMULTI));
84ac5fd7
NC
2265
2266void
2267INIT()
2268 CODE:
2269 sv_inc(get_sv("XS::APItest::INIT_called", GV_ADD|GV_ADDMULTI));
2270
2271void
2272END()
2273 CODE:
2274 sv_inc(get_sv("XS::APItest::END_called", GV_ADD|GV_ADDMULTI));
30685b56
NC
2275
2276void
2277utf16_to_utf8 (sv, ...)
2278 SV* sv
2279 ALIAS:
2280 utf16_to_utf8_reversed = 1
2281 PREINIT:
2282 STRLEN len;
2283 U8 *source;
2284 SV *dest;
2285 I32 got; /* Gah, badly thought out APIs */
2286 CODE:
2287 source = (U8 *)SvPVbyte(sv, len);
2288 /* Optionally only convert part of the buffer. */
2289 if (items > 1) {
2290 len = SvUV(ST(1));
2291 }
2292 /* Mortalise this right now, as we'll be testing croak()s */
2293 dest = sv_2mortal(newSV(len * 3 / 2 + 1));
2294 if (ix) {
25f2e844 2295 utf16_to_utf8_reversed(source, (U8 *)SvPVX(dest), len, &got);
30685b56 2296 } else {
25f2e844 2297 utf16_to_utf8(source, (U8 *)SvPVX(dest), len, &got);
30685b56
NC
2298 }
2299 SvCUR_set(dest, got);
2300 SvPVX(dest)[got] = '\0';
2301 SvPOK_on(dest);
2302 ST(0) = dest;
2303 XSRETURN(1);
879d0c72 2304
6bd7445c
GG
2305void
2306my_exit(int exitcode)
2307 PPCODE:
2308 my_exit(exitcode);
d97c33b5 2309
209e41dc
NT
2310U8
2311first_byte(sv)
2312 SV *sv
2313 CODE:
2314 char *s;
2315 STRLEN len;
2316 s = SvPVbyte(sv, len);
2317 RETVAL = s[0];
2318 OUTPUT:
2319 RETVAL
2320
d97c33b5
DM
2321I32
2322sv_count()
2323 CODE:
2324 RETVAL = PL_sv_count;
2325 OUTPUT:
2326 RETVAL
13b6b3bc
BM
2327
2328void
2329bhk_record(bool on)
2330 CODE:
2331 dMY_CXT;
2332 MY_CXT.bhk_record = on;
2333 if (on)
2334 av_clear(MY_CXT.bhkav);
65bfe90c 2335
defdfed5 2336void
d9088386
Z
2337test_magic_chain()
2338 PREINIT:
2339 SV *sv;
2340 MAGIC *callmg, *uvarmg;
2341 CODE:
2342 sv = sv_2mortal(newSV(0));
11f9f0ed
NC
2343 if (SvTYPE(sv) >= SVt_PVMG) croak_fail();
2344 if (SvMAGICAL(sv)) croak_fail();
d9088386 2345 sv_magic(sv, &PL_sv_yes, PERL_MAGIC_checkcall, (char*)&callmg, 0);
11f9f0ed
NC
2346 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2347 if (!SvMAGICAL(sv)) croak_fail();
2348 if (mg_find(sv, PERL_MAGIC_uvar)) croak_fail();
d9088386 2349 callmg = mg_find(sv, PERL_MAGIC_checkcall);
11f9f0ed 2350 if (!callmg) croak_fail();
d9088386 2351 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 2352 croak_fail();
d9088386 2353 sv_magic(sv, &PL_sv_no, PERL_MAGIC_uvar, (char*)&uvarmg, 0);
11f9f0ed
NC
2354 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2355 if (!SvMAGICAL(sv)) croak_fail();
2356 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
d9088386 2357 uvarmg = mg_find(sv, PERL_MAGIC_uvar);
11f9f0ed 2358 if (!uvarmg) croak_fail();
d9088386 2359 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 2360 croak_fail();
d9088386 2361 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 2362 croak_fail();
d9088386 2363 mg_free_type(sv, PERL_MAGIC_vec);
11f9f0ed
NC
2364 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2365 if (!SvMAGICAL(sv)) croak_fail();
2366 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
2367 if (mg_find(sv, PERL_MAGIC_uvar) != uvarmg) croak_fail();
d9088386 2368 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 2369 croak_fail();
d9088386 2370 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 2371 croak_fail();
d9088386 2372 mg_free_type(sv, PERL_MAGIC_uvar);
11f9f0ed
NC
2373 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2374 if (!SvMAGICAL(sv)) croak_fail();
2375 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
2376 if (mg_find(sv, PERL_MAGIC_uvar)) croak_fail();
d9088386 2377 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 2378 croak_fail();
d9088386 2379 sv_magic(sv, &PL_sv_no, PERL_MAGIC_uvar, (char*)&uvarmg, 0);
11f9f0ed
NC
2380 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2381 if (!SvMAGICAL(sv)) croak_fail();
2382 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
d9088386 2383 uvarmg = mg_find(sv, PERL_MAGIC_uvar);
11f9f0ed 2384 if (!uvarmg) croak_fail();
d9088386 2385 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 2386 croak_fail();
d9088386 2387 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 2388 croak_fail();
d9088386 2389 mg_free_type(sv, PERL_MAGIC_checkcall);
11f9f0ed
NC
2390 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2391 if (!SvMAGICAL(sv)) croak_fail();
2392 if (mg_find(sv, PERL_MAGIC_uvar) != uvarmg) croak_fail();
2393 if (mg_find(sv, PERL_MAGIC_checkcall)) croak_fail();
d9088386 2394 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 2395 croak_fail();
d9088386 2396 mg_free_type(sv, PERL_MAGIC_uvar);
11f9f0ed
NC
2397 if (SvMAGICAL(sv)) croak_fail();
2398 if (mg_find(sv, PERL_MAGIC_checkcall)) croak_fail();
2399 if (mg_find(sv, PERL_MAGIC_uvar)) croak_fail();
d9088386
Z
2400
2401void
2402test_op_contextualize()
2403 PREINIT:
2404 OP *o;
2405 CODE:
2406 o = newSVOP(OP_CONST, 0, newSViv(0));
2407 o->op_flags &= ~OPf_WANT;
2408 o = op_contextualize(o, G_SCALAR);
2409 if (o->op_type != OP_CONST ||
2410 (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR)
11f9f0ed 2411 croak_fail();
d9088386
Z
2412 op_free(o);
2413 o = newSVOP(OP_CONST, 0, newSViv(0));
2414 o->op_flags &= ~OPf_WANT;
2415 o = op_contextualize(o, G_ARRAY);
2416 if (o->op_type != OP_CONST ||
2417 (o->op_flags & OPf_WANT) != OPf_WANT_LIST)
11f9f0ed 2418 croak_fail();
d9088386
Z
2419 op_free(o);
2420 o = newSVOP(OP_CONST, 0, newSViv(0));
2421 o->op_flags &= ~OPf_WANT;
2422 o = op_contextualize(o, G_VOID);
11f9f0ed 2423 if (o->op_type != OP_NULL) croak_fail();
d9088386
Z
2424 op_free(o);
2425
2426void
2427test_rv2cv_op_cv()
2428 PROTOTYPE:
2429 PREINIT:
2430 GV *troc_gv, *wibble_gv;
2431 CV *troc_cv;
2432 OP *o;
2433 CODE:
2434 troc_gv = gv_fetchpv("XS::APItest::test_rv2cv_op_cv", 0, SVt_PVGV);
2435 troc_cv = get_cv("XS::APItest::test_rv2cv_op_cv", 0);
2436 wibble_gv = gv_fetchpv("XS::APItest::wibble", 0, SVt_PVGV);
2437 o = newCVREF(0, newGVOP(OP_GV, 0, troc_gv));
11f9f0ed 2438 if (rv2cv_op_cv(o, 0) != troc_cv) croak_fail();
d9088386 2439 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV) != (CV*)troc_gv)
11f9f0ed 2440 croak_fail();
d9088386 2441 o->op_private |= OPpENTERSUB_AMPER;
11f9f0ed
NC
2442 if (rv2cv_op_cv(o, 0)) croak_fail();
2443 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386 2444 o->op_private &= ~OPpENTERSUB_AMPER;
11f9f0ed
NC
2445 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
2446 if (rv2cv_op_cv(o, RV2CVOPCV_MARK_EARLY) != troc_cv) croak_fail();
2447 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
d9088386
Z
2448 op_free(o);
2449 o = newSVOP(OP_CONST, 0, newSVpv("XS::APItest::test_rv2cv_op_cv", 0));
2450 o->op_private = OPpCONST_BARE;
2451 o = newCVREF(0, o);
11f9f0ed 2452 if (rv2cv_op_cv(o, 0) != troc_cv) croak_fail();
d9088386 2453 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV) != (CV*)troc_gv)
11f9f0ed 2454 croak_fail();
d9088386 2455 o->op_private |= OPpENTERSUB_AMPER;
11f9f0ed
NC
2456 if (rv2cv_op_cv(o, 0)) croak_fail();
2457 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386
Z
2458 op_free(o);
2459 o = newCVREF(0, newSVOP(OP_CONST, 0, newRV_inc((SV*)troc_cv)));
11f9f0ed 2460 if (rv2cv_op_cv(o, 0) != troc_cv) croak_fail();
d9088386 2461 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV) != (CV*)troc_gv)
11f9f0ed 2462 croak_fail();
d9088386 2463 o->op_private |= OPpENTERSUB_AMPER;
11f9f0ed
NC
2464 if (rv2cv_op_cv(o, 0)) croak_fail();
2465 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386 2466 o->op_private &= ~OPpENTERSUB_AMPER;
11f9f0ed
NC
2467 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
2468 if (rv2cv_op_cv(o, RV2CVOPCV_MARK_EARLY) != troc_cv) croak_fail();
2469 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
d9088386
Z
2470 op_free(o);
2471 o = newCVREF(0, newUNOP(OP_RAND, 0, newSVOP(OP_CONST, 0, newSViv(0))));
11f9f0ed
NC
2472 if (rv2cv_op_cv(o, 0)) croak_fail();
2473 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386 2474 o->op_private |= OPpENTERSUB_AMPER;
11f9f0ed
NC
2475 if (rv2cv_op_cv(o, 0)) croak_fail();
2476 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386 2477 o->op_private &= ~OPpENTERSUB_AMPER;
11f9f0ed
NC
2478 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
2479 if (rv2cv_op_cv(o, RV2CVOPCV_MARK_EARLY)) croak_fail();
2480 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
d9088386
Z
2481 op_free(o);
2482 o = newUNOP(OP_RAND, 0, newSVOP(OP_CONST, 0, newSViv(0)));
11f9f0ed
NC
2483 if (rv2cv_op_cv(o, 0)) croak_fail();
2484 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386
Z
2485 op_free(o);
2486
2487void
2488test_cv_getset_call_checker()
2489 PREINIT:
2490 CV *troc_cv, *tsh_cv;
2491 Perl_call_checker ckfun;
2492 SV *ckobj;
2493 CODE:
2494#define check_cc(cv, xckfun, xckobj) \
2495 do { \
2496 cv_get_call_checker((cv), &ckfun, &ckobj); \
779bc08a
NC
2497 if (ckfun != (xckfun)) croak_fail_ne(FPTR2DPTR(void *, ckfun), xckfun); \
2498 if (ckobj != (xckobj)) croak_fail_ne(FPTR2DPTR(void *, ckobj), xckobj); \
d9088386
Z
2499 } while(0)
2500 troc_cv = get_cv("XS::APItest::test_rv2cv_op_cv", 0);
2501 tsh_cv = get_cv("XS::APItest::test_savehints", 0);
2502 check_cc(troc_cv, Perl_ck_entersub_args_proto_or_list, (SV*)troc_cv);
2503 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv);
2504 cv_set_call_checker(tsh_cv, Perl_ck_entersub_args_proto_or_list,
2505 &PL_sv_yes);
2506 check_cc(troc_cv, Perl_ck_entersub_args_proto_or_list, (SV*)troc_cv);
2507 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, &PL_sv_yes);
2508 cv_set_call_checker(troc_cv, THX_ck_entersub_args_scalars, &PL_sv_no);
2509 check_cc(troc_cv, THX_ck_entersub_args_scalars, &PL_sv_no);
2510 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, &PL_sv_yes);
2511 cv_set_call_checker(tsh_cv, Perl_ck_entersub_args_proto_or_list,
2512 (SV*)tsh_cv);
2513 check_cc(troc_cv, THX_ck_entersub_args_scalars, &PL_sv_no);
2514 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv);
2515 cv_set_call_checker(troc_cv, Perl_ck_entersub_args_proto_or_list,
2516 (SV*)troc_cv);
2517 check_cc(troc_cv, Perl_ck_entersub_args_proto_or_list, (SV*)troc_cv);
2518 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv);
11f9f0ed
NC
2519 if (SvMAGICAL((SV*)troc_cv) || SvMAGIC((SV*)troc_cv)) croak_fail();
2520 if (SvMAGICAL((SV*)tsh_cv) || SvMAGIC((SV*)tsh_cv)) croak_fail();
d9088386
Z
2521#undef check_cc
2522
2523void
2524cv_set_call_checker_lists(CV *cv)
2525 CODE:
2526 cv_set_call_checker(cv, THX_ck_entersub_args_lists, &PL_sv_undef);
2527
2528void
2529cv_set_call_checker_scalars(CV *cv)
2530 CODE:
2531 cv_set_call_checker(cv, THX_ck_entersub_args_scalars, &PL_sv_undef);
2532
2533void
2534cv_set_call_checker_proto(CV *cv, SV *proto)
2535 CODE:
2536 if (SvROK(proto))
2537 proto = SvRV(proto);
2538 cv_set_call_checker(cv, Perl_ck_entersub_args_proto, proto);
2539
2540void
2541cv_set_call_checker_proto_or_list(CV *cv, SV *proto)
2542 CODE:
2543 if (SvROK(proto))
2544 proto = SvRV(proto);
2545 cv_set_call_checker(cv, Perl_ck_entersub_args_proto_or_list, proto);
2546
2547void
2548cv_set_call_checker_multi_sum(CV *cv)
2549 CODE:
2550 cv_set_call_checker(cv, THX_ck_entersub_multi_sum, &PL_sv_undef);
2551
2552void
20439bc7
Z
2553test_cophh()
2554 PREINIT:
2555 COPHH *a, *b;
2556 CODE:
2557#define check_ph(EXPR) \
2558 do { if((EXPR) != &PL_sv_placeholder) croak("fail"); } while(0)
2559#define check_iv(EXPR, EXPECT) \
2560 do { if(SvIV(EXPR) != (EXPECT)) croak("fail"); } while(0)
2561#define msvpvs(STR) sv_2mortal(newSVpvs(STR))
2562#define msviv(VALUE) sv_2mortal(newSViv(VALUE))
2563 a = cophh_new_empty();
2564 check_ph(cophh_fetch_pvn(a, "foo_1", 5, 0, 0));
2565 check_ph(cophh_fetch_pvs(a, "foo_1", 0));
2566 check_ph(cophh_fetch_pv(a, "foo_1", 0, 0));
2567 check_ph(cophh_fetch_sv(a, msvpvs("foo_1"), 0, 0));
2568 a = cophh_store_pvn(a, "foo_1abc", 5, 0, msviv(111), 0);
2569 a = cophh_store_pvs(a, "foo_2", msviv(222), 0);
2570 a = cophh_store_pv(a, "foo_3", 0, msviv(333), 0);
2571 a = cophh_store_sv(a, msvpvs("foo_4"), 0, msviv(444), 0);
2572 check_iv(cophh_fetch_pvn(a, "foo_1xyz", 5, 0, 0), 111);
2573 check_iv(cophh_fetch_pvs(a, "foo_1", 0), 111);
2574 check_iv(cophh_fetch_pv(a, "foo_1", 0, 0), 111);
2575 check_iv(cophh_fetch_sv(a, msvpvs("foo_1"), 0, 0), 111);
2576 check_iv(cophh_fetch_pvs(a, "foo_2", 0), 222);
2577 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
2578 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
2579 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
2580 b = cophh_copy(a);
2581 b = cophh_store_pvs(b, "foo_1", msviv(1111), 0);
2582 check_iv(cophh_fetch_pvs(a, "foo_1", 0), 111);
2583 check_iv(cophh_fetch_pvs(a, "foo_2", 0), 222);
2584 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
2585 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
2586 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
2587 check_iv(cophh_fetch_pvs(b, "foo_1", 0), 1111);
2588 check_iv(cophh_fetch_pvs(b, "foo_2", 0), 222);
2589 check_iv(cophh_fetch_pvs(b, "foo_3", 0), 333);
2590 check_iv(cophh_fetch_pvs(b, "foo_4", 0), 444);
2591 check_ph(cophh_fetch_pvs(b, "foo_5", 0));
2592 a = cophh_delete_pvn(a, "foo_1abc", 5, 0, 0);
2593 a = cophh_delete_pvs(a, "foo_2", 0);
2594 b = cophh_delete_pv(b, "foo_3", 0, 0);
2595 b = cophh_delete_sv(b, msvpvs("foo_4"), 0, 0);
2596 check_ph(cophh_fetch_pvs(a, "foo_1", 0));
2597 check_ph(cophh_fetch_pvs(a, "foo_2", 0));
2598 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
2599 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
2600 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
2601 check_iv(cophh_fetch_pvs(b, "foo_1", 0), 1111);
2602 check_iv(cophh_fetch_pvs(b, "foo_2", 0), 222);
2603 check_ph(cophh_fetch_pvs(b, "foo_3", 0));
2604 check_ph(cophh_fetch_pvs(b, "foo_4", 0));
2605 check_ph(cophh_fetch_pvs(b, "foo_5", 0));
2606 b = cophh_delete_pvs(b, "foo_3", 0);
2607 b = cophh_delete_pvs(b, "foo_5", 0);
2608 check_iv(cophh_fetch_pvs(b, "foo_1", 0), 1111);
2609 check_iv(cophh_fetch_pvs(b, "foo_2", 0), 222);
2610 check_ph(cophh_fetch_pvs(b, "foo_3", 0));
2611 check_ph(cophh_fetch_pvs(b, "foo_4", 0));
2612 check_ph(cophh_fetch_pvs(b, "foo_5", 0));
2613 cophh_free(b);
2614 check_ph(cophh_fetch_pvs(a, "foo_1", 0));
2615 check_ph(cophh_fetch_pvs(a, "foo_2", 0));
2616 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
2617 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
2618 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
2619 a = cophh_store_pvs(a, "foo_1", msviv(11111), COPHH_KEY_UTF8);
2620 a = cophh_store_pvs(a, "foo_\xaa", msviv(123), 0);
2621 a = cophh_store_pvs(a, "foo_\xc2\xbb", msviv(456), COPHH_KEY_UTF8);
2622 a = cophh_store_pvs(a, "foo_\xc3\x8c", msviv(789), COPHH_KEY_UTF8);
2623 a = cophh_store_pvs(a, "foo_\xd9\xa6", msviv(666), COPHH_KEY_UTF8);
2624 check_iv(cophh_fetch_pvs(a, "foo_1", 0), 11111);
2625 check_iv(cophh_fetch_pvs(a, "foo_1", COPHH_KEY_UTF8), 11111);
2626 check_iv(cophh_fetch_pvs(a, "foo_\xaa", 0), 123);
2627 check_iv(cophh_fetch_pvs(a, "foo_\xc2\xaa", COPHH_KEY_UTF8), 123);
2628 check_ph(cophh_fetch_pvs(a, "foo_\xc2\xaa", 0));
2629 check_iv(cophh_fetch_pvs(a, "foo_\xbb", 0), 456);
2630 check_iv(cophh_fetch_pvs(a, "foo_\xc2\xbb", COPHH_KEY_UTF8), 456);
2631 check_ph(cophh_fetch_pvs(a, "foo_\xc2\xbb", 0));
2632 check_iv(cophh_fetch_pvs(a, "foo_\xcc", 0), 789);
2633 check_iv(cophh_fetch_pvs(a, "foo_\xc3\x8c", COPHH_KEY_UTF8), 789);
2634 check_ph(cophh_fetch_pvs(a, "foo_\xc2\x8c", 0));
2635 check_iv(cophh_fetch_pvs(a, "foo_\xd9\xa6", COPHH_KEY_UTF8), 666);
2636 check_ph(cophh_fetch_pvs(a, "foo_\xd9\xa6", 0));
3987a177
Z
2637 ENTER;
2638 SAVEFREECOPHH(a);
2639 LEAVE;
20439bc7
Z
2640#undef check_ph
2641#undef check_iv
2642#undef msvpvs
2643#undef msviv
2644
8375c93e
RU
2645void
2646test_coplabel()
2647 PREINIT:
2648 COP *cop;
32708f0b
CB
2649 const char *label;
2650 STRLEN len;
2651 U32 utf8;
8375c93e
RU
2652 CODE:
2653 cop = &PL_compiling;
32708f0b
CB
2654 Perl_cop_store_label(aTHX_ cop, "foo", 3, 0);
2655 label = Perl_cop_fetch_label(aTHX_ cop, &len, &utf8);
2656 if (strcmp(label,"foo")) croak("fail # cop_fetch_label label");
2657 if (len != 3) croak("fail # cop_fetch_label len");
2658 if (utf8) croak("fail # cop_fetch_label utf8");
8375c93e 2659 /* SMALL GERMAN UMLAUT A */
a1cd495a 2660 Perl_cop_store_label(aTHX_ cop, "fo\xc3\xa4", 4, SVf_UTF8);
32708f0b 2661 label = Perl_cop_fetch_label(aTHX_ cop, &len, &utf8);
a1cd495a 2662 if (strcmp(label,"fo\xc3\xa4")) croak("fail # cop_fetch_label label");
72024711 2663 if (len != 4) croak("fail # cop_fetch_label len");
32708f0b 2664 if (!utf8) croak("fail # cop_fetch_label utf8");
8375c93e
RU
2665
2666
20439bc7
Z
2667HV *
2668example_cophh_2hv()
2669 PREINIT:
2670 COPHH *a;
2671 CODE:
2672#define msviv(VALUE) sv_2mortal(newSViv(VALUE))
2673 a = cophh_new_empty();
2674 a = cophh_store_pvs(a, "foo_0", msviv(999), 0);
2675 a = cophh_store_pvs(a, "foo_1", msviv(111), 0);
2676 a = cophh_store_pvs(a, "foo_\xaa", msviv(123), 0);
2677 a = cophh_store_pvs(a, "foo_\xc2\xbb", msviv(456), COPHH_KEY_UTF8);
2678 a = cophh_store_pvs(a, "foo_\xc3\x8c", msviv(789), COPHH_KEY_UTF8);
2679 a = cophh_store_pvs(a, "foo_\xd9\xa6", msviv(666), COPHH_KEY_UTF8);
2680 a = cophh_delete_pvs(a, "foo_0", 0);
2681 a = cophh_delete_pvs(a, "foo_2", 0);
2682 RETVAL = cophh_2hv(a, 0);
2683 cophh_free(a);
2684#undef msviv
2685 OUTPUT:
2686 RETVAL
2687
2688void
defdfed5
Z
2689test_savehints()
2690 PREINIT:
2691 SV **svp, *sv;
2692 CODE:
2693#define store_hint(KEY, VALUE) \
2694 sv_setiv_mg(*hv_fetchs(GvHV(PL_hintgv), KEY, 1), (VALUE))
2695#define hint_ok(KEY, EXPECT) \
2696 ((svp = hv_fetchs(GvHV(PL_hintgv), KEY, 0)) && \
2697 (sv = *svp) && SvIV(sv) == (EXPECT) && \
20439bc7 2698 (sv = cop_hints_fetch_pvs(&PL_compiling, KEY, 0)) && \
defdfed5
Z
2699 SvIV(sv) == (EXPECT))
2700#define check_hint(KEY, EXPECT) \
11f9f0ed 2701 do { if (!hint_ok(KEY, EXPECT)) croak_fail(); } while(0)
defdfed5
Z
2702 PL_hints |= HINT_LOCALIZE_HH;
2703 ENTER;
2704 SAVEHINTS();
2705 PL_hints &= HINT_INTEGER;
2706 store_hint("t0", 123);
2707 store_hint("t1", 456);
11f9f0ed 2708 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2709 check_hint("t0", 123); check_hint("t1", 456);
2710 ENTER;
2711 SAVEHINTS();
11f9f0ed 2712 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2713 check_hint("t0", 123); check_hint("t1", 456);
2714 PL_hints |= HINT_INTEGER;
2715 store_hint("t0", 321);
11f9f0ed 2716 if (!(PL_hints & HINT_INTEGER)) croak_fail();
defdfed5
Z
2717 check_hint("t0", 321); check_hint("t1", 456);
2718 LEAVE;
11f9f0ed 2719 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2720 check_hint("t0", 123); check_hint("t1", 456);
2721 ENTER;
2722 SAVEHINTS();
11f9f0ed 2723 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2724 check_hint("t0", 123); check_hint("t1", 456);
2725 store_hint("t1", 654);
11f9f0ed 2726 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2727 check_hint("t0", 123); check_hint("t1", 654);
2728 LEAVE;
11f9f0ed 2729 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2730 check_hint("t0", 123); check_hint("t1", 456);
2731 LEAVE;
2732#undef store_hint
2733#undef hint_ok
2734#undef check_hint
2735
2736void
2737test_copyhints()
2738 PREINIT:
2739 HV *a, *b;
2740 CODE:
2741 PL_hints |= HINT_LOCALIZE_HH;
2742 ENTER;
2743 SAVEHINTS();
2744 sv_setiv_mg(*hv_fetchs(GvHV(PL_hintgv), "t0", 1), 123);
20439bc7
Z
2745 if (SvIV(cop_hints_fetch_pvs(&PL_compiling, "t0", 0)) != 123)
2746 croak_fail();
defdfed5
Z
2747 a = newHVhv(GvHV(PL_hintgv));
2748 sv_2mortal((SV*)a);
2749 sv_setiv_mg(*hv_fetchs(a, "t0", 1), 456);
20439bc7
Z
2750 if (SvIV(cop_hints_fetch_pvs(&PL_compiling, "t0", 0)) != 123)
2751 croak_fail();
defdfed5
Z
2752 b = hv_copy_hints_hv(a);
2753 sv_2mortal((SV*)b);
2754 sv_setiv_mg(*hv_fetchs(b, "t0", 1), 789);
20439bc7
Z
2755 if (SvIV(cop_hints_fetch_pvs(&PL_compiling, "t0", 0)) != 789)
2756 croak_fail();
defdfed5
Z
2757 LEAVE;
2758
201c7e1f 2759void
2fcb4757
Z
2760test_op_list()
2761 PREINIT:
2762 OP *a;
2763 CODE:
2764#define iv_op(iv) newSVOP(OP_CONST, 0, newSViv(iv))
2765#define check_op(o, expect) \
2766 do { \
2767 if (strcmp(test_op_list_describe(o), (expect))) \
2768 croak("fail %s %s", test_op_list_describe(o), (expect)); \
2769 } while(0)
2770 a = op_append_elem(OP_LIST, NULL, NULL);
2771 check_op(a, "");
2772 a = op_append_elem(OP_LIST, iv_op(1), a);
2773 check_op(a, "const(1).");
2774 a = op_append_elem(OP_LIST, NULL, a);
2775 check_op(a, "const(1).");
2776 a = op_append_elem(OP_LIST, a, iv_op(2));
2777 check_op(a, "list[pushmark.const(1).const(2).]");
2778 a = op_append_elem(OP_LIST, a, iv_op(3));
2779 check_op(a, "list[pushmark.const(1).const(2).const(3).]");
2780 a = op_append_elem(OP_LIST, a, NULL);
2781 check_op(a, "list[pushmark.const(1).const(2).const(3).]");
2782 a = op_append_elem(OP_LIST, NULL, a);
2783 check_op(a, "list[pushmark.const(1).const(2).const(3).]");
2784 a = op_append_elem(OP_LIST, iv_op(4), a);
2785 check_op(a, "list[pushmark.const(4)."
2786 "list[pushmark.const(1).const(2).const(3).]]");
2787 a = op_append_elem(OP_LIST, a, iv_op(5));
2788 check_op(a, "list[pushmark.const(4)."
2789 "list[pushmark.const(1).const(2).const(3).]const(5).]");
2790 a = op_append_elem(OP_LIST, a,
2791 op_append_elem(OP_LIST, iv_op(7), iv_op(6)));
2792 check_op(a, "list[pushmark.const(4)."
2793 "list[pushmark.const(1).const(2).const(3).]const(5)."
2794 "list[pushmark.const(7).const(6).]]");
2795 op_free(a);
2796 a = op_append_elem(OP_LINESEQ, iv_op(1), iv_op(2));
2797 check_op(a, "lineseq[const(1).const(2).]");
2798 a = op_append_elem(OP_LINESEQ, a, iv_op(3));
2799 check_op(a, "lineseq[const(1).const(2).const(3).]");
2800 op_free(a);
2801 a = op_append_elem(OP_LINESEQ,
2802 op_append_elem(OP_LIST, iv_op(1), iv_op(2)),
2803 iv_op(3));
2804 check_op(a, "lineseq[list[pushmark.const(1).const(2).]const(3).]");
2805 op_free(a);
2806 a = op_prepend_elem(OP_LIST, NULL, NULL);
2807 check_op(a, "");
2808 a = op_prepend_elem(OP_LIST, a, iv_op(1));
2809 check_op(a, "const(1).");
2810 a = op_prepend_elem(OP_LIST, a, NULL);
2811 check_op(a, "const(1).");
2812 a = op_prepend_elem(OP_LIST, iv_op(2), a);
2813 check_op(a, "list[pushmark.const(2).const(1).]");
2814 a = op_prepend_elem(OP_LIST, iv_op(3), a);
2815 check_op(a, "list[pushmark.const(3).const(2).const(1).]");
2816 a = op_prepend_elem(OP_LIST, NULL, a);
2817 check_op(a, "list[pushmark.const(3).const(2).const(1).]");
2818 a = op_prepend_elem(OP_LIST, a, NULL);
2819 check_op(a, "list[pushmark.const(3).const(2).const(1).]");
2820 a = op_prepend_elem(OP_LIST, a, iv_op(4));
2821 check_op(a, "list[pushmark."
2822 "list[pushmark.const(3).const(2).const(1).]const(4).]");
2823 a = op_prepend_elem(OP_LIST, iv_op(5), a);
2824 check_op(a, "list[pushmark.const(5)."
2825 "list[pushmark.const(3).const(2).const(1).]const(4).]");
2826 a = op_prepend_elem(OP_LIST,
2827 op_prepend_elem(OP_LIST, iv_op(6), iv_op(7)), a);
2828 check_op(a, "list[pushmark.list[pushmark.const(6).const(7).]const(5)."
2829 "list[pushmark.const(3).const(2).const(1).]const(4).]");
2830 op_free(a);
2831 a = op_prepend_elem(OP_LINESEQ, iv_op(2), iv_op(1));
2832 check_op(a, "lineseq[const(2).const(1).]");
2833 a = op_prepend_elem(OP_LINESEQ, iv_op(3), a);
2834 check_op(a, "lineseq[const(3).const(2).const(1).]");
2835 op_free(a);
2836 a = op_prepend_elem(OP_LINESEQ, iv_op(3),
2837 op_prepend_elem(OP_LIST, iv_op(2), iv_op(1)));
2838 check_op(a, "lineseq[const(3).list[pushmark.const(2).const(1).]]");
2839 op_free(a);
2840 a = op_append_list(OP_LINESEQ, NULL, NULL);
2841 check_op(a, "");
2842 a = op_append_list(OP_LINESEQ, iv_op(1), a);
2843 check_op(a, "const(1).");
2844 a = op_append_list(OP_LINESEQ, NULL, a);
2845 check_op(a, "const(1).");
2846 a = op_append_list(OP_LINESEQ, a, iv_op(2));
2847 check_op(a, "lineseq[const(1).const(2).]");
2848 a = op_append_list(OP_LINESEQ, a, iv_op(3));
2849 check_op(a, "lineseq[const(1).const(2).const(3).]");
2850 a = op_append_list(OP_LINESEQ, iv_op(4), a);
2851 check_op(a, "lineseq[const(4).const(1).const(2).const(3).]");
2852 a = op_append_list(OP_LINESEQ, a, NULL);
2853 check_op(a, "lineseq[const(4).const(1).const(2).const(3).]");
2854 a = op_append_list(OP_LINESEQ, NULL, a);
2855 check_op(a, "lineseq[const(4).const(1).const(2).const(3).]");
2856 a = op_append_list(OP_LINESEQ, a,
2857 op_append_list(OP_LINESEQ, iv_op(5), iv_op(6)));
2858 check_op(a, "lineseq[const(4).const(1).const(2).const(3)."
2859 "const(5).const(6).]");
2860 op_free(a);
2861 a = op_append_list(OP_LINESEQ,
2862 op_append_list(OP_LINESEQ, iv_op(1), iv_op(2)),
2863 op_append_list(OP_LIST, iv_op(3), iv_op(4)));
2864 check_op(a, "lineseq[const(1).const(2)."
2865 "list[pushmark.const(3).const(4).]]");
2866 op_free(a);
2867 a = op_append_list(OP_LINESEQ,
2868 op_append_list(OP_LIST, iv_op(1), iv_op(2)),
2869 op_append_list(OP_LINESEQ, iv_op(3), iv_op(4)));
2870 check_op(a, "lineseq[list[pushmark.const(1).const(2).]"
2871 "const(3).const(4).]");
2872 op_free(a);
2fcb4757
Z
2873#undef check_op
2874
2875void
5983a79d
BM
2876test_op_linklist ()
2877 PREINIT:
2878 OP *o;
2879 CODE:
2880#define check_ll(o, expect) \
2881 STMT_START { \
2882 if (strNE(test_op_linklist_describe(o), (expect))) \
2883 croak("fail %s %s", test_op_linklist_describe(o), (expect)); \
2884 } STMT_END
2885 o = iv_op(1);
2886 check_ll(o, ".const1");
2887 op_free(o);
2888
2889 o = mkUNOP(OP_NOT, iv_op(1));
2890 check_ll(o, ".const1.not");
2891 op_free(o);
2892
2893 o = mkUNOP(OP_NOT, mkUNOP(OP_NEGATE, iv_op(1)));
2894 check_ll(o, ".const1.negate.not");
2895 op_free(o);
2896
2897 o = mkBINOP(OP_ADD, iv_op(1), iv_op(2));
2898 check_ll(o, ".const1.const2.add");
2899 op_free(o);
2900
2901 o = mkBINOP(OP_ADD, mkUNOP(OP_NOT, iv_op(1)), iv_op(2));
2902 check_ll(o, ".const1.not.const2.add");
2903 op_free(o);
2904
2905 o = mkUNOP(OP_NOT, mkBINOP(OP_ADD, iv_op(1), iv_op(2)));
2906 check_ll(o, ".const1.const2.add.not");
2907 op_free(o);
2908
2909 o = mkLISTOP(OP_LINESEQ, iv_op(1), iv_op(2), iv_op(3));
2910 check_ll(o, ".const1.const2.const3.lineseq");
2911 op_free(o);
2912
2913 o = mkLISTOP(OP_LINESEQ,
2914 mkBINOP(OP_ADD, iv_op(1), iv_op(2)),
2915 mkUNOP(OP_NOT, iv_op(3)),
2916 mkLISTOP(OP_SUBSTR, iv_op(4), iv_op(5), iv_op(6)));
2917 check_ll(o, ".const1.const2.add.const3.not"
2918 ".const4.const5.const6.substr.lineseq");
2919 op_free(o);
2920
2921 o = mkBINOP(OP_ADD, iv_op(1), iv_op(2));
2922 LINKLIST(o);
2923 o = mkBINOP(OP_SUBTRACT, o, iv_op(3));
2924 check_ll(o, ".const1.const2.add.const3.subtract");
2925 op_free(o);
2926#undef check_ll
2927#undef iv_op
2928
2929void
201c7e1f
FR
2930peep_enable ()
2931 PREINIT:
2932 dMY_CXT;
2933 CODE:
2934 av_clear(MY_CXT.peep_recorder);
2935 av_clear(MY_CXT.rpeep_recorder);
2936 MY_CXT.peep_recording = 1;
2937
2938void
2939peep_disable ()
2940 PREINIT:
2941 dMY_CXT;
2942 CODE:
2943 MY_CXT.peep_recording = 0;
2944
2945SV *
2946peep_record ()
2947 PREINIT:
2948 dMY_CXT;
2949 CODE:
95d2461a 2950 RETVAL = newRV_inc((SV *)MY_CXT.peep_recorder);
201c7e1f
FR
2951 OUTPUT:
2952 RETVAL
2953
2954SV *
2955rpeep_record ()
2956 PREINIT:
2957 dMY_CXT;
2958 CODE:
95d2461a 2959 RETVAL = newRV_inc((SV *)MY_CXT.rpeep_recorder);
201c7e1f
FR
2960 OUTPUT:
2961 RETVAL
2962
9c540340
DM
2963=pod
2964
2965multicall_each: call a sub for each item in the list. Used to test MULTICALL
2966
2967=cut
2968
2969void
2970multicall_each(block,...)
2971 SV * block
2972PROTOTYPE: &@
2973CODE:
2974{
2975 dMULTICALL;
2976 int index;
2977 GV *gv;
2978 HV *stash;
2979 I32 gimme = G_SCALAR;
2980 SV **args = &PL_stack_base[ax];
2981 CV *cv;
2982
2983 if(items <= 1) {
2984 XSRETURN_UNDEF;
2985 }
2986 cv = sv_2cv(block, &stash, &gv, 0);
2987 if (cv == Nullcv) {
2988 croak("multicall_each: not a subroutine reference");
2989 }
2990 PUSH_MULTICALL(cv);
2991 SAVESPTR(GvSV(PL_defgv));
2992
2993 for(index = 1 ; index < items ; index++) {
2994 GvSV(PL_defgv) = args[index];
2995 MULTICALL;
2996 }
2997 POP_MULTICALL;
2998 XSRETURN_UNDEF;
2999}
3000
7b81e549
GG
3001#ifdef USE_ITHREADS
3002
3003void
3004clone_with_stack()
3005CODE:
3006{
3007 PerlInterpreter *interp = aTHX; /* The original interpreter */
3008 PerlInterpreter *interp_dup; /* The duplicate interpreter */
3009 int oldscope = 1; /* We are responsible for all scopes */
3010
3011 interp_dup = perl_clone(interp, CLONEf_COPY_STACKS | CLONEf_CLONE_HOST );
3012
3013 /* destroy old perl */
3014 PERL_SET_CONTEXT(interp);
3015
3016 POPSTACK_TO(PL_mainstack);
3017 dounwind(-1);
3018 LEAVE_SCOPE(0);
3019
3020 while (interp->Iscopestack_ix > 1)
3021 LEAVE;
3022 FREETMPS;
3023
3024 perl_destruct(interp);
3025 perl_free(interp);
3026
3027 /* switch to new perl */
3028 PERL_SET_CONTEXT(interp_dup);
3029
3030 /* continue after 'clone_with_stack' */
3031 interp_dup->Iop = interp_dup->Iop->op_next;
3032
3033 /* run with new perl */
3034 Perl_runops_standard(interp_dup);
3035
3036 /* We may have additional unclosed scopes if fork() was called
3037 * from within a BEGIN block. See perlfork.pod for more details.
3038 * We cannot clean up these other scopes because they belong to a
3039 * different interpreter, but we also cannot leave PL_scopestack_ix
3040 * dangling because that can trigger an assertion in perl_destruct().
3041 */
3042 if (PL_scopestack_ix > oldscope) {
3043 PL_scopestack[oldscope-1] = PL_scopestack[PL_scopestack_ix-1];
3044 PL_scopestack_ix = oldscope;
3045 }
3046
3047 perl_destruct(interp_dup);
3048 perl_free(interp_dup);
3049
3050 /* call the real 'exit' not PerlProc_exit */
3051#undef exit
3052 exit(0);
3053}
3054
3055#endif /* USE_ITHREDS */
9c540340 3056
88b5a879
GF
3057SV*
3058take_svref(SVREF sv)
3059CODE:
3060 RETVAL = newRV_inc(sv);
3061OUTPUT:
3062 RETVAL
3063
3064SV*
3065take_avref(AV* av)
3066CODE:
3067 RETVAL = newRV_inc((SV*)av);
3068OUTPUT:
3069 RETVAL
3070
3071SV*
3072take_hvref(HV* hv)
3073CODE:
3074 RETVAL = newRV_inc((SV*)hv);
3075OUTPUT:
3076 RETVAL
3077
3078
3079SV*
3080take_cvref(CV* cv)
3081CODE:
3082 RETVAL = newRV_inc((SV*)cv);
3083OUTPUT:
3084 RETVAL
3085
3086
e2fe06dd
EB
3087BOOT:
3088 {
3089 HV* stash;
3090 SV** meth = NULL;
3091 CV* cv;
3092 stash = gv_stashpv("XS::APItest::TempLv", 0);
3093 if (stash)
3094 meth = hv_fetchs(stash, "make_temp_mg_lv", 0);
3095 if (!meth)
3096 croak("lost method 'make_temp_mg_lv'");
3097 cv = GvCV(*meth);
3098 CvLVALUE_on(cv);
3099 }
83f8bb56
Z
3100
3101BOOT:
3102{
3103 hintkey_rpn_sv = newSVpvs_share("XS::APItest/rpn");
3104 hintkey_calcrpn_sv = newSVpvs_share("XS::APItest/calcrpn");
3105 hintkey_stufftest_sv = newSVpvs_share("XS::APItest/stufftest");
3106 hintkey_swaptwostmts_sv = newSVpvs_share("XS::APItest/swaptwostmts");
07ffcb73 3107 hintkey_looprest_sv = newSVpvs_share("XS::APItest/looprest");
a7aaec61 3108 hintkey_scopelessblock_sv = newSVpvs_share("XS::APItest/scopelessblock");
9eb5c532
Z
3109 hintkey_stmtasexpr_sv = newSVpvs_share("XS::APItest/stmtasexpr");
3110 hintkey_stmtsasexpr_sv = newSVpvs_share("XS::APItest/stmtsasexpr");
e53d8f76
Z
3111 hintkey_loopblock_sv = newSVpvs_share("XS::APItest/loopblock");
3112 hintkey_blockasexpr_sv = newSVpvs_share("XS::APItest/blockasexpr");
361d9b55
Z
3113 hintkey_swaplabel_sv = newSVpvs_share("XS::APItest/swaplabel");
3114 hintkey_labelconst_sv = newSVpvs_share("XS::APItest/labelconst");
78cdf107
Z
3115 hintkey_arrayfullexpr_sv = newSVpvs_share("XS::APItest/arrayfullexpr");
3116 hintkey_arraylistexpr_sv = newSVpvs_share("XS::APItest/arraylistexpr");
3117 hintkey_arraytermexpr_sv = newSVpvs_share("XS::APItest/arraytermexpr");
3118 hintkey_arrayarithexpr_sv = newSVpvs_share("XS::APItest/arrayarithexpr");
3119 hintkey_arrayexprflags_sv = newSVpvs_share("XS::APItest/arrayexprflags");
83f8bb56
Z
3120 next_keyword_plugin = PL_keyword_plugin;
3121 PL_keyword_plugin = my_keyword_plugin;
3122}
8f89e5a9
Z
3123
3124void
3125establish_cleanup(...)
3126PROTOTYPE: $
3127CODE:
c33e8be1 3128 PERL_UNUSED_VAR(items);
8f89e5a9
Z
3129 croak("establish_cleanup called as a function");
3130
3131BOOT:
3132{
3133 CV *estcv = get_cv("XS::APItest::establish_cleanup", 0);
3134 cv_set_call_checker(estcv, THX_ck_entersub_establish_cleanup, (SV*)estcv);
3135}
3ad73efd
Z
3136
3137void
3138postinc(...)
3139PROTOTYPE: $
3140CODE:
c33e8be1 3141 PERL_UNUSED_VAR(items);
3ad73efd
Z
3142 croak("postinc called as a function");
3143
27fcb6ee
FC
3144void
3145filter()
3146CODE:
3147 filter_add(filter_call, NULL);
3148
3ad73efd
Z
3149BOOT:
3150{
3151 CV *asscv = get_cv("XS::APItest::postinc", 0);
3152 cv_set_call_checker(asscv, THX_ck_entersub_postinc, (SV*)asscv);
3153}
26ab20ee 3154
fba8e77b
FC
3155SV *
3156lv_temp_object()
3157CODE:
3158 RETVAL =
3159 sv_bless(
3160 newRV_noinc(newSV(0)),
3161 gv_stashpvs("XS::APItest::TempObj",GV_ADD)
3162 ); /* Package defined in test script */
3163OUTPUT:
3164 RETVAL
3165
7d6175ef 3166void
65b22876 3167fill_hash_with_nulls(HV *hv)
a2bf5ab1 3168PREINIT:
7d6175ef 3169 UV i = 0;
a2bf5ab1 3170CODE:
7d6175ef
FC
3171 for(; i < 1000; ++i) {
3172 HE *entry = hv_fetch_ent(hv, sv_2mortal(newSVuv(i)), 1, 0);
3173 SvREFCNT_dec(HeVAL(entry));
3174 HeVAL(entry) = NULL;
3175 }
3176
3f4d1d78
FC
3177HV *
3178newHVhv(HV *hv)
3179CODE:
3180 RETVAL = newHVhv(hv);
3181OUTPUT:
3182 RETVAL
3183
fa819c1c
FC
3184bool
3185SvIsCOW(SV *sv)
3186CODE:
3187 RETVAL = SvIsCOW(sv);
3188OUTPUT:
3189 RETVAL
3190
15103811
Z
3191void
3192pad_scalar(...)
3193PROTOTYPE: $$
3194CODE:
3195 PERL_UNUSED_VAR(items);
3196 croak("pad_scalar called as a function");
3197
3198BOOT:
3199{
3200 CV *pscv = get_cv("XS::APItest::pad_scalar", 0);
3201 cv_set_call_checker(pscv, THX_ck_entersub_pad_scalar, (SV*)pscv);
3202}
3203
ab8f91e9
BF
3204SV*
3205fetch_pad_names( cv )
3206CV* cv
3207 PREINIT:
3208 I32 i;
3209 AV *pad_namelist;
3210 AV *retav = newAV();
3211 CODE:
3212 pad_namelist = (AV*) *av_fetch(CvPADLIST(cv), 0, FALSE);
3213
3214 for ( i = av_len(pad_namelist); i >= 0; i-- ) {
3215 SV** name_ptr = av_fetch(pad_namelist, i, 0);
3216
3217 if (name_ptr && SvPOKp(*name_ptr)) {
3218 av_push(retav, newSVsv(*name_ptr));
3219 }
3220 }
3221 RETVAL = newRV_noinc((SV*)retav);
3222 OUTPUT:
3223 RETVAL
3224
c12735bb
Z
3225STRLEN
3226underscore_length()
3227PROTOTYPE:
3228PREINIT:
3229 SV *u;
3230 U8 *pv;
3231 STRLEN bytelen;
3232CODE:
3233 u = find_rundefsv();
3234 pv = (U8*)SvPV(u, bytelen);
3235 RETVAL = SvUTF8(u) ? utf8_length(pv, pv+bytelen) : bytelen;
3236OUTPUT:
3237 RETVAL
3238
e8ed61c5
FC
3239void
3240stringify(SV *sv)
3241PREINIT:
3242 const char *pv;
3243CODE:
3244 pv = SvPV_nolen(sv);
3245
c682ebef
FC
3246SV *
3247HvENAME(HV *hv)
3248CODE:
3249 RETVAL = hv && HvENAME(hv)
3250 ? newSVpvn_flags(
3251 HvENAME(hv),HvENAMELEN(hv),
3252 (HvENAMEUTF8(hv) ? SVf_UTF8 : 0)
3253 )
3254 : NULL;
3255OUTPUT:
3256 RETVAL
3257
2fc49ef1
FC
3258int
3259xs_cmp(int a, int b)
3260CODE:
3261 /* Odd sorting (odd numbers first), to make sure we are actually
3262 being called */
3263 RETVAL = a % 2 != b % 2
3264 ? a % 2 ? -1 : 1
3265 : a < b ? -1 : a == b ? 0 : 1;
3266OUTPUT:
3267 RETVAL
3268
c19fd8b4
FC
3269SV *
3270xs_cmp_undef(SV *a, SV *b)
3271CODE:
3272 RETVAL = &PL_sv_undef;
3273OUTPUT:
3274 RETVAL
3275
fe46cbda
FC
3276char *
3277SvPVbyte(SV *sv)
3278CODE:
3279 RETVAL = SvPVbyte_nolen(sv);
3280OUTPUT:
3281 RETVAL
3282
3283char *
3284SvPVutf8(SV *sv)
3285CODE:
3286 RETVAL = SvPVutf8_nolen(sv);
3287OUTPUT:
3288 RETVAL
3289
e8ed61c5 3290
120b7a08
S
3291MODULE = XS::APItest PACKAGE = XS::APItest::AUTOLOADtest
3292
3293int
3294AUTOLOAD(...)
3295 INIT:
3296 SV* comms;
120b7a08
S
3297 SV* class_and_method;
3298 SV* tmp;
3299 CODE:
bb619f37 3300 class_and_method = GvSV(CvGV(cv));
120b7a08
S
3301 comms = get_sv("main::the_method", 1);
3302 if (class_and_method == NULL) {
3303 RETVAL = 1;
3304 } else if (!SvOK(class_and_method)) {
3305 RETVAL = 2;
3306 } else if (!SvPOK(class_and_method)) {
3307 RETVAL = 3;
3308 } else {
bb619f37 3309 sv_setsv(comms, class_and_method);
120b7a08
S
3310 RETVAL = 0;
3311 }
3312 OUTPUT: RETVAL
3313
3314
26ab20ee
FR
3315MODULE = XS::APItest PACKAGE = XS::APItest::Magic
3316
3317PROTOTYPES: DISABLE
3318
3319void
3320sv_magic_foo(SV *sv, SV *thingy)
3321ALIAS:
3322 sv_magic_bar = 1
3323CODE:
3324 sv_magicext(SvRV(sv), NULL, PERL_MAGIC_ext, ix ? &vtbl_bar : &vtbl_foo, (const char *)thingy, 0);
3325
3326SV *
3327mg_find_foo(SV *sv)
3328ALIAS:
3329 mg_find_bar = 1
3330CODE:
3331 MAGIC *mg = mg_findext(SvRV(sv), PERL_MAGIC_ext, ix ? &vtbl_bar : &vtbl_foo);
3332 RETVAL = mg ? SvREFCNT_inc((SV *)mg->mg_ptr) : &PL_sv_undef;
3333OUTPUT:
3334 RETVAL
3335
3336void
3337sv_unmagic_foo(SV *sv)
3338ALIAS:
3339 sv_unmagic_bar = 1
3340CODE:
3341 sv_unmagicext(SvRV(sv), PERL_MAGIC_ext, ix ? &vtbl_bar : &vtbl_foo);
406700cc
NC
3342
3343UV
3344test_get_vtbl()
3345 PREINIT:
3346 MGVTBL *have;
3347 MGVTBL *want;
3348 CODE:
3349#define test_get_this_vtable(name) \
3350 want = CAT2(&PL_vtbl_, name); \
3351 have = get_vtbl(CAT2(want_vtbl_, name)); \
3352 if (have != want) \
3353 croak("fail %p!=%p for get_vtbl(want_vtbl_" STRINGIFY(name) ") at " __FILE__ " line %d", have, want, __LINE__)
3354
3355 test_get_this_vtable(sv);
3356 test_get_this_vtable(env);
3357 test_get_this_vtable(envelem);
3358 test_get_this_vtable(sigelem);
3359 test_get_this_vtable(pack);
3360 test_get_this_vtable(packelem);
3361 test_get_this_vtable(dbline);
3362 test_get_this_vtable(isa);
3363 test_get_this_vtable(isaelem);
3364 test_get_this_vtable(arylen);
3365 test_get_this_vtable(mglob);
3366 test_get_this_vtable(nkeys);
3367 test_get_this_vtable(taint);
3368 test_get_this_vtable(substr);
3369 test_get_this_vtable(vec);
3370 test_get_this_vtable(pos);
3371 test_get_this_vtable(bm);
3372 test_get_this_vtable(fm);
3373 test_get_this_vtable(uvar);
3374 test_get_this_vtable(defelem);
3375 test_get_this_vtable(regexp);
3376 test_get_this_vtable(regdata);
3377 test_get_this_vtable(regdatum);
3378#ifdef USE_LOCALE_COLLATE
3379 test_get_this_vtable(collxfrm);
3380#endif
3381 test_get_this_vtable(amagic);
3382 test_get_this_vtable(amagicelem);
3383 test_get_this_vtable(backref);
3384 test_get_this_vtable(utf8);
3385
3386 RETVAL = PTR2UV(get_vtbl(-1));
3387 OUTPUT:
3388 RETVAL