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.121 by root, Tue Oct 29 00:06:40 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
690 SvGETMAGIC (sv); 744 SvGETMAGIC (sv);
691 svt = SvTYPE (sv); 745 svt = SvTYPE (sv);
692 746
693 if (expect_false (SvOBJECT (sv))) 747 if (expect_false (SvOBJECT (sv)))
694 { 748 {
695 HV *boolean_stash = !JSON_SLOW || types_boolean_stash
696 ? types_boolean_stash
697 : gv_stashpv ("Types::Serialiser::Boolean", 1);
698 HV *stash = SvSTASH (sv); 749 HV *stash = SvSTASH (sv);
699 750
700 if (stash == boolean_stash) 751 if (stash == bool_stash)
701 { 752 {
702 if (SvIV (sv)) 753 if (SvIV (sv))
703 encode_str (enc, "true", 4, 0); 754 encode_str (enc, "true", 4, 0);
704 else 755 else
705 encode_str (enc, "false", 5, 0); 756 encode_str (enc, "false", 5, 0);
707 else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0))) 758 else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0)))
708 { 759 {
709 int count; 760 int count;
710 dSP; 761 dSP;
711 762
712 ENTER; SAVETMPS; PUSHMARK (SP); 763 ENTER; SAVETMPS;
764 SAVESTACK_POS ();
765 PUSHMARK (SP);
713 EXTEND (SP, 2); 766 EXTEND (SP, 2);
714 // we re-bless the reference to get overload and other niceties right 767 // we re-bless the reference to get overload and other niceties right
715 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 768 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
716 PUSHs (sv_json); 769 PUSHs (sv_json);
717 770
719 count = call_sv ((SV *)GvCV (method), G_ARRAY); 772 count = call_sv ((SV *)GvCV (method), G_ARRAY);
720 SPAGAIN; 773 SPAGAIN;
721 774
722 // catch this surprisingly common error 775 // catch this surprisingly common error
723 if (SvROK (TOPs) && SvRV (TOPs) == sv) 776 if (SvROK (TOPs) && SvRV (TOPs) == sv)
724 croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); 777 croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
725 778
726 encode_ch (enc, '('); 779 encode_ch (enc, '(');
727 encode_ch (enc, '"'); 780 encode_ch (enc, '"');
728 encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 781 encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash));
729 encode_ch (enc, '"'); 782 encode_ch (enc, '"');
738 encode_ch (enc, ','); 791 encode_ch (enc, ',');
739 } 792 }
740 793
741 encode_ch (enc, ']'); 794 encode_ch (enc, ']');
742 795
743 PUTBACK;
744
745 FREETMPS; LEAVE; 796 FREETMPS; LEAVE;
746 } 797 }
747 else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0))) 798 else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0)))
748 { 799 {
749 dSP; 800 dSP;
750 801
751 ENTER; SAVETMPS; PUSHMARK (SP); 802 ENTER; SAVETMPS;
803 PUSHMARK (SP);
752 // 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
753 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 805 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
754 806
755 // calling with G_SCALAR ensures that we always get a 1 return value 807 // calling with G_SCALAR ensures that we always get a 1 return value
756 PUTBACK; 808 PUTBACK;
778 encode_hv (enc, (HV *)sv); 830 encode_hv (enc, (HV *)sv);
779 else if (svt == SVt_PVAV) 831 else if (svt == SVt_PVAV)
780 encode_av (enc, (AV *)sv); 832 encode_av (enc, (AV *)sv);
781 else if (svt < SVt_PVAV) 833 else if (svt < SVt_PVAV)
782 { 834 {
783 STRLEN len = 0; 835 int bool_type = ref_bool_type (sv);
784 char *pv = svt ? SvPV (sv, len) : 0;
785 836
786 if (len == 1 && *pv == '1') 837 if (bool_type == 1)
787 encode_str (enc, "true", 4, 0); 838 encode_str (enc, "true", 4, 0);
788 else if (len == 1 && *pv == '0') 839 else if (bool_type == 0)
789 encode_str (enc, "false", 5, 0); 840 encode_str (enc, "false", 5, 0);
790 else if (enc->json.flags & F_ALLOW_UNKNOWN) 841 else if (enc->json.flags & F_ALLOW_UNKNOWN)
791 encode_str (enc, "null", 4, 0); 842 encode_str (enc, "null", 4, 0);
792 else 843 else
793 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",
876static SV * 927static SV *
877encode_json (SV *scalar, JSON *json) 928encode_json (SV *scalar, JSON *json)
878{ 929{
879 enc_t enc; 930 enc_t enc;
880 931
881 if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) 932 if (!(json->flags & F_ALLOW_NONREF) && json_nonref (scalar))
882 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)");
883 934
884 enc.json = *json; 935 enc.json = *json;
885 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 936 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
886 enc.cur = SvPVX (enc.sv); 937 enc.cur = SvPVX (enc.sv);
1093 *cur++ = *dec_cur++; 1144 *cur++ = *dec_cur++;
1094 while (--clen); 1145 while (--clen);
1095 1146
1096 utf8 = 1; 1147 utf8 = 1;
1097 } 1148 }
1149 else if (ch == '\t' && dec->json.flags & F_RELAXED)
1150 *cur++ = ch;
1098 else 1151 else
1099 { 1152 {
1100 --dec_cur; 1153 --dec_cur;
1101 1154
1102 if (!ch) 1155 if (!ch)
1424 1477
1425 hv_iterinit (hv); 1478 hv_iterinit (hv);
1426 he = hv_iternext (hv); 1479 he = hv_iternext (hv);
1427 hv_iterinit (hv); 1480 hv_iterinit (hv);
1428 1481
1429 // the next line creates a mortal sv each time its called. 1482 // the next line creates a mortal sv each time it's called.
1430 // might want to optimise this for common cases. 1483 // might want to optimise this for common cases.
1431 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);
1432 1485
1433 if (cb) 1486 if (cb)
1434 { 1487 {
1435 dSP; 1488 dSP;
1436 int count; 1489 int count;
1437 1490
1438 ENTER; SAVETMPS; PUSHMARK (SP); 1491 ENTER; SAVETMPS;
1492 SAVESTACK_POS ();
1493 PUSHMARK (SP);
1439 XPUSHs (HeVAL (he)); 1494 XPUSHs (HeVAL (he));
1440 sv_2mortal (sv); 1495 sv_2mortal (sv);
1441 1496
1442 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; 1497 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1443 1498
1456 if (dec->json.cb_object) 1511 if (dec->json.cb_object)
1457 { 1512 {
1458 dSP; 1513 dSP;
1459 int count; 1514 int count;
1460 1515
1461 ENTER; SAVETMPS; PUSHMARK (SP); 1516 ENTER; SAVETMPS;
1517 SAVESTACK_POS ();
1518 PUSHMARK (SP);
1462 XPUSHs (sv_2mortal (sv)); 1519 XPUSHs (sv_2mortal (sv));
1463 1520
1464 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; 1521 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN;
1465 1522
1466 if (count == 1) 1523 if (count == 1)
1533 if (!method) 1590 if (!method)
1534 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)");
1535 1592
1536 dSP; 1593 dSP;
1537 1594
1538 ENTER; SAVETMPS; PUSHMARK (SP); 1595 ENTER; SAVETMPS;
1596 PUSHMARK (SP);
1539 EXTEND (SP, len + 2); 1597 EXTEND (SP, len + 2);
1540 // 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
1541 PUSHs (tag); 1599 PUSHs (tag);
1542 PUSHs (sv_json); 1600 PUSHs (sv_json);
1543 1601
1585 case 't': 1643 case 't':
1586 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1644 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1587 { 1645 {
1588 dec->cur += 4; 1646 dec->cur += 4;
1589#if JSON_SLOW 1647#if JSON_SLOW
1590 types_true = get_bool ("Types::Serialiser::true"); 1648 bool_true = get_bool ("Types::Serialiser::true");
1591#endif 1649#endif
1592 return newSVsv (types_true); 1650 return newSVsv (bool_true);
1593 } 1651 }
1594 else 1652 else
1595 ERR ("'true' expected"); 1653 ERR ("'true' expected");
1596 1654
1597 break; 1655 break;
1599 case 'f': 1657 case 'f':
1600 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1658 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1601 { 1659 {
1602 dec->cur += 5; 1660 dec->cur += 5;
1603#if JSON_SLOW 1661#if JSON_SLOW
1604 types_false = get_bool ("Types::Serialiser::false"); 1662 bool_false = get_bool ("Types::Serialiser::false");
1605#endif 1663#endif
1606 return newSVsv (types_false); 1664 return newSVsv (bool_false);
1607 } 1665 }
1608 else 1666 else
1609 ERR ("'false' expected"); 1667 ERR ("'false' expected");
1610 1668
1611 break; 1669 break;
1725 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1783 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1726 } 1784 }
1727 1785
1728 sv = sv_2mortal (sv); 1786 sv = sv_2mortal (sv);
1729 1787
1730 if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) 1788 if (!(dec.json.flags & F_ALLOW_NONREF) && json_nonref (sv))
1731 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)");
1732 1790
1733 return sv; 1791 return sv;
1734} 1792}
1735 1793
1905 i >= '0' && i <= '9' ? i - '0' 1963 i >= '0' && i <= '9' ? i - '0'
1906 : i >= 'a' && i <= 'f' ? i - 'a' + 10 1964 : i >= 'a' && i <= 'f' ? i - 'a' + 10
1907 : i >= 'A' && i <= 'F' ? i - 'A' + 10 1965 : i >= 'A' && i <= 'F' ? i - 'A' + 10
1908 : -1; 1966 : -1;
1909 1967
1910 json_stash = gv_stashpv ("JSON::XS" , 1); 1968 json_stash = gv_stashpv ("JSON::XS" , 1);
1911 types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); 1969 bool_stash = gv_stashpv ("Types::Serialiser::Boolean", 1);
1912
1913 types_true = get_bool ("Types::Serialiser::true"); 1970 bool_true = get_bool ("Types::Serialiser::true");
1914 types_false = get_bool ("Types::Serialiser::false"); 1971 bool_false = get_bool ("Types::Serialiser::false");
1915 1972
1916 sv_json = newSVpv ("JSON", 0); 1973 sv_json = newSVpv ("JSON", 0);
1917 SvREADONLY_on (sv_json); 1974 SvREADONLY_on (sv_json);
1918 1975
1919 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 */
1921 1978
1922PROTOTYPES: DISABLE 1979PROTOTYPES: DISABLE
1923 1980
1924void CLONE (...) 1981void CLONE (...)
1925 CODE: 1982 CODE:
1926 json_stash = 0; 1983 json_stash = 0;
1927 types_boolean_stash = 0; 1984 bool_stash = 0;
1928 1985
1929void new (char *klass) 1986void new (char *klass)
1930 PPCODE: 1987 PPCODE:
1931{ 1988{
1932 SV *pv = NEWSV (0, sizeof (JSON)); 1989 SV *pv = NEWSV (0, sizeof (JSON));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines