This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
add missing STATIC to S_finalize_op
[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) {
1068 PERL_UNUSED_ARG(level);
1069 GV **gvp = (GV **)hv_fetchs(stash, "ISA", 0);
1070 return gvp && *gvp && GvAV(*gvp)
1071 ? GvAV(*gvp)
1072 : (AV *)sv_2mortal((SV *)newAV());
1073}
1074
27fcb6ee 1075
9777c6aa
SM
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__);
02cab674 1158 mymro.resolve = myget_linear_isa;
ad6a4f4a 1159 mymro.name = "justisa";
02cab674
FC
1160 mymro.length = 7;
1161 mymro.kflags = 0;
1162 mymro.hash = 0;
1163 Perl_mro_register(aTHX_ &mymro);
7b20c7cd
NC
1164
1165void
1166XS_VERSION_defined(...)
1167 PPCODE:
1168 XS_VERSION_BOOTCHECK;
1169 XSRETURN_EMPTY;
1170
88c4b02d
NC
1171void
1172XS_APIVERSION_valid(...)
1173 PPCODE:
1174 XS_APIVERSION_BOOTCHECK;
1175 XSRETURN_EMPTY;
1176
ffe53d21
NC
1177MODULE = XS::APItest:Hash PACKAGE = XS::APItest::Hash
1178
b54b4831
NC
1179void
1180rot13_hash(hash)
1181 HV *hash
1182 CODE:
1183 {
1184 struct ufuncs uf;
1185 uf.uf_val = rot13_key;
1186 uf.uf_set = 0;
1187 uf.uf_index = 0;
1188
1189 sv_magic((SV*)hash, NULL, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
1190 }
1191
53c40a8f
NC
1192void
1193bitflip_hash(hash)
1194 HV *hash
1195 CODE:
1196 {
1197 struct ufuncs uf;
1198 uf.uf_val = bitflip_key;
1199 uf.uf_set = 0;
1200 uf.uf_index = 0;
1201
1202 sv_magic((SV*)hash, NULL, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
1203 }
1204
028f8eaa
MHM
1205#define UTF8KLEN(sv, len) (SvUTF8(sv) ? -(I32)len : (I32)len)
1206
0314122a
NC
1207bool
1208exists(hash, key_sv)
1209 PREINIT:
1210 STRLEN len;
1211 const char *key;
1212 INPUT:
1213 HV *hash
1214 SV *key_sv
1215 CODE:
1216 key = SvPV(key_sv, len);
028f8eaa 1217 RETVAL = hv_exists(hash, key, UTF8KLEN(key_sv, len));
0314122a
NC
1218 OUTPUT:
1219 RETVAL
1220
bdee33e4
NC
1221bool
1222exists_ent(hash, key_sv)
1223 PREINIT:
1224 INPUT:
1225 HV *hash
1226 SV *key_sv
1227 CODE:
1228 RETVAL = hv_exists_ent(hash, key_sv, 0);
1229 OUTPUT:
1230 RETVAL
1231
b60cf05a 1232SV *
55289a74 1233delete(hash, key_sv, flags = 0)
b60cf05a
NC
1234 PREINIT:
1235 STRLEN len;
1236 const char *key;
1237 INPUT:
1238 HV *hash
1239 SV *key_sv
55289a74 1240 I32 flags;
b60cf05a
NC
1241 CODE:
1242 key = SvPV(key_sv, len);
1243 /* It's already mortal, so need to increase reference count. */
55289a74
NC
1244 RETVAL
1245 = SvREFCNT_inc(hv_delete(hash, key, UTF8KLEN(key_sv, len), flags));
1246 OUTPUT:
1247 RETVAL
1248
1249SV *
1250delete_ent(hash, key_sv, flags = 0)
1251 INPUT:
1252 HV *hash
1253 SV *key_sv
1254 I32 flags;
1255 CODE:
1256 /* It's already mortal, so need to increase reference count. */
1257 RETVAL = SvREFCNT_inc(hv_delete_ent(hash, key_sv, flags, 0));
b60cf05a
NC
1258 OUTPUT:
1259 RETVAL
1260
1261SV *
858117f8
NC
1262store_ent(hash, key, value)
1263 PREINIT:
1264 SV *copy;
1265 HE *result;
1266 INPUT:
1267 HV *hash
1268 SV *key
1269 SV *value
1270 CODE:
1271 copy = newSV(0);
1272 result = hv_store_ent(hash, key, copy, 0);
1273 SvSetMagicSV(copy, value);
1274 if (!result) {
1275 SvREFCNT_dec(copy);
1276 XSRETURN_EMPTY;
1277 }
1278 /* It's about to become mortal, so need to increase reference count.
1279 */
1280 RETVAL = SvREFCNT_inc(HeVAL(result));
1281 OUTPUT:
1282 RETVAL
1283
858117f8 1284SV *
b60cf05a
NC
1285store(hash, key_sv, value)
1286 PREINIT:
1287 STRLEN len;
1288 const char *key;
1289 SV *copy;
1290 SV **result;
1291 INPUT:
1292 HV *hash
1293 SV *key_sv
1294 SV *value
1295 CODE:
1296 key = SvPV(key_sv, len);
1297 copy = newSV(0);
028f8eaa 1298 result = hv_store(hash, key, UTF8KLEN(key_sv, len), copy, 0);
858117f8 1299 SvSetMagicSV(copy, value);
b60cf05a
NC
1300 if (!result) {
1301 SvREFCNT_dec(copy);
1302 XSRETURN_EMPTY;
1303 }
1304 /* It's about to become mortal, so need to increase reference count.
1305 */
1306 RETVAL = SvREFCNT_inc(*result);
1307 OUTPUT:
1308 RETVAL
1309
bdee33e4
NC
1310SV *
1311fetch_ent(hash, key_sv)
1312 PREINIT:
1313 HE *result;
1314 INPUT:
1315 HV *hash
1316 SV *key_sv
1317 CODE:
1318 result = hv_fetch_ent(hash, key_sv, 0, 0);
1319 if (!result) {
1320 XSRETURN_EMPTY;
1321 }
1322 /* Force mg_get */
1323 RETVAL = newSVsv(HeVAL(result));
1324 OUTPUT:
1325 RETVAL
b60cf05a
NC
1326
1327SV *
1328fetch(hash, key_sv)
1329 PREINIT:
1330 STRLEN len;
1331 const char *key;
1332 SV **result;
1333 INPUT:
1334 HV *hash
1335 SV *key_sv
1336 CODE:
1337 key = SvPV(key_sv, len);
028f8eaa 1338 result = hv_fetch(hash, key, UTF8KLEN(key_sv, len), 0);
b60cf05a
NC
1339 if (!result) {
1340 XSRETURN_EMPTY;
1341 }
1342 /* Force mg_get */
1343 RETVAL = newSVsv(*result);
1344 OUTPUT:
1345 RETVAL
2dc92170 1346
9568a123
NC
1347#if defined (hv_common)
1348
6b4de907
NC
1349SV *
1350common(params)
1351 INPUT:
1352 HV *params
1353 PREINIT:
1354 HE *result;
1355 HV *hv = NULL;
1356 SV *keysv = NULL;
1357 const char *key = NULL;
1358 STRLEN klen = 0;
1359 int flags = 0;
1360 int action = 0;
1361 SV *val = NULL;
1362 U32 hash = 0;
1363 SV **svp;
1364 CODE:
1365 if ((svp = hv_fetchs(params, "hv", 0))) {
1366 SV *const rv = *svp;
1367 if (!SvROK(rv))
1368 croak("common passed a non-reference for parameter hv");
1369 hv = (HV *)SvRV(rv);
1370 }
1371 if ((svp = hv_fetchs(params, "keysv", 0)))
1372 keysv = *svp;
1373 if ((svp = hv_fetchs(params, "keypv", 0))) {
1374 key = SvPV_const(*svp, klen);
1375 if (SvUTF8(*svp))
1376 flags = HVhek_UTF8;
1377 }
1378 if ((svp = hv_fetchs(params, "action", 0)))
1379 action = SvIV(*svp);
1380 if ((svp = hv_fetchs(params, "val", 0)))
527df579 1381 val = newSVsv(*svp);
6b4de907 1382 if ((svp = hv_fetchs(params, "hash", 0)))
a44093a9 1383 hash = SvUV(*svp);
6b4de907 1384
527df579
NC
1385 if ((svp = hv_fetchs(params, "hash_pv", 0))) {
1386 PERL_HASH(hash, key, klen);
1387 }
58ca560a
NC
1388 if ((svp = hv_fetchs(params, "hash_sv", 0))) {
1389 STRLEN len;
1390 const char *const p = SvPV(keysv, len);
1391 PERL_HASH(hash, p, len);
1392 }
527df579 1393
a75fcbca 1394 result = (HE *)hv_common(hv, keysv, key, klen, flags, action, val, hash);
6b4de907
NC
1395 if (!result) {
1396 XSRETURN_EMPTY;
1397 }
1398 /* Force mg_get */
1399 RETVAL = newSVsv(HeVAL(result));
1400 OUTPUT:
1401 RETVAL
1402
9568a123
NC
1403#endif
1404
439efdfe 1405void
2dc92170
NC
1406test_hv_free_ent()
1407 PPCODE:
1408 test_freeent(&Perl_hv_free_ent);
1409 XSRETURN(4);
1410
439efdfe 1411void
2dc92170
NC
1412test_hv_delayfree_ent()
1413 PPCODE:
1414 test_freeent(&Perl_hv_delayfree_ent);
1415 XSRETURN(4);
35ab5632
NC
1416
1417SV *
1418test_share_unshare_pvn(input)
1419 PREINIT:
35ab5632
NC
1420 STRLEN len;
1421 U32 hash;
1422 char *pvx;
1423 char *p;
1424 INPUT:
1425 SV *input
1426 CODE:
1427 pvx = SvPV(input, len);
1428 PERL_HASH(hash, pvx, len);
1429 p = sharepvn(pvx, len, hash);
1430 RETVAL = newSVpvn(p, len);
1431 unsharepvn(p, len, hash);
1432 OUTPUT:
1433 RETVAL
d8c5b3c5 1434
9568a123
NC
1435#if PERL_VERSION >= 9
1436
d8c5b3c5
NC
1437bool
1438refcounted_he_exists(key, level=0)
1439 SV *key
1440 IV level
1441 CODE:
1442 if (level) {
1443 croak("level must be zero, not %"IVdf, level);
1444 }
20439bc7 1445 RETVAL = (cop_hints_fetch_sv(PL_curcop, key, 0, 0) != &PL_sv_placeholder);
d8c5b3c5
NC
1446 OUTPUT:
1447 RETVAL
1448
d8c5b3c5
NC
1449SV *
1450refcounted_he_fetch(key, level=0)
1451 SV *key
1452 IV level
1453 CODE:
1454 if (level) {
1455 croak("level must be zero, not %"IVdf, level);
1456 }
20439bc7 1457 RETVAL = cop_hints_fetch_sv(PL_curcop, key, 0, 0);
d8c5b3c5
NC
1458 SvREFCNT_inc(RETVAL);
1459 OUTPUT:
1460 RETVAL
65bfe90c 1461
9568a123 1462#endif
65bfe90c 1463
0314122a
NC
1464=pod
1465
1466sub TIEHASH { bless {}, $_[0] }
1467sub STORE { $_[0]->{$_[1]} = $_[2] }
1468sub FETCH { $_[0]->{$_[1]} }
1469sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
1470sub NEXTKEY { each %{$_[0]} }
1471sub EXISTS { exists $_[0]->{$_[1]} }
1472sub DELETE { delete $_[0]->{$_[1]} }
1473sub CLEAR { %{$_[0]} = () }
1474
1475=cut
1476
e2fe06dd
EB
1477MODULE = XS::APItest:TempLv PACKAGE = XS::APItest::TempLv
1478
1479void
1480make_temp_mg_lv(sv)
1481SV* sv
1482 PREINIT:
1483 SV * const lv = newSV_type(SVt_PVLV);
1484 STRLEN len;
1485 PPCODE:
1486 SvPV(sv, len);
1487
1488 sv_magic(lv, NULL, PERL_MAGIC_substr, NULL, 0);
1489 LvTYPE(lv) = 'x';
1490 LvTARG(lv) = SvREFCNT_inc_simple(sv);
1491 LvTARGOFF(lv) = len == 0 ? 0 : 1;
1492 LvTARGLEN(lv) = len < 2 ? 0 : len-2;
1493
1494 EXTEND(SP, 1);
1495 ST(0) = sv_2mortal(lv);
1496 XSRETURN(1);
1497
1498
36c2b1d0
NC
1499MODULE = XS::APItest::PtrTable PACKAGE = XS::APItest::PtrTable PREFIX = ptr_table_
1500
1501void
1502ptr_table_new(classname)
1503const char * classname
1504 PPCODE:
1505 PUSHs(sv_setref_pv(sv_newmortal(), classname, (void*)ptr_table_new()));
1506
1507void
1508DESTROY(table)
1509XS::APItest::PtrTable table
1510 CODE:
1511 ptr_table_free(table);
1512
1513void
992b2363 1514ptr_table_store(table, from, to)
36c2b1d0 1515XS::APItest::PtrTable table
992b2363
NC
1516SVREF from
1517SVREF to
36c2b1d0 1518 CODE:
992b2363 1519 ptr_table_store(table, from, to);
36c2b1d0
NC
1520
1521UV
992b2363 1522ptr_table_fetch(table, from)
36c2b1d0 1523XS::APItest::PtrTable table
992b2363 1524SVREF from
36c2b1d0 1525 CODE:
992b2363 1526 RETVAL = PTR2UV(ptr_table_fetch(table, from));
36c2b1d0
NC
1527 OUTPUT:
1528 RETVAL
1529
1530void
1531ptr_table_split(table)
1532XS::APItest::PtrTable table
1533
1534void
1535ptr_table_clear(table)
1536XS::APItest::PtrTable table
1537
6911735f
FC
1538MODULE = XS::APItest::AutoLoader PACKAGE = XS::APItest::AutoLoader
1539
1540SV *
1541AUTOLOAD()
1542 CODE:
1543 RETVAL = newSVpvn_flags(SvPVX(cv), SvCUR(cv), SvUTF8(cv));
1544 OUTPUT:
1545 RETVAL
1546
8fa6a409
FC
1547SV *
1548AUTOLOADp(...)
1549 PROTOTYPE: *$
1550 CODE:
1551 RETVAL = newSVpvn_flags(SvPVX(cv), SvCUR(cv), SvUTF8(cv));
1552 OUTPUT:
1553 RETVAL
1554
6911735f 1555
3e61d65a
JH
1556MODULE = XS::APItest PACKAGE = XS::APItest
1557
1558PROTOTYPES: DISABLE
1559
f568d64d
BM
1560HV *
1561xop_custom_ops ()
1562 CODE:
1563 RETVAL = PL_custom_ops;
1564 OUTPUT:
1565 RETVAL
1566
1567HV *
1568xop_custom_op_names ()
1569 CODE:
1570 PL_custom_op_names = newHV();
1571 RETVAL = PL_custom_op_names;
1572 OUTPUT:
1573 RETVAL
1574
1575HV *
1576xop_custom_op_descs ()
1577 CODE:
1578 PL_custom_op_descs = newHV();
1579 RETVAL = PL_custom_op_descs;
1580 OUTPUT:
1581 RETVAL
1582
1583void
1584xop_register ()
1585 CODE:
1586 XopENTRY_set(&my_xop, xop_name, "my_xop");
1587 XopENTRY_set(&my_xop, xop_desc, "XOP for testing");
1588 XopENTRY_set(&my_xop, xop_class, OA_UNOP);
1589 XopENTRY_set(&my_xop, xop_peep, peep_xop);
1590 Perl_custom_op_register(aTHX_ pp_xop, &my_xop);
1591
1592void
1593xop_clear ()
1594 CODE:
1595 XopDISABLE(&my_xop, xop_name);
1596 XopDISABLE(&my_xop, xop_desc);
1597 XopDISABLE(&my_xop, xop_class);
1598 XopDISABLE(&my_xop, xop_peep);
1599
1600IV
1601xop_my_xop ()
1602 CODE:
1603 RETVAL = PTR2IV(&my_xop);
1604 OUTPUT:
1605 RETVAL
1606
1607IV
1608xop_ppaddr ()
1609 CODE:
1610 RETVAL = PTR2IV(pp_xop);
1611 OUTPUT:
1612 RETVAL
1613
1614IV
1615xop_OA_UNOP ()
1616 CODE:
1617 RETVAL = OA_UNOP;
1618 OUTPUT:
1619 RETVAL
1620
1621AV *
1622xop_build_optree ()
1623 CODE:
1624 dMY_CXT;
1625 UNOP *unop;
1626 OP *kid;
1627
1628 MY_CXT.xop_record = newAV();
1629
1630 kid = newSVOP(OP_CONST, 0, newSViv(42));
1631
1632 NewOp(1102, unop, 1, UNOP);
1633 unop->op_type = OP_CUSTOM;
1634 unop->op_ppaddr = pp_xop;
1635 unop->op_flags = OPf_KIDS;
1636 unop->op_private = 0;
1637 unop->op_first = kid;
1638 unop->op_next = NULL;
1639 kid->op_next = (OP*)unop;
1640
b58757d5
BM
1641 av_push(MY_CXT.xop_record, newSVpvf("unop:%"UVxf, PTR2UV(unop)));
1642 av_push(MY_CXT.xop_record, newSVpvf("kid:%"UVxf, PTR2UV(kid)));
f568d64d
BM
1643
1644 av_push(MY_CXT.xop_record, newSVpvf("NAME:%s", OP_NAME((OP*)unop)));
1645 av_push(MY_CXT.xop_record, newSVpvf("DESC:%s", OP_DESC((OP*)unop)));
c33e8be1 1646 av_push(MY_CXT.xop_record, newSVpvf("CLASS:%d", (int)OP_CLASS((OP*)unop)));
f568d64d
BM
1647
1648 PL_rpeepp(aTHX_ kid);
1649
1650 FreeOp(kid);
1651 FreeOp(unop);
1652
1653 RETVAL = MY_CXT.xop_record;
1654 MY_CXT.xop_record = NULL;
1655 OUTPUT:
1656 RETVAL
1657
85ce96a1
DM
1658BOOT:
1659{
1660 MY_CXT_INIT;
03569ecf 1661
85ce96a1
DM
1662 MY_CXT.i = 99;
1663 MY_CXT.sv = newSVpv("initial",0);
13b6b3bc
BM
1664
1665 MY_CXT.bhkav = get_av("XS::APItest::bhkav", GV_ADDMULTI);
1666 MY_CXT.bhk_record = 0;
1667
a88d97bf
BM
1668 BhkENTRY_set(&bhk_test, bhk_start, blockhook_test_start);
1669 BhkENTRY_set(&bhk_test, bhk_pre_end, blockhook_test_pre_end);
1670 BhkENTRY_set(&bhk_test, bhk_post_end, blockhook_test_post_end);
1671 BhkENTRY_set(&bhk_test, bhk_eval, blockhook_test_eval);
13b6b3bc
BM
1672 Perl_blockhook_register(aTHX_ &bhk_test);
1673
65bfe90c 1674 MY_CXT.cscgv = gv_fetchpvs("XS::APItest::COMPILE_SCOPE_CONTAINER",
13b6b3bc 1675 GV_ADDMULTI, SVt_PVAV);
03569ecf
BM
1676 MY_CXT.cscav = GvAV(MY_CXT.cscgv);
1677
a88d97bf
BM
1678 BhkENTRY_set(&bhk_csc, bhk_start, blockhook_csc_start);
1679 BhkENTRY_set(&bhk_csc, bhk_pre_end, blockhook_csc_pre_end);
13b6b3bc 1680 Perl_blockhook_register(aTHX_ &bhk_csc);
201c7e1f
FR
1681
1682 MY_CXT.peep_recorder = newAV();
1683 MY_CXT.rpeep_recorder = newAV();
1684
1685 MY_CXT.orig_peep = PL_peepp;
1686 MY_CXT.orig_rpeep = PL_rpeepp;
1687 PL_peepp = my_peep;
1688 PL_rpeepp = my_rpeep;
65bfe90c 1689}
85ce96a1
DM
1690
1691void
1692CLONE(...)
1693 CODE:
1694 MY_CXT_CLONE;
c33e8be1 1695 PERL_UNUSED_VAR(items);
85ce96a1 1696 MY_CXT.sv = newSVpv("initial_clone",0);
65bfe90c 1697 MY_CXT.cscgv = gv_fetchpvs("XS::APItest::COMPILE_SCOPE_CONTAINER",
13b6b3bc 1698 GV_ADDMULTI, SVt_PVAV);
03569ecf 1699 MY_CXT.cscav = NULL;
13b6b3bc
BM
1700 MY_CXT.bhkav = get_av("XS::APItest::bhkav", GV_ADDMULTI);
1701 MY_CXT.bhk_record = 0;
201c7e1f
FR
1702 MY_CXT.peep_recorder = newAV();
1703 MY_CXT.rpeep_recorder = newAV();
85ce96a1 1704
3e61d65a
JH
1705void
1706print_double(val)
1707 double val
1708 CODE:
1709 printf("%5.3f\n",val);
1710
1711int
1712have_long_double()
1713 CODE:
1714#ifdef HAS_LONG_DOUBLE
1715 RETVAL = 1;
1716#else
1717 RETVAL = 0;
1718#endif
cabb36f0
CN
1719 OUTPUT:
1720 RETVAL
3e61d65a
JH
1721
1722void
1723print_long_double()
1724 CODE:
1725#ifdef HAS_LONG_DOUBLE
fc0bf671 1726# if defined(PERL_PRIfldbl) && (LONG_DOUBLESIZE > DOUBLESIZE)
3e61d65a
JH
1727 long double val = 7.0;
1728 printf("%5.3" PERL_PRIfldbl "\n",val);
1729# else
1730 double val = 7.0;
1731 printf("%5.3f\n",val);
1732# endif
1733#endif
1734
1735void
3e61d65a
JH
1736print_int(val)
1737 int val
1738 CODE:
1739 printf("%d\n",val);
1740
1741void
1742print_long(val)
1743 long val
1744 CODE:
1745 printf("%ld\n",val);
1746
1747void
1748print_float(val)
1749 float val
1750 CODE:
1751 printf("%5.3f\n",val);
9d911683
NIS
1752
1753void
1754print_flush()
1755 CODE:
1756 fflush(stdout);
d4b90eee
SH
1757
1758void
1759mpushp()
1760 PPCODE:
1761 EXTEND(SP, 3);
1762 mPUSHp("one", 3);
1763 mPUSHp("two", 3);
1764 mPUSHp("three", 5);
1765 XSRETURN(3);
1766
1767void
1768mpushn()
1769 PPCODE:
1770 EXTEND(SP, 3);
1771 mPUSHn(0.5);
1772 mPUSHn(-0.25);
1773 mPUSHn(0.125);
1774 XSRETURN(3);
1775
1776void
1777mpushi()
1778 PPCODE:
1779 EXTEND(SP, 3);
d75b63cf
MHM
1780 mPUSHi(-1);
1781 mPUSHi(2);
1782 mPUSHi(-3);
d4b90eee
SH
1783 XSRETURN(3);
1784
1785void
1786mpushu()
1787 PPCODE:
1788 EXTEND(SP, 3);
d75b63cf
MHM
1789 mPUSHu(1);
1790 mPUSHu(2);
1791 mPUSHu(3);
d4b90eee
SH
1792 XSRETURN(3);
1793
1794void
1795mxpushp()
1796 PPCODE:
1797 mXPUSHp("one", 3);
1798 mXPUSHp("two", 3);
1799 mXPUSHp("three", 5);
1800 XSRETURN(3);
1801
1802void
1803mxpushn()
1804 PPCODE:
1805 mXPUSHn(0.5);
1806 mXPUSHn(-0.25);
1807 mXPUSHn(0.125);
1808 XSRETURN(3);
1809
1810void
1811mxpushi()
1812 PPCODE:
d75b63cf
MHM
1813 mXPUSHi(-1);
1814 mXPUSHi(2);
1815 mXPUSHi(-3);
d4b90eee
SH
1816 XSRETURN(3);
1817
1818void
1819mxpushu()
1820 PPCODE:
d75b63cf
MHM
1821 mXPUSHu(1);
1822 mXPUSHu(2);
1823 mXPUSHu(3);
d4b90eee 1824 XSRETURN(3);
d1f347d7
DM
1825
1826
1827void
1828call_sv(sv, flags, ...)
1829 SV* sv
1830 I32 flags
1831 PREINIT:
1832 I32 i;
1833 PPCODE:
1834 for (i=0; i<items-2; i++)
1835 ST(i) = ST(i+2); /* pop first two args */
1836 PUSHMARK(SP);
1837 SP += items - 2;
1838 PUTBACK;
1839 i = call_sv(sv, flags);
1840 SPAGAIN;
1841 EXTEND(SP, 1);
1842 PUSHs(sv_2mortal(newSViv(i)));
1843
1844void
1845call_pv(subname, flags, ...)
1846 char* subname
1847 I32 flags
1848 PREINIT:
1849 I32 i;
1850 PPCODE:
1851 for (i=0; i<items-2; i++)
1852 ST(i) = ST(i+2); /* pop first two args */
1853 PUSHMARK(SP);
1854 SP += items - 2;
1855 PUTBACK;
1856 i = call_pv(subname, flags);
1857 SPAGAIN;
1858 EXTEND(SP, 1);
1859 PUSHs(sv_2mortal(newSViv(i)));
1860
1861void
1862call_method(methname, flags, ...)
1863 char* methname
1864 I32 flags
1865 PREINIT:
1866 I32 i;
1867 PPCODE:
1868 for (i=0; i<items-2; i++)
1869 ST(i) = ST(i+2); /* pop first two args */
1870 PUSHMARK(SP);
1871 SP += items - 2;
1872 PUTBACK;
1873 i = call_method(methname, flags);
1874 SPAGAIN;
1875 EXTEND(SP, 1);
1876 PUSHs(sv_2mortal(newSViv(i)));
1877
1878void
3453414d
BF
1879newCONSTSUB_type(stash, name, flags, type)
1880 HV* stash
1881 SV* name
1882 I32 flags
1883 int type
1884 PREINIT:
1885 CV* cv;
1886 PPCODE:
1887 switch (type) {
1888 case 0:
1889 cv = newCONSTSUB(stash, SvPV_nolen(name), NULL);
1890 break;
1891 case 1:
1892 cv = newCONSTSUB_flags(stash, SvPV_nolen(name), flags | SvUTF8(name), NULL);
1893 break;
1894 }
1895 EXTEND(SP, 2);
1896 PUSHs( CvCONST(cv) ? &PL_sv_yes : &PL_sv_no );
1897 PUSHs((SV*)CvGV(cv));
1898
1899void
e6066781
BF
1900gv_init_type(namesv, multi, flags, type)
1901 SV* namesv
1902 int multi
1903 I32 flags
1904 int type
1905 PREINIT:
1906 STRLEN len;
1907 const char * const name = SvPV_const(namesv, len);
1908 GV *gv = *(GV**)hv_fetch(PL_defstash, name, len, TRUE);
1909 PPCODE:
1910 if (SvTYPE(gv) == SVt_PVGV)
1911 Perl_croak(aTHX_ "GV is already a PVGV");
04ec7e59 1912 if (multi) flags |= GV_ADDMULTI;
e6066781
BF
1913 switch (type) {
1914 case 0:
1915 gv_init(gv, PL_defstash, name, len, multi);
1916 break;
1917 case 1:
04ec7e59 1918 gv_init_sv(gv, PL_defstash, namesv, flags);
e6066781
BF
1919 break;
1920 case 2:
04ec7e59 1921 gv_init_pv(gv, PL_defstash, name, flags | SvUTF8(namesv));
e6066781
BF
1922 break;
1923 case 3:
04ec7e59 1924 gv_init_pvn(gv, PL_defstash, name, len, flags | SvUTF8(namesv));
e6066781
BF
1925 break;
1926 }
1927 XPUSHs( gv ? (SV*)gv : &PL_sv_undef);
1928
1929void
e6919483
BF
1930gv_fetchmeth_type(stash, methname, type, level, flags)
1931 HV* stash
1932 SV* methname
1933 int type
1934 I32 level
1935 I32 flags
1936 PREINIT:
1937 STRLEN len;
1938 const char * const name = SvPV_const(methname, len);
1939 GV* gv;
1940 PPCODE:
1941 switch (type) {
1942 case 0:
1943 gv = gv_fetchmeth(stash, name, len, level);
1944 break;
1945 case 1:
1946 gv = gv_fetchmeth_sv(stash, methname, level, flags);
1947 break;
1948 case 2:
1949 gv = gv_fetchmeth_pv(stash, name, level, flags | SvUTF8(methname));
1950 break;
1951 case 3:
1952 gv = gv_fetchmeth_pvn(stash, name, len, level, flags | SvUTF8(methname));
1953 break;
1954 }
1955 XPUSHs( gv ? MUTABLE_SV(gv) : &PL_sv_undef );
1956
1957void
d21989ed
BF
1958gv_fetchmeth_autoload_type(stash, methname, type, level, flags)
1959 HV* stash
1960 SV* methname
1961 int type
1962 I32 level
1963 I32 flags
1964 PREINIT:
1965 STRLEN len;
1966 const char * const name = SvPV_const(methname, len);
1967 GV* gv;
1968 PPCODE:
1969 switch (type) {
1970 case 0:
1971 gv = gv_fetchmeth_autoload(stash, name, len, level);
1972 break;
1973 case 1:
1974 gv = gv_fetchmeth_sv_autoload(stash, methname, level, flags);
1975 break;
1976 case 2:
1977 gv = gv_fetchmeth_pv_autoload(stash, name, level, flags | SvUTF8(methname));
1978 break;
1979 case 3:
1980 gv = gv_fetchmeth_pvn_autoload(stash, name, len, level, flags | SvUTF8(methname));
1981 break;
1982 }
1983 XPUSHs( gv ? MUTABLE_SV(gv) : &PL_sv_undef );
1984
1985void
44130a26
BF
1986gv_fetchmethod_flags_type(stash, methname, type, flags)
1987 HV* stash
1988 SV* methname
1989 int type
1990 I32 flags
1991 PREINIT:
1992 GV* gv;
1993 PPCODE:
1994 switch (type) {
1995 case 0:
1996 gv = gv_fetchmethod_flags(stash, SvPVX_const(methname), flags);
1997 break;
1998 case 1:
1999 gv = gv_fetchmethod_sv_flags(stash, methname, flags);
2000 break;
2001 case 2:
2002 gv = gv_fetchmethod_pv_flags(stash, SvPV_nolen(methname), flags | SvUTF8(methname));
2003 break;
2004 case 3: {
2005 STRLEN len;
2006 const char * const name = SvPV_const(methname, len);
2007 gv = gv_fetchmethod_pvn_flags(stash, name, len, flags | SvUTF8(methname));
2008 break;
2009 }
2010 }
5fba3c91 2011 XPUSHs( gv ? (SV*)gv : &PL_sv_undef);
44130a26 2012
5fba3c91 2013void
0eeb01b9 2014gv_autoload_type(stash, methname, type, method)
5fba3c91
BF
2015 HV* stash
2016 SV* methname
2017 int type
2018 I32 method
5fba3c91
BF
2019 PREINIT:
2020 STRLEN len;
2021 const char * const name = SvPV_const(methname, len);
2022 GV* gv;
0eeb01b9 2023 I32 flags = method ? GV_AUTOLOAD_ISMETHOD : 0;
5fba3c91
BF
2024 PPCODE:
2025 switch (type) {
2026 case 0:
2027 gv = gv_autoload4(stash, name, len, method);
2028 break;
2029 case 1:
0eeb01b9 2030 gv = gv_autoload_sv(stash, methname, flags);
5fba3c91
BF
2031 break;
2032 case 2:
0eeb01b9 2033 gv = gv_autoload_pv(stash, name, flags | SvUTF8(methname));
5fba3c91
BF
2034 break;
2035 case 3:
0eeb01b9 2036 gv = gv_autoload_pvn(stash, name, len, flags | SvUTF8(methname));
5fba3c91
BF
2037 break;
2038 }
44130a26
BF
2039 XPUSHs( gv ? (SV*)gv : &PL_sv_undef);
2040
2041void
84c7b88c
BF
2042whichsig_type(namesv, type)
2043 SV* namesv
2044 int type
2045 PREINIT:
2046 STRLEN len;
2047 const char * const name = SvPV_const(namesv, len);
2048 I32 i;
2049 PPCODE:
2050 switch (type) {
2051 case 0:
2052 i = whichsig(name);
2053 break;
2054 case 1:
2055 i = whichsig_sv(namesv);
2056 break;
2057 case 2:
2058 i = whichsig_pv(name);
2059 break;
2060 case 3:
2061 i = whichsig_pvn(name, len);
2062 break;
2063 }
2064 XPUSHs(sv_2mortal(newSViv(i)));
2065
2066void
d1f347d7
DM
2067eval_sv(sv, flags)
2068 SV* sv
2069 I32 flags
2070 PREINIT:
2071 I32 i;
2072 PPCODE:
2073 PUTBACK;
2074 i = eval_sv(sv, flags);
2075 SPAGAIN;
2076 EXTEND(SP, 1);
2077 PUSHs(sv_2mortal(newSViv(i)));
2078
b8e65a9b 2079void
d1f347d7
DM
2080eval_pv(p, croak_on_error)
2081 const char* p
2082 I32 croak_on_error
d1f347d7
DM
2083 PPCODE:
2084 PUTBACK;
2085 EXTEND(SP, 1);
2086 PUSHs(eval_pv(p, croak_on_error));
2087
2088void
2089require_pv(pv)
2090 const char* pv
d1f347d7
DM
2091 PPCODE:
2092 PUTBACK;
2093 require_pv(pv);
2094
0ca3a874 2095int
7a646707 2096apitest_exception(throw_e)
0ca3a874
MHM
2097 int throw_e
2098 OUTPUT:
2099 RETVAL
d1f347d7 2100
ef469b03 2101void
7e7a3dfc
GA
2102mycroak(sv)
2103 SV* sv
ef469b03 2104 CODE:
7e7a3dfc
GA
2105 if (SvOK(sv)) {
2106 Perl_croak(aTHX_ "%s", SvPV_nolen(sv));
2107 }
2108 else {
2109 Perl_croak(aTHX_ NULL);
2110 }
5d2b1485
NC
2111
2112SV*
2113strtab()
2114 CODE:
2115 RETVAL = newRV_inc((SV*)PL_strtab);
2116 OUTPUT:
2117 RETVAL
85ce96a1
DM
2118
2119int
2120my_cxt_getint()
2121 CODE:
2122 dMY_CXT;
2123 RETVAL = my_cxt_getint_p(aMY_CXT);
2124 OUTPUT:
2125 RETVAL
2126
2127void
2128my_cxt_setint(i)
2129 int i;
2130 CODE:
2131 dMY_CXT;
2132 my_cxt_setint_p(aMY_CXT_ i);
2133
2134void
9568a123
NC
2135my_cxt_getsv(how)
2136 bool how;
85ce96a1 2137 PPCODE:
85ce96a1 2138 EXTEND(SP, 1);
9568a123 2139 ST(0) = how ? my_cxt_getsv_interp_context() : my_cxt_getsv_interp();
85ce96a1
DM
2140 XSRETURN(1);
2141
2142void
2143my_cxt_setsv(sv)
2144 SV *sv;
2145 CODE:
2146 dMY_CXT;
2147 SvREFCNT_dec(MY_CXT.sv);
2148 my_cxt_setsv_p(sv _aMY_CXT);
2149 SvREFCNT_inc(sv);
34482cd6
NC
2150
2151bool
2152sv_setsv_cow_hashkey_core()
2153
2154bool
2155sv_setsv_cow_hashkey_notcore()
84ac5fd7
NC
2156
2157void
218787bd
VP
2158rmagical_cast(sv, type)
2159 SV *sv;
2160 SV *type;
2161 PREINIT:
2162 struct ufuncs uf;
2163 PPCODE:
2164 if (!SvOK(sv) || !SvROK(sv) || !SvOK(type)) { XSRETURN_UNDEF; }
2165 sv = SvRV(sv);
2166 if (SvTYPE(sv) != SVt_PVHV) { XSRETURN_UNDEF; }
2167 uf.uf_val = rmagical_a_dummy;
2168 uf.uf_set = NULL;
2169 uf.uf_index = 0;
2170 if (SvTRUE(type)) { /* b */
2171 sv_magicext(sv, NULL, PERL_MAGIC_ext, &rmagical_b, NULL, 0);
2172 } else { /* a */
2173 sv_magic(sv, NULL, PERL_MAGIC_uvar, (char *) &uf, sizeof(uf));
2174 }
2175 XSRETURN_YES;
2176
2177void
2178rmagical_flags(sv)
2179 SV *sv;
2180 PPCODE:
2181 if (!SvOK(sv) || !SvROK(sv)) { XSRETURN_UNDEF; }
2182 sv = SvRV(sv);
2183 EXTEND(SP, 3);
2184 mXPUSHu(SvFLAGS(sv) & SVs_GMG);
2185 mXPUSHu(SvFLAGS(sv) & SVs_SMG);
2186 mXPUSHu(SvFLAGS(sv) & SVs_RMG);
2187 XSRETURN(3);
2188
2189void
90d1f214
BM
2190my_caller(level)
2191 I32 level
2192 PREINIT:
2193 const PERL_CONTEXT *cx, *dbcx;
2194 const char *pv;
2195 const GV *gv;
2196 HV *hv;
2197 PPCODE:
2198 cx = caller_cx(level, &dbcx);
2199 EXTEND(SP, 8);
2200
2201 pv = CopSTASHPV(cx->blk_oldcop);
2202 ST(0) = pv ? sv_2mortal(newSVpv(pv, 0)) : &PL_sv_undef;
2203 gv = CvGV(cx->blk_sub.cv);
2204 ST(1) = isGV(gv) ? sv_2mortal(newSVpv(GvNAME(gv), 0)) : &PL_sv_undef;
2205
2206 pv = CopSTASHPV(dbcx->blk_oldcop);
2207 ST(2) = pv ? sv_2mortal(newSVpv(pv, 0)) : &PL_sv_undef;
2208 gv = CvGV(dbcx->blk_sub.cv);
2209 ST(3) = isGV(gv) ? sv_2mortal(newSVpv(GvNAME(gv), 0)) : &PL_sv_undef;
2210
20439bc7
Z
2211 ST(4) = cop_hints_fetch_pvs(cx->blk_oldcop, "foo", 0);
2212 ST(5) = cop_hints_fetch_pvn(cx->blk_oldcop, "foo", 3, 0, 0);
2213 ST(6) = cop_hints_fetch_sv(cx->blk_oldcop,
2214 sv_2mortal(newSVpvn("foo", 3)), 0, 0);
90d1f214 2215
20439bc7 2216 hv = cop_hints_2hv(cx->blk_oldcop, 0);
90d1f214
BM
2217 ST(7) = hv ? sv_2mortal(newRV_noinc((SV *)hv)) : &PL_sv_undef;
2218
2219 XSRETURN(8);
2220
2221void
f9c17636
MB
2222DPeek (sv)
2223 SV *sv
2224
2225 PPCODE:
5b1f7359 2226 ST (0) = newSVpv (Perl_sv_peek (aTHX_ sv), 0);
f9c17636
MB
2227 XSRETURN (1);
2228
2229void
84ac5fd7
NC
2230BEGIN()
2231 CODE:
2232 sv_inc(get_sv("XS::APItest::BEGIN_called", GV_ADD|GV_ADDMULTI));
2233
2234void
2235CHECK()
2236 CODE:
2237 sv_inc(get_sv("XS::APItest::CHECK_called", GV_ADD|GV_ADDMULTI));
2238
2239void
2240UNITCHECK()
2241 CODE:
0932863f 2242 sv_inc(get_sv("XS::APItest::UNITCHECK_called", GV_ADD|GV_ADDMULTI));
84ac5fd7
NC
2243
2244void
2245INIT()
2246 CODE:
2247 sv_inc(get_sv("XS::APItest::INIT_called", GV_ADD|GV_ADDMULTI));
2248
2249void
2250END()
2251 CODE:
2252 sv_inc(get_sv("XS::APItest::END_called", GV_ADD|GV_ADDMULTI));
30685b56
NC
2253
2254void
2255utf16_to_utf8 (sv, ...)
2256 SV* sv
2257 ALIAS:
2258 utf16_to_utf8_reversed = 1
2259 PREINIT:
2260 STRLEN len;
2261 U8 *source;
2262 SV *dest;
2263 I32 got; /* Gah, badly thought out APIs */
2264 CODE:
2265 source = (U8 *)SvPVbyte(sv, len);
2266 /* Optionally only convert part of the buffer. */
2267 if (items > 1) {
2268 len = SvUV(ST(1));
2269 }
2270 /* Mortalise this right now, as we'll be testing croak()s */
2271 dest = sv_2mortal(newSV(len * 3 / 2 + 1));
2272 if (ix) {
25f2e844 2273 utf16_to_utf8_reversed(source, (U8 *)SvPVX(dest), len, &got);
30685b56 2274 } else {
25f2e844 2275 utf16_to_utf8(source, (U8 *)SvPVX(dest), len, &got);
30685b56
NC
2276 }
2277 SvCUR_set(dest, got);
2278 SvPVX(dest)[got] = '\0';
2279 SvPOK_on(dest);
2280 ST(0) = dest;
2281 XSRETURN(1);
879d0c72 2282
6bd7445c
GG
2283void
2284my_exit(int exitcode)
2285 PPCODE:
2286 my_exit(exitcode);
d97c33b5 2287
209e41dc
NT
2288U8
2289first_byte(sv)
2290 SV *sv
2291 CODE:
2292 char *s;
2293 STRLEN len;
2294 s = SvPVbyte(sv, len);
2295 RETVAL = s[0];
2296 OUTPUT:
2297 RETVAL
2298
d97c33b5
DM
2299I32
2300sv_count()
2301 CODE:
2302 RETVAL = PL_sv_count;
2303 OUTPUT:
2304 RETVAL
13b6b3bc
BM
2305
2306void
2307bhk_record(bool on)
2308 CODE:
2309 dMY_CXT;
2310 MY_CXT.bhk_record = on;
2311 if (on)
2312 av_clear(MY_CXT.bhkav);
65bfe90c 2313
defdfed5 2314void
d9088386
Z
2315test_magic_chain()
2316 PREINIT:
2317 SV *sv;
2318 MAGIC *callmg, *uvarmg;
2319 CODE:
2320 sv = sv_2mortal(newSV(0));
11f9f0ed
NC
2321 if (SvTYPE(sv) >= SVt_PVMG) croak_fail();
2322 if (SvMAGICAL(sv)) croak_fail();
d9088386 2323 sv_magic(sv, &PL_sv_yes, PERL_MAGIC_checkcall, (char*)&callmg, 0);
11f9f0ed
NC
2324 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2325 if (!SvMAGICAL(sv)) croak_fail();
2326 if (mg_find(sv, PERL_MAGIC_uvar)) croak_fail();
d9088386 2327 callmg = mg_find(sv, PERL_MAGIC_checkcall);
11f9f0ed 2328 if (!callmg) croak_fail();
d9088386 2329 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 2330 croak_fail();
d9088386 2331 sv_magic(sv, &PL_sv_no, PERL_MAGIC_uvar, (char*)&uvarmg, 0);
11f9f0ed
NC
2332 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2333 if (!SvMAGICAL(sv)) croak_fail();
2334 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
d9088386 2335 uvarmg = mg_find(sv, PERL_MAGIC_uvar);
11f9f0ed 2336 if (!uvarmg) croak_fail();
d9088386 2337 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 2338 croak_fail();
d9088386 2339 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 2340 croak_fail();
d9088386 2341 mg_free_type(sv, PERL_MAGIC_vec);
11f9f0ed
NC
2342 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2343 if (!SvMAGICAL(sv)) croak_fail();
2344 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
2345 if (mg_find(sv, PERL_MAGIC_uvar) != uvarmg) croak_fail();
d9088386 2346 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 2347 croak_fail();
d9088386 2348 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 2349 croak_fail();
d9088386 2350 mg_free_type(sv, PERL_MAGIC_uvar);
11f9f0ed
NC
2351 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2352 if (!SvMAGICAL(sv)) croak_fail();
2353 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
2354 if (mg_find(sv, PERL_MAGIC_uvar)) croak_fail();
d9088386 2355 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 2356 croak_fail();
d9088386 2357 sv_magic(sv, &PL_sv_no, PERL_MAGIC_uvar, (char*)&uvarmg, 0);
11f9f0ed
NC
2358 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2359 if (!SvMAGICAL(sv)) croak_fail();
2360 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
d9088386 2361 uvarmg = mg_find(sv, PERL_MAGIC_uvar);
11f9f0ed 2362 if (!uvarmg) croak_fail();
d9088386 2363 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 2364 croak_fail();
d9088386 2365 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 2366 croak_fail();
d9088386 2367 mg_free_type(sv, PERL_MAGIC_checkcall);
11f9f0ed
NC
2368 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2369 if (!SvMAGICAL(sv)) croak_fail();
2370 if (mg_find(sv, PERL_MAGIC_uvar) != uvarmg) croak_fail();
2371 if (mg_find(sv, PERL_MAGIC_checkcall)) croak_fail();
d9088386 2372 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 2373 croak_fail();
d9088386 2374 mg_free_type(sv, PERL_MAGIC_uvar);
11f9f0ed
NC
2375 if (SvMAGICAL(sv)) croak_fail();
2376 if (mg_find(sv, PERL_MAGIC_checkcall)) croak_fail();
2377 if (mg_find(sv, PERL_MAGIC_uvar)) croak_fail();
d9088386
Z
2378
2379void
2380test_op_contextualize()
2381 PREINIT:
2382 OP *o;
2383 CODE:
2384 o = newSVOP(OP_CONST, 0, newSViv(0));
2385 o->op_flags &= ~OPf_WANT;
2386 o = op_contextualize(o, G_SCALAR);
2387 if (o->op_type != OP_CONST ||
2388 (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR)
11f9f0ed 2389 croak_fail();
d9088386
Z
2390 op_free(o);
2391 o = newSVOP(OP_CONST, 0, newSViv(0));
2392 o->op_flags &= ~OPf_WANT;
2393 o = op_contextualize(o, G_ARRAY);
2394 if (o->op_type != OP_CONST ||
2395 (o->op_flags & OPf_WANT) != OPf_WANT_LIST)
11f9f0ed 2396 croak_fail();
d9088386
Z
2397 op_free(o);
2398 o = newSVOP(OP_CONST, 0, newSViv(0));
2399 o->op_flags &= ~OPf_WANT;
2400 o = op_contextualize(o, G_VOID);
11f9f0ed 2401 if (o->op_type != OP_NULL) croak_fail();
d9088386
Z
2402 op_free(o);
2403
2404void
2405test_rv2cv_op_cv()
2406 PROTOTYPE:
2407 PREINIT:
2408 GV *troc_gv, *wibble_gv;
2409 CV *troc_cv;
2410 OP *o;
2411 CODE:
2412 troc_gv = gv_fetchpv("XS::APItest::test_rv2cv_op_cv", 0, SVt_PVGV);
2413 troc_cv = get_cv("XS::APItest::test_rv2cv_op_cv", 0);
2414 wibble_gv = gv_fetchpv("XS::APItest::wibble", 0, SVt_PVGV);
2415 o = newCVREF(0, newGVOP(OP_GV, 0, troc_gv));
11f9f0ed 2416 if (rv2cv_op_cv(o, 0) != troc_cv) croak_fail();
d9088386 2417 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV) != (CV*)troc_gv)
11f9f0ed 2418 croak_fail();
d9088386 2419 o->op_private |= OPpENTERSUB_AMPER;
11f9f0ed
NC
2420 if (rv2cv_op_cv(o, 0)) croak_fail();
2421 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386 2422 o->op_private &= ~OPpENTERSUB_AMPER;
11f9f0ed
NC
2423 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
2424 if (rv2cv_op_cv(o, RV2CVOPCV_MARK_EARLY) != troc_cv) croak_fail();
2425 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
d9088386
Z
2426 op_free(o);
2427 o = newSVOP(OP_CONST, 0, newSVpv("XS::APItest::test_rv2cv_op_cv", 0));
2428 o->op_private = OPpCONST_BARE;
2429 o = newCVREF(0, o);
11f9f0ed 2430 if (rv2cv_op_cv(o, 0) != troc_cv) croak_fail();
d9088386 2431 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV) != (CV*)troc_gv)
11f9f0ed 2432 croak_fail();
d9088386 2433 o->op_private |= OPpENTERSUB_AMPER;
11f9f0ed
NC
2434 if (rv2cv_op_cv(o, 0)) croak_fail();
2435 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386
Z
2436 op_free(o);
2437 o = newCVREF(0, newSVOP(OP_CONST, 0, newRV_inc((SV*)troc_cv)));
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 = newCVREF(0, newUNOP(OP_RAND, 0, newSVOP(OP_CONST, 0, newSViv(0))));
11f9f0ed
NC
2450 if (rv2cv_op_cv(o, 0)) croak_fail();
2451 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386 2452 o->op_private |= OPpENTERSUB_AMPER;
11f9f0ed
NC
2453 if (rv2cv_op_cv(o, 0)) croak_fail();
2454 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386 2455 o->op_private &= ~OPpENTERSUB_AMPER;
11f9f0ed
NC
2456 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
2457 if (rv2cv_op_cv(o, RV2CVOPCV_MARK_EARLY)) croak_fail();
2458 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
d9088386
Z
2459 op_free(o);
2460 o = newUNOP(OP_RAND, 0, newSVOP(OP_CONST, 0, newSViv(0)));
11f9f0ed
NC
2461 if (rv2cv_op_cv(o, 0)) croak_fail();
2462 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386
Z
2463 op_free(o);
2464
2465void
2466test_cv_getset_call_checker()
2467 PREINIT:
2468 CV *troc_cv, *tsh_cv;
2469 Perl_call_checker ckfun;
2470 SV *ckobj;
2471 CODE:
2472#define check_cc(cv, xckfun, xckobj) \
2473 do { \
2474 cv_get_call_checker((cv), &ckfun, &ckobj); \
779bc08a
NC
2475 if (ckfun != (xckfun)) croak_fail_ne(FPTR2DPTR(void *, ckfun), xckfun); \
2476 if (ckobj != (xckobj)) croak_fail_ne(FPTR2DPTR(void *, ckobj), xckobj); \
d9088386
Z
2477 } while(0)
2478 troc_cv = get_cv("XS::APItest::test_rv2cv_op_cv", 0);
2479 tsh_cv = get_cv("XS::APItest::test_savehints", 0);
2480 check_cc(troc_cv, Perl_ck_entersub_args_proto_or_list, (SV*)troc_cv);
2481 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv);
2482 cv_set_call_checker(tsh_cv, Perl_ck_entersub_args_proto_or_list,
2483 &PL_sv_yes);
2484 check_cc(troc_cv, Perl_ck_entersub_args_proto_or_list, (SV*)troc_cv);
2485 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, &PL_sv_yes);
2486 cv_set_call_checker(troc_cv, THX_ck_entersub_args_scalars, &PL_sv_no);
2487 check_cc(troc_cv, THX_ck_entersub_args_scalars, &PL_sv_no);
2488 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, &PL_sv_yes);
2489 cv_set_call_checker(tsh_cv, Perl_ck_entersub_args_proto_or_list,
2490 (SV*)tsh_cv);
2491 check_cc(troc_cv, THX_ck_entersub_args_scalars, &PL_sv_no);
2492 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv);
2493 cv_set_call_checker(troc_cv, Perl_ck_entersub_args_proto_or_list,
2494 (SV*)troc_cv);
2495 check_cc(troc_cv, Perl_ck_entersub_args_proto_or_list, (SV*)troc_cv);
2496 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv);
11f9f0ed
NC
2497 if (SvMAGICAL((SV*)troc_cv) || SvMAGIC((SV*)troc_cv)) croak_fail();
2498 if (SvMAGICAL((SV*)tsh_cv) || SvMAGIC((SV*)tsh_cv)) croak_fail();
d9088386
Z
2499#undef check_cc
2500
2501void
2502cv_set_call_checker_lists(CV *cv)
2503 CODE:
2504 cv_set_call_checker(cv, THX_ck_entersub_args_lists, &PL_sv_undef);
2505
2506void
2507cv_set_call_checker_scalars(CV *cv)
2508 CODE:
2509 cv_set_call_checker(cv, THX_ck_entersub_args_scalars, &PL_sv_undef);
2510
2511void
2512cv_set_call_checker_proto(CV *cv, SV *proto)
2513 CODE:
2514 if (SvROK(proto))
2515 proto = SvRV(proto);
2516 cv_set_call_checker(cv, Perl_ck_entersub_args_proto, proto);
2517
2518void
2519cv_set_call_checker_proto_or_list(CV *cv, SV *proto)
2520 CODE:
2521 if (SvROK(proto))
2522 proto = SvRV(proto);
2523 cv_set_call_checker(cv, Perl_ck_entersub_args_proto_or_list, proto);
2524
2525void
2526cv_set_call_checker_multi_sum(CV *cv)
2527 CODE:
2528 cv_set_call_checker(cv, THX_ck_entersub_multi_sum, &PL_sv_undef);
2529
2530void
20439bc7
Z
2531test_cophh()
2532 PREINIT:
2533 COPHH *a, *b;
2534 CODE:
2535#define check_ph(EXPR) \
2536 do { if((EXPR) != &PL_sv_placeholder) croak("fail"); } while(0)
2537#define check_iv(EXPR, EXPECT) \
2538 do { if(SvIV(EXPR) != (EXPECT)) croak("fail"); } while(0)
2539#define msvpvs(STR) sv_2mortal(newSVpvs(STR))
2540#define msviv(VALUE) sv_2mortal(newSViv(VALUE))
2541 a = cophh_new_empty();
2542 check_ph(cophh_fetch_pvn(a, "foo_1", 5, 0, 0));
2543 check_ph(cophh_fetch_pvs(a, "foo_1", 0));
2544 check_ph(cophh_fetch_pv(a, "foo_1", 0, 0));
2545 check_ph(cophh_fetch_sv(a, msvpvs("foo_1"), 0, 0));
2546 a = cophh_store_pvn(a, "foo_1abc", 5, 0, msviv(111), 0);
2547 a = cophh_store_pvs(a, "foo_2", msviv(222), 0);
2548 a = cophh_store_pv(a, "foo_3", 0, msviv(333), 0);
2549 a = cophh_store_sv(a, msvpvs("foo_4"), 0, msviv(444), 0);
2550 check_iv(cophh_fetch_pvn(a, "foo_1xyz", 5, 0, 0), 111);
2551 check_iv(cophh_fetch_pvs(a, "foo_1", 0), 111);
2552 check_iv(cophh_fetch_pv(a, "foo_1", 0, 0), 111);
2553 check_iv(cophh_fetch_sv(a, msvpvs("foo_1"), 0, 0), 111);
2554 check_iv(cophh_fetch_pvs(a, "foo_2", 0), 222);
2555 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
2556 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
2557 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
2558 b = cophh_copy(a);
2559 b = cophh_store_pvs(b, "foo_1", msviv(1111), 0);
2560 check_iv(cophh_fetch_pvs(a, "foo_1", 0), 111);
2561 check_iv(cophh_fetch_pvs(a, "foo_2", 0), 222);
2562 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
2563 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
2564 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
2565 check_iv(cophh_fetch_pvs(b, "foo_1", 0), 1111);
2566 check_iv(cophh_fetch_pvs(b, "foo_2", 0), 222);
2567 check_iv(cophh_fetch_pvs(b, "foo_3", 0), 333);
2568 check_iv(cophh_fetch_pvs(b, "foo_4", 0), 444);
2569 check_ph(cophh_fetch_pvs(b, "foo_5", 0));
2570 a = cophh_delete_pvn(a, "foo_1abc", 5, 0, 0);
2571 a = cophh_delete_pvs(a, "foo_2", 0);
2572 b = cophh_delete_pv(b, "foo_3", 0, 0);
2573 b = cophh_delete_sv(b, msvpvs("foo_4"), 0, 0);
2574 check_ph(cophh_fetch_pvs(a, "foo_1", 0));
2575 check_ph(cophh_fetch_pvs(a, "foo_2", 0));
2576 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
2577 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
2578 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
2579 check_iv(cophh_fetch_pvs(b, "foo_1", 0), 1111);
2580 check_iv(cophh_fetch_pvs(b, "foo_2", 0), 222);
2581 check_ph(cophh_fetch_pvs(b, "foo_3", 0));
2582 check_ph(cophh_fetch_pvs(b, "foo_4", 0));
2583 check_ph(cophh_fetch_pvs(b, "foo_5", 0));
2584 b = cophh_delete_pvs(b, "foo_3", 0);
2585 b = cophh_delete_pvs(b, "foo_5", 0);
2586 check_iv(cophh_fetch_pvs(b, "foo_1", 0), 1111);
2587 check_iv(cophh_fetch_pvs(b, "foo_2", 0), 222);
2588 check_ph(cophh_fetch_pvs(b, "foo_3", 0));
2589 check_ph(cophh_fetch_pvs(b, "foo_4", 0));
2590 check_ph(cophh_fetch_pvs(b, "foo_5", 0));
2591 cophh_free(b);
2592 check_ph(cophh_fetch_pvs(a, "foo_1", 0));
2593 check_ph(cophh_fetch_pvs(a, "foo_2", 0));
2594 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
2595 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
2596 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
2597 a = cophh_store_pvs(a, "foo_1", msviv(11111), COPHH_KEY_UTF8);
2598 a = cophh_store_pvs(a, "foo_\xaa", msviv(123), 0);
2599 a = cophh_store_pvs(a, "foo_\xc2\xbb", msviv(456), COPHH_KEY_UTF8);
2600 a = cophh_store_pvs(a, "foo_\xc3\x8c", msviv(789), COPHH_KEY_UTF8);
2601 a = cophh_store_pvs(a, "foo_\xd9\xa6", msviv(666), COPHH_KEY_UTF8);
2602 check_iv(cophh_fetch_pvs(a, "foo_1", 0), 11111);
2603 check_iv(cophh_fetch_pvs(a, "foo_1", COPHH_KEY_UTF8), 11111);
2604 check_iv(cophh_fetch_pvs(a, "foo_\xaa", 0), 123);
2605 check_iv(cophh_fetch_pvs(a, "foo_\xc2\xaa", COPHH_KEY_UTF8), 123);
2606 check_ph(cophh_fetch_pvs(a, "foo_\xc2\xaa", 0));
2607 check_iv(cophh_fetch_pvs(a, "foo_\xbb", 0), 456);
2608 check_iv(cophh_fetch_pvs(a, "foo_\xc2\xbb", COPHH_KEY_UTF8), 456);
2609 check_ph(cophh_fetch_pvs(a, "foo_\xc2\xbb", 0));
2610 check_iv(cophh_fetch_pvs(a, "foo_\xcc", 0), 789);
2611 check_iv(cophh_fetch_pvs(a, "foo_\xc3\x8c", COPHH_KEY_UTF8), 789);
2612 check_ph(cophh_fetch_pvs(a, "foo_\xc2\x8c", 0));
2613 check_iv(cophh_fetch_pvs(a, "foo_\xd9\xa6", COPHH_KEY_UTF8), 666);
2614 check_ph(cophh_fetch_pvs(a, "foo_\xd9\xa6", 0));
3987a177
Z
2615 ENTER;
2616 SAVEFREECOPHH(a);
2617 LEAVE;
20439bc7
Z
2618#undef check_ph
2619#undef check_iv
2620#undef msvpvs
2621#undef msviv
2622
8375c93e
RU
2623void
2624test_coplabel()
2625 PREINIT:
2626 COP *cop;
32708f0b
CB
2627 const char *label;
2628 STRLEN len;
2629 U32 utf8;
8375c93e
RU
2630 CODE:
2631 cop = &PL_compiling;
32708f0b
CB
2632 Perl_cop_store_label(aTHX_ cop, "foo", 3, 0);
2633 label = Perl_cop_fetch_label(aTHX_ cop, &len, &utf8);
2634 if (strcmp(label,"foo")) croak("fail # cop_fetch_label label");
2635 if (len != 3) croak("fail # cop_fetch_label len");
2636 if (utf8) croak("fail # cop_fetch_label utf8");
8375c93e 2637 /* SMALL GERMAN UMLAUT A */
32708f0b
CB
2638 Perl_cop_store_label(aTHX_ cop, "foä", 4, SVf_UTF8);
2639 label = Perl_cop_fetch_label(aTHX_ cop, &len, &utf8);
2640 if (strcmp(label,"foä")) croak("fail # cop_fetch_label label");
72024711 2641 if (len != 4) croak("fail # cop_fetch_label len");
32708f0b 2642 if (!utf8) croak("fail # cop_fetch_label utf8");
8375c93e
RU
2643
2644
20439bc7
Z
2645HV *
2646example_cophh_2hv()
2647 PREINIT:
2648 COPHH *a;
2649 CODE:
2650#define msviv(VALUE) sv_2mortal(newSViv(VALUE))
2651 a = cophh_new_empty();
2652 a = cophh_store_pvs(a, "foo_0", msviv(999), 0);
2653 a = cophh_store_pvs(a, "foo_1", msviv(111), 0);
2654 a = cophh_store_pvs(a, "foo_\xaa", msviv(123), 0);
2655 a = cophh_store_pvs(a, "foo_\xc2\xbb", msviv(456), COPHH_KEY_UTF8);
2656 a = cophh_store_pvs(a, "foo_\xc3\x8c", msviv(789), COPHH_KEY_UTF8);
2657 a = cophh_store_pvs(a, "foo_\xd9\xa6", msviv(666), COPHH_KEY_UTF8);
2658 a = cophh_delete_pvs(a, "foo_0", 0);
2659 a = cophh_delete_pvs(a, "foo_2", 0);
2660 RETVAL = cophh_2hv(a, 0);
2661 cophh_free(a);
2662#undef msviv
2663 OUTPUT:
2664 RETVAL
2665
2666void
defdfed5
Z
2667test_savehints()
2668 PREINIT:
2669 SV **svp, *sv;
2670 CODE:
2671#define store_hint(KEY, VALUE) \
2672 sv_setiv_mg(*hv_fetchs(GvHV(PL_hintgv), KEY, 1), (VALUE))
2673#define hint_ok(KEY, EXPECT) \
2674 ((svp = hv_fetchs(GvHV(PL_hintgv), KEY, 0)) && \
2675 (sv = *svp) && SvIV(sv) == (EXPECT) && \
20439bc7 2676 (sv = cop_hints_fetch_pvs(&PL_compiling, KEY, 0)) && \
defdfed5
Z
2677 SvIV(sv) == (EXPECT))
2678#define check_hint(KEY, EXPECT) \
11f9f0ed 2679 do { if (!hint_ok(KEY, EXPECT)) croak_fail(); } while(0)
defdfed5
Z
2680 PL_hints |= HINT_LOCALIZE_HH;
2681 ENTER;
2682 SAVEHINTS();
2683 PL_hints &= HINT_INTEGER;
2684 store_hint("t0", 123);
2685 store_hint("t1", 456);
11f9f0ed 2686 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2687 check_hint("t0", 123); check_hint("t1", 456);
2688 ENTER;
2689 SAVEHINTS();
11f9f0ed 2690 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2691 check_hint("t0", 123); check_hint("t1", 456);
2692 PL_hints |= HINT_INTEGER;
2693 store_hint("t0", 321);
11f9f0ed 2694 if (!(PL_hints & HINT_INTEGER)) croak_fail();
defdfed5
Z
2695 check_hint("t0", 321); check_hint("t1", 456);
2696 LEAVE;
11f9f0ed 2697 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2698 check_hint("t0", 123); check_hint("t1", 456);
2699 ENTER;
2700 SAVEHINTS();
11f9f0ed 2701 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2702 check_hint("t0", 123); check_hint("t1", 456);
2703 store_hint("t1", 654);
11f9f0ed 2704 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2705 check_hint("t0", 123); check_hint("t1", 654);
2706 LEAVE;
11f9f0ed 2707 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2708 check_hint("t0", 123); check_hint("t1", 456);
2709 LEAVE;
2710#undef store_hint
2711#undef hint_ok
2712#undef check_hint
2713
2714void
2715test_copyhints()
2716 PREINIT:
2717 HV *a, *b;
2718 CODE:
2719 PL_hints |= HINT_LOCALIZE_HH;
2720 ENTER;
2721 SAVEHINTS();
2722 sv_setiv_mg(*hv_fetchs(GvHV(PL_hintgv), "t0", 1), 123);
20439bc7
Z
2723 if (SvIV(cop_hints_fetch_pvs(&PL_compiling, "t0", 0)) != 123)
2724 croak_fail();
defdfed5
Z
2725 a = newHVhv(GvHV(PL_hintgv));
2726 sv_2mortal((SV*)a);
2727 sv_setiv_mg(*hv_fetchs(a, "t0", 1), 456);
20439bc7
Z
2728 if (SvIV(cop_hints_fetch_pvs(&PL_compiling, "t0", 0)) != 123)
2729 croak_fail();
defdfed5
Z
2730 b = hv_copy_hints_hv(a);
2731 sv_2mortal((SV*)b);
2732 sv_setiv_mg(*hv_fetchs(b, "t0", 1), 789);
20439bc7
Z
2733 if (SvIV(cop_hints_fetch_pvs(&PL_compiling, "t0", 0)) != 789)
2734 croak_fail();
defdfed5
Z
2735 LEAVE;
2736
201c7e1f 2737void
2fcb4757
Z
2738test_op_list()
2739 PREINIT:
2740 OP *a;
2741 CODE:
2742#define iv_op(iv) newSVOP(OP_CONST, 0, newSViv(iv))
2743#define check_op(o, expect) \
2744 do { \
2745 if (strcmp(test_op_list_describe(o), (expect))) \
2746 croak("fail %s %s", test_op_list_describe(o), (expect)); \
2747 } while(0)
2748 a = op_append_elem(OP_LIST, NULL, NULL);
2749 check_op(a, "");
2750 a = op_append_elem(OP_LIST, iv_op(1), a);
2751 check_op(a, "const(1).");
2752 a = op_append_elem(OP_LIST, NULL, a);
2753 check_op(a, "const(1).");
2754 a = op_append_elem(OP_LIST, a, iv_op(2));
2755 check_op(a, "list[pushmark.const(1).const(2).]");
2756 a = op_append_elem(OP_LIST, a, iv_op(3));
2757 check_op(a, "list[pushmark.const(1).const(2).const(3).]");
2758 a = op_append_elem(OP_LIST, a, NULL);
2759 check_op(a, "list[pushmark.const(1).const(2).const(3).]");
2760 a = op_append_elem(OP_LIST, NULL, a);
2761 check_op(a, "list[pushmark.const(1).const(2).const(3).]");
2762 a = op_append_elem(OP_LIST, iv_op(4), a);
2763 check_op(a, "list[pushmark.const(4)."
2764 "list[pushmark.const(1).const(2).const(3).]]");
2765 a = op_append_elem(OP_LIST, a, iv_op(5));
2766 check_op(a, "list[pushmark.const(4)."
2767 "list[pushmark.const(1).const(2).const(3).]const(5).]");
2768 a = op_append_elem(OP_LIST, a,
2769 op_append_elem(OP_LIST, iv_op(7), iv_op(6)));
2770 check_op(a, "list[pushmark.const(4)."
2771 "list[pushmark.const(1).const(2).const(3).]const(5)."
2772 "list[pushmark.const(7).const(6).]]");
2773 op_free(a);
2774 a = op_append_elem(OP_LINESEQ, iv_op(1), iv_op(2));
2775 check_op(a, "lineseq[const(1).const(2).]");
2776 a = op_append_elem(OP_LINESEQ, a, iv_op(3));
2777 check_op(a, "lineseq[const(1).const(2).const(3).]");
2778 op_free(a);
2779 a = op_append_elem(OP_LINESEQ,
2780 op_append_elem(OP_LIST, iv_op(1), iv_op(2)),
2781 iv_op(3));
2782 check_op(a, "lineseq[list[pushmark.const(1).const(2).]const(3).]");
2783 op_free(a);
2784 a = op_prepend_elem(OP_LIST, NULL, NULL);
2785 check_op(a, "");
2786 a = op_prepend_elem(OP_LIST, a, iv_op(1));
2787 check_op(a, "const(1).");
2788 a = op_prepend_elem(OP_LIST, a, NULL);
2789 check_op(a, "const(1).");
2790 a = op_prepend_elem(OP_LIST, iv_op(2), a);
2791 check_op(a, "list[pushmark.const(2).const(1).]");
2792 a = op_prepend_elem(OP_LIST, iv_op(3), a);
2793 check_op(a, "list[pushmark.const(3).const(2).const(1).]");
2794 a = op_prepend_elem(OP_LIST, NULL, a);
2795 check_op(a, "list[pushmark.const(3).const(2).const(1).]");
2796 a = op_prepend_elem(OP_LIST, a, NULL);
2797 check_op(a, "list[pushmark.const(3).const(2).const(1).]");
2798 a = op_prepend_elem(OP_LIST, a, iv_op(4));
2799 check_op(a, "list[pushmark."
2800 "list[pushmark.const(3).const(2).const(1).]const(4).]");
2801 a = op_prepend_elem(OP_LIST, iv_op(5), a);
2802 check_op(a, "list[pushmark.const(5)."
2803 "list[pushmark.const(3).const(2).const(1).]const(4).]");
2804 a = op_prepend_elem(OP_LIST,
2805 op_prepend_elem(OP_LIST, iv_op(6), iv_op(7)), a);
2806 check_op(a, "list[pushmark.list[pushmark.const(6).const(7).]const(5)."
2807 "list[pushmark.const(3).const(2).const(1).]const(4).]");
2808 op_free(a);
2809 a = op_prepend_elem(OP_LINESEQ, iv_op(2), iv_op(1));
2810 check_op(a, "lineseq[const(2).const(1).]");
2811 a = op_prepend_elem(OP_LINESEQ, iv_op(3), a);
2812 check_op(a, "lineseq[const(3).const(2).const(1).]");
2813 op_free(a);
2814 a = op_prepend_elem(OP_LINESEQ, iv_op(3),
2815 op_prepend_elem(OP_LIST, iv_op(2), iv_op(1)));
2816 check_op(a, "lineseq[const(3).list[pushmark.const(2).const(1).]]");
2817 op_free(a);
2818 a = op_append_list(OP_LINESEQ, NULL, NULL);
2819 check_op(a, "");
2820 a = op_append_list(OP_LINESEQ, iv_op(1), a);
2821 check_op(a, "const(1).");
2822 a = op_append_list(OP_LINESEQ, NULL, a);
2823 check_op(a, "const(1).");
2824 a = op_append_list(OP_LINESEQ, a, iv_op(2));
2825 check_op(a, "lineseq[const(1).const(2).]");
2826 a = op_append_list(OP_LINESEQ, a, iv_op(3));
2827 check_op(a, "lineseq[const(1).const(2).const(3).]");
2828 a = op_append_list(OP_LINESEQ, iv_op(4), a);
2829 check_op(a, "lineseq[const(4).const(1).const(2).const(3).]");
2830 a = op_append_list(OP_LINESEQ, a, NULL);
2831 check_op(a, "lineseq[const(4).const(1).const(2).const(3).]");
2832 a = op_append_list(OP_LINESEQ, NULL, a);
2833 check_op(a, "lineseq[const(4).const(1).const(2).const(3).]");
2834 a = op_append_list(OP_LINESEQ, a,
2835 op_append_list(OP_LINESEQ, iv_op(5), iv_op(6)));
2836 check_op(a, "lineseq[const(4).const(1).const(2).const(3)."
2837 "const(5).const(6).]");
2838 op_free(a);
2839 a = op_append_list(OP_LINESEQ,
2840 op_append_list(OP_LINESEQ, iv_op(1), iv_op(2)),
2841 op_append_list(OP_LIST, iv_op(3), iv_op(4)));
2842 check_op(a, "lineseq[const(1).const(2)."
2843 "list[pushmark.const(3).const(4).]]");
2844 op_free(a);
2845 a = op_append_list(OP_LINESEQ,
2846 op_append_list(OP_LIST, iv_op(1), iv_op(2)),
2847 op_append_list(OP_LINESEQ, iv_op(3), iv_op(4)));
2848 check_op(a, "lineseq[list[pushmark.const(1).const(2).]"
2849 "const(3).const(4).]");
2850 op_free(a);
2fcb4757
Z
2851#undef check_op
2852
2853void
5983a79d
BM
2854test_op_linklist ()
2855 PREINIT:
2856 OP *o;
2857 CODE:
2858#define check_ll(o, expect) \
2859 STMT_START { \
2860 if (strNE(test_op_linklist_describe(o), (expect))) \
2861 croak("fail %s %s", test_op_linklist_describe(o), (expect)); \
2862 } STMT_END
2863 o = iv_op(1);
2864 check_ll(o, ".const1");
2865 op_free(o);
2866
2867 o = mkUNOP(OP_NOT, iv_op(1));
2868 check_ll(o, ".const1.not");
2869 op_free(o);
2870
2871 o = mkUNOP(OP_NOT, mkUNOP(OP_NEGATE, iv_op(1)));
2872 check_ll(o, ".const1.negate.not");
2873 op_free(o);
2874
2875 o = mkBINOP(OP_ADD, iv_op(1), iv_op(2));
2876 check_ll(o, ".const1.const2.add");
2877 op_free(o);
2878
2879 o = mkBINOP(OP_ADD, mkUNOP(OP_NOT, iv_op(1)), iv_op(2));
2880 check_ll(o, ".const1.not.const2.add");
2881 op_free(o);
2882
2883 o = mkUNOP(OP_NOT, mkBINOP(OP_ADD, iv_op(1), iv_op(2)));
2884 check_ll(o, ".const1.const2.add.not");
2885 op_free(o);
2886
2887 o = mkLISTOP(OP_LINESEQ, iv_op(1), iv_op(2), iv_op(3));
2888 check_ll(o, ".const1.const2.const3.lineseq");
2889 op_free(o);
2890
2891 o = mkLISTOP(OP_LINESEQ,
2892 mkBINOP(OP_ADD, iv_op(1), iv_op(2)),
2893 mkUNOP(OP_NOT, iv_op(3)),
2894 mkLISTOP(OP_SUBSTR, iv_op(4), iv_op(5), iv_op(6)));
2895 check_ll(o, ".const1.const2.add.const3.not"
2896 ".const4.const5.const6.substr.lineseq");
2897 op_free(o);
2898
2899 o = mkBINOP(OP_ADD, iv_op(1), iv_op(2));
2900 LINKLIST(o);
2901 o = mkBINOP(OP_SUBTRACT, o, iv_op(3));
2902 check_ll(o, ".const1.const2.add.const3.subtract");
2903 op_free(o);
2904#undef check_ll
2905#undef iv_op
2906
2907void
201c7e1f
FR
2908peep_enable ()
2909 PREINIT:
2910 dMY_CXT;
2911 CODE:
2912 av_clear(MY_CXT.peep_recorder);
2913 av_clear(MY_CXT.rpeep_recorder);
2914 MY_CXT.peep_recording = 1;
2915
2916void
2917peep_disable ()
2918 PREINIT:
2919 dMY_CXT;
2920 CODE:
2921 MY_CXT.peep_recording = 0;
2922
2923SV *
2924peep_record ()
2925 PREINIT:
2926 dMY_CXT;
2927 CODE:
95d2461a 2928 RETVAL = newRV_inc((SV *)MY_CXT.peep_recorder);
201c7e1f
FR
2929 OUTPUT:
2930 RETVAL
2931
2932SV *
2933rpeep_record ()
2934 PREINIT:
2935 dMY_CXT;
2936 CODE:
95d2461a 2937 RETVAL = newRV_inc((SV *)MY_CXT.rpeep_recorder);
201c7e1f
FR
2938 OUTPUT:
2939 RETVAL
2940
9c540340
DM
2941=pod
2942
2943multicall_each: call a sub for each item in the list. Used to test MULTICALL
2944
2945=cut
2946
2947void
2948multicall_each(block,...)
2949 SV * block
2950PROTOTYPE: &@
2951CODE:
2952{
2953 dMULTICALL;
2954 int index;
2955 GV *gv;
2956 HV *stash;
2957 I32 gimme = G_SCALAR;
2958 SV **args = &PL_stack_base[ax];
2959 CV *cv;
2960
2961 if(items <= 1) {
2962 XSRETURN_UNDEF;
2963 }
2964 cv = sv_2cv(block, &stash, &gv, 0);
2965 if (cv == Nullcv) {
2966 croak("multicall_each: not a subroutine reference");
2967 }
2968 PUSH_MULTICALL(cv);
2969 SAVESPTR(GvSV(PL_defgv));
2970
2971 for(index = 1 ; index < items ; index++) {
2972 GvSV(PL_defgv) = args[index];
2973 MULTICALL;
2974 }
2975 POP_MULTICALL;
2976 XSRETURN_UNDEF;
2977}
2978
7b81e549
GG
2979#ifdef USE_ITHREADS
2980
2981void
2982clone_with_stack()
2983CODE:
2984{
2985 PerlInterpreter *interp = aTHX; /* The original interpreter */
2986 PerlInterpreter *interp_dup; /* The duplicate interpreter */
2987 int oldscope = 1; /* We are responsible for all scopes */
2988
2989 interp_dup = perl_clone(interp, CLONEf_COPY_STACKS | CLONEf_CLONE_HOST );
2990
2991 /* destroy old perl */
2992 PERL_SET_CONTEXT(interp);
2993
2994 POPSTACK_TO(PL_mainstack);
2995 dounwind(-1);
2996 LEAVE_SCOPE(0);
2997
2998 while (interp->Iscopestack_ix > 1)
2999 LEAVE;
3000 FREETMPS;
3001
3002 perl_destruct(interp);
3003 perl_free(interp);
3004
3005 /* switch to new perl */
3006 PERL_SET_CONTEXT(interp_dup);
3007
3008 /* continue after 'clone_with_stack' */
3009 interp_dup->Iop = interp_dup->Iop->op_next;
3010
3011 /* run with new perl */
3012 Perl_runops_standard(interp_dup);
3013
3014 /* We may have additional unclosed scopes if fork() was called
3015 * from within a BEGIN block. See perlfork.pod for more details.
3016 * We cannot clean up these other scopes because they belong to a
3017 * different interpreter, but we also cannot leave PL_scopestack_ix
3018 * dangling because that can trigger an assertion in perl_destruct().
3019 */
3020 if (PL_scopestack_ix > oldscope) {
3021 PL_scopestack[oldscope-1] = PL_scopestack[PL_scopestack_ix-1];
3022 PL_scopestack_ix = oldscope;
3023 }
3024
3025 perl_destruct(interp_dup);
3026 perl_free(interp_dup);
3027
3028 /* call the real 'exit' not PerlProc_exit */
3029#undef exit
3030 exit(0);
3031}
3032
3033#endif /* USE_ITHREDS */
9c540340 3034
88b5a879 3035SV*
3036take_svref(SVREF sv)
3037CODE:
3038 RETVAL = newRV_inc(sv);
3039OUTPUT:
3040 RETVAL
3041
3042SV*
3043take_avref(AV* av)
3044CODE:
3045 RETVAL = newRV_inc((SV*)av);
3046OUTPUT:
3047 RETVAL
3048
3049SV*
3050take_hvref(HV* hv)
3051CODE:
3052 RETVAL = newRV_inc((SV*)hv);
3053OUTPUT:
3054 RETVAL
3055
3056
3057SV*
3058take_cvref(CV* cv)
3059CODE:
3060 RETVAL = newRV_inc((SV*)cv);
3061OUTPUT:
3062 RETVAL
3063
3064
e2fe06dd
EB
3065BOOT:
3066 {
3067 HV* stash;
3068 SV** meth = NULL;
3069 CV* cv;
3070 stash = gv_stashpv("XS::APItest::TempLv", 0);
3071 if (stash)
3072 meth = hv_fetchs(stash, "make_temp_mg_lv", 0);
3073 if (!meth)
3074 croak("lost method 'make_temp_mg_lv'");
3075 cv = GvCV(*meth);
3076 CvLVALUE_on(cv);
3077 }
83f8bb56
Z
3078
3079BOOT:
3080{
3081 hintkey_rpn_sv = newSVpvs_share("XS::APItest/rpn");
3082 hintkey_calcrpn_sv = newSVpvs_share("XS::APItest/calcrpn");
3083 hintkey_stufftest_sv = newSVpvs_share("XS::APItest/stufftest");
3084 hintkey_swaptwostmts_sv = newSVpvs_share("XS::APItest/swaptwostmts");
07ffcb73 3085 hintkey_looprest_sv = newSVpvs_share("XS::APItest/looprest");
a7aaec61 3086 hintkey_scopelessblock_sv = newSVpvs_share("XS::APItest/scopelessblock");
9eb5c532
Z
3087 hintkey_stmtasexpr_sv = newSVpvs_share("XS::APItest/stmtasexpr");
3088 hintkey_stmtsasexpr_sv = newSVpvs_share("XS::APItest/stmtsasexpr");
e53d8f76
Z
3089 hintkey_loopblock_sv = newSVpvs_share("XS::APItest/loopblock");
3090 hintkey_blockasexpr_sv = newSVpvs_share("XS::APItest/blockasexpr");
361d9b55
Z
3091 hintkey_swaplabel_sv = newSVpvs_share("XS::APItest/swaplabel");
3092 hintkey_labelconst_sv = newSVpvs_share("XS::APItest/labelconst");
78cdf107
Z
3093 hintkey_arrayfullexpr_sv = newSVpvs_share("XS::APItest/arrayfullexpr");
3094 hintkey_arraylistexpr_sv = newSVpvs_share("XS::APItest/arraylistexpr");
3095 hintkey_arraytermexpr_sv = newSVpvs_share("XS::APItest/arraytermexpr");
3096 hintkey_arrayarithexpr_sv = newSVpvs_share("XS::APItest/arrayarithexpr");
3097 hintkey_arrayexprflags_sv = newSVpvs_share("XS::APItest/arrayexprflags");
83f8bb56
Z
3098 next_keyword_plugin = PL_keyword_plugin;
3099 PL_keyword_plugin = my_keyword_plugin;
3100}
8f89e5a9
Z
3101
3102void
3103establish_cleanup(...)
3104PROTOTYPE: $
3105CODE:
c33e8be1 3106 PERL_UNUSED_VAR(items);
8f89e5a9
Z
3107 croak("establish_cleanup called as a function");
3108
3109BOOT:
3110{
3111 CV *estcv = get_cv("XS::APItest::establish_cleanup", 0);
3112 cv_set_call_checker(estcv, THX_ck_entersub_establish_cleanup, (SV*)estcv);
3113}
3ad73efd
Z
3114
3115void
3116postinc(...)
3117PROTOTYPE: $
3118CODE:
c33e8be1 3119 PERL_UNUSED_VAR(items);
3ad73efd
Z
3120 croak("postinc called as a function");
3121
27fcb6ee
FC
3122void
3123filter()
3124CODE:
3125 filter_add(filter_call, NULL);
3126
3ad73efd
Z
3127BOOT:
3128{
3129 CV *asscv = get_cv("XS::APItest::postinc", 0);
3130 cv_set_call_checker(asscv, THX_ck_entersub_postinc, (SV*)asscv);
3131}
26ab20ee 3132
fba8e77b
FC
3133SV *
3134lv_temp_object()
3135CODE:
3136 RETVAL =
3137 sv_bless(
3138 newRV_noinc(newSV(0)),
3139 gv_stashpvs("XS::APItest::TempObj",GV_ADD)
3140 ); /* Package defined in test script */
3141OUTPUT:
3142 RETVAL
3143
7d6175ef
FC
3144void
3145fill_hash_with_nulls(HV *hv)
a2bf5ab1 3146PREINIT:
7d6175ef 3147 UV i = 0;
a2bf5ab1 3148CODE:
7d6175ef
FC
3149 for(; i < 1000; ++i) {
3150 HE *entry = hv_fetch_ent(hv, sv_2mortal(newSVuv(i)), 1, 0);
3151 SvREFCNT_dec(HeVAL(entry));
3152 HeVAL(entry) = NULL;
3153 }
3154
fa819c1c
FC
3155bool
3156SvIsCOW(SV *sv)
3157CODE:
3158 RETVAL = SvIsCOW(sv);
3159OUTPUT:
3160 RETVAL
3161
15103811
Z
3162void
3163pad_scalar(...)
3164PROTOTYPE: $$
3165CODE:
3166 PERL_UNUSED_VAR(items);
3167 croak("pad_scalar called as a function");
3168
3169BOOT:
3170{
3171 CV *pscv = get_cv("XS::APItest::pad_scalar", 0);
3172 cv_set_call_checker(pscv, THX_ck_entersub_pad_scalar, (SV*)pscv);
3173}
3174
ab8f91e9
BF
3175SV*
3176fetch_pad_names( cv )
3177CV* cv
3178 PREINIT:
3179 I32 i;
3180 AV *pad_namelist;
3181 AV *retav = newAV();
3182 CODE:
3183 pad_namelist = (AV*) *av_fetch(CvPADLIST(cv), 0, FALSE);
3184
3185 for ( i = av_len(pad_namelist); i >= 0; i-- ) {
3186 SV** name_ptr = av_fetch(pad_namelist, i, 0);
3187
3188 if (name_ptr && SvPOKp(*name_ptr)) {
3189 av_push(retav, newSVsv(*name_ptr));
3190 }
3191 }
3192 RETVAL = newRV_noinc((SV*)retav);
3193 OUTPUT:
3194 RETVAL
3195
c12735bb
Z
3196STRLEN
3197underscore_length()
3198PROTOTYPE:
3199PREINIT:
3200 SV *u;
3201 U8 *pv;
3202 STRLEN bytelen;
3203CODE:
3204 u = find_rundefsv();
3205 pv = (U8*)SvPV(u, bytelen);
3206 RETVAL = SvUTF8(u) ? utf8_length(pv, pv+bytelen) : bytelen;
3207OUTPUT:
3208 RETVAL
3209
e8ed61c5
FC
3210void
3211stringify(SV *sv)
3212PREINIT:
3213 const char *pv;
3214CODE:
3215 pv = SvPV_nolen(sv);
3216
c682ebef
FC
3217SV *
3218HvENAME(HV *hv)
3219CODE:
3220 RETVAL = hv && HvENAME(hv)
3221 ? newSVpvn_flags(
3222 HvENAME(hv),HvENAMELEN(hv),
3223 (HvENAMEUTF8(hv) ? SVf_UTF8 : 0)
3224 )
3225 : NULL;
3226OUTPUT:
3227 RETVAL
3228
e8ed61c5 3229
26ab20ee
FR
3230MODULE = XS::APItest PACKAGE = XS::APItest::Magic
3231
3232PROTOTYPES: DISABLE
3233
3234void
3235sv_magic_foo(SV *sv, SV *thingy)
3236ALIAS:
3237 sv_magic_bar = 1
3238CODE:
3239 sv_magicext(SvRV(sv), NULL, PERL_MAGIC_ext, ix ? &vtbl_bar : &vtbl_foo, (const char *)thingy, 0);
3240
3241SV *
3242mg_find_foo(SV *sv)
3243ALIAS:
3244 mg_find_bar = 1
3245CODE:
3246 MAGIC *mg = mg_findext(SvRV(sv), PERL_MAGIC_ext, ix ? &vtbl_bar : &vtbl_foo);
3247 RETVAL = mg ? SvREFCNT_inc((SV *)mg->mg_ptr) : &PL_sv_undef;
3248OUTPUT:
3249 RETVAL
3250
3251void
3252sv_unmagic_foo(SV *sv)
3253ALIAS:
3254 sv_unmagic_bar = 1
3255CODE:
3256 sv_unmagicext(SvRV(sv), PERL_MAGIC_ext, ix ? &vtbl_bar : &vtbl_foo);
406700cc
NC
3257
3258UV
3259test_get_vtbl()
3260 PREINIT:
3261 MGVTBL *have;
3262 MGVTBL *want;
3263 CODE:
3264#define test_get_this_vtable(name) \
3265 want = CAT2(&PL_vtbl_, name); \
3266 have = get_vtbl(CAT2(want_vtbl_, name)); \
3267 if (have != want) \
3268 croak("fail %p!=%p for get_vtbl(want_vtbl_" STRINGIFY(name) ") at " __FILE__ " line %d", have, want, __LINE__)
3269
3270 test_get_this_vtable(sv);
3271 test_get_this_vtable(env);
3272 test_get_this_vtable(envelem);
3273 test_get_this_vtable(sigelem);
3274 test_get_this_vtable(pack);
3275 test_get_this_vtable(packelem);
3276 test_get_this_vtable(dbline);
3277 test_get_this_vtable(isa);
3278 test_get_this_vtable(isaelem);
3279 test_get_this_vtable(arylen);
3280 test_get_this_vtable(mglob);
3281 test_get_this_vtable(nkeys);
3282 test_get_this_vtable(taint);
3283 test_get_this_vtable(substr);
3284 test_get_this_vtable(vec);
3285 test_get_this_vtable(pos);
3286 test_get_this_vtable(bm);
3287 test_get_this_vtable(fm);
3288 test_get_this_vtable(uvar);
3289 test_get_this_vtable(defelem);
3290 test_get_this_vtable(regexp);
3291 test_get_this_vtable(regdata);
3292 test_get_this_vtable(regdatum);
3293#ifdef USE_LOCALE_COLLATE
3294 test_get_this_vtable(collxfrm);
3295#endif
3296 test_get_this_vtable(amagic);
3297 test_get_this_vtable(amagicelem);
3298 test_get_this_vtable(backref);
3299 test_get_this_vtable(utf8);
3300
3301 RETVAL = PTR2UV(get_vtbl(-1));
3302 OUTPUT:
3303 RETVAL