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