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.118 by root, Fri Oct 25 20:27:57 2013 UTC vs.
Revision 1.119 by root, Mon Oct 28 23:19:54 2013 UTC

33#define F_SHRINK 0x00000200UL 33#define F_SHRINK 0x00000200UL
34#define F_ALLOW_BLESSED 0x00000400UL 34#define F_ALLOW_BLESSED 0x00000400UL
35#define F_CONV_BLESSED 0x00000800UL 35#define F_CONV_BLESSED 0x00000800UL
36#define F_RELAXED 0x00001000UL 36#define F_RELAXED 0x00001000UL
37#define F_ALLOW_UNKNOWN 0x00002000UL 37#define F_ALLOW_UNKNOWN 0x00002000UL
38#define F_ALLOW_TAGS 0x00004000UL
38#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing 39#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing
39 40
40#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER 41#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER
41 42
42#define INIT_SIZE 32 // initial scalar size to be allocated 43#define INIT_SIZE 32 // initial scalar size to be allocated
75#endif 76#endif
76 77
77// the amount of HEs to allocate on the stack, when sorting keys 78// the amount of HEs to allocate on the stack, when sorting keys
78#define STACK_HES 64 79#define STACK_HES 64
79 80
80static HV *json_stash, *json_boolean_stash; // JSON::XS:: 81static HV *json_stash, *types_boolean_stash; // JSON::XS::
81static SV *json_true, *json_false; 82static SV *types_true, *types_false, *sv_json;
82 83
83enum { 84enum {
84 INCR_M_WS = 0, // initial whitespace skipping, must be 0 85 INCR_M_WS = 0, // initial whitespace skipping, must be 0
85 INCR_M_STR, // inside string 86 INCR_M_STR, // inside string
86 INCR_M_BS, // inside backslash 87 INCR_M_BS, // inside backslash
688 SvGETMAGIC (sv); 689 SvGETMAGIC (sv);
689 svt = SvTYPE (sv); 690 svt = SvTYPE (sv);
690 691
691 if (expect_false (SvOBJECT (sv))) 692 if (expect_false (SvOBJECT (sv)))
692 { 693 {
693 HV *stash = !JSON_SLOW || json_boolean_stash 694 HV *boolean_stash = !JSON_SLOW || types_boolean_stash
694 ? json_boolean_stash 695 ? types_boolean_stash
695 : gv_stashpv ("JSON::XS::Boolean", 1); 696 : gv_stashpv ("Types::Serialiser::Boolean", 1);
697 HV *stash = SvSTASH (sv);
696 698
697 if (SvSTASH (sv) == stash) 699 if (stash == boolean_stash)
698 { 700 {
699 if (SvIV (sv)) 701 if (SvIV (sv))
700 encode_str (enc, "true", 4, 0); 702 encode_str (enc, "true", 4, 0);
701 else 703 else
702 encode_str (enc, "false", 5, 0); 704 encode_str (enc, "false", 5, 0);
703 } 705 }
704 else 706 else if (enc->json.flags & F_CONV_BLESSED)
705 { 707 {
706#if 0 708 GV *to_json = gv_fetchmethod_autoload (stash, "TO_JSON", 0);
707 if (0 && sv_derived_from (rv, "JSON::Literal")) 709
708 { 710 if (to_json)
709 // not yet
710 } 711 {
711#endif
712 if (enc->json.flags & F_CONV_BLESSED)
713 { 712 dSP;
713
714 ENTER; SAVETMPS; PUSHMARK (SP);
714 // we re-bless the reference to get overload and other niceties right 715 // we re-bless the reference to get overload and other niceties right
715 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0);
716
717 if (to_json)
718 {
719 dSP;
720
721 ENTER; SAVETMPS; PUSHMARK (SP);
722 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); 716 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
723 717
724 // calling with G_SCALAR ensures that we always get a 1 return value 718 // calling with G_SCALAR ensures that we always get a 1 return value
725 PUTBACK; 719 PUTBACK;
726 call_sv ((SV *)GvCV (to_json), G_SCALAR); 720 call_sv ((SV *)GvCV (to_json), G_SCALAR);
727 SPAGAIN; 721 SPAGAIN;
728 722
729 // catch this surprisingly common error 723 // catch this surprisingly common error
730 if (SvROK (TOPs) && SvRV (TOPs) == sv) 724 if (SvROK (TOPs) && SvRV (TOPs) == sv)
731 croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); 725 croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
732 726
733 sv = POPs; 727 sv = POPs;
734 PUTBACK; 728 PUTBACK;
735 729
736 encode_sv (enc, sv); 730 encode_sv (enc, sv);
737 731
738 FREETMPS; LEAVE; 732 FREETMPS; LEAVE;
739 }
740 else if (enc->json.flags & F_ALLOW_BLESSED)
741 encode_str (enc, "null", 4, 0);
742 else
743 croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it",
744 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
745 } 733 }
746 else if (enc->json.flags & F_ALLOW_BLESSED) 734 else if (enc->json.flags & F_ALLOW_BLESSED)
747 encode_str (enc, "null", 4, 0); 735 encode_str (enc, "null", 4, 0);
748 else 736 else
749 croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", 737 croak ("encountered object '%s' when conv_object is enabled, but TO_JSON is missing and allow_blessed disabled",
750 SvPV_nolen (sv_2mortal (newRV_inc (sv)))); 738 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
751 } 739 }
740 else if (enc->json.flags & F_ALLOW_TAGS)
741 {
742 GV *method = gv_fetchmethod_autoload (stash, "FREEZE", 0);
743
744 if (method)
745 {
746 int count;
747 dSP;
748
749 ENTER; SAVETMPS; PUSHMARK (SP);
750 EXTEND (SP, 2);
751 // we re-bless the reference to get overload and other niceties right
752 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
753 PUSHs (sv_json);
754
755 PUTBACK;
756 count = call_sv ((SV *)GvCV (method), G_ARRAY);
757 SPAGAIN;
758
759 // catch this surprisingly common error
760 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)));
762
763 encode_ch (enc, '(');
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;
781
782 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 }
790 else if (enc->json.flags & F_ALLOW_BLESSED)
791 encode_str (enc, "null", 4, 0);
792 else
793 croak ("encountered object '%s', but neither allow_blessed, convert_blessed nor allow_tags settings are enabled",
794 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
752 } 795 }
753 else if (svt == SVt_PVHV) 796 else if (svt == SVt_PVHV)
754 encode_hv (enc, (HV *)sv); 797 encode_hv (enc, (HV *)sv);
755 else if (svt == SVt_PVAV) 798 else if (svt == SVt_PVAV)
756 encode_av (enc, (AV *)sv); 799 encode_av (enc, (AV *)sv);
1458 DEC_DEC_DEPTH; 1501 DEC_DEC_DEPTH;
1459 return 0; 1502 return 0;
1460} 1503}
1461 1504
1462static SV * 1505static SV *
1506decode_tag (dec_t *dec)
1507{
1508 SV *tag = 0;
1509 SV *val = 0;
1510
1511 if (!(dec->json.flags & F_ALLOW_TAGS))
1512 ERR ("malformed JSON string, neither array, object, number, string or atom");
1513
1514 ++dec->cur;
1515
1516 tag = decode_sv (dec);
1517 if (!tag)
1518 goto fail;
1519
1520 if (!SvPOK (tag))
1521 ERR ("malformed JSON string, (tag) must be a string");
1522
1523 if (*dec->cur != ')')
1524 ERR (") expected after tag");
1525
1526 ++dec->cur;
1527
1528 val = decode_sv (dec);
1529 if (!val)
1530 goto fail;
1531
1532 if (!SvROK (val) || SvTYPE (SvRV (val)) != SVt_PVAV)
1533 ERR ("malformed JSON string, tag value must be an array");
1534
1535 {
1536 AV *av = (AV *)SvRV (val);
1537 int i, len = av_len (av) + 1;
1538 HV *stash = gv_stashsv (tag, 0);
1539 SV *sv;
1540
1541 if (!stash)
1542 ERR ("cannot decode perl-object (package does not exist)");
1543
1544 GV *method = gv_fetchmethod_autoload (stash, "THAW", 0);
1545
1546 if (!method)
1547 ERR ("cannot decode perl-object (package does not have a THAW method)");
1548
1549 dSP;
1550
1551 ENTER; SAVETMPS; PUSHMARK (SP);
1552 EXTEND (SP, len + 2);
1553 // we re-bless the reference to get overload and other niceties right
1554 PUSHs (tag);
1555 PUSHs (sv_json);
1556
1557 for (i = 0; i < len; ++i)
1558 PUSHs (*av_fetch (av, i, 1));
1559
1560 PUTBACK;
1561 call_sv ((SV *)GvCV (method), G_SCALAR);
1562 SPAGAIN;
1563
1564 SvREFCNT_dec (tag);
1565 SvREFCNT_dec (val);
1566 sv = SvREFCNT_inc (POPs);
1567
1568 PUTBACK;
1569
1570 FREETMPS; LEAVE;
1571
1572 return sv;
1573 }
1574
1575fail:
1576 SvREFCNT_dec (tag);
1577 SvREFCNT_dec (val);
1578 return 0;
1579}
1580
1581static SV *
1463decode_sv (dec_t *dec) 1582decode_sv (dec_t *dec)
1464{ 1583{
1465 // the beauty of JSON: you need exactly one character lookahead 1584 // the beauty of JSON: you need exactly one character lookahead
1466 // to parse everything. 1585 // to parse everything.
1467 switch (*dec->cur) 1586 switch (*dec->cur)
1468 { 1587 {
1469 case '"': ++dec->cur; return decode_str (dec); 1588 case '"': ++dec->cur; return decode_str (dec);
1470 case '[': ++dec->cur; return decode_av (dec); 1589 case '[': ++dec->cur; return decode_av (dec);
1471 case '{': ++dec->cur; return decode_hv (dec); 1590 case '{': ++dec->cur; return decode_hv (dec);
1591 case '(': return decode_tag (dec);
1472 1592
1473 case '-': 1593 case '-':
1474 case '0': case '1': case '2': case '3': case '4': 1594 case '0': case '1': case '2': case '3': case '4':
1475 case '5': case '6': case '7': case '8': case '9': 1595 case '5': case '6': case '7': case '8': case '9':
1476 return decode_num (dec); 1596 return decode_num (dec);
1478 case 't': 1598 case 't':
1479 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1599 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1480 { 1600 {
1481 dec->cur += 4; 1601 dec->cur += 4;
1482#if JSON_SLOW 1602#if JSON_SLOW
1483 json_true = get_bool ("JSON::XS::true"); 1603 types_true = get_bool ("Types::Serialiser::true");
1484#endif 1604#endif
1485 return newSVsv (json_true); 1605 return newSVsv (types_true);
1486 } 1606 }
1487 else 1607 else
1488 ERR ("'true' expected"); 1608 ERR ("'true' expected");
1489 1609
1490 break; 1610 break;
1492 case 'f': 1612 case 'f':
1493 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1613 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1494 { 1614 {
1495 dec->cur += 5; 1615 dec->cur += 5;
1496#if JSON_SLOW 1616#if JSON_SLOW
1497 json_false = get_bool ("JSON::XS::false"); 1617 types_false = get_bool ("Types::Serialiser::false");
1498#endif 1618#endif
1499 return newSVsv (json_false); 1619 return newSVsv (types_false);
1500 } 1620 }
1501 else 1621 else
1502 ERR ("'false' expected"); 1622 ERR ("'false' expected");
1503 1623
1504 break; 1624 break;
1513 ERR ("'null' expected"); 1633 ERR ("'null' expected");
1514 1634
1515 break; 1635 break;
1516 1636
1517 default: 1637 default:
1518 ERR ("malformed JSON string, neither array, object, number, string or atom"); 1638 ERR ("malformed JSON string, neither tag, array, object, number, string or atom");
1519 break; 1639 break;
1520 } 1640 }
1521 1641
1522fail: 1642fail:
1523 return 0; 1643 return 0;
1750 self->incr_mode = INCR_M_STR; 1870 self->incr_mode = INCR_M_STR;
1751 goto incr_m_str; 1871 goto incr_m_str;
1752 1872
1753 case '[': 1873 case '[':
1754 case '{': 1874 case '{':
1875 case '(':
1755 if (++self->incr_nest > self->max_depth) 1876 if (++self->incr_nest > self->max_depth)
1756 croak (ERR_NESTING_EXCEEDED); 1877 croak (ERR_NESTING_EXCEEDED);
1757 break; 1878 break;
1758 1879
1759 case ']': 1880 case ']':
1760 case '}': 1881 case '}':
1761 if (--self->incr_nest <= 0) 1882 if (--self->incr_nest <= 0)
1762 goto interrupt; 1883 goto interrupt;
1884 break;
1885
1886 case ')':
1887 --self->incr_nest;
1763 break; 1888 break;
1764 1889
1765 case '#': 1890 case '#':
1766 self->incr_mode = INCR_M_C1; 1891 self->incr_mode = INCR_M_C1;
1767 goto incr_m_c; 1892 goto incr_m_c;
1793 i >= '0' && i <= '9' ? i - '0' 1918 i >= '0' && i <= '9' ? i - '0'
1794 : i >= 'a' && i <= 'f' ? i - 'a' + 10 1919 : i >= 'a' && i <= 'f' ? i - 'a' + 10
1795 : i >= 'A' && i <= 'F' ? i - 'A' + 10 1920 : i >= 'A' && i <= 'F' ? i - 'A' + 10
1796 : -1; 1921 : -1;
1797 1922
1798 json_stash = gv_stashpv ("JSON::XS" , 1); 1923 json_stash = gv_stashpv ("JSON::XS" , 1);
1799 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); 1924 types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1);
1800 1925
1801 json_true = get_bool ("JSON::XS::true"); 1926 types_true = get_bool ("Types::Serialiser::true");
1802 json_false = get_bool ("JSON::XS::false"); 1927 types_false = get_bool ("Types::Serialiser::false");
1928
1929 sv_json = newSVpv ("JSON", 0);
1930 SvREADONLY_on (sv_json);
1803 1931
1804 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ 1932 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */
1805} 1933}
1806 1934
1807PROTOTYPES: DISABLE 1935PROTOTYPES: DISABLE
1808 1936
1809void CLONE (...) 1937void CLONE (...)
1810 CODE: 1938 CODE:
1811 json_stash = 0; 1939 json_stash = 0;
1812 json_boolean_stash = 0; 1940 types_boolean_stash = 0;
1813 1941
1814void new (char *klass) 1942void new (char *klass)
1815 PPCODE: 1943 PPCODE:
1816{ 1944{
1817 SV *pv = NEWSV (0, sizeof (JSON)); 1945 SV *pv = NEWSV (0, sizeof (JSON));
1837 shrink = F_SHRINK 1965 shrink = F_SHRINK
1838 allow_blessed = F_ALLOW_BLESSED 1966 allow_blessed = F_ALLOW_BLESSED
1839 convert_blessed = F_CONV_BLESSED 1967 convert_blessed = F_CONV_BLESSED
1840 relaxed = F_RELAXED 1968 relaxed = F_RELAXED
1841 allow_unknown = F_ALLOW_UNKNOWN 1969 allow_unknown = F_ALLOW_UNKNOWN
1970 allow_tags = F_ALLOW_TAGS
1842 PPCODE: 1971 PPCODE:
1843{ 1972{
1844 if (enable) 1973 if (enable)
1845 self->flags |= ix; 1974 self->flags |= ix;
1846 else 1975 else
1862 get_shrink = F_SHRINK 1991 get_shrink = F_SHRINK
1863 get_allow_blessed = F_ALLOW_BLESSED 1992 get_allow_blessed = F_ALLOW_BLESSED
1864 get_convert_blessed = F_CONV_BLESSED 1993 get_convert_blessed = F_CONV_BLESSED
1865 get_relaxed = F_RELAXED 1994 get_relaxed = F_RELAXED
1866 get_allow_unknown = F_ALLOW_UNKNOWN 1995 get_allow_unknown = F_ALLOW_UNKNOWN
1996 get_allow_tags = F_ALLOW_TAGS
1867 PPCODE: 1997 PPCODE:
1868 XPUSHs (boolSV (self->flags & ix)); 1998 XPUSHs (boolSV (self->flags & ix));
1869 1999
1870void max_depth (JSON *self, U32 max_depth = 0x80000000UL) 2000void max_depth (JSON *self, U32 max_depth = 0x80000000UL)
1871 PPCODE: 2001 PPCODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines