This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
APItest: Correct parameter sign
[perl5.git] / ext / XS-APItest / APItest.xs
CommitLineData
6a93a7e5 1#define PERL_IN_XS_APITEST
73e43954
HS
2
3/* We want to be able to test things that aren't API yet. */
4#define PERL_EXT
5
0d5fcae7
FC
6/* Do *not* define PERL_NO_GET_CONTEXT. This is the one place where we get
7 to test implicit Perl_get_context(). */
8
3e61d65a
JH
9#include "EXTERN.h"
10#include "perl.h"
11#include "XSUB.h"
765da300
FC
12
13typedef FILE NativeFile;
14
62df7c7d 15#include "fakesdio.h" /* Causes us to use PerlIO below */
3e61d65a 16
36c2b1d0
NC
17typedef SV *SVREF;
18typedef PTR_TBL_t *XS__APItest__PtrTable;
6da090e6
FC
19typedef PerlIO * InputStream;
20typedef PerlIO * OutputStream;
85ce96a1 21
11f9f0ed 22#define croak_fail() croak("fail at " __FILE__ " line %d", __LINE__)
36791795
Z
23#define croak_fail_nep(h, w) croak("fail %p!=%p at " __FILE__ " line %d", (h), (w), __LINE__)
24#define croak_fail_nei(h, w) croak("fail %d!=%d at " __FILE__ " line %d", (int)(h), (int)(w), __LINE__)
11f9f0ed 25
72900640
KW
26#ifdef EBCDIC
27
28void
29cat_utf8a2n(SV* sv, const char * const ascii_utf8, STRLEN len)
30{
31 /* Converts variant UTF-8 text pointed to by 'ascii_utf8' of length 'len',
32 * to UTF-EBCDIC, appending that text to the text already in 'sv'.
33 * Currently doesn't work on invariants, as that is unneeded here, and we
34 * could get double translations if we did.
35 *
36 * It has the algorithm for strict UTF-8 hard-coded in to find the code
37 * point it represents, then calls uvchr_to_utf8() to convert to
38 * UTF-EBCDIC).
39 *
40 * Note that this uses code points, not characters. Thus if the input is
41 * the UTF-8 for the code point 0xFF, the output will be the UTF-EBCDIC for
42 * 0xFF, even though that code point represents different characters on
43 * ASCII vs EBCDIC platforms. */
44
45 dTHX;
46 char * p = (char *) ascii_utf8;
47 const char * const e = p + len;
48
49 while (p < e) {
50 UV code_point;
51 U8 native_utf8[UTF8_MAXBYTES + 1];
52 U8 * char_end;
53 U8 start = (U8) *p;
54
55 /* Start bytes are the same in both UTF-8 and I8, therefore we can
56 * treat this ASCII UTF-8 byte as an I8 byte. But PL_utf8skip[] is
57 * indexed by NATIVE_UTF8 bytes, so transform to that */
58 STRLEN char_bytes_len = PL_utf8skip[I8_TO_NATIVE_UTF8(start)];
59
60 if (start < 0xc2) {
61 croak("fail: Expecting start byte, instead got 0x%X at %s line %d",
62 (U8) *p, __FILE__, __LINE__);
63 }
64 code_point = (start & (((char_bytes_len) >= 7)
65 ? 0x00
66 : (0x1F >> ((char_bytes_len)-2))));
67 p++;
68 while (p < e && ((( (U8) *p) & 0xC0) == 0x80)) {
69
70 code_point = (code_point << 6) | (( (U8) *p) & 0x3F);
71 p++;
72 }
73
74 char_end = uvchr_to_utf8(native_utf8, code_point);
75 sv_catpvn(sv, (char *) native_utf8, char_end - native_utf8);
76 }
77}
78
79#endif
80
85ce96a1
DM
81/* for my_cxt tests */
82
83#define MY_CXT_KEY "XS::APItest::_guts" XS_VERSION
84
85typedef struct {
86 int i;
87 SV *sv;
03569ecf
BM
88 GV *cscgv;
89 AV *cscav;
13b6b3bc
BM
90 AV *bhkav;
91 bool bhk_record;
201c7e1f
FR
92 peep_t orig_peep;
93 peep_t orig_rpeep;
94 int peep_recording;
95 AV *peep_recorder;
96 AV *rpeep_recorder;
f568d64d 97 AV *xop_record;
85ce96a1
DM
98} my_cxt_t;
99
100START_MY_CXT
101
80c1439f
DM
102int
103S_myset_set(pTHX_ SV* sv, MAGIC* mg)
104{
105 SV *isv = (SV*)mg->mg_ptr;
106
107 PERL_UNUSED_ARG(sv);
108 SvIVX(isv)++;
109 return 0;
110}
111
26ab20ee 112MGVTBL vtbl_foo, vtbl_bar;
80c1439f
DM
113MGVTBL vtbl_myset = { 0, S_myset_set, 0, 0, 0, 0, 0, 0 };
114
26ab20ee 115
85ce96a1 116/* indirect functions to test the [pa]MY_CXT macros */
f16dd614 117
85ce96a1
DM
118int
119my_cxt_getint_p(pMY_CXT)
120{
121 return MY_CXT.i;
122}
f16dd614 123
85ce96a1
DM
124void
125my_cxt_setint_p(pMY_CXT_ int i)
126{
127 MY_CXT.i = i;
128}
f16dd614
DM
129
130SV*
9568a123 131my_cxt_getsv_interp_context(void)
f16dd614 132{
f16dd614
DM
133 dTHX;
134 dMY_CXT_INTERP(my_perl);
9568a123
NC
135 return MY_CXT.sv;
136}
137
138SV*
139my_cxt_getsv_interp(void)
140{
f16dd614 141 dMY_CXT;
f16dd614
DM
142 return MY_CXT.sv;
143}
144
85ce96a1
DM
145void
146my_cxt_setsv_p(SV* sv _pMY_CXT)
147{
148 MY_CXT.sv = sv;
149}
150
151
9b5c3821 152/* from exception.c */
7a646707 153int apitest_exception(int);
0314122a 154
ff66e713
SH
155/* from core_or_not.inc */
156bool sv_setsv_cow_hashkey_core(void);
157bool sv_setsv_cow_hashkey_notcore(void);
158
2dc92170
NC
159/* A routine to test hv_delayfree_ent
160 (which itself is tested by testing on hv_free_ent */
161
5aaab254 162typedef void (freeent_function)(pTHX_ HV *, HE *);
2dc92170
NC
163
164void
2e66fe90 165test_freeent(freeent_function *f) {
2dc92170
NC
166 dSP;
167 HV *test_hash = newHV();
168 HE *victim;
169 SV *test_scalar;
170 U32 results[4];
171 int i;
172
8afd2d2e
NC
173#ifdef PURIFY
174 victim = (HE*)safemalloc(sizeof(HE));
175#else
2dc92170
NC
176 /* Storing then deleting something should ensure that a hash entry is
177 available. */
33e1c218
YO
178 (void) hv_stores(test_hash, "", &PL_sv_yes);
179 (void) hv_deletes(test_hash, "", 0);
2dc92170
NC
180
181 /* We need to "inline" new_he here as it's static, and the functions we
182 test expect to be able to call del_HE on the HE */
6a93a7e5 183 if (!PL_body_roots[HE_SVSLOT])
2dc92170 184 croak("PL_he_root is 0");
8a722a80 185 victim = (HE*) PL_body_roots[HE_SVSLOT];
6a93a7e5 186 PL_body_roots[HE_SVSLOT] = HeNEXT(victim);
8afd2d2e 187#endif
2dc92170
NC
188
189 victim->hent_hek = Perl_share_hek(aTHX_ "", 0, 0);
190
191 test_scalar = newSV(0);
192 SvREFCNT_inc(test_scalar);
de616631 193 HeVAL(victim) = test_scalar;
2dc92170
NC
194
195 /* Need this little game else we free the temps on the return stack. */
196 results[0] = SvREFCNT(test_scalar);
197 SAVETMPS;
198 results[1] = SvREFCNT(test_scalar);
199 f(aTHX_ test_hash, victim);
200 results[2] = SvREFCNT(test_scalar);
201 FREETMPS;
202 results[3] = SvREFCNT(test_scalar);
203
204 i = 0;
205 do {
04849b38 206 mXPUSHu(results[i]);
c33e8be1 207 } while (++i < (int)(sizeof(results)/sizeof(results[0])));
2dc92170
NC
208
209 /* Goodbye to our extra reference. */
210 SvREFCNT_dec(test_scalar);
211}
212
7425681b
KW
213/* Not that it matters much, but it's handy for the flipped character to just
214 * be the opposite case (at least for ASCII-range and most Latin1 as well). */
215#define FLIP_BIT ('A' ^ 'a')
b54b4831
NC
216
217static I32
53c40a8f
NC
218bitflip_key(pTHX_ IV action, SV *field) {
219 MAGIC *mg = mg_find(field, PERL_MAGIC_uvar);
220 SV *keysv;
c33e8be1 221 PERL_UNUSED_ARG(action);
53c40a8f
NC
222 if (mg && (keysv = mg->mg_obj)) {
223 STRLEN len;
224 const char *p = SvPV(keysv, len);
225
226 if (len) {
7425681b
KW
227 /* Allow for the flipped val to be longer than the original. This
228 * is just for testing, so can afford to have some slop */
229 const STRLEN newlen = len * 2;
230
231 SV *newkey = newSV(newlen);
232 const char * const new_p_orig = SvPVX(newkey);
233 char *new_p = (char *) new_p_orig;
53c40a8f
NC
234
235 if (SvUTF8(keysv)) {
236 const char *const end = p + len;
237 while (p < end) {
7425681b
KW
238 STRLEN curlen;
239 UV chr = utf8_to_uvchr_buf((U8 *)p, (U8 *) end, &curlen);
240
241 /* Make sure don't exceed bounds */
242 assert(new_p - new_p_orig + curlen < newlen);
243
244 new_p = (char *)uvchr_to_utf8((U8 *)new_p, chr ^ FLIP_BIT);
245 p += curlen;
53c40a8f
NC
246 }
247 SvUTF8_on(newkey);
248 } else {
249 while (len--)
7425681b 250 *new_p++ = *p++ ^ FLIP_BIT;
53c40a8f
NC
251 }
252 *new_p = '\0';
7425681b 253 SvCUR_set(newkey, new_p - new_p_orig);
53c40a8f
NC
254 SvPOK_on(newkey);
255
256 mg->mg_obj = newkey;
257 }
258 }
259 return 0;
260}
261
262static I32
b54b4831
NC
263rot13_key(pTHX_ IV action, SV *field) {
264 MAGIC *mg = mg_find(field, PERL_MAGIC_uvar);
265 SV *keysv;
c33e8be1 266 PERL_UNUSED_ARG(action);
b54b4831
NC
267 if (mg && (keysv = mg->mg_obj)) {
268 STRLEN len;
269 const char *p = SvPV(keysv, len);
270
271 if (len) {
272 SV *newkey = newSV(len);
273 char *new_p = SvPVX(newkey);
274
275 /* There's a deliberate fencepost error here to loop len + 1 times
276 to copy the trailing \0 */
277 do {
278 char new_c = *p++;
279 /* Try doing this cleanly and clearly in EBCDIC another way: */
280 switch (new_c) {
281 case 'A': new_c = 'N'; break;
282 case 'B': new_c = 'O'; break;
283 case 'C': new_c = 'P'; break;
284 case 'D': new_c = 'Q'; break;
285 case 'E': new_c = 'R'; break;
286 case 'F': new_c = 'S'; break;
287 case 'G': new_c = 'T'; break;
288 case 'H': new_c = 'U'; break;
289 case 'I': new_c = 'V'; break;
290 case 'J': new_c = 'W'; break;
291 case 'K': new_c = 'X'; break;
292 case 'L': new_c = 'Y'; break;
293 case 'M': new_c = 'Z'; break;
294 case 'N': new_c = 'A'; break;
295 case 'O': new_c = 'B'; break;
296 case 'P': new_c = 'C'; break;
297 case 'Q': new_c = 'D'; break;
298 case 'R': new_c = 'E'; break;
299 case 'S': new_c = 'F'; break;
300 case 'T': new_c = 'G'; break;
301 case 'U': new_c = 'H'; break;
302 case 'V': new_c = 'I'; break;
303 case 'W': new_c = 'J'; break;
304 case 'X': new_c = 'K'; break;
305 case 'Y': new_c = 'L'; break;
306 case 'Z': new_c = 'M'; break;
307 case 'a': new_c = 'n'; break;
308 case 'b': new_c = 'o'; break;
309 case 'c': new_c = 'p'; break;
310 case 'd': new_c = 'q'; break;
311 case 'e': new_c = 'r'; break;
312 case 'f': new_c = 's'; break;
313 case 'g': new_c = 't'; break;
314 case 'h': new_c = 'u'; break;
315 case 'i': new_c = 'v'; break;
316 case 'j': new_c = 'w'; break;
317 case 'k': new_c = 'x'; break;
318 case 'l': new_c = 'y'; break;
319 case 'm': new_c = 'z'; break;
320 case 'n': new_c = 'a'; break;
321 case 'o': new_c = 'b'; break;
322 case 'p': new_c = 'c'; break;
323 case 'q': new_c = 'd'; break;
324 case 'r': new_c = 'e'; break;
325 case 's': new_c = 'f'; break;
326 case 't': new_c = 'g'; break;
327 case 'u': new_c = 'h'; break;
328 case 'v': new_c = 'i'; break;
329 case 'w': new_c = 'j'; break;
330 case 'x': new_c = 'k'; break;
331 case 'y': new_c = 'l'; break;
332 case 'z': new_c = 'm'; break;
333 }
334 *new_p++ = new_c;
335 } while (len--);
336 SvCUR_set(newkey, SvCUR(keysv));
337 SvPOK_on(newkey);
338 if (SvUTF8(keysv))
339 SvUTF8_on(newkey);
340
341 mg->mg_obj = newkey;
342 }
343 }
344 return 0;
345}
346
218787bd
VP
347STATIC I32
348rmagical_a_dummy(pTHX_ IV idx, SV *sv) {
c33e8be1
Z
349 PERL_UNUSED_ARG(idx);
350 PERL_UNUSED_ARG(sv);
218787bd
VP
351 return 0;
352}
353
50495f39
JH
354/* We could do "= { 0 };" but some versions of gcc do warn
355 * (with -Wextra) about missing initializer, this is probably gcc
356 * being a bit too paranoid. But since this is file-static, we can
357 * just have it without initializer, since it should get
358 * zero-initialized. */
359STATIC MGVTBL rmagical_b;
218787bd 360
03569ecf 361STATIC void
13b6b3bc 362blockhook_csc_start(pTHX_ int full)
03569ecf
BM
363{
364 dMY_CXT;
365 AV *const cur = GvAV(MY_CXT.cscgv);
366
c33e8be1 367 PERL_UNUSED_ARG(full);
03569ecf
BM
368 SAVEGENERICSV(GvAV(MY_CXT.cscgv));
369
370 if (cur) {
371 I32 i;
d024465f 372 AV *const new_av = newAV();
03569ecf 373
b9f2b683 374 for (i = 0; i <= av_tindex(cur); i++) {
d024465f 375 av_store(new_av, i, newSVsv(*av_fetch(cur, i, 0)));
03569ecf
BM
376 }
377
d024465f 378 GvAV(MY_CXT.cscgv) = new_av;
03569ecf
BM
379 }
380}
381
382STATIC void
13b6b3bc 383blockhook_csc_pre_end(pTHX_ OP **o)
03569ecf
BM
384{
385 dMY_CXT;
386
c33e8be1 387 PERL_UNUSED_ARG(o);
03569ecf
BM
388 /* if we hit the end of a scope we missed the start of, we need to
389 * unconditionally clear @CSC */
52db365a 390 if (GvAV(MY_CXT.cscgv) == MY_CXT.cscav && MY_CXT.cscav) {
03569ecf 391 av_clear(MY_CXT.cscav);
52db365a 392 }
03569ecf
BM
393
394}
395
13b6b3bc
BM
396STATIC void
397blockhook_test_start(pTHX_ int full)
398{
399 dMY_CXT;
400 AV *av;
401
402 if (MY_CXT.bhk_record) {
403 av = newAV();
404 av_push(av, newSVpvs("start"));
405 av_push(av, newSViv(full));
406 av_push(MY_CXT.bhkav, newRV_noinc(MUTABLE_SV(av)));
407 }
408}
409
410STATIC void
411blockhook_test_pre_end(pTHX_ OP **o)
412{
413 dMY_CXT;
414
c33e8be1 415 PERL_UNUSED_ARG(o);
13b6b3bc
BM
416 if (MY_CXT.bhk_record)
417 av_push(MY_CXT.bhkav, newSVpvs("pre_end"));
418}
419
420STATIC void
421blockhook_test_post_end(pTHX_ OP **o)
422{
423 dMY_CXT;
424
c33e8be1 425 PERL_UNUSED_ARG(o);
13b6b3bc
BM
426 if (MY_CXT.bhk_record)
427 av_push(MY_CXT.bhkav, newSVpvs("post_end"));
428}
429
430STATIC void
431blockhook_test_eval(pTHX_ OP *const o)
432{
433 dMY_CXT;
434 AV *av;
435
436 if (MY_CXT.bhk_record) {
437 av = newAV();
438 av_push(av, newSVpvs("eval"));
439 av_push(av, newSVpv(OP_NAME(o), 0));
440 av_push(MY_CXT.bhkav, newRV_noinc(MUTABLE_SV(av)));
441 }
442}
443
444STATIC BHK bhk_csc, bhk_test;
445
201c7e1f
FR
446STATIC void
447my_peep (pTHX_ OP *o)
448{
449 dMY_CXT;
450
451 if (!o)
452 return;
453
454 MY_CXT.orig_peep(aTHX_ o);
455
456 if (!MY_CXT.peep_recording)
457 return;
458
459 for (; o; o = o->op_next) {
460 if (o->op_type == OP_CONST && cSVOPx_sv(o) && SvPOK(cSVOPx_sv(o))) {
461 av_push(MY_CXT.peep_recorder, newSVsv(cSVOPx_sv(o)));
462 }
463 }
464}
465
466STATIC void
467my_rpeep (pTHX_ OP *o)
468{
469 dMY_CXT;
470
471 if (!o)
472 return;
473
474 MY_CXT.orig_rpeep(aTHX_ o);
475
476 if (!MY_CXT.peep_recording)
477 return;
478
479 for (; o; o = o->op_next) {
480 if (o->op_type == OP_CONST && cSVOPx_sv(o) && SvPOK(cSVOPx_sv(o))) {
481 av_push(MY_CXT.rpeep_recorder, newSVsv(cSVOPx_sv(o)));
482 }
483 }
484}
485
d9088386
Z
486STATIC OP *
487THX_ck_entersub_args_lists(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
488{
c33e8be1
Z
489 PERL_UNUSED_ARG(namegv);
490 PERL_UNUSED_ARG(ckobj);
d9088386
Z
491 return ck_entersub_args_list(entersubop);
492}
493
494STATIC OP *
495THX_ck_entersub_args_scalars(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
496{
497 OP *aop = cUNOPx(entersubop)->op_first;
c33e8be1
Z
498 PERL_UNUSED_ARG(namegv);
499 PERL_UNUSED_ARG(ckobj);
e6dae479 500 if (!OpHAS_SIBLING(aop))
d9088386 501 aop = cUNOPx(aop)->op_first;
e6dae479 502 for (aop = OpSIBLING(aop); OpHAS_SIBLING(aop); aop = OpSIBLING(aop)) {
d9088386
Z
503 op_contextualize(aop, G_SCALAR);
504 }
505 return entersubop;
506}
507
508STATIC OP *
509THX_ck_entersub_multi_sum(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
510{
511 OP *sumop = NULL;
3253bf85 512 OP *parent = entersubop;
d9088386 513 OP *pushop = cUNOPx(entersubop)->op_first;
c33e8be1
Z
514 PERL_UNUSED_ARG(namegv);
515 PERL_UNUSED_ARG(ckobj);
e6dae479 516 if (!OpHAS_SIBLING(pushop)) {
3253bf85 517 parent = pushop;
d9088386 518 pushop = cUNOPx(pushop)->op_first;
3253bf85 519 }
d9088386 520 while (1) {
e6dae479
FC
521 OP *aop = OpSIBLING(pushop);
522 if (!OpHAS_SIBLING(aop))
d9088386 523 break;
3253bf85
DM
524 /* cut out first arg */
525 op_sibling_splice(parent, pushop, 1, NULL);
d9088386
Z
526 op_contextualize(aop, G_SCALAR);
527 if (sumop) {
528 sumop = newBINOP(OP_ADD, 0, sumop, aop);
529 } else {
530 sumop = aop;
531 }
532 }
533 if (!sumop)
534 sumop = newSVOP(OP_CONST, 0, newSViv(0));
535 op_free(entersubop);
536 return sumop;
537}
538
2e66fe90 539STATIC void test_op_list_describe_part(SV *res, OP *o);
2fcb4757 540STATIC void
2e66fe90 541test_op_list_describe_part(SV *res, OP *o)
2fcb4757
Z
542{
543 sv_catpv(res, PL_op_name[o->op_type]);
544 switch (o->op_type) {
545 case OP_CONST: {
546 sv_catpvf(res, "(%d)", (int)SvIV(cSVOPx(o)->op_sv));
547 } break;
548 }
549 if (o->op_flags & OPf_KIDS) {
550 OP *k;
551 sv_catpvs(res, "[");
e6dae479 552 for (k = cUNOPx(o)->op_first; k; k = OpSIBLING(k))
2e66fe90 553 test_op_list_describe_part(res, k);
2fcb4757
Z
554 sv_catpvs(res, "]");
555 } else {
556 sv_catpvs(res, ".");
557 }
558}
559
560STATIC char *
2e66fe90 561test_op_list_describe(OP *o)
2fcb4757
Z
562{
563 SV *res = sv_2mortal(newSVpvs(""));
564 if (o)
2e66fe90 565 test_op_list_describe_part(res, o);
2fcb4757
Z
566 return SvPVX(res);
567}
568
b7b1e41b 569/* the real new*OP functions have a tendency to call fold_constants, and
5983a79d
BM
570 * other such unhelpful things, so we need our own versions for testing */
571
572#define mkUNOP(t, f) THX_mkUNOP(aTHX_ (t), (f))
573static OP *
574THX_mkUNOP(pTHX_ U32 type, OP *first)
575{
576 UNOP *unop;
577 NewOp(1103, unop, 1, UNOP);
578 unop->op_type = (OPCODE)type;
29e61fd9 579 op_sibling_splice((OP*)unop, NULL, 0, first);
5983a79d
BM
580 return (OP *)unop;
581}
582
583#define mkBINOP(t, f, l) THX_mkBINOP(aTHX_ (t), (f), (l))
584static OP *
585THX_mkBINOP(pTHX_ U32 type, OP *first, OP *last)
586{
587 BINOP *binop;
588 NewOp(1103, binop, 1, BINOP);
589 binop->op_type = (OPCODE)type;
29e61fd9
DM
590 op_sibling_splice((OP*)binop, NULL, 0, last);
591 op_sibling_splice((OP*)binop, NULL, 0, first);
5983a79d
BM
592 return (OP *)binop;
593}
594
595#define mkLISTOP(t, f, s, l) THX_mkLISTOP(aTHX_ (t), (f), (s), (l))
596static OP *
597THX_mkLISTOP(pTHX_ U32 type, OP *first, OP *sib, OP *last)
598{
599 LISTOP *listop;
600 NewOp(1103, listop, 1, LISTOP);
601 listop->op_type = (OPCODE)type;
29e61fd9
DM
602 op_sibling_splice((OP*)listop, NULL, 0, last);
603 op_sibling_splice((OP*)listop, NULL, 0, sib);
604 op_sibling_splice((OP*)listop, NULL, 0, first);
5983a79d
BM
605 return (OP *)listop;
606}
607
608static char *
2e66fe90 609test_op_linklist_describe(OP *start)
5983a79d
BM
610{
611 SV *rv = sv_2mortal(newSVpvs(""));
612 OP *o;
613 o = start = LINKLIST(start);
614 do {
615 sv_catpvs(rv, ".");
616 sv_catpv(rv, OP_NAME(o));
617 if (o->op_type == OP_CONST)
618 sv_catsv(rv, cSVOPo->op_sv);
619 o = o->op_next;
620 } while (o && o != start);
621 return SvPVX(rv);
622}
623
8f89e5a9
Z
624/** establish_cleanup operator, ripped off from Scope::Cleanup **/
625
626STATIC void
627THX_run_cleanup(pTHX_ void *cleanup_code_ref)
628{
629 dSP;
a59fa18f 630 PUSHSTACK;
8f89e5a9
Z
631 ENTER;
632 SAVETMPS;
633 PUSHMARK(SP);
634 call_sv((SV*)cleanup_code_ref, G_VOID|G_DISCARD);
635 FREETMPS;
636 LEAVE;
a59fa18f 637 POPSTACK;
8f89e5a9
Z
638}
639
640STATIC OP *
641THX_pp_establish_cleanup(pTHX)
642{
643 dSP;
644 SV *cleanup_code_ref;
645 cleanup_code_ref = newSVsv(POPs);
646 SAVEFREESV(cleanup_code_ref);
647 SAVEDESTRUCTOR_X(THX_run_cleanup, cleanup_code_ref);
648 if(GIMME_V != G_VOID) PUSHs(&PL_sv_undef);
649 RETURN;
650}
651
652STATIC OP *
653THX_ck_entersub_establish_cleanup(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
654{
3253bf85 655 OP *parent, *pushop, *argop, *estop;
8f89e5a9 656 ck_entersub_args_proto(entersubop, namegv, ckobj);
3253bf85 657 parent = entersubop;
8f89e5a9 658 pushop = cUNOPx(entersubop)->op_first;
e6dae479 659 if(!OpHAS_SIBLING(pushop)) {
3253bf85 660 parent = pushop;
1ed44841 661 pushop = cUNOPx(pushop)->op_first;
3253bf85
DM
662 }
663 /* extract out first arg, then delete the rest of the tree */
e6dae479 664 argop = OpSIBLING(pushop);
3253bf85 665 op_sibling_splice(parent, pushop, 1, NULL);
8f89e5a9 666 op_free(entersubop);
3253bf85
DM
667
668 estop = mkUNOP(OP_RAND, argop);
8f89e5a9 669 estop->op_ppaddr = THX_pp_establish_cleanup;
8f89e5a9
Z
670 PL_hints |= HINT_BLOCK_SCOPE;
671 return estop;
672}
673
3ad73efd
Z
674STATIC OP *
675THX_ck_entersub_postinc(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
676{
3253bf85 677 OP *parent, *pushop, *argop;
3ad73efd 678 ck_entersub_args_proto(entersubop, namegv, ckobj);
3253bf85 679 parent = entersubop;
3ad73efd 680 pushop = cUNOPx(entersubop)->op_first;
e6dae479 681 if(!OpHAS_SIBLING(pushop)) {
3253bf85 682 parent = pushop;
1ed44841 683 pushop = cUNOPx(pushop)->op_first;
3253bf85 684 }
e6dae479 685 argop = OpSIBLING(pushop);
3253bf85 686 op_sibling_splice(parent, pushop, 1, NULL);
3ad73efd
Z
687 op_free(entersubop);
688 return newUNOP(OP_POSTINC, 0,
689 op_lvalue(op_contextualize(argop, G_SCALAR), OP_POSTINC));
690}
691
15103811
Z
692STATIC OP *
693THX_ck_entersub_pad_scalar(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
694{
695 OP *pushop, *argop;
696 PADOFFSET padoff = NOT_IN_PAD;
697 SV *a0, *a1;
698 ck_entersub_args_proto(entersubop, namegv, ckobj);
699 pushop = cUNOPx(entersubop)->op_first;
e6dae479 700 if(!OpHAS_SIBLING(pushop))
1ed44841 701 pushop = cUNOPx(pushop)->op_first;
e6dae479
FC
702 argop = OpSIBLING(pushop);
703 if(argop->op_type != OP_CONST || OpSIBLING(argop)->op_type != OP_CONST)
15103811
Z
704 croak("bad argument expression type for pad_scalar()");
705 a0 = cSVOPx_sv(argop);
e6dae479 706 a1 = cSVOPx_sv(OpSIBLING(argop));
15103811
Z
707 switch(SvIV(a0)) {
708 case 1: {
709 SV *namesv = sv_2mortal(newSVpvs("$"));
710 sv_catsv(namesv, a1);
711 padoff = pad_findmy_sv(namesv, 0);
712 } break;
713 case 2: {
714 char *namepv;
715 STRLEN namelen;
716 SV *namesv = sv_2mortal(newSVpvs("$"));
717 sv_catsv(namesv, a1);
718 namepv = SvPV(namesv, namelen);
ab8f91e9 719 padoff = pad_findmy_pvn(namepv, namelen, SvUTF8(namesv));
15103811
Z
720 } break;
721 case 3: {
722 char *namepv;
723 SV *namesv = sv_2mortal(newSVpvs("$"));
724 sv_catsv(namesv, a1);
725 namepv = SvPV_nolen(namesv);
ab8f91e9 726 padoff = pad_findmy_pv(namepv, SvUTF8(namesv));
15103811
Z
727 } break;
728 case 4: {
729 padoff = pad_findmy_pvs("$foo", 0);
730 } break;
731 default: croak("bad type value for pad_scalar()");
732 }
733 op_free(entersubop);
734 if(padoff == NOT_IN_PAD) {
735 return newSVOP(OP_CONST, 0, newSVpvs("NOT_IN_PAD"));
736 } else if(PAD_COMPNAME_FLAGS_isOUR(padoff)) {
737 return newSVOP(OP_CONST, 0, newSVpvs("NOT_MY"));
738 } else {
739 OP *padop = newOP(OP_PADSV, 0);
740 padop->op_targ = padoff;
741 return padop;
742 }
743}
744
83f8bb56
Z
745/** RPN keyword parser **/
746
747#define sv_is_glob(sv) (SvTYPE(sv) == SVt_PVGV)
748#define sv_is_regexp(sv) (SvTYPE(sv) == SVt_REGEXP)
749#define sv_is_string(sv) \
750 (!sv_is_glob(sv) && !sv_is_regexp(sv) && \
751 (SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK|SVp_IOK|SVp_NOK|SVp_POK)))
752
753static SV *hintkey_rpn_sv, *hintkey_calcrpn_sv, *hintkey_stufftest_sv;
07ffcb73 754static SV *hintkey_swaptwostmts_sv, *hintkey_looprest_sv;
a7aaec61 755static SV *hintkey_scopelessblock_sv;
e53d8f76
Z
756static SV *hintkey_stmtasexpr_sv, *hintkey_stmtsasexpr_sv;
757static SV *hintkey_loopblock_sv, *hintkey_blockasexpr_sv;
361d9b55 758static SV *hintkey_swaplabel_sv, *hintkey_labelconst_sv;
78cdf107
Z
759static SV *hintkey_arrayfullexpr_sv, *hintkey_arraylistexpr_sv;
760static SV *hintkey_arraytermexpr_sv, *hintkey_arrayarithexpr_sv;
761static SV *hintkey_arrayexprflags_sv;
ce409cc8 762static SV *hintkey_DEFSV_sv;
25f5d540 763static SV *hintkey_with_vars_sv;
03d05f6e 764static SV *hintkey_join_with_space_sv;
83f8bb56
Z
765static int (*next_keyword_plugin)(pTHX_ char *, STRLEN, OP **);
766
767/* low-level parser helpers */
768
769#define PL_bufptr (PL_parser->bufptr)
770#define PL_bufend (PL_parser->bufend)
771
772/* RPN parser */
773
774#define parse_var() THX_parse_var(aTHX)
775static OP *THX_parse_var(pTHX)
776{
777 char *s = PL_bufptr;
778 char *start = s;
779 PADOFFSET varpos;
780 OP *padop;
781 if(*s != '$') croak("RPN syntax error");
782 while(1) {
783 char c = *++s;
784 if(!isALNUM(c)) break;
785 }
786 if(s-start < 2) croak("RPN syntax error");
787 lex_read_to(s);
cc76b5cc 788 varpos = pad_findmy_pvn(start, s-start, 0);
83f8bb56
Z
789 if(varpos == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(varpos))
790 croak("RPN only supports \"my\" variables");
791 padop = newOP(OP_PADSV, 0);
792 padop->op_targ = varpos;
793 return padop;
794}
795
796#define push_rpn_item(o) \
3253bf85
DM
797 op_sibling_splice(parent, NULL, 0, o);
798#define pop_rpn_item() ( \
799 (tmpop = op_sibling_splice(parent, NULL, 1, NULL)) \
800 ? tmpop : (croak("RPN stack underflow"), (OP*)NULL))
83f8bb56
Z
801
802#define parse_rpn_expr() THX_parse_rpn_expr(aTHX)
803static OP *THX_parse_rpn_expr(pTHX)
804{
3253bf85
DM
805 OP *tmpop;
806 /* fake parent for splice to mess with */
807 OP *parent = mkBINOP(OP_NULL, NULL, NULL);
808
83f8bb56
Z
809 while(1) {
810 I32 c;
811 lex_read_space(0);
812 c = lex_peek_unichar(0);
813 switch(c) {
814 case /*(*/')': case /*{*/'}': {
815 OP *result = pop_rpn_item();
3253bf85
DM
816 if(cLISTOPx(parent)->op_first)
817 croak("RPN expression must return a single value");
818 op_free(parent);
83f8bb56
Z
819 return result;
820 } break;
821 case '0': case '1': case '2': case '3': case '4':
822 case '5': case '6': case '7': case '8': case '9': {
823 UV val = 0;
824 do {
825 lex_read_unichar(0);
826 val = 10*val + (c - '0');
827 c = lex_peek_unichar(0);
828 } while(c >= '0' && c <= '9');
829 push_rpn_item(newSVOP(OP_CONST, 0, newSVuv(val)));
830 } break;
831 case '$': {
832 push_rpn_item(parse_var());
833 } break;
834 case '+': {
835 OP *b = pop_rpn_item();
836 OP *a = pop_rpn_item();
837 lex_read_unichar(0);
838 push_rpn_item(newBINOP(OP_I_ADD, 0, a, b));
839 } break;
840 case '-': {
841 OP *b = pop_rpn_item();
842 OP *a = pop_rpn_item();
843 lex_read_unichar(0);
844 push_rpn_item(newBINOP(OP_I_SUBTRACT, 0, a, b));
845 } break;
846 case '*': {
847 OP *b = pop_rpn_item();
848 OP *a = pop_rpn_item();
849 lex_read_unichar(0);
850 push_rpn_item(newBINOP(OP_I_MULTIPLY, 0, a, b));
851 } break;
852 case '/': {
853 OP *b = pop_rpn_item();
854 OP *a = pop_rpn_item();
855 lex_read_unichar(0);
856 push_rpn_item(newBINOP(OP_I_DIVIDE, 0, a, b));
857 } break;
858 case '%': {
859 OP *b = pop_rpn_item();
860 OP *a = pop_rpn_item();
861 lex_read_unichar(0);
862 push_rpn_item(newBINOP(OP_I_MODULO, 0, a, b));
863 } break;
864 default: {
865 croak("RPN syntax error");
866 } break;
867 }
868 }
869}
870
871#define parse_keyword_rpn() THX_parse_keyword_rpn(aTHX)
872static OP *THX_parse_keyword_rpn(pTHX)
873{
874 OP *op;
875 lex_read_space(0);
876 if(lex_peek_unichar(0) != '('/*)*/)
877 croak("RPN expression must be parenthesised");
878 lex_read_unichar(0);
879 op = parse_rpn_expr();
880 if(lex_peek_unichar(0) != /*(*/')')
881 croak("RPN expression must be parenthesised");
882 lex_read_unichar(0);
883 return op;
884}
885
886#define parse_keyword_calcrpn() THX_parse_keyword_calcrpn(aTHX)
887static OP *THX_parse_keyword_calcrpn(pTHX)
888{
889 OP *varop, *exprop;
890 lex_read_space(0);
891 varop = parse_var();
892 lex_read_space(0);
893 if(lex_peek_unichar(0) != '{'/*}*/)
894 croak("RPN expression must be braced");
895 lex_read_unichar(0);
896 exprop = parse_rpn_expr();
897 if(lex_peek_unichar(0) != /*{*/'}')
898 croak("RPN expression must be braced");
899 lex_read_unichar(0);
900 return newASSIGNOP(OPf_STACKED, varop, 0, exprop);
901}
902
903#define parse_keyword_stufftest() THX_parse_keyword_stufftest(aTHX)
904static OP *THX_parse_keyword_stufftest(pTHX)
905{
906 I32 c;
907 bool do_stuff;
908 lex_read_space(0);
909 do_stuff = lex_peek_unichar(0) == '+';
910 if(do_stuff) {
911 lex_read_unichar(0);
912 lex_read_space(0);
913 }
914 c = lex_peek_unichar(0);
915 if(c == ';') {
916 lex_read_unichar(0);
917 } else if(c != /*{*/'}') {
918 croak("syntax error");
919 }
920 if(do_stuff) lex_stuff_pvs(" ", 0);
921 return newOP(OP_NULL, 0);
922}
923
924#define parse_keyword_swaptwostmts() THX_parse_keyword_swaptwostmts(aTHX)
925static OP *THX_parse_keyword_swaptwostmts(pTHX)
926{
927 OP *a, *b;
928 a = parse_fullstmt(0);
929 b = parse_fullstmt(0);
930 if(a && b)
931 PL_hints |= HINT_BLOCK_SCOPE;
2fcb4757 932 return op_append_list(OP_LINESEQ, b, a);
83f8bb56
Z
933}
934
07ffcb73
Z
935#define parse_keyword_looprest() THX_parse_keyword_looprest(aTHX)
936static OP *THX_parse_keyword_looprest(pTHX)
937{
94bf0465
Z
938 return newWHILEOP(0, 1, NULL, newSVOP(OP_CONST, 0, &PL_sv_yes),
939 parse_stmtseq(0), NULL, 1);
07ffcb73
Z
940}
941
a7aaec61
Z
942#define parse_keyword_scopelessblock() THX_parse_keyword_scopelessblock(aTHX)
943static OP *THX_parse_keyword_scopelessblock(pTHX)
944{
945 I32 c;
946 OP *body;
947 lex_read_space(0);
948 if(lex_peek_unichar(0) != '{'/*}*/) croak("syntax error");
949 lex_read_unichar(0);
950 body = parse_stmtseq(0);
951 c = lex_peek_unichar(0);
952 if(c != /*{*/'}' && c != /*[*/']' && c != /*(*/')') croak("syntax error");
953 lex_read_unichar(0);
954 return body;
955}
956
9eb5c532
Z
957#define parse_keyword_stmtasexpr() THX_parse_keyword_stmtasexpr(aTHX)
958static OP *THX_parse_keyword_stmtasexpr(pTHX)
959{
8359b381 960 OP *o = parse_barestmt(0);
3ad73efd
Z
961 if (!o) o = newOP(OP_STUB, 0);
962 if (PL_hints & HINT_BLOCK_SCOPE) o->op_flags |= OPf_PARENS;
963 return op_scope(o);
9eb5c532
Z
964}
965
966#define parse_keyword_stmtsasexpr() THX_parse_keyword_stmtsasexpr(aTHX)
967static OP *THX_parse_keyword_stmtsasexpr(pTHX)
968{
969 OP *o;
970 lex_read_space(0);
971 if(lex_peek_unichar(0) != '{'/*}*/) croak("syntax error");
972 lex_read_unichar(0);
973 o = parse_stmtseq(0);
974 lex_read_space(0);
975 if(lex_peek_unichar(0) != /*{*/'}') croak("syntax error");
976 lex_read_unichar(0);
3ad73efd
Z
977 if (!o) o = newOP(OP_STUB, 0);
978 if (PL_hints & HINT_BLOCK_SCOPE) o->op_flags |= OPf_PARENS;
979 return op_scope(o);
9eb5c532
Z
980}
981
e53d8f76
Z
982#define parse_keyword_loopblock() THX_parse_keyword_loopblock(aTHX)
983static OP *THX_parse_keyword_loopblock(pTHX)
984{
94bf0465
Z
985 return newWHILEOP(0, 1, NULL, newSVOP(OP_CONST, 0, &PL_sv_yes),
986 parse_block(0), NULL, 1);
e53d8f76
Z
987}
988
989#define parse_keyword_blockasexpr() THX_parse_keyword_blockasexpr(aTHX)
990static OP *THX_parse_keyword_blockasexpr(pTHX)
991{
992 OP *o = parse_block(0);
3ad73efd
Z
993 if (!o) o = newOP(OP_STUB, 0);
994 if (PL_hints & HINT_BLOCK_SCOPE) o->op_flags |= OPf_PARENS;
995 return op_scope(o);
e53d8f76
Z
996}
997
361d9b55
Z
998#define parse_keyword_swaplabel() THX_parse_keyword_swaplabel(aTHX)
999static OP *THX_parse_keyword_swaplabel(pTHX)
1000{
1001 OP *sop = parse_barestmt(0);
1002 SV *label = parse_label(PARSE_OPTIONAL);
1003 if (label) sv_2mortal(label);
5db1eb8d
BF
1004 return newSTATEOP(label ? SvUTF8(label) : 0,
1005 label ? savepv(SvPVX(label)) : NULL,
1006 sop);
361d9b55
Z
1007}
1008
1009#define parse_keyword_labelconst() THX_parse_keyword_labelconst(aTHX)
1010static OP *THX_parse_keyword_labelconst(pTHX)
1011{
1012 return newSVOP(OP_CONST, 0, parse_label(0));
1013}
1014
78cdf107
Z
1015#define parse_keyword_arrayfullexpr() THX_parse_keyword_arrayfullexpr(aTHX)
1016static OP *THX_parse_keyword_arrayfullexpr(pTHX)
1017{
1018 return newANONLIST(parse_fullexpr(0));
1019}
1020
1021#define parse_keyword_arraylistexpr() THX_parse_keyword_arraylistexpr(aTHX)
1022static OP *THX_parse_keyword_arraylistexpr(pTHX)
1023{
1024 return newANONLIST(parse_listexpr(0));
1025}
1026
1027#define parse_keyword_arraytermexpr() THX_parse_keyword_arraytermexpr(aTHX)
1028static OP *THX_parse_keyword_arraytermexpr(pTHX)
1029{
1030 return newANONLIST(parse_termexpr(0));
1031}
1032
1033#define parse_keyword_arrayarithexpr() THX_parse_keyword_arrayarithexpr(aTHX)
1034static OP *THX_parse_keyword_arrayarithexpr(pTHX)
1035{
1036 return newANONLIST(parse_arithexpr(0));
1037}
1038
1039#define parse_keyword_arrayexprflags() THX_parse_keyword_arrayexprflags(aTHX)
1040static OP *THX_parse_keyword_arrayexprflags(pTHX)
1041{
1042 U32 flags = 0;
1043 I32 c;
1044 OP *o;
1045 lex_read_space(0);
1046 c = lex_peek_unichar(0);
1047 if (c != '!' && c != '?') croak("syntax error");
1048 lex_read_unichar(0);
1049 if (c == '?') flags |= PARSE_OPTIONAL;
1050 o = parse_listexpr(flags);
1051 return o ? newANONLIST(o) : newANONHASH(newOP(OP_STUB, 0));
1052}
1053
ce409cc8
LM
1054#define parse_keyword_DEFSV() THX_parse_keyword_DEFSV(aTHX)
1055static OP *THX_parse_keyword_DEFSV(pTHX)
1056{
1057 return newDEFSVOP();
1058}
1059
1d07f51c
LM
1060#define sv_cat_c(a,b) THX_sv_cat_c(aTHX_ a, b)
1061static void THX_sv_cat_c(pTHX_ SV *sv, U32 c) {
25f5d540
LM
1062 char ds[UTF8_MAXBYTES + 1], *d;
1063 d = (char *)uvchr_to_utf8((U8 *)ds, c);
1064 if (d - ds > 1) {
1065 sv_utf8_upgrade(sv);
1066 }
1067 sv_catpvn(sv, ds, d - ds);
1068}
1069
1070#define parse_keyword_with_vars() THX_parse_keyword_with_vars(aTHX)
1071static OP *THX_parse_keyword_with_vars(pTHX)
1072{
1073 I32 c;
1074 IV count;
1075 int save_ix;
1076 OP *vardeclseq, *body;
1077
1078 save_ix = block_start(TRUE);
1079 vardeclseq = NULL;
1080
1081 count = 0;
1082
1083 lex_read_space(0);
1084 c = lex_peek_unichar(0);
1085 while (c != '{') {
1086 SV *varname;
1087 PADOFFSET padoff;
1088
1089 if (c == -1) {
1090 croak("unexpected EOF; expecting '{'");
1091 }
1092
1093 if (!isIDFIRST_uni(c)) {
1094 croak("unexpected '%c'; expecting an identifier", (int)c);
1095 }
1096
1097 varname = newSVpvs("$");
1098 if (lex_bufutf8()) {
1099 SvUTF8_on(varname);
1100 }
1101
1102 sv_cat_c(varname, c);
1103 lex_read_unichar(0);
1104
1105 while (c = lex_peek_unichar(0), c != -1 && isIDCONT_uni(c)) {
1106 sv_cat_c(varname, c);
1107 lex_read_unichar(0);
1108 }
1109
1110 padoff = pad_add_name_sv(varname, padadd_NO_DUP_CHECK, NULL, NULL);
1111
1112 {
1113 OP *my_var = newOP(OP_PADSV, OPf_MOD | (OPpLVAL_INTRO << 8));
1114 my_var->op_targ = padoff;
1115
1116 vardeclseq = op_append_list(
1117 OP_LINESEQ,
1118 vardeclseq,
1119 newSTATEOP(
1120 0, NULL,
1121 newASSIGNOP(
1122 OPf_STACKED,
1123 my_var, 0,
1124 newSVOP(
1125 OP_CONST, 0,
1126 newSViv(++count)
1127 )
1128 )
1129 )
1130 );
1131 }
1132
1133 lex_read_space(0);
1134 c = lex_peek_unichar(0);
1135 }
1136
1137 intro_my();
1138
1139 body = parse_block(0);
1140
1141 return block_end(save_ix, op_append_list(OP_LINESEQ, vardeclseq, body));
1142}
1143
03d05f6e
LM
1144#define parse_join_with_space() THX_parse_join_with_space(aTHX)
1145static OP *THX_parse_join_with_space(pTHX)
1146{
1147 OP *delim, *args;
1148
1149 args = parse_listexpr(0);
1150 delim = newSVOP(OP_CONST, 0, newSVpvs(" "));
1151 return op_convert_list(OP_JOIN, 0, op_prepend_elem(OP_LIST, delim, args));
1152}
1153
83f8bb56
Z
1154/* plugin glue */
1155
1156#define keyword_active(hintkey_sv) THX_keyword_active(aTHX_ hintkey_sv)
1157static int THX_keyword_active(pTHX_ SV *hintkey_sv)
1158{
1159 HE *he;
1160 if(!GvHV(PL_hintgv)) return 0;
1161 he = hv_fetch_ent(GvHV(PL_hintgv), hintkey_sv, 0,
1162 SvSHARED_HASH(hintkey_sv));
1163 return he && SvTRUE(HeVAL(he));
1164}
1165
1166static int my_keyword_plugin(pTHX_
1167 char *keyword_ptr, STRLEN keyword_len, OP **op_ptr)
1168{
b59bf0b2 1169 if (memEQs(keyword_ptr, keyword_len, "rpn") &&
83f8bb56
Z
1170 keyword_active(hintkey_rpn_sv)) {
1171 *op_ptr = parse_keyword_rpn();
1172 return KEYWORD_PLUGIN_EXPR;
b59bf0b2 1173 } else if (memEQs(keyword_ptr, keyword_len, "calcrpn") &&
83f8bb56
Z
1174 keyword_active(hintkey_calcrpn_sv)) {
1175 *op_ptr = parse_keyword_calcrpn();
1176 return KEYWORD_PLUGIN_STMT;
b59bf0b2 1177 } else if (memEQs(keyword_ptr, keyword_len, "stufftest") &&
83f8bb56
Z
1178 keyword_active(hintkey_stufftest_sv)) {
1179 *op_ptr = parse_keyword_stufftest();
1180 return KEYWORD_PLUGIN_STMT;
b59bf0b2 1181 } else if (memEQs(keyword_ptr, keyword_len, "swaptwostmts") &&
83f8bb56
Z
1182 keyword_active(hintkey_swaptwostmts_sv)) {
1183 *op_ptr = parse_keyword_swaptwostmts();
1184 return KEYWORD_PLUGIN_STMT;
b59bf0b2 1185 } else if (memEQs(keyword_ptr, keyword_len, "looprest") &&
07ffcb73
Z
1186 keyword_active(hintkey_looprest_sv)) {
1187 *op_ptr = parse_keyword_looprest();
1188 return KEYWORD_PLUGIN_STMT;
b59bf0b2 1189 } else if (memEQs(keyword_ptr, keyword_len, "scopelessblock") &&
a7aaec61
Z
1190 keyword_active(hintkey_scopelessblock_sv)) {
1191 *op_ptr = parse_keyword_scopelessblock();
1192 return KEYWORD_PLUGIN_STMT;
b59bf0b2 1193 } else if (memEQs(keyword_ptr, keyword_len, "stmtasexpr") &&
9eb5c532
Z
1194 keyword_active(hintkey_stmtasexpr_sv)) {
1195 *op_ptr = parse_keyword_stmtasexpr();
1196 return KEYWORD_PLUGIN_EXPR;
b59bf0b2 1197 } else if (memEQs(keyword_ptr, keyword_len, "stmtsasexpr") &&
9eb5c532
Z
1198 keyword_active(hintkey_stmtsasexpr_sv)) {
1199 *op_ptr = parse_keyword_stmtsasexpr();
1200 return KEYWORD_PLUGIN_EXPR;
b59bf0b2 1201 } else if (memEQs(keyword_ptr, keyword_len, "loopblock") &&
e53d8f76
Z
1202 keyword_active(hintkey_loopblock_sv)) {
1203 *op_ptr = parse_keyword_loopblock();
1204 return KEYWORD_PLUGIN_STMT;
b59bf0b2 1205 } else if (memEQs(keyword_ptr, keyword_len, "blockasexpr") &&
e53d8f76
Z
1206 keyword_active(hintkey_blockasexpr_sv)) {
1207 *op_ptr = parse_keyword_blockasexpr();
1208 return KEYWORD_PLUGIN_EXPR;
b59bf0b2 1209 } else if (memEQs(keyword_ptr, keyword_len, "swaplabel") &&
361d9b55
Z
1210 keyword_active(hintkey_swaplabel_sv)) {
1211 *op_ptr = parse_keyword_swaplabel();
1212 return KEYWORD_PLUGIN_STMT;
b59bf0b2 1213 } else if (memEQs(keyword_ptr, keyword_len, "labelconst") &&
361d9b55
Z
1214 keyword_active(hintkey_labelconst_sv)) {
1215 *op_ptr = parse_keyword_labelconst();
1216 return KEYWORD_PLUGIN_EXPR;
b59bf0b2 1217 } else if (memEQs(keyword_ptr, keyword_len, "arrayfullexpr") &&
78cdf107
Z
1218 keyword_active(hintkey_arrayfullexpr_sv)) {
1219 *op_ptr = parse_keyword_arrayfullexpr();
1220 return KEYWORD_PLUGIN_EXPR;
b59bf0b2 1221 } else if (memEQs(keyword_ptr, keyword_len, "arraylistexpr") &&
78cdf107
Z
1222 keyword_active(hintkey_arraylistexpr_sv)) {
1223 *op_ptr = parse_keyword_arraylistexpr();
1224 return KEYWORD_PLUGIN_EXPR;
b59bf0b2 1225 } else if (memEQs(keyword_ptr, keyword_len, "arraytermexpr") &&
78cdf107
Z
1226 keyword_active(hintkey_arraytermexpr_sv)) {
1227 *op_ptr = parse_keyword_arraytermexpr();
1228 return KEYWORD_PLUGIN_EXPR;
b59bf0b2 1229 } else if (memEQs(keyword_ptr, keyword_len, "arrayarithexpr") &&
78cdf107
Z
1230 keyword_active(hintkey_arrayarithexpr_sv)) {
1231 *op_ptr = parse_keyword_arrayarithexpr();
1232 return KEYWORD_PLUGIN_EXPR;
b59bf0b2 1233 } else if (memEQs(keyword_ptr, keyword_len, "arrayexprflags") &&
78cdf107
Z
1234 keyword_active(hintkey_arrayexprflags_sv)) {
1235 *op_ptr = parse_keyword_arrayexprflags();
1236 return KEYWORD_PLUGIN_EXPR;
b59bf0b2 1237 } else if (memEQs(keyword_ptr, keyword_len, "DEFSV") &&
ce409cc8
LM
1238 keyword_active(hintkey_DEFSV_sv)) {
1239 *op_ptr = parse_keyword_DEFSV();
1240 return KEYWORD_PLUGIN_EXPR;
b59bf0b2 1241 } else if (memEQs(keyword_ptr, keyword_len, "with_vars") &&
25f5d540
LM
1242 keyword_active(hintkey_with_vars_sv)) {
1243 *op_ptr = parse_keyword_with_vars();
1244 return KEYWORD_PLUGIN_STMT;
b59bf0b2 1245 } else if (memEQs(keyword_ptr, keyword_len, "join_with_space") &&
03d05f6e
LM
1246 keyword_active(hintkey_join_with_space_sv)) {
1247 *op_ptr = parse_join_with_space();
1248 return KEYWORD_PLUGIN_EXPR;
83f8bb56 1249 } else {
fbc25bdd 1250 assert(next_keyword_plugin != my_keyword_plugin);
83f8bb56
Z
1251 return next_keyword_plugin(aTHX_ keyword_ptr, keyword_len, op_ptr);
1252 }
1253}
1254
f568d64d
BM
1255static XOP my_xop;
1256
1257static OP *
1258pp_xop(pTHX)
1259{
1260 return PL_op->op_next;
1261}
1262
1263static void
1264peep_xop(pTHX_ OP *o, OP *oldop)
1265{
1266 dMY_CXT;
147e3846
KW
1267 av_push(MY_CXT.xop_record, newSVpvf("peep:%" UVxf, PTR2UV(o)));
1268 av_push(MY_CXT.xop_record, newSVpvf("oldop:%" UVxf, PTR2UV(oldop)));
f568d64d
BM
1269}
1270
27fcb6ee
FC
1271static I32
1272filter_call(pTHX_ int idx, SV *buf_sv, int maxlen)
1273{
27fcb6ee
FC
1274 char *p;
1275 char *end;
1276 int n = FILTER_READ(idx + 1, buf_sv, maxlen);
1277
1278 if (n<=0) return n;
1279
1280 p = SvPV_force_nolen(buf_sv);
1281 end = p + SvCUR(buf_sv);
1282 while (p < end) {
1283 if (*p == 'o') *p = 'e';
1284 p++;
1285 }
1286 return SvCUR(buf_sv);
1287}
1288
02cab674
FC
1289static AV *
1290myget_linear_isa(pTHX_ HV *stash, U32 level) {
02cab674 1291 GV **gvp = (GV **)hv_fetchs(stash, "ISA", 0);
6dbddf4a 1292 PERL_UNUSED_ARG(level);
02cab674
FC
1293 return gvp && *gvp && GvAV(*gvp)
1294 ? GvAV(*gvp)
1295 : (AV *)sv_2mortal((SV *)newAV());
1296}
1297
27fcb6ee 1298
9777c6aa
SM
1299XS_EXTERNAL(XS_XS__APItest__XSUB_XS_VERSION_undef);
1300XS_EXTERNAL(XS_XS__APItest__XSUB_XS_VERSION_empty);
1301XS_EXTERNAL(XS_XS__APItest__XSUB_XS_APIVERSION_invalid);
7b20c7cd 1302
02cab674
FC
1303static struct mro_alg mymro;
1304
e8570548
Z
1305static Perl_check_t addissub_nxck_add;
1306
1307static OP *
1308addissub_myck_add(pTHX_ OP *op)
1309{
1310 SV **flag_svp = hv_fetchs(GvHV(PL_hintgv), "XS::APItest/addissub", 0);
1311 OP *aop, *bop;
1312 U8 flags;
1313 if (!(flag_svp && SvTRUE(*flag_svp) && (op->op_flags & OPf_KIDS) &&
e6dae479
FC
1314 (aop = cBINOPx(op)->op_first) && (bop = OpSIBLING(aop)) &&
1315 !OpHAS_SIBLING(bop)))
e8570548 1316 return addissub_nxck_add(aTHX_ op);
e8570548 1317 flags = op->op_flags;
3253bf85
DM
1318 op_sibling_splice(op, NULL, 1, NULL); /* excise aop */
1319 op_sibling_splice(op, NULL, 1, NULL); /* excise bop */
1320 op_free(op); /* free the empty husk */
1321 flags &= ~OPf_KIDS;
e8570548
Z
1322 return newBINOP(OP_SUBTRACT, flags, aop, bop);
1323}
02cab674 1324
39c012bc
FC
1325static Perl_check_t old_ck_rv2cv;
1326
1327static OP *
1328my_ck_rv2cv(pTHX_ OP *o)
1329{
1330 SV *ref;
1331 SV **flag_svp = hv_fetchs(GvHV(PL_hintgv), "XS::APItest/addunder", 0);
1332 OP *aop;
1333
1334 if (flag_svp && SvTRUE(*flag_svp) && (o->op_flags & OPf_KIDS)
1335 && (aop = cUNOPx(o)->op_first) && aop->op_type == OP_CONST
1336 && aop->op_private & (OPpCONST_ENTERED|OPpCONST_BARE)
1337 && (ref = cSVOPx(aop)->op_sv) && SvPOK(ref) && SvCUR(ref)
1338 && *(SvEND(ref)-1) == 'o')
1339 {
1340 SvGROW(ref, SvCUR(ref)+2);
1341 *SvEND(ref) = '_';
1342 SvCUR(ref)++;
1343 *SvEND(ref) = '\0';
1344 }
1345 return old_ck_rv2cv(aTHX_ o);
1346}
1347
55289a74
NC
1348#include "const-c.inc"
1349
ffe53d21 1350MODULE = XS::APItest PACKAGE = XS::APItest
0314122a 1351
55289a74
NC
1352INCLUDE: const-xs.inc
1353
ffe53d21
NC
1354INCLUDE: numeric.xs
1355
11f9ab1a
TC
1356void
1357assertx(int x)
1358 CODE:
1359 /* this only needs to compile and checks that assert() can be
1360 used this way syntactically */
950c540d
JH
1361 (void)(assert(x), 1);
1362 (void)(x);
11f9ab1a 1363
fed3ba5d
NC
1364MODULE = XS::APItest::utf8 PACKAGE = XS::APItest::utf8
1365
1366int
1367bytes_cmp_utf8(bytes, utf8)
1368 SV *bytes
1369 SV *utf8
1370 PREINIT:
1371 const U8 *b;
1372 STRLEN blen;
1373 const U8 *u;
1374 STRLEN ulen;
1375 CODE:
1376 b = (const U8 *)SvPVbyte(bytes, blen);
1377 u = (const U8 *)SvPVbyte(utf8, ulen);
1378 RETVAL = bytes_cmp_utf8(b, blen, u, ulen);
1379 OUTPUT:
1380 RETVAL
1381
eb83ed87 1382AV *
ae315a0a
KW
1383test_utf8_to_bytes(bytes, lenp)
1384 unsigned char * bytes
0a9f8f95 1385 ssize_t lenp
ae315a0a
KW
1386 PREINIT:
1387 char * ret;
1388 CODE:
1389 RETVAL = newAV();
1390 sv_2mortal((SV*)RETVAL);
1391
1392 ret = (char *) utf8_to_bytes(bytes, &lenp);
1393 av_push(RETVAL, newSVpv(ret, 0));
1394 av_push(RETVAL, newSViv(lenp));
1395 av_push(RETVAL, newSVpv((const char *) bytes, 0));
1396
1397 OUTPUT:
1398 RETVAL
1399
1400AV *
37657a5b
KW
1401test_utf8n_to_uvchr_msgs(s, len, flags)
1402 char *s
1403 STRLEN len
1404 U32 flags
1405 PREINIT:
1406 STRLEN retlen;
1407 UV ret;
1408 U32 errors;
1409 AV *msgs = NULL;
1410
1411 CODE:
1412 RETVAL = newAV();
1413 sv_2mortal((SV*)RETVAL);
1414
1415 ret = utf8n_to_uvchr_msgs((U8*) s,
1416 len,
1417 &retlen,
1418 flags,
1419 &errors,
1420 &msgs);
1421
1422 /* Returns the return value in [0]; <retlen> in [1], <errors> in [2] */
1423 av_push(RETVAL, newSVuv(ret));
1424 if (retlen == (STRLEN) -1) {
1425 av_push(RETVAL, newSViv(-1));
1426 }
1427 else {
1428 av_push(RETVAL, newSVuv(retlen));
1429 }
1430 av_push(RETVAL, newSVuv(errors));
1431
1432 /* And any messages in [3] */
1433 if (msgs) {
1434 av_push(RETVAL, newRV_noinc((SV*)msgs));
1435 }
1436
1437 OUTPUT:
1438 RETVAL
1439
1440AV *
f9380377 1441test_utf8n_to_uvchr_error(s, len, flags)
eb83ed87 1442
9c13cd3c
KW
1443 char *s
1444 STRLEN len
1445 U32 flags
eb83ed87
KW
1446 PREINIT:
1447 STRLEN retlen;
1448 UV ret;
f9380377 1449 U32 errors;
eb83ed87
KW
1450
1451 CODE:
f9380377
KW
1452 /* Now that utf8n_to_uvchr() is a trivial wrapper for
1453 * utf8n_to_uvchr_error(), call the latter with the inputs. It always
1454 * asks for the actual length to be returned and errors to be returned
eb83ed87
KW
1455 *
1456 * Length to assume <s> is; not checked, so could have buffer overflow
1457 */
1458 RETVAL = newAV();
1459 sv_2mortal((SV*)RETVAL);
1460
9c13cd3c
KW
1461 ret = utf8n_to_uvchr_error((U8*) s,
1462 len,
f9380377 1463 &retlen,
9c13cd3c 1464 flags,
f9380377 1465 &errors);
eb83ed87 1466
f9380377 1467 /* Returns the return value in [0]; <retlen> in [1], <errors> in [2] */
eb83ed87
KW
1468 av_push(RETVAL, newSVuv(ret));
1469 if (retlen == (STRLEN) -1) {
1470 av_push(RETVAL, newSViv(-1));
1471 }
1472 else {
1473 av_push(RETVAL, newSVuv(retlen));
1474 }
f9380377 1475 av_push(RETVAL, newSVuv(errors));
eb83ed87
KW
1476
1477 OUTPUT:
1478 RETVAL
1479
75ffa578
KW
1480AV *
1481test_valid_utf8_to_uvchr(s)
1482
1483 SV *s
1484 PREINIT:
1485 STRLEN retlen;
1486 UV ret;
75ffa578
KW
1487
1488 CODE:
1489 /* Call utf8n_to_uvchr() with the inputs. It always asks for the
1490 * actual length to be returned
1491 *
1492 * Length to assume <s> is; not checked, so could have buffer overflow
1493 */
1494 RETVAL = newAV();
1495 sv_2mortal((SV*)RETVAL);
1496
70250a0b 1497 ret = valid_utf8_to_uvchr((U8*) SvPV_nolen(s), &retlen);
75ffa578
KW
1498
1499 /* Returns the return value in [0]; <retlen> in [1] */
1500 av_push(RETVAL, newSVuv(ret));
1501 av_push(RETVAL, newSVuv(retlen));
1502
1503 OUTPUT:
1504 RETVAL
1505
046d01eb
KW
1506SV *
1507test_uvchr_to_utf8_flags(uv, flags)
1508
1509 SV *uv
1510 SV *flags
1511 PREINIT:
81b3f640 1512 U8 dest[UTF8_MAXBYTES + 1];
046d01eb
KW
1513 U8 *ret;
1514
1515 CODE:
1516 /* Call uvchr_to_utf8_flags() with the inputs. */
1517 ret = uvchr_to_utf8_flags(dest, SvUV(uv), SvUV(flags));
1518 if (! ret) {
1519 XSRETURN_UNDEF;
1520 }
1521 RETVAL = newSVpvn((char *) dest, ret - dest);
1522
1523 OUTPUT:
1524 RETVAL
1525
33f38593
KW
1526AV *
1527test_uvchr_to_utf8_flags_msgs(uv, flags)
1528
1529 SV *uv
1530 SV *flags
1531 PREINIT:
1532 U8 dest[UTF8_MAXBYTES + 1];
1533 U8 *ret;
1534
1535 CODE:
1536 HV *msgs = NULL;
1537 RETVAL = newAV();
1538 sv_2mortal((SV*)RETVAL);
1539
1540 ret = uvchr_to_utf8_flags_msgs(dest, SvUV(uv), SvUV(flags), &msgs);
1541
1542 if (ret) {
1543 av_push(RETVAL, newSVpvn((char *) dest, ret - dest));
1544 }
1545 else {
1546 av_push(RETVAL, &PL_sv_undef);
1547 }
1548
1549 if (msgs) {
1550 av_push(RETVAL, newRV_noinc((SV*)msgs));
1551 }
1552
1553 OUTPUT:
1554 RETVAL
1555
7d255dc8
NC
1556MODULE = XS::APItest:Overload PACKAGE = XS::APItest::Overload
1557
c33e8be1 1558void
25a9ffce 1559amagic_deref_call(sv, what)
7d255dc8
NC
1560 SV *sv
1561 int what
1562 PPCODE:
7d255dc8 1563 /* The reference is owned by something else. */
25a9ffce 1564 PUSHs(amagic_deref_call(sv, what));
7d255dc8 1565
e89bfaa6
NC
1566# I'd certainly like to discourage the use of this macro, given that we now
1567# have amagic_deref_call
1568
c33e8be1 1569void
e89bfaa6
NC
1570tryAMAGICunDEREF_var(sv, what)
1571 SV *sv
1572 int what
1573 PPCODE:
1574 {
1575 SV **sp = &sv;
1576 switch(what) {
1577 case to_av_amg:
1578 tryAMAGICunDEREF(to_av);
1579 break;
1580 case to_cv_amg:
1581 tryAMAGICunDEREF(to_cv);
1582 break;
1583 case to_gv_amg:
1584 tryAMAGICunDEREF(to_gv);
1585 break;
1586 case to_hv_amg:
1587 tryAMAGICunDEREF(to_hv);
1588 break;
1589 case to_sv_amg:
1590 tryAMAGICunDEREF(to_sv);
1591 break;
1592 default:
1593 croak("Invalid value %d passed to tryAMAGICunDEREF_var", what);
1594 }
1595 }
1596 /* The reference is owned by something else. */
1597 PUSHs(sv);
1598
7b20c7cd
NC
1599MODULE = XS::APItest PACKAGE = XS::APItest::XSUB
1600
1601BOOT:
1602 newXS("XS::APItest::XSUB::XS_VERSION_undef", XS_XS__APItest__XSUB_XS_VERSION_undef, __FILE__);
f9cc56fa 1603 newXS("XS::APItest::XSUB::XS_VERSION_empty", XS_XS__APItest__XSUB_XS_VERSION_empty, __FILE__);
88c4b02d 1604 newXS("XS::APItest::XSUB::XS_APIVERSION_invalid", XS_XS__APItest__XSUB_XS_APIVERSION_invalid, __FILE__);
7b20c7cd
NC
1605
1606void
1607XS_VERSION_defined(...)
1608 PPCODE:
1609 XS_VERSION_BOOTCHECK;
1610 XSRETURN_EMPTY;
1611
88c4b02d
NC
1612void
1613XS_APIVERSION_valid(...)
1614 PPCODE:
1615 XS_APIVERSION_BOOTCHECK;
1616 XSRETURN_EMPTY;
1617
50ceb817
DB
1618void
1619xsreturn( int len )
1620 PPCODE:
1621 int i = 0;
1622 EXTEND( SP, len );
1623 for ( ; i < len; i++ ) {
1624 ST(i) = sv_2mortal( newSViv(i) );
1625 }
1626 XSRETURN( len );
1627
1628void
1629xsreturn_iv()
1630 PPCODE:
052efbb4 1631 XSRETURN_IV(I32_MIN + 1);
50ceb817
DB
1632
1633void
1634xsreturn_uv()
1635 PPCODE:
7e327f76 1636 XSRETURN_UV( (U32)((1U<<31) + 1) );
50ceb817
DB
1637
1638void
1639xsreturn_nv()
1640 PPCODE:
1641 XSRETURN_NV(0.25);
1642
1643void
1644xsreturn_pv()
1645 PPCODE:
1646 XSRETURN_PV("returned");
1647
1648void
1649xsreturn_pvn()
1650 PPCODE:
1651 XSRETURN_PVN("returned too much",8);
1652
1653void
1654xsreturn_no()
1655 PPCODE:
1656 XSRETURN_NO;
1657
1658void
1659xsreturn_yes()
1660 PPCODE:
1661 XSRETURN_YES;
1662
1663void
1664xsreturn_undef()
1665 PPCODE:
1666 XSRETURN_UNDEF;
1667
1668void
1669xsreturn_empty()
1670 PPCODE:
1671 XSRETURN_EMPTY;
1672
ffe53d21
NC
1673MODULE = XS::APItest:Hash PACKAGE = XS::APItest::Hash
1674
b54b4831
NC
1675void
1676rot13_hash(hash)
1677 HV *hash
1678 CODE:
1679 {
1680 struct ufuncs uf;
1681 uf.uf_val = rot13_key;
1682 uf.uf_set = 0;
1683 uf.uf_index = 0;
1684
1685 sv_magic((SV*)hash, NULL, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
1686 }
1687
53c40a8f
NC
1688void
1689bitflip_hash(hash)
1690 HV *hash
1691 CODE:
1692 {
1693 struct ufuncs uf;
1694 uf.uf_val = bitflip_key;
1695 uf.uf_set = 0;
1696 uf.uf_index = 0;
1697
1698 sv_magic((SV*)hash, NULL, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
1699 }
1700
028f8eaa
MHM
1701#define UTF8KLEN(sv, len) (SvUTF8(sv) ? -(I32)len : (I32)len)
1702
0314122a
NC
1703bool
1704exists(hash, key_sv)
1705 PREINIT:
1706 STRLEN len;
1707 const char *key;
1708 INPUT:
1709 HV *hash
1710 SV *key_sv
1711 CODE:
1712 key = SvPV(key_sv, len);
028f8eaa 1713 RETVAL = hv_exists(hash, key, UTF8KLEN(key_sv, len));
0314122a
NC
1714 OUTPUT:
1715 RETVAL
1716
bdee33e4
NC
1717bool
1718exists_ent(hash, key_sv)
1719 PREINIT:
1720 INPUT:
1721 HV *hash
1722 SV *key_sv
1723 CODE:
1724 RETVAL = hv_exists_ent(hash, key_sv, 0);
1725 OUTPUT:
1726 RETVAL
1727
b60cf05a 1728SV *
55289a74 1729delete(hash, key_sv, flags = 0)
b60cf05a
NC
1730 PREINIT:
1731 STRLEN len;
1732 const char *key;
1733 INPUT:
1734 HV *hash
1735 SV *key_sv
55289a74 1736 I32 flags;
b60cf05a
NC
1737 CODE:
1738 key = SvPV(key_sv, len);
1739 /* It's already mortal, so need to increase reference count. */
55289a74
NC
1740 RETVAL
1741 = SvREFCNT_inc(hv_delete(hash, key, UTF8KLEN(key_sv, len), flags));
1742 OUTPUT:
1743 RETVAL
1744
1745SV *
1746delete_ent(hash, key_sv, flags = 0)
1747 INPUT:
1748 HV *hash
1749 SV *key_sv
1750 I32 flags;
1751 CODE:
1752 /* It's already mortal, so need to increase reference count. */
1753 RETVAL = SvREFCNT_inc(hv_delete_ent(hash, key_sv, flags, 0));
b60cf05a
NC
1754 OUTPUT:
1755 RETVAL
1756
1757SV *
858117f8
NC
1758store_ent(hash, key, value)
1759 PREINIT:
1760 SV *copy;
1761 HE *result;
1762 INPUT:
1763 HV *hash
1764 SV *key
1765 SV *value
1766 CODE:
1767 copy = newSV(0);
1768 result = hv_store_ent(hash, key, copy, 0);
1769 SvSetMagicSV(copy, value);
1770 if (!result) {
1771 SvREFCNT_dec(copy);
1772 XSRETURN_EMPTY;
1773 }
1774 /* It's about to become mortal, so need to increase reference count.
1775 */
1776 RETVAL = SvREFCNT_inc(HeVAL(result));
1777 OUTPUT:
1778 RETVAL
1779
858117f8 1780SV *
b60cf05a
NC
1781store(hash, key_sv, value)
1782 PREINIT:
1783 STRLEN len;
1784 const char *key;
1785 SV *copy;
1786 SV **result;
1787 INPUT:
1788 HV *hash
1789 SV *key_sv
1790 SV *value
1791 CODE:
1792 key = SvPV(key_sv, len);
1793 copy = newSV(0);
028f8eaa 1794 result = hv_store(hash, key, UTF8KLEN(key_sv, len), copy, 0);
858117f8 1795 SvSetMagicSV(copy, value);
b60cf05a
NC
1796 if (!result) {
1797 SvREFCNT_dec(copy);
1798 XSRETURN_EMPTY;
1799 }
1800 /* It's about to become mortal, so need to increase reference count.
1801 */
1802 RETVAL = SvREFCNT_inc(*result);
1803 OUTPUT:
1804 RETVAL
1805
bdee33e4
NC
1806SV *
1807fetch_ent(hash, key_sv)
1808 PREINIT:
1809 HE *result;
1810 INPUT:
1811 HV *hash
1812 SV *key_sv
1813 CODE:
1814 result = hv_fetch_ent(hash, key_sv, 0, 0);
1815 if (!result) {
1816 XSRETURN_EMPTY;
1817 }
1818 /* Force mg_get */
1819 RETVAL = newSVsv(HeVAL(result));
1820 OUTPUT:
1821 RETVAL
b60cf05a
NC
1822
1823SV *
1824fetch(hash, key_sv)
1825 PREINIT:
1826 STRLEN len;
1827 const char *key;
1828 SV **result;
1829 INPUT:
1830 HV *hash
1831 SV *key_sv
1832 CODE:
1833 key = SvPV(key_sv, len);
028f8eaa 1834 result = hv_fetch(hash, key, UTF8KLEN(key_sv, len), 0);
b60cf05a
NC
1835 if (!result) {
1836 XSRETURN_EMPTY;
1837 }
1838 /* Force mg_get */
1839 RETVAL = newSVsv(*result);
1840 OUTPUT:
1841 RETVAL
2dc92170 1842
9568a123
NC
1843#if defined (hv_common)
1844
6b4de907
NC
1845SV *
1846common(params)
1847 INPUT:
1848 HV *params
1849 PREINIT:
1850 HE *result;
1851 HV *hv = NULL;
1852 SV *keysv = NULL;
1853 const char *key = NULL;
1854 STRLEN klen = 0;
1855 int flags = 0;
1856 int action = 0;
1857 SV *val = NULL;
1858 U32 hash = 0;
1859 SV **svp;
1860 CODE:
1861 if ((svp = hv_fetchs(params, "hv", 0))) {
1862 SV *const rv = *svp;
1863 if (!SvROK(rv))
1864 croak("common passed a non-reference for parameter hv");
1865 hv = (HV *)SvRV(rv);
1866 }
1867 if ((svp = hv_fetchs(params, "keysv", 0)))
1868 keysv = *svp;
1869 if ((svp = hv_fetchs(params, "keypv", 0))) {
1870 key = SvPV_const(*svp, klen);
1871 if (SvUTF8(*svp))
1872 flags = HVhek_UTF8;
1873 }
1874 if ((svp = hv_fetchs(params, "action", 0)))
1875 action = SvIV(*svp);
1876 if ((svp = hv_fetchs(params, "val", 0)))
527df579 1877 val = newSVsv(*svp);
6b4de907 1878 if ((svp = hv_fetchs(params, "hash", 0)))
a44093a9 1879 hash = SvUV(*svp);
6b4de907 1880
de092133 1881 if (hv_fetchs(params, "hash_pv", 0)) {
9959d439 1882 assert(key);
527df579
NC
1883 PERL_HASH(hash, key, klen);
1884 }
de092133 1885 if (hv_fetchs(params, "hash_sv", 0)) {
9959d439
JH
1886 assert(keysv);
1887 {
1888 STRLEN len;
1889 const char *const p = SvPV(keysv, len);
1890 PERL_HASH(hash, p, len);
1891 }
58ca560a 1892 }
527df579 1893
a75fcbca 1894 result = (HE *)hv_common(hv, keysv, key, klen, flags, action, val, hash);
6b4de907
NC
1895 if (!result) {
1896 XSRETURN_EMPTY;
1897 }
1898 /* Force mg_get */
1899 RETVAL = newSVsv(HeVAL(result));
1900 OUTPUT:
1901 RETVAL
1902
9568a123
NC
1903#endif
1904
439efdfe 1905void
2dc92170
NC
1906test_hv_free_ent()
1907 PPCODE:
2e66fe90 1908 test_freeent(&Perl_hv_free_ent);
2dc92170
NC
1909 XSRETURN(4);
1910
439efdfe 1911void
2dc92170
NC
1912test_hv_delayfree_ent()
1913 PPCODE:
2e66fe90 1914 test_freeent(&Perl_hv_delayfree_ent);
2dc92170 1915 XSRETURN(4);
35ab5632
NC
1916
1917SV *
1918test_share_unshare_pvn(input)
1919 PREINIT:
35ab5632
NC
1920 STRLEN len;
1921 U32 hash;
1922 char *pvx;
1923 char *p;
1924 INPUT:
1925 SV *input
1926 CODE:
1927 pvx = SvPV(input, len);
1928 PERL_HASH(hash, pvx, len);
1929 p = sharepvn(pvx, len, hash);
1930 RETVAL = newSVpvn(p, len);
1931 unsharepvn(p, len, hash);
1932 OUTPUT:
1933 RETVAL
d8c5b3c5 1934
9568a123
NC
1935#if PERL_VERSION >= 9
1936
d8c5b3c5
NC
1937bool
1938refcounted_he_exists(key, level=0)
1939 SV *key
1940 IV level
1941 CODE:
1942 if (level) {
147e3846 1943 croak("level must be zero, not %" IVdf, level);
d8c5b3c5 1944 }
20439bc7 1945 RETVAL = (cop_hints_fetch_sv(PL_curcop, key, 0, 0) != &PL_sv_placeholder);
d8c5b3c5
NC
1946 OUTPUT:
1947 RETVAL
1948
d8c5b3c5
NC
1949SV *
1950refcounted_he_fetch(key, level=0)
1951 SV *key
1952 IV level
1953 CODE:
1954 if (level) {
147e3846 1955 croak("level must be zero, not %" IVdf, level);
d8c5b3c5 1956 }
20439bc7 1957 RETVAL = cop_hints_fetch_sv(PL_curcop, key, 0, 0);
d8c5b3c5
NC
1958 SvREFCNT_inc(RETVAL);
1959 OUTPUT:
1960 RETVAL
65bfe90c 1961
9568a123 1962#endif
65bfe90c 1963
5f39160d
TC
1964void
1965test_force_keys(HV *hv)
1966 PREINIT:
1967 HE *he;
052a7c76 1968 SSize_t count = 0;
5f39160d
TC
1969 PPCODE:
1970 hv_iterinit(hv);
1971 he = hv_iternext(hv);
1972 while (he) {
1973 SV *sv = HeSVKEY_force(he);
1974 ++count;
1975 EXTEND(SP, count);
1976 PUSHs(sv_mortalcopy(sv));
1977 he = hv_iternext(hv);
1978 }
1979
0314122a
NC
1980=pod
1981
1982sub TIEHASH { bless {}, $_[0] }
1983sub STORE { $_[0]->{$_[1]} = $_[2] }
1984sub FETCH { $_[0]->{$_[1]} }
1985sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
1986sub NEXTKEY { each %{$_[0]} }
1987sub EXISTS { exists $_[0]->{$_[1]} }
1988sub DELETE { delete $_[0]->{$_[1]} }
1989sub CLEAR { %{$_[0]} = () }
1990
1991=cut
1992
e2fe06dd
EB
1993MODULE = XS::APItest:TempLv PACKAGE = XS::APItest::TempLv
1994
1995void
1996make_temp_mg_lv(sv)
1997SV* sv
1998 PREINIT:
1999 SV * const lv = newSV_type(SVt_PVLV);
2000 STRLEN len;
2001 PPCODE:
2002 SvPV(sv, len);
2003
2004 sv_magic(lv, NULL, PERL_MAGIC_substr, NULL, 0);
2005 LvTYPE(lv) = 'x';
2006 LvTARG(lv) = SvREFCNT_inc_simple(sv);
2007 LvTARGOFF(lv) = len == 0 ? 0 : 1;
2008 LvTARGLEN(lv) = len < 2 ? 0 : len-2;
2009
2010 EXTEND(SP, 1);
2011 ST(0) = sv_2mortal(lv);
2012 XSRETURN(1);
2013
2014
36c2b1d0
NC
2015MODULE = XS::APItest::PtrTable PACKAGE = XS::APItest::PtrTable PREFIX = ptr_table_
2016
2017void
2018ptr_table_new(classname)
2019const char * classname
2020 PPCODE:
2021 PUSHs(sv_setref_pv(sv_newmortal(), classname, (void*)ptr_table_new()));
2022
2023void
2024DESTROY(table)
2025XS::APItest::PtrTable table
2026 CODE:
2027 ptr_table_free(table);
2028
2029void
992b2363 2030ptr_table_store(table, from, to)
36c2b1d0 2031XS::APItest::PtrTable table
992b2363
NC
2032SVREF from
2033SVREF to
36c2b1d0 2034 CODE:
992b2363 2035 ptr_table_store(table, from, to);
36c2b1d0
NC
2036
2037UV
992b2363 2038ptr_table_fetch(table, from)
36c2b1d0 2039XS::APItest::PtrTable table
992b2363 2040SVREF from
36c2b1d0 2041 CODE:
992b2363 2042 RETVAL = PTR2UV(ptr_table_fetch(table, from));
36c2b1d0
NC
2043 OUTPUT:
2044 RETVAL
2045
2046void
2047ptr_table_split(table)
2048XS::APItest::PtrTable table
2049
2050void
2051ptr_table_clear(table)
2052XS::APItest::PtrTable table
2053
6911735f
FC
2054MODULE = XS::APItest::AutoLoader PACKAGE = XS::APItest::AutoLoader
2055
2056SV *
2057AUTOLOAD()
2058 CODE:
2059 RETVAL = newSVpvn_flags(SvPVX(cv), SvCUR(cv), SvUTF8(cv));
2060 OUTPUT:
2061 RETVAL
2062
8fa6a409
FC
2063SV *
2064AUTOLOADp(...)
2065 PROTOTYPE: *$
2066 CODE:
5f74c55e 2067 PERL_UNUSED_ARG(items);
8fa6a409
FC
2068 RETVAL = newSVpvn_flags(SvPVX(cv), SvCUR(cv), SvUTF8(cv));
2069 OUTPUT:
2070 RETVAL
2071
6911735f 2072
3e61d65a
JH
2073MODULE = XS::APItest PACKAGE = XS::APItest
2074
2075PROTOTYPES: DISABLE
2076
67ecba2f
FC
2077BOOT:
2078 mymro.resolve = myget_linear_isa;
2079 mymro.name = "justisa";
2080 mymro.length = 7;
2081 mymro.kflags = 0;
2082 mymro.hash = 0;
2083 Perl_mro_register(aTHX_ &mymro);
2084
f568d64d
BM
2085HV *
2086xop_custom_ops ()
2087 CODE:
2088 RETVAL = PL_custom_ops;
2089 OUTPUT:
2090 RETVAL
2091
2092HV *
2093xop_custom_op_names ()
2094 CODE:
2095 PL_custom_op_names = newHV();
2096 RETVAL = PL_custom_op_names;
2097 OUTPUT:
2098 RETVAL
2099
2100HV *
2101xop_custom_op_descs ()
2102 CODE:
2103 PL_custom_op_descs = newHV();
2104 RETVAL = PL_custom_op_descs;
2105 OUTPUT:
2106 RETVAL
2107
2108void
2109xop_register ()
2110 CODE:
2111 XopENTRY_set(&my_xop, xop_name, "my_xop");
2112 XopENTRY_set(&my_xop, xop_desc, "XOP for testing");
2113 XopENTRY_set(&my_xop, xop_class, OA_UNOP);
2114 XopENTRY_set(&my_xop, xop_peep, peep_xop);
2115 Perl_custom_op_register(aTHX_ pp_xop, &my_xop);
2116
2117void
2118xop_clear ()
2119 CODE:
2120 XopDISABLE(&my_xop, xop_name);
2121 XopDISABLE(&my_xop, xop_desc);
2122 XopDISABLE(&my_xop, xop_class);
2123 XopDISABLE(&my_xop, xop_peep);
2124
2125IV
2126xop_my_xop ()
2127 CODE:
2128 RETVAL = PTR2IV(&my_xop);
2129 OUTPUT:
2130 RETVAL
2131
2132IV
2133xop_ppaddr ()
2134 CODE:
2135 RETVAL = PTR2IV(pp_xop);
2136 OUTPUT:
2137 RETVAL
2138
2139IV
2140xop_OA_UNOP ()
2141 CODE:
2142 RETVAL = OA_UNOP;
2143 OUTPUT:
2144 RETVAL
2145
2146AV *
2147xop_build_optree ()
2148 CODE:
2149 dMY_CXT;
2150 UNOP *unop;
2151 OP *kid;
2152
2153 MY_CXT.xop_record = newAV();
2154
2155 kid = newSVOP(OP_CONST, 0, newSViv(42));
2156
3253bf85 2157 unop = (UNOP*)mkUNOP(OP_CUSTOM, kid);
f568d64d 2158 unop->op_ppaddr = pp_xop;
f568d64d 2159 unop->op_private = 0;
f568d64d
BM
2160 unop->op_next = NULL;
2161 kid->op_next = (OP*)unop;
2162
147e3846
KW
2163 av_push(MY_CXT.xop_record, newSVpvf("unop:%" UVxf, PTR2UV(unop)));
2164 av_push(MY_CXT.xop_record, newSVpvf("kid:%" UVxf, PTR2UV(kid)));
f568d64d
BM
2165
2166 av_push(MY_CXT.xop_record, newSVpvf("NAME:%s", OP_NAME((OP*)unop)));
2167 av_push(MY_CXT.xop_record, newSVpvf("DESC:%s", OP_DESC((OP*)unop)));
c33e8be1 2168 av_push(MY_CXT.xop_record, newSVpvf("CLASS:%d", (int)OP_CLASS((OP*)unop)));
f568d64d
BM
2169
2170 PL_rpeepp(aTHX_ kid);
2171
2172 FreeOp(kid);
2173 FreeOp(unop);
2174
2175 RETVAL = MY_CXT.xop_record;
2176 MY_CXT.xop_record = NULL;
2177 OUTPUT:
2178 RETVAL
2179
ae103e09
DD
2180IV
2181xop_from_custom_op ()
2182 CODE:
2183/* author note: this test doesn't imply Perl_custom_op_xop is or isn't public
2184 API or that Perl_custom_op_xop is known to be used outside the core */
2185 UNOP *unop;
2186 XOP *xop;
2187
3253bf85 2188 unop = (UNOP*)mkUNOP(OP_CUSTOM, NULL);
ae103e09 2189 unop->op_ppaddr = pp_xop;
ae103e09 2190 unop->op_private = 0;
ae103e09
DD
2191 unop->op_next = NULL;
2192
2193 xop = Perl_custom_op_xop(aTHX_ (OP *)unop);
2194 FreeOp(unop);
2195 RETVAL = PTR2IV(xop);
2196 OUTPUT:
2197 RETVAL
2198
85ce96a1
DM
2199BOOT:
2200{
2201 MY_CXT_INIT;
03569ecf 2202
85ce96a1
DM
2203 MY_CXT.i = 99;
2204 MY_CXT.sv = newSVpv("initial",0);
13b6b3bc
BM
2205
2206 MY_CXT.bhkav = get_av("XS::APItest::bhkav", GV_ADDMULTI);
2207 MY_CXT.bhk_record = 0;
2208
a88d97bf
BM
2209 BhkENTRY_set(&bhk_test, bhk_start, blockhook_test_start);
2210 BhkENTRY_set(&bhk_test, bhk_pre_end, blockhook_test_pre_end);
2211 BhkENTRY_set(&bhk_test, bhk_post_end, blockhook_test_post_end);
2212 BhkENTRY_set(&bhk_test, bhk_eval, blockhook_test_eval);
13b6b3bc
BM
2213 Perl_blockhook_register(aTHX_ &bhk_test);
2214
65bfe90c 2215 MY_CXT.cscgv = gv_fetchpvs("XS::APItest::COMPILE_SCOPE_CONTAINER",
13b6b3bc 2216 GV_ADDMULTI, SVt_PVAV);
03569ecf
BM
2217 MY_CXT.cscav = GvAV(MY_CXT.cscgv);
2218
a88d97bf
BM
2219 BhkENTRY_set(&bhk_csc, bhk_start, blockhook_csc_start);
2220 BhkENTRY_set(&bhk_csc, bhk_pre_end, blockhook_csc_pre_end);
13b6b3bc 2221 Perl_blockhook_register(aTHX_ &bhk_csc);
201c7e1f
FR
2222
2223 MY_CXT.peep_recorder = newAV();
2224 MY_CXT.rpeep_recorder = newAV();
2225
2226 MY_CXT.orig_peep = PL_peepp;
2227 MY_CXT.orig_rpeep = PL_rpeepp;
2228 PL_peepp = my_peep;
2229 PL_rpeepp = my_rpeep;
65bfe90c 2230}
85ce96a1
DM
2231
2232void
2233CLONE(...)
2234 CODE:
2235 MY_CXT_CLONE;
c33e8be1 2236 PERL_UNUSED_VAR(items);
85ce96a1 2237 MY_CXT.sv = newSVpv("initial_clone",0);
65bfe90c 2238 MY_CXT.cscgv = gv_fetchpvs("XS::APItest::COMPILE_SCOPE_CONTAINER",
13b6b3bc 2239 GV_ADDMULTI, SVt_PVAV);
03569ecf 2240 MY_CXT.cscav = NULL;
13b6b3bc
BM
2241 MY_CXT.bhkav = get_av("XS::APItest::bhkav", GV_ADDMULTI);
2242 MY_CXT.bhk_record = 0;
201c7e1f
FR
2243 MY_CXT.peep_recorder = newAV();
2244 MY_CXT.rpeep_recorder = newAV();
85ce96a1 2245
3e61d65a
JH
2246void
2247print_double(val)
2248 double val
2249 CODE:
2250 printf("%5.3f\n",val);
2251
2252int
2253have_long_double()
2254 CODE:
2255#ifdef HAS_LONG_DOUBLE
2256 RETVAL = 1;
2257#else
2258 RETVAL = 0;
2259#endif
cabb36f0
CN
2260 OUTPUT:
2261 RETVAL
3e61d65a
JH
2262
2263void
2264print_long_double()
2265 CODE:
2266#ifdef HAS_LONG_DOUBLE
fc0bf671 2267# if defined(PERL_PRIfldbl) && (LONG_DOUBLESIZE > DOUBLESIZE)
3e61d65a
JH
2268 long double val = 7.0;
2269 printf("%5.3" PERL_PRIfldbl "\n",val);
2270# else
2271 double val = 7.0;
2272 printf("%5.3f\n",val);
2273# endif
2274#endif
2275
2276void
3e61d65a
JH
2277print_int(val)
2278 int val
2279 CODE:
2280 printf("%d\n",val);
2281
2282void
2283print_long(val)
2284 long val
2285 CODE:
2286 printf("%ld\n",val);
2287
2288void
2289print_float(val)
2290 float val
2291 CODE:
2292 printf("%5.3f\n",val);
9d911683
NIS
2293
2294void
2295print_flush()
2296 CODE:
2297 fflush(stdout);
d4b90eee
SH
2298
2299void
2300mpushp()
2301 PPCODE:
2302 EXTEND(SP, 3);
2303 mPUSHp("one", 3);
2304 mPUSHp("two", 3);
2305 mPUSHp("three", 5);
2306 XSRETURN(3);
2307
2308void
2309mpushn()
2310 PPCODE:
2311 EXTEND(SP, 3);
2312 mPUSHn(0.5);
2313 mPUSHn(-0.25);
2314 mPUSHn(0.125);
2315 XSRETURN(3);
2316
2317void
2318mpushi()
2319 PPCODE:
2320 EXTEND(SP, 3);
d75b63cf
MHM
2321 mPUSHi(-1);
2322 mPUSHi(2);
2323 mPUSHi(-3);
d4b90eee
SH
2324 XSRETURN(3);
2325
2326void
2327mpushu()
2328 PPCODE:
2329 EXTEND(SP, 3);
d75b63cf
MHM
2330 mPUSHu(1);
2331 mPUSHu(2);
2332 mPUSHu(3);
d4b90eee
SH
2333 XSRETURN(3);
2334
2335void
2336mxpushp()
2337 PPCODE:
2338 mXPUSHp("one", 3);
2339 mXPUSHp("two", 3);
2340 mXPUSHp("three", 5);
2341 XSRETURN(3);
2342
2343void
2344mxpushn()
2345 PPCODE:
2346 mXPUSHn(0.5);
2347 mXPUSHn(-0.25);
2348 mXPUSHn(0.125);
2349 XSRETURN(3);
2350
2351void
2352mxpushi()
2353 PPCODE:
d75b63cf
MHM
2354 mXPUSHi(-1);
2355 mXPUSHi(2);
2356 mXPUSHi(-3);
d4b90eee
SH
2357 XSRETURN(3);
2358
2359void
2360mxpushu()
2361 PPCODE:
d75b63cf
MHM
2362 mXPUSHu(1);
2363 mXPUSHu(2);
2364 mXPUSHu(3);
d4b90eee 2365 XSRETURN(3);
d1f347d7 2366
6768377c
DM
2367
2368 # test_EXTEND(): excerise the EXTEND() macro.
2369 # After calling EXTEND(), it also does *(p+n) = NULL and
2370 # *PL_stack_max = NULL to allow valgrind etc to spot if the stack hasn't
2371 # actually been extended properly.
2372 #
2373 # max_offset specifies the SP to use. It is treated as a signed offset
2374 # from PL_stack_max.
2375 # nsv is the SV holding the value of n indicating how many slots
2376 # to extend the stack by.
2377 # use_ss is a boolean indicating that n should be cast to a SSize_t
2378
2379void
2380test_EXTEND(max_offset, nsv, use_ss)
2381 IV max_offset;
2382 SV *nsv;
2383 bool use_ss;
2384PREINIT:
2385 SV **sp = PL_stack_max + max_offset;
2386PPCODE:
2387 if (use_ss) {
2388 SSize_t n = (SSize_t)SvIV(nsv);
2389 EXTEND(sp, n);
2390 *(sp + n) = NULL;
2391 }
2392 else {
2393 IV n = SvIV(nsv);
2394 EXTEND(sp, n);
2395 *(sp + n) = NULL;
2396 }
2397 *PL_stack_max = NULL;
2398
2399
a85ce6f0
DD
2400void
2401call_sv_C()
2402PREINIT:
2403 CV * i_sub;
2404 GV * i_gv;
2405 I32 retcnt;
2406 SV * errsv;
2407 char * errstr;
bdb7e3f0 2408 STRLEN errlen;
a85ce6f0
DD
2409 SV * miscsv = sv_newmortal();
2410 HV * hv = (HV*)sv_2mortal((SV*)newHV());
2411CODE:
2412 i_sub = get_cv("i", 0);
2413 PUSHMARK(SP);
2414 /* PUTBACK not needed since this sub was called with 0 args, and is calling
2415 0 args, so global SP doesn't need to be moved before a call_* */
2416 retcnt = call_sv((SV*)i_sub, 0); /* try a CV* */
2417 SPAGAIN;
2418 SP -= retcnt; /* dont care about return count, wipe everything off */
2419 sv_setpvs(miscsv, "i");
2420 PUSHMARK(SP);
2421 retcnt = call_sv(miscsv, 0); /* try a PV */
2422 SPAGAIN;
2423 SP -= retcnt;
2424 /* no add and SVt_NULL are intentional, sub i should be defined already */
2425 i_gv = gv_fetchpvn_flags("i", sizeof("i")-1, 0, SVt_NULL);
2426 PUSHMARK(SP);
2427 retcnt = call_sv((SV*)i_gv, 0); /* try a GV* */
2428 SPAGAIN;
2429 SP -= retcnt;
2430 /* the tests below are not declaring this being public API behavior,
2431 only current internal behavior, these tests can be changed in the
2432 future if necessery */
2433 PUSHMARK(SP);
0740a29d 2434 retcnt = call_sv(&PL_sv_yes, G_EVAL);
a85ce6f0
DD
2435 SPAGAIN;
2436 SP -= retcnt;
0740a29d
Z
2437 errsv = ERRSV;
2438 errstr = SvPV(errsv, errlen);
2439 if(memBEGINs(errstr, errlen, "Undefined subroutine &main::1 called at")) {
2440 PUSHMARK(SP);
2441 retcnt = call_sv((SV*)i_sub, 0); /* call again to increase counter */
2442 SPAGAIN;
2443 SP -= retcnt;
2444 }
a85ce6f0
DD
2445 PUSHMARK(SP);
2446 retcnt = call_sv(&PL_sv_no, G_EVAL);
2447 SPAGAIN;
2448 SP -= retcnt;
2449 errsv = ERRSV;
bdb7e3f0
KW
2450 errstr = SvPV(errsv, errlen);
2451 if(memBEGINs(errstr, errlen, "Undefined subroutine &main:: called at")) {
a85ce6f0
DD
2452 PUSHMARK(SP);
2453 retcnt = call_sv((SV*)i_sub, 0); /* call again to increase counter */
2454 SPAGAIN;
2455 SP -= retcnt;
2456 }
2457 PUSHMARK(SP);
2458 retcnt = call_sv(&PL_sv_undef, G_EVAL);
2459 SPAGAIN;
2460 SP -= retcnt;
2461 errsv = ERRSV;
bdb7e3f0
KW
2462 errstr = SvPV(errsv, errlen);
2463 if(memBEGINs(errstr, errlen, "Can't use an undefined value as a subroutine reference at")) {
a85ce6f0
DD
2464 PUSHMARK(SP);
2465 retcnt = call_sv((SV*)i_sub, 0); /* call again to increase counter */
2466 SPAGAIN;
2467 SP -= retcnt;
2468 }
2469 PUSHMARK(SP);
2470 retcnt = call_sv((SV*)hv, G_EVAL);
2471 SPAGAIN;
2472 SP -= retcnt;
2473 errsv = ERRSV;
bdb7e3f0
KW
2474 errstr = SvPV(errsv, errlen);
2475 if(memBEGINs(errstr, errlen, "Not a CODE reference at")) {
a85ce6f0
DD
2476 PUSHMARK(SP);
2477 retcnt = call_sv((SV*)i_sub, 0); /* call again to increase counter */
2478 SPAGAIN;
2479 SP -= retcnt;
2480 }
d1f347d7
DM
2481
2482void
2483call_sv(sv, flags, ...)
2484 SV* sv
2485 I32 flags
2486 PREINIT:
2487 I32 i;
2488 PPCODE:
2489 for (i=0; i<items-2; i++)
2490 ST(i) = ST(i+2); /* pop first two args */
2491 PUSHMARK(SP);
2492 SP += items - 2;
2493 PUTBACK;
2494 i = call_sv(sv, flags);
2495 SPAGAIN;
2496 EXTEND(SP, 1);
2497 PUSHs(sv_2mortal(newSViv(i)));
2498
2499void
2500call_pv(subname, flags, ...)
2501 char* subname
2502 I32 flags
2503 PREINIT:
2504 I32 i;
2505 PPCODE:
2506 for (i=0; i<items-2; i++)
2507 ST(i) = ST(i+2); /* pop first two args */
2508 PUSHMARK(SP);
2509 SP += items - 2;
2510 PUTBACK;
2511 i = call_pv(subname, flags);
2512 SPAGAIN;
2513 EXTEND(SP, 1);
2514 PUSHs(sv_2mortal(newSViv(i)));
2515
2516void
c06180d6
FC
2517call_argv(subname, flags, ...)
2518 char* subname
2519 I32 flags
2520 PREINIT:
2521 I32 i;
2522 char *tmpary[4];
2523 PPCODE:
2524 for (i=0; i<items-2; i++)
2525 tmpary[i] = SvPV_nolen(ST(i+2)); /* ignore first two args */
2526 tmpary[i] = NULL;
2527 PUTBACK;
2528 i = call_argv(subname, flags, tmpary);
2529 SPAGAIN;
2530 EXTEND(SP, 1);
2531 PUSHs(sv_2mortal(newSViv(i)));
2532
2533void
d1f347d7
DM
2534call_method(methname, flags, ...)
2535 char* methname
2536 I32 flags
2537 PREINIT:
2538 I32 i;
2539 PPCODE:
2540 for (i=0; i<items-2; i++)
2541 ST(i) = ST(i+2); /* pop first two args */
2542 PUSHMARK(SP);
2543 SP += items - 2;
2544 PUTBACK;
2545 i = call_method(methname, flags);
2546 SPAGAIN;
2547 EXTEND(SP, 1);
2548 PUSHs(sv_2mortal(newSViv(i)));
2549
2550void
c0810f8e 2551newCONSTSUB(stash, name, flags, sv)
3453414d
BF
2552 HV* stash
2553 SV* name
2554 I32 flags
8f82b567 2555 SV* sv
c0810f8e
NC
2556 ALIAS:
2557 newCONSTSUB_flags = 1
3453414d 2558 PREINIT:
5f74c55e 2559 CV* mycv = NULL;
9c0a6090
FC
2560 STRLEN len;
2561 const char *pv = SvPV(name, len);
3453414d 2562 PPCODE:
c0810f8e 2563 switch (ix) {
3453414d 2564 case 0:
dc4b20bd 2565 mycv = newCONSTSUB(stash, pv, SvOK(sv) ? SvREFCNT_inc(sv) : NULL);
3453414d
BF
2566 break;
2567 case 1:
dc4b20bd 2568 mycv = newCONSTSUB_flags(
0fc4714a 2569 stash, pv, len, flags | SvUTF8(name), SvOK(sv) ? SvREFCNT_inc(sv) : NULL
8f82b567 2570 );
3453414d
BF
2571 break;
2572 }
2573 EXTEND(SP, 2);
316ebaf2 2574 assert(mycv);
dc4b20bd
CB
2575 PUSHs( CvCONST(mycv) ? &PL_sv_yes : &PL_sv_no );
2576 PUSHs((SV*)CvGV(mycv));
3453414d
BF
2577
2578void
e6066781
BF
2579gv_init_type(namesv, multi, flags, type)
2580 SV* namesv
2581 int multi
2582 I32 flags
2583 int type
2584 PREINIT:
2585 STRLEN len;
2586 const char * const name = SvPV_const(namesv, len);
2587 GV *gv = *(GV**)hv_fetch(PL_defstash, name, len, TRUE);
2588 PPCODE:
2589 if (SvTYPE(gv) == SVt_PVGV)
2590 Perl_croak(aTHX_ "GV is already a PVGV");
04ec7e59 2591 if (multi) flags |= GV_ADDMULTI;
e6066781
BF
2592 switch (type) {
2593 case 0:
2594 gv_init(gv, PL_defstash, name, len, multi);
2595 break;
2596 case 1:
04ec7e59 2597 gv_init_sv(gv, PL_defstash, namesv, flags);
e6066781
BF
2598 break;
2599 case 2:
04ec7e59 2600 gv_init_pv(gv, PL_defstash, name, flags | SvUTF8(namesv));
e6066781
BF
2601 break;
2602 case 3:
04ec7e59 2603 gv_init_pvn(gv, PL_defstash, name, len, flags | SvUTF8(namesv));
e6066781
BF
2604 break;
2605 }
2606 XPUSHs( gv ? (SV*)gv : &PL_sv_undef);
2607
2608void
e6919483
BF
2609gv_fetchmeth_type(stash, methname, type, level, flags)
2610 HV* stash
2611 SV* methname
2612 int type
2613 I32 level
2614 I32 flags
2615 PREINIT:
2616 STRLEN len;
2617 const char * const name = SvPV_const(methname, len);
5f74c55e 2618 GV* gv = NULL;
e6919483
BF
2619 PPCODE:
2620 switch (type) {
2621 case 0:
2622 gv = gv_fetchmeth(stash, name, len, level);
2623 break;
2624 case 1:
2625 gv = gv_fetchmeth_sv(stash, methname, level, flags);
2626 break;
2627 case 2:
2628 gv = gv_fetchmeth_pv(stash, name, level, flags | SvUTF8(methname));
2629 break;
2630 case 3:
2631 gv = gv_fetchmeth_pvn(stash, name, len, level, flags | SvUTF8(methname));
2632 break;
2633 }
2634 XPUSHs( gv ? MUTABLE_SV(gv) : &PL_sv_undef );
2635
2636void
d21989ed
BF
2637gv_fetchmeth_autoload_type(stash, methname, type, level, flags)
2638 HV* stash
2639 SV* methname
2640 int type
2641 I32 level
2642 I32 flags
2643 PREINIT:
2644 STRLEN len;
2645 const char * const name = SvPV_const(methname, len);
5f74c55e 2646 GV* gv = NULL;
d21989ed
BF
2647 PPCODE:
2648 switch (type) {
2649 case 0:
2650 gv = gv_fetchmeth_autoload(stash, name, len, level);
2651 break;
2652 case 1:
2653 gv = gv_fetchmeth_sv_autoload(stash, methname, level, flags);
2654 break;
2655 case 2:
2656 gv = gv_fetchmeth_pv_autoload(stash, name, level, flags | SvUTF8(methname));
2657 break;
2658 case 3:
2659 gv = gv_fetchmeth_pvn_autoload(stash, name, len, level, flags | SvUTF8(methname));
2660 break;
2661 }
2662 XPUSHs( gv ? MUTABLE_SV(gv) : &PL_sv_undef );
2663
2664void
44130a26
BF
2665gv_fetchmethod_flags_type(stash, methname, type, flags)
2666 HV* stash
2667 SV* methname
2668 int type
2669 I32 flags
2670 PREINIT:
5f74c55e 2671 GV* gv = NULL;
44130a26
BF
2672 PPCODE:
2673 switch (type) {
2674 case 0:
2675 gv = gv_fetchmethod_flags(stash, SvPVX_const(methname), flags);
2676 break;
2677 case 1:
2678 gv = gv_fetchmethod_sv_flags(stash, methname, flags);
2679 break;
2680 case 2:
2681 gv = gv_fetchmethod_pv_flags(stash, SvPV_nolen(methname), flags | SvUTF8(methname));
2682 break;
2683 case 3: {
2684 STRLEN len;
2685 const char * const name = SvPV_const(methname, len);
2686 gv = gv_fetchmethod_pvn_flags(stash, name, len, flags | SvUTF8(methname));
2687 break;
2688 }
1665b718
FC
2689 case 4:
2690 gv = gv_fetchmethod_pvn_flags(stash, SvPV_nolen(methname),
2691 flags, SvUTF8(methname));
44130a26 2692 }
5fba3c91 2693 XPUSHs( gv ? (SV*)gv : &PL_sv_undef);
44130a26 2694
5fba3c91 2695void
0eeb01b9 2696gv_autoload_type(stash, methname, type, method)
5fba3c91
BF
2697 HV* stash
2698 SV* methname
2699 int type
2700 I32 method
5fba3c91
BF
2701 PREINIT:
2702 STRLEN len;
2703 const char * const name = SvPV_const(methname, len);
5f74c55e 2704 GV* gv = NULL;
0eeb01b9 2705 I32 flags = method ? GV_AUTOLOAD_ISMETHOD : 0;
5fba3c91
BF
2706 PPCODE:
2707 switch (type) {
2708 case 0:
2709 gv = gv_autoload4(stash, name, len, method);
2710 break;
2711 case 1:
0eeb01b9 2712 gv = gv_autoload_sv(stash, methname, flags);
5fba3c91
BF
2713 break;
2714 case 2:
0eeb01b9 2715 gv = gv_autoload_pv(stash, name, flags | SvUTF8(methname));
5fba3c91
BF
2716 break;
2717 case 3:
0eeb01b9 2718 gv = gv_autoload_pvn(stash, name, len, flags | SvUTF8(methname));
5fba3c91
BF
2719 break;
2720 }
44130a26
BF
2721 XPUSHs( gv ? (SV*)gv : &PL_sv_undef);
2722
16a6e5a4
TC
2723SV *
2724gv_const_sv(SV *name)
2725 PREINIT:
2726 GV *gv;
2727 CODE:
2728 if (SvPOK(name)) {
2729 HV *stash = gv_stashpv("main",0);
2730 HE *he = hv_fetch_ent(stash, name, 0, 0);
2731 gv = (GV *)HeVAL(he);
2732 }
2733 else {
2734 gv = (GV *)name;
2735 }
2736 RETVAL = gv_const_sv(gv);
2737 if (!RETVAL)
2738 XSRETURN_EMPTY;
2739 RETVAL = newSVsv(RETVAL);
2740 OUTPUT:
2741 RETVAL
2742
44130a26 2743void
84c7b88c
BF
2744whichsig_type(namesv, type)
2745 SV* namesv
2746 int type
2747 PREINIT:
2748 STRLEN len;
2749 const char * const name = SvPV_const(namesv, len);
5f74c55e 2750 I32 i = 0;
84c7b88c
BF
2751 PPCODE:
2752 switch (type) {
2753 case 0:
2754 i = whichsig(name);
2755 break;
2756 case 1:
2757 i = whichsig_sv(namesv);
2758 break;
2759 case 2:
2760 i = whichsig_pv(name);
2761 break;
2762 case 3:
2763 i = whichsig_pvn(name, len);
2764 break;
2765 }
2766 XPUSHs(sv_2mortal(newSViv(i)));
2767
2768void
d1f347d7
DM
2769eval_sv(sv, flags)
2770 SV* sv
2771 I32 flags
2772 PREINIT:
2773 I32 i;
2774 PPCODE:
2775 PUTBACK;
2776 i = eval_sv(sv, flags);
2777 SPAGAIN;
2778 EXTEND(SP, 1);
2779 PUSHs(sv_2mortal(newSViv(i)));
2780
b8e65a9b 2781void
d1f347d7
DM
2782eval_pv(p, croak_on_error)
2783 const char* p
2784 I32 croak_on_error
d1f347d7
DM
2785 PPCODE:
2786 PUTBACK;
2787 EXTEND(SP, 1);
2788 PUSHs(eval_pv(p, croak_on_error));
2789
2790void
2791require_pv(pv)
2792 const char* pv
d1f347d7
DM
2793 PPCODE:
2794 PUTBACK;
2795 require_pv(pv);
2796
0ca3a874 2797int
7a646707 2798apitest_exception(throw_e)
0ca3a874
MHM
2799 int throw_e
2800 OUTPUT:
2801 RETVAL
d1f347d7 2802
ef469b03 2803void
7e7a3dfc
GA
2804mycroak(sv)
2805 SV* sv
ef469b03 2806 CODE:
7e7a3dfc
GA
2807 if (SvOK(sv)) {
2808 Perl_croak(aTHX_ "%s", SvPV_nolen(sv));
2809 }
2810 else {
2811 Perl_croak(aTHX_ NULL);
2812 }
5d2b1485
NC
2813
2814SV*
2815strtab()
2816 CODE:
2817 RETVAL = newRV_inc((SV*)PL_strtab);
2818 OUTPUT:
2819 RETVAL
85ce96a1
DM
2820
2821int
2822my_cxt_getint()
2823 CODE:
2824 dMY_CXT;
2825 RETVAL = my_cxt_getint_p(aMY_CXT);
2826 OUTPUT:
2827 RETVAL
2828
2829void
2830my_cxt_setint(i)
2831 int i;
2832 CODE:
2833 dMY_CXT;
2834 my_cxt_setint_p(aMY_CXT_ i);
2835
2836void
9568a123
NC
2837my_cxt_getsv(how)
2838 bool how;
85ce96a1 2839 PPCODE:
85ce96a1 2840 EXTEND(SP, 1);
9568a123 2841 ST(0) = how ? my_cxt_getsv_interp_context() : my_cxt_getsv_interp();
85ce96a1
DM
2842 XSRETURN(1);
2843
2844void
2845my_cxt_setsv(sv)
2846 SV *sv;
2847 CODE:
2848 dMY_CXT;
2849 SvREFCNT_dec(MY_CXT.sv);
2850 my_cxt_setsv_p(sv _aMY_CXT);
2851 SvREFCNT_inc(sv);
34482cd6
NC
2852
2853bool
2854sv_setsv_cow_hashkey_core()
2855
2856bool
2857sv_setsv_cow_hashkey_notcore()
84ac5fd7
NC
2858
2859void
74ee33f2
FC
2860sv_set_deref(SV *sv, SV *sv2, int which)
2861 CODE:
2862 {
2863 STRLEN len;
2864 const char *pv = SvPV(sv2,len);
2865 if (!SvROK(sv)) croak("Not a ref");
2866 sv = SvRV(sv);
2867 switch (which) {
2868 case 0: sv_setsv(sv,sv2); break;
2869 case 1: sv_setpv(sv,pv); break;
2870 case 2: sv_setpvn(sv,pv,len); break;
2871 }
2872 }
2873
2874void
218787bd
VP
2875rmagical_cast(sv, type)
2876 SV *sv;
2877 SV *type;
2878 PREINIT:
2879 struct ufuncs uf;
2880 PPCODE:
2881 if (!SvOK(sv) || !SvROK(sv) || !SvOK(type)) { XSRETURN_UNDEF; }
2882 sv = SvRV(sv);
2883 if (SvTYPE(sv) != SVt_PVHV) { XSRETURN_UNDEF; }
2884 uf.uf_val = rmagical_a_dummy;
2885 uf.uf_set = NULL;
2886 uf.uf_index = 0;
2887 if (SvTRUE(type)) { /* b */
2888 sv_magicext(sv, NULL, PERL_MAGIC_ext, &rmagical_b, NULL, 0);
2889 } else { /* a */
2890 sv_magic(sv, NULL, PERL_MAGIC_uvar, (char *) &uf, sizeof(uf));
2891 }
2892 XSRETURN_YES;
2893
2894void
2895rmagical_flags(sv)
2896 SV *sv;
2897 PPCODE:
2898 if (!SvOK(sv) || !SvROK(sv)) { XSRETURN_UNDEF; }
2899 sv = SvRV(sv);
2900 EXTEND(SP, 3);
2901 mXPUSHu(SvFLAGS(sv) & SVs_GMG);
2902 mXPUSHu(SvFLAGS(sv) & SVs_SMG);
2903 mXPUSHu(SvFLAGS(sv) & SVs_RMG);
2904 XSRETURN(3);
2905
2906void
90d1f214
BM
2907my_caller(level)
2908 I32 level
2909 PREINIT:
2910 const PERL_CONTEXT *cx, *dbcx;
2911 const char *pv;
2912 const GV *gv;
2913 HV *hv;
2914 PPCODE:
2915 cx = caller_cx(level, &dbcx);
2916 EXTEND(SP, 8);
2917
2918 pv = CopSTASHPV(cx->blk_oldcop);
2919 ST(0) = pv ? sv_2mortal(newSVpv(pv, 0)) : &PL_sv_undef;
2920 gv = CvGV(cx->blk_sub.cv);
2921 ST(1) = isGV(gv) ? sv_2mortal(newSVpv(GvNAME(gv), 0)) : &PL_sv_undef;
2922
2923 pv = CopSTASHPV(dbcx->blk_oldcop);
2924 ST(2) = pv ? sv_2mortal(newSVpv(pv, 0)) : &PL_sv_undef;
2925 gv = CvGV(dbcx->blk_sub.cv);
2926 ST(3) = isGV(gv) ? sv_2mortal(newSVpv(GvNAME(gv), 0)) : &PL_sv_undef;
2927
20439bc7
Z
2928 ST(4) = cop_hints_fetch_pvs(cx->blk_oldcop, "foo", 0);
2929 ST(5) = cop_hints_fetch_pvn(cx->blk_oldcop, "foo", 3, 0, 0);
2930 ST(6) = cop_hints_fetch_sv(cx->blk_oldcop,
c2b90b61 2931 sv_2mortal(newSVpvs("foo")), 0, 0);
90d1f214 2932
20439bc7 2933 hv = cop_hints_2hv(cx->blk_oldcop, 0);
90d1f214
BM
2934 ST(7) = hv ? sv_2mortal(newRV_noinc((SV *)hv)) : &PL_sv_undef;
2935
2936 XSRETURN(8);
2937
2938void
f9c17636
MB
2939DPeek (sv)
2940 SV *sv
2941
2942 PPCODE:
5b1f7359 2943 ST (0) = newSVpv (Perl_sv_peek (aTHX_ sv), 0);
f9c17636
MB
2944 XSRETURN (1);
2945
2946void
84ac5fd7
NC
2947BEGIN()
2948 CODE:
2949 sv_inc(get_sv("XS::APItest::BEGIN_called", GV_ADD|GV_ADDMULTI));
2950
2951void
2952CHECK()
2953 CODE:
2954 sv_inc(get_sv("XS::APItest::CHECK_called", GV_ADD|GV_ADDMULTI));
2955
2956void
2957UNITCHECK()
2958 CODE:
0932863f 2959 sv_inc(get_sv("XS::APItest::UNITCHECK_called", GV_ADD|GV_ADDMULTI));
84ac5fd7
NC
2960
2961void
2962INIT()
2963 CODE:
2964 sv_inc(get_sv("XS::APItest::INIT_called", GV_ADD|GV_ADDMULTI));
2965
2966void
2967END()
2968 CODE:
2969 sv_inc(get_sv("XS::APItest::END_called", GV_ADD|GV_ADDMULTI));
30685b56
NC
2970
2971void
2972utf16_to_utf8 (sv, ...)
2973 SV* sv
2974 ALIAS:
2975 utf16_to_utf8_reversed = 1
2976 PREINIT:
2977 STRLEN len;
2978 U8 *source;
2979 SV *dest;
2980 I32 got; /* Gah, badly thought out APIs */
2981 CODE:
a4d7a71b 2982 if (ix) (void)SvPV_force_nolen(sv);
30685b56
NC
2983 source = (U8 *)SvPVbyte(sv, len);
2984 /* Optionally only convert part of the buffer. */
2985 if (items > 1) {
2986 len = SvUV(ST(1));
2987 }
2988 /* Mortalise this right now, as we'll be testing croak()s */
624504c5 2989 dest = sv_2mortal(newSV(len * 2 + 1));
30685b56 2990 if (ix) {
25f2e844 2991 utf16_to_utf8_reversed(source, (U8 *)SvPVX(dest), len, &got);
30685b56 2992 } else {
25f2e844 2993 utf16_to_utf8(source, (U8 *)SvPVX(dest), len, &got);
30685b56
NC
2994 }
2995 SvCUR_set(dest, got);
2996 SvPVX(dest)[got] = '\0';
2997 SvPOK_on(dest);
2998 ST(0) = dest;
2999 XSRETURN(1);
879d0c72 3000
6bd7445c
GG
3001void
3002my_exit(int exitcode)
3003 PPCODE:
3004 my_exit(exitcode);
d97c33b5 3005
209e41dc
NT
3006U8
3007first_byte(sv)
3008 SV *sv
3009 CODE:
3010 char *s;
3011 STRLEN len;
3012 s = SvPVbyte(sv, len);
3013 RETVAL = s[0];
3014 OUTPUT:
3015 RETVAL
3016
d97c33b5
DM
3017I32
3018sv_count()
3019 CODE:
3020 RETVAL = PL_sv_count;
3021 OUTPUT:
3022 RETVAL
13b6b3bc
BM
3023
3024void
3025bhk_record(bool on)
3026 CODE:
3027 dMY_CXT;
3028 MY_CXT.bhk_record = on;
3029 if (on)
3030 av_clear(MY_CXT.bhkav);
65bfe90c 3031
defdfed5 3032void
d9088386
Z
3033test_magic_chain()
3034 PREINIT:
3035 SV *sv;
3036 MAGIC *callmg, *uvarmg;
3037 CODE:
3038 sv = sv_2mortal(newSV(0));
11f9f0ed
NC
3039 if (SvTYPE(sv) >= SVt_PVMG) croak_fail();
3040 if (SvMAGICAL(sv)) croak_fail();
d9088386 3041 sv_magic(sv, &PL_sv_yes, PERL_MAGIC_checkcall, (char*)&callmg, 0);
11f9f0ed
NC
3042 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
3043 if (!SvMAGICAL(sv)) croak_fail();
3044 if (mg_find(sv, PERL_MAGIC_uvar)) croak_fail();
d9088386 3045 callmg = mg_find(sv, PERL_MAGIC_checkcall);
11f9f0ed 3046 if (!callmg) croak_fail();
d9088386 3047 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 3048 croak_fail();
d9088386 3049 sv_magic(sv, &PL_sv_no, PERL_MAGIC_uvar, (char*)&uvarmg, 0);
11f9f0ed
NC
3050 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
3051 if (!SvMAGICAL(sv)) croak_fail();
3052 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
d9088386 3053 uvarmg = mg_find(sv, PERL_MAGIC_uvar);
11f9f0ed 3054 if (!uvarmg) croak_fail();
d9088386 3055 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 3056 croak_fail();
d9088386 3057 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 3058 croak_fail();
d9088386 3059 mg_free_type(sv, PERL_MAGIC_vec);
11f9f0ed
NC
3060 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
3061 if (!SvMAGICAL(sv)) croak_fail();
3062 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
3063 if (mg_find(sv, PERL_MAGIC_uvar) != uvarmg) croak_fail();
d9088386 3064 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 3065 croak_fail();
d9088386 3066 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 3067 croak_fail();
d9088386 3068 mg_free_type(sv, PERL_MAGIC_uvar);
11f9f0ed
NC
3069 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
3070 if (!SvMAGICAL(sv)) croak_fail();
3071 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
3072 if (mg_find(sv, PERL_MAGIC_uvar)) croak_fail();
d9088386 3073 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 3074 croak_fail();
d9088386 3075 sv_magic(sv, &PL_sv_no, PERL_MAGIC_uvar, (char*)&uvarmg, 0);
11f9f0ed
NC
3076 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
3077 if (!SvMAGICAL(sv)) croak_fail();
3078 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
d9088386 3079 uvarmg = mg_find(sv, PERL_MAGIC_uvar);
11f9f0ed 3080 if (!uvarmg) croak_fail();
d9088386 3081 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 3082 croak_fail();
d9088386 3083 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 3084 croak_fail();
d9088386 3085 mg_free_type(sv, PERL_MAGIC_checkcall);
11f9f0ed
NC
3086 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
3087 if (!SvMAGICAL(sv)) croak_fail();
3088 if (mg_find(sv, PERL_MAGIC_uvar) != uvarmg) croak_fail();
3089 if (mg_find(sv, PERL_MAGIC_checkcall)) croak_fail();
d9088386 3090 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 3091 croak_fail();
d9088386 3092 mg_free_type(sv, PERL_MAGIC_uvar);
11f9f0ed
NC
3093 if (SvMAGICAL(sv)) croak_fail();
3094 if (mg_find(sv, PERL_MAGIC_checkcall)) croak_fail();
3095 if (mg_find(sv, PERL_MAGIC_uvar)) croak_fail();
d9088386
Z
3096
3097void
3098test_op_contextualize()
3099 PREINIT:
3100 OP *o;
3101 CODE:
3102 o = newSVOP(OP_CONST, 0, newSViv(0));
3103 o->op_flags &= ~OPf_WANT;
3104 o = op_contextualize(o, G_SCALAR);
3105 if (o->op_type != OP_CONST ||
3106 (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR)
11f9f0ed 3107 croak_fail();
d9088386
Z
3108 op_free(o);
3109 o = newSVOP(OP_CONST, 0, newSViv(0));
3110 o->op_flags &= ~OPf_WANT;
3111 o = op_contextualize(o, G_ARRAY);
3112 if (o->op_type != OP_CONST ||
3113 (o->op_flags & OPf_WANT) != OPf_WANT_LIST)
11f9f0ed 3114 croak_fail();
d9088386
Z
3115 op_free(o);
3116 o = newSVOP(OP_CONST, 0, newSViv(0));
3117 o->op_flags &= ~OPf_WANT;
3118 o = op_contextualize(o, G_VOID);
11f9f0ed 3119 if (o->op_type != OP_NULL) croak_fail();
d9088386
Z
3120 op_free(o);
3121
3122void
3123test_rv2cv_op_cv()
3124 PROTOTYPE:
3125 PREINIT:
7616a0c2 3126 GV *troc_gv;
d9088386
Z
3127 CV *troc_cv;
3128 OP *o;
3129 CODE:
3130 troc_gv = gv_fetchpv("XS::APItest::test_rv2cv_op_cv", 0, SVt_PVGV);
3131 troc_cv = get_cv("XS::APItest::test_rv2cv_op_cv", 0);
d9088386 3132 o = newCVREF(0, newGVOP(OP_GV, 0, troc_gv));
11f9f0ed 3133 if (rv2cv_op_cv(o, 0) != troc_cv) croak_fail();
d9088386 3134 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV) != (CV*)troc_gv)
11f9f0ed 3135 croak_fail();
d9088386 3136 o->op_private |= OPpENTERSUB_AMPER;
11f9f0ed
NC
3137 if (rv2cv_op_cv(o, 0)) croak_fail();
3138 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386 3139 o->op_private &= ~OPpENTERSUB_AMPER;
11f9f0ed
NC
3140 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
3141 if (rv2cv_op_cv(o, RV2CVOPCV_MARK_EARLY) != troc_cv) croak_fail();
3142 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
d9088386
Z
3143 op_free(o);
3144 o = newSVOP(OP_CONST, 0, newSVpv("XS::APItest::test_rv2cv_op_cv", 0));
3145 o->op_private = OPpCONST_BARE;
3146 o = newCVREF(0, o);
11f9f0ed 3147 if (rv2cv_op_cv(o, 0) != troc_cv) croak_fail();
d9088386 3148 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV) != (CV*)troc_gv)
11f9f0ed 3149 croak_fail();
d9088386 3150 o->op_private |= OPpENTERSUB_AMPER;
11f9f0ed
NC
3151 if (rv2cv_op_cv(o, 0)) croak_fail();
3152 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386
Z
3153 op_free(o);
3154 o = newCVREF(0, newSVOP(OP_CONST, 0, newRV_inc((SV*)troc_cv)));
11f9f0ed 3155 if (rv2cv_op_cv(o, 0) != troc_cv) croak_fail();
d9088386 3156 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV) != (CV*)troc_gv)
11f9f0ed 3157 croak_fail();
d9088386 3158 o->op_private |= OPpENTERSUB_AMPER;
11f9f0ed
NC
3159 if (rv2cv_op_cv(o, 0)) croak_fail();
3160 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386 3161 o->op_private &= ~OPpENTERSUB_AMPER;
11f9f0ed
NC
3162 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
3163 if (rv2cv_op_cv(o, RV2CVOPCV_MARK_EARLY) != troc_cv) croak_fail();
3164 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
d9088386
Z
3165 op_free(o);
3166 o = newCVREF(0, newUNOP(OP_RAND, 0, newSVOP(OP_CONST, 0, newSViv(0))));
11f9f0ed
NC
3167 if (rv2cv_op_cv(o, 0)) croak_fail();
3168 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386 3169 o->op_private |= OPpENTERSUB_AMPER;
11f9f0ed
NC
3170 if (rv2cv_op_cv(o, 0)) croak_fail();
3171 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386 3172 o->op_private &= ~OPpENTERSUB_AMPER;
11f9f0ed
NC
3173 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
3174 if (rv2cv_op_cv(o, RV2CVOPCV_MARK_EARLY)) croak_fail();
3175 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
d9088386
Z
3176 op_free(o);
3177 o = newUNOP(OP_RAND, 0, newSVOP(OP_CONST, 0, newSViv(0)));
11f9f0ed
NC
3178 if (rv2cv_op_cv(o, 0)) croak_fail();
3179 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386
Z
3180 op_free(o);
3181
3182void
3183test_cv_getset_call_checker()
3184 PREINIT:
3185 CV *troc_cv, *tsh_cv;
3186 Perl_call_checker ckfun;
3187 SV *ckobj;
36791795 3188 U32 ckflags;
d9088386 3189 CODE:
36791795 3190#define check_cc(cv, xckfun, xckobj, xckflags) \
d9088386
Z
3191 do { \
3192 cv_get_call_checker((cv), &ckfun, &ckobj); \
36791795
Z
3193 if (ckfun != (xckfun)) croak_fail_nep(FPTR2DPTR(void *, ckfun), xckfun); \
3194 if (ckobj != (xckobj)) croak_fail_nep(FPTR2DPTR(void *, ckobj), xckobj); \
3195 cv_get_call_checker_flags((cv), CALL_CHECKER_REQUIRE_GV, &ckfun, &ckobj, &ckflags); \
3196 if (ckfun != (xckfun)) croak_fail_nep(FPTR2DPTR(void *, ckfun), xckfun); \
3197 if (ckobj != (xckobj)) croak_fail_nep(FPTR2DPTR(void *, ckobj), xckobj); \
3198 if (ckflags != CALL_CHECKER_REQUIRE_GV) croak_fail_nei(ckflags, CALL_CHECKER_REQUIRE_GV); \
3199 cv_get_call_checker_flags((cv), 0, &ckfun, &ckobj, &ckflags); \
3200 if (ckfun != (xckfun)) croak_fail_nep(FPTR2DPTR(void *, ckfun), xckfun); \
3201 if (ckobj != (xckobj)) croak_fail_nep(FPTR2DPTR(void *, ckobj), xckobj); \
3202 if (ckflags != (xckflags)) croak_fail_nei(ckflags, (xckflags)); \
d9088386
Z
3203 } while(0)
3204 troc_cv = get_cv("XS::APItest::test_rv2cv_op_cv", 0);
3205 tsh_cv = get_cv("XS::APItest::test_savehints", 0);
36791795
Z
3206 check_cc(troc_cv, Perl_ck_entersub_args_proto_or_list, (SV*)troc_cv, 0);
3207 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv, 0);
d9088386
Z
3208 cv_set_call_checker(tsh_cv, Perl_ck_entersub_args_proto_or_list,
3209 &PL_sv_yes);
36791795
Z
3210 check_cc(troc_cv, Perl_ck_entersub_args_proto_or_list, (SV*)troc_cv, 0);
3211 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, &PL_sv_yes, CALL_CHECKER_REQUIRE_GV);
d9088386 3212 cv_set_call_checker(troc_cv, THX_ck_entersub_args_scalars, &PL_sv_no);
36791795
Z
3213 check_cc(troc_cv, THX_ck_entersub_args_scalars, &PL_sv_no, CALL_CHECKER_REQUIRE_GV);
3214 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, &PL_sv_yes, CALL_CHECKER_REQUIRE_GV);
d9088386
Z
3215 cv_set_call_checker(tsh_cv, Perl_ck_entersub_args_proto_or_list,
3216 (SV*)tsh_cv);
36791795
Z
3217 check_cc(troc_cv, THX_ck_entersub_args_scalars, &PL_sv_no, CALL_CHECKER_REQUIRE_GV);
3218 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv, 0);
d9088386
Z
3219 cv_set_call_checker(troc_cv, Perl_ck_entersub_args_proto_or_list,
3220 (SV*)troc_cv);
36791795
Z
3221 check_cc(troc_cv, Perl_ck_entersub_args_proto_or_list, (SV*)troc_cv, 0);
3222 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv, 0);
11f9f0ed
NC
3223 if (SvMAGICAL((SV*)troc_cv) || SvMAGIC((SV*)troc_cv)) croak_fail();
3224 if (SvMAGICAL((SV*)tsh_cv) || SvMAGIC((SV*)tsh_cv)) croak_fail();
36791795
Z
3225 cv_set_call_checker_flags(tsh_cv, Perl_ck_entersub_args_proto_or_list,
3226 &PL_sv_yes, 0);
3227 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, &PL_sv_yes, 0);
3228 cv_set_call_checker_flags(tsh_cv, Perl_ck_entersub_args_proto_or_list,
3229 &PL_sv_yes, CALL_CHECKER_REQUIRE_GV);
3230 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, &PL_sv_yes, CALL_CHECKER_REQUIRE_GV);
3231 cv_set_call_checker_flags(tsh_cv, Perl_ck_entersub_args_proto_or_list,
3232 (SV*)tsh_cv, 0);
3233 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv, 0);
3234 if (SvMAGICAL((SV*)tsh_cv) || SvMAGIC((SV*)tsh_cv)) croak_fail();
3235 cv_set_call_checker_flags(tsh_cv, Perl_ck_entersub_args_proto_or_list,
3236 &PL_sv_yes, CALL_CHECKER_REQUIRE_GV);
3237 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, &PL_sv_yes, CALL_CHECKER_REQUIRE_GV);
3238 cv_set_call_checker_flags(tsh_cv, Perl_ck_entersub_args_proto_or_list,
3239 (SV*)tsh_cv, CALL_CHECKER_REQUIRE_GV);
3240 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv, 0);
3241 if (SvMAGICAL((SV*)tsh_cv) || SvMAGIC((SV*)tsh_cv)) croak_fail();
d9088386
Z
3242#undef check_cc
3243
3244void
3245cv_set_call_checker_lists(CV *cv)
3246 CODE:
3247 cv_set_call_checker(cv, THX_ck_entersub_args_lists, &PL_sv_undef);
3248
3249void
3250cv_set_call_checker_scalars(CV *cv)
3251 CODE:
3252 cv_set_call_checker(cv, THX_ck_entersub_args_scalars, &PL_sv_undef);
3253
3254void
3255cv_set_call_checker_proto(CV *cv, SV *proto)
3256 CODE:
3257 if (SvROK(proto))
3258 proto = SvRV(proto);
3259 cv_set_call_checker(cv, Perl_ck_entersub_args_proto, proto);
3260
3261void
3262cv_set_call_checker_proto_or_list(CV *cv, SV *proto)
3263 CODE:
3264 if (SvROK(proto))
3265 proto = SvRV(proto);
3266 cv_set_call_checker(cv, Perl_ck_entersub_args_proto_or_list, proto);
3267
3268void
3269cv_set_call_checker_multi_sum(CV *cv)
3270 CODE:
3271 cv_set_call_checker(cv, THX_ck_entersub_multi_sum, &PL_sv_undef);
3272
3273void
20439bc7
Z
3274test_cophh()
3275 PREINIT:
3276 COPHH *a, *b;
72900640
KW
3277#ifdef EBCDIC
3278 SV* key_sv;
3279 char * key_name;
3280 STRLEN key_len;
3281#endif
20439bc7
Z
3282 CODE:
3283#define check_ph(EXPR) \
3284 do { if((EXPR) != &PL_sv_placeholder) croak("fail"); } while(0)
3285#define check_iv(EXPR, EXPECT) \
3286 do { if(SvIV(EXPR) != (EXPECT)) croak("fail"); } while(0)
3287#define msvpvs(STR) sv_2mortal(newSVpvs(STR))
3288#define msviv(VALUE) sv_2mortal(newSViv(VALUE))
3289 a = cophh_new_empty();
3290 check_ph(cophh_fetch_pvn(a, "foo_1", 5, 0, 0));
3291 check_ph(cophh_fetch_pvs(a, "foo_1", 0));
3292 check_ph(cophh_fetch_pv(a, "foo_1", 0, 0));
3293 check_ph(cophh_fetch_sv(a, msvpvs("foo_1"), 0, 0));
3294 a = cophh_store_pvn(a, "foo_1abc", 5, 0, msviv(111), 0);
3295 a = cophh_store_pvs(a, "foo_2", msviv(222), 0);
3296 a = cophh_store_pv(a, "foo_3", 0, msviv(333), 0);
3297 a = cophh_store_sv(a, msvpvs("foo_4"), 0, msviv(444), 0);
3298 check_iv(cophh_fetch_pvn(a, "foo_1xyz", 5, 0, 0), 111);
3299 check_iv(cophh_fetch_pvs(a, "foo_1", 0), 111);
3300 check_iv(cophh_fetch_pv(a, "foo_1", 0, 0), 111);
3301 check_iv(cophh_fetch_sv(a, msvpvs("foo_1"), 0, 0), 111);
3302 check_iv(cophh_fetch_pvs(a, "foo_2", 0), 222);
3303 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
3304 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
3305 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
3306 b = cophh_copy(a);
3307 b = cophh_store_pvs(b, "foo_1", msviv(1111), 0);
3308 check_iv(cophh_fetch_pvs(a, "foo_1", 0), 111);
3309 check_iv(cophh_fetch_pvs(a, "foo_2", 0), 222);
3310 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
3311 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
3312 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
3313 check_iv(cophh_fetch_pvs(b, "foo_1", 0), 1111);
3314 check_iv(cophh_fetch_pvs(b, "foo_2", 0), 222);
3315 check_iv(cophh_fetch_pvs(b, "foo_3", 0), 333);
3316 check_iv(cophh_fetch_pvs(b, "foo_4", 0), 444);
3317 check_ph(cophh_fetch_pvs(b, "foo_5", 0));
3318 a = cophh_delete_pvn(a, "foo_1abc", 5, 0, 0);
3319 a = cophh_delete_pvs(a, "foo_2", 0);
3320 b = cophh_delete_pv(b, "foo_3", 0, 0);
3321 b = cophh_delete_sv(b, msvpvs("foo_4"), 0, 0);
3322 check_ph(cophh_fetch_pvs(a, "foo_1", 0));
3323 check_ph(cophh_fetch_pvs(a, "foo_2", 0));
3324 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
3325 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
3326 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
3327 check_iv(cophh_fetch_pvs(b, "foo_1", 0), 1111);
3328 check_iv(cophh_fetch_pvs(b, "foo_2", 0), 222);
3329 check_ph(cophh_fetch_pvs(b, "foo_3", 0));
3330 check_ph(cophh_fetch_pvs(b, "foo_4", 0));
3331 check_ph(cophh_fetch_pvs(b, "foo_5", 0));
3332 b = cophh_delete_pvs(b, "foo_3", 0);
3333 b = cophh_delete_pvs(b, "foo_5", 0);
3334 check_iv(cophh_fetch_pvs(b, "foo_1", 0), 1111);
3335 check_iv(cophh_fetch_pvs(b, "foo_2", 0), 222);
3336 check_ph(cophh_fetch_pvs(b, "foo_3", 0));
3337 check_ph(cophh_fetch_pvs(b, "foo_4", 0));
3338 check_ph(cophh_fetch_pvs(b, "foo_5", 0));
3339 cophh_free(b);
3340 check_ph(cophh_fetch_pvs(a, "foo_1", 0));
3341 check_ph(cophh_fetch_pvs(a, "foo_2", 0));
3342 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
3343 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
3344 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
72900640 3345 a = cophh_store_pvs(a, "foo_1", msviv(11111), COPHH_KEY_UTF8);
20439bc7 3346 a = cophh_store_pvs(a, "foo_\xaa", msviv(123), 0);
72900640 3347#ifndef EBCDIC
20439bc7 3348 a = cophh_store_pvs(a, "foo_\xc2\xbb", msviv(456), COPHH_KEY_UTF8);
72900640
KW
3349#else
3350 /* On EBCDIC, we need to translate the UTF-8 in the ASCII test to the
3351 * equivalent UTF-EBCDIC for the code page. This is done at runtime
3352 * (with the helper function in this file). Therefore we can't use
3353 * cophhh_store_pvs(), as we don't have literal string */
3354 key_sv = sv_2mortal(newSVpvs("foo_"));
3355 cat_utf8a2n(key_sv, STR_WITH_LEN("\xc2\xbb"));
3356 key_name = SvPV(key_sv, key_len);
3357 a = cophh_store_pvn(a, key_name, key_len, 0, msviv(456), COPHH_KEY_UTF8);
3358#endif
3359#ifndef EBCDIC
20439bc7 3360 a = cophh_store_pvs(a, "foo_\xc3\x8c", msviv(789), COPHH_KEY_UTF8);
72900640
KW
3361#else
3362 sv_setpvs(key_sv, "foo_");
3363 cat_utf8a2n(key_sv, STR_WITH_LEN("\xc3\x8c"));
3364 key_name = SvPV(key_sv, key_len);
3365 a = cophh_store_pvn(a, key_name, key_len, 0, msviv(789), COPHH_KEY_UTF8);
3366#endif
3367#ifndef EBCDIC
20439bc7 3368 a = cophh_store_pvs(a, "foo_\xd9\xa6", msviv(666), COPHH_KEY_UTF8);
72900640
KW
3369#else
3370 sv_setpvs(key_sv, "foo_");
3371 cat_utf8a2n(key_sv, STR_WITH_LEN("\xd9\xa6"));
3372 key_name = SvPV(key_sv, key_len);
3373 a = cophh_store_pvn(a, key_name, key_len, 0, msviv(666), COPHH_KEY_UTF8);
3374#endif
20439bc7
Z
3375 check_iv(cophh_fetch_pvs(a, "foo_1", 0), 11111);
3376 check_iv(cophh_fetch_pvs(a, "foo_1", COPHH_KEY_UTF8), 11111);
3377 check_iv(cophh_fetch_pvs(a, "foo_\xaa", 0), 123);
72900640 3378#ifndef EBCDIC
20439bc7
Z
3379 check_iv(cophh_fetch_pvs(a, "foo_\xc2\xaa", COPHH_KEY_UTF8), 123);
3380 check_ph(cophh_fetch_pvs(a, "foo_\xc2\xaa", 0));
72900640
KW
3381#else
3382 sv_setpvs(key_sv, "foo_");
3383 cat_utf8a2n(key_sv, STR_WITH_LEN("\xc2\xaa"));
3384 key_name = SvPV(key_sv, key_len);
3385 check_iv(cophh_fetch_pvn(a, key_name, key_len, 0, COPHH_KEY_UTF8), 123);
3386 check_ph(cophh_fetch_pvn(a, key_name, key_len, 0, 0));
3387#endif
20439bc7 3388 check_iv(cophh_fetch_pvs(a, "foo_\xbb", 0), 456);
72900640 3389#ifndef EBCDIC
20439bc7
Z
3390 check_iv(cophh_fetch_pvs(a, "foo_\xc2\xbb", COPHH_KEY_UTF8), 456);
3391 check_ph(cophh_fetch_pvs(a, "foo_\xc2\xbb", 0));
72900640
KW
3392#else
3393 sv_setpvs(key_sv, "foo_");
3394 cat_utf8a2n(key_sv, STR_WITH_LEN("\xc2\xbb"));
3395 key_name = SvPV(key_sv, key_len);
3396 check_iv(cophh_fetch_pvn(a, key_name, key_len, 0, COPHH_KEY_UTF8), 456);
3397 check_ph(cophh_fetch_pvn(a, key_name, key_len, 0, 0));
3398#endif
20439bc7 3399 check_iv(cophh_fetch_pvs(a, "foo_\xcc", 0), 789);
72900640 3400#ifndef EBCDIC
20439bc7
Z
3401 check_iv(cophh_fetch_pvs(a, "foo_\xc3\x8c", COPHH_KEY_UTF8), 789);
3402 check_ph(cophh_fetch_pvs(a, "foo_\xc2\x8c", 0));
72900640
KW
3403#else
3404 sv_setpvs(key_sv, "foo_");
3405 cat_utf8a2n(key_sv, STR_WITH_LEN("\xc3\x8c"));
3406 key_name = SvPV(key_sv, key_len);
3407 check_iv(cophh_fetch_pvn(a, key_name, key_len, 0, COPHH_KEY_UTF8), 789);
3408 check_ph(cophh_fetch_pvn(a, key_name, key_len, 0, 0));
3409#endif
3410#ifndef EBCDIC
20439bc7
Z
3411 check_iv(cophh_fetch_pvs(a, "foo_\xd9\xa6", COPHH_KEY_UTF8), 666);
3412 check_ph(cophh_fetch_pvs(a, "foo_\xd9\xa6", 0));
72900640
KW
3413#else
3414 sv_setpvs(key_sv, "foo_");
3415 cat_utf8a2n(key_sv, STR_WITH_LEN("\xd9\xa6"));
3416 key_name = SvPV(key_sv, key_len);
3417 check_iv(cophh_fetch_pvn(a, key_name, key_len, 0, COPHH_KEY_UTF8), 666);
3418 check_ph(cophh_fetch_pvn(a, key_name, key_len, 0, 0));
3419#endif
3987a177
Z
3420 ENTER;
3421 SAVEFREECOPHH(a);
3422 LEAVE;
20439bc7
Z
3423#undef check_ph
3424#undef check_iv
3425#undef msvpvs
3426#undef msviv
3427
8375c93e
RU
3428void
3429test_coplabel()
3430 PREINIT:
3431 COP *cop;
32708f0b
CB
3432 const char *label;
3433 STRLEN len;
3434 U32 utf8;
8375c93e
RU
3435 CODE:
3436 cop = &PL_compiling;
32708f0b
CB
3437 Perl_cop_store_label(aTHX_ cop, "foo", 3, 0);
3438 label = Perl_cop_fetch_label(aTHX_ cop, &len, &utf8);
083b2a61 3439 if (strNE(label,"foo")) croak("fail # cop_fetch_label label");
32708f0b
CB
3440 if (len != 3) croak("fail # cop_fetch_label len");
3441 if (utf8) croak("fail # cop_fetch_label utf8");
8375c93e 3442 /* SMALL GERMAN UMLAUT A */
a1cd495a 3443 Perl_cop_store_label(aTHX_ cop, "fo\xc3\xa4", 4, SVf_UTF8);
32708f0b 3444 label = Perl_cop_fetch_label(aTHX_ cop, &len, &utf8);
083b2a61 3445 if (strNE(label,"fo\xc3\xa4")) croak("fail # cop_fetch_label label");
72024711 3446 if (len != 4) croak("fail # cop_fetch_label len");
32708f0b 3447 if (!utf8) croak("fail # cop_fetch_label utf8");
8375c93e
RU
3448
3449
20439bc7
Z
3450HV *
3451example_cophh_2hv()
3452 PREINIT:
3453 COPHH *a;
72900640
KW
3454#ifdef EBCDIC
3455 SV* key_sv;
3456 char * key_name;
3457 STRLEN key_len;
3458#endif
20439bc7
Z
3459 CODE:
3460#define msviv(VALUE) sv_2mortal(newSViv(VALUE))
3461 a = cophh_new_empty();
3462 a = cophh_store_pvs(a, "foo_0", msviv(999), 0);
3463 a = cophh_store_pvs(a, "foo_1", msviv(111), 0);
3464 a = cophh_store_pvs(a, "foo_\xaa", msviv(123), 0);
72900640 3465#ifndef EBCDIC
20439bc7 3466 a = cophh_store_pvs(a, "foo_\xc2\xbb", msviv(456), COPHH_KEY_UTF8);
72900640
KW
3467#else
3468 key_sv = sv_2mortal(newSVpvs("foo_"));
3469 cat_utf8a2n(key_sv, STR_WITH_LEN("\xc2\xbb"));
3470 key_name = SvPV(key_sv, key_len);
3471 a = cophh_store_pvn(a, key_name, key_len, 0, msviv(456), COPHH_KEY_UTF8);
3472#endif
3473#ifndef EBCDIC
20439bc7 3474 a = cophh_store_pvs(a, "foo_\xc3\x8c", msviv(789), COPHH_KEY_UTF8);
72900640
KW
3475#else
3476 sv_setpvs(key_sv, "foo_");
3477 cat_utf8a2n(key_sv, STR_WITH_LEN("\xc3\x8c"));
3478 key_name = SvPV(key_sv, key_len);
3479 a = cophh_store_pvn(a, key_name, key_len, 0, msviv(789), COPHH_KEY_UTF8);
3480#endif
3481#ifndef EBCDIC
20439bc7 3482 a = cophh_store_pvs(a, "foo_\xd9\xa6", msviv(666), COPHH_KEY_UTF8);
72900640
KW
3483#else
3484 sv_setpvs(key_sv, "foo_");
3485 cat_utf8a2n(key_sv, STR_WITH_LEN("\xd9\xa6"));
3486 key_name = SvPV(key_sv, key_len);
3487 a = cophh_store_pvn(a, key_name, key_len, 0, msviv(666), COPHH_KEY_UTF8);
3488#endif
20439bc7
Z
3489 a = cophh_delete_pvs(a, "foo_0", 0);
3490 a = cophh_delete_pvs(a, "foo_2", 0);
3491 RETVAL = cophh_2hv(a, 0);
3492 cophh_free(a);
3493#undef msviv
3494 OUTPUT:
3495 RETVAL
3496
3497void
defdfed5
Z
3498test_savehints()
3499 PREINIT:
3500 SV **svp, *sv;
3501 CODE:
3502#define store_hint(KEY, VALUE) \
3503 sv_setiv_mg(*hv_fetchs(GvHV(PL_hintgv), KEY, 1), (VALUE))
3504#define hint_ok(KEY, EXPECT) \
3505 ((svp = hv_fetchs(GvHV(PL_hintgv), KEY, 0)) && \
3506 (sv = *svp) && SvIV(sv) == (EXPECT) && \
20439bc7 3507 (sv = cop_hints_fetch_pvs(&PL_compiling, KEY, 0)) && \
defdfed5
Z
3508 SvIV(sv) == (EXPECT))
3509#define check_hint(KEY, EXPECT) \
11f9f0ed 3510 do { if (!hint_ok(KEY, EXPECT)) croak_fail(); } while(0)
defdfed5
Z
3511 PL_hints |= HINT_LOCALIZE_HH;
3512 ENTER;
3513 SAVEHINTS();
3514 PL_hints &= HINT_INTEGER;
3515 store_hint("t0", 123);
3516 store_hint("t1", 456);
11f9f0ed 3517 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
3518 check_hint("t0", 123); check_hint("t1", 456);
3519 ENTER;
3520 SAVEHINTS();
11f9f0ed 3521 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
3522 check_hint("t0", 123); check_hint("t1", 456);
3523 PL_hints |= HINT_INTEGER;
3524 store_hint("t0", 321);
11f9f0ed 3525 if (!(PL_hints & HINT_INTEGER)) croak_fail();
defdfed5
Z
3526 check_hint("t0", 321); check_hint("t1", 456);
3527 LEAVE;
11f9f0ed 3528 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
3529 check_hint("t0", 123); check_hint("t1", 456);
3530 ENTER;
3531 SAVEHINTS();
11f9f0ed 3532 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
3533 check_hint("t0", 123); check_hint("t1", 456);
3534 store_hint("t1", 654);
11f9f0ed 3535 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
3536 check_hint("t0", 123); check_hint("t1", 654);
3537 LEAVE;
11f9f0ed 3538 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
3539 check_hint("t0", 123); check_hint("t1", 456);
3540 LEAVE;
3541#undef store_hint
3542#undef hint_ok
3543#undef check_hint
3544
3545void
3546test_copyhints()
3547 PREINIT:
3548 HV *a, *b;
3549 CODE:
3550 PL_hints |= HINT_LOCALIZE_HH;
3551 ENTER;
3552 SAVEHINTS();
3553 sv_setiv_mg(*hv_fetchs(GvHV(PL_hintgv), "t0", 1), 123);
20439bc7
Z
3554 if (SvIV(cop_hints_fetch_pvs(&PL_compiling, "t0", 0)) != 123)
3555 croak_fail();
defdfed5
Z
3556 a = newHVhv(GvHV(PL_hintgv));
3557 sv_2mortal((SV*)a);
3558 sv_setiv_mg(*hv_fetchs(a, "t0", 1), 456);
20439bc7
Z
3559 if (SvIV(cop_hints_fetch_pvs(&PL_compiling, "t0", 0)) != 123)
3560 croak_fail();
defdfed5
Z
3561 b = hv_copy_hints_hv(a);
3562 sv_2mortal((SV*)b);
3563 sv_setiv_mg(*hv_fetchs(b, "t0", 1), 789);
20439bc7
Z
3564 if (SvIV(cop_hints_fetch_pvs(&PL_compiling, "t0", 0)) != 789)
3565 croak_fail();
defdfed5
Z
3566 LEAVE;
3567
201c7e1f 3568void
2fcb4757
Z
3569test_op_list()
3570 PREINIT:
3571 OP *a;
3572 CODE:
3573#define iv_op(iv) newSVOP(OP_CONST, 0, newSViv(iv))
3574#define check_op(o, expect) \
3575 do { \
083b2a61 3576 if (strNE(test_op_list_describe(o), (expect))) \
2e66fe90 3577 croak("fail %s %s", test_op_list_describe(o), (expect)); \
2fcb4757
Z
3578 } while(0)
3579 a = op_append_elem(OP_LIST, NULL, NULL);
3580 check_op(a, "");
3581 a = op_append_elem(OP_LIST, iv_op(1), a);
3582 check_op(a, "const(1).");
3583 a = op_append_elem(OP_LIST, NULL, a);
3584 check_op(a, "const(1).");
3585 a = op_append_elem(OP_LIST, a, iv_op(2));
3586 check_op(a, "list[pushmark.const(1).const(2).]");
3587 a = op_append_elem(OP_LIST, a, iv_op(3));
3588 check_op(a, "list[pushmark.const(1).const(2).const(3).]");
3589 a = op_append_elem(OP_LIST, a, NULL);
3590 check_op(a, "list[pushmark.const(1).const(2).const(3).]");
3591 a = op_append_elem(OP_LIST, NULL, a);
3592 check_op(a, "list[pushmark.const(1).const(2).const(3).]");
3593 a = op_append_elem(OP_LIST, iv_op(4), a);
3594 check_op(a, "list[pushmark.const(4)."
3595 "list[pushmark.const(1).const(2).const(3).]]");
3596 a = op_append_elem(OP_LIST, a, iv_op(5));
3597 check_op(a, "list[pushmark.const(4)."
3598 "list[pushmark.const(1).const(2).const(3).]const(5).]");
3599 a = op_append_elem(OP_LIST, a,
3600 op_append_elem(OP_LIST, iv_op(7), iv_op(6)));
3601 check_op(a, "list[pushmark.const(4)."
3602 "list[pushmark.const(1).const(2).const(3).]const(5)."
3603 "list[pushmark.const(7).const(6).]]");
3604 op_free(a);
3605 a = op_append_elem(OP_LINESEQ, iv_op(1), iv_op(2));
3606 check_op(a, "lineseq[const(1).const(2).]");
3607 a = op_append_elem(OP_LINESEQ, a, iv_op(3));
3608 check_op(a, "lineseq[const(1).const(2).const(3).]");
3609 op_free(a);
3610 a = op_append_elem(OP_LINESEQ,
3611 op_append_elem(OP_LIST, iv_op(1), iv_op(2)),
3612 iv_op(3));
3613 check_op(a, "lineseq[list[pushmark.const(1).const(2).]const(3).]");
3614 op_free(a);
3615 a = op_prepend_elem(OP_LIST, NULL, NULL);
3616 check_op(a, "");
3617 a = op_prepend_elem(OP_LIST, a, iv_op(1));
3618 check_op(a, "const(1).");
3619 a = op_prepend_elem(OP_LIST, a, NULL);
3620 check_op(a, "const(1).");
3621 a = op_prepend_elem(OP_LIST, iv_op(2), a);
3622 check_op(a, "list[pushmark.const(2).const(1).]");
3623 a = op_prepend_elem(OP_LIST, iv_op(3), a);
3624 check_op(a, "list[pushmark.const(3).const(2).const(1).]");
3625 a = op_prepend_elem(OP_LIST, NULL, a);
3626 check_op(a, "list[pushmark.const(3).const(2).const(1).]");
3627 a = op_prepend_elem(OP_LIST, a, NULL);
3628 check_op(a, "list[pushmark.const(3).const(2).const(1).]");
3629 a = op_prepend_elem(OP_LIST, a, iv_op(4));
3630 check_op(a, "list[pushmark."
3631 "list[pushmark.const(3).const(2).const(1).]const(4).]");
3632 a = op_prepend_elem(OP_LIST, iv_op(5), a);
3633 check_op(a, "list[pushmark.const(5)."
3634 "list[pushmark.const(3).const(2).const(1).]const(4).]");
3635 a = op_prepend_elem(OP_LIST,
3636 op_prepend_elem(OP_LIST, iv_op(6), iv_op(7)), a);
3637 check_op(a, "list[pushmark.list[pushmark.const(6).const(7).]const(5)."
3638 "list[pushmark.const(3).const(2).const(1).]const(4).]");
3639 op_free(a);
3640 a = op_prepend_elem(OP_LINESEQ, iv_op(2), iv_op(1));
3641 check_op(a, "lineseq[const(2).const(1).]");
3642 a = op_prepend_elem(OP_LINESEQ, iv_op(3), a);
3643 check_op(a, "lineseq[const(3).const(2).const(1).]");
3644 op_free(a);
3645 a = op_prepend_elem(OP_LINESEQ, iv_op(3),
3646 op_prepend_elem(OP_LIST, iv_op(2), iv_op(1)));
3647 check_op(a, "lineseq[const(3).list[pushmark.const(2).const(1).]]");
3648 op_free(a);
3649 a = op_append_list(OP_LINESEQ, NULL, NULL);
3650 check_op(a, "");
3651 a = op_append_list(OP_LINESEQ, iv_op(1), a);
3652 check_op(a, "const(1).");
3653 a = op_append_list(OP_LINESEQ, NULL, a);
3654 check_op(a, "const(1).");
3655 a = op_append_list(OP_LINESEQ, a, iv_op(2));
3656 check_op(a, "lineseq[const(1).const(2).]");
3657 a = op_append_list(OP_LINESEQ, a, iv_op(3));
3658 check_op(a, "lineseq[const(1).const(2).const(3).]");
3659 a = op_append_list(OP_LINESEQ, iv_op(4), a);
3660 check_op(a, "lineseq[const(4).const(1).const(2).const(3).]");
3661 a = op_append_list(OP_LINESEQ, a, NULL);
3662 check_op(a, "lineseq[const(4).const(1).const(2).const(3).]");
3663 a = op_append_list(OP_LINESEQ, NULL, a);
3664 check_op(a, "lineseq[const(4).const(1).const(2).const(3).]");
3665 a = op_append_list(OP_LINESEQ, a,
3666 op_append_list(OP_LINESEQ, iv_op(5), iv_op(6)));
3667 check_op(a, "lineseq[const(4).const(1).const(2).const(3)."
3668 "const(5).const(6).]");
3669 op_free(a);
3670 a = op_append_list(OP_LINESEQ,
3671 op_append_list(OP_LINESEQ, iv_op(1), iv_op(2)),
3672 op_append_list(OP_LIST, iv_op(3), iv_op(4)));
3673 check_op(a, "lineseq[const(1).const(2)."
3674 "list[pushmark.const(3).const(4).]]");
3675 op_free(a);
3676 a = op_append_list(OP_LINESEQ,
3677 op_append_list(OP_LIST, iv_op(1), iv_op(2)),
3678 op_append_list(OP_LINESEQ, iv_op(3), iv_op(4)));
3679 check_op(a, "lineseq[list[pushmark.const(1).const(2).]"
3680 "const(3).const(4).]");
3681 op_free(a);
2fcb4757
Z
3682#undef check_op
3683
3684void
5983a79d
BM
3685test_op_linklist ()
3686 PREINIT:
3687 OP *o;
3688 CODE:
3689#define check_ll(o, expect) \
3690 STMT_START { \
2e66fe90
FC
3691 if (strNE(test_op_linklist_describe(o), (expect))) \
3692 croak("fail %s %s", test_op_linklist_describe(o), (expect)); \
5983a79d
BM
3693 } STMT_END
3694 o = iv_op(1);
3695 check_ll(o, ".const1");
3696 op_free(o);
3697
3698 o = mkUNOP(OP_NOT, iv_op(1));
3699 check_ll(o, ".const1.not");
3700 op_free(o);
3701
3702 o = mkUNOP(OP_NOT, mkUNOP(OP_NEGATE, iv_op(1)));
3703 check_ll(o, ".const1.negate.not");
3704 op_free(o);
3705
3706 o = mkBINOP(OP_ADD, iv_op(1), iv_op(2));
3707 check_ll(o, ".const1.const2.add");
3708 op_free(o);
3709
3710 o = mkBINOP(OP_ADD, mkUNOP(OP_NOT, iv_op(1)), iv_op(2));
3711 check_ll(o, ".const1.not.const2.add");
3712 op_free(o);
3713
3714 o = mkUNOP(OP_NOT, mkBINOP(OP_ADD, iv_op(1), iv_op(2)));
3715 check_ll(o, ".const1.const2.add.not");
3716 op_free(o);
3717
3718 o = mkLISTOP(OP_LINESEQ, iv_op(1), iv_op(2), iv_op(3));
3719 check_ll(o, ".const1.const2.const3.lineseq");
3720 op_free(o);
3721
3722 o = mkLISTOP(OP_LINESEQ,
3723 mkBINOP(OP_ADD, iv_op(1), iv_op(2)),
3724 mkUNOP(OP_NOT, iv_op(3)),
3725 mkLISTOP(OP_SUBSTR, iv_op(4), iv_op(5), iv_op(6)));
3726 check_ll(o, ".const1.const2.add.const3.not"
3727 ".const4.const5.const6.substr.lineseq");
3728 op_free(o);
3729
3730 o = mkBINOP(OP_ADD, iv_op(1), iv_op(2));
3731 LINKLIST(o);
3732 o = mkBINOP(OP_SUBTRACT, o, iv_op(3));
3733 check_ll(o, ".const1.const2.add.const3.subtract");
3734 op_free(o);
3735#undef check_ll
3736#undef iv_op
3737
3738void
201c7e1f
FR
3739peep_enable ()
3740 PREINIT:
3741 dMY_CXT;
3742 CODE:
3743 av_clear(MY_CXT.peep_recorder);
3744 av_clear(MY_CXT.rpeep_recorder);
3745 MY_CXT.peep_recording = 1;
3746
3747void
3748peep_disable ()
3749 PREINIT:
3750 dMY_CXT;
3751 CODE:
3752 MY_CXT.peep_recording = 0;
3753
3754SV *
3755peep_record ()
3756 PREINIT:
3757 dMY_CXT;
3758 CODE:
95d2461a 3759 RETVAL = newRV_inc((SV *)MY_CXT.peep_recorder);
201c7e1f
FR
3760 OUTPUT:
3761 RETVAL
3762
3763SV *
3764rpeep_record ()
3765 PREINIT:
3766 dMY_CXT;
3767 CODE:
95d2461a 3768 RETVAL = newRV_inc((SV *)MY_CXT.rpeep_recorder);
201c7e1f
FR
3769 OUTPUT:
3770 RETVAL
3771
9c540340
DM
3772=pod
3773
3774multicall_each: call a sub for each item in the list. Used to test MULTICALL
3775
3776=cut
3777
3778void
3779multicall_each(block,...)
3780 SV * block
3781PROTOTYPE: &@
3782CODE:
3783{
3784 dMULTICALL;
3785 int index;
3786 GV *gv;
3787 HV *stash;
3788 I32 gimme = G_SCALAR;
3789 SV **args = &PL_stack_base[ax];
3790 CV *cv;
3791
3792 if(items <= 1) {
3793 XSRETURN_UNDEF;
3794 }
3795 cv = sv_2cv(block, &stash, &gv, 0);
3796 if (cv == Nullcv) {
3797 croak("multicall_each: not a subroutine reference");
3798 }
3799 PUSH_MULTICALL(cv);
3800 SAVESPTR(GvSV(PL_defgv));
3801
3802 for(index = 1 ; index < items ; index++) {
3803 GvSV(PL_defgv) = args[index];
3804 MULTICALL;
3805 }
3806 POP_MULTICALL;
3807 XSRETURN_UNDEF;
3808}
3809
1f0ba93b
DM
3810=pod
3811
3812multicall_return(): call the passed sub once in the specificed context
3813and return whatever it returns
3814
3815=cut
3816
3817void
3818multicall_return(block, context)
3819 SV *block
3820 I32 context
3821PROTOTYPE: &$
3822CODE:
3823{
3824 dSP;
3825 dMULTICALL;
3826 GV *gv;
3827 HV *stash;
3828 I32 gimme = context;
3829 CV *cv;
3830 AV *av;
3831 SV **p;
052a7c76 3832 SSize_t i, size;
1f0ba93b
DM
3833
3834 cv = sv_2cv(block, &stash, &gv, 0);
3835 if (cv == Nullcv) {
3836 croak("multicall_return not a subroutine reference");
3837 }