XS.xs

filter: Statementstmt 94.8% 384 / 405 Branchbran 68.9% 218 / 316 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 83.5% 602 / 721 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/JQ/XS.pm ↑ Summary .jq-build/stage/include/jv.h »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #include "ppport.h"
7  
8 #include <jq.h>
9 #include <jv.h>
10  
11 /* Built against the jq vendored in this distribution rather than the one the
12 * OS packages (the default; "perl Makefile.PL JQ_SYSTEM=1" picks the other).
13 * jq's public headers carry no version, so the build stages a copy of the
14 * version.h of the jq it actually compiled. */
15 #ifdef JQ_XS_EMBEDDED
16 # include <jq_version.h>
17 #endif
18  
19 /* Error callback context to capture compilation errors */
20 typedef struct {
21 SV *buffer;
22 } ErrorContext;
23  
24 /* The C struct wrapped as the JQ::XS object via T_PTROBJ.
25 *
26 * Everything a caller can configure lives here rather than in a Perl hash,
27 * because the object is a blessed pointer, not a hashref. output_opts is the
28 * option hash process_json() formats with, kept alongside the jv_dump_string
29 * flags it compiles down to so that a per-call override can be merged over it
30 * without recomputing anything the caller did not ask to change. */
31 typedef struct {
32 jq_state *state;
33 SV *program;
34  
35 /* Output formatting for process_json() */
36 SV *output_opts; /* hashref: what set_output() was given */
37 int dump_flags; /* JV_PRINT_* flags compiled from output_opts */
38 int raw_output; /* emit strings unquoted, like jq -r */
39  
40 int start_flags; /* jq_start() flags: JQ_DEBUG_TRACE and friends */
41 int die_on_halt_error; /* croak when halt_error left a message */
42  
43 /* Perl code refs behind the debug, stderr and input/inputs builtins */
44 SV *debug_cb;
45 SV *stderr_cb;
46 SV *input_cb;
47  
48 /* Set by the last run: what halt/halt_error left behind */
49 int halted;
50 SV *exit_code;
51 SV *error_message;
52  
53 /* An exception thrown by the debug or stderr callback. It cannot be
54 * croaked from inside libjq -- that would longjmp out through jq's own C
55 * frames, abandoning the jq_state mid-run -- so it is parked here and
56 * rethrown once jq_next() has stopped producing. */
57 SV *cb_error;
58  
59 /* libjq's state machine is not reentrant, so a callback that reenters
60 * process() on the same object has to be turned away. */
61 int running;
62 } jq_xs_t;
63 typedef jq_xs_t *JQ__XS; /* XS type "JQ::XS" -> C type "JQ__XS", blessed class "JQ::XS" */
64  
65 /* Forward declaration of conversion functions - need pTHX for Perl context */
66 static SV *jv_to_sv(pTHX_ jv);
67 static jv sv_to_jv(pTHX_ SV *);
68  
69 /* Error callback for jq_set_error_cb */
70 4 static void error_callback(void *user_data, jv msg) {
71 dTHX;
72 4 ErrorContext *ctx = (ErrorContext *)user_data;
73 8 ▶ if (ctx && ctx->buffer && jv_is_valid(msg)) {
Branch: 3 branches
BranchTrueFalseTotal
if (ctx && ctx->buffer && jv_is_valid(msg)) { 4 0 4
if (ctx && ctx->buffer && jv_is_valid(msg)) { 4 0 4
if (ctx && ctx->buffer && jv_is_valid(msg)) { 4 0 4
74 4 jv err_text = jq_format_error(msg);
75 4 ▶ if (jv_is_valid(err_text)) {
Branch: 1 branch
BranchTrueFalseTotal
if (jv_is_valid(err_text)) { 4 0 4
76 4 const char *text = jv_string_value(err_text);
77 4 ▶ if (text) {
Branch: 1 branch
BranchTrueFalseTotal
if (text) { 4 0 4
78 4 sv_catpv(ctx->buffer, text);
79 4 sv_catpv(ctx->buffer, "\n");
80 }
81 4 jv_free(err_text);
82 }
83 } else {
84 0 jv_free(msg);
85 }
86 4 }
87  
88 /* Convert jv to SV - handles all jv types */
89 181 static SV *jv_to_sv(pTHX_ jv v) {
90 181 jv_kind kind = jv_get_kind(v);
91 181 SV *result = NULL;
92  
93 181 switch (kind) {
94 4 case JV_KIND_NULL:
95 4 result = newSV(0);
96 4 break;
97  
98 9 case JV_KIND_FALSE:
99 9 result = newSV(0);
100 9 sv_setref_iv(result, "JSON::PP::Boolean", 0);
101 9 break;
102  
103 9 case JV_KIND_TRUE:
104 9 result = newSV(0);
105 9 sv_setref_iv(result, "JSON::PP::Boolean", 1);
106 9 break;
107  
108 57 case JV_KIND_NUMBER:
109 57 ▶ if (jv_is_integer(v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (jv_is_integer(v)) { 55 2 57
110 55 double d = jv_number_value(v);
111 55 ▶ if (d >= IV_MIN && d <= IV_MAX) {
Branch: 2 branches
BranchTrueFalseTotal
if (d >= IV_MIN && d <= IV_MAX) { 55 0 55
if (d >= IV_MIN && d <= IV_MAX) { 55 0 55
112 55 result = newSViv((IV)d);
113 } else {
114 0 result = newSVnv(d);
115 }
116 } else {
117 2 result = newSVnv(jv_number_value(v));
118 }
119 57 break;
120  
121 64 case JV_KIND_STRING: {
122 64 jv copy = jv_copy(v);
123 64 int len = jv_string_length_bytes(copy);
124 64 const char *str = jv_string_value(v);
125 64 result = newSVpvn(str, len);
126 64 SvUTF8_on(result);
127 64 break;
128 }
129  
130 17 case JV_KIND_ARRAY: {
131 17 AV *av = newAV();
132 67 ▶ jv_array_foreach(v, i, item) {
Branch: 3 branches
BranchTrueFalseTotal
jv_array_foreach(v, i, item) { 33 17 50
jv_array_foreach(v, i, item) { 33 17 50
jv_array_foreach(v, i, item) { 17 17 34
133 33 SV *item_sv = jv_to_sv(aTHX_ item);
134 33 av_push(av, item_sv);
135 }
136 17 result = newRV_noinc((SV *)av);
137 17 break;
138 }
139  
140 16 case JV_KIND_OBJECT: {
141 16 HV *hv = newHV();
142 56 ▶ jv_object_foreach(v, key, val) {
Branch: 3 branches
BranchTrueFalseTotal
jv_object_foreach(v, key, val) { 24 16 40
jv_object_foreach(v, key, val) { 24 16 40
jv_object_foreach(v, key, val) { 16 16 32
143 24 SV *key_sv = jv_to_sv(aTHX_ key);
144 24 SV *val_sv = jv_to_sv(aTHX_ val);
145 24 hv_store_ent(hv, key_sv, val_sv, 0);
146 24 SvREFCNT_dec(key_sv);
147 }
148 16 result = newRV_noinc((SV *)hv);
149 16 break;
150 }
151  
152 5 case JV_KIND_INVALID:
153 default:
154 5 result = newSV(0);
155 5 break;
156 }
157  
158 181 jv_free(v);
159 181 return result;
160 }
161  
162 /* Convert SV to jv - handles all Perl types */
163 614 static jv sv_to_jv(pTHX_ SV *sv) {
164 /* Perl's native boolean SVs: the immortals returned by comparison and
165 * logical operators. On perl < 5.36 a copy of one is indistinguishable
166 * from an ordinary dualvar, so only identity can be checked. */
167 614 ▶ if (sv == &PL_sv_yes) return jv_true();
Branch: 1 branch
BranchTrueFalseTotal
if (sv == &PL_sv_yes) return jv_true(); 3 611 614
168 611 ▶ if (sv == &PL_sv_no) return jv_false();
Branch: 1 branch
BranchTrueFalseTotal
if (sv == &PL_sv_no) return jv_false(); 2 609 611
169 #ifdef SvIsBOOL
170 /* perl >= 5.36: copies keep their boolean flag (builtin::true/false) */
171 609 ▶ if (SvIsBOOL(sv)) return SvTRUE(sv) ? jv_true() : jv_false();
Branch: 2 branches
BranchTrueFalseTotal
if (SvIsBOOL(sv)) return SvTRUE(sv) ? jv_true() : jv_false(); 0 609 609
if (SvIsBOOL(sv)) return SvTRUE(sv) ? jv_true() : jv_false(); 0 0 0
172 #endif
173  
174 609 ▶ if (!SvOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) { 23 586 609
175 23 return jv_null();
176 }
177  
178 586 ▶ if (SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) { 503 83 586
179 503 SV *ref = SvRV(sv);
180  
181 /* Blessed booleans: JSON::PP::Boolean and friends */
182 503 ▶ if (SvOBJECT(ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOBJECT(ref)) { 4 499 503
183 4 ▶ if (sv_derived_from(sv, "JSON::PP::Boolean")
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(sv, "JSON::PP::Boolean") 0 4 4
184 0 ▶ || sv_derived_from(sv, "Types::Serialiser::Boolean")
Branch: 1 branch
BranchTrueFalseTotal
|| sv_derived_from(sv, "Types::Serialiser::Boolean") 0 0 0
185 0 ▶ || sv_derived_from(sv, "boolean")) {
Branch: 1 branch
BranchTrueFalseTotal
|| sv_derived_from(sv, "boolean")) { 0 0 0
186 4 ▶ return SvTRUE(ref) ? jv_true() : jv_false();
Branch: 1 branch
BranchTrueFalseTotal
return SvTRUE(ref) ? jv_true() : jv_false(); 2 2 4
187 }
188 }
189  
190 /* Array reference */
191 499 ▶ if (SvTYPE(ref) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(ref) == SVt_PVAV) { 197 302 499
192 197 AV *av = (AV *)ref;
193 197 jv arr = jv_array();
194 197 I32 len = av_len(av) + 1;
195 228 ▶ for (I32 i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (I32 i = 0; i < len; i++) { 31 197 228
196 31 SV **item = av_fetch(av, i, 0);
197 31 ▶ if (item) {
Branch: 1 branch
BranchTrueFalseTotal
if (item) { 31 0 31
198 31 arr = jv_array_append(arr, sv_to_jv(aTHX_ *item));
199 } else {
200 0 arr = jv_array_append(arr, jv_null());
201 }
202 }
203 197 return arr;
204 }
205  
206 /* Hash reference */
207 302 ▶ if (SvTYPE(ref) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(ref) == SVt_PVHV) { 296 6 302
208 296 HV *hv = (HV *)ref;
209 296 jv obj = jv_object();
210 HE *he;
211 296 hv_iterinit(hv);
212 606 ▶ while ((he = hv_iternext(hv)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv)) != NULL) { 310 296 606
213 310 ▶ SV *key_sv = HeSVKEY(he);
Branch: 2 branches
BranchTrueFalseTotal
SV *key_sv = HeSVKEY(he); 310 0 310
SV *key_sv = HeSVKEY(he); 0 310 310
214 310 ▶ if (!key_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!key_sv) { 310 0 310
215 310 key_sv = hv_iterkeysv(he);
216 }
217 310 SV *val_sv = HeVAL(he);
218 310 char *key_str = SvPVutf8_nolen(key_sv);
219 310 jv key = jv_string(key_str);
220 310 jv val = sv_to_jv(aTHX_ val_sv);
221 310 obj = jv_object_set(obj, key, val);
222 }
223 296 return obj;
224 }
225  
226 /* Unblessed reference to a plain scalar: \1 is true, \0 is false */
227 6 ▶ if (SvTYPE(ref) < SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(ref) < SVt_PVAV) { 6 0 6
228 6 ▶ return SvTRUE(ref) ? jv_true() : jv_false();
Branch: 1 branch
BranchTrueFalseTotal
return SvTRUE(ref) ? jv_true() : jv_false(); 3 3 6
229 }
230 }
231  
232 /* Check numeric type first */
233 83 ▶ if (SvIOK(sv) && !SvPOK(sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvIOK(sv) && !SvPOK(sv)) { 49 34 83
if (SvIOK(sv) && !SvPOK(sv)) { 49 0 49
234 49 return jv_number((double)SvIV(sv));
235 }
236 34 ▶ if (SvNOK(sv) && !SvPOK(sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvNOK(sv) && !SvPOK(sv)) { 2 32 34
if (SvNOK(sv) && !SvPOK(sv)) { 2 0 2
237 2 return jv_number(SvNV(sv));
238 }
239  
240 /* Everything else is a string */
241 STRLEN len;
242 32 char *str = SvPVutf8(sv, len);
243 32 return jv_string_sized(str, len);
244 }
245  
246 /* ------------------------------------------------------------------------ *
247 * Does this program pull in a module?
248 *
249 * jq resolves "include"/"import" through the JQ_LIBRARY_PATH attribute, but
250 * emptying that attribute is not enough to stop it: linker.c's
251 * default_search() prepends "." to the search list whenever a dependency
252 * names no search path of its own, so 'include "foo";' still finds ./foo.jq,
253 * and 'include "foo" {search:"/x"};' bypasses the attribute altogether.
254 * Refusing modules therefore has to be decided from the program text.
255 *
256 * That is exact rather than a heuristic, because jq's grammar (parser.y,
257 * TopLevel) only accepts imports in the program prologue:
258 *
259 * TopLevel: Module Imports Query | Module Imports FuncDefs
260 * Module: <empty> | "module" Query ';'
261 *
262 * so if the first token after an optional module header is neither "import"
263 * nor "include", the program has no imports at all. Looking only at the
264 * prologue is also what keeps legal programs working: "include" is one of
265 * parser.y's Keywords, accepted as an object key and after '.', so
266 * {include: 1} and .foo.include are valid jq that a scan of the whole text
267 * would reject.
268 *
269 * The tokenizing mirrors lexer.l: whitespace is [ \r\n\t], '#' opens a
270 * comment that ends at the next newline except that a backslash before a
271 * backslash or a newline continues it, and "import"/"include" lose to the
272 * longer IDENT rule when an identifier character or a "::" follows.
273 * ------------------------------------------------------------------------ */
274  
275 24 static int jqxs_is_ident_char(char c) {
276 24 ▶ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
Branch: 3 branches
BranchTrueFalseTotal
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') 0 0 0
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') 0 24 24
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') 0 0 0
277 48 ▶ || (c >= '0' && c <= '9') || c == '_';
Branch: 4 branches
BranchTrueFalseTotal
|| (c >= '0' && c <= '9') || c == '_'; 0 24 24
|| (c >= '0' && c <= '9') || c == '_'; 0 24 24
|| (c >= '0' && c <= '9') || c == '_'; 0 0 0
|| (c >= '0' && c <= '9') || c == '_'; 0 24 24
278 }
279  
280 /* Advance past whitespace and comments. With continuations set, a backslash
281 * before a backslash or a newline carries a comment onto the next line, which
282 * is what jq 1.7 added to its lexer; clear it for the jq 1.6 rule, where a
283 * comment simply ends at the newline. */
284 134 static const char *jqxs_skip_blank(const char *p, const char *end,
285 int continuations) {
286 168 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 168 0 168
287 168 char c = *p;
288 168 ▶ if (c == ' ' || c == '\t' || c == '\r' || c == '\n') { p++; continue; }
Branch: 4 branches
BranchTrueFalseTotal
if (c == ' ' || c == '\t' || c == '\r' || c == '\n') { p++; continue; } 148 20 168
if (c == ' ' || c == '\t' || c == '\r' || c == '\n') { p++; continue; } 148 0 148
if (c == ' ' || c == '\t' || c == '\r' || c == '\n') { p++; continue; } 148 0 148
if (c == ' ' || c == '\t' || c == '\r' || c == '\n') { p++; continue; } 4 144 148
289 144 ▶ if (c != '#') break;
Branch: 1 branch
BranchTrueFalseTotal
if (c != '#') break; 134 10 144
290  
291 10 p++; /* inside a comment */
292 166 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 166 0 166
293 166 ▶ if (continuations && *p == '\\') {
Branch: 2 branches
BranchTrueFalseTotal
if (continuations && *p == '\\') { 108 58 166
if (continuations && *p == '\\') { 2 106 108
294 2 ▶ if (p + 1 < end && (p[1] == '\\' || p[1] == '\n')) { p += 2; continue; }
Branch: 3 branches
BranchTrueFalseTotal
if (p + 1 < end && (p[1] == '\\' || p[1] == '\n')) { p += 2; continue; } 2 0 2
if (p + 1 < end && (p[1] == '\\' || p[1] == '\n')) { p += 2; continue; } 2 0 2
if (p + 1 < end && (p[1] == '\\' || p[1] == '\n')) { p += 2; continue; } 2 0 2
295 0 ▶ if (p + 2 < end && p[1] == '\r' && p[2] == '\n') { p += 3; continue; }
Branch: 3 branches
BranchTrueFalseTotal
if (p + 2 < end && p[1] == '\r' && p[2] == '\n') { p += 3; continue; } 0 0 0
if (p + 2 < end && p[1] == '\r' && p[2] == '\n') { p += 3; continue; } 0 0 0
if (p + 2 < end && p[1] == '\r' && p[2] == '\n') { p += 3; continue; } 0 0 0
296 0 p++; /* a lone backslash is just text */
297 0 continue;
298 }
299 164 ▶ if (*p == '\n') { p++; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\n') { p++; break; } 10 154 164
300 154 ▶ if (*p == '\r' && p + 1 < end && p[1] == '\n') { p += 2; break; }
Branch: 3 branches
BranchTrueFalseTotal
if (*p == '\r' && p + 1 < end && p[1] == '\n') { p += 2; break; } 0 154 154
if (*p == '\r' && p + 1 < end && p[1] == '\n') { p += 2; break; } 0 0 0
if (*p == '\r' && p + 1 < end && p[1] == '\n') { p += 2; break; } 0 0 0
301 154 p++;
302 }
303 }
304 134 return p;
305 }
306  
307 /* Is the token at p exactly this keyword? */
308 160 static int jqxs_word_is(const char *p, const char *end, const char *word) {
309 160 STRLEN n = strlen(word);
310 const char *q;
311  
312 160 ▶ if ((STRLEN)(end - p) < n || memcmp(p, word, n) != 0) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if ((STRLEN)(end - p) < n || memcmp(p, word, n) != 0) return 0; 134 26 160
if ((STRLEN)(end - p) < n || memcmp(p, word, n) != 0) return 0; 110 24 134
313  
314 24 q = p + n;
315 /* "includes" and "include::x" both lex as one longer IDENT, not a keyword. */
316 24 ▶ if (q < end && jqxs_is_ident_char(*q)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (q < end && jqxs_is_ident_char(*q)) return 0; 24 0 24
if (q < end && jqxs_is_ident_char(*q)) return 0; 0 24 24
317 24 ▶ if (q + 1 < end && q[0] == ':' && q[1] == ':') return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (q + 1 < end && q[0] == ':' && q[1] == ':') return 0; 24 0 24
if (q + 1 < end && q[0] == ':' && q[1] == ':') return 0; 0 24 24
if (q + 1 < end && q[0] == ':' && q[1] == ':') return 0; 0 0 0
318 24 return 1;
319 }
320  
321 /* Advance past the ';' closing a "module <metadata>;" header, or return NULL
322 * if it cannot be found. Only enough of the metadata expression is understood
323 * to locate that ';': nesting and string literals. String interpolation makes
324 * the metadata non-constant, which jq rejects, so meeting one is treated as
325 * "cannot tell" rather than parsed. */
326 8 static const char *jqxs_skip_module_header(const char *p, const char *end,
327 int continuations) {
328 8 int depth = 0;
329  
330 for (;;) {
331 72 p = jqxs_skip_blank(p, end, continuations);
332 72 ▶ if (p >= end) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) return NULL; 0 72 72
333  
334 72 ▶ if (*p == '"') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '"') { 8 64 72
335 8 p++;
336 20 ▶ while (p < end && *p != '"') {
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p != '"') { 20 0 20
while (p < end && *p != '"') { 12 8 20
337 12 ▶ if (*p == '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\\') { 0 12 12
338 0 ▶ if (p + 1 < end && p[1] == '(') return NULL; /* interpolation */
Branch: 2 branches
BranchTrueFalseTotal
if (p + 1 < end && p[1] == '(') return NULL; /* interpolation */ 0 0 0
if (p + 1 < end && p[1] == '(') return NULL; /* interpolation */ 0 0 0
339 0 p += 2;
340 0 continue;
341 }
342 12 p++;
343 }
344 8 ▶ if (p >= end) return NULL; /* unterminated */
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) return NULL; /* unterminated */ 0 8 8
345 8 p++;
346 8 continue;
347 }
348  
349 64 ▶ if (*p == '(' || *p == '[' || *p == '{') { depth++; p++; continue; }
Branch: 3 branches
BranchTrueFalseTotal
if (*p == '(' || *p == '[' || *p == '{') { depth++; p++; continue; } 64 0 64
if (*p == '(' || *p == '[' || *p == '{') { depth++; p++; continue; } 64 0 64
if (*p == '(' || *p == '[' || *p == '{') { depth++; p++; continue; } 8 56 64
350 56 ▶ if (*p == ')' || *p == ']' || *p == '}') { if (depth) depth--; p++; continue; }
Branch: 4 branches
BranchTrueFalseTotal
if (*p == ')' || *p == ']' || *p == '}') { if (depth) depth--; p++; continue; } 56 0 56
if (*p == ')' || *p == ']' || *p == '}') { if (depth) depth--; p++; continue; } 56 0 56
if (*p == ')' || *p == ']' || *p == '}') { if (depth) depth--; p++; continue; } 8 48 56
if (*p == ')' || *p == ']' || *p == '}') { if (depth) depth--; p++; continue; } 8 0 8
351 48 ▶ if (*p == ';' && depth == 0) return p + 1;
Branch: 2 branches
BranchTrueFalseTotal
if (*p == ';' && depth == 0) return p + 1; 8 40 48
if (*p == ';' && depth == 0) return p + 1; 8 0 8
352 40 p++;
353 }
354 }
355  
356 54 static int jqxs_prologue_imports(const char *src, STRLEN len,
357 int continuations) {
358 54 const char *end = src + len;
359 54 const char *p = jqxs_skip_blank(src, end, continuations);
360  
361 54 ▶ if (jqxs_word_is(p, end, "module")) {
Branch: 1 branch
BranchTrueFalseTotal
if (jqxs_word_is(p, end, "module")) { 8 46 54
362 8 const char *after = jqxs_skip_module_header(p + (sizeof("module") - 1),
363 end, continuations);
364 /* An unparseable header is a program jq will reject anyway; erring towards
365 * "has imports" keeps the answer conservative for a caller who forbade
366 * them. */
367 8 ▶ if (!after) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!after) return 1; 0 8 8
368 8 p = jqxs_skip_blank(after, end, continuations);
369 }
370  
371 54 ▶ return jqxs_word_is(p, end, "import") || jqxs_word_is(p, end, "include");
Branch: 2 branches
BranchTrueFalseTotal
return jqxs_word_is(p, end, "import") || jqxs_word_is(p, end, "include"); 52 2 54
return jqxs_word_is(p, end, "import") || jqxs_word_is(p, end, "include"); 14 38 52
372 }
373  
374 34 static int jqxs_program_has_imports(const char *src, STRLEN len) {
375 /* The two comment rules disagree about one thing: whether an "include" on
376 * the line after a comment ending in a backslash is a directive or more
377 * comment. jq 1.6 says directive, jq 1.7 and later say comment, and a
378 * JQ_SYSTEM=1 build can be linked against either -- so ask under both and
379 * take the union, leaving no reading in which allow_includes => 0 is the
380 * lenient one. The only programs this costs are those the two libjqs read
381 * differently, which already cannot mean the same thing on both. */
382 34 return jqxs_prologue_imports(src, len, 1)
383 34 ▶ || jqxs_prologue_imports(src, len, 0);
Branch: 2 branches
BranchTrueFalseTotal
|| jqxs_prologue_imports(src, len, 0); 20 14 34
|| jqxs_prologue_imports(src, len, 0); 2 18 20
384 }
385  
386 /* ------------------------------------------------------------------------ *
387 * Calling back into Perl from inside libjq
388 *
389 * All three callbacks run on the C stack of jq_next(), so none of them may let
390 * a Perl exception escape: unwinding from there would leave the jq_state
391 * half-way through a program. Each therefore calls with G_EVAL and deals
392 * with $@ itself.
393 * ------------------------------------------------------------------------ */
394  
395 /* The debug and stderr builtins: hand the value to Perl, ignore the result. */
396 4 static void jqxs_call_msg_cb(pTHX_ jq_xs_t *self, SV *code, jv msg) {
397 4 dSP;
398 4 SV *arg = jv_to_sv(aTHX_ msg); /* consumes msg */
399  
400 4 ENTER;
401 4 SAVETMPS;
402 4 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 4 4
403 4 ▶ XPUSHs(sv_2mortal(arg));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(arg)); 0 4 4
404 4 PUTBACK;
405  
406 4 call_sv(code, G_VOID|G_DISCARD|G_EVAL);
407  
408 4 ▶ if (SvTRUE(ERRSV) && !self->cb_error) {
Branch: 3 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV) && !self->cb_error) { 4 0 4
if (SvTRUE(ERRSV) && !self->cb_error) { 2 2 4
if (SvTRUE(ERRSV) && !self->cb_error) { 1 1 2
409 1 ▶ self->cb_error = newSVsv(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
self->cb_error = newSVsv(ERRSV); 1 0 1
410 }
411  
412 4 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 4 0 4
413 4 LEAVE;
414 4 }
415  
416 3 static void jqxs_debug_cb(void *data, jv msg) {
417 dTHX;
418 3 jq_xs_t *self = (jq_xs_t *)data;
419 3 jqxs_call_msg_cb(aTHX_ self, self->debug_cb, msg);
420 3 }
421  
422 /* jq_set_stderr_cb, and the "stderr" builtin it serves, arrived in jq 1.7.
423 * The vendored build always has it; a JQ_SYSTEM=1 build against an older
424 * libjq does not, and Makefile.PL link-probes for it. */
425 #ifdef JQ_XS_HAVE_STDERR_CB
426 1 static void jqxs_stderr_cb(void *data, jv msg) {
427 dTHX;
428 1 jq_xs_t *self = (jq_xs_t *)data;
429 1 jqxs_call_msg_cb(aTHX_ self, self->stderr_cb, msg);
430 1 }
431 #endif
432  
433 /* The input/inputs builtins. An empty return list ends the stream (libjq
434 * turns an invalid carrying no message into the "break" that builtin.jq's
435 * "def inputs" catches); anything else, undef included, is one more value, so
436 * a callback can still yield JSON null. A Perl exception becomes a jq error,
437 * which is what lets the filter's own try/catch see it. */
438 14 static jv jqxs_input_cb(jq_state *jq, void *data) {
439 dTHX;
440 14 jq_xs_t *self = (jq_xs_t *)data;
441 jv result;
442 I32 count;
443 14 dSP;
444  
445 PERL_UNUSED_ARG(jq);
446  
447 14 ENTER;
448 14 SAVETMPS;
449 14 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 14 14
450 14 PUTBACK;
451  
452 /* G_ARRAY rather than its 5.36 spelling G_LIST: this builds on 5.26.3. */
453 14 count = call_sv(self->input_cb, G_ARRAY|G_EVAL);
454 14 SPAGAIN;
455  
456 14 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 14 0 14
if (SvTRUE(ERRSV)) { 3 11 14
457 STRLEN len;
458 3 ▶ const char *msg = SvPVutf8(ERRSV, len);
Branch: 1 branch
BranchTrueFalseTotal
const char *msg = SvPVutf8(ERRSV, len); 3 0 3
459 3 result = jv_invalid_with_msg(jv_string_sized(msg, len));
460 11 ▶ } else if (count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (count == 0) { 5 6 11
461 5 result = jv_invalid(); /* end of stream */
462 } else {
463 6 result = sv_to_jv(aTHX_ SP[1 - count]); /* first of what was returned */
464 }
465  
466 14 SP -= count;
467 14 PUTBACK;
468 14 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 9 5 14
469 14 LEAVE;
470  
471 14 return result;
472 }
473  
474 /* Empty one of the stored SV slots. */
475 822 static void jqxs_clear_sv(pTHX_ SV **slot) {
476 822 ▶ if (*slot) {
Branch: 1 branch
BranchTrueFalseTotal
if (*slot) { 20 802 822
477 20 SvREFCNT_dec(*slot);
478 20 *slot = NULL;
479 }
480 822 }
481  
482 /* Replace one of the stored code refs, taking a reference to the new one. */
483 12 static void jqxs_store_cb(pTHX_ SV **slot, SV *code, const char *what) {
484 12 SV *old = *slot;
485  
486 12 ▶ if (code && SvOK(code)) {
Branch: 2 branches
BranchTrueFalseTotal
if (code && SvOK(code)) { 12 0 12
if (code && SvOK(code)) { 11 1 12
487 11 ▶ if (!(SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV)) { 10 1 11
if (!(SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV)) { 0 10 10
488 1 croak("%s must be a code reference", what);
489 }
490 10 *slot = SvREFCNT_inc_simple_NN(code);
491 } else {
492 1 *slot = NULL;
493 }
494  
495 11 ▶ if (old) SvREFCNT_dec(old);
Branch: 1 branch
BranchTrueFalseTotal
if (old) SvREFCNT_dec(old); 2 9 11
496 11 }
497  
498 MODULE = JQ::XS PACKAGE = JQ::XS
499  
500 JQ::XS
501 _new(SV *klass, SV *program_sv, SV *args_sv, SV *attrs_sv, int allow_includes)
502 PREINIT:
503 jq_xs_t *self;
504 ErrorContext ctx;
505 const char *prog;
506 STRLEN prog_len;
507 jv args;
508 CODE:
509 PERL_UNUSED_VAR(klass);
510 101 ▶ if (!SvOK(program_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(program_sv)) { 0 101 101
511 0 croak("program argument required");
512 }
513 101 prog = SvPV(program_sv, prog_len);
514  
515 101 ▶ if (!allow_includes && jqxs_program_has_imports(prog, prog_len)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!allow_includes && jqxs_program_has_imports(prog, prog_len)) { 18 83 101
if (!allow_includes && jqxs_program_has_imports(prog, prog_len)) { 8 10 18
516 8 croak("jq compile error: include/import is not allowed "
517 "by this JQ::XS object");
518 }
519  
520 93 Newxz(self, 1, jq_xs_t);
521 93 self->state = jq_init();
522 93 ▶ if (!self->state) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->state) { 0 93 93
523 0 Safefree(self);
524 0 croak("jq_init failed");
525 }
526  
527 /* Clear the callback slots before anything can consult them. jq 1.6's
528 * jq_init() mallocs the jq_state without initialising these three, so a
529 * JQ_SYSTEM=1 build against it starts out with whatever the allocator
530 * last left in that block -- and if that was a torn-down JQ::XS object,
531 * it is one of these trampolines paired with a freed jq_xs_t, which
532 * jq_next() will happily call. jq 1.7 and later zero them in jq_init and
533 * do not need this; it costs three stores either way. */
534 93 jq_set_input_cb(self->state, NULL, NULL);
535 93 jq_set_debug_cb(self->state, NULL, NULL);
536 #ifdef JQ_XS_HAVE_STDERR_CB
537 93 jq_set_stderr_cb(self->state, NULL, NULL);
538 #endif
539  
540 93 self->program = newSVsv(program_sv);
541 93 self->output_opts = newRV_noinc((SV *)newHV());
542  
543 /* Attributes have to be in place before the program is compiled:
544 * JQ_LIBRARY_PATH is what the linker searches while it compiles. */
545 93 ▶ if (SvOK(attrs_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(attrs_sv)) { 93 0 93
546 93 HV *attrs = (HV *)SvRV(attrs_sv);
547 HE *he;
548 93 hv_iterinit(attrs);
549 187 ▶ while ((he = hv_iternext(attrs)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(attrs)) != NULL) { 94 93 187
550 94 SV *key_sv = hv_iterkeysv(he);
551 STRLEN klen;
552 94 char *kstr = SvPVutf8(key_sv, klen);
553 94 jq_set_attr(self->state, jv_string_sized(kstr, klen),
554 sv_to_jv(aTHX_ HeVAL(he)));
555 }
556 }
557  
558 /* jq_compile_args takes the named arguments as an object; $ARGS is just
559 * one more of them, which is how the jq command line builds it too. */
560 93 ▶ args = SvOK(args_sv) ? sv_to_jv(aTHX_ args_sv) : jv_object();
Branch: 1 branch
BranchTrueFalseTotal
args = SvOK(args_sv) ? sv_to_jv(aTHX_ args_sv) : jv_object(); 93 0 93
561  
562 93 ctx.buffer = sv_2mortal(newSVpv("", 0));
563 93 jq_set_error_cb(self->state, error_callback, &ctx);
564  
565 93 jq_compile_args(self->state, prog, args);
566  
567 93 jq_set_error_cb(self->state, NULL, NULL);
568  
569 93 ▶ if (SvCUR(ctx.buffer) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(ctx.buffer) > 0) { 2 91 93
570 2 jq_teardown(&self->state);
571 2 SvREFCNT_dec(self->program);
572 2 SvREFCNT_dec(self->output_opts);
573 2 Safefree(self);
574 4 croak("jq compile error: %s", SvPV_nolen(ctx.buffer));
575 }
576 91 RETVAL = self;
577 OUTPUT:
578 RETVAL
579  
580 SV *
581 program(JQ::XS self)
582 CODE:
583 1 RETVAL = newSVsv(self->program);
584 OUTPUT:
585 RETVAL
586  
587 void
588 _set_output(JQ::XS self, SV *opts, int dump_flags, int raw_output)
589 CODE:
590 91 SvREFCNT_dec(self->output_opts);
591 91 self->output_opts = newSVsv(opts);
592 91 self->dump_flags = dump_flags;
593 91 self->raw_output = raw_output;
594  
595 SV *
596 output_options(JQ::XS self)
597 PREINIT:
598 HV *copy;
599 CODE:
600 /* A copy, so that mutating what a caller got back cannot desynchronise
601 * the hash from the flags compiled out of it. */
602 5 copy = newHVhv((HV *)SvRV(self->output_opts));
603 5 RETVAL = newRV_noinc((SV *)copy);
604 OUTPUT:
605 RETVAL
606  
607 int
608 flags(JQ::XS self, ...)
609 CODE:
610 6 RETVAL = self->start_flags;
611 6 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 3 3 6
612 3 ▶ self->start_flags = SvOK(ST(1)) ? (int)SvIV(ST(1)) : 0;
Branch: 1 branch
BranchTrueFalseTotal
self->start_flags = SvOK(ST(1)) ? (int)SvIV(ST(1)) : 0; 3 0 3
613 }
614 OUTPUT:
615 RETVAL
616  
617 int
618 die_on_halt_error(JQ::XS self, ...)
619 CODE:
620 3 RETVAL = self->die_on_halt_error;
621 3 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 2 1 3
622 2 self->die_on_halt_error = SvTRUE(ST(1)) ? 1 : 0;
623 }
624 OUTPUT:
625 RETVAL
626  
627 void
628 set_debug_cb(JQ::XS self, SV *code = &PL_sv_undef)
629 CODE:
630 4 jqxs_store_cb(aTHX_ &self->debug_cb, code, "debug callback");
631 3 ▶ if (self->debug_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->debug_cb) { 2 1 3
632 2 jq_set_debug_cb(self->state, jqxs_debug_cb, self);
633 } else {
634 1 jq_set_debug_cb(self->state, NULL, NULL);
635 }
636  
637 void
638 set_stderr_cb(JQ::XS self, SV *code = &PL_sv_undef)
639 CODE:
640 #ifdef JQ_XS_HAVE_STDERR_CB
641 1 jqxs_store_cb(aTHX_ &self->stderr_cb, code, "stderr callback");
642 1 ▶ if (self->stderr_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->stderr_cb) { 1 0 1
643 1 jq_set_stderr_cb(self->state, jqxs_stderr_cb, self);
644 } else {
645 0 jq_set_stderr_cb(self->state, NULL, NULL);
646 }
647 #else
648 PERL_UNUSED_VAR(self);
649 /* Disconnecting it is what an unsupporting libjq already does. */
650 if (SvOK(code)) {
651 croak("this JQ::XS was built against a libjq with no jq_set_stderr_cb "
652 "(jq 1.7 added it), so the stderr builtin cannot be served");
653 }
654 #endif
655  
656 void
657 set_input_cb(JQ::XS self, SV *code = &PL_sv_undef)
658 CODE:
659 7 jqxs_store_cb(aTHX_ &self->input_cb, code, "input callback");
660 7 ▶ if (self->input_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->input_cb) { 7 0 7
661 7 jq_set_input_cb(self->state, jqxs_input_cb, self);
662 } else {
663 0 jq_set_input_cb(self->state, NULL, NULL);
664 }
665  
666 void
667 set_attr(JQ::XS self, SV *name, SV *value)
668 PREINIT:
669 STRLEN len;
670 char *str;
671 CODE:
672 2 str = SvPVutf8(name, len);
673 2 jq_set_attr(self->state, jv_string_sized(str, len), sv_to_jv(aTHX_ value));
674  
675 SV *
676 attr(JQ::XS self, SV *name)
677 PREINIT:
678 STRLEN len;
679 char *str;
680 jv value;
681 CODE:
682 5 str = SvPVutf8(name, len);
683 5 value = jq_get_attr(self->state, jv_string_sized(str, len));
684 /* An attribute that was never set comes back invalid, which jv_to_sv maps
685 * to undef -- the same as an attribute explicitly set to null. */
686 5 RETVAL = jv_to_sv(aTHX_ value);
687 OUTPUT:
688 RETVAL
689  
690 int
691 halted(JQ::XS self)
692 CODE:
693 5 ▶ RETVAL = self->halted;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->halted; 0 5 5
694 OUTPUT:
695 RETVAL
696  
697 SV *
698 exit_code(JQ::XS self)
699 CODE:
700 2 ▶ RETVAL = self->exit_code ? newSVsv(self->exit_code) : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->exit_code ? newSVsv(self->exit_code) : newSV(0); 1 1 2
701 OUTPUT:
702 RETVAL
703  
704 SV *
705 error_message(JQ::XS self)
706 CODE:
707 3 ▶ RETVAL = self->error_message ? newSVsv(self->error_message) : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->error_message ? newSVsv(self->error_message) : newSV(0); 3 0 3
708 OUTPUT:
709 RETVAL
710  
711 void
712 dump_disassembly(JQ::XS self, int indent = 0)
713 CODE:
714 /* jq prints this with C printf() on the process's stdout, which is not
715 * Perl's STDOUT handle: flush Perl's first so anything already written
716 * through it comes out ahead of the disassembly, and fflush(NULL)
717 * afterwards to push out the disassembly itself without having to name
718 * a FILE * that PerlIO may have redefined. */
719 0 PerlIO_flush(PerlIO_stdout());
720 0 jq_dump_disassembly(self->state, indent);
721 0 fflush(NULL);
722  
723 AV *
724 _xs_process(JQ::XS self, SV *input_sv, int as_json, int dump_flags, int raw_output)
725 PREINIT:
726 jv input;
727 jv res;
728 AV *results;
729 SV *output_sv;
730 int halted;
731 CODE:
732 /* -1 means "whatever the object was configured with". */
733 94 ▶ if (dump_flags < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (dump_flags < 0) { 93 1 94
734 93 dump_flags = self->dump_flags;
735 93 raw_output = self->raw_output;
736 }
737  
738 94 ▶ if (self->running) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->running) { 1 93 94
739 1 croak("JQ::XS: cannot reenter process() on a filter that is already running");
740 }
741  
742 /* Parse/convert input */
743 93 ▶ if (as_json) {
Branch: 1 branch
BranchTrueFalseTotal
if (as_json) { 18 75 93
744 STRLEN len;
745 18 const char *json_str = SvPVutf8(input_sv, len);
746 18 input = jv_parse_sized(json_str, len);
747 18 ▶ if (!jv_is_valid(input)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!jv_is_valid(input)) { 1 17 18
748 1 jv err_msg = jv_invalid_get_msg(input);
749 SV *errsv;
750 1 ▶ if (jv_get_kind(err_msg) == JV_KIND_STRING) {
Branch: 1 branch
BranchTrueFalseTotal
if (jv_get_kind(err_msg) == JV_KIND_STRING) { 1 0 1
751 1 errsv = sv_2mortal(newSVpv(jv_string_value(err_msg), 0));
752 } else {
753 0 errsv = sv_2mortal(newSVpv("unknown parse error", 0));
754 }
755 1 jv_free(err_msg);
756 1 croak("Invalid JSON: %s", SvPV_nolen(errsv));
757 }
758 } else {
759 75 input = sv_to_jv(aTHX_ input_sv);
760 }
761  
762 /* Forget what the previous run halted with */
763 92 self->halted = 0;
764 92 jqxs_clear_sv(aTHX_ &self->exit_code);
765 92 jqxs_clear_sv(aTHX_ &self->error_message);
766 92 jqxs_clear_sv(aTHX_ &self->cb_error);
767  
768 /* Run jq filter */
769 92 self->running = 1;
770 92 jq_start(self->state, input, self->start_flags);
771  
772 /* Collect results */
773 92 results = newAV();
774 92 sv_2mortal((SV *)results);
775 184 ▶ while (jv_is_valid(res = jq_next(self->state))) {
Branch: 1 branch
BranchTrueFalseTotal
while (jv_is_valid(res = jq_next(self->state))) { 92 92 184
776 92 ▶ if (!as_json) {
Branch: 1 branch
BranchTrueFalseTotal
if (!as_json) { 73 19 92
777 73 output_sv = jv_to_sv(aTHX_ res);
778 73 av_push(results, output_sv);
779 19 ▶ } else if (raw_output && jv_get_kind(res) == JV_KIND_STRING
Branch: 2 branches
BranchTrueFalseTotal
} else if (raw_output && jv_get_kind(res) == JV_KIND_STRING 5 14 19
} else if (raw_output && jv_get_kind(res) == JV_KIND_STRING 3 2 5
780 5 ▶ && !(dump_flags & JV_PRINT_ASCII)) {
Branch: 1 branch
BranchTrueFalseTotal
&& !(dump_flags & JV_PRINT_ASCII)) { 2 1 3
781 /* jq -r: a string result is its own output. Sized, because unlike a
782 * JSON dump -- where a NUL comes back escaped -- a raw string can
783 * contain one. "jq -r -a" deliberately does not come through here:
784 * it dumps the string escaped and quoted, and this follows suit. */
785 2 jv sized = jv_copy(res);
786 2 int len = jv_string_length_bytes(sized);
787 2 output_sv = newSVpvn(jv_string_value(res), len);
788 2 SvUTF8_on(output_sv);
789 2 av_push(results, output_sv);
790 2 jv_free(res);
791 } else {
792 17 jv json_str_jv = jv_dump_string(res, dump_flags);
793 17 jv sized = jv_copy(json_str_jv);
794 17 int len = jv_string_length_bytes(sized);
795 17 output_sv = newSVpvn(jv_string_value(json_str_jv), len);
796 17 SvUTF8_on(output_sv);
797 17 av_push(results, output_sv);
798 17 jv_free(json_str_jv);
799 }
800 }
801  
802 /* halt and halt_error stop the program without producing a result, so the
803 * loop above ends on an invalid just as an uncaught error does. jq's own
804 * main.c checks for the halt first, because that invalid may carry a
805 * message belonging to the halt rather than to a failed filter. */
806 92 halted = jq_halted(self->state);
807 92 ▶ if (halted) {
Branch: 1 branch
BranchTrueFalseTotal
if (halted) { 6 86 92
808 6 self->halted = 1;
809 6 self->exit_code = jv_to_sv(aTHX_ jq_get_exit_code(self->state));
810 6 self->error_message = jv_to_sv(aTHX_ jq_get_error_message(self->state));
811 6 jv_free(res);
812 86 ▶ } else if (!jv_is_valid(res)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!jv_is_valid(res)) { 86 0 86
813 86 ▶ if (jv_invalid_has_msg(jv_copy(res))) {
Branch: 1 branch
BranchTrueFalseTotal
if (jv_invalid_has_msg(jv_copy(res))) { 4 82 86
814 4 jv err_msg = jv_invalid_get_msg(res);
815 4 jv err_text_jv = jq_format_error(err_msg);
816 SV *errsv;
817 4 ▶ if (jv_get_kind(err_text_jv) == JV_KIND_STRING) {
Branch: 1 branch
BranchTrueFalseTotal
if (jv_get_kind(err_text_jv) == JV_KIND_STRING) { 4 0 4
818 4 errsv = sv_2mortal(newSVpv(jv_string_value(err_text_jv), 0));
819 } else {
820 0 errsv = sv_2mortal(newSVpv("unknown runtime error", 0));
821 }
822 4 jv_free(err_text_jv);
823 4 self->running = 0;
824 4 croak("jq runtime error: %s", SvPV_nolen(errsv));
825 }
826 82 jv_free(res);
827 }
828 88 self->running = 0;
829  
830 /* An exception the debug or stderr callback threw could not be raised
831 * from inside libjq; now that the run is over, it can be. */
832 88 ▶ if (self->cb_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->cb_error) { 1 87 88
833 1 SV *err = sv_2mortal(self->cb_error);
834 1 self->cb_error = NULL;
835 1 croak_sv(err);
836 }
837  
838 /* halt_error is jq's way of failing on purpose. Left to itself it is
839 * silent, so a caller who asked for it gets an exception instead. */
840 87 ▶ if (halted && self->die_on_halt_error && SvOK(self->error_message)) {
Branch: 3 branches
BranchTrueFalseTotal
if (halted && self->die_on_halt_error && SvOK(self->error_message)) { 6 81 87
if (halted && self->die_on_halt_error && SvOK(self->error_message)) { 2 4 6
if (halted && self->die_on_halt_error && SvOK(self->error_message)) { 1 1 2
841 1 croak_sv(sv_2mortal(newSVsv(self->error_message)));
842 }
843  
844 86 RETVAL = results;
845 OUTPUT:
846 RETVAL
847  
848 SV *
849 jq_version()
850 CODE:
851 #ifdef JQ_XS_EMBEDDED
852 3 RETVAL = newSVpvs(JQ_VERSION);
853 #else
854 /* The OS libjq exposes no version, at build time or at run time. */
855 RETVAL = newSV(0);
856 #endif
857 OUTPUT:
858 RETVAL
859  
860 int
861 set_colors(const char *spec)
862 CODE:
863 2 RETVAL = jq_set_colors(spec);
864 OUTPUT:
865 RETVAL
866  
867 SV *
868 features()
869 PREINIT:
870 HV *hv;
871 CODE:
872 /* What this particular build can do, which under JQ_SYSTEM=1 depends on
873 * the libjq it was linked against. */
874 5 hv = newHV();
875 5 hv_stores(hv, "embedded_jq",
876 #ifdef JQ_XS_EMBEDDED
877 newSViv(1)
878 #else
879 newSViv(0)
880 #endif
881 );
882 5 hv_stores(hv, "stderr_cb",
883 #ifdef JQ_XS_HAVE_STDERR_CB
884 newSViv(1)
885 #else
886 newSViv(0)
887 #endif
888 );
889 5 RETVAL = newRV_noinc((SV *)hv);
890 OUTPUT:
891 RETVAL
892  
893 SV *
894 _program_has_imports(SV *program_sv)
895 PREINIT:
896 const char *prog;
897 STRLEN len;
898 CODE:
899 16 prog = SvPV(program_sv, len);
900 16 ▶ RETVAL = boolSV(jqxs_program_has_imports(prog, len));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = boolSV(jqxs_program_has_imports(prog, len)); 8 8 16
901 OUTPUT:
902 RETVAL
903  
904 SV *
905 parse_json(SV *text_sv)
906 PREINIT:
907 STRLEN len;
908 const char *text;
909 jv value;
910 CODE:
911 3 text = SvPVutf8(text_sv, len);
912 3 value = jv_parse_sized(text, len);
913 3 ▶ if (!jv_is_valid(value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!jv_is_valid(value)) { 1 2 3
914 1 jv msg = jv_invalid_get_msg(value);
915 1 ▶ SV *errsv = sv_2mortal(newSVpv(
Branch: 1 branch
BranchTrueFalseTotal
SV *errsv = sv_2mortal(newSVpv( 1 0 1
916 jv_get_kind(msg) == JV_KIND_STRING ? jv_string_value(msg)
917 : "unknown parse error", 0));
918 1 jv_free(msg);
919 1 croak("Invalid JSON: %s", SvPV_nolen(errsv));
920 }
921 2 RETVAL = jv_to_sv(aTHX_ value);
922 OUTPUT:
923 RETVAL
924  
925 void
926 parse_json_stream(SV *text_sv)
927 PREINIT:
928 STRLEN len;
929 const char *text;
930 jv_parser *parser;
931 jv value;
932 PPCODE:
933 /* jq's own reader: one buffer may hold any number of whitespace- or
934 * newline-separated JSON values, which is what "jq ." consumes from a
935 * stream and what jv_parse_sized rejects. */
936 3 text = SvPVutf8(text_sv, len);
937 3 parser = jv_parser_new(0);
938 3 jv_parser_set_buf(parser, text, (int)len, 0);
939  
940 7 ▶ while (jv_is_valid(value = jv_parser_next(parser))) {
Branch: 1 branch
BranchTrueFalseTotal
while (jv_is_valid(value = jv_parser_next(parser))) { 4 3 7
941 4 ▶ XPUSHs(sv_2mortal(jv_to_sv(aTHX_ value)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(jv_to_sv(aTHX_ value))); 0 4 4
942 }
943  
944 3 ▶ if (jv_invalid_has_msg(jv_copy(value))) {
Branch: 1 branch
BranchTrueFalseTotal
if (jv_invalid_has_msg(jv_copy(value))) { 1 2 3
945 1 jv msg = jv_invalid_get_msg(value);
946 1 ▶ SV *errsv = sv_2mortal(newSVpv(
Branch: 1 branch
BranchTrueFalseTotal
SV *errsv = sv_2mortal(newSVpv( 1 0 1
947 jv_get_kind(msg) == JV_KIND_STRING ? jv_string_value(msg)
948 : "unknown parse error", 0));
949 1 jv_free(msg);
950 1 jv_parser_free(parser);
951 1 croak("Invalid JSON: %s", SvPV_nolen(errsv));
952 }
953 2 jv_free(value);
954 2 jv_parser_free(parser);
955  
956 SV *
957 _to_json(SV *data_sv, int dump_flags)
958 PREINIT:
959 jv text;
960 jv sized;
961 int len;
962 CODE:
963 3 text = jv_dump_string(sv_to_jv(aTHX_ data_sv), dump_flags);
964 3 sized = jv_copy(text);
965 3 len = jv_string_length_bytes(sized);
966 3 RETVAL = newSVpvn(jv_string_value(text), len);
967 3 SvUTF8_on(RETVAL);
968 3 jv_free(text);
969 OUTPUT:
970 RETVAL
971  
972 void
973 DESTROY(JQ::XS self)
974 CODE:
975 91 ▶ if (self) {
Branch: 1 branch
BranchTrueFalseTotal
if (self) { 91 0 91
976 91 ▶ if (self->state) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->state) { 91 0 91
977 91 jq_teardown(&self->state);
978 }
979 91 SvREFCNT_dec(self->program);
980 91 SvREFCNT_dec(self->output_opts);
981 91 jqxs_clear_sv(aTHX_ &self->debug_cb);
982 91 jqxs_clear_sv(aTHX_ &self->stderr_cb);
983 91 jqxs_clear_sv(aTHX_ &self->input_cb);
984 91 jqxs_clear_sv(aTHX_ &self->exit_code);
985 91 jqxs_clear_sv(aTHX_ &self->error_message);
986 91 jqxs_clear_sv(aTHX_ &self->cb_error);
987 91 Safefree(self);
988 }
« blib/lib/JQ/XS.pm ↑ Summary .jq-build/stage/include/jv.h »