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.122 by root, Tue Oct 29 15:55:49 2013 UTC vs.
Revision 1.131 by root, Thu Aug 17 01:42:20 2017 UTC

6#include <string.h> 6#include <string.h>
7#include <stdlib.h> 7#include <stdlib.h>
8#include <stdio.h> 8#include <stdio.h>
9#include <limits.h> 9#include <limits.h>
10#include <float.h> 10#include <float.h>
11#include <inttypes.h>
11 12
12#if defined(__BORLANDC__) || defined(_MSC_VER) 13#if defined(__BORLANDC__) || defined(_MSC_VER)
13# define snprintf _snprintf // C compilers have this in stdio.h 14# define snprintf _snprintf // C compilers have this in stdio.h
14#endif 15#endif
15 16
79#define ERR_NESTING_EXCEEDED "json text or perl structure exceeds maximum nesting level (max_depth set too low?)" 80#define ERR_NESTING_EXCEEDED "json text or perl structure exceeds maximum nesting level (max_depth set too low?)"
80 81
81#ifdef USE_ITHREADS 82#ifdef USE_ITHREADS
82# define JSON_SLOW 1 83# define JSON_SLOW 1
83# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) 84# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1))
85# define BOOL_STASH (bool_stash ? bool_stash : gv_stashpv ("Types::Serialiser::Boolean", 1))
84#else 86#else
85# define JSON_SLOW 0 87# define JSON_SLOW 0
86# define JSON_STASH json_stash 88# define JSON_STASH json_stash
89# define BOOL_STASH bool_stash
87#endif 90#endif
88 91
89// the amount of HEs to allocate on the stack, when sorting keys 92// the amount of HEs to allocate on the stack, when sorting keys
90#define STACK_HES 64 93#define STACK_HES 64
91 94
92static HV *json_stash, *types_boolean_stash; // JSON::XS:: 95static HV *json_stash, *bool_stash; // JSON::XS::, Types::Serialiser::Boolean::
93static SV *types_true, *types_false, *sv_json; 96static SV *bool_true, *bool_false, *sv_json;
94 97
95enum { 98enum {
96 INCR_M_WS = 0, // initial whitespace skipping, must be 0 99 INCR_M_WS = 0, // initial whitespace skipping, must be 0
97 INCR_M_STR, // inside string 100 INCR_M_STR, // inside string
98 INCR_M_BS, // inside backslash 101 INCR_M_BS, // inside backslash
150 SvPV_shrink_to_cur (sv); 153 SvPV_shrink_to_cur (sv);
151#elif defined (SvPV_renew) 154#elif defined (SvPV_renew)
152 SvPV_renew (sv, SvCUR (sv) + 1); 155 SvPV_renew (sv, SvCUR (sv) + 1);
153#endif 156#endif
154 } 157 }
158}
159
160/* adds two STRLENs together, slow, and with paranoia */
161STRLEN
162strlen_sum (STRLEN l1, STRLEN l2)
163{
164 size_t sum = l1 + l2;
165
166 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum)
167 croak ("JSON::XS: string size overflow");
168
169 return sum;
170}
171
172/* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */
173static char *
174json_sv_grow (SV *sv, size_t len1, size_t len2)
175{
176 len1 = strlen_sum (len1, len2);
177 len1 = strlen_sum (len1, len1 >> 1);
178
179 if (len1 > 4096 - 24)
180 len1 = (len1 | 4095) - 24;
181
182 return SvGROW (sv, len1);
155} 183}
156 184
157// decode an utf-8 character and return it, or (UV)-1 in 185// decode an utf-8 character and return it, or (UV)-1 in
158// case of an error. 186// case of an error.
159// we special-case "safe" characters from U+80 .. U+7FF, 187// we special-case "safe" characters from U+80 .. U+7FF,
299 // a recursion depth of ten gives us >>500 bits 327 // a recursion depth of ten gives us >>500 bits
300 json_atof_scan1 (s, &accum, &expo, 0, 10); 328 json_atof_scan1 (s, &accum, &expo, 0, 10);
301 329
302 return neg ? -accum : accum; 330 return neg ? -accum : accum;
303} 331}
332
333// target of scalar reference is bool? -1 == nope, 0 == false, 1 == true
334static int
335ref_bool_type (SV *sv)
336{
337 svtype svt = SvTYPE (sv);
338
339 if (svt < SVt_PVAV)
340 {
341 STRLEN len = 0;
342 char *pv = svt ? SvPV (sv, len) : 0;
343
344 if (len == 1)
345 if (*pv == '1')
346 return 1;
347 else if (*pv == '0')
348 return 0;
349 }
350
351 return -1;
352}
353
354// returns whether scalar is not a reference in the sense of allow_nonref
355static int
356json_nonref (SV *scalar)
357{
358 if (!SvROK (scalar))
359 return 1;
360
361 scalar = SvRV (scalar);
362
363 if (SvTYPE (scalar) >= SVt_PVMG)
364 {
365 if (SvSTASH (scalar) == bool_stash)
366 return 1;
367
368 if (!SvOBJECT (scalar) && ref_bool_type (scalar) >= 0)
369 return 1;
370 }
371
372 return 0;
373}
374
304///////////////////////////////////////////////////////////////////////////// 375/////////////////////////////////////////////////////////////////////////////
305// encoder 376// encoder
306 377
307// structure used for encoding JSON 378// structure used for encoding JSON
308typedef struct 379typedef struct
316} enc_t; 387} enc_t;
317 388
318INLINE void 389INLINE void
319need (enc_t *enc, STRLEN len) 390need (enc_t *enc, STRLEN len)
320{ 391{
321 if (expect_false (enc->cur + len >= enc->end)) 392 if (expect_false ((uintptr_t)(enc->end - enc->cur) < len))
322 { 393 {
323 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); 394 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv);
324 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); 395 char *buf = json_sv_grow (enc->sv, cur, len);
325 enc->cur = SvPVX (enc->sv) + cur; 396 enc->cur = buf + cur;
326 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 397 enc->end = buf + SvLEN (enc->sv) - 1;
327 } 398 }
328} 399}
329 400
330INLINE void 401INLINE void
331encode_ch (enc_t *enc, char ch) 402encode_ch (enc_t *enc, char ch)
347 418
348 if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case 419 if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case
349 { 420 {
350 if (expect_false (ch == '"')) // but with slow exceptions 421 if (expect_false (ch == '"')) // but with slow exceptions
351 { 422 {
352 need (enc, len += 1); 423 need (enc, len + 1);
353 *enc->cur++ = '\\'; 424 *enc->cur++ = '\\';
354 *enc->cur++ = '"'; 425 *enc->cur++ = '"';
355 } 426 }
356 else if (expect_false (ch == '\\')) 427 else if (expect_false (ch == '\\'))
357 { 428 {
358 need (enc, len += 1); 429 need (enc, len + 1);
359 *enc->cur++ = '\\'; 430 *enc->cur++ = '\\';
360 *enc->cur++ = '\\'; 431 *enc->cur++ = '\\';
361 } 432 }
362 else 433 else
363 *enc->cur++ = ch; 434 *enc->cur++ = ch;
366 } 437 }
367 else 438 else
368 { 439 {
369 switch (ch) 440 switch (ch)
370 { 441 {
371 case '\010': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'b'; ++str; break; 442 case '\010': need (enc, len + 1); *enc->cur++ = '\\'; *enc->cur++ = 'b'; ++str; break;
372 case '\011': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 't'; ++str; break; 443 case '\011': need (enc, len + 1); *enc->cur++ = '\\'; *enc->cur++ = 't'; ++str; break;
373 case '\012': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'n'; ++str; break; 444 case '\012': need (enc, len + 1); *enc->cur++ = '\\'; *enc->cur++ = 'n'; ++str; break;
374 case '\014': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'f'; ++str; break; 445 case '\014': need (enc, len + 1); *enc->cur++ = '\\'; *enc->cur++ = 'f'; ++str; break;
375 case '\015': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'r'; ++str; break; 446 case '\015': need (enc, len + 1); *enc->cur++ = '\\'; *enc->cur++ = 'r'; ++str; break;
376 447
377 default: 448 default:
378 { 449 {
379 STRLEN clen; 450 STRLEN clen;
380 UV uch; 451 UV uch;
396 if (uch >= 0x10000UL) 467 if (uch >= 0x10000UL)
397 { 468 {
398 if (uch >= 0x110000UL) 469 if (uch >= 0x110000UL)
399 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); 470 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
400 471
401 need (enc, len += 11); 472 need (enc, len + 11);
402 sprintf (enc->cur, "\\u%04x\\u%04x", 473 sprintf (enc->cur, "\\u%04x\\u%04x",
403 (int)((uch - 0x10000) / 0x400 + 0xD800), 474 (int)((uch - 0x10000) / 0x400 + 0xD800),
404 (int)((uch - 0x10000) % 0x400 + 0xDC00)); 475 (int)((uch - 0x10000) % 0x400 + 0xDC00));
405 enc->cur += 12; 476 enc->cur += 12;
406 } 477 }
407 else 478 else
408 { 479 {
409 need (enc, len += 5); 480 need (enc, len + 5);
410 *enc->cur++ = '\\'; 481 *enc->cur++ = '\\';
411 *enc->cur++ = 'u'; 482 *enc->cur++ = 'u';
412 *enc->cur++ = PL_hexdigit [ uch >> 12 ]; 483 *enc->cur++ = PL_hexdigit [ uch >> 12 ];
413 *enc->cur++ = PL_hexdigit [(uch >> 8) & 15]; 484 *enc->cur++ = PL_hexdigit [(uch >> 8) & 15];
414 *enc->cur++ = PL_hexdigit [(uch >> 4) & 15]; 485 *enc->cur++ = PL_hexdigit [(uch >> 4) & 15];
422 *enc->cur++ = uch; 493 *enc->cur++ = uch;
423 str += clen; 494 str += clen;
424 } 495 }
425 else if (is_utf8) 496 else if (is_utf8)
426 { 497 {
427 need (enc, len += clen); 498 need (enc, len + clen);
428 do 499 do
429 { 500 {
430 *enc->cur++ = *str++; 501 *enc->cur++ = *str++;
431 } 502 }
432 while (--clen); 503 while (--clen);
433 } 504 }
434 else 505 else
435 { 506 {
436 need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed 507 need (enc, len + UTF8_MAXBYTES - 1); // never more than 11 bytes needed
437 enc->cur = encode_utf8 (enc->cur, uch); 508 enc->cur = encode_utf8 (enc->cur, uch);
438 ++str; 509 ++str;
439 } 510 }
440 } 511 }
441 } 512 }
701 SvGETMAGIC (sv); 772 SvGETMAGIC (sv);
702 svt = SvTYPE (sv); 773 svt = SvTYPE (sv);
703 774
704 if (expect_false (SvOBJECT (sv))) 775 if (expect_false (SvOBJECT (sv)))
705 { 776 {
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); 777 HV *stash = SvSTASH (sv);
710 778
711 if (stash == boolean_stash) 779 if (stash == bool_stash)
712 { 780 {
713 if (SvIV (sv)) 781 if (SvIV (sv))
714 encode_str (enc, "true", 4, 0); 782 encode_str (enc, "true", 4, 0);
715 else 783 else
716 encode_str (enc, "false", 5, 0); 784 encode_str (enc, "false", 5, 0);
718 else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0))) 786 else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0)))
719 { 787 {
720 int count; 788 int count;
721 dSP; 789 dSP;
722 790
723 ENTER; SAVETMPS; PUSHMARK (SP); 791 ENTER; SAVETMPS;
792 SAVESTACK_POS ();
793 PUSHMARK (SP);
724 EXTEND (SP, 2); 794 EXTEND (SP, 2);
725 // we re-bless the reference to get overload and other niceties right 795 // we re-bless the reference to get overload and other niceties right
726 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 796 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
727 PUSHs (sv_json); 797 PUSHs (sv_json);
728 798
730 count = call_sv ((SV *)GvCV (method), G_ARRAY); 800 count = call_sv ((SV *)GvCV (method), G_ARRAY);
731 SPAGAIN; 801 SPAGAIN;
732 802
733 // catch this surprisingly common error 803 // catch this surprisingly common error
734 if (SvROK (TOPs) && SvRV (TOPs) == sv) 804 if (SvROK (TOPs) && SvRV (TOPs) == sv)
735 croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); 805 croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
736 806
737 encode_ch (enc, '('); 807 encode_ch (enc, '(');
738 encode_ch (enc, '"'); 808 encode_ch (enc, '"');
739 encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 809 encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash));
740 encode_ch (enc, '"'); 810 encode_ch (enc, '"');
749 encode_ch (enc, ','); 819 encode_ch (enc, ',');
750 } 820 }
751 821
752 encode_ch (enc, ']'); 822 encode_ch (enc, ']');
753 823
754 PUTBACK;
755
756 FREETMPS; LEAVE; 824 FREETMPS; LEAVE;
757 } 825 }
758 else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0))) 826 else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0)))
759 { 827 {
760 dSP; 828 dSP;
761 829
762 ENTER; SAVETMPS; PUSHMARK (SP); 830 ENTER; SAVETMPS;
831 PUSHMARK (SP);
763 // we re-bless the reference to get overload and other niceties right 832 // we re-bless the reference to get overload and other niceties right
764 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 833 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
765 834
766 // calling with G_SCALAR ensures that we always get a 1 return value 835 // calling with G_SCALAR ensures that we always get a 1 return value
767 PUTBACK; 836 PUTBACK;
789 encode_hv (enc, (HV *)sv); 858 encode_hv (enc, (HV *)sv);
790 else if (svt == SVt_PVAV) 859 else if (svt == SVt_PVAV)
791 encode_av (enc, (AV *)sv); 860 encode_av (enc, (AV *)sv);
792 else if (svt < SVt_PVAV) 861 else if (svt < SVt_PVAV)
793 { 862 {
794 STRLEN len = 0; 863 int bool_type = ref_bool_type (sv);
795 char *pv = svt ? SvPV (sv, len) : 0;
796 864
797 if (len == 1 && *pv == '1') 865 if (bool_type == 1)
798 encode_str (enc, "true", 4, 0); 866 encode_str (enc, "true", 4, 0);
799 else if (len == 1 && *pv == '0') 867 else if (bool_type == 0)
800 encode_str (enc, "false", 5, 0); 868 encode_str (enc, "false", 5, 0);
801 else if (enc->json.flags & F_ALLOW_UNKNOWN) 869 else if (enc->json.flags & F_ALLOW_UNKNOWN)
802 encode_str (enc, "null", 4, 0); 870 encode_str (enc, "null", 4, 0);
803 else 871 else
804 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", 872 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1",
887static SV * 955static SV *
888encode_json (SV *scalar, JSON *json) 956encode_json (SV *scalar, JSON *json)
889{ 957{
890 enc_t enc; 958 enc_t enc;
891 959
892 if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) 960 if (!(json->flags & F_ALLOW_NONREF) && json_nonref (scalar))
893 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); 961 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)");
894 962
895 enc.json = *json; 963 enc.json = *json;
896 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 964 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
897 enc.cur = SvPVX (enc.sv); 965 enc.cur = SvPVX (enc.sv);
1104 *cur++ = *dec_cur++; 1172 *cur++ = *dec_cur++;
1105 while (--clen); 1173 while (--clen);
1106 1174
1107 utf8 = 1; 1175 utf8 = 1;
1108 } 1176 }
1177 else if (ch == '\t' && dec->json.flags & F_RELAXED)
1178 *cur++ = ch;
1109 else 1179 else
1110 { 1180 {
1111 --dec_cur; 1181 --dec_cur;
1112 1182
1113 if (!ch) 1183 if (!ch)
1123 1193
1124 if (sv) 1194 if (sv)
1125 { 1195 {
1126 STRLEN cur = SvCUR (sv); 1196 STRLEN cur = SvCUR (sv);
1127 1197
1128 if (SvLEN (sv) <= cur + len) 1198 if (SvLEN (sv) - cur <= len)
1129 SvGROW (sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); 1199 json_sv_grow (sv, cur, len);
1130 1200
1131 memcpy (SvPVX (sv) + SvCUR (sv), buf, len); 1201 memcpy (SvPVX (sv) + SvCUR (sv), buf, len);
1132 SvCUR_set (sv, SvCUR (sv) + len); 1202 SvCUR_set (sv, SvCUR (sv) + len);
1133 } 1203 }
1134 else 1204 else
1435 1505
1436 hv_iterinit (hv); 1506 hv_iterinit (hv);
1437 he = hv_iternext (hv); 1507 he = hv_iternext (hv);
1438 hv_iterinit (hv); 1508 hv_iterinit (hv);
1439 1509
1440 // the next line creates a mortal sv each time its called. 1510 // the next line creates a mortal sv each time it's called.
1441 // might want to optimise this for common cases. 1511 // might want to optimise this for common cases.
1442 cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); 1512 cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0);
1443 1513
1444 if (cb) 1514 if (cb)
1445 { 1515 {
1446 dSP; 1516 dSP;
1447 int count; 1517 int count;
1448 1518
1449 ENTER; SAVETMPS; PUSHMARK (SP); 1519 ENTER; SAVETMPS;
1520 SAVESTACK_POS ();
1521 PUSHMARK (SP);
1450 XPUSHs (HeVAL (he)); 1522 XPUSHs (HeVAL (he));
1451 sv_2mortal (sv); 1523 sv_2mortal (sv);
1452 1524
1453 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; 1525 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1454 1526
1467 if (dec->json.cb_object) 1539 if (dec->json.cb_object)
1468 { 1540 {
1469 dSP; 1541 dSP;
1470 int count; 1542 int count;
1471 1543
1472 ENTER; SAVETMPS; PUSHMARK (SP); 1544 ENTER; SAVETMPS;
1545 SAVESTACK_POS ();
1546 PUSHMARK (SP);
1473 XPUSHs (sv_2mortal (sv)); 1547 XPUSHs (sv_2mortal (sv));
1474 1548
1475 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; 1549 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN;
1476 1550
1477 if (count == 1) 1551 if (count == 1)
1544 if (!method) 1618 if (!method)
1545 ERR ("cannot decode perl-object (package does not have a THAW method)"); 1619 ERR ("cannot decode perl-object (package does not have a THAW method)");
1546 1620
1547 dSP; 1621 dSP;
1548 1622
1549 ENTER; SAVETMPS; PUSHMARK (SP); 1623 ENTER; SAVETMPS;
1624 PUSHMARK (SP);
1550 EXTEND (SP, len + 2); 1625 EXTEND (SP, len + 2);
1551 // we re-bless the reference to get overload and other niceties right 1626 // we re-bless the reference to get overload and other niceties right
1552 PUSHs (tag); 1627 PUSHs (tag);
1553 PUSHs (sv_json); 1628 PUSHs (sv_json);
1554 1629
1596 case 't': 1671 case 't':
1597 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1672 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1598 { 1673 {
1599 dec->cur += 4; 1674 dec->cur += 4;
1600#if JSON_SLOW 1675#if JSON_SLOW
1601 types_true = get_bool ("Types::Serialiser::true"); 1676 bool_true = get_bool ("Types::Serialiser::true");
1602#endif 1677#endif
1603 return newSVsv (types_true); 1678 return newSVsv (bool_true);
1604 } 1679 }
1605 else 1680 else
1606 ERR ("'true' expected"); 1681 ERR ("'true' expected");
1607 1682
1608 break; 1683 break;
1610 case 'f': 1685 case 'f':
1611 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1686 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1612 { 1687 {
1613 dec->cur += 5; 1688 dec->cur += 5;
1614#if JSON_SLOW 1689#if JSON_SLOW
1615 types_false = get_bool ("Types::Serialiser::false"); 1690 bool_false = get_bool ("Types::Serialiser::false");
1616#endif 1691#endif
1617 return newSVsv (types_false); 1692 return newSVsv (bool_false);
1618 } 1693 }
1619 else 1694 else
1620 ERR ("'false' expected"); 1695 ERR ("'false' expected");
1621 1696
1622 break; 1697 break;
1640fail: 1715fail:
1641 return 0; 1716 return 0;
1642} 1717}
1643 1718
1644static SV * 1719static SV *
1645decode_json (SV *string, JSON *json, char **offset_return) 1720decode_json (SV *string, JSON *json, STRLEN *offset_return)
1646{ 1721{
1647 dec_t dec; 1722 dec_t dec;
1648 SV *sv; 1723 SV *sv;
1649 1724
1650 /* work around bugs in 5.10 where manipulating magic values 1725 /* work around bugs in 5.10 where manipulating magic values
1651 * makes perl ignore the magic in subsequent accesses. 1726 * makes perl ignore the magic in subsequent accesses.
1652 * also make a copy of non-PV values, to get them into a clean 1727 * also make a copy of non-PV values, to get them into a clean
1653 * state (SvPV should do that, but it's buggy, see below). 1728 * state (SvPV should do that, but it's buggy, see below).
1729 *
1730 * SvIsCOW_shared_hash works around a bug in perl (possibly 5.16),
1731 * as reported by Reini Urban.
1654 */ 1732 */
1655 /*SvGETMAGIC (string);*/ 1733 /*SvGETMAGIC (string);*/
1656 if (SvMAGICAL (string) || !SvPOK (string)) 1734 if (SvMAGICAL (string) || !SvPOK (string) || SvIsCOW_shared_hash (string))
1657 string = sv_2mortal (newSVsv (string)); 1735 string = sv_2mortal (newSVsv (string));
1658 1736
1659 SvUPGRADE (string, SVt_PV); 1737 SvUPGRADE (string, SVt_PV);
1660 1738
1661 /* work around a bug in perl 5.10, which causes SvCUR to fail an 1739 /* work around a bug in perl 5.10, which causes SvCUR to fail an
1700 1778
1701 decode_ws (&dec); 1779 decode_ws (&dec);
1702 sv = decode_sv (&dec); 1780 sv = decode_sv (&dec);
1703 1781
1704 if (offset_return) 1782 if (offset_return)
1705 *offset_return = dec.cur; 1783 *offset_return = dec.cur - SvPVX (string);
1706 1784 else if (sv)
1707 if (!(offset_return || !sv))
1708 { 1785 {
1709 // check for trailing garbage 1786 // check for trailing garbage
1710 decode_ws (&dec); 1787 decode_ws (&dec);
1711 1788
1712 if (*dec.cur) 1789 if (*dec.cur)
1736 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1813 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1737 } 1814 }
1738 1815
1739 sv = sv_2mortal (sv); 1816 sv = sv_2mortal (sv);
1740 1817
1741 if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) 1818 if (!(dec.json.flags & F_ALLOW_NONREF) && json_nonref (sv))
1742 croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); 1819 croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)");
1743 1820
1744 return sv; 1821 return sv;
1745} 1822}
1746 1823
1916 i >= '0' && i <= '9' ? i - '0' 1993 i >= '0' && i <= '9' ? i - '0'
1917 : i >= 'a' && i <= 'f' ? i - 'a' + 10 1994 : i >= 'a' && i <= 'f' ? i - 'a' + 10
1918 : i >= 'A' && i <= 'F' ? i - 'A' + 10 1995 : i >= 'A' && i <= 'F' ? i - 'A' + 10
1919 : -1; 1996 : -1;
1920 1997
1921 json_stash = gv_stashpv ("JSON::XS" , 1); 1998 json_stash = gv_stashpv ("JSON::XS" , 1);
1922 types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); 1999 bool_stash = gv_stashpv ("Types::Serialiser::Boolean", 1);
1923
1924 types_true = get_bool ("Types::Serialiser::true"); 2000 bool_true = get_bool ("Types::Serialiser::true");
1925 types_false = get_bool ("Types::Serialiser::false"); 2001 bool_false = get_bool ("Types::Serialiser::false");
1926 2002
1927 sv_json = newSVpv ("JSON", 0); 2003 sv_json = newSVpv ("JSON", 0);
1928 SvREADONLY_on (sv_json); 2004 SvREADONLY_on (sv_json);
1929 2005
1930 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ 2006 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */
1932 2008
1933PROTOTYPES: DISABLE 2009PROTOTYPES: DISABLE
1934 2010
1935void CLONE (...) 2011void CLONE (...)
1936 CODE: 2012 CODE:
1937 json_stash = 0; 2013 json_stash = 0;
1938 types_boolean_stash = 0; 2014 bool_stash = 0;
1939 2015
1940void new (char *klass) 2016void new (char *klass)
1941 PPCODE: 2017 PPCODE:
1942{ 2018{
1943 SV *pv = NEWSV (0, sizeof (JSON)); 2019 SV *pv = NEWSV (0, sizeof (JSON));
2060 2136
2061void decode_prefix (JSON *self, SV *jsonstr) 2137void decode_prefix (JSON *self, SV *jsonstr)
2062 PPCODE: 2138 PPCODE:
2063{ 2139{
2064 SV *sv; 2140 SV *sv;
2065 char *offset; 2141 STRLEN offset;
2066 PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN; 2142 PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN;
2067 EXTEND (SP, 2); 2143 EXTEND (SP, 2);
2068 PUSHs (sv); 2144 PUSHs (sv);
2069 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); 2145 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, SvPV_nolen (jsonstr) + offset))));
2070} 2146}
2071 2147
2072void incr_parse (JSON *self, SV *jsonstr = 0) 2148void incr_parse (JSON *self, SV *jsonstr = 0)
2073 PPCODE: 2149 PPCODE:
2074{ 2150{
2108 { 2184 {
2109 STRLEN len; 2185 STRLEN len;
2110 const char *str = SvPV (jsonstr, len); 2186 const char *str = SvPV (jsonstr, len);
2111 STRLEN cur = SvCUR (self->incr_text); 2187 STRLEN cur = SvCUR (self->incr_text);
2112 2188
2113 if (SvLEN (self->incr_text) <= cur + len) 2189 if (SvLEN (self->incr_text) - cur <= len)
2114 SvGROW (self->incr_text, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); 2190 json_sv_grow (self->incr_text, cur, len);
2115 2191
2116 Move (str, SvEND (self->incr_text), len, char); 2192 Move (str, SvEND (self->incr_text), len, char);
2117 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); 2193 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len);
2118 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there 2194 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there
2119 } 2195 }
2121 2197
2122 if (GIMME_V != G_VOID) 2198 if (GIMME_V != G_VOID)
2123 do 2199 do
2124 { 2200 {
2125 SV *sv; 2201 SV *sv;
2126 char *offset; 2202 STRLEN offset;
2127 2203
2128 if (!INCR_DONE (self)) 2204 if (!INCR_DONE (self))
2129 { 2205 {
2130 incr_parse (self); 2206 incr_parse (self);
2131 2207
2147 } 2223 }
2148 2224
2149 PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN; 2225 PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN;
2150 XPUSHs (sv); 2226 XPUSHs (sv);
2151 2227
2152 self->incr_pos -= offset - SvPVX (self->incr_text); 2228 self->incr_pos -= offset;
2153 self->incr_nest = 0; 2229 self->incr_nest = 0;
2154 self->incr_mode = 0; 2230 self->incr_mode = 0;
2155 2231
2156 sv_chop (self->incr_text, offset); 2232 sv_chop (self->incr_text, SvPVX (self->incr_text) + offset);
2157 } 2233 }
2158 while (GIMME_V == G_ARRAY); 2234 while (GIMME_V == G_ARRAY);
2159} 2235}
2160 2236
2161SV *incr_text (JSON *self) 2237SV *incr_text (JSON *self)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines