YY.xs

filter: Statementstmt 90.6% 1449 / 1599 Branchbran 67.3% 813 / 1208 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.5% 2262 / 2807 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« yyjson.h ↑ Summary
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 /* Under PERL_IMPLICIT_SYS (Windows/Strawberry Perl) perl.h remaps the libc
7 allocator names to function-like PerlMem_* macros. yyjson's allocator
8 struct has members literally named malloc/realloc/free, and yyjson.h's
9 inline code calls them as `alc.free(ctx, ptr)` -- the macro then sees two
10 args and the build dies ("PerlMem_free passed 2 arguments"). Restore the
11 real libc names before including yyjson; the bundled yyjson uses the libc
12 allocator and yyjson_free_buf() below releases its buffers with libc free(),
13 so the names must agree. */
14 #undef malloc
15 #undef realloc
16 #undef calloc
17 #undef free
18  
19 #include "yyjson.h"
20 #include "XSParseKeyword.h"
21  
22 #include <string.h>
23  
24 /* flags stored in the JSON::YY object (IV inside the hash) */
25 #define F_UTF8 0x01
26 #define F_PRETTY 0x02
27 #define F_CANONICAL 0x04
28 #define F_ALLOW_NONREF 0x08
29 #define F_ALLOW_UNKNOWN 0x10
30 #define F_ALLOW_BLESSED 0x20
31 #define F_CONVERT_BLESSED 0x40
32 #define F_BOOL 0x80
33  
34 #define MAX_DEPTH_DEFAULT 512
35  
36 typedef struct {
37 U32 flags;
38 U32 max_depth;
39 } json_yy_t;
40  
41 /* ---- ithreads ----
42 perl_clone() copies mg_ptr verbatim, so without an svt_dup a live handle is
43 owned by two interpreters and freed twice. Coders are copied; docs and
44 iterators cannot be, so their clones are emptied and using one croaks. */
45 static int
46 0 json_yy_dup_disown(pTHX_ MAGIC *mg, CLONE_PARAMS *param) {
47 PERL_UNUSED_ARG(param);
48 0 mg->mg_ptr = NULL;
49 0 return 0;
50 }
51  
52 /* handle that came out of a thread clone */
53 #define CHECK_CLONED(mg, what) \
54 STMT_START { \
55 if (!(mg)->mg_ptr) \
56 croak(what " cannot be shared between threads"); \
57 } STMT_END
58  
59 /* sv_magicext() does not derive MGf_DUP from the vtable, and perl_clone()
60 only calls svt_dup when it is set. */
61 static inline MAGIC *
62 28237 attach_ext_magic(pTHX_ SV *sv, const MGVTBL *vtbl, void *ptr) {
63 28237 MAGIC *mg = sv_magicext(sv, NULL, PERL_MAGIC_ext, vtbl,
64 (const char *)ptr, 0);
65 28237 mg->mg_flags |= MGf_DUP;
66 28237 return mg;
67 }
68  
69 /* magic vtable for json_yy_t stored on HV */
70 static int
71 82 json_yy_magic_free(pTHX_ SV *sv, MAGIC *mg) {
72 PERL_UNUSED_ARG(sv);
73 82 ▶ if (mg->mg_ptr)
Branch: 1 branch
BranchTrueFalseTotal
if (mg->mg_ptr) 82 0 82
74 82 Safefree(mg->mg_ptr);
75 82 return 0;
76 }
77  
78 static int
79 0 json_yy_magic_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param) {
80 PERL_UNUSED_ARG(param);
81 0 ▶ if (mg->mg_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
if (mg->mg_ptr) { 0 0 0
82 json_yy_t *copy;
83 0 Newx(copy, 1, json_yy_t);
84 0 *copy = *(json_yy_t *)mg->mg_ptr;
85 0 mg->mg_ptr = (char *)copy;
86 }
87 0 return 0;
88 }
89  
90 static MGVTBL json_yy_vtbl = {
91 NULL, NULL, NULL, NULL,
92 json_yy_magic_free,
93 NULL, json_yy_magic_dup, NULL
94 };
95  
96 static inline json_yy_t *
97 1657 get_self(pTHX_ SV *self_sv) {
98 1657 ▶ if (!SvROK(self_sv))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self_sv)) 0 1657 1657
99 0 croak("not a JSON::YY object");
100 1657 MAGIC *mg = mg_findext(SvRV(self_sv), PERL_MAGIC_ext, &json_yy_vtbl);
101 1657 ▶ if (!mg)
Branch: 1 branch
BranchTrueFalseTotal
if (!mg) 0 1657 1657
102 0 croak("not a JSON::YY object");
103 1657 ▶ CHECK_CLONED(mg, "JSON::YY object");
Branch: 1 branch
BranchTrueFalseTotal
CHECK_CLONED(mg, "JSON::YY object"); 0 1657 1657
104 1657 return (json_yy_t *)mg->mg_ptr;
105 }
106  
107 static MGVTBL empty_vtbl = {0};
108  
109 /* forward declarations */
110 static inline int is_ascii(const char *s, size_t len);
111  
112 /* Characters of `sv` as UTF-8 without touching the caller's scalar: SvPVutf8()
113 upgrades in place, so `jdoc $body` would leave $body a character string and
114 break a later decode_json($body). Only latin-1 high bytes need the copy. */
115 static const char *
116 41794 sv_pv_utf8_nomod(pTHX_ SV *sv, STRLEN *lenp) {
117 41794 SvGETMAGIC(sv);
118 41794 const char *s = SvPV_nomg(sv, *lenp);
119 41794 ▶ if (SvUTF8(sv) || is_ascii(s, *lenp))
Branch: 2 branches
BranchTrueFalseTotal
if (SvUTF8(sv) || is_ascii(s, *lenp)) 41790 4 41794
if (SvUTF8(sv) || is_ascii(s, *lenp)) 41782 8 41790
120 41786 return s;
121 8 SV *tmp = sv_2mortal(newSVpvn(s, *lenp));
122 8 sv_utf8_upgrade(tmp);
123 8 return SvPV_const(tmp, *lenp);
124 }
125  
126 /* Immutable parse buffer behind decode_json_ro, refcounted: its SVs are
127 zero-copy (SvLEN 0) and perl_clone() shares those PV pointers rather than
128 copying, so an emptied clone would read freed memory. Deliberately shared
129 between interpreters, so it uses libc malloc/free rather than perl's
130 per-interpreter pool -- Newx/Safefree here is "Free to wrong pool" under
131 PERL_IMPLICIT_SYS. */
132 typedef struct {
133 yyjson_doc *doc;
134 U32 refcnt;
135 } json_yy_ro_t;
136  
137 #ifdef USE_ITHREADS
138 static perl_mutex json_yy_ro_mutex;
139 static int json_yy_ro_mutex_ready = 0;
140 # define RO_LOCK() MUTEX_LOCK(&json_yy_ro_mutex)
141 # define RO_UNLOCK() MUTEX_UNLOCK(&json_yy_ro_mutex)
142 #else
143 # define RO_LOCK() NOOP
144 # define RO_UNLOCK() NOOP
145 #endif
146  
147 /* doc holder magic: frees yyjson_doc when the last reference goes */
148 static int
149 1214 docholder_magic_free(pTHX_ SV *sv, MAGIC *mg) {
150 PERL_UNUSED_ARG(sv);
151 1214 json_yy_ro_t *h = (json_yy_ro_t *)mg->mg_ptr;
152 1214 ▶ if (h) {
Branch: 1 branch
BranchTrueFalseTotal
if (h) { 1214 0 1214
153 int last;
154 RO_LOCK();
155 1214 last = (--h->refcnt == 0);
156 RO_UNLOCK();
157 1214 ▶ if (last) {
Branch: 1 branch
BranchTrueFalseTotal
if (last) { 1214 0 1214
158 1214 ▶ yyjson_doc_free(h->doc);
Branch: 1 branch
BranchTrueFalseTotal
yyjson_doc_free(h->doc); 1214 0 1214
159 1214 free(h);
160 }
161 }
162 1214 return 0;
163 }
164  
165 static int
166 0 docholder_magic_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param) {
167 PERL_UNUSED_ARG(param);
168 0 json_yy_ro_t *h = (json_yy_ro_t *)mg->mg_ptr;
169 0 ▶ if (h) {
Branch: 1 branch
BranchTrueFalseTotal
if (h) { 0 0 0
170 RO_LOCK();
171 0 h->refcnt++;
172 RO_UNLOCK();
173 }
174 0 return 0;
175 }
176  
177 /* magic free for a yyjson_mut_doc holder: frees the doc when its SV dies */
178 static int
179 5057 mut_docholder_magic_free(pTHX_ SV *sv, MAGIC *mg) {
180 PERL_UNUSED_ARG(sv);
181 5057 yyjson_mut_doc *doc = (yyjson_mut_doc *)mg->mg_ptr;
182 5057 ▶ if (doc)
Branch: 1 branch
BranchTrueFalseTotal
if (doc) 7 5050 5057
183 7 yyjson_mut_doc_free(doc);
184 5057 return 0;
185 }
186  
187 static MGVTBL docholder_magic_vtbl = {
188 NULL, NULL, NULL, NULL,
189 docholder_magic_free,
190 NULL, docholder_magic_dup, NULL
191 };
192  
193 static MGVTBL mut_docholder_vtbl = {
194 NULL, NULL, NULL, NULL,
195 mut_docholder_magic_free,
196 NULL, json_yy_dup_disown, NULL
197 };
198  
199 /* free() a yyjson-allocated (libc malloc) buffer at scope exit, so the buffer
200 is released even if the newSVpvn that copies it croaks on OOM. Used via
201 ENTER/SAVEDESTRUCTOR_X(...)/LEAVE around the copy. */
202 15113 static void yyjson_free_buf(pTHX_ void *p) { free(p); }
203  
204 /* yyjson parses integers as 64-bit. On 32-bit-IV perls a straight cast to
205 IV/UV truncates, so fall back to an NV when the value doesn't fit. The #if
206 compiles this out on 64-bit-IV builds (the common case). */
207 27100 static inline SV * sv_from_u64(pTHX_ uint64_t u) {
208 #if UVSIZE < 8
209 if (u > (uint64_t)UV_MAX) return newSVnv((NV)u);
210 #endif
211 27100 return newSVuv((UV)u);
212 }
213 13 static inline SV * sv_from_i64(pTHX_ int64_t i) {
214 #if IVSIZE < 8
215 if (i < (int64_t)IV_MIN || i > (int64_t)IV_MAX) return newSVnv((NV)i);
216 #endif
217 13 return newSViv((IV)i);
218 }
219  
220 /* Decode a yyjson number (UINT/SINT/REAL) to a fresh SV via the width-safe int
221 helpers above. Two variants for immutable and mutable yyjson values. */
222 27072 ▶static inline SV * yyjson_num_to_sv(pTHX_ yyjson_val *val) {
Branch: 1 branch
BranchTrueFalseTotal
static inline SV * yyjson_num_to_sv(pTHX_ yyjson_val *val) { 27072 0 27072
223 27072 yyjson_subtype st = yyjson_get_subtype(val);
224 54125 ▶ if (st == YYJSON_SUBTYPE_UINT) return sv_from_u64(aTHX_ yyjson_get_uint(val));
Branch: 1 branch
BranchTrueFalseTotal
if (st == YYJSON_SUBTYPE_UINT) return sv_from_u64(aTHX_ yyjson_get_uint(val)); 27053 19 27072
225 22 ▶ if (st == YYJSON_SUBTYPE_SINT) return sv_from_i64(aTHX_ yyjson_get_sint(val));
Branch: 1 branch
BranchTrueFalseTotal
if (st == YYJSON_SUBTYPE_SINT) return sv_from_i64(aTHX_ yyjson_get_sint(val)); 3 16 19
226 16 return newSVnv(yyjson_get_real(val));
227 }
228 57 ▶static inline SV * yyjson_mut_num_to_sv(pTHX_ yyjson_mut_val *val) {
Branch: 1 branch
BranchTrueFalseTotal
static inline SV * yyjson_mut_num_to_sv(pTHX_ yyjson_mut_val *val) { 57 0 57
229 57 yyjson_subtype st = yyjson_mut_get_subtype(val);
230 104 ▶ if (st == YYJSON_SUBTYPE_UINT) return sv_from_u64(aTHX_ yyjson_mut_get_uint(val));
Branch: 1 branch
BranchTrueFalseTotal
if (st == YYJSON_SUBTYPE_UINT) return sv_from_u64(aTHX_ yyjson_mut_get_uint(val)); 47 10 57
231 20 ▶ if (st == YYJSON_SUBTYPE_SINT) return sv_from_i64(aTHX_ yyjson_mut_get_sint(val));
Branch: 1 branch
BranchTrueFalseTotal
if (st == YYJSON_SUBTYPE_SINT) return sv_from_i64(aTHX_ yyjson_mut_get_sint(val)); 10 0 10
232 0 return newSVnv(yyjson_mut_get_real(val));
233 }
234  
235 /* Reject a parsed document nested deeper than `limit`: our materialisers
236 recurse, as do the copy/equals routines behind jclone/jwrite/jeq. Immutable
237 values sit flat in document order (uni.ofs skips a subtree), so one linear
238 pass with a stack of end pointers measures depth without recursing. */
239 static int
240 29156 doc_too_deep(pTHX_ yyjson_doc *doc, U32 limit) {
241 29156 yyjson_val *val = doc->root;
242 58312 ▶ if (!val || !unsafe_yyjson_is_ctn(val))
Branch: 2 branches
BranchTrueFalseTotal
if (!val || !unsafe_yyjson_is_ctn(val)) 29156 0 29156
if (!val || !unsafe_yyjson_is_ctn(val)) 14 29142 29156
243 14 return 0;
244  
245 29142 yyjson_val *end = unsafe_yyjson_get_next(val);
246 yyjson_val **stack;
247 29142 U32 cap = 64, depth = 0;
248 29142 int too_deep = 0;
249  
250 29142 Newx(stack, cap, yyjson_val *);
251 109525 ▶ for (; val < end; val++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; val < end; val++) { 80402 29123 109525
252 80402 ▶ while (depth && val >= stack[depth - 1]) depth--;
Branch: 2 branches
BranchTrueFalseTotal
while (depth && val >= stack[depth - 1]) depth--; 51260 29142 80402
while (depth && val >= stack[depth - 1]) depth--; 0 51260 51260
253 80402 ▶ if (unsafe_yyjson_is_ctn(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unsafe_yyjson_is_ctn(val)) { 50377 30025 80402
254 50377 ▶ if (depth >= limit) { too_deep = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (depth >= limit) { too_deep = 1; break; } 19 50358 50377
255 50358 yyjson_val *sub_end = unsafe_yyjson_get_next(val);
256 /* an n-slot subtree cannot nest deeper than n levels, so small
257 ones are provably fine and get skipped whole */
258 50358 ▶ if ((size_t)(sub_end - val) <= (size_t)(limit - depth)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)(sub_end - val) <= (size_t)(limit - depth)) { 41114 9244 50358
259 41114 val = sub_end - 1; /* the loop's ++ lands on sub_end */
260 41114 continue;
261 }
262 9244 ▶ if (depth == cap) { cap *= 2; Renew(stack, cap, yyjson_val *); }
Branch: 1 branch
BranchTrueFalseTotal
if (depth == cap) { cap *= 2; Renew(stack, cap, yyjson_val *); } 54 9190 9244
263 9244 stack[depth++] = sub_end;
264 }
265 }
266 29142 Safefree(stack);
267 29142 return too_deep;
268 }
269  
270 /* frees the document before croaking */
271 #define CHECK_DOC_DEPTH(doc, limit, what) \
272 STMT_START { \
273 if (doc_too_deep(aTHX_ (doc), (limit))) { \
274 yyjson_doc_free(doc); \
275 croak(what ": maximum nesting depth exceeded"); \
276 } \
277 } STMT_END
278  
279 /* The materialisers return NULL when the depth budget runs out rather than
280 croaking mid-build, so the caller drops the partial structure with one
281 SvREFCNT_dec. */
282 typedef struct {
283 U32 flags;
284 SV *bool_true;
285 SV *bool_false;
286 } json_yy_decode_ctx_t;
287  
288 static SV * yyjson_val_to_sv(pTHX_ yyjson_val *val, U32 budget, const json_yy_decode_ctx_t *ctx);
289 static SV * yyjson_val_to_sv_ro(pTHX_ yyjson_val *val, SV *doc_sv, U32 budget);
290 static yyjson_mut_val * sv_to_yyjson_val(pTHX_ yyjson_mut_doc *doc, SV *sv,
291 json_yy_t *self, U32 depth);
292  
293 /* ---- JSON::YY::Doc -- opaque yyjson mutable document handle ---- */
294  
295 typedef struct {
296 yyjson_mut_doc *doc; /* the mutable document */
297 yyjson_mut_val *root; /* value this handle points at (may be subtree) */
298 SV *owner; /* NULL=owns doc, non-NULL=RV to parent Doc (borrowed) */
299 } json_yy_doc_t;
300  
301 static int
302 21872 json_yy_doc_magic_free(pTHX_ SV *sv, MAGIC *mg) {
303 PERL_UNUSED_ARG(sv);
304 21872 json_yy_doc_t *d = (json_yy_doc_t *)mg->mg_ptr;
305 21872 ▶ if (d) {
Branch: 1 branch
BranchTrueFalseTotal
if (d) { 21872 0 21872
306 21872 ▶ if (d->owner) {
Branch: 1 branch
BranchTrueFalseTotal
if (d->owner) { 1665 20207 21872
307 1665 SvREFCNT_dec(d->owner);
308 } else {
309 20207 ▶ if (d->doc)
Branch: 1 branch
BranchTrueFalseTotal
if (d->doc) 20207 0 20207
310 20207 yyjson_mut_doc_free(d->doc);
311 }
312 21872 Safefree(d);
313 }
314 21872 return 0;
315 }
316  
317 static MGVTBL json_yy_doc_vtbl = {
318 NULL, NULL, NULL, NULL,
319 json_yy_doc_magic_free,
320 NULL, json_yy_dup_disown, NULL
321 };
322  
323 static inline json_yy_doc_t *
324 31665 get_doc(pTHX_ SV *sv) {
325 31665 ▶ if (!SvROK(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(sv)) 0 31665 31665
326 0 croak("not a JSON::YY::Doc object");
327 31665 MAGIC *mg = mg_findext(SvRV(sv), PERL_MAGIC_ext, &json_yy_doc_vtbl);
328 31665 ▶ if (!mg)
Branch: 1 branch
BranchTrueFalseTotal
if (!mg) 2 31663 31665
329 2 croak("not a JSON::YY::Doc object");
330 31663 ▶ CHECK_CLONED(mg, "JSON::YY::Doc");
Branch: 1 branch
BranchTrueFalseTotal
CHECK_CLONED(mg, "JSON::YY::Doc"); 0 31663 31663
331 31663 return (json_yy_doc_t *)mg->mg_ptr;
332 }
333  
334 /* create a new Doc SV. if owner is non-NULL, this is a borrowing ref. */
335 static SV *
336 21872 new_doc_sv(pTHX_ yyjson_mut_doc *doc, yyjson_mut_val *root, SV *owner) {
337 json_yy_doc_t *d;
338 21872 HV *hv = newHV();
339 21872 Newxz(d, 1, json_yy_doc_t);
340 21872 d->doc = doc;
341 21872 d->root = root;
342 21872 ▶ if (owner) {
Branch: 1 branch
BranchTrueFalseTotal
if (owner) { 1665 20207 21872
343 1665 d->owner = owner;
344 1665 SvREFCNT_inc_simple_void_NN(owner);
345 }
346 21872 attach_ext_magic(aTHX_ (SV *)hv, &json_yy_doc_vtbl, d);
347 21872 return sv_bless(newRV_noinc((SV *)hv),
348 gv_stashpvs("JSON::YY::Doc", GV_ADD));
349 }
350  
351 /* resolve a path on a Doc, returning the yyjson_mut_val* or NULL.
352 path must be UTF-8 encoded (use sv_pv_utf8_nomod on caller side). */
353 static inline yyjson_mut_val *
354 20882 doc_resolve_path(pTHX_ json_yy_doc_t *d, const char *path, STRLEN path_len) {
355 20882 ▶ if (path_len == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (path_len == 0) 15121 5761 20882
356 15121 return d->root;
357 11522 ▶ return yyjson_mut_ptr_getn(d->root, path, path_len);
Branch: 1 branch
BranchTrueFalseTotal
return yyjson_mut_ptr_getn(d->root, path, path_len); 0 5761 5761
358 }
359  
360  
361 /* forward decl */
362 static SV * yyjson_mut_val_to_sv(pTHX_ yyjson_mut_val *val, U32 budget);
363  
364 /* ---- keyword plugin: Doc keyword ops ---- */
365  
366 /* pp_jdoc: parse JSON string → Doc */
367 10132 static OP * pp_jdoc_impl(pTHX) {
368 10132 dSP;
369 10132 SV *json_sv = POPs;
370 STRLEN len;
371 10132 const char *json = sv_pv_utf8_nomod(aTHX_ json_sv, &len);
372  
373 yyjson_read_err err;
374 10132 yyjson_doc *idoc = yyjson_read_opts((char *)json, len, YYJSON_READ_NOFLAG, NULL, &err);
375 10132 ▶ if (!idoc)
Branch: 1 branch
BranchTrueFalseTotal
if (!idoc) 1 10131 10132
376 1 croak("jdoc: JSON parse error: %s at byte offset %zu", err.msg, err.pos);
377 10135 ▶ CHECK_DOC_DEPTH(idoc, MAX_DEPTH_DEFAULT, "jdoc");
Branch: 1 branch
BranchTrueFalseTotal
CHECK_DOC_DEPTH(idoc, MAX_DEPTH_DEFAULT, "jdoc"); 4 10127 10131
378  
379 10127 yyjson_mut_doc *mdoc = yyjson_doc_mut_copy(idoc, NULL);
380 yyjson_doc_free(idoc);
381 10127 ▶ if (!mdoc)
Branch: 1 branch
BranchTrueFalseTotal
if (!mdoc) 0 10127 10127
382 0 croak("jdoc: failed to create mutable document");
383  
384 10127 yyjson_mut_val *root = yyjson_mut_doc_get_root(mdoc);
385 10127 SV *result = new_doc_sv(aTHX_ mdoc, root, NULL);
386 10127 ▶ XPUSHs(sv_2mortal(result));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(result)); 0 10127 10127
387 10127 RETURN;
388 }
389  
390 /* pp_jget: get subtree ref (borrowing) */
391 613 static OP * pp_jget_impl(pTHX) {
392 613 dSP;
393 613 SV *path_sv = POPs;
394 613 SV *doc_sv = POPs;
395 613 json_yy_doc_t *d = get_doc(aTHX_ doc_sv);
396 STRLEN path_len;
397 613 const char *path = sv_pv_utf8_nomod(aTHX_ path_sv, &path_len);
398  
399 613 yyjson_mut_val *val = doc_resolve_path(aTHX_ d, path, path_len);
400 613 ▶ if (!val)
Branch: 1 branch
BranchTrueFalseTotal
if (!val) 1 612 613
401 1 croak("jget: path not found: %.*s", (int)path_len, path);
402  
403 612 SV *result = new_doc_sv(aTHX_ d->doc, val, doc_sv);
404 612 ▶ XPUSHs(sv_2mortal(result));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(result)); 0 612 612
405 612 RETURN;
406 }
407  
408 /* pp_jgetp: get as Perl value */
409 102 static OP * pp_jgetp_impl(pTHX) {
410 102 dSP;
411 102 SV *path_sv = POPs;
412 102 SV *doc_sv = POPs;
413 102 json_yy_doc_t *d = get_doc(aTHX_ doc_sv);
414 STRLEN path_len;
415 100 const char *path = sv_pv_utf8_nomod(aTHX_ path_sv, &path_len);
416  
417 100 yyjson_mut_val *val = doc_resolve_path(aTHX_ d, path, path_len);
418 100 ▶ if (!val) {
Branch: 1 branch
BranchTrueFalseTotal
if (!val) { 6 94 100
419 6 ▶ XPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_undef); 0 6 6
420 6 RETURN;
421 }
422  
423 94 SV *result = yyjson_mut_val_to_sv(aTHX_ val, MAX_DEPTH_DEFAULT);
424 94 ▶ if (!result)
Branch: 1 branch
BranchTrueFalseTotal
if (!result) 1 93 94
425 1 croak("jgetp: maximum nesting depth exceeded");
426 93 ▶ XPUSHs(sv_2mortal(result));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(result)); 0 93 93
427 93 RETURN;
428 }
429  
430 /* Place new_val at path within Doc d. The path is resolved from d->root, so on
431 a borrowed subtree Doc (from jget) it stays subtree-relative; for a
432 non-borrowed Doc d->root is the doc root. An empty path replaces the root
433 (forbidden on a borrowed Doc). Tries set first, then add (for "/-" array
434 append). Croaks "<op>: ..." on failure. Shared by jset and jraw. */
435 static void
436 10648 doc_set_at_path(pTHX_ json_yy_doc_t *d, const char *path, STRLEN path_len,
437 yyjson_mut_val *new_val, const char *op) {
438 /* Each component becomes a nested parent, so a long path would build a
439 document deeper than jclone/jeq/jwrite can walk. ('/' is always a
440 separator; a slash inside a key is spelled ~1.) */
441 {
442 10648 STRLEN i, comps = 0;
443 1528280 ▶ for (i = 0; i < path_len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < path_len; i++) 1517632 10648 1528280
444 1517632 ▶ if (path[i] == '/') comps++;
Branch: 1 branch
BranchTrueFalseTotal
if (path[i] == '/') comps++; 212190 1305442 1517632
445 10648 ▶ if (comps > MAX_DEPTH_DEFAULT)
Branch: 1 branch
BranchTrueFalseTotal
if (comps > MAX_DEPTH_DEFAULT) 3 10645 10648
446 3 croak("%s: path nests deeper than the maximum depth", op);
447 }
448  
449 10645 ▶ if (path_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (path_len == 0) { 4 10641 10645
450 4 ▶ if (d->owner)
Branch: 1 branch
BranchTrueFalseTotal
if (d->owner) 2 2 4
451 2 croak("%s: cannot replace root of a borrowed Doc; jclone it first", op);
452 2 ▶ yyjson_mut_doc_set_root(d->doc, new_val);
Branch: 1 branch
BranchTrueFalseTotal
yyjson_mut_doc_set_root(d->doc, new_val); 2 0 2
453 2 d->root = new_val;
454 2 return;
455 }
456 yyjson_ptr_err perr;
457 10641 ▶ bool ok = yyjson_mut_ptr_setx(d->root, path, path_len, new_val,
Branch: 1 branch
BranchTrueFalseTotal
bool ok = yyjson_mut_ptr_setx(d->root, path, path_len, new_val, 10641 0 10641
458 d->doc, true, NULL, &perr);
459 10641 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 7 10634 10641
460 7 perr = (yyjson_ptr_err){0};
461 14 ▶ ok = yyjson_mut_ptr_addx(d->root, path, path_len, new_val,
Branch: 1 branch
BranchTrueFalseTotal
ok = yyjson_mut_ptr_addx(d->root, path, path_len, new_val, 7 0 7
462 d->doc, true, NULL, &perr);
463 }
464 10641 ▶ if (!ok)
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) 0 10641 10641
465 0 ▶ croak("%s: failed to set path %.*s: %s", op,
Branch: 1 branch
BranchTrueFalseTotal
croak("%s: failed to set path %.*s: %s", op, 0 0 0
466 (int)path_len, path, perr.msg ? perr.msg : "unknown error");
467 }
468  
469 /* pp_jset: set value at path */
470 10641 static OP * pp_jset_impl(pTHX) {
471 10641 dSP;
472 10641 SV *value_sv = POPs;
473 10641 SV *path_sv = POPs;
474 10641 SV *doc_sv = POPs;
475 10641 json_yy_doc_t *d = get_doc(aTHX_ doc_sv);
476 STRLEN path_len;
477 10641 const char *path = sv_pv_utf8_nomod(aTHX_ path_sv, &path_len);
478  
479 yyjson_mut_val *new_val;
480  
481 /* check if value is a Doc */
482 10651 ▶ if (SvROK(value_sv) && sv_derived_from(value_sv, "JSON::YY::Doc")) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(value_sv) && sv_derived_from(value_sv, "JSON::YY::Doc")) { 621 10020 10641
if (SvROK(value_sv) && sv_derived_from(value_sv, "JSON::YY::Doc")) { 10 611 621
483 10 json_yy_doc_t *vd = get_doc(aTHX_ value_sv);
484 10 new_val = yyjson_mut_val_mut_copy(d->doc, vd->root);
485 10 ▶ if (!new_val)
Branch: 1 branch
BranchTrueFalseTotal
if (!new_val) 0 10 10
486 0 croak("jset: failed to copy Doc value");
487 } else {
488 /* convert Perl value to yyjson_mut_val */
489 json_yy_t self_stack;
490 10631 self_stack.flags = F_UTF8 | F_ALLOW_NONREF | F_ALLOW_BLESSED
491 | F_CONVERT_BLESSED;
492 10631 self_stack.max_depth = MAX_DEPTH_DEFAULT;
493 10631 new_val = sv_to_yyjson_val(aTHX_ d->doc, value_sv, &self_stack, 0);
494 }
495  
496 10641 doc_set_at_path(aTHX_ d, path, path_len, new_val, "jset");
497  
498 10638 ▶ XPUSHs(doc_sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(doc_sv); 0 10638 10638
499 10638 RETURN;
500 }
501  
502 /* pp_jdel: delete at path, return removed subtree as Doc */
503 12 static OP * pp_jdel_impl(pTHX) {
504 12 dSP;
505 12 SV *path_sv = POPs;
506 12 SV *doc_sv = POPs;
507 12 json_yy_doc_t *d = get_doc(aTHX_ doc_sv);
508 STRLEN path_len;
509 12 const char *path = sv_pv_utf8_nomod(aTHX_ path_sv, &path_len);
510  
511 12 ▶ if (path_len == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (path_len == 0) 2 10 12
512 2 croak("jdel: cannot delete root");
513  
514 /* Resolve from d->root (subtree-relative on a borrowed Doc, matching
515 jget/jgetp reads). Copy BEFORE removing, so an OOM while building the
516 returned Doc leaves the parent unmodified -- jdel stays transactional. */
517 10 ▶ yyjson_mut_val *target = yyjson_mut_ptr_getn(d->root, path, path_len);
Branch: 1 branch
BranchTrueFalseTotal
yyjson_mut_val *target = yyjson_mut_ptr_getn(d->root, path, path_len); 0 10 10
518 10 ▶ if (!target) {
Branch: 1 branch
BranchTrueFalseTotal
if (!target) { 4 6 10
519 4 ▶ XPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_undef); 0 4 4
520 4 RETURN;
521 }
522  
523 /* deep copy into an independent doc (safe from later parent mutations) */
524 6 yyjson_mut_doc *new_doc = yyjson_mut_doc_new(NULL);
525 6 ▶ if (!new_doc)
Branch: 1 branch
BranchTrueFalseTotal
if (!new_doc) 0 6 6
526 0 croak("jdel: out of memory");
527 6 yyjson_mut_val *copy = yyjson_mut_val_mut_copy(new_doc, target);
528 6 ▶ if (!copy) {
Branch: 1 branch
BranchTrueFalseTotal
if (!copy) { 0 6 6
529 0 yyjson_mut_doc_free(new_doc);
530 0 croak("jdel: out of memory copying removed value");
531 }
532 yyjson_mut_doc_set_root(new_doc, copy);
533  
534 /* copy secured; now remove from the parent */
535 6 yyjson_ptr_ctx ctx = {0};
536 6 yyjson_ptr_err perr = {0};
537 12 ▶ if (!yyjson_mut_ptr_removex(d->root, path, path_len, &ctx, &perr)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!yyjson_mut_ptr_removex(d->root, path, path_len, &ctx, &perr)) { 6 0 6
if (!yyjson_mut_ptr_removex(d->root, path, path_len, &ctx, &perr)) { 0 6 6
538 /* the path resolved a moment ago; returning the subtree while the
539 parent still holds it would be a lie */
540 0 yyjson_mut_doc_free(new_doc);
541 0 ▶ croak("jdel: failed to remove path %.*s: %s", (int)path_len, path,
Branch: 1 branch
BranchTrueFalseTotal
croak("jdel: failed to remove path %.*s: %s", (int)path_len, path, 0 0 0
542 perr.msg ? perr.msg : "unknown error");
543 }
544 6 SV *result = new_doc_sv(aTHX_ new_doc, copy, NULL);
545 6 ▶ XPUSHs(sv_2mortal(result));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(result)); 0 6 6
546 6 RETURN;
547 }
548  
549 /* pp_jhas: check if path exists */
550 9 static OP * pp_jhas_impl(pTHX) {
551 9 dSP;
552 9 SV *path_sv = POPs;
553 9 SV *doc_sv = POPs;
554 9 json_yy_doc_t *d = get_doc(aTHX_ doc_sv);
555 STRLEN path_len;
556 9 const char *path = sv_pv_utf8_nomod(aTHX_ path_sv, &path_len);
557  
558 9 yyjson_mut_val *val = doc_resolve_path(aTHX_ d, path, path_len);
559 9 ▶ XPUSHs(val ? &PL_sv_yes : &PL_sv_no);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(val ? &PL_sv_yes : &PL_sv_no); 0 9 9
XPUSHs(val ? &PL_sv_yes : &PL_sv_no); 2 7 9
560 9 RETURN;
561 }
562  
563 /* pp_jclone: deep copy doc/subtree → new independent Doc */
564 5003 static OP * pp_jclone_impl(pTHX) {
565 5003 dSP;
566 5003 SV *path_sv = POPs;
567 5003 SV *doc_sv = POPs;
568 5003 json_yy_doc_t *d = get_doc(aTHX_ doc_sv);
569 STRLEN path_len;
570 5003 const char *path = sv_pv_utf8_nomod(aTHX_ path_sv, &path_len);
571  
572 5003 yyjson_mut_val *src = doc_resolve_path(aTHX_ d, path, path_len);
573 5003 ▶ if (!src)
Branch: 1 branch
BranchTrueFalseTotal
if (!src) 0 5003 5003
574 0 croak("jclone: path not found: %.*s", (int)path_len, path);
575  
576 5003 yyjson_mut_doc *new_doc = yyjson_mut_doc_new(NULL);
577 5003 ▶ if (!new_doc)
Branch: 1 branch
BranchTrueFalseTotal
if (!new_doc) 0 5003 5003
578 0 croak("jclone: failed to create document");
579  
580 5003 yyjson_mut_val *new_root = yyjson_mut_val_mut_copy(new_doc, src);
581 5003 ▶ if (!new_root) {
Branch: 1 branch
BranchTrueFalseTotal
if (!new_root) { 0 5003 5003
582 0 yyjson_mut_doc_free(new_doc);
583 0 croak("jclone: failed to copy value");
584 }
585 yyjson_mut_doc_set_root(new_doc, new_root);
586  
587 5003 SV *result = new_doc_sv(aTHX_ new_doc, new_root, NULL);
588 5003 ▶ XPUSHs(sv_2mortal(result));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(result)); 0 5003 5003
589 5003 RETURN;
590 }
591  
592 /* pp_jencode: serialize doc/subtree to JSON bytes */
593 15040 static OP * pp_jencode_impl(pTHX) {
594 15040 dSP;
595 15040 SV *path_sv = POPs;
596 15040 SV *doc_sv = POPs;
597 15040 json_yy_doc_t *d = get_doc(aTHX_ doc_sv);
598 STRLEN path_len;
599 15040 const char *path = sv_pv_utf8_nomod(aTHX_ path_sv, &path_len);
600  
601 15040 yyjson_mut_val *val = doc_resolve_path(aTHX_ d, path, path_len);
602 15040 ▶ if (!val)
Branch: 1 branch
BranchTrueFalseTotal
if (!val) 0 15040 15040
603 0 croak("jencode: path not found: %.*s", (int)path_len, path);
604  
605 size_t json_len;
606 yyjson_write_err werr;
607 char *json;
608  
609 15040 ▶ if (val == d->root && !d->owner) {
Branch: 2 branches
BranchTrueFalseTotal
if (val == d->root && !d->owner) { 15031 9 15040
if (val == d->root && !d->owner) { 15024 7 15031
610 /* full doc -- use doc write */
611 15024 json = yyjson_mut_write_opts(d->doc, YYJSON_WRITE_NOFLAG, NULL, &json_len, &werr);
612 } else {
613 /* subtree -- use val write */
614 16 json = yyjson_mut_val_write_opts(val, YYJSON_WRITE_NOFLAG, NULL, &json_len, &werr);
615 }
616 15040 ▶ if (!json)
Branch: 1 branch
BranchTrueFalseTotal
if (!json) 0 15040 15040
617 0 croak("jencode: write error: %s", werr.msg);
618  
619 15040 ENTER;
620 15040 SAVEDESTRUCTOR_X(yyjson_free_buf, json);
621 15040 SV *result = newSVpvn(json, json_len);
622 15040 LEAVE;
623 15040 ▶ XPUSHs(sv_2mortal(result));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(result)); 0 15040 15040
624 15040 RETURN;
625 }
626  
627 /* pp_jstr: create JSON string value */
628 5 static OP * pp_jstr_impl(pTHX) {
629 5 dSP;
630 5 SV *val_sv = POPs;
631 STRLEN len;
632 5 const char *str = sv_pv_utf8_nomod(aTHX_ val_sv, &len);
633 5 yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
634 10 ▶ yyjson_mut_val *root = yyjson_mut_strncpy(doc, str, len);
Branch: 2 branches
BranchTrueFalseTotal
yyjson_mut_val *root = yyjson_mut_strncpy(doc, str, len); 5 0 5
yyjson_mut_val *root = yyjson_mut_strncpy(doc, str, len); 5 0 5
635 yyjson_mut_doc_set_root(doc, root);
636 5 ▶ XPUSHs(sv_2mortal(new_doc_sv(aTHX_ doc, root, NULL)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(new_doc_sv(aTHX_ doc, root, NULL))); 0 5 5
637 5 RETURN;
638 }
639  
640 /* pp_jnum: a string argument is grokked rather than run through SvNV, so
641 jnum "42" is an integer and jnum "abc" croaks. */
642 25 static OP * pp_jnum_impl(pTHX) {
643 25 dSP;
644 25 SV *val_sv = POPs;
645 25 SvGETMAGIC(val_sv);
646  
647 /* settle the value first: a document allocated before the croaks below
648 would leak on the unwind */
649 25 enum { NUM_UINT, NUM_SINT, NUM_REAL } kind = NUM_REAL;
650 25 uint64_t uval = 0;
651 25 int64_t ival = 0;
652 25 NV nval = 0;
653  
654 25 ▶ if (SvIOK(val_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(val_sv)) { 4 21 25
655 4 ▶ if (SvIsUV(val_sv)) { kind = NUM_UINT; uval = (uint64_t)SvUVX(val_sv); }
Branch: 1 branch
BranchTrueFalseTotal
if (SvIsUV(val_sv)) { kind = NUM_UINT; uval = (uint64_t)SvUVX(val_sv); } 0 4 4
656 4 else { kind = NUM_SINT; ival = (int64_t)SvIVX(val_sv); }
657 21 ▶ } else if (SvNOK(val_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvNOK(val_sv)) { 2 19 21
658 2 nval = SvNVX(val_sv);
659 /* narrowed: a JSON real is a C double, so a value that is finite only
660 as a long double still cannot be represented */
661 2 ▶ if (Perl_isnan((double)nval) || Perl_isinf((double)nval))
Branch: 2 branches
BranchTrueFalseTotal
if (Perl_isnan((double)nval) || Perl_isinf((double)nval)) 2 0 2
if (Perl_isnan((double)nval) || Perl_isinf((double)nval)) 0 2 2
662 0 croak("jnum: cannot use NaN or Infinity as a JSON number");
663 19 ▶ } else if (SvPOKp(val_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvPOKp(val_sv)) { 18 1 19
664 STRLEN nlen;
665 18 const char *nstr = SvPV_const(val_sv, nlen);
666 UV uv;
667 18 int m_int_ok = 0;
668 18 int t = grok_number(nstr, nlen, &uv);
669 18 ▶ if (!t || (t & (IS_NUMBER_NAN | IS_NUMBER_INFINITY)))
Branch: 2 branches
BranchTrueFalseTotal
if (!t || (t & (IS_NUMBER_NAN | IS_NUMBER_INFINITY))) 15 3 18
if (!t || (t & (IS_NUMBER_NAN | IS_NUMBER_INFINITY))) 2 13 15
670 5 croak("jnum: not a number: \"%.*s\"", (int)nlen, nstr);
671 20 ▶ if (!(t & IS_NUMBER_NOT_INT) && (t & IS_NUMBER_IN_UV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(t & IS_NUMBER_NOT_INT) && (t & IS_NUMBER_IN_UV)) { 7 6 13
if (!(t & IS_NUMBER_NOT_INT) && (t & IS_NUMBER_IN_UV)) { 7 0 7
672 /* widened to uint64_t: (UV)INT64_MAX truncates on a 32-bit UV */
673 7 uint64_t u = (uint64_t)uv;
674 7 ▶ if (!(t & IS_NUMBER_NEG)) { kind = NUM_UINT; uval = u; m_int_ok = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (!(t & IS_NUMBER_NEG)) { kind = NUM_UINT; uval = u; m_int_ok = 1; } 5 2 7
675 2 ▶ else if (u <= (uint64_t)INT64_MAX) { kind = NUM_SINT; ival = -(int64_t)u; m_int_ok = 1; }
Branch: 1 branch
BranchTrueFalseTotal
else if (u <= (uint64_t)INT64_MAX) { kind = NUM_SINT; ival = -(int64_t)u; m_int_ok = 1; } 1 1 2
676 1 ▶ else if (u == (uint64_t)INT64_MAX + 1) { kind = NUM_SINT; ival = INT64_MIN; m_int_ok = 1; }
Branch: 1 branch
BranchTrueFalseTotal
else if (u == (uint64_t)INT64_MAX + 1) { kind = NUM_SINT; ival = INT64_MIN; m_int_ok = 1; } 1 0 1
677 0 else nval = SvNV(val_sv);
678 } else {
679 6 nval = SvNV(val_sv);
680 }
681 /* "1e999" groks fine but overflows to Inf as a double; reject it here
682 rather than building a document that only fails later on write */
683 13 ▶ if (!m_int_ok && (Perl_isnan((double)nval) || Perl_isinf((double)nval)))
Branch: 3 branches
BranchTrueFalseTotal
if (!m_int_ok && (Perl_isnan((double)nval) || Perl_isinf((double)nval))) 6 7 13
if (!m_int_ok && (Perl_isnan((double)nval) || Perl_isinf((double)nval))) 6 0 6
if (!m_int_ok && (Perl_isnan((double)nval) || Perl_isinf((double)nval))) 2 4 6
684 2 croak("jnum: not a number: \"%.*s\"", (int)nlen, nstr);
685 } else {
686 1 ▶ croak("jnum: not a number: %s", SvOK(val_sv) ? "reference" : "undef");
Branch: 1 branch
BranchTrueFalseTotal
croak("jnum: not a number: %s", SvOK(val_sv) ? "reference" : "undef"); 0 1 1
687 }
688  
689 17 yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
690 17 ▶ yyjson_mut_val *root = kind == NUM_UINT ? yyjson_mut_uint(doc, uval)
Branch: 1 branch
BranchTrueFalseTotal
yyjson_mut_val *root = kind == NUM_UINT ? yyjson_mut_uint(doc, uval) 17 0 17
691 29 ▶ : kind == NUM_SINT ? yyjson_mut_sint(doc, ival)
Branch: 1 branch
BranchTrueFalseTotal
: kind == NUM_SINT ? yyjson_mut_sint(doc, ival) 5 12 17
692 18 ▶ : yyjson_mut_real(doc, nval);
Branch: 1 branch
BranchTrueFalseTotal
: yyjson_mut_real(doc, nval); 6 6 12
693 yyjson_mut_doc_set_root(doc, root);
694 17 ▶ XPUSHs(sv_2mortal(new_doc_sv(aTHX_ doc, root, NULL)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(new_doc_sv(aTHX_ doc, root, NULL))); 0 17 17
695 17 RETURN;
696 }
697  
698 /* pp_jbool: create JSON boolean */
699 4 static OP * pp_jbool_impl(pTHX) {
700 4 dSP;
701 4 SV *val_sv = POPs;
702 4 yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
703 8 ▶ yyjson_mut_val *root = yyjson_mut_bool(doc, SvTRUE(val_sv));
Branch: 2 branches
BranchTrueFalseTotal
yyjson_mut_val *root = yyjson_mut_bool(doc, SvTRUE(val_sv)); 4 0 4
yyjson_mut_val *root = yyjson_mut_bool(doc, SvTRUE(val_sv)); 4 0 4
704 yyjson_mut_doc_set_root(doc, root);
705 4 ▶ XPUSHs(sv_2mortal(new_doc_sv(aTHX_ doc, root, NULL)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(new_doc_sv(aTHX_ doc, root, NULL))); 0 4 4
706 4 RETURN;
707 }
708  
709 /* pp_jnull: create JSON null */
710 2 static OP * pp_jnull_impl(pTHX) {
711 2 dSP;
712 2 yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
713 2 ▶ yyjson_mut_val *root = yyjson_mut_null(doc);
Branch: 1 branch
BranchTrueFalseTotal
yyjson_mut_val *root = yyjson_mut_null(doc); 2 0 2
714 yyjson_mut_doc_set_root(doc, root);
715 2 ▶ XPUSHs(sv_2mortal(new_doc_sv(aTHX_ doc, root, NULL)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(new_doc_sv(aTHX_ doc, root, NULL))); 0 2 2
716 2 RETURN;
717 }
718  
719 /* pp_jarr: create empty JSON array */
720 3 static OP * pp_jarr_impl(pTHX) {
721 3 dSP;
722 3 yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
723 3 ▶ yyjson_mut_val *root = yyjson_mut_arr(doc);
Branch: 1 branch
BranchTrueFalseTotal
yyjson_mut_val *root = yyjson_mut_arr(doc); 3 0 3
724 yyjson_mut_doc_set_root(doc, root);
725 3 ▶ XPUSHs(sv_2mortal(new_doc_sv(aTHX_ doc, root, NULL)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(new_doc_sv(aTHX_ doc, root, NULL))); 0 3 3
726 3 RETURN;
727 }
728  
729 /* pp_jobj: create empty JSON object */
730 2 static OP * pp_jobj_impl(pTHX) {
731 2 dSP;
732 2 yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
733 2 ▶ yyjson_mut_val *root = yyjson_mut_obj(doc);
Branch: 1 branch
BranchTrueFalseTotal
yyjson_mut_val *root = yyjson_mut_obj(doc); 2 0 2
734 yyjson_mut_doc_set_root(doc, root);
735 2 ▶ XPUSHs(sv_2mortal(new_doc_sv(aTHX_ doc, root, NULL)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(new_doc_sv(aTHX_ doc, root, NULL))); 0 2 2
736 2 RETURN;
737 }
738  
739 /* pp_jtype: get type string */
740 11 static OP * pp_jtype_impl(pTHX) {
741 11 dSP;
742 11 SV *path_sv = POPs;
743 11 SV *doc_sv = POPs;
744 11 json_yy_doc_t *d = get_doc(aTHX_ doc_sv);
745 STRLEN path_len;
746 11 const char *path = sv_pv_utf8_nomod(aTHX_ path_sv, &path_len);
747  
748 11 yyjson_mut_val *val = doc_resolve_path(aTHX_ d, path, path_len);
749 11 ▶ if (!val) {
Branch: 1 branch
BranchTrueFalseTotal
if (!val) { 1 10 11
750 1 ▶ XPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_undef); 0 1 1
751 1 RETURN;
752 }
753  
754 const char *type;
755 10 switch (yyjson_mut_get_type(val)) {
756 2 case YYJSON_TYPE_OBJ: type = "object"; break;
757 5 case YYJSON_TYPE_ARR: type = "array"; break;
758 1 case YYJSON_TYPE_STR: type = "string"; break;
759 2 case YYJSON_TYPE_NUM: type = "number"; break;
760 0 case YYJSON_TYPE_BOOL: type = "boolean"; break;
761 0 case YYJSON_TYPE_NULL: type = "null"; break;
762 0 default: type = "unknown"; break;
763 }
764 10 ▶ XPUSHs(sv_2mortal(newSVpv(type, 0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpv(type, 0))); 0 10 10
765 10 RETURN;
766 }
767  
768 /* pp_jlen: get array length or object key count */
769 9 static OP * pp_jlen_impl(pTHX) {
770 9 dSP;
771 9 SV *path_sv = POPs;
772 9 SV *doc_sv = POPs;
773 9 json_yy_doc_t *d = get_doc(aTHX_ doc_sv);
774 STRLEN path_len;
775 9 const char *path = sv_pv_utf8_nomod(aTHX_ path_sv, &path_len);
776  
777 9 yyjson_mut_val *val = doc_resolve_path(aTHX_ d, path, path_len);
778 9 ▶ if (!val)
Branch: 1 branch
BranchTrueFalseTotal
if (!val) 1 8 9
779 1 croak("jlen: path not found: %.*s", (int)path_len, path);
780  
781 size_t len;
782 8 ▶ if (yyjson_mut_is_arr(val))
Branch: 1 branch
BranchTrueFalseTotal
if (yyjson_mut_is_arr(val)) 5 3 8
783 5 len = yyjson_mut_arr_size(val);
784 3 ▶ else if (yyjson_mut_is_obj(val))
Branch: 1 branch
BranchTrueFalseTotal
else if (yyjson_mut_is_obj(val)) 2 1 3
785 2 len = yyjson_mut_obj_size(val);
786 1 ▶ else if (yyjson_mut_is_str(val))
Branch: 1 branch
BranchTrueFalseTotal
else if (yyjson_mut_is_str(val)) 1 0 1
787 1 len = yyjson_mut_get_len(val);
788 else
789 0 croak("jlen: value at path is not a container or string");
790  
791 8 ▶ XPUSHs(sv_2mortal(newSViv((IV)len)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv((IV)len))); 0 8 8
792 8 RETURN;
793 }
794  
795 /* pp_jkeys: get object keys as list */
796 4 static OP * pp_jkeys_impl(pTHX) {
797 4 dSP;
798 4 SV *path_sv = POPs;
799 4 SV *doc_sv = POPs;
800 4 json_yy_doc_t *d = get_doc(aTHX_ doc_sv);
801 STRLEN path_len;
802 4 const char *path = sv_pv_utf8_nomod(aTHX_ path_sv, &path_len);
803  
804 4 yyjson_mut_val *val = doc_resolve_path(aTHX_ d, path, path_len);
805 8 ▶ if (!val || !yyjson_mut_is_obj(val))
Branch: 2 branches
BranchTrueFalseTotal
if (!val || !yyjson_mut_is_obj(val)) 4 0 4
if (!val || !yyjson_mut_is_obj(val)) 1 3 4
806 1 croak("jkeys: path does not point to an object");
807  
808 size_t idx, max;
809 yyjson_mut_val *key, *v;
810 6 ▶ EXTEND(SP, (SSize_t)yyjson_mut_obj_size(val));
Branch: 3 branches
BranchTrueFalseTotal
EXTEND(SP, (SSize_t)yyjson_mut_obj_size(val)); 3 0 3
EXTEND(SP, (SSize_t)yyjson_mut_obj_size(val)); 3 0 3
EXTEND(SP, (SSize_t)yyjson_mut_obj_size(val)); 0 3 3
811 11 ▶ yyjson_mut_obj_foreach(val, idx, max, key, v) {
Branch: 4 branches
BranchTrueFalseTotal
yyjson_mut_obj_foreach(val, idx, max, key, v) { 3 0 3
yyjson_mut_obj_foreach(val, idx, max, key, v) { 3 0 3
yyjson_mut_obj_foreach(val, idx, max, key, v) { 3 0 3
yyjson_mut_obj_foreach(val, idx, max, key, v) { 5 3 8
812 5 ▶ const char *kstr = yyjson_mut_get_str(key);
Branch: 1 branch
BranchTrueFalseTotal
const char *kstr = yyjson_mut_get_str(key); 5 0 5
813 5 size_t klen = yyjson_mut_get_len(key);
814 5 SV *ksv = newSVpvn(kstr, klen);
815 5 ▶ if (!is_ascii(kstr, klen))
Branch: 1 branch
BranchTrueFalseTotal
if (!is_ascii(kstr, klen)) 1 4 5
816 1 SvUTF8_on(ksv);
817 5 PUSHs(sv_2mortal(ksv));
818 }
819 3 RETURN;
820 }
821  
822 /* ---- Iterator: pull-style for arrays and objects ---- */
823  
824 typedef struct {
825 union {
826 yyjson_mut_arr_iter arr;
827 yyjson_mut_obj_iter obj;
828 } it;
829 int is_obj;
830 yyjson_mut_val *cur_key; /* for objects: key from last jnext */
831 yyjson_mut_doc *doc;
832 SV *owner; /* refcounted parent Doc SV */
833 } json_yy_iter_t;
834  
835 static int
836 12 json_yy_iter_magic_free(pTHX_ SV *sv, MAGIC *mg) {
837 PERL_UNUSED_ARG(sv);
838 12 json_yy_iter_t *it = (json_yy_iter_t *)mg->mg_ptr;
839 12 ▶ if (it) {
Branch: 1 branch
BranchTrueFalseTotal
if (it) { 12 0 12
840 12 ▶ if (it->owner)
Branch: 1 branch
BranchTrueFalseTotal
if (it->owner) 12 0 12
841 12 SvREFCNT_dec(it->owner);
842 12 Safefree(it);
843 }
844 12 return 0;
845 }
846  
847 static MGVTBL json_yy_iter_vtbl = {
848 NULL, NULL, NULL, NULL,
849 json_yy_iter_magic_free,
850 NULL, json_yy_dup_disown, NULL
851 };
852  
853 static inline json_yy_iter_t *
854 1030 get_iter(pTHX_ SV *sv) {
855 1030 ▶ if (!SvROK(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(sv)) 0 1030 1030
856 0 croak("not a JSON::YY::Iter object");
857 1030 MAGIC *mg = mg_findext(SvRV(sv), PERL_MAGIC_ext, &json_yy_iter_vtbl);
858 1030 ▶ if (!mg)
Branch: 1 branch
BranchTrueFalseTotal
if (!mg) 1 1029 1030
859 1 croak("not a JSON::YY::Iter object");
860 1029 ▶ CHECK_CLONED(mg, "JSON::YY::Iter");
Branch: 1 branch
BranchTrueFalseTotal
CHECK_CLONED(mg, "JSON::YY::Iter"); 0 1029 1029
861 1029 return (json_yy_iter_t *)mg->mg_ptr;
862 }
863  
864 /* pp_jiter: create iterator for array/object at path */
865 13 static OP * pp_jiter_impl(pTHX) {
866 13 dSP;
867 13 SV *path_sv = POPs;
868 13 SV *doc_sv = POPs;
869 13 json_yy_doc_t *d = get_doc(aTHX_ doc_sv);
870 STRLEN path_len;
871 13 const char *path = sv_pv_utf8_nomod(aTHX_ path_sv, &path_len);
872  
873 13 yyjson_mut_val *val = doc_resolve_path(aTHX_ d, path, path_len);
874 13 ▶ if (!val)
Branch: 1 branch
BranchTrueFalseTotal
if (!val) 0 13 13
875 0 croak("jiter: path not found: %.*s", (int)path_len, path);
876 16 ▶ if (!yyjson_mut_is_arr(val) && !yyjson_mut_is_obj(val))
Branch: 2 branches
BranchTrueFalseTotal
if (!yyjson_mut_is_arr(val) && !yyjson_mut_is_obj(val)) 3 10 13
if (!yyjson_mut_is_arr(val) && !yyjson_mut_is_obj(val)) 1 2 3
877 1 croak("jiter: value at path is not an array or object");
878  
879 json_yy_iter_t *it;
880 12 Newxz(it, 1, json_yy_iter_t);
881 12 it->doc = d->doc;
882 12 it->owner = doc_sv;
883 12 SvREFCNT_inc_simple_void_NN(doc_sv);
884 12 ▶ it->cur_key = NULL;
Branch: 1 branch
BranchTrueFalseTotal
it->cur_key = NULL; 12 0 12
885  
886 12 ▶ if (yyjson_mut_is_obj(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (yyjson_mut_is_obj(val)) { 2 10 12
887 2 it->is_obj = 1;
888 2 ▶ yyjson_mut_obj_iter_init(val, &it->it.obj);
Branch: 1 branch
BranchTrueFalseTotal
yyjson_mut_obj_iter_init(val, &it->it.obj); 2 0 2
889 } else {
890 10 it->is_obj = 0;
891 10 ▶ yyjson_mut_arr_iter_init(val, &it->it.arr);
Branch: 1 branch
BranchTrueFalseTotal
yyjson_mut_arr_iter_init(val, &it->it.arr); 10 0 10
892 }
893  
894 12 HV *hv = newHV();
895 12 attach_ext_magic(aTHX_ (SV *)hv, &json_yy_iter_vtbl, it);
896 12 SV *result = sv_bless(newRV_noinc((SV *)hv),
897 gv_stashpvs("JSON::YY::Iter", GV_ADD));
898 12 ▶ XPUSHs(sv_2mortal(result));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(result)); 0 12 12
899 12 RETURN;
900 }
901  
902 /* pp_jnext: advance iterator, return next value as Doc or undef */
903 1027 static OP * pp_jnext_impl(pTHX) {
904 1027 dSP;
905 1027 SV *iter_sv = POPs;
906 1027 json_yy_iter_t *it = get_iter(aTHX_ iter_sv);
907  
908 1026 yyjson_mut_val *val = NULL;
909  
910 1026 ▶ if (it->is_obj) {
Branch: 1 branch
BranchTrueFalseTotal
if (it->is_obj) { 5 1021 1026
911 10 ▶ if (yyjson_mut_obj_iter_has_next(&it->it.obj)) {
Branch: 2 branches
BranchTrueFalseTotal
if (yyjson_mut_obj_iter_has_next(&it->it.obj)) { 5 0 5
if (yyjson_mut_obj_iter_has_next(&it->it.obj)) { 3 2 5
912 3 ▶ it->cur_key = yyjson_mut_obj_iter_next(&it->it.obj);
Branch: 1 branch
BranchTrueFalseTotal
it->cur_key = yyjson_mut_obj_iter_next(&it->it.obj); 3 0 3
913 6 ▶ val = yyjson_mut_obj_iter_get_val(it->cur_key);
Branch: 1 branch
BranchTrueFalseTotal
val = yyjson_mut_obj_iter_get_val(it->cur_key); 3 0 3
914 }
915 } else {
916 2042 ▶ if (yyjson_mut_arr_iter_has_next(&it->it.arr)) {
Branch: 2 branches
BranchTrueFalseTotal
if (yyjson_mut_arr_iter_has_next(&it->it.arr)) { 1021 0 1021
if (yyjson_mut_arr_iter_has_next(&it->it.arr)) { 1014 7 1021
917 2028 ▶ val = yyjson_mut_arr_iter_next(&it->it.arr);
Branch: 1 branch
BranchTrueFalseTotal
val = yyjson_mut_arr_iter_next(&it->it.arr); 1014 0 1014
918 }
919 }
920  
921 1026 ▶ if (!val) {
Branch: 1 branch
BranchTrueFalseTotal
if (!val) { 9 1017 1026
922 9 ▶ XPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_undef); 0 9 9
923 9 RETURN;
924 }
925  
926 /* return value as borrowing Doc */
927 1017 SV *result = new_doc_sv(aTHX_ it->doc, val, it->owner);
928 1017 ▶ XPUSHs(sv_2mortal(result));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(result)); 0 1017 1017
929 1017 RETURN;
930 }
931  
932 /* pp_jkey: get current key from object iterator */
933 3 static OP * pp_jkey_impl(pTHX) {
934 3 dSP;
935 3 SV *iter_sv = POPs;
936 3 json_yy_iter_t *it = get_iter(aTHX_ iter_sv);
937  
938 3 ▶ if (!it->is_obj)
Branch: 1 branch
BranchTrueFalseTotal
if (!it->is_obj) 0 3 3
939 0 croak("jkey: iterator is not over an object");
940 3 ▶ if (!it->cur_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (!it->cur_key) { 0 3 3
941 0 ▶ XPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_undef); 0 0 0
942 0 RETURN;
943 }
944  
945 3 ▶ const char *kstr = yyjson_mut_get_str(it->cur_key);
Branch: 1 branch
BranchTrueFalseTotal
const char *kstr = yyjson_mut_get_str(it->cur_key); 3 0 3
946 3 ▶ size_t klen = yyjson_mut_get_len(it->cur_key);
Branch: 1 branch
BranchTrueFalseTotal
size_t klen = yyjson_mut_get_len(it->cur_key); 3 0 3
947 3 SV *sv = newSVpvn(kstr, klen);
948 3 ▶ if (!is_ascii(kstr, klen))
Branch: 1 branch
BranchTrueFalseTotal
if (!is_ascii(kstr, klen)) 0 3 3
949 0 SvUTF8_on(sv);
950 3 ▶ XPUSHs(sv_2mortal(sv));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(sv)); 0 3 3
951 3 RETURN;
952 }
953  
954 /* pp_jpatch: apply RFC 6902 JSON Patch */
955 18 static OP * pp_jpatch_impl(pTHX) {
956 18 dSP;
957 18 SV *patch_sv = POPs;
958 18 SV *doc_sv = POPs;
959 18 json_yy_doc_t *d = get_doc(aTHX_ doc_sv);
960 18 ▶ if (d->owner)
Branch: 1 branch
BranchTrueFalseTotal
if (d->owner) 1 17 18
961 1 croak("jpatch: cannot patch a borrowed Doc; jclone it first");
962 17 json_yy_doc_t *p = get_doc(aTHX_ patch_sv);
963  
964 17 yyjson_patch_err perr = {0};
965 17 yyjson_mut_val *result = yyjson_mut_patch(d->doc, d->root, p->root, &perr);
966 17 ▶ if (!result)
Branch: 1 branch
BranchTrueFalseTotal
if (!result) 1 16 17
967 1 ▶ croak("jpatch: %s at index %zu", perr.msg ? perr.msg : "patch failed", perr.idx);
Branch: 1 branch
BranchTrueFalseTotal
croak("jpatch: %s at index %zu", perr.msg ? perr.msg : "patch failed", perr.idx); 1 0 1
968  
969 16 ▶ yyjson_mut_doc_set_root(d->doc, result);
Branch: 1 branch
BranchTrueFalseTotal
yyjson_mut_doc_set_root(d->doc, result); 16 0 16
970 16 d->root = result;
971 16 ▶ XPUSHs(doc_sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(doc_sv); 0 16 16
972 16 RETURN;
973 }
974  
975 /* pp_jmerge: apply RFC 7386 JSON Merge Patch */
976 2 static OP * pp_jmerge_impl(pTHX) {
977 2 dSP;
978 2 SV *patch_sv = POPs;
979 2 SV *doc_sv = POPs;
980 2 json_yy_doc_t *d = get_doc(aTHX_ doc_sv);
981 2 ▶ if (d->owner)
Branch: 1 branch
BranchTrueFalseTotal
if (d->owner) 1 1 2
982 1 croak("jmerge: cannot merge into a borrowed Doc; jclone it first");
983 1 json_yy_doc_t *p = get_doc(aTHX_ patch_sv);
984  
985 1 yyjson_mut_val *result = yyjson_mut_merge_patch(d->doc, d->root, p->root);
986 1 ▶ if (!result)
Branch: 1 branch
BranchTrueFalseTotal
if (!result) 0 1 1
987 0 croak("jmerge: merge patch failed");
988  
989 1 ▶ yyjson_mut_doc_set_root(d->doc, result);
Branch: 1 branch
BranchTrueFalseTotal
yyjson_mut_doc_set_root(d->doc, result); 1 0 1
990 1 d->root = result;
991 1 ▶ XPUSHs(doc_sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(doc_sv); 0 1 1
992 1 RETURN;
993 }
994  
995 /* pp_jfrom: create Doc from Perl data (not JSON string) */
996 5034 static OP * pp_jfrom_impl(pTHX) {
997 5034 dSP;
998 5034 SV *data = POPs;
999  
1000 5034 yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
1001 5034 ▶ if (!doc) croak("jfrom: failed to create document");
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) croak("jfrom: failed to create document"); 0 5034 5034
1002  
1003 json_yy_t self_stack;
1004 5034 self_stack.flags = F_UTF8 | F_ALLOW_NONREF | F_ALLOW_BLESSED
1005 | F_CONVERT_BLESSED;
1006 5034 self_stack.max_depth = MAX_DEPTH_DEFAULT;
1007  
1008 /* wrap doc in a holder SV so it's freed on croak */
1009 5034 SV *guard = newSV(0);
1010 5034 attach_ext_magic(aTHX_ guard, &mut_docholder_vtbl, doc);
1011 5034 sv_2mortal(guard);
1012  
1013 5034 yyjson_mut_val *root = sv_to_yyjson_val(aTHX_ doc, data, &self_stack, 0);
1014 yyjson_mut_doc_set_root(doc, root);
1015  
1016 /* transfer doc ownership to the Doc SV; disarm the guard */
1017 5031 mg_findext(guard, PERL_MAGIC_ext, &mut_docholder_vtbl)->mg_ptr = NULL;
1018 5031 SV *result = new_doc_sv(aTHX_ doc, root, NULL);
1019 5031 ▶ XPUSHs(sv_2mortal(result));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(result)); 0 5031 5031
1020 5031 RETURN;
1021 }
1022  
1023 /* pp_jvals: get object values as list */
1024 1 static OP * pp_jvals_impl(pTHX) {
1025 1 dSP;
1026 1 SV *path_sv = POPs;
1027 1 SV *doc_sv = POPs;
1028 1 json_yy_doc_t *d = get_doc(aTHX_ doc_sv);
1029 STRLEN path_len;
1030 1 const char *path = sv_pv_utf8_nomod(aTHX_ path_sv, &path_len);
1031  
1032 1 yyjson_mut_val *val = doc_resolve_path(aTHX_ d, path, path_len);
1033 2 ▶ if (!val || !yyjson_mut_is_obj(val))
Branch: 2 branches
BranchTrueFalseTotal
if (!val || !yyjson_mut_is_obj(val)) 1 0 1
if (!val || !yyjson_mut_is_obj(val)) 0 1 1
1034 0 croak("jvals: path does not point to an object");
1035  
1036 size_t idx, max;
1037 yyjson_mut_val *key, *v;
1038 2 ▶ EXTEND(SP, (SSize_t)yyjson_mut_obj_size(val));
Branch: 3 branches
BranchTrueFalseTotal
EXTEND(SP, (SSize_t)yyjson_mut_obj_size(val)); 1 0 1
EXTEND(SP, (SSize_t)yyjson_mut_obj_size(val)); 1 0 1
EXTEND(SP, (SSize_t)yyjson_mut_obj_size(val)); 0 1 1
1039 5 ▶ yyjson_mut_obj_foreach(val, idx, max, key, v) {
Branch: 4 branches
BranchTrueFalseTotal
yyjson_mut_obj_foreach(val, idx, max, key, v) { 1 0 1
yyjson_mut_obj_foreach(val, idx, max, key, v) { 1 0 1
yyjson_mut_obj_foreach(val, idx, max, key, v) { 1 0 1
yyjson_mut_obj_foreach(val, idx, max, key, v) { 3 1 4
1040 3 SV *vsv = new_doc_sv(aTHX_ d->doc, v, doc_sv);
1041 3 PUSHs(sv_2mortal(vsv));
1042 }
1043 1 RETURN;
1044 }
1045  
1046 /* pp_jeq: deep equality comparison */
1047 3 static OP * pp_jeq_impl(pTHX) {
1048 3 dSP;
1049 3 SV *b_sv = POPs;
1050 3 SV *a_sv = POPs;
1051 3 json_yy_doc_t *a = get_doc(aTHX_ a_sv);
1052 3 json_yy_doc_t *b = get_doc(aTHX_ b_sv);
1053 3 ▶ bool eq = yyjson_mut_equals(a->root, b->root);
Branch: 1 branch
BranchTrueFalseTotal
bool eq = yyjson_mut_equals(a->root, b->root); 3 0 3
1054 3 ▶ XPUSHs(eq ? &PL_sv_yes : &PL_sv_no);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(eq ? &PL_sv_yes : &PL_sv_no); 0 3 3
XPUSHs(eq ? &PL_sv_yes : &PL_sv_no); 2 1 3
1055 3 RETURN;
1056 }
1057  
1058 /* pp_jpp: pretty-print encode */
1059 1 static OP * pp_jpp_impl(pTHX) {
1060 1 dSP;
1061 1 SV *path_sv = POPs;
1062 1 SV *doc_sv = POPs;
1063 1 json_yy_doc_t *d = get_doc(aTHX_ doc_sv);
1064 STRLEN path_len;
1065 1 const char *path = sv_pv_utf8_nomod(aTHX_ path_sv, &path_len);
1066  
1067 1 yyjson_mut_val *val = doc_resolve_path(aTHX_ d, path, path_len);
1068 1 ▶ if (!val)
Branch: 1 branch
BranchTrueFalseTotal
if (!val) 0 1 1
1069 0 croak("jpp: path not found: %.*s", (int)path_len, path);
1070  
1071 size_t json_len;
1072 yyjson_write_err werr;
1073 1 char *json = yyjson_mut_val_write_opts(val, YYJSON_WRITE_PRETTY, NULL,
1074 &json_len, &werr);
1075 1 ▶ if (!json)
Branch: 1 branch
BranchTrueFalseTotal
if (!json) 0 1 1
1076 0 croak("jpp: write error: %s", werr.msg);
1077 1 ENTER;
1078 1 SAVEDESTRUCTOR_X(yyjson_free_buf, json);
1079 1 SV *result = newSVpvn(json, json_len);
1080 1 LEAVE;
1081 1 ▶ XPUSHs(sv_2mortal(result));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(result)); 0 1 1
1082 1 RETURN;
1083 }
1084  
1085 /* pp_jraw: insert raw JSON string at path */
1086 8 static OP * pp_jraw_impl(pTHX) {
1087 8 dSP;
1088 8 SV *json_sv = POPs;
1089 8 SV *path_sv = POPs;
1090 8 SV *doc_sv = POPs;
1091 8 json_yy_doc_t *d = get_doc(aTHX_ doc_sv);
1092 STRLEN path_len;
1093 8 const char *path = sv_pv_utf8_nomod(aTHX_ path_sv, &path_len);
1094 STRLEN json_len;
1095 8 const char *json = sv_pv_utf8_nomod(aTHX_ json_sv, &json_len);
1096  
1097 /* parse the raw JSON fragment */
1098 8 yyjson_doc *idoc = yyjson_read(json, json_len, YYJSON_READ_NOFLAG);
1099 8 ▶ if (!idoc)
Branch: 1 branch
BranchTrueFalseTotal
if (!idoc) 0 8 8
1100 0 croak("jraw: invalid JSON fragment");
1101 9 ▶ CHECK_DOC_DEPTH(idoc, MAX_DEPTH_DEFAULT, "jraw");
Branch: 1 branch
BranchTrueFalseTotal
CHECK_DOC_DEPTH(idoc, MAX_DEPTH_DEFAULT, "jraw"); 1 7 8
1102  
1103 /* copy into mutable doc */
1104 7 yyjson_val *iroot = yyjson_doc_get_root(idoc);
1105 7 yyjson_mut_val *new_val = yyjson_val_mut_copy(d->doc, iroot);
1106 yyjson_doc_free(idoc);
1107  
1108 7 ▶ if (!new_val)
Branch: 1 branch
BranchTrueFalseTotal
if (!new_val) 0 7 7
1109 0 croak("jraw: failed to copy value");
1110  
1111 7 doc_set_at_path(aTHX_ d, path, path_len, new_val, "jraw");
1112  
1113 5 ▶ XPUSHs(doc_sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(doc_sv); 0 5 5
1114 5 RETURN;
1115 }
1116  
1117 /* type predicate macros -- all follow same pattern */
1118 #define PP_JIS(name, check_fn) \
1119 static OP * pp_##name##_impl(pTHX) { \
1120 dSP; \
1121 SV *path_sv = POPs; \
1122 SV *doc_sv = POPs; \
1123 json_yy_doc_t *d = get_doc(aTHX_ doc_sv); \
1124 STRLEN path_len; \
1125 const char *path = sv_pv_utf8_nomod(aTHX_ path_sv, &path_len); \
1126 yyjson_mut_val *val = doc_resolve_path(aTHX_ d, path, path_len); \
1127 XPUSHs(val && check_fn(val) ? &PL_sv_yes : &PL_sv_no); \
1128 RETURN; \
1129 }
1130  
1131 3 ▶static inline bool is_mut_int(yyjson_mut_val *v) {
Branch: 1 branch
BranchTrueFalseTotal
static inline bool is_mut_int(yyjson_mut_val *v) { 3 0 3
1132 4 ▶ return yyjson_mut_is_uint(v) || yyjson_mut_is_sint(v);
Branch: 2 branches
BranchTrueFalseTotal
return yyjson_mut_is_uint(v) || yyjson_mut_is_sint(v); 1 2 3
return yyjson_mut_is_uint(v) || yyjson_mut_is_sint(v); 0 1 1
1133 }
1134  
1135 7 ▶PP_JIS(jis_obj, yyjson_mut_is_obj)
Branch: 3 branches
BranchTrueFalseTotal
PP_JIS(jis_obj, yyjson_mut_is_obj) 0 4 4
PP_JIS(jis_obj, yyjson_mut_is_obj) 3 1 4
PP_JIS(jis_obj, yyjson_mut_is_obj) 2 1 3
1136 6 ▶PP_JIS(jis_arr, yyjson_mut_is_arr)
Branch: 3 branches
BranchTrueFalseTotal
PP_JIS(jis_arr, yyjson_mut_is_arr) 0 3 3
PP_JIS(jis_arr, yyjson_mut_is_arr) 3 0 3
PP_JIS(jis_arr, yyjson_mut_is_arr) 1 2 3
1137 5 ▶PP_JIS(jis_str, yyjson_mut_is_str)
Branch: 3 branches
BranchTrueFalseTotal
PP_JIS(jis_str, yyjson_mut_is_str) 0 3 3
PP_JIS(jis_str, yyjson_mut_is_str) 2 1 3
PP_JIS(jis_str, yyjson_mut_is_str) 1 1 2
1138 6 ▶PP_JIS(jis_num, yyjson_mut_is_num)
Branch: 3 branches
BranchTrueFalseTotal
PP_JIS(jis_num, yyjson_mut_is_num) 0 3 3
PP_JIS(jis_num, yyjson_mut_is_num) 3 0 3
PP_JIS(jis_num, yyjson_mut_is_num) 2 1 3
1139 3 ▶PP_JIS(jis_int, is_mut_int)
Branch: 3 branches
BranchTrueFalseTotal
PP_JIS(jis_int, is_mut_int) 0 3 3
PP_JIS(jis_int, is_mut_int) 3 0 3
PP_JIS(jis_int, is_mut_int) 2 1 3
1140 8 ▶PP_JIS(jis_real, yyjson_mut_is_real)
Branch: 3 branches
BranchTrueFalseTotal
PP_JIS(jis_real, yyjson_mut_is_real) 0 4 4
PP_JIS(jis_real, yyjson_mut_is_real) 4 0 4
PP_JIS(jis_real, yyjson_mut_is_real) 3 1 4
1141 12 ▶PP_JIS(jis_bool, yyjson_mut_is_bool)
Branch: 3 branches
BranchTrueFalseTotal
PP_JIS(jis_bool, yyjson_mut_is_bool) 0 6 6
PP_JIS(jis_bool, yyjson_mut_is_bool) 6 0 6
PP_JIS(jis_bool, yyjson_mut_is_bool) 5 1 6
1142 4 ▶PP_JIS(jis_null, yyjson_mut_is_null)
Branch: 3 branches
BranchTrueFalseTotal
PP_JIS(jis_null, yyjson_mut_is_null) 0 2 2
PP_JIS(jis_null, yyjson_mut_is_null) 2 0 2
PP_JIS(jis_null, yyjson_mut_is_null) 1 1 2
1143  
1144 #undef PP_JIS
1145  
1146 /* pp_jread: read JSON file → Doc */
1147 5 static OP * pp_jread_impl(pTHX) {
1148 5 dSP;
1149 5 SV *path_sv = POPs;
1150 STRLEN len;
1151 5 const char *path = SvPV(path_sv, len);
1152  
1153 yyjson_read_err err;
1154 5 yyjson_doc *idoc = yyjson_read_file(path, YYJSON_READ_NOFLAG, NULL, &err);
1155 5 ▶ if (!idoc)
Branch: 1 branch
BranchTrueFalseTotal
if (!idoc) 1 4 5
1156 1 ▶ croak("jread: %s: %s", path, err.msg ? err.msg : "read failed");
Branch: 1 branch
BranchTrueFalseTotal
croak("jread: %s: %s", path, err.msg ? err.msg : "read failed"); 1 0 1
1157 5 ▶ CHECK_DOC_DEPTH(idoc, MAX_DEPTH_DEFAULT, "jread");
Branch: 1 branch
BranchTrueFalseTotal
CHECK_DOC_DEPTH(idoc, MAX_DEPTH_DEFAULT, "jread"); 1 3 4
1158  
1159 3 yyjson_mut_doc *mdoc = yyjson_doc_mut_copy(idoc, NULL);
1160 yyjson_doc_free(idoc);
1161 3 ▶ if (!mdoc)
Branch: 1 branch
BranchTrueFalseTotal
if (!mdoc) 0 3 3
1162 0 croak("jread: failed to create mutable document");
1163  
1164 3 yyjson_mut_val *root = yyjson_mut_doc_get_root(mdoc);
1165 3 SV *result = new_doc_sv(aTHX_ mdoc, root, NULL);
1166 3 ▶ XPUSHs(sv_2mortal(result));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(result)); 0 3 3
1167 3 RETURN;
1168 }
1169  
1170 /* pp_jwrite: write Doc to JSON file */
1171 3 static OP * pp_jwrite_impl(pTHX) {
1172 3 dSP;
1173 3 SV *path_sv = POPs;
1174 3 SV *doc_sv = POPs;
1175 3 json_yy_doc_t *d = get_doc(aTHX_ doc_sv);
1176 STRLEN len;
1177 3 const char *path = SvPV(path_sv, len);
1178  
1179 yyjson_write_err werr;
1180 /* write the subtree root, not necessarily the full doc */
1181 3 yyjson_mut_doc *tmp_doc = yyjson_mut_doc_new(NULL);
1182 3 ▶ if (!tmp_doc)
Branch: 1 branch
BranchTrueFalseTotal
if (!tmp_doc) 0 3 3
1183 0 croak("jwrite: out of memory");
1184 3 yyjson_mut_val *copy = yyjson_mut_val_mut_copy(tmp_doc, d->root);
1185 3 ▶ if (!copy) {
Branch: 1 branch
BranchTrueFalseTotal
if (!copy) { 0 3 3
1186 0 yyjson_mut_doc_free(tmp_doc);
1187 0 croak("jwrite: out of memory");
1188 }
1189 yyjson_mut_doc_set_root(tmp_doc, copy);
1190  
1191 3 bool ok = yyjson_mut_write_file(path, tmp_doc, YYJSON_WRITE_PRETTY, NULL, &werr);
1192 3 yyjson_mut_doc_free(tmp_doc);
1193  
1194 3 ▶ if (!ok)
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) 0 3 3
1195 0 ▶ croak("jwrite: %s: %s", path, werr.msg ? werr.msg : "write failed");
Branch: 1 branch
BranchTrueFalseTotal
croak("jwrite: %s: %s", path, werr.msg ? werr.msg : "write failed"); 0 0 0
1196  
1197 3 ▶ XPUSHs(doc_sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(doc_sv); 0 3 3
1198 3 RETURN;
1199 }
1200  
1201 /* pp_jpaths: enumerate all leaf paths */
1202  
1203 /* returns 0, or 1 if the value nests deeper than the budget allows */
1204 static int
1205 534 ▶collect_paths(pTHX_ yyjson_mut_val *val, SV *prefix, AV *result, U32 budget) {
Branch: 1 branch
BranchTrueFalseTotal
collect_paths(pTHX_ yyjson_mut_val *val, SV *prefix, AV *result, U32 budget) { 534 0 534
1206 534 ▶ if (yyjson_mut_is_obj(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (yyjson_mut_is_obj(val)) { 528 6 534
1207 528 ▶ if (!budget) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!budget) return 1; 1 527 528
1208 size_t idx, max;
1209 yyjson_mut_val *key, *v;
1210 1077 ▶ yyjson_mut_obj_foreach(val, idx, max, key, v) {
Branch: 4 branches
BranchTrueFalseTotal
yyjson_mut_obj_foreach(val, idx, max, key, v) { 527 0 527
yyjson_mut_obj_foreach(val, idx, max, key, v) { 524 3 527
yyjson_mut_obj_foreach(val, idx, max, key, v) { 524 3 527
yyjson_mut_obj_foreach(val, idx, max, key, v) { 535 15 550
1211 535 ▶ const char *kstr = yyjson_mut_get_str(key);
Branch: 1 branch
BranchTrueFalseTotal
const char *kstr = yyjson_mut_get_str(key); 535 0 535
1212 535 size_t klen = yyjson_mut_get_len(key);
1213 535 SV *path = newSVsv(prefix);
1214 535 sv_catpvs(path, "/");
1215 /* escape ~ and / in key per RFC 6901 */
1216 535 const char *p = kstr;
1217 535 const char *end = kstr + klen;
1218 1072 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 537 535 1072
1219 537 const char *special = p;
1220 1102 ▶ while (special < end && *special != '~' && *special != '/')
Branch: 3 branches
BranchTrueFalseTotal
while (special < end && *special != '~' && *special != '/') 567 535 1102
while (special < end && *special != '~' && *special != '/') 566 1 567
while (special < end && *special != '~' && *special != '/') 565 1 566
1221 565 special++;
1222 537 ▶ if (special > p)
Branch: 1 branch
BranchTrueFalseTotal
if (special > p) 537 0 537
1223 537 sv_catpvn(path, p, special - p);
1224 537 ▶ if (special < end) {
Branch: 1 branch
BranchTrueFalseTotal
if (special < end) { 2 535 537
1225 2 ▶ if (*special == '~') sv_catpvs(path, "~0");
Branch: 1 branch
BranchTrueFalseTotal
if (*special == '~') sv_catpvs(path, "~0"); 1 1 2
1226 1 else sv_catpvs(path, "~1");
1227 2 special++;
1228 }
1229 537 p = special;
1230 }
1231 563 ▶ if (yyjson_mut_is_obj(v) || yyjson_mut_is_arr(v)) {
Branch: 2 branches
BranchTrueFalseTotal
if (yyjson_mut_is_obj(v) || yyjson_mut_is_arr(v)) { 16 519 535
if (yyjson_mut_is_obj(v) || yyjson_mut_is_arr(v)) { 5 11 16
1232 524 int deep = collect_paths(aTHX_ v, path, result, budget - 1);
1233 524 SvREFCNT_dec(path); /* path was used as prefix, not pushed */
1234 524 ▶ if (deep) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (deep) return 1; 512 12 524
1235 } else {
1236 11 av_push(result, path); /* transfers ownership */
1237 }
1238 }
1239 6 ▶ } else if (yyjson_mut_is_arr(val)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (yyjson_mut_is_arr(val)) { 5 1 6
1240 5 ▶ if (!budget) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!budget) return 1; 0 5 5
1241 size_t idx, max;
1242 yyjson_mut_val *item;
1243 19 ▶ yyjson_mut_arr_foreach(val, idx, max, item) {
Branch: 3 branches
BranchTrueFalseTotal
yyjson_mut_arr_foreach(val, idx, max, item) { 5 0 5
yyjson_mut_arr_foreach(val, idx, max, item) { 5 0 5
yyjson_mut_arr_foreach(val, idx, max, item) { 4 5 9
1244 4 SV *path = newSVsv(prefix);
1245 4 sv_catpvs(path, "/");
1246 char idxbuf[24];
1247 4 int idxlen = snprintf(idxbuf, sizeof(idxbuf), "%zu", idx);
1248 4 sv_catpvn(path, idxbuf, idxlen);
1249 8 ▶ if (yyjson_mut_is_obj(item) || yyjson_mut_is_arr(item)) {
Branch: 2 branches
BranchTrueFalseTotal
if (yyjson_mut_is_obj(item) || yyjson_mut_is_arr(item)) { 4 0 4
if (yyjson_mut_is_obj(item) || yyjson_mut_is_arr(item)) { 0 4 4
1250 0 int deep = collect_paths(aTHX_ item, path, result, budget - 1);
1251 0 SvREFCNT_dec(path);
1252 0 ▶ if (deep) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (deep) return 1; 0 0 0
1253 } else {
1254 4 av_push(result, path);
1255 }
1256 }
1257 } else {
1258 /* leaf -- the prefix itself is the path */
1259 1 av_push(result, newSVsv(prefix));
1260 }
1261 21 return 0;
1262 }
1263  
1264 10 static OP * pp_jpaths_impl(pTHX) {
1265 10 dSP;
1266 10 SV *path_sv = POPs;
1267 10 SV *doc_sv = POPs;
1268 10 json_yy_doc_t *d = get_doc(aTHX_ doc_sv);
1269 STRLEN path_len;
1270 10 const char *path = sv_pv_utf8_nomod(aTHX_ path_sv, &path_len);
1271  
1272 10 yyjson_mut_val *val = doc_resolve_path(aTHX_ d, path, path_len);
1273 10 ▶ if (!val)
Branch: 1 branch
BranchTrueFalseTotal
if (!val) 0 10 10
1274 0 croak("jpaths: path not found: %.*s", (int)path_len, path);
1275  
1276 10 AV *paths = newAV();
1277 10 SV *prefix = newSVpvn(path, path_len);
1278 10 int deep = collect_paths(aTHX_ val, prefix, paths, MAX_DEPTH_DEFAULT);
1279 10 SvREFCNT_dec(prefix);
1280 10 ▶ if (deep) {
Branch: 1 branch
BranchTrueFalseTotal
if (deep) { 1 9 10
1281 1 SvREFCNT_dec((SV *)paths);
1282 1 croak("jpaths: maximum nesting depth exceeded");
1283 }
1284  
1285 9 SSize_t count = av_len(paths) + 1;
1286 9 ▶ EXTEND(SP, count);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, count); 9 0 9
EXTEND(SP, count); 0 9 9
1287 25 ▶ for (SSize_t i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (SSize_t i = 0; i < count; i++) { 16 9 25
1288 16 SV **svp = av_fetch(paths, i, 0);
1289 16 SV *pv = *svp;
1290 /* a path with a non-ASCII key must come back flagged, or feeding it to
1291 jget/jencode re-reads those bytes as latin-1 (jkeys does the same) */
1292 16 ▶ if (!SvUTF8(pv) && !is_ascii(SvPVX(pv), SvCUR(pv)))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvUTF8(pv) && !is_ascii(SvPVX(pv), SvCUR(pv))) 16 0 16
if (!SvUTF8(pv) && !is_ascii(SvPVX(pv), SvCUR(pv))) 1 15 16
1293 1 SvUTF8_on(pv);
1294 16 PUSHs(sv_2mortal(SvREFCNT_inc(pv)));
1295 }
1296 9 SvREFCNT_dec((SV *)paths);
1297 9 RETURN;
1298 }
1299  
1300 /* pp_jfind: find first array element where key == value */
1301 40 static OP * pp_jfind_impl(pTHX) {
1302 40 dSP;
1303 40 SV *match_sv = POPs; /* value to match */
1304 40 SV *key_sv = POPs; /* key path within each element */
1305 40 SV *path_sv = POPs; /* array path */
1306 40 SV *doc_sv = POPs;
1307 40 json_yy_doc_t *d = get_doc(aTHX_ doc_sv);
1308 STRLEN path_len, key_len, match_len;
1309 40 const char *path = sv_pv_utf8_nomod(aTHX_ path_sv, &path_len);
1310 40 const char *key = sv_pv_utf8_nomod(aTHX_ key_sv, &key_len);
1311 40 const char *match = sv_pv_utf8_nomod(aTHX_ match_sv, &match_len);
1312  
1313 40 yyjson_mut_val *arr = doc_resolve_path(aTHX_ d, path, path_len);
1314 80 ▶ if (!arr || !yyjson_mut_is_arr(arr)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!arr || !yyjson_mut_is_arr(arr)) { 40 0 40
if (!arr || !yyjson_mut_is_arr(arr)) { 1 39 40
1315 1 ▶ XPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_undef); 0 1 1
1316 1 RETURN;
1317 }
1318  
1319 /* Classify the match once: a non-numeric string must not compare equal to
1320 a numeric field (SvNV("abc") is 0.0), and an integer must compare as
1321 64-bit so large ids do not collide through double rounding. */
1322 39 int m_num = 0, m_int = 0, m_neg = 0;
1323 39 UV m_uv = 0;
1324 39 NV m_nv = 0;
1325 39 ▶ if (SvIOK(match_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(match_sv)) { 13 26 39
1326 13 m_num = m_int = 1;
1327 13 ▶ if (SvIsUV(match_sv)) m_uv = SvUVX(match_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (SvIsUV(match_sv)) m_uv = SvUVX(match_sv); 2 11 13
1328 else {
1329 11 IV iv = SvIVX(match_sv);
1330 11 ▶ if (iv < 0) { m_neg = 1; m_uv = (UV)(-(iv + 1)) + 1; } else m_uv = (UV)iv;
Branch: 1 branch
BranchTrueFalseTotal
if (iv < 0) { m_neg = 1; m_uv = (UV)(-(iv + 1)) + 1; } else m_uv = (UV)iv; 3 8 11
1331 }
1332 26 ▶ } else if (SvNOK(match_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvNOK(match_sv)) { 2 24 26
1333 2 m_num = 1; m_nv = SvNVX(match_sv);
1334 } else {
1335 24 int t = grok_number(match, match_len, &m_uv);
1336 24 ▶ if (t && !(t & (IS_NUMBER_NAN | IS_NUMBER_INFINITY))) {
Branch: 2 branches
BranchTrueFalseTotal
if (t && !(t & (IS_NUMBER_NAN | IS_NUMBER_INFINITY))) { 12 12 24
if (t && !(t & (IS_NUMBER_NAN | IS_NUMBER_INFINITY))) { 12 0 12
1337 12 m_num = 1;
1338 12 m_neg = (t & IS_NUMBER_NEG) ? 1 : 0;
1339 12 ▶ if (!(t & IS_NUMBER_NOT_INT) && (t & IS_NUMBER_IN_UV)) m_int = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (!(t & IS_NUMBER_NOT_INT) && (t & IS_NUMBER_IN_UV)) m_int = 1; 9 3 12
if (!(t & IS_NUMBER_NOT_INT) && (t & IS_NUMBER_IN_UV)) m_int = 1; 9 0 9
1340 3 else m_nv = SvNV(match_sv);
1341 }
1342 }
1343  
1344 size_t idx, max;
1345 yyjson_mut_val *item;
1346 164 ▶ yyjson_mut_arr_foreach(arr, idx, max, item) {
Branch: 3 branches
BranchTrueFalseTotal
yyjson_mut_arr_foreach(arr, idx, max, item) { 39 0 39
yyjson_mut_arr_foreach(arr, idx, max, item) { 39 0 39
yyjson_mut_arr_foreach(arr, idx, max, item) { 80 6 86
1347 /* look up key within this element */
1348 80 yyjson_mut_val *field = NULL;
1349 80 ▶ if (key_len == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (key_len == 0) 2 78 80
1350 2 field = item;
1351 78 ▶ else if (yyjson_mut_is_obj(item) || yyjson_mut_is_arr(item))
Branch: 2 branches
BranchTrueFalseTotal
else if (yyjson_mut_is_obj(item) || yyjson_mut_is_arr(item)) 0 78 78
else if (yyjson_mut_is_obj(item) || yyjson_mut_is_arr(item)) 0 0 0
1352 156 ▶ field = yyjson_mut_ptr_getn(item, key, key_len);
Branch: 1 branch
BranchTrueFalseTotal
field = yyjson_mut_ptr_getn(item, key, key_len); 0 78 78
1353  
1354 80 ▶ if (!field) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!field) continue; 0 80 80
1355  
1356 /* compare: string match */
1357 80 ▶ if (yyjson_mut_is_str(field)) {
Branch: 1 branch
BranchTrueFalseTotal
if (yyjson_mut_is_str(field)) { 10 70 80
1358 20 ▶ if (yyjson_mut_equals_strn(field, match, match_len)) {
Branch: 2 branches
BranchTrueFalseTotal
if (yyjson_mut_equals_strn(field, match, match_len)) { 10 0 10
if (yyjson_mut_equals_strn(field, match, match_len)) { 4 6 10
1359 4 SV *result = new_doc_sv(aTHX_ d->doc, item, doc_sv);
1360 4 ▶ XPUSHs(sv_2mortal(result));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(result)); 0 4 4
1361 4 RETURN;
1362 }
1363 }
1364 /* compare: number. Integer matches use 64-bit compares so values above
1365 2^53 (e.g. Snowflake IDs) don't collide through double rounding. */
1366 94 ▶ else if (yyjson_mut_is_num(field) && m_num) {
Branch: 2 branches
BranchTrueFalseTotal
else if (yyjson_mut_is_num(field) && m_num) { 66 4 70
else if (yyjson_mut_is_num(field) && m_num) { 50 16 66
1367 50 yyjson_subtype st = yyjson_mut_get_subtype(field);
1368 int eq;
1369 50 ▶ if (m_int && st != YYJSON_SUBTYPE_REAL) {
Branch: 2 branches
BranchTrueFalseTotal
if (m_int && st != YYJSON_SUBTYPE_REAL) { 38 12 50
if (m_int && st != YYJSON_SUBTYPE_REAL) { 38 0 38
1370 38 ▶ if (!m_neg || m_uv == 0) /* -0 is 0 */
Branch: 2 branches
BranchTrueFalseTotal
if (!m_neg || m_uv == 0) /* -0 is 0 */ 13 25 38
if (!m_neg || m_uv == 0) /* -0 is 0 */ 1 12 13
1371 26 eq = (st == YYJSON_SUBTYPE_UINT)
1372 26 ? (yyjson_mut_get_uint(field) == (uint64_t)m_uv)
1373 52 ▶ : (yyjson_mut_get_sint(field) >= 0 &&
Branch: 2 branches
BranchTrueFalseTotal
: (yyjson_mut_get_sint(field) >= 0 && 26 0 26
: (yyjson_mut_get_sint(field) >= 0 && 0 0 0
1374 0 ▶ (uint64_t)yyjson_mut_get_sint(field) == (uint64_t)m_uv);
Branch: 1 branch
BranchTrueFalseTotal
(uint64_t)yyjson_mut_get_sint(field) == (uint64_t)m_uv); 0 0 0
1375 12 ▶ else if ((uint64_t)m_uv <= (uint64_t)INT64_MAX)
Branch: 1 branch
BranchTrueFalseTotal
else if ((uint64_t)m_uv <= (uint64_t)INT64_MAX) 10 2 12
1376 15 ▶ eq = (st == YYJSON_SUBTYPE_SINT) &&
Branch: 1 branch
BranchTrueFalseTotal
eq = (st == YYJSON_SUBTYPE_SINT) && 5 5 10
1377 5 ▶ (yyjson_mut_get_sint(field) == -(int64_t)m_uv);
Branch: 1 branch
BranchTrueFalseTotal
(yyjson_mut_get_sint(field) == -(int64_t)m_uv); 4 1 5
1378 else /* INT64_MIN is representable, -(int64_t)m_uv is not */
1379 2 eq = (st == YYJSON_SUBTYPE_SINT) &&
1380 4 ▶ ((uint64_t)m_uv == (uint64_t)INT64_MAX + 1) &&
Branch: 2 branches
BranchTrueFalseTotal
((uint64_t)m_uv == (uint64_t)INT64_MAX + 1) && 2 0 2
((uint64_t)m_uv == (uint64_t)INT64_MAX + 1) && 2 0 2
1381 2 ▶ (yyjson_mut_get_sint(field) == INT64_MIN);
Branch: 1 branch
BranchTrueFalseTotal
(yyjson_mut_get_sint(field) == INT64_MIN); 2 0 2
1382 } else {
1383 /* narrowed to double: yyjson stores reals as C double, and on a
1384 long-double perl a bare == would never be equal */
1385 12 ▶ NV nv = m_int ? (m_neg ? -(NV)m_uv : (NV)m_uv) : m_nv;
Branch: 3 branches
BranchTrueFalseTotal
NV nv = m_int ? (m_neg ? -(NV)m_uv : (NV)m_uv) : m_nv; 0 12 12
NV nv = m_int ? (m_neg ? -(NV)m_uv : (NV)m_uv) : m_nv; 0 0 0
NV nv = m_int ? (m_neg ? -(NV)m_uv : (NV)m_uv) : m_nv; 12 0 12
1386 12 eq = ((double)nv == yyjson_mut_get_num(field));
1387 }
1388 50 ▶ if (eq) {
Branch: 1 branch
BranchTrueFalseTotal
if (eq) { 26 24 50
1389 26 SV *result = new_doc_sv(aTHX_ d->doc, item, doc_sv);
1390 26 ▶ XPUSHs(sv_2mortal(result));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(result)); 0 26 26
1391 26 RETURN;
1392 }
1393 }
1394 /* compare: bool -- match against string "true"/"false" */
1395 20 ▶ else if (yyjson_mut_is_bool(field)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (yyjson_mut_is_bool(field)) { 3 17 20
1396 3 bool bval = yyjson_mut_get_bool(field);
1397 3 ▶ if ((bval && match_len == 4 && memcmp(match, "true", 4) == 0) ||
Branch: 3 branches
BranchTrueFalseTotal
if ((bval && match_len == 4 && memcmp(match, "true", 4) == 0) || 2 1 3
if ((bval && match_len == 4 && memcmp(match, "true", 4) == 0) || 1 1 2
if ((bval && match_len == 4 && memcmp(match, "true", 4) == 0) || 0 1 1
1398 2 ▶ (!bval && match_len == 5 && memcmp(match, "false", 5) == 0)) {
Branch: 3 branches
BranchTrueFalseTotal
(!bval && match_len == 5 && memcmp(match, "false", 5) == 0)) { 1 1 2
(!bval && match_len == 5 && memcmp(match, "false", 5) == 0)) { 1 0 1
(!bval && match_len == 5 && memcmp(match, "false", 5) == 0)) { 1 0 1
1399 2 SV *result = new_doc_sv(aTHX_ d->doc, item, doc_sv);
1400 2 ▶ XPUSHs(sv_2mortal(result));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(result)); 0 2 2
1401 2 RETURN;
1402 }
1403 }
1404 /* compare: null -- match against the string "null" */
1405 17 ▶ else if (yyjson_mut_is_null(field)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (yyjson_mut_is_null(field)) { 1 16 17
1406 1 ▶ if (match_len == 4 && memcmp(match, "null", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (match_len == 4 && memcmp(match, "null", 4) == 0) { 1 0 1
if (match_len == 4 && memcmp(match, "null", 4) == 0) { 1 0 1
1407 1 SV *result = new_doc_sv(aTHX_ d->doc, item, doc_sv);
1408 1 ▶ XPUSHs(sv_2mortal(result));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(result)); 0 1 1
1409 1 RETURN;
1410 }
1411 }
1412 }
1413  
1414 6 ▶ XPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_undef); 0 6 6
1415 6 RETURN;
1416 }
1417  
1418 /* ---- end Doc keyword ops ---- */
1419  
1420 /* check if a string is pure ASCII (no bytes >= 0x80) */
1421 static inline int
1422 118110 is_ascii(const char *s, size_t len) {
1423 118110 const unsigned char *p = (const unsigned char *)s;
1424 118110 size_t i = 0;
1425 2157562 ▶ for (; i + 7 < len; i += 8) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i + 7 < len; i += 8) { 2039464 118098 2157562
1426 uint64_t chunk;
1427 2039464 memcpy(&chunk, p + i, 8);
1428 2039464 ▶ if (chunk & UINT64_C(0x8080808080808080))
Branch: 1 branch
BranchTrueFalseTotal
if (chunk & UINT64_C(0x8080808080808080)) 12 2039452 2039464
1429 12 return 0;
1430 }
1431 370411 ▶ for (; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i < len; i++) { 252346 118065 370411
1432 252346 ▶ if (p[i] >= 0x80)
Branch: 1 branch
BranchTrueFalseTotal
if (p[i] >= 0x80) 33 252313 252346
1433 33 return 0;
1434 }
1435 118065 return 1;
1436 }
1437  
1438 /* ---- zero-copy string SV ---- */
1439 /* SvLEN=0 tells Perl it doesn't own the buffer; the bytes live inside the
1440 yyjson_doc. Each borrowing SV carries magic holding a (refcounted) ref to
1441 the doc holder, so the doc outlives every value that points into it --
1442 including a reference taken to a nested value (e.g. \$data->[0]) that
1443 outlives the root container. sv_setsv still copies, so values extracted by
1444 assignment remain independent. */
1445 static inline SV *
1446 2214 new_sv_zerocopy(pTHX_ const char *str, size_t len, SV *doc_sv) {
1447 2214 SV *sv = newSV_type(SVt_PV);
1448 2214 SvPV_set(sv, (char *)str);
1449 2214 SvCUR_set(sv, len);
1450 2214 SvLEN_set(sv, 0);
1451 2214 SvPOK_on(sv);
1452 2214 ▶ if (!is_ascii(str, len))
Branch: 1 branch
BranchTrueFalseTotal
if (!is_ascii(str, len)) 0 2214 2214
1453 0 SvUTF8_on(sv);
1454 2214 sv_magicext(sv, doc_sv, PERL_MAGIC_ext, &empty_vtbl, NULL, 0);
1455 2214 SvREADONLY_on(sv);
1456 2214 return sv;
1457 }
1458  
1459 /* ---- DECODE: yyjson value -> Perl SV ---- */
1460  
1461 /* Decoded booleans are copies of $JSON::PP::true/false, as in JSON::PP; only
1462 an undefined one is filled in, never a value the caller set. */
1463 static SV *
1464 30 get_bool_sv(pTHX_ bool val) {
1465 30 ▶ SV *sv = get_sv(val ? "JSON::PP::true" : "JSON::PP::false", GV_ADD);
Branch: 1 branch
BranchTrueFalseTotal
SV *sv = get_sv(val ? "JSON::PP::true" : "JSON::PP::false", GV_ADD); 15 15 30
1466 30 ▶ if (!SvOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) { 0 30 30
1467 0 SV *rv = newRV_noinc(newSViv(val ? 1 : 0));
1468 0 sv_bless(rv, gv_stashpv("JSON::PP::Boolean", GV_ADD));
1469 0 sv_setsv(sv, rv);
1470 0 SvREFCNT_dec(rv);
1471 }
1472 30 return sv;
1473 }
1474  
1475 static inline bool
1476 2500 bool_class(const char *name, STRLEN len) {
1477 2500 switch (len) {
1478 0 case 7: return memEQ(name, "boolean", 7);
1479 21 case 17: return memEQ(name, "JSON::PP::Boolean", 17)
1480 21 ▶ || memEQ(name, "JSON::XS::Boolean", 17);
Branch: 2 branches
BranchTrueFalseTotal
|| memEQ(name, "JSON::XS::Boolean", 17); 0 21 21
|| memEQ(name, "JSON::XS::Boolean", 17); 0 0 0
1481 0 case 26: return memEQ(name, "Types::Serialiser::Boolean", 26);
1482 0 case 30: return memEQ(name, "Types::Serialiser::BooleanBase", 30);
1483 }
1484 2479 return FALSE;
1485 }
1486  
1487 /* sv_derived_from would re-run get-magic on a tied element */
1488 static bool
1489 2498 is_bool_obj(pTHX_ SV *deref) {
1490 2498 AV *isa = mro_get_linear_isa(SvSTASH(deref));
1491 2498 SV **svp = AvARRAY(isa);
1492 SSize_t i;
1493 4977 ▶ for (i = 0; i <= AvFILLp(isa); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= AvFILLp(isa); i++) { 2500 2477 4977
1494 STRLEN len;
1495 2500 const char *name = SvPV_const(svp[i], len);
1496 2500 ▶ if (bool_class(name, len))
Branch: 1 branch
BranchTrueFalseTotal
if (bool_class(name, len)) 21 2479 2500
1497 21 return TRUE;
1498 }
1499 2477 return FALSE;
1500 }
1501  
1502 static inline bool
1503 21 bool_obj_is_true(pTHX_ SV *sv, SV *deref) {
1504 21 ▶ if (SvTYPE(deref) < SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(deref) < SVt_PVAV) 21 0 21
1505 21 return SvTRUE_nomg(deref);
1506 0 return SvTRUE_nomg(sv);
1507 }
1508  
1509 static SV *
1510 82152 ▶yyjson_val_to_sv(pTHX_ yyjson_val *val, U32 budget, const json_yy_decode_ctx_t *ctx) {
Branch: 1 branch
BranchTrueFalseTotal
yyjson_val_to_sv(pTHX_ yyjson_val *val, U32 budget, const json_yy_decode_ctx_t *ctx) { 82152 0 82152
1511 82152 switch (yyjson_get_type(val)) {
1512 5 case YYJSON_TYPE_NULL:
1513 5 return SvREFCNT_inc_simple_NN(&PL_sv_undef);
1514  
1515 32 case YYJSON_TYPE_BOOL:
1516 32 ▶ if (ctx && (ctx->flags & F_BOOL))
Branch: 2 branches
BranchTrueFalseTotal
if (ctx && (ctx->flags & F_BOOL)) 25 7 32
if (ctx && (ctx->flags & F_BOOL)) 20 5 25
1517 20 ▶ return newSVsv(yyjson_get_bool(val) ? ctx->bool_true : ctx->bool_false);
Branch: 1 branch
BranchTrueFalseTotal
return newSVsv(yyjson_get_bool(val) ? ctx->bool_true : ctx->bool_false); 13 7 20
1518 12 return yyjson_get_bool(val)
1519 7 ? SvREFCNT_inc_simple_NN(&PL_sv_yes)
1520 12 ▶ : SvREFCNT_inc_simple_NN(&PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
: SvREFCNT_inc_simple_NN(&PL_sv_no); 7 5 12
1521  
1522 26065 case YYJSON_TYPE_NUM:
1523 26065 return yyjson_num_to_sv(aTHX_ val);
1524  
1525 25616 ▶ case YYJSON_TYPE_STR: {
Branch: 1 branch
BranchTrueFalseTotal
case YYJSON_TYPE_STR: { 25616 0 25616
1526 25616 ▶ const char *str = yyjson_get_str(val);
Branch: 1 branch
BranchTrueFalseTotal
const char *str = yyjson_get_str(val); 25616 0 25616
1527 25616 size_t len = yyjson_get_len(val);
1528 25616 SV *sv = newSVpvn(str, len);
1529 /* only set UTF-8 flag if non-ASCII */
1530 25616 ▶ if (!is_ascii(str, len))
Branch: 1 branch
BranchTrueFalseTotal
if (!is_ascii(str, len)) 22 25594 25616
1531 22 SvUTF8_on(sv);
1532 25616 return sv;
1533 }
1534  
1535 10110 case YYJSON_TYPE_ARR: {
1536 10110 ▶ if (!budget) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!budget) return NULL; 0 10110 10110
1537 10110 size_t count = yyjson_arr_size(val);
1538 10110 AV *av = newAV();
1539 10110 ▶ if (count > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) 10105 5 10110
1540 10105 av_extend(av, (SSize_t)count - 1);
1541 10110 SV *rv = newRV_noinc((SV *)av);
1542 size_t idx, max;
1543 yyjson_val *item;
1544 106386 ▶ yyjson_arr_foreach(val, idx, max, item) {
Branch: 3 branches
BranchTrueFalseTotal
yyjson_arr_foreach(val, idx, max, item) { 10110 0 10110
yyjson_arr_foreach(val, idx, max, item) { 10110 0 10110
yyjson_arr_foreach(val, idx, max, item) { 38028 10110 48138
1545 38028 SV *item_sv = yyjson_val_to_sv(aTHX_ item, budget - 1, ctx);
1546 38028 ▶ if (!item_sv) { SvREFCNT_dec(rv); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!item_sv) { SvREFCNT_dec(rv); return NULL; } 0 38028 38028
1547 38028 av_push(av, item_sv);
1548 }
1549 10110 return rv;
1550 }
1551  
1552 20324 case YYJSON_TYPE_OBJ: {
1553 20324 ▶ if (!budget) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!budget) return NULL; 0 20324 20324
1554 20324 size_t count = yyjson_obj_size(val);
1555 20324 HV *hv = newHV();
1556 20324 ▶ if (count > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) 20324 0 20324
1557 20324 hv_ksplit(hv, count);
1558 20324 SV *rv = newRV_noinc((SV *)hv);
1559 size_t idx, max;
1560 yyjson_val *key, *value;
1561 113660 ▶ yyjson_obj_foreach(val, idx, max, key, value) {
Branch: 3 branches
BranchTrueFalseTotal
yyjson_obj_foreach(val, idx, max, key, value) { 20324 0 20324
yyjson_obj_foreach(val, idx, max, key, value) { 20324 0 20324
yyjson_obj_foreach(val, idx, max, key, value) { 26344 20324 46668
1562 26344 ▶ const char *kstr = yyjson_get_str(key);
Branch: 1 branch
BranchTrueFalseTotal
const char *kstr = yyjson_get_str(key); 26344 0 26344
1563 26344 STRLEN klen = (STRLEN)yyjson_get_len(key);
1564 26344 SV *val_sv = yyjson_val_to_sv(aTHX_ value, budget - 1, ctx);
1565 26344 ▶ if (!val_sv) { SvREFCNT_dec(rv); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!val_sv) { SvREFCNT_dec(rv); return NULL; } 0 26344 26344
1566 26344 ▶ if (!is_ascii(kstr, klen))
Branch: 1 branch
BranchTrueFalseTotal
if (!is_ascii(kstr, klen)) 2 26342 26344
1567 2 hv_store(hv, kstr, -(I32)klen, val_sv, 0);
1568 else
1569 26342 hv_store(hv, kstr, (I32)klen, val_sv, 0);
1570 }
1571 20324 return rv;
1572 }
1573  
1574 0 default:
1575 0 return SvREFCNT_inc_simple_NN(&PL_sv_undef);
1576 }
1577 }
1578  
1579 /* ---- DECODE: yyjson mutable value -> Perl SV ---- */
1580  
1581 static SV *
1582 624 ▶yyjson_mut_val_to_sv(pTHX_ yyjson_mut_val *val, U32 budget) {
Branch: 1 branch
BranchTrueFalseTotal
yyjson_mut_val_to_sv(pTHX_ yyjson_mut_val *val, U32 budget) { 624 0 624
1583 624 switch (yyjson_mut_get_type(val)) {
1584 1 case YYJSON_TYPE_NULL:
1585 1 return SvREFCNT_inc_simple_NN(&PL_sv_undef);
1586  
1587 2 ▶ case YYJSON_TYPE_BOOL:
Branch: 1 branch
BranchTrueFalseTotal
case YYJSON_TYPE_BOOL: 2 0 2
1588 2 return yyjson_mut_get_bool(val)
1589 1 ? SvREFCNT_inc_simple_NN(&PL_sv_yes)
1590 2 ▶ : SvREFCNT_inc_simple_NN(&PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
: SvREFCNT_inc_simple_NN(&PL_sv_no); 1 1 2
1591  
1592 57 case YYJSON_TYPE_NUM:
1593 57 return yyjson_mut_num_to_sv(aTHX_ val);
1594  
1595 44 ▶ case YYJSON_TYPE_STR: {
Branch: 1 branch
BranchTrueFalseTotal
case YYJSON_TYPE_STR: { 44 0 44
1596 44 ▶ const char *str = yyjson_mut_get_str(val);
Branch: 1 branch
BranchTrueFalseTotal
const char *str = yyjson_mut_get_str(val); 44 0 44
1597 44 size_t len = yyjson_mut_get_len(val);
1598 44 SV *sv = newSVpvn(str, len);
1599 44 ▶ if (!is_ascii(str, len))
Branch: 1 branch
BranchTrueFalseTotal
if (!is_ascii(str, len)) 5 39 44
1600 5 SvUTF8_on(sv);
1601 44 return sv;
1602 }
1603  
1604 6 case YYJSON_TYPE_ARR: {
1605 6 ▶ if (!budget) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!budget) return NULL; 0 6 6
1606 6 size_t count = yyjson_mut_arr_size(val);
1607 6 AV *av = newAV();
1608 6 ▶ if (count > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) 6 0 6
1609 6 av_extend(av, (SSize_t)count - 1);
1610 6 SV *rv = newRV_noinc((SV *)av);
1611 size_t idx, max;
1612 yyjson_mut_val *item;
1613 33 ▶ yyjson_mut_arr_foreach(val, idx, max, item) {
Branch: 3 branches
BranchTrueFalseTotal
yyjson_mut_arr_foreach(val, idx, max, item) { 6 0 6
yyjson_mut_arr_foreach(val, idx, max, item) { 6 0 6
yyjson_mut_arr_foreach(val, idx, max, item) { 15 6 21
1614 15 SV *item_sv = yyjson_mut_val_to_sv(aTHX_ item, budget - 1);
1615 15 ▶ if (!item_sv) { SvREFCNT_dec(rv); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!item_sv) { SvREFCNT_dec(rv); return NULL; } 0 15 15
1616 15 av_push(av, item_sv);
1617 }
1618 6 return rv;
1619 }
1620  
1621 514 case YYJSON_TYPE_OBJ: {
1622 514 ▶ if (!budget) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!budget) return NULL; 1 513 514
1623 513 size_t count = yyjson_mut_obj_size(val);
1624 513 HV *hv = newHV();
1625 513 ▶ if (count > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) 513 0 513
1626 513 hv_ksplit(hv, count);
1627 513 SV *rv = newRV_noinc((SV *)hv);
1628 size_t idx, max;
1629 yyjson_mut_val *key, *value;
1630 1029 ▶ yyjson_mut_obj_foreach(val, idx, max, key, value) {
Branch: 4 branches
BranchTrueFalseTotal
yyjson_mut_obj_foreach(val, idx, max, key, value) { 513 0 513
yyjson_mut_obj_foreach(val, idx, max, key, value) { 513 0 513
yyjson_mut_obj_foreach(val, idx, max, key, value) { 513 0 513
yyjson_mut_obj_foreach(val, idx, max, key, value) { 515 1 516
1631 515 ▶ const char *kstr = yyjson_mut_get_str(key);
Branch: 1 branch
BranchTrueFalseTotal
const char *kstr = yyjson_mut_get_str(key); 515 0 515
1632 515 STRLEN klen = (STRLEN)yyjson_mut_get_len(key);
1633 515 SV *val_sv = yyjson_mut_val_to_sv(aTHX_ value, budget - 1);
1634 515 ▶ if (!val_sv) { SvREFCNT_dec(rv); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!val_sv) { SvREFCNT_dec(rv); return NULL; } 512 3 515
1635 3 ▶ if (!is_ascii(kstr, klen))
Branch: 1 branch
BranchTrueFalseTotal
if (!is_ascii(kstr, klen)) 0 3 3
1636 0 hv_store(hv, kstr, -(I32)klen, val_sv, 0);
1637 else
1638 3 hv_store(hv, kstr, (I32)klen, val_sv, 0);
1639 }
1640 1 return rv;
1641 }
1642  
1643 0 default:
1644 0 return SvREFCNT_inc_simple_NN(&PL_sv_undef);
1645 }
1646 }
1647  
1648 /* ---- zero-copy readonly decoder ---- */
1649 /* doc_sv: an SV holding the yyjson_doc* (refcounted, freed on DESTROY) */
1650  
1651 static SV *
1652 5439 ▶yyjson_val_to_sv_ro(pTHX_ yyjson_val *val, SV *doc_sv, U32 budget) {
Branch: 1 branch
BranchTrueFalseTotal
yyjson_val_to_sv_ro(pTHX_ yyjson_val *val, SV *doc_sv, U32 budget) { 5439 0 5439
1653 5439 switch (yyjson_get_type(val)) {
1654 1 case YYJSON_TYPE_NULL:
1655 1 return SvREFCNT_inc_simple_NN(&PL_sv_undef);
1656  
1657 3 ▶ case YYJSON_TYPE_BOOL:
Branch: 1 branch
BranchTrueFalseTotal
case YYJSON_TYPE_BOOL: 3 0 3
1658 3 return yyjson_get_bool(val)
1659 2 ? SvREFCNT_inc_simple_NN(&PL_sv_yes)
1660 3 ▶ : SvREFCNT_inc_simple_NN(&PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
: SvREFCNT_inc_simple_NN(&PL_sv_no); 2 1 3
1661  
1662 1007 case YYJSON_TYPE_NUM: {
1663 1007 SV *nsv = yyjson_num_to_sv(aTHX_ val);
1664 1007 SvREADONLY_on(nsv);
1665 1007 return nsv;
1666 }
1667  
1668 2214 ▶ case YYJSON_TYPE_STR:
Branch: 1 branch
BranchTrueFalseTotal
case YYJSON_TYPE_STR: 2214 0 2214
1669 /* zero-copy: SV borrows string memory from yyjson_doc */
1670 4428 ▶ return new_sv_zerocopy(aTHX_
Branch: 1 branch
BranchTrueFalseTotal
return new_sv_zerocopy(aTHX_ 2214 0 2214
1671 yyjson_get_str(val), yyjson_get_len(val), doc_sv);
1672  
1673 1205 case YYJSON_TYPE_ARR: {
1674 1205 ▶ if (!budget) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!budget) return NULL; 0 1205 1205
1675 1205 size_t count = yyjson_arr_size(val);
1676 1205 AV *av = newAV();
1677 1205 ▶ if (count > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) 1205 0 1205
1678 1205 av_extend(av, (SSize_t)count - 1);
1679 1205 SV *rv = newRV_noinc((SV *)av);
1680 size_t idx, max;
1681 yyjson_val *item;
1682 8035 ▶ yyjson_arr_foreach(val, idx, max, item) {
Branch: 3 branches
BranchTrueFalseTotal
yyjson_arr_foreach(val, idx, max, item) { 1205 0 1205
yyjson_arr_foreach(val, idx, max, item) { 1205 0 1205
yyjson_arr_foreach(val, idx, max, item) { 2210 1205 3415
1683 2210 SV *item_sv = yyjson_val_to_sv_ro(aTHX_ item, doc_sv, budget - 1);
1684 2210 ▶ if (!item_sv) { SvREFCNT_dec(rv); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!item_sv) { SvREFCNT_dec(rv); return NULL; } 0 2210 2210
1685 2210 av_push(av, item_sv);
1686 }
1687 1205 SvREADONLY_on((SV *)av);
1688 1205 return rv;
1689 }
1690  
1691 1009 case YYJSON_TYPE_OBJ: {
1692 1009 ▶ if (!budget) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!budget) return NULL; 0 1009 1009
1693 1009 size_t count = yyjson_obj_size(val);
1694 1009 HV *hv = newHV();
1695 1009 ▶ if (count > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) 1009 0 1009
1696 1009 hv_ksplit(hv, count);
1697 1009 SV *rv = newRV_noinc((SV *)hv);
1698 size_t idx, max;
1699 yyjson_val *key, *value;
1700 7057 ▶ yyjson_obj_foreach(val, idx, max, key, value) {
Branch: 3 branches
BranchTrueFalseTotal
yyjson_obj_foreach(val, idx, max, key, value) { 1009 0 1009
yyjson_obj_foreach(val, idx, max, key, value) { 1009 0 1009
yyjson_obj_foreach(val, idx, max, key, value) { 2015 1009 3024
1701 2015 ▶ const char *kstr = yyjson_get_str(key);
Branch: 1 branch
BranchTrueFalseTotal
const char *kstr = yyjson_get_str(key); 2015 0 2015
1702 2015 STRLEN klen = (STRLEN)yyjson_get_len(key);
1703 2015 SV *val_sv = yyjson_val_to_sv_ro(aTHX_ value, doc_sv, budget - 1);
1704 2015 ▶ if (!val_sv) { SvREFCNT_dec(rv); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!val_sv) { SvREFCNT_dec(rv); return NULL; } 0 2015 2015
1705 2015 ▶ if (!is_ascii(kstr, klen))
Branch: 1 branch
BranchTrueFalseTotal
if (!is_ascii(kstr, klen)) 0 2015 2015
1706 0 hv_store(hv, kstr, -(I32)klen, val_sv, 0);
1707 else
1708 2015 hv_store(hv, kstr, (I32)klen, val_sv, 0);
1709 }
1710 1009 SvREADONLY_on((SV *)hv);
1711 1009 return rv;
1712 }
1713  
1714 0 default:
1715 0 return SvREFCNT_inc_simple_NN(&PL_sv_undef);
1716 }
1717 }
1718  
1719 /* opaque SV releasing the yyjson_doc when the last reference across all
1720 interpreters goes */
1721 static SV *
1722 1214 new_doc_holder(pTHX_ yyjson_doc *doc) {
1723 json_yy_ro_t *h;
1724 1214 SV *sv = newSV(0);
1725 1214 h = (json_yy_ro_t *)malloc(sizeof(json_yy_ro_t));
1726 1214 ▶ if (!h) croak("out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!h) croak("out of memory"); 0 1214 1214
1727 1214 h->doc = doc;
1728 1214 h->refcnt = 1;
1729 1214 attach_ext_magic(aTHX_ sv, &docholder_magic_vtbl, h);
1730 1214 return sv;
1731 }
1732  
1733 /* ---- DIRECT ENCODE: single-pass SV -> JSON bytes ---- */
1734 /* Bypasses yyjson_mut_doc entirely for maximum throughput */
1735  
1736 /* 0 = passthrough, 1 = \uXXXX, other = the char after a backslash,
1737 ESC_WIDEN = latin-1 byte to widen. The ASCII half is shared. */
1738 #define ESC_WIDEN 0xff
1739  
1740 #define ESCAPE_TABLE_ASCII_HALF \
1741 /* 0x00-0x1f: control characters need \uXXXX */ \
1742 1,1,1,1,1,1,1,1, 'b','t','n',1,'f','r',1,1, \
1743 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, \
1744 /* 0x20-0x7f */ \
1745 0,0,'"',0,0,0,0,0, 0,0,0,0,0,0,0,0, /* " at 0x22 */ \
1746 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, /* 0x30-0x3f */ \
1747 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, /* 0x40-0x4f */ \
1748 0,0,0,0,0,0,0,0, 0,0,0,0,'\\',0,0,0, /* \\ at 0x5c */ \
1749 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, \
1750 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0
1751  
1752 #define ESCAPE_TABLE_HIGH_HALF(v) \
1753 v,v,v,v,v,v,v,v, v,v,v,v,v,v,v,v, \
1754 v,v,v,v,v,v,v,v, v,v,v,v,v,v,v,v, \
1755 v,v,v,v,v,v,v,v, v,v,v,v,v,v,v,v, \
1756 v,v,v,v,v,v,v,v, v,v,v,v,v,v,v,v, \
1757 v,v,v,v,v,v,v,v, v,v,v,v,v,v,v,v, \
1758 v,v,v,v,v,v,v,v, v,v,v,v,v,v,v,v, \
1759 v,v,v,v,v,v,v,v, v,v,v,v,v,v,v,v, \
1760 v,v,v,v,v,v,v,v, v,v,v,v,v,v,v,v
1761  
1762 /* source is UTF-8: high bytes pass through untouched */
1763 static const uint8_t escape_table[256] = {
1764 ESCAPE_TABLE_ASCII_HALF,
1765 ESCAPE_TABLE_HIGH_HALF(0)
1766 };
1767  
1768 /* no UTF8 flag: high bytes are latin-1 and must be widened. A table rather
1769 than an extra test keeps the run scan at one lookup per byte. */
1770 static const uint8_t escape_table_latin1[256] = {
1771 ESCAPE_TABLE_ASCII_HALF,
1772 ESCAPE_TABLE_HIGH_HALF(ESC_WIDEN)
1773 };
1774  
1775 /* Room for `need` more bytes plus the NUL. Compared additively: the
1776 subtractive form underflows once cur reaches SvLEN, disabling all growth. */
1777 static inline void
1778 146697 buf_ensure(pTHX_ SV *buf, size_t need) {
1779 146697 STRLEN cur = SvCUR(buf);
1780 146697 ▶ if (SvLEN(buf) < cur + need + 1)
Branch: 1 branch
BranchTrueFalseTotal
if (SvLEN(buf) < cur + need + 1) 28772 117925 146697
1781 28772 ▶ SvGROW(buf, (cur + need + 1) * 2);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(buf, (cur + need + 1) * 2); 28772 0 28772
SvGROW(buf, (cur + need + 1) * 2); 28772 0 28772
1782 146697 }
1783  
1784 static inline void
1785 87284 buf_cat_c(pTHX_ SV *buf, char c) {
1786 87284 buf_ensure(aTHX_ buf, 1);
1787 87284 char *p = SvPVX(buf) + SvCUR(buf);
1788 87284 *p = c;
1789 87284 SvCUR_set(buf, SvCUR(buf) + 1);
1790 87284 }
1791  
1792 static inline void
1793 2482 buf_cat_mem(pTHX_ SV *buf, const char *s, size_t n) {
1794 2482 buf_ensure(aTHX_ buf, n);
1795 2482 char *p = SvPVX(buf) + SvCUR(buf);
1796 2482 memcpy(p, s, n);
1797 2482 SvCUR_set(buf, SvCUR(buf) + n);
1798 2482 }
1799  
1800 /* Does the string need escaping? high_too also reports bytes >= 0x80 (latin-1
1801 needing widening), riding along in the same scan. */
1802 static inline int
1803 49493 needs_escape(const char *s, size_t len, int high_too) {
1804 /* check 8 bytes at a time for common case (no control chars, no " or \) */
1805 /* bytes needing escape: 0x00-0x1f, 0x22 ("), 0x5c (\) */
1806 49493 const unsigned char *p = (const unsigned char *)s;
1807 /* masks rather than branches, so the ASCII case is unchanged */
1808 49493 ▶ const uint64_t hi_mask = high_too ? UINT64_C(0x8080808080808080) : 0;
Branch: 1 branch
BranchTrueFalseTotal
const uint64_t hi_mask = high_too ? UINT64_C(0x8080808080808080) : 0; 49488 5 49493
1809 49493 ▶ const unsigned char hi_byte = high_too ? 0x80 : 0;
Branch: 1 branch
BranchTrueFalseTotal
const unsigned char hi_byte = high_too ? 0x80 : 0; 49488 5 49493
1810 49493 size_t i = 0;
1811 1560861 ▶ for (; i + 7 < len; i += 8) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i + 7 < len; i += 8) { 1536867 23994 1560861
1812 uint64_t chunk;
1813 1536867 memcpy(&chunk, p + i, 8);
1814 1536867 ▶ if (chunk & hi_mask)
Branch: 1 branch
BranchTrueFalseTotal
if (chunk & hi_mask) 7 1536860 1536867
1815 25499 return 1;
1816 /* any byte < 0x20? subtract 0x20 from each byte; underflow sets high bit */
1817 1536860 ▶ if ((chunk - UINT64_C(0x2020202020202020)) & ~chunk & UINT64_C(0x8080808080808080))
Branch: 1 branch
BranchTrueFalseTotal
if ((chunk - UINT64_C(0x2020202020202020)) & ~chunk & UINT64_C(0x8080808080808080)) 20628 1516232 1536860
1818 20628 return 1;
1819 /* check for " (0x22) or \ (0x5c) byte by byte in chunk */
1820 1516232 uint64_t xor_quote = chunk ^ UINT64_C(0x2222222222222222);
1821 1516232 uint64_t xor_bslash = chunk ^ UINT64_C(0x5c5c5c5c5c5c5c5c);
1822 /* a byte is zero iff (v - 0x01) & ~v & 0x80 */
1823 #define HAS_ZERO(v) (((v) - UINT64_C(0x0101010101010101)) & ~(v) & UINT64_C(0x8080808080808080))
1824 1516232 ▶ if (HAS_ZERO(xor_quote) || HAS_ZERO(xor_bslash))
Branch: 2 branches
BranchTrueFalseTotal
if (HAS_ZERO(xor_quote) || HAS_ZERO(xor_bslash)) 1513800 2432 1516232
if (HAS_ZERO(xor_quote) || HAS_ZERO(xor_bslash)) 2432 1511368 1513800
1825 4864 return 1;
1826 #undef HAS_ZERO
1827 }
1828 56573 ▶ for (; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i < len; i++) { 32842 23731 56573
1829 32842 ▶ if (escape_table[p[i]] | (p[i] & hi_byte))
Branch: 1 branch
BranchTrueFalseTotal
if (escape_table[p[i]] | (p[i] & hi_byte)) 263 32579 32842
1830 263 return 1;
1831 }
1832 23731 return 0;
1833 }
1834  
1835 /* latin1: the source had no UTF8 flag, so bytes >= 0x80 are latin-1 characters
1836 needing widening to UTF-8. Callers pass 0 for UTF-8 or ASCII input. */
1837 /* the body is big enough that a plain `inline` hint is ignored, and the two
1838 instantiations below are only worth anything if `latin1` folds away */
1839 #if defined(__GNUC__) || defined(__clang__)
1840 # define JSON_YY_FORCE_INLINE inline __attribute__((always_inline))
1841 #elif defined(_MSC_VER)
1842 # define JSON_YY_FORCE_INLINE __forceinline
1843 #else
1844 # define JSON_YY_FORCE_INLINE inline
1845 #endif
1846  
1847 static JSON_YY_FORCE_INLINE void
1848 buf_cat_escaped_impl(pTHX_ SV *buf, const char *s, size_t len, const int latin1) {
1849 /* fast path: no escaping needed (very common for JSON keys/values).
1850 With latin1 set the scan also rejects high bytes, so reaching here
1851 means the memcpy below is byte-identical UTF-8. */
1852 49493 if (!needs_escape(s, len, latin1)) {
1853 23731 buf_ensure(aTHX_ buf, len + 2);
1854 23731 char *out = SvPVX(buf) + SvCUR(buf);
1855 23731 *out++ = '"';
1856 23731 memcpy(out, s, len);
1857 23731 out += len;
1858 23731 *out++ = '"';
1859 23731 SvCUR_set(buf, out - SvPVX(buf));
1860 23731 return;
1861 }
1862  
1863 /* slow path: need escaping (and/or widening) */
1864 static const char hex_digits[] = "0123456789abcdef";
1865 25762 const uint8_t *tbl = latin1 ? escape_table_latin1 : escape_table;
1866 25762 buf_ensure(aTHX_ buf, len + 2 + 16); /* some headroom */
1867 25762 char *out = SvPVX(buf) + SvCUR(buf);
1868 /* SvLEN-1 is reserved for the NUL the callers write at SvPVX[SvCUR];
1869 reaching it would push SvCUR to SvLEN and defeat buf_ensure */
1870 25762 char *out_end = SvPVX(buf) + SvLEN(buf) - 2;
1871 25762 *out++ = '"';
1872  
1873 25762 const char *end = s + len;
1874 693045 ▶ while (s < end) {
Branch: 2 branches
BranchTrueFalseTotal
while (s < end) { 667283 25762 693045
while (s < end) { 0 0 0
1875 /* ensure we have room for at least one escaped char */
1876 667283 ▶ if (out + 8 > out_end) {
Branch: 2 branches
BranchTrueFalseTotal
if (out + 8 > out_end) { 5860 661423 667283
if (out + 8 > out_end) { 0 0 0
1877 5860 SvCUR_set(buf, out - SvPVX(buf));
1878 5860 buf_ensure(aTHX_ buf, (end - s) * 2 + 8);
1879 5860 out = SvPVX(buf) + SvCUR(buf);
1880 5860 out_end = SvPVX(buf) + SvLEN(buf) - 2;
1881 }
1882  
1883 667283 unsigned char c = *s;
1884 667283 uint8_t esc = tbl[c];
1885 667283 ▶ if (!esc) {
Branch: 2 branches
BranchTrueFalseTotal
if (!esc) { 25723 641560 667283
if (!esc) { 0 0 0
1886 /* scan for run of safe chars */
1887 25723 const char *safe = s + 1;
1888 1274229 ▶ while (safe < end && !tbl[(unsigned char)*safe])
Branch: 4 branches
BranchTrueFalseTotal
while (safe < end && !tbl[(unsigned char)*safe]) 1248914 25315 1274229
while (safe < end && !tbl[(unsigned char)*safe]) 1248506 408 1248914
while (safe < end && !tbl[(unsigned char)*safe]) 0 0 0
while (safe < end && !tbl[(unsigned char)*safe]) 0 0 0
1889 1248506 safe++;
1890 25723 size_t n = safe - s;
1891 25723 ▶ if (out + n > out_end) {
Branch: 2 branches
BranchTrueFalseTotal
if (out + n > out_end) { 1578 24145 25723
if (out + n > out_end) { 0 0 0
1892 1578 SvCUR_set(buf, out - SvPVX(buf));
1893 1578 buf_ensure(aTHX_ buf, n + (end - safe) * 2 + 8);
1894 1578 out = SvPVX(buf) + SvCUR(buf);
1895 1578 out_end = SvPVX(buf) + SvLEN(buf) - 2;
1896 }
1897 25723 memcpy(out, s, n);
1898 25723 out += n;
1899 25723 s = safe;
1900 641560 ▶ } else if (esc == ESC_WIDEN) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (esc == ESC_WIDEN) { 869 640691 641560
} else if (esc == ESC_WIDEN) { 0 0 0
1901 /* latin-1 character: re-encode as two UTF-8 bytes */
1902 869 *out++ = (char)(0xc0 | (c >> 6));
1903 869 *out++ = (char)(0x80 | (c & 0x3f));
1904 869 s++;
1905 640691 ▶ } else if (esc > 1) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (esc > 1) { 301227 339464 640691
} else if (esc > 1) { 0 0 0
1906 301227 *out++ = '\\';
1907 301227 *out++ = (char)esc;
1908 301227 s++;
1909 } else {
1910 339464 *out++ = '\\'; *out++ = 'u'; *out++ = '0'; *out++ = '0';
1911 339464 *out++ = hex_digits[c >> 4];
1912 339464 *out++ = hex_digits[c & 0x0f];
1913 339464 s++;
1914 }
1915 }
1916 25762 *out++ = '"';
1917 25762 SvCUR_set(buf, out - SvPVX(buf));
1918 }
1919  
1920 /* Instantiated twice so `latin1` folds to a constant in each: the UTF-8
1921 variant loses the widening branch, the mask and the table indirection. */
1922 static void
1923 5 buf_cat_escaped_utf8(pTHX_ SV *buf, const char *s, size_t len) {
1924 buf_cat_escaped_impl(aTHX_ buf, s, len, 0);
1925 5 }
1926 static void
1927 49488 buf_cat_escaped_latin1(pTHX_ SV *buf, const char *s, size_t len) {
1928 buf_cat_escaped_impl(aTHX_ buf, s, len, 1);
1929 49488 }
1930 static inline void
1931 49493 buf_cat_escaped_str(pTHX_ SV *buf, const char *s, size_t len, int latin1) {
1932 49493 ▶ if (latin1) buf_cat_escaped_latin1(aTHX_ buf, s, len);
Branch: 1 branch
BranchTrueFalseTotal
if (latin1) buf_cat_escaped_latin1(aTHX_ buf, s, len); 49488 5 49493
1933 5 else buf_cat_escaped_utf8(aTHX_ buf, s, len);
1934 49493 }
1935  
1936 /* fast unsigned integer to buffer */
1937 static void
1938 2438 buf_cat_uv(pTHX_ SV *buf, UV val) {
1939 char tmp[24];
1940 2438 char *p = tmp + sizeof(tmp);
1941 2438 ▶ if (val == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (val == 0) { 1 2437 2438
1942 1 *--p = '0';
1943 } else {
1944 9356 ▶ while (val) {
Branch: 1 branch
BranchTrueFalseTotal
while (val) { 6919 2437 9356
1945 6919 *--p = '0' + (val % 10);
1946 6919 val /= 10;
1947 }
1948 }
1949 2438 buf_cat_mem(aTHX_ buf, p, (tmp + sizeof(tmp)) - p);
1950 2438 }
1951  
1952 static void
1953 2438 buf_cat_iv(pTHX_ SV *buf, IV val) {
1954 2438 ▶ if (val < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (val < 0) { 2 2436 2438
1955 2 buf_cat_c(aTHX_ buf, '-');
1956 /* handle IV_MIN carefully */
1957 2 buf_cat_uv(aTHX_ buf, (UV)(-(val + 1)) + 1);
1958 } else {
1959 2436 buf_cat_uv(aTHX_ buf, (UV)val);
1960 }
1961 2438 }
1962  
1963 static void
1964 22 buf_cat_nv(pTHX_ SV *buf, NV val) {
1965 /* Stringify via Perl's NV->PV: sv_2pv does the correct per-nvtype thing
1966 (Gconvert for double/long double, quadmath_snprintf for __float128) and
1967 forces a C-locale '.' radix -- avoiding both the quadmath Gconvert
1968 garbage (2.5 -> "0") and the LC_NUMERIC "2,5" bug. NaN/Inf are rejected
1969 by the caller. */
1970 STRLEN len;
1971 22 SV *tmp = sv_2mortal(newSVnv(val));
1972 22 const char *s = SvPV_const(tmp, len);
1973 /* NV_DIG digits can round a finite value past the largest double, giving
1974 a number our own decoder rejects as infinity. Only huge values can. */
1975 22 ▶ if (UNLIKELY(val > (NV)1.0e308 || val < (NV)-1.0e308)) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNLIKELY(val > (NV)1.0e308 || val < (NV)-1.0e308)) { 19 3 22
if (UNLIKELY(val > (NV)1.0e308 || val < (NV)-1.0e308)) { 0 19 19
1976 3 SV *back = sv_2mortal(newSVpvn(s, len));
1977 3 ▶ if (Perl_isinf(SvNV(back))) {
Branch: 2 branches
BranchTrueFalseTotal
if (Perl_isinf(SvNV(back))) { 3 0 3
if (Perl_isinf(SvNV(back))) { 3 0 3
1978 3 sv_setpvs(tmp, "");
1979 3 Perl_sv_catpvf(aTHX_ tmp, "%.*" NVgf, (int)NV_DIG + 3, val);
1980 3 s = SvPV_const(tmp, len);
1981 }
1982 }
1983 22 buf_cat_mem(aTHX_ buf, s, len);
1984 22 }
1985  
1986 static json_yy_t default_self = { F_UTF8 | F_ALLOW_NONREF, MAX_DEPTH_DEFAULT };
1987  
1988 /* A scalar can hold a string and a number at once. Encode as a number only
1989 when the number's own text is exactly the string: "42" stays 42, while
1990 "007", "42\n" and $! stay strings. Matches Cpanel::JSON::XS. */
1991 static int
1992 13 sv_num_is_faithful(pTHX_ SV *sv, const char *str, STRLEN len) {
1993 13 ▶ if (SvIOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(sv)) { 11 2 13
1994 char buf[32];
1995 11 char *end = buf + sizeof(buf);
1996 11 char *p = end;
1997 UV uv;
1998 11 int neg = 0;
1999 11 ▶ if (SvIsUV(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvIsUV(sv)) 0 11 11
2000 0 uv = SvUVX(sv);
2001 else {
2002 11 IV iv = SvIVX(sv);
2003 11 ▶ if (iv < 0) { neg = 1; uv = (UV)(-(iv + 1)) + 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (iv < 0) { neg = 1; uv = (UV)(-(iv + 1)) + 1; } 0 11 11
2004 11 else uv = (UV)iv;
2005 }
2006 11 ▶ if (!uv) *--p = '0';
Branch: 1 branch
BranchTrueFalseTotal
if (!uv) *--p = '0'; 0 11 11
2007 29 ▶ else while (uv) { *--p = (char)('0' + (uv % 10)); uv /= 10; }
Branch: 1 branch
BranchTrueFalseTotal
else while (uv) { *--p = (char)('0' + (uv % 10)); uv /= 10; } 18 11 29
2008 11 ▶ if (neg) *--p = '-';
Branch: 1 branch
BranchTrueFalseTotal
if (neg) *--p = '-'; 0 11 11
2009 11 ▶ return (STRLEN)(end - p) == len && memcmp(p, str, len) == 0;
Branch: 2 branches
BranchTrueFalseTotal
return (STRLEN)(end - p) == len && memcmp(p, str, len) == 0; 3 8 11
return (STRLEN)(end - p) == len && memcmp(p, str, len) == 0; 3 0 3
2010 }
2011 2 ▶ if (SvNOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOK(sv)) { 2 0 2
2012 /* same stringification the encoder would emit (see buf_cat_nv) */
2013 STRLEN nlen;
2014 2 SV *tmp = sv_2mortal(newSVnv(SvNVX(sv)));
2015 2 const char *n = SvPV_const(tmp, nlen);
2016 2 ▶ return nlen == len && memcmp(n, str, len) == 0;
Branch: 2 branches
BranchTrueFalseTotal
return nlen == len && memcmp(n, str, len) == 0; 1 1 2
return nlen == len && memcmp(n, str, len) == 0; 1 0 1
2017 }
2018 0 return 0;
2019 }
2020  
2021 /* true/false decode to PL_sv_yes/PL_sv_no and re-encode as 1/0; PL_sv_no's PV
2022 is "", which would otherwise fail the test above and emit an empty string. */
2023 #ifdef SvIsBOOL
2024 # define SV_IS_BOOL(sv) (SvIsBOOL(sv) || (sv) == &PL_sv_yes || (sv) == &PL_sv_no)
2025 #else
2026 /* no bool flag before 5.36: match the shape perl gives a copy of !!1 / !!0 --
2027 all three slots set, with PV "1"/IV 1 or PV ""/IV 0. An ordinary string that
2028 was merely used numerically does not get all three. */
2029 # define SV_IS_BOOL(sv) \
2030 ((sv) == &PL_sv_yes || (sv) == &PL_sv_no || \
2031 (SvIOK(sv) && SvNOK(sv) && SvPOK(sv) && \
2032 ((SvCUR(sv) == 0 && SvIVX(sv) == 0) || \
2033 (SvCUR(sv) == 1 && SvPVX(sv)[0] == '1' && SvIVX(sv) == 1))))
2034 #endif
2035  
2036 static void
2037 65497 direct_encode_sv(pTHX_ SV *buf, SV *sv, U32 depth, json_yy_t *self) {
2038 65497 ▶ if (depth > self->max_depth)
Branch: 1 branch
BranchTrueFalseTotal
if (depth > self->max_depth) 2 65495 65497
2039 2 croak("maximum nesting depth exceeded");
2040  
2041 65495 SvGETMAGIC(sv); /* tied values arrive unresolved and would encode as null */
2042  
2043 65495 ▶ if (!SvOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) { 2 65493 65495
2044 2 buf_cat_mem(aTHX_ buf, "null", 4);
2045 2 return;
2046 }
2047  
2048 65493 ▶ if (SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) { 33320 32173 65493
2049 33320 SV *deref = SvRV(sv);
2050  
2051 33320 ▶ if (SvOBJECT(deref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOBJECT(deref)) { 1457 31863 33320
2052 1457 ▶ if (is_bool_obj(aTHX_ deref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_bool_obj(aTHX_ deref)) { 14 1443 1457
2053 14 ▶ if (bool_obj_is_true(aTHX_ sv, deref))
Branch: 1 branch
BranchTrueFalseTotal
if (bool_obj_is_true(aTHX_ sv, deref)) 7 7 14
2054 7 buf_cat_mem(aTHX_ buf, "true", 4);
2055 else
2056 7 buf_cat_mem(aTHX_ buf, "false", 5);
2057 14 return;
2058 }
2059  
2060 /* convert_blessed applies only if the class has a TO_JSON;
2061 otherwise fall through to allow_blessed, as JSON::XS does */
2062 2883 if ((self->flags & F_CONVERT_BLESSED) &&
2063 1440 gv_fetchmethod_autoload(SvSTASH(deref), "TO_JSON", 0)) {
2064 1434 dSP;
2065 1434 ENTER; SAVETMPS;
2066 1434 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1434 1434
2067 1434 ▶ XPUSHs(sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv); 0 1434 1434
2068 1434 PUTBACK;
2069 1434 int count = call_method("TO_JSON", G_SCALAR | G_EVAL);
2070 1434 SPAGAIN;
2071 1434 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 1434 0 1434
if (SvTRUE(ERRSV)) { 1 1433 1434
2072 1 ▶ SV *err = ERRSV;
Branch: 1 branch
BranchTrueFalseTotal
SV *err = ERRSV; 1 0 1
2073 1 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 1 0 1
2074 1 croak("TO_JSON method failed: %" SVf, SVfARG(err));
2075 }
2076 1433 ▶ SV *result = count > 0 ? POPs : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *result = count > 0 ? POPs : &PL_sv_undef; 1433 0 1433
2077 1433 SvREFCNT_inc(result);
2078 1433 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 1433 0 1433
2079 /* mortalize rather than an explicit dec: if the recursive
2080 encode croaks on an unencodable TO_JSON result, the mortal
2081 is still freed on unwind (an explicit dec would be skipped). */
2082 1433 sv_2mortal(result);
2083 /* depth+1: a TO_JSON returning its own object would otherwise
2084 recurse forever past max_depth */
2085 1433 direct_encode_sv(aTHX_ buf, result, depth + 1, self);
2086 403 return;
2087 }
2088 9 ▶ if (self->flags & F_ALLOW_BLESSED) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->flags & F_ALLOW_BLESSED) { 3 6 9
2089 3 buf_cat_mem(aTHX_ buf, "null", 4);
2090 3 return;
2091 }
2092 6 croak("encountered object '%s', but neither allow_blessed nor "
2093 "convert_blessed is enabled (or the TO_JSON method is missing)",
2094 sv_reftype(deref, 1));
2095 }
2096  
2097 /* scalar ref: boolean */
2098 31863 ▶ if (SvTYPE(deref) < SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(deref) < SVt_PVAV) { 1 31862 31863
2099 1 ▶ if (SvTRUE(deref))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(deref)) 1 0 1
2100 1 buf_cat_mem(aTHX_ buf, "true", 4);
2101 else
2102 0 buf_cat_mem(aTHX_ buf, "false", 5);
2103 1 return;
2104 }
2105  
2106 31862 ▶ if (SvTYPE(deref) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(deref) == SVt_PVAV) { 13075 18787 31862
2107 13075 AV *av = (AV *)deref;
2108 13075 SSize_t len = av_len(av) + 1;
2109 13075 ▶ if (depth >= self->max_depth) /* count containers, as decode does */
Branch: 1 branch
BranchTrueFalseTotal
if (depth >= self->max_depth) /* count containers, as decode does */ 2 13073 13075
2110 2 croak("maximum nesting depth exceeded");
2111 13073 buf_cat_c(aTHX_ buf, '[');
2112 30439 ▶ for (SSize_t i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (SSize_t i = 0; i < len; i++) { 18393 12046 30439
2113 18393 ▶ if (i) buf_cat_c(aTHX_ buf, ',');
Branch: 1 branch
BranchTrueFalseTotal
if (i) buf_cat_c(aTHX_ buf, ','); 5323 13070 18393
2114 18393 SV **elem = av_fetch(av, i, 0);
2115 18393 ▶ direct_encode_sv(aTHX_ buf, elem ? *elem : &PL_sv_undef,
Branch: 1 branch
BranchTrueFalseTotal
direct_encode_sv(aTHX_ buf, elem ? *elem : &PL_sv_undef, 18393 0 18393
2116 depth + 1, self);
2117 }
2118 12046 buf_cat_c(aTHX_ buf, ']');
2119 12046 return;
2120 }
2121  
2122 18787 ▶ if (SvTYPE(deref) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(deref) == SVt_PVHV) { 18787 0 18787
2123 18787 HV *hv = (HV *)deref;
2124 18787 ▶ if (depth >= self->max_depth) /* count containers, as decode does */
Branch: 1 branch
BranchTrueFalseTotal
if (depth >= self->max_depth) /* count containers, as decode does */ 5 18782 18787
2125 5 croak("maximum nesting depth exceeded");
2126 18782 buf_cat_c(aTHX_ buf, '{');
2127 18782 hv_iterinit(hv);
2128 HE *he;
2129 18782 int first = 1;
2130 /* HeVAL() is NULL on a tied hash; hv_iterval() runs FETCH.
2131 Guarded so plain hashes keep the inline fast path. */
2132 18782 const bool magical = cBOOL(SvRMAGICAL(hv));
2133 37032 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 19798 17234 37032
2134 19798 ▶ if (!first) buf_cat_c(aTHX_ buf, ',');
Branch: 1 branch
BranchTrueFalseTotal
if (!first) buf_cat_c(aTHX_ buf, ','); 1016 18782 19798
2135 19798 first = 0;
2136 STRLEN klen;
2137 19803 ▶ const char *kstr = HePV(he, klen);
Branch: 1 branch
BranchTrueFalseTotal
const char *kstr = HePV(he, klen); 5 19793 19798
2138 19798 ▶ buf_cat_escaped_str(aTHX_ buf, kstr, klen, !HeUTF8(he));
Branch: 1 branch
BranchTrueFalseTotal
buf_cat_escaped_str(aTHX_ buf, kstr, klen, !HeUTF8(he)); 5 19793 19798
2139 19798 buf_cat_c(aTHX_ buf, ':');
2140 19805 ▶ direct_encode_sv(aTHX_ buf,
Branch: 1 branch
BranchTrueFalseTotal
direct_encode_sv(aTHX_ buf, 7 19791 19798
2141 7 magical ? hv_iterval(hv, he) : HeVAL(he),
2142 depth + 1, self);
2143 }
2144 17234 buf_cat_c(aTHX_ buf, '}');
2145 17234 return;
2146 }
2147  
2148 0 ▶ if (self->flags & F_ALLOW_UNKNOWN) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->flags & F_ALLOW_UNKNOWN) { 0 0 0
2149 0 buf_cat_mem(aTHX_ buf, "null", 4);
2150 0 return;
2151 }
2152 0 croak("cannot encode reference to %s", sv_reftype(deref, 0));
2153 }
2154  
2155 32173 ▶ if (SvNOK(sv) && (Perl_isnan(SvNVX(sv)) || Perl_isinf(SvNVX(sv))))
Branch: 3 branches
BranchTrueFalseTotal
if (SvNOK(sv) && (Perl_isnan(SvNVX(sv)) || Perl_isinf(SvNVX(sv)))) 39 32134 32173
if (SvNOK(sv) && (Perl_isnan(SvNVX(sv)) || Perl_isinf(SvNVX(sv)))) 37 2 39
if (SvNOK(sv) && (Perl_isnan(SvNVX(sv)) || Perl_isinf(SvNVX(sv)))) 4 33 37
2156 6 croak("cannot encode NaN or Infinity as JSON");
2157  
2158 32167 ▶ if (SV_IS_BOOL(sv)) {
Branch: 3 branches
BranchTrueFalseTotal
if (SV_IS_BOOL(sv)) { 32157 10 32167
if (SV_IS_BOOL(sv)) { 32157 0 32157
if (SV_IS_BOOL(sv)) { 0 32157 32157
2159 10 ▶ buf_cat_c(aTHX_ buf, SvTRUE(sv) ? '1' : '0');
Branch: 1 branch
BranchTrueFalseTotal
buf_cat_c(aTHX_ buf, SvTRUE(sv) ? '1' : '0'); 5 5 10
2160 10 return;
2161 }
2162  
2163 /* SvPOKp, not SvPOK: stringifying a number leaves only the private flag */
2164 32157 ▶ if (SvPOKp(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOKp(sv)) { 29698 2459 32157
2165 STRLEN len;
2166 29698 const char *str = SvPV_nomg(sv, len); /* get magic ran on entry */
2167 29698 ▶ if ((SvIOK(sv) || SvNOK(sv)) && sv_num_is_faithful(aTHX_ sv, str, len)) {
Branch: 3 branches
BranchTrueFalseTotal
if ((SvIOK(sv) || SvNOK(sv)) && sv_num_is_faithful(aTHX_ sv, str, len)) { 29691 7 29698
if ((SvIOK(sv) || SvNOK(sv)) && sv_num_is_faithful(aTHX_ sv, str, len)) { 2 29689 29691
if ((SvIOK(sv) || SvNOK(sv)) && sv_num_is_faithful(aTHX_ sv, str, len)) { 3 6 9
2168 /* NVs go through buf_cat_nv even so, for the near-DBL_MAX fixup */
2169 3 ▶ if (SvIOK(sv)) buf_cat_mem(aTHX_ buf, str, len);
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(sv)) buf_cat_mem(aTHX_ buf, str, len); 2 1 3
2170 1 else buf_cat_nv(aTHX_ buf, SvNVX(sv));
2171 3 return;
2172 }
2173 29695 buf_cat_escaped_str(aTHX_ buf, str, len, !SvUTF8(sv));
2174 29695 return;
2175 }
2176  
2177 2459 ▶ if (SvIOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(sv)) { 2438 21 2459
2178 2438 ▶ if (SvIsUV(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvIsUV(sv)) 0 2438 2438
2179 0 buf_cat_uv(aTHX_ buf, SvUVX(sv));
2180 else
2181 2438 buf_cat_iv(aTHX_ buf, SvIVX(sv));
2182 2438 return;
2183 }
2184  
2185 21 ▶ if (SvNOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOK(sv)) { 21 0 21
2186 21 buf_cat_nv(aTHX_ buf, SvNVX(sv));
2187 21 return;
2188 }
2189  
2190 0 buf_cat_mem(aTHX_ buf, "null", 4);
2191 }
2192  
2193 /* ---- ENCODE: Perl SV -> yyjson mutable value (used for OO API) ---- */
2194  
2195 static yyjson_mut_val *
2196 48318 sv_to_yyjson_val(pTHX_ yyjson_mut_doc *doc, SV *sv, json_yy_t *self, U32 depth) {
2197 48318 ▶ if (depth > self->max_depth)
Branch: 1 branch
BranchTrueFalseTotal
if (depth > self->max_depth) 2 48316 48318
2198 2 croak("maximum nesting depth exceeded");
2199  
2200 48316 SvGETMAGIC(sv); /* see direct_encode_sv() */
2201  
2202 48316 ▶ if (!SvOK(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) 1 48315 48316
2203 1 return yyjson_mut_null(doc);
2204  
2205 48315 ▶ if (SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) { 18240 30075 48315
2206 18240 SV *deref = SvRV(sv);
2207  
2208 /* check for blessed objects */
2209 18240 ▶ if (SvOBJECT(deref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOBJECT(deref)) { 1041 17199 18240
2210 1041 ▶ if (is_bool_obj(aTHX_ deref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_bool_obj(aTHX_ deref)) { 7 1034 1041
2211 7 return bool_obj_is_true(aTHX_ sv, deref)
2212 4 ? yyjson_mut_bool(doc, 1)
2213 14 ▶ : yyjson_mut_bool(doc, 0);
Branch: 1 branch
BranchTrueFalseTotal
: yyjson_mut_bool(doc, 0); 4 3 7
2214 }
2215  
2216 /* convert_blessed: call TO_JSON, if the class has one (see
2217 direct_encode_sv) */
2218 2068 if ((self->flags & F_CONVERT_BLESSED) &&
2219 1034 gv_fetchmethod_autoload(SvSTASH(deref), "TO_JSON", 0)) {
2220 1031 dSP;
2221 1031 ENTER; SAVETMPS;
2222 1031 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1031 1031
2223 1031 ▶ XPUSHs(sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv); 0 1031 1031
2224 1031 PUTBACK;
2225 1031 int count = call_method("TO_JSON", G_SCALAR | G_EVAL);
2226 1031 SPAGAIN;
2227 1031 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 1031 0 1031
if (SvTRUE(ERRSV)) { 0 1031 1031
2228 0 ▶ SV *err = ERRSV;
Branch: 1 branch
BranchTrueFalseTotal
SV *err = ERRSV; 0 0 0
2229 0 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 0 0 0
2230 0 croak("TO_JSON method failed: %" SVf, SVfARG(err));
2231 }
2232 1031 ▶ SV *result = count > 0 ? POPs : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *result = count > 0 ? POPs : &PL_sv_undef; 1031 0 1031
2233 1031 SvREFCNT_inc(result);
2234 1031 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 1031 0 1031
2235 /* mortalize rather than an explicit dec: if the recursive
2236 encode croaks on an unencodable TO_JSON result, the mortal
2237 is still freed on unwind (an explicit dec would be skipped). */
2238 1031 sv_2mortal(result);
2239 /* depth+1: see direct_encode_sv() */
2240 1031 return sv_to_yyjson_val(aTHX_ doc, result, self, depth + 1);
2241 }
2242 /* allow_blessed: encode as null */
2243 3 ▶ if (self->flags & F_ALLOW_BLESSED)
Branch: 1 branch
BranchTrueFalseTotal
if (self->flags & F_ALLOW_BLESSED) 3 0 3
2244 3 return yyjson_mut_null(doc);
2245 0 croak("encountered object '%s', but neither allow_blessed nor "
2246 "convert_blessed is enabled (or the TO_JSON method is missing)",
2247 sv_reftype(deref, 1));
2248 }
2249  
2250 /* scalar ref: \1 = true, \0 = false */
2251 17199 ▶ if (SvTYPE(deref) < SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(deref) < SVt_PVAV) { 1 17198 17199
2252 1 return SvTRUE(deref)
2253 1 ? yyjson_mut_bool(doc, 1)
2254 2 ▶ : yyjson_mut_bool(doc, 0);
Branch: 1 branch
BranchTrueFalseTotal
: yyjson_mut_bool(doc, 0); 1 0 1
2255 }
2256  
2257 17198 switch (SvTYPE(deref)) {
2258 6553 case SVt_PVAV: {
2259 6553 AV *av = (AV *)deref;
2260 6553 ▶ if (depth >= self->max_depth) /* see direct_encode_sv() */
Branch: 1 branch
BranchTrueFalseTotal
if (depth >= self->max_depth) /* see direct_encode_sv() */ 3 6550 6553
2261 3 croak("maximum nesting depth exceeded");
2262 6550 yyjson_mut_val *arr = yyjson_mut_arr(doc);
2263 6550 SSize_t len = av_len(av) + 1;
2264 21575 ▶ for (SSize_t i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (SSize_t i = 0; i < len; i++) { 16561 5014 21575
2265 16561 SV **elem = av_fetch(av, i, 0);
2266 16561 ▶ yyjson_mut_val *v = sv_to_yyjson_val(aTHX_ doc, elem ? *elem : &PL_sv_undef, self, depth + 1);
Branch: 1 branch
BranchTrueFalseTotal
yyjson_mut_val *v = sv_to_yyjson_val(aTHX_ doc, elem ? *elem : &PL_sv_undef, self, depth + 1); 16561 0 16561
2267 yyjson_mut_arr_append(arr, v);
2268 }
2269 5014 return arr;
2270 }
2271  
2272 10645 case SVt_PVHV: {
2273 10645 HV *hv = (HV *)deref;
2274 10645 ▶ if (depth >= self->max_depth) /* see direct_encode_sv() */
Branch: 1 branch
BranchTrueFalseTotal
if (depth >= self->max_depth) /* see direct_encode_sv() */ 0 10645 10645
2275 0 croak("maximum nesting depth exceeded");
2276 10645 yyjson_mut_val *obj = yyjson_mut_obj(doc);
2277 10645 hv_iterinit(hv);
2278 HE *he;
2279 10645 const bool magical = cBOOL(SvRMAGICAL(hv)); /* see direct_encode_sv() */
2280 25683 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 15038 10645 25683
2281 STRLEN klen;
2282 15042 ▶ const char *kstr = HePV(he, klen);
Branch: 1 branch
BranchTrueFalseTotal
const char *kstr = HePV(he, klen); 4 15034 15038
2283 15038 ▶ if (!HeUTF8(he) && !is_ascii(kstr, klen)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!HeUTF8(he) && !is_ascii(kstr, klen)) { 4 15034 15038
if (!HeUTF8(he) && !is_ascii(kstr, klen)) { 15037 1 15038
if (!HeUTF8(he) && !is_ascii(kstr, klen)) { 2 15035 15037
2284 /* latin-1 key: widen, or yyjson rejects it on write */
2285 2 SV *ktmp = sv_2mortal(newSVpvn(kstr, klen));
2286 2 sv_utf8_upgrade(ktmp);
2287 2 kstr = SvPV_const(ktmp, klen);
2288 }
2289 /* copy the key before hv_iterval() runs FETCH, which
2290 could invalidate the entry kstr points into */
2291 15038 ▶ yyjson_mut_val *k = yyjson_mut_strncpy(doc, kstr, klen);
Branch: 1 branch
BranchTrueFalseTotal
yyjson_mut_val *k = yyjson_mut_strncpy(doc, kstr, klen); 15038 0 15038
2292 15038 ▶ SV *val = magical ? hv_iterval(hv, he) : HeVAL(he);
Branch: 1 branch
BranchTrueFalseTotal
SV *val = magical ? hv_iterval(hv, he) : HeVAL(he); 4 15034 15038
2293 15038 yyjson_mut_val *v = sv_to_yyjson_val(aTHX_ doc, val, self, depth + 1);
2294 yyjson_mut_obj_add(obj, k, v);
2295 }
2296 10645 return obj;
2297 }
2298  
2299 0 default:
2300 0 ▶ if (self->flags & F_ALLOW_UNKNOWN)
Branch: 1 branch
BranchTrueFalseTotal
if (self->flags & F_ALLOW_UNKNOWN) 0 0 0
2301 0 return yyjson_mut_null(doc);
2302 0 croak("cannot encode reference to %s", sv_reftype(deref, 0));
2303 }
2304 }
2305  
2306 30075 ▶ if (SvNOK(sv) && (Perl_isnan(SvNVX(sv)) || Perl_isinf(SvNVX(sv))))
Branch: 3 branches
BranchTrueFalseTotal
if (SvNOK(sv) && (Perl_isnan(SvNVX(sv)) || Perl_isinf(SvNVX(sv)))) 4 30071 30075
if (SvNOK(sv) && (Perl_isnan(SvNVX(sv)) || Perl_isinf(SvNVX(sv)))) 4 0 4
if (SvNOK(sv) && (Perl_isnan(SvNVX(sv)) || Perl_isinf(SvNVX(sv)))) 2 2 4
2307 2 croak("cannot encode NaN or Infinity as JSON");
2308  
2309 30073 ▶ if (SV_IS_BOOL(sv))
Branch: 3 branches
BranchTrueFalseTotal
if (SV_IS_BOOL(sv)) 30073 0 30073
if (SV_IS_BOOL(sv)) 30073 0 30073
if (SV_IS_BOOL(sv)) 0 30073 30073
2310 0 ▶ return yyjson_mut_sint(doc, SvTRUE(sv) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
return yyjson_mut_sint(doc, SvTRUE(sv) ? 1 : 0); 0 0 0
2311  
2312 /* see sv_num_is_faithful(). One SvPV for the whole branch: fetching twice
2313 could emit a different string than the one just tested. */
2314 30073 ▶ if (SvPOKp(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOKp(sv)) { 5024 25049 30073
2315 STRLEN len;
2316 5024 const char *str = SvPV_nomg(sv, len); /* get magic ran on entry */
2317 5024 ▶ if ((SvIOK(sv) || SvNOK(sv)) && sv_num_is_faithful(aTHX_ sv, str, len)) {
Branch: 3 branches
BranchTrueFalseTotal
if ((SvIOK(sv) || SvNOK(sv)) && sv_num_is_faithful(aTHX_ sv, str, len)) { 5020 4 5024
if ((SvIOK(sv) || SvNOK(sv)) && sv_num_is_faithful(aTHX_ sv, str, len)) { 0 5020 5020
if ((SvIOK(sv) || SvNOK(sv)) && sv_num_is_faithful(aTHX_ sv, str, len)) { 1 3 4
2318 1 ▶ if (SvIOK(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(sv)) 1 0 1
2319 0 ▶ return SvIsUV(sv) ? yyjson_mut_uint(doc, (uint64_t)SvUVX(sv))
Branch: 1 branch
BranchTrueFalseTotal
return SvIsUV(sv) ? yyjson_mut_uint(doc, (uint64_t)SvUVX(sv)) 0 0 0
2320 2 ▶ : yyjson_mut_sint(doc, (int64_t)SvIVX(sv));
Branch: 2 branches
BranchTrueFalseTotal
: yyjson_mut_sint(doc, (int64_t)SvIVX(sv)); 0 1 1
: yyjson_mut_sint(doc, (int64_t)SvIVX(sv)); 1 0 1
2321 0 ▶ return yyjson_mut_real(doc, SvNVX(sv));
Branch: 1 branch
BranchTrueFalseTotal
return yyjson_mut_real(doc, SvNVX(sv)); 0 0 0
2322 }
2323 5023 ▶ if (!SvUTF8(sv) && !is_ascii(str, len)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvUTF8(sv) && !is_ascii(str, len)) { 5023 0 5023
if (!SvUTF8(sv) && !is_ascii(str, len)) { 4 5019 5023
2324 /* latin-1: widen on a copy, never in the caller's SV */
2325 4 SV *tmp = sv_2mortal(newSVpvn(str, len));
2326 4 sv_utf8_upgrade(tmp);
2327 4 str = SvPV_const(tmp, len);
2328 }
2329 10046 ▶ return yyjson_mut_strncpy(doc, str, len);
Branch: 1 branch
BranchTrueFalseTotal
return yyjson_mut_strncpy(doc, str, len); 5023 0 5023
2330 }
2331  
2332 25049 ▶ if (SvIOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(sv)) { 25047 2 25049
2333 25047 ▶ if (SvIsUV(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvIsUV(sv)) 0 25047 25047
2334 0 ▶ return yyjson_mut_uint(doc, (uint64_t)SvUVX(sv));
Branch: 1 branch
BranchTrueFalseTotal
return yyjson_mut_uint(doc, (uint64_t)SvUVX(sv)); 0 0 0
2335 50094 ▶ return yyjson_mut_sint(doc, (int64_t)SvIVX(sv));
Branch: 1 branch
BranchTrueFalseTotal
return yyjson_mut_sint(doc, (int64_t)SvIVX(sv)); 25047 0 25047
2336 }
2337  
2338 2 ▶ if (SvNOK(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOK(sv)) 2 0 2
2339 4 ▶ return yyjson_mut_real(doc, SvNVX(sv));
Branch: 1 branch
BranchTrueFalseTotal
return yyjson_mut_real(doc, SvNVX(sv)); 2 0 2
2340  
2341 0 return yyjson_mut_null(doc);
2342 }
2343  
2344 /* ---- custom ops for keyword API ---- */
2345  
2346 /* pp function for decode_json keyword */
2347 static OP *
2348 17766 pp_decode_json_impl(pTHX) {
2349 17766 dSP;
2350 17766 SV *json_sv = POPs;
2351 STRLEN len;
2352 17766 const char *json = SvPV(json_sv, len);
2353  
2354 yyjson_read_err err;
2355 17766 yyjson_doc *doc = yyjson_read_opts((char *)json, len, YYJSON_READ_NOFLAG, NULL, &err);
2356 17766 ▶ if (!doc)
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) 3 17763 17766
2357 3 croak("JSON decode error: %s at byte offset %zu", err.msg, err.pos);
2358 17770 ▶ CHECK_DOC_DEPTH(doc, MAX_DEPTH_DEFAULT, "JSON decode error");
Branch: 1 branch
BranchTrueFalseTotal
CHECK_DOC_DEPTH(doc, MAX_DEPTH_DEFAULT, "JSON decode error"); 7 17756 17763
2359  
2360 17756 yyjson_val *root = yyjson_doc_get_root(doc);
2361 17756 ▶ if (!root) {
Branch: 1 branch
BranchTrueFalseTotal
if (!root) { 0 17756 17756
2362 yyjson_doc_free(doc);
2363 0 croak("JSON decode error: empty document");
2364 }
2365  
2366 17756 SV *result = yyjson_val_to_sv(aTHX_ root, MAX_DEPTH_DEFAULT, NULL);
2367 yyjson_doc_free(doc);
2368 17756 ▶ if (!result)
Branch: 1 branch
BranchTrueFalseTotal
if (!result) 0 17756 17756
2369 0 croak("JSON decode error: maximum nesting depth exceeded");
2370  
2371 17756 ▶ XPUSHs(sv_2mortal(result));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(result)); 0 17756 17756
2372 17756 RETURN;
2373 }
2374  
2375 /* pp function for encode_json keyword */
2376 static OP *
2377 24387 pp_encode_json_impl(pTHX) {
2378 24387 dSP;
2379 24387 SV *data = POPs;
2380  
2381 24387 SV *result = newSV(64);
2382 24387 SvPOK_on(result);
2383 24387 SvCUR_set(result, 0);
2384 /* mortalize before encoding: direct_encode_sv can croak (max depth,
2385 NaN/Inf, unencodable ref) and an unguarded result would leak.
2386 The OO encoders use SAVEFREESV for the same reason. */
2387 24387 sv_2mortal(result);
2388 24387 direct_encode_sv(aTHX_ result, data, 0, &default_self);
2389 24376 *(SvPVX(result) + SvCUR(result)) = '\0';
2390  
2391 24376 ▶ XPUSHs(result);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(result); 0 24376 24376
2392 24376 RETURN;
2393 }
2394  
2395 /* pp function for decode_json_ro keyword */
2396 static OP *
2397 1216 pp_decode_json_ro_impl(pTHX) {
2398 1216 dSP;
2399 1216 SV *json_sv = POPs;
2400 STRLEN len;
2401 1216 const char *json = SvPV(json_sv, len);
2402  
2403 yyjson_read_err err;
2404 1216 yyjson_doc *doc = yyjson_read_opts((char *)json, len, YYJSON_READ_NOFLAG, NULL, &err);
2405 1216 ▶ if (!doc)
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) 1 1215 1216
2406 1 croak("JSON decode error: %s at byte offset %zu", err.msg, err.pos);
2407 1216 ▶ CHECK_DOC_DEPTH(doc, MAX_DEPTH_DEFAULT, "JSON decode error");
Branch: 1 branch
BranchTrueFalseTotal
CHECK_DOC_DEPTH(doc, MAX_DEPTH_DEFAULT, "JSON decode error"); 1 1214 1215
2408  
2409 1214 yyjson_val *root = yyjson_doc_get_root(doc);
2410 1214 ▶ if (!root) {
Branch: 1 branch
BranchTrueFalseTotal
if (!root) { 0 1214 1214
2411 yyjson_doc_free(doc);
2412 0 croak("JSON decode error: empty document");
2413 }
2414  
2415 1214 SV *doc_sv = new_doc_holder(aTHX_ doc);
2416 1214 SV *result = yyjson_val_to_sv_ro(aTHX_ root, doc_sv, MAX_DEPTH_DEFAULT);
2417 1214 ▶ if (!result) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result) { 0 1214 1214
2418 0 SvREFCNT_dec(doc_sv);
2419 0 croak("JSON decode error: maximum nesting depth exceeded");
2420 }
2421  
2422 /* attach doc_sv to keep yyjson_doc alive while zero-copy SVs exist.
2423 skip for null/bool roots -- they return immortal globals that must
2424 not accumulate magic. */
2425 1214 yyjson_type rtype = yyjson_get_type(root);
2426 1214 ▶ if (rtype != YYJSON_TYPE_NULL && rtype != YYJSON_TYPE_BOOL) {
Branch: 2 branches
BranchTrueFalseTotal
if (rtype != YYJSON_TYPE_NULL && rtype != YYJSON_TYPE_BOOL) { 1213 1 1214
if (rtype != YYJSON_TYPE_NULL && rtype != YYJSON_TYPE_BOOL) { 1211 2 1213
2427 1211 ▶ SV *anchor = SvROK(result) ? SvRV(result) : result;
Branch: 1 branch
BranchTrueFalseTotal
SV *anchor = SvROK(result) ? SvRV(result) : result; 1209 2 1211
2428 1211 sv_magicext(anchor, doc_sv, PERL_MAGIC_ext, &empty_vtbl, NULL, 0);
2429 }
2430 1214 SvREFCNT_dec(doc_sv);
2431  
2432 1214 ▶ XPUSHs(sv_2mortal(result));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(result)); 0 1214 1214
2433 1214 RETURN;
2434 }
2435  
2436 /* ---- XS::Parse::Keyword op builders ---- */
2437  
2438 static OP *
2439 387 make_custom_unop(pTHX_ Perl_ppaddr_t ppfunc, OP *arg) {
2440 387 OP *o = newUNOP(OP_NULL, 0, arg);
2441 387 o->op_type = OP_CUSTOM;
2442 387 o->op_ppaddr = ppfunc;
2443 387 return o;
2444 }
2445  
2446 static OP *
2447 252 make_custom_binop(pTHX_ Perl_ppaddr_t ppfunc, OP *a, OP *b) {
2448 252 OP *o = newBINOP(OP_NULL, 0, a, b);
2449 252 o->op_type = OP_CUSTOM;
2450 252 o->op_ppaddr = ppfunc;
2451 252 return o;
2452 }
2453  
2454 static OP *
2455 51 make_custom_3op(pTHX_ Perl_ppaddr_t ppfunc, OP *a, OP *b, OP *c) {
2456 51 OP *ab = newBINOP(OP_NULL, 0, a, b);
2457 51 OP *o = newBINOP(OP_NULL, 0, ab, c);
2458 51 o->op_type = OP_CUSTOM;
2459 51 o->op_ppaddr = ppfunc;
2460 51 return o;
2461 }
2462  
2463 static OP *
2464 26 make_custom_4op(pTHX_ Perl_ppaddr_t ppfunc, OP *a, OP *b, OP *c, OP *d) {
2465 26 OP *ab = newBINOP(OP_NULL, 0, a, b);
2466 26 OP *cd = newBINOP(OP_NULL, 0, c, d);
2467 26 OP *o = newBINOP(OP_NULL, 0, ab, cd);
2468 26 o->op_type = OP_CUSTOM;
2469 26 o->op_ppaddr = ppfunc;
2470 26 return o;
2471 }
2472  
2473 /* ---- XS::Parse::Keyword hooks ---- */
2474  
2475 /* macro to define build callback + hooks for 0-arg keyword */
2476 #define XPK_KW0(name, ppfunc) \
2477 static int build_kw_##name(pTHX_ OP **out, XSParseKeywordPiece *args[], \
2478 size_t nargs, void *hookdata) { \
2479 (void)args; PERL_UNUSED_ARG(nargs); PERL_UNUSED_ARG(hookdata); \
2480 OP *o = newOP(OP_NULL, 0); o->op_type = OP_CUSTOM; o->op_ppaddr = ppfunc; \
2481 *out = o; return KEYWORD_PLUGIN_EXPR; \
2482 } \
2483 static const struct XSParseKeywordHooks hooks_##name = { \
2484 .permit_hintkey = "JSON::YY/" #name, \
2485 .pieces = (const struct XSParseKeywordPieceType []){ {0} }, \
2486 .build = &build_kw_##name, \
2487 };
2488  
2489 /* macro for 1-arg keyword */
2490 #define XPK_KW1(name, ppfunc) \
2491 static int build_kw_##name(pTHX_ OP **out, XSParseKeywordPiece *args[], \
2492 size_t nargs, void *hookdata) { \
2493 PERL_UNUSED_ARG(nargs); PERL_UNUSED_ARG(hookdata); \
2494 *out = make_custom_unop(aTHX_ ppfunc, args[0]->op); \
2495 return KEYWORD_PLUGIN_EXPR; \
2496 } \
2497 static const struct XSParseKeywordHooks hooks_##name = { \
2498 .permit_hintkey = "JSON::YY/" #name, \
2499 .pieces = (const struct XSParseKeywordPieceType []){ XPK_TERMEXPR, {0} }, \
2500 .build = &build_kw_##name, \
2501 };
2502  
2503 /* macro for 2-arg keyword */
2504 #define XPK_KW2(name, ppfunc) \
2505 static int build_kw_##name(pTHX_ OP **out, XSParseKeywordPiece *args[], \
2506 size_t nargs, void *hookdata) { \
2507 PERL_UNUSED_ARG(nargs); PERL_UNUSED_ARG(hookdata); \
2508 *out = make_custom_binop(aTHX_ ppfunc, args[0]->op, args[1]->op); \
2509 return KEYWORD_PLUGIN_EXPR; \
2510 } \
2511 static const struct XSParseKeywordHooks hooks_##name = { \
2512 .permit_hintkey = "JSON::YY/" #name, \
2513 .pieces = (const struct XSParseKeywordPieceType []){ \
2514 XPK_TERMEXPR, XPK_COMMA, XPK_TERMEXPR, {0} }, \
2515 .build = &build_kw_##name, \
2516 };
2517  
2518 /* macro for 3-arg keyword */
2519 #define XPK_KW3(name, ppfunc) \
2520 static int build_kw_##name(pTHX_ OP **out, XSParseKeywordPiece *args[], \
2521 size_t nargs, void *hookdata) { \
2522 PERL_UNUSED_ARG(nargs); PERL_UNUSED_ARG(hookdata); \
2523 *out = make_custom_3op(aTHX_ ppfunc, args[0]->op, args[1]->op, args[2]->op); \
2524 return KEYWORD_PLUGIN_EXPR; \
2525 } \
2526 static const struct XSParseKeywordHooks hooks_##name = { \
2527 .permit_hintkey = "JSON::YY/" #name, \
2528 .pieces = (const struct XSParseKeywordPieceType []){ \
2529 XPK_TERMEXPR, XPK_COMMA, XPK_TERMEXPR, XPK_COMMA, XPK_TERMEXPR, {0} }, \
2530 .build = &build_kw_##name, \
2531 };
2532  
2533 /* macro for 4-arg keyword */
2534 #define XPK_KW4(name, ppfunc) \
2535 static int build_kw_##name(pTHX_ OP **out, XSParseKeywordPiece *args[], \
2536 size_t nargs, void *hookdata) { \
2537 PERL_UNUSED_ARG(nargs); PERL_UNUSED_ARG(hookdata); \
2538 *out = make_custom_4op(aTHX_ ppfunc, args[0]->op, args[1]->op, \
2539 args[2]->op, args[3]->op); \
2540 return KEYWORD_PLUGIN_EXPR; \
2541 } \
2542 static const struct XSParseKeywordHooks hooks_##name = { \
2543 .permit_hintkey = "JSON::YY/" #name, \
2544 .pieces = (const struct XSParseKeywordPieceType []){ \
2545 XPK_TERMEXPR, XPK_COMMA, XPK_TERMEXPR, XPK_COMMA, \
2546 XPK_TERMEXPR, XPK_COMMA, XPK_TERMEXPR, {0} }, \
2547 .build = &build_kw_##name, \
2548 };
2549  
2550 /* functional API */
2551 89 XPK_KW1(encode_json, pp_encode_json_impl)
2552 85 XPK_KW1(decode_json, pp_decode_json_impl)
2553 17 XPK_KW1(decode_json_ro, pp_decode_json_ro_impl)
2554  
2555 /* doc creation */
2556 116 XPK_KW1(jdoc, pp_jdoc_impl)
2557 35 XPK_KW1(jfrom, pp_jfrom_impl)
2558 5 XPK_KW1(jread, pp_jread_impl)
2559  
2560 /* value constructors */
2561 5 XPK_KW1(jstr, pp_jstr_impl)
2562 20 XPK_KW1(jnum, pp_jnum_impl)
2563 4 XPK_KW1(jbool, pp_jbool_impl)
2564 2 XPK_KW0(jnull, pp_jnull_impl)
2565 3 XPK_KW0(jarr, pp_jarr_impl)
2566 2 XPK_KW0(jobj, pp_jobj_impl)
2567  
2568 /* path ops */
2569 14 XPK_KW2(jget, pp_jget_impl)
2570 81 XPK_KW2(jgetp, pp_jgetp_impl)
2571 43 XPK_KW3(jset, pp_jset_impl)
2572 12 XPK_KW2(jdel, pp_jdel_impl)
2573 9 XPK_KW2(jhas, pp_jhas_impl)
2574 4 XPK_KW2(jclone, pp_jclone_impl)
2575 3 XPK_KW2(jwrite, pp_jwrite_impl)
2576 39 XPK_KW2(jencode, pp_jencode_impl)
2577 1 XPK_KW2(jpp, pp_jpp_impl)
2578 8 XPK_KW3(jraw, pp_jraw_impl)
2579  
2580 /* inspection */
2581 11 XPK_KW2(jtype, pp_jtype_impl)
2582 9 XPK_KW2(jlen, pp_jlen_impl)
2583 4 XPK_KW2(jkeys, pp_jkeys_impl)
2584 1 XPK_KW2(jvals, pp_jvals_impl)
2585 10 XPK_KW2(jpaths, pp_jpaths_impl)
2586 26 XPK_KW4(jfind, pp_jfind_impl)
2587  
2588 /* iteration */
2589 11 XPK_KW2(jiter, pp_jiter_impl)
2590 10 XPK_KW1(jnext, pp_jnext_impl)
2591 1 XPK_KW1(jkey, pp_jkey_impl)
2592  
2593 /* patching */
2594 9 XPK_KW2(jpatch, pp_jpatch_impl)
2595 2 XPK_KW2(jmerge, pp_jmerge_impl)
2596  
2597 /* comparison */
2598 3 XPK_KW2(jeq, pp_jeq_impl)
2599  
2600 /* type predicates */
2601 4 XPK_KW2(jis_obj, pp_jis_obj_impl)
2602 3 XPK_KW2(jis_arr, pp_jis_arr_impl)
2603 3 XPK_KW2(jis_str, pp_jis_str_impl)
2604 3 XPK_KW2(jis_num, pp_jis_num_impl)
2605 3 XPK_KW2(jis_int, pp_jis_int_impl)
2606 4 XPK_KW2(jis_real, pp_jis_real_impl)
2607 6 XPK_KW2(jis_bool, pp_jis_bool_impl)
2608 2 XPK_KW2(jis_null, pp_jis_null_impl)
2609  
2610 /* alias: jdecode = jgetp */
2611 1 XPK_KW2(jdecode, pp_jgetp_impl)
2612  
2613 MODULE = JSON::YY PACKAGE = JSON::YY
2614  
2615 BOOT:
2616 {
2617 #ifdef USE_ITHREADS
2618 /* guards the decode_json_ro buffer refcount; the flag keeps a second
2619 interpreter loading the module from re-initialising a live mutex */
2620 if (!json_yy_ro_mutex_ready) {
2621 MUTEX_INIT(&json_yy_ro_mutex);
2622 json_yy_ro_mutex_ready = 1;
2623 }
2624 #endif
2625  
2626 29 boot_xs_parse_keyword(0.40);
2627  
2628 /* functional API keywords */
2629 29 register_xs_parse_keyword("encode_json", &hooks_encode_json, NULL);
2630 29 register_xs_parse_keyword("decode_json", &hooks_decode_json, NULL);
2631 29 register_xs_parse_keyword("decode_json_ro", &hooks_decode_json_ro, NULL);
2632  
2633 /* doc creation */
2634 29 register_xs_parse_keyword("jdoc", &hooks_jdoc, NULL);
2635 29 register_xs_parse_keyword("jfrom", &hooks_jfrom, NULL);
2636 29 register_xs_parse_keyword("jread", &hooks_jread, NULL);
2637  
2638 /* value constructors */
2639 29 register_xs_parse_keyword("jstr", &hooks_jstr, NULL);
2640 29 register_xs_parse_keyword("jnum", &hooks_jnum, NULL);
2641 29 register_xs_parse_keyword("jbool", &hooks_jbool, NULL);
2642 29 register_xs_parse_keyword("jnull", &hooks_jnull, NULL);
2643 29 register_xs_parse_keyword("jarr", &hooks_jarr, NULL);
2644 29 register_xs_parse_keyword("jobj", &hooks_jobj, NULL);
2645  
2646 /* path operations */
2647 29 register_xs_parse_keyword("jget", &hooks_jget, NULL);
2648 29 register_xs_parse_keyword("jgetp", &hooks_jgetp, NULL);
2649 29 register_xs_parse_keyword("jset", &hooks_jset, NULL);
2650 29 register_xs_parse_keyword("jdel", &hooks_jdel, NULL);
2651 29 register_xs_parse_keyword("jhas", &hooks_jhas, NULL);
2652 29 register_xs_parse_keyword("jclone", &hooks_jclone, NULL);
2653 29 register_xs_parse_keyword("jwrite", &hooks_jwrite, NULL);
2654 29 register_xs_parse_keyword("jencode", &hooks_jencode, NULL);
2655 29 register_xs_parse_keyword("jpp", &hooks_jpp, NULL);
2656 29 register_xs_parse_keyword("jraw", &hooks_jraw, NULL);
2657  
2658 /* inspection */
2659 29 register_xs_parse_keyword("jtype", &hooks_jtype, NULL);
2660 29 register_xs_parse_keyword("jlen", &hooks_jlen, NULL);
2661 29 register_xs_parse_keyword("jkeys", &hooks_jkeys, NULL);
2662 29 register_xs_parse_keyword("jvals", &hooks_jvals, NULL);
2663 29 register_xs_parse_keyword("jpaths", &hooks_jpaths, NULL);
2664 29 register_xs_parse_keyword("jfind", &hooks_jfind, NULL);
2665  
2666 /* iteration */
2667 29 register_xs_parse_keyword("jiter", &hooks_jiter, NULL);
2668 29 register_xs_parse_keyword("jnext", &hooks_jnext, NULL);
2669 29 register_xs_parse_keyword("jkey", &hooks_jkey, NULL);
2670  
2671 /* patching */
2672 29 register_xs_parse_keyword("jpatch", &hooks_jpatch, NULL);
2673 29 register_xs_parse_keyword("jmerge", &hooks_jmerge, NULL);
2674  
2675 /* comparison */
2676 29 register_xs_parse_keyword("jeq", &hooks_jeq, NULL);
2677  
2678 /* type predicates */
2679 29 register_xs_parse_keyword("jis_obj", &hooks_jis_obj, NULL);
2680 29 register_xs_parse_keyword("jis_arr", &hooks_jis_arr, NULL);
2681 29 register_xs_parse_keyword("jis_str", &hooks_jis_str, NULL);
2682 29 register_xs_parse_keyword("jis_num", &hooks_jis_num, NULL);
2683 29 register_xs_parse_keyword("jis_int", &hooks_jis_int, NULL);
2684 29 register_xs_parse_keyword("jis_real", &hooks_jis_real, NULL);
2685 29 register_xs_parse_keyword("jis_bool", &hooks_jis_bool, NULL);
2686 29 register_xs_parse_keyword("jis_null", &hooks_jis_null, NULL);
2687  
2688 /* alias */
2689 29 register_xs_parse_keyword("jdecode", &hooks_jdecode, NULL);
2690 }
2691  
2692 SV *
2693 new(const char *klass)
2694 CODE:
2695 {
2696 json_yy_t *self;
2697 82 HV *hv = newHV();
2698 82 Newxz(self, 1, json_yy_t);
2699 82 self->flags = F_ALLOW_NONREF;
2700 82 self->max_depth = MAX_DEPTH_DEFAULT;
2701 82 attach_ext_magic(aTHX_ (SV *)hv, &json_yy_vtbl, self);
2702 82 RETVAL = sv_bless(newRV_noinc((SV *)hv), gv_stashpv(klass, GV_ADD));
2703 }
2704 OUTPUT:
2705 RETVAL
2706  
2707 void
2708 _set_utf8(SV *self_sv, int val)
2709 CODE:
2710 42 ▶ if (val) get_self(aTHX_ self_sv)->flags |= F_UTF8;
Branch: 1 branch
BranchTrueFalseTotal
if (val) get_self(aTHX_ self_sv)->flags |= F_UTF8; 42 0 42
2711 0 else get_self(aTHX_ self_sv)->flags &= ~F_UTF8;
2712  
2713 void
2714 _set_pretty(SV *self_sv, int val)
2715 CODE:
2716 22 ▶ if (val) get_self(aTHX_ self_sv)->flags |= F_PRETTY;
Branch: 1 branch
BranchTrueFalseTotal
if (val) get_self(aTHX_ self_sv)->flags |= F_PRETTY; 22 0 22
2717 0 else get_self(aTHX_ self_sv)->flags &= ~F_PRETTY;
2718  
2719 void
2720 _set_canonical(SV *self_sv, int val)
2721 CODE:
2722 0 ▶ if (val) get_self(aTHX_ self_sv)->flags |= F_CANONICAL;
Branch: 1 branch
BranchTrueFalseTotal
if (val) get_self(aTHX_ self_sv)->flags |= F_CANONICAL; 0 0 0
2723 0 else get_self(aTHX_ self_sv)->flags &= ~F_CANONICAL;
2724  
2725 void
2726 _set_allow_nonref(SV *self_sv, int val)
2727 CODE:
2728 8 ▶ if (val) get_self(aTHX_ self_sv)->flags |= F_ALLOW_NONREF;
Branch: 1 branch
BranchTrueFalseTotal
if (val) get_self(aTHX_ self_sv)->flags |= F_ALLOW_NONREF; 6 2 8
2729 2 else get_self(aTHX_ self_sv)->flags &= ~F_ALLOW_NONREF;
2730  
2731 void
2732 _set_allow_unknown(SV *self_sv, int val)
2733 CODE:
2734 0 ▶ if (val) get_self(aTHX_ self_sv)->flags |= F_ALLOW_UNKNOWN;
Branch: 1 branch
BranchTrueFalseTotal
if (val) get_self(aTHX_ self_sv)->flags |= F_ALLOW_UNKNOWN; 0 0 0
2735 0 else get_self(aTHX_ self_sv)->flags &= ~F_ALLOW_UNKNOWN;
2736  
2737 void
2738 _set_allow_blessed(SV *self_sv, int val)
2739 CODE:
2740 5 ▶ if (val) get_self(aTHX_ self_sv)->flags |= F_ALLOW_BLESSED;
Branch: 1 branch
BranchTrueFalseTotal
if (val) get_self(aTHX_ self_sv)->flags |= F_ALLOW_BLESSED; 5 0 5
2741 0 else get_self(aTHX_ self_sv)->flags &= ~F_ALLOW_BLESSED;
2742  
2743 void
2744 _set_convert_blessed(SV *self_sv, int val)
2745 CODE:
2746 12 ▶ if (val) get_self(aTHX_ self_sv)->flags |= F_CONVERT_BLESSED;
Branch: 1 branch
BranchTrueFalseTotal
if (val) get_self(aTHX_ self_sv)->flags |= F_CONVERT_BLESSED; 12 0 12
2747 0 else get_self(aTHX_ self_sv)->flags &= ~F_CONVERT_BLESSED;
2748  
2749 void
2750 _set_max_depth(SV *self_sv, U32 val)
2751 CODE:
2752 {
2753 11 json_yy_t *self = get_self(aTHX_ self_sv);
2754 11 self->max_depth = val;
2755 }
2756  
2757 void
2758 _set_bool(SV *self_sv, int val)
2759 CODE:
2760 13 ▶ if (val) get_self(aTHX_ self_sv)->flags |= F_BOOL;
Branch: 1 branch
BranchTrueFalseTotal
if (val) get_self(aTHX_ self_sv)->flags |= F_BOOL; 12 1 13
2761 1 else get_self(aTHX_ self_sv)->flags &= ~F_BOOL;
2762  
2763 SV *
2764 decode(SV *self_sv, SV *json_sv)
2765 CODE:
2766 {
2767 28 json_yy_t *self = get_self(aTHX_ self_sv);
2768 STRLEN len;
2769 const char *json;
2770  
2771 28 ▶ if (self->flags & F_UTF8) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->flags & F_UTF8) { 8 20 28
2772 8 json = SvPV(json_sv, len); /* utf8 mode: input is raw bytes */
2773 } else {
2774 20 json = sv_pv_utf8_nomod(aTHX_ json_sv, &len); /* character mode: encode to UTF-8 */
2775 }
2776  
2777 yyjson_read_err err;
2778 28 yyjson_doc *doc = yyjson_read_opts((char *)json, len, YYJSON_READ_NOFLAG, NULL, &err);
2779 28 ▶ if (!doc)
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) 0 28 28
2780 0 croak("JSON decode error: %s at byte offset %zu", err.msg, err.pos);
2781 31 ▶ CHECK_DOC_DEPTH(doc, self->max_depth, "JSON decode error");
Branch: 1 branch
BranchTrueFalseTotal
CHECK_DOC_DEPTH(doc, self->max_depth, "JSON decode error"); 3 25 28
2782  
2783 25 yyjson_val *root = yyjson_doc_get_root(doc);
2784 25 ▶ if (!root) {
Branch: 1 branch
BranchTrueFalseTotal
if (!root) { 0 25 25
2785 yyjson_doc_free(doc);
2786 0 croak("JSON decode error: empty document");
2787 }
2788  
2789 /* check nonref */
2790 25 ▶ if (!(self->flags & F_ALLOW_NONREF)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(self->flags & F_ALLOW_NONREF)) { 1 24 25
2791 1 yyjson_type t = yyjson_get_type(root);
2792 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
2793 yyjson_doc_free(doc);
2794 1 croak("JSON text must be an object or array (but found number, string, true, false or null)");
2795 }
2796 }
2797  
2798 json_yy_decode_ctx_t ctx;
2799 24 ctx.flags = self->flags;
2800 24 ▶ if (self->flags & F_BOOL) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->flags & F_BOOL) { 15 9 24
2801 15 ctx.bool_true = get_bool_sv(aTHX_ 1);
2802 15 ctx.bool_false = get_bool_sv(aTHX_ 0);
2803 } else {
2804 9 ctx.bool_true = NULL;
2805 9 ctx.bool_false = NULL;
2806 }
2807  
2808 24 RETVAL = yyjson_val_to_sv(aTHX_ root, self->max_depth, &ctx);
2809 yyjson_doc_free(doc);
2810 24 ▶ if (!RETVAL)
Branch: 1 branch
BranchTrueFalseTotal
if (!RETVAL) 0 24 24
2811 0 croak("JSON decode error: maximum nesting depth exceeded");
2812 }
2813 OUTPUT:
2814 RETVAL
2815  
2816 SV *
2817 decode_doc(SV *self_sv, SV *json_sv)
2818 CODE:
2819 {
2820 7 json_yy_t *self = get_self(aTHX_ self_sv);
2821 STRLEN len;
2822 const char *json;
2823  
2824 7 ▶ if (self->flags & F_UTF8) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->flags & F_UTF8) { 1 6 7
2825 1 json = SvPV(json_sv, len);
2826 } else {
2827 6 json = sv_pv_utf8_nomod(aTHX_ json_sv, &len);
2828 }
2829  
2830 yyjson_read_err err;
2831 7 yyjson_doc *idoc = yyjson_read_opts((char *)json, len, YYJSON_READ_NOFLAG, NULL, &err);
2832 7 ▶ if (!idoc)
Branch: 1 branch
BranchTrueFalseTotal
if (!idoc) 0 7 7
2833 0 croak("JSON decode error: %s at byte offset %zu", err.msg, err.pos);
2834 9 ▶ CHECK_DOC_DEPTH(idoc, self->max_depth, "decode_doc");
Branch: 1 branch
BranchTrueFalseTotal
CHECK_DOC_DEPTH(idoc, self->max_depth, "decode_doc"); 2 5 7
2835  
2836 5 yyjson_mut_doc *mdoc = yyjson_doc_mut_copy(idoc, NULL);
2837 yyjson_doc_free(idoc);
2838 5 ▶ if (!mdoc)
Branch: 1 branch
BranchTrueFalseTotal
if (!mdoc) 0 5 5
2839 0 croak("decode_doc: failed to create mutable document");
2840  
2841 5 yyjson_mut_val *root = yyjson_mut_doc_get_root(mdoc);
2842 5 ▶ if (!(self->flags & F_ALLOW_NONREF)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(self->flags & F_ALLOW_NONREF)) { 3 2 5
2843 3 yyjson_type t = yyjson_mut_get_type(root);
2844 3 ▶ if (t != YYJSON_TYPE_ARR && t != YYJSON_TYPE_OBJ) {
Branch: 2 branches
BranchTrueFalseTotal
if (t != YYJSON_TYPE_ARR && t != YYJSON_TYPE_OBJ) { 2 1 3
if (t != YYJSON_TYPE_ARR && t != YYJSON_TYPE_OBJ) { 1 1 2
2845 1 yyjson_mut_doc_free(mdoc);
2846 1 croak("JSON text must be an object or array (but found number, string, true, false or null)");
2847 }
2848 }
2849 4 RETVAL = new_doc_sv(aTHX_ mdoc, root, NULL);
2850 }
2851 OUTPUT:
2852 RETVAL
2853  
2854 SV *
2855 encode(SV *self_sv, SV *data)
2856 CODE:
2857 {
2858 1509 json_yy_t *self = get_self(aTHX_ self_sv);
2859  
2860 /* check nonref */
2861 1509 ▶ if (!(self->flags & F_ALLOW_NONREF)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(self->flags & F_ALLOW_NONREF)) { 1 1508 1509
2862 1 ▶ if (!SvROK(data) || (SvTYPE(SvRV(data)) != SVt_PVAV && SvTYPE(SvRV(data)) != SVt_PVHV))
Branch: 3 branches
BranchTrueFalseTotal
if (!SvROK(data) || (SvTYPE(SvRV(data)) != SVt_PVAV && SvTYPE(SvRV(data)) != SVt_PVHV)) 0 1 1
if (!SvROK(data) || (SvTYPE(SvRV(data)) != SVt_PVAV && SvTYPE(SvRV(data)) != SVt_PVHV)) 0 0 0
if (!SvROK(data) || (SvTYPE(SvRV(data)) != SVt_PVAV && SvTYPE(SvRV(data)) != SVt_PVHV)) 0 0 0
2863 1 croak("hash- or arrayref expected (not a simple scalar)");
2864 }
2865  
2866 /* hybrid: use the direct encoder unless a yyjson-only feature (pretty) is
2867 requested. F_CANONICAL is accepted for JSON::XS compatibility but ignored
2868 by both paths -- yyjson has no sorted-key writer. */
2869 1508 ▶ if (!(self->flags & F_PRETTY)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(self->flags & F_PRETTY)) { 1485 23 1508
2870 1485 RETVAL = newSV(64);
2871 1485 SvPOK_on(RETVAL);
2872 1485 SvCUR_set(RETVAL, 0);
2873 1485 SAVEFREESV(RETVAL);
2874 1485 direct_encode_sv(aTHX_ RETVAL, data, 0, self);
2875 1474 SvREFCNT_inc_simple_void_NN(RETVAL);
2876 1474 *(SvPVX(RETVAL) + SvCUR(RETVAL)) = '\0';
2877 1474 ▶ if (!(self->flags & F_UTF8))
Branch: 1 branch
BranchTrueFalseTotal
if (!(self->flags & F_UTF8)) 4 1470 1474
2878 4 SvUTF8_on(RETVAL);
2879 } else {
2880 /* yyjson path for pretty */
2881 23 yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
2882 23 SV *doc_guard = sv_2mortal(newSV(0));
2883 23 attach_ext_magic(aTHX_ doc_guard, &mut_docholder_vtbl, doc);
2884 23 yyjson_mut_val *root = sv_to_yyjson_val(aTHX_ doc, data, self, 0);
2885 yyjson_mut_doc_set_root(doc, root);
2886  
2887 size_t json_len;
2888 yyjson_write_err werr;
2889 19 char *json = yyjson_mut_write_opts(doc, YYJSON_WRITE_PRETTY, NULL, &json_len, &werr);
2890 /* disarm guard before explicit free */
2891 19 mg_findext(doc_guard, PERL_MAGIC_ext, &mut_docholder_vtbl)->mg_ptr = NULL;
2892 19 yyjson_mut_doc_free(doc);
2893  
2894 19 ▶ if (!json)
Branch: 1 branch
BranchTrueFalseTotal
if (!json) 0 19 19
2895 0 croak("JSON encode error: %s", werr.msg);
2896  
2897 19 ENTER;
2898 19 SAVEDESTRUCTOR_X(yyjson_free_buf, json);
2899 19 ▶ if (self->flags & F_UTF8) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->flags & F_UTF8) { 14 5 19
2900 14 RETVAL = newSVpvn(json, json_len);
2901 } else {
2902 5 RETVAL = newSVpvn_utf8(json, json_len, 1);
2903 }
2904 19 LEAVE;
2905 }
2906 }
2907 OUTPUT:
2908 RETVAL
2909  
2910 SV *
2911 _xs_encode_json(SV *data)
2912 CODE:
2913 {
2914 1 RETVAL = newSV(64);
2915 1 SvPOK_on(RETVAL);
2916 1 SvCUR_set(RETVAL, 0);
2917 1 SAVEFREESV(RETVAL);
2918 1 direct_encode_sv(aTHX_ RETVAL, data, 0, &default_self);
2919 1 SvREFCNT_inc_simple_void_NN(RETVAL);
2920 1 *(SvPVX(RETVAL) + SvCUR(RETVAL)) = '\0';
2921 }
2922 OUTPUT:
2923 RETVAL
2924  
2925 SV *
2926 _xs_decode_json(SV *json_sv)
2927 CODE:
2928 {
2929 STRLEN len;
2930 0 const char *json = SvPV(json_sv, len);
2931  
2932 yyjson_read_err err;
2933 0 yyjson_doc *doc = yyjson_read_opts((char *)json, len, YYJSON_READ_NOFLAG, NULL, &err);
2934 0 ▶ if (!doc)
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) 0 0 0
2935 0 croak("JSON decode error: %s at byte offset %zu", err.msg, err.pos);
2936 0 ▶ CHECK_DOC_DEPTH(doc, MAX_DEPTH_DEFAULT, "JSON decode error");
Branch: 1 branch
BranchTrueFalseTotal
CHECK_DOC_DEPTH(doc, MAX_DEPTH_DEFAULT, "JSON decode error"); 0 0 0
2937  
2938 0 yyjson_val *root = yyjson_doc_get_root(doc);
2939 0 ▶ if (!root) {
Branch: 1 branch
BranchTrueFalseTotal
if (!root) { 0 0 0
2940 yyjson_doc_free(doc);
2941 0 croak("JSON decode error: empty document");
2942 }
2943  
2944 0 RETVAL = yyjson_val_to_sv(aTHX_ root, MAX_DEPTH_DEFAULT, NULL);
2945 yyjson_doc_free(doc);
2946 0 ▶ if (!RETVAL)
Branch: 1 branch
BranchTrueFalseTotal
if (!RETVAL) 0 0 0
2947 0 croak("JSON decode error: maximum nesting depth exceeded");
2948 }
2949 OUTPUT:
2950 RETVAL
2951  
2952 SV *
2953 _xs_decode_json_ro(SV *json_sv)
2954 CODE:
2955 {
2956 STRLEN len;
2957 0 const char *json = SvPV(json_sv, len);
2958  
2959 yyjson_read_err err;
2960 0 yyjson_doc *doc = yyjson_read_opts((char *)json, len, YYJSON_READ_NOFLAG, NULL, &err);
2961 0 ▶ if (!doc)
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) 0 0 0
2962 0 croak("JSON decode error: %s at byte offset %zu", err.msg, err.pos);
2963 0 ▶ CHECK_DOC_DEPTH(doc, MAX_DEPTH_DEFAULT, "JSON decode error");
Branch: 1 branch
BranchTrueFalseTotal
CHECK_DOC_DEPTH(doc, MAX_DEPTH_DEFAULT, "JSON decode error"); 0 0 0
2964  
2965 0 yyjson_val *root = yyjson_doc_get_root(doc);
2966 0 ▶ if (!root) {
Branch: 1 branch
BranchTrueFalseTotal
if (!root) { 0 0 0
2967 yyjson_doc_free(doc);
2968 0 croak("JSON decode error: empty document");
2969 }
2970  
2971 /* doc ownership transfers to the holder SV */
2972 0 SV *doc_sv = new_doc_holder(aTHX_ doc);
2973  
2974 0 RETVAL = yyjson_val_to_sv_ro(aTHX_ root, doc_sv, MAX_DEPTH_DEFAULT);
2975 0 ▶ if (!RETVAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (!RETVAL) { 0 0 0
2976 0 SvREFCNT_dec(doc_sv);
2977 0 croak("JSON decode error: maximum nesting depth exceeded");
2978 }
2979  
2980 /* attach doc_sv to keep yyjson_doc alive while zero-copy SVs exist.
2981 skip for null/bool -- they return immortal globals. */
2982 {
2983 0 yyjson_type rtype = yyjson_get_type(root);
2984 0 ▶ if (rtype != YYJSON_TYPE_NULL && rtype != YYJSON_TYPE_BOOL) {
Branch: 2 branches
BranchTrueFalseTotal
if (rtype != YYJSON_TYPE_NULL && rtype != YYJSON_TYPE_BOOL) { 0 0 0
if (rtype != YYJSON_TYPE_NULL && rtype != YYJSON_TYPE_BOOL) { 0 0 0
2985 0 ▶ SV *anchor = SvROK(RETVAL) ? SvRV(RETVAL) : RETVAL;
Branch: 1 branch
BranchTrueFalseTotal
SV *anchor = SvROK(RETVAL) ? SvRV(RETVAL) : RETVAL; 0 0 0
2986 0 sv_magicext(anchor, doc_sv, PERL_MAGIC_ext, &empty_vtbl, NULL, 0);
2987 }
2988 }
2989 0 SvREFCNT_dec(doc_sv);
2990 }
2991 OUTPUT:
2992 RETVAL
2993  
2994  
2995 # XS helpers for Doc overloading
2996  
2997 SV *
2998 _doc_stringify(SV *self_sv)
2999 CODE:
3000 {
3001 53 json_yy_doc_t *d = get_doc(aTHX_ self_sv);
3002 size_t json_len;
3003 yyjson_write_err werr;
3004 53 char *json = yyjson_mut_val_write_opts(d->root, YYJSON_WRITE_NOFLAG, NULL, &json_len, &werr);
3005 53 ▶ if (!json)
Branch: 1 branch
BranchTrueFalseTotal
if (!json) 0 53 53
3006 0 croak("JSON::YY::Doc: stringify error: %s", werr.msg);
3007 53 ENTER;
3008 53 SAVEDESTRUCTOR_X(yyjson_free_buf, json);
3009 53 RETVAL = newSVpvn(json, json_len);
3010 53 LEAVE;
3011 }
3012 OUTPUT:
3013 RETVAL
3014  
3015 SV *
3016 _doc_eq(SV *a_sv, SV *b_sv)
3017 CODE:
3018 {
3019 5 ▶ if (!SvROK(b_sv) || !sv_derived_from(b_sv, "JSON::YY::Doc"))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(b_sv) || !sv_derived_from(b_sv, "JSON::YY::Doc")) 5 0 5
if (!SvROK(b_sv) || !sv_derived_from(b_sv, "JSON::YY::Doc")) 0 5 5
3020 0 XSRETURN_NO;
3021 5 json_yy_doc_t *a = get_doc(aTHX_ a_sv);
3022 5 json_yy_doc_t *b = get_doc(aTHX_ b_sv);
3023 5 ▶ RETVAL = yyjson_mut_equals(a->root, b->root)
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = yyjson_mut_equals(a->root, b->root) 5 0 5
3024 5 ▶ ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
? &PL_sv_yes : &PL_sv_no; 2 3 5
3025 5 SvREFCNT_inc_simple_void_NN(RETVAL);
3026 }
3027 OUTPUT:
3028 RETVAL
« yyjson.h ↑ Summary