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.106 by root, Tue Jan 19 01:36:34 2010 UTC vs.
Revision 1.115 by root, Fri Dec 21 05:16:19 2012 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
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 }
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, you might want to report this",
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{
1401 dSP; 1411 dSP;
1402 int count; 1412 int count;
1403 1413
1404 ENTER; SAVETMPS; PUSHMARK (SP); 1414 ENTER; SAVETMPS; PUSHMARK (SP);
1405 XPUSHs (HeVAL (he)); 1415 XPUSHs (HeVAL (he));
1416 sv_2mortal (sv);
1406 1417
1407 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; 1418 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1408 1419
1409 if (count == 1) 1420 if (count == 1)
1410 { 1421 {
1411 sv = newSVsv (POPs); 1422 sv = newSVsv (POPs);
1412 FREETMPS; LEAVE; 1423 FREETMPS; LEAVE;
1413 return sv; 1424 return sv;
1414 } 1425 }
1415 1426
1427 SvREFCNT_inc (sv);
1416 FREETMPS; LEAVE; 1428 FREETMPS; LEAVE;
1417 } 1429 }
1418 } 1430 }
1419 1431
1420 if (dec->json.cb_object) 1432 if (dec->json.cb_object)
1516{ 1528{
1517 dec_t dec; 1529 dec_t dec;
1518 SV *sv; 1530 SV *sv;
1519 1531
1520 /* work around bugs in 5.10 where manipulating magic values 1532 /* work around bugs in 5.10 where manipulating magic values
1521 * will perl ignore the magic in subsequent accesses 1533 * will 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).
1522 */ 1536 */
1523 /*SvGETMAGIC (string);*/ 1537 /*SvGETMAGIC (string);*/
1524 if (SvMAGICAL (string)) 1538 if (SvMAGICAL (string) || !SvPOK (string))
1525 string = sv_2mortal (newSVsv (string)); 1539 string = sv_2mortal (newSVsv (string));
1526 1540
1527 SvUPGRADE (string, SVt_PV); 1541 SvUPGRADE (string, SVt_PV);
1528 1542
1529 /* 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
1546 if (offset > json->max_size && json->max_size) 1560 if (offset > json->max_size && json->max_size)
1547 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",
1548 (unsigned long)SvCUR (string), (unsigned long)json->max_size); 1562 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1549 } 1563 }
1550 1564
1551 if (json->flags & F_UTF8) 1565 if (DECODE_WANTS_OCTETS (json))
1552 sv_utf8_downgrade (string, 0); 1566 sv_utf8_downgrade (string, 0);
1553 else 1567 else
1554 sv_utf8_upgrade (string); 1568 sv_utf8_upgrade (string);
1555 1569
1556 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 1570 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
1598 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);
1599 LEAVE; 1613 LEAVE;
1600 1614
1601 croak ("%s, at character offset %d (before \"%s\")", 1615 croak ("%s, at character offset %d (before \"%s\")",
1602 dec.err, 1616 dec.err,
1603 ptr_to_index (string, dec.cur), 1617 (int)ptr_to_index (string, dec.cur),
1604 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1618 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1605 } 1619 }
1606 1620
1607 sv = sv_2mortal (sv); 1621 sv = sv_2mortal (sv);
1608 1622
1798 json_boolean_stash = 0; 1812 json_boolean_stash = 0;
1799 1813
1800void new (char *klass) 1814void new (char *klass)
1801 PPCODE: 1815 PPCODE:
1802{ 1816{
1803 SV *pv = NEWSV (0, sizeof (JSON)); 1817 SV *pv = NEWSV (0, sizeof (JSON));
1804 SvPOK_only (pv); 1818 SvPOK_only (pv);
1805 json_init ((JSON *)SvPVX (pv)); 1819 json_init ((JSON *)SvPVX (pv));
1806 XPUSHs (sv_2mortal (sv_bless ( 1820 XPUSHs (sv_2mortal (sv_bless (
1807 newRV_noinc (pv), 1821 newRV_noinc (pv),
1808 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) 1822 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1885} 1899}
1886 1900
1887void 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)
1888 PPCODE: 1902 PPCODE:
1889{ 1903{
1890 if (!self->cb_sk_object) 1904 if (!self->cb_sk_object)
1891 self->cb_sk_object = newHV (); 1905 self->cb_sk_object = newHV ();
1892 1906
1893 if (SvOK (cb)) 1907 if (SvOK (cb))
1894 hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); 1908 hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0);
1895 else 1909 else
1906 XPUSHs (ST (0)); 1920 XPUSHs (ST (0));
1907} 1921}
1908 1922
1909void encode (JSON *self, SV *scalar) 1923void encode (JSON *self, SV *scalar)
1910 PPCODE: 1924 PPCODE:
1911 XPUSHs (encode_json (scalar, self)); 1925 PUTBACK; scalar = encode_json (scalar, self); SPAGAIN;
1926 XPUSHs (scalar);
1912 1927
1913void decode (JSON *self, SV *jsonstr) 1928void decode (JSON *self, SV *jsonstr)
1914 PPCODE: 1929 PPCODE:
1915 XPUSHs (decode_json (jsonstr, self, 0)); 1930 PUTBACK; jsonstr = decode_json (jsonstr, self, 0); SPAGAIN;
1931 XPUSHs (jsonstr);
1916 1932
1917void decode_prefix (JSON *self, SV *jsonstr) 1933void decode_prefix (JSON *self, SV *jsonstr)
1918 PPCODE: 1934 PPCODE:
1919{ 1935{
1936 SV *sv;
1920 char *offset; 1937 char *offset;
1938 PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN;
1921 EXTEND (SP, 2); 1939 EXTEND (SP, 2);
1922 PUSHs (decode_json (jsonstr, self, &offset)); 1940 PUSHs (sv);
1923 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); 1941 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset))));
1924} 1942}
1925 1943
1926void incr_parse (JSON *self, SV *jsonstr = 0) 1944void incr_parse (JSON *self, SV *jsonstr = 0)
1927 PPCODE: 1945 PPCODE:
1928{ 1946{
1929 if (!self->incr_text) 1947 if (!self->incr_text)
1930 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 }
1931 1968
1932 // append data, if any 1969 // append data, if any
1933 if (jsonstr) 1970 if (jsonstr)
1934 { 1971 {
1972 /* make sure both strings have same encoding */
1973 if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text))
1935 if (SvUTF8 (jsonstr)) 1974 if (SvUTF8 (jsonstr))
1975 sv_utf8_downgrade (jsonstr, 0);
1936 { 1976 else
1937 if (!SvUTF8 (self->incr_text))
1938 {
1939 /* utf-8-ness differs, need to upgrade */
1940 sv_utf8_upgrade (self->incr_text);
1941
1942 if (self->incr_pos)
1943 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1944 - (U8 *)SvPVX (self->incr_text);
1945 }
1946 }
1947 else if (SvUTF8 (self->incr_text))
1948 sv_utf8_upgrade (jsonstr); 1977 sv_utf8_upgrade (jsonstr);
1949 1978
1979 /* and then just blindly append */
1950 { 1980 {
1951 STRLEN len; 1981 STRLEN len;
1952 const char *str = SvPV (jsonstr, len); 1982 const char *str = SvPV (jsonstr, len);
1953 STRLEN cur = SvCUR (self->incr_text); 1983 STRLEN cur = SvCUR (self->incr_text);
1954 1984
1962 } 1992 }
1963 1993
1964 if (GIMME_V != G_VOID) 1994 if (GIMME_V != G_VOID)
1965 do 1995 do
1966 { 1996 {
1997 SV *sv;
1967 char *offset; 1998 char *offset;
1968 1999
1969 if (!INCR_DONE (self)) 2000 if (!INCR_DONE (self))
1970 { 2001 {
1971 incr_parse (self); 2002 incr_parse (self);
1973 if (self->incr_pos > self->max_size && self->max_size) 2004 if (self->incr_pos > self->max_size && self->max_size)
1974 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",
1975 (unsigned long)self->incr_pos, (unsigned long)self->max_size); 2006 (unsigned long)self->incr_pos, (unsigned long)self->max_size);
1976 2007
1977 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
1978 break; 2017 break;
2018 }
1979 } 2019 }
1980 2020
1981 XPUSHs (decode_json (self->incr_text, self, &offset)); 2021 PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN;
2022 XPUSHs (sv);
1982 2023
1983 self->incr_pos -= offset - SvPVX (self->incr_text); 2024 self->incr_pos -= offset - SvPVX (self->incr_text);
1984 self->incr_nest = 0; 2025 self->incr_nest = 0;
1985 self->incr_mode = 0; 2026 self->incr_mode = 0;
1986 2027
2038 PPCODE: 2079 PPCODE:
2039{ 2080{
2040 JSON json; 2081 JSON json;
2041 json_init (&json); 2082 json_init (&json);
2042 json.flags |= ix; 2083 json.flags |= ix;
2043 XPUSHs (encode_json (scalar, &json)); 2084 PUTBACK; scalar = encode_json (scalar, &json); SPAGAIN;
2085 XPUSHs (scalar);
2044} 2086}
2045 2087
2046void decode_json (SV *jsonstr) 2088void decode_json (SV *jsonstr)
2047 ALIAS: 2089 ALIAS:
2048 from_json_ = 0 2090 from_json_ = 0
2050 PPCODE: 2092 PPCODE:
2051{ 2093{
2052 JSON json; 2094 JSON json;
2053 json_init (&json); 2095 json_init (&json);
2054 json.flags |= ix; 2096 json.flags |= ix;
2055 XPUSHs (decode_json (jsonstr, &json, 0)); 2097 PUTBACK; jsonstr = decode_json (jsonstr, &json, 0); SPAGAIN;
2098 XPUSHs (jsonstr);
2056} 2099}
2057 2100

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines