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