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