diff --git a/lib/source-map/base64-vlq.js b/lib/source-map/base64-vlq.js index 1b67bb37..b4ff136b 100644 --- a/lib/source-map/base64-vlq.js +++ b/lib/source-map/base64-vlq.js @@ -115,9 +115,9 @@ define(function (require, exports, module) { /** * Decodes the next base 64 VLQ value from the given string and returns the - * value and the rest of the string. + * value and the rest of the string via the out parameter. */ - exports.decode = function base64VLQ_decode(aStr) { + exports.decode = function base64VLQ_decode(aStr, aOutParam) { var i = 0; var strLen = aStr.length; var result = 0; @@ -135,10 +135,8 @@ define(function (require, exports, module) { shift += VLQ_BASE_SHIFT; } while (continuation); - return { - value: fromVLQSigned(result), - rest: aStr.slice(i) - }; + aOutParam.value = fromVLQSigned(result); + aOutParam.rest = aStr.slice(i); }; }); diff --git a/lib/source-map/source-map-consumer.js b/lib/source-map/source-map-consumer.js index eec18bd3..ea643bf0 100644 --- a/lib/source-map/source-map-consumer.js +++ b/lib/source-map/source-map-consumer.js @@ -177,6 +177,12 @@ define(function (require, exports, module) { } }); + SourceMapConsumer.prototype._nextCharIsMappingSeparator = + function SourceMapConsumer_nextCharIsMappingSeparator(aStr) { + var c = aStr.charAt(0); + return c === ";" || c === ","; + }; + /** * Parse the mappings in a string in to a data structure which we can easily * query (the ordered arrays in the `this.__generatedMappings` and @@ -190,10 +196,9 @@ define(function (require, exports, module) { var previousOriginalColumn = 0; var previousSource = 0; var previousName = 0; - var mappingSeparator = /^[,;]/; var str = aStr; + var temp = {}; var mapping; - var temp; while (str.length > 0) { if (str.charAt(0) === ';') { @@ -209,41 +214,41 @@ define(function (require, exports, module) { mapping.generatedLine = generatedLine; // Generated column. - temp = base64VLQ.decode(str); + base64VLQ.decode(str, temp); mapping.generatedColumn = previousGeneratedColumn + temp.value; previousGeneratedColumn = mapping.generatedColumn; str = temp.rest; - if (str.length > 0 && !mappingSeparator.test(str.charAt(0))) { + if (str.length > 0 && !this._nextCharIsMappingSeparator(str)) { // Original source. - temp = base64VLQ.decode(str); + base64VLQ.decode(str, temp); mapping.source = this._sources.at(previousSource + temp.value); previousSource += temp.value; str = temp.rest; - if (str.length === 0 || mappingSeparator.test(str.charAt(0))) { + if (str.length === 0 || this._nextCharIsMappingSeparator(str)) { throw new Error('Found a source, but no line and column'); } // Original line. - temp = base64VLQ.decode(str); + base64VLQ.decode(str, temp); mapping.originalLine = previousOriginalLine + temp.value; previousOriginalLine = mapping.originalLine; // Lines are stored 0-based mapping.originalLine += 1; str = temp.rest; - if (str.length === 0 || mappingSeparator.test(str.charAt(0))) { + if (str.length === 0 || this._nextCharIsMappingSeparator(str)) { throw new Error('Found a source and line, but no column'); } // Original column. - temp = base64VLQ.decode(str); + base64VLQ.decode(str, temp); mapping.originalColumn = previousOriginalColumn + temp.value; previousOriginalColumn = mapping.originalColumn; str = temp.rest; - if (str.length > 0 && !mappingSeparator.test(str.charAt(0))) { + if (str.length > 0 && !this._nextCharIsMappingSeparator(str)) { // Original name. - temp = base64VLQ.decode(str); + base64VLQ.decode(str, temp); mapping.name = this._names.at(previousName + temp.value); previousName += temp.value; str = temp.rest; diff --git a/test/source-map/test-base64-vlq.js b/test/source-map/test-base64-vlq.js index 653a874e..6fd0d99f 100644 --- a/test/source-map/test-base64-vlq.js +++ b/test/source-map/test-base64-vlq.js @@ -12,10 +12,9 @@ define(function (require, exports, module) { var base64VLQ = require('../../lib/source-map/base64-vlq'); exports['test normal encoding and decoding'] = function (assert, util) { - var result; + var result = {}; for (var i = -255; i < 256; i++) { - result = base64VLQ.decode(base64VLQ.encode(i)); - assert.ok(result); + base64VLQ.decode(base64VLQ.encode(i), result); assert.equal(result.value, i); assert.equal(result.rest, ""); }