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.118 by root, Fri Oct 25 20:27: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
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)
1452{ 1464{
1453 // the beauty of JSON: you need exactly one character lookahead 1465 // the beauty of JSON: you need exactly one character lookahead
1454 // to parse everything. 1466 // to parse everything.
1455 switch (*dec->cur) 1467 switch (*dec->cur)
1456 { 1468 {
1457 case '"': ++dec->cur; return decode_str (dec); 1469 case '"': ++dec->cur; return decode_str (dec);
1458 case '[': ++dec->cur; return decode_av (dec); 1470 case '[': ++dec->cur; return decode_av (dec);
1459 case '{': ++dec->cur; return decode_hv (dec); 1471 case '{': ++dec->cur; return decode_hv (dec);
1460 1472
1461 case '-': 1473 case '-':
1462 case '0': case '1': case '2': case '3': case '4': 1474 case '0': case '1': case '2': case '3': case '4':
1463 case '5': case '6': case '7': case '8': case '9': 1475 case '5': case '6': case '7': case '8': case '9':
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 * 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).
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
2030 SvREFCNT_dec (self->incr_text); 2071 SvREFCNT_dec (self->incr_text);
2031 2072
2032PROTOTYPES: ENABLE 2073PROTOTYPES: ENABLE
2033 2074
2034void encode_json (SV *scalar) 2075void encode_json (SV *scalar)
2035 ALIAS:
2036 to_json_ = 0
2037 encode_json = F_UTF8
2038 PPCODE: 2076 PPCODE:
2039{ 2077{
2040 JSON json; 2078 JSON json;
2041 json_init (&json); 2079 json_init (&json);
2042 json.flags |= ix; 2080 json.flags |= F_UTF8;
2043 XPUSHs (encode_json (scalar, &json)); 2081 PUTBACK; scalar = encode_json (scalar, &json); SPAGAIN;
2082 XPUSHs (scalar);
2044} 2083}
2045 2084
2046void decode_json (SV *jsonstr) 2085void decode_json (SV *jsonstr)
2047 ALIAS:
2048 from_json_ = 0
2049 decode_json = F_UTF8
2050 PPCODE: 2086 PPCODE:
2051{ 2087{
2052 JSON json; 2088 JSON json;
2053 json_init (&json); 2089 json_init (&json);
2054 json.flags |= ix; 2090 json.flags |= F_UTF8;
2055 XPUSHs (decode_json (jsonstr, &json, 0)); 2091 PUTBACK; jsonstr = decode_json (jsonstr, &json, 0); SPAGAIN;
2092 XPUSHs (jsonstr);
2056} 2093}
2057 2094

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines