ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/XS.xs
(Generate patch)

Comparing JSON-XS/XS.xs (file contents):
Revision 1.119 by root, Mon Oct 28 23:19:54 2013 UTC vs.
Revision 1.126 by root, Sun Feb 21 16:18:19 2016 UTC

15 15
16// some old perls do not have this, try to make it work, no 16// some old perls do not have this, try to make it work, no
17// guarantees, though. if it breaks, you get to keep the pieces. 17// guarantees, though. if it breaks, you get to keep the pieces.
18#ifndef UTF8_MAXBYTES 18#ifndef UTF8_MAXBYTES
19# define UTF8_MAXBYTES 13 19# define UTF8_MAXBYTES 13
20#endif
21
22// compatibility with perl <5.18
23#ifndef HvNAMELEN_get
24# define HvNAMELEN_get(hv) strlen (HvNAME (hv))
25#endif
26#ifndef HvNAMELEN
27# define HvNAMELEN(hv) HvNAMELEN_get (hv)
28#endif
29#ifndef HvNAMEUTF8
30# define HvNAMEUTF8(hv) 0
20#endif 31#endif
21 32
22// three extra for rounding, sign, and end of string 33// three extra for rounding, sign, and end of string
23#define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 3) 34#define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 3)
24 35
68#define ERR_NESTING_EXCEEDED "json text or perl structure exceeds maximum nesting level (max_depth set too low?)" 79#define ERR_NESTING_EXCEEDED "json text or perl structure exceeds maximum nesting level (max_depth set too low?)"
69 80
70#ifdef USE_ITHREADS 81#ifdef USE_ITHREADS
71# define JSON_SLOW 1 82# define JSON_SLOW 1
72# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) 83# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1))
84# define BOOL_STASH (bool_stash ? bool_stash : gv_stashpv ("Types::Serialiser::Boolean", 1))
73#else 85#else
74# define JSON_SLOW 0 86# define JSON_SLOW 0
75# define JSON_STASH json_stash 87# define JSON_STASH json_stash
88# define BOOL_STASH bool_stash
76#endif 89#endif
77 90
78// the amount of HEs to allocate on the stack, when sorting keys 91// the amount of HEs to allocate on the stack, when sorting keys
79#define STACK_HES 64 92#define STACK_HES 64
80 93
81static HV *json_stash, *types_boolean_stash; // JSON::XS:: 94static HV *json_stash, *bool_stash; // JSON::XS::, Types::Serialiser::Boolean::
82static SV *types_true, *types_false, *sv_json; 95static SV *bool_true, *bool_false, *sv_json;
83 96
84enum { 97enum {
85 INCR_M_WS = 0, // initial whitespace skipping, must be 0 98 INCR_M_WS = 0, // initial whitespace skipping, must be 0
86 INCR_M_STR, // inside string 99 INCR_M_STR, // inside string
87 INCR_M_BS, // inside backslash 100 INCR_M_BS, // inside backslash
288 // a recursion depth of ten gives us >>500 bits 301 // a recursion depth of ten gives us >>500 bits
289 json_atof_scan1 (s, &accum, &expo, 0, 10); 302 json_atof_scan1 (s, &accum, &expo, 0, 10);
290 303
291 return neg ? -accum : accum; 304 return neg ? -accum : accum;
292} 305}
306
307// target of scalar reference is bool? -1 == nope, 0 == false, 1 == true
308static int
309ref_bool_type (SV *sv)
310{
311 svtype svt = SvTYPE (sv);
312
313 if (svt < SVt_PVAV)
314 {
315 STRLEN len = 0;
316 char *pv = svt ? SvPV (sv, len) : 0;
317
318 if (len == 1)
319 if (*pv == '1')
320 return 1;
321 else if (*pv == '0')
322 return 0;
323
324 }
325
326 return -1;
327}
328
329// returns whether scalar is not a reference in the sense of allow_nonref
330static int
331json_nonref (SV *scalar)
332{
333 if (!SvROK (scalar))
334 return 1;
335
336 scalar = SvRV (scalar);
337
338 if (SvSTASH (scalar) == bool_stash)
339 return 1;
340
341 if (!SvOBJECT (scalar) && ref_bool_type (scalar) >= 0)
342 return 1;
343
344 return 0;
345}
346
293///////////////////////////////////////////////////////////////////////////// 347/////////////////////////////////////////////////////////////////////////////
294// encoder 348// encoder
295 349
296// structure used for encoding JSON 350// structure used for encoding JSON
297typedef struct 351typedef struct
683// encode objects, arrays and special \0=false and \1=true values. 737// encode objects, arrays and special \0=false and \1=true values.
684static void 738static void
685encode_rv (enc_t *enc, SV *sv) 739encode_rv (enc_t *enc, SV *sv)
686{ 740{
687 svtype svt; 741 svtype svt;
742 GV *method;
688 743
689 SvGETMAGIC (sv); 744 SvGETMAGIC (sv);
690 svt = SvTYPE (sv); 745 svt = SvTYPE (sv);
691 746
692 if (expect_false (SvOBJECT (sv))) 747 if (expect_false (SvOBJECT (sv)))
693 { 748 {
694 HV *boolean_stash = !JSON_SLOW || types_boolean_stash
695 ? types_boolean_stash
696 : gv_stashpv ("Types::Serialiser::Boolean", 1);
697 HV *stash = SvSTASH (sv); 749 HV *stash = SvSTASH (sv);
698 750
699 if (stash == boolean_stash) 751 if (stash == bool_stash)
700 { 752 {
701 if (SvIV (sv)) 753 if (SvIV (sv))
702 encode_str (enc, "true", 4, 0); 754 encode_str (enc, "true", 4, 0);
703 else 755 else
704 encode_str (enc, "false", 5, 0); 756 encode_str (enc, "false", 5, 0);
705 } 757 }
706 else if (enc->json.flags & F_CONV_BLESSED) 758 else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0)))
707 { 759 {
708 GV *to_json = gv_fetchmethod_autoload (stash, "TO_JSON", 0); 760 int count;
761 dSP;
709 762
763 ENTER; SAVETMPS;
764 SAVESTACK_POS ();
765 PUSHMARK (SP);
766 EXTEND (SP, 2);
767 // we re-bless the reference to get overload and other niceties right
768 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
710 if (to_json) 769 PUSHs (sv_json);
770
771 PUTBACK;
772 count = call_sv ((SV *)GvCV (method), G_ARRAY);
773 SPAGAIN;
774
775 // catch this surprisingly common error
776 if (SvROK (TOPs) && SvRV (TOPs) == sv)
777 croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
778
779 encode_ch (enc, '(');
780 encode_ch (enc, '"');
781 encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash));
782 encode_ch (enc, '"');
783 encode_ch (enc, ')');
784 encode_ch (enc, '[');
785
786 while (count)
711 { 787 {
712 dSP; 788 encode_sv (enc, SP[1 - count--]);
713 789
714 ENTER; SAVETMPS; PUSHMARK (SP); 790 if (count)
715 // we re-bless the reference to get overload and other niceties right
716 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
717
718 // calling with G_SCALAR ensures that we always get a 1 return value
719 PUTBACK;
720 call_sv ((SV *)GvCV (to_json), G_SCALAR);
721 SPAGAIN;
722
723 // catch this surprisingly common error
724 if (SvROK (TOPs) && SvRV (TOPs) == sv)
725 croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
726
727 sv = POPs;
728 PUTBACK;
729
730 encode_sv (enc, sv); 791 encode_ch (enc, ',');
731
732 FREETMPS; LEAVE;
733 } 792 }
734 else if (enc->json.flags & F_ALLOW_BLESSED) 793
735 encode_str (enc, "null", 4, 0); 794 encode_ch (enc, ']');
736 else 795
737 croak ("encountered object '%s' when conv_object is enabled, but TO_JSON is missing and allow_blessed disabled", 796 FREETMPS; LEAVE;
738 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
739 } 797 }
740 else if (enc->json.flags & F_ALLOW_TAGS) 798 else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0)))
741 { 799 {
742 GV *method = gv_fetchmethod_autoload (stash, "FREEZE", 0);
743
744 if (method)
745 {
746 int count;
747 dSP; 800 dSP;
748 801
749 ENTER; SAVETMPS; PUSHMARK (SP); 802 ENTER; SAVETMPS;
750 EXTEND (SP, 2); 803 PUSHMARK (SP);
751 // we re-bless the reference to get overload and other niceties right 804 // we re-bless the reference to get overload and other niceties right
752 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 805 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
753 PUSHs (sv_json);
754 806
807 // calling with G_SCALAR ensures that we always get a 1 return value
755 PUTBACK; 808 PUTBACK;
756 count = call_sv ((SV *)GvCV (method), G_ARRAY); 809 call_sv ((SV *)GvCV (method), G_SCALAR);
757 SPAGAIN; 810 SPAGAIN;
758 811
759 // catch this surprisingly common error 812 // catch this surprisingly common error
760 if (SvROK (TOPs) && SvRV (TOPs) == sv) 813 if (SvROK (TOPs) && SvRV (TOPs) == sv)
761 croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); 814 croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
762 815
763 encode_ch (enc, '('); 816 sv = POPs;
764 encode_ch (enc, '"');
765 encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash));
766 encode_ch (enc, '"');
767 encode_ch (enc, ')');
768 encode_ch (enc, '[');
769
770 while (count)
771 {
772 encode_sv (enc, SP[1 - count--]);
773
774 if (count)
775 encode_ch (enc, ',');
776 }
777
778 encode_ch (enc, ']');
779
780 PUTBACK; 817 PUTBACK;
781 818
819 encode_sv (enc, sv);
820
782 FREETMPS; LEAVE; 821 FREETMPS; LEAVE;
783 }
784 else if (enc->json.flags & F_ALLOW_BLESSED)
785 encode_str (enc, "null", 4, 0);
786 else
787 croak ("encountered object '%s' when allow_tags is enabled, but FREEZE is missing and allow_blessed disabled",
788 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
789 } 822 }
790 else if (enc->json.flags & F_ALLOW_BLESSED) 823 else if (enc->json.flags & F_ALLOW_BLESSED)
791 encode_str (enc, "null", 4, 0); 824 encode_str (enc, "null", 4, 0);
792 else 825 else
793 croak ("encountered object '%s', but neither allow_blessed, convert_blessed nor allow_tags settings are enabled", 826 croak ("encountered object '%s', but neither allow_blessed, convert_blessed nor allow_tags settings are enabled (or TO_JSON/FREEZE method missing)",
794 SvPV_nolen (sv_2mortal (newRV_inc (sv)))); 827 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
795 } 828 }
796 else if (svt == SVt_PVHV) 829 else if (svt == SVt_PVHV)
797 encode_hv (enc, (HV *)sv); 830 encode_hv (enc, (HV *)sv);
798 else if (svt == SVt_PVAV) 831 else if (svt == SVt_PVAV)
799 encode_av (enc, (AV *)sv); 832 encode_av (enc, (AV *)sv);
800 else if (svt < SVt_PVAV) 833 else if (svt < SVt_PVAV)
801 { 834 {
802 STRLEN len = 0; 835 int bool_type = ref_bool_type (sv);
803 char *pv = svt ? SvPV (sv, len) : 0;
804 836
805 if (len == 1 && *pv == '1') 837 if (bool_type == 1)
806 encode_str (enc, "true", 4, 0); 838 encode_str (enc, "true", 4, 0);
807 else if (len == 1 && *pv == '0') 839 else if (bool_type == 0)
808 encode_str (enc, "false", 5, 0); 840 encode_str (enc, "false", 5, 0);
809 else if (enc->json.flags & F_ALLOW_UNKNOWN) 841 else if (enc->json.flags & F_ALLOW_UNKNOWN)
810 encode_str (enc, "null", 4, 0); 842 encode_str (enc, "null", 4, 0);
811 else 843 else
812 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", 844 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1",
895static SV * 927static SV *
896encode_json (SV *scalar, JSON *json) 928encode_json (SV *scalar, JSON *json)
897{ 929{
898 enc_t enc; 930 enc_t enc;
899 931
900 if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) 932 if (!(json->flags & F_ALLOW_NONREF) && json_nonref (scalar))
901 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); 933 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)");
902 934
903 enc.json = *json; 935 enc.json = *json;
904 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 936 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
905 enc.cur = SvPVX (enc.sv); 937 enc.cur = SvPVX (enc.sv);
1112 *cur++ = *dec_cur++; 1144 *cur++ = *dec_cur++;
1113 while (--clen); 1145 while (--clen);
1114 1146
1115 utf8 = 1; 1147 utf8 = 1;
1116 } 1148 }
1149 else if (ch == '\t' && dec->json.flags & F_RELAXED)
1150 *cur++ = ch;
1117 else 1151 else
1118 { 1152 {
1119 --dec_cur; 1153 --dec_cur;
1120 1154
1121 if (!ch) 1155 if (!ch)
1443 1477
1444 hv_iterinit (hv); 1478 hv_iterinit (hv);
1445 he = hv_iternext (hv); 1479 he = hv_iternext (hv);
1446 hv_iterinit (hv); 1480 hv_iterinit (hv);
1447 1481
1448 // the next line creates a mortal sv each time its called. 1482 // the next line creates a mortal sv each time it's called.
1449 // might want to optimise this for common cases. 1483 // might want to optimise this for common cases.
1450 cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); 1484 cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0);
1451 1485
1452 if (cb) 1486 if (cb)
1453 { 1487 {
1454 dSP; 1488 dSP;
1455 int count; 1489 int count;
1456 1490
1457 ENTER; SAVETMPS; PUSHMARK (SP); 1491 ENTER; SAVETMPS;
1492 SAVESTACK_POS ();
1493 PUSHMARK (SP);
1458 XPUSHs (HeVAL (he)); 1494 XPUSHs (HeVAL (he));
1459 sv_2mortal (sv); 1495 sv_2mortal (sv);
1460 1496
1461 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; 1497 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1462 1498
1475 if (dec->json.cb_object) 1511 if (dec->json.cb_object)
1476 { 1512 {
1477 dSP; 1513 dSP;
1478 int count; 1514 int count;
1479 1515
1480 ENTER; SAVETMPS; PUSHMARK (SP); 1516 ENTER; SAVETMPS;
1517 SAVESTACK_POS ();
1518 PUSHMARK (SP);
1481 XPUSHs (sv_2mortal (sv)); 1519 XPUSHs (sv_2mortal (sv));
1482 1520
1483 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; 1521 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN;
1484 1522
1485 if (count == 1) 1523 if (count == 1)
1511 if (!(dec->json.flags & F_ALLOW_TAGS)) 1549 if (!(dec->json.flags & F_ALLOW_TAGS))
1512 ERR ("malformed JSON string, neither array, object, number, string or atom"); 1550 ERR ("malformed JSON string, neither array, object, number, string or atom");
1513 1551
1514 ++dec->cur; 1552 ++dec->cur;
1515 1553
1554 decode_ws (dec);
1555
1516 tag = decode_sv (dec); 1556 tag = decode_sv (dec);
1517 if (!tag) 1557 if (!tag)
1518 goto fail; 1558 goto fail;
1519 1559
1520 if (!SvPOK (tag)) 1560 if (!SvPOK (tag))
1521 ERR ("malformed JSON string, (tag) must be a string"); 1561 ERR ("malformed JSON string, (tag) must be a string");
1522 1562
1563 decode_ws (dec);
1564
1523 if (*dec->cur != ')') 1565 if (*dec->cur != ')')
1524 ERR (") expected after tag"); 1566 ERR (") expected after tag");
1525 1567
1526 ++dec->cur; 1568 ++dec->cur;
1569
1570 decode_ws (dec);
1527 1571
1528 val = decode_sv (dec); 1572 val = decode_sv (dec);
1529 if (!val) 1573 if (!val)
1530 goto fail; 1574 goto fail;
1531 1575
1546 if (!method) 1590 if (!method)
1547 ERR ("cannot decode perl-object (package does not have a THAW method)"); 1591 ERR ("cannot decode perl-object (package does not have a THAW method)");
1548 1592
1549 dSP; 1593 dSP;
1550 1594
1551 ENTER; SAVETMPS; PUSHMARK (SP); 1595 ENTER; SAVETMPS;
1596 PUSHMARK (SP);
1552 EXTEND (SP, len + 2); 1597 EXTEND (SP, len + 2);
1553 // we re-bless the reference to get overload and other niceties right 1598 // we re-bless the reference to get overload and other niceties right
1554 PUSHs (tag); 1599 PUSHs (tag);
1555 PUSHs (sv_json); 1600 PUSHs (sv_json);
1556 1601
1598 case 't': 1643 case 't':
1599 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1644 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1600 { 1645 {
1601 dec->cur += 4; 1646 dec->cur += 4;
1602#if JSON_SLOW 1647#if JSON_SLOW
1603 types_true = get_bool ("Types::Serialiser::true"); 1648 bool_true = get_bool ("Types::Serialiser::true");
1604#endif 1649#endif
1605 return newSVsv (types_true); 1650 return newSVsv (bool_true);
1606 } 1651 }
1607 else 1652 else
1608 ERR ("'true' expected"); 1653 ERR ("'true' expected");
1609 1654
1610 break; 1655 break;
1612 case 'f': 1657 case 'f':
1613 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1658 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1614 { 1659 {
1615 dec->cur += 5; 1660 dec->cur += 5;
1616#if JSON_SLOW 1661#if JSON_SLOW
1617 types_false = get_bool ("Types::Serialiser::false"); 1662 bool_false = get_bool ("Types::Serialiser::false");
1618#endif 1663#endif
1619 return newSVsv (types_false); 1664 return newSVsv (bool_false);
1620 } 1665 }
1621 else 1666 else
1622 ERR ("'false' expected"); 1667 ERR ("'false' expected");
1623 1668
1624 break; 1669 break;
1738 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1783 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1739 } 1784 }
1740 1785
1741 sv = sv_2mortal (sv); 1786 sv = sv_2mortal (sv);
1742 1787
1743 if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) 1788 if (!(dec.json.flags & F_ALLOW_NONREF) && json_nonref (sv))
1744 croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); 1789 croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)");
1745 1790
1746 return sv; 1791 return sv;
1747} 1792}
1748 1793
1918 i >= '0' && i <= '9' ? i - '0' 1963 i >= '0' && i <= '9' ? i - '0'
1919 : i >= 'a' && i <= 'f' ? i - 'a' + 10 1964 : i >= 'a' && i <= 'f' ? i - 'a' + 10
1920 : i >= 'A' && i <= 'F' ? i - 'A' + 10 1965 : i >= 'A' && i <= 'F' ? i - 'A' + 10
1921 : -1; 1966 : -1;
1922 1967
1923 json_stash = gv_stashpv ("JSON::XS" , 1); 1968 json_stash = gv_stashpv ("JSON::XS" , 1);
1924 types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); 1969 bool_stash = gv_stashpv ("Types::Serialiser::Boolean", 1);
1925
1926 types_true = get_bool ("Types::Serialiser::true"); 1970 bool_true = get_bool ("Types::Serialiser::true");
1927 types_false = get_bool ("Types::Serialiser::false"); 1971 bool_false = get_bool ("Types::Serialiser::false");
1928 1972
1929 sv_json = newSVpv ("JSON", 0); 1973 sv_json = newSVpv ("JSON", 0);
1930 SvREADONLY_on (sv_json); 1974 SvREADONLY_on (sv_json);
1931 1975
1932 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ 1976 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */
1934 1978
1935PROTOTYPES: DISABLE 1979PROTOTYPES: DISABLE
1936 1980
1937void CLONE (...) 1981void CLONE (...)
1938 CODE: 1982 CODE:
1939 json_stash = 0; 1983 json_stash = 0;
1940 types_boolean_stash = 0; 1984 bool_stash = 0;
1941 1985
1942void new (char *klass) 1986void new (char *klass)
1943 PPCODE: 1987 PPCODE:
1944{ 1988{
1945 SV *pv = NEWSV (0, sizeof (JSON)); 1989 SV *pv = NEWSV (0, sizeof (JSON));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines