… | |
… | |
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); |
… | |
… | |
572 | |
598 | |
573 | SV *k = decode_sv (dec); |
599 | SV *k = decode_sv (dec); |
574 | SV *v = decode_sv (dec); |
600 | SV *v = decode_sv (dec); |
575 | |
601 | |
576 | hv_store_ent (hv, k, v, 0); |
602 | hv_store_ent (hv, k, v, 0); |
|
|
603 | SvREFCNT_dec (k); |
577 | } |
604 | } |
578 | } |
605 | } |
579 | else |
606 | else |
580 | { |
607 | { |
581 | int len = decode_uint (dec); |
608 | int len = decode_uint (dec); |
… | |
… | |
584 | { |
611 | { |
585 | SV *k = decode_sv (dec); |
612 | SV *k = decode_sv (dec); |
586 | SV *v = decode_sv (dec); |
613 | SV *v = decode_sv (dec); |
587 | |
614 | |
588 | hv_store_ent (hv, k, v, 0); |
615 | hv_store_ent (hv, k, v, 0); |
|
|
616 | SvREFCNT_dec (k); |
589 | } |
617 | } |
590 | } |
618 | } |
591 | |
619 | |
592 | DEC_DEC_DEPTH; |
620 | DEC_DEC_DEPTH; |
593 | return newRV_noinc ((SV *)hv); |
621 | return newRV_noinc ((SV *)hv); |
… | |
… | |
648 | UV tag = decode_uint (dec); |
676 | UV tag = decode_uint (dec); |
649 | SV *sv = decode_sv (dec); |
677 | SV *sv = decode_sv (dec); |
650 | |
678 | |
651 | if (tag == CBOR_TAG_MAGIC) |
679 | if (tag == CBOR_TAG_MAGIC) |
652 | return sv; |
680 | return sv; |
653 | |
|
|
654 | if (tag == CBOR_TAG_PERL_OBJECT) |
681 | else if (tag == CBOR_TAG_PERL_OBJECT) |
655 | { |
682 | { |
656 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
683 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
657 | ERR ("corrupted CBOR data (non-array perl object)"); |
684 | ERR ("corrupted CBOR data (non-array perl object)"); |
|
|
685 | |
|
|
686 | AV *av = (AV *)SvRV (sv); |
|
|
687 | int len = av_len (av) + 1; |
|
|
688 | HV *stash = gv_stashsv (*av_fetch (av, 0, 1), 0); |
|
|
689 | |
|
|
690 | if (!stash) |
|
|
691 | ERR ("cannot decode perl-object (package does not exist)"); |
|
|
692 | |
|
|
693 | GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); |
658 | |
694 | |
659 | // TODO |
695 | if (!method) |
|
|
696 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
|
|
697 | |
|
|
698 | dSP; |
|
|
699 | |
|
|
700 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
701 | EXTEND (SP, len + 1); |
|
|
702 | // we re-bless the reference to get overload and other niceties right |
|
|
703 | PUSHs (*av_fetch (av, 0, 1)); |
|
|
704 | PUSHs (sv_cbor); |
|
|
705 | |
|
|
706 | int i; |
|
|
707 | |
|
|
708 | for (i = 1; i < len; ++i) |
|
|
709 | PUSHs (*av_fetch (av, i, 1)); |
|
|
710 | |
|
|
711 | PUTBACK; |
|
|
712 | call_sv ((SV *)GvCV (method), G_SCALAR); |
|
|
713 | SPAGAIN; |
|
|
714 | |
|
|
715 | SvREFCNT_dec (sv); |
|
|
716 | sv = SvREFCNT_inc (POPs); |
|
|
717 | |
|
|
718 | PUTBACK; |
|
|
719 | |
|
|
720 | FREETMPS; LEAVE; |
|
|
721 | |
|
|
722 | return sv; |
|
|
723 | } |
|
|
724 | else |
660 | } |
725 | { |
661 | |
|
|
662 | AV *av = newAV (); |
726 | AV *av = newAV (); |
663 | av_push (av, newSVuv (tag)); |
727 | av_push (av, newSVuv (tag)); |
664 | av_push (av, sv); |
728 | av_push (av, sv); |
665 | |
729 | |
666 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
730 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
667 | ? cbor_tagged_stash |
731 | ? cbor_tagged_stash |
668 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
732 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
669 | |
733 | |
670 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
734 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
|
|
735 | } |
671 | |
736 | |
672 | fail: |
737 | fail: |
673 | SvREFCNT_dec (sv); |
738 | SvREFCNT_dec (sv); |
674 | return &PL_sv_undef; |
739 | return &PL_sv_undef; |
675 | } |
740 | } |
… | |
… | |
846 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
911 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
847 | |
912 | |
848 | types_true = get_bool ("Types::Serialiser::true" ); |
913 | types_true = get_bool ("Types::Serialiser::true" ); |
849 | types_false = get_bool ("Types::Serialiser::false"); |
914 | types_false = get_bool ("Types::Serialiser::false"); |
850 | types_error = get_bool ("Types::Serialiser::error"); |
915 | types_error = get_bool ("Types::Serialiser::error"); |
|
|
916 | |
|
|
917 | sv_cbor = newSVpv ("CBOR", 0); |
|
|
918 | SvREADONLY_on (sv_cbor); |
851 | } |
919 | } |
852 | |
920 | |
853 | PROTOTYPES: DISABLE |
921 | PROTOTYPES: DISABLE |
854 | |
922 | |
855 | void CLONE (...) |
923 | void CLONE (...) |