Skip to content

Commit a35d205

Browse files
authored
Merge pull request #2 from cspotcode/sync-constructors-backport
Sync constructors, backported to 0.7.3
2 parents 5632cad + cc44e1c commit a35d205

File tree

4 files changed

+253
-218
lines changed

4 files changed

+253
-218
lines changed

lib/read-wasm.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ module.exports = function readWasm() {
1515
});
1616
});
1717
};
18+
module.exports.sync = function readWasmSync() {
19+
const wasmPath = path.join(__dirname, "mappings.wasm");
20+
return fs.readFileSync(wasmPath).buffer;
21+
};
1822

1923
module.exports.initialize = _ => {
2024
console.debug("SourceMapConsumer.initialize is a no-op when running in node.js");

lib/source-map-consumer.js

Lines changed: 130 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,7 @@ const INTERNAL = Symbol("smcInternal");
1616

1717
class SourceMapConsumer {
1818
constructor(aSourceMap, aSourceMapURL) {
19-
// If the constructor was called by super(), just return Promise<this>.
20-
// Yes, this is a hack to retain the pre-existing API of the base-class
21-
// constructor also being an async factory function.
22-
if (aSourceMap == INTERNAL) {
23-
return Promise.resolve(this);
24-
}
25-
19+
if (aSourceMap === INTERNAL) return this;
2620
return _factory(aSourceMap, aSourceMapURL);
2721
}
2822

@@ -198,74 +192,71 @@ exports.SourceMapConsumer = SourceMapConsumer;
198192
*/
199193
class BasicSourceMapConsumer extends SourceMapConsumer {
200194
constructor(aSourceMap, aSourceMapURL) {
201-
return super(INTERNAL).then(that => {
202-
let sourceMap = aSourceMap;
203-
if (typeof aSourceMap === "string") {
204-
sourceMap = util.parseSourceMapInput(aSourceMap);
205-
}
195+
super(INTERNAL);
196+
let sourceMap = aSourceMap;
197+
if (typeof aSourceMap === "string") {
198+
sourceMap = util.parseSourceMapInput(aSourceMap);
199+
}
206200

207-
const version = util.getArg(sourceMap, "version");
208-
let sources = util.getArg(sourceMap, "sources");
209-
// Sass 3.3 leaves out the 'names' array, so we deviate from the spec (which
210-
// requires the array) to play nice here.
211-
const names = util.getArg(sourceMap, "names", []);
212-
let sourceRoot = util.getArg(sourceMap, "sourceRoot", null);
213-
const sourcesContent = util.getArg(sourceMap, "sourcesContent", null);
214-
const mappings = util.getArg(sourceMap, "mappings");
215-
const file = util.getArg(sourceMap, "file", null);
216-
217-
// Once again, Sass deviates from the spec and supplies the version as a
218-
// string rather than a number, so we use loose equality checking here.
219-
if (version != that._version) {
220-
throw new Error("Unsupported version: " + version);
221-
}
201+
const version = util.getArg(sourceMap, "version");
202+
let sources = util.getArg(sourceMap, "sources");
203+
// Sass 3.3 leaves out the 'names' array, so we deviate from the spec (which
204+
// requires the array) to play nice here.
205+
const names = util.getArg(sourceMap, "names", []);
206+
let sourceRoot = util.getArg(sourceMap, "sourceRoot", null);
207+
const sourcesContent = util.getArg(sourceMap, "sourcesContent", null);
208+
const mappings = util.getArg(sourceMap, "mappings");
209+
const file = util.getArg(sourceMap, "file", null);
210+
211+
// Once again, Sass deviates from the spec and supplies the version as a
212+
// string rather than a number, so we use loose equality checking here.
213+
if (version != this._version) {
214+
throw new Error("Unsupported version: " + version);
215+
}
222216

223-
if (sourceRoot) {
224-
sourceRoot = util.normalize(sourceRoot);
225-
}
217+
if (sourceRoot) {
218+
sourceRoot = util.normalize(sourceRoot);
219+
}
226220

227-
sources = sources
228-
.map(String)
229-
// Some source maps produce relative source paths like "./foo.js" instead of
230-
// "foo.js". Normalize these first so that future comparisons will succeed.
231-
// See bugzil.la/1090768.
232-
.map(util.normalize)
233-
// Always ensure that absolute sources are internally stored relative to
234-
// the source root, if the source root is absolute. Not doing this would
235-
// be particularly problematic when the source root is a prefix of the
236-
// source (valid, but why??). See github issue #199 and bugzil.la/1188982.
237-
.map(function(source) {
238-
return sourceRoot && util.isAbsolute(sourceRoot) && util.isAbsolute(source)
239-
? util.relative(sourceRoot, source)
240-
: source;
241-
});
221+
sources = sources
222+
.map(String)
223+
// Some source maps produce relative source paths like "./foo.js" instead of
224+
// "foo.js". Normalize these first so that future comparisons will succeed.
225+
// See bugzil.la/1090768.
226+
.map(util.normalize)
227+
// Always ensure that absolute sources are internally stored relative to
228+
// the source root, if the source root is absolute. Not doing this would
229+
// be particularly problematic when the source root is a prefix of the
230+
// source (valid, but why??). See github issue #199 and bugzil.la/1188982.
231+
.map(function(source) {
232+
return sourceRoot && util.isAbsolute(sourceRoot) && util.isAbsolute(source)
233+
? util.relative(sourceRoot, source)
234+
: source;
235+
});
242236

243-
// Pass `true` below to allow duplicate names and sources. While source maps
244-
// are intended to be compressed and deduplicated, the TypeScript compiler
245-
// sometimes generates source maps with duplicates in them. See Github issue
246-
// #72 and bugzil.la/889492.
247-
that._names = ArraySet.fromArray(names.map(String), true);
248-
that._sources = ArraySet.fromArray(sources, true);
237+
// Pass `true` below to allow duplicate names and sources. While source maps
238+
// are intended to be compressed and deduplicated, the TypeScript compiler
239+
// sometimes generates source maps with duplicates in them. See Github issue
240+
// #72 and bugzil.la/889492.
241+
this._names = ArraySet.fromArray(names.map(String), true);
242+
this._sources = ArraySet.fromArray(sources, true);
249243

250-
that._absoluteSources = that._sources.toArray().map(function(s) {
251-
return util.computeSourceURL(sourceRoot, s, aSourceMapURL);
252-
});
244+
this._absoluteSources = this._sources.toArray().map(function(s) {
245+
return util.computeSourceURL(sourceRoot, s, aSourceMapURL);
246+
});
253247

254-
that.sourceRoot = sourceRoot;
255-
that.sourcesContent = sourcesContent;
256-
that._mappings = mappings;
257-
that._sourceMapURL = aSourceMapURL;
258-
that.file = file;
248+
this.sourceRoot = sourceRoot;
249+
this.sourcesContent = sourcesContent;
250+
this._mappings = mappings;
251+
this._sourceMapURL = aSourceMapURL;
252+
this.file = file;
259253

260-
that._computedColumnSpans = false;
261-
that._mappingsPtr = 0;
262-
that._wasm = null;
254+
this._computedColumnSpans = false;
255+
this._mappingsPtr = 0;
256+
this._wasm = null;
263257

264-
return wasm().then(w => {
265-
that._wasm = w;
266-
return that;
267-
});
268-
});
258+
const w = wasm.sync();
259+
this._wasm = w;
269260
}
270261

271262
/**
@@ -383,14 +374,14 @@ class BasicSourceMapConsumer extends SourceMapConsumer {
383374
},
384375
() => {
385376
switch (order) {
386-
case SourceMapConsumer.GENERATED_ORDER:
387-
this._wasm.exports.by_generated_location(this._getMappingsPtr());
388-
break;
389-
case SourceMapConsumer.ORIGINAL_ORDER:
390-
this._wasm.exports.by_original_location(this._getMappingsPtr());
391-
break;
392-
default:
393-
throw new Error("Unknown order of iteration.");
377+
case SourceMapConsumer.GENERATED_ORDER:
378+
this._wasm.exports.by_generated_location(this._getMappingsPtr());
379+
break;
380+
case SourceMapConsumer.ORIGINAL_ORDER:
381+
this._wasm.exports.by_original_location(this._getMappingsPtr());
382+
break;
383+
default:
384+
throw new Error("Unknown order of iteration.");
394385
}
395386
}
396387
);
@@ -746,62 +737,56 @@ exports.BasicSourceMapConsumer = BasicSourceMapConsumer;
746737
*/
747738
class IndexedSourceMapConsumer extends SourceMapConsumer {
748739
constructor(aSourceMap, aSourceMapURL) {
749-
return super(INTERNAL).then(that => {
750-
let sourceMap = aSourceMap;
751-
if (typeof aSourceMap === "string") {
752-
sourceMap = util.parseSourceMapInput(aSourceMap);
753-
}
740+
super(INTERNAL);
741+
let sourceMap = aSourceMap;
742+
if (typeof aSourceMap === "string") {
743+
sourceMap = util.parseSourceMapInput(aSourceMap);
744+
}
754745

755-
const version = util.getArg(sourceMap, "version");
756-
const sections = util.getArg(sourceMap, "sections");
746+
const version = util.getArg(sourceMap, "version");
747+
const sections = util.getArg(sourceMap, "sections");
757748

758-
if (version != that._version) {
759-
throw new Error("Unsupported version: " + version);
749+
if (version != this._version) {
750+
throw new Error("Unsupported version: " + version);
751+
}
752+
753+
this._sources = new ArraySet();
754+
this._names = new ArraySet();
755+
this.__generatedMappings = null;
756+
this.__originalMappings = null;
757+
this.__generatedMappingsUnsorted = null;
758+
this.__originalMappingsUnsorted = null;
759+
760+
let lastOffset = {
761+
line: -1,
762+
column: 0
763+
};
764+
this._sections = sections.map(s => {
765+
if (s.url) {
766+
// The url field will require support for asynchronicity.
767+
// See https://github.com/mozilla/source-map/issues/16
768+
throw new Error("Support for url field in sections not implemented.");
760769
}
770+
const offset = util.getArg(s, "offset");
771+
const offsetLine = util.getArg(offset, "line");
772+
const offsetColumn = util.getArg(offset, "column");
761773

762-
that._sources = new ArraySet();
763-
that._names = new ArraySet();
764-
that.__generatedMappings = null;
765-
that.__originalMappings = null;
766-
that.__generatedMappingsUnsorted = null;
767-
that.__originalMappingsUnsorted = null;
774+
if (offsetLine < lastOffset.line ||
775+
(offsetLine === lastOffset.line && offsetColumn < lastOffset.column)) {
776+
throw new Error("Section offsets must be ordered and non-overlapping.");
777+
}
778+
lastOffset = offset;
768779

769-
let lastOffset = {
770-
line: -1,
771-
column: 0
780+
const consumer = new SourceMapConsumer(util.getArg(s, "map"), aSourceMapURL);
781+
return {
782+
generatedOffset: {
783+
// The offset fields are 0-based, but we use 1-based indices when
784+
// encoding/decoding from VLQ.
785+
generatedLine: offsetLine + 1,
786+
generatedColumn: offsetColumn + 1
787+
},
788+
consumer
772789
};
773-
return Promise.all(sections.map(s => {
774-
if (s.url) {
775-
// The url field will require support for asynchronicity.
776-
// See https://github.com/mozilla/source-map/issues/16
777-
throw new Error("Support for url field in sections not implemented.");
778-
}
779-
const offset = util.getArg(s, "offset");
780-
const offsetLine = util.getArg(offset, "line");
781-
const offsetColumn = util.getArg(offset, "column");
782-
783-
if (offsetLine < lastOffset.line ||
784-
(offsetLine === lastOffset.line && offsetColumn < lastOffset.column)) {
785-
throw new Error("Section offsets must be ordered and non-overlapping.");
786-
}
787-
lastOffset = offset;
788-
789-
const cons = new SourceMapConsumer(util.getArg(s, "map"), aSourceMapURL);
790-
return cons.then(consumer => {
791-
return {
792-
generatedOffset: {
793-
// The offset fields are 0-based, but we use 1-based indices when
794-
// encoding/decoding from VLQ.
795-
generatedLine: offsetLine + 1,
796-
generatedColumn: offsetColumn + 1
797-
},
798-
consumer
799-
};
800-
});
801-
})).then(s => {
802-
that._sections = s;
803-
return that;
804-
});
805790
});
806791
}
807792

@@ -926,7 +911,7 @@ class IndexedSourceMapConsumer extends SourceMapConsumer {
926911
}
927912

928913
return (aNeedle.generatedColumn -
929-
section.generatedOffset.generatedColumn);
914+
section.generatedOffset.generatedColumn);
930915
});
931916
const section = this._sections[sectionIndex];
932917

@@ -944,8 +929,8 @@ class IndexedSourceMapConsumer extends SourceMapConsumer {
944929
(section.generatedOffset.generatedLine - 1),
945930
column: needle.generatedColumn -
946931
(section.generatedOffset.generatedLine === needle.generatedLine
947-
? section.generatedOffset.generatedColumn - 1
948-
: 0),
932+
? section.generatedOffset.generatedColumn - 1
933+
: 0),
949934
bias: aArgs.bias
950935
});
951936
}
@@ -1014,8 +999,8 @@ class IndexedSourceMapConsumer extends SourceMapConsumer {
1014999
(section.generatedOffset.generatedLine - 1),
10151000
column: generatedPosition.column +
10161001
(section.generatedOffset.generatedLine === generatedPosition.line
1017-
? section.generatedOffset.generatedColumn - 1
1018-
: 0)
1002+
? section.generatedOffset.generatedColumn - 1
1003+
: 0)
10191004
};
10201005
return ret;
10211006
}
@@ -1068,8 +1053,8 @@ class IndexedSourceMapConsumer extends SourceMapConsumer {
10681053
(section.generatedOffset.generatedLine - 1),
10691054
generatedColumn: mapping.generatedColumn +
10701055
(section.generatedOffset.generatedLine === mapping.generatedLine
1071-
? section.generatedOffset.generatedColumn - 1
1072-
: 0),
1056+
? section.generatedOffset.generatedColumn - 1
1057+
: 0),
10731058
originalLine: mapping.originalLine,
10741059
originalColumn: mapping.originalColumn,
10751060
name
@@ -1122,19 +1107,19 @@ class IndexedSourceMapConsumer extends SourceMapConsumer {
11221107
* we are searching for in the given "haystack" of mappings.
11231108
*/
11241109
_findMapping(aNeedle, aMappings, aLineName,
1125-
aColumnName, aComparator, aBias) {
1110+
aColumnName, aComparator, aBias) {
11261111
// To return the position we are searching for, we must first find the
11271112
// mapping for the given position and then return the opposite position it
11281113
// points to. Because the mappings are sorted, we can use binary search to
11291114
// find the best mapping.
11301115

11311116
if (aNeedle[aLineName] <= 0) {
11321117
throw new TypeError("Line must be greater than or equal to 1, got "
1133-
+ aNeedle[aLineName]);
1118+
+ aNeedle[aLineName]);
11341119
}
11351120
if (aNeedle[aColumnName] < 0) {
11361121
throw new TypeError("Column must be greater than or equal to 0, got "
1137-
+ aNeedle[aColumnName]);
1122+
+ aNeedle[aColumnName]);
11381123
}
11391124

11401125
return binarySearch.search(aNeedle, aMappings, aComparator, aBias);
@@ -1169,11 +1154,11 @@ class IndexedSourceMapConsumer extends SourceMapConsumer {
11691154
const mappings = [];
11701155

11711156
let index = this._findMapping(needle,
1172-
this._originalMappings,
1173-
"originalLine",
1174-
"originalColumn",
1175-
util.compareByOriginalPositions,
1176-
binarySearch.LEAST_UPPER_BOUND);
1157+
this._originalMappings,
1158+
"originalLine",
1159+
"originalColumn",
1160+
util.compareByOriginalPositions,
1161+
binarySearch.LEAST_UPPER_BOUND);
11771162
if (index >= 0) {
11781163
let mapping = this._originalMappings[index];
11791164

@@ -1205,8 +1190,8 @@ class IndexedSourceMapConsumer extends SourceMapConsumer {
12051190
// Since mappings are sorted, this is guaranteed to find all mappings for
12061191
// the line we are searching for.
12071192
while (mapping &&
1208-
mapping.originalLine === line &&
1209-
mapping.originalColumn == originalColumn) {
1193+
mapping.originalLine === line &&
1194+
mapping.originalColumn == originalColumn) {
12101195
let lastColumn = mapping.lastGeneratedColumn;
12111196
if (this._computedColumnSpans && lastColumn === null) {
12121197
lastColumn = Infinity;
@@ -1244,9 +1229,9 @@ function _factory(aSourceMap, aSourceMapURL) {
12441229
}
12451230

12461231
const consumer = sourceMap.sections != null
1247-
? new IndexedSourceMapConsumer(sourceMap, aSourceMapURL)
1248-
: new BasicSourceMapConsumer(sourceMap, aSourceMapURL);
1249-
return Promise.resolve(consumer);
1232+
? new IndexedSourceMapConsumer(sourceMap, aSourceMapURL)
1233+
: new BasicSourceMapConsumer(sourceMap, aSourceMapURL);
1234+
return consumer;
12501235
}
12511236

12521237
function _factoryBSM(aSourceMap, aSourceMapURL) {

0 commit comments

Comments
 (0)