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