SL.xs

filter: Statementstmt 88.9% 570 / 641 Branchbran 64.2% 329 / 512 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 77.9% 899 / 1153 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« jsonxs_inline.h ↑ Summary
1 #include "perl-jsonsl.h"
2 #include "jsonxs_inline.h"
3  
4 /*
5 * JSON::SL, JSON::SL::Boolean
6 */
7 #define MY_CXT_KEY "JSON::SL::_guts" XS_VERSION
8  
9 typedef struct {
10 PLJSONSL* quick;
11 HV *stash_obj;
12 HV *stash_boolean;
13 HV *stash_tuba;
14 } my_cxt_t;
15 START_MY_CXT
16  
17 static int PLJSONSL_Escape_Table_dfl[0x80];
18 #define ESCTBL PLJSONSL_Escape_Table_dfl
19  
20 #define PLJSONSL_CROAK_USAGE(m) \
21 die("JSON::SL: %s %s", GvNAME(CvGV(cv)), m)
22  
23  
24 #ifdef PLJSONSL_HAVE_HV_COMMON
25 #define storeget_he(pjsn, hv, buf, len, value, is_utf8) \
26 hv_common((HV*)(hv), NULL, /*KEYSV*/ \
27 buf, len, \
28 ((is_utf8)?HVhek_UTF8:0), /*flags*/\
29 HV_FETCH_ISSTORE, /*action*/ \
30 value, \
31 0 /* HASH (tell perl to compute) */)
32  
33 #define delete_he(pjsn,hv,he) delete_he_THX(aTHX_ pjsn, hv, he)
34 static void
35 6 delete_he_THX(pTHX_ const PLJSONSL *dummy, HV* hv, HE* he)
36 {
37 char *kbuf;
38 STRLEN klen;
39 int is_utf8;
40  
41 6 ▶ kbuf = HePV(he, klen);
Branch: 1 branch
BranchTrueFalseTotal
kbuf = HePV(he, klen); 0 6 6
42 6 ▶ is_utf8 = HeUTF8(he);
Branch: 1 branch
BranchTrueFalseTotal
is_utf8 = HeUTF8(he); 0 6 6
43 6 hv_common(hv, NULL, /* KEYSV */
44 kbuf, klen,
45 (is_utf8 ? HVhek_UTF8 : 0), /*flags*/
46 HV_DELETE|G_DISCARD, /*action*/
47 NULL, /* value */
48 HeHASH(he) /*hash value - already computed */);
49 6 }
50  
51 #define PLJSONSL_INIT_KSV(blah)
52 #define PLJSONSL_DESTROY_KSV(blah)
53  
54  
55 #else
56 /* probably very dangerous, but the beginning of hv_store_common
57 * looks quite simple...
58 */
59  
60 #define CLOBBER_PV(sv,buf,len) \
61 SvCUR_set(sv, len); \
62 SvPVX(sv) = buf; \
63 SvUTF8_off(sv);
64  
65 #define UNCLOBBER_PV(sv) \
66 SvCUR_set(sv, 0); \
67 SvPVX(sv) = NULL; \
68  
69 #define PLJSONSL_INIT_KSV(pjsn) \
70 pjsn->ksv = newSV(16); \
71 pjsn->ksv_origpv = SvPVX(pjsn->ksv); \
72 SvLEN_set(pjsn->ksv, 0); \
73 SvPOK_on(pjsn->ksv);
74  
75 #define PLJSONSL_DESTROY_KSV(pjsn) \
76 CLOBBER_PV(pjsn->ksv, pjsn->ksv_origpv, 0); \
77 SvREFCNT_dec(pjsn->ksv);
78  
79 #define storeget_he(...) storeget_he_THX(aTHX_ __VA_ARGS__)
80 static HE*
81 storeget_he_THX(pTHX_ PLJSONSL *pjsn, HV *hv, const char *buf, size_t len,
82 SV *value, int is_utf8)
83 {
84 HE *ret;
85  
86 CLOBBER_PV(pjsn->ksv, buf, len);
87 if (is_utf8) {
88 SvUTF8_on(pjsn->ksv);
89 }
90 ret = hv_store_ent(hv, pjsn->ksv, value, 0);
91 UNCLOBBER_PV(pjsn->ksv);
92 return ret;
93 }
94  
95 #define delete_he(...) delete_he_THX(aTHX_ __VA_ARGS__)
96 static void
97 delete_he_THX(pTHX_ PLJSONSL *pjsn, HV *hv, HE* he)
98 {
99 char *kbuf;
100 STRLEN klen;
101 kbuf = HePV(he, klen);
102  
103 CLOBBER_PV(pjsn->ksv, buf, len);
104 if (HeUTF8(he)) {
105 SvUTF8_on(pjsn->ksv);
106 }
107 (void)hv_delete_ent(hv, pjsn->ksv, G_DISCARD, HeHASH(he));
108 UNCLOBBER_PV(pjsn->ksv);
109 }
110  
111 #endif /* HAVE_HV_COMMON */
112  
113  
114 #define REFDEC_FIELD(pjsn, fld) \
115 if (pjsn->fld != NULL) \
116 { \
117 SvREFCNT_dec(pjsn->fld); \
118 pjsn->fld = NULL; \
119 } \
120  
121  
122  
123 #define GET_STATE_BUFFER(pjsn, pos) \
124 (char*)(SvPVX(pjsn->buf) + (pos - pjsn->pos_min_valid))
125  
126 #define PLJSONSL_NEWSVUV_fast(sv, val) \
127 sv = newSV(0); \
128 sv_upgrade(sv, SVt_IV); \
129 SvIOK_only(sv); \
130 SvUVX(sv) = val;
131  
132 /**
133 * These 'common' functions are generic enough to work
134 * on all objects wiht a common pjsn head.
135 */
136  
137 #define pljsonsl_common_mknumeric(s,b,n) \
138 pljsonsl_common_mknumeric_THX(aTHX_ s,b,n)
139 static SV *
140 33 pljsonsl_common_mknumeric_THX(pTHX_
141 struct jsonsl_state_st *state,
142 const char *buf,
143 size_t nbuf)
144 {
145 #define die_numeric(err) \
146 die("JSON::SL - Malformed number (%s)", err);
147  
148 SV *newsv;
149 33 switch (state->special_flags) {
150 /* Simple signed/unsigned numbers, no exponents or fractions to worry about */
151 17 case JSONSL_SPECIALf_UNSIGNED:
152 17 ▶ if (nbuf == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (nbuf == 1) { 12 5 17
153 12 ▶ PLJSONSL_NEWSVUV_fast(newsv, state->nelem);
Branch: 4 branches
BranchTrueFalseTotal
PLJSONSL_NEWSVUV_fast(newsv, state->nelem); 0 12 12
PLJSONSL_NEWSVUV_fast(newsv, state->nelem); 0 0 0
PLJSONSL_NEWSVUV_fast(newsv, state->nelem); 0 0 0
PLJSONSL_NEWSVUV_fast(newsv, state->nelem); 0 12 12
154 12 break;
155 } /* else, ndigits > 1 */
156 5 ▶ if (*buf == '0') { die_numeric("leading zero for non-fraction"); }
Branch: 1 branch
BranchTrueFalseTotal
if (*buf == '0') { die_numeric("leading zero for non-fraction"); } 2 3 5
157 3 ▶ if (nbuf < (UV_DIG-1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (nbuf < (UV_DIG-1)) { 3 0 3
158 3 ▶ PLJSONSL_NEWSVUV_fast(newsv, state->nelem);
Branch: 4 branches
BranchTrueFalseTotal
PLJSONSL_NEWSVUV_fast(newsv, state->nelem); 0 3 3
PLJSONSL_NEWSVUV_fast(newsv, state->nelem); 0 0 0
PLJSONSL_NEWSVUV_fast(newsv, state->nelem); 0 0 0
PLJSONSL_NEWSVUV_fast(newsv, state->nelem); 0 3 3
159 3 break;
160 } /* else, potential overflow */
161 0 newsv = jsonxs_inline_process_number(buf);
162 0 break;
163  
164 1 case JSONSL_SPECIALf_SIGNED:
165 1 nbuf--;
166 1 ▶ if (nbuf == 0) { die_numeric("found lone '-'"); }
Branch: 1 branch
BranchTrueFalseTotal
if (nbuf == 0) { die_numeric("found lone '-'"); } 0 1 1
167 1 ▶ if (nbuf > 1 && buf[1] == '0') {
Branch: 2 branches
BranchTrueFalseTotal
if (nbuf > 1 && buf[1] == '0') { 1 0 1
if (nbuf > 1 && buf[1] == '0') { 0 1 1
168 0 die_numeric("Leading 0 after '-'");
169 }
170 1 ▶ if (nbuf < (IV_DIG-1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (nbuf < (IV_DIG-1)) { 1 0 1
171 1 newsv = newSViv(-((IV)state->nelem));
172 1 break;
173 } /*else */
174 0 newsv = jsonxs_inline_process_number(buf);
175 0 break;
176  
177 15 default:
178 15 ▶ if (state->special_flags & JSONSL_SPECIALf_NUMNOINT) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->special_flags & JSONSL_SPECIALf_NUMNOINT) { 15 0 15
179 15 newsv = jsonxs_inline_process_number(buf);
180 }
181 12 break;
182 }
183 28 return newsv;
184 #undef die_numeric
185 }
186  
187 #define pljsonsl_common_mkboolean(pjsn_head, value) \
188 pljsonsl_common_mkboolean_THX(aTHX_ pjsn_head, value)
189  
190 static SV *
191 4 pljsonsl_common_mkboolean_THX(pTHX_
192 PLJSONSL *pjsn_head,
193 jsonsl_special_t specialf)
194 {
195 SV *retsv, *ivsv;
196 4 ivsv = newSViv(specialf == JSONSL_SPECIALf_TRUE);
197 4 retsv = newRV_noinc(ivsv);
198 4 sv_bless(retsv, pjsn_head->stash_boolean);
199 4 return retsv;
200 }
201  
202 #define pljsonsl_common_initialize(mycxt, pjsn_head, max_levels) \
203 pljsonsl_common_initialize_THX(aTHX_ mycxt, pjsn_head, max_levels)
204  
205 static void
206 33 pljsonsl_common_initialize_THX(pTHX_
207 my_cxt_t *mycxt,
208 PLJSONSL *pjsn_head,
209 size_t max_levels)
210 {
211 33 pjsn_head->jsn = jsonsl_new(max_levels+2);
212 33 pjsn_head->jsn->data = pjsn_head;
213 33 pjsn_head->stash_boolean = mycxt->stash_boolean;
214 PLJSONSL_mkTHX(pjsn_head);
215 33 memcpy(pjsn_head->escape_table, ESCTBL, sizeof(ESCTBL));
216 33 }
217  
218  
219 #define process_special(pjsn,st) process_special_THX(aTHX_ pjsn,st)
220 static inline void
221 35 process_special_THX(pTHX_
222 PLJSONSL *pjsn,
223 struct jsonsl_state_st *state)
224 {
225 SV *newsv;
226 35 char *buf = GET_STATE_BUFFER(pjsn, state->pos_begin);
227  
228 35 switch (state->special_flags) {
229 /* might look redundant, but is most common, so it's first */
230 18 case JSONSL_SPECIALf_UNSIGNED:
231 case JSONSL_SPECIALf_SIGNED:
232 18 newsv = pljsonsl_common_mknumeric(state,
233 buf,
234 state->pos_cur - state->pos_begin);
235 16 break;
236  
237 2 case JSONSL_SPECIALf_TRUE:
238 case JSONSL_SPECIALf_FALSE:
239 2 newsv = pljsonsl_common_mkboolean(pjsn, state->special_flags);
240 2 break;
241 2 case JSONSL_SPECIALf_NULL:
242 2 newsv = newSV(0);
243 2 break;
244 13 default:
245 13 newsv = pljsonsl_common_mknumeric(state,
246 buf,
247 state->pos_cur - state->pos_begin);
248 10 break;
249 }
250  
251 30 ▶ if (newsv == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (newsv == NULL) { 0 30 30
252 0 warn("Buffer is %p", buf);
253 0 warn("Length is %lu", state->pos_cur - state->pos_begin);
254 0 warn("Special flag is %d", state->special_flags);
255 0 die("WTF!");
256 }
257  
258 30 state->sv = newsv;
259 30 return;
260 }
261  
262 /**
263 * This is called to clean up any quotes, and possibly
264 * handle \u-escapes in the future
265 */
266 #define process_string(pjsn,st) process_string_THX(aTHX_ pjsn,st)
267 static void
268 25 process_string_THX(pTHX_
269 PLJSONSL* pjsn,
270 struct jsonsl_state_st *state)
271 {
272 SV *retsv;
273 25 char *buf = GET_STATE_BUFFER(pjsn, state->pos_begin);
274 size_t buflen;
275 25 buf++;
276 25 buflen = (state->pos_cur - state->pos_begin) - 1;
277 25 retsv = newSV(buflen);
278  
279 25 sv_upgrade(retsv, SVt_PV);
280 25 SvPOK_on(retsv);
281  
282 25 ▶ if (state->nescapes == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->nescapes == 0) { 16 9 25
283 16 SvCUR_set(retsv, buflen);
284 16 memcpy(SvPVX(retsv), buf, buflen);
285 } else {
286 jsonsl_error_t err;
287 jsonsl_special_t flags;
288 size_t newlen;
289 9 newlen = jsonsl_util_unescape_ex(buf,
290 SvPVX(retsv),
291 buflen,
292 9 pjsn->escape_table,
293 &flags,
294 &err, NULL);
295 9 ▶ if (!newlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (!newlen) { 0 9 9
296 0 SvREFCNT_dec(retsv);
297 0 die("Could not unescape string: %s", jsonsl_strerror(err));
298 }
299 /* Shrink the buffer to the effective new size */
300 9 SvCUR_set(retsv, newlen);
301 9 ▶ if (flags & JSONSL_SPECIALf_NONASCII) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & JSONSL_SPECIALf_NONASCII) { 0 9 9
302 0 SvUTF8_on(retsv);
303 }
304 }
305  
306 25 state->sv = retsv;
307 25 ▶ if (pjsn->options.utf8) {
Branch: 1 branch
BranchTrueFalseTotal
if (pjsn->options.utf8) { 1 24 25
308 1 SvUTF8_on(state->sv);
309 }
310 25 }
311  
312  
313 /**
314 * This function will try and determine if the current
315 * item is a matched result (which should be returned to
316 * the user).
317 * If this is a complete match, the SV (along with relevant info)
318 * will be pushed to the result stack and return true. Returns
319 * false otherwise.
320 */
321 #define object_mkresult(pjsn,st_p,st_c) object_mkresult_THX(aTHX_ pjsn, st_p,st_c)
322 static inline int
323 1160 object_mkresult_THX(pTHX_
324 PLJSONSL *pjsn,
325 struct jsonsl_state_st *parent,
326 struct jsonsl_state_st *child)
327 {
328 #define STORE_INFO(b, v) \
329 (void)hv_stores(info_hv, PLJSONSL_INFO_KEY_##b, v)
330 HV *info_hv;
331  
332 1160 ▶ if (pjsn->options.object_drip == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
if (pjsn->options.object_drip == 0 && 1155 5 1160
333 1155 ▶ (child->matchres != JSONSL_MATCH_COMPLETE || child->type == JSONSL_T_HKEY)) {
Branch: 2 branches
BranchTrueFalseTotal
(child->matchres != JSONSL_MATCH_COMPLETE || child->type == JSONSL_T_HKEY)) { 8 1147 1155
(child->matchres != JSONSL_MATCH_COMPLETE || child->type == JSONSL_T_HKEY)) { 0 8 8
334 1147 return 0;
335 }
336  
337 13 info_hv = newHV();
338 13 ▶ if (SvTYPE(child->sv) == SVt_PVHV || SvTYPE(child->sv) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTYPE(child->sv) == SVt_PVHV || SvTYPE(child->sv) == SVt_PVAV) { 9 4 13
if (SvTYPE(child->sv) == SVt_PVHV || SvTYPE(child->sv) == SVt_PVAV) { 2 7 9
339 6 STORE_INFO(VALUE, newRV_noinc(child->sv));
340 } else {
341 7 STORE_INFO(VALUE, child->sv);
342 }
343  
344 13 ▶ if (pjsn->options.noqstr == 0 && pjsn->options.object_drip == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (pjsn->options.noqstr == 0 && pjsn->options.object_drip == 0) { 12 1 13
if (pjsn->options.noqstr == 0 && pjsn->options.object_drip == 0) { 7 5 12
345 7 STORE_INFO(QUERY, newSVpvn_share(child->matchjpr->orig,
346 child->matchjpr->norig, 0));
347 }
348  
349 13 ▶ if (pjsn->options.nopath == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (pjsn->options.nopath == 0) { 12 1 13
350 SV *pathstr;
351 int ii;
352 12 pathstr = newSVpvs("/");
353 33 ▶ for (ii = 2; ii <= (child->level); ii++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ii = 2; ii <= (child->level); ii++) { 21 12 33
354 21 struct jsonsl_state_st *cur = pjsn->jsn->stack + ii;
355 21 ▶ struct jsonsl_state_st *prev = jsonsl_last_state(pjsn->jsn, cur);
Branch: 1 branch
BranchTrueFalseTotal
struct jsonsl_state_st *prev = jsonsl_last_state(pjsn->jsn, cur); 21 0 21
356 21 ▶ if (prev->type == JSONSL_T_LIST) {
Branch: 1 branch
BranchTrueFalseTotal
if (prev->type == JSONSL_T_LIST) { 13 8 21
357 13 sv_catpvf(pathstr, "%d/", cur->u_loc.idx);
358 } else {
359 char *kbuf;
360 STRLEN klen;
361 assert(cur->u_loc.key);
362 8 ▶ kbuf = HePV(cur->u_loc.key, klen);
Branch: 1 branch
BranchTrueFalseTotal
kbuf = HePV(cur->u_loc.key, klen); 0 8 8
363 8 sv_catpvn(pathstr, kbuf, klen);
364 8 sv_catpvs(pathstr, "/");
365 8 ▶ if (HeKUTF8(cur->u_loc.key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (HeKUTF8(cur->u_loc.key)) { 0 8 8
366 0 SvUTF8_on(pathstr);
367 }
368 }
369 }
370 /* Trim the trailing '/' from the path string */
371 12 ▶ if (SvCUR(pathstr) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(pathstr) != 1) { 12 0 12
372 12 SvCUR_set(pathstr, SvCUR(pathstr)-1);
373 }
374 12 STORE_INFO(PATH, pathstr);
375 }
376  
377 /**
378 * For the sake of allowing inspection of the object tree, array
379 * and hash types are always added to their parents, even if they
380 * are a complete match to be removed from the stack.
381 */
382 13 ▶ if (parent && parent->sv) {
Branch: 2 branches
BranchTrueFalseTotal
if (parent && parent->sv) { 13 0 13
if (parent && parent->sv) { 13 0 13
383 13 SvREADONLY_off(parent->sv);
384 13 SvREFCNT_inc_simple_void_NN(child->sv);
385 13 ▶ if (parent->type == JSONSL_T_LIST) {
Branch: 1 branch
BranchTrueFalseTotal
if (parent->type == JSONSL_T_LIST) { 7 6 13
386 7 SV *popped_sv = av_pop((AV*)parent->sv);
387 7 ▶ if (popped_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (popped_sv) { 7 0 7
388 7 SvREFCNT_dec(popped_sv);
389 }
390 } else {
391 6 HE* child_he = child->u_loc.key;
392 6 SvREADONLY_off(HeVAL(child_he));
393 6 delete_he(pjsn, (HV*)parent->sv, child_he);
394 #if 0
395 /* for perls with hv_common, the above should be a macro for this: */
396 hv_common((HV*)parent->sv,
397 NULL, kbuf, klen,
398 0, /*maybe HVhek_UTF8?*/
399 HV_DELETE|G_DISCARD ,
400 NULL,
401 HeHASH(child->u_loc.key));
402 #endif
403 6 child->u_loc.key = NULL;
404 }
405  
406 13 SvREADONLY_on(parent->sv);
407 13 SvREADONLY_off(child->sv);
408 }
409  
410 13 av_push(pjsn->results, newRV_noinc((SV*)info_hv));
411 13 return 1;
412 #undef STORE_INFO
413 }
414  
415  
416 /**
417 * Because we only want to maintain 'complete' elements, for
418 * strings we ensure that their SVs do not get created until
419 * the entire string is done (as a partial string would
420 * not be of much use to the user anyway).
421 * The opposite is true of hashes and arrays, which we create
422 * immediately.
423 */
424 3219 static void body_push_callback(jsonsl_t jsn,
425 jsonsl_action_t action,
426 register struct jsonsl_state_st *state,
427 const char *at)
428 {
429 struct jsonsl_state_st *parent;
430 SV *newsv;
431 char *mkey;
432 size_t mnkey;
433 3219 register PLJSONSL *pjsn = (PLJSONSL*)jsn->data;
434 PLJSONSL_dTHX(pjsn);
435  
436 /* Reset the position first */
437  
438 3219 pjsn->keep_pos = state->pos_begin;
439  
440 3219 ▶ parent = jsonsl_last_state(jsn, state);
Branch: 1 branch
BranchTrueFalseTotal
parent = jsonsl_last_state(jsn, state); 3219 0 3219
441 /* Here we set up parent positioning variables.. */
442 3219 ▶ if (parent->type == JSONSL_T_OBJECT) {
Branch: 1 branch
BranchTrueFalseTotal
if (parent->type == JSONSL_T_OBJECT) { 2137 1082 3219
443 2137 ▶ if (state->type == JSONSL_T_HKEY) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->type == JSONSL_T_HKEY) { 1071 1066 2137
444 1071 return;
445 }
446 assert(pjsn->curhk);
447 1066 mkey = HeKEY(pjsn->curhk);
448 1066 mnkey = HeKLEN(pjsn->curhk);
449 /**
450 * Set the HE of our current value to the current HK, and then
451 * remove curhk's visibility.
452 */
453 1066 state->u_loc.key = pjsn->curhk;
454 1066 pjsn->curhk = NULL;
455 } else {
456 1082 state->u_loc.idx = parent->nelem - 1;
457 1082 mkey = NULL;
458 1082 mnkey = state->u_loc.idx;
459 }
460  
461 2148 ▶ if (parent->matchres == JSONSL_MATCH_POSSIBLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (parent->matchres == JSONSL_MATCH_POSSIBLE) { 12 2136 2148
462 12 state->matchjpr = jsonsl_jpr_match_state(jsn, state, mkey, mnkey,
463 12 &state->matchres);
464 } else {
465 2136 state->matchjpr = NULL;
466 2136 state->matchres = JSONSL_MATCH_NOMATCH;
467 }
468  
469 /**
470 * Ignore warnings about uninitialized newsv variable.
471 */
472 2148 ▶ if (!JSONSL_STATE_IS_CONTAINER(state)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!JSONSL_STATE_IS_CONTAINER(state)) { 1118 1030 2148
if (!JSONSL_STATE_IS_CONTAINER(state)) { 61 1057 1118
473 61 return; /* nothing more to do here. String types are added at POP */
474 }
475  
476 2087 ▶ if (state->type == JSONSL_T_OBJECT) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->type == JSONSL_T_OBJECT) { 1030 1057 2087
477 1030 newsv = (SV*)newHV();
478 1057 ▶ } else if (state->type == JSONSL_T_LIST) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (state->type == JSONSL_T_LIST) { 1057 0 1057
479 1057 newsv = (SV*)newAV();
480 } else {
481 0 die("WTF");
482 }
483  
484 2087 SvREADONLY_on(newsv);
485 2087 ▶ if (parent->type == JSONSL_T_LIST) {
Branch: 1 branch
BranchTrueFalseTotal
if (parent->type == JSONSL_T_LIST) { 1059 1028 2087
486 1059 SvREADONLY_off(parent->sv);
487 1059 av_push((AV*)parent->sv, newRV_noinc(newsv));
488 1059 SvREADONLY_on(parent->sv);
489 } else {
490 /* we have the HE. */
491 1028 HeVAL(state->u_loc.key) = newRV_noinc(newsv);
492 1028 SvREADONLY_on(HeVAL(state->u_loc.key));
493 }
494  
495 2087 state->sv = newsv;
496 }
497  
498 /**
499 * Creates a new HE*. We use this HE later on using HeVAL to assign the value.
500 */
501  
502 #define create_hk(pjsn,st_c,st_p) create_hk_THX(aTHX_ pjsn,st_c,st_p)
503 static void
504 1071 create_hk_THX(pTHX_ PLJSONSL *pjsn,
505 struct jsonsl_state_st *state,
506 struct jsonsl_state_st *parent)
507 {
508 1071 char *buf = GET_STATE_BUFFER(pjsn, state->pos_begin);
509 1071 STRLEN len = (state->pos_cur - state->pos_begin)-1;
510  
511 assert(pjsn->curhk == NULL);
512 1071 buf++;
513  
514 1071 SvREADONLY_off(parent->sv);
515  
516 1071 ▶ if (state->nescapes) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->nescapes) { 2 1069 1071
517 /* we have escapes within a key. rare, but allowable. No choice
518 * but to allocate a new buffer for it
519 */
520  
521 /* This sets state->sv to the key sv. would be nice if there was a cleaner
522 * path to this
523 */
524 2 process_string(pjsn, state);
525 2 pjsn->curhk = hv_store_ent((HV*)parent->sv, state->sv, &PL_sv_undef, 0);
526 2 SvREFCNT_dec(state->sv);
527 2 state->sv = NULL;
528 } else {
529  
530 /**
531 * Fast path, no copying to new SV.
532 * We need to store &PL_sv_undef first to fool hv_common
533 * into thinking we're not doing anything special. Then
534 * we switch it out to &PL_sv_placeholder so it doesn't appear
535 * visible.
536 */
537 1069 ▶ pjsn->curhk = storeget_he(pjsn, parent->sv, buf, len, &PL_sv_undef,
Branch: 2 branches
BranchTrueFalseTotal
pjsn->curhk = storeget_he(pjsn, parent->sv, buf, len, &PL_sv_undef, 1068 1 1069
pjsn->curhk = storeget_he(pjsn, parent->sv, buf, len, &PL_sv_undef, 0 1068 1068
538 /*determine if UTF8: */
539 pjsn->options.utf8 || state->special_flags == JSONSL_SPECIALf_NONASCII);
540 #if 0
541 /* which is really this: */
542 pjsn->curhk = hv_common((HV*)parent->sv, /* HV*/
543 NULL, /* keysv */
544 buf, len,
545 0, /* flags. Maybe setting utf8 */
546 HV_FETCH_ISSTORE, /*action*/
547 &PL_sv_undef, /*value*/
548 0);
549 #endif
550 }
551  
552 1071 HeVAL(pjsn->curhk) = &PL_sv_placeholder;
553 1071 SvREADONLY_on(parent->sv);
554 1071 }
555  
556 /* forward-declare initial state handler */
557 static void initial_callback(jsonsl_t jsn,
558 jsonsl_action_t action,
559 struct jsonsl_state_st *state,
560 const char *at);
561  
562 /**
563 * In this callback we ensure to clean up our strings and push it
564 * into the parent SV
565 */
566 2236 static void body_pop_callback(jsonsl_t jsn,
567 jsonsl_action_t action,
568 register struct jsonsl_state_st *state,
569 const char *at)
570 {
571 /* Ending of an element */
572 2236 ▶ struct jsonsl_state_st *parent = jsonsl_last_state(jsn, state);
Branch: 1 branch
BranchTrueFalseTotal
struct jsonsl_state_st *parent = jsonsl_last_state(jsn, state); 2193 43 2236
573 2236 register PLJSONSL *pjsn = (PLJSONSL*)jsn->data;
574 PLJSONSL_dTHX(pjsn);
575  
576 #define INSERT_STRING \
577 if (parent && object_mkresult(pjsn, parent, state) == 0) { \
578 SvREADONLY_off(parent->sv); \
579 if (parent->type == JSONSL_T_OBJECT) { \
580 assert(state->u_loc.key); \
581 HeVAL(state->u_loc.key) = state->sv; \
582 } else { \
583 av_push((AV*)parent->sv, state->sv); \
584 } \
585 SvREADONLY_on(parent->sv); \
586 } \
587  
588 2236 ▶ if (state->type == JSONSL_T_STRING) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->type == JSONSL_T_STRING) { 23 2213 2236
589 23 process_string(pjsn, state);
590 23 ▶ INSERT_STRING;
Branch: 3 branches
BranchTrueFalseTotal
INSERT_STRING; 23 0 23
INSERT_STRING; 18 5 23
INSERT_STRING; 14 4 18
591 2213 ▶ } else if (state->type == JSONSL_T_HKEY) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (state->type == JSONSL_T_HKEY) { 1071 1142 2213
592 assert(parent->type == JSONSL_T_OBJECT);
593 1071 create_hk(pjsn, state, parent);
594 1142 ▶ } else if (state->type == JSONSL_T_SPECIAL) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (state->type == JSONSL_T_SPECIAL) { 35 1107 1142
595 assert(state->special_flags);
596 35 process_special(pjsn, state);
597 30 ▶ INSERT_STRING;
Branch: 3 branches
BranchTrueFalseTotal
INSERT_STRING; 30 0 30
INSERT_STRING; 28 2 30
INSERT_STRING; 11 17 28
598 } else {
599 1107 SvREADONLY_off(state->sv);
600 1107 object_mkresult(pjsn, parent, state);
601 }
602  
603 #undef INSERT_STRING
604  
605 2231 ▶ if (state->sv == pjsn->root) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->sv == pjsn->root) { 43 2188 2231
606 43 ▶ if (pjsn->njprs == 0 && pjsn->options.object_drip == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (pjsn->njprs == 0 && pjsn->options.object_drip == 0) { 38 5 43
if (pjsn->njprs == 0 && pjsn->options.object_drip == 0) { 38 0 38
607 38 av_push(pjsn->results, newRV_noinc(pjsn->root));
608 } /* otherwise, already pushed */
609 43 pjsn->root = NULL;
610 43 jsn->action_callback_PUSH = initial_callback;
611 }
612  
613 2231 state->u_loc.idx = -1;
614 2231 state->sv = NULL;
615 2231 pjsn->keep_pos = 0;
616  
617 2231 }
618  
619 9 static int error_callback(jsonsl_t jsn,
620 jsonsl_error_t err,
621 struct jsonsl_state_st *state,
622 char *at)
623 {
624 9 PLJSONSL *pjsn = (PLJSONSL*)jsn->data;
625 PLJSONSL_dTHX(pjsn);
626 /**
627 * TODO: allow option for user-defined recovery function
628 */
629  
630 9 die("JSON::SL - Got error %s at position %lu", jsonsl_strerror(err), jsn->pos);
631 return 0;
632 }
633  
634 4 static void invoke_root_cb(PLJSONSL *pjsn)
635 {
636 PLJSONSL_dTHX(pjsn);
637 4 ▶ if (!pjsn->options.root_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pjsn->options.root_callback) { 0 4 4
638 0 return;
639 }
640 4 dSP;
641 4 ENTER;
642 4 SAVETMPS;
643 4 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 4 4
644 4 ▶ XPUSHs(sv_2mortal(newRV_inc(pjsn->root)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_inc(pjsn->root))); 0 4 4
645 4 PUTBACK;
646 4 call_sv(pjsn->options.root_callback, G_DISCARD);
647 4 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 4 0 4
648 4 LEAVE;
649 }
650  
651 60 static void initial_callback(jsonsl_t jsn,
652 jsonsl_action_t action,
653 struct jsonsl_state_st *state,
654 const char *at)
655 {
656 60 PLJSONSL *pjsn = (PLJSONSL*)jsn->data;
657 PLJSONSL_dTHX(pjsn);
658  
659 assert(action == JSONSL_ACTION_PUSH);
660 60 ▶ if (state->type == JSONSL_T_LIST) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->type == JSONSL_T_LIST) { 26 34 60
661 26 pjsn->root = (SV*)newAV();
662 34 ▶ } else if (state->type == JSONSL_T_OBJECT) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (state->type == JSONSL_T_OBJECT) { 34 0 34
663 34 pjsn->root = (SV*)newHV();
664 } else {
665 0 die("Found type %s as root element", jsonsl_strtype(state->type));
666 }
667  
668 60 ▶ if (pjsn->options.root_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (pjsn->options.root_callback) { 4 56 60
669 4 invoke_root_cb(pjsn);
670 }
671  
672 60 state->sv = pjsn->root;
673 60 jsn->action_callback = NULL;
674 60 jsn->action_callback_PUSH = body_push_callback;
675 60 jsn->action_callback_POP = body_pop_callback;
676 60 jsonsl_jpr_match_state(jsn, state, NULL, 0, &state->matchres);
677 /* Mark root element as read only */
678 60 SvREADONLY_on(pjsn->root);
679 60 }
680  
681 #define CHECK_MAX_SIZE(pjsn,input) \
682 if (pjsn->options.max_size && SvCUR(input) > pjsn->options.max_size) { \
683 die("JSON::SL - max_size is %lu, but input is %lu bytes", \
684 pjsn->options.max_size, SvCUR(input)); \
685 }
686  
687 #define pljsonsl_feed_incr(pjsn,str) pljsonsl_feed_incr_THX(aTHX_ pjsn,str)
688 static void
689 24 pljsonsl_feed_incr_THX(pTHX_ PLJSONSL* pjsn, SV *input)
690 {
691 24 size_t start_pos = pjsn->jsn->pos;
692 24 STRLEN cur_len = SvCUR(pjsn->buf);
693  
694 24 ▶ if (!SvPOK(input)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvPOK(input)) { 0 24 24
695 0 die("Input is not a string");
696 }
697  
698 24 ▶ CHECK_MAX_SIZE(pjsn, input)
Branch: 2 branches
BranchTrueFalseTotal
CHECK_MAX_SIZE(pjsn, input) 2 22 24
CHECK_MAX_SIZE(pjsn, input) 1 1 2
699  
700 23 pjsn->pos_min_valid = pjsn->jsn->pos - cur_len;
701 23 ▶ if (SvUTF8(input)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(input)) { 1 22 23
702 1 pjsn->options.utf8 = 1;
703 }
704 23 sv_catpvn(pjsn->buf, SvPVX_const(input), SvCUR(input));
705 23 jsonsl_feed(pjsn->jsn,
706 23 SvPVX_const(pjsn->buf) + (SvCUR(pjsn->buf)-SvCUR(input)),
707 23 SvCUR(input));
708 /**
709 * Callbacks may detect the beginning of a string, in which case
710 * we need to ensure the continuity of the string. In this case
711 * pos_keep is set to the position of the input stream (not the SV *input,
712 * but rather jsn->pos) from which we should begin buffering data.
713 *
714 * Now we might need to chop. The amount of bytes to chop is the
715 * difference between start_pos and the keep_pos
716 * variable (if any)
717 */
718 11 ▶ if (pjsn->keep_pos == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (pjsn->keep_pos == 0) { 11 0 11
719 11 SvCUR_set(pjsn->buf, 0);
720 0 ▶ } else if (pjsn->keep_pos > start_pos) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (pjsn->keep_pos > start_pos) { 0 0 0
721 0 sv_chop(pjsn->buf, SvPVX_const(pjsn->buf) + (pjsn->keep_pos - start_pos));
722 }
723 11 }
724  
725 static PLJSONSL*
726 32 pljsonsl_get_and_initialize_global(pTHX)
727 {
728 dMY_CXT;
729 PLJSONSL *pjsn;
730 32 ▶ if (MY_CXT.quick == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.quick == NULL) { 7 25 32
731 7 Newxz(pjsn, 1, PLJSONSL);
732 7 pljsonsl_common_initialize(&MY_CXT, pjsn, PLJSONSL_MAX_DEFAULT-1);
733 7 pjsn->priv_global.is_global = 1;
734 PLJSONSL_INIT_KSV(pjsn);
735 7 MY_CXT.quick = pjsn;
736 }
737  
738 32 pjsn = MY_CXT.quick;
739 32 jsonsl_reset(pjsn->jsn);
740 32 jsonsl_enable_all_callbacks(pjsn->jsn);
741 32 pjsn->jsn->error_callback = error_callback;
742 32 pjsn->jsn->action_callback_PUSH = initial_callback;
743 32 pjsn->results = (AV*)sv_2mortal((SV*)newAV());
744 32 return pjsn;
745 }
746  
747 #define pljsonsl_feed_oneshot(pjsn,str) pljsonsl_feed_oneshot_THX(aTHX_ pjsn,str)
748 static void
749 32 pljsonsl_feed_oneshot_THX(pTHX_ PLJSONSL* pjsn, SV *input)
750 {
751 32 ▶ if (!SvPOK(input)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvPOK(input)) { 0 32 32
752 0 die("Input is not a string");
753 }
754  
755 32 ▶ if (SvUTF8(input)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(input)) { 0 32 32
756 0 pjsn->options.utf8 = 1;
757 }
758 32 ▶ CHECK_MAX_SIZE(pjsn, input);
Branch: 2 branches
BranchTrueFalseTotal
CHECK_MAX_SIZE(pjsn, input); 0 32 32
CHECK_MAX_SIZE(pjsn, input); 0 0 0
759 32 pjsn->buf = input;
760 32 jsonsl_feed(pjsn->jsn, SvPVX_const(input), SvCUR(input));
761 30 pjsn->buf = NULL;
762 30 pjsn->options.utf8 = 0;
763 /* the current root is never in the result stack..
764 * so mortalizing it won't hurt anyone.
765 */
766 30 }
767  
768 /**
769 * Takes an array ref (or list?) of JSONPointer strings and converts
770 * them to JPR objects. Dies on error
771 */
772 #define pljsonsl_set_jsonpointer(pjsn,jprstr) \
773 pljsonsl_set_jsonpointer_THX(aTHX_ pjsn,jprstr)
774 static void
775 4 pljsonsl_set_jsonpointer_THX(pTHX_ PLJSONSL *pjsn, AV *paths)
776 {
777 jsonsl_jpr_t *jprs;
778 jsonsl_error_t err;
779 int ii;
780 4 int max = av_len(paths)+1;
781 const char *diestr, *pathstr;
782  
783 4 ▶ if (!max) {
Branch: 1 branch
BranchTrueFalseTotal
if (!max) { 0 4 4
784 0 die("No paths given!");
785 }
786  
787 4 Newxz(jprs, max, jsonsl_jpr_t);
788  
789 8 ▶ for (ii = 0; ii < max; ii++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ii = 0; ii < max; ii++) { 4 4 8
790 4 SV **tmpsv = av_fetch(paths, ii, 0);
791 4 ▶ if (tmpsv == NULL || SvPOK(*tmpsv) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (tmpsv == NULL || SvPOK(*tmpsv) == 0) { 4 0 4
if (tmpsv == NULL || SvPOK(*tmpsv) == 0) { 0 4 4
792 0 diestr = "Found empty path";
793 0 goto GT_ERR;
794 }
795 4 jprs[ii] = jsonsl_jpr_new(SvPVX_const(*tmpsv), &err);
796 4 ▶ if (jprs[ii] == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (jprs[ii] == NULL) { 0 4 4
797 0 pathstr = SvPVX_const(*tmpsv);
798 0 goto GT_ERR;
799 }
800 }
801  
802 4 jsonsl_jpr_match_state_init(pjsn->jsn, jprs, max);
803 4 pjsn->jprs = jprs;
804 4 pjsn->njprs = max;
805 4 return;
806  
807 0 GT_ERR:
808 0 ▶ for (ii = 0; ii < max; ii++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ii = 0; ii < max; ii++) { 0 0 0
809 0 ▶ if (jprs[ii] == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (jprs[ii] == NULL) { 0 0 0
810 0 break;
811 }
812 0 jsonsl_jpr_destroy(jprs[ii]);
813 }
814 0 Safefree(jprs);
815 0 ▶ if (pathstr) {
Branch: 1 branch
BranchTrueFalseTotal
if (pathstr) { 0 0 0
816 0 die("Couldn't convert %s to jsonpointer: %s", pathstr, jsonsl_strerror(err));
817 } else {
818 0 die(diestr);
819 }
820 }
821  
822  
823  
824  
825  
826 /**
827 * JSON::SL::Tuba functions.
828 * In case you haven't wondered already, 'Tuba' is a play on 'SAX'.
829 */
830  
831  
832 /**
833 * This is our quick version of MRO caching. Maybe I'll swap this out
834 * for something which already exists (as I get the feeling I've reinveted
835 * the wheel here.
836 * namep is populated with the handler name, gvp is a pointer to the GV**
837 * - or an offset into the PLTUBA's methgv structure.
838 */
839 static void
840 55 pltuba_get_method_info(PLTUBA *tuba,
841 jsonsl_action_t action,
842 pltuba_callback_type cbtype,
843 GV ***gvpp,
844 const char **namep)
845 {
846 55 GV **methgvp = NULL;
847 55 const char *methname = NULL;
848 55 cbtype &= 0x7f;
849  
850 55 ▶ if (tuba->options.cb_unified) {
Branch: 1 branch
BranchTrueFalseTotal
if (tuba->options.cb_unified) { 0 55 55
851 0 methname = "on_any";
852 0 methgvp = &tuba->methgv.on_any;
853 0 goto GT_RETASGN;
854 }
855 #define PLTUBA_METH_GETMETH
856 #include "srcout/tuba_dispatch_getmeth.h"
857 #undef PLTUBA_METH_GETMETH
858 55 GT_RETASGN:
859 55 ▶ if (gvpp) {
Branch: 1 branch
BranchTrueFalseTotal
if (gvpp) { 55 0 55
860 55 *gvpp = methgvp;
861 }
862 55 ▶ if (namep) {
Branch: 1 branch
BranchTrueFalseTotal
if (namep) { 55 0 55
863 55 *namep = methname;
864 }
865 55 }
866  
867 static void
868 4 pltuba_invalidate_gvs_THX(pTHX_ PLTUBA *tuba)
869 {
870  
871 #define X(action,type) \
872 REFDEC_FIELD(tuba, methgv.action## _ ##type)
873  
874 4 ▶ PLTUBA_XMETHGV
Branch: 33 branches
BranchTrueFalseTotal
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 1 3 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
PLTUBA_XMETHGV 0 4 4
875 #undef X
876 4 }
877  
878 /**
879 * Maps a 'jsonsl' type to a tuba callback type.
880 */
881 static pltuba_callback_type
882 54 convert_to_tuba_cbt(struct jsonsl_state_st *state)
883 {
884 54 ▶ if (state->type != JSONSL_T_SPECIAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->type != JSONSL_T_SPECIAL) { 44 10 54
885 44 return state->type;
886 }
887 10 ▶ if (state->special_flags & JSONSL_SPECIALf_BOOLEAN) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->special_flags & JSONSL_SPECIALf_BOOLEAN) { 4 6 10
888 4 return PLTUBA_CALLBACK_BOOLEAN;
889 6 ▶ } else if (state->special_flags & JSONSL_SPECIALf_NUMERIC) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (state->special_flags & JSONSL_SPECIALf_NUMERIC) { 4 2 6
890 4 return PLTUBA_CALLBACK_NUMBER;
891 2 ▶ } else if (state->special_flags == JSONSL_SPECIALf_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (state->special_flags == JSONSL_SPECIALf_NULL) { 2 0 2
892 2 return PLTUBA_CALLBACK_NULL;
893 }
894 0 die("wtf?");
895 return 0;
896 }
897  
898 /**
899 * This function invokes the selected callback (if it exists).
900 */
901 #define pltuba_invoke_callback(tb,a,cbt,sv) \
902 pltuba_invoke_callback_THX(aTHX_ tb,a,cbt,sv)
903 static void
904 25 pltuba_invoke_callback_THX(pTHX_ PLTUBA *tuba,
905 int action,
906 pltuba_callback_type cbtype,
907 SV *mextrasv)
908 {
909 25 dSP;
910 25 GV **methp = NULL;
911 25 GV *meth = NULL;
912 25 const char *meth_name = NULL;
913 25 int effective_type = cbtype;
914 25 int effective_action = action;
915 25 int stop_mro = 0;
916 /**
917 * If we are in a pop mode of a callback with the accumulator flag set,
918 * then we provide the data in the SV as the argument (maybe with some
919 * conversion into an appropriate object), otherwise, we just signal as
920 * normal.
921 */
922 25 cbtype &= 0x7f;
923  
924 25 ▶ if (tuba->accum && action == JSONSL_ACTION_POP) {
Branch: 2 branches
BranchTrueFalseTotal
if (tuba->accum && action == JSONSL_ACTION_POP) { 13 12 25
if (tuba->accum && action == JSONSL_ACTION_POP) { 13 0 13
925 13 effective_action = PLTUBA_ACTION_ON;
926 13 pltuba_get_method_info(tuba, PLTUBA_ACTION_ON, cbtype, &methp, &meth_name);
927 assert(mextrasv == NULL);
928 13 mextrasv = tuba->accum;
929 13 tuba->accum = NULL;
930 } else {
931 12 pltuba_get_method_info(tuba, action, cbtype, &methp, &meth_name);
932 }
933  
934 25 ▶ if (meth_name == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (meth_name == NULL) { 0 25 25
935 0 die("Can't find method name. Action=%c, Type=%c", action, cbtype);
936 }
937  
938 25 ▶ if (!mextrasv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!mextrasv) { 12 13 25
939 12 mextrasv = &PL_sv_undef;
940 } else {
941 13 sv_2mortal(mextrasv);
942 }
943  
944 assert(methp);
945  
946 25 ▶ if (tuba->last_stash != SvSTASH(SvRV(tuba->selfrv))) {
Branch: 1 branch
BranchTrueFalseTotal
if (tuba->last_stash != SvSTASH(SvRV(tuba->selfrv))) { 2 23 25
947 2 pltuba_invalidate_gvs_THX(aTHX_ tuba);
948 2 tuba->last_stash = SvSTASH(SvRV(tuba->selfrv));
949 }
950  
951 do {
952 50 ▶ if (*methp == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (*methp == NULL) { 31 19 50
953 31 meth = gv_fetchmethod_autoload(SvSTASH(SvRV(tuba->selfrv)), meth_name, 1);
954 31 ▶ if (meth && GvCV(meth)) {
Branch: 2 branches
BranchTrueFalseTotal
if (meth && GvCV(meth)) { 1 30 31
if (meth && GvCV(meth)) { 1 0 1
955 1 ▶ if (tuba->options.no_cache_mro == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (tuba->options.no_cache_mro == 0) { 1 0 1
956 1 *methp = meth;
957 1 SvREFCNT_inc(meth);
958 }
959 1 break;
960 } /* else */
961 30 pltuba_get_method_info(tuba, PLTUBA_ACTION_ON,
962 PLTUBA_CALLBACK_ANY, &methp, &meth_name);
963 assert(methp && meth_name);
964 30 stop_mro++;
965 } else {
966 19 meth = *methp;
967 19 break;
968 }
969 30 ▶ } while (stop_mro < 2);
Branch: 1 branch
BranchTrueFalseTotal
} while (stop_mro < 2); 25 5 30
970  
971 25 PLTUBA_SET_PARAMFIELDS_dv(tuba, Mode, effective_action);
972 25 PLTUBA_SET_PARAMFIELDS_dv(tuba, Type, effective_type);
973  
974 /**
975 * We still want a SAVETMPS/FREETMPS pair active before we decide
976 * to call a function or not, as the contents mextrasv and possibly
977 * some of the hash values are mortalized.
978 */
979 25 ENTER; SAVETMPS;
980 25 ▶ if (meth && GvCV(meth)) {
Branch: 2 branches
BranchTrueFalseTotal
if (meth && GvCV(meth)) { 20 5 25
if (meth && GvCV(meth)) { 20 0 20
981 20 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 20 20
982 20 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 20 20
983 20 PUSHs(tuba->selfrv);
984 20 PUSHs(tuba->paramhvrv);
985 20 ▶ if (mextrasv != &PL_sv_undef) {
Branch: 1 branch
BranchTrueFalseTotal
if (mextrasv != &PL_sv_undef) { 11 9 20
986 11 ▶ XPUSHs(mextrasv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(mextrasv); 0 11 11
987 }
988 20 PUTBACK;
989 20 call_sv((SV*)GvCV(meth), G_DISCARD);
990 } else {
991 5 ▶ if (!tuba->options.allow_unhandled) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tuba->options.allow_unhandled) { 0 5 5
992 0 die("Tuba: Cannot find handler for mode 0x%02x action 0x%02x",
993 effective_action, effective_type);
994 }
995 }
996 25 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 25 25
997 25 }
998  
999 /**
1000 * Flush characters between the invocation of the last callback
1001 * and the current one. the until argument is the end position (inclusive)
1002 * at which we should stop submitting 'character' data.
1003 */
1004 #define pltuba_flush_characters(tb,end) \
1005 pltuba_flush_characters_THX(aTHX_ tb,end)
1006 static void
1007 23 pltuba_flush_characters_THX(pTHX_ PLTUBA *tuba, size_t until)
1008 {
1009 STRLEN toFlush;
1010 const char *buf;
1011 SV *chunksv;
1012  
1013 23 ▶ if (!tuba->keep_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tuba->keep_pos) { 0 23 23
1014 0 return;
1015 }
1016  
1017 23 toFlush = (until - tuba->keep_pos);
1018 23 ▶ if (toFlush == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (toFlush == 0) { 0 23 23
1019 0 return;
1020 }
1021 23 buf = GET_STATE_BUFFER(tuba, tuba->keep_pos);
1022  
1023 23 ▶ if (tuba->shift_quote) {
Branch: 1 branch
BranchTrueFalseTotal
if (tuba->shift_quote) { 18 5 23
1024 18 buf++;
1025 18 toFlush--;
1026 }
1027  
1028 23 tuba->keep_pos = 0;
1029 23 tuba->shift_quote = 0;
1030  
1031 23 ▶ if (toFlush == 0 && tuba->shift_quote == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (toFlush == 0 && tuba->shift_quote == 0) { 0 23 23
if (toFlush == 0 && tuba->shift_quote == 0) { 0 0 0
1032 /* if we have no data and the count was not artificially decremented, then
1033 * don't invoke the callback
1034 */
1035 0 return;
1036 }
1037  
1038 /* if accumulator mode is on, don't send the data right away.
1039 * buffer it instead */
1040 23 ▶ if (tuba->accum) {
Branch: 1 branch
BranchTrueFalseTotal
if (tuba->accum) { 23 0 23
1041 23 sv_catpvn(tuba->accum, buf, toFlush);
1042 23 return;
1043 } /* else, no accum for this state */
1044  
1045 0 chunksv = newSVpvn(buf, toFlush);
1046 0 pltuba_invoke_callback(tuba,
1047 PLTUBA_ACTION_ON,
1048 PLTUBA_CALLBACK_DATA,
1049 chunksv);
1050 /**
1051 * SV has been mortalized by the invoke_callback function
1052 */
1053 }
1054 /**
1055 * Push callback. This is easy because we never actually do any character
1056 * data here.
1057 */
1058 static void
1059 27 pltuba_jsonsl_push_callback(jsonsl_t jsn,
1060 jsonsl_action_t action,
1061 struct jsonsl_state_st *state,
1062 const char *at)
1063 {
1064 27 PLTUBA *tuba = (PLTUBA*)jsn->data;
1065 PLJSONSL_dTHX(tuba);
1066 27 ▶ struct jsonsl_state_st *parent = jsonsl_last_state(jsn, state);
Branch: 1 branch
BranchTrueFalseTotal
struct jsonsl_state_st *parent = jsonsl_last_state(jsn, state); 25 2 27
1067 27 pltuba_callback_type cbt = convert_to_tuba_cbt(state);
1068 27 ▶ if (state->level == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->level == 1) { 2 25 27
1069 2 pltuba_invoke_callback(tuba, action, PLTUBA_CALLBACK_JSON, NULL);
1070 } else {
1071 assert(parent);
1072 25 ▶ if (parent->type == JSONSL_T_LIST) {
Branch: 1 branch
BranchTrueFalseTotal
if (parent->type == JSONSL_T_LIST) { 5 20 25
1073 5 PLTUBA_SET_PARAMFIELDS_iv(tuba, Index, parent->nelem-1);
1074 } else {
1075 20 PLTUBA_RESET_PARAMFIELD(tuba, Index);
1076 }
1077 }
1078  
1079 27 ▶ if (tuba->accum_options[cbt & 0x7f]) {
Branch: 1 branch
BranchTrueFalseTotal
if (tuba->accum_options[cbt & 0x7f]) { 23 4 27
1080 assert(tuba->accum == NULL);
1081 /* accum is only ever valid for atomic types */
1082 23 tuba->accum = newSVpvn("", 0);
1083 } else {
1084 4 ▶ if (JSONSL_STATE_IS_CONTAINER(state) && tuba->kaccum) {
Branch: 3 branches
BranchTrueFalseTotal
if (JSONSL_STATE_IS_CONTAINER(state) && tuba->kaccum) { 2 2 4
if (JSONSL_STATE_IS_CONTAINER(state) && tuba->kaccum) { 2 0 2
if (JSONSL_STATE_IS_CONTAINER(state) && tuba->kaccum) { 2 2 4
1085 2 sv_2mortal(tuba->kaccum);
1086 2 tuba->kaccum = NULL;
1087 2 pltuba_invoke_callback(tuba, action, cbt, NULL);
1088 2 PLTUBA_RESET_PARAMFIELD(tuba, Key);
1089 } else {
1090 2 pltuba_invoke_callback(tuba, action, cbt, NULL);
1091 }
1092 }
1093  
1094 /* This is a different branch and must get executed regardless
1095 * of whether we invoke a callback or use the accumulator */
1096 27 ▶ if (!JSONSL_STATE_IS_CONTAINER(state)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!JSONSL_STATE_IS_CONTAINER(state)) { 25 2 27
if (!JSONSL_STATE_IS_CONTAINER(state)) { 23 2 25
1097 23 tuba->keep_pos = state->pos_begin;
1098 23 ▶ if (state->type & JSONSL_Tf_STRINGY) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->type & JSONSL_Tf_STRINGY) { 18 5 23
1099 18 tuba->shift_quote = 1;
1100 }
1101 } else {
1102 4 tuba->keep_pos = 0;
1103 }
1104 27 }
1105  
1106 /**
1107 * If we're special, then convert all weird stuff to their
1108 * proper perly form. Simple plain integers are not weird and
1109 * can be stringified on demand.
1110 * This is akin to JSON::SL's process_string and process_special
1111 * functions.
1112 */
1113 #define pltuba_process_accum(tuba, state) \
1114 pltuba_process_accum_THX(aTHX_ tuba, state)
1115 static void
1116 23 pltuba_process_accum_THX(pTHX_
1117 PLTUBA *tuba,
1118 struct jsonsl_state_st *state)
1119 {
1120 23 ▶ if (state->type == JSONSL_T_SPECIAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->type == JSONSL_T_SPECIAL) { 5 18 23
1121 SV *newsv;
1122  
1123 5 ▶ if ( (state->special_flags & JSONSL_SPECIALf_NUMERIC) &&
Branch: 1 branch
BranchTrueFalseTotal
if ( (state->special_flags & JSONSL_SPECIALf_NUMERIC) && 2 3 5
1124 2 ▶ (state->special_flags & JSONSL_SPECIALf_NUMNOINT) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
(state->special_flags & JSONSL_SPECIALf_NUMNOINT) == 0) { 0 2 2
1125 0 goto GT_NONEWSV;
1126  
1127 5 ▶ } else if (state->special_flags & JSONSL_SPECIALf_NUMNOINT) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (state->special_flags & JSONSL_SPECIALf_NUMNOINT) { 2 3 5
1128 2 newsv = pljsonsl_common_mknumeric(state,
1129 SvPVX_const(tuba->accum),
1130 state->pos_cur - state->pos_begin);
1131 3 ▶ } else if (state->special_flags & JSONSL_SPECIALf_BOOLEAN) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (state->special_flags & JSONSL_SPECIALf_BOOLEAN) { 2 1 3
1132 2 newsv = pljsonsl_common_mkboolean((PLJSONSL*)tuba,
1133 state->special_flags);
1134 } else {
1135 1 newsv = &PL_sv_undef;
1136 }
1137 5 SvREFCNT_dec(tuba->accum);
1138 5 tuba->accum = newsv;
1139 5 GT_NONEWSV:
1140 ;
1141 } else {
1142 18 ▶ if (tuba->options.utf8) {
Branch: 1 branch
BranchTrueFalseTotal
if (tuba->options.utf8) { 0 18 18
1143 0 SvUTF8_on(tuba->accum);
1144 }
1145 18 ▶ if (state->nescapes) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->nescapes) { 2 16 18
1146 jsonsl_error_t err;
1147 jsonsl_special_t flags;
1148 size_t newlen;
1149 2 newlen = jsonsl_util_unescape_ex(SvPVX_const(tuba->accum),
1150 2 SvPVX(tuba->accum),
1151 2 SvCUR(tuba->accum),
1152 2 tuba->escape_table,
1153 &flags,
1154 &err,
1155 NULL);
1156 2 ▶ if (newlen == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (newlen == 0) { 0 2 2
1157 0 die("Could not unescape string: %s", jsonsl_strerror(err));
1158 }
1159 2 SvCUR_set(tuba->accum, newlen);
1160 2 ▶ if (flags & JSONSL_SPECIALf_NONASCII) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & JSONSL_SPECIALf_NONASCII) { 0 2 2
1161 0 SvUTF8_on(tuba->accum);
1162 }
1163 }
1164 }
1165 23 }
1166  
1167 static void
1168 27 pltuba_jsonsl_pop_callback(jsonsl_t jsn,
1169 jsonsl_action_t action,
1170 struct jsonsl_state_st *state,
1171 const char *at)
1172 {
1173 27 PLTUBA *tuba = (PLTUBA*)jsn->data;
1174 PLJSONSL_dTHX(tuba);
1175 27 pltuba_callback_type cbt = convert_to_tuba_cbt(state);
1176  
1177 27 ▶ if (!JSONSL_STATE_IS_CONTAINER(state)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!JSONSL_STATE_IS_CONTAINER(state)) { 25 2 27
if (!JSONSL_STATE_IS_CONTAINER(state)) { 23 2 25
1178 /* Special handling for character crap.. */
1179 23 pltuba_flush_characters(tuba, state->pos_cur);
1180  
1181  
1182 23 ▶ if (tuba->accum) {
Branch: 1 branch
BranchTrueFalseTotal
if (tuba->accum) { 23 0 23
1183 23 pltuba_process_accum(tuba, state);
1184 } else {
1185 0 ▶ if (state->nescapes) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->nescapes) { 0 0 0
1186 0 PLTUBA_SET_PARAMFIELDS_sv(tuba, Escaped, &PL_sv_yes);
1187 }
1188 }
1189  
1190 23 ▶ if (state->type == JSONSL_T_HKEY &&
Branch: 1 branch
BranchTrueFalseTotal
if (state->type == JSONSL_T_HKEY && 10 13 23
1191 10 ▶ tuba->options.accum_kv) {
Branch: 1 branch
BranchTrueFalseTotal
tuba->options.accum_kv) { 10 0 10
1192 /**
1193 * If we are accumulating the key then don't flush characters under
1194 * any circumstances. Just swap over the accumulator buffer
1195 */
1196 assert(tuba->accum);
1197 10 tuba->kaccum = tuba->accum;
1198 10 tuba->accum = NULL;
1199 10 tuba->keep_pos = 0;
1200 10 PLTUBA_SET_PARAMFIELDS_sv(tuba, Key, tuba->kaccum);
1201 10 return;
1202 }
1203 }
1204  
1205 17 ▶ if (tuba->kaccum && state->type != JSONSL_T_HKEY) {
Branch: 2 branches
BranchTrueFalseTotal
if (tuba->kaccum && state->type != JSONSL_T_HKEY) { 8 9 17
if (tuba->kaccum && state->type != JSONSL_T_HKEY) { 8 0 8
1206 8 sv_2mortal(tuba->kaccum);
1207 8 tuba->kaccum = NULL;
1208 }
1209  
1210 17 pltuba_invoke_callback(tuba, action, cbt, NULL);
1211  
1212 /**
1213 * Clear all fields
1214 */
1215 #define X(kname) \
1216 PLTUBA_RESET_PARAMFIELD(tuba, kname);
1217 17 PLTUBA_XPARAMS;
1218 #undef X
1219  
1220 17 ▶ if (state->level == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->level == 1) { 2 15 17
1221 2 pltuba_invoke_callback(tuba, action, PLTUBA_CALLBACK_JSON, NULL);
1222 }
1223 17 tuba->keep_pos = 0;
1224 }
1225  
1226 static int
1227 0 pltuba_jsonsl_error_callback(jsonsl_t jsn,
1228 jsonsl_error_t error,
1229 struct jsonsl_state_st *state,
1230 char *at)
1231 {
1232 /**
1233 * This needs special handling, as we will be receiving a return
1234 * value from Perl for this..
1235 */
1236 0 die ("Got error: %s", jsonsl_strerror(error));
1237 return 0;
1238 }
1239  
1240 #define pltuba_feed(tb,str) pltuba_feed_THX(aTHX_ tb,str)
1241 static void
1242 2 pltuba_feed_THX(pTHX_ PLTUBA *tuba, SV *input)
1243 {
1244 2 ▶ if (!SvPOK(input)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvPOK(input)) { 0 2 2
1245 0 die("Input is not string!");
1246 }
1247 2 tuba->buf = input;
1248 2 tuba->pos_min_valid = tuba->jsn->pos;
1249  
1250 2 SvREADONLY_on(input);
1251 2 jsonsl_feed(tuba->jsn, SvPVX_const(input), SvCUR(input));
1252 2 ▶ if (tuba->keep_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (tuba->keep_pos) { 0 2 2
1253 0 pltuba_flush_characters(tuba, tuba->jsn->pos);
1254 0 tuba->keep_pos = tuba->jsn->pos;
1255 }
1256 2 SvREADONLY_off(input);
1257 2 }
1258  
1259 static SV *
1260 2 pltuba_initialize_THX(pTHX_ const char *pkg)
1261 {
1262 SV *ptriv, *retrv;
1263 HV *hvret;
1264 HV *subclass;
1265 dMY_CXT;
1266  
1267 /* Initialize our internal C data structures */
1268 PLTUBA *tuba;
1269 2 Newxz(tuba, 1, PLTUBA);
1270 2 pljsonsl_common_initialize(&MY_CXT, (PLJSONSL*)tuba, PLJSONSL_MAX_DEFAULT);
1271  
1272 2 tuba->jsn->action_callback_PUSH = pltuba_jsonsl_push_callback;
1273 2 tuba->jsn->action_callback_POP = pltuba_jsonsl_pop_callback;
1274 2 tuba->jsn->error_callback = pltuba_jsonsl_error_callback;
1275 2 jsonsl_enable_all_callbacks(tuba->jsn);
1276  
1277 2 ptriv = newSViv(PTR2IV(tuba));
1278 2 SvREADONLY_on(ptriv);
1279  
1280 /* The Perl object .. */
1281 2 hvret = newHV();
1282 2 (void)hv_stores(hvret, PLTUBA_HKEY_NAME, ptriv);
1283 2 tuba->selfrv = newRV_inc((SV*)hvret);
1284 2 sv_rvweaken(tuba->selfrv);
1285 2 retrv = newRV_noinc((SV*)hvret);
1286  
1287 2 subclass = gv_stashpv(pkg, GV_ADD);
1288 2 sv_bless(retrv, subclass);
1289  
1290 2 tuba->paramhv = newHV();
1291 2 tuba->paramhvrv = newRV_noinc((SV*)tuba->paramhv);
1292 {
1293 2 SV *ksv = newSV(0);
1294 HE *tmphe;
1295  
1296 #define X(kname) \
1297 sv_setpvs(ksv, #kname); \
1298 tmphe = hv_store_ent(tuba->paramhv, ksv, &PL_sv_undef, 0); \
1299 HeVAL(tmphe) = &PL_sv_placeholder; \
1300 assert(tmphe); \
1301 tuba->p_ents.pe_##kname.he = tmphe;
1302  
1303 2 PLTUBA_XPARAMS;
1304 #undef X
1305 }
1306  
1307 #define initialize_param_iv(b) \
1308 PLTUBA_PARAM_FIELD(tuba, b).sv = newSViv(0); \
1309 SvREADONLY_on(PLTUBA_PARAM_FIELD(tuba,b).sv);
1310 #define initialize_param_dualvar(b) \
1311 PLTUBA_PARAM_FIELD(tuba, b).sv = newSViv(0); \
1312 sv_setpv(PLTUBA_PARAM_FIELD(tuba, b).sv, " "); \
1313 SvIOK_on(PLTUBA_PARAM_FIELD(tuba,b).sv); \
1314 SvREADONLY_on(PLTUBA_PARAM_FIELD(tuba,b).sv);
1315  
1316 2 initialize_param_iv(Index);
1317 2 initialize_param_dualvar(Mode);
1318 2 initialize_param_dualvar(Type);
1319  
1320 #undef initialize_param_iv
1321 #undef initialize_param_dualvar
1322  
1323 2 SvREADONLY_on(tuba->paramhv);
1324 2 return retrv;
1325 }
1326  
1327 /**
1328 * Initialize our thread-local context
1329 */
1330 #define POPULATE_CXT \
1331 MY_CXT.stash_obj = gv_stashpv(PLJSONSL_CLASS_NAME, GV_ADD); \
1332 MY_CXT.stash_boolean = gv_stashpv(PLJSONSL_BOOLEAN_NAME, GV_ADD); \
1333 MY_CXT.stash_tuba = gv_stashpv(PLTUBA_CLASS_NAME, GV_ADD); \
1334 MY_CXT.quick = NULL;
1335  
1336  
1337 /**
1338 * These two macros arrange for the contents of the result stack to be returned
1339 * to perlspace.
1340 */
1341 #define dRESULT_VARS \
1342 int result_count; \
1343 int result_iter; \
1344 SV *result_sv;
1345  
1346 #define RETURN_RESULTS(pjsn) \
1347 switch(GIMME_V) { \
1348 case G_VOID: \
1349 result_count = 0; \
1350 break; \
1351 case G_SCALAR: \
1352 result_sv = av_shift(pjsn->results); \
1353 if (result_sv == &PL_sv_undef) { \
1354 result_count = 0; \
1355 break; \
1356 } \
1357 XPUSHs(sv_2mortal(result_sv)); \
1358 result_count = 1; \
1359 break; \
1360 case G_ARRAY: \
1361 result_count = av_len(pjsn->results) + 1; \
1362 if (result_count == 0) { \
1363 break; \
1364 } \
1365 EXTEND(SP, result_count); \
1366 for (result_iter = 0; result_iter < result_count; result_iter++) { \
1367 result_sv = av_delete(pjsn->results, result_iter, 0); \
1368 /*already mortal according to av_delete*/ \
1369 PUSHs(result_sv); \
1370 } \
1371 av_clear(pjsn->results); \
1372 break; \
1373 default: \
1374 die("eh? (RETURN_RESULTS)"); \
1375 result_count = 0; \
1376 break; \
1377 }
1378  
1379  
1380  
1381  
1382 MODULE = JSON::SL PACKAGE = JSON::SL PREFIX = PLJSONSL_
1383  
1384 PROTOTYPES: DISABLED
1385  
1386 BOOT:
1387 {
1388 MY_CXT_INIT;
1389 19 POPULATE_CXT;
1390 19 PLJSONSL_ESCTBL_INIT(ESCTBL);
1391 }
1392  
1393 SV *
1394 PLJSONSL_new(SV *pkg, ...)
1395 PREINIT:
1396 PLJSONSL *pjsn;
1397 SV *ptriv, *retrv;
1398 int levels;
1399 dMY_CXT;
1400 CODE:
1401 (void)pkg;
1402 24 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 3 21 24
1403 3 ▶ if (!SvIOK(ST(1))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvIOK(ST(1))) { 0 3 3
1404 0 die("Second argument (if provided) must be numeric");
1405 }
1406 3 levels = SvIV(ST(1));
1407 3 ▶ if (levels < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (levels < 2) { 0 3 3
1408 0 die ("Levels must be at least 2");
1409 }
1410 } else {
1411 21 levels = PLJSONSL_MAX_DEFAULT;
1412 }
1413  
1414 24 Newxz(pjsn, 1, PLJSONSL);
1415 24 pljsonsl_common_initialize(&MY_CXT, pjsn, levels);
1416 24 ptriv = newSViv(PTR2IV(pjsn));
1417 24 retrv = newRV_noinc(ptriv);
1418 24 sv_bless(retrv, MY_CXT.stash_obj);
1419 24 pjsn->buf = newSVpvn("", 0);
1420  
1421 24 jsonsl_enable_all_callbacks(pjsn->jsn);
1422 24 pjsn->jsn->action_callback = initial_callback;
1423 24 pjsn->jsn->error_callback = error_callback;
1424  
1425 24 pjsn->results = newAV();
1426 PLJSONSL_INIT_KSV(pjsn);
1427 24 RETVAL = retrv;
1428  
1429 OUTPUT: RETVAL
1430  
1431  
1432 void
1433 PLJSONSL_set_jsonpointer(PLJSONSL *pjsn, AV *paths)
1434 PPCODE:
1435 4 pljsonsl_set_jsonpointer(pjsn, paths);
1436  
1437 SV *
1438 PLJSONSL_root(PLJSONSL *pjsn)
1439 CODE:
1440 3 ▶ if (pjsn->root) {
Branch: 1 branch
BranchTrueFalseTotal
if (pjsn->root) { 3 0 3
1441 3 RETVAL = newRV_inc(pjsn->root);
1442 } else {
1443 0 RETVAL = &PL_sv_undef;
1444 }
1445 OUTPUT: RETVAL
1446  
1447 void
1448 PLJSONSL__modify_readonly(PLJSONSL *pjsn, SV *ref)
1449 ALIAS:
1450 make_referrent_writeable = 1
1451 make_referrent_readonly = 2
1452 CODE:
1453 3 ▶ if (!SvROK(ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(ref)) { 0 3 3
1454 0 die("Variable is not a reference!");
1455 }
1456 3 ▶ if (ix == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ix == 0) { 0 3 3
1457 0 PLJSONSL_CROAK_USAGE("use make_referrent_writeable or make_referrent_readonly");
1458 3 ▶ } else if (ix == 1) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (ix == 1) { 1 2 3
1459 1 SvREADONLY_off(SvRV(ref));
1460 2 ▶ } else if (ix == 2) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (ix == 2) { 2 0 2
1461 2 SvREADONLY_on(SvRV(ref));
1462 }
1463  
1464 int
1465 PLJSONSL_referrent_is_writeable(PLJSONSL *pjsn, SV *ref)
1466 CODE:
1467 5 ▶ if (!SvROK(ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(ref)) { 0 5 5
1468 0 die("Variable is not a reference!");
1469 }
1470 5 ▶ RETVAL = SvREADONLY(SvRV(ref)) == 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = SvREADONLY(SvRV(ref)) == 0; 0 5 5
1471 OUTPUT: RETVAL
1472  
1473  
1474 void
1475 PLJSONSL_feed(PLJSONSL *pjsn, SV *input)
1476 ALIAS:
1477 incr_parse =1
1478  
1479 PPCODE:
1480 {
1481 dRESULT_VARS;
1482 24 pljsonsl_feed_incr(pjsn, input);
1483 20 ▶ RETURN_RESULTS(pjsn);
Branch: 6 branches
BranchTrueFalseTotal
RETURN_RESULTS(pjsn); 0 5 5
RETURN_RESULTS(pjsn); 0 5 5
RETURN_RESULTS(pjsn); 0 3 3
RETURN_RESULTS(pjsn); 3 0 3
RETURN_RESULTS(pjsn); 0 3 3
RETURN_RESULTS(pjsn); 9 3 12
1484 }
1485  
1486 void
1487 PLJSONSL_fetch(PLJSONSL *pjsn)
1488 PPCODE:
1489 {
1490 dRESULT_VARS;
1491 5 ▶ RETURN_RESULTS(pjsn);
Branch: 6 branches
BranchTrueFalseTotal
RETURN_RESULTS(pjsn); 0 1 1
RETURN_RESULTS(pjsn); 0 1 1
RETURN_RESULTS(pjsn); 0 1 1
RETURN_RESULTS(pjsn); 1 0 1
RETURN_RESULTS(pjsn); 0 1 1
RETURN_RESULTS(pjsn); 3 1 4
1492 }
1493  
1494 int
1495 PLJSONSL__escape_table_chr(PLJSONSL *pjsn, U8 chrc, ...)
1496 CODE:
1497 2 ▶ if (chrc > 0x7f) {
Branch: 1 branch
BranchTrueFalseTotal
if (chrc > 0x7f) { 0 2 2
1498 0 warn("Attempted to set non-ASCII escape preference");
1499 0 RETVAL = -1;
1500 } else {
1501 2 RETVAL = pjsn->escape_table[chrc];
1502 2 ▶ if (items == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 3) { 2 0 2
1503 2 pjsn->escape_table[chrc] = SvIV(ST(2));
1504 }
1505 }
1506 OUTPUT: RETVAL
1507  
1508 void
1509 PLJSONSL_reset(PLJSONSL *pjsn)
1510 CODE:
1511 2 ▶ REFDEC_FIELD(pjsn, root);
Branch: 1 branch
BranchTrueFalseTotal
REFDEC_FIELD(pjsn, root); 0 2 2
1512  
1513 2 ▶ if (pjsn->results) {
Branch: 1 branch
BranchTrueFalseTotal
if (pjsn->results) { 2 0 2
1514 2 av_clear(pjsn->results);
1515 }
1516 2 ▶ if (pjsn->buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (pjsn->buf) { 2 0 2
1517 2 SvCUR_set(pjsn->buf, 0);
1518 }
1519  
1520 2 jsonsl_reset(pjsn->jsn);
1521 2 pjsn->pos_min_valid = 0;
1522 2 pjsn->keep_pos = 0;
1523 2 pjsn->curhk = NULL;
1524 2 pjsn->jsn->action_callback_PUSH = initial_callback;
1525  
1526 SV*
1527 PLJSONSL_root_callback(PLJSONSL *pjsn, SV *callback)
1528 CODE:
1529 4 RETVAL = pjsn->options.root_callback;
1530 4 ▶ if (RETVAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL) { 1 3 4
1531 1 SvREFCNT_inc(RETVAL);
1532 } else {
1533 3 RETVAL = &PL_sv_undef;
1534 }
1535  
1536 4 ▶ if (SvTYPE(callback) == SVt_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(callback) == SVt_NULL) { 1 3 4
1537 1 ▶ if (pjsn->options.root_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (pjsn->options.root_callback) { 1 0 1
1538 1 SvREFCNT_dec(pjsn->options.root_callback);
1539 1 pjsn->options.root_callback = NULL;
1540 }
1541 } else {
1542 3 ▶ if (SvTYPE(callback) != SVt_RV ||
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(callback) != SVt_RV || 2 1 3
1543 2 ▶ SvTYPE(SvRV(callback)) != SVt_PVCV) {
Branch: 1 branch
BranchTrueFalseTotal
SvTYPE(SvRV(callback)) != SVt_PVCV) { 0 2 2
1544 1 die("Second argument must be undef or a CODE ref");
1545 }
1546 2 ▶ if (pjsn->options.root_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (pjsn->options.root_callback) { 0 2 2
1547 0 SvREFCNT_dec(pjsn->options.root_callback);
1548 }
1549 2 pjsn->options.root_callback = newRV_inc(SvRV(callback));
1550 }
1551  
1552 OUTPUT: RETVAL
1553  
1554 void
1555 PLJSONSL_DESTROY(PLJSONSL *pjsn)
1556 PREINIT:
1557 int ii;
1558  
1559 CODE:
1560 24 ▶ if (pjsn->priv_global.is_global == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (pjsn->priv_global.is_global == 0) { 24 0 24
1561 24 ▶ REFDEC_FIELD(pjsn, root);
Branch: 1 branch
BranchTrueFalseTotal
REFDEC_FIELD(pjsn, root); 14 10 24
1562 24 ▶ REFDEC_FIELD(pjsn, results);
Branch: 1 branch
BranchTrueFalseTotal
REFDEC_FIELD(pjsn, results); 24 0 24
1563 24 ▶ REFDEC_FIELD(pjsn, buf);
Branch: 1 branch
BranchTrueFalseTotal
REFDEC_FIELD(pjsn, buf); 24 0 24
1564 24 ▶ REFDEC_FIELD(pjsn, options.root_callback);
Branch: 1 branch
BranchTrueFalseTotal
REFDEC_FIELD(pjsn, options.root_callback); 1 23 24
1565 } /* else, it's a mortal and shouldn't be freed */
1566 24 jsonsl_jpr_match_state_cleanup(pjsn->jsn);
1567 24 ▶ if (pjsn->jprs) {
Branch: 1 branch
BranchTrueFalseTotal
if (pjsn->jprs) { 4 20 24
1568 8 ▶ for ( ii = 0; ii < pjsn->njprs; ii++) {
Branch: 1 branch
BranchTrueFalseTotal
for ( ii = 0; ii < pjsn->njprs; ii++) { 4 4 8
1569 4 ▶ if (pjsn->jprs[ii] == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (pjsn->jprs[ii] == NULL) { 0 4 4
1570 0 break;
1571 }
1572 4 jsonsl_jpr_destroy(pjsn->jprs[ii]);
1573 }
1574 4 Safefree(pjsn->jprs);
1575 4 pjsn->jprs = NULL;
1576 }
1577 24 ▶ if (pjsn->jsn) {
Branch: 1 branch
BranchTrueFalseTotal
if (pjsn->jsn) { 24 0 24
1578 24 jsonsl_destroy(pjsn->jsn);
1579 24 pjsn->jsn = NULL;
1580 }
1581 PLJSONSL_DESTROY_KSV(pjsn);
1582 24 Safefree(pjsn);
1583  
1584 void
1585 PLJSONSL_decode_json(SV *input)
1586 PREINIT:
1587 PLJSONSL* pjsn;
1588 dRESULT_VARS;
1589  
1590 PPCODE:
1591 32 pjsn = pljsonsl_get_and_initialize_global(aTHX);
1592 32 pljsonsl_feed_oneshot(pjsn, input);
1593  
1594 30 pjsn->curhk = NULL;
1595 30 pjsn->keep_pos = 0;
1596 30 pjsn->pos_min_valid = 0;
1597 30 pjsn->jsn->action_callback_PUSH = initial_callback;
1598  
1599 30 ▶ RETURN_RESULTS(pjsn);
Branch: 6 branches
BranchTrueFalseTotal
RETURN_RESULTS(pjsn); 0 29 29
RETURN_RESULTS(pjsn); 0 29 29
RETURN_RESULTS(pjsn); 0 0 0
RETURN_RESULTS(pjsn); 0 0 0
RETURN_RESULTS(pjsn); 0 0 0
RETURN_RESULTS(pjsn); 0 0 0
1600 30 ▶ if (result_count == 0 && av_len(pjsn->results) == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if (result_count == 0 && av_len(pjsn->results) == -1) { 1 29 30
if (result_count == 0 && av_len(pjsn->results) == -1) { 1 0 1
1601 1 die("Incomplete JSON string?");
1602 }
1603  
1604 SV *
1605 PLJSONSL_unescape_json_string(SV *input)
1606 PREINIT:
1607 size_t origlen, newlen;
1608 3 ▶ SV *retsv = NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *retsv = NULL; 0 3 3
1609 char *errpos;
1610 jsonsl_error_t err;
1611 jsonsl_special_t flags;
1612  
1613 CODE:
1614 3 ▶ if (!SvPOK(input)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvPOK(input)) { 0 3 3
1615 0 die("Input is not a valid string");
1616 }
1617 3 origlen = SvCUR(input);
1618 3 ▶ if (origlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (origlen) { 3 0 3
1619 3 retsv = newSV(origlen);
1620 3 newlen = jsonsl_util_unescape_ex(SvPVX_const(input), SvPVX(retsv),
1621 3 SvCUR(input), ESCTBL, &flags,
1622 &err, (const char**)&errpos);
1623 3 ▶ if (newlen == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (newlen == 0) { 1 2 3
1624 1 SvREFCNT_dec(retsv);
1625 1 die("Could not unescape: %s at pos %lu ('%c'..)",
1626 jsonsl_strerror(err),
1627 errpos - SvPVX_const(input),
1628 *errpos
1629 );
1630 }
1631  
1632 2 SvCUR_set(retsv, newlen);
1633 2 SvPOK_only(retsv);
1634 2 ▶ if (SvUTF8(input) || (flags & JSONSL_SPECIALf_NONASCII)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvUTF8(input) || (flags & JSONSL_SPECIALf_NONASCII)) { 2 0 2
if (SvUTF8(input) || (flags & JSONSL_SPECIALf_NONASCII)) { 1 1 2
1635 1 SvUTF8_on(retsv);
1636 }
1637 } else {
1638 0 retsv = &PL_sv_undef;
1639 }
1640 2 RETVAL = retsv;
1641 OUTPUT: RETVAL
1642  
1643  
1644  
1645 void
1646 PLJSONSL_CLONE(PLJSONSL *pjsn)
1647 CODE:
1648 {
1649 MY_CXT_CLONE;
1650 0 POPULATE_CXT;
1651 }
1652  
1653 MODULE = JSON::SL PACKAGE = JSON::SL::Tuba PREFIX = PLTUBA_
1654  
1655 SV *
1656 PLTUBA__initialize(const char *pkg)
1657 CODE:
1658 2 RETVAL = pltuba_initialize_THX(aTHX_ pkg);
1659 OUTPUT: RETVAL
1660  
1661 void
1662 PLTUBA_DESTROY(PLTUBA* tuba)
1663 CODE:
1664 2 jsonsl_destroy(tuba->jsn);
1665 2 tuba->jsn = NULL;
1666  
1667 2 ▶ REFDEC_FIELD(tuba, accum);
Branch: 1 branch
BranchTrueFalseTotal
REFDEC_FIELD(tuba, accum); 0 2 2
1668 2 ▶ REFDEC_FIELD(tuba, kaccum);
Branch: 1 branch
BranchTrueFalseTotal
REFDEC_FIELD(tuba, kaccum); 0 2 2
1669 2 ▶ REFDEC_FIELD(tuba, selfrv);
Branch: 1 branch
BranchTrueFalseTotal
REFDEC_FIELD(tuba, selfrv); 2 0 2
1670 #define X(kname) \
1671 PLTUBA_RESET_PARAMFIELD(tuba, kname); \
1672 REFDEC_FIELD(tuba, p_ents.pe_##kname.sv);
1673 2 ▶ PLTUBA_XPARAMS;
Branch: 6 branches
BranchTrueFalseTotal
PLTUBA_XPARAMS; 0 2 2
PLTUBA_XPARAMS; 0 2 2
PLTUBA_XPARAMS; 2 0 2
PLTUBA_XPARAMS; 2 0 2
PLTUBA_XPARAMS; 0 2 2
PLTUBA_XPARAMS; 2 0 2
1674 #undef X
1675 2 ▶ REFDEC_FIELD(tuba, paramhvrv);
Branch: 1 branch
BranchTrueFalseTotal
REFDEC_FIELD(tuba, paramhvrv); 2 0 2
1676 /* Implicit that the hash has been decrementas as well.
1677 * Don't do another dec
1678 */
1679 2 tuba->paramhv = NULL;
1680 2 pltuba_invalidate_gvs_THX(aTHX_ tuba);
1681 2 Safefree(tuba);
1682  
1683 int
1684 PLTUBA__ax_opt(PLTUBA *tuba, int mode, ...)
1685 CODE:
1686 20 RETVAL = tuba->accum_options[mode & 0xff];
1687 20 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 15 5 20
1688 15 tuba->accum_options[mode & 0xff] = SvIV(ST(2));
1689 }
1690 OUTPUT: RETVAL
1691  
1692 int
1693 PLTUBA_accum_kv(PLTUBA *tuba, ...)
1694 CODE:
1695 4 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 0 4 4
1696 0 die("accum_kv(..boolean)");
1697 }
1698 4 RETVAL = tuba->options.accum_kv;
1699 4 ▶ if (items == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 2) { 3 1 4
1700 3 int newval = SvIV(ST(1));
1701 3 ▶ if (newval) {
Branch: 1 branch
BranchTrueFalseTotal
if (newval) { 3 0 3
1702 3 tuba->accum_options['#'] = 1;
1703 }
1704 3 tuba->options.accum_kv = newval;
1705 }
1706 OUTPUT: RETVAL
1707  
1708  
1709 void
1710 PLTUBA__parse(PLTUBA* tuba, SV *input)
1711 CODE:
1712 2 pltuba_feed(tuba, input);
1713  
1714  
1715 INCLUDE: srcout/option_accessors.xs
« jsonxs_inline.h ↑ Summary