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.125 by root, Sun Feb 21 15:37:53 2016 UTC vs.
Revision 1.126 by root, Sun Feb 21 16:18:19 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
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
304///////////////////////////////////////////////////////////////////////////// 347/////////////////////////////////////////////////////////////////////////////
305// encoder 348// encoder
306 349
307// structure used for encoding JSON 350// structure used for encoding JSON
308typedef struct 351typedef struct
701 SvGETMAGIC (sv); 744 SvGETMAGIC (sv);
702 svt = SvTYPE (sv); 745 svt = SvTYPE (sv);
703 746
704 if (expect_false (SvOBJECT (sv))) 747 if (expect_false (SvOBJECT (sv)))
705 { 748 {
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); 749 HV *stash = SvSTASH (sv);
710 750
711 if (stash == boolean_stash) 751 if (stash == bool_stash)
712 { 752 {
713 if (SvIV (sv)) 753 if (SvIV (sv))
714 encode_str (enc, "true", 4, 0); 754 encode_str (enc, "true", 4, 0);
715 else 755 else
716 encode_str (enc, "false", 5, 0); 756 encode_str (enc, "false", 5, 0);
790 encode_hv (enc, (HV *)sv); 830 encode_hv (enc, (HV *)sv);
791 else if (svt == SVt_PVAV) 831 else if (svt == SVt_PVAV)
792 encode_av (enc, (AV *)sv); 832 encode_av (enc, (AV *)sv);
793 else if (svt < SVt_PVAV) 833 else if (svt < SVt_PVAV)
794 { 834 {
795 STRLEN len = 0; 835 int bool_type = ref_bool_type (sv);
796 char *pv = svt ? SvPV (sv, len) : 0;
797 836
798 if (len == 1 && *pv == '1') 837 if (bool_type == 1)
799 encode_str (enc, "true", 4, 0); 838 encode_str (enc, "true", 4, 0);
800 else if (len == 1 && *pv == '0') 839 else if (bool_type == 0)
801 encode_str (enc, "false", 5, 0); 840 encode_str (enc, "false", 5, 0);
802 else if (enc->json.flags & F_ALLOW_UNKNOWN) 841 else if (enc->json.flags & F_ALLOW_UNKNOWN)
803 encode_str (enc, "null", 4, 0); 842 encode_str (enc, "null", 4, 0);
804 else 843 else
805 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",
883 else 922 else
884 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, check your input data", 923 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, check your input data",
885 SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); 924 SvPV_nolen (sv), (unsigned int)SvFLAGS (sv));
886} 925}
887 926
888static int
889json_scalar (SV *scalar)
890{
891 return 0;//D
892 if (!SvROK (scalar))
893 return 1;
894}
895
896static SV * 927static SV *
897encode_json (SV *scalar, JSON *json) 928encode_json (SV *scalar, JSON *json)
898{ 929{
899 enc_t enc; 930 enc_t enc;
900 931
901 if (!(json->flags & F_ALLOW_NONREF) && json_scalar (scalar)) 932 if (!(json->flags & F_ALLOW_NONREF) && json_nonref (scalar))
902 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)");
903 934
904 enc.json = *json; 935 enc.json = *json;
905 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 936 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
906 enc.cur = SvPVX (enc.sv); 937 enc.cur = SvPVX (enc.sv);
1612 case 't': 1643 case 't':
1613 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1644 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1614 { 1645 {
1615 dec->cur += 4; 1646 dec->cur += 4;
1616#if JSON_SLOW 1647#if JSON_SLOW
1617 types_true = get_bool ("Types::Serialiser::true"); 1648 bool_true = get_bool ("Types::Serialiser::true");
1618#endif 1649#endif
1619 return newSVsv (types_true); 1650 return newSVsv (bool_true);
1620 } 1651 }
1621 else 1652 else
1622 ERR ("'true' expected"); 1653 ERR ("'true' expected");
1623 1654
1624 break; 1655 break;
1626 case 'f': 1657 case 'f':
1627 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1658 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1628 { 1659 {
1629 dec->cur += 5; 1660 dec->cur += 5;
1630#if JSON_SLOW 1661#if JSON_SLOW
1631 types_false = get_bool ("Types::Serialiser::false"); 1662 bool_false = get_bool ("Types::Serialiser::false");
1632#endif 1663#endif
1633 return newSVsv (types_false); 1664 return newSVsv (bool_false);
1634 } 1665 }
1635 else 1666 else
1636 ERR ("'false' expected"); 1667 ERR ("'false' expected");
1637 1668
1638 break; 1669 break;
1752 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1783 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1753 } 1784 }
1754 1785
1755 sv = sv_2mortal (sv); 1786 sv = sv_2mortal (sv);
1756 1787
1757 if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) 1788 if (!(dec.json.flags & F_ALLOW_NONREF) && json_nonref (sv))
1758 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)");
1759 1790
1760 return sv; 1791 return sv;
1761} 1792}
1762 1793
1932 i >= '0' && i <= '9' ? i - '0' 1963 i >= '0' && i <= '9' ? i - '0'
1933 : i >= 'a' && i <= 'f' ? i - 'a' + 10 1964 : i >= 'a' && i <= 'f' ? i - 'a' + 10
1934 : i >= 'A' && i <= 'F' ? i - 'A' + 10 1965 : i >= 'A' && i <= 'F' ? i - 'A' + 10
1935 : -1; 1966 : -1;
1936 1967
1937 json_stash = gv_stashpv ("JSON::XS" , 1); 1968 json_stash = gv_stashpv ("JSON::XS" , 1);
1938 types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); 1969 bool_stash = gv_stashpv ("Types::Serialiser::Boolean", 1);
1939
1940 types_true = get_bool ("Types::Serialiser::true"); 1970 bool_true = get_bool ("Types::Serialiser::true");
1941 types_false = get_bool ("Types::Serialiser::false"); 1971 bool_false = get_bool ("Types::Serialiser::false");
1942 1972
1943 sv_json = newSVpv ("JSON", 0); 1973 sv_json = newSVpv ("JSON", 0);
1944 SvREADONLY_on (sv_json); 1974 SvREADONLY_on (sv_json);
1945 1975
1946 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 */
1948 1978
1949PROTOTYPES: DISABLE 1979PROTOTYPES: DISABLE
1950 1980
1951void CLONE (...) 1981void CLONE (...)
1952 CODE: 1982 CODE:
1953 json_stash = 0; 1983 json_stash = 0;
1954 types_boolean_stash = 0; 1984 bool_stash = 0;
1955 1985
1956void new (char *klass) 1986void new (char *klass)
1957 PPCODE: 1987 PPCODE:
1958{ 1988{
1959 SV *pv = NEWSV (0, sizeof (JSON)); 1989 SV *pv = NEWSV (0, sizeof (JSON));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines