WebInspector.SourceMap = function(sourceMappingURL, payload)
{
if (!WebInspector.SourceMap.prototype._base64Map) {
const base64Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
WebInspector.SourceMap.prototype._base64Map = {};
for (var i = 0; i < base64Digits.length; ++i)
WebInspector.SourceMap.prototype._base64Map[base64Digits.charAt(i)] = i;
}
this._sourceMappingURL = sourceMappingURL;
this._reverseMappingsBySourceURL = {};
this._mappings = [];
this._sources = {};
this._sourceContentByURL = {};
this._parseMappingPayload(payload);
}
WebInspector.SourceMap.load = function(sourceMapURL, compiledURL)
{
try {
var response = InspectorFrontendHost.loadResourceSynchronously(sourceMapURL);
if (!response)
return null;
if (response.slice(0, 3) === ")]}")
response = response.substring(response.indexOf('\n'));
var payload = (JSON.parse(response));
var baseURL = sourceMapURL.startsWith("data:") ? compiledURL : sourceMapURL;
return new WebInspector.SourceMap(baseURL, payload);
} catch(e) {
console.error(e.message);
return null;
}
}
WebInspector.SourceMap.prototype = {
sources: function()
{
return Object.keys(this._sources);
},
sourceContent: function(sourceURL)
{
return this._sourceContentByURL[sourceURL];
},
_parseMappingPayload: function(mappingPayload)
{
if (mappingPayload.sections)
this._parseSections(mappingPayload.sections);
else
this._parseMap(mappingPayload, 0, 0);
},
_parseSections: function(sections)
{
for (var i = 0; i < sections.length; ++i) {
var section = sections[i];
this._parseMap(section.map, section.offset.line, section.offset.column);
}
},
findEntry: function(lineNumber, columnNumber)
{
var first = 0;
var count = this._mappings.length;
while (count > 1) {
var step = count >> 1;
var middle = first + step;
var mapping = this._mappings[middle];
if (lineNumber < mapping[0] || (lineNumber === mapping[0] && columnNumber < mapping[1]))
count = step;
else {
first = middle;
count -= step;
}
}
var entry = this._mappings[first];
if (!first && entry && (lineNumber < entry[0] || (lineNumber === entry[0] && columnNumber < entry[1])))
return null;
return entry;
},
findEntryReversed: function(sourceURL, lineNumber)
{
var mappings = this._reverseMappingsBySourceURL[sourceURL];
for ( ; lineNumber < mappings.length; ++lineNumber) {
var mapping = mappings[lineNumber];
if (mapping)
return mapping;
}
return this._mappings[0];
},
_parseMap: function(map, lineNumber, columnNumber)
{
var sourceIndex = 0;
var sourceLineNumber = 0;
var sourceColumnNumber = 0;
var nameIndex = 0;
var sources = [];
var originalToCanonicalURLMap = {};
for (var i = 0; i < map.sources.length; ++i) {
var originalSourceURL = map.sources[i];
var href = (map.sourceRoot ? map.sourceRoot + "/" : "") + originalSourceURL;
var url = WebInspector.ParsedURL.completeURL(this._sourceMappingURL, href) || href;
originalToCanonicalURLMap[originalSourceURL] = url;
sources.push(url);
this._sources[url] = true;
if (map.sourcesContent && map.sourcesContent[i])
this._sourceContentByURL[url] = map.sourcesContent[i];
}
var stringCharIterator = new WebInspector.SourceMap.StringCharIterator(map.mappings);
var sourceURL = sources[sourceIndex];
while (true) {
if (stringCharIterator.peek() === ",")
stringCharIterator.next();
else {
while (stringCharIterator.peek() === ";") {
lineNumber += 1;
columnNumber = 0;
stringCharIterator.next();
}
if (!stringCharIterator.hasNext())
break;
}
columnNumber += this._decodeVLQ(stringCharIterator);
if (this._isSeparator(stringCharIterator.peek())) {
this._mappings.push([lineNumber, columnNumber]);
continue;
}
var sourceIndexDelta = this._decodeVLQ(stringCharIterator);
if (sourceIndexDelta) {
sourceIndex += sourceIndexDelta;
sourceURL = sources[sourceIndex];
}
sourceLineNumber += this._decodeVLQ(stringCharIterator);
sourceColumnNumber += this._decodeVLQ(stringCharIterator);
if (!this._isSeparator(stringCharIterator.peek()))
nameIndex += this._decodeVLQ(stringCharIterator);
this._mappings.push([lineNumber, columnNumber, sourceURL, sourceLineNumber, sourceColumnNumber]);
}
for (var i = 0; i < this._mappings.length; ++i) {
var mapping = this._mappings[i];
var url = mapping[2];
if (!url)
continue;
if (!this._reverseMappingsBySourceURL[url])
this._reverseMappingsBySourceURL[url] = [];
var reverseMappings = this._reverseMappingsBySourceURL[url];
var sourceLine = mapping[3];
if (!reverseMappings[sourceLine])
reverseMappings[sourceLine] = [mapping[0], mapping[1]];
}
},
_isSeparator: function(char)
{
return char === "," || char === ";";
},
_decodeVLQ: function(stringCharIterator)
{
var result = 0;
var shift = 0;
do {
var digit = this._base64Map[stringCharIterator.next()];
result += (digit & this._VLQ_BASE_MASK) << shift;
shift += this._VLQ_BASE_SHIFT;
} while (digit & this._VLQ_CONTINUATION_MASK);
var negative = result & 1;
result >>= 1;
return negative ? -result : result;
},
_VLQ_BASE_SHIFT: 5,
_VLQ_BASE_MASK: (1 << 5) - 1,
_VLQ_CONTINUATION_MASK: 1 << 5
}
WebInspector.SourceMap.StringCharIterator = function(string)
{
this._string = string;
this._position = 0;
}
WebInspector.SourceMap.StringCharIterator.prototype = {
next: function()
{
return this._string.charAt(this._position++);
},
peek: function()
{
return this._string.charAt(this._position);
},
hasNext: function()
{
return this._position < this._string.length;
}
}