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