… | |
… | |
60 | #else |
60 | #else |
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, *cbor_boolean_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 *cbor_true, *cbor_false; |
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; |
… | |
… | |
259 | SvGETMAGIC (sv); |
259 | SvGETMAGIC (sv); |
260 | svt = SvTYPE (sv); |
260 | svt = SvTYPE (sv); |
261 | |
261 | |
262 | if (ecb_expect_false (SvOBJECT (sv))) |
262 | if (ecb_expect_false (SvOBJECT (sv))) |
263 | { |
263 | { |
264 | HV *boolean_stash = !CBOR_SLOW || cbor_boolean_stash |
264 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
265 | ? cbor_boolean_stash |
265 | ? types_boolean_stash |
266 | : gv_stashpv ("CBOR::XS::Boolean", 1); |
266 | : gv_stashpv ("Types::Serialiser::Boolean", 1); |
|
|
267 | HV *error_stash = !CBOR_SLOW || types_error_stash |
|
|
268 | ? types_error_stash |
|
|
269 | : gv_stashpv ("Types::Serialiser::Error", 1); |
267 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
270 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
268 | ? cbor_tagged_stash |
271 | ? cbor_tagged_stash |
269 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
272 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
270 | |
273 | |
|
|
274 | HV *stash = SvSTASH (sv); |
|
|
275 | GV *method; |
|
|
276 | |
271 | if (SvSTASH (sv) == boolean_stash) |
277 | if (stash == boolean_stash) |
272 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
278 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
|
|
279 | else if (stash == error_stash) |
|
|
280 | encode_ch (enc, 0xe0 | 23); |
273 | else if (SvSTASH (sv) == tagged_stash) |
281 | else if (stash == tagged_stash) |
274 | { |
282 | { |
275 | if (svt != SVt_PVAV) |
283 | if (svt != SVt_PVAV) |
276 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
284 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
277 | |
285 | |
278 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
286 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
279 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
287 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
280 | } |
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 | } |
281 | else |
341 | else |
282 | { |
|
|
283 | // we re-bless the reference to get overload and other niceties right |
|
|
284 | GV *to_cbor = gv_fetchmethod_autoload (SvSTASH (sv), "TO_CBOR", 0); |
|
|
285 | |
|
|
286 | if (to_cbor) |
|
|
287 | { |
|
|
288 | dSP; |
|
|
289 | |
|
|
290 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
291 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
292 | |
|
|
293 | // calling with G_SCALAR ensures that we always get a 1 return value |
|
|
294 | PUTBACK; |
|
|
295 | call_sv ((SV *)GvCV (to_cbor), G_SCALAR); |
|
|
296 | SPAGAIN; |
|
|
297 | |
|
|
298 | // catch this surprisingly common error |
|
|
299 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
300 | croak ("%s::TO_CBOR method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
301 | |
|
|
302 | sv = POPs; |
|
|
303 | PUTBACK; |
|
|
304 | |
|
|
305 | encode_sv (enc, sv); |
|
|
306 | |
|
|
307 | FREETMPS; LEAVE; |
|
|
308 | } |
|
|
309 | else |
|
|
310 | 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", |
311 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
343 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
312 | } |
|
|
313 | } |
344 | } |
314 | else if (svt == SVt_PVHV) |
345 | else if (svt == SVt_PVHV) |
315 | encode_hv (enc, (HV *)sv); |
346 | encode_hv (enc, (HV *)sv); |
316 | else if (svt == SVt_PVAV) |
347 | else if (svt == SVt_PVAV) |
317 | encode_av (enc, (AV *)sv); |
348 | encode_av (enc, (AV *)sv); |
… | |
… | |
567 | |
598 | |
568 | SV *k = decode_sv (dec); |
599 | SV *k = decode_sv (dec); |
569 | SV *v = decode_sv (dec); |
600 | SV *v = decode_sv (dec); |
570 | |
601 | |
571 | hv_store_ent (hv, k, v, 0); |
602 | hv_store_ent (hv, k, v, 0); |
|
|
603 | SvREFCNT_dec (k); |
572 | } |
604 | } |
573 | } |
605 | } |
574 | else |
606 | else |
575 | { |
607 | { |
576 | int len = decode_uint (dec); |
608 | int len = decode_uint (dec); |
… | |
… | |
579 | { |
611 | { |
580 | SV *k = decode_sv (dec); |
612 | SV *k = decode_sv (dec); |
581 | SV *v = decode_sv (dec); |
613 | SV *v = decode_sv (dec); |
582 | |
614 | |
583 | hv_store_ent (hv, k, v, 0); |
615 | hv_store_ent (hv, k, v, 0); |
|
|
616 | SvREFCNT_dec (k); |
584 | } |
617 | } |
585 | } |
618 | } |
586 | |
619 | |
587 | DEC_DEC_DEPTH; |
620 | DEC_DEC_DEPTH; |
588 | return newRV_noinc ((SV *)hv); |
621 | return newRV_noinc ((SV *)hv); |
… | |
… | |
643 | UV tag = decode_uint (dec); |
676 | UV tag = decode_uint (dec); |
644 | SV *sv = decode_sv (dec); |
677 | SV *sv = decode_sv (dec); |
645 | |
678 | |
646 | if (tag == CBOR_TAG_MAGIC) |
679 | if (tag == CBOR_TAG_MAGIC) |
647 | return sv; |
680 | return sv; |
648 | |
|
|
649 | if (tag == CBOR_TAG_PERL_OBJECT) |
681 | else if (tag == CBOR_TAG_PERL_OBJECT) |
650 | { |
682 | { |
651 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
683 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
652 | 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); |
653 | |
694 | |
654 | // 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 |
655 | } |
725 | { |
656 | |
|
|
657 | AV *av = newAV (); |
726 | AV *av = newAV (); |
658 | av_push (av, newSVuv (tag)); |
727 | av_push (av, newSVuv (tag)); |
659 | av_push (av, sv); |
728 | av_push (av, sv); |
660 | |
729 | |
661 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
730 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
662 | ? cbor_tagged_stash |
731 | ? cbor_tagged_stash |
663 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
732 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
664 | |
733 | |
665 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
734 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
|
|
735 | } |
666 | |
736 | |
667 | fail: |
737 | fail: |
668 | SvREFCNT_dec (sv); |
738 | SvREFCNT_dec (sv); |
669 | return &PL_sv_undef; |
739 | return &PL_sv_undef; |
670 | } |
740 | } |
… | |
… | |
693 | case 7: // misc |
763 | case 7: // misc |
694 | switch (*dec->cur++ & 31) |
764 | switch (*dec->cur++ & 31) |
695 | { |
765 | { |
696 | case 20: |
766 | case 20: |
697 | #if CBOR_SLOW |
767 | #if CBOR_SLOW |
698 | cbor_false = get_bool ("CBOR::XS::false"); |
768 | types_false = get_bool ("Types::Serialiser::false"); |
699 | #endif |
769 | #endif |
700 | return newSVsv (cbor_false); |
770 | return newSVsv (types_false); |
701 | case 21: |
771 | case 21: |
702 | #if CBOR_SLOW |
772 | #if CBOR_SLOW |
703 | cbor_true = get_bool ("CBOR::XS::true"); |
773 | types_true = get_bool ("Types::Serialiser::true"); |
704 | #endif |
774 | #endif |
705 | return newSVsv (cbor_true); |
775 | return newSVsv (types_true); |
706 | case 22: |
776 | case 22: |
707 | return newSVsv (&PL_sv_undef); |
777 | return newSVsv (&PL_sv_undef); |
|
|
778 | case 23: |
|
|
779 | #if CBOR_SLOW |
|
|
780 | types_error = get_bool ("Types::Serialiser::error"); |
|
|
781 | #endif |
|
|
782 | return newSVsv (types_error); |
708 | |
783 | |
709 | case 25: |
784 | case 25: |
710 | { |
785 | { |
711 | WANT (2); |
786 | WANT (2); |
712 | |
787 | |
… | |
… | |
828 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
903 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
829 | |
904 | |
830 | BOOT: |
905 | BOOT: |
831 | { |
906 | { |
832 | cbor_stash = gv_stashpv ("CBOR::XS" , 1); |
907 | cbor_stash = gv_stashpv ("CBOR::XS" , 1); |
833 | cbor_boolean_stash = gv_stashpv ("CBOR::XS::Boolean", 1); |
|
|
834 | cbor_tagged_stash = gv_stashpv ("CBOR::XS::Tagged" , 1); |
908 | cbor_tagged_stash = gv_stashpv ("CBOR::XS::Tagged" , 1); |
835 | |
909 | |
836 | cbor_true = get_bool ("CBOR::XS::true"); |
910 | types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
837 | cbor_false = get_bool ("CBOR::XS::false"); |
911 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
|
|
912 | |
|
|
913 | types_true = get_bool ("Types::Serialiser::true" ); |
|
|
914 | types_false = get_bool ("Types::Serialiser::false"); |
|
|
915 | types_error = get_bool ("Types::Serialiser::error"); |
|
|
916 | |
|
|
917 | sv_cbor = newSVpv ("CBOR", 0); |
|
|
918 | SvREADONLY_on (sv_cbor); |
838 | } |
919 | } |
839 | |
920 | |
840 | PROTOTYPES: DISABLE |
921 | PROTOTYPES: DISABLE |
841 | |
922 | |
842 | void CLONE (...) |
923 | void CLONE (...) |
843 | CODE: |
924 | CODE: |
844 | cbor_stash = 0; |
925 | cbor_stash = 0; |
845 | cbor_boolean_stash = 0; |
|
|
846 | cbor_tagged_stash = 0; |
926 | cbor_tagged_stash = 0; |
|
|
927 | types_error_stash = 0; |
|
|
928 | types_boolean_stash = 0; |
847 | |
929 | |
848 | void new (char *klass) |
930 | void new (char *klass) |
849 | PPCODE: |
931 | PPCODE: |
850 | { |
932 | { |
851 | SV *pv = NEWSV (0, sizeof (CBOR)); |
933 | SV *pv = NEWSV (0, sizeof (CBOR)); |