XS.xs

filter: Statementstmt 93.2% 442 / 474 Branchbran 62.1% 215 / 346 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.1% 657 / 820 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/JSON/Schema/Corvus/XS.pm ↑ Summary
1 /* JSON::Schema::Corvus::XS: the corvus-json-schema C library for Perl.
2 *
3 * Validators, documents and collectors are blessed scalar references holding the C library's handles, freed in
4 * DESTROY. They are not cloned into new ithreads (CLONE_SKIP): there they are unblessed references to undef.
5 *
6 * Nothing here lets a Perl exception unwind through the C library. A custom format or a resolver written in Perl is
7 * called under G_EVAL, and an exception it throws is kept (pending) and thrown once the library call that ran it has
8 * returned and its resources are freed. Perl data is encoded to JSON text (encode_value) before the library is
9 * called, so that a value that has no JSON form croaks before anything is allocated. */
10  
11 #define PERL_NO_GET_CONTEXT
12 #include "EXTERN.h"
13 #include "perl.h"
14 #include "XSUB.h"
15  
16 #include "corvus_json_schema.h"
17  
18 #include <math.h>
19  
20 #define ERROR_CLASS "JSON::Schema::Corvus::XS::Error"
21 #define VALIDATOR_CLASS "JSON::Schema::Corvus::XS::Validator"
22 #define DOCUMENT_CLASS "JSON::Schema::Corvus::XS::Document"
23 #define COLLECTOR_CLASS "JSON::Schema::Corvus::XS::ResultsCollector"
24  
25 /* The deepest Perl structure encode_value writes, as JSON::PP's default max_depth. */
26 #define MAX_ENCODE_DEPTH 512
27  
28 #define MY_CXT_KEY "JSON::Schema::Corvus::XS::_guts" XS_VERSION
29  
30 typedef struct {
31 /* An exception a Perl callback threw during the current library call, to throw when it returns. */
32 SV *pending;
33 } my_cxt_t;
34  
35 START_MY_CXT
36  
37 /* The interpreter a callback belongs to, in a Perl with more than one (ithreads): the library calls back on the thread
38 * that called it, and a callback met on another thread (which cannot happen while validators are not cloned into
39 * new threads) answers without calling Perl. */
40 #ifdef MULTIPLICITY
41 # define THIS_PERL aTHX
42 # define ON_ANOTHER_THREAD(box) ((PerlInterpreter *)PERL_GET_THX != (box)->perl)
43 #else
44 # define THIS_PERL NULL
45 # define ON_ANOTHER_THREAD(box) 0
46 #endif
47  
48 /* A Perl callback handed to the C library: a custom format or the resolver. */
49 typedef struct {
50 PerlInterpreter *perl;
51 SV *code;
52 } callback_box;
53  
54 4 static void set_pending(pTHX_ SV *error) {
55 dMY_CXT;
56 4 ▶ if (!MY_CXT.pending) {
Branch: 1 branch
BranchTrueFalseTotal
if (!MY_CXT.pending) { 4 0 4
57 4 MY_CXT.pending = newSVsv(error);
58 }
59 4 }
60  
61 106 static void clear_pending(pTHX) {
62 dMY_CXT;
63 106 ▶ if (MY_CXT.pending) {
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.pending) { 0 106 106
64 0 SvREFCNT_dec(MY_CXT.pending);
65 0 MY_CXT.pending = NULL;
66 }
67 106 }
68  
69 /* Throws the exception a callback threw during the call just made, if any. */
70 70 static void throw_pending(pTHX) {
71 dMY_CXT;
72 70 SV *error = MY_CXT.pending;
73 70 ▶ if (error) {
Branch: 1 branch
BranchTrueFalseTotal
if (error) { 4 66 70
74 4 MY_CXT.pending = NULL;
75 4 croak_sv(sv_2mortal(error));
76 }
77 66 }
78  
79 /* The class of the exception for a status. */
80 10 static const char *error_class(cjs_status status) {
81 10 switch (status) {
82 4 case CJS_INVALID_UTF8:
83 case CJS_INVALID_JSON:
84 4 return ERROR_CLASS "::InvalidJson";
85 3 case CJS_COMPILATION_FAILED:
86 3 return ERROR_CLASS "::Compilation";
87 1 case CJS_PATTERN_TOO_LARGE:
88 1 return ERROR_CLASS "::PatternTooLarge";
89 1 case CJS_DEPTH_EXCEEDED:
90 1 return ERROR_CLASS "::Depth";
91 1 case CJS_PATTERN_MATCH_LIMIT:
92 1 return ERROR_CLASS "::PatternMatchLimit";
93 0 default:
94 0 return ERROR_CLASS;
95 }
96 }
97  
98 /* Throws the exception for a failed call: an object with the message, the status and, for invalid JSON or UTF-8, the
99 * byte offset at which the text was found to be invalid. */
100 10 static void throw_status(pTHX_ cjs_status status) {
101 10 cjs_str message = cjs_last_error_message();
102 10 HV *error = newHV();
103 10 (void)hv_stores(error, "message", newSVpvn_utf8(message.ptr, message.len, 1));
104 10 (void)hv_stores(error, "status", newSVuv(status));
105 10 ▶ if (status == CJS_INVALID_JSON || status == CJS_INVALID_UTF8) {
Branch: 2 branches
BranchTrueFalseTotal
if (status == CJS_INVALID_JSON || status == CJS_INVALID_UTF8) { 7 3 10
if (status == CJS_INVALID_JSON || status == CJS_INVALID_UTF8) { 1 6 7
106 4 (void)hv_stores(error, "offset", newSVuv(cjs_last_error_offset()));
107 }
108 10 croak_sv(sv_2mortal(sv_bless(newRV_noinc((SV *)error), gv_stashpv(error_class(status), GV_ADD))));
109 }
110  
111 2 static void throw_usage(pTHX_ const char *message) {
112 2 HV *error = newHV();
113 2 (void)hv_stores(error, "message", newSVpv(message, 0));
114 2 (void)hv_stores(error, "status", newSVuv(CJS_INVALID_ARGUMENT));
115 2 croak_sv(sv_2mortal(sv_bless(newRV_noinc((SV *)error), gv_stashpv(ERROR_CLASS, GV_ADD))));
116 }
117  
118 /* The bytes of a text argument: a string with the UTF8 flag gives its UTF-8, and one without gives its bytes, which
119 * the library reads as UTF-8. */
120 90 static const char *text_of(pTHX_ SV *sv, STRLEN *len) {
121 90 ▶ if (!SvOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) { 1 89 90
122 1 throw_usage(aTHX_ "the text is undef");
123 }
124 89 return SvPV(sv, *len);
125 }
126  
127 82 static void *handle_of(pTHX_ SV *sv, const char *class) {
128 82 ▶ if (!sv_isobject(sv) || !sv_derived_from(sv, class)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!sv_isobject(sv) || !sv_derived_from(sv, class)) { 81 1 82
if (!sv_isobject(sv) || !sv_derived_from(sv, class)) { 0 81 81
129 char message[200];
130 1 snprintf(message, sizeof message, "expected a %s", class);
131 1 throw_usage(aTHX_ message);
132 }
133 81 void *handle = INT2PTR(void *, SvIV(SvRV(sv)));
134 81 ▶ if (!handle) {
Branch: 1 branch
BranchTrueFalseTotal
if (!handle) { 0 81 81
135 char message[200];
136 0 snprintf(message, sizeof message, "the %s has been freed", class);
137 0 throw_usage(aTHX_ message);
138 }
139 81 return handle;
140 }
141  
142 43 static SV *wrap(pTHX_ void *handle, const char *class) {
143 43 return sv_setref_pv(newSV(0), class, handle);
144 }
145  
146 /* --- Encoding Perl data as JSON text, by JSON::PP's rules --- */
147  
148 static void encode_value(pTHX_ SV *out, SV *value, int depth);
149  
150 43 static void encode_string(pTHX_ SV *out, const char *p, STRLEN len) {
151 static const char hex[] = "0123456789abcdef";
152 43 STRLEN i, start = 0;
153 43 sv_catpvs(out, "\"");
154 204 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 161 43 204
155 161 unsigned char c = (unsigned char)p[i];
156 161 const char *escape = NULL;
157 char unicode[7];
158 161 ▶ if (c >= 0x20 && c != '"' && c != '\\') {
Branch: 3 branches
BranchTrueFalseTotal
if (c >= 0x20 && c != '"' && c != '\\') { 159 2 161
if (c >= 0x20 && c != '"' && c != '\\') { 158 1 159
if (c >= 0x20 && c != '"' && c != '\\') { 157 1 158
159 157 continue;
160 }
161 4 switch (c) {
162 1 case '"': escape = "\\\""; break;
163 1 case '\\': escape = "\\\\"; break;
164 0 case '\b': escape = "\\b"; break;
165 0 case '\f': escape = "\\f"; break;
166 0 case '\n': escape = "\\n"; break;
167 0 case '\r': escape = "\\r"; break;
168 1 case '\t': escape = "\\t"; break;
169 1 default:
170 1 unicode[0] = '\\'; unicode[1] = 'u'; unicode[2] = '0'; unicode[3] = '0';
171 1 unicode[4] = hex[c >> 4]; unicode[5] = hex[c & 0xf]; unicode[6] = '\0';
172 1 escape = unicode;
173 }
174 4 sv_catpvn(out, p + start, i - start);
175 4 sv_catpv(out, escape);
176 4 start = i + 1;
177 }
178 43 sv_catpvn(out, p + start, len - start);
179 43 sv_catpvs(out, "\"");
180 43 }
181  
182 /* A string value or a name, as UTF-8. */
183 43 static void encode_text_sv(pTHX_ SV *out, SV *sv) {
184 STRLEN len;
185 const char *p;
186 43 SV *copy = sv_2mortal(newSVsv(sv));
187 43 p = SvPVutf8(copy, len);
188 43 encode_string(aTHX_ out, p, len);
189 43 }
190  
191 7 static int compare_keys(const void *a, const void *b) {
192 dTHX;
193 7 SV *x = *(SV *const *)a;
194 7 SV *y = *(SV *const *)b;
195 STRLEN xl, yl;
196 7 ▶ const char *xp = SvPVutf8(x, xl);
Branch: 1 branch
BranchTrueFalseTotal
const char *xp = SvPVutf8(x, xl); 0 7 7
197 7 const char *yp = SvPVutf8(y, yl);
198 7 int c = memcmp(xp, yp, xl < yl ? xl : yl);
199 7 ▶ return c ? c : (xl < yl ? -1 : xl > yl);
Branch: 2 branches
BranchTrueFalseTotal
return c ? c : (xl < yl ? -1 : xl > yl); 0 7 7
return c ? c : (xl < yl ? -1 : xl > yl); 0 0 0
200 }
201  
202 18 static void encode_hash(pTHX_ SV *out, HV *hv, int depth) {
203 18 I32 count = hv_iterinit(hv);
204 SV **keys;
205 HE *entry;
206 18 I32 i = 0;
207 18 ▶ if (count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count == 0) { 0 18 18
208 0 sv_catpvs(out, "{}");
209 0 return;
210 }
211 /* Names in order of their UTF-8 bytes (JSON::PP's canonical order), so that the order of an evaluation's results
212 * does not change with Perl's hash order. */
213 18 Newx(keys, count, SV *);
214 18 SAVEFREEPV(keys);
215 43 ▶ while ((entry = hv_iternext(hv)) && i < count) {
Branch: 2 branches
BranchTrueFalseTotal
while ((entry = hv_iternext(hv)) && i < count) { 25 18 43
while ((entry = hv_iternext(hv)) && i < count) { 25 0 25
216 25 keys[i++] = sv_2mortal(newSVsv(hv_iterkeysv(entry)));
217 }
218 18 count = i;
219 18 qsort(keys, count, sizeof(SV *), compare_keys);
220 18 sv_catpvs(out, "{");
221 43 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 25 18 43
222 25 HE *he = hv_fetch_ent(hv, keys[i], 0, 0);
223 25 ▶ if (i > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (i > 0) { 7 18 25
224 7 sv_catpvs(out, ",");
225 }
226 25 encode_text_sv(aTHX_ out, keys[i]);
227 25 sv_catpvs(out, ":");
228 25 ▶ encode_value(aTHX_ out, he ? HeVAL(he) : &PL_sv_undef, depth + 1);
Branch: 1 branch
BranchTrueFalseTotal
encode_value(aTHX_ out, he ? HeVAL(he) : &PL_sv_undef, depth + 1); 25 0 25
229 }
230 18 sv_catpvs(out, "}");
231 }
232  
233 518 static void encode_array(pTHX_ SV *out, AV *av, int depth) {
234 518 SSize_t i, last = av_len(av);
235 518 sv_catpvs(out, "[");
236 525 ▶ for (i = 0; i <= last; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= last; i++) { 521 4 525
237 521 SV **item = av_fetch(av, i, 0);
238 521 ▶ if (i > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (i > 0) { 3 518 521
239 3 sv_catpvs(out, ",");
240 }
241 521 ▶ encode_value(aTHX_ out, item ? *item : &PL_sv_undef, depth + 1);
Branch: 1 branch
BranchTrueFalseTotal
encode_value(aTHX_ out, item ? *item : &PL_sv_undef, depth + 1); 521 0 521
242 }
243 4 sv_catpvs(out, "]");
244 4 }
245  
246 14 static void encode_number(pTHX_ SV *out, SV *value) {
247 14 ▶ if (SvNOK(value) && !SvIOK(value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvNOK(value) && !SvIOK(value)) { 2 12 14
if (SvNOK(value) && !SvIOK(value)) { 2 0 2
248 2 NV nv = SvNV(value);
249 2 ▶ if (Perl_isinfnan(nv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (Perl_isinfnan(nv)) { 1 1 2
250 1 ▶ croak("JSON::Schema::Corvus::XS: %s has no JSON form", Perl_isnan(nv) ? "NaN" : "an infinite number");
Branch: 1 branch
BranchTrueFalseTotal
croak("JSON::Schema::Corvus::XS: %s has no JSON form", Perl_isnan(nv) ? "NaN" : "an infinite number"); 0 1 1
251 }
252 }
253 /* As JSON::PP writes a number: Perl's own text for it. */
254 {
255 STRLEN len;
256 13 SV *copy = sv_2mortal(newSVsv(value));
257 13 const char *p = SvPV(copy, len);
258 13 sv_catpvn(out, p, len);
259 }
260 13 }
261  
262 /* A Math::BigInt or Math::BigFloat, as the text its bstr method gives, which must be a JSON number. */
263 5 static void encode_bignum(pTHX_ SV *out, SV *value) {
264 5 dSP;
265 int count;
266 5 STRLEN len, i = 0;
267 const char *p;
268 SV *text;
269 5 bool digits = false;
270 5 ENTER;
271 5 SAVETMPS;
272 5 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 5 5
273 5 ▶ XPUSHs(value);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(value); 0 5 5
274 5 PUTBACK;
275 5 count = call_method("bstr", G_SCALAR);
276 5 SPAGAIN;
277 5 ▶ text = count == 1 ? newSVsv(POPs) : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
text = count == 1 ? newSVsv(POPs) : newSV(0); 5 0 5
278 5 PUTBACK;
279 5 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 5 0 5
280 5 LEAVE;
281 5 sv_2mortal(text);
282 10 ▶ p = SvOK(text) ? SvPV(text, len) : (len = 0, "");
Branch: 1 branch
BranchTrueFalseTotal
p = SvOK(text) ? SvPV(text, len) : (len = 0, ""); 5 0 5
283 /* -?digits(.digits)?([eE][+-]?digits)? */
284 5 ▶ if (i < len && p[i] == '-') i++;
Branch: 2 branches
BranchTrueFalseTotal
if (i < len && p[i] == '-') i++; 5 0 5
if (i < len && p[i] == '-') i++; 1 4 5
285 217 ▶ while (i < len && isDIGIT(p[i])) { i++; digits = true; }
Branch: 2 branches
BranchTrueFalseTotal
while (i < len && isDIGIT(p[i])) { i++; digits = true; } 214 3 217
while (i < len && isDIGIT(p[i])) { i++; digits = true; } 212 2 214
286 5 ▶ if (digits && i < len && p[i] == '.') {
Branch: 3 branches
BranchTrueFalseTotal
if (digits && i < len && p[i] == '.') { 4 1 5
if (digits && i < len && p[i] == '.') { 1 3 4
if (digits && i < len && p[i] == '.') { 1 0 1
287 1 bool fraction = false;
288 1 i++;
289 2 ▶ while (i < len && isDIGIT(p[i])) { i++; fraction = true; }
Branch: 2 branches
BranchTrueFalseTotal
while (i < len && isDIGIT(p[i])) { i++; fraction = true; } 1 1 2
while (i < len && isDIGIT(p[i])) { i++; fraction = true; } 1 0 1
290 1 digits = fraction;
291 }
292 5 ▶ if (digits && i < len && (p[i] == 'e' || p[i] == 'E')) {
Branch: 4 branches
BranchTrueFalseTotal
if (digits && i < len && (p[i] == 'e' || p[i] == 'E')) { 4 1 5
if (digits && i < len && (p[i] == 'e' || p[i] == 'E')) { 0 4 4
if (digits && i < len && (p[i] == 'e' || p[i] == 'E')) { 0 0 0
if (digits && i < len && (p[i] == 'e' || p[i] == 'E')) { 0 0 0
293 0 bool exponent = false;
294 0 i++;
295 0 ▶ if (i < len && (p[i] == '+' || p[i] == '-')) i++;
Branch: 3 branches
BranchTrueFalseTotal
if (i < len && (p[i] == '+' || p[i] == '-')) i++; 0 0 0
if (i < len && (p[i] == '+' || p[i] == '-')) i++; 0 0 0
if (i < len && (p[i] == '+' || p[i] == '-')) i++; 0 0 0
296 0 ▶ while (i < len && isDIGIT(p[i])) { i++; exponent = true; }
Branch: 2 branches
BranchTrueFalseTotal
while (i < len && isDIGIT(p[i])) { i++; exponent = true; } 0 0 0
while (i < len && isDIGIT(p[i])) { i++; exponent = true; } 0 0 0
297 0 digits = exponent;
298 }
299 5 ▶ if (!digits || i != len) {
Branch: 2 branches
BranchTrueFalseTotal
if (!digits || i != len) { 4 1 5
if (!digits || i != len) { 0 4 4
300 1 ▶ croak("JSON::Schema::Corvus::XS: the %s %" SVf " has no JSON form", HvNAME(SvSTASH(SvRV(value))), SVfARG(text));
Branch: 6 branches
BranchTrueFalseTotal
croak("JSON::Schema::Corvus::XS: the %s %" SVf " has no JSON form", HvNAME(SvSTASH(SvRV(value))), SVfARG(text)); 1 0 1
croak("JSON::Schema::Corvus::XS: the %s %" SVf " has no JSON form", HvNAME(SvSTASH(SvRV(value))), SVfARG(text)); 1 0 1
croak("JSON::Schema::Corvus::XS: the %s %" SVf " has no JSON form", HvNAME(SvSTASH(SvRV(value))), SVfARG(text)); 0 1 1
croak("JSON::Schema::Corvus::XS: the %s %" SVf " has no JSON form", HvNAME(SvSTASH(SvRV(value))), SVfARG(text)); 0 0 0
croak("JSON::Schema::Corvus::XS: the %s %" SVf " has no JSON form", HvNAME(SvSTASH(SvRV(value))), SVfARG(text)); 1 0 1
croak("JSON::Schema::Corvus::XS: the %s %" SVf " has no JSON form", HvNAME(SvSTASH(SvRV(value))), SVfARG(text)); 0 1 1
301 }
302 4 sv_catpvn(out, p, len);
303 4 }
304  
305 588 static void encode_value(pTHX_ SV *out, SV *value, int depth) {
306 U32 flags;
307 588 ▶ if (depth > MAX_ENCODE_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
if (depth > MAX_ENCODE_DEPTH) { 1 587 588
308 1 croak("JSON::Schema::Corvus::XS: the data is nested more than %d deep (or holds a cycle)", MAX_ENCODE_DEPTH);
309 }
310 587 SvGETMAGIC(value);
311 587 ▶ if (!SvOK(value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(value)) { 2 585 587
312 2 sv_catpvs(out, "null");
313 2 return;
314 }
315 #ifdef SvIsBOOL
316 585 ▶ if (SvIsBOOL(value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIsBOOL(value)) { 2 583 585
317 2 ▶ if (SvTRUE_nomg(value)) sv_catpvs(out, "true"); else sv_catpvs(out, "false");
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE_nomg(value)) sv_catpvs(out, "true"); else sv_catpvs(out, "false"); 1 1 2
318 2 return;
319 }
320 #endif
321 583 ▶ if (SvROK(value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(value)) { 551 32 583
322 551 SV *target = SvRV(value);
323 551 ▶ if (SvOBJECT(target)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOBJECT(target)) { 11 540 551
324 11 HV *stash = SvSTASH(target);
325 GV *to_json;
326 11 ▶ if (sv_derived_from(value, "JSON::PP::Boolean")) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(value, "JSON::PP::Boolean")) { 4 7 11
327 4 ▶ if (SvTRUE(value)) sv_catpvs(out, "true"); else sv_catpvs(out, "false");
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(value)) sv_catpvs(out, "true"); else sv_catpvs(out, "false"); 2 2 4
328 4 return;
329 }
330 /* Math::BigInt and Math::BigFloat are numbers, written exactly, as JSON::PP writes them with allow_bignum. */
331 7 ▶ if (sv_derived_from(value, "Math::BigInt") || sv_derived_from(value, "Math::BigFloat")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_derived_from(value, "Math::BigInt") || sv_derived_from(value, "Math::BigFloat")) { 2 5 7
if (sv_derived_from(value, "Math::BigInt") || sv_derived_from(value, "Math::BigFloat")) { 0 2 2
332 5 encode_bignum(aTHX_ out, value);
333 4 return;
334 }
335 2 to_json = gv_fetchmethod_autoload(stash, "TO_JSON", 0);
336 2 ▶ if (to_json) {
Branch: 1 branch
BranchTrueFalseTotal
if (to_json) { 1 1 2
337 1 dSP;
338 int count;
339 SV *result;
340 1 ENTER;
341 1 SAVETMPS;
342 1 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1 1
343 1 ▶ XPUSHs(value);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(value); 0 1 1
344 1 PUTBACK;
345 1 count = call_sv(MUTABLE_SV(GvCV(to_json)), G_SCALAR);
346 1 SPAGAIN;
347 1 ▶ result = count == 1 ? newSVsv(POPs) : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
result = count == 1 ? newSVsv(POPs) : newSV(0); 1 0 1
348 1 PUTBACK;
349 1 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 1 0 1
350 1 LEAVE;
351 1 sv_2mortal(result);
352 1 ▶ if (SvROK(result) && SvRV(result) == target) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(result) && SvRV(result) == target) { 1 0 1
if (SvROK(result) && SvRV(result) == target) { 0 1 1
353 0 ▶ croak("JSON::Schema::Corvus::XS: %s::TO_JSON returned the object itself", HvNAME(stash));
Branch: 6 branches
BranchTrueFalseTotal
croak("JSON::Schema::Corvus::XS: %s::TO_JSON returned the object itself", HvNAME(stash)); 0 0 0
croak("JSON::Schema::Corvus::XS: %s::TO_JSON returned the object itself", HvNAME(stash)); 0 0 0
croak("JSON::Schema::Corvus::XS: %s::TO_JSON returned the object itself", HvNAME(stash)); 0 0 0
croak("JSON::Schema::Corvus::XS: %s::TO_JSON returned the object itself", HvNAME(stash)); 0 0 0
croak("JSON::Schema::Corvus::XS: %s::TO_JSON returned the object itself", HvNAME(stash)); 0 0 0
croak("JSON::Schema::Corvus::XS: %s::TO_JSON returned the object itself", HvNAME(stash)); 0 0 0
354 }
355 1 encode_value(aTHX_ out, result, depth + 1);
356 1 return;
357 }
358 1 ▶ croak("JSON::Schema::Corvus::XS: an object of class %s has no JSON form (it has no TO_JSON method)",
Branch: 6 branches
BranchTrueFalseTotal
croak("JSON::Schema::Corvus::XS: an object of class %s has no JSON form (it has no TO_JSON method)", 1 0 1
croak("JSON::Schema::Corvus::XS: an object of class %s has no JSON form (it has no TO_JSON method)", 1 0 1
croak("JSON::Schema::Corvus::XS: an object of class %s has no JSON form (it has no TO_JSON method)", 0 1 1
croak("JSON::Schema::Corvus::XS: an object of class %s has no JSON form (it has no TO_JSON method)", 0 0 0
croak("JSON::Schema::Corvus::XS: an object of class %s has no JSON form (it has no TO_JSON method)", 1 0 1
croak("JSON::Schema::Corvus::XS: an object of class %s has no JSON form (it has no TO_JSON method)", 0 1 1
359 HvNAME(stash));
360 }
361 540 switch (SvTYPE(target)) {
362 18 case SVt_PVHV:
363 18 encode_hash(aTHX_ out, (HV *)target, depth);
364 18 return;
365 518 case SVt_PVAV:
366 518 encode_array(aTHX_ out, (AV *)target, depth);
367 4 return;
368 4 default:
369 /* \1 and \0 are true and false, as JSON::PP has them. */
370 4 ▶ if (SvTYPE(target) < SVt_PVAV && !SvROK(target) && SvOK(target)) {
Branch: 3 branches
BranchTrueFalseTotal
if (SvTYPE(target) < SVt_PVAV && !SvROK(target) && SvOK(target)) { 2 2 4
if (SvTYPE(target) < SVt_PVAV && !SvROK(target) && SvOK(target)) { 2 0 2
if (SvTYPE(target) < SVt_PVAV && !SvROK(target) && SvOK(target)) { 2 0 2
371 STRLEN len;
372 2 const char *p = SvPV(target, len);
373 3 ▶ if (len == 1 && p[0] == '1') { sv_catpvs(out, "true"); return; }
Branch: 2 branches
BranchTrueFalseTotal
if (len == 1 && p[0] == '1') { sv_catpvs(out, "true"); return; } 2 0 2
if (len == 1 && p[0] == '1') { sv_catpvs(out, "true"); return; } 1 1 2
374 1 ▶ if (len == 1 && p[0] == '0') { sv_catpvs(out, "false"); return; }
Branch: 2 branches
BranchTrueFalseTotal
if (len == 1 && p[0] == '0') { sv_catpvs(out, "false"); return; } 1 0 1
if (len == 1 && p[0] == '0') { sv_catpvs(out, "false"); return; } 1 0 1
375 }
376 2 croak("JSON::Schema::Corvus::XS: a reference that is not to a hash or an array has no JSON form "
377 "(\\1 and \\0 are true and false)");
378 }
379 }
380 /* As JSON::PP: a scalar with a number and no string is a number, and anything else is a string. */
381 32 flags = SvFLAGS(value);
382 32 ▶ if ((flags & (SVp_IOK | SVp_NOK)) && !(flags & SVp_POK)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((flags & (SVp_IOK | SVp_NOK)) && !(flags & SVp_POK)) { 15 17 32
if ((flags & (SVp_IOK | SVp_NOK)) && !(flags & SVp_POK)) { 14 1 15
383 14 encode_number(aTHX_ out, value);
384 13 return;
385 }
386 18 encode_text_sv(aTHX_ out, value);
387 }
388  
389 /* Perl data as JSON text, a mortal SV of UTF-8 bytes. */
390 41 static SV *encode_data(pTHX_ SV *data) {
391 41 SV *out = sv_2mortal(newSVpvs(""));
392 41 encode_value(aTHX_ out, data, 0);
393 35 return out;
394 }
395  
396 /* --- Callbacks --- */
397  
398 12 static callback_box *new_box(pTHX_ SV *code) {
399 callback_box *box;
400 12 Newx(box, 1, callback_box);
401 12 box->perl = THIS_PERL;
402 12 box->code = newSVsv(code);
403 12 return box;
404 }
405  
406 12 static void free_box(void *user_data) {
407 12 callback_box *box = (callback_box *)user_data;
408 12 ▶ if (box) {
Branch: 1 branch
BranchTrueFalseTotal
if (box) { 12 0 12
409 dTHXa(box->perl);
410 12 SvREFCNT_dec(box->code);
411 12 Safefree(box);
412 }
413 12 }
414  
415 7 static bool call_format(void *user_data, const char *value, size_t len) {
416 7 callback_box *box = (callback_box *)user_data;
417 7 bool result = false;
418 int count;
419 dTHXa(box->perl);
420 if (ON_ANOTHER_THREAD(box)) {
421 return false;
422 }
423 {
424 7 dSP;
425 7 ENTER;
426 7 SAVETMPS;
427 7 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 7 7
428 7 ▶ XPUSHs(sv_2mortal(newSVpvn_utf8(value, len, 1)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn_utf8(value, len, 1))); 0 7 7
429 7 PUTBACK;
430 7 count = call_sv(box->code, G_SCALAR | G_EVAL);
431 7 SPAGAIN;
432 7 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 7 0 7
if (SvTRUE(ERRSV)) { 2 5 7
433 2 ▶ set_pending(aTHX_ ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
set_pending(aTHX_ ERRSV); 2 0 2
434 2 ▶ if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) (void)POPs; 2 0 2
435 }
436 5 ▶ else if (count == 1) {
Branch: 1 branch
BranchTrueFalseTotal
else if (count == 1) { 5 0 5
437 /* Popped first: before 5.32, SvTRUE evaluates its argument more than once. */
438 5 SV *answer = POPs;
439 5 result = SvTRUE(answer);
440 }
441 7 PUTBACK;
442 7 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 7 0 7
443 7 LEAVE;
444 }
445 7 return result;
446 }
447  
448 7 static cjs_status call_resolver(void *user_data, const char *uri, size_t uri_len, cjs_resolved *out) {
449 7 callback_box *box = (callback_box *)user_data;
450 7 cjs_status status = CJS_OK;
451 int count;
452 dTHXa(box->perl);
453 if (ON_ANOTHER_THREAD(box)) {
454 static const char message[] = "the resolver was called on another thread";
455 cjs_resolved_set_error(out, message, sizeof message - 1);
456 return CJS_COMPILATION_FAILED;
457 }
458 {
459 7 dSP;
460 7 ENTER;
461 7 SAVETMPS;
462 7 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 7 7
463 7 ▶ XPUSHs(sv_2mortal(newSVpvn_utf8(uri, uri_len, 1)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn_utf8(uri, uri_len, 1))); 0 7 7
464 7 PUTBACK;
465 7 count = call_sv(box->code, G_SCALAR | G_EVAL);
466 7 SPAGAIN;
467 7 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 7 0 7
if (SvTRUE(ERRSV)) { 1 6 7
468 static const char message[] = "the resolver died";
469 1 ▶ set_pending(aTHX_ ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
set_pending(aTHX_ ERRSV); 1 0 1
470 1 ▶ if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) (void)POPs; 1 0 1
471 1 cjs_resolved_set_error(out, message, sizeof message - 1);
472 1 status = CJS_COMPILATION_FAILED;
473 }
474 6 ▶ else if (count == 1) {
Branch: 1 branch
BranchTrueFalseTotal
else if (count == 1) { 6 0 6
475 6 SV *document = POPs;
476 6 PUTBACK;
477 6 ▶ if (SvOK(document)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(document)) { 5 1 6
478 STRLEN len;
479 const char *text;
480 7 ▶ if (SvROK(document) && !sv_isobject(document)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(document) && !sv_isobject(document)) { 2 3 5
if (SvROK(document) && !sv_isobject(document)) { 2 0 2
481 /* Perl data, encoded under G_EVAL: data with no JSON form fails the compilation, and its message is
482 * thrown when the compilation returns. */
483 int encoded;
484 SV *json;
485 2 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2 2
486 2 ▶ XPUSHs(document);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(document); 0 2 2
487 2 PUTBACK;
488 2 encoded = call_pv("JSON::Schema::Corvus::XS::encode_json", G_SCALAR | G_EVAL);
489 2 SPAGAIN;
490 2 ▶ json = encoded == 1 ? POPs : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
json = encoded == 1 ? POPs : &PL_sv_undef; 2 0 2
491 2 PUTBACK;
492 2 ▶ if (SvTRUE(ERRSV) || !SvOK(json)) {
Branch: 3 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV) || !SvOK(json)) { 2 0 2
if (SvTRUE(ERRSV) || !SvOK(json)) { 1 1 2
if (SvTRUE(ERRSV) || !SvOK(json)) { 0 1 1
493 static const char message[] = "the resolver returned data that has no JSON form";
494 1 ▶ set_pending(aTHX_ ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
set_pending(aTHX_ ERRSV); 1 0 1
495 1 cjs_resolved_set_error(out, message, sizeof message - 1);
496 1 status = CJS_COMPILATION_FAILED;
497 }
498 else {
499 1 text = SvPV(json, len);
500 1 status = cjs_resolved_set_json(out, text, len);
501 }
502 }
503 else {
504 3 text = SvPV(document, len);
505 3 status = cjs_resolved_set_json(out, text, len);
506 }
507 }
508 6 SPAGAIN;
509 }
510 7 PUTBACK;
511 7 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 7 0 7
512 7 LEAVE;
513 }
514 7 return status;
515 }
516  
517 /* --- Options --- */
518  
519 430 static SV *option(pTHX_ HV *options, const char *name) {
520 430 SV **value = hv_fetch(options, name, strlen(name), 0);
521 430 ▶ return value && SvOK(*value) ? *value : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return value && SvOK(*value) ? *value : NULL; 31 399 430
return value && SvOK(*value) ? *value : NULL; 31 0 31
522 }
523  
524 /* Builds the C library's options from the hash the Perl layer has checked. Croaks (freeing them) on a bad value. */
525 43 static cjs_options *build_options(pTHX_ HV *hv) {
526 43 cjs_options *options = cjs_options_new();
527 43 cjs_status status = CJS_OK;
528 SV *value;
529 43 ▶ if (!options) {
Branch: 1 branch
BranchTrueFalseTotal
if (!options) { 0 43 43
530 0 throw_status(aTHX_ CJS_PANIC);
531 }
532 43 ▶ if ((value = option(aTHX_ hv, "default_dialect")) && status == CJS_OK) {
Branch: 2 branches
BranchTrueFalseTotal
if ((value = option(aTHX_ hv, "default_dialect")) && status == CJS_OK) { 5 38 43
if ((value = option(aTHX_ hv, "default_dialect")) && status == CJS_OK) { 5 0 5
533 5 status = cjs_options_set_default_dialect(options, (cjs_dialect)SvUV(value));
534 }
535 43 ▶ if ((value = option(aTHX_ hv, "assert_format")) && status == CJS_OK) {
Branch: 2 branches
BranchTrueFalseTotal
if ((value = option(aTHX_ hv, "assert_format")) && status == CJS_OK) { 6 37 43
if ((value = option(aTHX_ hv, "assert_format")) && status == CJS_OK) { 6 0 6
536 6 ▶ status = cjs_options_set_assert_format(options, SvTRUE(value) ? CJS_TRUE : CJS_FALSE);
Branch: 1 branch
BranchTrueFalseTotal
status = cjs_options_set_assert_format(options, SvTRUE(value) ? CJS_TRUE : CJS_FALSE); 6 0 6
537 }
538 43 ▶ if ((value = option(aTHX_ hv, "assert_format_in_legacy_drafts")) && status == CJS_OK) {
Branch: 2 branches
BranchTrueFalseTotal
if ((value = option(aTHX_ hv, "assert_format_in_legacy_drafts")) && status == CJS_OK) { 1 42 43
if ((value = option(aTHX_ hv, "assert_format_in_legacy_drafts")) && status == CJS_OK) { 1 0 1
539 1 status = cjs_options_set_assert_format_in_legacy_drafts(options, SvTRUE(value));
540 }
541 43 ▶ if ((value = option(aTHX_ hv, "assert_content")) && status == CJS_OK) {
Branch: 2 branches
BranchTrueFalseTotal
if ((value = option(aTHX_ hv, "assert_content")) && status == CJS_OK) { 1 42 43
if ((value = option(aTHX_ hv, "assert_content")) && status == CJS_OK) { 1 0 1
542 1 status = cjs_options_set_assert_content(options, SvTRUE(value));
543 }
544 43 ▶ if ((value = option(aTHX_ hv, "base_uri")) && status == CJS_OK) {
Branch: 2 branches
BranchTrueFalseTotal
if ((value = option(aTHX_ hv, "base_uri")) && status == CJS_OK) { 1 42 43
if ((value = option(aTHX_ hv, "base_uri")) && status == CJS_OK) { 1 0 1
545 STRLEN len;
546 1 const char *text = SvPVutf8(value, len);
547 1 status = cjs_options_set_base_uri(options, text, len);
548 }
549 43 ▶ if ((value = option(aTHX_ hv, "entry_point")) && status == CJS_OK) {
Branch: 2 branches
BranchTrueFalseTotal
if ((value = option(aTHX_ hv, "entry_point")) && status == CJS_OK) { 1 42 43
if ((value = option(aTHX_ hv, "entry_point")) && status == CJS_OK) { 1 0 1
550 STRLEN len;
551 1 const char *text = SvPVutf8(value, len);
552 1 status = cjs_options_set_entry_point(options, text, len);
553 }
554 43 ▶ if ((value = option(aTHX_ hv, "max_depth")) && status == CJS_OK) {
Branch: 2 branches
BranchTrueFalseTotal
if ((value = option(aTHX_ hv, "max_depth")) && status == CJS_OK) { 1 42 43
if ((value = option(aTHX_ hv, "max_depth")) && status == CJS_OK) { 1 0 1
555 1 status = cjs_options_set_max_depth(options, (uint32_t)SvUV(value));
556 }
557 43 ▶ if ((value = option(aTHX_ hv, "pattern_match_limit_ms")) && status == CJS_OK) {
Branch: 2 branches
BranchTrueFalseTotal
if ((value = option(aTHX_ hv, "pattern_match_limit_ms")) && status == CJS_OK) { 3 40 43
if ((value = option(aTHX_ hv, "pattern_match_limit_ms")) && status == CJS_OK) { 3 0 3
558 3 status = cjs_options_set_pattern_match_limit_ms(options, (uint64_t)SvUV(value));
559 }
560 43 ▶ if ((value = option(aTHX_ hv, "formats")) && status == CJS_OK) {
Branch: 2 branches
BranchTrueFalseTotal
if ((value = option(aTHX_ hv, "formats")) && status == CJS_OK) { 5 38 43
if ((value = option(aTHX_ hv, "formats")) && status == CJS_OK) { 5 0 5
561 5 HV *formats = (HV *)SvRV(value);
562 HE *entry;
563 5 hv_iterinit(formats);
564 10 ▶ while (status == CJS_OK && (entry = hv_iternext(formats))) {
Branch: 2 branches
BranchTrueFalseTotal
while (status == CJS_OK && (entry = hv_iternext(formats))) { 10 0 10
while (status == CJS_OK && (entry = hv_iternext(formats))) { 5 5 10
565 STRLEN len;
566 5 SV *name = hv_iterkeysv(entry);
567 5 const char *text = SvPVutf8(name, len);
568 5 callback_box *box = new_box(aTHX_ HeVAL(entry));
569 5 status = cjs_options_add_format(options, text, len, call_format, box, free_box);
570 5 ▶ if (status != CJS_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != CJS_OK) { 0 5 5
571 0 free_box(box);
572 }
573 }
574 }
575 43 ▶ if ((value = option(aTHX_ hv, "resolve_document")) && status == CJS_OK) {
Branch: 2 branches
BranchTrueFalseTotal
if ((value = option(aTHX_ hv, "resolve_document")) && status == CJS_OK) { 7 36 43
if ((value = option(aTHX_ hv, "resolve_document")) && status == CJS_OK) { 7 0 7
576 7 callback_box *box = new_box(aTHX_ value);
577 7 status = cjs_options_set_resolver(options, call_resolver, box, free_box);
578 7 ▶ if (status != CJS_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != CJS_OK) { 0 7 7
579 0 free_box(box);
580 }
581 }
582 43 ▶ if (status != CJS_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != CJS_OK) { 0 43 43
583 0 cjs_options_free(options);
584 0 throw_status(aTHX_ status);
585 }
586 43 return options;
587 }
588  
589 43 static SV *compile(pTHX_ const char *text, STRLEN len, bool uri, HV *hv) {
590 43 cjs_options *options = build_options(aTHX_ hv);
591 43 cjs_validator *validator = NULL;
592 cjs_status status;
593 43 clear_pending(aTHX);
594 43 ▶ status = uri ? cjs_compile_uri(text, len, options, &validator) : cjs_compile(text, len, options, &validator);
Branch: 1 branch
BranchTrueFalseTotal
status = uri ? cjs_compile_uri(text, len, options, &validator) : cjs_compile(text, len, options, &validator); 2 41 43
595 43 cjs_options_free(options);
596 43 ▶ if (status != CJS_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != CJS_OK) { 7 36 43
597 7 cjs_validator_free(validator);
598 7 throw_pending(aTHX);
599 5 throw_status(aTHX_ status);
600 }
601 /* A format callback can run during compilation (a patternProperties pattern matched against declared names does
602 * not, but stay safe): an exception it threw wins. */
603 36 ▶ if (validator) {
Branch: 1 branch
BranchTrueFalseTotal
if (validator) { 36 0 36
604 dMY_CXT;
605 36 ▶ if (MY_CXT.pending) {
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.pending) { 0 36 36
606 0 cjs_validator_free(validator);
607 0 throw_pending(aTHX);
608 }
609 }
610 36 return wrap(aTHX_ validator, VALIDATOR_CLASS);
611 }
612  
613 /* The result of a validation call: throws the pending exception or the status, or gives the verdict. */
614 63 static SV *verdict(pTHX_ cjs_status status, bool valid) {
615 63 throw_pending(aTHX);
616 61 ▶ if (status != CJS_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != CJS_OK) { 4 57 61
617 4 throw_status(aTHX_ status);
618 }
619 57 ▶ return valid ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
return valid ? &PL_sv_yes : &PL_sv_no; 30 27 57
620 }
621  
622 51 static SV *row_string(pTHX_ cjs_str s) {
623 51 ▶ return newSVpvn_utf8(s.ptr ? s.ptr : "", s.len, 1);
Branch: 1 branch
BranchTrueFalseTotal
return newSVpvn_utf8(s.ptr ? s.ptr : "", s.len, 1); 51 0 51
624 }
625  
626 MODULE = JSON::Schema::Corvus::XS PACKAGE = JSON::Schema::Corvus::XS
627  
628 PROTOTYPES: DISABLE
629  
630 BOOT:
631 {
632 MY_CXT_INIT;
633 4 MY_CXT.pending = NULL;
634 }
635  
636 void
637 CLONE(...)
638 CODE:
639 MY_CXT_CLONE;
640 0 MY_CXT.pending = NULL;
641  
642 unsigned int
643 library_version()
644 CODE:
645 5 RETVAL = cjs_version();
646 OUTPUT:
647 RETVAL
648  
649 SV *
650 library_version_string()
651 CODE:
652 1 RETVAL = row_string(aTHX_ cjs_version_string());
653 OUTPUT:
654 RETVAL
655  
656 SV *
657 _compile_text(SV *schema, HV *options)
658 CODE:
659 STRLEN len;
660 34 const char *text = text_of(aTHX_ schema, &len);
661 34 RETVAL = compile(aTHX_ text, len, false, options);
662 OUTPUT:
663 RETVAL
664  
665 SV *
666 _compile_data(SV *schema, HV *options)
667 CODE:
668 STRLEN len;
669 7 SV *json = encode_data(aTHX_ schema);
670 7 const char *text = SvPV(json, len);
671 7 RETVAL = compile(aTHX_ text, len, false, options);
672 OUTPUT:
673 RETVAL
674  
675 SV *
676 _compile_uri(SV *uri, HV *options)
677 CODE:
678 STRLEN len;
679 2 const char *text = SvPVutf8(uri, len);
680 2 RETVAL = compile(aTHX_ text, len, true, options);
681 OUTPUT:
682 RETVAL
683  
684 SV *
685 encode_json(SV *data)
686 CODE:
687 26 RETVAL = newSVsv(encode_data(aTHX_ data));
688 OUTPUT:
689 RETVAL
690  
691 MODULE = JSON::Schema::Corvus::XS PACKAGE = JSON::Schema::Corvus::XS::Validator
692  
693 int
694 CLONE_SKIP(...)
695 CODE:
696 0 ▶ RETVAL = 1;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = 1; 0 0 0
697 OUTPUT:
698 RETVAL
699  
700 void
701 DESTROY(SV *self)
702 CODE:
703 36 ▶ if (SvROK(self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(self)) { 36 0 36
704 36 cjs_validator_free(INT2PTR(cjs_validator *, SvIV(SvRV(self))));
705 36 sv_setiv(SvRV(self), 0);
706 }
707  
708 SV *
709 is_valid_json(SV *self, SV *json)
710 CODE:
711 52 cjs_validator *validator = handle_of(aTHX_ self, VALIDATOR_CLASS);
712 STRLEN len;
713 51 const char *text = text_of(aTHX_ json, &len);
714 50 bool valid = false;
715 cjs_status status;
716 50 clear_pending(aTHX);
717 50 status = cjs_validator_validate_json(validator, text, len, &valid);
718 50 RETVAL = verdict(aTHX_ status, valid);
719 OUTPUT:
720 RETVAL
721  
722 SV *
723 is_valid(SV *self, SV *data)
724 CODE:
725 6 cjs_validator *validator = handle_of(aTHX_ self, VALIDATOR_CLASS);
726 STRLEN len;
727 6 SV *json = encode_data(aTHX_ data);
728 6 const char *text = SvPV(json, len);
729 6 bool valid = false;
730 cjs_status status;
731 6 clear_pending(aTHX);
732 6 status = cjs_validator_validate_json(validator, text, len, &valid);
733 6 RETVAL = verdict(aTHX_ status, valid);
734 OUTPUT:
735 RETVAL
736  
737 SV *
738 is_valid_document(SV *self, SV *document)
739 CODE:
740 2 cjs_validator *validator = handle_of(aTHX_ self, VALIDATOR_CLASS);
741 2 cjs_document *doc = handle_of(aTHX_ document, DOCUMENT_CLASS);
742 2 bool valid = false;
743 cjs_status status;
744 2 clear_pending(aTHX);
745 2 status = cjs_validator_validate_document(validator, doc, &valid);
746 2 RETVAL = verdict(aTHX_ status, valid);
747 OUTPUT:
748 RETVAL
749  
750 SV *
751 evaluate_json(SV *self, SV *json, SV *collector)
752 CODE:
753 3 cjs_validator *validator = handle_of(aTHX_ self, VALIDATOR_CLASS);
754 3 cjs_collector *results = handle_of(aTHX_ collector, COLLECTOR_CLASS);
755 STRLEN len;
756 3 const char *text = text_of(aTHX_ json, &len);
757 3 bool valid = false;
758 cjs_status status;
759 3 clear_pending(aTHX);
760 3 status = cjs_validator_evaluate_json(validator, text, len, results, &valid);
761 3 RETVAL = verdict(aTHX_ status, valid);
762 OUTPUT:
763 RETVAL
764  
765 SV *
766 evaluate(SV *self, SV *data, SV *collector)
767 CODE:
768 1 cjs_validator *validator = handle_of(aTHX_ self, VALIDATOR_CLASS);
769 1 cjs_collector *results = handle_of(aTHX_ collector, COLLECTOR_CLASS);
770 STRLEN len;
771 1 SV *json = encode_data(aTHX_ data);
772 1 const char *text = SvPV(json, len);
773 1 bool valid = false;
774 cjs_status status;
775 1 clear_pending(aTHX);
776 1 status = cjs_validator_evaluate_json(validator, text, len, results, &valid);
777 1 RETVAL = verdict(aTHX_ status, valid);
778 OUTPUT:
779 RETVAL
780  
781 SV *
782 evaluate_document(SV *self, SV *document, SV *collector)
783 CODE:
784 1 cjs_validator *validator = handle_of(aTHX_ self, VALIDATOR_CLASS);
785 1 cjs_document *doc = handle_of(aTHX_ document, DOCUMENT_CLASS);
786 1 cjs_collector *results = handle_of(aTHX_ collector, COLLECTOR_CLASS);
787 1 bool valid = false;
788 cjs_status status;
789 1 clear_pending(aTHX);
790 1 status = cjs_validator_evaluate_document(validator, doc, results, &valid);
791 1 RETVAL = verdict(aTHX_ status, valid);
792 OUTPUT:
793 RETVAL
794  
795 MODULE = JSON::Schema::Corvus::XS PACKAGE = JSON::Schema::Corvus::XS::Document
796  
797 int
798 CLONE_SKIP(...)
799 CODE:
800 0 ▶ RETVAL = 1;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = 1; 0 0 0
801 OUTPUT:
802 RETVAL
803  
804 SV *
805 parse(SV *class, SV *json)
806 CODE:
807 STRLEN len;
808 2 const char *text = text_of(aTHX_ json, &len);
809 2 cjs_document *document = NULL;
810 2 cjs_status status = cjs_document_parse(text, len, &document);
811 PERL_UNUSED_VAR(class);
812 2 ▶ if (status != CJS_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != CJS_OK) { 1 1 2
813 1 throw_status(aTHX_ status);
814 }
815 1 RETVAL = wrap(aTHX_ document, DOCUMENT_CLASS);
816 OUTPUT:
817 RETVAL
818  
819 SV *
820 from_data(SV *class, SV *data)
821 CODE:
822 STRLEN len;
823 1 SV *json = encode_data(aTHX_ data);
824 1 const char *text = SvPV(json, len);
825 1 cjs_document *document = NULL;
826 1 cjs_status status = cjs_document_parse(text, len, &document);
827 PERL_UNUSED_VAR(class);
828 1 ▶ if (status != CJS_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != CJS_OK) { 0 1 1
829 0 throw_status(aTHX_ status);
830 }
831 1 RETVAL = wrap(aTHX_ document, DOCUMENT_CLASS);
832 OUTPUT:
833 RETVAL
834  
835 void
836 DESTROY(SV *self)
837 CODE:
838 2 ▶ if (SvROK(self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(self)) { 2 0 2
839 2 cjs_document_free(INT2PTR(cjs_document *, SvIV(SvRV(self))));
840 2 sv_setiv(SvRV(self), 0);
841 }
842  
843 MODULE = JSON::Schema::Corvus::XS PACKAGE = JSON::Schema::Corvus::XS::ResultsCollector
844  
845 int
846 CLONE_SKIP(...)
847 CODE:
848 0 ▶ RETVAL = 1;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = 1; 0 0 0
849 OUTPUT:
850 RETVAL
851  
852 SV *
853 _new(SV *class, unsigned int level)
854 CODE:
855 5 cjs_collector *collector = cjs_collector_new((cjs_results_level)level);
856 PERL_UNUSED_VAR(class);
857 5 ▶ if (!collector) {
Branch: 1 branch
BranchTrueFalseTotal
if (!collector) { 0 5 5
858 0 throw_usage(aTHX_ "the level is not basic, detailed or verbose");
859 }
860 5 RETVAL = wrap(aTHX_ collector, COLLECTOR_CLASS);
861 OUTPUT:
862 RETVAL
863  
864 void
865 DESTROY(SV *self)
866 CODE:
867 5 ▶ if (SvROK(self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(self)) { 5 0 5
868 5 cjs_collector_free(INT2PTR(cjs_collector *, SvIV(SvRV(self))));
869 5 sv_setiv(SvRV(self), 0);
870 }
871  
872 void
873 clear(SV *self)
874 CODE:
875 1 cjs_collector_clear(handle_of(aTHX_ self, COLLECTOR_CLASS));
876  
877 UV
878 count(SV *self)
879 CODE:
880 3 RETVAL = cjs_collector_count(handle_of(aTHX_ self, COLLECTOR_CLASS));
881 OUTPUT:
882 RETVAL
883  
884 SV *
885 results(SV *self)
886 CODE:
887 3 cjs_collector *collector = handle_of(aTHX_ self, COLLECTOR_CLASS);
888 3 size_t i, count = cjs_collector_count(collector);
889 3 AV *rows = newAV();
890 3 av_extend(rows, count);
891 15 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 12 3 15
892 12 HV *row = newHV();
893 12 ▶ (void)hv_stores(row, "is_match", cjs_collector_is_match(collector, i) ? &PL_sv_yes : &PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(row, "is_match", cjs_collector_is_match(collector, i) ? &PL_sv_yes : &PL_sv_no); 0 12 12
894 12 (void)hv_stores(row, "message", row_string(aTHX_ cjs_collector_message(collector, i)));
895 12 (void)hv_stores(row, "evaluation_location", row_string(aTHX_ cjs_collector_evaluation_location(collector, i)));
896 12 (void)hv_stores(row, "schema_evaluation_location",
897 row_string(aTHX_ cjs_collector_schema_location(collector, i)));
898 12 (void)hv_stores(row, "document_evaluation_location",
899 row_string(aTHX_ cjs_collector_instance_location(collector, i)));
900 12 av_push(rows, newRV_noinc((SV *)row));
901 }
902 3 RETVAL = newRV_noinc((SV *)rows);
903 OUTPUT:
904 RETVAL
905  
906 SV *
907 annotations_json(SV *self)
908 CODE:
909 2 cjs_collector *collector = handle_of(aTHX_ self, COLLECTOR_CLASS);
910 2 cjs_str json = { NULL, 0 };
911 2 cjs_status status = cjs_collector_annotations_json(collector, &json);
912 2 ▶ if (status != CJS_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != CJS_OK) { 0 2 2
913 0 throw_status(aTHX_ status);
914 }
915 2 RETVAL = row_string(aTHX_ json);
916 OUTPUT:
917 RETVAL
« blib/lib/JSON/Schema/Corvus/XS.pm ↑ Summary