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