This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
API test for find_rundefsv()
[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
83f8bb56
Z
590/** RPN keyword parser **/
591
592#define sv_is_glob(sv) (SvTYPE(sv) == SVt_PVGV)
593#define sv_is_regexp(sv) (SvTYPE(sv) == SVt_REGEXP)
594#define sv_is_string(sv) \
595 (!sv_is_glob(sv) && !sv_is_regexp(sv) && \
596 (SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK|SVp_IOK|SVp_NOK|SVp_POK)))
597
598static SV *hintkey_rpn_sv, *hintkey_calcrpn_sv, *hintkey_stufftest_sv;
07ffcb73 599static SV *hintkey_swaptwostmts_sv, *hintkey_looprest_sv;
a7aaec61 600static SV *hintkey_scopelessblock_sv;
e53d8f76
Z
601static SV *hintkey_stmtasexpr_sv, *hintkey_stmtsasexpr_sv;
602static SV *hintkey_loopblock_sv, *hintkey_blockasexpr_sv;
361d9b55 603static SV *hintkey_swaplabel_sv, *hintkey_labelconst_sv;
78cdf107
Z
604static SV *hintkey_arrayfullexpr_sv, *hintkey_arraylistexpr_sv;
605static SV *hintkey_arraytermexpr_sv, *hintkey_arrayarithexpr_sv;
606static SV *hintkey_arrayexprflags_sv;
83f8bb56
Z
607static int (*next_keyword_plugin)(pTHX_ char *, STRLEN, OP **);
608
609/* low-level parser helpers */
610
611#define PL_bufptr (PL_parser->bufptr)
612#define PL_bufend (PL_parser->bufend)
613
614/* RPN parser */
615
616#define parse_var() THX_parse_var(aTHX)
617static OP *THX_parse_var(pTHX)
618{
619 char *s = PL_bufptr;
620 char *start = s;
621 PADOFFSET varpos;
622 OP *padop;
623 if(*s != '$') croak("RPN syntax error");
624 while(1) {
625 char c = *++s;
626 if(!isALNUM(c)) break;
627 }
628 if(s-start < 2) croak("RPN syntax error");
629 lex_read_to(s);
cc76b5cc 630 varpos = pad_findmy_pvn(start, s-start, 0);
83f8bb56
Z
631 if(varpos == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(varpos))
632 croak("RPN only supports \"my\" variables");
633 padop = newOP(OP_PADSV, 0);
634 padop->op_targ = varpos;
635 return padop;
636}
637
638#define push_rpn_item(o) \
639 (tmpop = (o), tmpop->op_sibling = stack, stack = tmpop)
640#define pop_rpn_item() \
641 (!stack ? (croak("RPN stack underflow"), (OP*)NULL) : \
642 (tmpop = stack, stack = stack->op_sibling, \
643 tmpop->op_sibling = NULL, tmpop))
644
645#define parse_rpn_expr() THX_parse_rpn_expr(aTHX)
646static OP *THX_parse_rpn_expr(pTHX)
647{
648 OP *stack = NULL, *tmpop;
649 while(1) {
650 I32 c;
651 lex_read_space(0);
652 c = lex_peek_unichar(0);
653 switch(c) {
654 case /*(*/')': case /*{*/'}': {
655 OP *result = pop_rpn_item();
656 if(stack) croak("RPN expression must return a single value");
657 return result;
658 } break;
659 case '0': case '1': case '2': case '3': case '4':
660 case '5': case '6': case '7': case '8': case '9': {
661 UV val = 0;
662 do {
663 lex_read_unichar(0);
664 val = 10*val + (c - '0');
665 c = lex_peek_unichar(0);
666 } while(c >= '0' && c <= '9');
667 push_rpn_item(newSVOP(OP_CONST, 0, newSVuv(val)));
668 } break;
669 case '$': {
670 push_rpn_item(parse_var());
671 } break;
672 case '+': {
673 OP *b = pop_rpn_item();
674 OP *a = pop_rpn_item();
675 lex_read_unichar(0);
676 push_rpn_item(newBINOP(OP_I_ADD, 0, a, b));
677 } break;
678 case '-': {
679 OP *b = pop_rpn_item();
680 OP *a = pop_rpn_item();
681 lex_read_unichar(0);
682 push_rpn_item(newBINOP(OP_I_SUBTRACT, 0, a, b));
683 } break;
684 case '*': {
685 OP *b = pop_rpn_item();
686 OP *a = pop_rpn_item();
687 lex_read_unichar(0);
688 push_rpn_item(newBINOP(OP_I_MULTIPLY, 0, a, b));
689 } break;
690 case '/': {
691 OP *b = pop_rpn_item();
692 OP *a = pop_rpn_item();
693 lex_read_unichar(0);
694 push_rpn_item(newBINOP(OP_I_DIVIDE, 0, a, b));
695 } break;
696 case '%': {
697 OP *b = pop_rpn_item();
698 OP *a = pop_rpn_item();
699 lex_read_unichar(0);
700 push_rpn_item(newBINOP(OP_I_MODULO, 0, a, b));
701 } break;
702 default: {
703 croak("RPN syntax error");
704 } break;
705 }
706 }
707}
708
709#define parse_keyword_rpn() THX_parse_keyword_rpn(aTHX)
710static OP *THX_parse_keyword_rpn(pTHX)
711{
712 OP *op;
713 lex_read_space(0);
714 if(lex_peek_unichar(0) != '('/*)*/)
715 croak("RPN expression must be parenthesised");
716 lex_read_unichar(0);
717 op = parse_rpn_expr();
718 if(lex_peek_unichar(0) != /*(*/')')
719 croak("RPN expression must be parenthesised");
720 lex_read_unichar(0);
721 return op;
722}
723
724#define parse_keyword_calcrpn() THX_parse_keyword_calcrpn(aTHX)
725static OP *THX_parse_keyword_calcrpn(pTHX)
726{
727 OP *varop, *exprop;
728 lex_read_space(0);
729 varop = parse_var();
730 lex_read_space(0);
731 if(lex_peek_unichar(0) != '{'/*}*/)
732 croak("RPN expression must be braced");
733 lex_read_unichar(0);
734 exprop = parse_rpn_expr();
735 if(lex_peek_unichar(0) != /*{*/'}')
736 croak("RPN expression must be braced");
737 lex_read_unichar(0);
738 return newASSIGNOP(OPf_STACKED, varop, 0, exprop);
739}
740
741#define parse_keyword_stufftest() THX_parse_keyword_stufftest(aTHX)
742static OP *THX_parse_keyword_stufftest(pTHX)
743{
744 I32 c;
745 bool do_stuff;
746 lex_read_space(0);
747 do_stuff = lex_peek_unichar(0) == '+';
748 if(do_stuff) {
749 lex_read_unichar(0);
750 lex_read_space(0);
751 }
752 c = lex_peek_unichar(0);
753 if(c == ';') {
754 lex_read_unichar(0);
755 } else if(c != /*{*/'}') {
756 croak("syntax error");
757 }
758 if(do_stuff) lex_stuff_pvs(" ", 0);
759 return newOP(OP_NULL, 0);
760}
761
762#define parse_keyword_swaptwostmts() THX_parse_keyword_swaptwostmts(aTHX)
763static OP *THX_parse_keyword_swaptwostmts(pTHX)
764{
765 OP *a, *b;
766 a = parse_fullstmt(0);
767 b = parse_fullstmt(0);
768 if(a && b)
769 PL_hints |= HINT_BLOCK_SCOPE;
2fcb4757 770 return op_append_list(OP_LINESEQ, b, a);
83f8bb56
Z
771}
772
07ffcb73
Z
773#define parse_keyword_looprest() THX_parse_keyword_looprest(aTHX)
774static OP *THX_parse_keyword_looprest(pTHX)
775{
94bf0465
Z
776 return newWHILEOP(0, 1, NULL, newSVOP(OP_CONST, 0, &PL_sv_yes),
777 parse_stmtseq(0), NULL, 1);
07ffcb73
Z
778}
779
a7aaec61
Z
780#define parse_keyword_scopelessblock() THX_parse_keyword_scopelessblock(aTHX)
781static OP *THX_parse_keyword_scopelessblock(pTHX)
782{
783 I32 c;
784 OP *body;
785 lex_read_space(0);
786 if(lex_peek_unichar(0) != '{'/*}*/) croak("syntax error");
787 lex_read_unichar(0);
788 body = parse_stmtseq(0);
789 c = lex_peek_unichar(0);
790 if(c != /*{*/'}' && c != /*[*/']' && c != /*(*/')') croak("syntax error");
791 lex_read_unichar(0);
792 return body;
793}
794
9eb5c532
Z
795#define parse_keyword_stmtasexpr() THX_parse_keyword_stmtasexpr(aTHX)
796static OP *THX_parse_keyword_stmtasexpr(pTHX)
797{
8359b381 798 OP *o = parse_barestmt(0);
3ad73efd
Z
799 if (!o) o = newOP(OP_STUB, 0);
800 if (PL_hints & HINT_BLOCK_SCOPE) o->op_flags |= OPf_PARENS;
801 return op_scope(o);
9eb5c532
Z
802}
803
804#define parse_keyword_stmtsasexpr() THX_parse_keyword_stmtsasexpr(aTHX)
805static OP *THX_parse_keyword_stmtsasexpr(pTHX)
806{
807 OP *o;
808 lex_read_space(0);
809 if(lex_peek_unichar(0) != '{'/*}*/) croak("syntax error");
810 lex_read_unichar(0);
811 o = parse_stmtseq(0);
812 lex_read_space(0);
813 if(lex_peek_unichar(0) != /*{*/'}') croak("syntax error");
814 lex_read_unichar(0);
3ad73efd
Z
815 if (!o) o = newOP(OP_STUB, 0);
816 if (PL_hints & HINT_BLOCK_SCOPE) o->op_flags |= OPf_PARENS;
817 return op_scope(o);
9eb5c532
Z
818}
819
e53d8f76
Z
820#define parse_keyword_loopblock() THX_parse_keyword_loopblock(aTHX)
821static OP *THX_parse_keyword_loopblock(pTHX)
822{
94bf0465
Z
823 return newWHILEOP(0, 1, NULL, newSVOP(OP_CONST, 0, &PL_sv_yes),
824 parse_block(0), NULL, 1);
e53d8f76
Z
825}
826
827#define parse_keyword_blockasexpr() THX_parse_keyword_blockasexpr(aTHX)
828static OP *THX_parse_keyword_blockasexpr(pTHX)
829{
830 OP *o = parse_block(0);
3ad73efd
Z
831 if (!o) o = newOP(OP_STUB, 0);
832 if (PL_hints & HINT_BLOCK_SCOPE) o->op_flags |= OPf_PARENS;
833 return op_scope(o);
e53d8f76
Z
834}
835
361d9b55
Z
836#define parse_keyword_swaplabel() THX_parse_keyword_swaplabel(aTHX)
837static OP *THX_parse_keyword_swaplabel(pTHX)
838{
839 OP *sop = parse_barestmt(0);
840 SV *label = parse_label(PARSE_OPTIONAL);
841 if (label) sv_2mortal(label);
842 return newSTATEOP(0, label ? savepv(SvPVX(label)) : NULL, sop);
843}
844
845#define parse_keyword_labelconst() THX_parse_keyword_labelconst(aTHX)
846static OP *THX_parse_keyword_labelconst(pTHX)
847{
848 return newSVOP(OP_CONST, 0, parse_label(0));
849}
850
78cdf107
Z
851#define parse_keyword_arrayfullexpr() THX_parse_keyword_arrayfullexpr(aTHX)
852static OP *THX_parse_keyword_arrayfullexpr(pTHX)
853{
854 return newANONLIST(parse_fullexpr(0));
855}
856
857#define parse_keyword_arraylistexpr() THX_parse_keyword_arraylistexpr(aTHX)
858static OP *THX_parse_keyword_arraylistexpr(pTHX)
859{
860 return newANONLIST(parse_listexpr(0));
861}
862
863#define parse_keyword_arraytermexpr() THX_parse_keyword_arraytermexpr(aTHX)
864static OP *THX_parse_keyword_arraytermexpr(pTHX)
865{
866 return newANONLIST(parse_termexpr(0));
867}
868
869#define parse_keyword_arrayarithexpr() THX_parse_keyword_arrayarithexpr(aTHX)
870static OP *THX_parse_keyword_arrayarithexpr(pTHX)
871{
872 return newANONLIST(parse_arithexpr(0));
873}
874
875#define parse_keyword_arrayexprflags() THX_parse_keyword_arrayexprflags(aTHX)
876static OP *THX_parse_keyword_arrayexprflags(pTHX)
877{
878 U32 flags = 0;
879 I32 c;
880 OP *o;
881 lex_read_space(0);
882 c = lex_peek_unichar(0);
883 if (c != '!' && c != '?') croak("syntax error");
884 lex_read_unichar(0);
885 if (c == '?') flags |= PARSE_OPTIONAL;
886 o = parse_listexpr(flags);
887 return o ? newANONLIST(o) : newANONHASH(newOP(OP_STUB, 0));
888}
889
83f8bb56
Z
890/* plugin glue */
891
892#define keyword_active(hintkey_sv) THX_keyword_active(aTHX_ hintkey_sv)
893static int THX_keyword_active(pTHX_ SV *hintkey_sv)
894{
895 HE *he;
896 if(!GvHV(PL_hintgv)) return 0;
897 he = hv_fetch_ent(GvHV(PL_hintgv), hintkey_sv, 0,
898 SvSHARED_HASH(hintkey_sv));
899 return he && SvTRUE(HeVAL(he));
900}
901
902static int my_keyword_plugin(pTHX_
903 char *keyword_ptr, STRLEN keyword_len, OP **op_ptr)
904{
905 if(keyword_len == 3 && strnEQ(keyword_ptr, "rpn", 3) &&
906 keyword_active(hintkey_rpn_sv)) {
907 *op_ptr = parse_keyword_rpn();
908 return KEYWORD_PLUGIN_EXPR;
909 } else if(keyword_len == 7 && strnEQ(keyword_ptr, "calcrpn", 7) &&
910 keyword_active(hintkey_calcrpn_sv)) {
911 *op_ptr = parse_keyword_calcrpn();
912 return KEYWORD_PLUGIN_STMT;
913 } else if(keyword_len == 9 && strnEQ(keyword_ptr, "stufftest", 9) &&
914 keyword_active(hintkey_stufftest_sv)) {
915 *op_ptr = parse_keyword_stufftest();
916 return KEYWORD_PLUGIN_STMT;
917 } else if(keyword_len == 12 &&
918 strnEQ(keyword_ptr, "swaptwostmts", 12) &&
919 keyword_active(hintkey_swaptwostmts_sv)) {
920 *op_ptr = parse_keyword_swaptwostmts();
921 return KEYWORD_PLUGIN_STMT;
07ffcb73
Z
922 } else if(keyword_len == 8 && strnEQ(keyword_ptr, "looprest", 8) &&
923 keyword_active(hintkey_looprest_sv)) {
924 *op_ptr = parse_keyword_looprest();
925 return KEYWORD_PLUGIN_STMT;
a7aaec61
Z
926 } else if(keyword_len == 14 && strnEQ(keyword_ptr, "scopelessblock", 14) &&
927 keyword_active(hintkey_scopelessblock_sv)) {
928 *op_ptr = parse_keyword_scopelessblock();
929 return KEYWORD_PLUGIN_STMT;
9eb5c532
Z
930 } else if(keyword_len == 10 && strnEQ(keyword_ptr, "stmtasexpr", 10) &&
931 keyword_active(hintkey_stmtasexpr_sv)) {
932 *op_ptr = parse_keyword_stmtasexpr();
933 return KEYWORD_PLUGIN_EXPR;
934 } else if(keyword_len == 11 && strnEQ(keyword_ptr, "stmtsasexpr", 11) &&
935 keyword_active(hintkey_stmtsasexpr_sv)) {
936 *op_ptr = parse_keyword_stmtsasexpr();
937 return KEYWORD_PLUGIN_EXPR;
e53d8f76
Z
938 } else if(keyword_len == 9 && strnEQ(keyword_ptr, "loopblock", 9) &&
939 keyword_active(hintkey_loopblock_sv)) {
940 *op_ptr = parse_keyword_loopblock();
941 return KEYWORD_PLUGIN_STMT;
942 } else if(keyword_len == 11 && strnEQ(keyword_ptr, "blockasexpr", 11) &&
943 keyword_active(hintkey_blockasexpr_sv)) {
944 *op_ptr = parse_keyword_blockasexpr();
945 return KEYWORD_PLUGIN_EXPR;
361d9b55
Z
946 } else if(keyword_len == 9 && strnEQ(keyword_ptr, "swaplabel", 9) &&
947 keyword_active(hintkey_swaplabel_sv)) {
948 *op_ptr = parse_keyword_swaplabel();
949 return KEYWORD_PLUGIN_STMT;
950 } else if(keyword_len == 10 && strnEQ(keyword_ptr, "labelconst", 10) &&
951 keyword_active(hintkey_labelconst_sv)) {
952 *op_ptr = parse_keyword_labelconst();
953 return KEYWORD_PLUGIN_EXPR;
78cdf107
Z
954 } else if(keyword_len == 13 && strnEQ(keyword_ptr, "arrayfullexpr", 13) &&
955 keyword_active(hintkey_arrayfullexpr_sv)) {
956 *op_ptr = parse_keyword_arrayfullexpr();
957 return KEYWORD_PLUGIN_EXPR;
958 } else if(keyword_len == 13 && strnEQ(keyword_ptr, "arraylistexpr", 13) &&
959 keyword_active(hintkey_arraylistexpr_sv)) {
960 *op_ptr = parse_keyword_arraylistexpr();
961 return KEYWORD_PLUGIN_EXPR;
962 } else if(keyword_len == 13 && strnEQ(keyword_ptr, "arraytermexpr", 13) &&
963 keyword_active(hintkey_arraytermexpr_sv)) {
964 *op_ptr = parse_keyword_arraytermexpr();
965 return KEYWORD_PLUGIN_EXPR;
966 } else if(keyword_len == 14 && strnEQ(keyword_ptr, "arrayarithexpr", 14) &&
967 keyword_active(hintkey_arrayarithexpr_sv)) {
968 *op_ptr = parse_keyword_arrayarithexpr();
969 return KEYWORD_PLUGIN_EXPR;
970 } else if(keyword_len == 14 && strnEQ(keyword_ptr, "arrayexprflags", 14) &&
971 keyword_active(hintkey_arrayexprflags_sv)) {
972 *op_ptr = parse_keyword_arrayexprflags();
973 return KEYWORD_PLUGIN_EXPR;
83f8bb56
Z
974 } else {
975 return next_keyword_plugin(aTHX_ keyword_ptr, keyword_len, op_ptr);
976 }
977}
978
f568d64d
BM
979static XOP my_xop;
980
981static OP *
982pp_xop(pTHX)
983{
984 return PL_op->op_next;
985}
986
987static void
988peep_xop(pTHX_ OP *o, OP *oldop)
989{
990 dMY_CXT;
b58757d5
BM
991 av_push(MY_CXT.xop_record, newSVpvf("peep:%"UVxf, PTR2UV(o)));
992 av_push(MY_CXT.xop_record, newSVpvf("oldop:%"UVxf, PTR2UV(oldop)));
f568d64d
BM
993}
994
27fcb6ee
FC
995static I32
996filter_call(pTHX_ int idx, SV *buf_sv, int maxlen)
997{
998 SV *my_sv = FILTER_DATA(idx);
999 char *p;
1000 char *end;
1001 int n = FILTER_READ(idx + 1, buf_sv, maxlen);
1002
1003 if (n<=0) return n;
1004
1005 p = SvPV_force_nolen(buf_sv);
1006 end = p + SvCUR(buf_sv);
1007 while (p < end) {
1008 if (*p == 'o') *p = 'e';
1009 p++;
1010 }
1011 return SvCUR(buf_sv);
1012}
1013
1014
7b20c7cd 1015XS(XS_XS__APItest__XSUB_XS_VERSION_undef);
f9cc56fa 1016XS(XS_XS__APItest__XSUB_XS_VERSION_empty);
88c4b02d 1017XS(XS_XS__APItest__XSUB_XS_APIVERSION_invalid);
7b20c7cd 1018
55289a74
NC
1019#include "const-c.inc"
1020
ffe53d21 1021MODULE = XS::APItest PACKAGE = XS::APItest
0314122a 1022
55289a74
NC
1023INCLUDE: const-xs.inc
1024
ffe53d21
NC
1025INCLUDE: numeric.xs
1026
fed3ba5d
NC
1027MODULE = XS::APItest::utf8 PACKAGE = XS::APItest::utf8
1028
1029int
1030bytes_cmp_utf8(bytes, utf8)
1031 SV *bytes
1032 SV *utf8
1033 PREINIT:
1034 const U8 *b;
1035 STRLEN blen;
1036 const U8 *u;
1037 STRLEN ulen;
1038 CODE:
1039 b = (const U8 *)SvPVbyte(bytes, blen);
1040 u = (const U8 *)SvPVbyte(utf8, ulen);
1041 RETVAL = bytes_cmp_utf8(b, blen, u, ulen);
1042 OUTPUT:
1043 RETVAL
1044
7d255dc8
NC
1045MODULE = XS::APItest:Overload PACKAGE = XS::APItest::Overload
1046
c33e8be1 1047void
25a9ffce 1048amagic_deref_call(sv, what)
7d255dc8
NC
1049 SV *sv
1050 int what
1051 PPCODE:
7d255dc8 1052 /* The reference is owned by something else. */
25a9ffce 1053 PUSHs(amagic_deref_call(sv, what));
7d255dc8 1054
e89bfaa6
NC
1055# I'd certainly like to discourage the use of this macro, given that we now
1056# have amagic_deref_call
1057
c33e8be1 1058void
e89bfaa6
NC
1059tryAMAGICunDEREF_var(sv, what)
1060 SV *sv
1061 int what
1062 PPCODE:
1063 {
1064 SV **sp = &sv;
1065 switch(what) {
1066 case to_av_amg:
1067 tryAMAGICunDEREF(to_av);
1068 break;
1069 case to_cv_amg:
1070 tryAMAGICunDEREF(to_cv);
1071 break;
1072 case to_gv_amg:
1073 tryAMAGICunDEREF(to_gv);
1074 break;
1075 case to_hv_amg:
1076 tryAMAGICunDEREF(to_hv);
1077 break;
1078 case to_sv_amg:
1079 tryAMAGICunDEREF(to_sv);
1080 break;
1081 default:
1082 croak("Invalid value %d passed to tryAMAGICunDEREF_var", what);
1083 }
1084 }
1085 /* The reference is owned by something else. */
1086 PUSHs(sv);
1087
7b20c7cd
NC
1088MODULE = XS::APItest PACKAGE = XS::APItest::XSUB
1089
1090BOOT:
1091 newXS("XS::APItest::XSUB::XS_VERSION_undef", XS_XS__APItest__XSUB_XS_VERSION_undef, __FILE__);
f9cc56fa 1092 newXS("XS::APItest::XSUB::XS_VERSION_empty", XS_XS__APItest__XSUB_XS_VERSION_empty, __FILE__);
88c4b02d 1093 newXS("XS::APItest::XSUB::XS_APIVERSION_invalid", XS_XS__APItest__XSUB_XS_APIVERSION_invalid, __FILE__);
7b20c7cd
NC
1094
1095void
1096XS_VERSION_defined(...)
1097 PPCODE:
1098 XS_VERSION_BOOTCHECK;
1099 XSRETURN_EMPTY;
1100
88c4b02d
NC
1101void
1102XS_APIVERSION_valid(...)
1103 PPCODE:
1104 XS_APIVERSION_BOOTCHECK;
1105 XSRETURN_EMPTY;
1106
ffe53d21
NC
1107MODULE = XS::APItest:Hash PACKAGE = XS::APItest::Hash
1108
b54b4831
NC
1109void
1110rot13_hash(hash)
1111 HV *hash
1112 CODE:
1113 {
1114 struct ufuncs uf;
1115 uf.uf_val = rot13_key;
1116 uf.uf_set = 0;
1117 uf.uf_index = 0;
1118
1119 sv_magic((SV*)hash, NULL, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
1120 }
1121
53c40a8f
NC
1122void
1123bitflip_hash(hash)
1124 HV *hash
1125 CODE:
1126 {
1127 struct ufuncs uf;
1128 uf.uf_val = bitflip_key;
1129 uf.uf_set = 0;
1130 uf.uf_index = 0;
1131
1132 sv_magic((SV*)hash, NULL, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
1133 }
1134
028f8eaa
MHM
1135#define UTF8KLEN(sv, len) (SvUTF8(sv) ? -(I32)len : (I32)len)
1136
0314122a
NC
1137bool
1138exists(hash, key_sv)
1139 PREINIT:
1140 STRLEN len;
1141 const char *key;
1142 INPUT:
1143 HV *hash
1144 SV *key_sv
1145 CODE:
1146 key = SvPV(key_sv, len);
028f8eaa 1147 RETVAL = hv_exists(hash, key, UTF8KLEN(key_sv, len));
0314122a
NC
1148 OUTPUT:
1149 RETVAL
1150
bdee33e4
NC
1151bool
1152exists_ent(hash, key_sv)
1153 PREINIT:
1154 INPUT:
1155 HV *hash
1156 SV *key_sv
1157 CODE:
1158 RETVAL = hv_exists_ent(hash, key_sv, 0);
1159 OUTPUT:
1160 RETVAL
1161
b60cf05a 1162SV *
55289a74 1163delete(hash, key_sv, flags = 0)
b60cf05a
NC
1164 PREINIT:
1165 STRLEN len;
1166 const char *key;
1167 INPUT:
1168 HV *hash
1169 SV *key_sv
55289a74 1170 I32 flags;
b60cf05a
NC
1171 CODE:
1172 key = SvPV(key_sv, len);
1173 /* It's already mortal, so need to increase reference count. */
55289a74
NC
1174 RETVAL
1175 = SvREFCNT_inc(hv_delete(hash, key, UTF8KLEN(key_sv, len), flags));
1176 OUTPUT:
1177 RETVAL
1178
1179SV *
1180delete_ent(hash, key_sv, flags = 0)
1181 INPUT:
1182 HV *hash
1183 SV *key_sv
1184 I32 flags;
1185 CODE:
1186 /* It's already mortal, so need to increase reference count. */
1187 RETVAL = SvREFCNT_inc(hv_delete_ent(hash, key_sv, flags, 0));
b60cf05a
NC
1188 OUTPUT:
1189 RETVAL
1190
1191SV *
858117f8
NC
1192store_ent(hash, key, value)
1193 PREINIT:
1194 SV *copy;
1195 HE *result;
1196 INPUT:
1197 HV *hash
1198 SV *key
1199 SV *value
1200 CODE:
1201 copy = newSV(0);
1202 result = hv_store_ent(hash, key, copy, 0);
1203 SvSetMagicSV(copy, value);
1204 if (!result) {
1205 SvREFCNT_dec(copy);
1206 XSRETURN_EMPTY;
1207 }
1208 /* It's about to become mortal, so need to increase reference count.
1209 */
1210 RETVAL = SvREFCNT_inc(HeVAL(result));
1211 OUTPUT:
1212 RETVAL
1213
858117f8 1214SV *
b60cf05a
NC
1215store(hash, key_sv, value)
1216 PREINIT:
1217 STRLEN len;
1218 const char *key;
1219 SV *copy;
1220 SV **result;
1221 INPUT:
1222 HV *hash
1223 SV *key_sv
1224 SV *value
1225 CODE:
1226 key = SvPV(key_sv, len);
1227 copy = newSV(0);
028f8eaa 1228 result = hv_store(hash, key, UTF8KLEN(key_sv, len), copy, 0);
858117f8 1229 SvSetMagicSV(copy, value);
b60cf05a
NC
1230 if (!result) {
1231 SvREFCNT_dec(copy);
1232 XSRETURN_EMPTY;
1233 }
1234 /* It's about to become mortal, so need to increase reference count.
1235 */
1236 RETVAL = SvREFCNT_inc(*result);
1237 OUTPUT:
1238 RETVAL
1239
bdee33e4
NC
1240SV *
1241fetch_ent(hash, key_sv)
1242 PREINIT:
1243 HE *result;
1244 INPUT:
1245 HV *hash
1246 SV *key_sv
1247 CODE:
1248 result = hv_fetch_ent(hash, key_sv, 0, 0);
1249 if (!result) {
1250 XSRETURN_EMPTY;
1251 }
1252 /* Force mg_get */
1253 RETVAL = newSVsv(HeVAL(result));
1254 OUTPUT:
1255 RETVAL
b60cf05a
NC
1256
1257SV *
1258fetch(hash, key_sv)
1259 PREINIT:
1260 STRLEN len;
1261 const char *key;
1262 SV **result;
1263 INPUT:
1264 HV *hash
1265 SV *key_sv
1266 CODE:
1267 key = SvPV(key_sv, len);
028f8eaa 1268 result = hv_fetch(hash, key, UTF8KLEN(key_sv, len), 0);
b60cf05a
NC
1269 if (!result) {
1270 XSRETURN_EMPTY;
1271 }
1272 /* Force mg_get */
1273 RETVAL = newSVsv(*result);
1274 OUTPUT:
1275 RETVAL
2dc92170 1276
9568a123
NC
1277#if defined (hv_common)
1278
6b4de907
NC
1279SV *
1280common(params)
1281 INPUT:
1282 HV *params
1283 PREINIT:
1284 HE *result;
1285 HV *hv = NULL;
1286 SV *keysv = NULL;
1287 const char *key = NULL;
1288 STRLEN klen = 0;
1289 int flags = 0;
1290 int action = 0;
1291 SV *val = NULL;
1292 U32 hash = 0;
1293 SV **svp;
1294 CODE:
1295 if ((svp = hv_fetchs(params, "hv", 0))) {
1296 SV *const rv = *svp;
1297 if (!SvROK(rv))
1298 croak("common passed a non-reference for parameter hv");
1299 hv = (HV *)SvRV(rv);
1300 }
1301 if ((svp = hv_fetchs(params, "keysv", 0)))
1302 keysv = *svp;
1303 if ((svp = hv_fetchs(params, "keypv", 0))) {
1304 key = SvPV_const(*svp, klen);
1305 if (SvUTF8(*svp))
1306 flags = HVhek_UTF8;
1307 }
1308 if ((svp = hv_fetchs(params, "action", 0)))
1309 action = SvIV(*svp);
1310 if ((svp = hv_fetchs(params, "val", 0)))
527df579 1311 val = newSVsv(*svp);
6b4de907 1312 if ((svp = hv_fetchs(params, "hash", 0)))
a44093a9 1313 hash = SvUV(*svp);
6b4de907 1314
527df579
NC
1315 if ((svp = hv_fetchs(params, "hash_pv", 0))) {
1316 PERL_HASH(hash, key, klen);
1317 }
58ca560a
NC
1318 if ((svp = hv_fetchs(params, "hash_sv", 0))) {
1319 STRLEN len;
1320 const char *const p = SvPV(keysv, len);
1321 PERL_HASH(hash, p, len);
1322 }
527df579 1323
a75fcbca 1324 result = (HE *)hv_common(hv, keysv, key, klen, flags, action, val, hash);
6b4de907
NC
1325 if (!result) {
1326 XSRETURN_EMPTY;
1327 }
1328 /* Force mg_get */
1329 RETVAL = newSVsv(HeVAL(result));
1330 OUTPUT:
1331 RETVAL
1332
9568a123
NC
1333#endif
1334
439efdfe 1335void
2dc92170
NC
1336test_hv_free_ent()
1337 PPCODE:
1338 test_freeent(&Perl_hv_free_ent);
1339 XSRETURN(4);
1340
439efdfe 1341void
2dc92170
NC
1342test_hv_delayfree_ent()
1343 PPCODE:
1344 test_freeent(&Perl_hv_delayfree_ent);
1345 XSRETURN(4);
35ab5632
NC
1346
1347SV *
1348test_share_unshare_pvn(input)
1349 PREINIT:
35ab5632
NC
1350 STRLEN len;
1351 U32 hash;
1352 char *pvx;
1353 char *p;
1354 INPUT:
1355 SV *input
1356 CODE:
1357 pvx = SvPV(input, len);
1358 PERL_HASH(hash, pvx, len);
1359 p = sharepvn(pvx, len, hash);
1360 RETVAL = newSVpvn(p, len);
1361 unsharepvn(p, len, hash);
1362 OUTPUT:
1363 RETVAL
d8c5b3c5 1364
9568a123
NC
1365#if PERL_VERSION >= 9
1366
d8c5b3c5
NC
1367bool
1368refcounted_he_exists(key, level=0)
1369 SV *key
1370 IV level
1371 CODE:
1372 if (level) {
1373 croak("level must be zero, not %"IVdf, level);
1374 }
20439bc7 1375 RETVAL = (cop_hints_fetch_sv(PL_curcop, key, 0, 0) != &PL_sv_placeholder);
d8c5b3c5
NC
1376 OUTPUT:
1377 RETVAL
1378
d8c5b3c5
NC
1379SV *
1380refcounted_he_fetch(key, level=0)
1381 SV *key
1382 IV level
1383 CODE:
1384 if (level) {
1385 croak("level must be zero, not %"IVdf, level);
1386 }
20439bc7 1387 RETVAL = cop_hints_fetch_sv(PL_curcop, key, 0, 0);
d8c5b3c5
NC
1388 SvREFCNT_inc(RETVAL);
1389 OUTPUT:
1390 RETVAL
65bfe90c 1391
9568a123 1392#endif
65bfe90c 1393
0314122a
NC
1394=pod
1395
1396sub TIEHASH { bless {}, $_[0] }
1397sub STORE { $_[0]->{$_[1]} = $_[2] }
1398sub FETCH { $_[0]->{$_[1]} }
1399sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
1400sub NEXTKEY { each %{$_[0]} }
1401sub EXISTS { exists $_[0]->{$_[1]} }
1402sub DELETE { delete $_[0]->{$_[1]} }
1403sub CLEAR { %{$_[0]} = () }
1404
1405=cut
1406
e2fe06dd
EB
1407MODULE = XS::APItest:TempLv PACKAGE = XS::APItest::TempLv
1408
1409void
1410make_temp_mg_lv(sv)
1411SV* sv
1412 PREINIT:
1413 SV * const lv = newSV_type(SVt_PVLV);
1414 STRLEN len;
1415 PPCODE:
1416 SvPV(sv, len);
1417
1418 sv_magic(lv, NULL, PERL_MAGIC_substr, NULL, 0);
1419 LvTYPE(lv) = 'x';
1420 LvTARG(lv) = SvREFCNT_inc_simple(sv);
1421 LvTARGOFF(lv) = len == 0 ? 0 : 1;
1422 LvTARGLEN(lv) = len < 2 ? 0 : len-2;
1423
1424 EXTEND(SP, 1);
1425 ST(0) = sv_2mortal(lv);
1426 XSRETURN(1);
1427
1428
36c2b1d0
NC
1429MODULE = XS::APItest::PtrTable PACKAGE = XS::APItest::PtrTable PREFIX = ptr_table_
1430
1431void
1432ptr_table_new(classname)
1433const char * classname
1434 PPCODE:
1435 PUSHs(sv_setref_pv(sv_newmortal(), classname, (void*)ptr_table_new()));
1436
1437void
1438DESTROY(table)
1439XS::APItest::PtrTable table
1440 CODE:
1441 ptr_table_free(table);
1442
1443void
992b2363 1444ptr_table_store(table, from, to)
36c2b1d0 1445XS::APItest::PtrTable table
992b2363
NC
1446SVREF from
1447SVREF to
36c2b1d0 1448 CODE:
992b2363 1449 ptr_table_store(table, from, to);
36c2b1d0
NC
1450
1451UV
992b2363 1452ptr_table_fetch(table, from)
36c2b1d0 1453XS::APItest::PtrTable table
992b2363 1454SVREF from
36c2b1d0 1455 CODE:
992b2363 1456 RETVAL = PTR2UV(ptr_table_fetch(table, from));
36c2b1d0
NC
1457 OUTPUT:
1458 RETVAL
1459
1460void
1461ptr_table_split(table)
1462XS::APItest::PtrTable table
1463
1464void
1465ptr_table_clear(table)
1466XS::APItest::PtrTable table
1467
3e61d65a
JH
1468MODULE = XS::APItest PACKAGE = XS::APItest
1469
1470PROTOTYPES: DISABLE
1471
f568d64d
BM
1472HV *
1473xop_custom_ops ()
1474 CODE:
1475 RETVAL = PL_custom_ops;
1476 OUTPUT:
1477 RETVAL
1478
1479HV *
1480xop_custom_op_names ()
1481 CODE:
1482 PL_custom_op_names = newHV();
1483 RETVAL = PL_custom_op_names;
1484 OUTPUT:
1485 RETVAL
1486
1487HV *
1488xop_custom_op_descs ()
1489 CODE:
1490 PL_custom_op_descs = newHV();
1491 RETVAL = PL_custom_op_descs;
1492 OUTPUT:
1493 RETVAL
1494
1495void
1496xop_register ()
1497 CODE:
1498 XopENTRY_set(&my_xop, xop_name, "my_xop");
1499 XopENTRY_set(&my_xop, xop_desc, "XOP for testing");
1500 XopENTRY_set(&my_xop, xop_class, OA_UNOP);
1501 XopENTRY_set(&my_xop, xop_peep, peep_xop);
1502 Perl_custom_op_register(aTHX_ pp_xop, &my_xop);
1503
1504void
1505xop_clear ()
1506 CODE:
1507 XopDISABLE(&my_xop, xop_name);
1508 XopDISABLE(&my_xop, xop_desc);
1509 XopDISABLE(&my_xop, xop_class);
1510 XopDISABLE(&my_xop, xop_peep);
1511
1512IV
1513xop_my_xop ()
1514 CODE:
1515 RETVAL = PTR2IV(&my_xop);
1516 OUTPUT:
1517 RETVAL
1518
1519IV
1520xop_ppaddr ()
1521 CODE:
1522 RETVAL = PTR2IV(pp_xop);
1523 OUTPUT:
1524 RETVAL
1525
1526IV
1527xop_OA_UNOP ()
1528 CODE:
1529 RETVAL = OA_UNOP;
1530 OUTPUT:
1531 RETVAL
1532
1533AV *
1534xop_build_optree ()
1535 CODE:
1536 dMY_CXT;
1537 UNOP *unop;
1538 OP *kid;
1539
1540 MY_CXT.xop_record = newAV();
1541
1542 kid = newSVOP(OP_CONST, 0, newSViv(42));
1543
1544 NewOp(1102, unop, 1, UNOP);
1545 unop->op_type = OP_CUSTOM;
1546 unop->op_ppaddr = pp_xop;
1547 unop->op_flags = OPf_KIDS;
1548 unop->op_private = 0;
1549 unop->op_first = kid;
1550 unop->op_next = NULL;
1551 kid->op_next = (OP*)unop;
1552
b58757d5
BM
1553 av_push(MY_CXT.xop_record, newSVpvf("unop:%"UVxf, PTR2UV(unop)));
1554 av_push(MY_CXT.xop_record, newSVpvf("kid:%"UVxf, PTR2UV(kid)));
f568d64d
BM
1555
1556 av_push(MY_CXT.xop_record, newSVpvf("NAME:%s", OP_NAME((OP*)unop)));
1557 av_push(MY_CXT.xop_record, newSVpvf("DESC:%s", OP_DESC((OP*)unop)));
c33e8be1 1558 av_push(MY_CXT.xop_record, newSVpvf("CLASS:%d", (int)OP_CLASS((OP*)unop)));
f568d64d
BM
1559
1560 PL_rpeepp(aTHX_ kid);
1561
1562 FreeOp(kid);
1563 FreeOp(unop);
1564
1565 RETVAL = MY_CXT.xop_record;
1566 MY_CXT.xop_record = NULL;
1567 OUTPUT:
1568 RETVAL
1569
85ce96a1
DM
1570BOOT:
1571{
1572 MY_CXT_INIT;
03569ecf 1573
85ce96a1
DM
1574 MY_CXT.i = 99;
1575 MY_CXT.sv = newSVpv("initial",0);
13b6b3bc
BM
1576
1577 MY_CXT.bhkav = get_av("XS::APItest::bhkav", GV_ADDMULTI);
1578 MY_CXT.bhk_record = 0;
1579
a88d97bf
BM
1580 BhkENTRY_set(&bhk_test, bhk_start, blockhook_test_start);
1581 BhkENTRY_set(&bhk_test, bhk_pre_end, blockhook_test_pre_end);
1582 BhkENTRY_set(&bhk_test, bhk_post_end, blockhook_test_post_end);
1583 BhkENTRY_set(&bhk_test, bhk_eval, blockhook_test_eval);
13b6b3bc
BM
1584 Perl_blockhook_register(aTHX_ &bhk_test);
1585
65bfe90c 1586 MY_CXT.cscgv = gv_fetchpvs("XS::APItest::COMPILE_SCOPE_CONTAINER",
13b6b3bc 1587 GV_ADDMULTI, SVt_PVAV);
03569ecf
BM
1588 MY_CXT.cscav = GvAV(MY_CXT.cscgv);
1589
a88d97bf
BM
1590 BhkENTRY_set(&bhk_csc, bhk_start, blockhook_csc_start);
1591 BhkENTRY_set(&bhk_csc, bhk_pre_end, blockhook_csc_pre_end);
13b6b3bc 1592 Perl_blockhook_register(aTHX_ &bhk_csc);
201c7e1f
FR
1593
1594 MY_CXT.peep_recorder = newAV();
1595 MY_CXT.rpeep_recorder = newAV();
1596
1597 MY_CXT.orig_peep = PL_peepp;
1598 MY_CXT.orig_rpeep = PL_rpeepp;
1599 PL_peepp = my_peep;
1600 PL_rpeepp = my_rpeep;
65bfe90c 1601}
85ce96a1
DM
1602
1603void
1604CLONE(...)
1605 CODE:
1606 MY_CXT_CLONE;
c33e8be1 1607 PERL_UNUSED_VAR(items);
85ce96a1 1608 MY_CXT.sv = newSVpv("initial_clone",0);
65bfe90c 1609 MY_CXT.cscgv = gv_fetchpvs("XS::APItest::COMPILE_SCOPE_CONTAINER",
13b6b3bc 1610 GV_ADDMULTI, SVt_PVAV);
03569ecf 1611 MY_CXT.cscav = NULL;
13b6b3bc
BM
1612 MY_CXT.bhkav = get_av("XS::APItest::bhkav", GV_ADDMULTI);
1613 MY_CXT.bhk_record = 0;
201c7e1f
FR
1614 MY_CXT.peep_recorder = newAV();
1615 MY_CXT.rpeep_recorder = newAV();
85ce96a1 1616
3e61d65a
JH
1617void
1618print_double(val)
1619 double val
1620 CODE:
1621 printf("%5.3f\n",val);
1622
1623int
1624have_long_double()
1625 CODE:
1626#ifdef HAS_LONG_DOUBLE
1627 RETVAL = 1;
1628#else
1629 RETVAL = 0;
1630#endif
cabb36f0
CN
1631 OUTPUT:
1632 RETVAL
3e61d65a
JH
1633
1634void
1635print_long_double()
1636 CODE:
1637#ifdef HAS_LONG_DOUBLE
fc0bf671 1638# if defined(PERL_PRIfldbl) && (LONG_DOUBLESIZE > DOUBLESIZE)
3e61d65a
JH
1639 long double val = 7.0;
1640 printf("%5.3" PERL_PRIfldbl "\n",val);
1641# else
1642 double val = 7.0;
1643 printf("%5.3f\n",val);
1644# endif
1645#endif
1646
1647void
3e61d65a
JH
1648print_int(val)
1649 int val
1650 CODE:
1651 printf("%d\n",val);
1652
1653void
1654print_long(val)
1655 long val
1656 CODE:
1657 printf("%ld\n",val);
1658
1659void
1660print_float(val)
1661 float val
1662 CODE:
1663 printf("%5.3f\n",val);
9d911683
NIS
1664
1665void
1666print_flush()
1667 CODE:
1668 fflush(stdout);
d4b90eee
SH
1669
1670void
1671mpushp()
1672 PPCODE:
1673 EXTEND(SP, 3);
1674 mPUSHp("one", 3);
1675 mPUSHp("two", 3);
1676 mPUSHp("three", 5);
1677 XSRETURN(3);
1678
1679void
1680mpushn()
1681 PPCODE:
1682 EXTEND(SP, 3);
1683 mPUSHn(0.5);
1684 mPUSHn(-0.25);
1685 mPUSHn(0.125);
1686 XSRETURN(3);
1687
1688void
1689mpushi()
1690 PPCODE:
1691 EXTEND(SP, 3);
d75b63cf
MHM
1692 mPUSHi(-1);
1693 mPUSHi(2);
1694 mPUSHi(-3);
d4b90eee
SH
1695 XSRETURN(3);
1696
1697void
1698mpushu()
1699 PPCODE:
1700 EXTEND(SP, 3);
d75b63cf
MHM
1701 mPUSHu(1);
1702 mPUSHu(2);
1703 mPUSHu(3);
d4b90eee
SH
1704 XSRETURN(3);
1705
1706void
1707mxpushp()
1708 PPCODE:
1709 mXPUSHp("one", 3);
1710 mXPUSHp("two", 3);
1711 mXPUSHp("three", 5);
1712 XSRETURN(3);
1713
1714void
1715mxpushn()
1716 PPCODE:
1717 mXPUSHn(0.5);
1718 mXPUSHn(-0.25);
1719 mXPUSHn(0.125);
1720 XSRETURN(3);
1721
1722void
1723mxpushi()
1724 PPCODE:
d75b63cf
MHM
1725 mXPUSHi(-1);
1726 mXPUSHi(2);
1727 mXPUSHi(-3);
d4b90eee
SH
1728 XSRETURN(3);
1729
1730void
1731mxpushu()
1732 PPCODE:
d75b63cf
MHM
1733 mXPUSHu(1);
1734 mXPUSHu(2);
1735 mXPUSHu(3);
d4b90eee 1736 XSRETURN(3);
d1f347d7
DM
1737
1738
1739void
1740call_sv(sv, flags, ...)
1741 SV* sv
1742 I32 flags
1743 PREINIT:
1744 I32 i;
1745 PPCODE:
1746 for (i=0; i<items-2; i++)
1747 ST(i) = ST(i+2); /* pop first two args */
1748 PUSHMARK(SP);
1749 SP += items - 2;
1750 PUTBACK;
1751 i = call_sv(sv, flags);
1752 SPAGAIN;
1753 EXTEND(SP, 1);
1754 PUSHs(sv_2mortal(newSViv(i)));
1755
1756void
1757call_pv(subname, flags, ...)
1758 char* subname
1759 I32 flags
1760 PREINIT:
1761 I32 i;
1762 PPCODE:
1763 for (i=0; i<items-2; i++)
1764 ST(i) = ST(i+2); /* pop first two args */
1765 PUSHMARK(SP);
1766 SP += items - 2;
1767 PUTBACK;
1768 i = call_pv(subname, flags);
1769 SPAGAIN;
1770 EXTEND(SP, 1);
1771 PUSHs(sv_2mortal(newSViv(i)));
1772
1773void
1774call_method(methname, flags, ...)
1775 char* methname
1776 I32 flags
1777 PREINIT:
1778 I32 i;
1779 PPCODE:
1780 for (i=0; i<items-2; i++)
1781 ST(i) = ST(i+2); /* pop first two args */
1782 PUSHMARK(SP);
1783 SP += items - 2;
1784 PUTBACK;
1785 i = call_method(methname, flags);
1786 SPAGAIN;
1787 EXTEND(SP, 1);
1788 PUSHs(sv_2mortal(newSViv(i)));
1789
1790void
1791eval_sv(sv, flags)
1792 SV* sv
1793 I32 flags
1794 PREINIT:
1795 I32 i;
1796 PPCODE:
1797 PUTBACK;
1798 i = eval_sv(sv, flags);
1799 SPAGAIN;
1800 EXTEND(SP, 1);
1801 PUSHs(sv_2mortal(newSViv(i)));
1802
b8e65a9b 1803void
d1f347d7
DM
1804eval_pv(p, croak_on_error)
1805 const char* p
1806 I32 croak_on_error
d1f347d7
DM
1807 PPCODE:
1808 PUTBACK;
1809 EXTEND(SP, 1);
1810 PUSHs(eval_pv(p, croak_on_error));
1811
1812void
1813require_pv(pv)
1814 const char* pv
d1f347d7
DM
1815 PPCODE:
1816 PUTBACK;
1817 require_pv(pv);
1818
0ca3a874 1819int
7a646707 1820apitest_exception(throw_e)
0ca3a874
MHM
1821 int throw_e
1822 OUTPUT:
1823 RETVAL
d1f347d7 1824
ef469b03 1825void
7e7a3dfc
GA
1826mycroak(sv)
1827 SV* sv
ef469b03 1828 CODE:
7e7a3dfc
GA
1829 if (SvOK(sv)) {
1830 Perl_croak(aTHX_ "%s", SvPV_nolen(sv));
1831 }
1832 else {
1833 Perl_croak(aTHX_ NULL);
1834 }
5d2b1485
NC
1835
1836SV*
1837strtab()
1838 CODE:
1839 RETVAL = newRV_inc((SV*)PL_strtab);
1840 OUTPUT:
1841 RETVAL
85ce96a1
DM
1842
1843int
1844my_cxt_getint()
1845 CODE:
1846 dMY_CXT;
1847 RETVAL = my_cxt_getint_p(aMY_CXT);
1848 OUTPUT:
1849 RETVAL
1850
1851void
1852my_cxt_setint(i)
1853 int i;
1854 CODE:
1855 dMY_CXT;
1856 my_cxt_setint_p(aMY_CXT_ i);
1857
1858void
9568a123
NC
1859my_cxt_getsv(how)
1860 bool how;
85ce96a1 1861 PPCODE:
85ce96a1 1862 EXTEND(SP, 1);
9568a123 1863 ST(0) = how ? my_cxt_getsv_interp_context() : my_cxt_getsv_interp();
85ce96a1
DM
1864 XSRETURN(1);
1865
1866void
1867my_cxt_setsv(sv)
1868 SV *sv;
1869 CODE:
1870 dMY_CXT;
1871 SvREFCNT_dec(MY_CXT.sv);
1872 my_cxt_setsv_p(sv _aMY_CXT);
1873 SvREFCNT_inc(sv);
34482cd6
NC
1874
1875bool
1876sv_setsv_cow_hashkey_core()
1877
1878bool
1879sv_setsv_cow_hashkey_notcore()
84ac5fd7
NC
1880
1881void
218787bd
VP
1882rmagical_cast(sv, type)
1883 SV *sv;
1884 SV *type;
1885 PREINIT:
1886 struct ufuncs uf;
1887 PPCODE:
1888 if (!SvOK(sv) || !SvROK(sv) || !SvOK(type)) { XSRETURN_UNDEF; }
1889 sv = SvRV(sv);
1890 if (SvTYPE(sv) != SVt_PVHV) { XSRETURN_UNDEF; }
1891 uf.uf_val = rmagical_a_dummy;
1892 uf.uf_set = NULL;
1893 uf.uf_index = 0;
1894 if (SvTRUE(type)) { /* b */
1895 sv_magicext(sv, NULL, PERL_MAGIC_ext, &rmagical_b, NULL, 0);
1896 } else { /* a */
1897 sv_magic(sv, NULL, PERL_MAGIC_uvar, (char *) &uf, sizeof(uf));
1898 }
1899 XSRETURN_YES;
1900
1901void
1902rmagical_flags(sv)
1903 SV *sv;
1904 PPCODE:
1905 if (!SvOK(sv) || !SvROK(sv)) { XSRETURN_UNDEF; }
1906 sv = SvRV(sv);
1907 EXTEND(SP, 3);
1908 mXPUSHu(SvFLAGS(sv) & SVs_GMG);
1909 mXPUSHu(SvFLAGS(sv) & SVs_SMG);
1910 mXPUSHu(SvFLAGS(sv) & SVs_RMG);
1911 XSRETURN(3);
1912
1913void
90d1f214
BM
1914my_caller(level)
1915 I32 level
1916 PREINIT:
1917 const PERL_CONTEXT *cx, *dbcx;
1918 const char *pv;
1919 const GV *gv;
1920 HV *hv;
1921 PPCODE:
1922 cx = caller_cx(level, &dbcx);
1923 EXTEND(SP, 8);
1924
1925 pv = CopSTASHPV(cx->blk_oldcop);
1926 ST(0) = pv ? sv_2mortal(newSVpv(pv, 0)) : &PL_sv_undef;
1927 gv = CvGV(cx->blk_sub.cv);
1928 ST(1) = isGV(gv) ? sv_2mortal(newSVpv(GvNAME(gv), 0)) : &PL_sv_undef;
1929
1930 pv = CopSTASHPV(dbcx->blk_oldcop);
1931 ST(2) = pv ? sv_2mortal(newSVpv(pv, 0)) : &PL_sv_undef;
1932 gv = CvGV(dbcx->blk_sub.cv);
1933 ST(3) = isGV(gv) ? sv_2mortal(newSVpv(GvNAME(gv), 0)) : &PL_sv_undef;
1934
20439bc7
Z
1935 ST(4) = cop_hints_fetch_pvs(cx->blk_oldcop, "foo", 0);
1936 ST(5) = cop_hints_fetch_pvn(cx->blk_oldcop, "foo", 3, 0, 0);
1937 ST(6) = cop_hints_fetch_sv(cx->blk_oldcop,
1938 sv_2mortal(newSVpvn("foo", 3)), 0, 0);
90d1f214 1939
20439bc7 1940 hv = cop_hints_2hv(cx->blk_oldcop, 0);
90d1f214
BM
1941 ST(7) = hv ? sv_2mortal(newRV_noinc((SV *)hv)) : &PL_sv_undef;
1942
1943 XSRETURN(8);
1944
1945void
f9c17636
MB
1946DPeek (sv)
1947 SV *sv
1948
1949 PPCODE:
5b1f7359 1950 ST (0) = newSVpv (Perl_sv_peek (aTHX_ sv), 0);
f9c17636
MB
1951 XSRETURN (1);
1952
1953void
84ac5fd7
NC
1954BEGIN()
1955 CODE:
1956 sv_inc(get_sv("XS::APItest::BEGIN_called", GV_ADD|GV_ADDMULTI));
1957
1958void
1959CHECK()
1960 CODE:
1961 sv_inc(get_sv("XS::APItest::CHECK_called", GV_ADD|GV_ADDMULTI));
1962
1963void
1964UNITCHECK()
1965 CODE:
0932863f 1966 sv_inc(get_sv("XS::APItest::UNITCHECK_called", GV_ADD|GV_ADDMULTI));
84ac5fd7
NC
1967
1968void
1969INIT()
1970 CODE:
1971 sv_inc(get_sv("XS::APItest::INIT_called", GV_ADD|GV_ADDMULTI));
1972
1973void
1974END()
1975 CODE:
1976 sv_inc(get_sv("XS::APItest::END_called", GV_ADD|GV_ADDMULTI));
30685b56
NC
1977
1978void
1979utf16_to_utf8 (sv, ...)
1980 SV* sv
1981 ALIAS:
1982 utf16_to_utf8_reversed = 1
1983 PREINIT:
1984 STRLEN len;
1985 U8 *source;
1986 SV *dest;
1987 I32 got; /* Gah, badly thought out APIs */
1988 CODE:
1989 source = (U8 *)SvPVbyte(sv, len);
1990 /* Optionally only convert part of the buffer. */
1991 if (items > 1) {
1992 len = SvUV(ST(1));
1993 }
1994 /* Mortalise this right now, as we'll be testing croak()s */
1995 dest = sv_2mortal(newSV(len * 3 / 2 + 1));
1996 if (ix) {
25f2e844 1997 utf16_to_utf8_reversed(source, (U8 *)SvPVX(dest), len, &got);
30685b56 1998 } else {
25f2e844 1999 utf16_to_utf8(source, (U8 *)SvPVX(dest), len, &got);
30685b56
NC
2000 }
2001 SvCUR_set(dest, got);
2002 SvPVX(dest)[got] = '\0';
2003 SvPOK_on(dest);
2004 ST(0) = dest;
2005 XSRETURN(1);
879d0c72 2006
6bd7445c
GG
2007void
2008my_exit(int exitcode)
2009 PPCODE:
2010 my_exit(exitcode);
d97c33b5 2011
209e41dc
NT
2012U8
2013first_byte(sv)
2014 SV *sv
2015 CODE:
2016 char *s;
2017 STRLEN len;
2018 s = SvPVbyte(sv, len);
2019 RETVAL = s[0];
2020 OUTPUT:
2021 RETVAL
2022
d97c33b5
DM
2023I32
2024sv_count()
2025 CODE:
2026 RETVAL = PL_sv_count;
2027 OUTPUT:
2028 RETVAL
13b6b3bc
BM
2029
2030void
2031bhk_record(bool on)
2032 CODE:
2033 dMY_CXT;
2034 MY_CXT.bhk_record = on;
2035 if (on)
2036 av_clear(MY_CXT.bhkav);
65bfe90c 2037
defdfed5 2038void
d9088386
Z
2039test_magic_chain()
2040 PREINIT:
2041 SV *sv;
2042 MAGIC *callmg, *uvarmg;
2043 CODE:
2044 sv = sv_2mortal(newSV(0));
11f9f0ed
NC
2045 if (SvTYPE(sv) >= SVt_PVMG) croak_fail();
2046 if (SvMAGICAL(sv)) croak_fail();
d9088386 2047 sv_magic(sv, &PL_sv_yes, PERL_MAGIC_checkcall, (char*)&callmg, 0);
11f9f0ed
NC
2048 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2049 if (!SvMAGICAL(sv)) croak_fail();
2050 if (mg_find(sv, PERL_MAGIC_uvar)) croak_fail();
d9088386 2051 callmg = mg_find(sv, PERL_MAGIC_checkcall);
11f9f0ed 2052 if (!callmg) croak_fail();
d9088386 2053 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 2054 croak_fail();
d9088386 2055 sv_magic(sv, &PL_sv_no, PERL_MAGIC_uvar, (char*)&uvarmg, 0);
11f9f0ed
NC
2056 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2057 if (!SvMAGICAL(sv)) croak_fail();
2058 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
d9088386 2059 uvarmg = mg_find(sv, PERL_MAGIC_uvar);
11f9f0ed 2060 if (!uvarmg) croak_fail();
d9088386 2061 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 2062 croak_fail();
d9088386 2063 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 2064 croak_fail();
d9088386 2065 mg_free_type(sv, PERL_MAGIC_vec);
11f9f0ed
NC
2066 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2067 if (!SvMAGICAL(sv)) croak_fail();
2068 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
2069 if (mg_find(sv, PERL_MAGIC_uvar) != uvarmg) croak_fail();
d9088386 2070 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 2071 croak_fail();
d9088386 2072 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 2073 croak_fail();
d9088386 2074 mg_free_type(sv, PERL_MAGIC_uvar);
11f9f0ed
NC
2075 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2076 if (!SvMAGICAL(sv)) croak_fail();
2077 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
2078 if (mg_find(sv, PERL_MAGIC_uvar)) croak_fail();
d9088386 2079 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 2080 croak_fail();
d9088386 2081 sv_magic(sv, &PL_sv_no, PERL_MAGIC_uvar, (char*)&uvarmg, 0);
11f9f0ed
NC
2082 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2083 if (!SvMAGICAL(sv)) croak_fail();
2084 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
d9088386 2085 uvarmg = mg_find(sv, PERL_MAGIC_uvar);
11f9f0ed 2086 if (!uvarmg) croak_fail();
d9088386 2087 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 2088 croak_fail();
d9088386 2089 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 2090 croak_fail();
d9088386 2091 mg_free_type(sv, PERL_MAGIC_checkcall);
11f9f0ed
NC
2092 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
2093 if (!SvMAGICAL(sv)) croak_fail();
2094 if (mg_find(sv, PERL_MAGIC_uvar) != uvarmg) croak_fail();
2095 if (mg_find(sv, PERL_MAGIC_checkcall)) croak_fail();
d9088386 2096 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 2097 croak_fail();
d9088386 2098 mg_free_type(sv, PERL_MAGIC_uvar);
11f9f0ed
NC
2099 if (SvMAGICAL(sv)) croak_fail();
2100 if (mg_find(sv, PERL_MAGIC_checkcall)) croak_fail();
2101 if (mg_find(sv, PERL_MAGIC_uvar)) croak_fail();
d9088386
Z
2102
2103void
2104test_op_contextualize()
2105 PREINIT:
2106 OP *o;
2107 CODE:
2108 o = newSVOP(OP_CONST, 0, newSViv(0));
2109 o->op_flags &= ~OPf_WANT;
2110 o = op_contextualize(o, G_SCALAR);
2111 if (o->op_type != OP_CONST ||
2112 (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR)
11f9f0ed 2113 croak_fail();
d9088386
Z
2114 op_free(o);
2115 o = newSVOP(OP_CONST, 0, newSViv(0));
2116 o->op_flags &= ~OPf_WANT;
2117 o = op_contextualize(o, G_ARRAY);
2118 if (o->op_type != OP_CONST ||
2119 (o->op_flags & OPf_WANT) != OPf_WANT_LIST)
11f9f0ed 2120 croak_fail();
d9088386
Z
2121 op_free(o);
2122 o = newSVOP(OP_CONST, 0, newSViv(0));
2123 o->op_flags &= ~OPf_WANT;
2124 o = op_contextualize(o, G_VOID);
11f9f0ed 2125 if (o->op_type != OP_NULL) croak_fail();
d9088386
Z
2126 op_free(o);
2127
2128void
2129test_rv2cv_op_cv()
2130 PROTOTYPE:
2131 PREINIT:
2132 GV *troc_gv, *wibble_gv;
2133 CV *troc_cv;
2134 OP *o;
2135 CODE:
2136 troc_gv = gv_fetchpv("XS::APItest::test_rv2cv_op_cv", 0, SVt_PVGV);
2137 troc_cv = get_cv("XS::APItest::test_rv2cv_op_cv", 0);
2138 wibble_gv = gv_fetchpv("XS::APItest::wibble", 0, SVt_PVGV);
2139 o = newCVREF(0, newGVOP(OP_GV, 0, troc_gv));
11f9f0ed 2140 if (rv2cv_op_cv(o, 0) != troc_cv) croak_fail();
d9088386 2141 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV) != (CV*)troc_gv)
11f9f0ed 2142 croak_fail();
d9088386 2143 o->op_private |= OPpENTERSUB_AMPER;
11f9f0ed
NC
2144 if (rv2cv_op_cv(o, 0)) croak_fail();
2145 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386 2146 o->op_private &= ~OPpENTERSUB_AMPER;
11f9f0ed
NC
2147 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
2148 if (rv2cv_op_cv(o, RV2CVOPCV_MARK_EARLY) != troc_cv) croak_fail();
2149 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
d9088386
Z
2150 op_free(o);
2151 o = newSVOP(OP_CONST, 0, newSVpv("XS::APItest::test_rv2cv_op_cv", 0));
2152 o->op_private = OPpCONST_BARE;
2153 o = newCVREF(0, o);
11f9f0ed 2154 if (rv2cv_op_cv(o, 0) != troc_cv) croak_fail();
d9088386 2155 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV) != (CV*)troc_gv)
11f9f0ed 2156 croak_fail();
d9088386 2157 o->op_private |= OPpENTERSUB_AMPER;
11f9f0ed
NC
2158 if (rv2cv_op_cv(o, 0)) croak_fail();
2159 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386
Z
2160 op_free(o);
2161 o = newCVREF(0, newSVOP(OP_CONST, 0, newRV_inc((SV*)troc_cv)));
11f9f0ed 2162 if (rv2cv_op_cv(o, 0) != troc_cv) croak_fail();
d9088386 2163 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV) != (CV*)troc_gv)
11f9f0ed 2164 croak_fail();
d9088386 2165 o->op_private |= OPpENTERSUB_AMPER;
11f9f0ed
NC
2166 if (rv2cv_op_cv(o, 0)) croak_fail();
2167 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386 2168 o->op_private &= ~OPpENTERSUB_AMPER;
11f9f0ed
NC
2169 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
2170 if (rv2cv_op_cv(o, RV2CVOPCV_MARK_EARLY) != troc_cv) croak_fail();
2171 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
d9088386
Z
2172 op_free(o);
2173 o = newCVREF(0, newUNOP(OP_RAND, 0, newSVOP(OP_CONST, 0, newSViv(0))));
11f9f0ed
NC
2174 if (rv2cv_op_cv(o, 0)) croak_fail();
2175 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386 2176 o->op_private |= OPpENTERSUB_AMPER;
11f9f0ed
NC
2177 if (rv2cv_op_cv(o, 0)) croak_fail();
2178 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386 2179 o->op_private &= ~OPpENTERSUB_AMPER;
11f9f0ed
NC
2180 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
2181 if (rv2cv_op_cv(o, RV2CVOPCV_MARK_EARLY)) croak_fail();
2182 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
d9088386
Z
2183 op_free(o);
2184 o = newUNOP(OP_RAND, 0, newSVOP(OP_CONST, 0, newSViv(0)));
11f9f0ed
NC
2185 if (rv2cv_op_cv(o, 0)) croak_fail();
2186 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386
Z
2187 op_free(o);
2188
2189void
2190test_cv_getset_call_checker()
2191 PREINIT:
2192 CV *troc_cv, *tsh_cv;
2193 Perl_call_checker ckfun;
2194 SV *ckobj;
2195 CODE:
2196#define check_cc(cv, xckfun, xckobj) \
2197 do { \
2198 cv_get_call_checker((cv), &ckfun, &ckobj); \
779bc08a
NC
2199 if (ckfun != (xckfun)) croak_fail_ne(FPTR2DPTR(void *, ckfun), xckfun); \
2200 if (ckobj != (xckobj)) croak_fail_ne(FPTR2DPTR(void *, ckobj), xckobj); \
d9088386
Z
2201 } while(0)
2202 troc_cv = get_cv("XS::APItest::test_rv2cv_op_cv", 0);
2203 tsh_cv = get_cv("XS::APItest::test_savehints", 0);
2204 check_cc(troc_cv, Perl_ck_entersub_args_proto_or_list, (SV*)troc_cv);
2205 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv);
2206 cv_set_call_checker(tsh_cv, Perl_ck_entersub_args_proto_or_list,
2207 &PL_sv_yes);
2208 check_cc(troc_cv, Perl_ck_entersub_args_proto_or_list, (SV*)troc_cv);
2209 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, &PL_sv_yes);
2210 cv_set_call_checker(troc_cv, THX_ck_entersub_args_scalars, &PL_sv_no);
2211 check_cc(troc_cv, THX_ck_entersub_args_scalars, &PL_sv_no);
2212 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, &PL_sv_yes);
2213 cv_set_call_checker(tsh_cv, Perl_ck_entersub_args_proto_or_list,
2214 (SV*)tsh_cv);
2215 check_cc(troc_cv, THX_ck_entersub_args_scalars, &PL_sv_no);
2216 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv);
2217 cv_set_call_checker(troc_cv, Perl_ck_entersub_args_proto_or_list,
2218 (SV*)troc_cv);
2219 check_cc(troc_cv, Perl_ck_entersub_args_proto_or_list, (SV*)troc_cv);
2220 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv);
11f9f0ed
NC
2221 if (SvMAGICAL((SV*)troc_cv) || SvMAGIC((SV*)troc_cv)) croak_fail();
2222 if (SvMAGICAL((SV*)tsh_cv) || SvMAGIC((SV*)tsh_cv)) croak_fail();
d9088386
Z
2223#undef check_cc
2224
2225void
2226cv_set_call_checker_lists(CV *cv)
2227 CODE:
2228 cv_set_call_checker(cv, THX_ck_entersub_args_lists, &PL_sv_undef);
2229
2230void
2231cv_set_call_checker_scalars(CV *cv)
2232 CODE:
2233 cv_set_call_checker(cv, THX_ck_entersub_args_scalars, &PL_sv_undef);
2234
2235void
2236cv_set_call_checker_proto(CV *cv, SV *proto)
2237 CODE:
2238 if (SvROK(proto))
2239 proto = SvRV(proto);
2240 cv_set_call_checker(cv, Perl_ck_entersub_args_proto, proto);
2241
2242void
2243cv_set_call_checker_proto_or_list(CV *cv, SV *proto)
2244 CODE:
2245 if (SvROK(proto))
2246 proto = SvRV(proto);
2247 cv_set_call_checker(cv, Perl_ck_entersub_args_proto_or_list, proto);
2248
2249void
2250cv_set_call_checker_multi_sum(CV *cv)
2251 CODE:
2252 cv_set_call_checker(cv, THX_ck_entersub_multi_sum, &PL_sv_undef);
2253
2254void
20439bc7
Z
2255test_cophh()
2256 PREINIT:
2257 COPHH *a, *b;
2258 CODE:
2259#define check_ph(EXPR) \
2260 do { if((EXPR) != &PL_sv_placeholder) croak("fail"); } while(0)
2261#define check_iv(EXPR, EXPECT) \
2262 do { if(SvIV(EXPR) != (EXPECT)) croak("fail"); } while(0)
2263#define msvpvs(STR) sv_2mortal(newSVpvs(STR))
2264#define msviv(VALUE) sv_2mortal(newSViv(VALUE))
2265 a = cophh_new_empty();
2266 check_ph(cophh_fetch_pvn(a, "foo_1", 5, 0, 0));
2267 check_ph(cophh_fetch_pvs(a, "foo_1", 0));
2268 check_ph(cophh_fetch_pv(a, "foo_1", 0, 0));
2269 check_ph(cophh_fetch_sv(a, msvpvs("foo_1"), 0, 0));
2270 a = cophh_store_pvn(a, "foo_1abc", 5, 0, msviv(111), 0);
2271 a = cophh_store_pvs(a, "foo_2", msviv(222), 0);
2272 a = cophh_store_pv(a, "foo_3", 0, msviv(333), 0);
2273 a = cophh_store_sv(a, msvpvs("foo_4"), 0, msviv(444), 0);
2274 check_iv(cophh_fetch_pvn(a, "foo_1xyz", 5, 0, 0), 111);
2275 check_iv(cophh_fetch_pvs(a, "foo_1", 0), 111);
2276 check_iv(cophh_fetch_pv(a, "foo_1", 0, 0), 111);
2277 check_iv(cophh_fetch_sv(a, msvpvs("foo_1"), 0, 0), 111);
2278 check_iv(cophh_fetch_pvs(a, "foo_2", 0), 222);
2279 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
2280 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
2281 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
2282 b = cophh_copy(a);
2283 b = cophh_store_pvs(b, "foo_1", msviv(1111), 0);
2284 check_iv(cophh_fetch_pvs(a, "foo_1", 0), 111);
2285 check_iv(cophh_fetch_pvs(a, "foo_2", 0), 222);
2286 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
2287 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
2288 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
2289 check_iv(cophh_fetch_pvs(b, "foo_1", 0), 1111);
2290 check_iv(cophh_fetch_pvs(b, "foo_2", 0), 222);
2291 check_iv(cophh_fetch_pvs(b, "foo_3", 0), 333);
2292 check_iv(cophh_fetch_pvs(b, "foo_4", 0), 444);
2293 check_ph(cophh_fetch_pvs(b, "foo_5", 0));
2294 a = cophh_delete_pvn(a, "foo_1abc", 5, 0, 0);
2295 a = cophh_delete_pvs(a, "foo_2", 0);
2296 b = cophh_delete_pv(b, "foo_3", 0, 0);
2297 b = cophh_delete_sv(b, msvpvs("foo_4"), 0, 0);
2298 check_ph(cophh_fetch_pvs(a, "foo_1", 0));
2299 check_ph(cophh_fetch_pvs(a, "foo_2", 0));
2300 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
2301 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
2302 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
2303 check_iv(cophh_fetch_pvs(b, "foo_1", 0), 1111);
2304 check_iv(cophh_fetch_pvs(b, "foo_2", 0), 222);
2305 check_ph(cophh_fetch_pvs(b, "foo_3", 0));
2306 check_ph(cophh_fetch_pvs(b, "foo_4", 0));
2307 check_ph(cophh_fetch_pvs(b, "foo_5", 0));
2308 b = cophh_delete_pvs(b, "foo_3", 0);
2309 b = cophh_delete_pvs(b, "foo_5", 0);
2310 check_iv(cophh_fetch_pvs(b, "foo_1", 0), 1111);
2311 check_iv(cophh_fetch_pvs(b, "foo_2", 0), 222);
2312 check_ph(cophh_fetch_pvs(b, "foo_3", 0));
2313 check_ph(cophh_fetch_pvs(b, "foo_4", 0));
2314 check_ph(cophh_fetch_pvs(b, "foo_5", 0));
2315 cophh_free(b);
2316 check_ph(cophh_fetch_pvs(a, "foo_1", 0));
2317 check_ph(cophh_fetch_pvs(a, "foo_2", 0));
2318 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
2319 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
2320 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
2321 a = cophh_store_pvs(a, "foo_1", msviv(11111), COPHH_KEY_UTF8);
2322 a = cophh_store_pvs(a, "foo_\xaa", msviv(123), 0);
2323 a = cophh_store_pvs(a, "foo_\xc2\xbb", msviv(456), COPHH_KEY_UTF8);
2324 a = cophh_store_pvs(a, "foo_\xc3\x8c", msviv(789), COPHH_KEY_UTF8);
2325 a = cophh_store_pvs(a, "foo_\xd9\xa6", msviv(666), COPHH_KEY_UTF8);
2326 check_iv(cophh_fetch_pvs(a, "foo_1", 0), 11111);
2327 check_iv(cophh_fetch_pvs(a, "foo_1", COPHH_KEY_UTF8), 11111);
2328 check_iv(cophh_fetch_pvs(a, "foo_\xaa", 0), 123);
2329 check_iv(cophh_fetch_pvs(a, "foo_\xc2\xaa", COPHH_KEY_UTF8), 123);
2330 check_ph(cophh_fetch_pvs(a, "foo_\xc2\xaa", 0));
2331 check_iv(cophh_fetch_pvs(a, "foo_\xbb", 0), 456);
2332 check_iv(cophh_fetch_pvs(a, "foo_\xc2\xbb", COPHH_KEY_UTF8), 456);
2333 check_ph(cophh_fetch_pvs(a, "foo_\xc2\xbb", 0));
2334 check_iv(cophh_fetch_pvs(a, "foo_\xcc", 0), 789);
2335 check_iv(cophh_fetch_pvs(a, "foo_\xc3\x8c", COPHH_KEY_UTF8), 789);
2336 check_ph(cophh_fetch_pvs(a, "foo_\xc2\x8c", 0));
2337 check_iv(cophh_fetch_pvs(a, "foo_\xd9\xa6", COPHH_KEY_UTF8), 666);
2338 check_ph(cophh_fetch_pvs(a, "foo_\xd9\xa6", 0));
3987a177
Z
2339 ENTER;
2340 SAVEFREECOPHH(a);
2341 LEAVE;
20439bc7
Z
2342#undef check_ph
2343#undef check_iv
2344#undef msvpvs
2345#undef msviv
2346
2347HV *
2348example_cophh_2hv()
2349 PREINIT:
2350 COPHH *a;
2351 CODE:
2352#define msviv(VALUE) sv_2mortal(newSViv(VALUE))
2353 a = cophh_new_empty();
2354 a = cophh_store_pvs(a, "foo_0", msviv(999), 0);
2355 a = cophh_store_pvs(a, "foo_1", msviv(111), 0);
2356 a = cophh_store_pvs(a, "foo_\xaa", msviv(123), 0);
2357 a = cophh_store_pvs(a, "foo_\xc2\xbb", msviv(456), COPHH_KEY_UTF8);
2358 a = cophh_store_pvs(a, "foo_\xc3\x8c", msviv(789), COPHH_KEY_UTF8);
2359 a = cophh_store_pvs(a, "foo_\xd9\xa6", msviv(666), COPHH_KEY_UTF8);
2360 a = cophh_delete_pvs(a, "foo_0", 0);
2361 a = cophh_delete_pvs(a, "foo_2", 0);
2362 RETVAL = cophh_2hv(a, 0);
2363 cophh_free(a);
2364#undef msviv
2365 OUTPUT:
2366 RETVAL
2367
2368void
defdfed5
Z
2369test_savehints()
2370 PREINIT:
2371 SV **svp, *sv;
2372 CODE:
2373#define store_hint(KEY, VALUE) \
2374 sv_setiv_mg(*hv_fetchs(GvHV(PL_hintgv), KEY, 1), (VALUE))
2375#define hint_ok(KEY, EXPECT) \
2376 ((svp = hv_fetchs(GvHV(PL_hintgv), KEY, 0)) && \
2377 (sv = *svp) && SvIV(sv) == (EXPECT) && \
20439bc7 2378 (sv = cop_hints_fetch_pvs(&PL_compiling, KEY, 0)) && \
defdfed5
Z
2379 SvIV(sv) == (EXPECT))
2380#define check_hint(KEY, EXPECT) \
11f9f0ed 2381 do { if (!hint_ok(KEY, EXPECT)) croak_fail(); } while(0)
defdfed5
Z
2382 PL_hints |= HINT_LOCALIZE_HH;
2383 ENTER;
2384 SAVEHINTS();
2385 PL_hints &= HINT_INTEGER;
2386 store_hint("t0", 123);
2387 store_hint("t1", 456);
11f9f0ed 2388 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2389 check_hint("t0", 123); check_hint("t1", 456);
2390 ENTER;
2391 SAVEHINTS();
11f9f0ed 2392 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2393 check_hint("t0", 123); check_hint("t1", 456);
2394 PL_hints |= HINT_INTEGER;
2395 store_hint("t0", 321);
11f9f0ed 2396 if (!(PL_hints & HINT_INTEGER)) croak_fail();
defdfed5
Z
2397 check_hint("t0", 321); check_hint("t1", 456);
2398 LEAVE;
11f9f0ed 2399 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2400 check_hint("t0", 123); check_hint("t1", 456);
2401 ENTER;
2402 SAVEHINTS();
11f9f0ed 2403 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2404 check_hint("t0", 123); check_hint("t1", 456);
2405 store_hint("t1", 654);
11f9f0ed 2406 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2407 check_hint("t0", 123); check_hint("t1", 654);
2408 LEAVE;
11f9f0ed 2409 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
2410 check_hint("t0", 123); check_hint("t1", 456);
2411 LEAVE;
2412#undef store_hint
2413#undef hint_ok
2414#undef check_hint
2415
2416void
2417test_copyhints()
2418 PREINIT:
2419 HV *a, *b;
2420 CODE:
2421 PL_hints |= HINT_LOCALIZE_HH;
2422 ENTER;
2423 SAVEHINTS();
2424 sv_setiv_mg(*hv_fetchs(GvHV(PL_hintgv), "t0", 1), 123);
20439bc7
Z
2425 if (SvIV(cop_hints_fetch_pvs(&PL_compiling, "t0", 0)) != 123)
2426 croak_fail();
defdfed5
Z
2427 a = newHVhv(GvHV(PL_hintgv));
2428 sv_2mortal((SV*)a);
2429 sv_setiv_mg(*hv_fetchs(a, "t0", 1), 456);
20439bc7
Z
2430 if (SvIV(cop_hints_fetch_pvs(&PL_compiling, "t0", 0)) != 123)
2431 croak_fail();
defdfed5
Z
2432 b = hv_copy_hints_hv(a);
2433 sv_2mortal((SV*)b);
2434 sv_setiv_mg(*hv_fetchs(b, "t0", 1), 789);
20439bc7
Z
2435 if (SvIV(cop_hints_fetch_pvs(&PL_compiling, "t0", 0)) != 789)
2436 croak_fail();
defdfed5
Z
2437 LEAVE;
2438
201c7e1f 2439void
2fcb4757
Z
2440test_op_list()
2441 PREINIT:
2442 OP *a;
2443 CODE:
2444#define iv_op(iv) newSVOP(OP_CONST, 0, newSViv(iv))
2445#define check_op(o, expect) \
2446 do { \
2447 if (strcmp(test_op_list_describe(o), (expect))) \
2448 croak("fail %s %s", test_op_list_describe(o), (expect)); \
2449 } while(0)
2450 a = op_append_elem(OP_LIST, NULL, NULL);
2451 check_op(a, "");
2452 a = op_append_elem(OP_LIST, iv_op(1), a);
2453 check_op(a, "const(1).");
2454 a = op_append_elem(OP_LIST, NULL, a);
2455 check_op(a, "const(1).");
2456 a = op_append_elem(OP_LIST, a, iv_op(2));
2457 check_op(a, "list[pushmark.const(1).const(2).]");
2458 a = op_append_elem(OP_LIST, a, iv_op(3));
2459 check_op(a, "list[pushmark.const(1).const(2).const(3).]");
2460 a = op_append_elem(OP_LIST, a, NULL);
2461 check_op(a, "list[pushmark.const(1).const(2).const(3).]");
2462 a = op_append_elem(OP_LIST, NULL, a);
2463 check_op(a, "list[pushmark.const(1).const(2).const(3).]");
2464 a = op_append_elem(OP_LIST, iv_op(4), a);
2465 check_op(a, "list[pushmark.const(4)."
2466 "list[pushmark.const(1).const(2).const(3).]]");
2467 a = op_append_elem(OP_LIST, a, iv_op(5));
2468 check_op(a, "list[pushmark.const(4)."
2469 "list[pushmark.const(1).const(2).const(3).]const(5).]");
2470 a = op_append_elem(OP_LIST, a,
2471 op_append_elem(OP_LIST, iv_op(7), iv_op(6)));
2472 check_op(a, "list[pushmark.const(4)."
2473 "list[pushmark.const(1).const(2).const(3).]const(5)."
2474 "list[pushmark.const(7).const(6).]]");
2475 op_free(a);
2476 a = op_append_elem(OP_LINESEQ, iv_op(1), iv_op(2));
2477 check_op(a, "lineseq[const(1).const(2).]");
2478 a = op_append_elem(OP_LINESEQ, a, iv_op(3));
2479 check_op(a, "lineseq[const(1).const(2).const(3).]");
2480 op_free(a);
2481 a = op_append_elem(OP_LINESEQ,
2482 op_append_elem(OP_LIST, iv_op(1), iv_op(2)),
2483 iv_op(3));
2484 check_op(a, "lineseq[list[pushmark.const(1).const(2).]const(3).]");
2485 op_free(a);
2486 a = op_prepend_elem(OP_LIST, NULL, NULL);
2487 check_op(a, "");
2488 a = op_prepend_elem(OP_LIST, a, iv_op(1));
2489 check_op(a, "const(1).");
2490 a = op_prepend_elem(OP_LIST, a, NULL);
2491 check_op(a, "const(1).");
2492 a = op_prepend_elem(OP_LIST, iv_op(2), a);
2493 check_op(a, "list[pushmark.const(2).const(1).]");
2494 a = op_prepend_elem(OP_LIST, iv_op(3), a);
2495 check_op(a, "list[pushmark.const(3).const(2).const(1).]");
2496 a = op_prepend_elem(OP_LIST, NULL, a);
2497 check_op(a, "list[pushmark.const(3).const(2).const(1).]");
2498 a = op_prepend_elem(OP_LIST, a, NULL);
2499 check_op(a, "list[pushmark.const(3).const(2).const(1).]");
2500 a = op_prepend_elem(OP_LIST, a, iv_op(4));
2501 check_op(a, "list[pushmark."
2502 "list[pushmark.const(3).const(2).const(1).]const(4).]");
2503 a = op_prepend_elem(OP_LIST, iv_op(5), a);
2504 check_op(a, "list[pushmark.const(5)."
2505 "list[pushmark.const(3).const(2).const(1).]const(4).]");
2506 a = op_prepend_elem(OP_LIST,
2507 op_prepend_elem(OP_LIST, iv_op(6), iv_op(7)), a);
2508 check_op(a, "list[pushmark.list[pushmark.const(6).const(7).]const(5)."
2509 "list[pushmark.const(3).const(2).const(1).]const(4).]");
2510 op_free(a);
2511 a = op_prepend_elem(OP_LINESEQ, iv_op(2), iv_op(1));
2512 check_op(a, "lineseq[const(2).const(1).]");
2513 a = op_prepend_elem(OP_LINESEQ, iv_op(3), a);
2514 check_op(a, "lineseq[const(3).const(2).const(1).]");
2515 op_free(a);
2516 a = op_prepend_elem(OP_LINESEQ, iv_op(3),
2517 op_prepend_elem(OP_LIST, iv_op(2), iv_op(1)));
2518 check_op(a, "lineseq[const(3).list[pushmark.const(2).const(1).]]");
2519 op_free(a);
2520 a = op_append_list(OP_LINESEQ, NULL, NULL);
2521 check_op(a, "");
2522 a = op_append_list(OP_LINESEQ, iv_op(1), a);
2523 check_op(a, "const(1).");
2524 a = op_append_list(OP_LINESEQ, NULL, a);
2525 check_op(a, "const(1).");
2526 a = op_append_list(OP_LINESEQ, a, iv_op(2));
2527 check_op(a, "lineseq[const(1).const(2).]");
2528 a = op_append_list(OP_LINESEQ, a, iv_op(3));
2529 check_op(a, "lineseq[const(1).const(2).const(3).]");
2530 a = op_append_list(OP_LINESEQ, iv_op(4), a);
2531 check_op(a, "lineseq[const(4).const(1).const(2).const(3).]");
2532 a = op_append_list(OP_LINESEQ, a, NULL);
2533 check_op(a, "lineseq[const(4).const(1).const(2).const(3).]");
2534 a = op_append_list(OP_LINESEQ, NULL, a);
2535 check_op(a, "lineseq[const(4).const(1).const(2).const(3).]");
2536 a = op_append_list(OP_LINESEQ, a,
2537 op_append_list(OP_LINESEQ, iv_op(5), iv_op(6)));
2538 check_op(a, "lineseq[const(4).const(1).const(2).const(3)."
2539 "const(5).const(6).]");
2540 op_free(a);
2541 a = op_append_list(OP_LINESEQ,
2542 op_append_list(OP_LINESEQ, iv_op(1), iv_op(2)),
2543 op_append_list(OP_LIST, iv_op(3), iv_op(4)));
2544 check_op(a, "lineseq[const(1).const(2)."
2545 "list[pushmark.const(3).const(4).]]");
2546 op_free(a);
2547 a = op_append_list(OP_LINESEQ,
2548 op_append_list(OP_LIST, iv_op(1), iv_op(2)),
2549 op_append_list(OP_LINESEQ, iv_op(3), iv_op(4)));
2550 check_op(a, "lineseq[list[pushmark.const(1).const(2).]"
2551 "const(3).const(4).]");
2552 op_free(a);
2fcb4757
Z
2553#undef check_op
2554
2555void
5983a79d
BM
2556test_op_linklist ()
2557 PREINIT:
2558 OP *o;
2559 CODE:
2560#define check_ll(o, expect) \
2561 STMT_START { \
2562 if (strNE(test_op_linklist_describe(o), (expect))) \
2563 croak("fail %s %s", test_op_linklist_describe(o), (expect)); \
2564 } STMT_END
2565 o = iv_op(1);
2566 check_ll(o, ".const1");
2567 op_free(o);
2568
2569 o = mkUNOP(OP_NOT, iv_op(1));
2570 check_ll(o, ".const1.not");
2571 op_free(o);
2572
2573 o = mkUNOP(OP_NOT, mkUNOP(OP_NEGATE, iv_op(1)));
2574 check_ll(o, ".const1.negate.not");
2575 op_free(o);
2576
2577 o = mkBINOP(OP_ADD, iv_op(1), iv_op(2));
2578 check_ll(o, ".const1.const2.add");
2579 op_free(o);
2580
2581 o = mkBINOP(OP_ADD, mkUNOP(OP_NOT, iv_op(1)), iv_op(2));
2582 check_ll(o, ".const1.not.const2.add");
2583 op_free(o);
2584
2585 o = mkUNOP(OP_NOT, mkBINOP(OP_ADD, iv_op(1), iv_op(2)));
2586 check_ll(o, ".const1.const2.add.not");
2587 op_free(o);
2588
2589 o = mkLISTOP(OP_LINESEQ, iv_op(1), iv_op(2), iv_op(3));
2590 check_ll(o, ".const1.const2.const3.lineseq");
2591 op_free(o);
2592
2593 o = mkLISTOP(OP_LINESEQ,
2594 mkBINOP(OP_ADD, iv_op(1), iv_op(2)),
2595 mkUNOP(OP_NOT, iv_op(3)),
2596 mkLISTOP(OP_SUBSTR, iv_op(4), iv_op(5), iv_op(6)));
2597 check_ll(o, ".const1.const2.add.const3.not"
2598 ".const4.const5.const6.substr.lineseq");
2599 op_free(o);
2600
2601 o = mkBINOP(OP_ADD, iv_op(1), iv_op(2));
2602 LINKLIST(o);
2603 o = mkBINOP(OP_SUBTRACT, o, iv_op(3));
2604 check_ll(o, ".const1.const2.add.const3.subtract");
2605 op_free(o);
2606#undef check_ll
2607#undef iv_op
2608
2609void
201c7e1f
FR
2610peep_enable ()
2611 PREINIT:
2612 dMY_CXT;
2613 CODE:
2614 av_clear(MY_CXT.peep_recorder);
2615 av_clear(MY_CXT.rpeep_recorder);
2616 MY_CXT.peep_recording = 1;
2617
2618void
2619peep_disable ()
2620 PREINIT:
2621 dMY_CXT;
2622 CODE:
2623 MY_CXT.peep_recording = 0;
2624
2625SV *
2626peep_record ()
2627 PREINIT:
2628 dMY_CXT;
2629 CODE:
95d2461a 2630 RETVAL = newRV_inc((SV *)MY_CXT.peep_recorder);
201c7e1f
FR
2631 OUTPUT:
2632 RETVAL
2633
2634SV *
2635rpeep_record ()
2636 PREINIT:
2637 dMY_CXT;
2638 CODE:
95d2461a 2639 RETVAL = newRV_inc((SV *)MY_CXT.rpeep_recorder);
201c7e1f
FR
2640 OUTPUT:
2641 RETVAL
2642
9c540340
DM
2643=pod
2644
2645multicall_each: call a sub for each item in the list. Used to test MULTICALL
2646
2647=cut
2648
2649void
2650multicall_each(block,...)
2651 SV * block
2652PROTOTYPE: &@
2653CODE:
2654{
2655 dMULTICALL;
2656 int index;
2657 GV *gv;
2658 HV *stash;
2659 I32 gimme = G_SCALAR;
2660 SV **args = &PL_stack_base[ax];
2661 CV *cv;
2662
2663 if(items <= 1) {
2664 XSRETURN_UNDEF;
2665 }
2666 cv = sv_2cv(block, &stash, &gv, 0);
2667 if (cv == Nullcv) {
2668 croak("multicall_each: not a subroutine reference");
2669 }
2670 PUSH_MULTICALL(cv);
2671 SAVESPTR(GvSV(PL_defgv));
2672
2673 for(index = 1 ; index < items ; index++) {
2674 GvSV(PL_defgv) = args[index];
2675 MULTICALL;
2676 }
2677 POP_MULTICALL;
2678 XSRETURN_UNDEF;
2679}
2680
7b81e549
GG
2681#ifdef USE_ITHREADS
2682
2683void
2684clone_with_stack()
2685CODE:
2686{
2687 PerlInterpreter *interp = aTHX; /* The original interpreter */
2688 PerlInterpreter *interp_dup; /* The duplicate interpreter */
2689 int oldscope = 1; /* We are responsible for all scopes */
2690
2691 interp_dup = perl_clone(interp, CLONEf_COPY_STACKS | CLONEf_CLONE_HOST );
2692
2693 /* destroy old perl */
2694 PERL_SET_CONTEXT(interp);
2695
2696 POPSTACK_TO(PL_mainstack);
2697 dounwind(-1);
2698 LEAVE_SCOPE(0);
2699
2700 while (interp->Iscopestack_ix > 1)
2701 LEAVE;
2702 FREETMPS;
2703
2704 perl_destruct(interp);
2705 perl_free(interp);
2706
2707 /* switch to new perl */
2708 PERL_SET_CONTEXT(interp_dup);
2709
2710 /* continue after 'clone_with_stack' */
2711 interp_dup->Iop = interp_dup->Iop->op_next;
2712
2713 /* run with new perl */
2714 Perl_runops_standard(interp_dup);
2715
2716 /* We may have additional unclosed scopes if fork() was called
2717 * from within a BEGIN block. See perlfork.pod for more details.
2718 * We cannot clean up these other scopes because they belong to a
2719 * different interpreter, but we also cannot leave PL_scopestack_ix
2720 * dangling because that can trigger an assertion in perl_destruct().
2721 */
2722 if (PL_scopestack_ix > oldscope) {
2723 PL_scopestack[oldscope-1] = PL_scopestack[PL_scopestack_ix-1];
2724 PL_scopestack_ix = oldscope;
2725 }
2726
2727 perl_destruct(interp_dup);
2728 perl_free(interp_dup);
2729
2730 /* call the real 'exit' not PerlProc_exit */
2731#undef exit
2732 exit(0);
2733}
2734
2735#endif /* USE_ITHREDS */
9c540340 2736
88b5a879
GF
2737SV*
2738take_svref(SVREF sv)
2739CODE:
2740 RETVAL = newRV_inc(sv);
2741OUTPUT:
2742 RETVAL
2743
2744SV*
2745take_avref(AV* av)
2746CODE:
2747 RETVAL = newRV_inc((SV*)av);
2748OUTPUT:
2749 RETVAL
2750
2751SV*
2752take_hvref(HV* hv)
2753CODE:
2754 RETVAL = newRV_inc((SV*)hv);
2755OUTPUT:
2756 RETVAL
2757
2758
2759SV*
2760take_cvref(CV* cv)
2761CODE:
2762 RETVAL = newRV_inc((SV*)cv);
2763OUTPUT:
2764 RETVAL
2765
2766
e2fe06dd
EB
2767BOOT:
2768 {
2769 HV* stash;
2770 SV** meth = NULL;
2771 CV* cv;
2772 stash = gv_stashpv("XS::APItest::TempLv", 0);
2773 if (stash)
2774 meth = hv_fetchs(stash, "make_temp_mg_lv", 0);
2775 if (!meth)
2776 croak("lost method 'make_temp_mg_lv'");
2777 cv = GvCV(*meth);
2778 CvLVALUE_on(cv);
2779 }
83f8bb56
Z
2780
2781BOOT:
2782{
2783 hintkey_rpn_sv = newSVpvs_share("XS::APItest/rpn");
2784 hintkey_calcrpn_sv = newSVpvs_share("XS::APItest/calcrpn");
2785 hintkey_stufftest_sv = newSVpvs_share("XS::APItest/stufftest");
2786 hintkey_swaptwostmts_sv = newSVpvs_share("XS::APItest/swaptwostmts");
07ffcb73 2787 hintkey_looprest_sv = newSVpvs_share("XS::APItest/looprest");
a7aaec61 2788 hintkey_scopelessblock_sv = newSVpvs_share("XS::APItest/scopelessblock");
9eb5c532
Z
2789 hintkey_stmtasexpr_sv = newSVpvs_share("XS::APItest/stmtasexpr");
2790 hintkey_stmtsasexpr_sv = newSVpvs_share("XS::APItest/stmtsasexpr");
e53d8f76
Z
2791 hintkey_loopblock_sv = newSVpvs_share("XS::APItest/loopblock");
2792 hintkey_blockasexpr_sv = newSVpvs_share("XS::APItest/blockasexpr");
361d9b55
Z
2793 hintkey_swaplabel_sv = newSVpvs_share("XS::APItest/swaplabel");
2794 hintkey_labelconst_sv = newSVpvs_share("XS::APItest/labelconst");
78cdf107
Z
2795 hintkey_arrayfullexpr_sv = newSVpvs_share("XS::APItest/arrayfullexpr");
2796 hintkey_arraylistexpr_sv = newSVpvs_share("XS::APItest/arraylistexpr");
2797 hintkey_arraytermexpr_sv = newSVpvs_share("XS::APItest/arraytermexpr");
2798 hintkey_arrayarithexpr_sv = newSVpvs_share("XS::APItest/arrayarithexpr");
2799 hintkey_arrayexprflags_sv = newSVpvs_share("XS::APItest/arrayexprflags");
83f8bb56
Z
2800 next_keyword_plugin = PL_keyword_plugin;
2801 PL_keyword_plugin = my_keyword_plugin;
2802}
8f89e5a9
Z
2803
2804void
2805establish_cleanup(...)
2806PROTOTYPE: $
2807CODE:
c33e8be1 2808 PERL_UNUSED_VAR(items);
8f89e5a9
Z
2809 croak("establish_cleanup called as a function");
2810
2811BOOT:
2812{
2813 CV *estcv = get_cv("XS::APItest::establish_cleanup", 0);
2814 cv_set_call_checker(estcv, THX_ck_entersub_establish_cleanup, (SV*)estcv);
2815}
3ad73efd
Z
2816
2817void
2818postinc(...)
2819PROTOTYPE: $
2820CODE:
c33e8be1 2821 PERL_UNUSED_VAR(items);
3ad73efd
Z
2822 croak("postinc called as a function");
2823
27fcb6ee
FC
2824void
2825filter()
2826CODE:
2827 filter_add(filter_call, NULL);
2828
3ad73efd
Z
2829BOOT:
2830{
2831 CV *asscv = get_cv("XS::APItest::postinc", 0);
2832 cv_set_call_checker(asscv, THX_ck_entersub_postinc, (SV*)asscv);
2833}
26ab20ee 2834
fba8e77b
FC
2835SV *
2836lv_temp_object()
2837CODE:
2838 RETVAL =
2839 sv_bless(
2840 newRV_noinc(newSV(0)),
2841 gv_stashpvs("XS::APItest::TempObj",GV_ADD)
2842 ); /* Package defined in test script */
2843OUTPUT:
2844 RETVAL
2845
7d6175ef
FC
2846void
2847fill_hash_with_nulls(HV *hv)
a2bf5ab1 2848PREINIT:
7d6175ef 2849 UV i = 0;
a2bf5ab1 2850CODE:
7d6175ef
FC
2851 for(; i < 1000; ++i) {
2852 HE *entry = hv_fetch_ent(hv, sv_2mortal(newSVuv(i)), 1, 0);
2853 SvREFCNT_dec(HeVAL(entry));
2854 HeVAL(entry) = NULL;
2855 }
2856
fa819c1c
FC
2857bool
2858SvIsCOW(SV *sv)
2859CODE:
2860 RETVAL = SvIsCOW(sv);
2861OUTPUT:
2862 RETVAL
2863
c12735bb
Z
2864STRLEN
2865underscore_length()
2866PROTOTYPE:
2867PREINIT:
2868 SV *u;
2869 U8 *pv;
2870 STRLEN bytelen;
2871CODE:
2872 u = find_rundefsv();
2873 pv = (U8*)SvPV(u, bytelen);
2874 RETVAL = SvUTF8(u) ? utf8_length(pv, pv+bytelen) : bytelen;
2875OUTPUT:
2876 RETVAL
2877
26ab20ee
FR
2878MODULE = XS::APItest PACKAGE = XS::APItest::Magic
2879
2880PROTOTYPES: DISABLE
2881
2882void
2883sv_magic_foo(SV *sv, SV *thingy)
2884ALIAS:
2885 sv_magic_bar = 1
2886CODE:
2887 sv_magicext(SvRV(sv), NULL, PERL_MAGIC_ext, ix ? &vtbl_bar : &vtbl_foo, (const char *)thingy, 0);
2888
2889SV *
2890mg_find_foo(SV *sv)
2891ALIAS:
2892 mg_find_bar = 1
2893CODE:
2894 MAGIC *mg = mg_findext(SvRV(sv), PERL_MAGIC_ext, ix ? &vtbl_bar : &vtbl_foo);
2895 RETVAL = mg ? SvREFCNT_inc((SV *)mg->mg_ptr) : &PL_sv_undef;
2896OUTPUT:
2897 RETVAL
2898
2899void
2900sv_unmagic_foo(SV *sv)
2901ALIAS:
2902 sv_unmagic_bar = 1
2903CODE:
2904 sv_unmagicext(SvRV(sv), PERL_MAGIC_ext, ix ? &vtbl_bar : &vtbl_foo);
406700cc
NC
2905
2906UV
2907test_get_vtbl()
2908 PREINIT:
2909 MGVTBL *have;
2910 MGVTBL *want;
2911 CODE:
2912#define test_get_this_vtable(name) \
2913 want = CAT2(&PL_vtbl_, name); \
2914 have = get_vtbl(CAT2(want_vtbl_, name)); \
2915 if (have != want) \
2916 croak("fail %p!=%p for get_vtbl(want_vtbl_" STRINGIFY(name) ") at " __FILE__ " line %d", have, want, __LINE__)
2917
2918 test_get_this_vtable(sv);
2919 test_get_this_vtable(env);
2920 test_get_this_vtable(envelem);
2921 test_get_this_vtable(sigelem);
2922 test_get_this_vtable(pack);
2923 test_get_this_vtable(packelem);
2924 test_get_this_vtable(dbline);
2925 test_get_this_vtable(isa);
2926 test_get_this_vtable(isaelem);
2927 test_get_this_vtable(arylen);
2928 test_get_this_vtable(mglob);
2929 test_get_this_vtable(nkeys);
2930 test_get_this_vtable(taint);
2931 test_get_this_vtable(substr);
2932 test_get_this_vtable(vec);
2933 test_get_this_vtable(pos);
2934 test_get_this_vtable(bm);
2935 test_get_this_vtable(fm);
2936 test_get_this_vtable(uvar);
2937 test_get_this_vtable(defelem);
2938 test_get_this_vtable(regexp);
2939 test_get_this_vtable(regdata);
2940 test_get_this_vtable(regdatum);
2941#ifdef USE_LOCALE_COLLATE
2942 test_get_this_vtable(collxfrm);
2943#endif
2944 test_get_this_vtable(amagic);
2945 test_get_this_vtable(amagicelem);
2946 test_get_this_vtable(backref);
2947 test_get_this_vtable(utf8);
2948
2949 RETVAL = PTR2UV(get_vtbl(-1));
2950 OUTPUT:
2951 RETVAL