--- CBOR-XS/XS.xs 2013/10/27 20:40:25 1.10 +++ CBOR-XS/XS.xs 2013/10/28 22:50:50 1.13 @@ -63,7 +63,7 @@ #endif static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: -static SV *types_true, *types_false, *types_error; +static SV *types_true, *types_false, *types_error, *sv_cbor; typedef struct { U32 flags; @@ -271,11 +271,14 @@ ? cbor_tagged_stash : gv_stashpv ("CBOR::XS::Tagged" , 1); - if (SvSTASH (sv) == boolean_stash) + HV *stash = SvSTASH (sv); + GV *method; + + if (stash == boolean_stash) encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); - else if (SvSTASH (sv) == error_stash) + else if (stash == error_stash) encode_ch (enc, 0xe0 | 23); - else if (SvSTASH (sv) == tagged_stash) + else if (stash == tagged_stash) { if (svt != SVt_PVAV) croak ("encountered CBOR::XS::Tagged object that isn't an array"); @@ -283,38 +286,61 @@ encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); } - else + else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) { + dSP; + + ENTER; SAVETMPS; PUSHMARK (SP); // we re-bless the reference to get overload and other niceties right - GV *to_cbor = gv_fetchmethod_autoload (SvSTASH (sv), "TO_CBOR", 0); + XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); - if (to_cbor) - { - dSP; + PUTBACK; + // G_SCALAR ensures that return value is 1 + call_sv ((SV *)GvCV (method), G_SCALAR); + SPAGAIN; - ENTER; SAVETMPS; PUSHMARK (SP); - XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); + // catch this surprisingly common error + if (SvROK (TOPs) && SvRV (TOPs) == sv) + croak ("%s::TO_CBOR method returned same object as was passed instead of a new one", HvNAME (stash)); - // calling with G_SCALAR ensures that we always get a 1 return value - PUTBACK; - call_sv ((SV *)GvCV (to_cbor), G_SCALAR); - SPAGAIN; + encode_sv (enc, POPs); - // catch this surprisingly common error - if (SvROK (TOPs) && SvRV (TOPs) == sv) - croak ("%s::TO_CBOR method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); + PUTBACK; - sv = POPs; - PUTBACK; + FREETMPS; LEAVE; + } + else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) + { + dSP; + + ENTER; SAVETMPS; PUSHMARK (SP); + EXTEND (SP, 2); + // we re-bless the reference to get overload and other niceties right + PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); + PUSHs (sv_cbor); - encode_sv (enc, sv); + PUTBACK; + int count = call_sv ((SV *)GvCV (method), G_ARRAY); + SPAGAIN; - FREETMPS; LEAVE; - } - else - croak ("encountered object '%s', but no TO_CBOR method available on it", - SvPV_nolen (sv_2mortal (newRV_inc (sv)))); + // catch this surprisingly common error + if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) + croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); + + encode_uint (enc, 0xc0, CBOR_TAG_PERL_OBJECT); + encode_uint (enc, 0x80, count + 1); + encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); + + while (count) + encode_sv (enc, SP[1 - count--]); + + PUTBACK; + + FREETMPS; LEAVE; } + else + croak ("encountered object '%s', but no TO_CBOR or FREEZE methods available on it", + SvPV_nolen (sv_2mortal (newRV_inc (sv)))); } else if (svt == SVt_PVHV) encode_hv (enc, (HV *)sv); @@ -574,6 +600,7 @@ SV *v = decode_sv (dec); hv_store_ent (hv, k, v, 0); + SvREFCNT_dec (k); } } else @@ -586,6 +613,7 @@ SV *v = decode_sv (dec); hv_store_ent (hv, k, v, 0); + SvREFCNT_dec (k); } } @@ -650,24 +678,61 @@ if (tag == CBOR_TAG_MAGIC) return sv; - - if (tag == CBOR_TAG_PERL_OBJECT) + else if (tag == CBOR_TAG_PERL_OBJECT) { if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) ERR ("corrupted CBOR data (non-array perl object)"); + + AV *av = (AV *)SvRV (sv); + int len = av_len (av) + 1; + HV *stash = gv_stashsv (*av_fetch (av, 0, 1), 0); + + if (!stash) + ERR ("cannot decode perl-object (package does not exist)"); + + GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); - // TODO - } + if (!method) + ERR ("cannot decode perl-object (package does not have a THAW method)"); + + dSP; - AV *av = newAV (); - av_push (av, newSVuv (tag)); - av_push (av, sv); + ENTER; SAVETMPS; PUSHMARK (SP); + EXTEND (SP, len + 1); + // we re-bless the reference to get overload and other niceties right + PUSHs (*av_fetch (av, 0, 1)); + PUSHs (sv_cbor); + + int i; + + for (i = 1; i < len; ++i) + PUSHs (*av_fetch (av, i, 1)); + + PUTBACK; + call_sv ((SV *)GvCV (method), G_SCALAR); + SPAGAIN; - HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash - ? cbor_tagged_stash - : gv_stashpv ("CBOR::XS::Tagged" , 1); + SvREFCNT_dec (sv); + sv = SvREFCNT_inc (POPs); - return sv_bless (newRV_noinc ((SV *)av), tagged_stash); + PUTBACK; + + FREETMPS; LEAVE; + + return sv; + } + else + { + AV *av = newAV (); + av_push (av, newSVuv (tag)); + av_push (av, sv); + + HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash + ? cbor_tagged_stash + : gv_stashpv ("CBOR::XS::Tagged" , 1); + + return sv_bless (newRV_noinc ((SV *)av), tagged_stash); + } fail: SvREFCNT_dec (sv); @@ -848,6 +913,9 @@ types_true = get_bool ("Types::Serialiser::true" ); types_false = get_bool ("Types::Serialiser::false"); types_error = get_bool ("Types::Serialiser::error"); + + sv_cbor = newSVpv ("CBOR", 0); + SvREADONLY_on (sv_cbor); } PROTOTYPES: DISABLE