SourceMap.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // Copyright 2013 the V8 project authors. All rights reserved.
  2. // Redistribution and use in source and binary forms, with or without
  3. // modification, are permitted provided that the following conditions are
  4. // met:
  5. //
  6. // * Redistributions of source code must retain the above copyright
  7. // notice, this list of conditions and the following disclaimer.
  8. // * Redistributions in binary form must reproduce the above
  9. // copyright notice, this list of conditions and the following
  10. // disclaimer in the documentation and/or other materials provided
  11. // with the distribution.
  12. // * Neither the name of Google Inc. nor the names of its
  13. // contributors may be used to endorse or promote products derived
  14. // from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. // This is a copy from blink dev tools, see:
  28. // http://src.chromium.org/viewvc/blink/trunk/Source/devtools/front_end/SourceMap.js
  29. // revision: 153407
  30. // Added to make the file work without dev tools
  31. WebInspector = {};
  32. WebInspector.ParsedURL = {};
  33. WebInspector.ParsedURL.completeURL = function(){};
  34. // start of original file content
  35. /*
  36. * Copyright (C) 2012 Google Inc. All rights reserved.
  37. *
  38. * Redistribution and use in source and binary forms, with or without
  39. * modification, are permitted provided that the following conditions are
  40. * met:
  41. *
  42. * * Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * * Redistributions in binary form must reproduce the above
  45. * copyright notice, this list of conditions and the following disclaimer
  46. * in the documentation and/or other materials provided with the
  47. * distribution.
  48. * * Neither the name of Google Inc. nor the names of its
  49. * contributors may be used to endorse or promote products derived from
  50. * this software without specific prior written permission.
  51. *
  52. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  53. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  54. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  55. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  56. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  57. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  58. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  59. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  60. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  61. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  62. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  63. */
  64. /**
  65. * Implements Source Map V3 model. See http://code.google.com/p/closure-compiler/wiki/SourceMaps
  66. * for format description.
  67. * @constructor
  68. * @param {string} sourceMappingURL
  69. * @param {SourceMapV3} payload
  70. */
  71. WebInspector.SourceMap = function(sourceMappingURL, payload)
  72. {
  73. if (!WebInspector.SourceMap.prototype._base64Map) {
  74. const base64Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  75. WebInspector.SourceMap.prototype._base64Map = {};
  76. for (var i = 0; i < base64Digits.length; ++i)
  77. WebInspector.SourceMap.prototype._base64Map[base64Digits.charAt(i)] = i;
  78. }
  79. this._sourceMappingURL = sourceMappingURL;
  80. this._reverseMappingsBySourceURL = {};
  81. this._mappings = [];
  82. this._sources = {};
  83. this._sourceContentByURL = {};
  84. this._parseMappingPayload(payload);
  85. }
  86. /**
  87. * @param {string} sourceMapURL
  88. * @param {string} compiledURL
  89. * @param {function(WebInspector.SourceMap)} callback
  90. */
  91. WebInspector.SourceMap.load = function(sourceMapURL, compiledURL, callback)
  92. {
  93. NetworkAgent.loadResourceForFrontend(WebInspector.resourceTreeModel.mainFrame.id, sourceMapURL, undefined, contentLoaded.bind(this));
  94. /**
  95. * @param {?Protocol.Error} error
  96. * @param {number} statusCode
  97. * @param {NetworkAgent.Headers} headers
  98. * @param {string} content
  99. */
  100. function contentLoaded(error, statusCode, headers, content)
  101. {
  102. if (error || !content || statusCode >= 400) {
  103. console.error("Could not load content for " + sourceMapURL + " : " + (error || ("HTTP status code: " + statusCode)));
  104. callback(null);
  105. return;
  106. }
  107. if (content.slice(0, 3) === ")]}")
  108. content = content.substring(content.indexOf('\n'));
  109. try {
  110. var payload = /** @type {SourceMapV3} */ (JSON.parse(content));
  111. var baseURL = sourceMapURL.startsWith("data:") ? compiledURL : sourceMapURL;
  112. callback(new WebInspector.SourceMap(baseURL, payload));
  113. } catch(e) {
  114. console.error(e.message);
  115. callback(null);
  116. }
  117. }
  118. }
  119. WebInspector.SourceMap.prototype = {
  120. /**
  121. * @return {Array.<string>}
  122. */
  123. sources: function()
  124. {
  125. return Object.keys(this._sources);
  126. },
  127. /**
  128. * @param {string} sourceURL
  129. * @return {string|undefined}
  130. */
  131. sourceContent: function(sourceURL)
  132. {
  133. return this._sourceContentByURL[sourceURL];
  134. },
  135. /**
  136. * @param {string} sourceURL
  137. * @param {WebInspector.ResourceType} contentType
  138. * @return {WebInspector.ContentProvider}
  139. */
  140. sourceContentProvider: function(sourceURL, contentType)
  141. {
  142. var lastIndexOfDot = sourceURL.lastIndexOf(".");
  143. var extension = lastIndexOfDot !== -1 ? sourceURL.substr(lastIndexOfDot + 1) : "";
  144. var mimeType = WebInspector.ResourceType.mimeTypesForExtensions[extension.toLowerCase()];
  145. var sourceContent = this.sourceContent(sourceURL);
  146. if (sourceContent)
  147. return new WebInspector.StaticContentProvider(contentType, sourceContent, mimeType);
  148. return new WebInspector.CompilerSourceMappingContentProvider(sourceURL, contentType, mimeType);
  149. },
  150. /**
  151. * @param {SourceMapV3} mappingPayload
  152. */
  153. _parseMappingPayload: function(mappingPayload)
  154. {
  155. if (mappingPayload.sections)
  156. this._parseSections(mappingPayload.sections);
  157. else
  158. this._parseMap(mappingPayload, 0, 0);
  159. },
  160. /**
  161. * @param {Array.<SourceMapV3.Section>} sections
  162. */
  163. _parseSections: function(sections)
  164. {
  165. for (var i = 0; i < sections.length; ++i) {
  166. var section = sections[i];
  167. this._parseMap(section.map, section.offset.line, section.offset.column);
  168. }
  169. },
  170. /**
  171. * @param {number} lineNumber in compiled resource
  172. * @param {number} columnNumber in compiled resource
  173. * @return {?Array}
  174. */
  175. findEntry: function(lineNumber, columnNumber)
  176. {
  177. var first = 0;
  178. var count = this._mappings.length;
  179. while (count > 1) {
  180. var step = count >> 1;
  181. var middle = first + step;
  182. var mapping = this._mappings[middle];
  183. if (lineNumber < mapping[0] || (lineNumber === mapping[0] && columnNumber < mapping[1]))
  184. count = step;
  185. else {
  186. first = middle;
  187. count -= step;
  188. }
  189. }
  190. var entry = this._mappings[first];
  191. if (!first && entry && (lineNumber < entry[0] || (lineNumber === entry[0] && columnNumber < entry[1])))
  192. return null;
  193. return entry;
  194. },
  195. /**
  196. * @param {string} sourceURL of the originating resource
  197. * @param {number} lineNumber in the originating resource
  198. * @return {Array}
  199. */
  200. findEntryReversed: function(sourceURL, lineNumber)
  201. {
  202. var mappings = this._reverseMappingsBySourceURL[sourceURL];
  203. for ( ; lineNumber < mappings.length; ++lineNumber) {
  204. var mapping = mappings[lineNumber];
  205. if (mapping)
  206. return mapping;
  207. }
  208. return this._mappings[0];
  209. },
  210. /**
  211. * @override
  212. */
  213. _parseMap: function(map, lineNumber, columnNumber)
  214. {
  215. var sourceIndex = 0;
  216. var sourceLineNumber = 0;
  217. var sourceColumnNumber = 0;
  218. var nameIndex = 0;
  219. var sources = [];
  220. var originalToCanonicalURLMap = {};
  221. for (var i = 0; i < map.sources.length; ++i) {
  222. var originalSourceURL = map.sources[i];
  223. var sourceRoot = map.sourceRoot || "";
  224. if (sourceRoot && !sourceRoot.endsWith("/"))
  225. sourceRoot += "/";
  226. var href = sourceRoot + originalSourceURL;
  227. var url = WebInspector.ParsedURL.completeURL(this._sourceMappingURL, href) || href;
  228. originalToCanonicalURLMap[originalSourceURL] = url;
  229. sources.push(url);
  230. this._sources[url] = true;
  231. if (map.sourcesContent && map.sourcesContent[i])
  232. this._sourceContentByURL[url] = map.sourcesContent[i];
  233. }
  234. var stringCharIterator = new WebInspector.SourceMap.StringCharIterator(map.mappings);
  235. var sourceURL = sources[sourceIndex];
  236. while (true) {
  237. if (stringCharIterator.peek() === ",")
  238. stringCharIterator.next();
  239. else {
  240. while (stringCharIterator.peek() === ";") {
  241. lineNumber += 1;
  242. columnNumber = 0;
  243. stringCharIterator.next();
  244. }
  245. if (!stringCharIterator.hasNext())
  246. break;
  247. }
  248. columnNumber += this._decodeVLQ(stringCharIterator);
  249. if (this._isSeparator(stringCharIterator.peek())) {
  250. this._mappings.push([lineNumber, columnNumber]);
  251. continue;
  252. }
  253. var sourceIndexDelta = this._decodeVLQ(stringCharIterator);
  254. if (sourceIndexDelta) {
  255. sourceIndex += sourceIndexDelta;
  256. sourceURL = sources[sourceIndex];
  257. }
  258. sourceLineNumber += this._decodeVLQ(stringCharIterator);
  259. sourceColumnNumber += this._decodeVLQ(stringCharIterator);
  260. if (!this._isSeparator(stringCharIterator.peek()))
  261. nameIndex += this._decodeVLQ(stringCharIterator);
  262. this._mappings.push([lineNumber, columnNumber, sourceURL, sourceLineNumber, sourceColumnNumber]);
  263. }
  264. for (var i = 0; i < this._mappings.length; ++i) {
  265. var mapping = this._mappings[i];
  266. var url = mapping[2];
  267. if (!url)
  268. continue;
  269. if (!this._reverseMappingsBySourceURL[url])
  270. this._reverseMappingsBySourceURL[url] = [];
  271. var reverseMappings = this._reverseMappingsBySourceURL[url];
  272. var sourceLine = mapping[3];
  273. if (!reverseMappings[sourceLine])
  274. reverseMappings[sourceLine] = [mapping[0], mapping[1]];
  275. }
  276. },
  277. /**
  278. * @param {string} char
  279. * @return {boolean}
  280. */
  281. _isSeparator: function(char)
  282. {
  283. return char === "," || char === ";";
  284. },
  285. /**
  286. * @param {WebInspector.SourceMap.StringCharIterator} stringCharIterator
  287. * @return {number}
  288. */
  289. _decodeVLQ: function(stringCharIterator)
  290. {
  291. // Read unsigned value.
  292. var result = 0;
  293. var shift = 0;
  294. do {
  295. var digit = this._base64Map[stringCharIterator.next()];
  296. result += (digit & this._VLQ_BASE_MASK) << shift;
  297. shift += this._VLQ_BASE_SHIFT;
  298. } while (digit & this._VLQ_CONTINUATION_MASK);
  299. // Fix the sign.
  300. var negative = result & 1;
  301. result >>= 1;
  302. return negative ? -result : result;
  303. },
  304. _VLQ_BASE_SHIFT: 5,
  305. _VLQ_BASE_MASK: (1 << 5) - 1,
  306. _VLQ_CONTINUATION_MASK: 1 << 5
  307. }
  308. /**
  309. * @constructor
  310. * @param {string} string
  311. */
  312. WebInspector.SourceMap.StringCharIterator = function(string)
  313. {
  314. this._string = string;
  315. this._position = 0;
  316. }
  317. WebInspector.SourceMap.StringCharIterator.prototype = {
  318. /**
  319. * @return {string}
  320. */
  321. next: function()
  322. {
  323. return this._string.charAt(this._position++);
  324. },
  325. /**
  326. * @return {string}
  327. */
  328. peek: function()
  329. {
  330. return this._string.charAt(this._position);
  331. },
  332. /**
  333. * @return {boolean}
  334. */
  335. hasNext: function()
  336. {
  337. return this._position < this._string.length;
  338. }
  339. }