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