| 1 | package # hide from CPAN | ||
| 2 | XS::Framework::ParseXS; | ||
| 3 | use 5.012; | ||
| 4 | use XS::Install::ParseXS; | ||
| 5 | |||
| 6 | my $tm_cast = 'T_TYPEMAP_CAST'; | ||
| 7 | |||
| 8 | XS::Install::ParseXS::add_no_typemap_callback(sub { | ||
| 9 | my ($typemaps, %args) = @_; | ||
| 10 | |||
| 11 | $typemaps->add_typemap( | ||
| 12 | ctype => $args{ctype}, | ||
| 13 | xstype => $tm_cast, | ||
| 14 | ); | ||
| 15 | |||
| 16 | unless ($typemaps->get_inputmap(xstype => $tm_cast)) { | ||
| 17 | $typemaps->add_inputmap( | ||
| 18 | xstype => $tm_cast, | ||
| 19 | code => '$var = xs::in<$type>($arg);'. | ||
| 20 | '${\\( $var eq q{THIS} ? qq{ if (!SvOK($arg)) throw \\"undef not allowed as THIS\\";} : q{} )}', | ||
| 21 | ); | ||
| 22 | $typemaps->add_outputmap( | ||
| 23 | xstype => $tm_cast, | ||
| 24 | code => '$arg = xs::out($var, PROTO).detach();', | ||
| 25 | ); | ||
| 26 | } | ||
| 27 | }); | ||
| 28 | |||
| 29 | XS::Install::ParseXS::add_pre_callback(sub { | ||
| 30 | my ($parser, $ctx) = @_; | ||
| 31 | my $lines = $parser->{line}; | ||
| 32 | my $linno = $parser->{line_no}; | ||
| 33 | my $func = $ctx->{func}; | ||
| 34 | my $args = $ctx->{args}; | ||
| 35 | my $fa = $args->[0]; | ||
| 36 | |||
| 37 | my $is_empty = XS::Install::ParseXS::is_empty($lines); | ||
| 38 | my $to = $parser->{typemap} || $parser->{typemaps_object}; # old/new ParseXS | ||
| 39 | |||
| 40 | if ($func eq 'DESTROY' and $fa and $fa->{name} eq 'THIS') { | ||
| 41 | my $in_tmap = $to->get_inputmap(ctype => $fa->{type}); | ||
| 42 | XS::Install::ParseXS::insert_code_bottom($parser, " xs::Typemap<$fa->{type}>().destroy($fa->{name}, SvRV(ST(0)));") | ||
| 43 | if $in_tmap && $in_tmap->xstype eq $tm_cast; | ||
| 44 | } | ||
| 45 | |||
| 46 | my $ret = $ctx->{ret}; | ||
| 47 | if ($ret !~ /^(void|SV\s*\*|bool)/) {{ | ||
| 48 | my $out_tmap = $to->get_outputmap(ctype => $ret); | ||
| 49 | last unless $out_tmap and $out_tmap->xstype eq $tm_cast; | ||
| 50 | |||
| 51 | if ($func eq 'new') { | ||
| 52 | XS::Install::ParseXS::insert_code_top($parser, " PROTO = $fa->{name};") if $fa->{name} and $fa->{name} !~ /_____unused/; | ||
| 53 | XS::Install::ParseXS::insert_code_bottom($parser, " RETVAL = ".XS::Install::ParseXS::default_constructor($ret, $args).';') if $is_empty; | ||
| 54 | } | ||
| 55 | |||
| 56 | XS::Install::ParseXS::insert_code_top($parser, " xs::Sv PROTO; PERL_UNUSED_VAR(PROTO);"); | ||
| 57 | }} | ||
| 58 | |||
| 59 | }); | ||
| 60 | |||
| 61 | XS::Install::ParseXS::add_post_callback(sub { | ||
| 62 | my $outref = shift; | ||
| 63 | if ($XS::Install::ParseXS::cplus) { | ||
| 64 | #wrap content of XSUBs into try-catch blocks | ||
| 65 | $$outref =~ s/$XS::Install::ParseXS::re_xsub/$1 { xs::throw_guard(cv, [=]() \n$2); }/g; | ||
| 66 | #wrap content of BOOT into try-catch blocks | ||
| 67 | $$outref =~ s/$XS::Install::ParseXS::re_boot/$1 { xs::throw_guard(cv, [=]() mutable\n$2); }/g; | ||
| 68 | } | ||
| 69 | }); | ||
| 70 | |||
| 71 | 1; |