This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
put signature ops in their own subtree.
[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;
996b0cb8 762static SV *hintkey_subsignature_sv;
ce409cc8 763static SV *hintkey_DEFSV_sv;
25f5d540 764static SV *hintkey_with_vars_sv;
03d05f6e 765static SV *hintkey_join_with_space_sv;
83f8bb56
Z
766static int (*next_keyword_plugin)(pTHX_ char *, STRLEN, OP **);
767
768/* low-level parser helpers */
769
770#define PL_bufptr (PL_parser->bufptr)
771#define PL_bufend (PL_parser->bufend)
772
773/* RPN parser */
774
775#define parse_var() THX_parse_var(aTHX)
776static OP *THX_parse_var(pTHX)
777{
778 char *s = PL_bufptr;
779 char *start = s;
780 PADOFFSET varpos;
781 OP *padop;
782 if(*s != '$') croak("RPN syntax error");
783 while(1) {
784 char c = *++s;
785 if(!isALNUM(c)) break;
786 }
787 if(s-start < 2) croak("RPN syntax error");
788 lex_read_to(s);
cc76b5cc 789 varpos = pad_findmy_pvn(start, s-start, 0);
83f8bb56
Z
790 if(varpos == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(varpos))
791 croak("RPN only supports \"my\" variables");
792 padop = newOP(OP_PADSV, 0);
793 padop->op_targ = varpos;
794 return padop;
795}
796
797#define push_rpn_item(o) \
3253bf85
DM
798 op_sibling_splice(parent, NULL, 0, o);
799#define pop_rpn_item() ( \
800 (tmpop = op_sibling_splice(parent, NULL, 1, NULL)) \
801 ? tmpop : (croak("RPN stack underflow"), (OP*)NULL))
83f8bb56
Z
802
803#define parse_rpn_expr() THX_parse_rpn_expr(aTHX)
804static OP *THX_parse_rpn_expr(pTHX)
805{
3253bf85
DM
806 OP *tmpop;
807 /* fake parent for splice to mess with */
808 OP *parent = mkBINOP(OP_NULL, NULL, NULL);
809
83f8bb56
Z
810 while(1) {
811 I32 c;
812 lex_read_space(0);
813 c = lex_peek_unichar(0);
814 switch(c) {
815 case /*(*/')': case /*{*/'}': {
816 OP *result = pop_rpn_item();
3253bf85
DM
817 if(cLISTOPx(parent)->op_first)
818 croak("RPN expression must return a single value");
819 op_free(parent);
83f8bb56
Z
820 return result;
821 } break;
822 case '0': case '1': case '2': case '3': case '4':
823 case '5': case '6': case '7': case '8': case '9': {
824 UV val = 0;
825 do {
826 lex_read_unichar(0);
827 val = 10*val + (c - '0');
828 c = lex_peek_unichar(0);
829 } while(c >= '0' && c <= '9');
830 push_rpn_item(newSVOP(OP_CONST, 0, newSVuv(val)));
831 } break;
832 case '$': {
833 push_rpn_item(parse_var());
834 } break;
835 case '+': {
836 OP *b = pop_rpn_item();
837 OP *a = pop_rpn_item();
838 lex_read_unichar(0);
839 push_rpn_item(newBINOP(OP_I_ADD, 0, a, b));
840 } break;
841 case '-': {
842 OP *b = pop_rpn_item();
843 OP *a = pop_rpn_item();
844 lex_read_unichar(0);
845 push_rpn_item(newBINOP(OP_I_SUBTRACT, 0, a, b));
846 } break;
847 case '*': {
848 OP *b = pop_rpn_item();
849 OP *a = pop_rpn_item();
850 lex_read_unichar(0);
851 push_rpn_item(newBINOP(OP_I_MULTIPLY, 0, a, b));
852 } break;
853 case '/': {
854 OP *b = pop_rpn_item();
855 OP *a = pop_rpn_item();
856 lex_read_unichar(0);
857 push_rpn_item(newBINOP(OP_I_DIVIDE, 0, a, b));
858 } break;
859 case '%': {
860 OP *b = pop_rpn_item();
861 OP *a = pop_rpn_item();
862 lex_read_unichar(0);
863 push_rpn_item(newBINOP(OP_I_MODULO, 0, a, b));
864 } break;
865 default: {
866 croak("RPN syntax error");
867 } break;
868 }
869 }
870}
871
872#define parse_keyword_rpn() THX_parse_keyword_rpn(aTHX)
873static OP *THX_parse_keyword_rpn(pTHX)
874{
875 OP *op;
876 lex_read_space(0);
877 if(lex_peek_unichar(0) != '('/*)*/)
878 croak("RPN expression must be parenthesised");
879 lex_read_unichar(0);
880 op = parse_rpn_expr();
881 if(lex_peek_unichar(0) != /*(*/')')
882 croak("RPN expression must be parenthesised");
883 lex_read_unichar(0);
884 return op;
885}
886
887#define parse_keyword_calcrpn() THX_parse_keyword_calcrpn(aTHX)
888static OP *THX_parse_keyword_calcrpn(pTHX)
889{
890 OP *varop, *exprop;
891 lex_read_space(0);
892 varop = parse_var();
893 lex_read_space(0);
894 if(lex_peek_unichar(0) != '{'/*}*/)
895 croak("RPN expression must be braced");
896 lex_read_unichar(0);
897 exprop = parse_rpn_expr();
898 if(lex_peek_unichar(0) != /*{*/'}')
899 croak("RPN expression must be braced");
900 lex_read_unichar(0);
901 return newASSIGNOP(OPf_STACKED, varop, 0, exprop);
902}
903
904#define parse_keyword_stufftest() THX_parse_keyword_stufftest(aTHX)
905static OP *THX_parse_keyword_stufftest(pTHX)
906{
907 I32 c;
908 bool do_stuff;
909 lex_read_space(0);
910 do_stuff = lex_peek_unichar(0) == '+';
911 if(do_stuff) {
912 lex_read_unichar(0);
913 lex_read_space(0);
914 }
915 c = lex_peek_unichar(0);
916 if(c == ';') {
917 lex_read_unichar(0);
918 } else if(c != /*{*/'}') {
919 croak("syntax error");
920 }
921 if(do_stuff) lex_stuff_pvs(" ", 0);
922 return newOP(OP_NULL, 0);
923}
924
925#define parse_keyword_swaptwostmts() THX_parse_keyword_swaptwostmts(aTHX)
926static OP *THX_parse_keyword_swaptwostmts(pTHX)
927{
928 OP *a, *b;
929 a = parse_fullstmt(0);
930 b = parse_fullstmt(0);
931 if(a && b)
932 PL_hints |= HINT_BLOCK_SCOPE;
2fcb4757 933 return op_append_list(OP_LINESEQ, b, a);
83f8bb56
Z
934}
935
07ffcb73
Z
936#define parse_keyword_looprest() THX_parse_keyword_looprest(aTHX)
937static OP *THX_parse_keyword_looprest(pTHX)
938{
94bf0465
Z
939 return newWHILEOP(0, 1, NULL, newSVOP(OP_CONST, 0, &PL_sv_yes),
940 parse_stmtseq(0), NULL, 1);
07ffcb73
Z
941}
942
a7aaec61
Z
943#define parse_keyword_scopelessblock() THX_parse_keyword_scopelessblock(aTHX)
944static OP *THX_parse_keyword_scopelessblock(pTHX)
945{
946 I32 c;
947 OP *body;
948 lex_read_space(0);
949 if(lex_peek_unichar(0) != '{'/*}*/) croak("syntax error");
950 lex_read_unichar(0);
951 body = parse_stmtseq(0);
952 c = lex_peek_unichar(0);
953 if(c != /*{*/'}' && c != /*[*/']' && c != /*(*/')') croak("syntax error");
954 lex_read_unichar(0);
955 return body;
956}
957
9eb5c532
Z
958#define parse_keyword_stmtasexpr() THX_parse_keyword_stmtasexpr(aTHX)
959static OP *THX_parse_keyword_stmtasexpr(pTHX)
960{
8359b381 961 OP *o = parse_barestmt(0);
3ad73efd
Z
962 if (!o) o = newOP(OP_STUB, 0);
963 if (PL_hints & HINT_BLOCK_SCOPE) o->op_flags |= OPf_PARENS;
964 return op_scope(o);
9eb5c532
Z
965}
966
967#define parse_keyword_stmtsasexpr() THX_parse_keyword_stmtsasexpr(aTHX)
968static OP *THX_parse_keyword_stmtsasexpr(pTHX)
969{
970 OP *o;
971 lex_read_space(0);
972 if(lex_peek_unichar(0) != '{'/*}*/) croak("syntax error");
973 lex_read_unichar(0);
974 o = parse_stmtseq(0);
975 lex_read_space(0);
976 if(lex_peek_unichar(0) != /*{*/'}') croak("syntax error");
977 lex_read_unichar(0);
3ad73efd
Z
978 if (!o) o = newOP(OP_STUB, 0);
979 if (PL_hints & HINT_BLOCK_SCOPE) o->op_flags |= OPf_PARENS;
980 return op_scope(o);
9eb5c532
Z
981}
982
e53d8f76
Z
983#define parse_keyword_loopblock() THX_parse_keyword_loopblock(aTHX)
984static OP *THX_parse_keyword_loopblock(pTHX)
985{
94bf0465
Z
986 return newWHILEOP(0, 1, NULL, newSVOP(OP_CONST, 0, &PL_sv_yes),
987 parse_block(0), NULL, 1);
e53d8f76
Z
988}
989
990#define parse_keyword_blockasexpr() THX_parse_keyword_blockasexpr(aTHX)
991static OP *THX_parse_keyword_blockasexpr(pTHX)
992{
993 OP *o = parse_block(0);
3ad73efd
Z
994 if (!o) o = newOP(OP_STUB, 0);
995 if (PL_hints & HINT_BLOCK_SCOPE) o->op_flags |= OPf_PARENS;
996 return op_scope(o);
e53d8f76
Z
997}
998
361d9b55
Z
999#define parse_keyword_swaplabel() THX_parse_keyword_swaplabel(aTHX)
1000static OP *THX_parse_keyword_swaplabel(pTHX)
1001{
1002 OP *sop = parse_barestmt(0);
1003 SV *label = parse_label(PARSE_OPTIONAL);
1004 if (label) sv_2mortal(label);
5db1eb8d
BF
1005 return newSTATEOP(label ? SvUTF8(label) : 0,
1006 label ? savepv(SvPVX(label)) : NULL,
1007 sop);
361d9b55
Z
1008}
1009
1010#define parse_keyword_labelconst() THX_parse_keyword_labelconst(aTHX)
1011static OP *THX_parse_keyword_labelconst(pTHX)
1012{
1013 return newSVOP(OP_CONST, 0, parse_label(0));
1014}
1015
78cdf107
Z
1016#define parse_keyword_arrayfullexpr() THX_parse_keyword_arrayfullexpr(aTHX)
1017static OP *THX_parse_keyword_arrayfullexpr(pTHX)
1018{
1019 return newANONLIST(parse_fullexpr(0));
1020}
1021
1022#define parse_keyword_arraylistexpr() THX_parse_keyword_arraylistexpr(aTHX)
1023static OP *THX_parse_keyword_arraylistexpr(pTHX)
1024{
1025 return newANONLIST(parse_listexpr(0));
1026}
1027
1028#define parse_keyword_arraytermexpr() THX_parse_keyword_arraytermexpr(aTHX)
1029static OP *THX_parse_keyword_arraytermexpr(pTHX)
1030{
1031 return newANONLIST(parse_termexpr(0));
1032}
1033
1034#define parse_keyword_arrayarithexpr() THX_parse_keyword_arrayarithexpr(aTHX)
1035static OP *THX_parse_keyword_arrayarithexpr(pTHX)
1036{
1037 return newANONLIST(parse_arithexpr(0));
1038}
1039
1040#define parse_keyword_arrayexprflags() THX_parse_keyword_arrayexprflags(aTHX)
1041static OP *THX_parse_keyword_arrayexprflags(pTHX)
1042{
1043 U32 flags = 0;
1044 I32 c;
1045 OP *o;
1046 lex_read_space(0);
1047 c = lex_peek_unichar(0);
1048 if (c != '!' && c != '?') croak("syntax error");
1049 lex_read_unichar(0);
1050 if (c == '?') flags |= PARSE_OPTIONAL;
1051 o = parse_listexpr(flags);
1052 return o ? newANONLIST(o) : newANONHASH(newOP(OP_STUB, 0));
1053}
1054
996b0cb8
PLE
1055#define parse_keyword_subsignature() THX_parse_keyword_subsignature(aTHX)
1056static OP *THX_parse_keyword_subsignature(pTHX)
1057{
1058 OP *retop = NULL, *sigop = parse_subsignature(0);
1059 OP *kid;
1060 int seen_nextstate = 0;
1061
1062 /* We can't yield the optree as is to the caller because it won't be
1063 * executable outside of a called sub. We'll have to convert it into
4df85778
DM
1064 * something safe for them to invoke.
1065 * sigop should be an OP_NULL above a OP_LINESEQ containing
1066 * OP_NEXTSTATE-separated OP_ARGCHECK and OP_ARGELEMs
996b0cb8 1067 */
4df85778
DM
1068 if(sigop->op_type != OP_NULL)
1069 croak("Expected parse_subsignature() to yield an OP_NULL");
1070
1071 if(!(sigop->op_flags & OPf_KIDS))
1072 croak("Expected parse_subsignature() to yield an OP_NULL with kids");
1073 sigop = cUNOPx(sigop)->op_first;
1074
996b0cb8
PLE
1075 if(sigop->op_type != OP_LINESEQ)
1076 croak("Expected parse_subsignature() to yield an OP_LINESEQ");
1077
1078 for(kid = cLISTOPx(sigop)->op_first; kid; kid = OpSIBLING(kid)) {
1079 switch(kid->op_type) {
1080 case OP_NEXTSTATE:
1081 /* Only emit the first one otherwise they get boring */
1082 if(seen_nextstate)
1083 break;
1084 seen_nextstate++;
1085 retop = op_append_list(OP_LIST, retop, newSVOP(OP_CONST, 0,
1086 /* newSVpvf("nextstate:%s:%d", CopFILE(cCOPx(kid)), cCOPx(kid)->cop_line))); */
1087 newSVpvf("nextstate:%d", cCOPx(kid)->cop_line)));
1088 break;
1089 case OP_ARGCHECK: {
1090 UNOP_AUX_item *aux = cUNOP_AUXx(kid)->op_aux;
1091 char slurpy = (char)(aux[2].iv);
1092 retop = op_append_list(OP_LIST, retop, newSVOP(OP_CONST, 0,
1093 newSVpvf("argcheck:%d:%d:%c", (int)(aux[0].iv), (int)(aux[1].iv), slurpy ? slurpy : '-')));
1094 break;
1095 }
1096 case OP_ARGELEM: {
1097 PADOFFSET padix = kid->op_targ;
1098 PADNAMELIST *names = PadlistNAMES(CvPADLIST(find_runcv(0)));
1099 char *namepv = PadnamePV(padnamelist_fetch(names, padix));
1100 retop = op_append_list(OP_LIST, retop, newSVOP(OP_CONST, 0,
1101 newSVpvf(kid->op_flags & OPf_KIDS ? "argelem:%s:d" : "argelem:%s", namepv)));
1102 break;
1103 }
1104 default:
1105 fprintf(stderr, "TODO: examine kid %p (optype=%s)\n", kid, PL_op_name[kid->op_type]);
1106 break;
1107 }
1108 }
1109
1110 op_free(sigop);
1111 return newANONLIST(retop);
1112}
1113
ce409cc8
LM
1114#define parse_keyword_DEFSV() THX_parse_keyword_DEFSV(aTHX)
1115static OP *THX_parse_keyword_DEFSV(pTHX)
1116{
1117 return newDEFSVOP();
1118}
1119
1d07f51c
LM
1120#define sv_cat_c(a,b) THX_sv_cat_c(aTHX_ a, b)
1121static void THX_sv_cat_c(pTHX_ SV *sv, U32 c) {
25f5d540
LM
1122 char ds[UTF8_MAXBYTES + 1], *d;
1123 d = (char *)uvchr_to_utf8((U8 *)ds, c);
1124 if (d - ds > 1) {
1125 sv_utf8_upgrade(sv);
1126 }
1127 sv_catpvn(sv, ds, d - ds);
1128}
1129
1130#define parse_keyword_with_vars() THX_parse_keyword_with_vars(aTHX)
1131static OP *THX_parse_keyword_with_vars(pTHX)
1132{
1133 I32 c;
1134 IV count;
1135 int save_ix;
1136 OP *vardeclseq, *body;
1137
1138 save_ix = block_start(TRUE);
1139 vardeclseq = NULL;
1140
1141 count = 0;
1142
1143 lex_read_space(0);
1144 c = lex_peek_unichar(0);
1145 while (c != '{') {
1146 SV *varname;
1147 PADOFFSET padoff;
1148
1149 if (c == -1) {
1150 croak("unexpected EOF; expecting '{'");
1151 }
1152
1153 if (!isIDFIRST_uni(c)) {
1154 croak("unexpected '%c'; expecting an identifier", (int)c);
1155 }
1156
1157 varname = newSVpvs("$");
1158 if (lex_bufutf8()) {
1159 SvUTF8_on(varname);
1160 }
1161
1162 sv_cat_c(varname, c);
1163 lex_read_unichar(0);
1164
1165 while (c = lex_peek_unichar(0), c != -1 && isIDCONT_uni(c)) {
1166 sv_cat_c(varname, c);
1167 lex_read_unichar(0);
1168 }
1169
1170 padoff = pad_add_name_sv(varname, padadd_NO_DUP_CHECK, NULL, NULL);
1171
1172 {
1173 OP *my_var = newOP(OP_PADSV, OPf_MOD | (OPpLVAL_INTRO << 8));
1174 my_var->op_targ = padoff;
1175
1176 vardeclseq = op_append_list(
1177 OP_LINESEQ,
1178 vardeclseq,
1179 newSTATEOP(
1180 0, NULL,
1181 newASSIGNOP(
1182 OPf_STACKED,
1183 my_var, 0,
1184 newSVOP(
1185 OP_CONST, 0,
1186 newSViv(++count)
1187 )
1188 )
1189 )
1190 );
1191 }
1192
1193 lex_read_space(0);
1194 c = lex_peek_unichar(0);
1195 }
1196
1197 intro_my();
1198
1199 body = parse_block(0);
1200
1201 return block_end(save_ix, op_append_list(OP_LINESEQ, vardeclseq, body));
1202}
1203
03d05f6e
LM
1204#define parse_join_with_space() THX_parse_join_with_space(aTHX)
1205static OP *THX_parse_join_with_space(pTHX)
1206{
1207 OP *delim, *args;
1208
1209 args = parse_listexpr(0);
1210 delim = newSVOP(OP_CONST, 0, newSVpvs(" "));
1211 return op_convert_list(OP_JOIN, 0, op_prepend_elem(OP_LIST, delim, args));
1212}
1213
83f8bb56
Z
1214/* plugin glue */
1215
1216#define keyword_active(hintkey_sv) THX_keyword_active(aTHX_ hintkey_sv)
1217static int THX_keyword_active(pTHX_ SV *hintkey_sv)
1218{
1219 HE *he;
1220 if(!GvHV(PL_hintgv)) return 0;
1221 he = hv_fetch_ent(GvHV(PL_hintgv), hintkey_sv, 0,
1222 SvSHARED_HASH(hintkey_sv));
1223 return he && SvTRUE(HeVAL(he));
1224}
1225
1226static int my_keyword_plugin(pTHX_
1227 char *keyword_ptr, STRLEN keyword_len, OP **op_ptr)
1228{
b59bf0b2 1229 if (memEQs(keyword_ptr, keyword_len, "rpn") &&
83f8bb56
Z
1230 keyword_active(hintkey_rpn_sv)) {
1231 *op_ptr = parse_keyword_rpn();
1232 return KEYWORD_PLUGIN_EXPR;
b59bf0b2 1233 } else if (memEQs(keyword_ptr, keyword_len, "calcrpn") &&
83f8bb56
Z
1234 keyword_active(hintkey_calcrpn_sv)) {
1235 *op_ptr = parse_keyword_calcrpn();
1236 return KEYWORD_PLUGIN_STMT;
b59bf0b2 1237 } else if (memEQs(keyword_ptr, keyword_len, "stufftest") &&
83f8bb56
Z
1238 keyword_active(hintkey_stufftest_sv)) {
1239 *op_ptr = parse_keyword_stufftest();
1240 return KEYWORD_PLUGIN_STMT;
b59bf0b2 1241 } else if (memEQs(keyword_ptr, keyword_len, "swaptwostmts") &&
83f8bb56
Z
1242 keyword_active(hintkey_swaptwostmts_sv)) {
1243 *op_ptr = parse_keyword_swaptwostmts();
1244 return KEYWORD_PLUGIN_STMT;
b59bf0b2 1245 } else if (memEQs(keyword_ptr, keyword_len, "looprest") &&
07ffcb73
Z
1246 keyword_active(hintkey_looprest_sv)) {
1247 *op_ptr = parse_keyword_looprest();
1248 return KEYWORD_PLUGIN_STMT;
b59bf0b2 1249 } else if (memEQs(keyword_ptr, keyword_len, "scopelessblock") &&
a7aaec61
Z
1250 keyword_active(hintkey_scopelessblock_sv)) {
1251 *op_ptr = parse_keyword_scopelessblock();
1252 return KEYWORD_PLUGIN_STMT;
b59bf0b2 1253 } else if (memEQs(keyword_ptr, keyword_len, "stmtasexpr") &&
9eb5c532
Z
1254 keyword_active(hintkey_stmtasexpr_sv)) {
1255 *op_ptr = parse_keyword_stmtasexpr();
1256 return KEYWORD_PLUGIN_EXPR;
b59bf0b2 1257 } else if (memEQs(keyword_ptr, keyword_len, "stmtsasexpr") &&
9eb5c532
Z
1258 keyword_active(hintkey_stmtsasexpr_sv)) {
1259 *op_ptr = parse_keyword_stmtsasexpr();
1260 return KEYWORD_PLUGIN_EXPR;
b59bf0b2 1261 } else if (memEQs(keyword_ptr, keyword_len, "loopblock") &&
e53d8f76
Z
1262 keyword_active(hintkey_loopblock_sv)) {
1263 *op_ptr = parse_keyword_loopblock();
1264 return KEYWORD_PLUGIN_STMT;
b59bf0b2 1265 } else if (memEQs(keyword_ptr, keyword_len, "blockasexpr") &&
e53d8f76
Z
1266 keyword_active(hintkey_blockasexpr_sv)) {
1267 *op_ptr = parse_keyword_blockasexpr();
1268 return KEYWORD_PLUGIN_EXPR;
b59bf0b2 1269 } else if (memEQs(keyword_ptr, keyword_len, "swaplabel") &&
361d9b55
Z
1270 keyword_active(hintkey_swaplabel_sv)) {
1271 *op_ptr = parse_keyword_swaplabel();
1272 return KEYWORD_PLUGIN_STMT;
b59bf0b2 1273 } else if (memEQs(keyword_ptr, keyword_len, "labelconst") &&
361d9b55
Z
1274 keyword_active(hintkey_labelconst_sv)) {
1275 *op_ptr = parse_keyword_labelconst();
1276 return KEYWORD_PLUGIN_EXPR;
b59bf0b2 1277 } else if (memEQs(keyword_ptr, keyword_len, "arrayfullexpr") &&
78cdf107
Z
1278 keyword_active(hintkey_arrayfullexpr_sv)) {
1279 *op_ptr = parse_keyword_arrayfullexpr();
1280 return KEYWORD_PLUGIN_EXPR;
b59bf0b2 1281 } else if (memEQs(keyword_ptr, keyword_len, "arraylistexpr") &&
78cdf107
Z
1282 keyword_active(hintkey_arraylistexpr_sv)) {
1283 *op_ptr = parse_keyword_arraylistexpr();
1284 return KEYWORD_PLUGIN_EXPR;
b59bf0b2 1285 } else if (memEQs(keyword_ptr, keyword_len, "arraytermexpr") &&
78cdf107
Z
1286 keyword_active(hintkey_arraytermexpr_sv)) {
1287 *op_ptr = parse_keyword_arraytermexpr();
1288 return KEYWORD_PLUGIN_EXPR;
b59bf0b2 1289 } else if (memEQs(keyword_ptr, keyword_len, "arrayarithexpr") &&
78cdf107
Z
1290 keyword_active(hintkey_arrayarithexpr_sv)) {
1291 *op_ptr = parse_keyword_arrayarithexpr();
1292 return KEYWORD_PLUGIN_EXPR;
b59bf0b2 1293 } else if (memEQs(keyword_ptr, keyword_len, "arrayexprflags") &&
78cdf107
Z
1294 keyword_active(hintkey_arrayexprflags_sv)) {
1295 *op_ptr = parse_keyword_arrayexprflags();
1296 return KEYWORD_PLUGIN_EXPR;
b59bf0b2 1297 } else if (memEQs(keyword_ptr, keyword_len, "DEFSV") &&
ce409cc8
LM
1298 keyword_active(hintkey_DEFSV_sv)) {
1299 *op_ptr = parse_keyword_DEFSV();
1300 return KEYWORD_PLUGIN_EXPR;
b59bf0b2 1301 } else if (memEQs(keyword_ptr, keyword_len, "with_vars") &&
25f5d540
LM
1302 keyword_active(hintkey_with_vars_sv)) {
1303 *op_ptr = parse_keyword_with_vars();
1304 return KEYWORD_PLUGIN_STMT;
b59bf0b2 1305 } else if (memEQs(keyword_ptr, keyword_len, "join_with_space") &&
03d05f6e
LM
1306 keyword_active(hintkey_join_with_space_sv)) {
1307 *op_ptr = parse_join_with_space();
1308 return KEYWORD_PLUGIN_EXPR;
996b0cb8
PLE
1309 } else if (memEQs(keyword_ptr, keyword_len, "subsignature") &&
1310 keyword_active(hintkey_subsignature_sv)) {
1311 *op_ptr = parse_keyword_subsignature();
1312 return KEYWORD_PLUGIN_EXPR;
83f8bb56 1313 } else {
fbc25bdd 1314 assert(next_keyword_plugin != my_keyword_plugin);
83f8bb56
Z
1315 return next_keyword_plugin(aTHX_ keyword_ptr, keyword_len, op_ptr);
1316 }
1317}
1318
f568d64d
BM
1319static XOP my_xop;
1320
1321static OP *
1322pp_xop(pTHX)
1323{
1324 return PL_op->op_next;
1325}
1326
1327static void
1328peep_xop(pTHX_ OP *o, OP *oldop)
1329{
1330 dMY_CXT;
147e3846
KW
1331 av_push(MY_CXT.xop_record, newSVpvf("peep:%" UVxf, PTR2UV(o)));
1332 av_push(MY_CXT.xop_record, newSVpvf("oldop:%" UVxf, PTR2UV(oldop)));
f568d64d
BM
1333}
1334
27fcb6ee
FC
1335static I32
1336filter_call(pTHX_ int idx, SV *buf_sv, int maxlen)
1337{
27fcb6ee
FC
1338 char *p;
1339 char *end;
1340 int n = FILTER_READ(idx + 1, buf_sv, maxlen);
1341
1342 if (n<=0) return n;
1343
1344 p = SvPV_force_nolen(buf_sv);
1345 end = p + SvCUR(buf_sv);
1346 while (p < end) {
1347 if (*p == 'o') *p = 'e';
1348 p++;
1349 }
1350 return SvCUR(buf_sv);
1351}
1352
02cab674
FC
1353static AV *
1354myget_linear_isa(pTHX_ HV *stash, U32 level) {
02cab674 1355 GV **gvp = (GV **)hv_fetchs(stash, "ISA", 0);
6dbddf4a 1356 PERL_UNUSED_ARG(level);
02cab674
FC
1357 return gvp && *gvp && GvAV(*gvp)
1358 ? GvAV(*gvp)
1359 : (AV *)sv_2mortal((SV *)newAV());
1360}
1361
27fcb6ee 1362
9777c6aa
SM
1363XS_EXTERNAL(XS_XS__APItest__XSUB_XS_VERSION_undef);
1364XS_EXTERNAL(XS_XS__APItest__XSUB_XS_VERSION_empty);
1365XS_EXTERNAL(XS_XS__APItest__XSUB_XS_APIVERSION_invalid);
7b20c7cd 1366
02cab674
FC
1367static struct mro_alg mymro;
1368
e8570548
Z
1369static Perl_check_t addissub_nxck_add;
1370
1371static OP *
1372addissub_myck_add(pTHX_ OP *op)
1373{
1374 SV **flag_svp = hv_fetchs(GvHV(PL_hintgv), "XS::APItest/addissub", 0);
1375 OP *aop, *bop;
1376 U8 flags;
1377 if (!(flag_svp && SvTRUE(*flag_svp) && (op->op_flags & OPf_KIDS) &&
e6dae479
FC
1378 (aop = cBINOPx(op)->op_first) && (bop = OpSIBLING(aop)) &&
1379 !OpHAS_SIBLING(bop)))
e8570548 1380 return addissub_nxck_add(aTHX_ op);
e8570548 1381 flags = op->op_flags;
3253bf85
DM
1382 op_sibling_splice(op, NULL, 1, NULL); /* excise aop */
1383 op_sibling_splice(op, NULL, 1, NULL); /* excise bop */
1384 op_free(op); /* free the empty husk */
1385 flags &= ~OPf_KIDS;
e8570548
Z
1386 return newBINOP(OP_SUBTRACT, flags, aop, bop);
1387}
02cab674 1388
39c012bc
FC
1389static Perl_check_t old_ck_rv2cv;
1390
1391static OP *
1392my_ck_rv2cv(pTHX_ OP *o)
1393{
1394 SV *ref;
1395 SV **flag_svp = hv_fetchs(GvHV(PL_hintgv), "XS::APItest/addunder", 0);
1396 OP *aop;
1397
1398 if (flag_svp && SvTRUE(*flag_svp) && (o->op_flags & OPf_KIDS)
1399 && (aop = cUNOPx(o)->op_first) && aop->op_type == OP_CONST
1400 && aop->op_private & (OPpCONST_ENTERED|OPpCONST_BARE)
1401 && (ref = cSVOPx(aop)->op_sv) && SvPOK(ref) && SvCUR(ref)
1402 && *(SvEND(ref)-1) == 'o')
1403 {
1404 SvGROW(ref, SvCUR(ref)+2);
1405 *SvEND(ref) = '_';
2324bdb9 1406 SvCUR(ref)++; /* Not _set, so we don't accidentally break non-PERL_CORE */
39c012bc
FC
1407 *SvEND(ref) = '\0';
1408 }
1409 return old_ck_rv2cv(aTHX_ o);
1410}
1411
55289a74
NC
1412#include "const-c.inc"
1413
ffe53d21 1414MODULE = XS::APItest PACKAGE = XS::APItest
0314122a 1415
55289a74
NC
1416INCLUDE: const-xs.inc
1417
ffe53d21
NC
1418INCLUDE: numeric.xs
1419
11f9ab1a
TC
1420void
1421assertx(int x)
1422 CODE:
1423 /* this only needs to compile and checks that assert() can be
1424 used this way syntactically */
950c540d
JH
1425 (void)(assert(x), 1);
1426 (void)(x);
11f9ab1a 1427
fed3ba5d
NC
1428MODULE = XS::APItest::utf8 PACKAGE = XS::APItest::utf8
1429
1430int
1431bytes_cmp_utf8(bytes, utf8)
1432 SV *bytes
1433 SV *utf8
1434 PREINIT:
1435 const U8 *b;
1436 STRLEN blen;
1437 const U8 *u;
1438 STRLEN ulen;
1439 CODE:
1440 b = (const U8 *)SvPVbyte(bytes, blen);
1441 u = (const U8 *)SvPVbyte(utf8, ulen);
1442 RETVAL = bytes_cmp_utf8(b, blen, u, ulen);
1443 OUTPUT:
1444 RETVAL
1445
eb83ed87 1446AV *
e0280e49 1447test_utf8_to_bytes(bytes, len)
8cfe356d 1448 U8 * bytes
e0280e49 1449 STRLEN len
ae315a0a
KW
1450 PREINIT:
1451 char * ret;
1452 CODE:
1453 RETVAL = newAV();
1454 sv_2mortal((SV*)RETVAL);
1455
e0280e49 1456 ret = (char *) utf8_to_bytes(bytes, &len);
ae315a0a 1457 av_push(RETVAL, newSVpv(ret, 0));
e0280e49
KW
1458
1459 /* utf8_to_bytes uses (STRLEN)-1 to signal errors, and we want to
1460 * return that as -1 to perl, so cast to SSize_t in case
1461 * sizeof(IV) > sizeof(STRLEN) */
1462 av_push(RETVAL, newSViv((SSize_t)len));
ae315a0a
KW
1463 av_push(RETVAL, newSVpv((const char *) bytes, 0));
1464
1465 OUTPUT:
1466 RETVAL
1467
1468AV *
37657a5b
KW
1469test_utf8n_to_uvchr_msgs(s, len, flags)
1470 char *s
1471 STRLEN len
1472 U32 flags
1473 PREINIT:
1474 STRLEN retlen;
1475 UV ret;
1476 U32 errors;
1477 AV *msgs = NULL;
1478
1479 CODE:
1480 RETVAL = newAV();
1481 sv_2mortal((SV*)RETVAL);
1482
1483 ret = utf8n_to_uvchr_msgs((U8*) s,
1484 len,
1485 &retlen,
1486 flags,
1487 &errors,
1488 &msgs);
1489
1490 /* Returns the return value in [0]; <retlen> in [1], <errors> in [2] */
1491 av_push(RETVAL, newSVuv(ret));
1492 if (retlen == (STRLEN) -1) {
1493 av_push(RETVAL, newSViv(-1));
1494 }
1495 else {
1496 av_push(RETVAL, newSVuv(retlen));
1497 }
1498 av_push(RETVAL, newSVuv(errors));
1499
1500 /* And any messages in [3] */
1501 if (msgs) {
1502 av_push(RETVAL, newRV_noinc((SV*)msgs));
1503 }
1504
1505 OUTPUT:
1506 RETVAL
1507
1508AV *
f9380377 1509test_utf8n_to_uvchr_error(s, len, flags)
eb83ed87 1510
9c13cd3c
KW
1511 char *s
1512 STRLEN len
1513 U32 flags
eb83ed87
KW
1514 PREINIT:
1515 STRLEN retlen;
1516 UV ret;
f9380377 1517 U32 errors;
eb83ed87
KW
1518
1519 CODE:
f9380377
KW
1520 /* Now that utf8n_to_uvchr() is a trivial wrapper for
1521 * utf8n_to_uvchr_error(), call the latter with the inputs. It always
1522 * asks for the actual length to be returned and errors to be returned
eb83ed87
KW
1523 *
1524 * Length to assume <s> is; not checked, so could have buffer overflow
1525 */
1526 RETVAL = newAV();
1527 sv_2mortal((SV*)RETVAL);
1528
9c13cd3c
KW
1529 ret = utf8n_to_uvchr_error((U8*) s,
1530 len,
f9380377 1531 &retlen,
9c13cd3c 1532 flags,
f9380377 1533 &errors);
eb83ed87 1534
f9380377 1535 /* Returns the return value in [0]; <retlen> in [1], <errors> in [2] */
eb83ed87
KW
1536 av_push(RETVAL, newSVuv(ret));
1537 if (retlen == (STRLEN) -1) {
1538 av_push(RETVAL, newSViv(-1));
1539 }
1540 else {
1541 av_push(RETVAL, newSVuv(retlen));
1542 }
f9380377 1543 av_push(RETVAL, newSVuv(errors));
eb83ed87
KW
1544
1545 OUTPUT:
1546 RETVAL
1547
75ffa578
KW
1548AV *
1549test_valid_utf8_to_uvchr(s)
1550
1551 SV *s
1552 PREINIT:
1553 STRLEN retlen;
1554 UV ret;
75ffa578
KW
1555
1556 CODE:
1557 /* Call utf8n_to_uvchr() with the inputs. It always asks for the
1558 * actual length to be returned
1559 *
1560 * Length to assume <s> is; not checked, so could have buffer overflow
1561 */
1562 RETVAL = newAV();
1563 sv_2mortal((SV*)RETVAL);
1564
70250a0b 1565 ret = valid_utf8_to_uvchr((U8*) SvPV_nolen(s), &retlen);
75ffa578
KW
1566
1567 /* Returns the return value in [0]; <retlen> in [1] */
1568 av_push(RETVAL, newSVuv(ret));
1569 av_push(RETVAL, newSVuv(retlen));
1570
1571 OUTPUT:
1572 RETVAL
1573
046d01eb
KW
1574SV *
1575test_uvchr_to_utf8_flags(uv, flags)
1576
1577 SV *uv
1578 SV *flags
1579 PREINIT:
81b3f640 1580 U8 dest[UTF8_MAXBYTES + 1];
046d01eb
KW
1581 U8 *ret;
1582
1583 CODE:
1584 /* Call uvchr_to_utf8_flags() with the inputs. */
1585 ret = uvchr_to_utf8_flags(dest, SvUV(uv), SvUV(flags));
1586 if (! ret) {
1587 XSRETURN_UNDEF;
1588 }
1589 RETVAL = newSVpvn((char *) dest, ret - dest);
1590
1591 OUTPUT:
1592 RETVAL
1593
33f38593
KW
1594AV *
1595test_uvchr_to_utf8_flags_msgs(uv, flags)
1596
1597 SV *uv
1598 SV *flags
1599 PREINIT:
1600 U8 dest[UTF8_MAXBYTES + 1];
1601 U8 *ret;
1602
1603 CODE:
1604 HV *msgs = NULL;
1605 RETVAL = newAV();
1606 sv_2mortal((SV*)RETVAL);
1607
1608 ret = uvchr_to_utf8_flags_msgs(dest, SvUV(uv), SvUV(flags), &msgs);
1609
1610 if (ret) {
1611 av_push(RETVAL, newSVpvn((char *) dest, ret - dest));
1612 }
1613 else {
1614 av_push(RETVAL, &PL_sv_undef);
1615 }
1616
1617 if (msgs) {
1618 av_push(RETVAL, newRV_noinc((SV*)msgs));
1619 }
1620
1621 OUTPUT:
1622 RETVAL
1623
7d255dc8
NC
1624MODULE = XS::APItest:Overload PACKAGE = XS::APItest::Overload
1625
c33e8be1 1626void
25a9ffce 1627amagic_deref_call(sv, what)
7d255dc8
NC
1628 SV *sv
1629 int what
1630 PPCODE:
7d255dc8 1631 /* The reference is owned by something else. */
25a9ffce 1632 PUSHs(amagic_deref_call(sv, what));
7d255dc8 1633
e89bfaa6
NC
1634# I'd certainly like to discourage the use of this macro, given that we now
1635# have amagic_deref_call
1636
c33e8be1 1637void
e89bfaa6
NC
1638tryAMAGICunDEREF_var(sv, what)
1639 SV *sv
1640 int what
1641 PPCODE:
1642 {
1643 SV **sp = &sv;
1644 switch(what) {
1645 case to_av_amg:
1646 tryAMAGICunDEREF(to_av);
1647 break;
1648 case to_cv_amg:
1649 tryAMAGICunDEREF(to_cv);
1650 break;
1651 case to_gv_amg:
1652 tryAMAGICunDEREF(to_gv);
1653 break;
1654 case to_hv_amg:
1655 tryAMAGICunDEREF(to_hv);
1656 break;
1657 case to_sv_amg:
1658 tryAMAGICunDEREF(to_sv);
1659 break;
1660 default:
1661 croak("Invalid value %d passed to tryAMAGICunDEREF_var", what);
1662 }
1663 }
1664 /* The reference is owned by something else. */
1665 PUSHs(sv);
1666
7b20c7cd
NC
1667MODULE = XS::APItest PACKAGE = XS::APItest::XSUB
1668
1669BOOT:
1670 newXS("XS::APItest::XSUB::XS_VERSION_undef", XS_XS__APItest__XSUB_XS_VERSION_undef, __FILE__);
f9cc56fa 1671 newXS("XS::APItest::XSUB::XS_VERSION_empty", XS_XS__APItest__XSUB_XS_VERSION_empty, __FILE__);
88c4b02d 1672 newXS("XS::APItest::XSUB::XS_APIVERSION_invalid", XS_XS__APItest__XSUB_XS_APIVERSION_invalid, __FILE__);
7b20c7cd
NC
1673
1674void
1675XS_VERSION_defined(...)
1676 PPCODE:
1677 XS_VERSION_BOOTCHECK;
1678 XSRETURN_EMPTY;
1679
88c4b02d
NC
1680void
1681XS_APIVERSION_valid(...)
1682 PPCODE:
1683 XS_APIVERSION_BOOTCHECK;
1684 XSRETURN_EMPTY;
1685
50ceb817
DB
1686void
1687xsreturn( int len )
1688 PPCODE:
1689 int i = 0;
1690 EXTEND( SP, len );
1691 for ( ; i < len; i++ ) {
1692 ST(i) = sv_2mortal( newSViv(i) );
1693 }
1694 XSRETURN( len );
1695
1696void
1697xsreturn_iv()
1698 PPCODE:
052efbb4 1699 XSRETURN_IV(I32_MIN + 1);
50ceb817
DB
1700
1701void
1702xsreturn_uv()
1703 PPCODE:
7e327f76 1704 XSRETURN_UV( (U32)((1U<<31) + 1) );
50ceb817
DB
1705
1706void
1707xsreturn_nv()
1708 PPCODE:
1709 XSRETURN_NV(0.25);
1710
1711void
1712xsreturn_pv()
1713 PPCODE:
1714 XSRETURN_PV("returned");
1715
1716void
1717xsreturn_pvn()
1718 PPCODE:
1719 XSRETURN_PVN("returned too much",8);
1720
1721void
1722xsreturn_no()
1723 PPCODE:
1724 XSRETURN_NO;
1725
1726void
1727xsreturn_yes()
1728 PPCODE:
1729 XSRETURN_YES;
1730
1731void
1732xsreturn_undef()
1733 PPCODE:
1734 XSRETURN_UNDEF;
1735
1736void
1737xsreturn_empty()
1738 PPCODE:
1739 XSRETURN_EMPTY;
1740
ffe53d21
NC
1741MODULE = XS::APItest:Hash PACKAGE = XS::APItest::Hash
1742
b54b4831
NC
1743void
1744rot13_hash(hash)
1745 HV *hash
1746 CODE:
1747 {
1748 struct ufuncs uf;
1749 uf.uf_val = rot13_key;
1750 uf.uf_set = 0;
1751 uf.uf_index = 0;
1752
1753 sv_magic((SV*)hash, NULL, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
1754 }
1755
53c40a8f
NC
1756void
1757bitflip_hash(hash)
1758 HV *hash
1759 CODE:
1760 {
1761 struct ufuncs uf;
1762 uf.uf_val = bitflip_key;
1763 uf.uf_set = 0;
1764 uf.uf_index = 0;
1765
1766 sv_magic((SV*)hash, NULL, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
1767 }
1768
028f8eaa
MHM
1769#define UTF8KLEN(sv, len) (SvUTF8(sv) ? -(I32)len : (I32)len)
1770
0314122a
NC
1771bool
1772exists(hash, key_sv)
1773 PREINIT:
1774 STRLEN len;
1775 const char *key;
1776 INPUT:
1777 HV *hash
1778 SV *key_sv
1779 CODE:
1780 key = SvPV(key_sv, len);
028f8eaa 1781 RETVAL = hv_exists(hash, key, UTF8KLEN(key_sv, len));
0314122a
NC
1782 OUTPUT:
1783 RETVAL
1784
bdee33e4
NC
1785bool
1786exists_ent(hash, key_sv)
1787 PREINIT:
1788 INPUT:
1789 HV *hash
1790 SV *key_sv
1791 CODE:
1792 RETVAL = hv_exists_ent(hash, key_sv, 0);
1793 OUTPUT:
1794 RETVAL
1795
b60cf05a 1796SV *
55289a74 1797delete(hash, key_sv, flags = 0)
b60cf05a
NC
1798 PREINIT:
1799 STRLEN len;
1800 const char *key;
1801 INPUT:
1802 HV *hash
1803 SV *key_sv
55289a74 1804 I32 flags;
b60cf05a
NC
1805 CODE:
1806 key = SvPV(key_sv, len);
1807 /* It's already mortal, so need to increase reference count. */
55289a74
NC
1808 RETVAL
1809 = SvREFCNT_inc(hv_delete(hash, key, UTF8KLEN(key_sv, len), flags));
1810 OUTPUT:
1811 RETVAL
1812
1813SV *
1814delete_ent(hash, key_sv, flags = 0)
1815 INPUT:
1816 HV *hash
1817 SV *key_sv
1818 I32 flags;
1819 CODE:
1820 /* It's already mortal, so need to increase reference count. */
1821 RETVAL = SvREFCNT_inc(hv_delete_ent(hash, key_sv, flags, 0));
b60cf05a
NC
1822 OUTPUT:
1823 RETVAL
1824
1825SV *
858117f8
NC
1826store_ent(hash, key, value)
1827 PREINIT:
1828 SV *copy;
1829 HE *result;
1830 INPUT:
1831 HV *hash
1832 SV *key
1833 SV *value
1834 CODE:
1835 copy = newSV(0);
1836 result = hv_store_ent(hash, key, copy, 0);
1837 SvSetMagicSV(copy, value);
1838 if (!result) {
1839 SvREFCNT_dec(copy);
1840 XSRETURN_EMPTY;
1841 }
1842 /* It's about to become mortal, so need to increase reference count.
1843 */
1844 RETVAL = SvREFCNT_inc(HeVAL(result));
1845 OUTPUT:
1846 RETVAL
1847
858117f8 1848SV *
b60cf05a
NC
1849store(hash, key_sv, value)
1850 PREINIT:
1851 STRLEN len;
1852 const char *key;
1853 SV *copy;
1854 SV **result;
1855 INPUT:
1856 HV *hash
1857 SV *key_sv
1858 SV *value
1859 CODE:
1860 key = SvPV(key_sv, len);
1861 copy = newSV(0);
028f8eaa 1862 result = hv_store(hash, key, UTF8KLEN(key_sv, len), copy, 0);
858117f8 1863 SvSetMagicSV(copy, value);
b60cf05a
NC
1864 if (!result) {
1865 SvREFCNT_dec(copy);
1866 XSRETURN_EMPTY;
1867 }
1868 /* It's about to become mortal, so need to increase reference count.
1869 */
1870 RETVAL = SvREFCNT_inc(*result);
1871 OUTPUT:
1872 RETVAL
1873
bdee33e4
NC
1874SV *
1875fetch_ent(hash, key_sv)
1876 PREINIT:
1877 HE *result;
1878 INPUT:
1879 HV *hash
1880 SV *key_sv
1881 CODE:
1882 result = hv_fetch_ent(hash, key_sv, 0, 0);
1883 if (!result) {
1884 XSRETURN_EMPTY;
1885 }
1886 /* Force mg_get */
1887 RETVAL = newSVsv(HeVAL(result));
1888 OUTPUT:
1889 RETVAL
b60cf05a
NC
1890
1891SV *
1892fetch(hash, key_sv)
1893 PREINIT:
1894 STRLEN len;
1895 const char *key;
1896 SV **result;
1897 INPUT:
1898 HV *hash
1899 SV *key_sv
1900 CODE:
1901 key = SvPV(key_sv, len);
028f8eaa 1902 result = hv_fetch(hash, key, UTF8KLEN(key_sv, len), 0);
b60cf05a
NC
1903 if (!result) {
1904 XSRETURN_EMPTY;
1905 }
1906 /* Force mg_get */
1907 RETVAL = newSVsv(*result);
1908 OUTPUT:
1909 RETVAL
2dc92170 1910
9568a123
NC
1911#if defined (hv_common)
1912
6b4de907
NC
1913SV *
1914common(params)
1915 INPUT:
1916 HV *params
1917 PREINIT:
1918 HE *result;
1919 HV *hv = NULL;
1920 SV *keysv = NULL;
1921 const char *key = NULL;
1922 STRLEN klen = 0;
1923 int flags = 0;
1924 int action = 0;
1925 SV *val = NULL;
1926 U32 hash = 0;
1927 SV **svp;
1928 CODE:
1929 if ((svp = hv_fetchs(params, "hv", 0))) {
1930 SV *const rv = *svp;
1931 if (!SvROK(rv))
1932 croak("common passed a non-reference for parameter hv");
1933 hv = (HV *)SvRV(rv);
1934 }
1935 if ((svp = hv_fetchs(params, "keysv", 0)))
1936 keysv = *svp;
1937 if ((svp = hv_fetchs(params, "keypv", 0))) {
1938 key = SvPV_const(*svp, klen);
1939 if (SvUTF8(*svp))
1940 flags = HVhek_UTF8;
1941 }
1942 if ((svp = hv_fetchs(params, "action", 0)))
1943 action = SvIV(*svp);
1944 if ((svp = hv_fetchs(params, "val", 0)))
527df579 1945 val = newSVsv(*svp);
6b4de907 1946 if ((svp = hv_fetchs(params, "hash", 0)))
a44093a9 1947 hash = SvUV(*svp);
6b4de907 1948
de092133 1949 if (hv_fetchs(params, "hash_pv", 0)) {
9959d439 1950 assert(key);
527df579
NC
1951 PERL_HASH(hash, key, klen);
1952 }
de092133 1953 if (hv_fetchs(params, "hash_sv", 0)) {
9959d439
JH
1954 assert(keysv);
1955 {
1956 STRLEN len;
1957 const char *const p = SvPV(keysv, len);
1958 PERL_HASH(hash, p, len);
1959 }
58ca560a 1960 }
527df579 1961
a75fcbca 1962 result = (HE *)hv_common(hv, keysv, key, klen, flags, action, val, hash);
6b4de907
NC
1963 if (!result) {
1964 XSRETURN_EMPTY;
1965 }
1966 /* Force mg_get */
1967 RETVAL = newSVsv(HeVAL(result));
1968 OUTPUT:
1969 RETVAL
1970
9568a123
NC
1971#endif
1972
439efdfe 1973void
2dc92170
NC
1974test_hv_free_ent()
1975 PPCODE:
2e66fe90 1976 test_freeent(&Perl_hv_free_ent);
2dc92170
NC
1977 XSRETURN(4);
1978
439efdfe 1979void
2dc92170
NC
1980test_hv_delayfree_ent()
1981 PPCODE:
2e66fe90 1982 test_freeent(&Perl_hv_delayfree_ent);
2dc92170 1983 XSRETURN(4);
35ab5632
NC
1984
1985SV *
1986test_share_unshare_pvn(input)
1987 PREINIT:
35ab5632
NC
1988 STRLEN len;
1989 U32 hash;
1990 char *pvx;
1991 char *p;
1992 INPUT:
1993 SV *input
1994 CODE:
1995 pvx = SvPV(input, len);
1996 PERL_HASH(hash, pvx, len);
1997 p = sharepvn(pvx, len, hash);
1998 RETVAL = newSVpvn(p, len);
1999 unsharepvn(p, len, hash);
2000 OUTPUT:
2001 RETVAL
d8c5b3c5 2002
9568a123
NC
2003#if PERL_VERSION >= 9
2004
d8c5b3c5
NC
2005bool
2006refcounted_he_exists(key, level=0)
2007 SV *key
2008 IV level
2009 CODE:
2010 if (level) {
147e3846 2011 croak("level must be zero, not %" IVdf, level);
d8c5b3c5 2012 }
20439bc7 2013 RETVAL = (cop_hints_fetch_sv(PL_curcop, key, 0, 0) != &PL_sv_placeholder);
d8c5b3c5
NC
2014 OUTPUT:
2015 RETVAL
2016
d8c5b3c5
NC
2017SV *
2018refcounted_he_fetch(key, level=0)
2019 SV *key
2020 IV level
2021 CODE:
2022 if (level) {
147e3846 2023 croak("level must be zero, not %" IVdf, level);
d8c5b3c5 2024 }
20439bc7 2025 RETVAL = cop_hints_fetch_sv(PL_curcop, key, 0, 0);
d8c5b3c5
NC
2026 SvREFCNT_inc(RETVAL);
2027 OUTPUT:
2028 RETVAL
65bfe90c 2029
9568a123 2030#endif
65bfe90c 2031
5f39160d
TC
2032void
2033test_force_keys(HV *hv)
2034 PREINIT:
2035 HE *he;
052a7c76 2036 SSize_t count = 0;
5f39160d
TC
2037 PPCODE:
2038 hv_iterinit(hv);
2039 he = hv_iternext(hv);
2040 while (he) {
2041 SV *sv = HeSVKEY_force(he);
2042 ++count;
2043 EXTEND(SP, count);
2044 PUSHs(sv_mortalcopy(sv));
2045 he = hv_iternext(hv);
2046 }
2047
0314122a
NC
2048=pod
2049
2050sub TIEHASH { bless {}, $_[0] }
2051sub STORE { $_[0]->{$_[1]} = $_[2] }
2052sub FETCH { $_[0]->{$_[1]} }
2053sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
2054sub NEXTKEY { each %{$_[0]} }
2055sub EXISTS { exists $_[0]->{$_[1]} }
2056sub DELETE { delete $_[0]->{$_[1]} }
2057sub CLEAR { %{$_[0]} = () }
2058
2059=cut
2060
e2fe06dd
EB
2061MODULE = XS::APItest:TempLv PACKAGE = XS::APItest::TempLv
2062
2063void
2064make_temp_mg_lv(sv)
2065SV* sv
2066 PREINIT:
2067 SV * const lv = newSV_type(SVt_PVLV);
2068 STRLEN len;
2069 PPCODE:
2070 SvPV(sv, len);
2071
2072 sv_magic(lv, NULL, PERL_MAGIC_substr, NULL, 0);
2073 LvTYPE(lv) = 'x';
2074 LvTARG(lv) = SvREFCNT_inc_simple(sv);
2075 LvTARGOFF(lv) = len == 0 ? 0 : 1;
2076 LvTARGLEN(lv) = len < 2 ? 0 : len-2;
2077
2078 EXTEND(SP, 1);
2079 ST(0) = sv_2mortal(lv);
2080 XSRETURN(1);
2081
2082
36c2b1d0
NC
2083MODULE = XS::APItest::PtrTable PACKAGE = XS::APItest::PtrTable PREFIX = ptr_table_
2084
2085void
2086ptr_table_new(classname)
2087const char * classname
2088 PPCODE:
2089 PUSHs(sv_setref_pv(sv_newmortal(), classname, (void*)ptr_table_new()));
2090
2091void
2092DESTROY(table)
2093XS::APItest::PtrTable table
2094 CODE:
2095 ptr_table_free(table);
2096
2097void
992b2363 2098ptr_table_store(table, from, to)
36c2b1d0 2099XS::APItest::PtrTable table
992b2363
NC
2100SVREF from
2101SVREF to
36c2b1d0 2102 CODE:
992b2363 2103 ptr_table_store(table, from, to);
36c2b1d0
NC
2104
2105UV
992b2363 2106ptr_table_fetch(table, from)
36c2b1d0 2107XS::APItest::PtrTable table
992b2363 2108SVREF from
36c2b1d0 2109 CODE:
992b2363 2110 RETVAL = PTR2UV(ptr_table_fetch(table, from));
36c2b1d0
NC
2111 OUTPUT:
2112 RETVAL
2113
2114void
2115ptr_table_split(table)
2116XS::APItest::PtrTable table
2117
2118void
2119ptr_table_clear(table)
2120XS::APItest::PtrTable table
2121
6911735f
FC
2122MODULE = XS::APItest::AutoLoader PACKAGE = XS::APItest::AutoLoader
2123
2124SV *
2125AUTOLOAD()
2126 CODE:
2127 RETVAL = newSVpvn_flags(SvPVX(cv), SvCUR(cv), SvUTF8(cv));
2128 OUTPUT:
2129 RETVAL
2130
8fa6a409
FC
2131SV *
2132AUTOLOADp(...)
2133 PROTOTYPE: *$
2134 CODE:
5f74c55e 2135 PERL_UNUSED_ARG(items);
8fa6a409
FC
2136 RETVAL = newSVpvn_flags(SvPVX(cv), SvCUR(cv), SvUTF8(cv));
2137 OUTPUT:
2138 RETVAL
2139
6911735f 2140
3e61d65a
JH
2141MODULE = XS::APItest PACKAGE = XS::APItest
2142
2143PROTOTYPES: DISABLE
2144
67ecba2f
FC
2145BOOT:
2146 mymro.resolve = myget_linear_isa;
2147 mymro.name = "justisa";
2148 mymro.length = 7;
2149 mymro.kflags = 0;
2150 mymro.hash = 0;
2151 Perl_mro_register(aTHX_ &mymro);
2152
f568d64d
BM
2153HV *
2154xop_custom_ops ()
2155 CODE:
2156 RETVAL = PL_custom_ops;
2157 OUTPUT:
2158 RETVAL
2159
2160HV *
2161xop_custom_op_names ()
2162 CODE:
2163 PL_custom_op_names = newHV();
2164 RETVAL = PL_custom_op_names;
2165 OUTPUT:
2166 RETVAL
2167
2168HV *
2169xop_custom_op_descs ()
2170 CODE:
2171 PL_custom_op_descs = newHV();
2172 RETVAL = PL_custom_op_descs;
2173 OUTPUT:
2174 RETVAL
2175
2176void
2177xop_register ()
2178 CODE:
2179 XopENTRY_set(&my_xop, xop_name, "my_xop");
2180 XopENTRY_set(&my_xop, xop_desc, "XOP for testing");
2181 XopENTRY_set(&my_xop, xop_class, OA_UNOP);
2182 XopENTRY_set(&my_xop, xop_peep, peep_xop);
2183 Perl_custom_op_register(aTHX_ pp_xop, &my_xop);
2184
2185void
2186xop_clear ()
2187 CODE:
2188 XopDISABLE(&my_xop, xop_name);
2189 XopDISABLE(&my_xop, xop_desc);
2190 XopDISABLE(&my_xop, xop_class);
2191 XopDISABLE(&my_xop, xop_peep);
2192
2193IV
2194xop_my_xop ()
2195 CODE:
2196 RETVAL = PTR2IV(&my_xop);
2197 OUTPUT:
2198 RETVAL
2199
2200IV
2201xop_ppaddr ()
2202 CODE:
2203 RETVAL = PTR2IV(pp_xop);
2204 OUTPUT:
2205 RETVAL
2206
2207IV
2208xop_OA_UNOP ()
2209 CODE:
2210 RETVAL = OA_UNOP;
2211 OUTPUT:
2212 RETVAL
2213
2214AV *
2215xop_build_optree ()
2216 CODE:
2217 dMY_CXT;
2218 UNOP *unop;
2219 OP *kid;
2220
2221 MY_CXT.xop_record = newAV();
2222
2223 kid = newSVOP(OP_CONST, 0, newSViv(42));
2224
3253bf85 2225 unop = (UNOP*)mkUNOP(OP_CUSTOM, kid);
f568d64d 2226 unop->op_ppaddr = pp_xop;
f568d64d 2227 unop->op_private = 0;
f568d64d
BM
2228 unop->op_next = NULL;
2229 kid->op_next = (OP*)unop;
2230
147e3846
KW
2231 av_push(MY_CXT.xop_record, newSVpvf("unop:%" UVxf, PTR2UV(unop)));
2232 av_push(MY_CXT.xop_record, newSVpvf("kid:%" UVxf, PTR2UV(kid)));
f568d64d
BM
2233
2234 av_push(MY_CXT.xop_record, newSVpvf("NAME:%s", OP_NAME((OP*)unop)));
2235 av_push(MY_CXT.xop_record, newSVpvf("DESC:%s", OP_DESC((OP*)unop)));
c33e8be1 2236 av_push(MY_CXT.xop_record, newSVpvf("CLASS:%d", (int)OP_CLASS((OP*)unop)));
f568d64d
BM
2237
2238 PL_rpeepp(aTHX_ kid);
2239
2240 FreeOp(kid);
2241 FreeOp(unop);
2242
2243 RETVAL = MY_CXT.xop_record;
2244 MY_CXT.xop_record = NULL;
2245 OUTPUT:
2246 RETVAL
2247
ae103e09
DD
2248IV
2249xop_from_custom_op ()
2250 CODE:
2251/* author note: this test doesn't imply Perl_custom_op_xop is or isn't public
2252 API or that Perl_custom_op_xop is known to be used outside the core */
2253 UNOP *unop;
2254 XOP *xop;
2255
3253bf85 2256 unop = (UNOP*)mkUNOP(OP_CUSTOM, NULL);
ae103e09 2257 unop->op_ppaddr = pp_xop;
ae103e09 2258 unop->op_private = 0;
ae103e09
DD
2259 unop->op_next = NULL;
2260
2261 xop = Perl_custom_op_xop(aTHX_ (OP *)unop);
2262 FreeOp(unop);
2263 RETVAL = PTR2IV(xop);
2264 OUTPUT:
2265 RETVAL
2266
85ce96a1
DM
2267BOOT:
2268{
2269 MY_CXT_INIT;
03569ecf 2270
85ce96a1
DM
2271 MY_CXT.i = 99;
2272 MY_CXT.sv = newSVpv("initial",0);
13b6b3bc
BM
2273
2274 MY_CXT.bhkav = get_av("XS::APItest::bhkav", GV_ADDMULTI);
2275 MY_CXT.bhk_record = 0;
2276
a88d97bf
BM
2277 BhkENTRY_set(&bhk_test, bhk_start, blockhook_test_start);
2278 BhkENTRY_set(&bhk_test, bhk_pre_end, blockhook_test_pre_end);
2279 BhkENTRY_set(&bhk_test, bhk_post_end, blockhook_test_post_end);
2280 BhkENTRY_set(&bhk_test, bhk_eval, blockhook_test_eval);
13b6b3bc
BM
2281 Perl_blockhook_register(aTHX_ &bhk_test);
2282
65bfe90c 2283 MY_CXT.cscgv = gv_fetchpvs("XS::APItest::COMPILE_SCOPE_CONTAINER",
13b6b3bc 2284 GV_ADDMULTI, SVt_PVAV);
03569ecf
BM
2285 MY_CXT.cscav = GvAV(MY_CXT.cscgv);
2286
a88d97bf
BM
2287 BhkENTRY_set(&bhk_csc, bhk_start, blockhook_csc_start);
2288 BhkENTRY_set(&bhk_csc, bhk_pre_end, blockhook_csc_pre_end);
13b6b3bc 2289 Perl_blockhook_register(aTHX_ &bhk_csc);
201c7e1f
FR
2290
2291 MY_CXT.peep_recorder = newAV();
2292 MY_CXT.rpeep_recorder = newAV();
2293
2294 MY_CXT.orig_peep = PL_peepp;
2295 MY_CXT.orig_rpeep = PL_rpeepp;
2296 PL_peepp = my_peep;
2297 PL_rpeepp = my_rpeep;
65bfe90c 2298}
85ce96a1
DM
2299
2300void
2301CLONE(...)
2302 CODE:
2303 MY_CXT_CLONE;
c33e8be1 2304 PERL_UNUSED_VAR(items);
85ce96a1 2305 MY_CXT.sv = newSVpv("initial_clone",0);
65bfe90c 2306 MY_CXT.cscgv = gv_fetchpvs("XS::APItest::COMPILE_SCOPE_CONTAINER",
13b6b3bc 2307 GV_ADDMULTI, SVt_PVAV);
03569ecf 2308 MY_CXT.cscav = NULL;
13b6b3bc
BM
2309 MY_CXT.bhkav = get_av("XS::APItest::bhkav", GV_ADDMULTI);
2310 MY_CXT.bhk_record = 0;
201c7e1f
FR
2311 MY_CXT.peep_recorder = newAV();
2312 MY_CXT.rpeep_recorder = newAV();
85ce96a1 2313
3e61d65a
JH
2314void
2315print_double(val)
2316 double val
2317 CODE:
2318 printf("%5.3f\n",val);
2319
2320int
2321have_long_double()
2322 CODE:
2323#ifdef HAS_LONG_DOUBLE
2324 RETVAL = 1;
2325#else
2326 RETVAL = 0;
2327#endif
cabb36f0
CN
2328 OUTPUT:
2329 RETVAL
3e61d65a
JH
2330
2331void
2332print_long_double()
2333 CODE:
2334#ifdef HAS_LONG_DOUBLE
fc0bf671 2335# if defined(PERL_PRIfldbl) && (LONG_DOUBLESIZE > DOUBLESIZE)
3e61d65a
JH
2336 long double val = 7.0;
2337 printf("%5.3" PERL_PRIfldbl "\n",val);
2338# else
2339 double val = 7.0;
2340 printf("%5.3f\n",val);
2341# endif
2342#endif
2343
2344void
3e61d65a
JH
2345print_int(val)
2346 int val
2347 CODE:
2348 printf("%d\n",val);
2349
2350void
2351print_long(val)
2352 long val
2353 CODE:
2354 printf("%ld\n",val);
2355
2356void
2357print_float(val)
2358 float val
2359 CODE:
2360 printf("%5.3f\n",val);
9d911683
NIS
2361
2362void
2363print_flush()
2364 CODE:
2365 fflush(stdout);
d4b90eee
SH
2366
2367void
2368mpushp()
2369 PPCODE:
2370 EXTEND(SP, 3);
2371 mPUSHp("one", 3);
2372 mPUSHp("two", 3);
2373 mPUSHp("three", 5);
2374 XSRETURN(3);
2375
2376void
2377mpushn()
2378 PPCODE:
2379 EXTEND(SP, 3);
2380 mPUSHn(0.5);
2381 mPUSHn(-0.25);
2382 mPUSHn(0.125);
2383 XSRETURN(3);
2384
2385void
2386mpushi()
2387 PPCODE:
2388 EXTEND(SP, 3);
d75b63cf
MHM
2389 mPUSHi(-1);
2390 mPUSHi(2);
2391 mPUSHi(-3);
d4b90eee
SH
2392 XSRETURN(3);
2393
2394void
2395mpushu()
2396 PPCODE:
2397 EXTEND(SP, 3);
d75b63cf
MHM
2398 mPUSHu(1);
2399 mPUSHu(2);
2400 mPUSHu(3);
d4b90eee
SH
2401 XSRETURN(3);
2402
2403void
2404mxpushp()
2405 PPCODE:
2406 mXPUSHp("one", 3);
2407 mXPUSHp("two", 3);
2408 mXPUSHp("three", 5);
2409 XSRETURN(3);
2410
2411void
2412mxpushn()
2413 PPCODE:
2414 mXPUSHn(0.5);
2415 mXPUSHn(-0.25);
2416 mXPUSHn(0.125);
2417 XSRETURN(3);
2418
2419void
2420mxpushi()
2421 PPCODE:
d75b63cf
MHM
2422 mXPUSHi(-1);
2423 mXPUSHi(2);
2424 mXPUSHi(-3);
d4b90eee
SH
2425 XSRETURN(3);
2426
2427void
2428mxpushu()
2429 PPCODE:
d75b63cf
MHM
2430 mXPUSHu(1);
2431 mXPUSHu(2);
2432 mXPUSHu(3);
d4b90eee 2433 XSRETURN(3);
d1f347d7 2434
6768377c
DM
2435
2436 # test_EXTEND(): excerise the EXTEND() macro.
2437 # After calling EXTEND(), it also does *(p+n) = NULL and
2438 # *PL_stack_max = NULL to allow valgrind etc to spot if the stack hasn't
2439 # actually been extended properly.
2440 #
2441 # max_offset specifies the SP to use. It is treated as a signed offset
2442 # from PL_stack_max.
2443 # nsv is the SV holding the value of n indicating how many slots
2444 # to extend the stack by.
2445 # use_ss is a boolean indicating that n should be cast to a SSize_t
2446
2447void
2448test_EXTEND(max_offset, nsv, use_ss)
2449 IV max_offset;
2450 SV *nsv;
2451 bool use_ss;
2452PREINIT:
2453 SV **sp = PL_stack_max + max_offset;
2454PPCODE:
2455 if (use_ss) {
2456 SSize_t n = (SSize_t)SvIV(nsv);
2457 EXTEND(sp, n);
2458 *(sp + n) = NULL;
2459 }
2460 else {
2461 IV n = SvIV(nsv);
2462 EXTEND(sp, n);
2463 *(sp + n) = NULL;
2464 }
2465 *PL_stack_max = NULL;
2466
2467
a85ce6f0
DD
2468void
2469call_sv_C()
2470PREINIT:
2471 CV * i_sub;
2472 GV * i_gv;
2473 I32 retcnt;
2474 SV * errsv;
2475 char * errstr;
bdb7e3f0 2476 STRLEN errlen;
a85ce6f0
DD
2477 SV * miscsv = sv_newmortal();
2478 HV * hv = (HV*)sv_2mortal((SV*)newHV());
2479CODE:
2480 i_sub = get_cv("i", 0);
2481 PUSHMARK(SP);
2482 /* PUTBACK not needed since this sub was called with 0 args, and is calling
2483 0 args, so global SP doesn't need to be moved before a call_* */
2484 retcnt = call_sv((SV*)i_sub, 0); /* try a CV* */
2485 SPAGAIN;
2486 SP -= retcnt; /* dont care about return count, wipe everything off */
2487 sv_setpvs(miscsv, "i");
2488 PUSHMARK(SP);
2489 retcnt = call_sv(miscsv, 0); /* try a PV */
2490 SPAGAIN;
2491 SP -= retcnt;
2492 /* no add and SVt_NULL are intentional, sub i should be defined already */
2493 i_gv = gv_fetchpvn_flags("i", sizeof("i")-1, 0, SVt_NULL);
2494 PUSHMARK(SP);
2495 retcnt = call_sv((SV*)i_gv, 0); /* try a GV* */
2496 SPAGAIN;
2497 SP -= retcnt;
2498 /* the tests below are not declaring this being public API behavior,
2499 only current internal behavior, these tests can be changed in the
2500 future if necessery */
2501 PUSHMARK(SP);
0740a29d 2502 retcnt = call_sv(&PL_sv_yes, G_EVAL);
a85ce6f0
DD
2503 SPAGAIN;
2504 SP -= retcnt;
0740a29d
Z
2505 errsv = ERRSV;
2506 errstr = SvPV(errsv, errlen);
2507 if(memBEGINs(errstr, errlen, "Undefined subroutine &main::1 called at")) {
2508 PUSHMARK(SP);
2509 retcnt = call_sv((SV*)i_sub, 0); /* call again to increase counter */
2510 SPAGAIN;
2511 SP -= retcnt;
2512 }
a85ce6f0
DD
2513 PUSHMARK(SP);
2514 retcnt = call_sv(&PL_sv_no, G_EVAL);
2515 SPAGAIN;
2516 SP -= retcnt;
2517 errsv = ERRSV;
bdb7e3f0
KW
2518 errstr = SvPV(errsv, errlen);
2519 if(memBEGINs(errstr, errlen, "Undefined subroutine &main:: called at")) {
a85ce6f0
DD
2520 PUSHMARK(SP);
2521 retcnt = call_sv((SV*)i_sub, 0); /* call again to increase counter */
2522 SPAGAIN;
2523 SP -= retcnt;
2524 }
2525 PUSHMARK(SP);
2526 retcnt = call_sv(&PL_sv_undef, G_EVAL);
2527 SPAGAIN;
2528 SP -= retcnt;
2529 errsv = ERRSV;
bdb7e3f0
KW
2530 errstr = SvPV(errsv, errlen);
2531 if(memBEGINs(errstr, errlen, "Can't use an undefined value as a subroutine reference at")) {
a85ce6f0
DD
2532 PUSHMARK(SP);
2533 retcnt = call_sv((SV*)i_sub, 0); /* call again to increase counter */
2534 SPAGAIN;
2535 SP -= retcnt;
2536 }
2537 PUSHMARK(SP);
2538 retcnt = call_sv((SV*)hv, G_EVAL);
2539 SPAGAIN;
2540 SP -= retcnt;
2541 errsv = ERRSV;
bdb7e3f0
KW
2542 errstr = SvPV(errsv, errlen);
2543 if(memBEGINs(errstr, errlen, "Not a CODE reference at")) {
a85ce6f0
DD
2544 PUSHMARK(SP);
2545 retcnt = call_sv((SV*)i_sub, 0); /* call again to increase counter */
2546 SPAGAIN;
2547 SP -= retcnt;
2548 }
d1f347d7
DM
2549
2550void
2551call_sv(sv, flags, ...)
2552 SV* sv
2553 I32 flags
2554 PREINIT:
2555 I32 i;
2556 PPCODE:
2557 for (i=0; i<items-2; i++)
2558 ST(i) = ST(i+2); /* pop first two args */
2559 PUSHMARK(SP);
2560 SP += items - 2;
2561 PUTBACK;
2562 i = call_sv(sv, flags);
2563 SPAGAIN;
2564 EXTEND(SP, 1);
2565 PUSHs(sv_2mortal(newSViv(i)));
2566
2567void
2568call_pv(subname, flags, ...)
2569 char* subname
2570 I32 flags
2571 PREINIT:
2572 I32 i;
2573 PPCODE:
2574 for (i=0; i<items-2; i++)
2575 ST(i) = ST(i+2); /* pop first two args */
2576 PUSHMARK(SP);
2577 SP += items - 2;
2578 PUTBACK;
2579 i = call_pv(subname, flags);
2580 SPAGAIN;
2581 EXTEND(SP, 1);
2582 PUSHs(sv_2mortal(newSViv(i)));
2583
2584void
c06180d6
FC
2585call_argv(subname, flags, ...)
2586 char* subname
2587 I32 flags
2588 PREINIT:
2589 I32 i;
2590 char *tmpary[4];
2591 PPCODE:
2592 for (i=0; i<items-2; i++)
2593 tmpary[i] = SvPV_nolen(ST(i+2)); /* ignore first two args */
2594 tmpary[i] = NULL;
2595 PUTBACK;
2596 i = call_argv(subname, flags, tmpary);
2597 SPAGAIN;
2598 EXTEND(SP, 1);
2599 PUSHs(sv_2mortal(newSViv(i)));
2600
2601void
d1f347d7
DM
2602call_method(methname, flags, ...)
2603 char* methname
2604 I32 flags
2605 PREINIT:
2606 I32 i;
2607 PPCODE:
2608 for (i=0; i<items-2; i++)
2609 ST(i) = ST(i+2); /* pop first two args */
2610 PUSHMARK(SP);
2611 SP += items - 2;
2612 PUTBACK;
2613 i = call_method(methname, flags);
2614 SPAGAIN;
2615 EXTEND(SP, 1);
2616 PUSHs(sv_2mortal(newSViv(i)));
2617
2618void
c0810f8e 2619newCONSTSUB(stash, name, flags, sv)
3453414d
BF
2620 HV* stash
2621 SV* name
2622 I32 flags
8f82b567 2623 SV* sv
c0810f8e
NC
2624 ALIAS:
2625 newCONSTSUB_flags = 1
3453414d 2626 PREINIT:
5f74c55e 2627 CV* mycv = NULL;
9c0a6090
FC
2628 STRLEN len;
2629 const char *pv = SvPV(name, len);
3453414d 2630 PPCODE:
c0810f8e 2631 switch (ix) {
3453414d 2632 case 0:
dc4b20bd 2633 mycv = newCONSTSUB(stash, pv, SvOK(sv) ? SvREFCNT_inc(sv) : NULL);
3453414d
BF
2634 break;
2635 case 1:
dc4b20bd 2636 mycv = newCONSTSUB_flags(
0fc4714a 2637 stash, pv, len, flags | SvUTF8(name), SvOK(sv) ? SvREFCNT_inc(sv) : NULL
8f82b567 2638 );
3453414d
BF
2639 break;
2640 }
2641 EXTEND(SP, 2);
316ebaf2 2642 assert(mycv);
dc4b20bd
CB
2643 PUSHs( CvCONST(mycv) ? &PL_sv_yes : &PL_sv_no );
2644 PUSHs((SV*)CvGV(mycv));
3453414d
BF
2645
2646void
e6066781
BF
2647gv_init_type(namesv, multi, flags, type)
2648 SV* namesv
2649 int multi
2650 I32 flags
2651 int type
2652 PREINIT:
2653 STRLEN len;
2654 const char * const name = SvPV_const(namesv, len);
2655 GV *gv = *(GV**)hv_fetch(PL_defstash, name, len, TRUE);
2656 PPCODE:
2657 if (SvTYPE(gv) == SVt_PVGV)
2658 Perl_croak(aTHX_ "GV is already a PVGV");
04ec7e59 2659 if (multi) flags |= GV_ADDMULTI;
e6066781
BF
2660 switch (type) {
2661 case 0:
2662 gv_init(gv, PL_defstash, name, len, multi);
2663 break;
2664 case 1:
04ec7e59 2665 gv_init_sv(gv, PL_defstash, namesv, flags);
e6066781
BF
2666 break;
2667 case 2:
04ec7e59 2668 gv_init_pv(gv, PL_defstash, name, flags | SvUTF8(namesv));
e6066781
BF
2669 break;
2670 case 3:
04ec7e59 2671 gv_init_pvn(gv, PL_defstash, name, len, flags | SvUTF8(namesv));
e6066781
BF
2672 break;
2673 }
2674 XPUSHs( gv ? (SV*)gv : &PL_sv_undef);
2675
2676void
e6919483
BF
2677gv_fetchmeth_type(stash, methname, type, level, flags)
2678 HV* stash
2679 SV* methname
2680 int type
2681 I32 level
2682 I32 flags
2683 PREINIT:
2684 STRLEN len;
2685 const char * const name = SvPV_const(methname, len);
5f74c55e 2686 GV* gv = NULL;
e6919483
BF
2687 PPCODE:
2688 switch (type) {
2689 case 0:
2690 gv = gv_fetchmeth(stash, name, len, level);
2691 break;
2692 case 1:
2693 gv = gv_fetchmeth_sv(stash, methname, level, flags);
2694 break;
2695 case 2:
2696 gv = gv_fetchmeth_pv(stash, name, level, flags | SvUTF8(methname));
2697 break;
2698 case 3:
2699 gv = gv_fetchmeth_pvn(stash, name, len, level, flags | SvUTF8(methname));
2700 break;
2701 }
2702 XPUSHs( gv ? MUTABLE_SV(gv) : &PL_sv_undef );
2703
2704void
d21989ed
BF
2705gv_fetchmeth_autoload_type(stash, methname, type, level, flags)
2706 HV* stash
2707 SV* methname
2708 int type
2709 I32 level
2710 I32 flags
2711 PREINIT:
2712 STRLEN len;
2713 const char * const name = SvPV_const(methname, len);
5f74c55e 2714 GV* gv = NULL;
d21989ed
BF
2715 PPCODE:
2716 switch (type) {
2717 case 0:
2718 gv = gv_fetchmeth_autoload(stash, name, len, level);
2719 break;
2720 case 1:
2721 gv = gv_fetchmeth_sv_autoload(stash, methname, level, flags);
2722 break;
2723 case 2:
2724 gv = gv_fetchmeth_pv_autoload(stash, name, level, flags | SvUTF8(methname));
2725 break;
2726 case 3:
2727 gv = gv_fetchmeth_pvn_autoload(stash, name, len, level, flags | SvUTF8(methname));
2728 break;
2729 }
2730 XPUSHs( gv ? MUTABLE_SV(gv) : &PL_sv_undef );
2731
2732void
44130a26
BF
2733gv_fetchmethod_flags_type(stash, methname, type, flags)
2734 HV* stash
2735 SV* methname
2736 int type
2737 I32 flags
2738 PREINIT:
5f74c55e 2739 GV* gv = NULL;
44130a26
BF
2740 PPCODE:
2741 switch (type) {
2742 case 0:
2743 gv = gv_fetchmethod_flags(stash, SvPVX_const(methname), flags);
2744 break;
2745 case 1:
2746 gv = gv_fetchmethod_sv_flags(stash, methname, flags);
2747 break;
2748 case 2:
2749 gv = gv_fetchmethod_pv_flags(stash, SvPV_nolen(methname), flags | SvUTF8(methname));
2750 break;
2751 case 3: {
2752 STRLEN len;
2753 const char * const name = SvPV_const(methname, len);
2754 gv = gv_fetchmethod_pvn_flags(stash, name, len, flags | SvUTF8(methname));
2755 break;
2756 }
1665b718
FC
2757 case 4:
2758 gv = gv_fetchmethod_pvn_flags(stash, SvPV_nolen(methname),
2759 flags, SvUTF8(methname));
44130a26 2760 }
5fba3c91 2761 XPUSHs( gv ? (SV*)gv : &PL_sv_undef);
44130a26 2762
5fba3c91 2763void
0eeb01b9 2764gv_autoload_type(stash, methname, type, method)
5fba3c91
BF
2765 HV* stash
2766 SV* methname
2767 int type
2768 I32 method
5fba3c91
BF
2769 PREINIT:
2770 STRLEN len;
2771 const char * const name = SvPV_const(methname, len);
5f74c55e 2772 GV* gv = NULL;
0eeb01b9 2773 I32 flags = method ? GV_AUTOLOAD_ISMETHOD : 0;
5fba3c91
BF
2774 PPCODE:
2775 switch (type) {
2776 case 0:
2777 gv = gv_autoload4(stash, name, len, method);
2778 break;
2779 case 1:
0eeb01b9 2780 gv = gv_autoload_sv(stash, methname, flags);
5fba3c91
BF
2781 break;
2782 case 2:
0eeb01b9 2783 gv = gv_autoload_pv(stash, name, flags | SvUTF8(methname));
5fba3c91
BF
2784 break;
2785 case 3:
0eeb01b9 2786 gv = gv_autoload_pvn(stash, name, len, flags | SvUTF8(methname));
5fba3c91
BF
2787 break;
2788 }
44130a26
BF
2789 XPUSHs( gv ? (SV*)gv : &PL_sv_undef);
2790
16a6e5a4
TC
2791SV *
2792gv_const_sv(SV *name)
2793 PREINIT:
2794 GV *gv;
2795 CODE:
2796 if (SvPOK(name)) {
2797 HV *stash = gv_stashpv("main",0);
2798 HE *he = hv_fetch_ent(stash, name, 0, 0);
2799 gv = (GV *)HeVAL(he);
2800 }
2801 else {
2802 gv = (GV *)name;
2803 }
2804 RETVAL = gv_const_sv(gv);
2805 if (!RETVAL)
2806 XSRETURN_EMPTY;
2807 RETVAL = newSVsv(RETVAL);
2808 OUTPUT:
2809 RETVAL
2810
44130a26 2811void
84c7b88c
BF
2812whichsig_type(namesv, type)
2813 SV* namesv
2814 int type
2815 PREINIT:
2816 STRLEN len;
2817 const char * const name = SvPV_const(namesv, len);
5f74c55e 2818 I32 i = 0;
84c7b88c
BF
2819 PPCODE:
2820 switch (type) {
2821 case 0:
2822 i = whichsig(name);
2823 break;
2824 case 1:
2825 i = whichsig_sv(namesv);
2826 break;
2827 case 2:
2828 i = whichsig_pv(name);
2829 break;
2830 case 3:
2831 i = whichsig_pvn(name, len);
2832 break;
2833 }
2834 XPUSHs(sv_2mortal(newSViv(i)));
2835
2836void
d1f347d7
DM
2837eval_sv(sv, flags)
2838 SV* sv
2839 I32 flags
2840 PREINIT:
2841 I32 i;
2842 PPCODE:
2843 PUTBACK;
2844 i = eval_sv(sv, flags);
2845 SPAGAIN;
2846 EXTEND(SP, 1);
2847 PUSHs(sv_2mortal(newSViv(i)));
2848
b8e65a9b 2849void
d1f347d7
DM
2850eval_pv(p, croak_on_error)
2851 const char* p
2852 I32 croak_on_error
d1f347d7
DM
2853 PPCODE:
2854 PUTBACK;
2855 EXTEND(SP, 1);
2856 PUSHs(eval_pv(p, croak_on_error));
2857
2858void
2859require_pv(pv)
2860 const char* pv
d1f347d7
DM
2861 PPCODE:
2862 PUTBACK;
2863 require_pv(pv);
2864
0ca3a874 2865int
7a646707 2866apitest_exception(throw_e)
0ca3a874
MHM
2867 int throw_e
2868 OUTPUT:
2869 RETVAL
d1f347d7 2870
ef469b03 2871void
7e7a3dfc
GA
2872mycroak(sv)
2873 SV* sv
ef469b03 2874 CODE:
7e7a3dfc
GA
2875 if (SvOK(sv)) {
2876 Perl_croak(aTHX_ "%s", SvPV_nolen(sv));
2877 }
2878 else {
2879 Perl_croak(aTHX_ NULL);
2880 }
5d2b1485
NC
2881
2882SV*
2883strtab()
2884 CODE:
2885 RETVAL = newRV_inc((SV*)PL_strtab);
2886 OUTPUT:
2887 RETVAL
85ce96a1
DM
2888
2889int
2890my_cxt_getint()
2891 CODE:
2892 dMY_CXT;
2893 RETVAL = my_cxt_getint_p(aMY_CXT);
2894 OUTPUT:
2895 RETVAL
2896
2897void
2898my_cxt_setint(i)
2899 int i;
2900 CODE:
2901 dMY_CXT;
2902 my_cxt_setint_p(aMY_CXT_ i);
2903
2904void
9568a123
NC
2905my_cxt_getsv(how)
2906 bool how;
85ce96a1 2907 PPCODE:
85ce96a1 2908 EXTEND(SP, 1);
9568a123 2909 ST(0) = how ? my_cxt_getsv_interp_context() : my_cxt_getsv_interp();
85ce96a1
DM
2910 XSRETURN(1);
2911
2912void
2913my_cxt_setsv(sv)
2914 SV *sv;
2915 CODE:
2916 dMY_CXT;
2917 SvREFCNT_dec(MY_CXT.sv);
2918 my_cxt_setsv_p(sv _aMY_CXT);
2919 SvREFCNT_inc(sv);
34482cd6
NC
2920
2921bool
2922sv_setsv_cow_hashkey_core()
2923
2924bool
2925sv_setsv_cow_hashkey_notcore()
84ac5fd7
NC
2926
2927void
74ee33f2
FC
2928sv_set_deref(SV *sv, SV *sv2, int which)
2929 CODE:
2930 {
2931 STRLEN len;
2932 const char *pv = SvPV(sv2,len);
2933 if (!SvROK(sv)) croak("Not a ref");
2934 sv = SvRV(sv);
2935 switch (which) {
2936 case 0: sv_setsv(sv,sv2); break;
2937 case 1: sv_setpv(sv,pv); break;
2938 case 2: sv_setpvn(sv,pv,len); break;
2939 }
2940 }
2941
2942void
218787bd
VP
2943rmagical_cast(sv, type)
2944 SV *sv;
2945 SV *type;
2946 PREINIT:
2947 struct ufuncs uf;
2948 PPCODE:
2949 if (!SvOK(sv) || !SvROK(sv) || !SvOK(type)) { XSRETURN_UNDEF; }
2950 sv = SvRV(sv);
2951 if (SvTYPE(sv) != SVt_PVHV) { XSRETURN_UNDEF; }
2952 uf.uf_val = rmagical_a_dummy;
2953 uf.uf_set = NULL;
2954 uf.uf_index = 0;
2955 if (SvTRUE(type)) { /* b */
2956 sv_magicext(sv, NULL, PERL_MAGIC_ext, &rmagical_b, NULL, 0);
2957 } else { /* a */
2958 sv_magic(sv, NULL, PERL_MAGIC_uvar, (char *) &uf, sizeof(uf));
2959 }
2960 XSRETURN_YES;
2961
2962void
2963rmagical_flags(sv)
2964 SV *sv;
2965 PPCODE:
2966 if (!SvOK(sv) || !SvROK(sv)) { XSRETURN_UNDEF; }
2967 sv = SvRV(sv);
2968 EXTEND(SP, 3);
2969 mXPUSHu(SvFLAGS(sv) & SVs_GMG);
2970 mXPUSHu(SvFLAGS(sv) & SVs_SMG);
2971 mXPUSHu(SvFLAGS(sv) & SVs_RMG);
2972 XSRETURN(3);
2973
2974void
90d1f214
BM
2975my_caller(level)
2976 I32 level
2977 PREINIT:
2978 const PERL_CONTEXT *cx, *dbcx;
2979 const char *pv;
2980 const GV *gv;
2981 HV *hv;
2982 PPCODE:
2983 cx = caller_cx(level, &dbcx);
2984 EXTEND(SP, 8);
2985
2986 pv = CopSTASHPV(cx->blk_oldcop);
2987 ST(0) = pv ? sv_2mortal(newSVpv(pv, 0)) : &PL_sv_undef;
2988 gv = CvGV(cx->blk_sub.cv);
2989 ST(1) = isGV(gv) ? sv_2mortal(newSVpv(GvNAME(gv), 0)) : &PL_sv_undef;
2990
2991 pv = CopSTASHPV(dbcx->blk_oldcop);
2992 ST(2) = pv ? sv_2mortal(newSVpv(pv, 0)) : &PL_sv_undef;
2993 gv = CvGV(dbcx->blk_sub.cv);
2994 ST(3) = isGV(gv) ? sv_2mortal(newSVpv(GvNAME(gv), 0)) : &PL_sv_undef;
2995
20439bc7
Z
2996 ST(4) = cop_hints_fetch_pvs(cx->blk_oldcop, "foo", 0);
2997 ST(5) = cop_hints_fetch_pvn(cx->blk_oldcop, "foo", 3, 0, 0);
2998 ST(6) = cop_hints_fetch_sv(cx->blk_oldcop,
c2b90b61 2999 sv_2mortal(newSVpvs("foo")), 0, 0);
90d1f214 3000
20439bc7 3001 hv = cop_hints_2hv(cx->blk_oldcop, 0);
90d1f214
BM
3002 ST(7) = hv ? sv_2mortal(newRV_noinc((SV *)hv)) : &PL_sv_undef;
3003
3004 XSRETURN(8);
3005
3006void
f9c17636
MB
3007DPeek (sv)
3008 SV *sv
3009
3010 PPCODE:
5b1f7359 3011 ST (0) = newSVpv (Perl_sv_peek (aTHX_ sv), 0);
f9c17636
MB
3012 XSRETURN (1);
3013
3014void
84ac5fd7
NC
3015BEGIN()
3016 CODE:
3017 sv_inc(get_sv("XS::APItest::BEGIN_called", GV_ADD|GV_ADDMULTI));
3018
3019void
3020CHECK()
3021 CODE:
3022 sv_inc(get_sv("XS::APItest::CHECK_called", GV_ADD|GV_ADDMULTI));
3023
3024void
3025UNITCHECK()
3026 CODE:
0932863f 3027 sv_inc(get_sv("XS::APItest::UNITCHECK_called", GV_ADD|GV_ADDMULTI));
84ac5fd7
NC
3028
3029void
3030INIT()
3031 CODE:
3032 sv_inc(get_sv("XS::APItest::INIT_called", GV_ADD|GV_ADDMULTI));
3033
3034void
3035END()
3036 CODE:
3037 sv_inc(get_sv("XS::APItest::END_called", GV_ADD|GV_ADDMULTI));
30685b56
NC
3038
3039void
3040utf16_to_utf8 (sv, ...)
3041 SV* sv
3042 ALIAS:
3043 utf16_to_utf8_reversed = 1
3044 PREINIT:
3045 STRLEN len;
3046 U8 *source;
3047 SV *dest;
3048 I32 got; /* Gah, badly thought out APIs */
3049 CODE:
a4d7a71b 3050 if (ix) (void)SvPV_force_nolen(sv);
30685b56
NC
3051 source = (U8 *)SvPVbyte(sv, len);
3052 /* Optionally only convert part of the buffer. */
3053 if (items > 1) {
3054 len = SvUV(ST(1));
3055 }
3056 /* Mortalise this right now, as we'll be testing croak()s */
624504c5 3057 dest = sv_2mortal(newSV(len * 2 + 1));
30685b56 3058 if (ix) {
25f2e844 3059 utf16_to_utf8_reversed(source, (U8 *)SvPVX(dest), len, &got);
30685b56 3060 } else {
25f2e844 3061 utf16_to_utf8(source, (U8 *)SvPVX(dest), len, &got);
30685b56
NC
3062 }
3063 SvCUR_set(dest, got);
3064 SvPVX(dest)[got] = '\0';
3065 SvPOK_on(dest);
3066 ST(0) = dest;
3067 XSRETURN(1);
879d0c72 3068
6bd7445c
GG
3069void
3070my_exit(int exitcode)
3071 PPCODE:
3072 my_exit(exitcode);
d97c33b5 3073
209e41dc
NT
3074U8
3075first_byte(sv)
3076 SV *sv
3077 CODE:
3078 char *s;
3079 STRLEN len;
3080 s = SvPVbyte(sv, len);
3081 RETVAL = s[0];
3082 OUTPUT:
3083 RETVAL
3084
d97c33b5
DM
3085I32
3086sv_count()
3087 CODE:
3088 RETVAL = PL_sv_count;
3089 OUTPUT:
3090 RETVAL
13b6b3bc
BM
3091
3092void
3093bhk_record(bool on)
3094 CODE:
3095 dMY_CXT;
3096 MY_CXT.bhk_record = on;
3097 if (on)
3098 av_clear(MY_CXT.bhkav);
65bfe90c 3099
defdfed5 3100void
d9088386
Z
3101test_magic_chain()
3102 PREINIT:
3103 SV *sv;
3104 MAGIC *callmg, *uvarmg;
3105 CODE:
3106 sv = sv_2mortal(newSV(0));
11f9f0ed
NC
3107 if (SvTYPE(sv) >= SVt_PVMG) croak_fail();
3108 if (SvMAGICAL(sv)) croak_fail();
d9088386 3109 sv_magic(sv, &PL_sv_yes, PERL_MAGIC_checkcall, (char*)&callmg, 0);
11f9f0ed
NC
3110 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
3111 if (!SvMAGICAL(sv)) croak_fail();
3112 if (mg_find(sv, PERL_MAGIC_uvar)) croak_fail();
d9088386 3113 callmg = mg_find(sv, PERL_MAGIC_checkcall);
11f9f0ed 3114 if (!callmg) croak_fail();
d9088386 3115 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 3116 croak_fail();
d9088386 3117 sv_magic(sv, &PL_sv_no, PERL_MAGIC_uvar, (char*)&uvarmg, 0);
11f9f0ed
NC
3118 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
3119 if (!SvMAGICAL(sv)) croak_fail();
3120 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
d9088386 3121 uvarmg = mg_find(sv, PERL_MAGIC_uvar);
11f9f0ed 3122 if (!uvarmg) croak_fail();
d9088386 3123 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 3124 croak_fail();
d9088386 3125 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 3126 croak_fail();
d9088386 3127 mg_free_type(sv, PERL_MAGIC_vec);
11f9f0ed
NC
3128 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
3129 if (!SvMAGICAL(sv)) croak_fail();
3130 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
3131 if (mg_find(sv, PERL_MAGIC_uvar) != uvarmg) croak_fail();
d9088386 3132 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 3133 croak_fail();
d9088386 3134 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 3135 croak_fail();
d9088386 3136 mg_free_type(sv, PERL_MAGIC_uvar);
11f9f0ed
NC
3137 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
3138 if (!SvMAGICAL(sv)) croak_fail();
3139 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
3140 if (mg_find(sv, PERL_MAGIC_uvar)) croak_fail();
d9088386 3141 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 3142 croak_fail();
d9088386 3143 sv_magic(sv, &PL_sv_no, PERL_MAGIC_uvar, (char*)&uvarmg, 0);
11f9f0ed
NC
3144 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
3145 if (!SvMAGICAL(sv)) croak_fail();
3146 if (mg_find(sv, PERL_MAGIC_checkcall) != callmg) croak_fail();
d9088386 3147 uvarmg = mg_find(sv, PERL_MAGIC_uvar);
11f9f0ed 3148 if (!uvarmg) croak_fail();
d9088386 3149 if (callmg->mg_obj != &PL_sv_yes || callmg->mg_ptr != (char*)&callmg)
11f9f0ed 3150 croak_fail();
d9088386 3151 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 3152 croak_fail();
d9088386 3153 mg_free_type(sv, PERL_MAGIC_checkcall);
11f9f0ed
NC
3154 if (SvTYPE(sv) < SVt_PVMG) croak_fail();
3155 if (!SvMAGICAL(sv)) croak_fail();
3156 if (mg_find(sv, PERL_MAGIC_uvar) != uvarmg) croak_fail();
3157 if (mg_find(sv, PERL_MAGIC_checkcall)) croak_fail();
d9088386 3158 if (uvarmg->mg_obj != &PL_sv_no || uvarmg->mg_ptr != (char*)&uvarmg)
11f9f0ed 3159 croak_fail();
d9088386 3160 mg_free_type(sv, PERL_MAGIC_uvar);
11f9f0ed
NC
3161 if (SvMAGICAL(sv)) croak_fail();
3162 if (mg_find(sv, PERL_MAGIC_checkcall)) croak_fail();
3163 if (mg_find(sv, PERL_MAGIC_uvar)) croak_fail();
d9088386
Z
3164
3165void
3166test_op_contextualize()
3167 PREINIT:
3168 OP *o;
3169 CODE:
3170 o = newSVOP(OP_CONST, 0, newSViv(0));
3171 o->op_flags &= ~OPf_WANT;
3172 o = op_contextualize(o, G_SCALAR);
3173 if (o->op_type != OP_CONST ||
3174 (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR)
11f9f0ed 3175 croak_fail();
d9088386
Z
3176 op_free(o);
3177 o = newSVOP(OP_CONST, 0, newSViv(0));
3178 o->op_flags &= ~OPf_WANT;
3179 o = op_contextualize(o, G_ARRAY);
3180 if (o->op_type != OP_CONST ||
3181 (o->op_flags & OPf_WANT) != OPf_WANT_LIST)
11f9f0ed 3182 croak_fail();
d9088386
Z
3183 op_free(o);
3184 o = newSVOP(OP_CONST, 0, newSViv(0));
3185 o->op_flags &= ~OPf_WANT;
3186 o = op_contextualize(o, G_VOID);
11f9f0ed 3187 if (o->op_type != OP_NULL) croak_fail();
d9088386
Z
3188 op_free(o);
3189
3190void
3191test_rv2cv_op_cv()
3192 PROTOTYPE:
3193 PREINIT:
7616a0c2 3194 GV *troc_gv;
d9088386
Z
3195 CV *troc_cv;
3196 OP *o;
3197 CODE:
3198 troc_gv = gv_fetchpv("XS::APItest::test_rv2cv_op_cv", 0, SVt_PVGV);
3199 troc_cv = get_cv("XS::APItest::test_rv2cv_op_cv", 0);
d9088386 3200 o = newCVREF(0, newGVOP(OP_GV, 0, troc_gv));
11f9f0ed 3201 if (rv2cv_op_cv(o, 0) != troc_cv) croak_fail();
d9088386 3202 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV) != (CV*)troc_gv)
11f9f0ed 3203 croak_fail();
d9088386 3204 o->op_private |= OPpENTERSUB_AMPER;
11f9f0ed
NC
3205 if (rv2cv_op_cv(o, 0)) croak_fail();
3206 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386 3207 o->op_private &= ~OPpENTERSUB_AMPER;
11f9f0ed
NC
3208 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
3209 if (rv2cv_op_cv(o, RV2CVOPCV_MARK_EARLY) != troc_cv) croak_fail();
3210 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
d9088386
Z
3211 op_free(o);
3212 o = newSVOP(OP_CONST, 0, newSVpv("XS::APItest::test_rv2cv_op_cv", 0));
3213 o->op_private = OPpCONST_BARE;
3214 o = newCVREF(0, o);
11f9f0ed 3215 if (rv2cv_op_cv(o, 0) != troc_cv) croak_fail();
d9088386 3216 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV) != (CV*)troc_gv)
11f9f0ed 3217 croak_fail();
d9088386 3218 o->op_private |= OPpENTERSUB_AMPER;
11f9f0ed
NC
3219 if (rv2cv_op_cv(o, 0)) croak_fail();
3220 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386
Z
3221 op_free(o);
3222 o = newCVREF(0, newSVOP(OP_CONST, 0, newRV_inc((SV*)troc_cv)));
11f9f0ed 3223 if (rv2cv_op_cv(o, 0) != troc_cv) croak_fail();
d9088386 3224 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV) != (CV*)troc_gv)
11f9f0ed 3225 croak_fail();
d9088386 3226 o->op_private |= OPpENTERSUB_AMPER;
11f9f0ed
NC
3227 if (rv2cv_op_cv(o, 0)) croak_fail();
3228 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386 3229 o->op_private &= ~OPpENTERSUB_AMPER;
11f9f0ed
NC
3230 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
3231 if (rv2cv_op_cv(o, RV2CVOPCV_MARK_EARLY) != troc_cv) croak_fail();
3232 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
d9088386
Z
3233 op_free(o);
3234 o = newCVREF(0, newUNOP(OP_RAND, 0, newSVOP(OP_CONST, 0, newSViv(0))));
11f9f0ed
NC
3235 if (rv2cv_op_cv(o, 0)) croak_fail();
3236 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386 3237 o->op_private |= OPpENTERSUB_AMPER;
11f9f0ed
NC
3238 if (rv2cv_op_cv(o, 0)) croak_fail();
3239 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386 3240 o->op_private &= ~OPpENTERSUB_AMPER;
11f9f0ed
NC
3241 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
3242 if (rv2cv_op_cv(o, RV2CVOPCV_MARK_EARLY)) croak_fail();
3243 if (cUNOPx(o)->op_first->op_private & OPpEARLY_CV) croak_fail();
d9088386
Z
3244 op_free(o);
3245 o = newUNOP(OP_RAND, 0, newSVOP(OP_CONST, 0, newSViv(0)));
11f9f0ed
NC
3246 if (rv2cv_op_cv(o, 0)) croak_fail();
3247 if (rv2cv_op_cv(o, RV2CVOPCV_RETURN_NAME_GV)) croak_fail();
d9088386
Z
3248 op_free(o);
3249
3250void
3251test_cv_getset_call_checker()
3252 PREINIT:
3253 CV *troc_cv, *tsh_cv;
3254 Perl_call_checker ckfun;
3255 SV *ckobj;
36791795 3256 U32 ckflags;
d9088386 3257 CODE:
36791795 3258#define check_cc(cv, xckfun, xckobj, xckflags) \
d9088386
Z
3259 do { \
3260 cv_get_call_checker((cv), &ckfun, &ckobj); \
36791795
Z
3261 if (ckfun != (xckfun)) croak_fail_nep(FPTR2DPTR(void *, ckfun), xckfun); \
3262 if (ckobj != (xckobj)) croak_fail_nep(FPTR2DPTR(void *, ckobj), xckobj); \
3263 cv_get_call_checker_flags((cv), CALL_CHECKER_REQUIRE_GV, &ckfun, &ckobj, &ckflags); \
3264 if (ckfun != (xckfun)) croak_fail_nep(FPTR2DPTR(void *, ckfun), xckfun); \
3265 if (ckobj != (xckobj)) croak_fail_nep(FPTR2DPTR(void *, ckobj), xckobj); \
3266 if (ckflags != CALL_CHECKER_REQUIRE_GV) croak_fail_nei(ckflags, CALL_CHECKER_REQUIRE_GV); \
3267 cv_get_call_checker_flags((cv), 0, &ckfun, &ckobj, &ckflags); \
3268 if (ckfun != (xckfun)) croak_fail_nep(FPTR2DPTR(void *, ckfun), xckfun); \
3269 if (ckobj != (xckobj)) croak_fail_nep(FPTR2DPTR(void *, ckobj), xckobj); \
3270 if (ckflags != (xckflags)) croak_fail_nei(ckflags, (xckflags)); \
d9088386
Z
3271 } while(0)
3272 troc_cv = get_cv("XS::APItest::test_rv2cv_op_cv", 0);
3273 tsh_cv = get_cv("XS::APItest::test_savehints", 0);
36791795
Z
3274 check_cc(troc_cv, Perl_ck_entersub_args_proto_or_list, (SV*)troc_cv, 0);
3275 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv, 0);
d9088386
Z
3276 cv_set_call_checker(tsh_cv, Perl_ck_entersub_args_proto_or_list,
3277 &PL_sv_yes);
36791795
Z
3278 check_cc(troc_cv, Perl_ck_entersub_args_proto_or_list, (SV*)troc_cv, 0);
3279 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, &PL_sv_yes, CALL_CHECKER_REQUIRE_GV);
d9088386 3280 cv_set_call_checker(troc_cv, THX_ck_entersub_args_scalars, &PL_sv_no);
36791795
Z
3281 check_cc(troc_cv, THX_ck_entersub_args_scalars, &PL_sv_no, CALL_CHECKER_REQUIRE_GV);
3282 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, &PL_sv_yes, CALL_CHECKER_REQUIRE_GV);
d9088386
Z
3283 cv_set_call_checker(tsh_cv, Perl_ck_entersub_args_proto_or_list,
3284 (SV*)tsh_cv);
36791795
Z
3285 check_cc(troc_cv, THX_ck_entersub_args_scalars, &PL_sv_no, CALL_CHECKER_REQUIRE_GV);
3286 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv, 0);
d9088386
Z
3287 cv_set_call_checker(troc_cv, Perl_ck_entersub_args_proto_or_list,
3288 (SV*)troc_cv);
36791795
Z
3289 check_cc(troc_cv, Perl_ck_entersub_args_proto_or_list, (SV*)troc_cv, 0);
3290 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv, 0);
11f9f0ed
NC
3291 if (SvMAGICAL((SV*)troc_cv) || SvMAGIC((SV*)troc_cv)) croak_fail();
3292 if (SvMAGICAL((SV*)tsh_cv) || SvMAGIC((SV*)tsh_cv)) croak_fail();
36791795
Z
3293 cv_set_call_checker_flags(tsh_cv, Perl_ck_entersub_args_proto_or_list,
3294 &PL_sv_yes, 0);
3295 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, &PL_sv_yes, 0);
3296 cv_set_call_checker_flags(tsh_cv, Perl_ck_entersub_args_proto_or_list,
3297 &PL_sv_yes, CALL_CHECKER_REQUIRE_GV);
3298 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, &PL_sv_yes, CALL_CHECKER_REQUIRE_GV);
3299 cv_set_call_checker_flags(tsh_cv, Perl_ck_entersub_args_proto_or_list,
3300 (SV*)tsh_cv, 0);
3301 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv, 0);
3302 if (SvMAGICAL((SV*)tsh_cv) || SvMAGIC((SV*)tsh_cv)) croak_fail();
3303 cv_set_call_checker_flags(tsh_cv, Perl_ck_entersub_args_proto_or_list,
3304 &PL_sv_yes, CALL_CHECKER_REQUIRE_GV);
3305 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, &PL_sv_yes, CALL_CHECKER_REQUIRE_GV);
3306 cv_set_call_checker_flags(tsh_cv, Perl_ck_entersub_args_proto_or_list,
3307 (SV*)tsh_cv, CALL_CHECKER_REQUIRE_GV);
3308 check_cc(tsh_cv, Perl_ck_entersub_args_proto_or_list, (SV*)tsh_cv, 0);
3309 if (SvMAGICAL((SV*)tsh_cv) || SvMAGIC((SV*)tsh_cv)) croak_fail();
d9088386
Z
3310#undef check_cc
3311
3312void
3313cv_set_call_checker_lists(CV *cv)
3314 CODE:
3315 cv_set_call_checker(cv, THX_ck_entersub_args_lists, &PL_sv_undef);
3316
3317void
3318cv_set_call_checker_scalars(CV *cv)
3319 CODE:
3320 cv_set_call_checker(cv, THX_ck_entersub_args_scalars, &PL_sv_undef);
3321
3322void
3323cv_set_call_checker_proto(CV *cv, SV *proto)
3324 CODE:
3325 if (SvROK(proto))
3326 proto = SvRV(proto);
3327 cv_set_call_checker(cv, Perl_ck_entersub_args_proto, proto);
3328
3329void
3330cv_set_call_checker_proto_or_list(CV *cv, SV *proto)
3331 CODE:
3332 if (SvROK(proto))
3333 proto = SvRV(proto);
3334 cv_set_call_checker(cv, Perl_ck_entersub_args_proto_or_list, proto);
3335
3336void
3337cv_set_call_checker_multi_sum(CV *cv)
3338 CODE:
3339 cv_set_call_checker(cv, THX_ck_entersub_multi_sum, &PL_sv_undef);
3340
3341void
20439bc7
Z
3342test_cophh()
3343 PREINIT:
3344 COPHH *a, *b;
72900640
KW
3345#ifdef EBCDIC
3346 SV* key_sv;
3347 char * key_name;
3348 STRLEN key_len;
3349#endif
20439bc7
Z
3350 CODE:
3351#define check_ph(EXPR) \
3352 do { if((EXPR) != &PL_sv_placeholder) croak("fail"); } while(0)
3353#define check_iv(EXPR, EXPECT) \
3354 do { if(SvIV(EXPR) != (EXPECT)) croak("fail"); } while(0)
3355#define msvpvs(STR) sv_2mortal(newSVpvs(STR))
3356#define msviv(VALUE) sv_2mortal(newSViv(VALUE))
3357 a = cophh_new_empty();
3358 check_ph(cophh_fetch_pvn(a, "foo_1", 5, 0, 0));
3359 check_ph(cophh_fetch_pvs(a, "foo_1", 0));
3360 check_ph(cophh_fetch_pv(a, "foo_1", 0, 0));
3361 check_ph(cophh_fetch_sv(a, msvpvs("foo_1"), 0, 0));
3362 a = cophh_store_pvn(a, "foo_1abc", 5, 0, msviv(111), 0);
3363 a = cophh_store_pvs(a, "foo_2", msviv(222), 0);
3364 a = cophh_store_pv(a, "foo_3", 0, msviv(333), 0);
3365 a = cophh_store_sv(a, msvpvs("foo_4"), 0, msviv(444), 0);
3366 check_iv(cophh_fetch_pvn(a, "foo_1xyz", 5, 0, 0), 111);
3367 check_iv(cophh_fetch_pvs(a, "foo_1", 0), 111);
3368 check_iv(cophh_fetch_pv(a, "foo_1", 0, 0), 111);
3369 check_iv(cophh_fetch_sv(a, msvpvs("foo_1"), 0, 0), 111);
3370 check_iv(cophh_fetch_pvs(a, "foo_2", 0), 222);
3371 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
3372 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
3373 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
3374 b = cophh_copy(a);
3375 b = cophh_store_pvs(b, "foo_1", msviv(1111), 0);
3376 check_iv(cophh_fetch_pvs(a, "foo_1", 0), 111);
3377 check_iv(cophh_fetch_pvs(a, "foo_2", 0), 222);
3378 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
3379 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
3380 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
3381 check_iv(cophh_fetch_pvs(b, "foo_1", 0), 1111);
3382 check_iv(cophh_fetch_pvs(b, "foo_2", 0), 222);
3383 check_iv(cophh_fetch_pvs(b, "foo_3", 0), 333);
3384 check_iv(cophh_fetch_pvs(b, "foo_4", 0), 444);
3385 check_ph(cophh_fetch_pvs(b, "foo_5", 0));
3386 a = cophh_delete_pvn(a, "foo_1abc", 5, 0, 0);
3387 a = cophh_delete_pvs(a, "foo_2", 0);
3388 b = cophh_delete_pv(b, "foo_3", 0, 0);
3389 b = cophh_delete_sv(b, msvpvs("foo_4"), 0, 0);
3390 check_ph(cophh_fetch_pvs(a, "foo_1", 0));
3391 check_ph(cophh_fetch_pvs(a, "foo_2", 0));
3392 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
3393 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
3394 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
3395 check_iv(cophh_fetch_pvs(b, "foo_1", 0), 1111);
3396 check_iv(cophh_fetch_pvs(b, "foo_2", 0), 222);
3397 check_ph(cophh_fetch_pvs(b, "foo_3", 0));
3398 check_ph(cophh_fetch_pvs(b, "foo_4", 0));
3399 check_ph(cophh_fetch_pvs(b, "foo_5", 0));
3400 b = cophh_delete_pvs(b, "foo_3", 0);
3401 b = cophh_delete_pvs(b, "foo_5", 0);
3402 check_iv(cophh_fetch_pvs(b, "foo_1", 0), 1111);
3403 check_iv(cophh_fetch_pvs(b, "foo_2", 0), 222);
3404 check_ph(cophh_fetch_pvs(b, "foo_3", 0));
3405 check_ph(cophh_fetch_pvs(b, "foo_4", 0));
3406 check_ph(cophh_fetch_pvs(b, "foo_5", 0));
3407 cophh_free(b);
3408 check_ph(cophh_fetch_pvs(a, "foo_1", 0));
3409 check_ph(cophh_fetch_pvs(a, "foo_2", 0));
3410 check_iv(cophh_fetch_pvs(a, "foo_3", 0), 333);
3411 check_iv(cophh_fetch_pvs(a, "foo_4", 0), 444);
3412 check_ph(cophh_fetch_pvs(a, "foo_5", 0));
72900640 3413 a = cophh_store_pvs(a, "foo_1", msviv(11111), COPHH_KEY_UTF8);
20439bc7 3414 a = cophh_store_pvs(a, "foo_\xaa", msviv(123), 0);
72900640 3415#ifndef EBCDIC
20439bc7 3416 a = cophh_store_pvs(a, "foo_\xc2\xbb", msviv(456), COPHH_KEY_UTF8);
72900640
KW
3417#else
3418 /* On EBCDIC, we need to translate the UTF-8 in the ASCII test to the
3419 * equivalent UTF-EBCDIC for the code page. This is done at runtime
3420 * (with the helper function in this file). Therefore we can't use
3421 * cophhh_store_pvs(), as we don't have literal string */
3422 key_sv = sv_2mortal(newSVpvs("foo_"));
3423 cat_utf8a2n(key_sv, STR_WITH_LEN("\xc2\xbb"));
3424 key_name = SvPV(key_sv, key_len);
3425 a = cophh_store_pvn(a, key_name, key_len, 0, msviv(456), COPHH_KEY_UTF8);
3426#endif
3427#ifndef EBCDIC
20439bc7 3428 a = cophh_store_pvs(a, "foo_\xc3\x8c", msviv(789), COPHH_KEY_UTF8);
72900640
KW
3429#else
3430 sv_setpvs(key_sv, "foo_");
3431 cat_utf8a2n(key_sv, STR_WITH_LEN("\xc3\x8c"));
3432 key_name = SvPV(key_sv, key_len);
3433 a = cophh_store_pvn(a, key_name, key_len, 0, msviv(789), COPHH_KEY_UTF8);
3434#endif
3435#ifndef EBCDIC
20439bc7 3436 a = cophh_store_pvs(a, "foo_\xd9\xa6", msviv(666), COPHH_KEY_UTF8);
72900640
KW
3437#else
3438 sv_setpvs(key_sv, "foo_");
3439 cat_utf8a2n(key_sv, STR_WITH_LEN("\xd9\xa6"));
3440 key_name = SvPV(key_sv, key_len);
3441 a = cophh_store_pvn(a, key_name, key_len, 0, msviv(666), COPHH_KEY_UTF8);
3442#endif
20439bc7
Z
3443 check_iv(cophh_fetch_pvs(a, "foo_1", 0), 11111);
3444 check_iv(cophh_fetch_pvs(a, "foo_1", COPHH_KEY_UTF8), 11111);
3445 check_iv(cophh_fetch_pvs(a, "foo_\xaa", 0), 123);
72900640 3446#ifndef EBCDIC
20439bc7
Z
3447 check_iv(cophh_fetch_pvs(a, "foo_\xc2\xaa", COPHH_KEY_UTF8), 123);
3448 check_ph(cophh_fetch_pvs(a, "foo_\xc2\xaa", 0));
72900640
KW
3449#else
3450 sv_setpvs(key_sv, "foo_");
3451 cat_utf8a2n(key_sv, STR_WITH_LEN("\xc2\xaa"));
3452 key_name = SvPV(key_sv, key_len);
3453 check_iv(cophh_fetch_pvn(a, key_name, key_len, 0, COPHH_KEY_UTF8), 123);
3454 check_ph(cophh_fetch_pvn(a, key_name, key_len, 0, 0));
3455#endif
20439bc7 3456 check_iv(cophh_fetch_pvs(a, "foo_\xbb", 0), 456);
72900640 3457#ifndef EBCDIC
20439bc7
Z
3458 check_iv(cophh_fetch_pvs(a, "foo_\xc2\xbb", COPHH_KEY_UTF8), 456);
3459 check_ph(cophh_fetch_pvs(a, "foo_\xc2\xbb", 0));
72900640
KW
3460#else
3461 sv_setpvs(key_sv, "foo_");
3462 cat_utf8a2n(key_sv, STR_WITH_LEN("\xc2\xbb"));
3463 key_name = SvPV(key_sv, key_len);
3464 check_iv(cophh_fetch_pvn(a, key_name, key_len, 0, COPHH_KEY_UTF8), 456);
3465 check_ph(cophh_fetch_pvn(a, key_name, key_len, 0, 0));
3466#endif
20439bc7 3467 check_iv(cophh_fetch_pvs(a, "foo_\xcc", 0), 789);
72900640 3468#ifndef EBCDIC
20439bc7
Z
3469 check_iv(cophh_fetch_pvs(a, "foo_\xc3\x8c", COPHH_KEY_UTF8), 789);
3470 check_ph(cophh_fetch_pvs(a, "foo_\xc2\x8c", 0));
72900640
KW
3471#else
3472 sv_setpvs(key_sv, "foo_");
3473 cat_utf8a2n(key_sv, STR_WITH_LEN("\xc3\x8c"));
3474 key_name = SvPV(key_sv, key_len);
3475 check_iv(cophh_fetch_pvn(a, key_name, key_len, 0, COPHH_KEY_UTF8), 789);
3476 check_ph(cophh_fetch_pvn(a, key_name, key_len, 0, 0));
3477#endif
3478#ifndef EBCDIC
20439bc7
Z
3479 check_iv(cophh_fetch_pvs(a, "foo_\xd9\xa6", COPHH_KEY_UTF8), 666);
3480 check_ph(cophh_fetch_pvs(a, "foo_\xd9\xa6", 0));
72900640
KW
3481#else
3482 sv_setpvs(key_sv, "foo_");
3483 cat_utf8a2n(key_sv, STR_WITH_LEN("\xd9\xa6"));
3484 key_name = SvPV(key_sv, key_len);
3485 check_iv(cophh_fetch_pvn(a, key_name, key_len, 0, COPHH_KEY_UTF8), 666);
3486 check_ph(cophh_fetch_pvn(a, key_name, key_len, 0, 0));
3487#endif
3987a177
Z
3488 ENTER;
3489 SAVEFREECOPHH(a);
3490 LEAVE;
20439bc7
Z
3491#undef check_ph
3492#undef check_iv
3493#undef msvpvs
3494#undef msviv
3495
8375c93e
RU
3496void
3497test_coplabel()
3498 PREINIT:
3499 COP *cop;
32708f0b
CB
3500 const char *label;
3501 STRLEN len;
3502 U32 utf8;
8375c93e
RU
3503 CODE:
3504 cop = &PL_compiling;
32708f0b
CB
3505 Perl_cop_store_label(aTHX_ cop, "foo", 3, 0);
3506 label = Perl_cop_fetch_label(aTHX_ cop, &len, &utf8);
083b2a61 3507 if (strNE(label,"foo")) croak("fail # cop_fetch_label label");
32708f0b
CB
3508 if (len != 3) croak("fail # cop_fetch_label len");
3509 if (utf8) croak("fail # cop_fetch_label utf8");
8375c93e 3510 /* SMALL GERMAN UMLAUT A */
a1cd495a 3511 Perl_cop_store_label(aTHX_ cop, "fo\xc3\xa4", 4, SVf_UTF8);
32708f0b 3512 label = Perl_cop_fetch_label(aTHX_ cop, &len, &utf8);
083b2a61 3513 if (strNE(label,"fo\xc3\xa4")) croak("fail # cop_fetch_label label");
72024711 3514 if (len != 4) croak("fail # cop_fetch_label len");
32708f0b 3515 if (!utf8) croak("fail # cop_fetch_label utf8");
8375c93e
RU
3516
3517
20439bc7
Z
3518HV *
3519example_cophh_2hv()
3520 PREINIT:
3521 COPHH *a;
72900640
KW
3522#ifdef EBCDIC
3523 SV* key_sv;
3524 char * key_name;
3525 STRLEN key_len;
3526#endif
20439bc7
Z
3527 CODE:
3528#define msviv(VALUE) sv_2mortal(newSViv(VALUE))
3529 a = cophh_new_empty();
3530 a = cophh_store_pvs(a, "foo_0", msviv(999), 0);
3531 a = cophh_store_pvs(a, "foo_1", msviv(111), 0);
3532 a = cophh_store_pvs(a, "foo_\xaa", msviv(123), 0);
72900640 3533#ifndef EBCDIC
20439bc7 3534 a = cophh_store_pvs(a, "foo_\xc2\xbb", msviv(456), COPHH_KEY_UTF8);
72900640
KW
3535#else
3536 key_sv = sv_2mortal(newSVpvs("foo_"));
3537 cat_utf8a2n(key_sv, STR_WITH_LEN("\xc2\xbb"));
3538 key_name = SvPV(key_sv, key_len);
3539 a = cophh_store_pvn(a, key_name, key_len, 0, msviv(456), COPHH_KEY_UTF8);
3540#endif
3541#ifndef EBCDIC
20439bc7 3542 a = cophh_store_pvs(a, "foo_\xc3\x8c", msviv(789), COPHH_KEY_UTF8);
72900640
KW
3543#else
3544 sv_setpvs(key_sv, "foo_");
3545 cat_utf8a2n(key_sv, STR_WITH_LEN("\xc3\x8c"));
3546 key_name = SvPV(key_sv, key_len);
3547 a = cophh_store_pvn(a, key_name, key_len, 0, msviv(789), COPHH_KEY_UTF8);
3548#endif
3549#ifndef EBCDIC
20439bc7 3550 a = cophh_store_pvs(a, "foo_\xd9\xa6", msviv(666), COPHH_KEY_UTF8);
72900640
KW
3551#else
3552 sv_setpvs(key_sv, "foo_");
3553 cat_utf8a2n(key_sv, STR_WITH_LEN("\xd9\xa6"));
3554 key_name = SvPV(key_sv, key_len);
3555 a = cophh_store_pvn(a, key_name, key_len, 0, msviv(666), COPHH_KEY_UTF8);
3556#endif
20439bc7
Z
3557 a = cophh_delete_pvs(a, "foo_0", 0);
3558 a = cophh_delete_pvs(a, "foo_2", 0);
3559 RETVAL = cophh_2hv(a, 0);
3560 cophh_free(a);
3561#undef msviv
3562 OUTPUT:
3563 RETVAL
3564
3565void
defdfed5
Z
3566test_savehints()
3567 PREINIT:
3568 SV **svp, *sv;
3569 CODE:
3570#define store_hint(KEY, VALUE) \
3571 sv_setiv_mg(*hv_fetchs(GvHV(PL_hintgv), KEY, 1), (VALUE))
3572#define hint_ok(KEY, EXPECT) \
3573 ((svp = hv_fetchs(GvHV(PL_hintgv), KEY, 0)) && \
3574 (sv = *svp) && SvIV(sv) == (EXPECT) && \
20439bc7 3575 (sv = cop_hints_fetch_pvs(&PL_compiling, KEY, 0)) && \
defdfed5
Z
3576 SvIV(sv) == (EXPECT))
3577#define check_hint(KEY, EXPECT) \
11f9f0ed 3578 do { if (!hint_ok(KEY, EXPECT)) croak_fail(); } while(0)
defdfed5
Z
3579 PL_hints |= HINT_LOCALIZE_HH;
3580 ENTER;
3581 SAVEHINTS();
3582 PL_hints &= HINT_INTEGER;
3583 store_hint("t0", 123);
3584 store_hint("t1", 456);
11f9f0ed 3585 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
3586 check_hint("t0", 123); check_hint("t1", 456);
3587 ENTER;
3588 SAVEHINTS();
11f9f0ed 3589 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
3590 check_hint("t0", 123); check_hint("t1", 456);
3591 PL_hints |= HINT_INTEGER;
3592 store_hint("t0", 321);
11f9f0ed 3593 if (!(PL_hints & HINT_INTEGER)) croak_fail();
defdfed5
Z
3594 check_hint("t0", 321); check_hint("t1", 456);
3595 LEAVE;
11f9f0ed 3596 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
3597 check_hint("t0", 123); check_hint("t1", 456);
3598 ENTER;
3599 SAVEHINTS();
11f9f0ed 3600 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
3601 check_hint("t0", 123); check_hint("t1", 456);
3602 store_hint("t1", 654);
11f9f0ed 3603 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
3604 check_hint("t0", 123); check_hint("t1", 654);
3605 LEAVE;
11f9f0ed 3606 if (PL_hints & HINT_INTEGER) croak_fail();
defdfed5
Z
3607 check_hint("t0", 123); check_hint("t1", 456);
3608 LEAVE;
3609#undef store_hint
3610#undef hint_ok
3611#undef check_hint
3612
3613void
3614test_copyhints()
3615 PREINIT:
3616 HV *a, *b;
3617 CODE:
3618 PL_hints |= HINT_LOCALIZE_HH;
3619 ENTER;
3620 SAVEHINTS();
3621 sv_setiv_mg(*hv_fetchs(GvHV(PL_hintgv), "t0", 1), 123);
20439bc7
Z
3622 if (SvIV(cop_hints_fetch_pvs(&PL_compiling, "t0", 0)) != 123)
3623 croak_fail();
defdfed5
Z
3624 a = newHVhv(GvHV(PL_hintgv));
3625 sv_2mortal((SV*)a);
3626 sv_setiv_mg(*hv_fetchs(a, "t0", 1), 456);
20439bc7
Z
3627 if (SvIV(cop_hints_fetch_pvs(&PL_compiling, "t0", 0)) != 123)
3628 croak_fail();
defdfed5
Z
3629 b = hv_copy_hints_hv(a);
3630 sv_2mortal((SV*)b);
3631 sv_setiv_mg(*hv_fetchs(b, "t0", 1), 789);
20439bc7
Z
3632 if (SvIV(cop_hints_fetch_pvs(&PL_compiling, "t0", 0)) != 789)
3633 croak_fail();
defdfed5
Z
3634 LEAVE;
3635
201c7e1f 3636void
2fcb4757
Z
3637test_op_list()
3638 PREINIT:
3639 OP *a;
3640 CODE:
3641#define iv_op(iv) newSVOP(OP_CONST, 0, newSViv(iv))
3642#define check_op(o, expect) \
3643 do { \
083b2a61 3644 if (strNE(test_op_list_describe(o), (expect))) \
2e66fe90 3645 croak("fail %s %s", test_op_list_describe(o), (expect)); \
2fcb4757
Z
3646 } while(0)
3647 a = op_append_elem(OP_LIST, NULL, NULL);
3648 check_op(a, "");
3649 a = op_append_elem(OP_LIST, iv_op(1), a);
3650 check_op(a, "const(1).");
3651 a = op_append_elem(OP_LIST, NULL, a);
3652 check_op(a, "const(1).");
3653 a = op_append_elem(OP_LIST, a, iv_op(2));
3654 check_op(a, "list[pushmark.const(1).const(2).]");
3655 a = op_append_elem(OP_LIST, a, iv_op(3));
3656 check_op(a, "list[pushmark.const(1).const(2).const(3).]");
3657 a = op_append_elem(OP_LIST, a, NULL);
3658 check_op(a, "list[pushmark.const(1).const(2).const(3).]");
3659 a = op_append_elem(OP_LIST, NULL, a);
3660 check_op(a, "list[pushmark.const(1).const(2).const(3).]");
3661 a = op_append_elem(OP_LIST, iv_op(4), a);
3662 check_op(a, "list[pushmark.const(4)."
3663 "list[pushmark.const(1).const(2).const(3).]]");
3664 a = op_append_elem(OP_LIST, a, iv_op(5));
3665 check_op(a, "list[pushmark.const(4)."
3666 "list[pushmark.const(1).const(2).const(3).]const(5).]");
3667 a = op_append_elem(OP_LIST, a,
3668 op_append_elem(OP_LIST, iv_op(7), iv_op(6)));
3669 check_op(a, "list[pushmark.const(4)."
3670 "list[pushmark.const(1).const(2).const(3).]const(5)."
3671 "list[pushmark.const(7).const(6).]]");
3672 op_free(a);
3673 a = op_append_elem(OP_LINESEQ, iv_op(1), iv_op(2));
3674 check_op(a, "lineseq[const(1).const(2).]");
3675 a = op_append_elem(OP_LINESEQ, a, iv_op(3));
3676 check_op(a, "lineseq[const(1).const(2).const(3).]");
3677 op_free(a);
3678 a = op_append_elem(OP_LINESEQ,
3679 op_append_elem(OP_LIST, iv_op(1), iv_op(2)),
3680 iv_op(3));
3681 check_op(a, "lineseq[list[pushmark.const(1).const(2).]const(3).]");
3682 op_free(a);
3683 a = op_prepend_elem(OP_LIST, NULL, NULL);
3684 check_op(a, "");
3685 a = op_prepend_elem(OP_LIST, a, iv_op(1));
3686 check_op(a, "const(1).");
3687 a = op_prepend_elem(OP_LIST, a, NULL);
3688 check_op(a, "const(1).");
3689 a = op_prepend_elem(OP_LIST, iv_op(2), a);
3690 check_op(a, "list[pushmark.const(2).const(1).]");
3691 a = op_prepend_elem(OP_LIST, iv_op(3), a);
3692 check_op(a, "list[pushmark.const(3).const(2).const(1).]");
3693 a = op_prepend_elem(OP_LIST, NULL, a);
3694 check_op(a, "list[pushmark.const(3).const(2).const(1).]");
3695 a = op_prepend_elem(OP_LIST, a, NULL);
3696 check_op(a, "list[pushmark.const(3).const(2).const(1).]");
3697 a = op_prepend_elem(OP_LIST, a, iv_op(4));
3698 check_op(a, "list[pushmark."
3699 "list[pushmark.const(3).const(2).const(1).]const(4).]");
3700 a = op_prepend_elem(OP_LIST, iv_op(5), a);
3701 check_op(a, "list[pushmark.const(5)."
3702 "list[pushmark.const(3).const(2).const(1).]const(4).]");
3703 a = op_prepend_elem(OP_LIST,
3704 op_prepend_elem(OP_LIST, iv_op(6), iv_op(7)), a);
3705 check_op(a, "list[pushmark.list[pushmark.const(6).const(7).]const(5)."
3706 "list[pushmark.const(3).const(2).const(1).]const(4).]");
3707 op_free(a);
3708 a = op_prepend_elem(OP_LINESEQ, iv_op(2), iv_op(1));
3709 check_op(a, "lineseq[const(2).const(1).]");
3710 a = op_prepend_elem(OP_LINESEQ, iv_op(3), a);
3711 check_op(a, "lineseq[const(3).const(2).const(1).]");
3712 op_free(a);
3713 a = op_prepend_elem(OP_LINESEQ, iv_op(3),
3714 op_prepend_elem(OP_LIST, iv_op(2), iv_op(1)));
3715 check_op(a, "lineseq[const(3).list[pushmark.const(2).const(1).]]");
3716 op_free(a);
3717 a = op_append_list(OP_LINESEQ, NULL, NULL);
3718 check_op(a, "");
3719 a = op_append_list(OP_LINESEQ, iv_op(1), a);
3720 check_op(a, "const(1).");
3721 a = op_append_list(OP_LINESEQ, NULL, a);
3722 check_op(a, "const(1).");
3723 a = op_append_list(OP_LINESEQ, a, iv_op(2));
3724 check_op(a, "lineseq[const(1).const(2).]");
3725 a = op_append_list(OP_LINESEQ, a, iv_op(3));
3726 check_op(a, "lineseq[const(1).const(2).const(3).]");
3727 a = op_append_list(OP_LINESEQ, iv_op(4), a);
3728 check_op(a, "lineseq[const(4).const(1).const(2).const(3).]");
3729 a = op_append_list(OP_LINESEQ, a, NULL);
3730 check_op(a, "lineseq[const(4).const(1).const(2).const(3).]");
3731 a = op_append_list(OP_LINESEQ, NULL, a);
3732 check_op(a, "lineseq[const(4).const(1).const(2).const(3).]");
3733 a = op_append_list(OP_LINESEQ, a,
3734 op_append_list(OP_LINESEQ, iv_op(5), iv_op(6)));
3735 check_op(a, "lineseq[const(4).const(1).const(2).const(3)."
3736 "const(5).const(6).]");
3737 op_free(a);
3738 a = op_append_list(OP_LINESEQ,
3739 op_append_list(OP_LINESEQ, iv_op(1), iv_op(2)),
3740 op_append_list(OP_LIST, iv_op(3), iv_op(4)));
3741 check_op(a, "lineseq[const(1).const(2)."
3742 "list[pushmark.const(3).const(4).]]");
3743 op_free(a);
3744 a = op_append_list(OP_LINESEQ,
3745 op_append_list(OP_LIST, iv_op(1), iv_op(2)),
3746 op_append_list(OP_LINESEQ, iv_op(3), iv_op(4)));
3747 check_op(a, "lineseq[list[pushmark.const(1).const(2).]"
3748 "const(3).const(4).]");
3749 op_free(a);
2fcb4757
Z
3750#undef check_op
3751
3752void
5983a79d
BM
3753test_op_linklist ()
3754 PREINIT:
3755 OP *o;
3756 CODE:
3757#define check_ll(o, expect) \
3758 STMT_START { \
2e66fe90
FC
3759 if (strNE(test_op_linklist_describe(o), (expect))) \
3760 croak("fail %s %s", test_op_linklist_describe(o), (expect)); \
5983a79d
BM
3761 } STMT_END
3762 o = iv_op(1);
3763 check_ll(o, ".const1");
3764 op_free(o);
3765
3766 o = mkUNOP(OP_NOT, iv_op(1));
3767 check_ll(o, ".const1.not");
3768 op_free(o);
3769
3770 o = mkUNOP(OP_NOT, mkUNOP(OP_NEGATE, iv_op(1)));
3771 check_ll(o, ".const1.negate.not");
3772 op_free(o);
3773
3774 o = mkBINOP(OP_ADD, iv_op(1), iv_op(2));
3775 check_ll(o, ".const1.const2.add");
3776 op_free(o);
3777
3778 o = mkBINOP(OP_ADD, mkUNOP(OP_NOT, iv_op(1)), iv_op(2));
3779 check_ll(o, ".const1.not.const2.add");
3780 op_free(o);
3781
3782 o = mkUNOP(OP_NOT, mkBINOP(OP_ADD, iv_op(1), iv_op(2)));
3783 check_ll(o, ".const1.const2.add.not");
3784 op_free(o);
3785
3786 o = mkLISTOP(OP_LINESEQ, iv_op(1), iv_op(2), iv_op(3));
3787 check_ll(o, ".const1.const2.const3.lineseq");
3788 op_free(o);
3789
3790 o = mkLISTOP(OP_LINESEQ,
3791 mkBINOP(OP_ADD, iv_op(1), iv_op(2)),
3792 mkUNOP(OP_NOT, iv_op(3)),
3793 mkLISTOP(OP_SUBSTR, iv_op(4), iv_op(5), iv_op(6)));
3794 check_ll(o, ".const1.const2.add.const3.not"
3795 ".const4.const5.const6.substr.lineseq");
3796 op_free(o);
3797
3798 o = mkBINOP(OP_ADD, iv_op(1), iv_op(2));
3799 LINKLIST(o);
3800 o = mkBINOP(OP_SUBTRACT, o, iv_op(3));
3801 check_ll(o, ".const1.const2.add.const3.subtract");
3802 op_free(o);
3803#undef check_ll
3804#undef iv_op
3805
3806void
201c7e1f
FR
3807peep_enable ()
3808 PREINIT:
3809 dMY_CXT;
3810 CODE:
3811 av_clear(MY_CXT.peep_recorder);
3812 av_clear(MY_CXT.rpeep_recorder);
3813 MY_CXT.peep_recording = 1;
3814
3815void
3816peep_disable ()
3817 PREINIT:
3818 dMY_CXT;
3819 CODE:
3820 MY_CXT.peep_recording = 0;
3821
3822SV *
3823peep_record ()
3824 PREINIT