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