| 1 |
|
| #ifndef SC_ABI_IMPL_H |
| 2 |
|
| #define SC_ABI_IMPL_H |
| 3 |
|
| |
| 4 |
|
| /* The table behind Struct::Codec::_abi_ptr. Private to this build; consumers |
| 5 |
|
| * see only include/sc_abi.h and resolve the address at runtime. |
| 6 |
|
| * |
| 7 |
|
| * Every entry points at a core function; nothing is IMPLEMENTED here. Needs |
| 8 |
|
| * sc_abi.h and sc_codec.h. |
| 9 |
|
| */ |
| 10 |
|
| |
| 11 |
|
| /* The version the table is built at and the version the public header |
| 12 |
|
| * promises are two files, and a bump to one and not the other is silent |
| 13 |
|
| * until a consumer refuses a provider that has what it wants. The array-size |
| 14 |
|
| * trick rather than static_assert, which is C11 and this dist is C89. */ |
| 15 |
|
| #define SC_ABI_IMPL_VERSION 1 |
| 16 |
|
| typedef char sc_abi_assert_version[(SC_ABI_IMPL_VERSION == SC_ABI_VERSION) ? 1 : -1]; |
| 17 |
|
| |
| 18 |
|
| static const sc_abi SC_ABI = { |
| 19 |
|
| SC_ABI_VERSION, |
| 20 |
|
| sc_encode, |
| 21 |
|
| sc_encode_to, |
| 22 |
|
| sc_decode |
| 23 |
|
| }; |
| 24 |
|
| |
| 25 |
|
| /* ---- the selftest --------------------------------------------------------- |
| 26 |
|
| * |
| 27 |
|
| * A structure built in C, encoded through the TABLE, decoded through the |
| 28 |
|
| * table, and every field checked - because a table is a list of addresses and |
| 29 |
|
| * nothing about compiling one proves an entry points where its declaration |
| 30 |
|
| * says. Numbered steps, and the number of the first that failed comes back, |
| 31 |
|
| * so a failure says WHICH. The bytes and the structure come back too, for the |
| 32 |
|
| * Perl side to compare against the Perl surface's encoding of the same data. |
| 33 |
|
| */ |
| 34 |
|
| |
| 35 |
|
| #define SC_STEP(n) do { *step = (n); } while (0) |
| 36 |
|
| |
| 37 |
1 |
| static SV *sc_abi_selftest(pTHX_ int *step, SV **data) { |
| 38 |
1 |
| const sc_abi *A = &SC_ABI; |
| 39 |
1 |
| HV *h = newHV(); |
| 40 |
1 |
| AV *shared = newAV(); |
| 41 |
1 |
| HV *obj = newHV(); |
| 42 |
1 |
| SV *in, *bytes = NULL, *out = NULL; |
| 43 |
|
| char small[8]; |
| 44 |
1 |
| STRLEN need = 0, blen, got; |
| 45 |
|
| |
| 46 |
1 |
| *step = 0; |
| 47 |
1 |
| *data = NULL; |
| 48 |
|
| |
| 49 |
|
| /* the structure: every kind, a shared referent, and an object */ |
| 50 |
1 |
| (void)hv_stores(h, "s", newSVpvs("hello")); |
| 51 |
|
| { |
| 52 |
1 |
| SV *u = newSVpvs("caf\xc3\xa9"); |
| 53 |
1 |
| SvUTF8_on(u); |
| 54 |
1 |
| (void)hv_stores(h, "u", u); |
| 55 |
|
| } |
| 56 |
1 |
| (void)hv_stores(h, "n", newSViv(-5)); |
| 57 |
1 |
| (void)hv_stores(h, "big", newSVuv(UV_MAX)); |
| 58 |
1 |
| (void)hv_stores(h, "f", newSVnv(1.5)); |
| 59 |
|
| /* A third is exact at no NV width, so on a perl built -Duselongdouble or |
| 60 |
|
| * -Dusequadmath this is the value the 8-byte wire float cannot hold and it |
| 61 |
|
| * goes through the table as decimal digits. On a double perl it is an |
| 62 |
|
| * ordinary float and the step below still holds. */ |
| 63 |
1 |
| (void)hv_stores(h, "w", newSVnv((NV)1 / (NV)3)); |
| 64 |
1 |
| (void)hv_stores(h, "un", newSV(0)); |
| 65 |
1 |
| av_push(shared, newSViv(1)); |
| 66 |
1 |
| av_push(shared, newSVpvs("two")); |
| 67 |
1 |
| (void)hv_stores(h, "a", newRV_inc((SV *)shared)); |
| 68 |
1 |
| (void)hv_stores(h, "a2", newRV_inc((SV *)shared)); |
| 69 |
1 |
| (void)hv_stores(obj, "k", newSViv(7)); |
| 70 |
1 |
| (void)hv_stores(h, "obj", sv_bless(newRV_inc((SV *)obj), gv_stashpvs("Struct::Codec::SelfTest", GV_ADD))); |
| 71 |
1 |
| SvREFCNT_dec((SV *)shared); |
| 72 |
1 |
| SvREFCNT_dec((SV *)obj); |
| 73 |
1 |
| in = newRV_noinc((SV *)h); |
| 74 |
1 |
| *data = in; |
| 75 |
|
| |
| 76 |
|
| /* 1: the version */ |
| 77 |
1 |
▶ | if (A->abi_version < SC_ABI_VERSION) { SC_STEP(1); goto done; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (A->abi_version < SC_ABI_VERSION) { SC_STEP(1); goto done; } |
0 |
1 |
1 |
|
| 78 |
|
| |
| 79 |
|
| /* 2: encode gives bytes with the header */ |
| 80 |
1 |
| bytes = (A->encode)(aTHX_ in); |
| 81 |
1 |
▶ | if (!bytes || !SvPOK(bytes) || SvUTF8(bytes)) { SC_STEP(2); goto done; } |
Branch: 3 branches
| Branch | True | False | Total |
| if (!bytes || !SvPOK(bytes) || SvUTF8(bytes)) { SC_STEP(2); goto done; } |
1 |
0 |
1 |
| if (!bytes || !SvPOK(bytes) || SvUTF8(bytes)) { SC_STEP(2); goto done; } |
1 |
0 |
1 |
| if (!bytes || !SvPOK(bytes) || SvUTF8(bytes)) { SC_STEP(2); goto done; } |
0 |
1 |
1 |
|
| 82 |
1 |
▶ | if (SvCUR(bytes) < SC_HDR_LEN + 1 || SvPVX(bytes)[0] != SC_MAGIC) { SC_STEP(2); goto done; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvCUR(bytes) < SC_HDR_LEN + 1 || SvPVX(bytes)[0] != SC_MAGIC) { SC_STEP(2); goto done; } |
1 |
0 |
1 |
| if (SvCUR(bytes) < SC_HDR_LEN + 1 || SvPVX(bytes)[0] != SC_MAGIC) { SC_STEP(2); goto done; } |
0 |
1 |
1 |
|
| 83 |
|
| |
| 84 |
|
| /* 3: decode gives the structure back */ |
| 85 |
1 |
| out = (A->decode)(aTHX_ SvPVX(bytes), SvCUR(bytes)); |
| 86 |
1 |
▶ | if (!out || !SvROK(out) || SvTYPE(SvRV(out)) != SVt_PVHV) { SC_STEP(3); goto done; } |
Branch: 3 branches
| Branch | True | False | Total |
| if (!out || !SvROK(out) || SvTYPE(SvRV(out)) != SVt_PVHV) { SC_STEP(3); goto done; } |
1 |
0 |
1 |
| if (!out || !SvROK(out) || SvTYPE(SvRV(out)) != SVt_PVHV) { SC_STEP(3); goto done; } |
1 |
0 |
1 |
| if (!out || !SvROK(out) || SvTYPE(SvRV(out)) != SVt_PVHV) { SC_STEP(3); goto done; } |
0 |
1 |
1 |
|
| 87 |
|
| { |
| 88 |
1 |
| HV *oh = (HV *)SvRV(out); |
| 89 |
|
| SV **v; |
| 90 |
1 |
▶ | if (HvUSEDKEYS(oh) != 10) { SC_STEP(3); goto done; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (HvUSEDKEYS(oh) != 10) { SC_STEP(3); goto done; } |
0 |
1 |
1 |
| if (HvUSEDKEYS(oh) != 10) { SC_STEP(3); goto done; } |
0 |
1 |
1 |
|
| 91 |
|
| |
| 92 |
|
| /* 4: a string is a string, and bytes stay bytes */ |
| 93 |
1 |
| v = hv_fetchs(oh, "s", 0); |
| 94 |
1 |
▶ | if (!v || !SvPOK(*v) || SvUTF8(*v) || !strEQ(SvPVX(*v), "hello")) { SC_STEP(4); goto done; } |
Branch: 4 branches
| Branch | True | False | Total |
| if (!v || !SvPOK(*v) || SvUTF8(*v) || !strEQ(SvPVX(*v), "hello")) { SC_STEP(4); goto done; } |
1 |
0 |
1 |
| if (!v || !SvPOK(*v) || SvUTF8(*v) || !strEQ(SvPVX(*v), "hello")) { SC_STEP(4); goto done; } |
1 |
0 |
1 |
| if (!v || !SvPOK(*v) || SvUTF8(*v) || !strEQ(SvPVX(*v), "hello")) { SC_STEP(4); goto done; } |
1 |
0 |
1 |
| if (!v || !SvPOK(*v) || SvUTF8(*v) || !strEQ(SvPVX(*v), "hello")) { SC_STEP(4); goto done; } |
0 |
1 |
1 |
|
| 95 |
|
| |
| 96 |
|
| /* 5: a utf8 string keeps its flag */ |
| 97 |
1 |
| v = hv_fetchs(oh, "u", 0); |
| 98 |
1 |
▶ | if (!v || !SvPOK(*v) || !SvUTF8(*v) || SvCUR(*v) != 5) { SC_STEP(5); goto done; } |
Branch: 4 branches
| Branch | True | False | Total |
| if (!v || !SvPOK(*v) || !SvUTF8(*v) || SvCUR(*v) != 5) { SC_STEP(5); goto done; } |
1 |
0 |
1 |
| if (!v || !SvPOK(*v) || !SvUTF8(*v) || SvCUR(*v) != 5) { SC_STEP(5); goto done; } |
1 |
0 |
1 |
| if (!v || !SvPOK(*v) || !SvUTF8(*v) || SvCUR(*v) != 5) { SC_STEP(5); goto done; } |
1 |
0 |
1 |
| if (!v || !SvPOK(*v) || !SvUTF8(*v) || SvCUR(*v) != 5) { SC_STEP(5); goto done; } |
0 |
1 |
1 |
|
| 99 |
|
| |
| 100 |
|
| /* 6: a negative integer, and a UV that does not fit an IV */ |
| 101 |
1 |
| v = hv_fetchs(oh, "n", 0); |
| 102 |
1 |
▶ | if (!v || !SvIOK(*v) || SvIV(*v) != -5) { SC_STEP(6); goto done; } |
Branch: 3 branches
| Branch | True | False | Total |
| if (!v || !SvIOK(*v) || SvIV(*v) != -5) { SC_STEP(6); goto done; } |
1 |
0 |
1 |
| if (!v || !SvIOK(*v) || SvIV(*v) != -5) { SC_STEP(6); goto done; } |
1 |
0 |
1 |
| if (!v || !SvIOK(*v) || SvIV(*v) != -5) { SC_STEP(6); goto done; } |
0 |
1 |
1 |
|
| 103 |
1 |
| v = hv_fetchs(oh, "big", 0); |
| 104 |
1 |
▶ | if (!v || !SvIOK(*v) || !SvIsUV(*v) || SvUV(*v) != UV_MAX) { SC_STEP(6); goto done; } |
Branch: 4 branches
| Branch | True | False | Total |
| if (!v || !SvIOK(*v) || !SvIsUV(*v) || SvUV(*v) != UV_MAX) { SC_STEP(6); goto done; } |
1 |
0 |
1 |
| if (!v || !SvIOK(*v) || !SvIsUV(*v) || SvUV(*v) != UV_MAX) { SC_STEP(6); goto done; } |
1 |
0 |
1 |
| if (!v || !SvIOK(*v) || !SvIsUV(*v) || SvUV(*v) != UV_MAX) { SC_STEP(6); goto done; } |
1 |
0 |
1 |
| if (!v || !SvIOK(*v) || !SvIsUV(*v) || SvUV(*v) != UV_MAX) { SC_STEP(6); goto done; } |
0 |
1 |
1 |
|
| 105 |
|
| |
| 106 |
|
| /* 7: a float, a float no double holds, and undef */ |
| 107 |
1 |
| v = hv_fetchs(oh, "f", 0); |
| 108 |
1 |
▶ | if (!v || !SvNOK(*v) || SvNV(*v) != 1.5) { SC_STEP(7); goto done; } |
Branch: 3 branches
| Branch | True | False | Total |
| if (!v || !SvNOK(*v) || SvNV(*v) != 1.5) { SC_STEP(7); goto done; } |
1 |
0 |
1 |
| if (!v || !SvNOK(*v) || SvNV(*v) != 1.5) { SC_STEP(7); goto done; } |
1 |
0 |
1 |
| if (!v || !SvNOK(*v) || SvNV(*v) != 1.5) { SC_STEP(7); goto done; } |
0 |
1 |
1 |
|
| 109 |
1 |
| v = hv_fetchs(oh, "w", 0); |
| 110 |
1 |
▶ | if (!v || !SvNOK(*v) || SvNV(*v) != (NV)1 / (NV)3) { SC_STEP(7); goto done; } |
Branch: 3 branches
| Branch | True | False | Total |
| if (!v || !SvNOK(*v) || SvNV(*v) != (NV)1 / (NV)3) { SC_STEP(7); goto done; } |
1 |
0 |
1 |
| if (!v || !SvNOK(*v) || SvNV(*v) != (NV)1 / (NV)3) { SC_STEP(7); goto done; } |
1 |
0 |
1 |
| if (!v || !SvNOK(*v) || SvNV(*v) != (NV)1 / (NV)3) { SC_STEP(7); goto done; } |
0 |
1 |
1 |
|
| 111 |
1 |
| v = hv_fetchs(oh, "un", 0); |
| 112 |
1 |
▶ | if (!v || SvOK(*v)) { SC_STEP(7); goto done; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (!v || SvOK(*v)) { SC_STEP(7); goto done; } |
1 |
0 |
1 |
| if (!v || SvOK(*v)) { SC_STEP(7); goto done; } |
0 |
1 |
1 |
|
| 113 |
|
| |
| 114 |
|
| /* 8: the array came back, and a and a2 share ONE referent */ |
| 115 |
|
| { |
| 116 |
1 |
| SV **a = hv_fetchs(oh, "a", 0), **a2 = hv_fetchs(oh, "a2", 0); |
| 117 |
1 |
▶ | if (!a || !a2 || !SvROK(*a) || !SvROK(*a2)) { SC_STEP(8); goto done; } |
Branch: 4 branches
| Branch | True | False | Total |
| if (!a || !a2 || !SvROK(*a) || !SvROK(*a2)) { SC_STEP(8); goto done; } |
1 |
0 |
1 |
| if (!a || !a2 || !SvROK(*a) || !SvROK(*a2)) { SC_STEP(8); goto done; } |
1 |
0 |
1 |
| if (!a || !a2 || !SvROK(*a) || !SvROK(*a2)) { SC_STEP(8); goto done; } |
1 |
0 |
1 |
| if (!a || !a2 || !SvROK(*a) || !SvROK(*a2)) { SC_STEP(8); goto done; } |
0 |
1 |
1 |
|
| 118 |
1 |
▶ | if (SvRV(*a) != SvRV(*a2)) { SC_STEP(8); goto done; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvRV(*a) != SvRV(*a2)) { SC_STEP(8); goto done; } |
0 |
1 |
1 |
|
| 119 |
1 |
▶ | if (SvTYPE(SvRV(*a)) != SVt_PVAV || av_len((AV *)SvRV(*a)) != 1) { SC_STEP(8); goto done; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTYPE(SvRV(*a)) != SVt_PVAV || av_len((AV *)SvRV(*a)) != 1) { SC_STEP(8); goto done; } |
1 |
0 |
1 |
| if (SvTYPE(SvRV(*a)) != SVt_PVAV || av_len((AV *)SvRV(*a)) != 1) { SC_STEP(8); goto done; } |
0 |
1 |
1 |
|
| 120 |
1 |
| v = av_fetch((AV *)SvRV(*a), 1, 0); |
| 121 |
1 |
▶ | if (!v || !SvPOK(*v) || !strEQ(SvPVX(*v), "two")) { SC_STEP(8); goto done; } |
Branch: 3 branches
| Branch | True | False | Total |
| if (!v || !SvPOK(*v) || !strEQ(SvPVX(*v), "two")) { SC_STEP(8); goto done; } |
1 |
0 |
1 |
| if (!v || !SvPOK(*v) || !strEQ(SvPVX(*v), "two")) { SC_STEP(8); goto done; } |
1 |
0 |
1 |
| if (!v || !SvPOK(*v) || !strEQ(SvPVX(*v), "two")) { SC_STEP(8); goto done; } |
0 |
1 |
1 |
|
| 122 |
|
| } |
| 123 |
|
| |
| 124 |
|
| /* 9: the object is blessed into its class */ |
| 125 |
1 |
| v = hv_fetchs(oh, "obj", 0); |
| 126 |
1 |
▶ | if (!v || !SvROK(*v) || !SvOBJECT(SvRV(*v)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!v || !SvROK(*v) || !SvOBJECT(SvRV(*v)) |
1 |
0 |
1 |
| if (!v || !SvROK(*v) || !SvOBJECT(SvRV(*v)) |
1 |
0 |
1 |
| if (!v || !SvROK(*v) || !SvOBJECT(SvRV(*v)) |
1 |
0 |
1 |
|
| 127 |
1 |
▶ | || !strEQ(HvNAME(SvSTASH(SvRV(*v))), "Struct::Codec::SelfTest")) { SC_STEP(9); goto done; } |
Branch: 7 branches
| Branch | True | False | Total |
| || !strEQ(HvNAME(SvSTASH(SvRV(*v))), "Struct::Codec::SelfTest")) { SC_STEP(9); goto done; } |
1 |
0 |
1 |
| || !strEQ(HvNAME(SvSTASH(SvRV(*v))), "Struct::Codec::SelfTest")) { SC_STEP(9); goto done; } |
1 |
0 |
1 |
| || !strEQ(HvNAME(SvSTASH(SvRV(*v))), "Struct::Codec::SelfTest")) { SC_STEP(9); goto done; } |
0 |
1 |
1 |
| || !strEQ(HvNAME(SvSTASH(SvRV(*v))), "Struct::Codec::SelfTest")) { SC_STEP(9); goto done; } |
0 |
0 |
0 |
| || !strEQ(HvNAME(SvSTASH(SvRV(*v))), "Struct::Codec::SelfTest")) { SC_STEP(9); goto done; } |
1 |
0 |
1 |
| || !strEQ(HvNAME(SvSTASH(SvRV(*v))), "Struct::Codec::SelfTest")) { SC_STEP(9); goto done; } |
0 |
1 |
1 |
| || !strEQ(HvNAME(SvSTASH(SvRV(*v))), "Struct::Codec::SelfTest")) { SC_STEP(9); goto done; } |
0 |
1 |
1 |
|
| 128 |
|
| } |
| 129 |
|
| |
| 130 |
|
| /* 10: encode_to refuses a buffer one byte short, allocating nothing, and |
| 131 |
|
| * reports what it needed */ |
| 132 |
1 |
| blen = SvCUR(bytes); |
| 133 |
|
| { |
| 134 |
|
| char *buf; |
| 135 |
1 |
| Newx(buf, blen, char); |
| 136 |
1 |
| got = (A->encode_to)(aTHX_ in, buf, blen - 1, &need); |
| 137 |
1 |
▶ | if (got != 0 || need != blen) { Safefree(buf); SC_STEP(10); goto done; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (got != 0 || need != blen) { Safefree(buf); SC_STEP(10); goto done; } |
1 |
0 |
1 |
| if (got != 0 || need != blen) { Safefree(buf); SC_STEP(10); goto done; } |
0 |
1 |
1 |
|
| 138 |
|
| |
| 139 |
|
| /* 11: and fills one exactly big enough with the same bytes */ |
| 140 |
1 |
| got = (A->encode_to)(aTHX_ in, buf, blen, &need); |
| 141 |
1 |
▶ | if (got != blen || need != blen || memcmp(buf, SvPVX(bytes), blen)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (got != blen || need != blen || memcmp(buf, SvPVX(bytes), blen)) { |
1 |
0 |
1 |
| if (got != blen || need != blen || memcmp(buf, SvPVX(bytes), blen)) { |
1 |
0 |
1 |
| if (got != blen || need != blen || memcmp(buf, SvPVX(bytes), blen)) { |
0 |
1 |
1 |
|
| 142 |
0 |
| Safefree(buf); SC_STEP(11); goto done; |
| 143 |
|
| } |
| 144 |
1 |
| Safefree(buf); |
| 145 |
|
| } |
| 146 |
|
| |
| 147 |
|
| /* 12: a tiny value fits a tiny buffer, and need may be NULL */ |
| 148 |
|
| { |
| 149 |
1 |
| SV *five = sv_2mortal(newSViv(5)); |
| 150 |
1 |
| got = (A->encode_to)(aTHX_ five, small, sizeof small, NULL); |
| 151 |
1 |
▶ | if (got != SC_HDR_LEN + 1 || (unsigned char)small[SC_HDR_LEN] != 5) { SC_STEP(12); goto done; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (got != SC_HDR_LEN + 1 || (unsigned char)small[SC_HDR_LEN] != 5) { SC_STEP(12); goto done; } |
1 |
0 |
1 |
| if (got != SC_HDR_LEN + 1 || (unsigned char)small[SC_HDR_LEN] != 5) { SC_STEP(12); goto done; } |
1 |
0 |
1 |
|
| 152 |
|
| } |
| 153 |
|
| |
| 154 |
1 |
| done: |
| 155 |
1 |
▶ | if (out) SvREFCNT_dec(out); |
Branch: 1 branch
| Branch | True | False | Total |
| if (out) SvREFCNT_dec(out); |
1 |
0 |
1 |
|
| 156 |
1 |
| return bytes; |
| 157 |
|
| } |
| 158 |
|
| |
| 159 |
|
| #undef SC_STEP |
| 160 |
|
| |
| 161 |
|
| #endif /* SC_ABI_IMPL_H */ |