This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
xs_version_bootcheck() is an implementation detail, rather than a public API.
[perl5.git] / ext / XS-APItest / APItest.xs
CommitLineData
6a93a7e5 1#define PERL_IN_XS_APITEST
3e61d65a
JH
2#include "EXTERN.h"
3#include "perl.h"
4#include "XSUB.h"
5
36c2b1d0
NC
6typedef SV *SVREF;
7typedef PTR_TBL_t *XS__APItest__PtrTable;
85ce96a1
DM
8
9/* for my_cxt tests */
10
11#define MY_CXT_KEY "XS::APItest::_guts" XS_VERSION
12
13typedef struct {
14 int i;
15 SV *sv;
03569ecf
BM
16 GV *cscgv;
17 AV *cscav;
13b6b3bc
BM
18 AV *bhkav;
19 bool bhk_record;
201c7e1f
FR
20 peep_t orig_peep;
21 peep_t orig_rpeep;
22 int peep_recording;
23 AV *peep_recorder;
24 AV *rpeep_recorder;
85ce96a1
DM
25} my_cxt_t;
26
27START_MY_CXT
28
29/* indirect functions to test the [pa]MY_CXT macros */
f16dd614 30
85ce96a1
DM
31int
32my_cxt_getint_p(pMY_CXT)
33{
34 return MY_CXT.i;
35}
f16dd614 36
85ce96a1
DM
37void
38my_cxt_setint_p(pMY_CXT_ int i)
39{
40 MY_CXT.i = i;
41}
f16dd614
DM
42
43SV*
9568a123 44my_cxt_getsv_interp_context(void)
f16dd614 45{
f16dd614
DM
46 dTHX;
47 dMY_CXT_INTERP(my_perl);
9568a123
NC
48 return MY_CXT.sv;
49}
50
51SV*
52my_cxt_getsv_interp(void)
53{
f16dd614 54 dMY_CXT;
f16dd614
DM
55 return MY_CXT.sv;
56}
57
85ce96a1
DM
58void
59my_cxt_setsv_p(SV* sv _pMY_CXT)
60{
61 MY_CXT.sv = sv;
62}
63
64
9b5c3821 65/* from exception.c */
7a646707 66int apitest_exception(int);
0314122a 67
ff66e713
SH
68/* from core_or_not.inc */
69bool sv_setsv_cow_hashkey_core(void);
70bool sv_setsv_cow_hashkey_notcore(void);
71
2dc92170
NC
72/* A routine to test hv_delayfree_ent
73 (which itself is tested by testing on hv_free_ent */
74
75typedef void (freeent_function)(pTHX_ HV *, register HE *);
76
77void
78test_freeent(freeent_function *f) {
79 dTHX;
80 dSP;
81 HV *test_hash = newHV();
82 HE *victim;
83 SV *test_scalar;
84 U32 results[4];
85 int i;
86
8afd2d2e
NC
87#ifdef PURIFY
88 victim = (HE*)safemalloc(sizeof(HE));
89#else
2dc92170
NC
90 /* Storing then deleting something should ensure that a hash entry is
91 available. */
92 hv_store(test_hash, "", 0, &PL_sv_yes, 0);
93 hv_delete(test_hash, "", 0, 0);
94
95 /* We need to "inline" new_he here as it's static, and the functions we
96 test expect to be able to call del_HE on the HE */
6a93a7e5 97 if (!PL_body_roots[HE_SVSLOT])
2dc92170 98 croak("PL_he_root is 0");
8a722a80 99 victim = (HE*) PL_body_roots[HE_SVSLOT];
6a93a7e5 100 PL_body_roots[HE_SVSLOT] = HeNEXT(victim);
8afd2d2e 101#endif
2dc92170
NC
102
103 victim->hent_hek = Perl_share_hek(aTHX_ "", 0, 0);
104
105 test_scalar = newSV(0);
106 SvREFCNT_inc(test_scalar);
de616631 107 HeVAL(victim) = test_scalar;
2dc92170
NC
108
109 /* Need this little game else we free the temps on the return stack. */
110 results[0] = SvREFCNT(test_scalar);
111 SAVETMPS;
112 results[1] = SvREFCNT(test_scalar);
113 f(aTHX_ test_hash, victim);
114 results[2] = SvREFCNT(test_scalar);
115 FREETMPS;
116 results[3] = SvREFCNT(test_scalar);
117
118 i = 0;
119 do {
120 mPUSHu(results[i]);
121 } while (++i < sizeof(results)/sizeof(results[0]));
122
123 /* Goodbye to our extra reference. */
124 SvREFCNT_dec(test_scalar);
125}
126
b54b4831
NC
127
128static I32
53c40a8f
NC
129bitflip_key(pTHX_ IV action, SV *field) {
130 MAGIC *mg = mg_find(field, PERL_MAGIC_uvar);
131 SV *keysv;
132 if (mg && (keysv = mg->mg_obj)) {
133 STRLEN len;
134 const char *p = SvPV(keysv, len);
135
136 if (len) {
137 SV *newkey = newSV(len);
138 char *new_p = SvPVX(newkey);
139
140 if (SvUTF8(keysv)) {
141 const char *const end = p + len;
142 while (p < end) {
143 STRLEN len;
a75fcbca
SP
144 UV chr = utf8_to_uvuni((U8 *)p, &len);
145 new_p = (char *)uvuni_to_utf8((U8 *)new_p, chr ^ 32);
53c40a8f
NC
146 p += len;
147 }
148 SvUTF8_on(newkey);
149 } else {
150 while (len--)
151 *new_p++ = *p++ ^ 32;
152 }
153 *new_p = '\0';
154 SvCUR_set(newkey, SvCUR(keysv));
155 SvPOK_on(newkey);
156
157 mg->mg_obj = newkey;
158 }
159 }
160 return 0;
161}
162
163static I32
b54b4831
NC
164rot13_key(pTHX_ IV action, SV *field) {
165 MAGIC *mg = mg_find(field, PERL_MAGIC_uvar);
166 SV *keysv;
167 if (mg && (keysv = mg->mg_obj)) {
168 STRLEN len;
169 const char *p = SvPV(keysv, len);
170
171 if (len) {
172 SV *newkey = newSV(len);
173 char *new_p = SvPVX(newkey);
174
175 /* There's a deliberate fencepost error here to loop len + 1 times
176 to copy the trailing \0 */
177 do {
178 char new_c = *p++;
179 /* Try doing this cleanly and clearly in EBCDIC another way: */
180 switch (new_c) {
181 case 'A': new_c = 'N'; break;
182 case 'B': new_c = 'O'; break;
183 case 'C': new_c = 'P'; break;
184 case 'D': new_c = 'Q'; break;
185 case 'E': new_c = 'R'; break;
186 case 'F': new_c = 'S'; break;
187 case 'G': new_c = 'T'; break;
188 case 'H': new_c = 'U'; break;
189 case 'I': new_c = 'V'; break;
190 case 'J': new_c = 'W'; break;
191 case 'K': new_c = 'X'; break;
192 case 'L': new_c = 'Y'; break;
193 case 'M': new_c = 'Z'; break;
194 case 'N': new_c = 'A'; break;
195 case 'O': new_c = 'B'; break;
196 case 'P': new_c = 'C'; break;
197 case 'Q': new_c = 'D'; break;
198 case 'R': new_c = 'E'; break;
199 case 'S': new_c = 'F'; break;
200 case 'T': new_c = 'G'; break;
201 case 'U': new_c = 'H'; break;
202 case 'V': new_c = 'I'; break;
203 case 'W': new_c = 'J'; break;
204 case 'X': new_c = 'K'; break;
205 case 'Y': new_c = 'L'; break;
206 case 'Z': new_c = 'M'; break;
207 case 'a': new_c = 'n'; break;
208 case 'b': new_c = 'o'; break;
209 case 'c': new_c = 'p'; break;
210 case 'd': new_c = 'q'; break;
211 case 'e': new_c = 'r'; break;
212 case 'f': new_c = 's'; break;
213 case 'g': new_c = 't'; break;
214 case 'h': new_c = 'u'; break;
215 case 'i': new_c = 'v'; break;
216 case 'j': new_c = 'w'; break;
217 case 'k': new_c = 'x'; break;
218 case 'l': new_c = 'y'; break;
219 case 'm': new_c = 'z'; break;
220 case 'n': new_c = 'a'; break;
221 case 'o': new_c = 'b'; break;
222 case 'p': new_c = 'c'; break;
223 case 'q': new_c = 'd'; break;
224 case 'r': new_c = 'e'; break;
225 case 's': new_c = 'f'; break;
226 case 't': new_c = 'g'; break;
227 case 'u': new_c = 'h'; break;
228 case 'v': new_c = 'i'; break;
229 case 'w': new_c = 'j'; break;
230 case 'x': new_c = 'k'; break;
231 case 'y': new_c = 'l'; break;
232 case 'z': new_c = 'm'; break;
233 }
234 *new_p++ = new_c;
235 } while (len--);
236 SvCUR_set(newkey, SvCUR(keysv));
237 SvPOK_on(newkey);
238 if (SvUTF8(keysv))
239 SvUTF8_on(newkey);
240
241 mg->mg_obj = newkey;
242 }
243 }
244 return 0;
245}
246
218787bd
VP
247STATIC I32
248rmagical_a_dummy(pTHX_ IV idx, SV *sv) {
249 return 0;
250}
251
252STATIC MGVTBL rmagical_b = { 0 };
253
03569ecf 254STATIC void
13b6b3bc 255blockhook_csc_start(pTHX_ int full)
03569ecf
BM
256{
257 dMY_CXT;
258 AV *const cur = GvAV(MY_CXT.cscgv);
259
260 SAVEGENERICSV(GvAV(MY_CXT.cscgv));
261
262 if (cur) {
263 I32 i;
d024465f 264 AV *const new_av = newAV();
03569ecf
BM
265
266 for (i = 0; i <= av_len(cur); i++) {
d024465f 267 av_store(new_av, i, newSVsv(*av_fetch(cur, i, 0)));
03569ecf
BM
268 }
269
d024465f 270 GvAV(MY_CXT.cscgv) = new_av;
03569ecf
BM
271 }
272}
273
274STATIC void
13b6b3bc 275blockhook_csc_pre_end(pTHX_ OP **o)
03569ecf
BM
276{
277 dMY_CXT;
278
279 /* if we hit the end of a scope we missed the start of, we need to
280 * unconditionally clear @CSC */
52db365a 281 if (GvAV(MY_CXT.cscgv) == MY_CXT.cscav && MY_CXT.cscav) {
03569ecf 282 av_clear(MY_CXT.cscav);
52db365a 283 }
03569ecf
BM
284
285}
286
13b6b3bc
BM
287STATIC void
288blockhook_test_start(pTHX_ int full)
289{
290 dMY_CXT;
291 AV *av;
292
293 if (MY_CXT.bhk_record) {
294 av = newAV();
295 av_push(av, newSVpvs("start"));
296 av_push(av, newSViv(full));
297 av_push(MY_CXT.bhkav, newRV_noinc(MUTABLE_SV(av)));
298 }
299}
300
301STATIC void
302blockhook_test_pre_end(pTHX_ OP **o)
303{
304 dMY_CXT;
305
306 if (MY_CXT.bhk_record)
307 av_push(MY_CXT.bhkav, newSVpvs("pre_end"));
308}
309
310STATIC void
311blockhook_test_post_end(pTHX_ OP **o)
312{
313 dMY_CXT;
314
315 if (MY_CXT.bhk_record)
316 av_push(MY_CXT.bhkav, newSVpvs("post_end"));
317}
318
319STATIC void
320blockhook_test_eval(pTHX_ OP *const o)
321{
322 dMY_CXT;
323 AV *av;
324
325 if (MY_CXT.bhk_record) {
326 av = newAV();
327 av_push(av, newSVpvs("eval"));
328 av_push(av, newSVpv(OP_NAME(o), 0));
329 av_push(MY_CXT.bhkav, newRV_noinc(MUTABLE_SV(av)));
330 }
331}
332
333STATIC BHK bhk_csc, bhk_test;
334
201c7e1f
FR
335STATIC void
336my_peep (pTHX_ OP *o)
337{
338 dMY_CXT;
339
340 if (!o)
341 return;
342
343 MY_CXT.orig_peep(aTHX_ o);
344
345 if (!MY_CXT.peep_recording)
346 return;
347
348 for (; o; o = o->op_next) {
349 if (o->op_type == OP_CONST && cSVOPx_sv(o) && SvPOK(cSVOPx_sv(o))) {
350 av_push(MY_CXT.peep_recorder, newSVsv(cSVOPx_sv(o)));
351 }
352 }
353}
354
355STATIC void
356my_rpeep (pTHX_ OP *o)
357{
358 dMY_CXT;
359
360 if (!o)
361 return;
362
363 MY_CXT.orig_rpeep(aTHX_ o);
364
365 if (!MY_CXT.peep_recording)
366 return;
367
368 for (; o; o = o->op_next) {
369 if (o->op_type == OP_CONST && cSVOPx_sv(o) && SvPOK(cSVOPx_sv(o))) {
370 av_push(MY_CXT.rpeep_recorder, newSVsv(cSVOPx_sv(o)));
371 }
372 }
373}
374
83f8bb56
Z
375/** RPN keyword parser **/
376
377#define sv_is_glob(sv) (SvTYPE(sv) == SVt_PVGV)
378#define sv_is_regexp(sv) (SvTYPE(sv) == SVt_REGEXP)
379#define sv_is_string(sv) \
380 (!sv_is_glob(sv) && !sv_is_regexp(sv) && \
381 (SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK|SVp_IOK|SVp_NOK|SVp_POK)))
382
383static SV *hintkey_rpn_sv, *hintkey_calcrpn_sv, *hintkey_stufftest_sv;
07ffcb73 384static SV *hintkey_swaptwostmts_sv, *hintkey_looprest_sv;
83f8bb56
Z
385static int (*next_keyword_plugin)(pTHX_ char *, STRLEN, OP **);
386
387/* low-level parser helpers */
388
389#define PL_bufptr (PL_parser->bufptr)
390#define PL_bufend (PL_parser->bufend)
391
392/* RPN parser */
393
394#define parse_var() THX_parse_var(aTHX)
395static OP *THX_parse_var(pTHX)
396{
397 char *s = PL_bufptr;
398 char *start = s;
399 PADOFFSET varpos;
400 OP *padop;
401 if(*s != '$') croak("RPN syntax error");
402 while(1) {
403 char c = *++s;
404 if(!isALNUM(c)) break;
405 }
406 if(s-start < 2) croak("RPN syntax error");
407 lex_read_to(s);
408 {
409 /* because pad_findmy() doesn't really use length yet */
410 SV *namesv = sv_2mortal(newSVpvn(start, s-start));
411 varpos = pad_findmy(SvPVX(namesv), s-start, 0);
412 }
413 if(varpos == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(varpos))
414 croak("RPN only supports \"my\" variables");
415 padop = newOP(OP_PADSV, 0);
416 padop->op_targ = varpos;
417 return padop;
418}
419
420#define push_rpn_item(o) \
421 (tmpop = (o), tmpop->op_sibling = stack, stack = tmpop)
422#define pop_rpn_item() \
423 (!stack ? (croak("RPN stack underflow"), (OP*)NULL) : \
424 (tmpop = stack, stack = stack->op_sibling, \
425 tmpop->op_sibling = NULL, tmpop))
426
427#define parse_rpn_expr() THX_parse_rpn_expr(aTHX)
428static OP *THX_parse_rpn_expr(pTHX)
429{
430 OP *stack = NULL, *tmpop;
431 while(1) {
432 I32 c;
433 lex_read_space(0);
434 c = lex_peek_unichar(0);
435 switch(c) {
436 case /*(*/')': case /*{*/'}': {
437 OP *result = pop_rpn_item();
438 if(stack) croak("RPN expression must return a single value");
439 return result;
440 } break;
441 case '0': case '1': case '2': case '3': case '4':
442 case '5': case '6': case '7': case '8': case '9': {
443 UV val = 0;
444 do {
445 lex_read_unichar(0);
446 val = 10*val + (c - '0');
447 c = lex_peek_unichar(0);
448 } while(c >= '0' && c <= '9');
449 push_rpn_item(newSVOP(OP_CONST, 0, newSVuv(val)));
450 } break;
451 case '$': {
452 push_rpn_item(parse_var());
453 } break;
454 case '+': {
455 OP *b = pop_rpn_item();
456 OP *a = pop_rpn_item();
457 lex_read_unichar(0);
458 push_rpn_item(newBINOP(OP_I_ADD, 0, a, b));
459 } break;
460 case '-': {
461 OP *b = pop_rpn_item();
462 OP *a = pop_rpn_item();
463 lex_read_unichar(0);
464 push_rpn_item(newBINOP(OP_I_SUBTRACT, 0, a, b));
465 } break;
466 case '*': {
467 OP *b = pop_rpn_item();
468 OP *a = pop_rpn_item();
469 lex_read_unichar(0);
470 push_rpn_item(newBINOP(OP_I_MULTIPLY, 0, a, b));
471 } break;
472 case '/': {
473 OP *b = pop_rpn_item();
474 OP *a = pop_rpn_item();
475 lex_read_unichar(0);
476 push_rpn_item(newBINOP(OP_I_DIVIDE, 0, a, b));
477 } break;
478 case '%': {
479 OP *b = pop_rpn_item();
480 OP *a = pop_rpn_item();
481 lex_read_unichar(0);
482 push_rpn_item(newBINOP(OP_I_MODULO, 0, a, b));
483 } break;
484 default: {
485 croak("RPN syntax error");
486 } break;
487 }
488 }
489}
490
491#define parse_keyword_rpn() THX_parse_keyword_rpn(aTHX)
492static OP *THX_parse_keyword_rpn(pTHX)
493{
494 OP *op;
495 lex_read_space(0);
496 if(lex_peek_unichar(0) != '('/*)*/)
497 croak("RPN expression must be parenthesised");
498 lex_read_unichar(0);
499 op = parse_rpn_expr();
500 if(lex_peek_unichar(0) != /*(*/')')
501 croak("RPN expression must be parenthesised");
502 lex_read_unichar(0);
503 return op;
504}
505
506#define parse_keyword_calcrpn() THX_parse_keyword_calcrpn(aTHX)
507static OP *THX_parse_keyword_calcrpn(pTHX)
508{
509 OP *varop, *exprop;
510 lex_read_space(0);
511 varop = parse_var();
512 lex_read_space(0);
513 if(lex_peek_unichar(0) != '{'/*}*/)
514 croak("RPN expression must be braced");
515 lex_read_unichar(0);
516 exprop = parse_rpn_expr();
517 if(lex_peek_unichar(0) != /*{*/'}')
518 croak("RPN expression must be braced");
519 lex_read_unichar(0);
520 return newASSIGNOP(OPf_STACKED, varop, 0, exprop);
521}
522
523#define parse_keyword_stufftest() THX_parse_keyword_stufftest(aTHX)
524static OP *THX_parse_keyword_stufftest(pTHX)
525{
526 I32 c;
527 bool do_stuff;
528 lex_read_space(0);
529 do_stuff = lex_peek_unichar(0) == '+';
530 if(do_stuff) {
531 lex_read_unichar(0);
532 lex_read_space(0);
533 }
534 c = lex_peek_unichar(0);
535 if(c == ';') {
536 lex_read_unichar(0);
537 } else if(c != /*{*/'}') {
538 croak("syntax error");
539 }
540 if(do_stuff) lex_stuff_pvs(" ", 0);
541 return newOP(OP_NULL, 0);
542}
543
544#define parse_keyword_swaptwostmts() THX_parse_keyword_swaptwostmts(aTHX)
545static OP *THX_parse_keyword_swaptwostmts(pTHX)
546{
547 OP *a, *b;
548 a = parse_fullstmt(0);
549 b = parse_fullstmt(0);
550 if(a && b)
551 PL_hints |= HINT_BLOCK_SCOPE;
552 /* should use append_list(), but that's not part of the public API */
553 return !a ? b : !b ? a : newLISTOP(OP_LINESEQ, 0, b, a);
554}
555
07ffcb73
Z
556#define parse_keyword_looprest() THX_parse_keyword_looprest(aTHX)
557static OP *THX_parse_keyword_looprest(pTHX)
558{
559 I32 condline;
560 OP *body;
561 condline = CopLINE(PL_curcop);
562 body = parse_stmtseq(0);
563 return newWHILEOP(0, 1, NULL, condline, newSVOP(OP_CONST, 0, &PL_sv_yes),
564 body, NULL, 1);
565}
566
83f8bb56
Z
567/* plugin glue */
568
569#define keyword_active(hintkey_sv) THX_keyword_active(aTHX_ hintkey_sv)
570static int THX_keyword_active(pTHX_ SV *hintkey_sv)
571{
572 HE *he;
573 if(!GvHV(PL_hintgv)) return 0;
574 he = hv_fetch_ent(GvHV(PL_hintgv), hintkey_sv, 0,
575 SvSHARED_HASH(hintkey_sv));
576 return he && SvTRUE(HeVAL(he));
577}
578
579static int my_keyword_plugin(pTHX_
580 char *keyword_ptr, STRLEN keyword_len, OP **op_ptr)
581{
582 if(keyword_len == 3 && strnEQ(keyword_ptr, "rpn", 3) &&
583 keyword_active(hintkey_rpn_sv)) {
584 *op_ptr = parse_keyword_rpn();
585 return KEYWORD_PLUGIN_EXPR;
586 } else if(keyword_len == 7 && strnEQ(keyword_ptr, "calcrpn", 7) &&
587 keyword_active(hintkey_calcrpn_sv)) {
588 *op_ptr = parse_keyword_calcrpn();
589 return KEYWORD_PLUGIN_STMT;
590 } else if(keyword_len == 9 && strnEQ(keyword_ptr, "stufftest", 9) &&
591 keyword_active(hintkey_stufftest_sv)) {
592 *op_ptr = parse_keyword_stufftest();
593 return KEYWORD_PLUGIN_STMT;
594 } else if(keyword_len == 12 &&
595 strnEQ(keyword_ptr, "swaptwostmts", 12) &&
596 keyword_active(hintkey_swaptwostmts_sv)) {
597 *op_ptr = parse_keyword_swaptwostmts();
598 return KEYWORD_PLUGIN_STMT;
07ffcb73
Z
599 } else if(keyword_len == 8 && strnEQ(keyword_ptr, "looprest", 8) &&
600 keyword_active(hintkey_looprest_sv)) {
601 *op_ptr = parse_keyword_looprest();
602 return KEYWORD_PLUGIN_STMT;
83f8bb56
Z
603 } else {
604 return next_keyword_plugin(aTHX_ keyword_ptr, keyword_len, op_ptr);
605 }
606}
607
7b20c7cd 608XS(XS_XS__APItest__XSUB_XS_VERSION_undef);
f9cc56fa 609XS(XS_XS__APItest__XSUB_XS_VERSION_empty);
7b20c7cd 610
55289a74
NC
611#include "const-c.inc"
612
ffe53d21 613MODULE = XS::APItest PACKAGE = XS::APItest
0314122a 614
55289a74
NC
615INCLUDE: const-xs.inc
616
ffe53d21
NC
617INCLUDE: numeric.xs
618
7b20c7cd
NC
619MODULE = XS::APItest PACKAGE = XS::APItest::XSUB
620
621BOOT:
622 newXS("XS::APItest::XSUB::XS_VERSION_undef", XS_XS__APItest__XSUB_XS_VERSION_undef, __FILE__);
f9cc56fa 623 newXS("XS::APItest::XSUB::XS_VERSION_empty", XS_XS__APItest__XSUB_XS_VERSION_empty, __FILE__);
7b20c7cd
NC
624
625void
626XS_VERSION_defined(...)
627 PPCODE:
628 XS_VERSION_BOOTCHECK;
629 XSRETURN_EMPTY;
630
ffe53d21
NC
631MODULE = XS::APItest:Hash PACKAGE = XS::APItest::Hash
632
b54b4831
NC
633void
634rot13_hash(hash)
635 HV *hash
636 CODE:
637 {
638 struct ufuncs uf;
639 uf.uf_val = rot13_key;
640 uf.uf_set = 0;
641 uf.uf_index = 0;
642
643 sv_magic((SV*)hash, NULL, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
644 }
645
53c40a8f
NC
646void
647bitflip_hash(hash)
648 HV *hash
649 CODE:
650 {
651 struct ufuncs uf;
652 uf.uf_val = bitflip_key;
653 uf.uf_set = 0;
654 uf.uf_index = 0;
655
656 sv_magic((SV*)hash, NULL, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
657 }
658
028f8eaa
MHM
659#define UTF8KLEN(sv, len) (SvUTF8(sv) ? -(I32)len : (I32)len)
660
0314122a
NC
661bool
662exists(hash, key_sv)
663 PREINIT:
664 STRLEN len;
665 const char *key;
666 INPUT:
667 HV *hash
668 SV *key_sv
669 CODE:
670 key = SvPV(key_sv, len);
028f8eaa 671 RETVAL = hv_exists(hash, key, UTF8KLEN(key_sv, len));
0314122a
NC
672 OUTPUT:
673 RETVAL
674
bdee33e4
NC
675bool
676exists_ent(hash, key_sv)
677 PREINIT:
678 INPUT:
679 HV *hash
680 SV *key_sv
681 CODE:
682 RETVAL = hv_exists_ent(hash, key_sv, 0);
683 OUTPUT:
684 RETVAL
685
b60cf05a 686SV *
55289a74 687delete(hash, key_sv, flags = 0)
b60cf05a
NC
688 PREINIT:
689 STRLEN len;
690 const char *key;
691 INPUT:
692 HV *hash
693 SV *key_sv
55289a74 694 I32 flags;
b60cf05a
NC
695 CODE:
696 key = SvPV(key_sv, len);
697 /* It's already mortal, so need to increase reference count. */
55289a74
NC
698 RETVAL
699 = SvREFCNT_inc(hv_delete(hash, key, UTF8KLEN(key_sv, len), flags));
700 OUTPUT:
701 RETVAL
702
703SV *
704delete_ent(hash, key_sv, flags = 0)
705 INPUT:
706 HV *hash
707 SV *key_sv
708 I32 flags;
709 CODE:
710 /* It's already mortal, so need to increase reference count. */
711 RETVAL = SvREFCNT_inc(hv_delete_ent(hash, key_sv, flags, 0));
b60cf05a
NC
712 OUTPUT:
713 RETVAL
714
715SV *
858117f8
NC
716store_ent(hash, key, value)
717 PREINIT:
718 SV *copy;
719 HE *result;
720 INPUT:
721 HV *hash
722 SV *key
723 SV *value
724 CODE:
725 copy = newSV(0);
726 result = hv_store_ent(hash, key, copy, 0);
727 SvSetMagicSV(copy, value);
728 if (!result) {
729 SvREFCNT_dec(copy);
730 XSRETURN_EMPTY;
731 }
732 /* It's about to become mortal, so need to increase reference count.
733 */
734 RETVAL = SvREFCNT_inc(HeVAL(result));
735 OUTPUT:
736 RETVAL
737
858117f8 738SV *
b60cf05a
NC
739store(hash, key_sv, value)
740 PREINIT:
741 STRLEN len;
742 const char *key;
743 SV *copy;
744 SV **result;
745 INPUT:
746 HV *hash
747 SV *key_sv
748 SV *value
749 CODE:
750 key = SvPV(key_sv, len);
751 copy = newSV(0);
028f8eaa 752 result = hv_store(hash, key, UTF8KLEN(key_sv, len), copy, 0);
858117f8 753 SvSetMagicSV(copy, value);
b60cf05a
NC
754 if (!result) {
755 SvREFCNT_dec(copy);
756 XSRETURN_EMPTY;
757 }
758 /* It's about to become mortal, so need to increase reference count.
759 */
760 RETVAL = SvREFCNT_inc(*result);
761 OUTPUT:
762 RETVAL
763
bdee33e4
NC
764SV *
765fetch_ent(hash, key_sv)
766 PREINIT:
767 HE *result;
768 INPUT:
769 HV *hash
770 SV *key_sv
771 CODE:
772 result = hv_fetch_ent(hash, key_sv, 0, 0);
773 if (!result) {
774 XSRETURN_EMPTY;
775 }
776 /* Force mg_get */
777 RETVAL = newSVsv(HeVAL(result));
778 OUTPUT:
779 RETVAL
b60cf05a
NC
780
781SV *
782fetch(hash, key_sv)
783 PREINIT:
784 STRLEN len;
785 const char *key;
786 SV **result;
787 INPUT:
788 HV *hash
789 SV *key_sv
790 CODE:
791 key = SvPV(key_sv, len);
028f8eaa 792 result = hv_fetch(hash, key, UTF8KLEN(key_sv, len), 0);
b60cf05a
NC
793 if (!result) {
794 XSRETURN_EMPTY;
795 }
796 /* Force mg_get */
797 RETVAL = newSVsv(*result);
798 OUTPUT:
799 RETVAL
2dc92170 800
9568a123
NC
801#if defined (hv_common)
802
6b4de907
NC
803SV *
804common(params)
805 INPUT:
806 HV *params
807 PREINIT:
808 HE *result;
809 HV *hv = NULL;
810 SV *keysv = NULL;
811 const char *key = NULL;
812 STRLEN klen = 0;
813 int flags = 0;
814 int action = 0;
815 SV *val = NULL;
816 U32 hash = 0;
817 SV **svp;
818 CODE:
819 if ((svp = hv_fetchs(params, "hv", 0))) {
820 SV *const rv = *svp;
821 if (!SvROK(rv))
822 croak("common passed a non-reference for parameter hv");
823 hv = (HV *)SvRV(rv);
824 }
825 if ((svp = hv_fetchs(params, "keysv", 0)))
826 keysv = *svp;
827 if ((svp = hv_fetchs(params, "keypv", 0))) {
828 key = SvPV_const(*svp, klen);
829 if (SvUTF8(*svp))
830 flags = HVhek_UTF8;
831 }
832 if ((svp = hv_fetchs(params, "action", 0)))
833 action = SvIV(*svp);
834 if ((svp = hv_fetchs(params, "val", 0)))
527df579 835 val = newSVsv(*svp);
6b4de907 836 if ((svp = hv_fetchs(params, "hash", 0)))
a44093a9 837 hash = SvUV(*svp);
6b4de907 838
527df579
NC
839 if ((svp = hv_fetchs(params, "hash_pv", 0))) {
840 PERL_HASH(hash, key, klen);
841 }
58ca560a
NC
842 if ((svp = hv_fetchs(params, "hash_sv", 0))) {
843 STRLEN len;
844 const char *const p = SvPV(keysv, len);
845 PERL_HASH(hash, p, len);
846 }
527df579 847
a75fcbca 848 result = (HE *)hv_common(hv, keysv, key, klen, flags, action, val, hash);
6b4de907
NC
849 if (!result) {
850 XSRETURN_EMPTY;
851 }
852 /* Force mg_get */
853 RETVAL = newSVsv(HeVAL(result));
854 OUTPUT:
855 RETVAL
856
9568a123
NC
857#endif
858
439efdfe 859void
2dc92170
NC
860test_hv_free_ent()
861 PPCODE:
862 test_freeent(&Perl_hv_free_ent);
863 XSRETURN(4);
864
439efdfe 865void
2dc92170
NC
866test_hv_delayfree_ent()
867 PPCODE:
868 test_freeent(&Perl_hv_delayfree_ent);
869 XSRETURN(4);
35ab5632
NC
870
871SV *
872test_share_unshare_pvn(input)
873 PREINIT:
35ab5632
NC
874 STRLEN len;
875 U32 hash;
876 char *pvx;
877 char *p;
878 INPUT:
879 SV *input
880 CODE:
881 pvx = SvPV(input, len);
882 PERL_HASH(hash, pvx, len);
883 p = sharepvn(pvx, len, hash);
884 RETVAL = newSVpvn(p, len);
885 unsharepvn(p, len, hash);
886 OUTPUT:
887 RETVAL
d8c5b3c5 888
9568a123
NC
889#if PERL_VERSION >= 9
890
d8c5b3c5
NC
891bool
892refcounted_he_exists(key, level=0)
893 SV *key
894 IV level
895 CODE:
896 if (level) {
897 croak("level must be zero, not %"IVdf, level);
898 }
899 RETVAL = (Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
900 key, NULL, 0, 0, 0)
901 != &PL_sv_placeholder);
902 OUTPUT:
903 RETVAL
904
d8c5b3c5
NC
905SV *
906refcounted_he_fetch(key, level=0)
907 SV *key
908 IV level
909 CODE:
910 if (level) {
911 croak("level must be zero, not %"IVdf, level);
912 }
913 RETVAL = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash, key,
914 NULL, 0, 0, 0);
915 SvREFCNT_inc(RETVAL);
916 OUTPUT:
917 RETVAL
65bfe90c 918
9568a123 919#endif
65bfe90c 920
0314122a
NC
921=pod
922
923sub TIEHASH { bless {}, $_[0] }
924sub STORE { $_[0]->{$_[1]} = $_[2] }
925sub FETCH { $_[0]->{$_[1]} }
926sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
927sub NEXTKEY { each %{$_[0]} }
928sub EXISTS { exists $_[0]->{$_[1]} }
929sub DELETE { delete $_[0]->{$_[1]} }
930sub CLEAR { %{$_[0]} = () }
931
932=cut
933
e2fe06dd
EB
934MODULE = XS::APItest:TempLv PACKAGE = XS::APItest::TempLv
935
936void
937make_temp_mg_lv(sv)
938SV* sv
939 PREINIT:
940 SV * const lv = newSV_type(SVt_PVLV);
941 STRLEN len;
942 PPCODE:
943 SvPV(sv, len);
944
945 sv_magic(lv, NULL, PERL_MAGIC_substr, NULL, 0);
946 LvTYPE(lv) = 'x';
947 LvTARG(lv) = SvREFCNT_inc_simple(sv);
948 LvTARGOFF(lv) = len == 0 ? 0 : 1;
949 LvTARGLEN(lv) = len < 2 ? 0 : len-2;
950
951 EXTEND(SP, 1);
952 ST(0) = sv_2mortal(lv);
953 XSRETURN(1);
954
955
36c2b1d0
NC
956MODULE = XS::APItest::PtrTable PACKAGE = XS::APItest::PtrTable PREFIX = ptr_table_
957
958void
959ptr_table_new(classname)
960const char * classname
961 PPCODE:
962 PUSHs(sv_setref_pv(sv_newmortal(), classname, (void*)ptr_table_new()));
963
964void
965DESTROY(table)
966XS::APItest::PtrTable table
967 CODE:
968 ptr_table_free(table);
969
970void
992b2363 971ptr_table_store(table, from, to)
36c2b1d0 972XS::APItest::PtrTable table
992b2363
NC
973SVREF from
974SVREF to
36c2b1d0 975 CODE:
992b2363 976 ptr_table_store(table, from, to);
36c2b1d0
NC
977
978UV
992b2363 979ptr_table_fetch(table, from)
36c2b1d0 980XS::APItest::PtrTable table
992b2363 981SVREF from
36c2b1d0 982 CODE:
992b2363 983 RETVAL = PTR2UV(ptr_table_fetch(table, from));
36c2b1d0
NC
984 OUTPUT:
985 RETVAL
986
987void
988ptr_table_split(table)
989XS::APItest::PtrTable table
990
991void
992ptr_table_clear(table)
993XS::APItest::PtrTable table
994
3e61d65a
JH
995MODULE = XS::APItest PACKAGE = XS::APItest
996
997PROTOTYPES: DISABLE
998
85ce96a1
DM
999BOOT:
1000{
1001 MY_CXT_INIT;
03569ecf 1002
85ce96a1
DM
1003 MY_CXT.i = 99;
1004 MY_CXT.sv = newSVpv("initial",0);
13b6b3bc
BM
1005
1006 MY_CXT.bhkav = get_av("XS::APItest::bhkav", GV_ADDMULTI);
1007 MY_CXT.bhk_record = 0;
1008
1009 BhkENTRY_set(&bhk_test, start, blockhook_test_start);
1010 BhkENTRY_set(&bhk_test, pre_end, blockhook_test_pre_end);
1011 BhkENTRY_set(&bhk_test, post_end, blockhook_test_post_end);
1012 BhkENTRY_set(&bhk_test, eval, blockhook_test_eval);
1013 Perl_blockhook_register(aTHX_ &bhk_test);
1014
65bfe90c 1015 MY_CXT.cscgv = gv_fetchpvs("XS::APItest::COMPILE_SCOPE_CONTAINER",
13b6b3bc 1016 GV_ADDMULTI, SVt_PVAV);
03569ecf
BM
1017 MY_CXT.cscav = GvAV(MY_CXT.cscgv);
1018
13b6b3bc
BM
1019 BhkENTRY_set(&bhk_csc, start, blockhook_csc_start);
1020 BhkENTRY_set(&bhk_csc, pre_end, blockhook_csc_pre_end);
1021 Perl_blockhook_register(aTHX_ &bhk_csc);
201c7e1f
FR
1022
1023 MY_CXT.peep_recorder = newAV();
1024 MY_CXT.rpeep_recorder = newAV();
1025
1026 MY_CXT.orig_peep = PL_peepp;
1027 MY_CXT.orig_rpeep = PL_rpeepp;
1028 PL_peepp = my_peep;
1029 PL_rpeepp = my_rpeep;
65bfe90c 1030}
85ce96a1
DM
1031
1032void
1033CLONE(...)
1034 CODE:
1035 MY_CXT_CLONE;
1036 MY_CXT.sv = newSVpv("initial_clone",0);
65bfe90c 1037 MY_CXT.cscgv = gv_fetchpvs("XS::APItest::COMPILE_SCOPE_CONTAINER",
13b6b3bc 1038 GV_ADDMULTI, SVt_PVAV);
03569ecf 1039 MY_CXT.cscav = NULL;
13b6b3bc
BM
1040 MY_CXT.bhkav = get_av("XS::APItest::bhkav", GV_ADDMULTI);
1041 MY_CXT.bhk_record = 0;
201c7e1f
FR
1042 MY_CXT.peep_recorder = newAV();
1043 MY_CXT.rpeep_recorder = newAV();
85ce96a1 1044
3e61d65a
JH
1045void
1046print_double(val)
1047 double val
1048 CODE:
1049 printf("%5.3f\n",val);
1050
1051int
1052have_long_double()
1053 CODE:
1054#ifdef HAS_LONG_DOUBLE
1055 RETVAL = 1;
1056#else
1057 RETVAL = 0;
1058#endif
cabb36f0
CN
1059 OUTPUT:
1060 RETVAL
3e61d65a
JH
1061
1062void
1063print_long_double()
1064 CODE:
1065#ifdef HAS_LONG_DOUBLE
fc0bf671 1066# if defined(PERL_PRIfldbl) && (LONG_DOUBLESIZE > DOUBLESIZE)
3e61d65a
JH
1067 long double val = 7.0;
1068 printf("%5.3" PERL_PRIfldbl "\n",val);
1069# else
1070 double val = 7.0;
1071 printf("%5.3f\n",val);
1072# endif
1073#endif
1074
1075void
3e61d65a
JH
1076print_int(val)
1077 int val
1078 CODE:
1079 printf("%d\n",val);
1080
1081void
1082print_long(val)
1083 long val
1084 CODE:
1085 printf("%ld\n",val);
1086
1087void
1088print_float(val)
1089 float val
1090 CODE:
1091 printf("%5.3f\n",val);
9d911683
NIS
1092
1093void
1094print_flush()
1095 CODE:
1096 fflush(stdout);
d4b90eee
SH
1097
1098void
1099mpushp()
1100 PPCODE:
1101 EXTEND(SP, 3);
1102 mPUSHp("one", 3);
1103 mPUSHp("two", 3);
1104 mPUSHp("three", 5);
1105 XSRETURN(3);
1106
1107void
1108mpushn()
1109 PPCODE:
1110 EXTEND(SP, 3);
1111 mPUSHn(0.5);
1112 mPUSHn(-0.25);
1113 mPUSHn(0.125);
1114 XSRETURN(3);
1115
1116void
1117mpushi()
1118 PPCODE:
1119 EXTEND(SP, 3);
d75b63cf
MHM
1120 mPUSHi(-1);
1121 mPUSHi(2);
1122 mPUSHi(-3);
d4b90eee
SH
1123 XSRETURN(3);
1124
1125void
1126mpushu()
1127 PPCODE:
1128 EXTEND(SP, 3);
d75b63cf
MHM
1129 mPUSHu(1);
1130 mPUSHu(2);
1131 mPUSHu(3);
d4b90eee
SH
1132 XSRETURN(3);
1133
1134void
1135mxpushp()
1136 PPCODE:
1137 mXPUSHp("one", 3);
1138 mXPUSHp("two", 3);
1139 mXPUSHp("three", 5);
1140 XSRETURN(3);
1141
1142void
1143mxpushn()
1144 PPCODE:
1145 mXPUSHn(0.5);
1146 mXPUSHn(-0.25);
1147 mXPUSHn(0.125);
1148 XSRETURN(3);
1149
1150void
1151mxpushi()
1152 PPCODE:
d75b63cf
MHM
1153 mXPUSHi(-1);
1154 mXPUSHi(2);
1155 mXPUSHi(-3);
d4b90eee
SH
1156 XSRETURN(3);
1157
1158void
1159mxpushu()
1160 PPCODE:
d75b63cf
MHM
1161 mXPUSHu(1);
1162 mXPUSHu(2);
1163 mXPUSHu(3);
d4b90eee 1164 XSRETURN(3);
d1f347d7
DM
1165
1166
1167void
1168call_sv(sv, flags, ...)
1169 SV* sv
1170 I32 flags
1171 PREINIT:
1172 I32 i;
1173 PPCODE:
1174 for (i=0; i<items-2; i++)
1175 ST(i) = ST(i+2); /* pop first two args */
1176 PUSHMARK(SP);
1177 SP += items - 2;
1178 PUTBACK;
1179 i = call_sv(sv, flags);
1180 SPAGAIN;
1181 EXTEND(SP, 1);
1182 PUSHs(sv_2mortal(newSViv(i)));
1183
1184void
1185call_pv(subname, flags, ...)
1186 char* subname
1187 I32 flags
1188 PREINIT:
1189 I32 i;
1190 PPCODE:
1191 for (i=0; i<items-2; i++)
1192 ST(i) = ST(i+2); /* pop first two args */
1193 PUSHMARK(SP);
1194 SP += items - 2;
1195 PUTBACK;
1196 i = call_pv(subname, flags);
1197 SPAGAIN;
1198 EXTEND(SP, 1);
1199 PUSHs(sv_2mortal(newSViv(i)));
1200
1201void
1202call_method(methname, flags, ...)
1203 char* methname
1204 I32 flags
1205 PREINIT:
1206 I32 i;
1207 PPCODE:
1208 for (i=0; i<items-2; i++)
1209 ST(i) = ST(i+2); /* pop first two args */
1210 PUSHMARK(SP);
1211 SP += items - 2;
1212 PUTBACK;
1213 i = call_method(methname, flags);
1214 SPAGAIN;
1215 EXTEND(SP, 1);
1216 PUSHs(sv_2mortal(newSViv(i)));
1217
1218void
1219eval_sv(sv, flags)
1220 SV* sv
1221 I32 flags
1222 PREINIT:
1223 I32 i;
1224 PPCODE:
1225 PUTBACK;
1226 i = eval_sv(sv, flags);
1227 SPAGAIN;
1228 EXTEND(SP, 1);
1229 PUSHs(sv_2mortal(newSViv(i)));
1230
b8e65a9b 1231void
d1f347d7
DM
1232eval_pv(p, croak_on_error)
1233 const char* p
1234 I32 croak_on_error
d1f347d7
DM
1235 PPCODE:
1236 PUTBACK;
1237 EXTEND(SP, 1);
1238 PUSHs(eval_pv(p, croak_on_error));
1239
1240void
1241require_pv(pv)
1242 const char* pv
d1f347d7
DM
1243 PPCODE:
1244 PUTBACK;
1245 require_pv(pv);
1246
0ca3a874 1247int
7a646707 1248apitest_exception(throw_e)
0ca3a874
MHM
1249 int throw_e
1250 OUTPUT:
1251 RETVAL
d1f347d7 1252
ef469b03 1253void
7e7a3dfc
GA
1254mycroak(sv)
1255 SV* sv
ef469b03 1256 CODE:
7e7a3dfc
GA
1257 if (SvOK(sv)) {
1258 Perl_croak(aTHX_ "%s", SvPV_nolen(sv));
1259 }
1260 else {
1261 Perl_croak(aTHX_ NULL);
1262 }
5d2b1485
NC
1263
1264SV*
1265strtab()
1266 CODE:
1267 RETVAL = newRV_inc((SV*)PL_strtab);
1268 OUTPUT:
1269 RETVAL
85ce96a1
DM
1270
1271int
1272my_cxt_getint()
1273 CODE:
1274 dMY_CXT;
1275 RETVAL = my_cxt_getint_p(aMY_CXT);
1276 OUTPUT:
1277 RETVAL
1278
1279void
1280my_cxt_setint(i)
1281 int i;
1282 CODE:
1283 dMY_CXT;
1284 my_cxt_setint_p(aMY_CXT_ i);
1285
1286void
9568a123
NC
1287my_cxt_getsv(how)
1288 bool how;
85ce96a1 1289 PPCODE:
85ce96a1 1290 EXTEND(SP, 1);
9568a123 1291 ST(0) = how ? my_cxt_getsv_interp_context() : my_cxt_getsv_interp();
85ce96a1
DM
1292 XSRETURN(1);
1293
1294void
1295my_cxt_setsv(sv)
1296 SV *sv;
1297 CODE:
1298 dMY_CXT;
1299 SvREFCNT_dec(MY_CXT.sv);
1300 my_cxt_setsv_p(sv _aMY_CXT);
1301 SvREFCNT_inc(sv);
34482cd6
NC
1302
1303bool
1304sv_setsv_cow_hashkey_core()
1305
1306bool
1307sv_setsv_cow_hashkey_notcore()
84ac5fd7
NC
1308
1309void
218787bd
VP
1310rmagical_cast(sv, type)
1311 SV *sv;
1312 SV *type;
1313 PREINIT:
1314 struct ufuncs uf;
1315 PPCODE:
1316 if (!SvOK(sv) || !SvROK(sv) || !SvOK(type)) { XSRETURN_UNDEF; }
1317 sv = SvRV(sv);
1318 if (SvTYPE(sv) != SVt_PVHV) { XSRETURN_UNDEF; }
1319 uf.uf_val = rmagical_a_dummy;
1320 uf.uf_set = NULL;
1321 uf.uf_index = 0;
1322 if (SvTRUE(type)) { /* b */
1323 sv_magicext(sv, NULL, PERL_MAGIC_ext, &rmagical_b, NULL, 0);
1324 } else { /* a */
1325 sv_magic(sv, NULL, PERL_MAGIC_uvar, (char *) &uf, sizeof(uf));
1326 }
1327 XSRETURN_YES;
1328
1329void
1330rmagical_flags(sv)
1331 SV *sv;
1332 PPCODE:
1333 if (!SvOK(sv) || !SvROK(sv)) { XSRETURN_UNDEF; }
1334 sv = SvRV(sv);
1335 EXTEND(SP, 3);
1336 mXPUSHu(SvFLAGS(sv) & SVs_GMG);
1337 mXPUSHu(SvFLAGS(sv) & SVs_SMG);
1338 mXPUSHu(SvFLAGS(sv) & SVs_RMG);
1339 XSRETURN(3);
1340
1341void
90d1f214
BM
1342my_caller(level)
1343 I32 level
1344 PREINIT:
1345 const PERL_CONTEXT *cx, *dbcx;
1346 const char *pv;
1347 const GV *gv;
1348 HV *hv;
1349 PPCODE:
1350 cx = caller_cx(level, &dbcx);
1351 EXTEND(SP, 8);
1352
1353 pv = CopSTASHPV(cx->blk_oldcop);
1354 ST(0) = pv ? sv_2mortal(newSVpv(pv, 0)) : &PL_sv_undef;
1355 gv = CvGV(cx->blk_sub.cv);
1356 ST(1) = isGV(gv) ? sv_2mortal(newSVpv(GvNAME(gv), 0)) : &PL_sv_undef;
1357
1358 pv = CopSTASHPV(dbcx->blk_oldcop);
1359 ST(2) = pv ? sv_2mortal(newSVpv(pv, 0)) : &PL_sv_undef;
1360 gv = CvGV(dbcx->blk_sub.cv);
1361 ST(3) = isGV(gv) ? sv_2mortal(newSVpv(GvNAME(gv), 0)) : &PL_sv_undef;
1362
1363 ST(4) = cop_hints_fetchpvs(cx->blk_oldcop, "foo");
1364 ST(5) = cop_hints_fetchpvn(cx->blk_oldcop, "foo", 3, 0, 0);
1365 ST(6) = cop_hints_fetchsv(cx->blk_oldcop,
1366 sv_2mortal(newSVpvn("foo", 3)), 0);
1367
1368 hv = cop_hints_2hv(cx->blk_oldcop);
1369 ST(7) = hv ? sv_2mortal(newRV_noinc((SV *)hv)) : &PL_sv_undef;
1370
1371 XSRETURN(8);
1372
1373void
f9c17636
MB
1374DPeek (sv)
1375 SV *sv
1376
1377 PPCODE:
5b1f7359 1378 ST (0) = newSVpv (Perl_sv_peek (aTHX_ sv), 0);
f9c17636
MB
1379 XSRETURN (1);
1380
1381void
84ac5fd7
NC
1382BEGIN()
1383 CODE:
1384 sv_inc(get_sv("XS::APItest::BEGIN_called", GV_ADD|GV_ADDMULTI));
1385
1386void
1387CHECK()
1388 CODE:
1389 sv_inc(get_sv("XS::APItest::CHECK_called", GV_ADD|GV_ADDMULTI));
1390
1391void
1392UNITCHECK()
1393 CODE:
0932863f 1394 sv_inc(get_sv("XS::APItest::UNITCHECK_called", GV_ADD|GV_ADDMULTI));
84ac5fd7
NC
1395
1396void
1397INIT()
1398 CODE:
1399 sv_inc(get_sv("XS::APItest::INIT_called", GV_ADD|GV_ADDMULTI));
1400
1401void
1402END()
1403 CODE:
1404 sv_inc(get_sv("XS::APItest::END_called", GV_ADD|GV_ADDMULTI));
30685b56
NC
1405
1406void
1407utf16_to_utf8 (sv, ...)
1408 SV* sv
1409 ALIAS:
1410 utf16_to_utf8_reversed = 1
1411 PREINIT:
1412 STRLEN len;
1413 U8 *source;
1414 SV *dest;
1415 I32 got; /* Gah, badly thought out APIs */
1416 CODE:
1417 source = (U8 *)SvPVbyte(sv, len);
1418 /* Optionally only convert part of the buffer. */
1419 if (items > 1) {
1420 len = SvUV(ST(1));
1421 }
1422 /* Mortalise this right now, as we'll be testing croak()s */
1423 dest = sv_2mortal(newSV(len * 3 / 2 + 1));
1424 if (ix) {
25f2e844 1425 utf16_to_utf8_reversed(source, (U8 *)SvPVX(dest), len, &got);
30685b56 1426 } else {
25f2e844 1427 utf16_to_utf8(source, (U8 *)SvPVX(dest), len, &got);
30685b56
NC
1428 }
1429 SvCUR_set(dest, got);
1430 SvPVX(dest)[got] = '\0';
1431 SvPOK_on(dest);
1432 ST(0) = dest;
1433 XSRETURN(1);
879d0c72 1434
6bd7445c
GG
1435void
1436my_exit(int exitcode)
1437 PPCODE:
1438 my_exit(exitcode);
d97c33b5
DM
1439
1440I32
1441sv_count()
1442 CODE:
1443 RETVAL = PL_sv_count;
1444 OUTPUT:
1445 RETVAL
13b6b3bc
BM
1446
1447void
1448bhk_record(bool on)
1449 CODE:
1450 dMY_CXT;
1451 MY_CXT.bhk_record = on;
1452 if (on)
1453 av_clear(MY_CXT.bhkav);
65bfe90c 1454
defdfed5
Z
1455void
1456test_savehints()
1457 PREINIT:
1458 SV **svp, *sv;
1459 CODE:
1460#define store_hint(KEY, VALUE) \
1461 sv_setiv_mg(*hv_fetchs(GvHV(PL_hintgv), KEY, 1), (VALUE))
1462#define hint_ok(KEY, EXPECT) \
1463 ((svp = hv_fetchs(GvHV(PL_hintgv), KEY, 0)) && \
1464 (sv = *svp) && SvIV(sv) == (EXPECT) && \
1465 (sv = cop_hints_fetchpvs(&PL_compiling, KEY)) && \
1466 SvIV(sv) == (EXPECT))
1467#define check_hint(KEY, EXPECT) \
1468 do { if (!hint_ok(KEY, EXPECT)) croak("fail"); } while(0)
1469 PL_hints |= HINT_LOCALIZE_HH;
1470 ENTER;
1471 SAVEHINTS();
1472 PL_hints &= HINT_INTEGER;
1473 store_hint("t0", 123);
1474 store_hint("t1", 456);
1475 if (PL_hints & HINT_INTEGER) croak("fail");
1476 check_hint("t0", 123); check_hint("t1", 456);
1477 ENTER;
1478 SAVEHINTS();
1479 if (PL_hints & HINT_INTEGER) croak("fail");
1480 check_hint("t0", 123); check_hint("t1", 456);
1481 PL_hints |= HINT_INTEGER;
1482 store_hint("t0", 321);
1483 if (!(PL_hints & HINT_INTEGER)) croak("fail");
1484 check_hint("t0", 321); check_hint("t1", 456);
1485 LEAVE;
1486 if (PL_hints & HINT_INTEGER) croak("fail");
1487 check_hint("t0", 123); check_hint("t1", 456);
1488 ENTER;
1489 SAVEHINTS();
1490 if (PL_hints & HINT_INTEGER) croak("fail");
1491 check_hint("t0", 123); check_hint("t1", 456);
1492 store_hint("t1", 654);
1493 if (PL_hints & HINT_INTEGER) croak("fail");
1494 check_hint("t0", 123); check_hint("t1", 654);
1495 LEAVE;
1496 if (PL_hints & HINT_INTEGER) croak("fail");
1497 check_hint("t0", 123); check_hint("t1", 456);
1498 LEAVE;
1499#undef store_hint
1500#undef hint_ok
1501#undef check_hint
1502
1503void
1504test_copyhints()
1505 PREINIT:
1506 HV *a, *b;
1507 CODE:
1508 PL_hints |= HINT_LOCALIZE_HH;
1509 ENTER;
1510 SAVEHINTS();
1511 sv_setiv_mg(*hv_fetchs(GvHV(PL_hintgv), "t0", 1), 123);
1512 if (SvIV(cop_hints_fetchpvs(&PL_compiling, "t0")) != 123) croak("fail");
1513 a = newHVhv(GvHV(PL_hintgv));
1514 sv_2mortal((SV*)a);
1515 sv_setiv_mg(*hv_fetchs(a, "t0", 1), 456);
1516 if (SvIV(cop_hints_fetchpvs(&PL_compiling, "t0")) != 123) croak("fail");
1517 b = hv_copy_hints_hv(a);
1518 sv_2mortal((SV*)b);
1519 sv_setiv_mg(*hv_fetchs(b, "t0", 1), 789);
1520 if (SvIV(cop_hints_fetchpvs(&PL_compiling, "t0")) != 789) croak("fail");
1521 LEAVE;
1522
201c7e1f
FR
1523void
1524peep_enable ()
1525 PREINIT:
1526 dMY_CXT;
1527 CODE:
1528 av_clear(MY_CXT.peep_recorder);
1529 av_clear(MY_CXT.rpeep_recorder);
1530 MY_CXT.peep_recording = 1;
1531
1532void
1533peep_disable ()
1534 PREINIT:
1535 dMY_CXT;
1536 CODE:
1537 MY_CXT.peep_recording = 0;
1538
1539SV *
1540peep_record ()
1541 PREINIT:
1542 dMY_CXT;
1543 CODE:
95d2461a 1544 RETVAL = newRV_inc((SV *)MY_CXT.peep_recorder);
201c7e1f
FR
1545 OUTPUT:
1546 RETVAL
1547
1548SV *
1549rpeep_record ()
1550 PREINIT:
1551 dMY_CXT;
1552 CODE:
95d2461a 1553 RETVAL = newRV_inc((SV *)MY_CXT.rpeep_recorder);
201c7e1f
FR
1554 OUTPUT:
1555 RETVAL
1556
e2fe06dd
EB
1557BOOT:
1558 {
1559 HV* stash;
1560 SV** meth = NULL;
1561 CV* cv;
1562 stash = gv_stashpv("XS::APItest::TempLv", 0);
1563 if (stash)
1564 meth = hv_fetchs(stash, "make_temp_mg_lv", 0);
1565 if (!meth)
1566 croak("lost method 'make_temp_mg_lv'");
1567 cv = GvCV(*meth);
1568 CvLVALUE_on(cv);
1569 }
83f8bb56
Z
1570
1571BOOT:
1572{
1573 hintkey_rpn_sv = newSVpvs_share("XS::APItest/rpn");
1574 hintkey_calcrpn_sv = newSVpvs_share("XS::APItest/calcrpn");
1575 hintkey_stufftest_sv = newSVpvs_share("XS::APItest/stufftest");
1576 hintkey_swaptwostmts_sv = newSVpvs_share("XS::APItest/swaptwostmts");
07ffcb73 1577 hintkey_looprest_sv = newSVpvs_share("XS::APItest/looprest");
83f8bb56
Z
1578 next_keyword_plugin = PL_keyword_plugin;
1579 PL_keyword_plugin = my_keyword_plugin;
1580}