This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
add stray save_* functions to the 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;
384static SV *hintkey_swaptwostmts_sv;
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
556/* plugin glue */
557
558#define keyword_active(hintkey_sv) THX_keyword_active(aTHX_ hintkey_sv)
559static int THX_keyword_active(pTHX_ SV *hintkey_sv)
560{
561 HE *he;
562 if(!GvHV(PL_hintgv)) return 0;
563 he = hv_fetch_ent(GvHV(PL_hintgv), hintkey_sv, 0,
564 SvSHARED_HASH(hintkey_sv));
565 return he && SvTRUE(HeVAL(he));
566}
567
568static int my_keyword_plugin(pTHX_
569 char *keyword_ptr, STRLEN keyword_len, OP **op_ptr)
570{
571 if(keyword_len == 3 && strnEQ(keyword_ptr, "rpn", 3) &&
572 keyword_active(hintkey_rpn_sv)) {
573 *op_ptr = parse_keyword_rpn();
574 return KEYWORD_PLUGIN_EXPR;
575 } else if(keyword_len == 7 && strnEQ(keyword_ptr, "calcrpn", 7) &&
576 keyword_active(hintkey_calcrpn_sv)) {
577 *op_ptr = parse_keyword_calcrpn();
578 return KEYWORD_PLUGIN_STMT;
579 } else if(keyword_len == 9 && strnEQ(keyword_ptr, "stufftest", 9) &&
580 keyword_active(hintkey_stufftest_sv)) {
581 *op_ptr = parse_keyword_stufftest();
582 return KEYWORD_PLUGIN_STMT;
583 } else if(keyword_len == 12 &&
584 strnEQ(keyword_ptr, "swaptwostmts", 12) &&
585 keyword_active(hintkey_swaptwostmts_sv)) {
586 *op_ptr = parse_keyword_swaptwostmts();
587 return KEYWORD_PLUGIN_STMT;
588 } else {
589 return next_keyword_plugin(aTHX_ keyword_ptr, keyword_len, op_ptr);
590 }
591}
592
55289a74
NC
593#include "const-c.inc"
594
ffe53d21 595MODULE = XS::APItest PACKAGE = XS::APItest
0314122a 596
55289a74
NC
597INCLUDE: const-xs.inc
598
ffe53d21
NC
599INCLUDE: numeric.xs
600
601MODULE = XS::APItest:Hash PACKAGE = XS::APItest::Hash
602
b54b4831
NC
603void
604rot13_hash(hash)
605 HV *hash
606 CODE:
607 {
608 struct ufuncs uf;
609 uf.uf_val = rot13_key;
610 uf.uf_set = 0;
611 uf.uf_index = 0;
612
613 sv_magic((SV*)hash, NULL, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
614 }
615
53c40a8f
NC
616void
617bitflip_hash(hash)
618 HV *hash
619 CODE:
620 {
621 struct ufuncs uf;
622 uf.uf_val = bitflip_key;
623 uf.uf_set = 0;
624 uf.uf_index = 0;
625
626 sv_magic((SV*)hash, NULL, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
627 }
628
028f8eaa
MHM
629#define UTF8KLEN(sv, len) (SvUTF8(sv) ? -(I32)len : (I32)len)
630
0314122a
NC
631bool
632exists(hash, key_sv)
633 PREINIT:
634 STRLEN len;
635 const char *key;
636 INPUT:
637 HV *hash
638 SV *key_sv
639 CODE:
640 key = SvPV(key_sv, len);
028f8eaa 641 RETVAL = hv_exists(hash, key, UTF8KLEN(key_sv, len));
0314122a
NC
642 OUTPUT:
643 RETVAL
644
bdee33e4
NC
645bool
646exists_ent(hash, key_sv)
647 PREINIT:
648 INPUT:
649 HV *hash
650 SV *key_sv
651 CODE:
652 RETVAL = hv_exists_ent(hash, key_sv, 0);
653 OUTPUT:
654 RETVAL
655
b60cf05a 656SV *
55289a74 657delete(hash, key_sv, flags = 0)
b60cf05a
NC
658 PREINIT:
659 STRLEN len;
660 const char *key;
661 INPUT:
662 HV *hash
663 SV *key_sv
55289a74 664 I32 flags;
b60cf05a
NC
665 CODE:
666 key = SvPV(key_sv, len);
667 /* It's already mortal, so need to increase reference count. */
55289a74
NC
668 RETVAL
669 = SvREFCNT_inc(hv_delete(hash, key, UTF8KLEN(key_sv, len), flags));
670 OUTPUT:
671 RETVAL
672
673SV *
674delete_ent(hash, key_sv, flags = 0)
675 INPUT:
676 HV *hash
677 SV *key_sv
678 I32 flags;
679 CODE:
680 /* It's already mortal, so need to increase reference count. */
681 RETVAL = SvREFCNT_inc(hv_delete_ent(hash, key_sv, flags, 0));
b60cf05a
NC
682 OUTPUT:
683 RETVAL
684
685SV *
858117f8
NC
686store_ent(hash, key, value)
687 PREINIT:
688 SV *copy;
689 HE *result;
690 INPUT:
691 HV *hash
692 SV *key
693 SV *value
694 CODE:
695 copy = newSV(0);
696 result = hv_store_ent(hash, key, copy, 0);
697 SvSetMagicSV(copy, value);
698 if (!result) {
699 SvREFCNT_dec(copy);
700 XSRETURN_EMPTY;
701 }
702 /* It's about to become mortal, so need to increase reference count.
703 */
704 RETVAL = SvREFCNT_inc(HeVAL(result));
705 OUTPUT:
706 RETVAL
707
858117f8 708SV *
b60cf05a
NC
709store(hash, key_sv, value)
710 PREINIT:
711 STRLEN len;
712 const char *key;
713 SV *copy;
714 SV **result;
715 INPUT:
716 HV *hash
717 SV *key_sv
718 SV *value
719 CODE:
720 key = SvPV(key_sv, len);
721 copy = newSV(0);
028f8eaa 722 result = hv_store(hash, key, UTF8KLEN(key_sv, len), copy, 0);
858117f8 723 SvSetMagicSV(copy, value);
b60cf05a
NC
724 if (!result) {
725 SvREFCNT_dec(copy);
726 XSRETURN_EMPTY;
727 }
728 /* It's about to become mortal, so need to increase reference count.
729 */
730 RETVAL = SvREFCNT_inc(*result);
731 OUTPUT:
732 RETVAL
733
bdee33e4
NC
734SV *
735fetch_ent(hash, key_sv)
736 PREINIT:
737 HE *result;
738 INPUT:
739 HV *hash
740 SV *key_sv
741 CODE:
742 result = hv_fetch_ent(hash, key_sv, 0, 0);
743 if (!result) {
744 XSRETURN_EMPTY;
745 }
746 /* Force mg_get */
747 RETVAL = newSVsv(HeVAL(result));
748 OUTPUT:
749 RETVAL
b60cf05a
NC
750
751SV *
752fetch(hash, key_sv)
753 PREINIT:
754 STRLEN len;
755 const char *key;
756 SV **result;
757 INPUT:
758 HV *hash
759 SV *key_sv
760 CODE:
761 key = SvPV(key_sv, len);
028f8eaa 762 result = hv_fetch(hash, key, UTF8KLEN(key_sv, len), 0);
b60cf05a
NC
763 if (!result) {
764 XSRETURN_EMPTY;
765 }
766 /* Force mg_get */
767 RETVAL = newSVsv(*result);
768 OUTPUT:
769 RETVAL
2dc92170 770
9568a123
NC
771#if defined (hv_common)
772
6b4de907
NC
773SV *
774common(params)
775 INPUT:
776 HV *params
777 PREINIT:
778 HE *result;
779 HV *hv = NULL;
780 SV *keysv = NULL;
781 const char *key = NULL;
782 STRLEN klen = 0;
783 int flags = 0;
784 int action = 0;
785 SV *val = NULL;
786 U32 hash = 0;
787 SV **svp;
788 CODE:
789 if ((svp = hv_fetchs(params, "hv", 0))) {
790 SV *const rv = *svp;
791 if (!SvROK(rv))
792 croak("common passed a non-reference for parameter hv");
793 hv = (HV *)SvRV(rv);
794 }
795 if ((svp = hv_fetchs(params, "keysv", 0)))
796 keysv = *svp;
797 if ((svp = hv_fetchs(params, "keypv", 0))) {
798 key = SvPV_const(*svp, klen);
799 if (SvUTF8(*svp))
800 flags = HVhek_UTF8;
801 }
802 if ((svp = hv_fetchs(params, "action", 0)))
803 action = SvIV(*svp);
804 if ((svp = hv_fetchs(params, "val", 0)))
527df579 805 val = newSVsv(*svp);
6b4de907 806 if ((svp = hv_fetchs(params, "hash", 0)))
a44093a9 807 hash = SvUV(*svp);
6b4de907 808
527df579
NC
809 if ((svp = hv_fetchs(params, "hash_pv", 0))) {
810 PERL_HASH(hash, key, klen);
811 }
58ca560a
NC
812 if ((svp = hv_fetchs(params, "hash_sv", 0))) {
813 STRLEN len;
814 const char *const p = SvPV(keysv, len);
815 PERL_HASH(hash, p, len);
816 }
527df579 817
a75fcbca 818 result = (HE *)hv_common(hv, keysv, key, klen, flags, action, val, hash);
6b4de907
NC
819 if (!result) {
820 XSRETURN_EMPTY;
821 }
822 /* Force mg_get */
823 RETVAL = newSVsv(HeVAL(result));
824 OUTPUT:
825 RETVAL
826
9568a123
NC
827#endif
828
439efdfe 829void
2dc92170
NC
830test_hv_free_ent()
831 PPCODE:
832 test_freeent(&Perl_hv_free_ent);
833 XSRETURN(4);
834
439efdfe 835void
2dc92170
NC
836test_hv_delayfree_ent()
837 PPCODE:
838 test_freeent(&Perl_hv_delayfree_ent);
839 XSRETURN(4);
35ab5632
NC
840
841SV *
842test_share_unshare_pvn(input)
843 PREINIT:
35ab5632
NC
844 STRLEN len;
845 U32 hash;
846 char *pvx;
847 char *p;
848 INPUT:
849 SV *input
850 CODE:
851 pvx = SvPV(input, len);
852 PERL_HASH(hash, pvx, len);
853 p = sharepvn(pvx, len, hash);
854 RETVAL = newSVpvn(p, len);
855 unsharepvn(p, len, hash);
856 OUTPUT:
857 RETVAL
d8c5b3c5 858
9568a123
NC
859#if PERL_VERSION >= 9
860
d8c5b3c5
NC
861bool
862refcounted_he_exists(key, level=0)
863 SV *key
864 IV level
865 CODE:
866 if (level) {
867 croak("level must be zero, not %"IVdf, level);
868 }
869 RETVAL = (Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
870 key, NULL, 0, 0, 0)
871 != &PL_sv_placeholder);
872 OUTPUT:
873 RETVAL
874
d8c5b3c5
NC
875SV *
876refcounted_he_fetch(key, level=0)
877 SV *key
878 IV level
879 CODE:
880 if (level) {
881 croak("level must be zero, not %"IVdf, level);
882 }
883 RETVAL = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash, key,
884 NULL, 0, 0, 0);
885 SvREFCNT_inc(RETVAL);
886 OUTPUT:
887 RETVAL
65bfe90c 888
9568a123 889#endif
65bfe90c 890
0314122a
NC
891=pod
892
893sub TIEHASH { bless {}, $_[0] }
894sub STORE { $_[0]->{$_[1]} = $_[2] }
895sub FETCH { $_[0]->{$_[1]} }
896sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
897sub NEXTKEY { each %{$_[0]} }
898sub EXISTS { exists $_[0]->{$_[1]} }
899sub DELETE { delete $_[0]->{$_[1]} }
900sub CLEAR { %{$_[0]} = () }
901
902=cut
903
e2fe06dd
EB
904MODULE = XS::APItest:TempLv PACKAGE = XS::APItest::TempLv
905
906void
907make_temp_mg_lv(sv)
908SV* sv
909 PREINIT:
910 SV * const lv = newSV_type(SVt_PVLV);
911 STRLEN len;
912 PPCODE:
913 SvPV(sv, len);
914
915 sv_magic(lv, NULL, PERL_MAGIC_substr, NULL, 0);
916 LvTYPE(lv) = 'x';
917 LvTARG(lv) = SvREFCNT_inc_simple(sv);
918 LvTARGOFF(lv) = len == 0 ? 0 : 1;
919 LvTARGLEN(lv) = len < 2 ? 0 : len-2;
920
921 EXTEND(SP, 1);
922 ST(0) = sv_2mortal(lv);
923 XSRETURN(1);
924
925
36c2b1d0
NC
926MODULE = XS::APItest::PtrTable PACKAGE = XS::APItest::PtrTable PREFIX = ptr_table_
927
928void
929ptr_table_new(classname)
930const char * classname
931 PPCODE:
932 PUSHs(sv_setref_pv(sv_newmortal(), classname, (void*)ptr_table_new()));
933
934void
935DESTROY(table)
936XS::APItest::PtrTable table
937 CODE:
938 ptr_table_free(table);
939
940void
992b2363 941ptr_table_store(table, from, to)
36c2b1d0 942XS::APItest::PtrTable table
992b2363
NC
943SVREF from
944SVREF to
36c2b1d0 945 CODE:
992b2363 946 ptr_table_store(table, from, to);
36c2b1d0
NC
947
948UV
992b2363 949ptr_table_fetch(table, from)
36c2b1d0 950XS::APItest::PtrTable table
992b2363 951SVREF from
36c2b1d0 952 CODE:
992b2363 953 RETVAL = PTR2UV(ptr_table_fetch(table, from));
36c2b1d0
NC
954 OUTPUT:
955 RETVAL
956
957void
958ptr_table_split(table)
959XS::APItest::PtrTable table
960
961void
962ptr_table_clear(table)
963XS::APItest::PtrTable table
964
3e61d65a
JH
965MODULE = XS::APItest PACKAGE = XS::APItest
966
967PROTOTYPES: DISABLE
968
85ce96a1
DM
969BOOT:
970{
971 MY_CXT_INIT;
03569ecf 972
85ce96a1
DM
973 MY_CXT.i = 99;
974 MY_CXT.sv = newSVpv("initial",0);
13b6b3bc
BM
975
976 MY_CXT.bhkav = get_av("XS::APItest::bhkav", GV_ADDMULTI);
977 MY_CXT.bhk_record = 0;
978
979 BhkENTRY_set(&bhk_test, start, blockhook_test_start);
980 BhkENTRY_set(&bhk_test, pre_end, blockhook_test_pre_end);
981 BhkENTRY_set(&bhk_test, post_end, blockhook_test_post_end);
982 BhkENTRY_set(&bhk_test, eval, blockhook_test_eval);
983 Perl_blockhook_register(aTHX_ &bhk_test);
984
65bfe90c 985 MY_CXT.cscgv = gv_fetchpvs("XS::APItest::COMPILE_SCOPE_CONTAINER",
13b6b3bc 986 GV_ADDMULTI, SVt_PVAV);
03569ecf
BM
987 MY_CXT.cscav = GvAV(MY_CXT.cscgv);
988
13b6b3bc
BM
989 BhkENTRY_set(&bhk_csc, start, blockhook_csc_start);
990 BhkENTRY_set(&bhk_csc, pre_end, blockhook_csc_pre_end);
991 Perl_blockhook_register(aTHX_ &bhk_csc);
201c7e1f
FR
992
993 MY_CXT.peep_recorder = newAV();
994 MY_CXT.rpeep_recorder = newAV();
995
996 MY_CXT.orig_peep = PL_peepp;
997 MY_CXT.orig_rpeep = PL_rpeepp;
998 PL_peepp = my_peep;
999 PL_rpeepp = my_rpeep;
65bfe90c 1000}
85ce96a1
DM
1001
1002void
1003CLONE(...)
1004 CODE:
1005 MY_CXT_CLONE;
1006 MY_CXT.sv = newSVpv("initial_clone",0);
65bfe90c 1007 MY_CXT.cscgv = gv_fetchpvs("XS::APItest::COMPILE_SCOPE_CONTAINER",
13b6b3bc 1008 GV_ADDMULTI, SVt_PVAV);
03569ecf 1009 MY_CXT.cscav = NULL;
13b6b3bc
BM
1010 MY_CXT.bhkav = get_av("XS::APItest::bhkav", GV_ADDMULTI);
1011 MY_CXT.bhk_record = 0;
201c7e1f
FR
1012 MY_CXT.peep_recorder = newAV();
1013 MY_CXT.rpeep_recorder = newAV();
85ce96a1 1014
3e61d65a
JH
1015void
1016print_double(val)
1017 double val
1018 CODE:
1019 printf("%5.3f\n",val);
1020
1021int
1022have_long_double()
1023 CODE:
1024#ifdef HAS_LONG_DOUBLE
1025 RETVAL = 1;
1026#else
1027 RETVAL = 0;
1028#endif
cabb36f0
CN
1029 OUTPUT:
1030 RETVAL
3e61d65a
JH
1031
1032void
1033print_long_double()
1034 CODE:
1035#ifdef HAS_LONG_DOUBLE
fc0bf671 1036# if defined(PERL_PRIfldbl) && (LONG_DOUBLESIZE > DOUBLESIZE)
3e61d65a
JH
1037 long double val = 7.0;
1038 printf("%5.3" PERL_PRIfldbl "\n",val);
1039# else
1040 double val = 7.0;
1041 printf("%5.3f\n",val);
1042# endif
1043#endif
1044
1045void
3e61d65a
JH
1046print_int(val)
1047 int val
1048 CODE:
1049 printf("%d\n",val);
1050
1051void
1052print_long(val)
1053 long val
1054 CODE:
1055 printf("%ld\n",val);
1056
1057void
1058print_float(val)
1059 float val
1060 CODE:
1061 printf("%5.3f\n",val);
9d911683
NIS
1062
1063void
1064print_flush()
1065 CODE:
1066 fflush(stdout);
d4b90eee
SH
1067
1068void
1069mpushp()
1070 PPCODE:
1071 EXTEND(SP, 3);
1072 mPUSHp("one", 3);
1073 mPUSHp("two", 3);
1074 mPUSHp("three", 5);
1075 XSRETURN(3);
1076
1077void
1078mpushn()
1079 PPCODE:
1080 EXTEND(SP, 3);
1081 mPUSHn(0.5);
1082 mPUSHn(-0.25);
1083 mPUSHn(0.125);
1084 XSRETURN(3);
1085
1086void
1087mpushi()
1088 PPCODE:
1089 EXTEND(SP, 3);
d75b63cf
MHM
1090 mPUSHi(-1);
1091 mPUSHi(2);
1092 mPUSHi(-3);
d4b90eee
SH
1093 XSRETURN(3);
1094
1095void
1096mpushu()
1097 PPCODE:
1098 EXTEND(SP, 3);
d75b63cf
MHM
1099 mPUSHu(1);
1100 mPUSHu(2);
1101 mPUSHu(3);
d4b90eee
SH
1102 XSRETURN(3);
1103
1104void
1105mxpushp()
1106 PPCODE:
1107 mXPUSHp("one", 3);
1108 mXPUSHp("two", 3);
1109 mXPUSHp("three", 5);
1110 XSRETURN(3);
1111
1112void
1113mxpushn()
1114 PPCODE:
1115 mXPUSHn(0.5);
1116 mXPUSHn(-0.25);
1117 mXPUSHn(0.125);
1118 XSRETURN(3);
1119
1120void
1121mxpushi()
1122 PPCODE:
d75b63cf
MHM
1123 mXPUSHi(-1);
1124 mXPUSHi(2);
1125 mXPUSHi(-3);
d4b90eee
SH
1126 XSRETURN(3);
1127
1128void
1129mxpushu()
1130 PPCODE:
d75b63cf
MHM
1131 mXPUSHu(1);
1132 mXPUSHu(2);
1133 mXPUSHu(3);
d4b90eee 1134 XSRETURN(3);
d1f347d7
DM
1135
1136
1137void
1138call_sv(sv, flags, ...)
1139 SV* sv
1140 I32 flags
1141 PREINIT:
1142 I32 i;
1143 PPCODE:
1144 for (i=0; i<items-2; i++)
1145 ST(i) = ST(i+2); /* pop first two args */
1146 PUSHMARK(SP);
1147 SP += items - 2;
1148 PUTBACK;
1149 i = call_sv(sv, flags);
1150 SPAGAIN;
1151 EXTEND(SP, 1);
1152 PUSHs(sv_2mortal(newSViv(i)));
1153
1154void
1155call_pv(subname, flags, ...)
1156 char* subname
1157 I32 flags
1158 PREINIT:
1159 I32 i;
1160 PPCODE:
1161 for (i=0; i<items-2; i++)
1162 ST(i) = ST(i+2); /* pop first two args */
1163 PUSHMARK(SP);
1164 SP += items - 2;
1165 PUTBACK;
1166 i = call_pv(subname, flags);
1167 SPAGAIN;
1168 EXTEND(SP, 1);
1169 PUSHs(sv_2mortal(newSViv(i)));
1170
1171void
1172call_method(methname, flags, ...)
1173 char* methname
1174 I32 flags
1175 PREINIT:
1176 I32 i;
1177 PPCODE:
1178 for (i=0; i<items-2; i++)
1179 ST(i) = ST(i+2); /* pop first two args */
1180 PUSHMARK(SP);
1181 SP += items - 2;
1182 PUTBACK;
1183 i = call_method(methname, flags);
1184 SPAGAIN;
1185 EXTEND(SP, 1);
1186 PUSHs(sv_2mortal(newSViv(i)));
1187
1188void
1189eval_sv(sv, flags)
1190 SV* sv
1191 I32 flags
1192 PREINIT:
1193 I32 i;
1194 PPCODE:
1195 PUTBACK;
1196 i = eval_sv(sv, flags);
1197 SPAGAIN;
1198 EXTEND(SP, 1);
1199 PUSHs(sv_2mortal(newSViv(i)));
1200
b8e65a9b 1201void
d1f347d7
DM
1202eval_pv(p, croak_on_error)
1203 const char* p
1204 I32 croak_on_error
d1f347d7
DM
1205 PPCODE:
1206 PUTBACK;
1207 EXTEND(SP, 1);
1208 PUSHs(eval_pv(p, croak_on_error));
1209
1210void
1211require_pv(pv)
1212 const char* pv
d1f347d7
DM
1213 PPCODE:
1214 PUTBACK;
1215 require_pv(pv);
1216
0ca3a874 1217int
7a646707 1218apitest_exception(throw_e)
0ca3a874
MHM
1219 int throw_e
1220 OUTPUT:
1221 RETVAL
d1f347d7 1222
ef469b03 1223void
7e7a3dfc
GA
1224mycroak(sv)
1225 SV* sv
ef469b03 1226 CODE:
7e7a3dfc
GA
1227 if (SvOK(sv)) {
1228 Perl_croak(aTHX_ "%s", SvPV_nolen(sv));
1229 }
1230 else {
1231 Perl_croak(aTHX_ NULL);
1232 }
5d2b1485
NC
1233
1234SV*
1235strtab()
1236 CODE:
1237 RETVAL = newRV_inc((SV*)PL_strtab);
1238 OUTPUT:
1239 RETVAL
85ce96a1
DM
1240
1241int
1242my_cxt_getint()
1243 CODE:
1244 dMY_CXT;
1245 RETVAL = my_cxt_getint_p(aMY_CXT);
1246 OUTPUT:
1247 RETVAL
1248
1249void
1250my_cxt_setint(i)
1251 int i;
1252 CODE:
1253 dMY_CXT;
1254 my_cxt_setint_p(aMY_CXT_ i);
1255
1256void
9568a123
NC
1257my_cxt_getsv(how)
1258 bool how;
85ce96a1 1259 PPCODE:
85ce96a1 1260 EXTEND(SP, 1);
9568a123 1261 ST(0) = how ? my_cxt_getsv_interp_context() : my_cxt_getsv_interp();
85ce96a1
DM
1262 XSRETURN(1);
1263
1264void
1265my_cxt_setsv(sv)
1266 SV *sv;
1267 CODE:
1268 dMY_CXT;
1269 SvREFCNT_dec(MY_CXT.sv);
1270 my_cxt_setsv_p(sv _aMY_CXT);
1271 SvREFCNT_inc(sv);
34482cd6
NC
1272
1273bool
1274sv_setsv_cow_hashkey_core()
1275
1276bool
1277sv_setsv_cow_hashkey_notcore()
84ac5fd7
NC
1278
1279void
218787bd
VP
1280rmagical_cast(sv, type)
1281 SV *sv;
1282 SV *type;
1283 PREINIT:
1284 struct ufuncs uf;
1285 PPCODE:
1286 if (!SvOK(sv) || !SvROK(sv) || !SvOK(type)) { XSRETURN_UNDEF; }
1287 sv = SvRV(sv);
1288 if (SvTYPE(sv) != SVt_PVHV) { XSRETURN_UNDEF; }
1289 uf.uf_val = rmagical_a_dummy;
1290 uf.uf_set = NULL;
1291 uf.uf_index = 0;
1292 if (SvTRUE(type)) { /* b */
1293 sv_magicext(sv, NULL, PERL_MAGIC_ext, &rmagical_b, NULL, 0);
1294 } else { /* a */
1295 sv_magic(sv, NULL, PERL_MAGIC_uvar, (char *) &uf, sizeof(uf));
1296 }
1297 XSRETURN_YES;
1298
1299void
1300rmagical_flags(sv)
1301 SV *sv;
1302 PPCODE:
1303 if (!SvOK(sv) || !SvROK(sv)) { XSRETURN_UNDEF; }
1304 sv = SvRV(sv);
1305 EXTEND(SP, 3);
1306 mXPUSHu(SvFLAGS(sv) & SVs_GMG);
1307 mXPUSHu(SvFLAGS(sv) & SVs_SMG);
1308 mXPUSHu(SvFLAGS(sv) & SVs_RMG);
1309 XSRETURN(3);
1310
1311void
90d1f214
BM
1312my_caller(level)
1313 I32 level
1314 PREINIT:
1315 const PERL_CONTEXT *cx, *dbcx;
1316 const char *pv;
1317 const GV *gv;
1318 HV *hv;
1319 PPCODE:
1320 cx = caller_cx(level, &dbcx);
1321 EXTEND(SP, 8);
1322
1323 pv = CopSTASHPV(cx->blk_oldcop);
1324 ST(0) = pv ? sv_2mortal(newSVpv(pv, 0)) : &PL_sv_undef;
1325 gv = CvGV(cx->blk_sub.cv);
1326 ST(1) = isGV(gv) ? sv_2mortal(newSVpv(GvNAME(gv), 0)) : &PL_sv_undef;
1327
1328 pv = CopSTASHPV(dbcx->blk_oldcop);
1329 ST(2) = pv ? sv_2mortal(newSVpv(pv, 0)) : &PL_sv_undef;
1330 gv = CvGV(dbcx->blk_sub.cv);
1331 ST(3) = isGV(gv) ? sv_2mortal(newSVpv(GvNAME(gv), 0)) : &PL_sv_undef;
1332
1333 ST(4) = cop_hints_fetchpvs(cx->blk_oldcop, "foo");
1334 ST(5) = cop_hints_fetchpvn(cx->blk_oldcop, "foo", 3, 0, 0);
1335 ST(6) = cop_hints_fetchsv(cx->blk_oldcop,
1336 sv_2mortal(newSVpvn("foo", 3)), 0);
1337
1338 hv = cop_hints_2hv(cx->blk_oldcop);
1339 ST(7) = hv ? sv_2mortal(newRV_noinc((SV *)hv)) : &PL_sv_undef;
1340
1341 XSRETURN(8);
1342
1343void
f9c17636
MB
1344DPeek (sv)
1345 SV *sv
1346
1347 PPCODE:
5b1f7359 1348 ST (0) = newSVpv (Perl_sv_peek (aTHX_ sv), 0);
f9c17636
MB
1349 XSRETURN (1);
1350
1351void
84ac5fd7
NC
1352BEGIN()
1353 CODE:
1354 sv_inc(get_sv("XS::APItest::BEGIN_called", GV_ADD|GV_ADDMULTI));
1355
1356void
1357CHECK()
1358 CODE:
1359 sv_inc(get_sv("XS::APItest::CHECK_called", GV_ADD|GV_ADDMULTI));
1360
1361void
1362UNITCHECK()
1363 CODE:
0932863f 1364 sv_inc(get_sv("XS::APItest::UNITCHECK_called", GV_ADD|GV_ADDMULTI));
84ac5fd7
NC
1365
1366void
1367INIT()
1368 CODE:
1369 sv_inc(get_sv("XS::APItest::INIT_called", GV_ADD|GV_ADDMULTI));
1370
1371void
1372END()
1373 CODE:
1374 sv_inc(get_sv("XS::APItest::END_called", GV_ADD|GV_ADDMULTI));
30685b56
NC
1375
1376void
1377utf16_to_utf8 (sv, ...)
1378 SV* sv
1379 ALIAS:
1380 utf16_to_utf8_reversed = 1
1381 PREINIT:
1382 STRLEN len;
1383 U8 *source;
1384 SV *dest;
1385 I32 got; /* Gah, badly thought out APIs */
1386 CODE:
1387 source = (U8 *)SvPVbyte(sv, len);
1388 /* Optionally only convert part of the buffer. */
1389 if (items > 1) {
1390 len = SvUV(ST(1));
1391 }
1392 /* Mortalise this right now, as we'll be testing croak()s */
1393 dest = sv_2mortal(newSV(len * 3 / 2 + 1));
1394 if (ix) {
25f2e844 1395 utf16_to_utf8_reversed(source, (U8 *)SvPVX(dest), len, &got);
30685b56 1396 } else {
25f2e844 1397 utf16_to_utf8(source, (U8 *)SvPVX(dest), len, &got);
30685b56
NC
1398 }
1399 SvCUR_set(dest, got);
1400 SvPVX(dest)[got] = '\0';
1401 SvPOK_on(dest);
1402 ST(0) = dest;
1403 XSRETURN(1);
879d0c72 1404
6bd7445c
GG
1405void
1406my_exit(int exitcode)
1407 PPCODE:
1408 my_exit(exitcode);
d97c33b5
DM
1409
1410I32
1411sv_count()
1412 CODE:
1413 RETVAL = PL_sv_count;
1414 OUTPUT:
1415 RETVAL
13b6b3bc
BM
1416
1417void
1418bhk_record(bool on)
1419 CODE:
1420 dMY_CXT;
1421 MY_CXT.bhk_record = on;
1422 if (on)
1423 av_clear(MY_CXT.bhkav);
65bfe90c 1424
defdfed5
Z
1425void
1426test_savehints()
1427 PREINIT:
1428 SV **svp, *sv;
1429 CODE:
1430#define store_hint(KEY, VALUE) \
1431 sv_setiv_mg(*hv_fetchs(GvHV(PL_hintgv), KEY, 1), (VALUE))
1432#define hint_ok(KEY, EXPECT) \
1433 ((svp = hv_fetchs(GvHV(PL_hintgv), KEY, 0)) && \
1434 (sv = *svp) && SvIV(sv) == (EXPECT) && \
1435 (sv = cop_hints_fetchpvs(&PL_compiling, KEY)) && \
1436 SvIV(sv) == (EXPECT))
1437#define check_hint(KEY, EXPECT) \
1438 do { if (!hint_ok(KEY, EXPECT)) croak("fail"); } while(0)
1439 PL_hints |= HINT_LOCALIZE_HH;
1440 ENTER;
1441 SAVEHINTS();
1442 PL_hints &= HINT_INTEGER;
1443 store_hint("t0", 123);
1444 store_hint("t1", 456);
1445 if (PL_hints & HINT_INTEGER) croak("fail");
1446 check_hint("t0", 123); check_hint("t1", 456);
1447 ENTER;
1448 SAVEHINTS();
1449 if (PL_hints & HINT_INTEGER) croak("fail");
1450 check_hint("t0", 123); check_hint("t1", 456);
1451 PL_hints |= HINT_INTEGER;
1452 store_hint("t0", 321);
1453 if (!(PL_hints & HINT_INTEGER)) croak("fail");
1454 check_hint("t0", 321); check_hint("t1", 456);
1455 LEAVE;
1456 if (PL_hints & HINT_INTEGER) croak("fail");
1457 check_hint("t0", 123); check_hint("t1", 456);
1458 ENTER;
1459 SAVEHINTS();
1460 if (PL_hints & HINT_INTEGER) croak("fail");
1461 check_hint("t0", 123); check_hint("t1", 456);
1462 store_hint("t1", 654);
1463 if (PL_hints & HINT_INTEGER) croak("fail");
1464 check_hint("t0", 123); check_hint("t1", 654);
1465 LEAVE;
1466 if (PL_hints & HINT_INTEGER) croak("fail");
1467 check_hint("t0", 123); check_hint("t1", 456);
1468 LEAVE;
1469#undef store_hint
1470#undef hint_ok
1471#undef check_hint
1472
1473void
1474test_copyhints()
1475 PREINIT:
1476 HV *a, *b;
1477 CODE:
1478 PL_hints |= HINT_LOCALIZE_HH;
1479 ENTER;
1480 SAVEHINTS();
1481 sv_setiv_mg(*hv_fetchs(GvHV(PL_hintgv), "t0", 1), 123);
1482 if (SvIV(cop_hints_fetchpvs(&PL_compiling, "t0")) != 123) croak("fail");
1483 a = newHVhv(GvHV(PL_hintgv));
1484 sv_2mortal((SV*)a);
1485 sv_setiv_mg(*hv_fetchs(a, "t0", 1), 456);
1486 if (SvIV(cop_hints_fetchpvs(&PL_compiling, "t0")) != 123) croak("fail");
1487 b = hv_copy_hints_hv(a);
1488 sv_2mortal((SV*)b);
1489 sv_setiv_mg(*hv_fetchs(b, "t0", 1), 789);
1490 if (SvIV(cop_hints_fetchpvs(&PL_compiling, "t0")) != 789) croak("fail");
1491 LEAVE;
1492
201c7e1f
FR
1493void
1494peep_enable ()
1495 PREINIT:
1496 dMY_CXT;
1497 CODE:
1498 av_clear(MY_CXT.peep_recorder);
1499 av_clear(MY_CXT.rpeep_recorder);
1500 MY_CXT.peep_recording = 1;
1501
1502void
1503peep_disable ()
1504 PREINIT:
1505 dMY_CXT;
1506 CODE:
1507 MY_CXT.peep_recording = 0;
1508
1509SV *
1510peep_record ()
1511 PREINIT:
1512 dMY_CXT;
1513 CODE:
95d2461a 1514 RETVAL = newRV_inc((SV *)MY_CXT.peep_recorder);
201c7e1f
FR
1515 OUTPUT:
1516 RETVAL
1517
1518SV *
1519rpeep_record ()
1520 PREINIT:
1521 dMY_CXT;
1522 CODE:
95d2461a 1523 RETVAL = newRV_inc((SV *)MY_CXT.rpeep_recorder);
201c7e1f
FR
1524 OUTPUT:
1525 RETVAL
1526
e2fe06dd
EB
1527BOOT:
1528 {
1529 HV* stash;
1530 SV** meth = NULL;
1531 CV* cv;
1532 stash = gv_stashpv("XS::APItest::TempLv", 0);
1533 if (stash)
1534 meth = hv_fetchs(stash, "make_temp_mg_lv", 0);
1535 if (!meth)
1536 croak("lost method 'make_temp_mg_lv'");
1537 cv = GvCV(*meth);
1538 CvLVALUE_on(cv);
1539 }
83f8bb56
Z
1540
1541BOOT:
1542{
1543 hintkey_rpn_sv = newSVpvs_share("XS::APItest/rpn");
1544 hintkey_calcrpn_sv = newSVpvs_share("XS::APItest/calcrpn");
1545 hintkey_stufftest_sv = newSVpvs_share("XS::APItest/stufftest");
1546 hintkey_swaptwostmts_sv = newSVpvs_share("XS::APItest/swaptwostmts");
1547 next_keyword_plugin = PL_keyword_plugin;
1548 PL_keyword_plugin = my_keyword_plugin;
1549}