SL.xs

filter: Statementstmt 89.0% 569 / 639 Branchbran 64.3% 328 / 510 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 78.0% 897 / 1149 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 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
694  
695 23 pjsn->pos_min_valid = pjsn->jsn->pos - cur_len;
696 23 ▶ if (SvUTF8(input)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(input)) { 1 22 23
697 1 pjsn->options.utf8 = 1;
698 }
699 23 sv_catpvn(pjsn->buf, SvPVX_const(input), SvCUR(input));
700 23 jsonsl_feed(pjsn->jsn,
701 23 SvPVX_const(pjsn->buf) + (SvCUR(pjsn->buf)-SvCUR(input)),
702 23 SvCUR(input));
703 /**
704 * Callbacks may detect the beginning of a string, in which case
705 * we need to ensure the continuity of the string. In this case
706 * pos_keep is set to the position of the input stream (not the SV *input,
707 * but rather jsn->pos) from which we should begin buffering data.
708 *
709 * Now we might need to chop. The amount of bytes to chop is the
710 * difference between start_pos and the keep_pos
711 * variable (if any)
712 */
713 11 ▶ if (pjsn->keep_pos == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (pjsn->keep_pos == 0) { 11 0 11
714 11 SvCUR_set(pjsn->buf, 0);
715 0 ▶ } else if (pjsn->keep_pos > start_pos) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (pjsn->keep_pos > start_pos) { 0 0 0
716 0 sv_chop(pjsn->buf, SvPVX_const(pjsn->buf) + (pjsn->keep_pos - start_pos));
717 }
718 11 }
719  
720 static PLJSONSL*
721 32 pljsonsl_get_and_initialize_global(pTHX)
722 {
723 dMY_CXT;
724 PLJSONSL *pjsn;
725 32 ▶ if (MY_CXT.quick == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.quick == NULL) { 7 25 32
726 7 Newxz(pjsn, 1, PLJSONSL);
727 7 pljsonsl_common_initialize(&MY_CXT, pjsn, PLJSONSL_MAX_DEFAULT-1);
728 7 pjsn->priv_global.is_global = 1;
729 PLJSONSL_INIT_KSV(pjsn);
730 7 MY_CXT.quick = pjsn;
731 }
732  
733 32 pjsn = MY_CXT.quick;
734 32 jsonsl_reset(pjsn->jsn);
735 32 jsonsl_enable_all_callbacks(pjsn->jsn);
736 32 pjsn->jsn->error_callback = error_callback;
737 32 pjsn->jsn->action_callback_PUSH = initial_callback;
738 32 pjsn->results = (AV*)sv_2mortal((SV*)newAV());
739 32 return pjsn;
740 }
741  
742 #define pljsonsl_feed_oneshot(pjsn,str) pljsonsl_feed_oneshot_THX(aTHX_ pjsn,str)
743 static void
744 32 pljsonsl_feed_oneshot_THX(pTHX_ PLJSONSL* pjsn, SV *input)
745 {
746 32 ▶ if (!SvPOK(input)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvPOK(input)) { 0 32 32
747 0 die("Input is not a string");
748 }
749  
750 32 ▶ if (SvUTF8(input)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(input)) { 0 32 32
751 0 pjsn->options.utf8 = 1;
752 }
753 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
754 32 pjsn->buf = input;
755 32 jsonsl_feed(pjsn->jsn, SvPVX_const(input), SvCUR(input));
756 30 pjsn->buf = NULL;
757 30 pjsn->options.utf8 = 0;
758 /* the current root is never in the result stack..
759 * so mortalizing it won't hurt anyone.
760 */
761 30 }
762  
763 /**
764 * Takes an array ref (or list?) of JSONPointer strings and converts
765 * them to JPR objects. Dies on error
766 */
767 #define pljsonsl_set_jsonpointer(pjsn,jprstr) \
768 pljsonsl_set_jsonpointer_THX(aTHX_ pjsn,jprstr)
769 static void
770 4 pljsonsl_set_jsonpointer_THX(pTHX_ PLJSONSL *pjsn, AV *paths)
771 {
772 jsonsl_jpr_t *jprs;
773 jsonsl_error_t err;
774 int ii;
775 4 int max = av_len(paths)+1;
776 const char *diestr, *pathstr;
777  
778 4 ▶ if (!max) {
Branch: 1 branch
BranchTrueFalseTotal
if (!max) { 0 4 4
779 0 die("No paths given!");
780 }
781  
782 4 Newxz(jprs, max, jsonsl_jpr_t);
783  
784 8 ▶ for (ii = 0; ii < max; ii++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ii = 0; ii < max; ii++) { 4 4 8
785 4 SV **tmpsv = av_fetch(paths, ii, 0);
786 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
787 0 diestr = "Found empty path";
788 0 goto GT_ERR;
789 }
790 4 jprs[ii] = jsonsl_jpr_new(SvPVX_const(*tmpsv), &err);
791 4 ▶ if (jprs[ii] == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (jprs[ii] == NULL) { 0 4 4
792 0 pathstr = SvPVX_const(*tmpsv);
793 0 goto GT_ERR;
794 }
795 }
796  
797 4 jsonsl_jpr_match_state_init(pjsn->jsn, jprs, max);
798 4 pjsn->jprs = jprs;
799 4 pjsn->njprs = max;
800 4 return;
801  
802 0 GT_ERR:
803 0 ▶ for (ii = 0; ii < max; ii++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ii = 0; ii < max; ii++) { 0 0 0
804 0 ▶ if (jprs[ii] == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (jprs[ii] == NULL) { 0 0 0
805 0 break;
806 }
807 0 jsonsl_jpr_destroy(jprs[ii]);
808 }
809 0 Safefree(jprs);
810 0 ▶ if (pathstr) {
Branch: 1 branch
BranchTrueFalseTotal
if (pathstr) { 0 0 0
811 0 die("Couldn't convert %s to jsonpointer: %s", pathstr, jsonsl_strerror(err));
812 } else {
813 0 die(diestr);
814 }
815 }
816  
817  
818  
819  
820  
821 /**
822 * JSON::SL::Tuba functions.
823 * In case you haven't wondered already, 'Tuba' is a play on 'SAX'.
824 */
825  
826  
827 /**
828 * This is our quick version of MRO caching. Maybe I'll swap this out
829 * for something which already exists (as I get the feeling I've reinveted
830 * the wheel here.
831 * namep is populated with the handler name, gvp is a pointer to the GV**
832 * - or an offset into the PLTUBA's methgv structure.
833 */
834 static void
835 55 pltuba_get_method_info(PLTUBA *tuba,
836 jsonsl_action_t action,
837 pltuba_callback_type cbtype,
838 GV ***gvpp,
839 const char **namep)
840 {
841 55 GV **methgvp = NULL;
842 55 const char *methname = NULL;
843 55 cbtype &= 0x7f;
844  
845 55 ▶ if (tuba->options.cb_unified) {
Branch: 1 branch
BranchTrueFalseTotal
if (tuba->options.cb_unified) { 0 55 55
846 0 methname = "on_any";
847 0 methgvp = &tuba->methgv.on_any;
848 0 goto GT_RETASGN;
849 }
850 #define PLTUBA_METH_GETMETH
851 #include "srcout/tuba_dispatch_getmeth.h"
852 #undef PLTUBA_METH_GETMETH
853 55 GT_RETASGN:
854 55 ▶ if (gvpp) {
Branch: 1 branch
BranchTrueFalseTotal
if (gvpp) { 55 0 55
855 55 *gvpp = methgvp;
856 }
857 55 ▶ if (namep) {
Branch: 1 branch
BranchTrueFalseTotal
if (namep) { 55 0 55
858 55 *namep = methname;
859 }
860 55 }
861  
862 static void
863 4 pltuba_invalidate_gvs_THX(pTHX_ PLTUBA *tuba)
864 {
865  
866 #define X(action,type) \
867 REFDEC_FIELD(tuba, methgv.action## _ ##type)
868  
869 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
870 #undef X
871 4 }
872  
873 /**
874 * Maps a 'jsonsl' type to a tuba callback type.
875 */
876 static pltuba_callback_type
877 54 convert_to_tuba_cbt(struct jsonsl_state_st *state)
878 {
879 54 ▶ if (state->type != JSONSL_T_SPECIAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->type != JSONSL_T_SPECIAL) { 44 10 54
880 44 return state->type;
881 }
882 10 ▶ if (state->special_flags & JSONSL_SPECIALf_BOOLEAN) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->special_flags & JSONSL_SPECIALf_BOOLEAN) { 4 6 10
883 4 return PLTUBA_CALLBACK_BOOLEAN;
884 6 ▶ } else if (state->special_flags & JSONSL_SPECIALf_NUMERIC) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (state->special_flags & JSONSL_SPECIALf_NUMERIC) { 4 2 6
885 4 return PLTUBA_CALLBACK_NUMBER;
886 2 ▶ } else if (state->special_flags == JSONSL_SPECIALf_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (state->special_flags == JSONSL_SPECIALf_NULL) { 2 0 2
887 2 return PLTUBA_CALLBACK_NULL;
888 }
889 0 die("wtf?");
890 return 0;
891 }
892  
893 /**
894 * This function invokes the selected callback (if it exists).
895 */
896 #define pltuba_invoke_callback(tb,a,cbt,sv) \
897 pltuba_invoke_callback_THX(aTHX_ tb,a,cbt,sv)
898 static void
899 25 pltuba_invoke_callback_THX(pTHX_ PLTUBA *tuba,
900 int action,
901 pltuba_callback_type cbtype,
902 SV *mextrasv)
903 {
904 25 dSP;
905 25 GV **methp = NULL;
906 25 GV *meth = NULL;
907 25 const char *meth_name = NULL;
908 25 int effective_type = cbtype;
909 25 int effective_action = action;
910 25 int stop_mro = 0;
911 /**
912 * If we are in a pop mode of a callback with the accumulator flag set,
913 * then we provide the data in the SV as the argument (maybe with some
914 * conversion into an appropriate object), otherwise, we just signal as
915 * normal.
916 */
917 25 cbtype &= 0x7f;
918  
919 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
920 13 effective_action = PLTUBA_ACTION_ON;
921 13 pltuba_get_method_info(tuba, PLTUBA_ACTION_ON, cbtype, &methp, &meth_name);
922 assert(mextrasv == NULL);
923 13 mextrasv = tuba->accum;
924 13 tuba->accum = NULL;
925 } else {
926 12 pltuba_get_method_info(tuba, action, cbtype, &methp, &meth_name);
927 }
928  
929 25 ▶ if (meth_name == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (meth_name == NULL) { 0 25 25
930 0 die("Can't find method name. Action=%c, Type=%c", action, cbtype);
931 }
932  
933 25 ▶ if (!mextrasv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!mextrasv) { 12 13 25
934 12 mextrasv = &PL_sv_undef;
935 } else {
936 13 sv_2mortal(mextrasv);
937 }
938  
939 assert(methp);
940  
941 25 ▶ if (tuba->last_stash != SvSTASH(SvRV(tuba->selfrv))) {
Branch: 1 branch
BranchTrueFalseTotal
if (tuba->last_stash != SvSTASH(SvRV(tuba->selfrv))) { 2 23 25
942 2 pltuba_invalidate_gvs_THX(aTHX_ tuba);
943 2 tuba->last_stash = SvSTASH(SvRV(tuba->selfrv));
944 }
945  
946 do {
947 50 ▶ if (*methp == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (*methp == NULL) { 31 19 50
948 31 meth = gv_fetchmethod_autoload(SvSTASH(SvRV(tuba->selfrv)), meth_name, 1);
949 31 ▶ if (meth && GvCV(meth)) {
Branch: 2 branches
BranchTrueFalseTotal
if (meth && GvCV(meth)) { 1 30 31
if (meth && GvCV(meth)) { 1 0 1
950 1 ▶ if (tuba->options.no_cache_mro == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (tuba->options.no_cache_mro == 0) { 1 0 1
951 1 *methp = meth;
952 1 SvREFCNT_inc(meth);
953 }
954 1 break;
955 } /* else */
956 30 pltuba_get_method_info(tuba, PLTUBA_ACTION_ON,
957 PLTUBA_CALLBACK_ANY, &methp, &meth_name);
958 assert(methp && meth_name);
959 30 stop_mro++;
960 } else {
961 19 meth = *methp;
962 19 break;
963 }
964 30 ▶ } while (stop_mro < 2);
Branch: 1 branch
BranchTrueFalseTotal
} while (stop_mro < 2); 25 5 30
965  
966 25 PLTUBA_SET_PARAMFIELDS_dv(tuba, Mode, effective_action);
967 25 PLTUBA_SET_PARAMFIELDS_dv(tuba, Type, effective_type);
968  
969 /**
970 * We still want a SAVETMPS/FREETMPS pair active before we decide
971 * to call a function or not, as the contents mextrasv and possibly
972 * some of the hash values are mortalized.
973 */
974 25 ENTER; SAVETMPS;
975 25 ▶ if (meth && GvCV(meth)) {
Branch: 2 branches
BranchTrueFalseTotal
if (meth && GvCV(meth)) { 20 5 25
if (meth && GvCV(meth)) { 20 0 20
976 20 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 20 20
977 20 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 20 20
978 20 PUSHs(tuba->selfrv);
979 20 PUSHs(tuba->paramhvrv);
980 20 ▶ if (mextrasv != &PL_sv_undef) {
Branch: 1 branch
BranchTrueFalseTotal
if (mextrasv != &PL_sv_undef) { 11 9 20
981 11 ▶ XPUSHs(mextrasv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(mextrasv); 0 11 11
982 }
983 20 PUTBACK;
984 20 call_sv((SV*)GvCV(meth), G_DISCARD);
985 } else {
986 5 ▶ if (!tuba->options.allow_unhandled) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tuba->options.allow_unhandled) { 0 5 5
987 0 die("Tuba: Cannot find handler for mode 0x%02x action 0x%02x",
988 effective_action, effective_type);
989 }
990 }
991 25 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 25 25
992 25 }
993  
994 /**
995 * Flush characters between the invocation of the last callback
996 * and the current one. the until argument is the end position (inclusive)
997 * at which we should stop submitting 'character' data.
998 */
999 #define pltuba_flush_characters(tb,end) \
1000 pltuba_flush_characters_THX(aTHX_ tb,end)
1001 static void
1002 23 pltuba_flush_characters_THX(pTHX_ PLTUBA *tuba, size_t until)
1003 {
1004 STRLEN toFlush;
1005 const char *buf;
1006 SV *chunksv;
1007  
1008 23 ▶ if (!tuba->keep_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tuba->keep_pos) { 0 23 23
1009 0 return;
1010 }
1011  
1012 23 toFlush = (until - tuba->keep_pos);
1013 23 ▶ if (toFlush == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (toFlush == 0) { 0 23 23
1014 0 return;
1015 }
1016 23 buf = GET_STATE_BUFFER(tuba, tuba->keep_pos);
1017  
1018 23 ▶ if (tuba->shift_quote) {
Branch: 1 branch
BranchTrueFalseTotal
if (tuba->shift_quote) { 18 5 23
1019 18 buf++;
1020 18 toFlush--;
1021 }
1022  
1023 23 tuba->keep_pos = 0;
1024 23 tuba->shift_quote = 0;
1025  
1026 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
1027 /* if we have no data and the count was not artificially decremented, then
1028 * don't invoke the callback
1029 */
1030 0 return;
1031 }
1032  
1033 /* if accumulator mode is on, don't send the data right away.
1034 * buffer it instead */
1035 23 ▶ if (tuba->accum) {
Branch: 1 branch
BranchTrueFalseTotal
if (tuba->accum) { 23 0 23
1036 23 sv_catpvn(tuba->accum, buf, toFlush);
1037 23 return;
1038 } /* else, no accum for this state */
1039  
1040 0 chunksv = newSVpvn(buf, toFlush);
1041 0 pltuba_invoke_callback(tuba,
1042 PLTUBA_ACTION_ON,
1043 PLTUBA_CALLBACK_DATA,
1044 chunksv);
1045 /**
1046 * SV has been mortalized by the invoke_callback function
1047 */
1048 }
1049 /**
1050 * Push callback. This is easy because we never actually do any character
1051 * data here.
1052 */
1053 static void
1054 27 pltuba_jsonsl_push_callback(jsonsl_t jsn,
1055 jsonsl_action_t action,
1056 struct jsonsl_state_st *state,
1057 const char *at)
1058 {
1059 27 PLTUBA *tuba = (PLTUBA*)jsn->data;
1060 PLJSONSL_dTHX(tuba);
1061 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
1062 27 pltuba_callback_type cbt = convert_to_tuba_cbt(state);
1063 27 ▶ if (state->level == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->level == 1) { 2 25 27
1064 2 pltuba_invoke_callback(tuba, action, PLTUBA_CALLBACK_JSON, NULL);
1065 } else {
1066 assert(parent);
1067 25 ▶ if (parent->type == JSONSL_T_LIST) {
Branch: 1 branch
BranchTrueFalseTotal
if (parent->type == JSONSL_T_LIST) { 5 20 25
1068 5 PLTUBA_SET_PARAMFIELDS_iv(tuba, Index, parent->nelem-1);
1069 } else {
1070 20 PLTUBA_RESET_PARAMFIELD(tuba, Index);
1071 }
1072 }
1073  
1074 27 ▶ if (tuba->accum_options[cbt & 0x7f]) {
Branch: 1 branch
BranchTrueFalseTotal
if (tuba->accum_options[cbt & 0x7f]) { 23 4 27
1075 assert(tuba->accum == NULL);
1076 /* accum is only ever valid for atomic types */
1077 23 tuba->accum = newSVpvn("", 0);
1078 } else {
1079 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
1080 2 sv_2mortal(tuba->kaccum);
1081 2 tuba->kaccum = NULL;
1082 2 pltuba_invoke_callback(tuba, action, cbt, NULL);
1083 2 PLTUBA_RESET_PARAMFIELD(tuba, Key);
1084 } else {
1085 2 pltuba_invoke_callback(tuba, action, cbt, NULL);
1086 }
1087 }
1088  
1089 /* This is a different branch and must get executed regardless
1090 * of whether we invoke a callback or use the accumulator */
1091 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
1092 23 tuba->keep_pos = state->pos_begin;
1093 23 ▶ if (state->type & JSONSL_Tf_STRINGY) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->type & JSONSL_Tf_STRINGY) { 18 5 23
1094 18 tuba->shift_quote = 1;
1095 }
1096 } else {
1097 4 tuba->keep_pos = 0;
1098 }
1099 27 }
1100  
1101 /**
1102 * If we're special, then convert all weird stuff to their
1103 * proper perly form. Simple plain integers are not weird and
1104 * can be stringified on demand.
1105 * This is akin to JSON::SL's process_string and process_special
1106 * functions.
1107 */
1108 #define pltuba_process_accum(tuba, state) \
1109 pltuba_process_accum_THX(aTHX_ tuba, state)
1110 static void
1111 23 pltuba_process_accum_THX(pTHX_
1112 PLTUBA *tuba,
1113 struct jsonsl_state_st *state)
1114 {
1115 23 ▶ if (state->type == JSONSL_T_SPECIAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->type == JSONSL_T_SPECIAL) { 5 18 23
1116 SV *newsv;
1117  
1118 5 ▶ if ( (state->special_flags & JSONSL_SPECIALf_NUMERIC) &&
Branch: 1 branch
BranchTrueFalseTotal
if ( (state->special_flags & JSONSL_SPECIALf_NUMERIC) && 2 3 5
1119 2 ▶ (state->special_flags & JSONSL_SPECIALf_NUMNOINT) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
(state->special_flags & JSONSL_SPECIALf_NUMNOINT) == 0) { 0 2 2
1120 0 goto GT_NONEWSV;
1121  
1122 5 ▶ } else if (state->special_flags & JSONSL_SPECIALf_NUMNOINT) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (state->special_flags & JSONSL_SPECIALf_NUMNOINT) { 2 3 5
1123 2 newsv = pljsonsl_common_mknumeric(state,
1124 SvPVX_const(tuba->accum),
1125 state->pos_cur - state->pos_begin);
1126 3 ▶ } else if (state->special_flags & JSONSL_SPECIALf_BOOLEAN) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (state->special_flags & JSONSL_SPECIALf_BOOLEAN) { 2 1 3
1127 2 newsv = pljsonsl_common_mkboolean((PLJSONSL*)tuba,
1128 state->special_flags);
1129 } else {
1130 1 newsv = &PL_sv_undef;
1131 }
1132 5 SvREFCNT_dec(tuba->accum);
1133 5 tuba->accum = newsv;
1134 5 GT_NONEWSV:
1135 ;
1136 } else {
1137 18 ▶ if (tuba->options.utf8) {
Branch: 1 branch
BranchTrueFalseTotal
if (tuba->options.utf8) { 0 18 18
1138 0 SvUTF8_on(tuba->accum);
1139 }
1140 18 ▶ if (state->nescapes) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->nescapes) { 2 16 18
1141 jsonsl_error_t err;
1142 jsonsl_special_t flags;
1143 size_t newlen;
1144 2 newlen = jsonsl_util_unescape_ex(SvPVX_const(tuba->accum),
1145 2 SvPVX(tuba->accum),
1146 2 SvCUR(tuba->accum),
1147 2 tuba->escape_table,
1148 &flags,
1149 &err,
1150 NULL);
1151 2 ▶ if (newlen == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (newlen == 0) { 0 2 2
1152 0 die("Could not unescape string: %s", jsonsl_strerror(err));
1153 }
1154 2 SvCUR_set(tuba->accum, newlen);
1155 2 ▶ if (flags & JSONSL_SPECIALf_NONASCII) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & JSONSL_SPECIALf_NONASCII) { 0 2 2
1156 0 SvUTF8_on(tuba->accum);
1157 }
1158 }
1159 }
1160 23 }
1161  
1162 static void
1163 27 pltuba_jsonsl_pop_callback(jsonsl_t jsn,
1164 jsonsl_action_t action,
1165 struct jsonsl_state_st *state,
1166 const char *at)
1167 {
1168 27 PLTUBA *tuba = (PLTUBA*)jsn->data;
1169 PLJSONSL_dTHX(tuba);
1170 27 pltuba_callback_type cbt = convert_to_tuba_cbt(state);
1171  
1172 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
1173 /* Special handling for character crap.. */
1174 23 pltuba_flush_characters(tuba, state->pos_cur);
1175  
1176  
1177 23 ▶ if (tuba->accum) {
Branch: 1 branch
BranchTrueFalseTotal
if (tuba->accum) { 23 0 23
1178 23 pltuba_process_accum(tuba, state);
1179 } else {
1180 0 ▶ if (state->nescapes) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->nescapes) { 0 0 0
1181 0 PLTUBA_SET_PARAMFIELDS_sv(tuba, Escaped, &PL_sv_yes);
1182 }
1183 }
1184  
1185 23 ▶ if (state->type == JSONSL_T_HKEY &&
Branch: 1 branch
BranchTrueFalseTotal
if (state->type == JSONSL_T_HKEY && 10 13 23
1186 10 ▶ tuba->options.accum_kv) {
Branch: 1 branch
BranchTrueFalseTotal
tuba->options.accum_kv) { 10 0 10
1187 /**
1188 * If we are accumulating the key then don't flush characters under
1189 * any circumstances. Just swap over the accumulator buffer
1190 */
1191 assert(tuba->accum);
1192 10 tuba->kaccum = tuba->accum;
1193 10 tuba->accum = NULL;
1194 10 tuba->keep_pos = 0;
1195 10 PLTUBA_SET_PARAMFIELDS_sv(tuba, Key, tuba->kaccum);
1196 10 return;
1197 }
1198 }
1199  
1200 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
1201 8 sv_2mortal(tuba->kaccum);
1202 8 tuba->kaccum = NULL;
1203 }
1204  
1205 17 pltuba_invoke_callback(tuba, action, cbt, NULL);
1206  
1207 /**
1208 * Clear all fields
1209 */
1210 #define X(kname) \
1211 PLTUBA_RESET_PARAMFIELD(tuba, kname);
1212 17 PLTUBA_XPARAMS;
1213 #undef X
1214  
1215 17 ▶ if (state->level == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->level == 1) { 2 15 17
1216 2 pltuba_invoke_callback(tuba, action, PLTUBA_CALLBACK_JSON, NULL);
1217 }
1218 17 tuba->keep_pos = 0;
1219 }
1220  
1221 static int
1222 0 pltuba_jsonsl_error_callback(jsonsl_t jsn,
1223 jsonsl_error_t error,
1224 struct jsonsl_state_st *state,
1225 char *at)
1226 {
1227 /**
1228 * This needs special handling, as we will be receiving a return
1229 * value from Perl for this..
1230 */
1231 0 die ("Got error: %s", jsonsl_strerror(error));
1232 return 0;
1233 }
1234  
1235 #define pltuba_feed(tb,str) pltuba_feed_THX(aTHX_ tb,str)
1236 static void
1237 2 pltuba_feed_THX(pTHX_ PLTUBA *tuba, SV *input)
1238 {
1239 2 ▶ if (!SvPOK(input)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvPOK(input)) { 0 2 2
1240 0 die("Input is not string!");
1241 }
1242 2 tuba->buf = input;
1243 2 tuba->pos_min_valid = tuba->jsn->pos;
1244  
1245 2 SvREADONLY_on(input);
1246 2 jsonsl_feed(tuba->jsn, SvPVX_const(input), SvCUR(input));
1247 2 ▶ if (tuba->keep_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (tuba->keep_pos) { 0 2 2
1248 0 pltuba_flush_characters(tuba, tuba->jsn->pos);
1249 0 tuba->keep_pos = tuba->jsn->pos;
1250 }
1251 2 SvREADONLY_off(input);
1252 2 }
1253  
1254 static SV *
1255 2 pltuba_initialize_THX(pTHX_ const char *pkg)
1256 {
1257 SV *ptriv, *retrv;
1258 HV *hvret;
1259 HV *subclass;
1260 dMY_CXT;
1261  
1262 /* Initialize our internal C data structures */
1263 PLTUBA *tuba;
1264 2 Newxz(tuba, 1, PLTUBA);
1265 2 pljsonsl_common_initialize(&MY_CXT, (PLJSONSL*)tuba, PLJSONSL_MAX_DEFAULT);
1266  
1267 2 tuba->jsn->action_callback_PUSH = pltuba_jsonsl_push_callback;
1268 2 tuba->jsn->action_callback_POP = pltuba_jsonsl_pop_callback;
1269 2 tuba->jsn->error_callback = pltuba_jsonsl_error_callback;
1270 2 jsonsl_enable_all_callbacks(tuba->jsn);
1271  
1272 2 ptriv = newSViv(PTR2IV(tuba));
1273 2 SvREADONLY_on(ptriv);
1274  
1275 /* The Perl object .. */
1276 2 hvret = newHV();
1277 2 (void)hv_stores(hvret, PLTUBA_HKEY_NAME, ptriv);
1278 2 tuba->selfrv = newRV_inc((SV*)hvret);
1279 2 sv_rvweaken(tuba->selfrv);
1280 2 retrv = newRV_noinc((SV*)hvret);
1281  
1282 2 subclass = gv_stashpv(pkg, GV_ADD);
1283 2 sv_bless(retrv, subclass);
1284  
1285 2 tuba->paramhv = newHV();
1286 2 tuba->paramhvrv = newRV_noinc((SV*)tuba->paramhv);
1287 {
1288 2 SV *ksv = newSV(0);
1289 HE *tmphe;
1290  
1291 #define X(kname) \
1292 sv_setpvs(ksv, #kname); \
1293 tmphe = hv_store_ent(tuba->paramhv, ksv, &PL_sv_undef, 0); \
1294 HeVAL(tmphe) = &PL_sv_placeholder; \
1295 assert(tmphe); \
1296 tuba->p_ents.pe_##kname.he = tmphe;
1297  
1298 2 PLTUBA_XPARAMS;
1299 #undef X
1300 }
1301  
1302 #define initialize_param_iv(b) \
1303 PLTUBA_PARAM_FIELD(tuba, b).sv = newSViv(0); \
1304 SvREADONLY_on(PLTUBA_PARAM_FIELD(tuba,b).sv);
1305 #define initialize_param_dualvar(b) \
1306 PLTUBA_PARAM_FIELD(tuba, b).sv = newSViv(0); \
1307 sv_setpv(PLTUBA_PARAM_FIELD(tuba, b).sv, " "); \
1308 SvIOK_on(PLTUBA_PARAM_FIELD(tuba,b).sv); \
1309 SvREADONLY_on(PLTUBA_PARAM_FIELD(tuba,b).sv);
1310  
1311 2 initialize_param_iv(Index);
1312 2 initialize_param_dualvar(Mode);
1313 2 initialize_param_dualvar(Type);
1314  
1315 #undef initialize_param_iv
1316 #undef initialize_param_dualvar
1317  
1318 2 SvREADONLY_on(tuba->paramhv);
1319 2 return retrv;
1320 }
1321  
1322 /**
1323 * Initialize our thread-local context
1324 */
1325 #define POPULATE_CXT \
1326 MY_CXT.stash_obj = gv_stashpv(PLJSONSL_CLASS_NAME, GV_ADD); \
1327 MY_CXT.stash_boolean = gv_stashpv(PLJSONSL_BOOLEAN_NAME, GV_ADD); \
1328 MY_CXT.stash_tuba = gv_stashpv(PLTUBA_CLASS_NAME, GV_ADD); \
1329 MY_CXT.quick = NULL;
1330  
1331  
1332 /**
1333 * These two macros arrange for the contents of the result stack to be returned
1334 * to perlspace.
1335 */
1336 #define dRESULT_VARS \
1337 int result_count; \
1338 int result_iter; \
1339 SV *result_sv;
1340  
1341 #define RETURN_RESULTS(pjsn) \
1342 switch(GIMME_V) { \
1343 case G_VOID: \
1344 result_count = 0; \
1345 break; \
1346 case G_SCALAR: \
1347 result_sv = av_shift(pjsn->results); \
1348 if (result_sv == &PL_sv_undef) { \
1349 result_count = 0; \
1350 break; \
1351 } \
1352 XPUSHs(sv_2mortal(result_sv)); \
1353 result_count = 1; \
1354 break; \
1355 case G_ARRAY: \
1356 result_count = av_len(pjsn->results) + 1; \
1357 if (result_count == 0) { \
1358 break; \
1359 } \
1360 EXTEND(SP, result_count); \
1361 for (result_iter = 0; result_iter < result_count; result_iter++) { \
1362 result_sv = av_delete(pjsn->results, result_iter, 0); \
1363 /*already mortal according to av_delete*/ \
1364 PUSHs(result_sv); \
1365 } \
1366 av_clear(pjsn->results); \
1367 break; \
1368 default: \
1369 die("eh? (RETURN_RESULTS)"); \
1370 result_count = 0; \
1371 break; \
1372 }
1373  
1374  
1375  
1376  
1377 MODULE = JSON::SL PACKAGE = JSON::SL PREFIX = PLJSONSL_
1378  
1379 PROTOTYPES: DISABLED
1380  
1381 BOOT:
1382 {
1383 MY_CXT_INIT;
1384 19 POPULATE_CXT;
1385 19 PLJSONSL_ESCTBL_INIT(ESCTBL);
1386 }
1387  
1388 SV *
1389 PLJSONSL_new(SV *pkg, ...)
1390 PREINIT:
1391 PLJSONSL *pjsn;
1392 SV *ptriv, *retrv;
1393 int levels;
1394 dMY_CXT;
1395 CODE:
1396 (void)pkg;
1397 24 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 3 21 24
1398 3 ▶ if (!SvIOK(ST(1))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvIOK(ST(1))) { 0 3 3
1399 0 die("Second argument (if provided) must be numeric");
1400 }
1401 3 levels = SvIV(ST(1));
1402 3 ▶ if (levels < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (levels < 2) { 0 3 3
1403 0 die ("Levels must be at least 2");
1404 }
1405 } else {
1406 21 levels = PLJSONSL_MAX_DEFAULT;
1407 }
1408  
1409 24 Newxz(pjsn, 1, PLJSONSL);
1410 24 pljsonsl_common_initialize(&MY_CXT, pjsn, levels);
1411 24 ptriv = newSViv(PTR2IV(pjsn));
1412 24 retrv = newRV_noinc(ptriv);
1413 24 sv_bless(retrv, MY_CXT.stash_obj);
1414 24 pjsn->buf = newSVpvn("", 0);
1415  
1416 24 jsonsl_enable_all_callbacks(pjsn->jsn);
1417 24 pjsn->jsn->action_callback = initial_callback;
1418 24 pjsn->jsn->error_callback = error_callback;
1419  
1420 24 pjsn->results = newAV();
1421 PLJSONSL_INIT_KSV(pjsn);
1422 24 RETVAL = retrv;
1423  
1424 OUTPUT: RETVAL
1425  
1426  
1427 void
1428 PLJSONSL_set_jsonpointer(PLJSONSL *pjsn, AV *paths)
1429 PPCODE:
1430 4 pljsonsl_set_jsonpointer(pjsn, paths);
1431  
1432 SV *
1433 PLJSONSL_root(PLJSONSL *pjsn)
1434 CODE:
1435 3 ▶ if (pjsn->root) {
Branch: 1 branch
BranchTrueFalseTotal
if (pjsn->root) { 3 0 3
1436 3 RETVAL = newRV_inc(pjsn->root);
1437 } else {
1438 0 RETVAL = &PL_sv_undef;
1439 }
1440 OUTPUT: RETVAL
1441  
1442 void
1443 PLJSONSL__modify_readonly(PLJSONSL *pjsn, SV *ref)
1444 ALIAS:
1445 make_referrent_writeable = 1
1446 make_referrent_readonly = 2
1447 CODE:
1448 3 ▶ if (!SvROK(ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(ref)) { 0 3 3
1449 0 die("Variable is not a reference!");
1450 }
1451 3 ▶ if (ix == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ix == 0) { 0 3 3
1452 0 PLJSONSL_CROAK_USAGE("use make_referrent_writeable or make_referrent_readonly");
1453 3 ▶ } else if (ix == 1) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (ix == 1) { 1 2 3
1454 1 SvREADONLY_off(SvRV(ref));
1455 2 ▶ } else if (ix == 2) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (ix == 2) { 2 0 2
1456 2 SvREADONLY_on(SvRV(ref));
1457 }
1458  
1459 int
1460 PLJSONSL_referrent_is_writeable(PLJSONSL *pjsn, SV *ref)
1461 CODE:
1462 5 ▶ if (!SvROK(ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(ref)) { 0 5 5
1463 0 die("Variable is not a reference!");
1464 }
1465 5 ▶ RETVAL = SvREADONLY(SvRV(ref)) == 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = SvREADONLY(SvRV(ref)) == 0; 0 5 5
1466 OUTPUT: RETVAL
1467  
1468  
1469 void
1470 PLJSONSL_feed(PLJSONSL *pjsn, SV *input)
1471 ALIAS:
1472 incr_parse =1
1473  
1474 PPCODE:
1475 {
1476 dRESULT_VARS;
1477 24 pljsonsl_feed_incr(pjsn, input);
1478 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
1479 }
1480  
1481 void
1482 PLJSONSL_fetch(PLJSONSL *pjsn)
1483 PPCODE:
1484 {
1485 dRESULT_VARS;
1486 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
1487 }
1488  
1489 int
1490 PLJSONSL__escape_table_chr(PLJSONSL *pjsn, U8 chrc, ...)
1491 CODE:
1492 2 ▶ if (chrc > 0x7f) {
Branch: 1 branch
BranchTrueFalseTotal
if (chrc > 0x7f) { 0 2 2
1493 0 warn("Attempted to set non-ASCII escape preference");
1494 0 RETVAL = -1;
1495 } else {
1496 2 RETVAL = pjsn->escape_table[chrc];
1497 2 ▶ if (items == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 3) { 2 0 2
1498 2 pjsn->escape_table[chrc] = SvIV(ST(2));
1499 }
1500 }
1501 OUTPUT: RETVAL
1502  
1503 void
1504 PLJSONSL_reset(PLJSONSL *pjsn)
1505 CODE:
1506 2 ▶ REFDEC_FIELD(pjsn, root);
Branch: 1 branch
BranchTrueFalseTotal
REFDEC_FIELD(pjsn, root); 0 2 2
1507  
1508 2 ▶ if (pjsn->results) {
Branch: 1 branch
BranchTrueFalseTotal
if (pjsn->results) { 2 0 2
1509 2 av_clear(pjsn->results);
1510 }
1511 2 ▶ if (pjsn->buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (pjsn->buf) { 2 0 2
1512 2 SvCUR_set(pjsn->buf, 0);
1513 }
1514  
1515 2 jsonsl_reset(pjsn->jsn);
1516 2 pjsn->pos_min_valid = 0;
1517 2 pjsn->keep_pos = 0;
1518 2 pjsn->curhk = NULL;
1519 2 pjsn->jsn->action_callback_PUSH = initial_callback;
1520  
1521 SV*
1522 PLJSONSL_root_callback(PLJSONSL *pjsn, SV *callback)
1523 CODE:
1524 4 RETVAL = pjsn->options.root_callback;
1525 4 ▶ if (RETVAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL) { 1 3 4
1526 1 SvREFCNT_inc(RETVAL);
1527 } else {
1528 3 RETVAL = &PL_sv_undef;
1529 }
1530  
1531 4 ▶ if (SvTYPE(callback) == SVt_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(callback) == SVt_NULL) { 1 3 4
1532 1 ▶ if (pjsn->options.root_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (pjsn->options.root_callback) { 1 0 1
1533 1 SvREFCNT_dec(pjsn->options.root_callback);
1534 1 pjsn->options.root_callback = NULL;
1535 }
1536 } else {
1537 3 ▶ if (SvTYPE(callback) != SVt_RV ||
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(callback) != SVt_RV || 2 1 3
1538 2 ▶ SvTYPE(SvRV(callback)) != SVt_PVCV) {
Branch: 1 branch
BranchTrueFalseTotal
SvTYPE(SvRV(callback)) != SVt_PVCV) { 0 2 2
1539 1 die("Second argument must be undef or a CODE ref");
1540 }
1541 2 ▶ if (pjsn->options.root_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (pjsn->options.root_callback) { 0 2 2
1542 0 SvREFCNT_dec(pjsn->options.root_callback);
1543 }
1544 2 pjsn->options.root_callback = newRV_inc(SvRV(callback));
1545 }
1546  
1547 OUTPUT: RETVAL
1548  
1549 void
1550 PLJSONSL_DESTROY(PLJSONSL *pjsn)
1551 PREINIT:
1552 int ii;
1553  
1554 CODE:
1555 24 ▶ if (pjsn->priv_global.is_global == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (pjsn->priv_global.is_global == 0) { 24 0 24
1556 24 ▶ REFDEC_FIELD(pjsn, root);
Branch: 1 branch
BranchTrueFalseTotal
REFDEC_FIELD(pjsn, root); 14 10 24
1557 24 ▶ REFDEC_FIELD(pjsn, results);
Branch: 1 branch
BranchTrueFalseTotal
REFDEC_FIELD(pjsn, results); 24 0 24
1558 24 ▶ REFDEC_FIELD(pjsn, buf);
Branch: 1 branch
BranchTrueFalseTotal
REFDEC_FIELD(pjsn, buf); 24 0 24
1559 24 ▶ REFDEC_FIELD(pjsn, options.root_callback);
Branch: 1 branch
BranchTrueFalseTotal
REFDEC_FIELD(pjsn, options.root_callback); 1 23 24
1560 } /* else, it's a mortal and shouldn't be freed */
1561 24 jsonsl_jpr_match_state_cleanup(pjsn->jsn);
1562 24 ▶ if (pjsn->jprs) {
Branch: 1 branch
BranchTrueFalseTotal
if (pjsn->jprs) { 4 20 24
1563 8 ▶ for ( ii = 0; ii < pjsn->njprs; ii++) {
Branch: 1 branch
BranchTrueFalseTotal
for ( ii = 0; ii < pjsn->njprs; ii++) { 4 4 8
1564 4 ▶ if (pjsn->jprs[ii] == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (pjsn->jprs[ii] == NULL) { 0 4 4
1565 0 break;
1566 }
1567 4 jsonsl_jpr_destroy(pjsn->jprs[ii]);
1568 }
1569 4 Safefree(pjsn->jprs);
1570 4 pjsn->jprs = NULL;
1571 }
1572 24 ▶ if (pjsn->jsn) {
Branch: 1 branch
BranchTrueFalseTotal
if (pjsn->jsn) { 24 0 24
1573 24 jsonsl_destroy(pjsn->jsn);
1574 24 pjsn->jsn = NULL;
1575 }
1576 PLJSONSL_DESTROY_KSV(pjsn);
1577 24 Safefree(pjsn);
1578  
1579 void
1580 PLJSONSL_decode_json(SV *input)
1581 PREINIT:
1582 PLJSONSL* pjsn;
1583 dRESULT_VARS;
1584  
1585 PPCODE:
1586 32 pjsn = pljsonsl_get_and_initialize_global(aTHX);
1587 32 pljsonsl_feed_oneshot(pjsn, input);
1588  
1589 30 pjsn->curhk = NULL;
1590 30 pjsn->keep_pos = 0;
1591 30 pjsn->pos_min_valid = 0;
1592 30 pjsn->jsn->action_callback_PUSH = initial_callback;
1593  
1594 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
1595 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
1596 1 die("Incomplete JSON string?");
1597 }
1598  
1599 SV *
1600 PLJSONSL_unescape_json_string(SV *input)
1601 PREINIT:
1602 size_t origlen, newlen;
1603 3 ▶ SV *retsv = NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *retsv = NULL; 0 3 3
1604 char *errpos;
1605 jsonsl_error_t err;
1606 jsonsl_special_t flags;
1607  
1608 CODE:
1609 3 ▶ if (!SvPOK(input)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvPOK(input)) { 0 3 3
1610 0 die("Input is not a valid string");
1611 }
1612 3 origlen = SvCUR(input);
1613 3 ▶ if (origlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (origlen) { 3 0 3
1614 3 retsv = newSV(origlen);
1615 3 newlen = jsonsl_util_unescape_ex(SvPVX_const(input), SvPVX(retsv),
1616 3 SvCUR(input), ESCTBL, &flags,
1617 &err, (const char**)&errpos);
1618 3 ▶ if (newlen == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (newlen == 0) { 1 2 3
1619 1 SvREFCNT_dec(retsv);
1620 1 die("Could not unescape: %s at pos %lu ('%c'..)",
1621 jsonsl_strerror(err),
1622 errpos - SvPVX_const(input),
1623 *errpos
1624 );
1625 }
1626  
1627 2 SvCUR_set(retsv, newlen);
1628 2 SvPOK_only(retsv);
1629 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
1630 1 SvUTF8_on(retsv);
1631 }
1632 } else {
1633 0 retsv = &PL_sv_undef;
1634 }
1635 2 RETVAL = retsv;
1636 OUTPUT: RETVAL
1637  
1638  
1639  
1640 void
1641 PLJSONSL_CLONE(PLJSONSL *pjsn)
1642 CODE:
1643 {
1644 MY_CXT_CLONE;
1645 0 POPULATE_CXT;
1646 }
1647  
1648 MODULE = JSON::SL PACKAGE = JSON::SL::Tuba PREFIX = PLTUBA_
1649  
1650 SV *
1651 PLTUBA__initialize(const char *pkg)
1652 CODE:
1653 2 RETVAL = pltuba_initialize_THX(aTHX_ pkg);
1654 OUTPUT: RETVAL
1655  
1656 void
1657 PLTUBA_DESTROY(PLTUBA* tuba)
1658 CODE:
1659 2 jsonsl_destroy(tuba->jsn);
1660 2 tuba->jsn = NULL;
1661  
1662 2 ▶ REFDEC_FIELD(tuba, accum);
Branch: 1 branch
BranchTrueFalseTotal
REFDEC_FIELD(tuba, accum); 0 2 2
1663 2 ▶ REFDEC_FIELD(tuba, kaccum);
Branch: 1 branch
BranchTrueFalseTotal
REFDEC_FIELD(tuba, kaccum); 0 2 2
1664 2 ▶ REFDEC_FIELD(tuba, selfrv);
Branch: 1 branch
BranchTrueFalseTotal
REFDEC_FIELD(tuba, selfrv); 2 0 2
1665 #define X(kname) \
1666 PLTUBA_RESET_PARAMFIELD(tuba, kname); \
1667 REFDEC_FIELD(tuba, p_ents.pe_##kname.sv);
1668 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
1669 #undef X
1670 2 ▶ REFDEC_FIELD(tuba, paramhvrv);
Branch: 1 branch
BranchTrueFalseTotal
REFDEC_FIELD(tuba, paramhvrv); 2 0 2
1671 /* Implicit that the hash has been decrementas as well.
1672 * Don't do another dec
1673 */
1674 2 tuba->paramhv = NULL;
1675 2 pltuba_invalidate_gvs_THX(aTHX_ tuba);
1676 2 Safefree(tuba);
1677  
1678 int
1679 PLTUBA__ax_opt(PLTUBA *tuba, int mode, ...)
1680 CODE:
1681 20 RETVAL = tuba->accum_options[mode & 0xff];
1682 20 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 15 5 20
1683 15 tuba->accum_options[mode & 0xff] = SvIV(ST(2));
1684 }
1685 OUTPUT: RETVAL
1686  
1687 int
1688 PLTUBA_accum_kv(PLTUBA *tuba, ...)
1689 CODE:
1690 4 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 0 4 4
1691 0 die("accum_kv(..boolean)");
1692 }
1693 4 RETVAL = tuba->options.accum_kv;
1694 4 ▶ if (items == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 2) { 3 1 4
1695 3 int newval = SvIV(ST(1));
1696 3 ▶ if (newval) {
Branch: 1 branch
BranchTrueFalseTotal
if (newval) { 3 0 3
1697 3 tuba->accum_options['#'] = 1;
1698 }
1699 3 tuba->options.accum_kv = newval;
1700 }
1701 OUTPUT: RETVAL
1702  
1703  
1704 void
1705 PLTUBA__parse(PLTUBA* tuba, SV *input)
1706 CODE:
1707 2 pltuba_feed(tuba, input);
1708  
1709  
1710 INCLUDE: srcout/option_accessors.xs
« jsonxs_inline.h ↑ Summary