| 1 |
|
| #ifndef JSF_FRJ_H |
| 2 |
|
| #define JSF_FRJ_H |
| 3 |
|
| |
| 4 |
|
| /* JSON decode through File::Raw::JSON's C ABI (frj_abi.h), resolved lazily at |
| 5 |
|
| * runtime via File::Raw::JSON::_abi_ptr - schema text and fetched documents are |
| 6 |
|
| * parsed in C with no per-call Perl codec dispatch. Mirrors jsf_fetch.h's |
| 7 |
|
| * resolve idiom. Reached through ExtUtils::Depends (no vendored header). Needs |
| 8 |
|
| * the Perl API (perl.h, included by the .xs). */ |
| 9 |
|
| |
| 10 |
|
| #include "frj_abi.h" |
| 11 |
|
| |
| 12 |
|
| static const frj_abi *JSF_FRJ = NULL; /* resolved on first decode */ |
| 13 |
|
| static int JSF_FRJ_TRIED = 0; |
| 14 |
|
| |
| 15 |
|
| /* Resolve File::Raw::JSON's ABI table once; NULL if unavailable. */ |
| 16 |
3 |
| static const frj_abi *jsf_frj(pTHX) { |
| 17 |
3 |
▶ | if (!JSF_FRJ && !JSF_FRJ_TRIED) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!JSF_FRJ && !JSF_FRJ_TRIED) { |
1 |
2 |
3 |
| if (!JSF_FRJ && !JSF_FRJ_TRIED) { |
1 |
0 |
1 |
|
| 18 |
1 |
| dSP; int count; IV p = 0; |
| 19 |
1 |
| JSF_FRJ_TRIED = 1; |
| 20 |
1 |
| eval_pv("require File::Raw::JSON;", FALSE); |
| 21 |
|
| /* The require runs arbitrary Perl and may have grown the value stack, |
| 22 |
|
| * which reallocates it; SP was captured before that and would then be |
| 23 |
|
| * a pointer into the freed block, published straight back to the |
| 24 |
|
| * interpreter by the PUTBACK below. */ |
| 25 |
1 |
| SPAGAIN; |
| 26 |
1 |
▶ | if (!SvTRUE(ERRSV)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvTRUE(ERRSV)) { |
1 |
0 |
1 |
| if (!SvTRUE(ERRSV)) { |
1 |
0 |
1 |
|
| 27 |
1 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; |
0 |
1 |
1 |
|
| 28 |
1 |
| count = call_pv("File::Raw::JSON::_abi_ptr", G_SCALAR | G_EVAL); |
| 29 |
1 |
| SPAGAIN; |
| 30 |
1 |
▶ | if (!SvTRUE(ERRSV) && count > 0) p = POPi; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!SvTRUE(ERRSV) && count > 0) p = POPi; |
1 |
0 |
1 |
| if (!SvTRUE(ERRSV) && count > 0) p = POPi; |
1 |
0 |
1 |
| if (!SvTRUE(ERRSV) && count > 0) p = POPi; |
1 |
0 |
1 |
|
| 31 |
0 |
▶ | else if (count > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (count > 0) (void)POPs; |
0 |
0 |
0 |
|
| 32 |
1 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
1 |
0 |
1 |
|
| 33 |
1 |
▶ | if (p) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p) { |
1 |
0 |
1 |
|
| 34 |
1 |
| const frj_abi *a = INT2PTR(const frj_abi *, p); |
| 35 |
1 |
▶ | if (a && a->abi_version == FRJ_ABI_VERSION) JSF_FRJ = a; |
Branch: 2 branches
| Branch | True | False | Total |
| if (a && a->abi_version == FRJ_ABI_VERSION) JSF_FRJ = a; |
1 |
0 |
1 |
| if (a && a->abi_version == FRJ_ABI_VERSION) JSF_FRJ = a; |
1 |
0 |
1 |
|
| 36 |
|
| } |
| 37 |
|
| } |
| 38 |
|
| } |
| 39 |
3 |
| return JSF_FRJ; |
| 40 |
|
| } |
| 41 |
|
| |
| 42 |
|
| /* Decode JSON bytes -> a mortal SV, or NULL if File::Raw::JSON is unavailable |
| 43 |
|
| * (the caller decides whether that is fatal). Croaks on malformed JSON. */ |
| 44 |
3 |
| static SV *jsf_frj_decode(pTHX_ SV *text) { |
| 45 |
3 |
| const frj_abi *J = jsf_frj(aTHX); |
| 46 |
|
| STRLEN len; |
| 47 |
|
| const char *pv; |
| 48 |
3 |
▶ | if (!J) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!J) return NULL; |
0 |
3 |
3 |
|
| 49 |
3 |
| pv = SvPV(text, len); |
| 50 |
3 |
| return sv_2mortal(J->decode(aTHX_ pv, len, NULL)); |
| 51 |
|
| } |
| 52 |
|
| |
| 53 |
|
| #endif /* JSF_FRJ_H */ |