| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| #include "EXTERN.h" |
| 3 |
|
| #include "perl.h" |
| 4 |
|
| #include "XSUB.h" |
| 5 |
|
| |
| 6 |
|
| #include "message.h" |
| 7 |
|
| |
| 8 |
|
| /* |
| 9 |
|
| * struct message |
| 10 |
|
| */ |
| 11 |
|
| |
| 12 |
1 |
| void S_destroy_message(pTHX_ const message* message_) { |
| 13 |
1 |
| PerlMemShared_free((message*)message_); |
| 14 |
1 |
| } |
| 15 |
|
| |
| 16 |
1 |
| static SV* S_message_get_sv(pTHX_ const message* message) { |
| 17 |
1 |
| SV* stored = newSVpvn(message->value, message->length); |
| 18 |
1 |
| return stored; |
| 19 |
|
| } |
| 20 |
|
| |
| 21 |
|
| #define message_get_sv(message) S_message_get_sv(aTHX_ message) |
| 22 |
|
| |
| 23 |
1 |
| static const message* S_message_new_sv(pTHX_ SV* value, enum message_type type) { |
| 24 |
|
| message* message; |
| 25 |
|
| const char* string; |
| 26 |
|
| STRLEN len; |
| 27 |
1 |
| string = SvPV(value, len); |
| 28 |
1 |
| message = PerlMemShared_calloc(1, sizeof(*message) + len + 1); |
| 29 |
1 |
| message->type = type; |
| 30 |
1 |
| message->length = len; |
| 31 |
1 |
| Copy(string, message->value, len, char); |
| 32 |
1 |
| return message; |
| 33 |
|
| } |
| 34 |
|
| |
| 35 |
|
| #define message_new_sv(value, type) S_message_new_sv(aTHX_ value, type) |
| 36 |
|
| |
| 37 |
0 |
| const message* S_message_store_value(pTHX_ SV* value) { |
| 38 |
0 |
| dSP; |
| 39 |
|
| const message* ret; |
| 40 |
0 |
| ENTER; |
| 41 |
0 |
| SAVETMPS; |
| 42 |
0 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
0 |
0 |
|
| 43 |
0 |
▶ | XPUSHs(sv_2mortal(newRV_inc(value))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newRV_inc(value))); |
0 |
0 |
0 |
|
| 44 |
0 |
| PUTBACK; |
| 45 |
0 |
| call_pv("Sereal::Encoder::encode_sereal", G_SCALAR); |
| 46 |
0 |
| SPAGAIN; |
| 47 |
0 |
| ret = message_new_sv(POPs, SEREAL); |
| 48 |
0 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
0 |
0 |
0 |
|
| 49 |
0 |
| LEAVE; |
| 50 |
0 |
| PUTBACK; |
| 51 |
0 |
| return ret; |
| 52 |
|
| } |
| 53 |
|
| |
| 54 |
1 |
| static int S_is_simple(pTHX_ SV* value) { |
| 55 |
1 |
▶ | return SvOK(value) && !SvROK(value) && !(SvPOK(value) && SvUTF8(value)); |
Branch: 4 branches
| Branch | True | False | Total |
| return SvOK(value) && !SvROK(value) && !(SvPOK(value) && SvUTF8(value)); |
1 |
0 |
1 |
| return SvOK(value) && !SvROK(value) && !(SvPOK(value) && SvUTF8(value)); |
1 |
0 |
1 |
| return SvOK(value) && !SvROK(value) && !(SvPOK(value) && SvUTF8(value)); |
1 |
0 |
1 |
| return SvOK(value) && !SvROK(value) && !(SvPOK(value) && SvUTF8(value)); |
1 |
0 |
1 |
|
| 56 |
|
| } |
| 57 |
|
| #define is_simple(value) S_is_simple(aTHX_ value) |
| 58 |
|
| |
| 59 |
1 |
| static int S_are_simple(pTHX_ SV** begin, SV** end) { |
| 60 |
|
| SV** current; |
| 61 |
2 |
▶ | for(current = begin; current <= end; current++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(current = begin; current <= end; current++) |
1 |
1 |
2 |
|
| 62 |
1 |
▶ | if (! is_simple(*current)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (! is_simple(*current)) |
0 |
1 |
1 |
|
| 63 |
0 |
| return FALSE; |
| 64 |
1 |
| return TRUE; |
| 65 |
|
| } |
| 66 |
|
| |
| 67 |
|
| #define are_simple(begin, end) S_are_simple(aTHX_ begin, end) |
| 68 |
|
| |
| 69 |
|
| static const char pack_template[] = "(I/a)*"; |
| 70 |
|
| |
| 71 |
1 |
| const message* S_message_from_stack(pTHX) { |
| 72 |
1 |
| dSP; dMARK; |
| 73 |
1 |
▶ | if (SP == MARK && is_simple(*SP)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SP == MARK && is_simple(*SP)) { |
0 |
1 |
1 |
| if (SP == MARK && is_simple(*SP)) { |
0 |
0 |
0 |
|
| 74 |
0 |
| return message_new_sv(MARK[0], STRING); |
| 75 |
|
| } |
| 76 |
1 |
▶ | else if (are_simple(MARK + 1, SP)) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (are_simple(MARK + 1, SP)) { |
1 |
0 |
1 |
|
| 77 |
1 |
| SV* tmp = sv_2mortal(newSVpvn("", 0)); |
| 78 |
1 |
| packlist(tmp, pack_template, pack_template + sizeof pack_template - 1, MARK + 1, SP + 1); |
| 79 |
1 |
| return message_new_sv(tmp, PACKED); |
| 80 |
|
| } |
| 81 |
|
| else { |
| 82 |
0 |
| SV* list = sv_2mortal((SV*)av_make(SP - MARK, MARK + 1)); |
| 83 |
0 |
| return message_store_value(list); |
| 84 |
|
| } |
| 85 |
|
| } |
| 86 |
|
| |
| 87 |
0 |
| SV* S_message_load_value(pTHX_ const message* message) { |
| 88 |
0 |
| dSP; |
| 89 |
|
| SV* ret; |
| 90 |
|
| |
| 91 |
0 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
0 |
0 |
|
| 92 |
0 |
▶ | XPUSHs(sv_2mortal(message_get_sv(message))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(message_get_sv(message))); |
0 |
0 |
0 |
|
| 93 |
0 |
| PUTBACK; |
| 94 |
0 |
| call_pv("Sereal::Decoder::decode_sereal", G_SCALAR); |
| 95 |
0 |
| SPAGAIN; |
| 96 |
0 |
| ret = POPs; |
| 97 |
0 |
| PUTBACK; |
| 98 |
0 |
| return ret; |
| 99 |
|
| } |
| 100 |
|
| |
| 101 |
1 |
| void S_message_to_stack(pTHX_ const message* message, U32 context) { |
| 102 |
1 |
| dSP; |
| 103 |
1 |
| switch(message->type) { |
| 104 |
0 |
| case STRING: |
| 105 |
0 |
| PUSHs(sv_2mortal(newRV_noinc(message_get_sv(message)))); |
| 106 |
0 |
| break; |
| 107 |
1 |
| case PACKED: { |
| 108 |
1 |
| SV* mess = sv_2mortal(message_get_sv(message)); |
| 109 |
|
| STRLEN len; |
| 110 |
1 |
| const char* packed = SvPV(mess, len); |
| 111 |
1 |
| PUTBACK; |
| 112 |
1 |
| unpackstring(pack_template, pack_template + sizeof pack_template - 1, packed, packed + len, 0); |
| 113 |
1 |
| SPAGAIN; |
| 114 |
1 |
| break; |
| 115 |
|
| } |
| 116 |
0 |
| case SEREAL: { |
| 117 |
0 |
| AV* values = (AV*) SvRV(message_load_value(message)); |
| 118 |
0 |
| SPAGAIN; |
| 119 |
|
| |
| 120 |
0 |
▶ | if (context == G_SCALAR) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (context == G_SCALAR) { |
0 |
0 |
0 |
|
| 121 |
0 |
| SV** ret = av_fetch(values, 0, FALSE); |
| 122 |
0 |
▶ | PUSHs(ret ? *ret : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHs(ret ? *ret : &PL_sv_undef); |
0 |
0 |
0 |
|
| 123 |
|
| } |
| 124 |
0 |
▶ | else if (context == G_ARRAY) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (context == G_ARRAY) { |
0 |
0 |
0 |
|
| 125 |
0 |
| UV count = av_len(values) + 1; |
| 126 |
0 |
▶ | EXTEND(SP, count); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, count); |
0 |
0 |
0 |
|
| 127 |
0 |
▶ | Copy(AvARRAY(values), SP + 1, count, SV*); |
Branch: 1 branch
| Branch | True | False | Total |
| Copy(AvARRAY(values), SP + 1, count, SV*); |
0 |
0 |
0 |
|
| 128 |
0 |
| SP += count; |
| 129 |
|
| } |
| 130 |
0 |
| break; |
| 131 |
|
| } |
| 132 |
0 |
| default: |
| 133 |
0 |
| Perl_croak(aTHX_ "Type %d is not yet implemented", message->type); |
| 134 |
|
| } |
| 135 |
|
| |
| 136 |
1 |
| PUTBACK; |
| 137 |
1 |
| } |
| 138 |
|
| |
| 139 |
0 |
| AV* S_message_to_array(pTHX_ const message* message) { |
| 140 |
0 |
| dSP; |
| 141 |
|
| AV* ret; |
| 142 |
0 |
| switch(message->type) { |
| 143 |
0 |
| case STRING: |
| 144 |
0 |
| ret = newAV(); |
| 145 |
0 |
| av_push(ret, message_get_sv(message)); |
| 146 |
0 |
| break; |
| 147 |
0 |
| case PACKED: { |
| 148 |
0 |
| SV* mess = message_get_sv(message); |
| 149 |
|
| STRLEN len; |
| 150 |
|
| int count; |
| 151 |
0 |
| const char* packed = SvPV(mess, len); |
| 152 |
0 |
| SV** mark = SP; |
| 153 |
0 |
| PUTBACK; |
| 154 |
0 |
| count = unpackstring(pack_template, pack_template + sizeof pack_template - 1, packed, packed + len, 0); |
| 155 |
0 |
| SPAGAIN; |
| 156 |
0 |
| ret = av_make(count, mark + 1); |
| 157 |
0 |
| break; |
| 158 |
|
| } |
| 159 |
0 |
| case SEREAL: { |
| 160 |
0 |
| ret = (AV*)SvREFCNT_inc(SvRV(message_load_value(message))); |
| 161 |
0 |
| SPAGAIN; |
| 162 |
0 |
| break; |
| 163 |
|
| } |
| 164 |
0 |
| default: |
| 165 |
0 |
| Perl_croak(aTHX_ "Type %d is not yet implemented", message->type); |
| 166 |
|
| } |
| 167 |
0 |
| PUTBACK; |
| 168 |
|
| |
| 169 |
0 |
| return ret; |
| 170 |
|
| } |
| 171 |
|
| |
| 172 |
0 |
| const message* S_message_clone(pTHX_ const message* origin) { |
| 173 |
|
| //return savesharedpvn(origin, sizeof(message) + origin->length + 1) |
| 174 |
0 |
| size_t size = sizeof(message) + origin->length + 1; |
| 175 |
0 |
| message* clone = PerlMemShared_calloc(1, size); |
| 176 |
0 |
| Copy(origin, clone, size, char); |
| 177 |
0 |
| return clone; |
| 178 |
|
| } |
| 179 |
|
| |