| 1 |
|
| #ifndef MDS_ABI_IMPL_H |
| 2 |
|
| #define MDS_ABI_IMPL_H |
| 3 |
|
| |
| 4 |
|
| /* Markdown::Simple-side implementation of the shared C ABI (mds_abi.h). |
| 5 |
|
| * Included by Simple.xs AFTER mds_session_mg_vtbl, mds_flags_from_hv |
| 6 |
|
| * and strip_markdown_except_lists_tables are in scope, since the session hangs |
| 7 |
|
| * off the object as ext magic and the wrappers below reach the parser through |
| 8 |
|
| * those file-statics. The engine is a unity build, so everything here shares a |
| 9 |
|
| * translation unit with them; consumers reach it only through the MDS_ABI |
| 10 |
|
| * table returned by Markdown::Simple::_abi_ptr. */ |
| 11 |
|
| |
| 12 |
|
| #include "mds_abi.h" |
| 13 |
|
| |
| 14 |
|
| /* Like mds_session_from_self but returns NULL where that croaks: a consumer |
| 15 |
|
| * probing whether an SV is a renderable session must be able to ask without an |
| 16 |
|
| * eval. */ |
| 17 |
6 |
| static void *mds_abi_session_of(pTHX_ SV *obj_sv) |
| 18 |
|
| { |
| 19 |
|
| SV *rv; |
| 20 |
|
| MAGIC *mg; |
| 21 |
6 |
▶ | if (!obj_sv || !SvROK(obj_sv)) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!obj_sv || !SvROK(obj_sv)) return NULL; |
6 |
0 |
6 |
| if (!obj_sv || !SvROK(obj_sv)) return NULL; |
1 |
5 |
6 |
|
| 22 |
5 |
| rv = SvRV(obj_sv); |
| 23 |
5 |
▶ | if (!sv_derived_from(obj_sv, "Markdown::Simple")) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_derived_from(obj_sv, "Markdown::Simple")) return NULL; |
2 |
3 |
5 |
|
| 24 |
3 |
▶ | for (mg = SvMAGIC(rv); mg; mg = mg->mg_moremagic) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (mg = SvMAGIC(rv); mg; mg = mg->mg_moremagic) { |
3 |
0 |
3 |
|
| 25 |
3 |
▶ | if (mg->mg_type == PERL_MAGIC_ext && |
Branch: 1 branch
| Branch | True | False | Total |
| if (mg->mg_type == PERL_MAGIC_ext && |
3 |
0 |
3 |
|
| 26 |
3 |
▶ | mg->mg_virtual == &mds_session_mg_vtbl) |
Branch: 1 branch
| Branch | True | False | Total |
| mg->mg_virtual == &mds_session_mg_vtbl) |
3 |
0 |
3 |
|
| 27 |
3 |
| return (void *)mg->mg_ptr; |
| 28 |
|
| } |
| 29 |
0 |
| return NULL; |
| 30 |
|
| } |
| 31 |
|
| |
| 32 |
5 |
| static unsigned mds_abi_flags_from_hv(pTHX_ HV *opts) |
| 33 |
|
| { |
| 34 |
5 |
| return mds_flags_from_hv(aTHX_ opts); |
| 35 |
|
| } |
| 36 |
|
| |
| 37 |
|
| /* Shared tail for both render entries: the parser signals malformed input |
| 38 |
|
| * under strict_utf8 by producing nothing, so an empty result with a non-empty |
| 39 |
|
| * input is the one case worth re-checking before calling it success. */ |
| 40 |
8 |
| static int mds_abi_check(pTHX_ const char *in, STRLEN len, unsigned flags, |
| 41 |
|
| SV *out, STRLEN before, SV **err) |
| 42 |
|
| { |
| 43 |
8 |
▶ | if (SvCUR(out) == before && len && (flags & MDS_FLAG_STRICT_UTF8)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (SvCUR(out) == before && len && (flags & MDS_FLAG_STRICT_UTF8)) { |
1 |
7 |
8 |
| if (SvCUR(out) == before && len && (flags & MDS_FLAG_STRICT_UTF8)) { |
1 |
0 |
1 |
| if (SvCUR(out) == before && len && (flags & MDS_FLAG_STRICT_UTF8)) { |
1 |
0 |
1 |
|
| 44 |
2 |
| const mds_simd_ops *ops = (flags & MDS_FLAG_NO_SIMD) |
| 45 |
1 |
▶ | ? mds_simd_ops_scalar() : mds_simd_get(); |
Branch: 1 branch
| Branch | True | False | Total |
| ? mds_simd_ops_scalar() : mds_simd_get(); |
0 |
1 |
1 |
|
| 46 |
1 |
▶ | if (!ops->validate_utf8(in, len)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ops->validate_utf8(in, len)) { |
1 |
0 |
1 |
|
| 47 |
1 |
▶ | if (err) *err = sv_2mortal(newSVpvs( |
Branch: 1 branch
| Branch | True | False | Total |
| if (err) *err = sv_2mortal(newSVpvs( |
1 |
0 |
1 |
|
| 48 |
|
| "Markdown::Simple: input is not valid UTF-8")); |
| 49 |
1 |
| return -1; |
| 50 |
|
| } |
| 51 |
|
| } |
| 52 |
7 |
| return 0; |
| 53 |
|
| } |
| 54 |
|
| |
| 55 |
3 |
| static int mds_abi_session_render(pTHX_ void *session, const char *in, |
| 56 |
|
| STRLEN len, SV *out, AV *toc, SV **err) |
| 57 |
|
| { |
| 58 |
3 |
| mds_session *s = (mds_session *)session; |
| 59 |
|
| STRLEN before; |
| 60 |
3 |
▶ | if (!s || !out) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!s || !out) { |
3 |
0 |
3 |
| if (!s || !out) { |
0 |
3 |
3 |
|
| 61 |
0 |
▶ | if (err) *err = sv_2mortal(newSVpvs( |
Branch: 1 branch
| Branch | True | False | Total |
| if (err) *err = sv_2mortal(newSVpvs( |
0 |
0 |
0 |
|
| 62 |
|
| "Markdown::Simple: session_render needs a session and an output SV")); |
| 63 |
0 |
| return -1; |
| 64 |
|
| } |
| 65 |
3 |
▶ | if (!in) { in = ""; len = 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!in) { in = ""; len = 0; } |
0 |
3 |
3 |
|
| 66 |
3 |
▶ | before = SvOK(out) ? SvCUR(out) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| before = SvOK(out) ? SvCUR(out) : 0; |
3 |
0 |
3 |
|
| 67 |
3 |
| mds_render_html_to_sv_toc(aTHX_ in, len, s->flags, out, |
| 68 |
|
| &s->arena, &s->scratch, toc); |
| 69 |
3 |
| return mds_abi_check(aTHX_ in, len, s->flags, out, before, err); |
| 70 |
|
| } |
| 71 |
|
| |
| 72 |
5 |
| static int mds_abi_render(pTHX_ const char *in, STRLEN len, unsigned flags, |
| 73 |
|
| SV *out, AV *toc, SV **err) |
| 74 |
|
| { |
| 75 |
|
| STRLEN before; |
| 76 |
5 |
▶ | if (!out) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!out) { |
0 |
5 |
5 |
|
| 77 |
0 |
▶ | if (err) *err = sv_2mortal(newSVpvs( |
Branch: 1 branch
| Branch | True | False | Total |
| if (err) *err = sv_2mortal(newSVpvs( |
0 |
0 |
0 |
|
| 78 |
|
| "Markdown::Simple: render needs an output SV")); |
| 79 |
0 |
| return -1; |
| 80 |
|
| } |
| 81 |
5 |
▶ | if (!in) { in = ""; len = 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!in) { in = ""; len = 0; } |
0 |
5 |
5 |
|
| 82 |
5 |
▶ | before = SvOK(out) ? SvCUR(out) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| before = SvOK(out) ? SvCUR(out) : 0; |
5 |
0 |
5 |
|
| 83 |
5 |
| mds_render_html_to_sv_toc(aTHX_ in, len, flags, out, NULL, NULL, toc); |
| 84 |
5 |
| return mds_abi_check(aTHX_ in, len, flags, out, before, err); |
| 85 |
|
| } |
| 86 |
|
| |
| 87 |
|
| /* strip_markdown_except_lists_tables walks a NUL-terminated string, so give it |
| 88 |
|
| * one: the ABI takes a pointer and a length, and a consumer's bytes are not |
| 89 |
|
| * required to be terminated or free of embedded NULs. newSVpvn terminates for |
| 90 |
|
| * us, and truncating at an embedded NUL is the same thing the Perl-visible |
| 91 |
|
| * strip_markdown does with such input. */ |
| 92 |
2 |
| static int mds_abi_strip(pTHX_ const char *in, STRLEN len, SV *out, SV **err) |
| 93 |
|
| { |
| 94 |
|
| SV *tmp, *res; |
| 95 |
2 |
▶ | if (!out) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!out) { |
0 |
2 |
2 |
|
| 96 |
0 |
▶ | if (err) *err = sv_2mortal(newSVpvs( |
Branch: 1 branch
| Branch | True | False | Total |
| if (err) *err = sv_2mortal(newSVpvs( |
0 |
0 |
0 |
|
| 97 |
|
| "Markdown::Simple: strip needs an output SV")); |
| 98 |
0 |
| return -1; |
| 99 |
|
| } |
| 100 |
2 |
▶ | if (!in) { in = ""; len = 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!in) { in = ""; len = 0; } |
0 |
2 |
2 |
|
| 101 |
2 |
| tmp = sv_2mortal(newSVpvn(in, len)); |
| 102 |
2 |
| res = strip_markdown_except_lists_tables(SvPVX(tmp)); |
| 103 |
2 |
▶ | if (!res) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!res) { |
0 |
2 |
2 |
|
| 104 |
0 |
▶ | if (err) *err = sv_2mortal(newSVpvs("Markdown::Simple: strip failed")); |
Branch: 1 branch
| Branch | True | False | Total |
| if (err) *err = sv_2mortal(newSVpvs("Markdown::Simple: strip failed")); |
0 |
0 |
0 |
|
| 105 |
0 |
| return -1; |
| 106 |
|
| } |
| 107 |
2 |
| sv_catsv(out, res); |
| 108 |
2 |
| SvREFCNT_dec(res); |
| 109 |
2 |
| return 0; |
| 110 |
|
| } |
| 111 |
|
| |
| 112 |
|
| static const mds_abi MDS_ABI = { |
| 113 |
|
| MDS_ABI_VERSION, |
| 114 |
|
| mds_abi_flags_from_hv, |
| 115 |
|
| mds_abi_session_of, |
| 116 |
|
| mds_abi_session_render, |
| 117 |
|
| mds_abi_render, |
| 118 |
|
| mds_abi_strip, |
| 119 |
|
| }; |
| 120 |
|
| |
| 121 |
|
| #endif /* MDS_ABI_IMPL_H */ |