… | |
… | |
50 | #define F_ALLOW_TAGS 0x00004000UL |
50 | #define F_ALLOW_TAGS 0x00004000UL |
51 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
51 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
52 | |
52 | |
53 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
53 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
54 | |
54 | |
55 | #define INIT_SIZE 32 // initial scalar size to be allocated |
55 | #define INIT_SIZE 64 // initial scalar size to be allocated |
56 | #define INDENT_STEP 3 // spaces per indentation level |
56 | #define INDENT_STEP 3 // spaces per indentation level |
57 | |
57 | |
58 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
58 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
59 | |
59 | |
60 | #define DECODE_WANTS_OCTETS(json) ((json)->flags & F_UTF8) |
60 | #define DECODE_WANTS_OCTETS(json) ((json)->flags & F_UTF8) |
… | |
… | |
78 | <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg))) |
78 | <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg))) |
79 | |
79 | |
80 | #define ERR_NESTING_EXCEEDED "json text or perl structure exceeds maximum nesting level (max_depth set too low?)" |
80 | #define ERR_NESTING_EXCEEDED "json text or perl structure exceeds maximum nesting level (max_depth set too low?)" |
81 | |
81 | |
82 | #ifdef USE_ITHREADS |
82 | #ifdef USE_ITHREADS |
83 | # define JSON_SLOW 1 |
|
|
84 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
83 | # define JSON_STASH (expect_true (json_stash) ? json_stash : gv_stashpv ("JSON::XS", 1)) |
85 | # define BOOL_STASH (bool_stash ? bool_stash : gv_stashpv ("Types::Serialiser::Boolean", 1)) |
84 | # define BOOL_STASH (expect_true (bool_stash) ? bool_stash : gv_stashpv ("Types::Serialiser::Boolean", 1)) |
|
|
85 | # define GET_BOOL(value) (expect_true (bool_ ## value) ? bool_ ## value : get_bool ("Types::Serialiser::" # value)) |
86 | #else |
86 | #else |
87 | # define JSON_SLOW 0 |
|
|
88 | # define JSON_STASH json_stash |
87 | # define JSON_STASH json_stash |
89 | # define BOOL_STASH bool_stash |
88 | # define BOOL_STASH bool_stash |
|
|
89 | # define GET_BOOL(value) bool_ ## value |
90 | #endif |
90 | #endif |
91 | |
91 | |
92 | // the amount of HEs to allocate on the stack, when sorting keys |
92 | // the amount of HEs to allocate on the stack, when sorting keys |
93 | #define STACK_HES 64 |
93 | #define STACK_HES 64 |
94 | |
94 | |
95 | static HV *json_stash, *bool_stash; // JSON::XS::, Types::Serialiser::Boolean:: |
95 | static HV *json_stash, *bool_stash; // JSON::XS::, Types::Serialiser::Boolean:: |
96 | static SV *bool_true, *bool_false, *sv_json; |
96 | static SV *bool_false, *bool_true; |
|
|
97 | static SV *sv_json; |
97 | |
98 | |
98 | enum { |
99 | enum { |
99 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
100 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
100 | INCR_M_STR, // inside string |
101 | INCR_M_STR, // inside string |
101 | INCR_M_BS, // inside backslash |
102 | INCR_M_BS, // inside backslash |
… | |
… | |
117 | // for the incremental parser |
118 | // for the incremental parser |
118 | SV *incr_text; // the source text so far |
119 | SV *incr_text; // the source text so far |
119 | STRLEN incr_pos; // the current offset into the text |
120 | STRLEN incr_pos; // the current offset into the text |
120 | int incr_nest; // {[]}-nesting level |
121 | int incr_nest; // {[]}-nesting level |
121 | unsigned char incr_mode; |
122 | unsigned char incr_mode; |
|
|
123 | |
|
|
124 | SV *v_false, *v_true; |
122 | } JSON; |
125 | } JSON; |
123 | |
126 | |
124 | INLINE void |
127 | INLINE void |
125 | json_init (JSON *json) |
128 | json_init (JSON *json) |
126 | { |
129 | { |
127 | Zero (json, 1, JSON); |
130 | static const JSON init = { F_ALLOW_NONREF, 512 }; |
128 | json->max_depth = 512; |
131 | |
|
|
132 | *json = init; |
129 | } |
133 | } |
130 | |
134 | |
131 | ///////////////////////////////////////////////////////////////////////////// |
135 | ///////////////////////////////////////////////////////////////////////////// |
132 | // utility functions |
136 | // utility functions |
133 | |
137 | |
… | |
… | |
776 | { |
780 | { |
777 | HV *stash = SvSTASH (sv); |
781 | HV *stash = SvSTASH (sv); |
778 | |
782 | |
779 | if (stash == bool_stash) |
783 | if (stash == bool_stash) |
780 | { |
784 | { |
781 | if (SvIV (sv)) |
|
|
782 | encode_str (enc, "true", 4, 0); |
785 | if (SvIV (sv)) encode_str (enc, "true" , 4, 0); |
783 | else |
|
|
784 | encode_str (enc, "false", 5, 0); |
786 | else encode_str (enc, "false", 5, 0); |
785 | } |
787 | } |
786 | else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0))) |
788 | else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0))) |
787 | { |
789 | { |
788 | int count; |
790 | int count; |
789 | dSP; |
791 | dSP; |
790 | |
792 | |
791 | ENTER; SAVETMPS; |
793 | ENTER; SAVETMPS; |
792 | SAVESTACK_POS (); |
|
|
793 | PUSHMARK (SP); |
794 | PUSHMARK (SP); |
794 | EXTEND (SP, 2); |
795 | EXTEND (SP, 2); |
795 | // we re-bless the reference to get overload and other niceties right |
796 | // we re-bless the reference to get overload and other niceties right |
796 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
797 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
797 | PUSHs (sv_json); |
798 | PUSHs (sv_json); |
… | |
… | |
809 | encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
810 | encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
810 | encode_ch (enc, '"'); |
811 | encode_ch (enc, '"'); |
811 | encode_ch (enc, ')'); |
812 | encode_ch (enc, ')'); |
812 | encode_ch (enc, '['); |
813 | encode_ch (enc, '['); |
813 | |
814 | |
814 | while (count) |
815 | if (count) |
815 | { |
816 | { |
|
|
817 | int i; |
|
|
818 | |
|
|
819 | for (i = 0; i < count - 1; ++i) |
|
|
820 | { |
816 | encode_sv (enc, SP[1 - count--]); |
821 | encode_sv (enc, SP[i + 1 - count]); |
817 | |
|
|
818 | if (count) |
|
|
819 | encode_ch (enc, ','); |
822 | encode_ch (enc, ','); |
|
|
823 | } |
|
|
824 | |
|
|
825 | encode_sv (enc, TOPs); |
|
|
826 | SP -= count; |
820 | } |
827 | } |
821 | |
828 | |
822 | encode_ch (enc, ']'); |
829 | encode_ch (enc, ']'); |
823 | |
830 | |
824 | FREETMPS; LEAVE; |
831 | FREETMPS; LEAVE; |
… | |
… | |
1515 | { |
1522 | { |
1516 | dSP; |
1523 | dSP; |
1517 | int count; |
1524 | int count; |
1518 | |
1525 | |
1519 | ENTER; SAVETMPS; |
1526 | ENTER; SAVETMPS; |
1520 | SAVESTACK_POS (); |
|
|
1521 | PUSHMARK (SP); |
1527 | PUSHMARK (SP); |
1522 | XPUSHs (HeVAL (he)); |
1528 | XPUSHs (HeVAL (he)); |
1523 | sv_2mortal (sv); |
1529 | sv_2mortal (sv); |
1524 | |
1530 | |
1525 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1531 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
… | |
… | |
1528 | { |
1534 | { |
1529 | sv = newSVsv (POPs); |
1535 | sv = newSVsv (POPs); |
1530 | FREETMPS; LEAVE; |
1536 | FREETMPS; LEAVE; |
1531 | return sv; |
1537 | return sv; |
1532 | } |
1538 | } |
|
|
1539 | else if (count) |
|
|
1540 | croak ("filter_json_single_key_object callbacks must not return more than one scalar"); |
1533 | |
1541 | |
1534 | SvREFCNT_inc (sv); |
1542 | SvREFCNT_inc (sv); |
1535 | FREETMPS; LEAVE; |
1543 | FREETMPS; LEAVE; |
1536 | } |
1544 | } |
1537 | } |
1545 | } |
… | |
… | |
1540 | { |
1548 | { |
1541 | dSP; |
1549 | dSP; |
1542 | int count; |
1550 | int count; |
1543 | |
1551 | |
1544 | ENTER; SAVETMPS; |
1552 | ENTER; SAVETMPS; |
1545 | SAVESTACK_POS (); |
|
|
1546 | PUSHMARK (SP); |
1553 | PUSHMARK (SP); |
1547 | XPUSHs (sv_2mortal (sv)); |
1554 | XPUSHs (sv_2mortal (sv)); |
1548 | |
1555 | |
1549 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1556 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1550 | |
1557 | |
… | |
… | |
1552 | { |
1559 | { |
1553 | sv = newSVsv (POPs); |
1560 | sv = newSVsv (POPs); |
1554 | FREETMPS; LEAVE; |
1561 | FREETMPS; LEAVE; |
1555 | return sv; |
1562 | return sv; |
1556 | } |
1563 | } |
|
|
1564 | else if (count) |
|
|
1565 | croak ("filter_json_object callbacks must not return more than one scalar"); |
1557 | |
1566 | |
1558 | SvREFCNT_inc (sv); |
1567 | SvREFCNT_inc (sv); |
1559 | FREETMPS; LEAVE; |
1568 | FREETMPS; LEAVE; |
1560 | } |
1569 | } |
1561 | } |
1570 | } |
… | |
… | |
1666 | case '-': |
1675 | case '-': |
1667 | case '0': case '1': case '2': case '3': case '4': |
1676 | case '0': case '1': case '2': case '3': case '4': |
1668 | case '5': case '6': case '7': case '8': case '9': |
1677 | case '5': case '6': case '7': case '8': case '9': |
1669 | return decode_num (dec); |
1678 | return decode_num (dec); |
1670 | |
1679 | |
|
|
1680 | case 'f': |
|
|
1681 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
|
|
1682 | { |
|
|
1683 | dec->cur += 5; |
|
|
1684 | |
|
|
1685 | if (expect_false (!dec->json.v_false)) |
|
|
1686 | dec->json.v_false = GET_BOOL (false); |
|
|
1687 | |
|
|
1688 | return newSVsv (dec->json.v_false); |
|
|
1689 | } |
|
|
1690 | else |
|
|
1691 | ERR ("'false' expected"); |
|
|
1692 | |
|
|
1693 | break; |
|
|
1694 | |
1671 | case 't': |
1695 | case 't': |
1672 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1696 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1673 | { |
1697 | { |
1674 | dec->cur += 4; |
1698 | dec->cur += 4; |
1675 | #if JSON_SLOW |
1699 | |
1676 | bool_true = get_bool ("Types::Serialiser::true"); |
1700 | if (expect_false (!dec->json.v_true)) |
1677 | #endif |
1701 | dec->json.v_true = GET_BOOL (true); |
|
|
1702 | |
1678 | return newSVsv (bool_true); |
1703 | return newSVsv (dec->json.v_true); |
1679 | } |
1704 | } |
1680 | else |
1705 | else |
1681 | ERR ("'true' expected"); |
1706 | ERR ("'true' expected"); |
1682 | |
|
|
1683 | break; |
|
|
1684 | |
|
|
1685 | case 'f': |
|
|
1686 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
|
|
1687 | { |
|
|
1688 | dec->cur += 5; |
|
|
1689 | #if JSON_SLOW |
|
|
1690 | bool_false = get_bool ("Types::Serialiser::false"); |
|
|
1691 | #endif |
|
|
1692 | return newSVsv (bool_false); |
|
|
1693 | } |
|
|
1694 | else |
|
|
1695 | ERR ("'false' expected"); |
|
|
1696 | |
1707 | |
1697 | break; |
1708 | break; |
1698 | |
1709 | |
1699 | case 'n': |
1710 | case 'n': |
1700 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) |
1711 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) |
… | |
… | |
1784 | else if (sv) |
1795 | else if (sv) |
1785 | { |
1796 | { |
1786 | // check for trailing garbage |
1797 | // check for trailing garbage |
1787 | decode_ws (&dec); |
1798 | decode_ws (&dec); |
1788 | |
1799 | |
1789 | if (*dec.cur) |
1800 | if (dec.cur != dec.end) |
1790 | { |
1801 | { |
1791 | dec.err = "garbage after JSON object"; |
1802 | dec.err = "garbage after JSON object"; |
1792 | SvREFCNT_dec (sv); |
1803 | SvREFCNT_dec (sv); |
1793 | sv = 0; |
1804 | sv = 0; |
1794 | } |
1805 | } |
… | |
… | |
1995 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
2006 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1996 | : -1; |
2007 | : -1; |
1997 | |
2008 | |
1998 | json_stash = gv_stashpv ("JSON::XS" , 1); |
2009 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1999 | bool_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
2010 | bool_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
|
|
2011 | bool_false = get_bool ("Types::Serialiser::false"); |
2000 | bool_true = get_bool ("Types::Serialiser::true"); |
2012 | bool_true = get_bool ("Types::Serialiser::true"); |
2001 | bool_false = get_bool ("Types::Serialiser::false"); |
|
|
2002 | |
2013 | |
2003 | sv_json = newSVpv ("JSON", 0); |
2014 | sv_json = newSVpv ("JSON", 0); |
2004 | SvREADONLY_on (sv_json); |
2015 | SvREADONLY_on (sv_json); |
2005 | |
2016 | |
2006 | CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
2017 | CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
… | |
… | |
2008 | |
2019 | |
2009 | PROTOTYPES: DISABLE |
2020 | PROTOTYPES: DISABLE |
2010 | |
2021 | |
2011 | void CLONE (...) |
2022 | void CLONE (...) |
2012 | CODE: |
2023 | CODE: |
|
|
2024 | // as long as these writes are atomic, the race should not matter |
|
|
2025 | // as existing threads either already use 0, or use the old value, |
|
|
2026 | // which is sitll correct for the initial thread. |
2013 | json_stash = 0; |
2027 | json_stash = 0; |
2014 | bool_stash = 0; |
2028 | bool_stash = 0; |
|
|
2029 | bool_false = 0; |
|
|
2030 | bool_true = 0; |
2015 | |
2031 | |
2016 | void new (char *klass) |
2032 | void new (char *klass) |
2017 | PPCODE: |
2033 | PPCODE: |
2018 | { |
2034 | { |
2019 | SV *pv = NEWSV (0, sizeof (JSON)); |
2035 | SV *pv = NEWSV (0, sizeof (JSON)); |
… | |
… | |
2022 | XPUSHs (sv_2mortal (sv_bless ( |
2038 | XPUSHs (sv_2mortal (sv_bless ( |
2023 | newRV_noinc (pv), |
2039 | newRV_noinc (pv), |
2024 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
2040 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
2025 | ))); |
2041 | ))); |
2026 | } |
2042 | } |
|
|
2043 | |
|
|
2044 | void boolean_values (JSON *self, SV *v_false = 0, SV *v_true = 0) |
|
|
2045 | PPCODE: |
|
|
2046 | self->v_false = newSVsv (v_false); |
|
|
2047 | self->v_true = newSVsv (v_true); |
|
|
2048 | XPUSHs (ST (0)); |
|
|
2049 | |
|
|
2050 | void get_boolean_values (JSON *self) |
|
|
2051 | PPCODE: |
|
|
2052 | if (self->v_false && self->v_true) |
|
|
2053 | { |
|
|
2054 | EXTEND (SP, 2); |
|
|
2055 | PUSHs (self->v_false); |
|
|
2056 | PUSHs (self->v_true); |
|
|
2057 | } |
2027 | |
2058 | |
2028 | void ascii (JSON *self, int enable = 1) |
2059 | void ascii (JSON *self, int enable = 1) |
2029 | ALIAS: |
2060 | ALIAS: |
2030 | ascii = F_ASCII |
2061 | ascii = F_ASCII |
2031 | latin1 = F_LATIN1 |
2062 | latin1 = F_LATIN1 |
… | |
… | |
2268 | self->incr_mode = 0; |
2299 | self->incr_mode = 0; |
2269 | } |
2300 | } |
2270 | |
2301 | |
2271 | void DESTROY (JSON *self) |
2302 | void DESTROY (JSON *self) |
2272 | CODE: |
2303 | CODE: |
|
|
2304 | SvREFCNT_dec (self->v_false); |
|
|
2305 | SvREFCNT_dec (self->v_true); |
2273 | SvREFCNT_dec (self->cb_sk_object); |
2306 | SvREFCNT_dec (self->cb_sk_object); |
2274 | SvREFCNT_dec (self->cb_object); |
2307 | SvREFCNT_dec (self->cb_object); |
2275 | SvREFCNT_dec (self->incr_text); |
2308 | SvREFCNT_dec (self->incr_text); |
2276 | |
2309 | |
2277 | PROTOTYPES: ENABLE |
2310 | PROTOTYPES: ENABLE |