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.122 by root, Tue Oct 29 15:55:49 2013 UTC vs.
Revision 1.128 by root, Fri Oct 7 05:18:48 2016 UTC

79#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?)"
80 80
81#ifdef USE_ITHREADS 81#ifdef USE_ITHREADS
82# define JSON_SLOW 1 82# define JSON_SLOW 1
83# 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))
84#else 85#else
85# define JSON_SLOW 0 86# define JSON_SLOW 0
86# define JSON_STASH json_stash 87# define JSON_STASH json_stash
88# define BOOL_STASH bool_stash
87#endif 89#endif
88 90
89// 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
90#define STACK_HES 64 92#define STACK_HES 64
91 93
92static HV *json_stash, *types_boolean_stash; // JSON::XS:: 94static HV *json_stash, *bool_stash; // JSON::XS::, Types::Serialiser::Boolean::
93static SV *types_true, *types_false, *sv_json; 95static SV *bool_true, *bool_false, *sv_json;
94 96
95enum { 97enum {
96 INCR_M_WS = 0, // initial whitespace skipping, must be 0 98 INCR_M_WS = 0, // initial whitespace skipping, must be 0
97 INCR_M_STR, // inside string 99 INCR_M_STR, // inside string
98 INCR_M_BS, // inside backslash 100 INCR_M_BS, // inside backslash
299 // a recursion depth of ten gives us >>500 bits 301 // a recursion depth of ten gives us >>500 bits
300 json_atof_scan1 (s, &accum, &expo, 0, 10); 302 json_atof_scan1 (s, &accum, &expo, 0, 10);
301 303
302 return neg ? -accum : accum; 304 return neg ? -accum : accum;
303} 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 return -1;
326}
327
328// returns whether scalar is not a reference in the sense of allow_nonref
329static int
330json_nonref (SV *scalar)
331{
332 if (!SvROK (scalar))
333 return 1;
334
335 scalar = SvRV (scalar);
336
337 if (SvTYPE (scalar) >= SVt_PVMG)
338 {
339 if (SvSTASH (scalar) == bool_stash)
340 return 1;
341
342 if (!SvOBJECT (scalar) && ref_bool_type (scalar) >= 0)
343 return 1;
344 }
345
346 return 0;
347}
348
304///////////////////////////////////////////////////////////////////////////// 349/////////////////////////////////////////////////////////////////////////////
305// encoder 350// encoder
306 351
307// structure used for encoding JSON 352// structure used for encoding JSON
308typedef struct 353typedef struct
701 SvGETMAGIC (sv); 746 SvGETMAGIC (sv);
702 svt = SvTYPE (sv); 747 svt = SvTYPE (sv);
703 748
704 if (expect_false (SvOBJECT (sv))) 749 if (expect_false (SvOBJECT (sv)))
705 { 750 {
706 HV *boolean_stash = !JSON_SLOW || types_boolean_stash
707 ? types_boolean_stash
708 : gv_stashpv ("Types::Serialiser::Boolean", 1);
709 HV *stash = SvSTASH (sv); 751 HV *stash = SvSTASH (sv);
710 752
711 if (stash == boolean_stash) 753 if (stash == bool_stash)
712 { 754 {
713 if (SvIV (sv)) 755 if (SvIV (sv))
714 encode_str (enc, "true", 4, 0); 756 encode_str (enc, "true", 4, 0);
715 else 757 else
716 encode_str (enc, "false", 5, 0); 758 encode_str (enc, "false", 5, 0);
718 else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0))) 760 else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0)))
719 { 761 {
720 int count; 762 int count;
721 dSP; 763 dSP;
722 764
723 ENTER; SAVETMPS; PUSHMARK (SP); 765 ENTER; SAVETMPS;
766 SAVESTACK_POS ();
767 PUSHMARK (SP);
724 EXTEND (SP, 2); 768 EXTEND (SP, 2);
725 // we re-bless the reference to get overload and other niceties right 769 // we re-bless the reference to get overload and other niceties right
726 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 770 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
727 PUSHs (sv_json); 771 PUSHs (sv_json);
728 772
730 count = call_sv ((SV *)GvCV (method), G_ARRAY); 774 count = call_sv ((SV *)GvCV (method), G_ARRAY);
731 SPAGAIN; 775 SPAGAIN;
732 776
733 // catch this surprisingly common error 777 // catch this surprisingly common error
734 if (SvROK (TOPs) && SvRV (TOPs) == sv) 778 if (SvROK (TOPs) && SvRV (TOPs) == sv)
735 croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); 779 croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
736 780
737 encode_ch (enc, '('); 781 encode_ch (enc, '(');
738 encode_ch (enc, '"'); 782 encode_ch (enc, '"');
739 encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 783 encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash));
740 encode_ch (enc, '"'); 784 encode_ch (enc, '"');
749 encode_ch (enc, ','); 793 encode_ch (enc, ',');
750 } 794 }
751 795
752 encode_ch (enc, ']'); 796 encode_ch (enc, ']');
753 797
754 PUTBACK;
755
756 FREETMPS; LEAVE; 798 FREETMPS; LEAVE;
757 } 799 }
758 else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0))) 800 else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0)))
759 { 801 {
760 dSP; 802 dSP;
761 803
762 ENTER; SAVETMPS; PUSHMARK (SP); 804 ENTER; SAVETMPS;
805 PUSHMARK (SP);
763 // we re-bless the reference to get overload and other niceties right 806 // we re-bless the reference to get overload and other niceties right
764 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 807 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
765 808
766 // calling with G_SCALAR ensures that we always get a 1 return value 809 // calling with G_SCALAR ensures that we always get a 1 return value
767 PUTBACK; 810 PUTBACK;
789 encode_hv (enc, (HV *)sv); 832 encode_hv (enc, (HV *)sv);
790 else if (svt == SVt_PVAV) 833 else if (svt == SVt_PVAV)
791 encode_av (enc, (AV *)sv); 834 encode_av (enc, (AV *)sv);
792 else if (svt < SVt_PVAV) 835 else if (svt < SVt_PVAV)
793 { 836 {
794 STRLEN len = 0; 837 int bool_type = ref_bool_type (sv);
795 char *pv = svt ? SvPV (sv, len) : 0;
796 838
797 if (len == 1 && *pv == '1') 839 if (bool_type == 1)
798 encode_str (enc, "true", 4, 0); 840 encode_str (enc, "true", 4, 0);
799 else if (len == 1 && *pv == '0') 841 else if (bool_type == 0)
800 encode_str (enc, "false", 5, 0); 842 encode_str (enc, "false", 5, 0);
801 else if (enc->json.flags & F_ALLOW_UNKNOWN) 843 else if (enc->json.flags & F_ALLOW_UNKNOWN)
802 encode_str (enc, "null", 4, 0); 844 encode_str (enc, "null", 4, 0);
803 else 845 else
804 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", 846 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1",
887static SV * 929static SV *
888encode_json (SV *scalar, JSON *json) 930encode_json (SV *scalar, JSON *json)
889{ 931{
890 enc_t enc; 932 enc_t enc;
891 933
892 if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) 934 if (!(json->flags & F_ALLOW_NONREF) && json_nonref (scalar))
893 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); 935 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)");
894 936
895 enc.json = *json; 937 enc.json = *json;
896 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 938 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
897 enc.cur = SvPVX (enc.sv); 939 enc.cur = SvPVX (enc.sv);
1104 *cur++ = *dec_cur++; 1146 *cur++ = *dec_cur++;
1105 while (--clen); 1147 while (--clen);
1106 1148
1107 utf8 = 1; 1149 utf8 = 1;
1108 } 1150 }
1151 else if (ch == '\t' && dec->json.flags & F_RELAXED)
1152 *cur++ = ch;
1109 else 1153 else
1110 { 1154 {
1111 --dec_cur; 1155 --dec_cur;
1112 1156
1113 if (!ch) 1157 if (!ch)
1435 1479
1436 hv_iterinit (hv); 1480 hv_iterinit (hv);
1437 he = hv_iternext (hv); 1481 he = hv_iternext (hv);
1438 hv_iterinit (hv); 1482 hv_iterinit (hv);
1439 1483
1440 // the next line creates a mortal sv each time its called. 1484 // the next line creates a mortal sv each time it's called.
1441 // might want to optimise this for common cases. 1485 // might want to optimise this for common cases.
1442 cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); 1486 cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0);
1443 1487
1444 if (cb) 1488 if (cb)
1445 { 1489 {
1446 dSP; 1490 dSP;
1447 int count; 1491 int count;
1448 1492
1449 ENTER; SAVETMPS; PUSHMARK (SP); 1493 ENTER; SAVETMPS;
1494 SAVESTACK_POS ();
1495 PUSHMARK (SP);
1450 XPUSHs (HeVAL (he)); 1496 XPUSHs (HeVAL (he));
1451 sv_2mortal (sv); 1497 sv_2mortal (sv);
1452 1498
1453 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; 1499 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1454 1500
1467 if (dec->json.cb_object) 1513 if (dec->json.cb_object)
1468 { 1514 {
1469 dSP; 1515 dSP;
1470 int count; 1516 int count;
1471 1517
1472 ENTER; SAVETMPS; PUSHMARK (SP); 1518 ENTER; SAVETMPS;
1519 SAVESTACK_POS ();
1520 PUSHMARK (SP);
1473 XPUSHs (sv_2mortal (sv)); 1521 XPUSHs (sv_2mortal (sv));
1474 1522
1475 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; 1523 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN;
1476 1524
1477 if (count == 1) 1525 if (count == 1)
1544 if (!method) 1592 if (!method)
1545 ERR ("cannot decode perl-object (package does not have a THAW method)"); 1593 ERR ("cannot decode perl-object (package does not have a THAW method)");
1546 1594
1547 dSP; 1595 dSP;
1548 1596
1549 ENTER; SAVETMPS; PUSHMARK (SP); 1597 ENTER; SAVETMPS;
1598 PUSHMARK (SP);
1550 EXTEND (SP, len + 2); 1599 EXTEND (SP, len + 2);
1551 // we re-bless the reference to get overload and other niceties right 1600 // we re-bless the reference to get overload and other niceties right
1552 PUSHs (tag); 1601 PUSHs (tag);
1553 PUSHs (sv_json); 1602 PUSHs (sv_json);
1554 1603
1596 case 't': 1645 case 't':
1597 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1646 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1598 { 1647 {
1599 dec->cur += 4; 1648 dec->cur += 4;
1600#if JSON_SLOW 1649#if JSON_SLOW
1601 types_true = get_bool ("Types::Serialiser::true"); 1650 bool_true = get_bool ("Types::Serialiser::true");
1602#endif 1651#endif
1603 return newSVsv (types_true); 1652 return newSVsv (bool_true);
1604 } 1653 }
1605 else 1654 else
1606 ERR ("'true' expected"); 1655 ERR ("'true' expected");
1607 1656
1608 break; 1657 break;
1610 case 'f': 1659 case 'f':
1611 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1660 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1612 { 1661 {
1613 dec->cur += 5; 1662 dec->cur += 5;
1614#if JSON_SLOW 1663#if JSON_SLOW
1615 types_false = get_bool ("Types::Serialiser::false"); 1664 bool_false = get_bool ("Types::Serialiser::false");
1616#endif 1665#endif
1617 return newSVsv (types_false); 1666 return newSVsv (bool_false);
1618 } 1667 }
1619 else 1668 else
1620 ERR ("'false' expected"); 1669 ERR ("'false' expected");
1621 1670
1622 break; 1671 break;
1649 1698
1650 /* work around bugs in 5.10 where manipulating magic values 1699 /* work around bugs in 5.10 where manipulating magic values
1651 * makes perl ignore the magic in subsequent accesses. 1700 * makes perl ignore the magic in subsequent accesses.
1652 * also make a copy of non-PV values, to get them into a clean 1701 * also make a copy of non-PV values, to get them into a clean
1653 * state (SvPV should do that, but it's buggy, see below). 1702 * state (SvPV should do that, but it's buggy, see below).
1703 *
1704 * SvIsCOW_shared_hash works around a bug in perl (possibly 5.16),
1705 * as reported by Reini Urban.
1654 */ 1706 */
1655 /*SvGETMAGIC (string);*/ 1707 /*SvGETMAGIC (string);*/
1656 if (SvMAGICAL (string) || !SvPOK (string)) 1708 if (SvMAGICAL (string) || !SvPOK (string) || SvIsCOW_shared_hash (string))
1657 string = sv_2mortal (newSVsv (string)); 1709 string = sv_2mortal (newSVsv (string));
1658 1710
1659 SvUPGRADE (string, SVt_PV); 1711 SvUPGRADE (string, SVt_PV);
1660 1712
1661 /* work around a bug in perl 5.10, which causes SvCUR to fail an 1713 /* work around a bug in perl 5.10, which causes SvCUR to fail an
1736 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1788 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1737 } 1789 }
1738 1790
1739 sv = sv_2mortal (sv); 1791 sv = sv_2mortal (sv);
1740 1792
1741 if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) 1793 if (!(dec.json.flags & F_ALLOW_NONREF) && json_nonref (sv))
1742 croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); 1794 croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)");
1743 1795
1744 return sv; 1796 return sv;
1745} 1797}
1746 1798
1916 i >= '0' && i <= '9' ? i - '0' 1968 i >= '0' && i <= '9' ? i - '0'
1917 : i >= 'a' && i <= 'f' ? i - 'a' + 10 1969 : i >= 'a' && i <= 'f' ? i - 'a' + 10
1918 : i >= 'A' && i <= 'F' ? i - 'A' + 10 1970 : i >= 'A' && i <= 'F' ? i - 'A' + 10
1919 : -1; 1971 : -1;
1920 1972
1921 json_stash = gv_stashpv ("JSON::XS" , 1); 1973 json_stash = gv_stashpv ("JSON::XS" , 1);
1922 types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); 1974 bool_stash = gv_stashpv ("Types::Serialiser::Boolean", 1);
1923
1924 types_true = get_bool ("Types::Serialiser::true"); 1975 bool_true = get_bool ("Types::Serialiser::true");
1925 types_false = get_bool ("Types::Serialiser::false"); 1976 bool_false = get_bool ("Types::Serialiser::false");
1926 1977
1927 sv_json = newSVpv ("JSON", 0); 1978 sv_json = newSVpv ("JSON", 0);
1928 SvREADONLY_on (sv_json); 1979 SvREADONLY_on (sv_json);
1929 1980
1930 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ 1981 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */
1932 1983
1933PROTOTYPES: DISABLE 1984PROTOTYPES: DISABLE
1934 1985
1935void CLONE (...) 1986void CLONE (...)
1936 CODE: 1987 CODE:
1937 json_stash = 0; 1988 json_stash = 0;
1938 types_boolean_stash = 0; 1989 bool_stash = 0;
1939 1990
1940void new (char *klass) 1991void new (char *klass)
1941 PPCODE: 1992 PPCODE:
1942{ 1993{
1943 SV *pv = NEWSV (0, sizeof (JSON)); 1994 SV *pv = NEWSV (0, sizeof (JSON));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines