… | |
… | |
61 | # define CBOR_SLOW 0 |
61 | # define CBOR_SLOW 0 |
62 | # define CBOR_STASH cbor_stash |
62 | # define CBOR_STASH cbor_stash |
63 | #endif |
63 | #endif |
64 | |
64 | |
65 | static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: |
65 | static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: |
66 | static SV *types_true, *types_false, *types_error; |
66 | static SV *types_true, *types_false, *types_error, *sv_cbor; |
67 | |
67 | |
68 | typedef struct { |
68 | typedef struct { |
69 | U32 flags; |
69 | U32 flags; |
70 | U32 max_depth; |
70 | U32 max_depth; |
71 | STRLEN max_size; |
71 | STRLEN max_size; |
… | |
… | |
269 | : gv_stashpv ("Types::Serialiser::Error", 1); |
269 | : gv_stashpv ("Types::Serialiser::Error", 1); |
270 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
270 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
271 | ? cbor_tagged_stash |
271 | ? cbor_tagged_stash |
272 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
272 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
273 | |
273 | |
|
|
274 | HV *stash = SvSTASH (sv); |
|
|
275 | GV *method; |
|
|
276 | |
274 | if (SvSTASH (sv) == boolean_stash) |
277 | if (stash == boolean_stash) |
275 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
278 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
276 | else if (SvSTASH (sv) == error_stash) |
279 | else if (stash == error_stash) |
277 | encode_ch (enc, 0xe0 | 23); |
280 | encode_ch (enc, 0xe0 | 23); |
278 | else if (SvSTASH (sv) == tagged_stash) |
281 | else if (stash == tagged_stash) |
279 | { |
282 | { |
280 | if (svt != SVt_PVAV) |
283 | if (svt != SVt_PVAV) |
281 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
284 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
282 | |
285 | |
283 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
286 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
284 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
287 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
285 | } |
288 | } |
|
|
289 | else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
|
|
290 | { |
|
|
291 | dSP; |
|
|
292 | |
|
|
293 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
294 | // we re-bless the reference to get overload and other niceties right |
|
|
295 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
|
|
296 | |
|
|
297 | PUTBACK; |
|
|
298 | // G_SCALAR ensures that return value is 1 |
|
|
299 | call_sv ((SV *)GvCV (method), G_SCALAR); |
|
|
300 | SPAGAIN; |
|
|
301 | |
|
|
302 | // catch this surprisingly common error |
|
|
303 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
304 | croak ("%s::TO_CBOR method returned same object as was passed instead of a new one", HvNAME (stash)); |
|
|
305 | |
|
|
306 | encode_sv (enc, POPs); |
|
|
307 | |
|
|
308 | PUTBACK; |
|
|
309 | |
|
|
310 | FREETMPS; LEAVE; |
|
|
311 | } |
|
|
312 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
|
|
313 | { |
|
|
314 | dSP; |
|
|
315 | |
|
|
316 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
317 | EXTEND (SP, 2); |
|
|
318 | // we re-bless the reference to get overload and other niceties right |
|
|
319 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
|
|
320 | PUSHs (sv_cbor); |
|
|
321 | |
|
|
322 | PUTBACK; |
|
|
323 | int count = call_sv ((SV *)GvCV (method), G_ARRAY); |
|
|
324 | SPAGAIN; |
|
|
325 | |
|
|
326 | // catch this surprisingly common error |
|
|
327 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
328 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
|
|
329 | |
|
|
330 | encode_uint (enc, 0xc0, CBOR_TAG_PERL_OBJECT); |
|
|
331 | encode_uint (enc, 0x80, count + 1); |
|
|
332 | encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
|
|
333 | |
|
|
334 | while (count) |
|
|
335 | encode_sv (enc, SP[1 - count--]); |
|
|
336 | |
|
|
337 | PUTBACK; |
|
|
338 | |
|
|
339 | FREETMPS; LEAVE; |
|
|
340 | } |
286 | else |
341 | else |
287 | { |
|
|
288 | // we re-bless the reference to get overload and other niceties right |
|
|
289 | GV *to_cbor = gv_fetchmethod_autoload (SvSTASH (sv), "TO_CBOR", 0); |
|
|
290 | |
|
|
291 | if (to_cbor) |
|
|
292 | { |
|
|
293 | dSP; |
|
|
294 | |
|
|
295 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
296 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
297 | |
|
|
298 | // calling with G_SCALAR ensures that we always get a 1 return value |
|
|
299 | PUTBACK; |
|
|
300 | call_sv ((SV *)GvCV (to_cbor), G_SCALAR); |
|
|
301 | SPAGAIN; |
|
|
302 | |
|
|
303 | // catch this surprisingly common error |
|
|
304 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
305 | croak ("%s::TO_CBOR method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
306 | |
|
|
307 | sv = POPs; |
|
|
308 | PUTBACK; |
|
|
309 | |
|
|
310 | encode_sv (enc, sv); |
|
|
311 | |
|
|
312 | FREETMPS; LEAVE; |
|
|
313 | } |
|
|
314 | else |
|
|
315 | croak ("encountered object '%s', but no TO_CBOR method available on it", |
342 | croak ("encountered object '%s', but no TO_CBOR or FREEZE methods available on it", |
316 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
343 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
317 | } |
|
|
318 | } |
344 | } |
319 | else if (svt == SVt_PVHV) |
345 | else if (svt == SVt_PVHV) |
320 | encode_hv (enc, (HV *)sv); |
346 | encode_hv (enc, (HV *)sv); |
321 | else if (svt == SVt_PVAV) |
347 | else if (svt == SVt_PVAV) |
322 | encode_av (enc, (AV *)sv); |
348 | encode_av (enc, (AV *)sv); |
… | |
… | |
648 | UV tag = decode_uint (dec); |
674 | UV tag = decode_uint (dec); |
649 | SV *sv = decode_sv (dec); |
675 | SV *sv = decode_sv (dec); |
650 | |
676 | |
651 | if (tag == CBOR_TAG_MAGIC) |
677 | if (tag == CBOR_TAG_MAGIC) |
652 | return sv; |
678 | return sv; |
653 | |
|
|
654 | if (tag == CBOR_TAG_PERL_OBJECT) |
679 | else if (tag == CBOR_TAG_PERL_OBJECT) |
655 | { |
680 | { |
656 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
681 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
657 | ERR ("corrupted CBOR data (non-array perl object)"); |
682 | ERR ("corrupted CBOR data (non-array perl object)"); |
|
|
683 | |
|
|
684 | AV *av = (AV *)SvRV (sv); |
|
|
685 | int len = av_len (av) + 1; |
|
|
686 | HV *stash = gv_stashsv (*av_fetch (av, 0, 1), 0); |
|
|
687 | |
|
|
688 | if (!stash) |
|
|
689 | ERR ("cannot decode perl-object (package does not exist)"); |
|
|
690 | |
|
|
691 | GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); |
658 | |
692 | |
659 | // TODO |
693 | if (!method) |
|
|
694 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
|
|
695 | |
|
|
696 | dSP; |
|
|
697 | |
|
|
698 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
699 | EXTEND (SP, len + 1); |
|
|
700 | // we re-bless the reference to get overload and other niceties right |
|
|
701 | PUSHs (*av_fetch (av, 0, 1)); |
|
|
702 | PUSHs (sv_cbor); |
|
|
703 | |
|
|
704 | int i; |
|
|
705 | |
|
|
706 | for (i = 1; i < len; ++i) |
|
|
707 | PUSHs (*av_fetch (av, i, 1)); |
|
|
708 | |
|
|
709 | PUTBACK; |
|
|
710 | call_sv ((SV *)GvCV (method), G_SCALAR); |
|
|
711 | SPAGAIN; |
|
|
712 | |
|
|
713 | sv = SvREFCNT_inc (POPs); |
|
|
714 | |
|
|
715 | PUTBACK; |
|
|
716 | |
|
|
717 | FREETMPS; LEAVE; |
|
|
718 | |
|
|
719 | return sv; |
|
|
720 | } |
|
|
721 | else |
660 | } |
722 | { |
661 | |
|
|
662 | AV *av = newAV (); |
723 | AV *av = newAV (); |
663 | av_push (av, newSVuv (tag)); |
724 | av_push (av, newSVuv (tag)); |
664 | av_push (av, sv); |
725 | av_push (av, sv); |
665 | |
726 | |
666 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
727 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
667 | ? cbor_tagged_stash |
728 | ? cbor_tagged_stash |
668 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
729 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
669 | |
730 | |
670 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
731 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
|
|
732 | } |
671 | |
733 | |
672 | fail: |
734 | fail: |
673 | SvREFCNT_dec (sv); |
735 | SvREFCNT_dec (sv); |
674 | return &PL_sv_undef; |
736 | return &PL_sv_undef; |
675 | } |
737 | } |
… | |
… | |
846 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
908 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
847 | |
909 | |
848 | types_true = get_bool ("Types::Serialiser::true" ); |
910 | types_true = get_bool ("Types::Serialiser::true" ); |
849 | types_false = get_bool ("Types::Serialiser::false"); |
911 | types_false = get_bool ("Types::Serialiser::false"); |
850 | types_error = get_bool ("Types::Serialiser::error"); |
912 | types_error = get_bool ("Types::Serialiser::error"); |
|
|
913 | |
|
|
914 | sv_cbor = newSVpv ("CBOR", 0); |
|
|
915 | SvREADONLY_on (sv_cbor); |
851 | } |
916 | } |
852 | |
917 | |
853 | PROTOTYPES: DISABLE |
918 | PROTOTYPES: DISABLE |
854 | |
919 | |
855 | void CLONE (...) |
920 | void CLONE (...) |