| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| #include "easyxs/easyxs.h" |
| 3 |
|
| |
| 4 |
|
| #include <stdbool.h> |
| 5 |
|
| #include <string.h> |
| 6 |
|
| |
| 7 |
|
| #include "tomlc17.h" |
| 8 |
|
| |
| 9 |
|
| /* Disabled for production because adding subprocess detection |
| 10 |
|
| would entail having a separate struct for the objects, which |
| 11 |
|
| seems likely to degrade performance. |
| 12 |
|
| */ |
| 13 |
|
| #define DETECT_LEAKS 0 |
| 14 |
|
| |
| 15 |
|
| #define DOCUMENT_CLASS "TOML::XS::Document" |
| 16 |
|
| #define TIMESTAMP_CLASS "TOML::XS::Timestamp" |
| 17 |
|
| #define BOOLEAN_CLASS "TOML::XS" |
| 18 |
|
| |
| 19 |
|
| #define PERL_TRUE get_sv(BOOLEAN_CLASS "::true", 0) |
| 20 |
|
| #define PERL_FALSE get_sv(BOOLEAN_CLASS "::false", 0) |
| 21 |
|
| |
| 22 |
|
| #define UNUSED(x) (void)(x) |
| 23 |
|
| |
| 24 |
|
| #ifdef PL_phase |
| 25 |
|
| #define _IS_GLOBAL_DESTRUCT (PL_phase == PERL_PHASE_DESTRUCT) |
| 26 |
|
| #else |
| 27 |
|
| #define _IS_GLOBAL_DESTRUCT PL_dirty |
| 28 |
|
| #endif |
| 29 |
|
| |
| 30 |
|
| #define _verify_no_null(tomlstr, tomllen) \ |
| 31 |
|
| if (strchr(tomlstr, 0) != (tomlstr + tomllen)) { \ |
| 32 |
|
| croak( \ |
| 33 |
|
| "String contains a NUL at index %" UVf "!", \ |
| 34 |
|
| (UV)(strchr(tomlstr, 0) - tomlstr) \ |
| 35 |
|
| ); \ |
| 36 |
|
| } |
| 37 |
|
| |
| 38 |
|
| #define _verify_valid_utf8(tomlstr, tomllen) \ |
| 39 |
|
| if (!is_utf8_string( (const U8*)tomlstr, tomllen )) { \ |
| 40 |
|
| U8* ep; \ |
| 41 |
|
| const U8** epptr = (const U8**) &ep; \ |
| 42 |
|
| is_utf8_string_loc((const U8*)tomlstr, tomllen, epptr); \ |
| 43 |
|
| croak( \ |
| 44 |
|
| "String contains non-UTF-8 at index %" UVf "!", \ |
| 45 |
|
| (UV)((char*) ep - tomlstr) \ |
| 46 |
|
| ); \ |
| 47 |
|
| } |
| 48 |
|
| |
| 49 |
|
| const char* type_name[] = { |
| 50 |
|
| [TOML_STRING] = "string", |
| 51 |
|
| [TOML_INT64] = "integer", |
| 52 |
|
| [TOML_FP64] = "float", |
| 53 |
|
| [TOML_BOOLEAN] = "boolean", |
| 54 |
|
| [TOML_DATE] = "date", |
| 55 |
|
| [TOML_TIME] = "time", |
| 56 |
|
| [TOML_DATETIME] = "datetime", |
| 57 |
|
| [TOML_DATETIMETZ] = "datetime", |
| 58 |
|
| }; |
| 59 |
|
| |
| 60 |
26 |
| void append_tz_to_sv(pTHX_ int16_t minutes, SV* sv) { |
| 61 |
26 |
▶ | char sign = (minutes < 0) ? '-' : '+'; |
Branch: 1 branch
| Branch | True | False | Total |
| char sign = (minutes < 0) ? '-' : '+'; |
26 |
0 |
26 |
|
| 62 |
26 |
| int abs_minutes = abs(minutes); |
| 63 |
|
| |
| 64 |
26 |
| int hours = abs_minutes / 60; |
| 65 |
26 |
| int mins = abs_minutes % 60; |
| 66 |
|
| |
| 67 |
26 |
| sv_catpvf(sv, "%c%02d:%02d", sign, hours, mins); |
| 68 |
26 |
| } |
| 69 |
|
| |
| 70 |
|
| /* perlclib describes grok_atoUV(), but it’s not public. :( */ |
| 71 |
27 |
| bool my_grok_atoUV(pTHX_ const char *pv, UV *valuep) { |
| 72 |
27 |
| int numtype = grok_number(pv, strlen(pv), valuep); |
| 73 |
|
| |
| 74 |
|
| /* The presence of any other flag in numtype indicates that |
| 75 |
|
| something besides a simple unsigned int was given. */ |
| 76 |
27 |
▶ | if (numtype == IS_NUMBER_IN_UV) return true; |
Branch: 1 branch
| Branch | True | False | Total |
| if (numtype == IS_NUMBER_IN_UV) return true; |
25 |
2 |
27 |
|
| 77 |
|
| |
| 78 |
2 |
| return false; |
| 79 |
|
| } |
| 80 |
|
| |
| 81 |
6 |
| static inline SV* _call_pv_scalar_1_1 (pTHX_ const char* fn, SV* arg) { |
| 82 |
6 |
| dSP; |
| 83 |
|
| |
| 84 |
6 |
| ENTER; |
| 85 |
6 |
| SAVETMPS; |
| 86 |
|
| |
| 87 |
6 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
6 |
6 |
|
| 88 |
6 |
▶ | EXTEND(SP, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); |
0 |
6 |
6 |
|
| 89 |
|
| |
| 90 |
6 |
| mPUSHs(arg); |
| 91 |
|
| |
| 92 |
6 |
| PUTBACK; |
| 93 |
|
| |
| 94 |
6 |
| unsigned count = call_pv(fn, G_SCALAR); |
| 95 |
|
| |
| 96 |
6 |
| SPAGAIN; |
| 97 |
|
| |
| 98 |
|
| SV* ret; |
| 99 |
|
| |
| 100 |
6 |
▶ | if (count > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (count > 0) { |
6 |
0 |
6 |
|
| 101 |
6 |
| ret = newSVsv(POPs); |
| 102 |
|
| } |
| 103 |
|
| else { |
| 104 |
0 |
| ret = &PL_sv_undef; |
| 105 |
|
| } |
| 106 |
|
| |
| 107 |
6 |
| PUTBACK; |
| 108 |
6 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
6 |
0 |
6 |
|
| 109 |
6 |
| LEAVE; |
| 110 |
|
| |
| 111 |
6 |
| return ret; |
| 112 |
|
| } |
| 113 |
|
| |
| 114 |
6 |
| static inline SV* _make_json_pointer_sv (pTHX_ SV** stack, unsigned stack_idx) { |
| 115 |
6 |
| AV* pointer = newAV(); |
| 116 |
|
| |
| 117 |
20 |
▶ | for (unsigned i=0; i<=stack_idx; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned i=0; i<=stack_idx; i++) { |
14 |
6 |
20 |
|
| 118 |
14 |
| av_push(pointer, newSVsv(stack[i])); |
| 119 |
|
| } |
| 120 |
|
| |
| 121 |
6 |
| SV* pointer_ar = newRV_noinc( (SV*) pointer ); |
| 122 |
|
| |
| 123 |
6 |
| return _call_pv_scalar_1_1(aTHX_ "TOML::XS::_BUILD_JSON_POINTER", pointer_ar); |
| 124 |
|
| } |
| 125 |
|
| |
| 126 |
182 |
| toml_datum_t _get_toml_timestamp_from_sv(pTHX_ SV *self_sv) { |
| 127 |
182 |
| toml_datum_t* datum = exs_structref_ptr(self_sv); |
| 128 |
|
| |
| 129 |
182 |
| return *datum; |
| 130 |
|
| } |
| 131 |
|
| |
| 132 |
|
| SV* _toml_datum_to_sv(pTHX_ toml_datum_t datum); |
| 133 |
|
| |
| 134 |
22 |
| SV* _toml_table_to_sv(pTHX_ toml_datum_t datum) { |
| 135 |
22 |
▶ | ASSUME(datum.type == TOML_TABLE); |
Branch: 1 branch
| Branch | True | False | Total |
| ASSUME(datum.type == TOML_TABLE); |
0 |
22 |
22 |
|
| 136 |
|
| |
| 137 |
|
| /* Doesn’t need to be mortal since this should not throw. |
| 138 |
|
| Should that ever change this will need to be mortal then |
| 139 |
|
| de-mortalized. |
| 140 |
|
| */ |
| 141 |
22 |
| HV* hv = newHV(); |
| 142 |
|
| |
| 143 |
70 |
▶ | for (int i = 0; i < datum.u.tab.size; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0; i < datum.u.tab.size; i++) { |
48 |
22 |
70 |
|
| 144 |
48 |
| SV* sv = _toml_datum_to_sv(aTHX_ datum.u.tab.value[i]); |
| 145 |
48 |
▶ | ASSUME(sv != NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| ASSUME(sv != NULL); |
0 |
48 |
48 |
|
| 146 |
|
| |
| 147 |
48 |
| const char* key = datum.u.tab.key[i]; |
| 148 |
48 |
| int keylen = datum.u.tab.len[i]; |
| 149 |
|
| |
| 150 |
48 |
| hv_store(hv, key, -keylen, sv, 0); |
| 151 |
|
| } |
| 152 |
|
| |
| 153 |
22 |
| return newRV_noinc( (SV *) hv ); |
| 154 |
|
| } |
| 155 |
|
| |
| 156 |
10 |
| SV* _toml_array_to_sv(pTHX_ toml_datum_t datum) { |
| 157 |
10 |
▶ | ASSUME(datum.type == TOML_ARRAY); |
Branch: 1 branch
| Branch | True | False | Total |
| ASSUME(datum.type == TOML_ARRAY); |
0 |
10 |
10 |
|
| 158 |
|
| |
| 159 |
|
| /* Doesn’t need to be mortal since this should not throw. |
| 160 |
|
| Should that ever change this will need to be mortal then |
| 161 |
|
| de-mortalized. |
| 162 |
|
| */ |
| 163 |
10 |
| AV* av = newAV(); |
| 164 |
|
| |
| 165 |
|
| //int size = toml_array_nelem(arr); |
| 166 |
10 |
| int size = datum.u.arr.size; |
| 167 |
|
| |
| 168 |
10 |
| av_extend(av, size - 1); |
| 169 |
|
| |
| 170 |
47 |
▶ | for (int i = 0; i<size; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0; i<size; i++) { |
37 |
10 |
47 |
|
| 171 |
37 |
| SV* sv = _toml_datum_to_sv(aTHX_ datum.u.arr.elem[i]); |
| 172 |
37 |
▶ | ASSUME(sv != NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| ASSUME(sv != NULL); |
0 |
37 |
37 |
|
| 173 |
|
| |
| 174 |
37 |
| av_store(av, i, sv); |
| 175 |
|
| } |
| 176 |
|
| |
| 177 |
10 |
| return newRV_noinc( (SV *) av ); |
| 178 |
|
| } |
| 179 |
|
| |
| 180 |
|
| |
| 181 |
118 |
| SV* _toml_datum_to_sv(pTHX_ toml_datum_t datum) { |
| 182 |
|
| SV* ret; |
| 183 |
|
| |
| 184 |
118 |
| switch (datum.type) { |
| 185 |
|
| case TOML_UNKNOWN: |
| 186 |
|
| ASSUME(FALSE); |
| 187 |
23 |
| case TOML_STRING: |
| 188 |
23 |
| return newSVpvn_utf8(datum.u.str.ptr, datum.u.str.len, TRUE); |
| 189 |
16 |
| case TOML_INT64: |
| 190 |
16 |
| return newSViv(datum.u.int64); |
| 191 |
14 |
| case TOML_FP64: |
| 192 |
14 |
| return newSVnv(datum.u.fp64); |
| 193 |
18 |
| case TOML_BOOLEAN: |
| 194 |
18 |
▶ | return SvREFCNT_inc(datum.u.boolean ? PERL_TRUE : PERL_FALSE); |
Branch: 1 branch
| Branch | True | False | Total |
| return SvREFCNT_inc(datum.u.boolean ? PERL_TRUE : PERL_FALSE); |
7 |
11 |
18 |
|
| 195 |
15 |
| case TOML_DATE: |
| 196 |
|
| case TOML_TIME: |
| 197 |
|
| case TOML_DATETIME: |
| 198 |
|
| case TOML_DATETIMETZ: |
| 199 |
15 |
| ret = exs_new_structref(toml_datum_t, TIMESTAMP_CLASS); |
| 200 |
15 |
| memcpy(exs_structref_ptr(ret), &datum, sizeof(toml_datum_t)); |
| 201 |
15 |
| return ret; |
| 202 |
10 |
| case TOML_ARRAY: |
| 203 |
10 |
| return _toml_array_to_sv(aTHX_ datum); |
| 204 |
22 |
| case TOML_TABLE: |
| 205 |
22 |
| return _toml_table_to_sv(aTHX_ datum); |
| 206 |
0 |
| default: |
| 207 |
0 |
| break; |
| 208 |
|
| } |
| 209 |
|
| |
| 210 |
0 |
| ASSUME(FALSE); |
| 211 |
|
| } |
| 212 |
|
| |
| 213 |
|
| toml_datum_t _drill_into_array(pTHX_ toml_datum_t datum, SV** stack, unsigned stack_idx, unsigned drill_len); |
| 214 |
|
| |
| 215 |
2 |
| static inline void _croak_on_nonfinal_drill( pTHX_ toml_type_t type, SV** stack, unsigned stack_idx, unsigned drill_len) { |
| 216 |
2 |
| SV* jsonpointer = _make_json_pointer_sv(aTHX_ stack, stack_idx); |
| 217 |
2 |
| sv_2mortal(jsonpointer); |
| 218 |
|
| |
| 219 |
2 |
| croak("Cannot descend into non-container (%s)! (JSON pointer: %" SVf ")", type_name[type], jsonpointer); |
| 220 |
|
| |
| 221 |
|
| assert(0); |
| 222 |
|
| } |
| 223 |
|
| |
| 224 |
72 |
| toml_datum_t _drill_into_table(pTHX_ toml_datum_t tabin, SV** stack, unsigned stack_idx, unsigned drill_len) { |
| 225 |
72 |
▶ | ASSUME(tabin.type == TOML_TABLE); |
Branch: 1 branch
| Branch | True | False | Total |
| ASSUME(tabin.type == TOML_TABLE); |
0 |
72 |
72 |
|
| 226 |
|
| |
| 227 |
72 |
| SV* key_sv = stack[stack_idx]; |
| 228 |
|
| |
| 229 |
72 |
▶ | if (!SvOK(key_sv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(key_sv)) { |
1 |
71 |
72 |
|
| 230 |
1 |
| croak("Uninitialized value given in pointer (#%d)!", stack_idx); |
| 231 |
|
| } |
| 232 |
|
| |
| 233 |
71 |
| char* key = SvPVutf8_nolen(key_sv); |
| 234 |
|
| |
| 235 |
71 |
| toml_datum_t next = toml_get(tabin, key); |
| 236 |
|
| |
| 237 |
71 |
▶ | if (next.type == TOML_UNKNOWN) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (next.type == TOML_UNKNOWN) { |
1 |
70 |
71 |
|
| 238 |
1 |
| SV* json_pointer = _make_json_pointer_sv(aTHX_ stack, stack_idx); |
| 239 |
1 |
| sv_2mortal(json_pointer); |
| 240 |
1 |
| croak("element not found (JSON pointer: %" SVf ")", json_pointer); |
| 241 |
|
| } |
| 242 |
|
| |
| 243 |
70 |
▶ | if (stack_idx == drill_len-1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (stack_idx == drill_len-1) { |
9 |
61 |
70 |
|
| 244 |
9 |
| return next; |
| 245 |
|
| } |
| 246 |
|
| |
| 247 |
61 |
| switch (next.type) { |
| 248 |
34 |
| case TOML_UNKNOWN: |
| 249 |
|
| assert(0); |
| 250 |
|
| case TOML_TABLE: |
| 251 |
34 |
| return _drill_into_table(aTHX_ next, stack, 1 + stack_idx, drill_len); |
| 252 |
26 |
| case TOML_ARRAY: |
| 253 |
26 |
| return _drill_into_array(aTHX_ next, stack, 1 + stack_idx, drill_len); |
| 254 |
1 |
| default: |
| 255 |
1 |
| break; |
| 256 |
|
| } |
| 257 |
|
| |
| 258 |
1 |
| _croak_on_nonfinal_drill(aTHX_ next.type, stack, stack_idx, drill_len); |
| 259 |
|
| |
| 260 |
0 |
| ASSUME(FALSE); |
| 261 |
|
| return next; // silence compiler warning |
| 262 |
|
| } |
| 263 |
|
| |
| 264 |
28 |
| toml_datum_t _drill_into_array(pTHX_ toml_datum_t datum, SV** stack, unsigned stack_idx, unsigned drill_len) { |
| 265 |
28 |
▶ | ASSUME(datum.type == TOML_ARRAY); |
Branch: 1 branch
| Branch | True | False | Total |
| ASSUME(datum.type == TOML_ARRAY); |
0 |
28 |
28 |
|
| 266 |
|
| |
| 267 |
|
| int i; |
| 268 |
|
| |
| 269 |
28 |
| SV* key_sv = stack[stack_idx]; |
| 270 |
|
| |
| 271 |
28 |
▶ | if (SvUOK(key_sv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvUOK(key_sv)) { |
0 |
28 |
28 |
|
| 272 |
0 |
| i = SvUV(key_sv); |
| 273 |
|
| } |
| 274 |
28 |
▶ | else if (!SvOK(key_sv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (!SvOK(key_sv)) { |
1 |
27 |
28 |
|
| 275 |
1 |
| croak("Undef given as pointer value (#%d)!", stack_idx); |
| 276 |
|
| } |
| 277 |
|
| else { |
| 278 |
|
| UV idx_uv; |
| 279 |
|
| |
| 280 |
27 |
▶ | if (my_grok_atoUV(aTHX_ SvPVbyte_nolen(key_sv), &idx_uv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (my_grok_atoUV(aTHX_ SvPVbyte_nolen(key_sv), &idx_uv)) { |
25 |
2 |
27 |
|
| 281 |
25 |
| i = idx_uv; |
| 282 |
|
| } |
| 283 |
|
| else { |
| 284 |
2 |
| SV* json_pointer = _make_json_pointer_sv(aTHX_ stack, stack_idx - 1); |
| 285 |
2 |
| sv_2mortal(json_pointer); |
| 286 |
2 |
| croak("Invalid array index (%" SVf ") given to array (JSON pointer: %" SVf ")!", key_sv, json_pointer); |
| 287 |
|
| } |
| 288 |
|
| } |
| 289 |
|
| |
| 290 |
25 |
▶ | if (i >= datum.u.arr.size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i >= datum.u.arr.size) { |
1 |
24 |
25 |
|
| 291 |
1 |
| SV* json_pointer = _make_json_pointer_sv(aTHX_ stack, stack_idx); |
| 292 |
1 |
| sv_2mortal(json_pointer); |
| 293 |
1 |
| croak("Index exceeds max array index (%d; JSON pointer: %" SVf ")", datum.u.arr.size - 1, json_pointer); |
| 294 |
|
| } |
| 295 |
|
| |
| 296 |
24 |
| toml_datum_t next = datum.u.arr.elem[i]; |
| 297 |
|
| |
| 298 |
24 |
▶ | if (stack_idx == drill_len-1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (stack_idx == drill_len-1) { |
20 |
4 |
24 |
|
| 299 |
20 |
| return next; |
| 300 |
|
| } |
| 301 |
|
| |
| 302 |
4 |
| switch (next.type) { |
| 303 |
1 |
| case TOML_UNKNOWN: |
| 304 |
|
| assert(0); |
| 305 |
|
| case TOML_TABLE: |
| 306 |
1 |
| return _drill_into_table(aTHX_ next, stack, 1 + stack_idx, drill_len); |
| 307 |
2 |
| case TOML_ARRAY: |
| 308 |
2 |
| return _drill_into_array(aTHX_ next, stack, 1 + stack_idx, drill_len); |
| 309 |
1 |
| default: |
| 310 |
1 |
| break; |
| 311 |
|
| } |
| 312 |
|
| |
| 313 |
1 |
| _croak_on_nonfinal_drill(aTHX_ next.type, stack, stack_idx, drill_len); |
| 314 |
|
| |
| 315 |
|
| assert(0); |
| 316 |
0 |
| return next; // silence compiler warning |
| 317 |
|
| } |
| 318 |
|
| |
| 319 |
|
| /* ---------------------------------------------------------------------- */ |
| 320 |
|
| |
| 321 |
|
| MODULE = TOML::XS PACKAGE = TOML::XS |
| 322 |
|
| |
| 323 |
|
| PROTOTYPES: DISABLE |
| 324 |
|
| |
| 325 |
|
| SV* |
| 326 |
|
| from_toml (SV* tomlsv) |
| 327 |
|
| CODE: |
| 328 |
|
| STRLEN tomllen; |
| 329 |
10 |
| char* tomlstr = SvPVbyte(tomlsv, tomllen); |
| 330 |
|
| |
| 331 |
10 |
▶ | _verify_no_null(tomlstr, tomllen); |
Branch: 1 branch
| Branch | True | False | Total |
| _verify_no_null(tomlstr, tomllen); |
1 |
9 |
10 |
|
| 332 |
|
| |
| 333 |
9 |
▶ | _verify_valid_utf8(tomlstr, tomllen); |
Branch: 1 branch
| Branch | True | False | Total |
| _verify_valid_utf8(tomlstr, tomllen); |
1 |
8 |
9 |
|
| 334 |
|
| |
| 335 |
8 |
| toml_result_t res = toml_parse(tomlstr, tomllen); |
| 336 |
|
| |
| 337 |
8 |
▶ | if (!res.ok) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!res.ok) { |
3 |
5 |
8 |
|
| 338 |
3 |
| toml_free(res); |
| 339 |
3 |
| croak("failed to parse TOML: %s", res.errmsg); |
| 340 |
|
| } |
| 341 |
|
| |
| 342 |
5 |
| SV* retsv = exs_new_structref(toml_result_t, DOCUMENT_CLASS); |
| 343 |
5 |
| memcpy(exs_structref_ptr(retsv), &res, sizeof(toml_result_t)); |
| 344 |
|
| |
| 345 |
5 |
| RETVAL = retsv; |
| 346 |
|
| OUTPUT: |
| 347 |
|
| RETVAL |
| 348 |
|
| |
| 349 |
|
| # ---------------------------------------------------------------------- |
| 350 |
|
| |
| 351 |
|
| INCLUDE: Document.xs |
| 352 |
|
| INCLUDE: Timestamp.xs |