PPCODE:
switch (ix) {
case 0:
- cv = newCONSTSUB(stash, pv, SvOK(sv) ? sv : NULL);
+ cv = newCONSTSUB(stash, pv, SvOK(sv) ? SvREFCNT_inc(sv) : NULL);
break;
case 1:
cv = newCONSTSUB_flags(
- stash, pv, len, flags | SvUTF8(name), SvOK(sv) ? sv : NULL
+ stash, pv, len, flags | SvUTF8(name), SvOK(sv) ? SvREFCNT_inc(sv) : NULL
);
break;
}
use strict;
use utf8;
use open qw( :utf8 :std );
-use Test::More tests => 14;
+use Test::More tests => 22;
use XS::APItest;
like $w, qr/Constant subroutine \x{100} redefined at /,
'newCONSTSUB constant redefinition warning + utf8';
}
+
+# XS::APItest was not handling references correctly here
+
+package Counter {
+ our $count = 0;
+
+ sub new {
+ ++$count;
+ my $o = bless [];
+ return $o;
+ }
+
+ sub DESTROY {
+ --$count;
+ }
+};
+
+foreach (['newCONSTSUB', 'ZZIP'],
+ ['newCONSTSUB_flags', 'BRRRAPP']) {
+ my ($using, $name) = @$_;
+ is($Counter::count, 0, 'No objects exist before we start');
+ my $sub = XS::APItest->can($using);
+ ($const, $glob) = $sub->(\%::, $name, 0, Counter->new());
+ is($const, 1, "subroutine generated by $using is CvCONST");
+ is($Counter::count, 1, '1 object now exists');
+ {
+ no warnings 'redefine';
+ *$glob = sub () {};
+ }
+ is($Counter::count, 0, 'no objects remain');
+}