Repository navigation
Expand file tree
/
Copy pathsource_map.cpp
More file actions
629 lines (589 loc) · 16.8 KB
/
Copy pathsource_map.cpp
File metadata and controls
629 lines (589 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#include "source_map.h"
#include <algorithm>
#include <array>
#include <cctype>
#include <cstdint>
#include <cstdlib>
#include <iterator>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
namespace huxerui::tsx::detail {
namespace {
constexpr std::string_view kSourceMappingUrl = "sourceMappingURL=";
constexpr std::string_view kBase64Marker = ";base64,";
constexpr std::string_view kDataPrefix = "data:";
int Base64Value(char character) noexcept {
if (character >= 'A' && character <= 'Z') {
return character - 'A';
}
if (character >= 'a' && character <= 'z') {
return character - 'a' + 26;
}
if (character >= '0' && character <= '9') {
return character - '0' + 52;
}
if (character == '+') {
return 62;
}
if (character == '/') {
return 63;
}
return -1;
}
std::optional<std::string> DecodeBase64(std::string_view text) {
std::string decoded;
decoded.reserve(text.size() * 3 / 4);
std::uint32_t accumulator = 0;
int bits = 0;
for (const char character : text) {
if (character == '=') {
break;
}
if (std::isspace(static_cast<unsigned char>(character)) != 0) {
continue;
}
const int value = Base64Value(character);
if (value < 0) {
return std::nullopt;
}
accumulator = (accumulator << 6U) | static_cast<std::uint32_t>(value);
bits += 6;
if (bits >= 8) {
bits -= 8;
decoded.push_back(static_cast<char>((accumulator >> static_cast<unsigned>(bits)) & 0xFFU));
accumulator &= (1U << static_cast<unsigned>(bits)) - 1U;
}
}
return decoded;
}
std::optional<std::string> ExtractInlineSourceMap(std::string_view bundle) {
const auto marker = bundle.rfind(kSourceMappingUrl);
if (marker == std::string_view::npos) {
return std::nullopt;
}
std::string_view value = bundle.substr(marker + kSourceMappingUrl.size());
if (const auto end = value.find_first_of("\r\n"); end != std::string_view::npos) {
value = value.substr(0, end);
}
if (!value.starts_with(kDataPrefix)) {
return std::nullopt;
}
const auto base64 = value.find(kBase64Marker);
if (base64 == std::string_view::npos) {
return std::nullopt;
}
return DecodeBase64(value.substr(base64 + kBase64Marker.size()));
}
void AppendUtf8(std::string& out, std::uint32_t code_point) {
if (code_point <= 0x7FU) {
out.push_back(static_cast<char>(code_point));
} else if (code_point <= 0x7FFU) {
out.push_back(static_cast<char>(0xC0U | (code_point >> 6U)));
out.push_back(static_cast<char>(0x80U | (code_point & 0x3FU)));
} else if (code_point <= 0xFFFFU) {
out.push_back(static_cast<char>(0xE0U | (code_point >> 12U)));
out.push_back(static_cast<char>(0x80U | ((code_point >> 6U) & 0x3FU)));
out.push_back(static_cast<char>(0x80U | (code_point & 0x3FU)));
} else {
out.push_back(static_cast<char>(0xF0U | (code_point >> 18U)));
out.push_back(static_cast<char>(0x80U | ((code_point >> 12U) & 0x3FU)));
out.push_back(static_cast<char>(0x80U | ((code_point >> 6U) & 0x3FU)));
out.push_back(static_cast<char>(0x80U | (code_point & 0x3FU)));
}
}
struct SourceMapFields {
std::vector<std::string> sources;
std::string source_root;
std::string mappings;
};
/// Minimal JSON reader for the source-map fields the bridge consumes. Unknown
/// values are skipped structurally, including `sourcesContent` strings.
class JsonReader final {
public:
explicit JsonReader(std::string_view text) noexcept : text_(text) {}
bool Parse(SourceMapFields& fields) {
SkipWhitespace();
if (!Consume('{')) {
return false;
}
SkipWhitespace();
if (Consume('}')) {
return AtEnd();
}
while (true) {
std::string key;
if (!ParseString(key)) {
return false;
}
SkipWhitespace();
if (!Consume(':')) {
return false;
}
SkipWhitespace();
if (key == "sources") {
if (!ParseStringArray(fields.sources)) {
return false;
}
} else if (key == "sourceRoot") {
if (!ParseString(fields.source_root)) {
return false;
}
} else if (key == "mappings") {
if (!ParseString(fields.mappings)) {
return false;
}
} else if (!SkipValue()) {
return false;
}
SkipWhitespace();
if (Consume(',')) {
SkipWhitespace();
continue;
}
return Consume('}') && AtEnd();
}
}
private:
[[nodiscard]] bool AtEnd() {
SkipWhitespace();
return position_ >= text_.size();
}
bool Consume(char expected) {
if (position_ < text_.size() && text_[position_] == expected) {
++position_;
return true;
}
return false;
}
void SkipWhitespace() {
while (position_ < text_.size()
&& std::isspace(static_cast<unsigned char>(text_[position_])) != 0) {
++position_;
}
}
bool ParseHex4(std::uint32_t& value) {
if (position_ + 4 > text_.size()) {
return false;
}
std::uint32_t parsed = 0;
for (int index = 0; index < 4; ++index) {
const char character = text_[position_++];
parsed <<= 4U;
if (character >= '0' && character <= '9') {
parsed |= static_cast<std::uint32_t>(character - '0');
} else if (character >= 'a' && character <= 'f') {
parsed |= static_cast<std::uint32_t>(character - 'a' + 10);
} else if (character >= 'A' && character <= 'F') {
parsed |= static_cast<std::uint32_t>(character - 'A' + 10);
} else {
return false;
}
}
value = parsed;
return true;
}
bool ParseString(std::string& out) {
out.clear();
if (!Consume('"')) {
return false;
}
while (position_ < text_.size()) {
const auto character = static_cast<unsigned char>(text_[position_++]);
if (character == '"') {
return true;
}
if (character < 0x20U) {
return false;
}
if (character != '\\') {
out.push_back(static_cast<char>(character));
continue;
}
if (position_ >= text_.size()) {
return false;
}
switch (text_[position_++]) {
case '"':
out.push_back('"');
break;
case '\\':
out.push_back('\\');
break;
case '/':
out.push_back('/');
break;
case 'b':
out.push_back('\b');
break;
case 'f':
out.push_back('\f');
break;
case 'n':
out.push_back('\n');
break;
case 'r':
out.push_back('\r');
break;
case 't':
out.push_back('\t');
break;
case 'u': {
std::uint32_t code_point = 0;
if (!ParseHex4(code_point)) {
return false;
}
if (code_point >= 0xD800U && code_point <= 0xDBFFU) {
if (position_ + 1 >= text_.size() || text_[position_] != '\\'
|| text_[position_ + 1] != 'u') {
return false;
}
position_ += 2;
std::uint32_t low = 0;
if (!ParseHex4(low) || low < 0xDC00U || low > 0xDFFFU) {
return false;
}
code_point = 0x10000U + ((code_point - 0xD800U) << 10U) + (low - 0xDC00U);
} else if (code_point >= 0xDC00U && code_point <= 0xDFFFU) {
return false;
}
AppendUtf8(out, code_point);
break;
}
default:
return false;
}
}
return false;
}
bool ParseStringArray(std::vector<std::string>& out) {
out.clear();
if (!Consume('[')) {
return false;
}
SkipWhitespace();
if (Consume(']')) {
return true;
}
while (true) {
std::string value;
if (!ParseString(value)) {
return false;
}
out.push_back(std::move(value));
SkipWhitespace();
if (Consume(',')) {
SkipWhitespace();
continue;
}
return Consume(']');
}
}
bool ConsumeLiteral(std::string_view literal) {
if (text_.substr(position_, literal.size()) != literal) {
return false;
}
position_ += literal.size();
return true;
}
bool SkipValue() {
SkipWhitespace();
if (position_ >= text_.size()) {
return false;
}
const char character = text_[position_];
if (character == '"') {
std::string ignored;
return ParseString(ignored);
}
if (character == '{') {
++position_;
SkipWhitespace();
if (Consume('}')) {
return true;
}
while (true) {
std::string key;
if (!ParseString(key)) {
return false;
}
SkipWhitespace();
if (!Consume(':')) {
return false;
}
if (!SkipValue()) {
return false;
}
SkipWhitespace();
if (Consume(',')) {
SkipWhitespace();
continue;
}
return Consume('}');
}
}
if (character == '[') {
++position_;
SkipWhitespace();
if (Consume(']')) {
return true;
}
while (true) {
if (!SkipValue()) {
return false;
}
SkipWhitespace();
if (Consume(',')) {
SkipWhitespace();
continue;
}
return Consume(']');
}
}
if (character == 't') {
return ConsumeLiteral("true");
}
if (character == 'f') {
return ConsumeLiteral("false");
}
if (character == 'n') {
return ConsumeLiteral("null");
}
if (character == '-' || (character >= '0' && character <= '9')) {
++position_;
while (position_ < text_.size()) {
const char next = text_[position_];
const bool number_character = (next >= '0' && next <= '9') || next == '.'
|| next == 'e' || next == 'E' || next == '+' || next == '-';
if (!number_character) {
break;
}
++position_;
}
return true;
}
return false;
}
std::string_view text_;
std::size_t position_ = 0;
};
std::optional<int> DecodeVlq(std::string_view text, std::size_t& position) {
std::uint32_t value = 0;
int shift = 0;
while (true) {
if (position >= text.size()) {
return std::nullopt;
}
const int digit = Base64Value(text[position++]);
if (digit < 0) {
return std::nullopt;
}
value |= static_cast<std::uint32_t>(digit & 31) << static_cast<unsigned>(shift);
if ((digit & 32) == 0) {
break;
}
shift += 5;
if (shift > 30) {
return std::nullopt;
}
}
const bool negative = (value & 1U) != 0;
const auto magnitude = static_cast<int>(value >> 1U);
return negative ? -magnitude : magnitude;
}
bool BuildLines(
std::string_view mappings,
std::size_t source_count,
std::vector<std::vector<SourceMap::Segment>>& lines
) {
lines.clear();
lines.emplace_back();
std::int64_t source = 0;
std::int64_t source_line = 0;
std::int64_t source_column = 0;
std::uint32_t generated_column = 0;
std::size_t position = 0;
while (position < mappings.size()) {
const char character = mappings[position];
if (character == ';') {
lines.emplace_back();
generated_column = 0;
++position;
continue;
}
if (character == ',') {
++position;
continue;
}
std::array<int, 5> fields{};
std::size_t count = 0;
while (position < mappings.size() && mappings[position] != ','
&& mappings[position] != ';') {
if (count == fields.size()) {
return false;
}
const auto field = DecodeVlq(mappings, position);
if (!field) {
return false;
}
fields[count++] = *field;
}
if (count == 0 || count == 2 || count == 3) {
return false;
}
const std::int64_t next_column = static_cast<std::int64_t>(generated_column) + fields[0];
if (next_column < 0) {
return false;
}
generated_column = static_cast<std::uint32_t>(next_column);
SourceMap::Segment segment;
segment.generated_column = generated_column;
if (count >= 4) {
source += fields[1];
source_line += fields[2];
source_column += fields[3];
if (source < 0 || source_line < 0 || source_column < 0) {
return false;
}
segment.source = static_cast<std::uint32_t>(source);
segment.line = static_cast<std::uint32_t>(source_line);
segment.column = static_cast<std::uint32_t>(source_column);
segment.mapped = segment.source < source_count;
}
lines.back().push_back(segment);
}
return true;
}
bool IsFrameBoundary(char character) noexcept {
return character == '(' || character == ' ' || character == '\t' || character == '\n';
}
} // namespace
std::optional<SourceMap> SourceMap::FromBundle(std::string_view bundle) {
const auto json = ExtractInlineSourceMap(bundle);
if (!json) {
return std::nullopt;
}
return FromJson(*json);
}
std::optional<SourceMap> SourceMap::FromJson(std::string_view json) {
SourceMapFields fields;
JsonReader reader(json);
if (!reader.Parse(fields) || fields.mappings.empty() || fields.sources.empty()) {
return std::nullopt;
}
SourceMap map;
map.sources_ = std::move(fields.sources);
map.source_root_ = std::move(fields.source_root);
if (!BuildLines(fields.mappings, map.sources_.size(), map.generated_lines_)) {
return std::nullopt;
}
return map;
}
std::optional<SourcePosition> SourceMap::Find(
int line,
int column,
const std::filesystem::path& base
) const {
if (line <= 0 || static_cast<std::size_t>(line) > generated_lines_.size()) {
return std::nullopt;
}
const auto& segments = generated_lines_[static_cast<std::size_t>(line - 1)];
if (segments.empty()) {
return std::nullopt;
}
const auto target = static_cast<std::uint32_t>(column > 0 ? column - 1 : 0);
const auto found = std::upper_bound(
segments.begin(),
segments.end(),
target,
[](std::uint32_t value, const Segment& segment) {
return value < segment.generated_column;
}
);
if (found == segments.begin()) {
return std::nullopt;
}
const auto& segment = *std::prev(found);
if (!segment.mapped || segment.source >= sources_.size()) {
return std::nullopt;
}
std::filesystem::path path(sources_[segment.source]);
if (path.is_relative() && !source_root_.empty()) {
path = std::filesystem::path(source_root_) / path;
}
if (path.is_relative() && !base.empty()) {
path = base / path;
}
return SourcePosition{
path.lexically_normal().generic_string(),
static_cast<int>(segment.line) + 1,
static_cast<int>(segment.column) + 1,
};
}
std::string RemapStack(
std::string_view stack,
std::string_view generated_filename,
const SourceMap& source_map,
const std::filesystem::path& base
) {
if (stack.empty() || generated_filename.empty()) {
return std::string(stack);
}
std::string remapped;
remapped.reserve(stack.size());
std::size_t cursor = 0;
while (cursor < stack.size()) {
const auto found = stack.find(generated_filename, cursor);
if (found == std::string_view::npos) {
remapped.append(stack.substr(cursor));
break;
}
if (found > cursor && !IsFrameBoundary(stack[found - 1])) {
remapped.append(stack.substr(cursor, found - cursor + generated_filename.size()));
cursor = found + generated_filename.size();
continue;
}
remapped.append(stack.substr(cursor, found - cursor));
std::size_t after = found + generated_filename.size();
if (after >= stack.size() || stack[after] != ':') {
remapped.append(generated_filename);
cursor = after;
continue;
}
std::size_t line_start = after + 1;
std::size_t line_end = line_start;
while (line_end < stack.size() && std::isdigit(static_cast<unsigned char>(stack[line_end])) != 0) {
++line_end;
}
std::size_t column_end = line_end;
if (line_end > line_start && line_end < stack.size() && stack[line_end] == ':') {
column_end = line_end + 1;
while (column_end < stack.size()
&& std::isdigit(static_cast<unsigned char>(stack[column_end])) != 0) {
++column_end;
}
}
const bool has_location = line_end > line_start && column_end > line_end + 1;
if (has_location) {
const int line = std::atoi(std::string(stack.substr(line_start, line_end - line_start)).c_str());
const int column = std::atoi(
std::string(stack.substr(line_end + 1, column_end - line_end - 1)).c_str()
);
if (const auto mapped = source_map.Find(line, column, base)) {
remapped.append(mapped->filename);
remapped.push_back(':');
remapped.append(std::to_string(mapped->line));
remapped.push_back(':');
remapped.append(std::to_string(mapped->column));
cursor = column_end;
continue;
}
}
remapped.append(stack.substr(found, after - found));
cursor = after;
}
return remapped;
}
} // namespace huxerui::tsx::detail