/
SourceMap.cpp
627 lines (512 loc) · 24.5 KB
/
SourceMap.cpp
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
#include <iostream>
#include <sstream>
#include "SourceMap.h"
#include "sourcemap-schema_generated.h"
SourceMapBinding::SourceMapBinding(const Napi::CallbackInfo &info) : Napi::ObjectWrap<SourceMapBinding>(info) {
// Just create instance...
}
SourceMapBinding::~SourceMapBinding() {}
void SourceMapBinding::addRawMappings(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() < 3) {
Napi::TypeError::New(env, "Expected 3-5 parameters").ThrowAsJavaScriptException();
return;
}
if (!info[0].IsString()) {
Napi::TypeError::New(env, "First parameter should be a string").ThrowAsJavaScriptException();
return;
}
if (!info[1].IsArray() || !info[2].IsArray()) {
Napi::TypeError::New(env,
"Second and third parameter should be an array of strings").ThrowAsJavaScriptException();
return;
}
if (info.Length() > 3 && !info[3].IsNumber()) {
Napi::TypeError::New(env, "Fourth parameter should be a positive number").ThrowAsJavaScriptException();
return;
}
if (info.Length() > 4 && !info[4].IsNumber()) {
Napi::TypeError::New(env, "Fifth parameter should be a positive number").ThrowAsJavaScriptException();
return;
}
std::string rawMappings = info[0].As<Napi::String>().Utf8Value();
Napi::Array sources = info[1].As<Napi::Array>();
Napi::Array names = info[2].As<Napi::Array>();
int lineOffset = info.Length() > 3 ? info[3].As<Napi::Number>().Int32Value() : 0;
int columnOffset = info.Length() > 4 ? info[4].As<Napi::Number>().Int32Value() : 0;
std::vector<int> namesIndex = _addNames(names);
std::vector<int> sourcesIndex = _addSources(sources);
_mapping_container.addVLQMappings(rawMappings, sourcesIndex, namesIndex, lineOffset, columnOffset);
}
void SourceMapBinding::addBufferMappings(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() < 1) {
Napi::TypeError::New(env, "Expected 1-3 parameters").ThrowAsJavaScriptException();
return;
}
if (!info[0].IsBuffer()) {
Napi::TypeError::New(env, "Expected a Buffer for the first parameter").ThrowAsJavaScriptException();
return;
}
if (info.Length() > 1 && !info[1].IsNumber()) {
Napi::TypeError::New(env, "Expected a lineOffset of type integer for the second parameter").ThrowAsJavaScriptException();
return;
}
if (info.Length() > 2 && !info[2].IsNumber()) {
Napi::TypeError::New(env, "Expected a columnOffset of type integer for the third parameter").ThrowAsJavaScriptException();
return;
}
auto mapBuffer = info[0].As<Napi::Buffer<uint8_t>>();
int lineOffset = info.Length() > 1 ? info[1].As<Napi::Number>().Int32Value() : 0;
int columnOffset = info.Length() > 2 ? info[2].As<Napi::Number>().Int32Value() : 0;
auto map = SourceMapSchema::GetMap(mapBuffer.Data());
std::vector<int> sources;
auto sourcesArray = map->sources();
sources.reserve(sourcesArray->size());
auto sourcesEnd = sourcesArray->end();
for (auto it = sourcesArray->begin(); it != sourcesEnd; ++it) {
std::string source = it->str();
sources.push_back(_mapping_container.addSource(source));
}
std::vector<int> names;
auto namesArray = map->names();
names.reserve(namesArray->size());
auto namesEnd = namesArray->end();
for (auto it = namesArray->begin(); it != namesEnd; ++it) {
std::string name = it->str();
names.push_back(_mapping_container.addName(name));
}
_mapping_container.createLinesIfUndefined(map->lineCount() + lineOffset);
auto lines = map->lines();
auto linesEnd = lines->end();
for (auto linesIterator = map->lines()->begin(); linesIterator != linesEnd; ++linesIterator) {
auto line = (*linesIterator);
auto segments = line->segments();
auto segmentsEnd = segments->end();
for (auto segmentIterator = segments->begin(); segmentIterator != segmentsEnd; ++segmentIterator) {
Position generated = Position{segmentIterator->generatedLine() + lineOffset,
segmentIterator->generatedColumn() + columnOffset};
Position original = Position{segmentIterator->originalLine(), segmentIterator->originalColumn()};
int source = segmentIterator->source() > -1 ? sources[segmentIterator->source()] : -1;
int name = segmentIterator->name() > -1 ? names[segmentIterator->name()] : -1;
_mapping_container.addMapping(generated, original, source, name);
}
}
}
void SourceMapBinding::extends(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() < 1) {
Napi::TypeError::New(env, "Expected 1 parameter of type buffer").ThrowAsJavaScriptException();
return;
}
if (!info[0].IsBuffer()) {
Napi::TypeError::New(env, "Expected a buffer for the first parameter").ThrowAsJavaScriptException();
return;
}
auto mapBuffer = info[0].As<Napi::Buffer<uint8_t>>();
auto map = SourceMapSchema::GetMap(mapBuffer.Data());
std::vector<int> sources;
auto sourcesArray = map->sources();
sources.reserve(sourcesArray->size());
auto sourcesEnd = sourcesArray->end();
for (auto it = sourcesArray->begin(); it != sourcesEnd; ++it) {
std::string source = it->str();
sources.push_back(_mapping_container.addSource(source));
}
std::vector<int> names;
auto namesArray = map->names();
names.reserve(namesArray->size());
auto namesEnd = namesArray->end();
for (auto it = namesArray->begin(); it != namesEnd; ++it) {
std::string name = it->str();
names.push_back(_mapping_container.addName(name));
}
auto originalLines = map->lines();
auto originalLineCount = map->lineCount();
std::vector<flatbuffers::Offset<SourceMapSchema::MappingLine>> lines_vector;
auto &mappingLinesVector = _mapping_container.getMappingLinesVector();
lines_vector.reserve(mappingLinesVector.size());
auto lineEnd = mappingLinesVector.end();
for (auto lineIterator = mappingLinesVector.begin(); lineIterator != lineEnd; ++lineIterator) {
auto &line = (*lineIterator);
auto &segments = line->_segments;
unsigned int segmentsCount = segments.size();
std::vector<SourceMapSchema::Mapping> mappings_vector;
mappings_vector.reserve(segments.size());
for (unsigned int i = 0; i < segmentsCount; ++i) {
Mapping &mapping = segments[i];
if (mapping.source > -1) {
int originalLineIndex = mapping.original.line;
if (originalLineCount >= originalLineIndex) {
int originalColumnIndex = mapping.original.column;
auto originalLine = originalLines->Get(originalLineIndex);
auto originalSegments = originalLine->segments();
int originalSegmentsSize = originalSegments->size();
if (originalSegmentsSize > 0) {
int startIndex = 0;
int stopIndex = originalSegmentsSize - 1;
int middleIndex = ((stopIndex + startIndex) / 2);
while (startIndex < stopIndex) {
int diff = originalSegments->Get(middleIndex)->generatedColumn() - originalColumnIndex;
if (diff > 0) {
--stopIndex;
} else if (diff < 0) {
++startIndex;
} else {
// It's the same...
break;
}
middleIndex = ((stopIndex + startIndex) / 2);
}
auto originalMapping = originalSegments->Get(middleIndex);
int originalSource = originalMapping->source();
mapping.source = originalSource > -1 ? sources[originalSource] : originalSource;
mapping.original = Position(originalMapping->originalLine(),
originalMapping->originalColumn());
int originalName = originalMapping->name();
if (originalName > -1) {
mapping.name = names[originalName];
} else {
mapping.name = -1;
}
}
}
}
}
}
}
Napi::Value SourceMapBinding::stringify(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Object obj = Napi::Object::New(env);
obj.Set("mappings", _mapping_container.toVLQMappings());
auto sourcesVector = _mapping_container.getSourcesVector();
int len = sourcesVector.size();
Napi::Array sourcesArray = Napi::Array::New(env, len);
for (int i = 0; i < len; ++i) {
sourcesArray.Set(i, sourcesVector[i]);
}
obj.Set("sources", sourcesArray);
auto namesVector = _mapping_container.getNamesVector();
len = namesVector.size();
Napi::Array namesArray = Napi::Array::New(env, len);
for (int i = 0; i < len; ++i) {
namesArray.Set(i, namesVector[i]);
}
obj.Set("names", namesArray);
return obj;
}
Napi::Value SourceMapBinding::toBuffer(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
flatbuffers::FlatBufferBuilder builder;
// Sort mappings
_mapping_container.sort();
std::vector<flatbuffers::Offset<flatbuffers::String>> names_vector;
auto namesVector = _mapping_container.getNamesVector();
names_vector.reserve(namesVector.size());
auto namesEnd = namesVector.end();
for (auto it = namesVector.begin(); it != namesEnd; ++it) {
names_vector.push_back(builder.CreateString(*it));
}
std::vector<flatbuffers::Offset<flatbuffers::String>> sources_vector;
auto sourcesVector = _mapping_container.getSourcesVector();
sources_vector.reserve(sourcesVector.size());
auto sourcesEnd = sourcesVector.end();
for (auto it = sourcesVector.begin(); it != sourcesEnd; ++it) {
sources_vector.push_back(builder.CreateString(*it));
}
std::vector<flatbuffers::Offset<SourceMapSchema::MappingLine>> lines_vector;
auto mappingLinesVector = _mapping_container.getMappingLinesVector();
lines_vector.reserve(mappingLinesVector.size());
auto lineEnd = mappingLinesVector.end();
for (auto lineIterator = mappingLinesVector.begin(); lineIterator != lineEnd; ++lineIterator) {
auto &line = (*lineIterator);
auto &segments = line->_segments;
auto segmentsEnd = segments.end();
std::vector<SourceMapSchema::Mapping> mappings_vector;
mappings_vector.reserve(segments.size());
for (auto segmentIterator = segments.begin(); segmentIterator != segmentsEnd; ++segmentIterator) {
Mapping &mapping = *segmentIterator;
mappings_vector.push_back(
SourceMapSchema::Mapping(mapping.generated.line, mapping.generated.column, mapping.original.line,
mapping.original.column, mapping.source, mapping.name));
}
lines_vector.push_back(SourceMapSchema::CreateMappingLineDirect(builder, line->lineNumber(), line->isSorted(),
&mappings_vector));
}
auto map = SourceMapSchema::CreateMapDirect(builder, &names_vector, &sources_vector,
_mapping_container.getGeneratedLines(), &lines_vector);
builder.Finish(map);
return Napi::Buffer<uint8_t>::Copy(env, builder.GetBufferPointer(), builder.GetSize());
}
Napi::Object SourceMapBinding::_mappingToObject(Napi::Env env, Mapping &mapping) {
Napi::Object mappingObject = Napi::Object::New(env);
Napi::Object generatedPositionObject = Napi::Object::New(env);
generatedPositionObject.Set("line", mapping.generated.line + 1);
generatedPositionObject.Set("column", mapping.generated.column);
mappingObject.Set("generated", generatedPositionObject);
int mappingSource = mapping.source;
if (mappingSource > -1) {
Napi::Object originalPositionObject = Napi::Object::New(env);
originalPositionObject.Set("line", mapping.original.line + 1);
originalPositionObject.Set("column", mapping.original.column);
mappingObject.Set("original", originalPositionObject);
mappingObject.Set("source", mappingSource);
}
int mappingName = mapping.name;
if (mappingName > -1) {
mappingObject.Set("name", mappingName);
}
return mappingObject;
}
// returns the sorted and processed map with decoded vlqs and all other map data
Napi::Value SourceMapBinding::getMap(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Object obj = Napi::Object::New(env);
// Sort mappings
_mapping_container.sort();
auto sourcesVector = _mapping_container.getSourcesVector();
int len = sourcesVector.size();
Napi::Array sourcesArray = Napi::Array::New(env, len);
for (int i = 0; i < len; ++i) {
sourcesArray.Set(i, sourcesVector[i]);
}
obj.Set("sources", sourcesArray);
auto namesVector = _mapping_container.getNamesVector();
len = namesVector.size();
Napi::Array namesArray = Napi::Array::New(env, len);
for (int i = 0; i < len; ++i) {
namesArray.Set(i, namesVector[i]);
}
obj.Set("names", namesArray);
auto mappingLinesVector = _mapping_container.getMappingLinesVector();
Napi::Array mappingsArray = Napi::Array::New(env, _mapping_container.getTotalSegments());
auto lineEnd = mappingLinesVector.end();
int currentMapping = 0;
for (auto lineIterator = mappingLinesVector.begin(); lineIterator != lineEnd; ++lineIterator) {
auto &line = (*lineIterator);
auto &segments = line->_segments;
auto segmentsEnd = segments.end();
for (auto segmentIterator = segments.begin(); segmentIterator != segmentsEnd; ++segmentIterator) {
Mapping &mapping = *segmentIterator;
mappingsArray.Set(currentMapping, _mappingToObject(env, mapping));
++currentMapping;
}
}
obj.Set("mappings", mappingsArray);
return obj;
}
// addIndexedMappings(array<mapping>, lineOffset, columnOffset): uses numbers for source and name with the index specified in the sources/names map/array in SourceMap instance
void SourceMapBinding::addIndexedMappings(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() < 1) {
Napi::TypeError::New(env, "Expected 1-3 parameters").ThrowAsJavaScriptException();
return;
}
if (!info[0].IsArray()) {
Napi::TypeError::New(env, "First parameter should be an array").ThrowAsJavaScriptException();
return;
}
if (info.Length() > 1 && !info[1].IsNumber()) {
Napi::TypeError::New(env,
"Second parameter should be a lineOffset of type integer").ThrowAsJavaScriptException();
return;
}
if (info.Length() > 2 && !info[2].IsNumber()) {
Napi::TypeError::New(env,
"Third parameter should be a lineOffset of type integer").ThrowAsJavaScriptException();
return;
}
const Napi::Array mappingsArray = info[0].As<Napi::Array>();
int lineOffset = info.Length() > 1 ? info[1].As<Napi::Number>().Int32Value() : 0;
int columnOffset = info.Length() > 2 ? info[2].As<Napi::Number>().Int32Value() : 0;
unsigned int length = mappingsArray.Length();
for (unsigned int i = 0; i < length; ++i) {
Napi::Value mapping = mappingsArray.Get(i);
Napi::Object mappingObject = mapping.As<Napi::Object>();
Napi::Object generated = mappingObject.Get("generated").As<Napi::Object>();
int generatedLine = generated.Get("line").As<Napi::Number>().Int32Value() - 1;
int generatedColumn = generated.Get("column").As<Napi::Number>().Int32Value();
Position generatedPosition = Position{generatedLine + lineOffset, generatedColumn + columnOffset};
Napi::Value originalPositionValue = mappingObject.Get("original");
if (originalPositionValue.IsObject()) {
Napi::Object originalPositionObject = originalPositionValue.As<Napi::Object>();
int originalLine = originalPositionObject.Get("line").As<Napi::Number>().Int32Value() - 1;
int originalColumn = originalPositionObject.Get("column").As<Napi::Number>().Int32Value();
Position originalPosition = Position{originalLine, originalColumn};
std::string sourceString = mappingObject.Get("source").As<Napi::String>().Utf8Value();
int source = _mapping_container.addSource(sourceString);
Napi::Value nameValue = mappingObject.Get("name");
if (nameValue.IsString()) {
std::string nameString = nameValue.As<Napi::String>().Utf8Value();
_mapping_container.addMapping(generatedPosition, originalPosition, source, _mapping_container.addName(nameString));
} else {
_mapping_container.addMapping(generatedPosition, originalPosition, source);
}
} else {
_mapping_container.addMapping(generatedPosition);
}
}
}
Napi::Value SourceMapBinding::getSourceIndex(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() < 1 || !info[0].IsString()) {
Napi::TypeError::New(env, "Expected a single string parameter").ThrowAsJavaScriptException();
return env.Null();
}
std::string source = info[0].As<Napi::String>().Utf8Value();
int index = _mapping_container.getSourceIndex(source);
return Napi::Number::New(env, index);
}
Napi::Value SourceMapBinding::getNameIndex(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() < 1 || !info[0].IsString()) {
Napi::TypeError::New(env, "Expected a single string parameter").ThrowAsJavaScriptException();
return env.Null();
}
std::string name = info[0].As<Napi::String>().Utf8Value();
int index = _mapping_container.getNameIndex(name);
return Napi::Number::New(env, index);
}
std::vector<int> SourceMapBinding::_addNames(Napi::Array &namesArray) {
std::vector<int> insertions;
int length = namesArray.Length();
for (int i = 0; i < length; ++i) {
std::string name = namesArray.Get(i).As<Napi::String>().Utf8Value();
insertions.push_back(_mapping_container.addName(name));
}
return insertions;
}
Napi::Value SourceMapBinding::addNames(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() != 1 || !info[0].IsArray()) {
Napi::TypeError::New(env, "Expected one parameter of type Array<string>").ThrowAsJavaScriptException();
return env.Null();
}
Napi::Array arr = info[0].As<Napi::Array>();
std::vector<int> indexes = _addNames(arr);
int size = indexes.size();
Napi::Array indexesArr = Napi::Array::New(env, size);
for (int i = 0; i < size; ++i) {
indexesArr.Set(i, Napi::Number::New(env, indexes[i]));
}
return indexesArr;
}
std::vector<int> SourceMapBinding::_addSources(Napi::Array &sourcesArray) {
std::vector<int> insertions;
unsigned int length = sourcesArray.Length();
for (unsigned int i = 0; i < length; ++i) {
std::string source = sourcesArray.Get(i).As<Napi::String>().Utf8Value();
insertions.push_back(_mapping_container.addSource(source));
}
return insertions;
}
Napi::Value SourceMapBinding::addSources(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() != 1 || !info[0].IsArray()) {
Napi::TypeError::New(env, "Expected one parameter of type Array<string>").ThrowAsJavaScriptException();
return env.Null();
}
Napi::Array arr = info[0].As<Napi::Array>();
std::vector<int> indexes = _addSources(arr);
int size = indexes.size();
Napi::Array indexesArr = Napi::Array::New(env, size);
for (int i = 0; i < size; ++i) {
indexesArr.Set(i, Napi::Number::New(env, indexes[i]));
}
return indexesArr;
}
void SourceMapBinding::addEmptyMap(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() < 2 || !info[0].IsString() || !info[1].IsString()) {
Napi::TypeError::New(env,
"Expected 2-4 parameters of type String: (sourceName, sourceContent, lineOffset = 0)").ThrowAsJavaScriptException();
return;
}
if (info.Length() == 3 && !info[2].IsNumber()) {
Napi::TypeError::New(env, "Expected third parameter to be a lineOffset of type Integer").ThrowAsJavaScriptException();
return;
}
std::string sourceName = info[0].As<Napi::String>().Utf8Value();
std::string sourceContent = info[1].As<Napi::String>().Utf8Value();
int lineOffset = info.Length() > 2 ? info[2].As<Napi::Number>().Int32Value() : 0;
int sourceIndex = _mapping_container.addSource(sourceName);
auto end = sourceContent.end();
for (auto it = sourceContent.begin(); it != end; ++it) {
const char &c = *it;
if (c == '\n') {
_mapping_container.addMapping(Position{lineOffset, 0}, Position{lineOffset, 0}, sourceIndex);
++lineOffset;
}
}
}
Napi::Value SourceMapBinding::findClosestMapping(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() < 2 || !info[0].IsNumber() || !info[1].IsNumber()) {
Napi::TypeError::New(env, "Expected 1 parameter of type buffer").ThrowAsJavaScriptException();
return env.Null();
}
int lineIndex = info[0].As<Napi::Number>().Int32Value() - 1;
int columnIndex = info[1].As<Napi::Number>().Int32Value();
if (lineIndex <= _mapping_container.getGeneratedLines()) {
auto &mappingLinesVector = _mapping_container.getMappingLinesVector();
auto &line = mappingLinesVector.at(lineIndex);
auto &segments = line->_segments;
unsigned int segmentsCount = segments.size();
std::vector<SourceMapSchema::Mapping> mappings_vector;
mappings_vector.reserve(segments.size());
int startIndex = 0;
int stopIndex = segmentsCount - 1;
int middleIndex = ((stopIndex + startIndex) / 2);
while (startIndex < stopIndex) {
Mapping &mapping = segments[middleIndex];
int diff = mapping.generated.column - columnIndex;
if (diff > 0) {
--stopIndex;
} else if (diff < 0) {
++startIndex;
} else {
// It's the same...
break;
}
middleIndex = ((stopIndex + startIndex) / 2);
}
return _mappingToObject(env, segments[middleIndex]);
}
return env.Null();
}
Napi::Object SourceMapBinding::Init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
Napi::Function func = DefineClass(env, "SourceMap", {
InstanceMethod("addRawMappings", &SourceMapBinding::addRawMappings),
InstanceMethod("addBufferMappings", &SourceMapBinding::addBufferMappings),
InstanceMethod("stringify", &SourceMapBinding::stringify),
InstanceMethod("toBuffer", &SourceMapBinding::toBuffer),
InstanceMethod("getMap", &SourceMapBinding::getMap),
InstanceMethod("addIndexedMappings", &SourceMapBinding::addIndexedMappings),
InstanceMethod("addNames", &SourceMapBinding::addNames),
InstanceMethod("addSources", &SourceMapBinding::addSources),
InstanceMethod("getSourceIndex", &SourceMapBinding::getSourceIndex),
InstanceMethod("getNameIndex", &SourceMapBinding::getNameIndex),
InstanceMethod("extends", &SourceMapBinding::extends),
InstanceMethod("addEmptyMap", &SourceMapBinding::addEmptyMap),
InstanceMethod("findClosestMapping", &SourceMapBinding::findClosestMapping),
});
exports.Set("SourceMap", func);
return exports;
}
NAPI_MODULE_INIT(/* env, exports */) {
return SourceMapBinding::Init(env, Napi::Object(env, exports));
}