YY.xs

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