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.107 by root, Wed Mar 17 01:45:43 2010 UTC vs.
Revision 1.117 by root, Fri Oct 25 20:19:57 2013 UTC

42#define INIT_SIZE 32 // initial scalar size to be allocated 42#define INIT_SIZE 32 // initial scalar size to be allocated
43#define INDENT_STEP 3 // spaces per indentation level 43#define INDENT_STEP 3 // spaces per indentation level
44 44
45#define SHORT_STRING_LEN 16384 // special-case strings of up to this size 45#define SHORT_STRING_LEN 16384 // special-case strings of up to this size
46 46
47#define DECODE_WANTS_OCTETS(json) ((json)->flags & F_UTF8)
48
47#define SB do { 49#define SB do {
48#define SE } while (0) 50#define SE } while (0)
49 51
50#if __GNUC__ >= 3 52#if __GNUC__ >= 3
51# define expect(expr,value) __builtin_expect ((expr), (value)) 53# define expect(expr,value) __builtin_expect ((expr), (value))
69# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) 71# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1))
70#else 72#else
71# define JSON_SLOW 0 73# define JSON_SLOW 0
72# define JSON_STASH json_stash 74# define JSON_STASH json_stash
73#endif 75#endif
76
77// the amount of HEs to allocate on the stack, when sorting keys
78#define STACK_HES 64
74 79
75static HV *json_stash, *json_boolean_stash; // JSON::XS:: 80static HV *json_stash, *json_boolean_stash; // JSON::XS::
76static SV *json_true, *json_false; 81static SV *json_true, *json_false;
77 82
78enum { 83enum {
258 } 263 }
259 264
260 // this relies greatly on the quality of the pow () 265 // this relies greatly on the quality of the pow ()
261 // implementation of the platform, but a good 266 // implementation of the platform, but a good
262 // implementation is hard to beat. 267 // implementation is hard to beat.
268 // (IEEE 754 conformant ones are required to be exact)
263 if (postdp) *expo -= eaccum; 269 if (postdp) *expo -= eaccum;
264 *accum += uaccum * Perl_pow (10., *expo); 270 *accum += uaccum * Perl_pow (10., *expo);
265 *expo += eaccum; 271 *expo += eaccum;
266} 272}
267 273
477 483
478 if (enc->indent >= enc->json.max_depth) 484 if (enc->indent >= enc->json.max_depth)
479 croak (ERR_NESTING_EXCEEDED); 485 croak (ERR_NESTING_EXCEEDED);
480 486
481 encode_ch (enc, '['); 487 encode_ch (enc, '[');
482 488
483 if (len >= 0) 489 if (len >= 0)
484 { 490 {
485 encode_nl (enc); ++enc->indent; 491 encode_nl (enc); ++enc->indent;
486 492
487 for (i = 0; i <= len; ++i) 493 for (i = 0; i <= len; ++i)
499 encode_comma (enc); 505 encode_comma (enc);
500 } 506 }
501 507
502 encode_nl (enc); --enc->indent; encode_indent (enc); 508 encode_nl (enc); --enc->indent; encode_indent (enc);
503 } 509 }
504 510
505 encode_ch (enc, ']'); 511 encode_ch (enc, ']');
506} 512}
507 513
508static void 514static void
509encode_hk (enc_t *enc, HE *he) 515encode_hk (enc_t *enc, HE *he)
513 if (HeKLEN (he) == HEf_SVKEY) 519 if (HeKLEN (he) == HEf_SVKEY)
514 { 520 {
515 SV *sv = HeSVKEY (he); 521 SV *sv = HeSVKEY (he);
516 STRLEN len; 522 STRLEN len;
517 char *str; 523 char *str;
518 524
519 SvGETMAGIC (sv); 525 SvGETMAGIC (sv);
520 str = SvPV (sv, len); 526 str = SvPV (sv, len);
521 527
522 encode_str (enc, str, len, SvUTF8 (sv)); 528 encode_str (enc, str, len, SvUTF8 (sv));
523 } 529 }
589 } 595 }
590 596
591 if (count) 597 if (count)
592 { 598 {
593 int i, fast = 1; 599 int i, fast = 1;
594#if defined(__BORLANDC__) || defined(_MSC_VER) 600 HE *hes_stack [STACK_HES];
595 HE **hes = _alloca (count * sizeof (HE)); 601 HE **hes = hes_stack;
596#else 602
597 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode 603 // allocate larger arrays on the heap
598#endif 604 if (count > STACK_HES)
605 {
606 SV *sv = sv_2mortal (NEWSV (0, count * sizeof (*hes)));
607 hes = (HE **)SvPVX (sv);
608 }
599 609
600 i = 0; 610 i = 0;
601 while ((he = hv_iternext (hv))) 611 while ((he = hv_iternext (hv)))
602 { 612 {
603 hes [i++] = he; 613 hes [i++] = he;
822 } 832 }
823 else 833 else
824 { 834 {
825 // large integer, use the (rather slow) snprintf way. 835 // large integer, use the (rather slow) snprintf way.
826 need (enc, IVUV_MAXCHARS); 836 need (enc, IVUV_MAXCHARS);
827 enc->cur += 837 enc->cur +=
828 SvIsUV(sv) 838 SvIsUV(sv)
829 ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) 839 ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv))
830 : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); 840 : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv));
831 } 841 }
832 } 842 }
833 else if (SvROK (sv)) 843 else if (SvROK (sv))
834 encode_rv (enc, SvRV (sv)); 844 encode_rv (enc, SvRV (sv));
835 else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) 845 else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN)
836 encode_str (enc, "null", 4, 0); 846 encode_str (enc, "null", 4, 0);
837 else 847 else
838 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", 848 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, check your input data",
839 SvPV_nolen (sv), SvFLAGS (sv)); 849 SvPV_nolen (sv), (unsigned int)SvFLAGS (sv));
840} 850}
841 851
842static SV * 852static SV *
843encode_json (SV *scalar, JSON *json) 853encode_json (SV *scalar, JSON *json)
844{ 854{
1256 if (*dec->cur == ']') 1266 if (*dec->cur == ']')
1257 { 1267 {
1258 ++dec->cur; 1268 ++dec->cur;
1259 break; 1269 break;
1260 } 1270 }
1261 1271
1262 if (*dec->cur != ',') 1272 if (*dec->cur != ',')
1263 ERR (", or ] expected while parsing array"); 1273 ERR (", or ] expected while parsing array");
1264 1274
1265 ++dec->cur; 1275 ++dec->cur;
1266 1276
1454{ 1464{
1455 // the beauty of JSON: you need exactly one character lookahead 1465 // the beauty of JSON: you need exactly one character lookahead
1456 // to parse everything. 1466 // to parse everything.
1457 switch (*dec->cur) 1467 switch (*dec->cur)
1458 { 1468 {
1459 case '"': ++dec->cur; return decode_str (dec); 1469 case '"': ++dec->cur; return decode_str (dec);
1460 case '[': ++dec->cur; return decode_av (dec); 1470 case '[': ++dec->cur; return decode_av (dec);
1461 case '{': ++dec->cur; return decode_hv (dec); 1471 case '{': ++dec->cur; return decode_hv (dec);
1462 1472
1463 case '-': 1473 case '-':
1464 case '0': case '1': case '2': case '3': case '4': 1474 case '0': case '1': case '2': case '3': case '4':
1465 case '5': case '6': case '7': case '8': case '9': 1475 case '5': case '6': case '7': case '8': case '9':
1518{ 1528{
1519 dec_t dec; 1529 dec_t dec;
1520 SV *sv; 1530 SV *sv;
1521 1531
1522 /* work around bugs in 5.10 where manipulating magic values 1532 /* work around bugs in 5.10 where manipulating magic values
1523 * will perl ignore the magic in subsequent accesses 1533 * makes perl ignore the magic in subsequent accesses.
1534 * also make a copy of non-PV values, to get them into a clean
1535 * state (SvPV should do that, but it's buggy, see below).
1524 */ 1536 */
1525 /*SvGETMAGIC (string);*/ 1537 /*SvGETMAGIC (string);*/
1526 if (SvMAGICAL (string)) 1538 if (SvMAGICAL (string) || !SvPOK (string))
1527 string = sv_2mortal (newSVsv (string)); 1539 string = sv_2mortal (newSVsv (string));
1528 1540
1529 SvUPGRADE (string, SVt_PV); 1541 SvUPGRADE (string, SVt_PV);
1530 1542
1531 /* work around a bug in perl 5.10, which causes SvCUR to fail an 1543 /* work around a bug in perl 5.10, which causes SvCUR to fail an
1548 if (offset > json->max_size && json->max_size) 1560 if (offset > json->max_size && json->max_size)
1549 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", 1561 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
1550 (unsigned long)SvCUR (string), (unsigned long)json->max_size); 1562 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1551 } 1563 }
1552 1564
1553 if (json->flags & F_UTF8) 1565 if (DECODE_WANTS_OCTETS (json))
1554 sv_utf8_downgrade (string, 0); 1566 sv_utf8_downgrade (string, 0);
1555 else 1567 else
1556 sv_utf8_upgrade (string); 1568 sv_utf8_upgrade (string);
1557 1569
1558 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 1570 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
1600 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); 1612 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ);
1601 LEAVE; 1613 LEAVE;
1602 1614
1603 croak ("%s, at character offset %d (before \"%s\")", 1615 croak ("%s, at character offset %d (before \"%s\")",
1604 dec.err, 1616 dec.err,
1605 ptr_to_index (string, dec.cur), 1617 (int)ptr_to_index (string, dec.cur),
1606 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1618 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1607 } 1619 }
1608 1620
1609 sv = sv_2mortal (sv); 1621 sv = sv_2mortal (sv);
1610 1622
1800 json_boolean_stash = 0; 1812 json_boolean_stash = 0;
1801 1813
1802void new (char *klass) 1814void new (char *klass)
1803 PPCODE: 1815 PPCODE:
1804{ 1816{
1805 SV *pv = NEWSV (0, sizeof (JSON)); 1817 SV *pv = NEWSV (0, sizeof (JSON));
1806 SvPOK_only (pv); 1818 SvPOK_only (pv);
1807 json_init ((JSON *)SvPVX (pv)); 1819 json_init ((JSON *)SvPVX (pv));
1808 XPUSHs (sv_2mortal (sv_bless ( 1820 XPUSHs (sv_2mortal (sv_bless (
1809 newRV_noinc (pv), 1821 newRV_noinc (pv),
1810 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) 1822 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1887} 1899}
1888 1900
1889void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) 1901void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef)
1890 PPCODE: 1902 PPCODE:
1891{ 1903{
1892 if (!self->cb_sk_object) 1904 if (!self->cb_sk_object)
1893 self->cb_sk_object = newHV (); 1905 self->cb_sk_object = newHV ();
1894 1906
1895 if (SvOK (cb)) 1907 if (SvOK (cb))
1896 hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); 1908 hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0);
1897 else 1909 else
1908 XPUSHs (ST (0)); 1920 XPUSHs (ST (0));
1909} 1921}
1910 1922
1911void encode (JSON *self, SV *scalar) 1923void encode (JSON *self, SV *scalar)
1912 PPCODE: 1924 PPCODE:
1913 XPUSHs (encode_json (scalar, self)); 1925 PUTBACK; scalar = encode_json (scalar, self); SPAGAIN;
1926 XPUSHs (scalar);
1914 1927
1915void decode (JSON *self, SV *jsonstr) 1928void decode (JSON *self, SV *jsonstr)
1916 PPCODE: 1929 PPCODE:
1917 XPUSHs (decode_json (jsonstr, self, 0)); 1930 PUTBACK; jsonstr = decode_json (jsonstr, self, 0); SPAGAIN;
1931 XPUSHs (jsonstr);
1918 1932
1919void decode_prefix (JSON *self, SV *jsonstr) 1933void decode_prefix (JSON *self, SV *jsonstr)
1920 PPCODE: 1934 PPCODE:
1921{ 1935{
1936 SV *sv;
1922 char *offset; 1937 char *offset;
1938 PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN;
1923 EXTEND (SP, 2); 1939 EXTEND (SP, 2);
1924 PUSHs (decode_json (jsonstr, self, &offset)); 1940 PUSHs (sv);
1925 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); 1941 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset))));
1926} 1942}
1927 1943
1928void incr_parse (JSON *self, SV *jsonstr = 0) 1944void incr_parse (JSON *self, SV *jsonstr = 0)
1929 PPCODE: 1945 PPCODE:
1930{ 1946{
1931 if (!self->incr_text) 1947 if (!self->incr_text)
1932 self->incr_text = newSVpvn ("", 0); 1948 self->incr_text = newSVpvn ("", 0);
1949
1950 /* if utf8-ness doesn't match the decoder, need to upgrade/downgrade */
1951 if (!DECODE_WANTS_OCTETS (self) == !SvUTF8 (self->incr_text))
1952 if (DECODE_WANTS_OCTETS (self))
1953 {
1954 if (self->incr_pos)
1955 self->incr_pos = utf8_length ((U8 *)SvPVX (self->incr_text),
1956 (U8 *)SvPVX (self->incr_text) + self->incr_pos);
1957
1958 sv_utf8_downgrade (self->incr_text, 0);
1959 }
1960 else
1961 {
1962 sv_utf8_upgrade (self->incr_text);
1963
1964 if (self->incr_pos)
1965 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1966 - (U8 *)SvPVX (self->incr_text);
1967 }
1933 1968
1934 // append data, if any 1969 // append data, if any
1935 if (jsonstr) 1970 if (jsonstr)
1936 { 1971 {
1972 /* make sure both strings have same encoding */
1973 if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text))
1937 if (SvUTF8 (jsonstr)) 1974 if (SvUTF8 (jsonstr))
1975 sv_utf8_downgrade (jsonstr, 0);
1938 { 1976 else
1939 if (!SvUTF8 (self->incr_text))
1940 {
1941 /* utf-8-ness differs, need to upgrade */
1942 sv_utf8_upgrade (self->incr_text);
1943
1944 if (self->incr_pos)
1945 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1946 - (U8 *)SvPVX (self->incr_text);
1947 }
1948 }
1949 else if (SvUTF8 (self->incr_text))
1950 sv_utf8_upgrade (jsonstr); 1977 sv_utf8_upgrade (jsonstr);
1951 1978
1979 /* and then just blindly append */
1952 { 1980 {
1953 STRLEN len; 1981 STRLEN len;
1954 const char *str = SvPV (jsonstr, len); 1982 const char *str = SvPV (jsonstr, len);
1955 STRLEN cur = SvCUR (self->incr_text); 1983 STRLEN cur = SvCUR (self->incr_text);
1956 1984
1964 } 1992 }
1965 1993
1966 if (GIMME_V != G_VOID) 1994 if (GIMME_V != G_VOID)
1967 do 1995 do
1968 { 1996 {
1997 SV *sv;
1969 char *offset; 1998 char *offset;
1970 1999
1971 if (!INCR_DONE (self)) 2000 if (!INCR_DONE (self))
1972 { 2001 {
1973 incr_parse (self); 2002 incr_parse (self);
1975 if (self->incr_pos > self->max_size && self->max_size) 2004 if (self->incr_pos > self->max_size && self->max_size)
1976 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", 2005 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
1977 (unsigned long)self->incr_pos, (unsigned long)self->max_size); 2006 (unsigned long)self->incr_pos, (unsigned long)self->max_size);
1978 2007
1979 if (!INCR_DONE (self)) 2008 if (!INCR_DONE (self))
2009 {
2010 // as an optimisation, do not accumulate white space in the incr buffer
2011 if (self->incr_mode == INCR_M_WS && self->incr_pos)
2012 {
2013 self->incr_pos = 0;
2014 SvCUR_set (self->incr_text, 0);
2015 }
2016
1980 break; 2017 break;
2018 }
1981 } 2019 }
1982 2020
1983 XPUSHs (decode_json (self->incr_text, self, &offset)); 2021 PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN;
2022 XPUSHs (sv);
1984 2023
1985 self->incr_pos -= offset - SvPVX (self->incr_text); 2024 self->incr_pos -= offset - SvPVX (self->incr_text);
1986 self->incr_nest = 0; 2025 self->incr_nest = 0;
1987 self->incr_mode = 0; 2026 self->incr_mode = 0;
1988 2027
2040 PPCODE: 2079 PPCODE:
2041{ 2080{
2042 JSON json; 2081 JSON json;
2043 json_init (&json); 2082 json_init (&json);
2044 json.flags |= ix; 2083 json.flags |= ix;
2045 XPUSHs (encode_json (scalar, &json)); 2084 PUTBACK; scalar = encode_json (scalar, &json); SPAGAIN;
2085 XPUSHs (scalar);
2046} 2086}
2047 2087
2048void decode_json (SV *jsonstr) 2088void decode_json (SV *jsonstr)
2049 ALIAS: 2089 ALIAS:
2050 from_json_ = 0 2090 from_json_ = 0
2052 PPCODE: 2092 PPCODE:
2053{ 2093{
2054 JSON json; 2094 JSON json;
2055 json_init (&json); 2095 json_init (&json);
2056 json.flags |= ix; 2096 json.flags |= ix;
2057 XPUSHs (decode_json (jsonstr, &json, 0)); 2097 PUTBACK; jsonstr = decode_json (jsonstr, &json, 0); SPAGAIN;
2098 XPUSHs (jsonstr);
2058} 2099}
2059 2100

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines