Skip to content

Commit

Permalink
fix convert_blessed <= 5.8 and enable test
Browse files Browse the repository at this point in the history
with 5.8 we need to rebless the rv to set AMG.
For safety recalc the overload magic methods also.

Fixes GH #37 for 5.8
  • Loading branch information
Reini Urban committed Nov 26, 2015
1 parent cc58e5e commit a8354e6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
17 changes: 17 additions & 0 deletions XS.xs
Expand Up @@ -860,12 +860,21 @@ encode_stringify(pTHX_ enc_t *enc, SV *sv)
SV *pv = NULL;
svtype type = SvTYPE(sv);
int amg = 0;
#if PERL_VERSION <= 8
MAGIC *mg;
#endif

/* SvAMAGIC without the ref */
#if PERL_VERSION > 17
#define MyAMG(sv) (SvOBJECT(sv) && HvAMAGIC(SvSTASH(sv)))
#else
#if PERL_VERSION > 8
#define MyAMG(sv) (SvOBJECT(sv) && (SvFLAGS(sv) & SVf_AMAGIC))
#else
#define MyAMG(sv) (SvOBJECT(sv) && ((SvFLAGS(sv) & SVf_AMAGIC) \
|| ((mg = mg_find((SV*)SvSTASH(sv), PERL_MAGIC_overload_table)) \
&& mg->mg_ptr && AMT_AMAGIC((AMT*)mg->mg_ptr))))
#endif
#endif

/* if no string overload found, check allow_blessed */
Expand Down Expand Up @@ -897,6 +906,14 @@ encode_stringify(pTHX_ enc_t *enc, SV *sv)
if (SvGMAGICAL(sv)) mg_get(sv);
if (MyAMG(sv)) { /* force a RV here */
SV* rv = newRV(SvREFCNT_inc(sv));
#if PERL_VERSION <= 8
HV *stash = SvSTASH(sv);
if (!SvSTASH(rv) || !(SvFLAGS(sv) & SVf_AMAGIC)) {
sv_bless(rv, stash);
Gv_AMupdate(stash);
SvFLAGS(sv) |= SVf_AMAGIC;
}
#endif
#if PERL_VERSION > 13
pv = AMG_CALLunary(rv, string_amg);
#else
Expand Down
8 changes: 6 additions & 2 deletions t/12_blessed.t
Expand Up @@ -41,7 +41,11 @@ ok ($js->encode ($o1) eq '{"__":""}');
ok ($js->encode ($o2) eq "null");
$js->allow_blessed->convert_blessed;
ok ($js->encode ($o1) eq '{"__":""}');
ok ($js->encode ($o2) == 1) or print STDERR "# ",$js->encode ($o2),"\n";
if ($] < 5.008) {
print "ok ",++$test," # skip 5.6\n"
} else {
ok ($js->encode ($o2) == 1) or print STDERR "# ",$js->encode ($o2),"\n";
}

$js->filter_json_object (sub { 5 });
$js->filter_json_single_key_object (a => sub { shift });
Expand All @@ -64,7 +68,7 @@ $js->filter_json_single_key_object ("a");
ok (4 == $js->decode ('[{"a":4}]')->[0]{a});

if ($]<5.008) {
print "ok 16 # skip 5.6\n";
print "ok 18 # skip 5.6\n";
} else {
$js->filter_json_single_key_object (a => sub { });
ok (4 == $js->decode ('[{"a":4}]')->[0]{a});
Expand Down
17 changes: 5 additions & 12 deletions t/54_stringify.t
Expand Up @@ -10,8 +10,7 @@ BEGIN {
}
}
use Time::Piece;
plan tests => 5;
#$] < 5.008 ? (skip_all => "5.6") : (tests => 5);
plan $] < 5.008 ? (skip_all => "5.6 no AMG yet") : (tests => 5);
use Cpanel::JSON::XS;

my $time = localtime;
Expand All @@ -32,20 +31,14 @@ package main;
my $object = bless ["foo"], 'Foo';
my $enc = $json->encode( { obj => $object } );

TODO: {
#local $TODO = 'Not yet ready';
is( $enc, '{"obj":"Foo <foo>"}', "mg object stringified" )
or diag($enc);
}
is( $enc, '{"obj":"Foo <foo>"}', "mg object stringified" )
or diag($enc);

$enc = $json->encode( { time => $time } );
isa_ok($time, "Time::Piece");

TODO: {
#local $TODO = 'Not yet ready';
# my $dec = $json->decode($enc);
is( $enc, qq({"time":"$time"}), 'mg Time::Piece object was stringified' );
}
# my $dec = $json->decode($enc);
is( $enc, qq({"time":"$time"}), 'mg Time::Piece object was stringified' );

$object = bless [], 'main';
$json->allow_blessed;
Expand Down

0 comments on commit a8354e6

Please sign in to comment.