Skip to content

Commit c07b5e5

Browse files
committed
ParseXS: make OUTPUT: SETMAGIC per-CASE
In code like: int foo(int a, int b) CASE: X OUTPUT: a SETMAGIC: DISABLE b CASE: Y OUTPUT: a SETMAGIC: DISABLE b when the caller's two args are updated with the final values of a and b, the first CASE calls SvSETMAGIC() after updating ST(0), but not after updating ST(1). This is as expected. However in the second CASE, the current setting of SETMAGIC (i.e disabled) from the first CASE is passed on and "inherited' by the second CASE: so in the second branch, magic isn't called on ST(0). This commit fixes that (i.e. calls SvSETMAGIC on ST(0) in the second CASE) by making the current SETMAGIC state per-xbody rather than per-xsub. This commit achieves that by: Remove the following field from the ExtUtils::ParseXS class: xsub_SETMAGIC_state and replace it with this new field in the ExtUtils::ParseXS::Node::xbody class: OUTPUT_SETMAGIC_state and adds 4 tests (the latter two of which failed before this commit).
1 parent bb670b1 commit c07b5e5

File tree

3 files changed

+48
-14
lines changed

3 files changed

+48
-14
lines changed

dist/ExtUtils-ParseXS/lib/ExtUtils/ParseXS.pm

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,6 @@ BEGIN {
254254
# whether that method has been listed by
255255
# OVERLOAD (for duplicate spotting).
256256

257-
# Per-XSUB OUTPUT section parsing state:
258-
259-
'xsub_SETMAGIC_state', # Bool: most recent value of SETMAGIC in an
260-
# OUTPUT section.
261-
262257
# Per-XSUB code-emitting state:
263258

264259
'xsub_deferred_code_lines', # A multi-line string containing lines of
@@ -654,11 +649,6 @@ EOM
654649
# Parse and code-emit an XSUB
655650
# ----------------------------------------------------------------
656651

657-
# Initialise more per-XSUB state
658-
659-
$self->{xsub_SETMAGIC_state} = 1; # SETMAGIC: ENABLE
660-
661-
662652
unshift @{$self->{line}}, $_;
663653
my $xsub = ExtUtils::ParseXS::Node::xsub->new();
664654
$self->{cur_xsub} = $xsub;

dist/ExtUtils-ParseXS/lib/ExtUtils/ParseXS/Node.pm

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2238,6 +2238,9 @@ BEGIN { $build_subclass->('', # parent
22382238

22392239
'seen_RETVAL_in_CODE', # Bool: have seen 'RETVAL' within a CODE block
22402240
'seen_autocall', # Bool: this xbody has an autocall node
2241+
'OUTPUT_SETMAGIC_state', # Bool: most recent value of SETMAGIC in an
2242+
# OUTPUT section.
2243+
22412244

22422245
# State during code emitting
22432246

@@ -2281,6 +2284,9 @@ sub parse {
22812284
$self->{ioparams} = $ioparams;
22822285
}
22832286

2287+
# by default, OUTPUT entries have SETMAGIC: ENABLE
2288+
$self->{OUTPUT_SETMAGIC_state} = 1;
2289+
22842290
for my $part (qw(input_part init_part code_part output_part cleanup_part)) {
22852291
my $kid = "ExtUtils::ParseXS::Node::$part"->new();
22862292
if ($kid->parse($pxs)) {
@@ -4107,12 +4113,12 @@ sub parse {
41074113
# set some sane default values in case we do one of the early returns
41084114
# below
41094115

4110-
$self->{do_setmagic} = $pxs->{xsub_SETMAGIC_state};
4116+
$self->{do_setmagic} = $pxs->{cur_xbody}{OUTPUT_SETMAGIC_state};
41114117
$self->{is_setmagic} = 0;
41124118

41134119
if ($line =~ /^\s*SETMAGIC\s*:\s*(ENABLE|DISABLE)\s*/) {
4114-
$pxs->{xsub_SETMAGIC_state} = ($1 eq "ENABLE" ? 1 : 0);
4115-
$self->{do_setmagic} = $pxs->{xsub_SETMAGIC_state};
4120+
$pxs->{cur_xbody}{OUTPUT_SETMAGIC_state} = ($1 eq "ENABLE" ? 1 : 0);
4121+
$self->{do_setmagic} = $pxs->{cur_xbody}{OUTPUT_SETMAGIC_state};
41164122
$self->{is_setmagic} = 1;
41174123
return;
41184124
}
@@ -4152,7 +4158,7 @@ sub parse {
41524158
$param->{in_output} = 1;
41534159
$param->{do_setmagic} = $outarg eq 'RETVAL'
41544160
? 0 # RETVAL never needs magic setting
4155-
: $pxs->{xsub_SETMAGIC_state};
4161+
: $pxs->{cur_xbody}{OUTPUT_SETMAGIC_state};
41564162
$self->{code} = $param->{output_code} = $outcode if length $outcode;
41574163

41584164
1;

dist/ExtUtils-ParseXS/t/001-basic.t

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2560,6 +2560,44 @@ EOF
25602560
[ 0, 0, qr/\QXSRETURN(1)/, "has XSRETURN" ],
25612561
],
25622562
2563+
[
2564+
"OUTPUT vars with set magic mixture per-CASE",
2565+
[ Q(<<'EOF') ],
2566+
|int
2567+
|foo(int a, int b)
2568+
| CASE: X
2569+
| OUTPUT:
2570+
| a
2571+
| SETMAGIC: DISABLE
2572+
| b
2573+
| CASE: Y
2574+
| OUTPUT:
2575+
| a
2576+
| SETMAGIC: DISABLE
2577+
| b
2578+
EOF
2579+
[ 0, 0, qr{\Qif (X)\E
2580+
.*
2581+
\QSvSETMAGIC(ST(0));\E
2582+
.*
2583+
\Qelse if (Y)\E
2584+
}sx, "X: set magic ST(0)" ],
2585+
[ 0, 1, qr{\Qif (X)\E
2586+
.*
2587+
\QSvSETMAGIC(ST(1));\E
2588+
.*
2589+
\Qelse if (Y)\E
2590+
}sx, "X: no magic ST(1)" ],
2591+
[ 0, 0, qr{\Qelse if (Y)\E
2592+
.*
2593+
\QSvSETMAGIC(ST(0));\E
2594+
}sx, "Y: set magic ST(0)" ],
2595+
[ 0, 1, qr{\Qelse if (Y)\E
2596+
.*
2597+
\QSvSETMAGIC(ST(1));\E
2598+
}sx, "Y: no magic ST(1)" ],
2599+
],
2600+
25632601
[
25642602
"duplicate OUTPUT RETVAL",
25652603
[ Q(<<'EOF') ],

0 commit comments

Comments
 (0)