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