SIMD.xs

filter: Statementstmt 92.7% 1183 / 1276 Branchbran 78.0% 841 / 1078 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 85.9% 2024 / 2354 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« simdjson.h ↑ Summary
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include "ppport.h"
5  
6 #include "simdjson_wrapper.h"
7  
8 #include <assert.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <limits.h>
13 #include <float.h>
14 #include <inttypes.h>
15  
16 #if defined(__BORLANDC__) || defined(_MSC_VER)
17 # define snprintf _snprintf // C compilers have this in stdio.h
18 #endif
19  
20 // some old perls do not have this, try to make it work, no
21 // guarantees, though. if it breaks, you get to keep the pieces.
22 #ifndef UTF8_MAXBYTES
23 # define UTF8_MAXBYTES 13
24 #endif
25  
26 // compatibility with perl <5.18
27 #ifndef HvNAMELEN_get
28 # define HvNAMELEN_get(hv) strlen (HvNAME (hv))
29 #endif
30 #ifndef HvNAMELEN
31 # define HvNAMELEN(hv) HvNAMELEN_get (hv)
32 #endif
33 #ifndef HvNAMEUTF8
34 # define HvNAMEUTF8(hv) 0
35 #endif
36  
37 // three extra for rounding, sign, and end of string
38 #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 3)
39  
40 #define F_ASCII 0x00000001UL
41 #define F_LATIN1 0x00000002UL
42 #define F_UTF8 0x00000004UL
43 #define F_INDENT 0x00000008UL
44 #define F_CANONICAL 0x00000010UL
45 #define F_SPACE_BEFORE 0x00000020UL
46 #define F_SPACE_AFTER 0x00000040UL
47 #define F_ALLOW_NONREF 0x00000100UL
48 #define F_SHRINK 0x00000200UL
49 #define F_ALLOW_BLESSED 0x00000400UL
50 #define F_CONV_BLESSED 0x00000800UL
51 #define F_RELAXED 0x00001000UL
52 #define F_ALLOW_UNKNOWN 0x00002000UL
53 #define F_ALLOW_TAGS 0x00004000UL
54 #define F_HOOK 0x00008000UL // some hooks exist, so slow-path processing
55 #define F_USE_SIMDJSON 0x00010000UL
56 #define F_CORE_BOOLS 0x00020000UL
57 #define F_ENCODE_CORE_BOOLS 0x00040000UL
58 #define F_NONREF_SET 0x00080000UL /* allow_nonref was called explicitly */
59  
60 #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER
61  
62 #define INIT_SIZE 64 // initial scalar size to be allocated
63 #define INDENT_STEP 3 // spaces per indentation level
64  
65 #define SHORT_STRING_LEN 16384 // special-case strings of up to this size
66  
67 #define DECODE_WANTS_OCTETS(json) ((json)->flags & F_UTF8)
68  
69 #define SB do {
70 #define SE } while (0)
71  
72 #if __GNUC__ >= 3
73 # define my_expect(expr,value) __builtin_expect ((expr), (value))
74 # define INLINE static inline
75 #else
76 # define my_expect(expr,value) (expr)
77 # define INLINE static
78 #endif
79  
80 #define expect_false(expr) my_expect ((expr) != 0, 0)
81 #define expect_true(expr) my_expect ((expr) != 0, 1)
82  
83 #define IN_RANGE_INC(type,val,beg,end) \
84 ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \
85 <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg)))
86  
87 #define ERR_NESTING_EXCEEDED "json text or perl structure exceeds maximum nesting level (max_depth set too low?)"
88  
89 #ifdef USE_ITHREADS
90 # define JSON_STASH (expect_true (json_stash) ? json_stash : gv_stashpv ("JSON::SIMD", 1))
91 # define BOOL_STASH (expect_true (bool_stash) ? bool_stash : gv_stashpv ("Types::Serialiser::Boolean", 1))
92 # define GET_BOOL(value) (expect_true (bool_ ## value) ? bool_ ## value : get_bool ("Types::Serialiser::" # value))
93 #else
94 # define JSON_STASH json_stash
95 # define BOOL_STASH bool_stash
96 # define GET_BOOL(value) bool_ ## value
97 #endif
98  
99 // the amount of HEs to allocate on the stack, when sorting keys
100 #define STACK_HES 64
101  
102 static HV *json_stash, *bool_stash; // JSON::SIMD::, Types::Serialiser::Boolean::
103 static SV *bool_false, *bool_true;
104 static SV *sv_json;
105  
106 enum {
107 INCR_M_WS = 0, // initial whitespace skipping, must be 0
108 INCR_M_TFN, // inside true/false/null
109 INCR_M_NUM, // inside number
110 INCR_M_STR, // inside string
111 INCR_M_BS, // inside backslash
112 INCR_M_C0, // inside comment in initial whitespace sequence
113 INCR_M_C1, // inside comment in other places
114 INCR_M_JSON // outside anything, count nesting
115 };
116  
117 #define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON)
118 /* the first text once complete, else everything buffered */
119 #define INCR_SIZE(json) (INCR_DONE (json) ? (json)->incr_pos : SvCUR ((json)->incr_text))
120  
121 // main JSON struct definition moved to simdjson_wrapper.h
122 // typedef struct { ... } JSON;
123  
124 INLINE void
125 98955 json_init (JSON *json)
126 {
127 static const JSON init = { F_ALLOW_NONREF|F_USE_SIMDJSON, 512 };
128  
129 98955 *json = init;
130 98955 }
131  
132 /////////////////////////////////////////////////////////////////////////////
133 // utility functions
134  
135 INLINE SV *
136 90 get_bool (const char *name)
137 {
138 90 SV *sv = get_sv (name, 1);
139  
140 90 SvREADONLY_on (sv);
141 90 SvREADONLY_on (SvRV (sv));
142  
143 90 return sv;
144 }
145  
146 INLINE void
147 18432 shrink (SV *sv)
148 {
149 18432 sv_utf8_downgrade (sv, 1);
150  
151 18432 ▶ if (SvLEN (sv) > SvCUR (sv) + 1)
Branch: 1 branch
BranchTrueFalseTotal
if (SvLEN (sv) > SvCUR (sv) + 1) 18422 10 18432
152 {
153 #ifdef SvPV_shrink_to_cur
154 18422 ▶ SvPV_shrink_to_cur (sv);
Branch: 3 branches
BranchTrueFalseTotal
SvPV_shrink_to_cur (sv); 18386 36 18422
SvPV_shrink_to_cur (sv); 0 18386 18386
SvPV_shrink_to_cur (sv); 18296 126 18422
155 #elif defined (SvPV_renew)
156 SvPV_renew (sv, SvCUR (sv) + 1);
157 #endif
158 }
159 18432 }
160  
161 /* adds two STRLENs together, slow, and with paranoia */
162 static STRLEN
163 360154 strlen_sum (STRLEN l1, STRLEN l2)
164 {
165 360154 size_t sum = l1 + l2;
166  
167 360154 ▶ if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum)
Branch: 1 branch
BranchTrueFalseTotal
if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) 0 360154 360154
168 0 croak ("JSON::SIMD: string size overflow");
169  
170 360154 return sum;
171 }
172  
173 /* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */
174 static char *
175 180077 json_sv_grow (SV *sv, size_t len1, size_t len2)
176 {
177 180077 len1 = strlen_sum (len1, len2);
178 180077 len1 = strlen_sum (len1, len1 >> 1);
179  
180 180077 ▶ if (len1 > 4096 - 24)
Branch: 1 branch
BranchTrueFalseTotal
if (len1 > 4096 - 24) 12921 167156 180077
181 12921 len1 = (len1 | 4095) - 24;
182  
183 180077 ▶ return SvGROW (sv, len1);
Branch: 2 branches
BranchTrueFalseTotal
return SvGROW (sv, len1); 180077 0 180077
return SvGROW (sv, len1); 129998 50079 180077
184 }
185  
186 // decode a utf-8 character and return it, or (UV)-1 in
187 // case of an error.
188 // we special-case "safe" characters from U+80 .. U+7FF,
189 // but use the very good perl function to parse anything else.
190 // note that we never call this function for a ascii codepoints
191 INLINE UV
192 12022477 decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen)
193 {
194 12022477 ▶ if (expect_true (len >= 2
Branch: 4 branches
BranchTrueFalseTotal
if (expect_true (len >= 2 12022461 16 12022477
if (expect_true (len >= 2 1344326 10678135 12022461
if (expect_true (len >= 2 1344326 0 1344326
if (expect_true (len >= 2 1344326 10678151 12022477
195 && IN_RANGE_INC (char, s[0], 0xc2, 0xdf)
196 && IN_RANGE_INC (char, s[1], 0x80, 0xbf)))
197 {
198 1344326 *clen = 2;
199 1344326 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
200 }
201 else
202 #if PERL_VERSION_GE(5,36,0)
203 10678151 return utf8n_to_uvchr (s, len, clen, UTF8_CHECK_ONLY);
204 #else
205 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY);
206 #endif
207 }
208  
209 // likewise for encoding, also never called for ascii codepoints
210 // this function takes advantage of this fact, although current gccs
211 // seem to optimise the check for >= 0x80 away anyways
212 INLINE unsigned char *
213 4745719 encode_utf8 (unsigned char *s, UV ch)
214 {
215 4745719 ▶ if (expect_false (ch < 0x000080))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false (ch < 0x000080)) 0 4745719 4745719
216 0 *s++ = ch;
217 4745719 ▶ else if (expect_true (ch < 0x000800))
Branch: 1 branch
BranchTrueFalseTotal
else if (expect_true (ch < 0x000800)) 3061552 1684167 4745719
218 3061552 *s++ = 0xc0 | ( ch >> 6),
219 3061552 *s++ = 0x80 | ( ch & 0x3f);
220 1684167 ▶ else if ( ch < 0x010000)
Branch: 1 branch
BranchTrueFalseTotal
else if ( ch < 0x010000) 838366 845801 1684167
221 838366 *s++ = 0xe0 | ( ch >> 12),
222 838366 *s++ = 0x80 | ((ch >> 6) & 0x3f),
223 838366 *s++ = 0x80 | ( ch & 0x3f);
224 845801 ▶ else if ( ch < 0x110000)
Branch: 1 branch
BranchTrueFalseTotal
else if ( ch < 0x110000) 845801 0 845801
225 845801 *s++ = 0xf0 | ( ch >> 18),
226 845801 *s++ = 0x80 | ((ch >> 12) & 0x3f),
227 845801 *s++ = 0x80 | ((ch >> 6) & 0x3f),
228 845801 *s++ = 0x80 | ( ch & 0x3f);
229  
230 4745719 return s;
231 }
232  
233 // convert offset pointer to character index, sv must be string
234 static STRLEN
235 1232 ptr_to_index (SV *sv, const U8 *offset)
236 {
237 1232 return SvUTF8 (sv)
238 1160 ? utf8_distance (offset, (U8*)SvPVX (sv))
239 2392 ▶ : offset - (U8*)SvPVX (sv);
Branch: 1 branch
BranchTrueFalseTotal
: offset - (U8*)SvPVX (sv); 1160 72 1232
240 }
241  
242 /////////////////////////////////////////////////////////////////////////////
243 // fp hell
244  
245 // scan a group of digits, and a trailing exponent
246 static void
247 10112 json_atof_scan1 (const char *s, NV *accum, int *expo, int postdp, int maxdepth)
248 {
249 10112 UV uaccum = 0;
250 10112 int eaccum = 0;
251  
252 // if we recurse too deep, skip all remaining digits
253 // to avoid a stack overflow attack
254 10112 ▶ if (expect_false (--maxdepth <= 0))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false (--maxdepth <= 0)) 9112 1000 10112
255 841000 ▶ while (*s >= '0' && *s <= '9')
Branch: 2 branches
BranchTrueFalseTotal
while (*s >= '0' && *s <= '9') 1000 840000 841000
while (*s >= '0' && *s <= '9') 0 840000 840000
256 840000 ++s;
257  
258 for (;;)
259 153213 {
260 163325 U8 dig = *s - '0';
261  
262 163325 ▶ if (expect_false (dig >= 10))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false (dig >= 10)) 2112 161213 163325
263 {
264 2112 ▶ if (dig == (U8)('.' - '0'))
Branch: 1 branch
BranchTrueFalseTotal
if (dig == (U8)('.' - '0')) 2045 67 2112
265 {
266 2045 ▶ if (postdp)
Branch: 1 branch
BranchTrueFalseTotal
if (postdp) 1000 1045 2045
267 1000 break;
268 1045 ++s;
269 1045 json_atof_scan1 (s, accum, expo, 1, maxdepth);
270 }
271 67 ▶ else if ((dig | ' ') == 'e' - '0')
Branch: 1 branch
BranchTrueFalseTotal
else if ((dig | ' ') == 'e' - '0') 53 14 67
272 {
273 53 int exp2 = 0;
274 53 int neg = 0;
275  
276 53 ++s;
277  
278 53 ▶ if (*s == '-')
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '-') 6 47 53
279 {
280 6 ++s;
281 6 neg = 1;
282 }
283 47 ▶ else if (*s == '+')
Branch: 1 branch
BranchTrueFalseTotal
else if (*s == '+') 29 18 47
284 29 ++s;
285  
286 138 ▶ while (*s >= '0' && *s <= '9')
Branch: 2 branches
BranchTrueFalseTotal
while (*s >= '0' && *s <= '9') 88 50 138
while (*s >= '0' && *s <= '9') 85 3 88
287 85 exp2 = exp2 * 10 + (*s++ - '0');
288  
289 53 ▶ *expo += neg ? -exp2 : exp2;
Branch: 1 branch
BranchTrueFalseTotal
*expo += neg ? -exp2 : exp2; 6 47 53
290 }
291  
292 1112 break;
293 }
294  
295 161213 ++s;
296  
297 161213 uaccum = uaccum * 10 + dig;
298 161213 ++eaccum;
299  
300 // if we have too many digits, then recurse for more
301 // we actually do this for rather few digits
302 161213 ▶ if (uaccum >= (UV_MAX - 9) / 10)
Branch: 1 branch
BranchTrueFalseTotal
if (uaccum >= (UV_MAX - 9) / 10) 8000 153213 161213
303 {
304 8000 ▶ if (postdp) *expo -= eaccum;
Branch: 1 branch
BranchTrueFalseTotal
if (postdp) *expo -= eaccum; 8000 0 8000
305 8000 json_atof_scan1 (s, accum, expo, postdp, maxdepth);
306 8000 ▶ if (postdp) *expo += eaccum;
Branch: 1 branch
BranchTrueFalseTotal
if (postdp) *expo += eaccum; 8000 0 8000
307  
308 8000 break;
309 }
310 }
311  
312 // this relies greatly on the quality of the pow ()
313 // implementation of the platform, but a good
314 // implementation is hard to beat.
315 // (IEEE 754 conformant ones are required to be exact)
316 10112 ▶ if (postdp) *expo -= eaccum;
Branch: 1 branch
BranchTrueFalseTotal
if (postdp) *expo -= eaccum; 9045 1067 10112
317 10112 *accum += uaccum * Perl_pow (10., *expo);
318 10112 *expo += eaccum;
319 10112 }
320  
321 // not static because we call it from simdjson too
322 NV
323 1067 json_atof (const char *s)
324 {
325 1067 NV accum = 0.;
326 1067 int expo = 0;
327 1067 int neg = 0;
328  
329 1067 ▶ if (*s == '-')
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '-') 16 1051 1067
330 {
331 16 ++s;
332 16 neg = 1;
333 }
334  
335 // a recursion depth of ten gives us >>500 bits
336 1067 json_atof_scan1 (s, &accum, &expo, 0, 10);
337  
338 1067 ▶ return neg ? -accum : accum;
Branch: 1 branch
BranchTrueFalseTotal
return neg ? -accum : accum; 16 1051 1067
339 }
340  
341 // target of scalar reference is bool? -1 == nope, 0 == false, 1 == true
342 static int
343 31522 ref_bool_type (SV *sv)
344 {
345 31522 svtype svt = SvTYPE (sv);
346  
347 31522 ▶ if (svt < SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
if (svt < SVt_PVAV) 10 31512 31522
348 {
349 10 STRLEN len = 0;
350 19 ▶ char *pv = svt ? SvPV (sv, len) : 0;
Branch: 1 branch
BranchTrueFalseTotal
char *pv = svt ? SvPV (sv, len) : 0; 9 1 10
351  
352 10 ▶ if (len == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (len == 1) 3 7 10
353 {
354 3 ▶ if (*pv == '1')
Branch: 1 branch
BranchTrueFalseTotal
if (*pv == '1') 1 2 3
355 2 return 1;
356 2 ▶ else if (*pv == '0')
Branch: 1 branch
BranchTrueFalseTotal
else if (*pv == '0') 1 1 2
357 1 return 0;
358 }
359 }
360  
361 31520 return -1;
362 }
363  
364 // returns whether scalar is not a reference in the sense of allow_nonref
365 static int
366 31514 json_nonref (SV *scalar)
367 {
368 31514 ▶ if (!SvROK (scalar))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK (scalar)) 2 31512 31514
369 2 return 1;
370  
371 31512 scalar = SvRV (scalar);
372  
373 31512 ▶ if (SvTYPE (scalar) >= SVt_PVMG)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE (scalar) >= SVt_PVMG) 31512 0 31512
374 {
375 31512 ▶ if (SvSTASH (scalar) == bool_stash)
Branch: 1 branch
BranchTrueFalseTotal
if (SvSTASH (scalar) == bool_stash) 0 31512 31512
376 0 return 1;
377  
378 31512 ▶ if (!SvOBJECT (scalar) && ref_bool_type (scalar) >= 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOBJECT (scalar) && ref_bool_type (scalar) >= 0) 31512 0 31512
if (!SvOBJECT (scalar) && ref_bool_type (scalar) >= 0) 0 31512 31512
379 0 return 1;
380 }
381  
382 31512 return 0;
383 }
384  
385 /////////////////////////////////////////////////////////////////////////////
386 // encoder
387  
388 // structure used for encoding JSON
389 typedef struct
390 {
391 char *cur; // SvPVX (sv) + current output position
392 char *end; // SvEND (sv)
393 SV *sv; // result scalar
394 JSON json;
395 U32 indent; // indentation level
396 UV limit; // escape character values >= this value when encoding
397 } enc_t;
398  
399 INLINE void
400 15490653 need (enc_t *enc, STRLEN len)
401 {
402 15490653 ▶ if (expect_false ((uintptr_t)(enc->end - enc->cur) < len))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false ((uintptr_t)(enc->end - enc->cur) < len)) 179776 15310877 15490653
403 {
404 179776 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv);
405 179776 char *buf = json_sv_grow (enc->sv, cur, len);
406 179776 enc->cur = buf + cur;
407 179776 enc->end = buf + SvLEN (enc->sv) - 1;
408 }
409 15490653 }
410  
411 INLINE void
412 208384 encode_ch (enc_t *enc, char ch)
413 {
414 208384 need (enc, 1);
415 208384 *enc->cur++ = ch;
416 208384 }
417  
418 static void
419 52009 encode_str (enc_t *enc, char *str, STRLEN len, int is_utf8)
420 {
421 52009 char *end = str + len;
422  
423 52009 need (enc, len);
424  
425 19014291 ▶ while (str < end)
Branch: 1 branch
BranchTrueFalseTotal
while (str < end) 18962282 52009 19014291
426 {
427 18962282 unsigned char ch = *(unsigned char *)str;
428  
429 18962282 ▶ if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case
Branch: 3 branches
BranchTrueFalseTotal
if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case 17575217 1387065 18962282
if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case 3814602 13760615 17575217
if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case 3814602 15147680 18962282
430 {
431 3814602 ▶ if (expect_false (ch == '"')) // but with slow exceptions
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false (ch == '"')) // but with slow exceptions 42236 3772366 3814602
432 {
433 42236 need (enc, len + 1);
434 42236 *enc->cur++ = '\\';
435 42236 *enc->cur++ = '"';
436 }
437 3772366 ▶ else if (expect_false (ch == '\\'))
Branch: 1 branch
BranchTrueFalseTotal
else if (expect_false (ch == '\\')) 38792 3733574 3772366
438 {
439 38792 need (enc, len + 1);
440 38792 *enc->cur++ = '\\';
441 38792 *enc->cur++ = '\\';
442 }
443 else
444 3733574 *enc->cur++ = ch;
445  
446 3814602 ++str;
447 }
448 else
449 {
450 15147680 switch (ch)
451 {
452 42430 case '\010': need (enc, len + 1); *enc->cur++ = '\\'; *enc->cur++ = 'b'; ++str; break;
453 51868 case '\011': need (enc, len + 1); *enc->cur++ = '\\'; *enc->cur++ = 't'; ++str; break;
454 44555 case '\012': need (enc, len + 1); *enc->cur++ = '\\'; *enc->cur++ = 'n'; ++str; break;
455 43694 case '\014': need (enc, len + 1); *enc->cur++ = '\\'; *enc->cur++ = 'f'; ++str; break;
456 43198 case '\015': need (enc, len + 1); *enc->cur++ = '\\'; *enc->cur++ = 'r'; ++str; break;
457  
458 14921935 default:
459 {
460 STRLEN clen;
461 UV uch;
462  
463 14921935 ▶ if (is_utf8)
Branch: 1 branch
BranchTrueFalseTotal
if (is_utf8) 9442198 5479737 14921935
464 {
465 9442198 uch = decode_utf8 ((unsigned char *)str, end - str, &clen);
466 9442198 ▶ if (clen == (STRLEN)-1)
Branch: 1 branch
BranchTrueFalseTotal
if (clen == (STRLEN)-1) 0 9442198 9442198
467 0 croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str);
468 }
469 else
470 {
471 5479737 uch = ch;
472 5479737 clen = 1;
473 }
474  
475 14921935 ▶ if (uch < 0x80/*0x20*/ || uch >= enc->limit)
Branch: 2 branches
BranchTrueFalseTotal
if (uch < 0x80/*0x20*/ || uch >= enc->limit) 13760615 1161320 14921935
if (uch < 0x80/*0x20*/ || uch >= enc->limit) 6880046 6880569 13760615
476 {
477 8041366 ▶ if (uch >= 0x10000UL)
Branch: 1 branch
BranchTrueFalseTotal
if (uch >= 0x10000UL) 2255467 5785899 8041366
478 {
479 2255467 ▶ if (uch >= 0x110000UL)
Branch: 1 branch
BranchTrueFalseTotal
if (uch >= 0x110000UL) 0 2255467 2255467
480 0 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
481  
482 2255467 need (enc, len + 11);
483 2255467 sprintf (enc->cur, "\\u%04x\\u%04x",
484 2255467 (int)((uch - 0x10000) / 0x400 + 0xD800),
485 2255467 (int)((uch - 0x10000) % 0x400 + 0xDC00));
486 2255467 enc->cur += 12;
487 }
488 else
489 {
490 5785899 need (enc, len + 5);
491 5785899 *enc->cur++ = '\\';
492 5785899 *enc->cur++ = 'u';
493 5785899 *enc->cur++ = PL_hexdigit [ uch >> 12 ];
494 5785899 *enc->cur++ = PL_hexdigit [(uch >> 8) & 15];
495 5785899 *enc->cur++ = PL_hexdigit [(uch >> 4) & 15];
496 5785899 *enc->cur++ = PL_hexdigit [(uch >> 0) & 15];
497 }
498  
499 8041366 str += clen;
500 }
501 6880569 ▶ else if (enc->json.flags & F_LATIN1)
Branch: 1 branch
BranchTrueFalseTotal
else if (enc->json.flags & F_LATIN1) 4 6880565 6880569
502 {
503 4 *enc->cur++ = uch;
504 4 str += clen;
505 }
506 6880565 ▶ else if (is_utf8)
Branch: 1 branch
BranchTrueFalseTotal
else if (is_utf8) 4715076 2165489 6880565
507 {
508 4715076 need (enc, len + clen);
509 do
510 {
511 16176680 *enc->cur++ = *str++;
512 }
513 16176680 ▶ while (--clen);
Branch: 1 branch
BranchTrueFalseTotal
while (--clen); 11461604 4715076 16176680
514 }
515 else
516 {
517 2165489 need (enc, len + UTF8_MAXBYTES - 1); // never more than 11 bytes needed
518 2165489 enc->cur = (char *)encode_utf8 ((U8*)enc->cur, uch);
519 2165489 ++str;
520 }
521 }
522 }
523 }
524  
525 18962282 --len;
526 }
527 52009 }
528  
529 INLINE void
530 103262 encode_indent (enc_t *enc)
531 {
532 103262 ▶ if (enc->json.flags & F_INDENT)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->json.flags & F_INDENT) 23 103239 103262
533 {
534 23 int spaces = enc->indent * INDENT_STEP;
535  
536 23 need (enc, spaces);
537 23 memset (enc->cur, ' ', spaces);
538 23 enc->cur += spaces;
539 }
540 103262 }
541  
542 INLINE void
543 10 encode_space (enc_t *enc)
544 {
545 10 need (enc, 1);
546 10 encode_ch (enc, ' ');
547 10 }
548  
549 INLINE void
550 151234 encode_nl (enc_t *enc)
551 {
552 151234 ▶ if (enc->json.flags & F_INDENT)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->json.flags & F_INDENT) 30 151204 151234
553 {
554 30 need (enc, 1);
555 30 encode_ch (enc, '\n');
556 }
557 151234 }
558  
559 INLINE void
560 2162 encode_comma (enc_t *enc)
561 {
562 2162 encode_ch (enc, ',');
563  
564 2162 ▶ if (enc->json.flags & F_INDENT)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->json.flags & F_INDENT) 5 2157 2162
565 5 encode_nl (enc);
566 2157 ▶ else if (enc->json.flags & F_SPACE_AFTER)
Branch: 1 branch
BranchTrueFalseTotal
else if (enc->json.flags & F_SPACE_AFTER) 0 2157 2157
567 0 encode_space (enc);
568 2162 }
569  
570 static void encode_sv (enc_t *enc, SV *sv);
571  
572 static void
573 49935 encode_av (enc_t *enc, AV *av)
574 {
575 49935 int i, len = av_len (av);
576  
577 49935 ▶ if (enc->indent >= enc->json.max_depth)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->indent >= enc->json.max_depth) 2 49933 49935
578 2 croak (ERR_NESTING_EXCEEDED);
579  
580 49933 encode_ch (enc, '[');
581  
582 49933 ▶ if (len >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (len >= 0) 49915 18 49933
583 {
584 49915 encode_nl (enc); ++enc->indent;
585  
586 101379 ▶ for (i = 0; i <= len; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= len; ++i) 51476 49903 101379
587 {
588 51476 SV **svp = av_fetch (av, i, 0);
589  
590 51476 encode_indent (enc);
591  
592 51476 ▶ if (svp)
Branch: 1 branch
BranchTrueFalseTotal
if (svp) 51474 2 51476
593 51474 encode_sv (enc, *svp);
594 else
595 2 encode_str (enc, "null", 4, 0);
596  
597 51464 ▶ if (i < len)
Branch: 1 branch
BranchTrueFalseTotal
if (i < len) 1561 49903 51464
598 1561 encode_comma (enc);
599 }
600  
601 49903 encode_nl (enc); --enc->indent; encode_indent (enc);
602 }
603  
604 49921 encode_ch (enc, ']');
605 49921 }
606  
607 static void
608 1244 encode_hk (enc_t *enc, HE *he)
609 {
610 1244 encode_ch (enc, '"');
611  
612 1244 ▶ if (HeKLEN (he) == HEf_SVKEY)
Branch: 1 branch
BranchTrueFalseTotal
if (HeKLEN (he) == HEf_SVKEY) 1 1243 1244
613 {
614 1 ▶ SV *sv = HeSVKEY (he);
Branch: 2 branches
BranchTrueFalseTotal
SV *sv = HeSVKEY (he); 1 0 1
SV *sv = HeSVKEY (he); 1 0 1
615 STRLEN len;
616 char *str;
617  
618 1 ▶ SvGETMAGIC (sv);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC (sv); 0 1 1
SvGETMAGIC (sv); 0 0 0
619 1 str = SvPV (sv, len);
620  
621 1 encode_str (enc, str, len, SvUTF8 (sv));
622 }
623 else
624 1243 encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he));
625  
626 1244 encode_ch (enc, '"');
627  
628 1244 ▶ if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
Branch: 1 branch
BranchTrueFalseTotal
if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); 5 1239 1244
629 1244 encode_ch (enc, ':');
630 1244 ▶ if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
Branch: 1 branch
BranchTrueFalseTotal
if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); 5 1239 1244
631 1244 }
632  
633 // compare hash entries, used when all keys are bytestrings
634 static int
635 424 he_cmp_fast (const void *a_, const void *b_)
636 {
637 int cmp;
638  
639 424 HE *a = *(HE **)a_;
640 424 HE *b = *(HE **)b_;
641  
642 424 STRLEN la = HeKLEN (a);
643 424 STRLEN lb = HeKLEN (b);
644  
645 424 ▶ if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la))) 396 28 424
646 396 cmp = lb - la;
647  
648 424 return cmp;
649 }
650  
651 // compare hash entries, used when some keys are sv's or utf-x
652 static int
653 746 he_cmp_slow (const void *a, const void *b)
654 {
655 746 ▶ return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a));
Branch: 4 branches
BranchTrueFalseTotal
return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); 746 0 746
return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); 0 746 746
return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); 746 0 746
return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); 0 746 746
656 }
657  
658 static void
659 663 encode_hv (enc_t *enc, HV *hv)
660 {
661 HE *he;
662  
663 663 ▶ if (enc->indent >= enc->json.max_depth)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->indent >= enc->json.max_depth) 2 661 663
664 2 croak (ERR_NESTING_EXCEEDED);
665  
666 661 encode_ch (enc, '{');
667  
668 // for canonical output we have to sort by keys first
669 // actually, this is mostly due to the stupid so-called
670 // security workaround added somewhere in 5.8.x
671 // that randomises hash orderings
672 661 ▶ if (enc->json.flags & F_CANONICAL && !SvRMAGICAL (hv))
Branch: 2 branches
BranchTrueFalseTotal
if (enc->json.flags & F_CANONICAL && !SvRMAGICAL (hv)) 435 226 661
if (enc->json.flags & F_CANONICAL && !SvRMAGICAL (hv)) 435 0 435
673 435 {
674 435 int count = hv_iterinit (hv);
675  
676 435 ▶ if (SvMAGICAL (hv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvMAGICAL (hv)) 0 435 435
677 {
678 // need to count by iterating. could improve by dynamically building the vector below
679 // but I don't care for the speed of this special case.
680 // note also that we will run into undefined behaviour when the two iterations
681 // do not result in the same count, something I might care for in some later release.
682  
683 0 count = 0;
684 0 ▶ while (hv_iternext (hv))
Branch: 1 branch
BranchTrueFalseTotal
while (hv_iternext (hv)) 0 0 0
685 0 ++count;
686  
687 0 hv_iterinit (hv);
688 }
689  
690 435 ▶ if (count)
Branch: 1 branch
BranchTrueFalseTotal
if (count) 423 12 435
691 {
692 423 int i, fast = 1;
693 HE *hes_stack [STACK_HES];
694 423 HE **hes = hes_stack;
695  
696 // allocate larger arrays on the heap
697 423 ▶ if (count > STACK_HES)
Branch: 1 branch
BranchTrueFalseTotal
if (count > STACK_HES) 0 423 423
698 {
699 0 SV *sv = sv_2mortal (NEWSV (0, count * sizeof (*hes)));
700 0 hes = (HE **)SvPVX (sv);
701 }
702  
703 423 i = 0;
704 1444 ▶ while ((he = hv_iternext (hv)))
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext (hv))) 1021 423 1444
705 {
706 1021 hes [i++] = he;
707 1021 ▶ if (HeKLEN (he) < 0 || HeKUTF8 (he))
Branch: 2 branches
BranchTrueFalseTotal
if (HeKLEN (he) < 0 || HeKUTF8 (he)) 1021 0 1021
if (HeKLEN (he) < 0 || HeKUTF8 (he)) 6 1015 1021
708 6 fast = 0;
709 }
710  
711 assert (i == count);
712  
713 423 ▶ if (fast)
Branch: 1 branch
BranchTrueFalseTotal
if (fast) 417 6 423
714 417 qsort (hes, count, sizeof (HE *), he_cmp_fast);
715 else
716 {
717 // hack to forcefully disable "use bytes"
718 6 COP cop = *PL_curcop;
719 6 cop.op_private = 0;
720  
721 6 ENTER;
722 6 SAVETMPS;
723  
724 6 SAVEVPTR (PL_curcop);
725 6 PL_curcop = &cop;
726  
727 6 qsort (hes, count, sizeof (HE *), he_cmp_slow);
728  
729 6 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 6 0 6
730 6 LEAVE;
731 }
732  
733 423 encode_nl (enc); ++enc->indent;
734  
735 1444 ▶ while (count--)
Branch: 1 branch
BranchTrueFalseTotal
while (count--) 1021 423 1444
736 {
737 1021 encode_indent (enc);
738 1021 he = hes [count];
739 1021 encode_hk (enc, he);
740 1021 ▶ encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
Branch: 1 branch
BranchTrueFalseTotal
encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); 0 1021 1021
741  
742 1021 ▶ if (count)
Branch: 1 branch
BranchTrueFalseTotal
if (count) 598 423 1021
743 598 encode_comma (enc);
744 }
745  
746 423 encode_nl (enc); --enc->indent; encode_indent (enc);
747 }
748 }
749 else
750 {
751 226 ▶ if (hv_iterinit (hv) || SvMAGICAL (hv))
Branch: 2 branches
BranchTrueFalseTotal
if (hv_iterinit (hv) || SvMAGICAL (hv)) 7 219 226
if (hv_iterinit (hv) || SvMAGICAL (hv)) 1 6 7
752 220 ▶ if ((he = hv_iternext (hv)))
Branch: 1 branch
BranchTrueFalseTotal
if ((he = hv_iternext (hv))) 220 0 220
753 {
754 220 encode_nl (enc); ++enc->indent;
755  
756 for (;;)
757 {
758 223 encode_indent (enc);
759 223 encode_hk (enc, he);
760 223 ▶ encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
Branch: 1 branch
BranchTrueFalseTotal
encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); 1 222 223
761  
762 219 ▶ if (!(he = hv_iternext (hv)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(he = hv_iternext (hv))) 216 3 219
763 216 break;
764  
765 3 encode_comma (enc);
766 }
767  
768 216 encode_nl (enc); --enc->indent; encode_indent (enc);
769 }
770 }
771  
772 657 encode_ch (enc, '}');
773 657 }
774  
775 // encode objects, arrays and special \0=false and \1=true values.
776 static void
777 50652 encode_rv (enc_t *enc, SV *sv)
778 {
779 svtype svt;
780 GV *method;
781  
782 50652 ▶ SvGETMAGIC (sv);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC (sv); 0 50652 50652
SvGETMAGIC (sv); 0 0 0
783 50652 svt = SvTYPE (sv);
784  
785 50652 ▶ if (expect_false (SvOBJECT (sv)))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false (SvOBJECT (sv))) 42 50610 50652
786 {
787 42 HV *stash = SvSTASH (sv);
788  
789 42 ▶ if (stash == bool_stash)
Branch: 1 branch
BranchTrueFalseTotal
if (stash == bool_stash) 28 14 42
790 {
791 28 ▶ if (SvIV (sv)) encode_str (enc, "true" , 4, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (SvIV (sv)) encode_str (enc, "true" , 4, 0); 14 14 28
792 14 else encode_str (enc, "false", 5, 0);
793 }
794 14 ▶ else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0)))
Branch: 2 branches
BranchTrueFalseTotal
else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0))) 2 12 14
else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0))) 1 1 2
795 1 {
796 int count;
797 1 dSP;
798  
799 1 ENTER; SAVETMPS;
800 1 ▶ PUSHMARK (SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK (SP); 0 1 1
801 1 ▶ EXTEND (SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND (SP, 2); 0 1 1
802 // we re-bless the reference to get overload and other niceties right
803 1 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
804 1 PUSHs (sv_json);
805  
806 1 PUTBACK;
807 1 count = call_sv ((SV *)GvCV (method), G_LIST);
808 1 SPAGAIN;
809  
810 // catch this surprisingly common error
811 1 ▶ if (SvROK (TOPs) && SvRV (TOPs) == sv)
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK (TOPs) && SvRV (TOPs) == sv) 0 1 1
if (SvROK (TOPs) && SvRV (TOPs) == sv) 0 0 0
812 0 ▶ croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
Branch: 6 branches
BranchTrueFalseTotal
croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); 0 0 0
croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); 0 0 0
croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); 0 0 0
croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); 0 0 0
croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); 0 0 0
croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); 0 0 0
813  
814 1 encode_ch (enc, '(');
815 1 encode_ch (enc, '"');
816 1 ▶ encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash));
Branch: 19 branches
BranchTrueFalseTotal
encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 1 0 1
encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 1 0 1
encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 0 1 1
encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 0 0 0
encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 1 0 1
encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 0 1 1
encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 0 1 1
encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 1 0 1
encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 1 0 1
encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 0 1 1
encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 0 0 0
encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 1 0 1
encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 0 1 1
encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 1 0 1
encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 1 0 1
encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 0 1 1
encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 0 0 0
encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 1 0 1
encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 0 1 1
817 1 encode_ch (enc, '"');
818 1 encode_ch (enc, ')');
819 1 encode_ch (enc, '[');
820  
821 1 ▶ if (count)
Branch: 1 branch
BranchTrueFalseTotal
if (count) 1 0 1
822 {
823 int i;
824  
825 3 ▶ for (i = 0; i < count - 1; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count - 1; ++i) 2 1 3
826 {
827 2 encode_sv (enc, SP[i + 1 - count]);
828 2 encode_ch (enc, ',');
829 }
830  
831 1 encode_sv (enc, TOPs);
832 1 SP -= count;
833 }
834  
835 1 PUTBACK;
836  
837 1 encode_ch (enc, ']');
838  
839 1 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 1 0 1
840 }
841 13 ▶ else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0)))
Branch: 2 branches
BranchTrueFalseTotal
else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0))) 5 8 13
else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0))) 3 2 5
842 3 {
843 3 dSP;
844  
845 3 ENTER; SAVETMPS;
846 3 ▶ PUSHMARK (SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK (SP); 0 3 3
847 // we re-bless the reference to get overload and other niceties right
848 3 ▶ XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 0 3 3
849  
850 // calling with G_SCALAR ensures that we always get a 1 return value
851 3 PUTBACK;
852 3 call_sv ((SV *)GvCV (method), G_SCALAR);
853 3 SPAGAIN;
854  
855 // catch this surprisingly common error
856 3 ▶ if (SvROK (TOPs) && SvRV (TOPs) == sv)
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK (TOPs) && SvRV (TOPs) == sv) 2 1 3
if (SvROK (TOPs) && SvRV (TOPs) == sv) 0 2 2
857 0 ▶ croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
Branch: 6 branches
BranchTrueFalseTotal
croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); 0 0 0
croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); 0 0 0
croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); 0 0 0
croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); 0 0 0
croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); 0 0 0
croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); 0 0 0
858  
859 3 sv = POPs;
860 3 PUTBACK;
861  
862 3 encode_sv (enc, sv);
863  
864 3 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 3 0 3
865 }
866 10 ▶ else if (enc->json.flags & F_ALLOW_BLESSED)
Branch: 1 branch
BranchTrueFalseTotal
else if (enc->json.flags & F_ALLOW_BLESSED) 6 4 10
867 6 encode_str (enc, "null", 4, 0);
868 else
869 8 croak ("encountered object '%s', but neither allow_blessed, convert_blessed nor allow_tags settings are enabled (or TO_JSON/FREEZE method missing)",
870 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
871 }
872 50610 ▶ else if (svt == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
else if (svt == SVt_PVHV) 663 49947 50610
873 663 encode_hv (enc, (HV *)sv);
874 49947 ▶ else if (svt == SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
else if (svt == SVt_PVAV) 49935 12 49947
875 49935 encode_av (enc, (AV *)sv);
876 12 ▶ else if (svt < SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
else if (svt < SVt_PVAV) 10 2 12
877 {
878 10 int bool_type = ref_bool_type (sv);
879  
880 10 ▶ if (bool_type == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (bool_type == 1) 1 9 10
881 1 encode_str (enc, "true", 4, 0);
882 9 ▶ else if (bool_type == 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (bool_type == 0) 1 8 9
883 1 encode_str (enc, "false", 5, 0);
884 8 ▶ else if (enc->json.flags & F_ALLOW_UNKNOWN)
Branch: 1 branch
BranchTrueFalseTotal
else if (enc->json.flags & F_ALLOW_UNKNOWN) 0 8 8
885 0 encode_str (enc, "null", 4, 0);
886 else
887 16 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1",
888 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
889 }
890 2 ▶ else if (enc->json.flags & F_ALLOW_UNKNOWN)
Branch: 1 branch
BranchTrueFalseTotal
else if (enc->json.flags & F_ALLOW_UNKNOWN) 0 2 2
891 0 encode_str (enc, "null", 4, 0);
892 else
893 4 croak ("encountered %s, but JSON can only represent references to arrays or hashes",
894 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
895 50618 }
896  
897 static void
898 102871 encode_sv (enc_t *enc, SV *sv)
899 {
900 102871 ▶ SvGETMAGIC (sv);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC (sv); 3 102868 102871
SvGETMAGIC (sv); 0 3 3
901  
902 #if PERL_VERSION_GE(5,36,0)
903 102871 ▶ if (enc->json.flags & F_ENCODE_CORE_BOOLS && SvIsBOOL (sv))
Branch: 2 branches
BranchTrueFalseTotal
if (enc->json.flags & F_ENCODE_CORE_BOOLS && SvIsBOOL (sv)) 12 102859 102871
if (enc->json.flags & F_ENCODE_CORE_BOOLS && SvIsBOOL (sv)) 8 4 12
904 {
905 16 ▶ if (SvTRUE_nomg_NN (sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE_nomg_NN (sv)) 4 4 8
906 4 encode_str (enc, "true", 4, 0);
907 else
908 4 encode_str (enc, "false", 5, 0);
909 }
910 else /* continues after the endif! */
911 #endif
912 102863 ▶ if (SvPOKp (sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOKp (sv)) 50635 52228 102863
913 {
914 STRLEN len;
915 50635 char *str = SvPV (sv, len);
916 50635 encode_ch (enc, '"');
917 50635 encode_str (enc, str, len, SvUTF8 (sv));
918 50635 encode_ch (enc, '"');
919 }
920 52228 ▶ else if (SvNOKp (sv))
Branch: 1 branch
BranchTrueFalseTotal
else if (SvNOKp (sv)) 147 52081 52228
921 {
922 // trust that perl will do the right thing w.r.t. JSON syntax.
923 147 need (enc, NV_DIG + 32);
924 #ifdef USE_QUADMATH
925 quadmath_snprintf(enc->cur, enc->end - enc->cur, "%.*Qg", (int)NV_DIG, SvNVX(sv));
926 #else
927 147 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur);
928 #endif
929 147 enc->cur += strlen (enc->cur);
930 }
931 52081 ▶ else if (SvIOKp (sv))
Branch: 1 branch
BranchTrueFalseTotal
else if (SvIOKp (sv)) 1346 50735 52081
932 {
933 // we assume we can always read an IV as a UV and vice versa
934 // we assume two's complement
935 // we assume no aliasing issues in the union
936 2692 ▶ if (SvIsUV (sv) ? SvUVX (sv) <= 59000
Branch: 2 branches
BranchTrueFalseTotal
if (SvIsUV (sv) ? SvUVX (sv) <= 59000 0 1346 1346
if (SvIsUV (sv) ? SvUVX (sv) <= 59000 1340 6 1346
937 1346 ▶ : SvIVX (sv) <= 59000 && SvIVX (sv) >= -59000)
Branch: 2 branches
BranchTrueFalseTotal
: SvIVX (sv) <= 59000 && SvIVX (sv) >= -59000) 1340 6 1346
: SvIVX (sv) <= 59000 && SvIVX (sv) >= -59000) 1340 0 1340
938 {
939 // optimise the "small number case"
940 // code will likely be branchless and use only a single multiplication
941 // works for numbers up to 59074
942 1340 I32 i = SvIVX (sv);
943 U32 u;
944 1340 char digit, nz = 0;
945  
946 1340 need (enc, 6);
947  
948 1340 *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0;
949 1340 u = i < 0 ? -i : i;
950  
951 // convert to 4.28 fixed-point representation
952 1340 u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits
953  
954 // now output digit by digit, each time masking out the integer part
955 // and multiplying by 5 while moving the decimal point one to the right,
956 // resulting in a net multiplication by 10.
957 // we always write the digit to memory but conditionally increment
958 // the pointer, to enable the use of conditional move instructions.
959 1340 ▶ digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffffUL) * 5;
Branch: 2 branches
BranchTrueFalseTotal
digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffffUL) * 5; 1340 0 1340
digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffffUL) * 5; 6 1334 1340
960 1340 ▶ digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffffUL) * 5;
Branch: 2 branches
BranchTrueFalseTotal
digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffffUL) * 5; 1334 6 1340
digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffffUL) * 5; 6 1328 1334
961 1340 ▶ digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffffUL) * 5;
Branch: 2 branches
BranchTrueFalseTotal
digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffffUL) * 5; 1328 12 1340
digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffffUL) * 5; 0 1328 1328
962 1340 ▶ digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffffUL) * 5;
Branch: 2 branches
BranchTrueFalseTotal
digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffffUL) * 5; 1328 12 1340
digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffffUL) * 5; 20 1308 1328
963 1340 digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0'
964 }
965 else
966 {
967 // large integer, use the (rather slow) snprintf way.
968 6 need (enc, IVUV_MAXCHARS);
969 6 enc->cur +=
970 6 SvIsUV(sv)
971 0 ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv))
972 6 ▶ : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv));
Branch: 1 branch
BranchTrueFalseTotal
: snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); 0 6 6
973 }
974 }
975 50735 ▶ else if (SvROK (sv))
Branch: 1 branch
BranchTrueFalseTotal
else if (SvROK (sv)) 50652 83 50735
976 50652 encode_rv (enc, SvRV (sv));
977 83 ▶ else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN)
Branch: 2 branches
BranchTrueFalseTotal
else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) 0 83 83
else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) 0 0 0
978 83 encode_str (enc, "null", 4, 0);
979 else
980 0 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, check your input data",
981 SvPV_nolen (sv), (unsigned int)SvFLAGS (sv));
982 102837 }
983  
984 static SV *
985 50147 encode_json (SV *scalar, JSON *json)
986 {
987 enc_t enc;
988  
989 50147 ▶ if (!(json->flags & F_ALLOW_NONREF) && json_nonref (scalar))
Branch: 2 branches
BranchTrueFalseTotal
if (!(json->flags & F_ALLOW_NONREF) && json_nonref (scalar)) 31116 19031 50147
if (!(json->flags & F_ALLOW_NONREF) && json_nonref (scalar)) 0 31116 31116
990 0 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)");
991  
992 50147 enc.json = *json;
993 50147 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
994 50147 enc.cur = SvPVX (enc.sv);
995 50147 enc.end = SvEND (enc.sv);
996 50147 enc.indent = 0;
997 100294 enc.limit = enc.json.flags & F_ASCII ? 0x000080UL
998 75714 ▶ : enc.json.flags & F_LATIN1 ? 0x000100UL
Branch: 1 branch
BranchTrueFalseTotal
: enc.json.flags & F_LATIN1 ? 0x000100UL 25567 24580 50147
999 25567 ▶ : 0x110000UL;
Branch: 1 branch
BranchTrueFalseTotal
: 0x110000UL; 4 25563 25567
1000  
1001 50147 SvPOK_only (enc.sv);
1002 50147 encode_sv (&enc, scalar);
1003 50129 encode_nl (&enc);
1004  
1005 50129 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
1006 50129 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
1007  
1008 50129 ▶ if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) 12913 37216 50129
1009 12913 SvUTF8_on (enc.sv);
1010  
1011 50129 ▶ if (enc.json.flags & F_SHRINK)
Branch: 1 branch
BranchTrueFalseTotal
if (enc.json.flags & F_SHRINK) 18432 31697 50129
1012 18432 shrink (enc.sv);
1013  
1014 50129 return enc.sv;
1015 }
1016  
1017 /////////////////////////////////////////////////////////////////////////////
1018 // decoder
1019  
1020 // structure used for decoding JSON
1021 // -- moved to simdjson_wrapper.h
1022 // typedef struct { ... } dec_t;
1023  
1024 static SV *
1025 102 emulate_at_pointer (SV *sv, SV *path)
1026 {
1027 static const char err_pointer[] =
1028 "INVALID_JSON_POINTER: Invalid JSON pointer syntax";
1029 static const char err_type[] =
1030 "INCORRECT_TYPE: The JSON element does not have the requested type";
1031 static const char err_bounds[] =
1032 "INDEX_OUT_OF_BOUNDS: Attempted to access an element of a JSON array "
1033 "that is beyond its length";
1034 static const char err_field[] =
1035 "NO_SUCH_FIELD: The JSON field referenced does not exist in this object";
1036  
1037 SV *current;
1038 SV *path_copy;
1039 SV *key_sv;
1040 const char *text;
1041 STRLEN text_len;
1042 STRLEN pos;
1043  
1044 /*
1045 * Keep our own scalar holding the current value. For a reference this
1046 * increments ownership of its referent; it does not deep-copy the tree.
1047 *
1048 * All allocations are mortal, so croak() paths require no explicit
1049 * cleanup. The caller must provide the normal Perl temporary scope.
1050 */
1051 102 current = sv_2mortal (newSVsv (sv));
1052  
1053 /* Preserve the internal convention that NULL means no pointer. */
1054 102 ▶ if (!path)
Branch: 1 branch
BranchTrueFalseTotal
if (!path) 0 102 102
1055 0 return current;
1056  
1057 /*
1058 * Fetch/copy the argument before stringification and UTF-8 conversion.
1059 * Never upgrade or write into the caller's path scalar.
1060 *
1061 * SvPVutf8 supplies the byte length, including any embedded NULs.
1062 * Do not modify path_copy after obtaining text.
1063 */
1064 102 path_copy = sv_2mortal (newSVsv (path));
1065 102 text = SvPVutf8 (path_copy, text_len);
1066  
1067 /* The empty pointer selects the complete document. */
1068 102 ▶ if (text_len == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (text_len == 0) 4 98 102
1069 4 return current;
1070  
1071 98 ▶ if (text[0] != '/')
Branch: 1 branch
BranchTrueFalseTotal
if (text[0] != '/') 2 96 98
1072 2 croak ("%s", err_pointer);
1073  
1074 /*
1075 * Preserve the existing scalar-document diagnostic, including for
1076 * booleans represented as references to scalars.
1077 */
1078 96 ▶ if (!SvROK (current))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK (current)) 5 91 96
1079 5 croak ("only the empty path is allowed for scalar documents");
1080  
1081 {
1082 91 svtype type = SvTYPE (SvRV (current));
1083  
1084 91 ▶ if (type != SVt_PVAV && type != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (type != SVt_PVAV && type != SVt_PVHV) 53 38 91
if (type != SVt_PVAV && type != SVt_PVHV) 2 51 53
1085 2 croak ("only the empty path is allowed for scalar documents");
1086 }
1087  
1088 /*
1089 * Reuse a Perl-managed token buffer. sv_setpvn() supplies writable
1090 * storage and room for its terminating NUL.
1091 */
1092 89 key_sv = sv_2mortal (newSVpvn ("", 0));
1093 89 pos = 1; /* Skip the initial slash. */
1094  
1095 for (;;)
1096 27 {
1097 SV *container;
1098 SV *next;
1099 svtype type;
1100 STRLEN start;
1101 STRLEN raw_len;
1102 STRLEN read_pos;
1103 STRLEN write_pos;
1104 STRLEN key_len;
1105 char *key;
1106  
1107 /*
1108 * This check is required on EVERY iteration, not just at the root.
1109 * A selected scalar is valid only when no components remain.
1110 */
1111 116 ▶ if (!SvROK (current))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK (current)) 6 110 116
1112 6 croak ("%s", err_type);
1113  
1114 110 container = SvRV (current);
1115 110 type = SvTYPE (container);
1116  
1117 110 ▶ if (type != SVt_PVAV && type != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (type != SVt_PVAV && type != SVt_PVHV) 61 49 110
if (type != SVt_PVAV && type != SVt_PVHV) 4 57 61
1118 4 croak ("%s", err_type);
1119  
1120 /*
1121 * Split on literal slashes before unescaping. An escaped slash
1122 * (~1) belongs to the token and must not become a separator.
1123 *
1124 * pos may equal text_len: this represents a trailing empty token.
1125 */
1126 106 start = pos;
1127  
1128 5709 ▶ while (pos < text_len && text[pos] != '/')
Branch: 2 branches
BranchTrueFalseTotal
while (pos < text_len && text[pos] != '/') 5632 77 5709
while (pos < text_len && text[pos] != '/') 5603 29 5632
1129 5603 ++pos;
1130  
1131 106 raw_len = pos - start;
1132 106 sv_setpvn (key_sv, text + start, raw_len);
1133 106 key = SvPVX (key_sv);
1134  
1135 /*
1136 * Decode in place. Decoding never increases the token length,
1137 * so the write position cannot overtake the read position.
1138 *
1139 * An embedded NUL is copied like any other ordinary character.
1140 */
1141 106 read_pos = 0;
1142 106 write_pos = 0;
1143  
1144 5690 ▶ while (read_pos < raw_len)
Branch: 1 branch
BranchTrueFalseTotal
while (read_pos < raw_len) 5590 100 5690
1145 {
1146 5590 char ch = key[read_pos++];
1147  
1148 5590 ▶ if (ch == '~')
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '~') 11 5579 5590
1149 {
1150 11 ▶ if (read_pos == raw_len)
Branch: 1 branch
BranchTrueFalseTotal
if (read_pos == raw_len) 2 9 11
1151 2 croak ("%s", err_pointer);
1152  
1153 9 ch = key[read_pos++];
1154  
1155 9 ▶ if (ch == '0')
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '0') 3 6 9
1156 3 ch = '~';
1157 6 ▶ else if (ch == '1')
Branch: 1 branch
BranchTrueFalseTotal
else if (ch == '1') 2 4 6
1158 2 ch = '/';
1159 else
1160 4 croak ("%s", err_pointer);
1161 }
1162  
1163 5584 key[write_pos++] = ch;
1164 }
1165  
1166 100 key_len = write_pos;
1167 100 key[key_len] = '\0';
1168 100 SvCUR_set (key_sv, key_len);
1169  
1170 /*
1171 * text was obtained with SvPVutf8(); decoding ASCII escapes
1172 * preserves that encoding. This also handles ASCII-only keys.
1173 */
1174 100 SvUTF8_on (key_sv);
1175  
1176 100 ▶ if (type == SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
if (type == SVt_PVAV) 48 52 100
1177 {
1178 48 AV *av = (AV *)container;
1179 SV **element;
1180 SSize_t last;
1181 STRLEN i;
1182 48 UV index = 0;
1183  
1184 /* RFC 6901 array indices: 0 or [1-9][0-9]*. */
1185 48 ▶ if (key_len == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (key_len == 0) 2 46 48
1186 2 croak ("%s", err_pointer);
1187  
1188 46 ▶ if (key_len > 1 && key[0] == '0')
Branch: 2 branches
BranchTrueFalseTotal
if (key_len > 1 && key[0] == '0') 22 24 46
if (key_len > 1 && key[0] == '0') 5 17 22
1189 5 croak ("%s", err_pointer);
1190  
1191 /*
1192 * "-" denotes the position after the last element, not an
1193 * existing element that this operation can return.
1194 */
1195 41 ▶ if (key_len == 1 && key[0] == '-')
Branch: 2 branches
BranchTrueFalseTotal
if (key_len == 1 && key[0] == '-') 24 17 41
if (key_len == 1 && key[0] == '-') 2 22 24
1196 2 croak ("%s", err_bounds);
1197  
1198 /*
1199 * Validate the entire token before converting it. Do not
1200 * accept signs, whitespace, exponents, decimal points or NUL.
1201 */
1202 583 ▶ for (i = 0; i < key_len; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < key_len; ++i) 553 30 583
1203 {
1204 553 unsigned char ch = (unsigned char)key[i];
1205  
1206 553 ▶ if (ch < '0' || ch > '9')
Branch: 2 branches
BranchTrueFalseTotal
if (ch < '0' || ch > '9') 547 6 553
if (ch < '0' || ch > '9') 3 544 547
1207 9 croak ("%s", err_type);
1208 }
1209  
1210 206 ▶ for (i = 0; i < key_len; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < key_len; ++i) 180 26 206
1211 {
1212 180 UV digit = (UV)((unsigned char)key[i] - '0');
1213  
1214 180 ▶ if (index > (UV_MAX - digit) / 10)
Branch: 1 branch
BranchTrueFalseTotal
if (index > (UV_MAX - digit) / 10) 4 176 180
1215 4 croak ("%s", err_bounds);
1216  
1217 176 index = index * 10 + digit;
1218 }
1219  
1220 26 last = av_len (av);
1221  
1222 /*
1223 * Check the negative empty-array sentinel before converting
1224 * to UV. The successful comparison also makes the later
1225 * conversion to SSize_t safe.
1226 */
1227 26 ▶ if (last < 0 || index > (UV)last)
Branch: 2 branches
BranchTrueFalseTotal
if (last < 0 || index > (UV)last) 22 4 26
if (last < 0 || index > (UV)last) 5 17 22
1228 9 croak ("%s", err_bounds);
1229  
1230 17 element = av_fetch (av, (SSize_t)index, 0);
1231  
1232 17 ▶ if (!element || !*element)
Branch: 2 branches
BranchTrueFalseTotal
if (!element || !*element) 17 0 17
if (!element || !*element) 0 17 17
1233 0 croak ("%s", err_bounds);
1234  
1235 17 next = *element;
1236 }
1237 else
1238 {
1239 HE *entry;
1240  
1241 /*
1242 * Perl hash keys ultimately use a signed I32 byte length.
1243 * Do not allow an oversized token to reach a narrowing
1244 * conversion inside the hash implementation.
1245 */
1246 52 ▶ if (key_len > (STRLEN)I32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (key_len > (STRLEN)I32_MAX) 0 52 52
1247 0 croak ("INVALID_JSON_POINTER: object key is too long");
1248  
1249 /*
1250 * The key SV carries both its length and UTF-8 flag.
1251 * Embedded NULs and the empty key remain significant.
1252 */
1253 52 entry = hv_fetch_ent ((HV *)container, key_sv, 0, 0);
1254  
1255 52 ▶ if (!entry)
Branch: 1 branch
BranchTrueFalseTotal
if (!entry) 3 49 52
1256 3 croak ("%s", err_field);
1257  
1258 49 next = HeVAL (entry);
1259  
1260 49 ▶ if (!next)
Branch: 1 branch
BranchTrueFalseTotal
if (!next) 0 49 49
1261 0 croak ("%s", err_field);
1262 }
1263  
1264 /*
1265 * Copy the selected scalar before advancing. Besides protecting
1266 * the returned value's lifetime, this avoids retaining a borrowed
1267 * hash/array slot across subsequent Perl API calls.
1268 *
1269 * The previous current value remains mortal and therefore alive
1270 * until the enclosing temporary scope is cleaned up.
1271 */
1272 66 current = sv_2mortal (newSVsv (next));
1273  
1274 66 ▶ if (pos == text_len)
Branch: 1 branch
BranchTrueFalseTotal
if (pos == text_len) 39 27 66
1275 39 return current;
1276  
1277 /*
1278 * text[pos] is a separator. Always process the following token,
1279 * even if it is empty because this is the final slash.
1280 */
1281 27 ++pos;
1282 }
1283 }
1284  
1285 INLINE void
1286 9 decode_comment (dec_t *dec)
1287 {
1288 // only '#'-style comments allowed a.t.m.
1289  
1290 47 ▶ while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d)
Branch: 3 branches
BranchTrueFalseTotal
while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) 47 0 47
while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) 38 9 47
while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) 38 0 38
1291 38 ++dec->cur;
1292 9 }
1293  
1294 INLINE void
1295 88960 decode_ws (dec_t *dec)
1296 {
1297 for (;;)
1298 3853 {
1299 92813 char ch = *dec->cur;
1300  
1301 92813 ▶ if (ch > 0x20)
Branch: 1 branch
BranchTrueFalseTotal
if (ch > 0x20) 70318 22495 92813
1302 {
1303 70318 ▶ if (expect_false (ch == '#'))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false (ch == '#')) 9 70309 70318
1304 {
1305 9 ▶ if (dec->json.flags & F_RELAXED)
Branch: 1 branch
BranchTrueFalseTotal
if (dec->json.flags & F_RELAXED) 9 0 9
1306 9 decode_comment (dec);
1307 else
1308 0 break;
1309 }
1310 else
1311 70309 break;
1312 }
1313 22495 ▶ else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
Branch: 4 branches
BranchTrueFalseTotal
else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09) 19275 3220 22495
else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09) 18916 359 19275
else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09) 18916 0 18916
else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09) 265 18651 18916
1314 break; // parse error, but let higher level handle it, gives better error messages
1315 else
1316 3844 ++dec->cur;
1317 }
1318 88960 }
1319  
1320 #define ERR(reason) SB dec->err = reason; goto fail; SE
1321  
1322 #define EXPECT_CH(ch) SB \
1323 if (*dec->cur != ch) \
1324 ERR (# ch " expected"); \
1325 ++dec->cur; \
1326 SE
1327  
1328 #define DEC_INC_DEPTH if (++dec->depth > dec->json.max_depth) ERR (ERR_NESTING_EXCEEDED)
1329 #define DEC_DEC_DEPTH --dec->depth
1330  
1331 static SV *decode_sv (dec_t *dec);
1332  
1333 static signed char decode_hexdigit[256];
1334  
1335 static UV
1336 3861560 decode_4hex (dec_t *dec)
1337 {
1338 signed char d1, d2, d3, d4;
1339 3861560 unsigned char *cur = (unsigned char *)dec->cur;
1340  
1341 3861560 ▶ d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected");
Branch: 1 branch
BranchTrueFalseTotal
d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected"); 0 3861560 3861560
1342 3861560 ▶ d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected");
Branch: 1 branch
BranchTrueFalseTotal
d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected"); 0 3861560 3861560
1343 3861560 ▶ d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected");
Branch: 1 branch
BranchTrueFalseTotal
d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected"); 0 3861560 3861560
1344 3861560 ▶ d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected");
Branch: 1 branch
BranchTrueFalseTotal
d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected"); 0 3861560 3861560
1345  
1346 3861560 dec->cur += 4;
1347  
1348 3861560 return ((UV)d1) << 12
1349 3861560 | ((UV)d2) << 8
1350 3861560 | ((UV)d3) << 4
1351 3861560 | ((UV)d4);
1352  
1353 0 fail:
1354 0 return (UV)-1;
1355 }
1356  
1357 static SV *
1358 19601 decode_str (dec_t *dec)
1359 {
1360 19601 SV *sv = 0;
1361 19601 int utf8 = 0;
1362 19601 char *dec_cur = dec->cur;
1363  
1364 do
1365 {
1366 char buf [SHORT_STRING_LEN + UTF8_MAXBYTES];
1367 19601 char *cur = buf;
1368  
1369 do
1370 {
1371 7134789 unsigned char ch = *(unsigned char *)dec_cur++;
1372  
1373 7134789 ▶ if (expect_false (ch == '"'))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false (ch == '"')) 19588 7115201 7134789
1374 {
1375 19588 --dec_cur;
1376 19588 break;
1377 }
1378 7115201 ▶ else if (expect_false (ch == '\\'))
Branch: 1 branch
BranchTrueFalseTotal
else if (expect_false (ch == '\\')) 3131001 3984200 7115201
1379 {
1380 3131001 switch (*dec_cur)
1381 {
1382 30533 case '\\':
1383 case '/':
1384 30533 case '"': *cur++ = *dec_cur++; break;
1385  
1386 15911 case 'b': ++dec_cur; *cur++ = '\010'; break;
1387 19451 case 't': ++dec_cur; *cur++ = '\011'; break;
1388 16760 case 'n': ++dec_cur; *cur++ = '\012'; break;
1389 16385 case 'f': ++dec_cur; *cur++ = '\014'; break;
1390 16199 case 'r': ++dec_cur; *cur++ = '\015'; break;
1391  
1392 3015758 case 'u':
1393 {
1394 UV lo, hi;
1395 3015758 ++dec_cur;
1396  
1397 3015758 dec->cur = dec_cur;
1398 3015758 hi = decode_4hex (dec);
1399 3015758 dec_cur = dec->cur;
1400 3015758 ▶ if (hi == (UV)-1)
Branch: 1 branch
BranchTrueFalseTotal
if (hi == (UV)-1) 0 3015758 3015758
1401 13 goto fail;
1402  
1403 // possibly a surrogate pair
1404 3015758 ▶ if (hi >= 0xd800)
Branch: 1 branch
BranchTrueFalseTotal
if (hi >= 0xd800) 852686 2163072 3015758
1405 {
1406 852686 ▶ if (hi < 0xdc00)
Branch: 1 branch
BranchTrueFalseTotal
if (hi < 0xdc00) 845803 6883 852686
1407 {
1408 845803 ▶ if (dec_cur [0] != '\\' || dec_cur [1] != 'u')
Branch: 2 branches
BranchTrueFalseTotal
if (dec_cur [0] != '\\' || dec_cur [1] != 'u') 845802 1 845803
if (dec_cur [0] != '\\' || dec_cur [1] != 'u') 0 845802 845802
1409 1 ERR ("missing low surrogate character in surrogate pair");
1410  
1411 845802 dec_cur += 2;
1412  
1413 845802 dec->cur = dec_cur;
1414 845802 lo = decode_4hex (dec);
1415 845802 dec_cur = dec->cur;
1416 845802 ▶ if (lo == (UV)-1)
Branch: 1 branch
BranchTrueFalseTotal
if (lo == (UV)-1) 0 845802 845802
1417 0 goto fail;
1418  
1419 845802 ▶ if (lo < 0xdc00 || lo >= 0xe000)
Branch: 2 branches
BranchTrueFalseTotal
if (lo < 0xdc00 || lo >= 0xe000) 845801 1 845802
if (lo < 0xdc00 || lo >= 0xe000) 0 845801 845801
1420 1 ERR ("surrogate pair expected");
1421  
1422 845801 hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000;
1423 }
1424 6883 ▶ else if (hi < 0xe000)
Branch: 1 branch
BranchTrueFalseTotal
else if (hi < 0xe000) 1 6882 6883
1425 1 ERR ("missing high surrogate character in surrogate pair");
1426 }
1427  
1428 3015755 ▶ if (hi >= 0x80)
Branch: 1 branch
BranchTrueFalseTotal
if (hi >= 0x80) 2580230 435525 3015755
1429 {
1430 2580230 utf8 = 1;
1431  
1432 2580230 cur = (char*)encode_utf8 ((U8*)cur, hi);
1433 }
1434 else
1435 435525 *cur++ = hi;
1436 }
1437 3015755 break;
1438  
1439 4 default:
1440 4 --dec_cur;
1441 4 ERR ("illegal backslash escape sequence in string");
1442 }
1443 }
1444 3984200 ▶ else if (expect_true (ch >= 0x20 && ch < 0x80))
Branch: 3 branches
BranchTrueFalseTotal
else if (expect_true (ch >= 0x20 && ch < 0x80)) 3984195 5 3984200
else if (expect_true (ch >= 0x20 && ch < 0x80)) 1403916 2580279 3984195
else if (expect_true (ch >= 0x20 && ch < 0x80)) 1403916 2580284 3984200
1445 1403916 *cur++ = ch;
1446 2580284 ▶ else if (ch >= 0x80)
Branch: 1 branch
BranchTrueFalseTotal
else if (ch >= 0x80) 2580279 5 2580284
1447 {
1448 STRLEN clen;
1449  
1450 2580279 --dec_cur;
1451  
1452 2580279 decode_utf8 ((U8*)dec_cur, dec->end - dec_cur, &clen);
1453 2580279 ▶ if (clen == (STRLEN)-1)
Branch: 1 branch
BranchTrueFalseTotal
if (clen == (STRLEN)-1) 1 2580278 2580279
1454 1 ERR ("malformed UTF-8 character in JSON string");
1455  
1456 do
1457 7690336 *cur++ = *dec_cur++;
1458 7690336 ▶ while (--clen);
Branch: 1 branch
BranchTrueFalseTotal
while (--clen); 5110058 2580278 7690336
1459  
1460 2580278 utf8 = 1;
1461 }
1462 5 ▶ else if (ch == '\t' && dec->json.flags & F_RELAXED)
Branch: 2 branches
BranchTrueFalseTotal
else if (ch == '\t' && dec->json.flags & F_RELAXED) 1 4 5
else if (ch == '\t' && dec->json.flags & F_RELAXED) 0 1 1
1463 0 *cur++ = ch;
1464 else
1465 {
1466 5 --dec_cur;
1467  
1468 5 ▶ if (!ch)
Branch: 1 branch
BranchTrueFalseTotal
if (!ch) 1 4 5
1469 1 ERR ("unexpected end of string while parsing JSON string");
1470 else
1471 4 ERR ("invalid character encountered while parsing JSON string");
1472 }
1473 }
1474 7115188 ▶ while (cur < buf + SHORT_STRING_LEN);
Branch: 1 branch
BranchTrueFalseTotal
while (cur < buf + SHORT_STRING_LEN); 7115188 0 7115188
1475  
1476 {
1477 19588 STRLEN len = cur - buf;
1478  
1479 19588 ▶ if (sv)
Branch: 1 branch
BranchTrueFalseTotal
if (sv) 0 19588 19588
1480 {
1481 0 STRLEN cur = SvCUR (sv);
1482  
1483 0 ▶ if (SvLEN (sv) - cur <= len)
Branch: 1 branch
BranchTrueFalseTotal
if (SvLEN (sv) - cur <= len) 0 0 0
1484 0 json_sv_grow (sv, cur, len);
1485  
1486 0 memcpy (SvPVX (sv) + SvCUR (sv), buf, len);
1487 0 SvCUR_set (sv, SvCUR (sv) + len);
1488 }
1489 else
1490 19588 sv = newSVpvn (buf, len);
1491 }
1492 }
1493 19588 ▶ while (*dec_cur != '"');
Branch: 1 branch
BranchTrueFalseTotal
while (*dec_cur != '"'); 0 19588 19588
1494  
1495 19588 ++dec_cur;
1496  
1497 19588 ▶ if (sv)
Branch: 1 branch
BranchTrueFalseTotal
if (sv) 19588 0 19588
1498 {
1499 19588 SvPOK_only (sv);
1500 19588 *SvEND (sv) = 0;
1501  
1502 19588 ▶ if (utf8)
Branch: 1 branch
BranchTrueFalseTotal
if (utf8) 17929 1659 19588
1503 17929 SvUTF8_on (sv);
1504 }
1505 else
1506 0 sv = newSVpvn ("", 0);
1507  
1508 19588 dec->cur = dec_cur;
1509 19588 return sv;
1510  
1511 13 fail:
1512 13 dec->cur = dec_cur;
1513 13 SvREFCNT_dec (sv);
1514 13 return 0;
1515 }
1516  
1517 static SV *
1518 1933 decode_num (dec_t *dec)
1519 {
1520 1933 int is_nv = 0;
1521 1933 char *start = dec->cur;
1522  
1523 // [minus]
1524 1933 ▶ if (*dec->cur == '-')
Branch: 1 branch
BranchTrueFalseTotal
if (*dec->cur == '-') 24 1909 1933
1525 24 ++dec->cur;
1526  
1527 1933 ▶ if (*dec->cur == '0')
Branch: 1 branch
BranchTrueFalseTotal
if (*dec->cur == '0') 1043 890 1933
1528 {
1529 1043 ++dec->cur;
1530 1043 ▶ if (*dec->cur >= '0' && *dec->cur <= '9')
Branch: 2 branches
BranchTrueFalseTotal
if (*dec->cur >= '0' && *dec->cur <= '9') 10 1033 1043
if (*dec->cur >= '0' && *dec->cur <= '9') 3 7 10
1531 3 ERR ("malformed number (leading zero must not be followed by another digit)");
1532 }
1533 890 ▶ else if (*dec->cur < '0' || *dec->cur > '9')
Branch: 2 branches
BranchTrueFalseTotal
else if (*dec->cur < '0' || *dec->cur > '9') 889 1 890
else if (*dec->cur < '0' || *dec->cur > '9') 1 888 889
1534 2 ERR ("malformed number (no digits after initial minus)");
1535 else
1536 do
1537 {
1538 1073 ++dec->cur;
1539 }
1540 1073 ▶ while (*dec->cur >= '0' && *dec->cur <= '9');
Branch: 2 branches
BranchTrueFalseTotal
while (*dec->cur >= '0' && *dec->cur <= '9'); 410 663 1073
while (*dec->cur >= '0' && *dec->cur <= '9'); 185 225 410
1541  
1542 // [frac]
1543 1928 ▶ if (*dec->cur == '.')
Branch: 1 branch
BranchTrueFalseTotal
if (*dec->cur == '.') 1050 878 1928
1544 {
1545 1050 ++dec->cur;
1546  
1547 1050 ▶ if (*dec->cur < '0' || *dec->cur > '9')
Branch: 2 branches
BranchTrueFalseTotal
if (*dec->cur < '0' || *dec->cur > '9') 1049 1 1050
if (*dec->cur < '0' || *dec->cur > '9') 1 1048 1049
1548 2 ERR ("malformed number (no digits after decimal point)");
1549  
1550 do
1551 {
1552 1000126 ++dec->cur;
1553 }
1554 1000126 ▶ while (*dec->cur >= '0' && *dec->cur <= '9');
Branch: 2 branches
BranchTrueFalseTotal
while (*dec->cur >= '0' && *dec->cur <= '9'); 999115 1011 1000126
while (*dec->cur >= '0' && *dec->cur <= '9'); 999078 37 999115
1555  
1556 1048 is_nv = 1;
1557 }
1558  
1559 // [exp]
1560 1926 ▶ if (*dec->cur == 'e' || *dec->cur == 'E')
Branch: 2 branches
BranchTrueFalseTotal
if (*dec->cur == 'e' || *dec->cur == 'E') 1891 35 1926
if (*dec->cur == 'e' || *dec->cur == 'E') 25 1866 1891
1561 {
1562 60 ++dec->cur;
1563  
1564 60 ▶ if (*dec->cur == '-' || *dec->cur == '+')
Branch: 2 branches
BranchTrueFalseTotal
if (*dec->cur == '-' || *dec->cur == '+') 53 7 60
if (*dec->cur == '-' || *dec->cur == '+') 32 21 53
1565 39 ++dec->cur;
1566  
1567 60 ▶ if (*dec->cur < '0' || *dec->cur > '9')
Branch: 2 branches
BranchTrueFalseTotal
if (*dec->cur < '0' || *dec->cur > '9') 55 5 60
if (*dec->cur < '0' || *dec->cur > '9') 2 53 55
1568 7 ERR ("malformed number (no digits after exp sign)");
1569  
1570 do
1571 {
1572 85 ++dec->cur;
1573 }
1574 85 ▶ while (*dec->cur >= '0' && *dec->cur <= '9');
Branch: 2 branches
BranchTrueFalseTotal
while (*dec->cur >= '0' && *dec->cur <= '9'); 35 50 85
while (*dec->cur >= '0' && *dec->cur <= '9'); 32 3 35
1575  
1576 53 is_nv = 1;
1577 }
1578  
1579 1919 ▶ if (!is_nv)
Branch: 1 branch
BranchTrueFalseTotal
if (!is_nv) 852 1067 1919
1580 {
1581 852 int len = dec->cur - start;
1582  
1583 // special case the rather common 1..5-digit-int case
1584 852 ▶ if (*start == '-')
Branch: 1 branch
BranchTrueFalseTotal
if (*start == '-') 4 848 852
1585 4 switch (len)
1586 {
1587 1 case 2: return newSViv (-(IV)( start [1] - '0' * 1));
1588 3 case 3: return newSViv (-(IV)( start [1] * 10 + start [2] - '0' * 11));
1589 0 case 4: return newSViv (-(IV)( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111));
1590 0 case 5: return newSViv (-(IV)( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
1591 0 case 6: return newSViv (-(IV)(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111));
1592 }
1593 else
1594 848 switch (len)
1595 {
1596 717 case 1: return newSViv ( start [0] - '0' * 1);
1597 121 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
1598 4 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111);
1599 4 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
1600 0 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111);
1601 }
1602  
1603 {
1604 UV uv;
1605 2 int numtype = grok_number (start, len, &uv);
1606 2 ▶ if (numtype & IS_NUMBER_IN_UV)
Branch: 1 branch
BranchTrueFalseTotal
if (numtype & IS_NUMBER_IN_UV) 2 0 2
1607 {
1608 2 ▶ if (numtype & IS_NUMBER_NEG)
Branch: 1 branch
BranchTrueFalseTotal
if (numtype & IS_NUMBER_NEG) 0 2 2
1609 {
1610 0 ▶ if (uv < (UV)IV_MIN)
Branch: 1 branch
BranchTrueFalseTotal
if (uv < (UV)IV_MIN) 0 0 0
1611 2 return newSViv (-(IV)uv);
1612 }
1613 else
1614 2 return newSVuv (uv);
1615 }
1616 }
1617  
1618 0 len -= *start == '-' ? 1 : 0;
1619  
1620 // does not fit into IV or UV, try NV
1621 0 ▶ if (len <= NV_DIG)
Branch: 1 branch
BranchTrueFalseTotal
if (len <= NV_DIG) 0 0 0
1622 // fits into NV without loss of precision
1623 0 return newSVnv (json_atof (start));
1624  
1625 // everything else fails, convert it to a string
1626 0 return newSVpvn (start, dec->cur - start);
1627 }
1628  
1629 // loss of precision here
1630 1067 return newSVnv (json_atof (start));
1631  
1632 14 fail:
1633 14 return 0;
1634 }
1635  
1636 static SV *
1637 21007 decode_av (dec_t *dec)
1638 {
1639 21007 AV *av = newAV ();
1640  
1641 21007 ▶ DEC_INC_DEPTH;
Branch: 1 branch
BranchTrueFalseTotal
DEC_INC_DEPTH; 3 21004 21007
1642 21004 decode_ws (dec);
1643  
1644 21004 ▶ if (*dec->cur == ']')
Branch: 1 branch
BranchTrueFalseTotal
if (*dec->cur == ']') 20976 28 21004
1645 28 ++dec->cur;
1646 else
1647 for (;;)
1648 896 {
1649 SV *value;
1650  
1651 21872 value = decode_sv (dec);
1652 21872 ▶ if (!value)
Branch: 1 branch
BranchTrueFalseTotal
if (!value) 563 21309 21872
1653 563 goto fail;
1654  
1655 21309 av_push (av, value);
1656  
1657 21309 decode_ws (dec);
1658  
1659 21309 ▶ if (*dec->cur == ']')
Branch: 1 branch
BranchTrueFalseTotal
if (*dec->cur == ']') 19407 1902 21309
1660 {
1661 19407 ++dec->cur;
1662 19407 break;
1663 }
1664  
1665 1902 ▶ if (*dec->cur != ',')
Branch: 1 branch
BranchTrueFalseTotal
if (*dec->cur != ',') 1004 898 1902
1666 1004 ERR (", or ] expected while parsing array");
1667  
1668 898 ++dec->cur;
1669  
1670 898 decode_ws (dec);
1671  
1672 898 ▶ if (*dec->cur == ']' && dec->json.flags & F_RELAXED)
Branch: 2 branches
BranchTrueFalseTotal
if (*dec->cur == ']' && dec->json.flags & F_RELAXED) 3 895 898
if (*dec->cur == ']' && dec->json.flags & F_RELAXED) 2 1 3
1673 {
1674 2 ++dec->cur;
1675 2 break;
1676 }
1677 }
1678  
1679 19437 DEC_DEC_DEPTH;
1680 19437 return newRV_noinc ((SV *)av);
1681  
1682 1570 fail:
1683 1570 SvREFCNT_dec (av);
1684 1570 DEC_DEC_DEPTH;
1685 1570 return 0;
1686 }
1687  
1688 static SV *
1689 14 filter_object (dec_t *dec, SV *sv, HV* hv)
1690 {
1691 14 ▶ if (dec->json.cb_sk_object && HvKEYS (hv) == 1)
Branch: 3 branches
BranchTrueFalseTotal
if (dec->json.cb_sk_object && HvKEYS (hv) == 1) 12 2 14
if (dec->json.cb_sk_object && HvKEYS (hv) == 1) 0 12 12
if (dec->json.cb_sk_object && HvKEYS (hv) == 1) 8 4 12
1692 {
1693 HE *cb, *he;
1694  
1695 8 hv_iterinit (hv);
1696 8 he = hv_iternext (hv);
1697 8 hv_iterinit (hv);
1698  
1699 // the next line creates a mortal sv each time it's called.
1700 // might want to optimise this for common cases.
1701 8 cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0);
1702  
1703 8 ▶ if (cb)
Branch: 1 branch
BranchTrueFalseTotal
if (cb) 6 2 8
1704 {
1705 6 dSP;
1706 int count;
1707  
1708 6 ENTER; SAVETMPS;
1709 6 ▶ PUSHMARK (SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK (SP); 0 6 6
1710 6 ▶ XPUSHs (HeVAL (he));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (HeVAL (he)); 0 6 6
1711 6 sv_2mortal (sv);
1712  
1713 6 PUTBACK; count = call_sv (sv_2mortal (SvREFCNT_inc (HeVAL (cb))), G_ARRAY); SPAGAIN;
1714  
1715 6 ▶ if (count == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (count == 1) 5 1 6
1716 {
1717 5 sv = newSVsv (POPs);
1718 5 PUTBACK;
1719 5 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 5 0 5
1720 5 return sv;
1721 }
1722 1 ▶ else if (count)
Branch: 1 branch
BranchTrueFalseTotal
else if (count) 0 1 1
1723 0 croak ("filter_json_single_key_object callbacks must not return more than one scalar");
1724  
1725 1 PUTBACK;
1726  
1727 1 SvREFCNT_inc (sv);
1728  
1729 1 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 1 0 1
1730 }
1731 }
1732  
1733 9 ▶ if (dec->json.cb_object)
Branch: 1 branch
BranchTrueFalseTotal
if (dec->json.cb_object) 7 2 9
1734 {
1735 7 dSP;
1736 int count;
1737  
1738 7 ENTER; SAVETMPS;
1739 7 ▶ PUSHMARK (SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK (SP); 0 7 7
1740 7 ▶ XPUSHs (sv_2mortal (sv));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (sv_2mortal (sv)); 0 7 7
1741  
1742 7 PUTBACK; count = call_sv (dec->json.cb_object, G_LIST); SPAGAIN;
1743  
1744 7 ▶ if (count == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (count == 1) 4 3 7
1745 4 sv = newSVsv (POPs);
1746 3 ▶ else if (count == 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (count == 0) 3 0 3
1747 3 SvREFCNT_inc (sv);
1748 else
1749 0 croak ("filter_json_object callbacks must not return more than one scalar");
1750  
1751 7 PUTBACK;
1752  
1753 7 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 7 0 7
1754 }
1755 9 return sv;
1756 }
1757  
1758 static SV *
1759 1445 decode_hv (dec_t *dec)
1760 {
1761 SV *sv;
1762 1445 HV *hv = newHV ();
1763  
1764 1445 ▶ DEC_INC_DEPTH;
Branch: 1 branch
BranchTrueFalseTotal
DEC_INC_DEPTH; 0 1445 1445
1765 1445 decode_ws (dec);
1766  
1767 1445 ▶ if (*dec->cur == '}')
Branch: 1 branch
BranchTrueFalseTotal
if (*dec->cur == '}') 8 1437 1445
1768 8 ++dec->cur;
1769 else
1770 for (;;)
1771 {
1772 1846 ▶ EXPECT_CH ('"');
Branch: 1 branch
BranchTrueFalseTotal
EXPECT_CH ('"'); 4 1842 1846
1773  
1774 // heuristic: assume that
1775 // a) decode_str + hv_store_ent are abysmally slow.
1776 // b) most hash keys are short, simple ascii text.
1777 // => try to "fast-match" such strings to avoid
1778 // the overhead of decode_str + hv_store_ent.
1779 {
1780 SV *value;
1781 1842 char *p = dec->cur;
1782 1842 char *e = p + 24; // only try up to 24 bytes
1783  
1784 for (;;)
1785 {
1786 // the >= 0x80 is false on most architectures
1787 3349 ▶ if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\')
Branch: 3 branches
BranchTrueFalseTotal
if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') 3340 9 3349
if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') 3313 27 3340
if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') 228 3085 3313
1788 {
1789 // slow path, back up and use decode_str
1790 264 SV *key = decode_str (dec);
1791 264 ▶ if (!key)
Branch: 1 branch
BranchTrueFalseTotal
if (!key) 0 264 264
1792 0 goto fail;
1793  
1794 264 decode_ws (dec);
1795 264 ▶ if (*dec->cur != ':')
Branch: 1 branch
BranchTrueFalseTotal
if (*dec->cur != ':') 0 264 264
1796 0 SvREFCNT_dec (key);
1797 264 ▶ EXPECT_CH (':');
Branch: 1 branch
BranchTrueFalseTotal
EXPECT_CH (':'); 0 264 264
1798  
1799 264 decode_ws (dec);
1800 264 value = decode_sv (dec);
1801 264 ▶ if (!value)
Branch: 1 branch
BranchTrueFalseTotal
if (!value) 1 263 264
1802 {
1803 1 SvREFCNT_dec (key);
1804 1 goto fail;
1805 }
1806  
1807 263 hv_store_ent (hv, key, value, 0);
1808 263 SvREFCNT_dec (key);
1809  
1810 263 break;
1811 }
1812 3085 ▶ else if (*p == '"')
Branch: 1 branch
BranchTrueFalseTotal
else if (*p == '"') 1578 1507 3085
1813 {
1814 // fast path, got a simple key
1815 1578 char *key = dec->cur;
1816 1578 int len = p - key;
1817 1578 dec->cur = p + 1;
1818  
1819 1578 ▶ decode_ws (dec); EXPECT_CH (':');
Branch: 1 branch
BranchTrueFalseTotal
decode_ws (dec); EXPECT_CH (':'); 3 1575 1578
1820  
1821 1575 decode_ws (dec);
1822 1575 value = decode_sv (dec);
1823 1575 ▶ if (!value)
Branch: 1 branch
BranchTrueFalseTotal
if (!value) 515 1060 1575
1824 515 goto fail;
1825  
1826 1060 hv_store (hv, key, len, value, 0);
1827  
1828 1060 break;
1829 }
1830  
1831 1507 ++p;
1832 }
1833 }
1834  
1835 1323 decode_ws (dec);
1836  
1837 1323 ▶ if (*dec->cur == '}')
Branch: 1 branch
BranchTrueFalseTotal
if (*dec->cur == '}') 910 413 1323
1838 {
1839 910 ++dec->cur;
1840 910 break;
1841 }
1842  
1843 413 ▶ if (*dec->cur != ',')
Branch: 1 branch
BranchTrueFalseTotal
if (*dec->cur != ',') 3 410 413
1844 3 ERR (", or } expected while parsing object/hash");
1845  
1846 410 ++dec->cur;
1847  
1848 410 decode_ws (dec);
1849  
1850 410 ▶ if (*dec->cur == '}' && dec->json.flags & F_RELAXED)
Branch: 2 branches
BranchTrueFalseTotal
if (*dec->cur == '}' && dec->json.flags & F_RELAXED) 2 408 410
if (*dec->cur == '}' && dec->json.flags & F_RELAXED) 1 1 2
1851 {
1852 1 ++dec->cur;
1853 1 break;
1854 }
1855 }
1856  
1857 919 DEC_DEC_DEPTH;
1858 919 sv = newRV_noinc ((SV *)hv);
1859  
1860 // check filter callbacks
1861 919 ▶ if (expect_false (dec->json.flags & F_HOOK))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false (dec->json.flags & F_HOOK)) 14 905 919
1862 14 sv = filter_object(dec, sv, hv);
1863  
1864 919 return sv;
1865  
1866 526 fail:
1867 526 SvREFCNT_dec (hv);
1868 526 DEC_DEC_DEPTH;
1869 526 return 0;
1870 }
1871  
1872 static SV *
1873 68 decode_tag (dec_t *dec)
1874 {
1875 68 SV *tag = 0;
1876 68 SV *val = 0;
1877  
1878 68 ▶ if (!(dec->json.flags & F_ALLOW_TAGS))
Branch: 1 branch
BranchTrueFalseTotal
if (!(dec->json.flags & F_ALLOW_TAGS)) 0 68 68
1879 0 ERR ("malformed JSON string, neither array, object, number, string or atom");
1880  
1881 68 ++dec->cur;
1882  
1883 68 decode_ws (dec);
1884  
1885 68 ▶ DEC_INC_DEPTH; /* a "(" here recurses straight back into decode_tag */
Branch: 1 branch
BranchTrueFalseTotal
DEC_INC_DEPTH; /* a "(" here recurses straight back into decode_tag */ 2 66 68
1886 66 tag = decode_sv (dec);
1887 66 DEC_DEC_DEPTH;
1888 66 ▶ if (!tag)
Branch: 1 branch
BranchTrueFalseTotal
if (!tag) 32 34 66
1889 32 goto fail;
1890  
1891 34 ▶ if (!SvPOK (tag))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvPOK (tag)) 0 34 34
1892 0 ERR ("malformed JSON string, (tag) must be a string");
1893  
1894 34 decode_ws (dec);
1895  
1896 34 ▶ if (*dec->cur != ')')
Branch: 1 branch
BranchTrueFalseTotal
if (*dec->cur != ')') 0 34 34
1897 0 ERR (") expected after tag");
1898  
1899 34 ++dec->cur;
1900  
1901 34 decode_ws (dec);
1902  
1903 34 ▶ if (*dec->cur == '[')
Branch: 1 branch
BranchTrueFalseTotal
if (*dec->cur == '[') 2 32 34
1904 2 val = decode_sv (dec); /* decode_av charges this level */
1905 else
1906 {
1907 32 ▶ DEC_INC_DEPTH; /* ("a")("a")... recurses through the value too */
Branch: 1 branch
BranchTrueFalseTotal
DEC_INC_DEPTH; /* ("a")("a")... recurses through the value too */ 0 32 32
1908 32 val = decode_sv (dec);
1909 32 DEC_DEC_DEPTH;
1910 }
1911 34 ▶ if (!val)
Branch: 1 branch
BranchTrueFalseTotal
if (!val) 32 2 34
1912 32 goto fail;
1913  
1914 2 ▶ if (!SvROK (val) || SvTYPE (SvRV (val)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK (val) || SvTYPE (SvRV (val)) != SVt_PVAV) 2 0 2
if (!SvROK (val) || SvTYPE (SvRV (val)) != SVt_PVAV) 0 2 2
1915 0 ERR ("malformed JSON string, tag value must be an array");
1916  
1917 {
1918 2 AV *av = (AV *)SvRV (val);
1919 2 int i, len = av_len (av) + 1;
1920 2 HV *stash = gv_stashsv (tag, 0);
1921 SV *sv;
1922  
1923 2 ▶ if (!stash)
Branch: 1 branch
BranchTrueFalseTotal
if (!stash) 0 2 2
1924 0 ERR ("cannot decode perl-object (package does not exist)");
1925  
1926 2 GV *method = gv_fetchmethod_autoload (stash, "THAW", 0);
1927
1928 2 ▶ if (!method)
Branch: 1 branch
BranchTrueFalseTotal
if (!method) 0 2 2
1929 0 ERR ("cannot decode perl-object (package does not have a THAW method)");
1930
1931 2 dSP;
1932  
1933 2 ENTER; SAVETMPS;
1934 2 SAVEFREESV (tag); /* also freed when THAW dies */
1935 2 SAVEFREESV (val);
1936 2 ▶ PUSHMARK (SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK (SP); 0 2 2
1937 2 ▶ EXTEND (SP, len + 2);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND (SP, len + 2); 2 0 2
EXTEND (SP, len + 2); 0 2 2
1938 // we re-bless the reference to get overload and other niceties right
1939 2 PUSHs (tag);
1940 2 PUSHs (sv_json);
1941  
1942 6 ▶ for (i = 0; i < len; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; ++i) 4 2 6
1943 4 PUSHs (*av_fetch (av, i, 1));
1944  
1945 2 PUTBACK;
1946 2 call_sv ((SV *)GvCV (method), G_SCALAR);
1947 1 SPAGAIN;
1948  
1949 1 sv = SvREFCNT_inc (POPs);
1950  
1951 1 PUTBACK;
1952  
1953 1 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 1 0 1
1954  
1955 1 return sv;
1956 }
1957  
1958 66 fail:
1959 66 SvREFCNT_dec (tag);
1960 66 SvREFCNT_dec (val);
1961 66 return 0;
1962 }
1963  
1964 static SV *
1965 43916 decode_sv (dec_t *dec)
1966 {
1967 // the beauty of JSON: you need exactly one character lookahead
1968 // to parse everything.
1969 43916 switch (*dec->cur)
1970 {
1971 19337 case '"': ++dec->cur; return decode_str (dec);
1972 21007 case '[': ++dec->cur; return decode_av (dec);
1973 1445 case '{': ++dec->cur; return decode_hv (dec);
1974 68 case '(': return decode_tag (dec);
1975  
1976 1933 case '-':
1977 case '0': case '1': case '2': case '3': case '4':
1978 case '5': case '6': case '7': case '8': case '9':
1979 1933 return decode_num (dec);
1980  
1981 9 case 'f':
1982 9 ▶ if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
Branch: 2 branches
BranchTrueFalseTotal
if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 9 0 9
if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 9 0 9
1983 {
1984 9 dec->cur += 5;
1985  
1986 9 ▶ if (expect_false (!dec->json.v_false))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false (!dec->json.v_false)) 7 2 9
1987 7 dec->json.v_false = GET_BOOL (false);
1988  
1989 9 return newSVsv (dec->json.v_false);
1990 }
1991 else
1992 0 ERR ("'false' expected");
1993  
1994 break;
1995  
1996 24 case 't':
1997 24 ▶ if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
Branch: 2 branches
BranchTrueFalseTotal
if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 24 0 24
if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 23 1 24
1998 {
1999 23 dec->cur += 4;
2000  
2001 23 ▶ if (expect_false (!dec->json.v_true))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false (!dec->json.v_true)) 21 2 23
2002 21 dec->json.v_true = GET_BOOL (true);
2003  
2004 23 return newSVsv (dec->json.v_true);
2005 }
2006 else
2007 1 ERR ("'true' expected");
2008  
2009 break;
2010  
2011 72 case 'n':
2012 72 ▶ if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4))
Branch: 2 branches
BranchTrueFalseTotal
if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) 72 0 72
if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) 71 1 72
2013 {
2014 71 dec->cur += 4;
2015 71 return newSVsv (&PL_sv_undef);
2016 }
2017 else
2018 1 ERR ("'null' expected");
2019  
2020 break;
2021  
2022 21 default:
2023 21 ERR ("malformed JSON string, neither tag, array, object, number, string or atom");
2024 break;
2025 }
2026  
2027 23 fail:
2028 23 return 0;
2029 }
2030  
2031 static SV *
2032 51534 decode_json (SV *string, JSON *json, STRLEN *offset_return, SV* path)
2033 {
2034 dec_t dec;
2035 SV *sv;
2036  
2037 /* work around bugs in 5.10 where manipulating magic values
2038 * makes perl ignore the magic in subsequent accesses.
2039 * also make a copy of non-PV values, to get them into a clean
2040 * state (SvPV should do that, but it's buggy, see below).
2041 *
2042 * SvIsCOW_shared_hash works around a bug in perl (possibly 5.16),
2043 * as reported by Reini Urban.
2044 */
2045 /*SvGETMAGIC (string);*/
2046 51534 ▶ if (SvMAGICAL (string) || !SvPOK (string) || SvIsCOW_shared_hash (string))
Branch: 4 branches
BranchTrueFalseTotal
if (SvMAGICAL (string) || !SvPOK (string) || SvIsCOW_shared_hash (string)) 51290 244 51534
if (SvMAGICAL (string) || !SvPOK (string) || SvIsCOW_shared_hash (string)) 51280 10 51290
if (SvMAGICAL (string) || !SvPOK (string) || SvIsCOW_shared_hash (string)) 172 51108 51280
if (SvMAGICAL (string) || !SvPOK (string) || SvIsCOW_shared_hash (string)) 0 172 172
2047 254 string = sv_2mortal (newSVsv (string));
2048  
2049 51534 ▶ SvUPGRADE (string, SVt_PV);
Branch: 1 branch
BranchTrueFalseTotal
SvUPGRADE (string, SVt_PV); 8 51526 51534
2050  
2051 /* work around a bug in perl 5.10, which causes SvCUR to fail an
2052 * assertion with -DDEBUGGING, although SvCUR is documented to
2053 * return the xpv_cur field which certainly exists after upgrading.
2054 * according to nicholas clark, calling SvPOK fixes this.
2055 * But it doesn't fix it, so try another workaround, call SvPV_nolen
2056 * and hope for the best.
2057 * Damnit, SvPV_nolen still trips over yet another assertion. This
2058 * assertion business is seriously broken, try yet another workaround
2059 * for the broken -DDEBUGGING.
2060 */
2061 {
2062 #ifdef DEBUGGING
2063 STRLEN offset = SvOK (string) ? sv_len (string) : 0;
2064 #else
2065 51534 STRLEN offset = SvCUR (string);
2066 #endif
2067  
2068 51534 ▶ if (offset > json->max_size && json->max_size)
Branch: 2 branches
BranchTrueFalseTotal
if (offset > json->max_size && json->max_size) 51524 10 51534
if (offset > json->max_size && json->max_size) 2 51522 51524
2069 2 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
2070 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
2071 }
2072  
2073 51532 ▶ if (DECODE_WANTS_OCTETS (json))
Branch: 1 branch
BranchTrueFalseTotal
if (DECODE_WANTS_OCTETS (json)) 24714 26818 51532
2074 24714 sv_utf8_downgrade (string, 0);
2075 else
2076 26818 sv_utf8_upgrade (string);
2077  
2078 51532 ▶ SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
Branch: 2 branches
BranchTrueFalseTotal
SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 51511 21 51532
SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 0 51511 51511
2079  
2080 51532 dec.json = *json;
2081 51532 dec.cur = SvPVX (string);
2082 51532 dec.end = SvEND (string);
2083 51532 dec.err = 0;
2084 51532 dec.depth = 0;
2085  
2086 /* a filter may replace or remove these while this decode still uses them */
2087 51532 ▶ if (dec.json.cb_object)
Branch: 1 branch
BranchTrueFalseTotal
if (dec.json.cb_object) 11 51521 51532
2088 11 sv_2mortal (SvREFCNT_inc (dec.json.cb_object));
2089 51532 ▶ if (dec.json.cb_sk_object)
Branch: 1 branch
BranchTrueFalseTotal
if (dec.json.cb_sk_object) 13 51519 51532
2090 13 sv_2mortal (SvREFCNT_inc ((SV *)dec.json.cb_sk_object));
2091  
2092 51532 ▶ if (dec.json.cb_object || dec.json.cb_sk_object) {
Branch: 2 branches
BranchTrueFalseTotal
if (dec.json.cb_object || dec.json.cb_sk_object) { 51521 11 51532
if (dec.json.cb_object || dec.json.cb_sk_object) { 5 51516 51521
2093 16 dec.json.flags |= F_HOOK;
2094 16 dec.json.flags &= ~F_USE_SIMDJSON;
2095 }
2096  
2097 51532 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
2098  
2099 51532 ▶ if (dec.json.flags & F_USE_SIMDJSON) {
Branch: 1 branch
BranchTrueFalseTotal
if (dec.json.flags & F_USE_SIMDJSON) { 31427 20105 51532
2100 31427 dec.error_code = 0;
2101 31427 dec.input = string;
2102 31427 dec.path = 0;
2103  
2104 // handle path
2105 31427 ▶ if (path) {
Branch: 1 branch
BranchTrueFalseTotal
if (path) { 43 31384 31427
2106 // repeat the voodoo above for the path argument too
2107 43 ▶ if (SvMAGICAL (path) || !SvPOK (path) || SvIsCOW_shared_hash (path))
Branch: 4 branches
BranchTrueFalseTotal
if (SvMAGICAL (path) || !SvPOK (path) || SvIsCOW_shared_hash (path)) 43 0 43
if (SvMAGICAL (path) || !SvPOK (path) || SvIsCOW_shared_hash (path)) 43 0 43
if (SvMAGICAL (path) || !SvPOK (path) || SvIsCOW_shared_hash (path)) 43 0 43
if (SvMAGICAL (path) || !SvPOK (path) || SvIsCOW_shared_hash (path)) 0 43 43
2108 0 path = sv_2mortal (newSVsv (path));
2109  
2110 43 ▶ SvUPGRADE (path, SVt_PV);
Branch: 1 branch
BranchTrueFalseTotal
SvUPGRADE (path, SVt_PV); 0 43 43
2111 43 dec.path = SvPVX(path);
2112 }
2113  
2114 // we don't want to mess with this from the C++ code
2115 31427 ▶ if (expect_false (!dec.json.v_true))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false (!dec.json.v_true)) 31418 9 31427
2116 31418 dec.json.v_true = GET_BOOL (true);
2117 31427 ▶ if (expect_false (!dec.json.v_false))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false (!dec.json.v_false)) 31418 9 31427
2118 31418 dec.json.v_false = GET_BOOL (false);
2119  
2120 31427 sv = simdjson_decode(&dec);
2121  
2122 31427 dec.end = SvEND(string);
2123  
2124 } else {
2125 20105 decode_ws (&dec);
2126 20105 sv = decode_sv (&dec);
2127 }
2128  
2129 51531 ▶ if (offset_return)
Branch: 1 branch
BranchTrueFalseTotal
if (offset_return) 596 50935 51531
2130 596 *offset_return = dec.cur - SvPVX (string);
2131 50935 ▶ else if (sv && !path)
Branch: 2 branches
BranchTrueFalseTotal
else if (sv && !path) 49783 1152 50935
else if (sv && !path) 49659 124 49783
2132 {
2133 // check for trailing garbage
2134 49659 ▶ if (!(dec.json.flags & F_USE_SIMDJSON)) // simdjson gobbles up trailing whitespace anyway
Branch: 1 branch
BranchTrueFalseTotal
if (!(dec.json.flags & F_USE_SIMDJSON)) // simdjson gobbles up trailing whitespace anyway 18649 31010 49659
2135 18649 decode_ws (&dec);
2136 49659 ▶ if (dec.cur != dec.end)
Branch: 1 branch
BranchTrueFalseTotal
if (dec.cur != dec.end) 40 49619 49659
2137 {
2138 40 dec.err = "garbage after JSON object";
2139 40 SvREFCNT_dec (sv);
2140 40 sv = 0;
2141 }
2142 }
2143  
2144 51531 ▶ if (!sv)
Branch: 1 branch
BranchTrueFalseTotal
if (!sv) 1200 50331 51531
2145 {
2146 1200 SV *uni = sv_newmortal ();
2147  
2148 // horrible hack to silence warning inside pv_uni_display
2149 1200 COP cop = *PL_curcop;
2150 1200 cop.cop_warnings = pWARN_NONE;
2151 1200 ENTER;
2152 1200 SAVEVPTR (PL_curcop);
2153 1200 PL_curcop = &cop;
2154 1200 pv_uni_display (uni, (U8*)dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ);
2155 1200 LEAVE;
2156  
2157 2329 ▶ croak ("%s, at character offset %d (before \"%s\")",
Branch: 1 branch
BranchTrueFalseTotal
croak ("%s, at character offset %d (before \"%s\")", 1129 71 1200
2158 dec.err,
2159 (int)ptr_to_index (string, (U8*)dec.cur),
2160 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
2161 }
2162  
2163 50331 sv = sv_2mortal (sv);
2164  
2165 50331 ▶ if (!(dec.json.flags & F_ALLOW_NONREF) && json_nonref (sv))
Branch: 2 branches
BranchTrueFalseTotal
if (!(dec.json.flags & F_ALLOW_NONREF) && json_nonref (sv)) 398 49933 50331
if (!(dec.json.flags & F_ALLOW_NONREF) && json_nonref (sv)) 2 396 398
2166 2 croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)");
2167  
2168 50329 ▶ if (expect_false(path && !(dec.json.flags & F_USE_SIMDJSON))) {
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false(path && !(dec.json.flags & F_USE_SIMDJSON))) { 124 50205 50329
if (expect_false(path && !(dec.json.flags & F_USE_SIMDJSON))) { 102 22 124
if (expect_false(path && !(dec.json.flags & F_USE_SIMDJSON))) { 102 50227 50329
2169 /* parse the path and derefer hash/array elements */
2170 102 sv = emulate_at_pointer(sv, path);
2171 }
2172  
2173 50270 return sv;
2174 }
2175  
2176 /////////////////////////////////////////////////////////////////////////////
2177 // incremental parser
2178  
2179 /* a number or true/false/null at the start of the buffer: not self-delimiting, so its
2180 end depends on how the input was chunked */
2181 static int
2182 340 incr_starts_bare_scalar (JSON *self)
2183 {
2184 340 const char *p = SvPVX (self->incr_text);
2185 for (;;)
2186 {
2187 508 ▶ while (*p == 0x20 || *p == 0x09 || *p == 0x0a || *p == 0x0d)
Branch: 4 branches
BranchTrueFalseTotal
while (*p == 0x20 || *p == 0x09 || *p == 0x0a || *p == 0x0d) 159 349 508
while (*p == 0x20 || *p == 0x09 || *p == 0x0a || *p == 0x0d) 0 349 349
while (*p == 0x20 || *p == 0x09 || *p == 0x0a || *p == 0x0d) 5 344 349
while (*p == 0x20 || *p == 0x09 || *p == 0x0a || *p == 0x0d) 0 344 344
2188 164 ++p;
2189 344 ▶ if (*p != '#' || !(self->flags & F_RELAXED))
Branch: 2 branches
BranchTrueFalseTotal
if (*p != '#' || !(self->flags & F_RELAXED)) 4 340 344
if (*p != '#' || !(self->flags & F_RELAXED)) 4 0 4
2190 break;
2191 22 ▶ while (*p && *p != 0x0a && *p != 0x0d)
Branch: 3 branches
BranchTrueFalseTotal
while (*p && *p != 0x0a && *p != 0x0d) 22 0 22
while (*p && *p != 0x0a && *p != 0x0d) 18 4 22
while (*p && *p != 0x0a && *p != 0x0d) 18 0 18
2192 18 ++p;
2193 }
2194 338 ▶ return *p == '-' || (*p >= '0' && *p <= '9')
Branch: 2 branches
BranchTrueFalseTotal
return *p == '-' || (*p >= '0' && *p <= '9') 310 28 338
return *p == '-' || (*p >= '0' && *p <= '9') 308 2 310
2195 678 ▶ || strnEQ (p, "true", 4) || strnEQ (p, "false", 5) || strnEQ (p, "null", 4);
Branch: 4 branches
BranchTrueFalseTotal
|| strnEQ (p, "true", 4) || strnEQ (p, "false", 5) || strnEQ (p, "null", 4); 338 2 340
|| strnEQ (p, "true", 4) || strnEQ (p, "false", 5) || strnEQ (p, "null", 4); 334 2 336
|| strnEQ (p, "true", 4) || strnEQ (p, "false", 5) || strnEQ (p, "null", 4); 332 2 334
|| strnEQ (p, "true", 4) || strnEQ (p, "false", 5) || strnEQ (p, "null", 4); 2 330 332
2196 }
2197  
2198 static void
2199 661 incr_parse (JSON *self)
2200 {
2201 661 const char *p = SvPVX (self->incr_text) + self->incr_pos;
2202  
2203 // the state machine here is a bit convoluted and could be simplified a lot
2204 // but this would make it slower, so...
2205  
2206 for (;;)
2207 {
2208 670 switch (self->incr_mode)
2209 {
2210 // reached end of a scalar, see if we are inside a nested structure or not
2211 2029 end_of_scalar:
2212 2029 self->incr_mode = INCR_M_JSON;
2213  
2214 2029 ▶ if (self->incr_nest) // end of a scalar inside array, object or tag
Branch: 1 branch
BranchTrueFalseTotal
if (self->incr_nest) // end of a scalar inside array, object or tag 1957 72 2029
2215 1957 goto incr_m_json;
2216 else // end of scalar outside structure, json text ends here
2217 72 goto interrupt;
2218  
2219 // only used for initial whitespace skipping
2220 1103 case INCR_M_WS:
2221 for (;;)
2222 {
2223 1103 ▶ if (*p > 0x20)
Branch: 1 branch
BranchTrueFalseTotal
if (*p > 0x20) 580 523 1103
2224 {
2225 580 ▶ if (*p == '#')
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '#') 6 574 580
2226 {
2227 6 self->incr_mode = INCR_M_C0;
2228 6 goto incr_m_c;
2229 }
2230 else
2231 {
2232 574 self->incr_mode = INCR_M_JSON;
2233 574 goto incr_m_json;
2234 }
2235 }
2236 523 ▶ else if (!*p)
Branch: 1 branch
BranchTrueFalseTotal
else if (!*p) 83 440 523
2237 83 goto interrupt;
2238  
2239 440 ++p;
2240 }
2241  
2242 // skip a single char inside a string (for \\-processing)
2243 0 case INCR_M_BS:
2244 0 ▶ if (!*p)
Branch: 1 branch
BranchTrueFalseTotal
if (!*p) 0 0 0
2245 0 goto interrupt;
2246  
2247 0 ++p;
2248 0 self->incr_mode = INCR_M_STR;
2249 0 goto incr_m_str;
2250  
2251 // inside #-style comments
2252 case INCR_M_C0:
2253 case INCR_M_C1:
2254 9 incr_m_c:
2255 for (;;)
2256 {
2257 45 ▶ if (*p == '\n')
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\n') 9 36 45
2258 {
2259 9 ▶ self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON;
Branch: 1 branch
BranchTrueFalseTotal
self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON; 6 3 9
2260 9 break;
2261 }
2262 36 ▶ else if (!*p)
Branch: 1 branch
BranchTrueFalseTotal
else if (!*p) 0 36 36
2263 0 goto interrupt;
2264  
2265 36 ++p;
2266 }
2267  
2268 9 break;
2269  
2270 // inside true/false/null
2271 case INCR_M_TFN:
2272 76 incr_m_tfn:
2273 for (;;)
2274 306 ▶ switch (*p++)
Branch: 1 branch
BranchTrueFalseTotal
switch (*p++) 230 76 306
2275 {
2276 230 case 'r': case 'u': case 'e': // tRUE, falsE, nUll
2277 case 'a': case 'l': case 's': // fALSe, nuLL
2278 // allowed
2279 230 break;
2280
2281 76 default:
2282 76 --p;
2283 76 goto end_of_scalar;
2284 }
2285  
2286 // inside a number
2287 case INCR_M_NUM:
2288 790 incr_m_num:
2289 for (;;)
2290 982 ▶ switch (*p++)
Branch: 1 branch
BranchTrueFalseTotal
switch (*p++) 192 790 982
2291 {
2292 192 case 'e': case 'E': case '.': case '+':
2293 case '-':
2294 case '0': case '1': case '2': case '3': case '4':
2295 case '5': case '6': case '7': case '8': case '9':
2296 // allowed
2297 192 break;
2298  
2299 790 default:
2300 790 --p;
2301 790 goto end_of_scalar;
2302 }
2303  
2304 // inside a string
2305 case INCR_M_STR:
2306 1163 incr_m_str:
2307 for (;;)
2308 {
2309 4147 ▶ if (*p == '"')
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '"') 1163 2984 4147
2310 {
2311 1163 ++p;
2312 1163 goto end_of_scalar;
2313 }
2314 2984 ▶ else if (*p == '\\')
Branch: 1 branch
BranchTrueFalseTotal
else if (*p == '\\') 991 1993 2984
2315 {
2316 991 ++p; // "virtually" consumes character after a literal backslash
2317  
2318 991 ▶ if (!*p) // if at end of string we have to switch modes
Branch: 1 branch
BranchTrueFalseTotal
if (!*p) // if at end of string we have to switch modes 0 991 991
2319 {
2320 0 self->incr_mode = INCR_M_BS;
2321 0 goto interrupt;
2322 }
2323 }
2324 1993 ▶ else if (!*p)
Branch: 1 branch
BranchTrueFalseTotal
else if (!*p) 0 1993 1993
2325 0 goto interrupt;
2326  
2327 2984 ++p;
2328 }
2329  
2330 // after initial ws, outside string
2331 case INCR_M_JSON:
2332 2538 incr_m_json:
2333 for (;;)
2334 {
2335 6271 switch (*p++)
2336 {
2337 4 case 0:
2338 4 --p;
2339 4 goto interrupt;
2340  
2341 1361 case 0x09:
2342 case 0x0a:
2343 case 0x0d:
2344 case 0x20:
2345 1361 ▶ if (!self->incr_nest)
Branch: 1 branch
BranchTrueFalseTotal
if (!self->incr_nest) 0 1361 1361
2346 {
2347 0 --p; // do not eat the whitespace, let the next round do it
2348 0 goto interrupt;
2349 }
2350 1361 break;
2351  
2352 // the following three blocks handle scalars. this makes the parser
2353 // more strict than required inside arrays or objects, and could
2354 // be moved to a special case on the toplevel (except strings)
2355 76 case 't':
2356 case 'f':
2357 case 'n':
2358 76 self->incr_mode = INCR_M_TFN;
2359 76 goto incr_m_tfn;
2360  
2361 790 case '-':
2362 case '0': case '1': case '2': case '3': case '4':
2363 case '5': case '6': case '7': case '8': case '9':
2364 790 self->incr_mode = INCR_M_NUM;
2365 790 goto incr_m_num;
2366  
2367 1163 case '"':
2368 1163 self->incr_mode = INCR_M_STR;
2369 1163 goto incr_m_str;
2370  
2371 704 case '[':
2372 case '{':
2373 case '(':
2374 704 ▶ if (++self->incr_nest > self->max_depth)
Branch: 1 branch
BranchTrueFalseTotal
if (++self->incr_nest > self->max_depth) 2 702 704
2375 2 croak (ERR_NESTING_EXCEEDED);
2376 702 break;
2377  
2378 698 case ']':
2379 case '}':
2380 698 ▶ if (--self->incr_nest <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (--self->incr_nest <= 0) 500 198 698
2381 500 goto interrupt;
2382 198 break;
2383  
2384 0 case ')':
2385 0 --self->incr_nest;
2386 0 break;
2387  
2388 3 case '#':
2389 3 self->incr_mode = INCR_M_C1;
2390 3 goto incr_m_c;
2391 }
2392 }
2393 }
2394  
2395 /* modechange:
2396 ;*/
2397 }
2398  
2399 659 interrupt:
2400 659 self->incr_pos = p - SvPVX (self->incr_text);
2401 //printf ("interrupt<%.*s>\n", self->incr_pos, SvPVX(self->incr_text));//D
2402 //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D
2403 659 }
2404  
2405 /*
2406 * Either supply two source SVs, or two NULLs to restore defaults.
2407 * Each non-NULL self->v_* field owns one reference.
2408 */
2409 static void
2410 8 json_set_boolean_values (pTHX_ JSON *self,
2411 SV *src_false, SV *src_true,
2412 int core_bools)
2413 {
2414 8 SV *new_false = NULL;
2415 8 SV *new_true = NULL;
2416 SV *old_false;
2417 SV *old_true;
2418  
2419 8 ENTER;
2420 8 SAVETMPS;
2421  
2422 /*
2423 * Protect BOTH inputs before copying either one. Copying a magical
2424 * input may execute Perl code, including another setter call.
2425 */
2426 8 ▶ if (src_false)
Branch: 1 branch
BranchTrueFalseTotal
if (src_false) 7 1 8
2427 7 SAVEFREESV (SvREFCNT_inc (src_false));
2428 8 ▶ if (src_true)
Branch: 1 branch
BranchTrueFalseTotal
if (src_true) 7 1 8
2429 7 SAVEFREESV (SvREFCNT_inc (src_true));
2430  
2431 /*
2432 * Mortalize the destination BEFORE copying into it, so a croak
2433 * during get-magic cannot strand an unowned destination scalar.
2434 */
2435 8 ▶ if (src_false)
Branch: 1 branch
BranchTrueFalseTotal
if (src_false) 7 1 8
2436 {
2437 7 new_false = sv_2mortal (newSV (0));
2438 7 sv_setsv (new_false, src_false);
2439 }
2440  
2441 8 ▶ if (src_true)
Branch: 1 branch
BranchTrueFalseTotal
if (src_true) 7 1 8
2442 {
2443 7 new_true = sv_2mortal (newSV (0));
2444 7 sv_setsv (new_true, src_true);
2445 }
2446  
2447 /*
2448 * Read the old fields only after copying: input magic may have
2449 * re-entered the setter and changed them.
2450 */
2451 8 old_false = self->v_false;
2452 8 old_true = self->v_true;
2453  
2454 /*
2455 * Publish the complete replacement before releasing anything that
2456 * might invoke DESTROY and re-enter Perl.
2457 */
2458 8 ▶ self->v_false = new_false ? SvREFCNT_inc (new_false) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
self->v_false = new_false ? SvREFCNT_inc (new_false) : NULL; 7 1 8
2459 8 ▶ self->v_true = new_true ? SvREFCNT_inc (new_true) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
self->v_true = new_true ? SvREFCNT_inc (new_true) : NULL; 7 1 8
2460  
2461 8 ▶ if (core_bools)
Branch: 1 branch
BranchTrueFalseTotal
if (core_bools) 3 5 8
2462 3 self->flags |= F_CORE_BOOLS;
2463 else
2464 5 self->flags &= ~F_CORE_BOOLS;
2465  
2466 /*
2467 * Transfer the old fields' owned references to scope cleanup.
2468 * Neither SAVEFREESV call increments the reference count.
2469 */
2470 8 ▶ if (old_false)
Branch: 1 branch
BranchTrueFalseTotal
if (old_false) 2 6 8
2471 2 SAVEFREESV (old_false);
2472 8 ▶ if (old_true)
Branch: 1 branch
BranchTrueFalseTotal
if (old_true) 2 6 8
2473 2 SAVEFREESV (old_true);
2474  
2475 8 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 7 1 8
2476 8 LEAVE;
2477 8 }
2478  
2479  
2480 /////////////////////////////////////////////////////////////////////////////
2481 // XS interface functions
2482  
2483 MODULE = JSON::SIMD PACKAGE = JSON::SIMD
2484  
2485 BOOT:
2486 {
2487 int i;
2488  
2489 11565 ▶ for (i = 0; i < 256; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 256; ++i) 11520 45 11565
2490 36360 ▶ decode_hexdigit [i] =
Branch: 3 branches
BranchTrueFalseTotal
decode_hexdigit [i] = 9360 2160 11520
decode_hexdigit [i] = 7155 3915 11070
decode_hexdigit [i] = 8325 2475 10800
2491 9360 ▶ i >= '0' && i <= '9' ? i - '0'
Branch: 1 branch
BranchTrueFalseTotal
i >= '0' && i <= '9' ? i - '0' 450 8910 9360
2492 7155 ▶ : i >= 'a' && i <= 'f' ? i - 'a' + 10
Branch: 1 branch
BranchTrueFalseTotal
: i >= 'a' && i <= 'f' ? i - 'a' + 10 270 6885 7155
2493 8325 ▶ : i >= 'A' && i <= 'F' ? i - 'A' + 10
Branch: 1 branch
BranchTrueFalseTotal
: i >= 'A' && i <= 'F' ? i - 'A' + 10 270 8055 8325
2494 : -1;
2495  
2496 45 json_stash = gv_stashpv ("JSON::SIMD" , 1);
2497 45 bool_stash = gv_stashpv ("Types::Serialiser::Boolean", 1);
2498 45 bool_false = get_bool ("Types::Serialiser::false");
2499 45 bool_true = get_bool ("Types::Serialiser::true");
2500  
2501 45 sv_json = newSVpv ("JSON", 0);
2502 45 SvREADONLY_on (sv_json);
2503  
2504 45 simdjson_global_init();
2505  
2506 45 CvNODEBUG_on (get_cv ("JSON::SIMD::incr_text", 0)); /* the debugger completely breaks lvalue subs */
2507 }
2508  
2509 PROTOTYPES: DISABLE
2510  
2511 void CLONE (...)
2512 CODE:
2513 // as long as these writes are atomic, the race should not matter
2514 // as existing threads either already use 0, or use the old value,
2515 // which is sitll correct for the initial thread.
2516 0 json_stash = 0;
2517 0 bool_stash = 0;
2518 0 bool_false = 0;
2519 0 bool_true = 0;
2520  
2521 void new (char *klass)
2522 PPCODE:
2523 {
2524 86286 SV *pv = NEWSV (0, sizeof (JSON));
2525 86286 SvPOK_only (pv);
2526 86286 json_init ((JSON *)SvPVX (pv));
2527 86286 ▶ XPUSHs (sv_2mortal (sv_bless (
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs (sv_2mortal (sv_bless ( 0 86286 86286
XPUSHs (sv_2mortal (sv_bless ( 0 86286 86286
2528 newRV_noinc (pv),
2529 strEQ (klass, "JSON::SIMD") ? JSON_STASH : gv_stashpv (klass, 1)
2530 )));
2531 }
2532  
2533 void boolean_values (JSON *self, SV *v_false = 0, SV *v_true = 0)
2534 PPCODE:
2535 {
2536 /* JSON::XS used to accept a single argument and silently set v_true to null. */
2537 5 ▶ if (v_false && !v_true)
Branch: 2 branches
BranchTrueFalseTotal
if (v_false && !v_true) 5 0 5
if (v_false && !v_true) 1 4 5
2538 1 croak ("boolean_values requires either zero or two arguments");
2539  
2540 4 PUTBACK;
2541 4 json_set_boolean_values (aTHX_ self, v_false, v_true, 0);
2542 4 SPAGAIN;
2543  
2544 4 ▶ XPUSHs (ST (0));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (ST (0)); 0 4 4
2545 }
2546  
2547 void get_boolean_values (JSON *self)
2548 PPCODE:
2549 5 ▶ if (self->v_false && self->v_true)
Branch: 2 branches
BranchTrueFalseTotal
if (self->v_false && self->v_true) 5 0 5
if (self->v_false && self->v_true) 5 0 5
2550 {
2551 5 ▶ EXTEND (SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND (SP, 2); 0 5 5
2552 5 PUSHs (self->v_false);
2553 5 PUSHs (self->v_true);
2554 }
2555  
2556  
2557 void core_bools (JSON *self, int enable = 1)
2558 PPCODE:
2559 {
2560 4 PUTBACK;
2561 4 ▶ json_set_boolean_values (
Branch: 2 branches
BranchTrueFalseTotal
json_set_boolean_values ( 3 1 4
json_set_boolean_values ( 3 1 4
2562 aTHX_ self,
2563 enable ? &PL_sv_no : NULL,
2564 enable ? &PL_sv_yes : NULL,
2565 enable
2566 );
2567 4 SPAGAIN;
2568  
2569 4 ▶ XPUSHs (ST (0));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (ST (0)); 0 4 4
2570 }
2571  
2572 void get_core_bools (JSON *self)
2573 PPCODE:
2574 {
2575 5 int result = self->flags & F_CORE_BOOLS;
2576 #if PERL_VERSION_GE(5,36,0)
2577 5 ▶ if (self->v_false && self->v_true && SvIsBOOL(self->v_false) && SvIsBOOL(self->v_true))
Branch: 4 branches
BranchTrueFalseTotal
if (self->v_false && self->v_true && SvIsBOOL(self->v_false) && SvIsBOOL(self->v_true)) 5 0 5
if (self->v_false && self->v_true && SvIsBOOL(self->v_false) && SvIsBOOL(self->v_true)) 5 0 5
if (self->v_false && self->v_true && SvIsBOOL(self->v_false) && SvIsBOOL(self->v_true)) 2 3 5
if (self->v_false && self->v_true && SvIsBOOL(self->v_false) && SvIsBOOL(self->v_true)) 2 0 2
2578 {
2579 2 result = F_CORE_BOOLS;
2580 }
2581 #endif
2582 5 ▶ XPUSHs (boolSV (result));
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs (boolSV (result)); 0 5 5
XPUSHs (boolSV (result)); 2 3 5
2583 }
2584  
2585 void ascii (JSON *self, int enable = 1)
2586 ALIAS:
2587 ascii = F_ASCII
2588 latin1 = F_LATIN1
2589 utf8 = F_UTF8
2590 indent = F_INDENT
2591 canonical = F_CANONICAL
2592 space_before = F_SPACE_BEFORE
2593 space_after = F_SPACE_AFTER
2594 pretty = F_PRETTY
2595 allow_nonref = F_ALLOW_NONREF
2596 shrink = F_SHRINK
2597 allow_blessed = F_ALLOW_BLESSED
2598 convert_blessed = F_CONV_BLESSED
2599 relaxed = F_RELAXED
2600 allow_unknown = F_ALLOW_UNKNOWN
2601 allow_tags = F_ALLOW_TAGS
2602 use_simdjson = F_USE_SIMDJSON
2603 encode_core_bools = F_ENCODE_CORE_BOOLS
2604 PPCODE:
2605 {
2606 196921 ▶ if (enable)
Branch: 1 branch
BranchTrueFalseTotal
if (enable) 123099 73822 196921
2607 123099 self->flags |= ix;
2608 else
2609 73822 self->flags &= ~ix;
2610  
2611 196921 ▶ if (self->flags & F_USE_SIMDJSON && self->flags & F_ALLOW_TAGS)
Branch: 2 branches
BranchTrueFalseTotal
if (self->flags & F_USE_SIMDJSON && self->flags & F_ALLOW_TAGS) 76980 119941 196921
if (self->flags & F_USE_SIMDJSON && self->flags & F_ALLOW_TAGS) 5 76975 76980
2612 5 self->flags &= ~F_USE_SIMDJSON;
2613 196921 ▶ if (self->flags & F_USE_SIMDJSON && self->flags & F_RELAXED)
Branch: 2 branches
BranchTrueFalseTotal
if (self->flags & F_USE_SIMDJSON && self->flags & F_RELAXED) 76975 119946 196921
if (self->flags & F_USE_SIMDJSON && self->flags & F_RELAXED) 17 76958 76975
2614 17 self->flags &= ~F_USE_SIMDJSON;
2615 196921 ▶ if (ix == F_ALLOW_NONREF)
Branch: 1 branch
BranchTrueFalseTotal
if (ix == F_ALLOW_NONREF) 49242 147679 196921
2616 49242 self->flags |= F_NONREF_SET;
2617  
2618 196921 ▶ XPUSHs (ST (0));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (ST (0)); 0 196921 196921
2619 }
2620  
2621 void get_ascii (JSON *self)
2622 ALIAS:
2623 get_ascii = F_ASCII
2624 get_latin1 = F_LATIN1
2625 get_utf8 = F_UTF8
2626 get_indent = F_INDENT
2627 get_canonical = F_CANONICAL
2628 get_space_before = F_SPACE_BEFORE
2629 get_space_after = F_SPACE_AFTER
2630 get_allow_nonref = F_ALLOW_NONREF
2631 get_shrink = F_SHRINK
2632 get_allow_blessed = F_ALLOW_BLESSED
2633 get_convert_blessed = F_CONV_BLESSED
2634 get_relaxed = F_RELAXED
2635 get_allow_unknown = F_ALLOW_UNKNOWN
2636 get_allow_tags = F_ALLOW_TAGS
2637 get_use_simdjson = F_USE_SIMDJSON
2638 get_encode_core_bools = F_ENCODE_CORE_BOOLS
2639 PPCODE:
2640 10 ▶ XPUSHs (boolSV (self->flags & ix));
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs (boolSV (self->flags & ix)); 0 10 10
XPUSHs (boolSV (self->flags & ix)); 4 6 10
2641  
2642 void max_depth (JSON *self, U32 max_depth = 0x80000000UL)
2643 PPCODE:
2644 11 self->max_depth = max_depth;
2645 11 ▶ XPUSHs (ST (0));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (ST (0)); 0 11 11
2646  
2647 U32 get_max_depth (JSON *self)
2648 CODE:
2649 0 ▶ RETVAL = self->max_depth;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->max_depth; 0 0 0
2650 OUTPUT:
2651 RETVAL
2652  
2653 void max_size (JSON *self, U32 max_size = 0)
2654 PPCODE:
2655 10 self->max_size = max_size;
2656 10 ▶ XPUSHs (ST (0));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (ST (0)); 0 10 10
2657  
2658 int get_max_size (JSON *self)
2659 CODE:
2660 0 ▶ RETVAL = self->max_size;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->max_size; 0 0 0
2661 OUTPUT:
2662 RETVAL
2663  
2664 void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
2665 PPCODE:
2666 {
2667 7 SvREFCNT_dec (self->cb_object);
2668 7 ▶ self->cb_object = SvOK (cb) ? newSVsv (cb) : 0;
Branch: 1 branch
BranchTrueFalseTotal
self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; 4 3 7
2669 7 ▶ if (self->cb_object) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->cb_object) { 4 3 7
2670 4 self->flags &= ~F_USE_SIMDJSON;
2671 }
2672  
2673 7 ▶ XPUSHs (ST (0));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (ST (0)); 0 7 7
2674 }
2675  
2676 void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef)
2677 PPCODE:
2678 {
2679 9 ▶ if (!self->cb_sk_object)
Branch: 1 branch
BranchTrueFalseTotal
if (!self->cb_sk_object) 4 5 9
2680 4 self->cb_sk_object = newHV ();
2681  
2682 9 ▶ if (SvOK (cb))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK (cb)) 6 3 9
2683 6 hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0);
2684 else
2685 {
2686 3 hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0);
2687  
2688 3 ▶ if (!HvKEYS (self->cb_sk_object))
Branch: 2 branches
BranchTrueFalseTotal
if (!HvKEYS (self->cb_sk_object)) 0 3 3
if (!HvKEYS (self->cb_sk_object)) 2 1 3
2689 {
2690 2 SvREFCNT_dec (self->cb_sk_object);
2691 2 self->cb_sk_object = 0;
2692 }
2693 }
2694 9 ▶ if (self->cb_sk_object) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->cb_sk_object) { 7 2 9
2695 7 self->flags &= ~F_USE_SIMDJSON;
2696 }
2697  
2698 9 ▶ XPUSHs (ST (0));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (ST (0)); 0 9 9
2699 }
2700  
2701 void encode (JSON *self, SV *scalar)
2702 PPCODE:
2703 49824 PUTBACK; scalar = encode_json (scalar, self); SPAGAIN;
2704 49806 ▶ XPUSHs (scalar);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (scalar); 0 49806 49806
2705  
2706 void decode (JSON *self, SV *jsonstr)
2707 PPCODE:
2708 38447 PUTBACK; jsonstr = decode_json (jsonstr, self, 0, 0); SPAGAIN;
2709 37277 ▶ XPUSHs (jsonstr);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (jsonstr); 0 37277 37277
2710  
2711 void decode_prefix (JSON *self, SV *jsonstr)
2712 PPCODE:
2713 {
2714 SV *sv;
2715 STRLEN offset;
2716 34 PUTBACK; sv = decode_json (jsonstr, self, &offset, 0); SPAGAIN;
2717 32 ▶ EXTEND (SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND (SP, 2); 0 32 32
2718 32 PUSHs (sv);
2719 32 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, (U8*)(SvPV_nolen (jsonstr) + offset)))));
2720 }
2721  
2722 void decode_at_pointer (JSON *self, SV *jsonstr, SV *path)
2723 PPCODE:
2724 145 PUTBACK; jsonstr = decode_json (jsonstr, self, 0, path); SPAGAIN;
2725 65 ▶ XPUSHs (jsonstr);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (jsonstr); 0 65 65
2726  
2727 void incr_parse (JSON *self, SV *jsonstr = 0)
2728 PPCODE:
2729 {
2730 1324 ▶ if (!self->incr_text)
Branch: 1 branch
BranchTrueFalseTotal
if (!self->incr_text) 55 1269 1324
2731 55 self->incr_text = newSVpvn ("", 0);
2732  
2733 /* if utf8-ness doesn't match the decoder, need to upgrade/downgrade */
2734 1324 ▶ if (!DECODE_WANTS_OCTETS (self) == !SvUTF8 (self->incr_text))
Branch: 1 branch
BranchTrueFalseTotal
if (!DECODE_WANTS_OCTETS (self) == !SvUTF8 (self->incr_text)) 55 1269 1324
2735 {
2736 55 ▶ if (DECODE_WANTS_OCTETS (self))
Branch: 1 branch
BranchTrueFalseTotal
if (DECODE_WANTS_OCTETS (self)) 0 55 55
2737 {
2738 0 ▶ if (self->incr_pos)
Branch: 1 branch
BranchTrueFalseTotal
if (self->incr_pos) 0 0 0
2739 0 self->incr_pos = utf8_length ((U8 *)SvPVX (self->incr_text),
2740 (U8 *)SvPVX (self->incr_text) + self->incr_pos);
2741  
2742 0 sv_utf8_downgrade (self->incr_text, 0);
2743 }
2744 else
2745 {
2746 55 sv_utf8_upgrade (self->incr_text);
2747  
2748 55 ▶ if (self->incr_pos)
Branch: 1 branch
BranchTrueFalseTotal
if (self->incr_pos) 0 55 55
2749 0 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
2750 0 - (U8 *)SvPVX (self->incr_text);
2751 }
2752 }
2753  
2754 // append data, if any
2755 1324 ▶ if (jsonstr)
Branch: 1 branch
BranchTrueFalseTotal
if (jsonstr) 699 625 1324
2756 {
2757 /* make sure both strings have same encoding */
2758 699 ▶ if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text))
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text)) 697 2 699
2759 {
2760 697 ▶ if (SvUTF8 (jsonstr))
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8 (jsonstr)) 0 697 697
2761 0 sv_utf8_downgrade (jsonstr, 0);
2762 else
2763 697 sv_utf8_upgrade (jsonstr);
2764 }
2765  
2766 /* and then just blindly append */
2767 {
2768 STRLEN len;
2769 699 const char *str = SvPV (jsonstr, len);
2770 699 STRLEN cur = SvCUR (self->incr_text);
2771  
2772 699 ▶ if (SvLEN (self->incr_text) - cur <= len)
Branch: 1 branch
BranchTrueFalseTotal
if (SvLEN (self->incr_text) - cur <= len) 301 398 699
2773 301 json_sv_grow (self->incr_text, cur, len);
2774  
2775 699 Move (str, SvEND (self->incr_text), len, char);
2776 699 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len);
2777 699 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there
2778 }
2779 }
2780  
2781 1324 ▶ if (GIMME_V == G_VOID && expect_false(self->max_size && SvCUR (self->incr_text) > self->max_size))
Branch: 4 branches
BranchTrueFalseTotal
if (GIMME_V == G_VOID && expect_false(self->max_size && SvCUR (self->incr_text) > self->max_size)) 669 655 1324
if (GIMME_V == G_VOID && expect_false(self->max_size && SvCUR (self->incr_text) > self->max_size)) 2 667 669
if (GIMME_V == G_VOID && expect_false(self->max_size && SvCUR (self->incr_text) > self->max_size)) 2 0 2
if (GIMME_V == G_VOID && expect_false(self->max_size && SvCUR (self->incr_text) > self->max_size)) 2 667 669
2782 2 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
2783 (unsigned long)SvCUR (self->incr_text), (unsigned long)self->max_size);
2784  
2785 1322 ▶ if (GIMME_V != G_VOID)
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V != G_VOID) 655 667 1322
2786 do
2787 {
2788 SV *sv;
2789 STRLEN offset;
2790  
2791 663 ▶ if (!INCR_DONE (self))
Branch: 2 branches
BranchTrueFalseTotal
if (!INCR_DONE (self)) 659 4 663
if (!INCR_DONE (self)) 657 2 659
2792 {
2793 661 incr_parse (self);
2794  
2795 659 ▶ if (expect_false(self->max_size && INCR_SIZE (self) > self->max_size))
Branch: 5 branches
BranchTrueFalseTotal
if (expect_false(self->max_size && INCR_SIZE (self) > self->max_size)) 6 653 659
if (expect_false(self->max_size && INCR_SIZE (self) > self->max_size)) 4 2 6
if (expect_false(self->max_size && INCR_SIZE (self) > self->max_size)) 2 2 4
if (expect_false(self->max_size && INCR_SIZE (self) > self->max_size)) 4 2 6
if (expect_false(self->max_size && INCR_SIZE (self) > self->max_size)) 4 655 659
2796 4 ▶ croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
Branch: 2 branches
BranchTrueFalseTotal
croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", 4 0 4
croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", 2 2 4
2797 (unsigned long)INCR_SIZE (self), (unsigned long)self->max_size);
2798  
2799 655 ▶ if (!INCR_DONE (self))
Branch: 2 branches
BranchTrueFalseTotal
if (!INCR_DONE (self)) 651 4 655
if (!INCR_DONE (self)) 81 570 651
2800 {
2801 // as an optimisation, do not accumulate white space in the incr buffer
2802 85 ▶ if (self->incr_mode == INCR_M_WS && self->incr_pos)
Branch: 2 branches
BranchTrueFalseTotal
if (self->incr_mode == INCR_M_WS && self->incr_pos) 81 4 85
if (self->incr_mode == INCR_M_WS && self->incr_pos) 5 76 81
2803 {
2804 5 self->incr_pos = 0;
2805 5 SvCUR_set (self->incr_text, 0);
2806 }
2807  
2808 85 break;
2809 }
2810 }
2811  
2812 // manually consume trailing whitespace, to work around simdjson
2813 572 ▶ if (self->flags & F_USE_SIMDJSON)
Branch: 1 branch
BranchTrueFalseTotal
if (self->flags & F_USE_SIMDJSON) 282 290 572
2814 {
2815 282 char *p = SvPVX (self->incr_text) + self->incr_pos;
2816 388 ▶ while (p && (*p == 0x20 || *p == 0x0a ||*p == 0x0d || *p == 0x09))
Branch: 5 branches
BranchTrueFalseTotal
while (p && (*p == 0x20 || *p == 0x0a ||*p == 0x0d || *p == 0x09)) 388 0 388
while (p && (*p == 0x20 || *p == 0x0a ||*p == 0x0d || *p == 0x09)) 106 282 388
while (p && (*p == 0x20 || *p == 0x0a ||*p == 0x0d || *p == 0x09)) 0 282 282
while (p && (*p == 0x20 || *p == 0x0a ||*p == 0x0d || *p == 0x09)) 0 282 282
while (p && (*p == 0x20 || *p == 0x0a ||*p == 0x0d || *p == 0x09)) 0 282 282
2817 {
2818 106 p++;
2819 106 self->incr_pos++;
2820 }
2821 }
2822  
2823 572 ▶ if (!(self->flags & F_NONREF_SET) && incr_starts_bare_scalar (self))
Branch: 2 branches
BranchTrueFalseTotal
if (!(self->flags & F_NONREF_SET) && incr_starts_bare_scalar (self)) 340 232 572
if (!(self->flags & F_NONREF_SET) && incr_starts_bare_scalar (self)) 10 330 340
2824 10 croak ("incremental parsing of a top-level number, true, false or null needs an explicit allow_nonref");
2825  
2826 562 SV *input = self->incr_text;
2827  
2828 562 ▶ if (self->flags & F_USE_SIMDJSON)
Branch: 1 branch
BranchTrueFalseTotal
if (self->flags & F_USE_SIMDJSON) 277 285 562
2829 {
2830 277 STRLEN len = self->incr_pos;
2831  
2832 /* Retain the existing whole-buffer size restriction. */
2833 277 ▶ if (self->max_size && SvCUR(input) > self->max_size)
Branch: 2 branches
BranchTrueFalseTotal
if (self->max_size && SvCUR(input) > self->max_size) 0 277 277
if (self->max_size && SvCUR(input) > self->max_size) 0 0 0
2834 0 croak("incremental input exceeds max_size");
2835  
2836 277 ▶ if (!len || len > SvCUR(input))
Branch: 2 branches
BranchTrueFalseTotal
if (!len || len > SvCUR(input)) 277 0 277
if (!len || len > SvCUR(input)) 0 277 277
2837 0 croak("invalid incremental document boundary");
2838  
2839 /*
2840 * Copy this document, not the unread suffix.
2841 * The existing decoder supplies any required SIMD padding.
2842 * This is wasteful and likely eats any performance
2843 * we would have gained from simdjson. The alternative
2844 * would have been making the simdjson wrapper intimately
2845 * aware of incremental parsing state, hurting the normal
2846 * hot path performance.
2847 * Not doing anything meant a quadratic performance breakdown.
2848 */
2849 277 input = sv_2mortal(newSVpvn(SvPVX(self->incr_text), len));
2850  
2851 277 ▶ if (SvUTF8(self->incr_text))
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(self->incr_text)) 277 0 277
2852 277 SvUTF8_on(input);
2853 }
2854  
2855 562 PUTBACK;
2856 562 sv = decode_json (input, self, &offset, 0);
2857 556 SPAGAIN;
2858  
2859  
2860 556 ▶ XPUSHs (sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (sv); 0 556 556
2861  
2862 556 self->incr_pos -= offset;
2863 556 self->incr_nest = 0;
2864 556 self->incr_mode = 0;
2865  
2866 556 sv_chop (self->incr_text, SvPVX (self->incr_text) + offset);
2867 }
2868 556 ▶ while (GIMME_V == G_LIST);
Branch: 1 branch
BranchTrueFalseTotal
while (GIMME_V == G_LIST); 8 548 556
2869 }
2870  
2871 SV *incr_text (JSON *self)
2872 ATTRS: lvalue
2873 CODE:
2874 {
2875 608 ▶ if (self->incr_pos)
Branch: 1 branch
BranchTrueFalseTotal
if (self->incr_pos) 0 608 608
2876 0 croak ("incr_text can not be called when the incremental parser already started parsing");
2877  
2878 608 ▶ RETVAL = self->incr_text ? SvREFCNT_inc (self->incr_text) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->incr_text ? SvREFCNT_inc (self->incr_text) : &PL_sv_undef; 608 0 608
2879 }
2880 OUTPUT:
2881 RETVAL
2882  
2883 void incr_skip (JSON *self)
2884 CODE:
2885 {
2886 4 ▶ if (self->incr_pos)
Branch: 1 branch
BranchTrueFalseTotal
if (self->incr_pos) 4 0 4
2887 {
2888 8 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + self->incr_pos);
2889 4 self->incr_pos = 0;
2890 4 self->incr_nest = 0;
2891 4 self->incr_mode = 0;
2892 }
2893 }
2894  
2895 void incr_reset (JSON *self)
2896 CODE:
2897 {
2898 0 SvREFCNT_dec (self->incr_text);
2899 0 self->incr_text = 0;
2900 0 self->incr_pos = 0;
2901 0 self->incr_nest = 0;
2902 0 self->incr_mode = 0;
2903 }
2904  
2905 void DESTROY (JSON *self)
2906 CODE:
2907 86286 SvREFCNT_dec (self->v_false);
2908 86286 SvREFCNT_dec (self->v_true);
2909 86286 SvREFCNT_dec (self->cb_sk_object);
2910 86286 SvREFCNT_dec (self->cb_object);
2911 86286 SvREFCNT_dec (self->incr_text);
2912  
2913 PROTOTYPES: ENABLE
2914  
2915 void encode_json (SV *scalar)
2916 PPCODE:
2917 {
2918 JSON json;
2919 323 json_init (&json);
2920 323 json.flags |= F_UTF8;
2921 323 PUTBACK; scalar = encode_json (scalar, &json); SPAGAIN;
2922 323 ▶ XPUSHs (scalar);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (scalar); 0 323 323
2923 }
2924  
2925 void decode_json (SV *jsonstr)
2926 PPCODE:
2927 {
2928 JSON json;
2929 12346 json_init (&json);
2930 12346 json.flags |= F_UTF8;
2931 12346 PUTBACK; jsonstr = decode_json (jsonstr, &json, 0, 0); SPAGAIN;
2932 12340 ▶ XPUSHs (jsonstr);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (jsonstr); 0 12340 12340
2933 }
2934  
2935 void simdjson_version ()
2936 PPCODE:
2937 {
2938 SV *version_info;
2939 0 PUTBACK; version_info = simdjson_get_version(); SPAGAIN;
2940 0 ▶ XPUSHs (version_info);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (version_info); 0 0 0
2941 }
2942  
2943 void is_core_bool (SV *scalar)
2944 PPCODE:
2945 {
2946 #if PERL_VERSION_GE(5,36,0)
2947 6 ▶ XPUSHs( boolSV( SvIsBOOL( scalar )));
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs( boolSV( SvIsBOOL( scalar ))); 0 6 6
XPUSHs( boolSV( SvIsBOOL( scalar ))); 4 2 6
2948 #else
2949 XPUSHs( boolSV( 0 ));
2950 #endif
2951 }
« simdjson.h ↑ Summary