file_raw_json.c

filter: Statementstmt 82.8% 392 / 473 Branchbran 57.7% 245 / 424 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 71.0% 637 / 897 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« yyjson.c ↑ Summary include/yyjson.h »
1 /*
2 * file_raw_json.c - File::Raw::JSON value-mapping + JSONL brace-balancer
3 */
4  
5 #include "file_raw_json.h"
6  
7 #include <string.h>
8 #include <stdio.h>
9 #include <math.h>
10 #include <limits.h>
11 #include "tie_orderedhash.h"
12  
13 /* ============================================================
14 * Defaults
15 * ============================================================ */
16  
17 void
18 355 json_options_defaults(json_options_t *o)
19 {
20 355 o->mode = JSON_MODE_DOCUMENT;
21 355 o->pretty = 0;
22 355 o->indent = 2;
23 355 o->sort_keys = 0;
24 355 o->canonical = 0;
25 355 o->utf8 = 1;
26 355 o->relaxed = 0;
27 355 o->allow_nonref = 1;
28 355 o->allow_nan_inf = 0;
29 355 o->ordered = 0;
30 355 o->max_depth = 512;
31 355 o->eol[0] = '\n';
32 355 o->eol[1] = '\0';
33 355 o->eol_len = 1;
34 355 }
35  
36 /* ============================================================
37 * JSONL brace-balancer
38 *
39 * Walk a buffer, find the byte range of the next balanced top-level
40 * JSON value (object or array). Skip leading whitespace; track string
41 * state and escape state; bail on truncation with NEED_MORE; bail on
42 * a non-opener with NO_OPENER.
43 *
44 * Equivalent to JSON::Lines's recursive Perl regex but:
45 * - O(n) byte scan with no backtracking
46 * - no PCRE2 dep
47 * - resumable: caller can retry with a longer buffer after NEED_MORE
48 * ============================================================ */
49  
50 jsonl_scan_t
51 6137 json_jsonl_next(const char *buf, STRLEN len,
52 STRLEN *out_start, STRLEN *out_end, STRLEN *next_pos)
53 {
54 6137 STRLEN i = 0;
55 6137 int depth = 0;
56 6137 int in_string = 0;
57 6137 int prev_backslash = 0;
58 char c;
59  
60 /* Skip ASCII whitespace before the value. */
61 6143 ▶ while (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < len) { 6142 1 6143
62 6142 c = buf[i];
63 6142 ▶ if (c != ' ' && c != '\t' && c != '\n' && c != '\r') break;
Branch: 4 branches
BranchTrueFalseTotal
if (c != ' ' && c != '\t' && c != '\n' && c != '\r') break; 6139 3 6142
if (c != ' ' && c != '\t' && c != '\n' && c != '\r') break; 6139 0 6139
if (c != ' ' && c != '\t' && c != '\n' && c != '\r') break; 6136 3 6139
if (c != ' ' && c != '\t' && c != '\n' && c != '\r') break; 6136 0 6136
64 6 i++;
65 }
66 6137 ▶ if (i >= len) {
Branch: 1 branch
BranchTrueFalseTotal
if (i >= len) { 1 6136 6137
67 1 *out_start = i;
68 1 *out_end = i;
69 1 *next_pos = i;
70 1 return JSONL_NO_OPENER; /* only whitespace */
71 }
72  
73 6136 c = buf[i];
74 6136 ▶ if (c != '[' && c != '{') {
Branch: 2 branches
BranchTrueFalseTotal
if (c != '[' && c != '{') { 6120 16 6136
if (c != '[' && c != '{') { 0 6120 6120
75 /* Non-opener: caller decides whether to skip or croak. */
76 0 *out_start = i;
77 0 *out_end = i;
78 0 *next_pos = i;
79 0 return JSONL_NO_OPENER;
80 }
81  
82 6136 *out_start = i;
83 6136 depth = 1;
84 6136 i++;
85  
86 265539 ▶ while (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < len) { 265534 5 265539
87 265534 c = buf[i];
88 265534 ▶ if (in_string) {
Branch: 1 branch
BranchTrueFalseTotal
if (in_string) { 166139 99395 265534
89 166139 ▶ if (prev_backslash) {
Branch: 1 branch
BranchTrueFalseTotal
if (prev_backslash) { 8 166131 166139
90 8 prev_backslash = 0;
91 166131 ▶ } else if (c == '\\') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == '\\') { 8 166123 166131
92 8 prev_backslash = 1;
93 166123 ▶ } else if (c == '"') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == '"') { 26276 139847 166123
94 26276 in_string = 0;
95 }
96 166139 i++;
97 166139 continue;
98 }
99 99395 switch (c) {
100 2034 case '[': case '{':
101 2034 depth++;
102 2034 break;
103 8163 case ']': case '}':
104 8163 depth--;
105 8163 ▶ if (depth == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (depth == 0) { 6131 2032 8163
106 6131 *out_end = i + 1;
107 /* Skip trailing whitespace so next_pos points at the
108 * next value (or end of buffer). */
109 6131 i++;
110 12235 ▶ while (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < len) { 12186 49 12235
111 12186 char w = buf[i];
112 12186 ▶ if (w != ' ' && w != '\t' && w != '\n' && w != '\r')
Branch: 4 branches
BranchTrueFalseTotal
if (w != ' ' && w != '\t' && w != '\n' && w != '\r') 12180 6 12186
if (w != ' ' && w != '\t' && w != '\n' && w != '\r') 12180 0 12180
if (w != ' ' && w != '\t' && w != '\n' && w != '\r') 6082 6098 12180
if (w != ' ' && w != '\t' && w != '\n' && w != '\r') 6082 0 6082
113 6082 break;
114 6104 i++;
115 }
116 6131 *next_pos = i;
117 6131 return JSONL_FOUND;
118 }
119 2032 break;
120 26279 case '"':
121 26279 in_string = 1;
122 26279 break;
123 62919 default:
124 /* literal data */
125 62919 break;
126 }
127 93264 i++;
128 }
129  
130 /* Hit EOF mid-value: caller must buffer the tail and retry. */
131 5 return JSONL_NEED_MORE;
132 }
133  
134 /* ============================================================
135 * Value mapping: yyjson_val -> Perl SV
136 *
137 * Recursive walker. Caller owns the returned SV (refcount 1).
138 * boolean_stash, if non-NULL, is the HV* of the class to bless
139 * true/false sentinels into.
140 * ============================================================ */
141  
142 static SV *make_bool_sv(pTHX_ int truth, HV *stash);
143 static HV *make_ordered_hv(pTHX);
144 static void ordered_hv_set(pTHX_ HV *hv, const char *key, STRLEN klen, SV *val);
145  
146 /* Internal recursive walker. Threads `depth` so we can enforce
147 * `max_depth` (the public option) without yyjson cooperation. */
148 static SV *
149 50200 sv_from_yyjson_d(pTHX_ yyjson_val *val, HV *boolean_stash,
150 int ordered, int depth, int max_depth)
151 {
152 yyjson_type t;
153 yyjson_subtype st;
154  
155 50200 ▶ if (!val) return newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (!val) return newSV(0); 0 50200 50200
156  
157 50200 ▶ t = yyjson_get_type(val);
Branch: 1 branch
BranchTrueFalseTotal
t = yyjson_get_type(val); 50200 0 50200
158 50200 st = yyjson_get_subtype(val);
159  
160 /* Depth check fires when we *enter* a container, so primitives
161 * one level past the cap still parse - matches what callers
162 * intuitively expect from "max_depth = N nested levels". */
163 50200 ▶ if ((t == YYJSON_TYPE_ARR || t == YYJSON_TYPE_OBJ) && depth >= max_depth) {
Branch: 3 branches
BranchTrueFalseTotal
if ((t == YYJSON_TYPE_ARR || t == YYJSON_TYPE_OBJ) && depth >= max_depth) { 46717 3483 50200
if ((t == YYJSON_TYPE_ARR || t == YYJSON_TYPE_OBJ) && depth >= max_depth) { 9511 37206 46717
if ((t == YYJSON_TYPE_ARR || t == YYJSON_TYPE_OBJ) && depth >= max_depth) { 1 12993 12994
164 1 croak("File::Raw::JSON: max_depth (%d) exceeded during decode",
165 max_depth);
166 }
167  
168 50199 switch (t) {
169 21 case YYJSON_TYPE_NULL:
170 21 return newSV(0);
171  
172 24 ▶ case YYJSON_TYPE_BOOL: {
Branch: 1 branch
BranchTrueFalseTotal
case YYJSON_TYPE_BOOL: { 24 0 24
173 /* Inline fast path for the default boolean class (~99% of
174 * decodes): pointer-compare the stash and bump the pre-
175 * built singleton's refcount. Saves a function call to
176 * make_bool_sv per boolean. */
177 24 int truth = yyjson_get_bool(val);
178 24 ▶ if (boolean_stash == g_frj_default_stash) {
Branch: 1 branch
BranchTrueFalseTotal
if (boolean_stash == g_frj_default_stash) { 22 2 24
179 22 ▶ SV *s = truth ? g_frj_true_sv : g_frj_false_sv;
Branch: 1 branch
BranchTrueFalseTotal
SV *s = truth ? g_frj_true_sv : g_frj_false_sv; 13 9 22
180 22 ▶ if (s) {
Branch: 1 branch
BranchTrueFalseTotal
if (s) { 22 0 22
181 22 SvREFCNT_inc_simple_void_NN(s);
182 22 return s;
183 }
184 }
185 2 return make_bool_sv(aTHX_ truth, boolean_stash);
186 }
187  
188 13846 case YYJSON_TYPE_NUM:
189 13846 switch (st) {
190 7813 ▶ case YYJSON_SUBTYPE_UINT: {
Branch: 1 branch
BranchTrueFalseTotal
case YYJSON_SUBTYPE_UINT: { 7813 0 7813
191 7813 uint64_t u = yyjson_get_uint(val);
192 7813 ▶ if (u <= (uint64_t)IV_MAX) return newSViv((IV)u);
Branch: 1 branch
BranchTrueFalseTotal
if (u <= (uint64_t)IV_MAX) return newSViv((IV)u); 7811 2 7813
193 2 return newSVuv((UV)u);
194 }
195 3 ▶ case YYJSON_SUBTYPE_SINT:
Branch: 1 branch
BranchTrueFalseTotal
case YYJSON_SUBTYPE_SINT: 3 0 3
196 3 return newSViv((IV)yyjson_get_sint(val));
197 6030 ▶ case YYJSON_SUBTYPE_REAL:
Branch: 1 branch
BranchTrueFalseTotal
case YYJSON_SUBTYPE_REAL: 6030 0 6030
198 6030 return newSVnv(yyjson_get_real(val));
199 0 default:
200 0 return newSV(0);
201 }
202  
203 23315 ▶ case YYJSON_TYPE_STR: {
Branch: 1 branch
BranchTrueFalseTotal
case YYJSON_TYPE_STR: { 23315 0 23315
204 23315 ▶ const char *s = yyjson_get_str(val);
Branch: 1 branch
BranchTrueFalseTotal
const char *s = yyjson_get_str(val); 23315 0 23315
205 23315 size_t n = yyjson_get_len(val);
206 23315 SV *out = newSVpvn(s, n);
207 23315 sv_utf8_decode(out);
208 23315 return out;
209 }
210  
211 3482 ▶ case YYJSON_TYPE_ARR: {
Branch: 1 branch
BranchTrueFalseTotal
case YYJSON_TYPE_ARR: { 3482 0 3482
212 3482 size_t n = yyjson_arr_size(val);
213 3482 AV *av = newAV();
214 /* Empty-array fast path: skip iter setup. */
215 3482 ▶ if (n == 0) return newRV_noinc((SV *)av);
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) return newRV_noinc((SV *)av); 26 3456 3482
216 {
217 yyjson_val *elem;
218 yyjson_arr_iter it;
219 3456 av_extend(av, (SSize_t)n);
220 yyjson_arr_iter_init(val, &it);
221 23042 ▶ while ((elem = yyjson_arr_iter_next(&it))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((elem = yyjson_arr_iter_next(&it))) { 16642 2944 19586
222 16642 av_push(av, sv_from_yyjson_d(aTHX_ elem, boolean_stash,
223 ordered, depth + 1,
224 max_depth));
225 }
226 }
227 2944 return newRV_noinc((SV *)av);
228 }
229  
230 9511 ▶ case YYJSON_TYPE_OBJ: {
Branch: 1 branch
BranchTrueFalseTotal
case YYJSON_TYPE_OBJ: { 9511 0 9511
231 /* Tied OrderedHash when ordered=>1 (each insert dispatches
232 * tie_oh_store so insertion order is preserved); plain HV
233 * with hv_store otherwise. */
234 9511 size_t n = yyjson_obj_size(val);
235 9511 ▶ HV *hv = ordered ? make_ordered_hv(aTHX) : newHV();
Branch: 1 branch
BranchTrueFalseTotal
HV *hv = ordered ? make_ordered_hv(aTHX) : newHV(); 85 9426 9511
236 /* Empty-object fast path. */
237 9511 ▶ if (n == 0) return newRV_noinc((SV *)hv);
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) return newRV_noinc((SV *)hv); 11 9500 9511
238 /* Pre-size the bucket table for the non-ordered case so
239 * hv_store doesn't trigger 2-3 splits + rehashes for any
240 * object with more than 8 keys. */
241 9500 ▶ if (!ordered && n > 8) hv_ksplit(hv, (IV)n);
Branch: 2 branches
BranchTrueFalseTotal
if (!ordered && n > 8) hv_ksplit(hv, (IV)n); 9416 84 9500
if (!ordered && n > 8) hv_ksplit(hv, (IV)n); 2 9414 9416
242 {
243 yyjson_val *key, *vv;
244 yyjson_obj_iter it;
245 yyjson_obj_iter_init(val, &it);
246 46290 ▶ while ((key = yyjson_obj_iter_next(&it))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((key = yyjson_obj_iter_next(&it))) { 27290 9500 36790
247 27290 ▶ vv = yyjson_obj_iter_get_val(key);
Branch: 1 branch
BranchTrueFalseTotal
vv = yyjson_obj_iter_get_val(key); 27290 0 27290
248 27290 ▶ const char *kp = yyjson_get_str(key);
Branch: 1 branch
BranchTrueFalseTotal
const char *kp = yyjson_get_str(key); 27290 0 27290
249 27290 size_t kl = yyjson_get_len(key);
250 27290 SV *child = sv_from_yyjson_d(aTHX_ vv, boolean_stash,
251 ordered, depth + 1,
252 max_depth);
253 27290 ▶ if (ordered) {
Branch: 1 branch
BranchTrueFalseTotal
if (ordered) { 414 26876 27290
254 414 ordered_hv_set(aTHX_ hv, kp, kl, child);
255 } else {
256 /* Negative klen tells hv_store the bytes are
257 * UTF-8 - so wide-char Perl literals (eg
258 * "\x{00e9}") match keys decoded from yyjson's
259 * native UTF-8 byte stream. */
260 26876 ▶ if (!hv_store(hv, kp, -(I32)kl, child, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_store(hv, kp, -(I32)kl, child, 0)) { 0 26876 26876
261 0 SvREFCNT_dec(child);
262 }
263 }
264 }
265 }
266 9500 return newRV_noinc((SV *)hv);
267 }
268  
269 0 default:
270 0 return newSV(0);
271 }
272 }
273  
274 SV *
275 6268 json_sv_from_yyjson(pTHX_ yyjson_val *val, HV *boolean_stash,
276 int ordered, int max_depth)
277 {
278 6268 return sv_from_yyjson_d(aTHX_ val, boolean_stash, ordered, 0, max_depth);
279 }
280  
281 static HV *
282 85 make_ordered_hv(pTHX)
283 {
284 85 HV *hv = newHV();
285 85 SV *tied = tie_oh_new(aTHX); /* refcount=1, owned */
286 85 sv_magic((SV *)hv, tied, PERL_MAGIC_tied, NULL, 0);
287 85 SvREFCNT_dec(tied); /* sv_magic took its own */
288 85 return hv;
289 }
290  
291 /* Insert (key, val) into the tied HV. Detects our impl object and
292 * calls tie_oh_store directly (no method dispatch). For foreign tie
293 * classes - if a caller hands us an HV tied to something we don't
294 * recognise - fall back to call_method("STORE") so the contract still
295 * holds. */
296 static void
297 414 ordered_hv_set(pTHX_ HV *hv, const char *key, STRLEN klen, SV *val)
298 {
299 414 MAGIC *mg = mg_find((SV *)hv, PERL_MAGIC_tied);
300 SV *tied_obj;
301 414 dSP;
302  
303 414 ▶ if (!mg || !mg->mg_obj) {
Branch: 2 branches
BranchTrueFalseTotal
if (!mg || !mg->mg_obj) { 414 0 414
if (!mg || !mg->mg_obj) { 0 414 414
304 /* No tie magic. Plain hv_store is fine. */
305 0 ▶ if (!hv_store(hv, key, (I32)klen, val, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_store(hv, key, (I32)klen, val, 0)) { 0 0 0
306 0 SvREFCNT_dec(val);
307 }
308 0 return;
309 }
310 414 tied_obj = mg->mg_obj;
311  
312 /* Fast path: our own class. tie_oh_store takes ownership of val,
313 * so no SvREFCNT bookkeeping needed. */
314 414 ▶ if (tie_oh_is_instance(aTHX_ tied_obj)) {
Branch: 1 branch
BranchTrueFalseTotal
if (tie_oh_is_instance(aTHX_ tied_obj)) { 414 0 414
315 414 tie_oh_store(aTHX_ tied_obj, key, klen, val);
316 414 return;
317 }
318  
319 /* Slow path: foreign tie class. Dispatch STORE via call_method,
320 * mortalising the value so it gets cleaned up after the call. */
321 0 ENTER; SAVETMPS;
322 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
323 0 ▶ XPUSHs(tied_obj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(tied_obj); 0 0 0
324 0 ▶ XPUSHs(sv_2mortal(newSVpvn(key, klen)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(key, klen))); 0 0 0
325 0 ▶ XPUSHs(sv_2mortal(val));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(val)); 0 0 0
326 0 PUTBACK;
327 0 call_method("STORE", G_DISCARD);
328 0 SPAGAIN;
329 0 PUTBACK;
330 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
331 }
332  
333 static SV *
334 2 make_bool_sv(pTHX_ int truth, HV *stash)
335 {
336 2 ▶ if (!stash) {
Branch: 1 branch
BranchTrueFalseTotal
if (!stash) { 0 2 2
337 0 ▶ return newSVsv(truth ? &PL_sv_yes : &PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
return newSVsv(truth ? &PL_sv_yes : &PL_sv_no); 0 0 0
338 }
339 /* Hot path: default class returns the pre-built read-only
340 * singleton. Caller's SvREFCNT_dec balances our SvREFCNT_inc. */
341 2 ▶ if (g_frj_default_stash && stash == g_frj_default_stash) {
Branch: 2 branches
BranchTrueFalseTotal
if (g_frj_default_stash && stash == g_frj_default_stash) { 2 0 2
if (g_frj_default_stash && stash == g_frj_default_stash) { 0 2 2
342 0 ▶ SV *s = truth ? g_frj_true_sv : g_frj_false_sv;
Branch: 1 branch
BranchTrueFalseTotal
SV *s = truth ? g_frj_true_sv : g_frj_false_sv; 0 0 0
343 0 ▶ if (s) {
Branch: 1 branch
BranchTrueFalseTotal
if (s) { 0 0 0
344 0 ▶ SvREFCNT_inc_simple_void(s);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_inc_simple_void(s); 0 0 0
345 0 return s;
346 }
347 /* Singletons not initialised yet (eg called from BOOT order
348 * race). Fall through to per-call allocation. */
349 }
350 {
351 2 SV *inner = newSViv(truth ? 1 : 0);
352 2 SV *rv = newRV_noinc(inner);
353 2 sv_bless(rv, stash);
354 2 return rv;
355 }
356 }
357  
358 /* ============================================================
359 * Value mapping: Perl SV -> yyjson_mut_val (for encode)
360 * ============================================================ */
361  
362 static int sv_is_known_boolean_class(pTHX_ SV *sv);
363  
364 /* Cycle detection.
365 *
366 * Cycles can only form when a value points back to one of its
367 * ancestors on the current descent path. Sibling subtrees can
368 * never form cycles with each other - they're disjoint by the time
369 * we visit them. So we only need to track the *active path*, not
370 * everything ever visited. For real-world JSON-shaped data the
371 * path is typically 3-10 deep; a linear scan of a small SV* array
372 * is dramatically cheaper than HV insert / lookup / delete.
373 *
374 * If a structure exceeds VISITED_STACK_MAX (rare; we'd croak under
375 * default max_depth=512 long before then), we fall back to an HV
376 * for the overflow. Combined with the inline stack the worst case
377 * stays correct without slowing the common case. */
378 #define VISITED_STACK_MAX 64
379  
380 typedef struct {
381 SV *stack[VISITED_STACK_MAX];
382 int depth;
383 HV *overflow; /* lazy; created only when stack is full */
384 } visited_t;
385  
386 PERL_STATIC_INLINE int
387 6637 visited_seen(pTHX_ visited_t *v, SV *target)
388 {
389 int i;
390 /* Linear scan from top of stack (most recently entered first -
391 * cycles tend to point near where we just were). */
392 17825 ▶ for (i = v->depth - 1; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = v->depth - 1; i >= 0; i--) { 11190 6635 17825
393 11190 ▶ if (v->stack[i] == target) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (v->stack[i] == target) return 1; 2 11188 11190
394 }
395 6635 ▶ if (v->overflow) {
Branch: 1 branch
BranchTrueFalseTotal
if (v->overflow) { 0 6635 6635
396 0 return hv_exists(v->overflow, (const char *)&target, sizeof(SV *));
397 }
398 6635 return 0;
399 }
400  
401 PERL_STATIC_INLINE void
402 6635 visited_enter(pTHX_ visited_t *v, SV *target)
403 {
404 6635 ▶ if (v->depth < VISITED_STACK_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (v->depth < VISITED_STACK_MAX) { 6635 0 6635
405 6635 v->stack[v->depth++] = target;
406 6635 return;
407 }
408 /* Stack full - spill to HV. */
409 0 ▶ if (!v->overflow) v->overflow = newHV();
Branch: 1 branch
BranchTrueFalseTotal
if (!v->overflow) v->overflow = newHV(); 0 0 0
410 0 SvREFCNT_inc_simple_void_NN(&PL_sv_undef);
411 0 ▶ if (!hv_store(v->overflow, (const char *)&target, sizeof(SV *),
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_store(v->overflow, (const char *)&target, sizeof(SV *), 0 0 0
412 &PL_sv_undef, 0))
413 0 SvREFCNT_dec(&PL_sv_undef);
414 }
415  
416 PERL_STATIC_INLINE void
417 6633 visited_leave(pTHX_ visited_t *v, SV *target)
418 {
419 /* Stack is LIFO - the last visited_enter must match this leave.
420 * Defensive: walk back if not at top (shouldn't happen). */
421 6633 ▶ if (v->depth > 0 && v->stack[v->depth - 1] == target) {
Branch: 2 branches
BranchTrueFalseTotal
if (v->depth > 0 && v->stack[v->depth - 1] == target) { 6633 0 6633
if (v->depth > 0 && v->stack[v->depth - 1] == target) { 6633 0 6633
422 6633 v->depth--;
423 6633 return;
424 }
425 0 ▶ if (v->overflow) {
Branch: 1 branch
BranchTrueFalseTotal
if (v->overflow) { 0 0 0
426 0 hv_delete(v->overflow, (const char *)&target, sizeof(SV *),
427 G_DISCARD);
428 }
429 }
430  
431 static yyjson_mut_val *sv_to_yyjson_v(pTHX_ SV *sv, yyjson_mut_doc *doc,
432 const json_options_t *opts,
433 visited_t *visited);
434  
435 yyjson_mut_val *
436 1156 json_sv_to_yyjson(pTHX_ SV *sv, yyjson_mut_doc *doc,
437 const json_options_t *opts)
438 {
439 visited_t visited;
440 yyjson_mut_val *r;
441 1156 visited.depth = 0;
442 1156 visited.overflow = NULL;
443 1156 r = sv_to_yyjson_v(aTHX_ sv, doc, opts, &visited);
444 1151 ▶ if (visited.overflow) SvREFCNT_dec((SV *)visited.overflow);
Branch: 1 branch
BranchTrueFalseTotal
if (visited.overflow) SvREFCNT_dec((SV *)visited.overflow); 0 1151 1151
445 1151 return r;
446 }
447  
448 static yyjson_mut_val *
449 32632 sv_to_yyjson_v(pTHX_ SV *sv, yyjson_mut_doc *doc,
450 const json_options_t *opts, visited_t *visited)
451 {
452 PERL_UNUSED_ARG(opts);
453  
454 32647 ▶ if (!sv || !SvOK(sv)) return yyjson_mut_null(doc);
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv)) return yyjson_mut_null(doc); 32632 0 32632
if (!sv || !SvOK(sv)) return yyjson_mut_null(doc); 15 32617 32632
455  
456 32617 ▶ if (SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) { 6656 25961 32617
457 6656 SV *target = SvRV(sv);
458 6656 ▶ if (SvOBJECT(target) && sv_is_known_boolean_class(aTHX_ sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvOBJECT(target) && sv_is_known_boolean_class(aTHX_ sv)) { 16 6640 6656
if (SvOBJECT(target) && sv_is_known_boolean_class(aTHX_ sv)) { 14 2 16
459 14 int truth = SvTRUE(target) ? 1 : 0;
460 28 ▶ return yyjson_mut_bool(doc, truth);
Branch: 1 branch
BranchTrueFalseTotal
return yyjson_mut_bool(doc, truth); 14 0 14
461 }
462 6642 ▶ if (SvTYPE(target) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(target) == SVt_PVAV) { 2229 4413 6642
463 2229 AV *av = (AV *)target;
464 yyjson_mut_val *arr;
465 SV **arr_ptr;
466 SSize_t i, n;
467 2229 ▶ if (visited_seen(aTHX_ visited, target)) {
Branch: 1 branch
BranchTrueFalseTotal
if (visited_seen(aTHX_ visited, target)) { 1 2228 2229
468 1 croak("File::Raw::JSON: circular reference detected "
469 "(array references itself)");
470 }
471 2228 visited_enter(aTHX_ visited, target);
472 2228 arr = yyjson_mut_arr(doc);
473 2228 n = av_len(av) + 1;
474 /* Direct AvARRAY access skips av_fetch's bounds + magic
475 * check per element. Holes (sparse arrays) come back as
476 * NULL pointers; we substitute &PL_sv_undef. */
477 2228 arr_ptr = AvARRAY(av);
478 17560 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 15333 2227 17560
479 15333 SV *ep = arr_ptr[i];
480 yyjson_mut_val *child =
481 15333 ▶ sv_to_yyjson_v(aTHX_ ep ? ep : &PL_sv_undef,
Branch: 1 branch
BranchTrueFalseTotal
sv_to_yyjson_v(aTHX_ ep ? ep : &PL_sv_undef, 15331 2 15333
482 doc, opts, visited);
483 yyjson_mut_arr_append(arr, child);
484 }
485 2227 visited_leave(aTHX_ visited, target);
486 2227 return arr;
487 }
488 4413 ▶ if (SvTYPE(target) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(target) == SVt_PVHV) { 4408 5 4413
489 /* Three dispatches:
490 * - Tie::OrderedHash: walk via the public C ABI
491 * tie_oh_iter_* (no method dispatch).
492 * - Other tied HVs: walk via hv_iternext (which goes
493 * through tied FIRSTKEY/NEXTKEY) and dispatch FETCH
494 * per key via call_method.
495 * - Untied HVs: walk via hv_iternext + hv_iterval.
496 *
497 * sort_keys / canonical: collect (key, val) pairs first,
498 * sort, emit. Default: walk and emit in one pass - no
499 * collection buffer alloc. HvUSEDKEYS returns 0 for tied
500 * HVs (their bucket storage is empty), so we can't use it
501 * to pre-size or short-circuit; the live walks above
502 * handle tied HVs correctly. */
503 4408 HV *hv = (HV *)target;
504 yyjson_mut_val *obj;
505 HE *he;
506 MAGIC *tied_mg;
507 4408 ▶ int do_sort = opts->sort_keys || opts->canonical;
Branch: 2 branches
BranchTrueFalseTotal
int do_sort = opts->sort_keys || opts->canonical; 260 4148 4408
int do_sort = opts->sort_keys || opts->canonical; 19 241 260
508  
509 4408 ▶ if (visited_seen(aTHX_ visited, target)) {
Branch: 1 branch
BranchTrueFalseTotal
if (visited_seen(aTHX_ visited, target)) { 1 4407 4408
510 1 croak("File::Raw::JSON: circular reference detected "
511 "(hash references itself)");
512 }
513 4407 visited_enter(aTHX_ visited, target);
514 4407 obj = yyjson_mut_obj(doc);
515 4407 tied_mg = mg_find((SV *)hv, PERL_MAGIC_tied);
516  
517 4407 ▶ if (do_sort) {
Branch: 1 branch
BranchTrueFalseTotal
if (do_sort) { 4167 240 4407
518 /* Collect-sort-emit. Most JSON objects have <32
519 * keys; stack-allocate the buffers for that case to
520 * skip the malloc/free pair (significant when
521 * sort_keys is set on a large array of small
522 * objects - was the dominant cost on the
523 * sort_keys+pretty bench at medium size). */
524 #define FRJ_SORT_STACK_SIZE 32
525 SV *stack_keys[FRJ_SORT_STACK_SIZE];
526 SV *stack_vals[FRJ_SORT_STACK_SIZE];
527 4167 SV **keys_buf = stack_keys;
528 4167 SV **vals_buf = stack_vals;
529 4167 SSize_t count = 0;
530 4167 SSize_t cap = FRJ_SORT_STACK_SIZE;
531 4167 int on_heap = 0;
532 SSize_t i;
533  
534 #define FRJ_PUSH_PAIR(KSV, VSV) STMT_START { \
535 if (count >= cap) { \
536 SSize_t new_cap = cap * 2; \
537 if (on_heap) { \
538 Renew(keys_buf, new_cap, SV *); \
539 Renew(vals_buf, new_cap, SV *); \
540 } else { \
541 SV **nk, **nv; \
542 Newx(nk, new_cap, SV *); \
543 Newx(nv, new_cap, SV *); \
544 memcpy(nk, keys_buf, sizeof(SV *) * (size_t)count); \
545 memcpy(nv, vals_buf, sizeof(SV *) * (size_t)count); \
546 keys_buf = nk; \
547 vals_buf = nv; \
548 on_heap = 1; \
549 } \
550 cap = new_cap; \
551 } \
552 keys_buf[count] = (KSV); \
553 vals_buf[count] = (VSV); \
554 count++; \
555 } STMT_END
556  
557 4167 ▶ if (tied_mg && tied_mg->mg_obj
Branch: 2 branches
BranchTrueFalseTotal
if (tied_mg && tied_mg->mg_obj 5 4162 4167
if (tied_mg && tied_mg->mg_obj 5 0 5
558 5 ▶ && tie_oh_is_instance(aTHX_ tied_mg->mg_obj))
Branch: 1 branch
BranchTrueFalseTotal
&& tie_oh_is_instance(aTHX_ tied_mg->mg_obj)) 5 0 5
559 5 {
560 5 SV *self = tied_mg->mg_obj;
561 tie_oh_iter_t it;
562 const char *key;
563 STRLEN klen;
564 SV *vsv;
565 5 tie_oh_iter_init(aTHX_ self, &it);
566 18 ▶ while (tie_oh_iter_next(aTHX_ self, &it,
Branch: 1 branch
BranchTrueFalseTotal
while (tie_oh_iter_next(aTHX_ self, &it, 13 5 18
567 &key, &klen, &vsv)) {
568 13 SV *ksv = sv_2mortal(newSVpvn(key, klen));
569 13 ▶ FRJ_PUSH_PAIR(ksv, vsv);
Branch: 6 branches
BranchTrueFalseTotal
FRJ_PUSH_PAIR(ksv, vsv); 0 13 13
FRJ_PUSH_PAIR(ksv, vsv); 0 0 0
FRJ_PUSH_PAIR(ksv, vsv); 0 0 0
FRJ_PUSH_PAIR(ksv, vsv); 0 0 0
FRJ_PUSH_PAIR(ksv, vsv); 0 0 0
FRJ_PUSH_PAIR(ksv, vsv); 0 0 0
570 }
571 4162 ▶ } else if (tied_mg && tied_mg->mg_obj) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (tied_mg && tied_mg->mg_obj) { 0 4162 4162
} else if (tied_mg && tied_mg->mg_obj) { 0 0 0
572 0 hv_iterinit(hv);
573 0 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 0 0 0
574 I32 klen;
575 0 const char *key = hv_iterkey(he, &klen);
576 SV *fetched, *copy, *ksv;
577 int rc;
578 0 dSP;
579 0 ENTER; SAVETMPS;
580 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
581 0 ▶ XPUSHs(tied_mg->mg_obj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(tied_mg->mg_obj); 0 0 0
582 0 ▶ XPUSHs(sv_2mortal(newSVpvn(key, klen)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(key, klen))); 0 0 0
583 0 PUTBACK;
584 0 rc = call_method("FETCH", G_SCALAR);
585 0 SPAGAIN;
586 0 ▶ fetched = rc > 0 ? POPs : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
fetched = rc > 0 ? POPs : &PL_sv_undef; 0 0 0
587 0 copy = newSVsv(fetched);
588 0 PUTBACK;
589 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
590 0 ksv = sv_2mortal(newSVpvn(key, klen));
591 0 ▶ FRJ_PUSH_PAIR(ksv, sv_2mortal(copy));
Branch: 6 branches
BranchTrueFalseTotal
FRJ_PUSH_PAIR(ksv, sv_2mortal(copy)); 0 0 0
FRJ_PUSH_PAIR(ksv, sv_2mortal(copy)); 0 0 0
FRJ_PUSH_PAIR(ksv, sv_2mortal(copy)); 0 0 0
FRJ_PUSH_PAIR(ksv, sv_2mortal(copy)); 0 0 0
FRJ_PUSH_PAIR(ksv, sv_2mortal(copy)); 0 0 0
FRJ_PUSH_PAIR(ksv, sv_2mortal(copy)); 0 0 0
592 }
593 } else {
594 /* Untied HV: HeVAL(he) is direct, no tie-magic
595 * re-check (hv_iterval would do that since it's
596 * the polymorphic accessor). */
597 4162 hv_iterinit(hv);
598 18563 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 14401 4162 18563
599 14401 ▶ FRJ_PUSH_PAIR(hv_iterkeysv(he), HeVAL(he));
Branch: 6 branches
BranchTrueFalseTotal
FRJ_PUSH_PAIR(hv_iterkeysv(he), HeVAL(he)); 0 14401 14401
FRJ_PUSH_PAIR(hv_iterkeysv(he), HeVAL(he)); 0 0 0
FRJ_PUSH_PAIR(hv_iterkeysv(he), HeVAL(he)); 0 0 0
FRJ_PUSH_PAIR(hv_iterkeysv(he), HeVAL(he)); 0 0 0
FRJ_PUSH_PAIR(hv_iterkeysv(he), HeVAL(he)); 0 0 0
FRJ_PUSH_PAIR(hv_iterkeysv(he), HeVAL(he)); 0 0 0
600 }
601 }
602  
603 #undef FRJ_PUSH_PAIR
604  
605 /* Insertion sort: small n typical, no comparator
606 * indirection. */
607 {
608 SSize_t a, b;
609 14419 ▶ for (a = 1; a < count; a++) {
Branch: 1 branch
BranchTrueFalseTotal
for (a = 1; a < count; a++) { 10252 4167 14419
610 10252 SV *kcur = keys_buf[a];
611 10252 SV *vcur = vals_buf[a];
612 STRLEN clen;
613 10252 const char *cpv = SvPV(kcur, clen);
614 10252 b = a - 1;
615 21486 ▶ while (b >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (b >= 0) { 17266 4220 21486
616 STRLEN plen;
617 17266 const char *ppv = SvPV(keys_buf[b], plen);
618 17266 STRLEN cmplen = clen < plen ? clen : plen;
619 17266 int rc = memcmp(cpv, ppv, cmplen);
620 17266 ▶ if (rc < 0 || (rc == 0 && clen < plen)) {
Branch: 3 branches
BranchTrueFalseTotal
if (rc < 0 || (rc == 0 && clen < plen)) { 6032 11234 17266
if (rc < 0 || (rc == 0 && clen < plen)) { 2 6030 6032
if (rc < 0 || (rc == 0 && clen < plen)) { 0 2 2
621 11234 keys_buf[b + 1] = keys_buf[b];
622 11234 vals_buf[b + 1] = vals_buf[b];
623 11234 b--;
624 } else break;
625 }
626 10252 keys_buf[b + 1] = kcur;
627 10252 vals_buf[b + 1] = vcur;
628 }
629 }
630  
631 18581 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 14414 4167 18581
632 STRLEN klen;
633 14414 const char *kp = SvPV(keys_buf[i], klen);
634 14414 ▶ yyjson_mut_val *kval = yyjson_mut_strn(doc, kp, (size_t)klen);
Branch: 1 branch
BranchTrueFalseTotal
yyjson_mut_val *kval = yyjson_mut_strn(doc, kp, (size_t)klen); 14414 0 14414
635 14414 yyjson_mut_val *vval = sv_to_yyjson_v(aTHX_ vals_buf[i],
636 doc, opts, visited);
637 yyjson_mut_obj_add(obj, kval, vval);
638 }
639  
640 4167 ▶ if (on_heap) {
Branch: 1 branch
BranchTrueFalseTotal
if (on_heap) { 0 4167 4167
641 0 Safefree(keys_buf);
642 0 Safefree(vals_buf);
643 }
644 #undef FRJ_SORT_STACK_SIZE
645 }
646 240 ▶ else if (tied_mg && tied_mg->mg_obj
Branch: 2 branches
BranchTrueFalseTotal
else if (tied_mg && tied_mg->mg_obj 54 186 240
else if (tied_mg && tied_mg->mg_obj 54 0 54
647 54 ▶ && tie_oh_is_instance(aTHX_ tied_mg->mg_obj))
Branch: 1 branch
BranchTrueFalseTotal
&& tie_oh_is_instance(aTHX_ tied_mg->mg_obj)) 54 0 54
648 54 {
649 /* Single-pass: tie_oh_iter_* + emit. */
650 54 SV *self = tied_mg->mg_obj;
651 tie_oh_iter_t it;
652 const char *key;
653 STRLEN klen;
654 SV *vsv;
655 54 tie_oh_iter_init(aTHX_ self, &it);
656 387 ▶ while (tie_oh_iter_next(aTHX_ self, &it,
Branch: 1 branch
BranchTrueFalseTotal
while (tie_oh_iter_next(aTHX_ self, &it, 333 54 387
657 &key, &klen, &vsv)) {
658 333 ▶ yyjson_mut_val *kval = yyjson_mut_strn(
Branch: 1 branch
BranchTrueFalseTotal
yyjson_mut_val *kval = yyjson_mut_strn( 333 0 333
659 doc, key, (size_t)klen);
660 333 yyjson_mut_val *vval = sv_to_yyjson_v(
661 aTHX_ vsv, doc, opts, visited);
662 yyjson_mut_obj_add(obj, kval, vval);
663 }
664 }
665 186 ▶ else if (tied_mg && tied_mg->mg_obj) {
Branch: 2 branches
BranchTrueFalseTotal
else if (tied_mg && tied_mg->mg_obj) { 0 186 186
else if (tied_mg && tied_mg->mg_obj) { 0 0 0
666 /* Single-pass: hv_iternext + per-key call_method FETCH. */
667 0 hv_iterinit(hv);
668 0 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 0 0 0
669 I32 klen;
670 0 const char *key = hv_iterkey(he, &klen);
671 SV *fetched, *copy, *vsv;
672 yyjson_mut_val *kval, *vval;
673 int rc;
674 0 dSP;
675 0 ENTER; SAVETMPS;
676 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
677 0 ▶ XPUSHs(tied_mg->mg_obj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(tied_mg->mg_obj); 0 0 0
678 0 ▶ XPUSHs(sv_2mortal(newSVpvn(key, klen)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(key, klen))); 0 0 0
679 0 PUTBACK;
680 0 rc = call_method("FETCH", G_SCALAR);
681 0 SPAGAIN;
682 0 ▶ fetched = rc > 0 ? POPs : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
fetched = rc > 0 ? POPs : &PL_sv_undef; 0 0 0
683 0 copy = newSVsv(fetched);
684 0 PUTBACK;
685 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
686 0 vsv = sv_2mortal(copy);
687  
688 0 ▶ kval = yyjson_mut_strn(doc, key, (size_t)klen);
Branch: 1 branch
BranchTrueFalseTotal
kval = yyjson_mut_strn(doc, key, (size_t)klen); 0 0 0
689 0 vval = sv_to_yyjson_v(aTHX_ vsv, doc, opts, visited);
690 yyjson_mut_obj_add(obj, kval, vval);
691 }
692 }
693 else {
694 /* Untied HV: HeVAL(he) is direct. hv_iterval is the
695 * tie-aware polymorphic accessor and would re-check
696 * magic per key; we already know we're not tied here. */
697 186 hv_iterinit(hv);
698 1581 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 1396 185 1581
699 I32 klen;
700 1396 const char *key = hv_iterkey(he, &klen);
701 1396 ▶ yyjson_mut_val *kval = yyjson_mut_strn(
Branch: 1 branch
BranchTrueFalseTotal
yyjson_mut_val *kval = yyjson_mut_strn( 1396 0 1396
702 doc, key, (size_t)klen);
703 1396 yyjson_mut_val *vval = sv_to_yyjson_v(
704 aTHX_ HeVAL(he), doc, opts, visited);
705 yyjson_mut_obj_add(obj, kval, vval);
706 }
707 }
708  
709 4406 visited_leave(aTHX_ visited, target);
710 4406 return obj;
711 }
712 /* Reject things that have no sane JSON encoding. */
713 {
714 5 svtype tt = SvTYPE(target);
715 5 ▶ if (tt == SVt_PVCV) {
Branch: 1 branch
BranchTrueFalseTotal
if (tt == SVt_PVCV) { 1 4 5
716 1 croak("File::Raw::JSON: cannot encode CODE reference as JSON");
717 }
718 4 ▶ if (tt == SVt_PVGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (tt == SVt_PVGV) { 1 3 4
719 1 croak("File::Raw::JSON: cannot encode GLOB reference as JSON");
720 }
721 /* SvRXOK is the portable regex check - handles SVt_REGEXP
722 * and the older magic-attached form. */
723 3 ▶ if (SvRXOK(sv) || SvRXOK(target)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvRXOK(sv) || SvRXOK(target)) { 2 1 3
if (SvRXOK(sv) || SvRXOK(target)) { 0 2 2
724 1 croak("File::Raw::JSON: cannot encode Regexp reference as JSON");
725 }
726 }
727 /* Non-blessed scalar refs are treated as JSON booleans by
728 * truthiness, matching JSON::XS / Cpanel::JSON::XS / JSON::PP:
729 * \1 -> true
730 * \0 -> false
731 * \"" -> false
732 * \"x" -> true
733 * Lets callers express booleans without loading a sentinel
734 * class. */
735 2 ▶ if (!SvOBJECT(target)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOBJECT(target)) { 2 0 2
736 4 ▶ return yyjson_mut_bool(doc, SvTRUE(target) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
return yyjson_mut_bool(doc, SvTRUE(target) ? 1 : 0); 2 0 2
737 }
738 /* Blessed non-recognised refs (eg bless \1, 'My::Class' that
739 * isn't a known boolean class) fall through to SvPV which
740 * emits the stringified form ("My::Class=SCALAR(0x...)"). */
741 {
742 STRLEN len;
743 0 const char *s = SvPV(sv, len);
744 0 ▶ return yyjson_mut_strn(doc, s, (size_t)len);
Branch: 1 branch
BranchTrueFalseTotal
return yyjson_mut_strn(doc, s, (size_t)len); 0 0 0
745 }
746 }
747  
748 25961 ▶ if (SvIOK(sv) && !SvNOK(sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvIOK(sv) && !SvNOK(sv)) { 5690 20271 25961
if (SvIOK(sv) && !SvNOK(sv)) { 1690 4000 5690
749 1692 ▶ if (SvIsUV(sv)) return yyjson_mut_uint(doc, (uint64_t)SvUV(sv));
Branch: 1 branch
BranchTrueFalseTotal
if (SvIsUV(sv)) return yyjson_mut_uint(doc, (uint64_t)SvUV(sv)); 2 1688 1690
750 3376 return yyjson_mut_sint(doc, (int64_t)SvIV(sv));
751 }
752 24271 ▶ if (SvNOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOK(sv)) { 6019 18252 24271
753 12038 return yyjson_mut_real(doc, SvNV(sv));
754 }
755 {
756 STRLEN len;
757 18252 const char *s = SvPV(sv, len);
758 36504 ▶ return yyjson_mut_strn(doc, s, (size_t)len);
Branch: 1 branch
BranchTrueFalseTotal
return yyjson_mut_strn(doc, s, (size_t)len); 18252 0 18252
759 }
760 }
761  
762 /* Recognise known boolean sentinel classes.
763 *
764 * Hot path: pointer-compare against g_frj_default_stash (cached at
765 * BOOT for File::Raw::JSON::Boolean). ~99% of decoded booleans
766 * come back blessed into this class, so a single pointer comparison
767 * resolves them without touching the stash name. Foreign classes
768 * fall through to the HvNAME_get + strEQ chain. */
769 static int
770 16 sv_is_known_boolean_class(pTHX_ SV *sv)
771 {
772 HV *stash;
773 const char *name;
774 16 ▶ if (!SvROK(sv)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(sv)) return 0; 0 16 16
775 16 ▶ if (!SvOBJECT(SvRV(sv))) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOBJECT(SvRV(sv))) return 0; 0 16 16
776 16 stash = SvSTASH(SvRV(sv));
777 16 ▶ if (!stash) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!stash) return 0; 0 16 16
778 /* Hot path: our default class. */
779 16 ▶ if (g_frj_default_stash && stash == g_frj_default_stash) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (g_frj_default_stash && stash == g_frj_default_stash) return 1; 16 0 16
if (g_frj_default_stash && stash == g_frj_default_stash) return 1; 12 4 16
780 /* Cold path: foreign boolean sentinels. */
781 4 ▶ name = HvNAME_get(stash);
Branch: 6 branches
BranchTrueFalseTotal
name = HvNAME_get(stash); 4 0 4
name = HvNAME_get(stash); 4 0 4
name = HvNAME_get(stash); 0 4 4
name = HvNAME_get(stash); 0 0 0
name = HvNAME_get(stash); 4 0 4
name = HvNAME_get(stash); 0 4 4
782 4 ▶ if (!name) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!name) return 0; 0 4 4
783 4 ▶ if (strEQ(name, "JSON::PP::Boolean")) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "JSON::PP::Boolean")) return 1; 2 2 4
784 2 ▶ if (strEQ(name, "Types::Serialiser::Boolean")) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "Types::Serialiser::Boolean")) return 1; 0 2 2
785 2 ▶ if (strEQ(name, "Cpanel::JSON::XS::Boolean")) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "Cpanel::JSON::XS::Boolean")) return 1; 0 2 2
786 2 ▶ if (strEQ(name, "JSON::XS::Boolean")) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "JSON::XS::Boolean")) return 1; 0 2 2
787 2 ▶ if (strEQ(name, "boolean")) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "boolean")) return 1; 0 2 2
788 /* Last: name-compare File::Raw::JSON::Boolean for the case where
789 * the stash pointer drifted (eg multiple interpreter contexts). */
790 2 return strEQ(name, "File::Raw::JSON::Boolean");
791 }
792  
793 /* ============================================================
794 * Decode entry points
795 * ============================================================ */
796  
797 static void
798 6 croak_yyjson_read(pTHX_ const yyjson_read_err *err,
799 const char *bytes, STRLEN len)
800 {
801 6 STRLEN ctx_off = err->pos > 16 ? err->pos - 16 : 0;
802 6 STRLEN ctx_end = err->pos + 16 < len ? err->pos + 16 : len;
803 6 STRLEN ctx_len = ctx_end - ctx_off;
804 6 SV *ctx = sv_2mortal(newSVpvn(bytes + ctx_off, ctx_len));
805 6 char *p = SvPVX(ctx);
806 STRLEN i;
807 50 ▶ for (i = 0; i < ctx_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ctx_len; i++) { 44 6 50
808 44 ▶ if (p[i] == '\n' || p[i] == '\r' || p[i] == '\t') p[i] = ' ';
Branch: 3 branches
BranchTrueFalseTotal
if (p[i] == '\n' || p[i] == '\r' || p[i] == '\t') p[i] = ' '; 43 1 44
if (p[i] == '\n' || p[i] == '\r' || p[i] == '\t') p[i] = ' '; 43 0 43
if (p[i] == '\n' || p[i] == '\r' || p[i] == '\t') p[i] = ' '; 0 43 43
809 }
810 6 ▶ croak("File::Raw::JSON: %s at byte offset %lu near \"%.*s\"",
Branch: 1 branch
BranchTrueFalseTotal
croak("File::Raw::JSON: %s at byte offset %lu near \"%.*s\"", 6 0 6
811 err->msg ? err->msg : "parse error",
812 (unsigned long)err->pos, (int)ctx_len, p);
813 }
814  
815 static yyjson_read_flag
816 234 build_read_flags(const json_options_t *opts)
817 {
818 234 yyjson_read_flag f = 0;
819 234 ▶ if (opts->relaxed) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts->relaxed) { 5 229 234
820 5 f |= YYJSON_READ_ALLOW_COMMENTS;
821 5 f |= YYJSON_READ_ALLOW_TRAILING_COMMAS;
822 }
823 234 ▶ if (opts->allow_nan_inf) f |= YYJSON_READ_ALLOW_INF_AND_NAN;
Branch: 1 branch
BranchTrueFalseTotal
if (opts->allow_nan_inf) f |= YYJSON_READ_ALLOW_INF_AND_NAN; 1 233 234
824 234 return f;
825 }
826  
827 /* Hybrid bump-then-malloc allocator for yyjson read.
828 *
829 * yyjson allocates one ~16-byte mut_val per JSON value via its
830 * configured allocator (default: malloc). For small/medium docs
831 * those allocations are pure overhead - we'd rather slice them off
832 * a pre-allocated stack buffer.
833 *
834 * This allocator does exactly that: serves from a stack-resident
835 * pool by bump pointer until exhausted, then falls back to malloc
836 * for individual oversize requests. free() is a no-op for in-pool
837 * pointers, real free() for malloc'd ones.
838 *
839 * Why hybrid (rather than just yyjson_alc_pool_init): the built-in
840 * pool init returns NULL on overflow, which fails the parse. We
841 * want graceful fallback so any doc parses, with the small/medium
842 * fast path getting the perf win. */
843 typedef struct {
844 char *pool;
845 size_t pool_size;
846 size_t pool_used;
847 } frj_alc_ctx_t;
848  
849 static void *
850 547 frj_alc_malloc(void *ctx, size_t size)
851 {
852 547 frj_alc_ctx_t *c = (frj_alc_ctx_t *)ctx;
853 /* 16-byte aligned bump (yyjson values are typically 16-byte). */
854 547 size_t aligned = (c->pool_used + 15) & ~(size_t)15;
855 547 ▶ if (aligned + size <= c->pool_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (aligned + size <= c->pool_size) { 538 9 547
856 538 c->pool_used = aligned + size;
857 538 return c->pool + aligned;
858 }
859 9 return malloc(size);
860 }
861  
862 static void *
863 83 frj_alc_realloc(void *ctx, void *ptr, size_t old_size, size_t size)
864 {
865 83 frj_alc_ctx_t *c = (frj_alc_ctx_t *)ctx;
866 83 ▶ if (ptr >= (void *)c->pool && ptr < (void *)(c->pool + c->pool_size)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ptr >= (void *)c->pool && ptr < (void *)(c->pool + c->pool_size)) { 79 4 83
if (ptr >= (void *)c->pool && ptr < (void *)(c->pool + c->pool_size)) { 79 0 79
867 /* In-pool memory: allocate fresh, copy, leave the old block
868 * in the pool (will be reclaimed when the pool is reset). */
869 79 void *new_ptr = frj_alc_malloc(ctx, size);
870 79 ▶ if (new_ptr && old_size > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (new_ptr && old_size > 0) { 79 0 79
if (new_ptr && old_size > 0) { 79 0 79
871 79 memcpy(new_ptr, ptr, old_size < size ? old_size : size);
872 }
873 79 return new_ptr;
874 }
875 4 return realloc(ptr, size);
876 }
877  
878 static void
879 466 frj_alc_free(void *ctx, void *ptr)
880 {
881 466 frj_alc_ctx_t *c = (frj_alc_ctx_t *)ctx;
882 466 ▶ if (ptr >= (void *)c->pool && ptr < (void *)(c->pool + c->pool_size)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ptr >= (void *)c->pool && ptr < (void *)(c->pool + c->pool_size)) { 458 8 466
if (ptr >= (void *)c->pool && ptr < (void *)(c->pool + c->pool_size)) { 458 0 458
883 458 return; /* No-op for pool memory; reset on caller exit */
884 }
885 8 free(ptr);
886 }
887  
888 #define FRJ_READ_POOL_BYTES (16 * 1024)
889  
890 SV *
891 144 json_decode_document(pTHX_ const char *bytes, STRLEN len,
892 const json_options_t *opts, HV *boolean_stash)
893 {
894 char pool_buf[FRJ_READ_POOL_BYTES];
895 frj_alc_ctx_t alc_ctx;
896 yyjson_alc alc;
897 yyjson_read_err err;
898 yyjson_doc *doc;
899 SV *out;
900 yyjson_val *root;
901  
902 144 alc_ctx.pool = pool_buf;
903 144 alc_ctx.pool_size = sizeof pool_buf;
904 144 alc_ctx.pool_used = 0;
905 144 alc.malloc = frj_alc_malloc;
906 144 alc.realloc = frj_alc_realloc;
907 144 alc.free = frj_alc_free;
908 144 alc.ctx = &alc_ctx;
909  
910 144 doc = yyjson_read_opts((char *)bytes, (size_t)len,
911 build_read_flags(opts), &alc, &err);
912  
913 144 ▶ if (!doc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) { 6 138 144
914 6 croak_yyjson_read(aTHX_ &err, bytes, len);
915 /* NOTREACHED */
916 }
917 138 root = yyjson_doc_get_root(doc);
918 138 ▶ if (!opts->allow_nonref && root) {
Branch: 2 branches
BranchTrueFalseTotal
if (!opts->allow_nonref && root) { 1 137 138
if (!opts->allow_nonref && root) { 1 0 1
919 1 yyjson_type t = yyjson_get_type(root);
920 1 ▶ if (t != YYJSON_TYPE_ARR && t != YYJSON_TYPE_OBJ) {
Branch: 2 branches
BranchTrueFalseTotal
if (t != YYJSON_TYPE_ARR && t != YYJSON_TYPE_OBJ) { 1 0 1
if (t != YYJSON_TYPE_ARR && t != YYJSON_TYPE_OBJ) { 1 0 1
921 yyjson_doc_free(doc);
922 1 croak("File::Raw::JSON: top-level value is not an object/array "
923 "and allow_nonref is false");
924 }
925 }
926 137 out = json_sv_from_yyjson(aTHX_ root, boolean_stash,
927 137 opts->ordered, opts->max_depth);
928 yyjson_doc_free(doc);
929 136 return out;
930 }
931  
932 AV *
933 38 json_decode_lines(pTHX_ const char *bytes, STRLEN len,
934 const json_options_t *opts, HV *boolean_stash)
935 {
936 38 AV *result = newAV();
937 38 STRLEN cursor = 0;
938 /* Per-line pool: reused across all lines. Reset to empty after
939 * each parse (the doc is freed before next parse, so all in-pool
940 * memory is logically reclaimed). */
941 char pool_buf[FRJ_READ_POOL_BYTES];
942 frj_alc_ctx_t alc_ctx;
943 yyjson_alc alc;
944 38 alc_ctx.pool = pool_buf;
945 38 alc_ctx.pool_size = sizeof pool_buf;
946 38 alc.malloc = frj_alc_malloc;
947 38 alc.realloc = frj_alc_realloc;
948 38 alc.free = frj_alc_free;
949 38 alc.ctx = &alc_ctx;
950  
951 128 ▶ while (cursor < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (cursor < len) { 92 36 128
952 STRLEN s, e, np;
953 jsonl_scan_t rc =
954 92 json_jsonl_next(bytes + cursor, len - cursor, &s, &e, &np);
955 92 ▶ if (rc == JSONL_FOUND) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == JSONL_FOUND) { 90 2 92
956 yyjson_read_err err;
957 yyjson_doc *doc;
958 90 alc_ctx.pool_used = 0; /* reset pool for next line */
959 90 doc = yyjson_read_opts(
960 90 (char *)bytes + cursor + s, (size_t)(e - s),
961 build_read_flags(opts), &alc, &err);
962 90 ▶ if (!doc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) { 0 90 90
963 0 err.pos += cursor + s;
964 0 SvREFCNT_dec((SV *)result);
965 0 croak_yyjson_read(aTHX_ &err, bytes, len);
966 }
967 180 ▶ av_push(result,
Branch: 1 branch
BranchTrueFalseTotal
av_push(result, 90 0 90
968 json_sv_from_yyjson(aTHX_ yyjson_doc_get_root(doc),
969 boolean_stash, opts->ordered,
970 opts->max_depth));
971 yyjson_doc_free(doc);
972 90 cursor += np;
973 90 continue;
974 }
975 2 ▶ if (rc == JSONL_NEED_MORE) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == JSONL_NEED_MORE) { 1 1 2
976 1 SvREFCNT_dec((SV *)result);
977 1 croak("File::Raw::JSON: truncated JSON value at byte offset %lu",
978 (unsigned long)(cursor + s));
979 }
980 /* JSONL_NO_OPENER: lenient = skip; strict = croak. */
981 1 ▶ if (s >= len - cursor) break; /* trailing whitespace */
Branch: 1 branch
BranchTrueFalseTotal
if (s >= len - cursor) break; /* trailing whitespace */ 1 0 1
982 0 ▶ if (opts->relaxed) { cursor += s + 1; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (opts->relaxed) { cursor += s + 1; continue; } 0 0 0
983 0 SvREFCNT_dec((SV *)result);
984 0 croak("File::Raw::JSON: unexpected byte at offset %lu "
985 "(expected '{' or '[' to start a JSONL value)",
986 (unsigned long)(cursor + s));
987 }
988 37 return result;
989 }
990  
991 /* ============================================================
992 * Encode entry points
993 * ============================================================ */
994  
995 static yyjson_write_flag
996 1151 build_write_flags(const json_options_t *opts)
997 {
998 1151 yyjson_write_flag f = 0;
999 1151 ▶ if (opts->pretty && !opts->canonical) {
Branch: 2 branches
BranchTrueFalseTotal
if (opts->pretty && !opts->canonical) { 14 1137 1151
if (opts->pretty && !opts->canonical) { 14 0 14
1000 14 ▶ if (opts->indent == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts->indent == 4) { 1 13 14
1001 1 f |= YYJSON_WRITE_PRETTY; /* 4 spaces */
1002 } else {
1003 13 f |= YYJSON_WRITE_PRETTY_TWO_SPACES;
1004 }
1005 }
1006 1151 ▶ if (!opts->utf8) f |= YYJSON_WRITE_ESCAPE_UNICODE;
Branch: 1 branch
BranchTrueFalseTotal
if (!opts->utf8) f |= YYJSON_WRITE_ESCAPE_UNICODE; 0 1151 1151
1007 1151 ▶ if (opts->allow_nan_inf) f |= YYJSON_WRITE_ALLOW_INF_AND_NAN;
Branch: 1 branch
BranchTrueFalseTotal
if (opts->allow_nan_inf) f |= YYJSON_WRITE_ALLOW_INF_AND_NAN; 1 1150 1151
1008 1151 return f;
1009 }
1010  
1011 SV *
1012 1156 json_encode_document(pTHX_ SV *value, const json_options_t *opts)
1013 {
1014 1156 yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
1015 yyjson_mut_val *root;
1016 yyjson_write_err err;
1017 char *out;
1018 size_t out_len;
1019 SV *result;
1020  
1021 1156 ▶ if (!doc) croak("File::Raw::JSON: out of memory (mut_doc_new)");
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) croak("File::Raw::JSON: out of memory (mut_doc_new)"); 0 1156 1156
1022  
1023 1156 root = json_sv_to_yyjson(aTHX_ value, doc, opts);
1024 yyjson_mut_doc_set_root(doc, root);
1025  
1026 1151 out = yyjson_mut_write_opts(doc, build_write_flags(opts), NULL,
1027 &out_len, &err);
1028 1151 ▶ if (!out) {
Branch: 1 branch
BranchTrueFalseTotal
if (!out) { 0 1151 1151
1029 0 yyjson_mut_doc_free(doc);
1030 0 ▶ croak("File::Raw::JSON: encode failed: %s",
Branch: 1 branch
BranchTrueFalseTotal
croak("File::Raw::JSON: encode failed: %s", 0 0 0
1031 err.msg ? err.msg : "unknown error");
1032 }
1033 1151 result = newSVpvn(out, out_len);
1034 1151 free(out);
1035 1151 yyjson_mut_doc_free(doc);
1036 1151 return result;
1037 }
1038  
1039 SV *
1040 13 json_encode_lines(pTHX_ SV *payload, const json_options_t *opts)
1041 {
1042 AV *av;
1043 SSize_t i, n;
1044 SV *out;
1045  
1046 13 ▶ if (!payload || !SvROK(payload) || SvTYPE(SvRV(payload)) != SVt_PVAV)
Branch: 3 branches
BranchTrueFalseTotal
if (!payload || !SvROK(payload) || SvTYPE(SvRV(payload)) != SVt_PVAV) 13 0 13
if (!payload || !SvROK(payload) || SvTYPE(SvRV(payload)) != SVt_PVAV) 12 1 13
if (!payload || !SvROK(payload) || SvTYPE(SvRV(payload)) != SVt_PVAV) 1 11 12
1047 2 croak("File::Raw::JSON: jsonl write expects an arrayref of records");
1048  
1049 11 av = (AV *)SvRV(payload);
1050 11 n = av_len(av) + 1;
1051 11 out = newSVpvn("", 0);
1052  
1053 1032 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1021 11 1032
1054 1021 SV **ep = av_fetch(av, i, 0);
1055 SV *rec_bytes;
1056 STRLEN blen;
1057 const char *bp;
1058 2042 ▶ rec_bytes = json_encode_document(aTHX_
Branch: 1 branch
BranchTrueFalseTotal
rec_bytes = json_encode_document(aTHX_ 1021 0 1021
1059 1021 ▶ (ep && *ep) ? *ep : &PL_sv_undef, opts);
Branch: 1 branch
BranchTrueFalseTotal
(ep && *ep) ? *ep : &PL_sv_undef, opts); 1021 0 1021
1060 1021 bp = SvPV(rec_bytes, blen);
1061 1021 sv_catpvn(out, bp, blen);
1062 1021 sv_catpvn(out, opts->eol, opts->eol_len);
1063 1021 SvREFCNT_dec(rec_bytes);
1064 }
1065 11 return out;
1066 }
« yyjson.c ↑ Summary include/yyjson.h »