Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80556 views
1
/* -*- Mode: js; js-indent-level: 2; -*- */
2
/*
3
* Copyright 2011 Mozilla Foundation and contributors
4
* Licensed under the New BSD license. See LICENSE or:
5
* http://opensource.org/licenses/BSD-3-Clause
6
*/
7
8
/**
9
* Define a module along with a payload.
10
* @param {string} moduleName Name for the payload
11
* @param {ignored} deps Ignored. For compatibility with CommonJS AMD Spec
12
* @param {function} payload Function with (require, exports, module) params
13
*/
14
function define(moduleName, deps, payload) {
15
if (typeof moduleName != "string") {
16
throw new TypeError('Expected string, got: ' + moduleName);
17
}
18
19
if (arguments.length == 2) {
20
payload = deps;
21
}
22
23
if (moduleName in define.modules) {
24
throw new Error("Module already defined: " + moduleName);
25
}
26
define.modules[moduleName] = payload;
27
};
28
29
/**
30
* The global store of un-instantiated modules
31
*/
32
define.modules = {};
33
34
35
/**
36
* We invoke require() in the context of a Domain so we can have multiple
37
* sets of modules running separate from each other.
38
* This contrasts with JSMs which are singletons, Domains allows us to
39
* optionally load a CommonJS module twice with separate data each time.
40
* Perhaps you want 2 command lines with a different set of commands in each,
41
* for example.
42
*/
43
function Domain() {
44
this.modules = {};
45
this._currentModule = null;
46
}
47
48
(function () {
49
50
/**
51
* Lookup module names and resolve them by calling the definition function if
52
* needed.
53
* There are 2 ways to call this, either with an array of dependencies and a
54
* callback to call when the dependencies are found (which can happen
55
* asynchronously in an in-page context) or with a single string an no callback
56
* where the dependency is resolved synchronously and returned.
57
* The API is designed to be compatible with the CommonJS AMD spec and
58
* RequireJS.
59
* @param {string[]|string} deps A name, or names for the payload
60
* @param {function|undefined} callback Function to call when the dependencies
61
* are resolved
62
* @return {undefined|object} The module required or undefined for
63
* array/callback method
64
*/
65
Domain.prototype.require = function(deps, callback) {
66
if (Array.isArray(deps)) {
67
var params = deps.map(function(dep) {
68
return this.lookup(dep);
69
}, this);
70
if (callback) {
71
callback.apply(null, params);
72
}
73
return undefined;
74
}
75
else {
76
return this.lookup(deps);
77
}
78
};
79
80
function normalize(path) {
81
var bits = path.split('/');
82
var i = 1;
83
while (i < bits.length) {
84
if (bits[i] === '..') {
85
bits.splice(i-1, 1);
86
} else if (bits[i] === '.') {
87
bits.splice(i, 1);
88
} else {
89
i++;
90
}
91
}
92
return bits.join('/');
93
}
94
95
function join(a, b) {
96
a = a.trim();
97
b = b.trim();
98
if (/^\//.test(b)) {
99
return b;
100
} else {
101
return a.replace(/\/*$/, '/') + b;
102
}
103
}
104
105
function dirname(path) {
106
var bits = path.split('/');
107
bits.pop();
108
return bits.join('/');
109
}
110
111
/**
112
* Lookup module names and resolve them by calling the definition function if
113
* needed.
114
* @param {string} moduleName A name for the payload to lookup
115
* @return {object} The module specified by aModuleName or null if not found.
116
*/
117
Domain.prototype.lookup = function(moduleName) {
118
if (/^\./.test(moduleName)) {
119
moduleName = normalize(join(dirname(this._currentModule), moduleName));
120
}
121
122
if (moduleName in this.modules) {
123
var module = this.modules[moduleName];
124
return module;
125
}
126
127
if (!(moduleName in define.modules)) {
128
throw new Error("Module not defined: " + moduleName);
129
}
130
131
var module = define.modules[moduleName];
132
133
if (typeof module == "function") {
134
var exports = {};
135
var previousModule = this._currentModule;
136
this._currentModule = moduleName;
137
module(this.require.bind(this), exports, { id: moduleName, uri: "" });
138
this._currentModule = previousModule;
139
module = exports;
140
}
141
142
// cache the resulting module object for next time
143
this.modules[moduleName] = module;
144
145
return module;
146
};
147
148
}());
149
150
define.Domain = Domain;
151
define.globalDomain = new Domain();
152
var require = define.globalDomain.require.bind(define.globalDomain);
153
/* -*- Mode: js; js-indent-level: 2; -*- */
154
/*
155
* Copyright 2011 Mozilla Foundation and contributors
156
* Licensed under the New BSD license. See LICENSE or:
157
* http://opensource.org/licenses/BSD-3-Clause
158
*/
159
define('source-map/source-map-generator', ['require', 'exports', 'module' , 'source-map/base64-vlq', 'source-map/util', 'source-map/array-set', 'source-map/mapping-list'], function(require, exports, module) {
160
161
var base64VLQ = require('./base64-vlq');
162
var util = require('./util');
163
var ArraySet = require('./array-set').ArraySet;
164
var MappingList = require('./mapping-list').MappingList;
165
166
/**
167
* An instance of the SourceMapGenerator represents a source map which is
168
* being built incrementally. You may pass an object with the following
169
* properties:
170
*
171
* - file: The filename of the generated source.
172
* - sourceRoot: A root for all relative URLs in this source map.
173
*/
174
function SourceMapGenerator(aArgs) {
175
if (!aArgs) {
176
aArgs = {};
177
}
178
this._file = util.getArg(aArgs, 'file', null);
179
this._sourceRoot = util.getArg(aArgs, 'sourceRoot', null);
180
this._skipValidation = util.getArg(aArgs, 'skipValidation', false);
181
this._sources = new ArraySet();
182
this._names = new ArraySet();
183
this._mappings = new MappingList();
184
this._sourcesContents = null;
185
}
186
187
SourceMapGenerator.prototype._version = 3;
188
189
/**
190
* Creates a new SourceMapGenerator based on a SourceMapConsumer
191
*
192
* @param aSourceMapConsumer The SourceMap.
193
*/
194
SourceMapGenerator.fromSourceMap =
195
function SourceMapGenerator_fromSourceMap(aSourceMapConsumer) {
196
var sourceRoot = aSourceMapConsumer.sourceRoot;
197
var generator = new SourceMapGenerator({
198
file: aSourceMapConsumer.file,
199
sourceRoot: sourceRoot
200
});
201
aSourceMapConsumer.eachMapping(function (mapping) {
202
var newMapping = {
203
generated: {
204
line: mapping.generatedLine,
205
column: mapping.generatedColumn
206
}
207
};
208
209
if (mapping.source != null) {
210
newMapping.source = mapping.source;
211
if (sourceRoot != null) {
212
newMapping.source = util.relative(sourceRoot, newMapping.source);
213
}
214
215
newMapping.original = {
216
line: mapping.originalLine,
217
column: mapping.originalColumn
218
};
219
220
if (mapping.name != null) {
221
newMapping.name = mapping.name;
222
}
223
}
224
225
generator.addMapping(newMapping);
226
});
227
aSourceMapConsumer.sources.forEach(function (sourceFile) {
228
var content = aSourceMapConsumer.sourceContentFor(sourceFile);
229
if (content != null) {
230
generator.setSourceContent(sourceFile, content);
231
}
232
});
233
return generator;
234
};
235
236
/**
237
* Add a single mapping from original source line and column to the generated
238
* source's line and column for this source map being created. The mapping
239
* object should have the following properties:
240
*
241
* - generated: An object with the generated line and column positions.
242
* - original: An object with the original line and column positions.
243
* - source: The original source file (relative to the sourceRoot).
244
* - name: An optional original token name for this mapping.
245
*/
246
SourceMapGenerator.prototype.addMapping =
247
function SourceMapGenerator_addMapping(aArgs) {
248
var generated = util.getArg(aArgs, 'generated');
249
var original = util.getArg(aArgs, 'original', null);
250
var source = util.getArg(aArgs, 'source', null);
251
var name = util.getArg(aArgs, 'name', null);
252
253
if (!this._skipValidation) {
254
this._validateMapping(generated, original, source, name);
255
}
256
257
if (source != null && !this._sources.has(source)) {
258
this._sources.add(source);
259
}
260
261
if (name != null && !this._names.has(name)) {
262
this._names.add(name);
263
}
264
265
this._mappings.add({
266
generatedLine: generated.line,
267
generatedColumn: generated.column,
268
originalLine: original != null && original.line,
269
originalColumn: original != null && original.column,
270
source: source,
271
name: name
272
});
273
};
274
275
/**
276
* Set the source content for a source file.
277
*/
278
SourceMapGenerator.prototype.setSourceContent =
279
function SourceMapGenerator_setSourceContent(aSourceFile, aSourceContent) {
280
var source = aSourceFile;
281
if (this._sourceRoot != null) {
282
source = util.relative(this._sourceRoot, source);
283
}
284
285
if (aSourceContent != null) {
286
// Add the source content to the _sourcesContents map.
287
// Create a new _sourcesContents map if the property is null.
288
if (!this._sourcesContents) {
289
this._sourcesContents = {};
290
}
291
this._sourcesContents[util.toSetString(source)] = aSourceContent;
292
} else if (this._sourcesContents) {
293
// Remove the source file from the _sourcesContents map.
294
// If the _sourcesContents map is empty, set the property to null.
295
delete this._sourcesContents[util.toSetString(source)];
296
if (Object.keys(this._sourcesContents).length === 0) {
297
this._sourcesContents = null;
298
}
299
}
300
};
301
302
/**
303
* Applies the mappings of a sub-source-map for a specific source file to the
304
* source map being generated. Each mapping to the supplied source file is
305
* rewritten using the supplied source map. Note: The resolution for the
306
* resulting mappings is the minimium of this map and the supplied map.
307
*
308
* @param aSourceMapConsumer The source map to be applied.
309
* @param aSourceFile Optional. The filename of the source file.
310
* If omitted, SourceMapConsumer's file property will be used.
311
* @param aSourceMapPath Optional. The dirname of the path to the source map
312
* to be applied. If relative, it is relative to the SourceMapConsumer.
313
* This parameter is needed when the two source maps aren't in the same
314
* directory, and the source map to be applied contains relative source
315
* paths. If so, those relative source paths need to be rewritten
316
* relative to the SourceMapGenerator.
317
*/
318
SourceMapGenerator.prototype.applySourceMap =
319
function SourceMapGenerator_applySourceMap(aSourceMapConsumer, aSourceFile, aSourceMapPath) {
320
var sourceFile = aSourceFile;
321
// If aSourceFile is omitted, we will use the file property of the SourceMap
322
if (aSourceFile == null) {
323
if (aSourceMapConsumer.file == null) {
324
throw new Error(
325
'SourceMapGenerator.prototype.applySourceMap requires either an explicit source file, ' +
326
'or the source map\'s "file" property. Both were omitted.'
327
);
328
}
329
sourceFile = aSourceMapConsumer.file;
330
}
331
var sourceRoot = this._sourceRoot;
332
// Make "sourceFile" relative if an absolute Url is passed.
333
if (sourceRoot != null) {
334
sourceFile = util.relative(sourceRoot, sourceFile);
335
}
336
// Applying the SourceMap can add and remove items from the sources and
337
// the names array.
338
var newSources = new ArraySet();
339
var newNames = new ArraySet();
340
341
// Find mappings for the "sourceFile"
342
this._mappings.unsortedForEach(function (mapping) {
343
if (mapping.source === sourceFile && mapping.originalLine != null) {
344
// Check if it can be mapped by the source map, then update the mapping.
345
var original = aSourceMapConsumer.originalPositionFor({
346
line: mapping.originalLine,
347
column: mapping.originalColumn
348
});
349
if (original.source != null) {
350
// Copy mapping
351
mapping.source = original.source;
352
if (aSourceMapPath != null) {
353
mapping.source = util.join(aSourceMapPath, mapping.source)
354
}
355
if (sourceRoot != null) {
356
mapping.source = util.relative(sourceRoot, mapping.source);
357
}
358
mapping.originalLine = original.line;
359
mapping.originalColumn = original.column;
360
if (original.name != null) {
361
mapping.name = original.name;
362
}
363
}
364
}
365
366
var source = mapping.source;
367
if (source != null && !newSources.has(source)) {
368
newSources.add(source);
369
}
370
371
var name = mapping.name;
372
if (name != null && !newNames.has(name)) {
373
newNames.add(name);
374
}
375
376
}, this);
377
this._sources = newSources;
378
this._names = newNames;
379
380
// Copy sourcesContents of applied map.
381
aSourceMapConsumer.sources.forEach(function (sourceFile) {
382
var content = aSourceMapConsumer.sourceContentFor(sourceFile);
383
if (content != null) {
384
if (aSourceMapPath != null) {
385
sourceFile = util.join(aSourceMapPath, sourceFile);
386
}
387
if (sourceRoot != null) {
388
sourceFile = util.relative(sourceRoot, sourceFile);
389
}
390
this.setSourceContent(sourceFile, content);
391
}
392
}, this);
393
};
394
395
/**
396
* A mapping can have one of the three levels of data:
397
*
398
* 1. Just the generated position.
399
* 2. The Generated position, original position, and original source.
400
* 3. Generated and original position, original source, as well as a name
401
* token.
402
*
403
* To maintain consistency, we validate that any new mapping being added falls
404
* in to one of these categories.
405
*/
406
SourceMapGenerator.prototype._validateMapping =
407
function SourceMapGenerator_validateMapping(aGenerated, aOriginal, aSource,
408
aName) {
409
if (aGenerated && 'line' in aGenerated && 'column' in aGenerated
410
&& aGenerated.line > 0 && aGenerated.column >= 0
411
&& !aOriginal && !aSource && !aName) {
412
// Case 1.
413
return;
414
}
415
else if (aGenerated && 'line' in aGenerated && 'column' in aGenerated
416
&& aOriginal && 'line' in aOriginal && 'column' in aOriginal
417
&& aGenerated.line > 0 && aGenerated.column >= 0
418
&& aOriginal.line > 0 && aOriginal.column >= 0
419
&& aSource) {
420
// Cases 2 and 3.
421
return;
422
}
423
else {
424
throw new Error('Invalid mapping: ' + JSON.stringify({
425
generated: aGenerated,
426
source: aSource,
427
original: aOriginal,
428
name: aName
429
}));
430
}
431
};
432
433
/**
434
* Serialize the accumulated mappings in to the stream of base 64 VLQs
435
* specified by the source map format.
436
*/
437
SourceMapGenerator.prototype._serializeMappings =
438
function SourceMapGenerator_serializeMappings() {
439
var previousGeneratedColumn = 0;
440
var previousGeneratedLine = 1;
441
var previousOriginalColumn = 0;
442
var previousOriginalLine = 0;
443
var previousName = 0;
444
var previousSource = 0;
445
var result = '';
446
var mapping;
447
448
var mappings = this._mappings.toArray();
449
450
for (var i = 0, len = mappings.length; i < len; i++) {
451
mapping = mappings[i];
452
453
if (mapping.generatedLine !== previousGeneratedLine) {
454
previousGeneratedColumn = 0;
455
while (mapping.generatedLine !== previousGeneratedLine) {
456
result += ';';
457
previousGeneratedLine++;
458
}
459
}
460
else {
461
if (i > 0) {
462
if (!util.compareByGeneratedPositions(mapping, mappings[i - 1])) {
463
continue;
464
}
465
result += ',';
466
}
467
}
468
469
result += base64VLQ.encode(mapping.generatedColumn
470
- previousGeneratedColumn);
471
previousGeneratedColumn = mapping.generatedColumn;
472
473
if (mapping.source != null) {
474
result += base64VLQ.encode(this._sources.indexOf(mapping.source)
475
- previousSource);
476
previousSource = this._sources.indexOf(mapping.source);
477
478
// lines are stored 0-based in SourceMap spec version 3
479
result += base64VLQ.encode(mapping.originalLine - 1
480
- previousOriginalLine);
481
previousOriginalLine = mapping.originalLine - 1;
482
483
result += base64VLQ.encode(mapping.originalColumn
484
- previousOriginalColumn);
485
previousOriginalColumn = mapping.originalColumn;
486
487
if (mapping.name != null) {
488
result += base64VLQ.encode(this._names.indexOf(mapping.name)
489
- previousName);
490
previousName = this._names.indexOf(mapping.name);
491
}
492
}
493
}
494
495
return result;
496
};
497
498
SourceMapGenerator.prototype._generateSourcesContent =
499
function SourceMapGenerator_generateSourcesContent(aSources, aSourceRoot) {
500
return aSources.map(function (source) {
501
if (!this._sourcesContents) {
502
return null;
503
}
504
if (aSourceRoot != null) {
505
source = util.relative(aSourceRoot, source);
506
}
507
var key = util.toSetString(source);
508
return Object.prototype.hasOwnProperty.call(this._sourcesContents,
509
key)
510
? this._sourcesContents[key]
511
: null;
512
}, this);
513
};
514
515
/**
516
* Externalize the source map.
517
*/
518
SourceMapGenerator.prototype.toJSON =
519
function SourceMapGenerator_toJSON() {
520
var map = {
521
version: this._version,
522
sources: this._sources.toArray(),
523
names: this._names.toArray(),
524
mappings: this._serializeMappings()
525
};
526
if (this._file != null) {
527
map.file = this._file;
528
}
529
if (this._sourceRoot != null) {
530
map.sourceRoot = this._sourceRoot;
531
}
532
if (this._sourcesContents) {
533
map.sourcesContent = this._generateSourcesContent(map.sources, map.sourceRoot);
534
}
535
536
return map;
537
};
538
539
/**
540
* Render the source map being generated to a string.
541
*/
542
SourceMapGenerator.prototype.toString =
543
function SourceMapGenerator_toString() {
544
return JSON.stringify(this.toJSON());
545
};
546
547
exports.SourceMapGenerator = SourceMapGenerator;
548
549
});
550
/* -*- Mode: js; js-indent-level: 2; -*- */
551
/*
552
* Copyright 2011 Mozilla Foundation and contributors
553
* Licensed under the New BSD license. See LICENSE or:
554
* http://opensource.org/licenses/BSD-3-Clause
555
*
556
* Based on the Base 64 VLQ implementation in Closure Compiler:
557
* https://code.google.com/p/closure-compiler/source/browse/trunk/src/com/google/debugging/sourcemap/Base64VLQ.java
558
*
559
* Copyright 2011 The Closure Compiler Authors. All rights reserved.
560
* Redistribution and use in source and binary forms, with or without
561
* modification, are permitted provided that the following conditions are
562
* met:
563
*
564
* * Redistributions of source code must retain the above copyright
565
* notice, this list of conditions and the following disclaimer.
566
* * Redistributions in binary form must reproduce the above
567
* copyright notice, this list of conditions and the following
568
* disclaimer in the documentation and/or other materials provided
569
* with the distribution.
570
* * Neither the name of Google Inc. nor the names of its
571
* contributors may be used to endorse or promote products derived
572
* from this software without specific prior written permission.
573
*
574
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
575
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
576
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
577
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
578
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
579
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
580
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
581
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
582
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
583
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
584
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
585
*/
586
define('source-map/base64-vlq', ['require', 'exports', 'module' , 'source-map/base64'], function(require, exports, module) {
587
588
var base64 = require('./base64');
589
590
// A single base 64 digit can contain 6 bits of data. For the base 64 variable
591
// length quantities we use in the source map spec, the first bit is the sign,
592
// the next four bits are the actual value, and the 6th bit is the
593
// continuation bit. The continuation bit tells us whether there are more
594
// digits in this value following this digit.
595
//
596
// Continuation
597
// | Sign
598
// | |
599
// V V
600
// 101011
601
602
var VLQ_BASE_SHIFT = 5;
603
604
// binary: 100000
605
var VLQ_BASE = 1 << VLQ_BASE_SHIFT;
606
607
// binary: 011111
608
var VLQ_BASE_MASK = VLQ_BASE - 1;
609
610
// binary: 100000
611
var VLQ_CONTINUATION_BIT = VLQ_BASE;
612
613
/**
614
* Converts from a two-complement value to a value where the sign bit is
615
* placed in the least significant bit. For example, as decimals:
616
* 1 becomes 2 (10 binary), -1 becomes 3 (11 binary)
617
* 2 becomes 4 (100 binary), -2 becomes 5 (101 binary)
618
*/
619
function toVLQSigned(aValue) {
620
return aValue < 0
621
? ((-aValue) << 1) + 1
622
: (aValue << 1) + 0;
623
}
624
625
/**
626
* Converts to a two-complement value from a value where the sign bit is
627
* placed in the least significant bit. For example, as decimals:
628
* 2 (10 binary) becomes 1, 3 (11 binary) becomes -1
629
* 4 (100 binary) becomes 2, 5 (101 binary) becomes -2
630
*/
631
function fromVLQSigned(aValue) {
632
var isNegative = (aValue & 1) === 1;
633
var shifted = aValue >> 1;
634
return isNegative
635
? -shifted
636
: shifted;
637
}
638
639
/**
640
* Returns the base 64 VLQ encoded value.
641
*/
642
exports.encode = function base64VLQ_encode(aValue) {
643
var encoded = "";
644
var digit;
645
646
var vlq = toVLQSigned(aValue);
647
648
do {
649
digit = vlq & VLQ_BASE_MASK;
650
vlq >>>= VLQ_BASE_SHIFT;
651
if (vlq > 0) {
652
// There are still more digits in this value, so we must make sure the
653
// continuation bit is marked.
654
digit |= VLQ_CONTINUATION_BIT;
655
}
656
encoded += base64.encode(digit);
657
} while (vlq > 0);
658
659
return encoded;
660
};
661
662
/**
663
* Decodes the next base 64 VLQ value from the given string and returns the
664
* value and the rest of the string via the out parameter.
665
*/
666
exports.decode = function base64VLQ_decode(aStr, aIndex, aOutParam) {
667
var strLen = aStr.length;
668
var result = 0;
669
var shift = 0;
670
var continuation, digit;
671
672
do {
673
if (aIndex >= strLen) {
674
throw new Error("Expected more digits in base 64 VLQ value.");
675
}
676
digit = base64.decode(aStr.charAt(aIndex++));
677
continuation = !!(digit & VLQ_CONTINUATION_BIT);
678
digit &= VLQ_BASE_MASK;
679
result = result + (digit << shift);
680
shift += VLQ_BASE_SHIFT;
681
} while (continuation);
682
683
aOutParam.value = fromVLQSigned(result);
684
aOutParam.rest = aIndex;
685
};
686
687
});
688
/* -*- Mode: js; js-indent-level: 2; -*- */
689
/*
690
* Copyright 2011 Mozilla Foundation and contributors
691
* Licensed under the New BSD license. See LICENSE or:
692
* http://opensource.org/licenses/BSD-3-Clause
693
*/
694
define('source-map/base64', ['require', 'exports', 'module' , ], function(require, exports, module) {
695
696
var charToIntMap = {};
697
var intToCharMap = {};
698
699
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
700
.split('')
701
.forEach(function (ch, index) {
702
charToIntMap[ch] = index;
703
intToCharMap[index] = ch;
704
});
705
706
/**
707
* Encode an integer in the range of 0 to 63 to a single base 64 digit.
708
*/
709
exports.encode = function base64_encode(aNumber) {
710
if (aNumber in intToCharMap) {
711
return intToCharMap[aNumber];
712
}
713
throw new TypeError("Must be between 0 and 63: " + aNumber);
714
};
715
716
/**
717
* Decode a single base 64 digit to an integer.
718
*/
719
exports.decode = function base64_decode(aChar) {
720
if (aChar in charToIntMap) {
721
return charToIntMap[aChar];
722
}
723
throw new TypeError("Not a valid base 64 digit: " + aChar);
724
};
725
726
});
727
/* -*- Mode: js; js-indent-level: 2; -*- */
728
/*
729
* Copyright 2011 Mozilla Foundation and contributors
730
* Licensed under the New BSD license. See LICENSE or:
731
* http://opensource.org/licenses/BSD-3-Clause
732
*/
733
define('source-map/util', ['require', 'exports', 'module' , ], function(require, exports, module) {
734
735
/**
736
* This is a helper function for getting values from parameter/options
737
* objects.
738
*
739
* @param args The object we are extracting values from
740
* @param name The name of the property we are getting.
741
* @param defaultValue An optional value to return if the property is missing
742
* from the object. If this is not specified and the property is missing, an
743
* error will be thrown.
744
*/
745
function getArg(aArgs, aName, aDefaultValue) {
746
if (aName in aArgs) {
747
return aArgs[aName];
748
} else if (arguments.length === 3) {
749
return aDefaultValue;
750
} else {
751
throw new Error('"' + aName + '" is a required argument.');
752
}
753
}
754
exports.getArg = getArg;
755
756
var urlRegexp = /^(?:([\w+\-.]+):)?\/\/(?:(\w+:\w+)@)?([\w.]*)(?::(\d+))?(\S*)$/;
757
var dataUrlRegexp = /^data:.+\,.+$/;
758
759
function urlParse(aUrl) {
760
var match = aUrl.match(urlRegexp);
761
if (!match) {
762
return null;
763
}
764
return {
765
scheme: match[1],
766
auth: match[2],
767
host: match[3],
768
port: match[4],
769
path: match[5]
770
};
771
}
772
exports.urlParse = urlParse;
773
774
function urlGenerate(aParsedUrl) {
775
var url = '';
776
if (aParsedUrl.scheme) {
777
url += aParsedUrl.scheme + ':';
778
}
779
url += '//';
780
if (aParsedUrl.auth) {
781
url += aParsedUrl.auth + '@';
782
}
783
if (aParsedUrl.host) {
784
url += aParsedUrl.host;
785
}
786
if (aParsedUrl.port) {
787
url += ":" + aParsedUrl.port
788
}
789
if (aParsedUrl.path) {
790
url += aParsedUrl.path;
791
}
792
return url;
793
}
794
exports.urlGenerate = urlGenerate;
795
796
/**
797
* Normalizes a path, or the path portion of a URL:
798
*
799
* - Replaces consequtive slashes with one slash.
800
* - Removes unnecessary '.' parts.
801
* - Removes unnecessary '<dir>/..' parts.
802
*
803
* Based on code in the Node.js 'path' core module.
804
*
805
* @param aPath The path or url to normalize.
806
*/
807
function normalize(aPath) {
808
var path = aPath;
809
var url = urlParse(aPath);
810
if (url) {
811
if (!url.path) {
812
return aPath;
813
}
814
path = url.path;
815
}
816
var isAbsolute = (path.charAt(0) === '/');
817
818
var parts = path.split(/\/+/);
819
for (var part, up = 0, i = parts.length - 1; i >= 0; i--) {
820
part = parts[i];
821
if (part === '.') {
822
parts.splice(i, 1);
823
} else if (part === '..') {
824
up++;
825
} else if (up > 0) {
826
if (part === '') {
827
// The first part is blank if the path is absolute. Trying to go
828
// above the root is a no-op. Therefore we can remove all '..' parts
829
// directly after the root.
830
parts.splice(i + 1, up);
831
up = 0;
832
} else {
833
parts.splice(i, 2);
834
up--;
835
}
836
}
837
}
838
path = parts.join('/');
839
840
if (path === '') {
841
path = isAbsolute ? '/' : '.';
842
}
843
844
if (url) {
845
url.path = path;
846
return urlGenerate(url);
847
}
848
return path;
849
}
850
exports.normalize = normalize;
851
852
/**
853
* Joins two paths/URLs.
854
*
855
* @param aRoot The root path or URL.
856
* @param aPath The path or URL to be joined with the root.
857
*
858
* - If aPath is a URL or a data URI, aPath is returned, unless aPath is a
859
* scheme-relative URL: Then the scheme of aRoot, if any, is prepended
860
* first.
861
* - Otherwise aPath is a path. If aRoot is a URL, then its path portion
862
* is updated with the result and aRoot is returned. Otherwise the result
863
* is returned.
864
* - If aPath is absolute, the result is aPath.
865
* - Otherwise the two paths are joined with a slash.
866
* - Joining for example 'http://' and 'www.example.com' is also supported.
867
*/
868
function join(aRoot, aPath) {
869
if (aRoot === "") {
870
aRoot = ".";
871
}
872
if (aPath === "") {
873
aPath = ".";
874
}
875
var aPathUrl = urlParse(aPath);
876
var aRootUrl = urlParse(aRoot);
877
if (aRootUrl) {
878
aRoot = aRootUrl.path || '/';
879
}
880
881
// `join(foo, '//www.example.org')`
882
if (aPathUrl && !aPathUrl.scheme) {
883
if (aRootUrl) {
884
aPathUrl.scheme = aRootUrl.scheme;
885
}
886
return urlGenerate(aPathUrl);
887
}
888
889
if (aPathUrl || aPath.match(dataUrlRegexp)) {
890
return aPath;
891
}
892
893
// `join('http://', 'www.example.com')`
894
if (aRootUrl && !aRootUrl.host && !aRootUrl.path) {
895
aRootUrl.host = aPath;
896
return urlGenerate(aRootUrl);
897
}
898
899
var joined = aPath.charAt(0) === '/'
900
? aPath
901
: normalize(aRoot.replace(/\/+$/, '') + '/' + aPath);
902
903
if (aRootUrl) {
904
aRootUrl.path = joined;
905
return urlGenerate(aRootUrl);
906
}
907
return joined;
908
}
909
exports.join = join;
910
911
/**
912
* Make a path relative to a URL or another path.
913
*
914
* @param aRoot The root path or URL.
915
* @param aPath The path or URL to be made relative to aRoot.
916
*/
917
function relative(aRoot, aPath) {
918
if (aRoot === "") {
919
aRoot = ".";
920
}
921
922
aRoot = aRoot.replace(/\/$/, '');
923
924
// XXX: It is possible to remove this block, and the tests still pass!
925
var url = urlParse(aRoot);
926
if (aPath.charAt(0) == "/" && url && url.path == "/") {
927
return aPath.slice(1);
928
}
929
930
return aPath.indexOf(aRoot + '/') === 0
931
? aPath.substr(aRoot.length + 1)
932
: aPath;
933
}
934
exports.relative = relative;
935
936
/**
937
* Because behavior goes wacky when you set `__proto__` on objects, we
938
* have to prefix all the strings in our set with an arbitrary character.
939
*
940
* See https://github.com/mozilla/source-map/pull/31 and
941
* https://github.com/mozilla/source-map/issues/30
942
*
943
* @param String aStr
944
*/
945
function toSetString(aStr) {
946
return '$' + aStr;
947
}
948
exports.toSetString = toSetString;
949
950
function fromSetString(aStr) {
951
return aStr.substr(1);
952
}
953
exports.fromSetString = fromSetString;
954
955
function strcmp(aStr1, aStr2) {
956
var s1 = aStr1 || "";
957
var s2 = aStr2 || "";
958
return (s1 > s2) - (s1 < s2);
959
}
960
961
/**
962
* Comparator between two mappings where the original positions are compared.
963
*
964
* Optionally pass in `true` as `onlyCompareGenerated` to consider two
965
* mappings with the same original source/line/column, but different generated
966
* line and column the same. Useful when searching for a mapping with a
967
* stubbed out mapping.
968
*/
969
function compareByOriginalPositions(mappingA, mappingB, onlyCompareOriginal) {
970
var cmp;
971
972
cmp = strcmp(mappingA.source, mappingB.source);
973
if (cmp) {
974
return cmp;
975
}
976
977
cmp = mappingA.originalLine - mappingB.originalLine;
978
if (cmp) {
979
return cmp;
980
}
981
982
cmp = mappingA.originalColumn - mappingB.originalColumn;
983
if (cmp || onlyCompareOriginal) {
984
return cmp;
985
}
986
987
cmp = strcmp(mappingA.name, mappingB.name);
988
if (cmp) {
989
return cmp;
990
}
991
992
cmp = mappingA.generatedLine - mappingB.generatedLine;
993
if (cmp) {
994
return cmp;
995
}
996
997
return mappingA.generatedColumn - mappingB.generatedColumn;
998
};
999
exports.compareByOriginalPositions = compareByOriginalPositions;
1000
1001
/**
1002
* Comparator between two mappings where the generated positions are
1003
* compared.
1004
*
1005
* Optionally pass in `true` as `onlyCompareGenerated` to consider two
1006
* mappings with the same generated line and column, but different
1007
* source/name/original line and column the same. Useful when searching for a
1008
* mapping with a stubbed out mapping.
1009
*/
1010
function compareByGeneratedPositions(mappingA, mappingB, onlyCompareGenerated) {
1011
var cmp;
1012
1013
cmp = mappingA.generatedLine - mappingB.generatedLine;
1014
if (cmp) {
1015
return cmp;
1016
}
1017
1018
cmp = mappingA.generatedColumn - mappingB.generatedColumn;
1019
if (cmp || onlyCompareGenerated) {
1020
return cmp;
1021
}
1022
1023
cmp = strcmp(mappingA.source, mappingB.source);
1024
if (cmp) {
1025
return cmp;
1026
}
1027
1028
cmp = mappingA.originalLine - mappingB.originalLine;
1029
if (cmp) {
1030
return cmp;
1031
}
1032
1033
cmp = mappingA.originalColumn - mappingB.originalColumn;
1034
if (cmp) {
1035
return cmp;
1036
}
1037
1038
return strcmp(mappingA.name, mappingB.name);
1039
};
1040
exports.compareByGeneratedPositions = compareByGeneratedPositions;
1041
1042
});
1043
/* -*- Mode: js; js-indent-level: 2; -*- */
1044
/*
1045
* Copyright 2011 Mozilla Foundation and contributors
1046
* Licensed under the New BSD license. See LICENSE or:
1047
* http://opensource.org/licenses/BSD-3-Clause
1048
*/
1049
define('source-map/array-set', ['require', 'exports', 'module' , 'source-map/util'], function(require, exports, module) {
1050
1051
var util = require('./util');
1052
1053
/**
1054
* A data structure which is a combination of an array and a set. Adding a new
1055
* member is O(1), testing for membership is O(1), and finding the index of an
1056
* element is O(1). Removing elements from the set is not supported. Only
1057
* strings are supported for membership.
1058
*/
1059
function ArraySet() {
1060
this._array = [];
1061
this._set = {};
1062
}
1063
1064
/**
1065
* Static method for creating ArraySet instances from an existing array.
1066
*/
1067
ArraySet.fromArray = function ArraySet_fromArray(aArray, aAllowDuplicates) {
1068
var set = new ArraySet();
1069
for (var i = 0, len = aArray.length; i < len; i++) {
1070
set.add(aArray[i], aAllowDuplicates);
1071
}
1072
return set;
1073
};
1074
1075
/**
1076
* Add the given string to this set.
1077
*
1078
* @param String aStr
1079
*/
1080
ArraySet.prototype.add = function ArraySet_add(aStr, aAllowDuplicates) {
1081
var isDuplicate = this.has(aStr);
1082
var idx = this._array.length;
1083
if (!isDuplicate || aAllowDuplicates) {
1084
this._array.push(aStr);
1085
}
1086
if (!isDuplicate) {
1087
this._set[util.toSetString(aStr)] = idx;
1088
}
1089
};
1090
1091
/**
1092
* Is the given string a member of this set?
1093
*
1094
* @param String aStr
1095
*/
1096
ArraySet.prototype.has = function ArraySet_has(aStr) {
1097
return Object.prototype.hasOwnProperty.call(this._set,
1098
util.toSetString(aStr));
1099
};
1100
1101
/**
1102
* What is the index of the given string in the array?
1103
*
1104
* @param String aStr
1105
*/
1106
ArraySet.prototype.indexOf = function ArraySet_indexOf(aStr) {
1107
if (this.has(aStr)) {
1108
return this._set[util.toSetString(aStr)];
1109
}
1110
throw new Error('"' + aStr + '" is not in the set.');
1111
};
1112
1113
/**
1114
* What is the element at the given index?
1115
*
1116
* @param Number aIdx
1117
*/
1118
ArraySet.prototype.at = function ArraySet_at(aIdx) {
1119
if (aIdx >= 0 && aIdx < this._array.length) {
1120
return this._array[aIdx];
1121
}
1122
throw new Error('No element indexed by ' + aIdx);
1123
};
1124
1125
/**
1126
* Returns the array representation of this set (which has the proper indices
1127
* indicated by indexOf). Note that this is a copy of the internal array used
1128
* for storing the members so that no one can mess with internal state.
1129
*/
1130
ArraySet.prototype.toArray = function ArraySet_toArray() {
1131
return this._array.slice();
1132
};
1133
1134
exports.ArraySet = ArraySet;
1135
1136
});
1137
/* -*- Mode: js; js-indent-level: 2; -*- */
1138
/*
1139
* Copyright 2014 Mozilla Foundation and contributors
1140
* Licensed under the New BSD license. See LICENSE or:
1141
* http://opensource.org/licenses/BSD-3-Clause
1142
*/
1143
define('source-map/mapping-list', ['require', 'exports', 'module' , 'source-map/util'], function(require, exports, module) {
1144
1145
var util = require('./util');
1146
1147
/**
1148
* Determine whether mappingB is after mappingA with respect to generated
1149
* position.
1150
*/
1151
function generatedPositionAfter(mappingA, mappingB) {
1152
// Optimized for most common case
1153
var lineA = mappingA.generatedLine;
1154
var lineB = mappingB.generatedLine;
1155
var columnA = mappingA.generatedColumn;
1156
var columnB = mappingB.generatedColumn;
1157
return lineB > lineA || lineB == lineA && columnB >= columnA ||
1158
util.compareByGeneratedPositions(mappingA, mappingB) <= 0;
1159
}
1160
1161
/**
1162
* A data structure to provide a sorted view of accumulated mappings in a
1163
* performance conscious manner. It trades a neglibable overhead in general
1164
* case for a large speedup in case of mappings being added in order.
1165
*/
1166
function MappingList() {
1167
this._array = [];
1168
this._sorted = true;
1169
// Serves as infimum
1170
this._last = {generatedLine: -1, generatedColumn: 0};
1171
}
1172
1173
/**
1174
* Iterate through internal items. This method takes the same arguments that
1175
* `Array.prototype.forEach` takes.
1176
*
1177
* NOTE: The order of the mappings is NOT guaranteed.
1178
*/
1179
MappingList.prototype.unsortedForEach =
1180
function MappingList_forEach(aCallback, aThisArg) {
1181
this._array.forEach(aCallback, aThisArg);
1182
};
1183
1184
/**
1185
* Add the given source mapping.
1186
*
1187
* @param Object aMapping
1188
*/
1189
MappingList.prototype.add = function MappingList_add(aMapping) {
1190
var mapping;
1191
if (generatedPositionAfter(this._last, aMapping)) {
1192
this._last = aMapping;
1193
this._array.push(aMapping);
1194
} else {
1195
this._sorted = false;
1196
this._array.push(aMapping);
1197
}
1198
};
1199
1200
/**
1201
* Returns the flat, sorted array of mappings. The mappings are sorted by
1202
* generated position.
1203
*
1204
* WARNING: This method returns internal data without copying, for
1205
* performance. The return value must NOT be mutated, and should be treated as
1206
* an immutable borrow. If you want to take ownership, you must make your own
1207
* copy.
1208
*/
1209
MappingList.prototype.toArray = function MappingList_toArray() {
1210
if (!this._sorted) {
1211
this._array.sort(util.compareByGeneratedPositions);
1212
this._sorted = true;
1213
}
1214
return this._array;
1215
};
1216
1217
exports.MappingList = MappingList;
1218
1219
});
1220
/* -*- Mode: js; js-indent-level: 2; -*- */
1221
/*
1222
* Copyright 2011 Mozilla Foundation and contributors
1223
* Licensed under the New BSD license. See LICENSE or:
1224
* http://opensource.org/licenses/BSD-3-Clause
1225
*/
1226
define('source-map/source-map-consumer', ['require', 'exports', 'module' , 'source-map/util', 'source-map/asm-parser', 'source-map/binary-search', 'source-map/array-set', 'source-map/base64-vlq'], function(require, exports, module) {
1227
1228
var util = require('./util');
1229
var asmParser = require('./asm-parser');
1230
var binarySearch = require('./binary-search');
1231
var ArraySet = require('./array-set').ArraySet;
1232
var base64VLQ = require('./base64-vlq');
1233
1234
function SourceMapConsumer(aSourceMap) {
1235
var sourceMap = aSourceMap;
1236
if (typeof aSourceMap === 'string') {
1237
sourceMap = JSON.parse(aSourceMap.replace(/^\)\]\}'/, ''));
1238
}
1239
1240
return sourceMap.sections != null
1241
? new IndexedSourceMapConsumer(sourceMap)
1242
: new BasicSourceMapConsumer(sourceMap);
1243
}
1244
1245
SourceMapConsumer.fromSourceMap = function(aSourceMap) {
1246
return BasicSourceMapConsumer.fromSourceMap(aSourceMap);
1247
}
1248
1249
/**
1250
* The version of the source mapping spec that we are consuming.
1251
*/
1252
SourceMapConsumer.prototype._version = 3;
1253
1254
// `__generatedMappings` and `__originalMappings` are arrays that hold the
1255
// parsed mapping coordinates from the source map's "mappings" attribute. They
1256
// are lazily instantiated, accessed via the `_generatedMappings` and
1257
// `_originalMappings` getters respectively, and we only parse the mappings
1258
// and create these arrays once queried for a source location. We jump through
1259
// these hoops because there can be many thousands of mappings, and parsing
1260
// them is expensive, so we only want to do it if we must.
1261
//
1262
// Each object in the arrays is of the form:
1263
//
1264
// {
1265
// generatedLine: The line number in the generated code,
1266
// generatedColumn: The column number in the generated code,
1267
// source: The path to the original source file that generated this
1268
// chunk of code,
1269
// originalLine: The line number in the original source that
1270
// corresponds to this chunk of generated code,
1271
// originalColumn: The column number in the original source that
1272
// corresponds to this chunk of generated code,
1273
// name: The name of the original symbol which generated this chunk of
1274
// code.
1275
// }
1276
//
1277
// All properties except for `generatedLine` and `generatedColumn` can be
1278
// `null`.
1279
//
1280
// `_generatedMappings` is ordered by the generated positions.
1281
//
1282
// `_originalMappings` is ordered by the original positions.
1283
1284
SourceMapConsumer.prototype.__generatedMappings = null;
1285
Object.defineProperty(SourceMapConsumer.prototype, '_generatedMappings', {
1286
get: function () {
1287
if (!this.__generatedMappings) {
1288
this.__generatedMappings = [];
1289
this.__originalMappings = [];
1290
this._parseMappings(this._mappings, this.sourceRoot);
1291
}
1292
1293
return this.__generatedMappings;
1294
}
1295
});
1296
1297
SourceMapConsumer.prototype.__originalMappings = null;
1298
Object.defineProperty(SourceMapConsumer.prototype, '_originalMappings', {
1299
get: function () {
1300
if (!this.__originalMappings) {
1301
this.__generatedMappings = [];
1302
this.__originalMappings = [];
1303
this._parseMappings(this._mappings, this.sourceRoot);
1304
}
1305
1306
return this.__originalMappings;
1307
}
1308
});
1309
1310
SourceMapConsumer.prototype._nextCharIsMappingSeparator =
1311
function SourceMapConsumer_nextCharIsMappingSeparator(aStr, index) {
1312
var c = aStr.charAt(index);
1313
return c === ";" || c === ",";
1314
};
1315
1316
/**
1317
* Parse the mappings in a string in to a data structure which we can easily
1318
* query (the ordered arrays in the `this.__generatedMappings` and
1319
* `this.__originalMappings` properties).
1320
*/
1321
SourceMapConsumer.prototype._parseMappings =
1322
function SourceMapConsumer_parseMappings(aStr, aSourceRoot) {
1323
throw new Error("Subclasses must implement _parseMappings");
1324
};
1325
1326
SourceMapConsumer.GENERATED_ORDER = 1;
1327
SourceMapConsumer.ORIGINAL_ORDER = 2;
1328
1329
SourceMapConsumer.LEAST_UPPER_BOUND = 1;
1330
SourceMapConsumer.GREATEST_LOWER_BOUND = 2;
1331
1332
/**
1333
* Iterate over each mapping between an original source/line/column and a
1334
* generated line/column in this source map.
1335
*
1336
* @param Function aCallback
1337
* The function that is called with each mapping.
1338
* @param Object aContext
1339
* Optional. If specified, this object will be the value of `this` every
1340
* time that `aCallback` is called.
1341
* @param aOrder
1342
* Either `SourceMapConsumer.GENERATED_ORDER` or
1343
* `SourceMapConsumer.ORIGINAL_ORDER`. Specifies whether you want to
1344
* iterate over the mappings sorted by the generated file's line/column
1345
* order or the original's source/line/column order, respectively. Defaults to
1346
* `SourceMapConsumer.GENERATED_ORDER`.
1347
*/
1348
SourceMapConsumer.prototype.eachMapping =
1349
function SourceMapConsumer_eachMapping(aCallback, aContext, aOrder) {
1350
var context = aContext || null;
1351
var order = aOrder || SourceMapConsumer.GENERATED_ORDER;
1352
1353
var mappings;
1354
switch (order) {
1355
case SourceMapConsumer.GENERATED_ORDER:
1356
mappings = this._generatedMappings;
1357
break;
1358
case SourceMapConsumer.ORIGINAL_ORDER:
1359
mappings = this._originalMappings;
1360
break;
1361
default:
1362
throw new Error("Unknown order of iteration.");
1363
}
1364
1365
var sourceRoot = this.sourceRoot;
1366
mappings.map(function (mapping) {
1367
var source = mapping.source;
1368
if (source != null && sourceRoot != null) {
1369
source = util.join(sourceRoot, source);
1370
}
1371
return {
1372
source: source,
1373
generatedLine: mapping.generatedLine,
1374
generatedColumn: mapping.generatedColumn,
1375
originalLine: mapping.originalLine,
1376
originalColumn: mapping.originalColumn,
1377
name: mapping.name
1378
};
1379
}).forEach(aCallback, context);
1380
};
1381
1382
/**
1383
* Returns all generated line and column information for the original source
1384
* and line provided. The only argument is an object with the following
1385
* properties:
1386
*
1387
* - source: The filename of the original source.
1388
* - line: The line number in the original source.
1389
*
1390
* and an array of objects is returned, each with the following properties:
1391
*
1392
* - line: The line number in the generated source, or null.
1393
* - column: The column number in the generated source, or null.
1394
*/
1395
SourceMapConsumer.prototype.allGeneratedPositionsFor =
1396
function SourceMapConsumer_allGeneratedPositionsFor(aArgs) {
1397
var needle = {
1398
source: util.getArg(aArgs, 'source'),
1399
originalLine: util.getArg(aArgs, 'line'),
1400
originalColumn: 0
1401
};
1402
1403
if (this.sourceRoot != null) {
1404
needle.source = util.relative(this.sourceRoot, needle.source);
1405
}
1406
1407
var mappings = [];
1408
1409
var index = this._findMapping(needle,
1410
this._originalMappings,
1411
"originalLine",
1412
"originalColumn",
1413
util.compareByOriginalPositions);
1414
if (index >= 0) {
1415
var mapping = this._originalMappings[index];
1416
1417
// Iterate until either we run out of mappings, or we run into
1418
// a mapping for a different line. Since mappings are sorted, this is
1419
// guaranteed to find all mappings for the line we are interested in.
1420
while (mapping && mapping.originalLine === needle.originalLine) {
1421
mappings.push({
1422
line: util.getArg(mapping, 'generatedLine', null),
1423
column: util.getArg(mapping, 'generatedColumn', null),
1424
lastColumn: util.getArg(mapping, 'lastGeneratedColumn', null)
1425
});
1426
1427
mapping = this._originalMappings[++index];
1428
}
1429
}
1430
1431
return mappings;
1432
};
1433
1434
exports.SourceMapConsumer = SourceMapConsumer;
1435
1436
/**
1437
* A BasicSourceMapConsumer instance represents a parsed source map which we can
1438
* query for information about the original file positions by giving it a file
1439
* position in the generated source.
1440
*
1441
* The only parameter is the raw source map (either as a JSON string, or
1442
* already parsed to an object). According to the spec, source maps have the
1443
* following attributes:
1444
*
1445
* - version: Which version of the source map spec this map is following.
1446
* - sources: An array of URLs to the original source files.
1447
* - names: An array of identifiers which can be referrenced by individual mappings.
1448
* - sourceRoot: Optional. The URL root from which all sources are relative.
1449
* - sourcesContent: Optional. An array of contents of the original source files.
1450
* - mappings: A string of base64 VLQs which contain the actual mappings.
1451
* - file: Optional. The generated file this source map is associated with.
1452
*
1453
* Here is an example source map, taken from the source map spec[0]:
1454
*
1455
* {
1456
* version : 3,
1457
* file: "out.js",
1458
* sourceRoot : "",
1459
* sources: ["foo.js", "bar.js"],
1460
* names: ["src", "maps", "are", "fun"],
1461
* mappings: "AA,AB;;ABCDE;"
1462
* }
1463
*
1464
* [0]: https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit?pli=1#
1465
*/
1466
function BasicSourceMapConsumer(aSourceMap) {
1467
var sourceMap = aSourceMap;
1468
if (typeof aSourceMap === 'string') {
1469
sourceMap = JSON.parse(aSourceMap.replace(/^\)\]\}'/, ''));
1470
}
1471
1472
var version = util.getArg(sourceMap, 'version');
1473
var sources = util.getArg(sourceMap, 'sources');
1474
// Sass 3.3 leaves out the 'names' array, so we deviate from the spec (which
1475
// requires the array) to play nice here.
1476
var names = util.getArg(sourceMap, 'names', []);
1477
var sourceRoot = util.getArg(sourceMap, 'sourceRoot', null);
1478
var sourcesContent = util.getArg(sourceMap, 'sourcesContent', null);
1479
var mappings = util.getArg(sourceMap, 'mappings');
1480
var file = util.getArg(sourceMap, 'file', null);
1481
1482
// Once again, Sass deviates from the spec and supplies the version as a
1483
// string rather than a number, so we use loose equality checking here.
1484
if (version != this._version) {
1485
throw new Error('Unsupported version: ' + version);
1486
}
1487
1488
// Some source maps produce relative source paths like "./foo.js" instead of
1489
// "foo.js". Normalize these first so that future comparisons will succeed.
1490
// See bugzil.la/1090768.
1491
sources = sources.map(util.normalize);
1492
1493
// Pass `true` below to allow duplicate names and sources. While source maps
1494
// are intended to be compressed and deduplicated, the TypeScript compiler
1495
// sometimes generates source maps with duplicates in them. See Github issue
1496
// #72 and bugzil.la/889492.
1497
this._names = ArraySet.fromArray(names, true);
1498
this._sources = ArraySet.fromArray(sources, true);
1499
1500
this.sourceRoot = sourceRoot;
1501
this.sourcesContent = sourcesContent;
1502
this._mappings = mappings;
1503
this.file = file;
1504
}
1505
1506
BasicSourceMapConsumer.prototype = Object.create(SourceMapConsumer.prototype);
1507
BasicSourceMapConsumer.prototype.consumer = SourceMapConsumer;
1508
1509
/**
1510
* Create a BasicSourceMapConsumer from a SourceMapGenerator.
1511
*
1512
* @param SourceMapGenerator aSourceMap
1513
* The source map that will be consumed.
1514
* @returns BasicSourceMapConsumer
1515
*/
1516
BasicSourceMapConsumer.fromSourceMap =
1517
function SourceMapConsumer_fromSourceMap(aSourceMap) {
1518
var smc = Object.create(BasicSourceMapConsumer.prototype);
1519
1520
smc._names = ArraySet.fromArray(aSourceMap._names.toArray(), true);
1521
smc._sources = ArraySet.fromArray(aSourceMap._sources.toArray(), true);
1522
smc.sourceRoot = aSourceMap._sourceRoot;
1523
smc.sourcesContent = aSourceMap._generateSourcesContent(smc._sources.toArray(),
1524
smc.sourceRoot);
1525
smc.file = aSourceMap._file;
1526
1527
smc.__generatedMappings = aSourceMap._mappings.toArray().slice();
1528
smc.__originalMappings = aSourceMap._mappings.toArray().slice()
1529
.sort(util.compareByOriginalPositions);
1530
1531
return smc;
1532
};
1533
1534
/**
1535
* The version of the source mapping spec that we are consuming.
1536
*/
1537
BasicSourceMapConsumer.prototype._version = 3;
1538
1539
/**
1540
* The list of original sources.
1541
*/
1542
Object.defineProperty(BasicSourceMapConsumer.prototype, 'sources', {
1543
get: function () {
1544
return this._sources.toArray().map(function (s) {
1545
return this.sourceRoot != null ? util.join(this.sourceRoot, s) : s;
1546
}, this);
1547
}
1548
});
1549
1550
/**
1551
* Parse the mappings in a string in to a data structure which we can easily
1552
* query (the ordered arrays in the `this.__generatedMappings` and
1553
* `this.__originalMappings` properties).
1554
*/
1555
BasicSourceMapConsumer.prototype._parseMappings =
1556
asmParser.parseMappings;
1557
1558
/**
1559
* Find the mapping that best matches the hypothetical "needle" mapping that
1560
* we are searching for in the given "haystack" of mappings.
1561
*/
1562
BasicSourceMapConsumer.prototype._findMapping =
1563
function SourceMapConsumer_findMapping(aNeedle, aMappings, aLineName,
1564
aColumnName, aComparator) {
1565
// To return the position we are searching for, we must first find the
1566
// mapping for the given position and then return the opposite position it
1567
// points to. Because the mappings are sorted, we can use binary search to
1568
// find the best mapping.
1569
1570
if (aNeedle[aLineName] <= 0) {
1571
throw new TypeError('Line must be greater than or equal to 1, got '
1572
+ aNeedle[aLineName]);
1573
}
1574
if (aNeedle[aColumnName] < 0) {
1575
throw new TypeError('Column must be greater than or equal to 0, got '
1576
+ aNeedle[aColumnName]);
1577
}
1578
1579
return binarySearch.search(aNeedle, aMappings, aComparator);
1580
};
1581
1582
/**
1583
* Compute the last column for each generated mapping. The last column is
1584
* inclusive.
1585
*/
1586
BasicSourceMapConsumer.prototype.computeColumnSpans =
1587
function SourceMapConsumer_computeColumnSpans() {
1588
for (var index = 0; index < this._generatedMappings.length; ++index) {
1589
var mapping = this._generatedMappings[index];
1590
1591
// Mappings do not contain a field for the last generated columnt. We
1592
// can come up with an optimistic estimate, however, by assuming that
1593
// mappings are contiguous (i.e. given two consecutive mappings, the
1594
// first mapping ends where the second one starts).
1595
if (index + 1 < this._generatedMappings.length) {
1596
var nextMapping = this._generatedMappings[index + 1];
1597
1598
if (mapping.generatedLine === nextMapping.generatedLine) {
1599
mapping.lastGeneratedColumn = nextMapping.generatedColumn - 1;
1600
continue;
1601
}
1602
}
1603
1604
// The last mapping for each line spans the entire line.
1605
mapping.lastGeneratedColumn = Infinity;
1606
}
1607
};
1608
1609
/**
1610
* Returns the original source, line, and column information for the generated
1611
* source's line and column positions provided. The only argument is an object
1612
* with the following properties:
1613
*
1614
* - line: The line number in the generated source.
1615
* - column: The column number in the generated source.
1616
*
1617
* and an object is returned with the following properties:
1618
*
1619
* - source: The original source file, or null.
1620
* - line: The line number in the original source, or null.
1621
* - column: The column number in the original source, or null.
1622
* - name: The original identifier, or null.
1623
*/
1624
BasicSourceMapConsumer.prototype.originalPositionFor =
1625
function SourceMapConsumer_originalPositionFor(aArgs) {
1626
var needle = {
1627
generatedLine: util.getArg(aArgs, 'line'),
1628
generatedColumn: util.getArg(aArgs, 'column')
1629
};
1630
1631
var index = this._findMapping(needle,
1632
this._generatedMappings,
1633
"generatedLine",
1634
"generatedColumn",
1635
util.compareByGeneratedPositions);
1636
1637
if (index >= 0) {
1638
var mapping = this._generatedMappings[index];
1639
1640
if (mapping.generatedLine === needle.generatedLine) {
1641
var source = util.getArg(mapping, 'source', null);
1642
if (source != null && this.sourceRoot != null) {
1643
source = util.join(this.sourceRoot, source);
1644
}
1645
return {
1646
source: source,
1647
line: util.getArg(mapping, 'originalLine', null),
1648
column: util.getArg(mapping, 'originalColumn', null),
1649
name: util.getArg(mapping, 'name', null)
1650
};
1651
}
1652
}
1653
1654
return {
1655
source: null,
1656
line: null,
1657
column: null,
1658
name: null
1659
};
1660
};
1661
1662
/**
1663
* Returns the original source content. The only argument is the url of the
1664
* original source file. Returns null if no original source content is
1665
* availible.
1666
*/
1667
BasicSourceMapConsumer.prototype.sourceContentFor =
1668
function SourceMapConsumer_sourceContentFor(aSource, nullOnMissing) {
1669
if (!this.sourcesContent) {
1670
return null;
1671
}
1672
1673
if (this.sourceRoot != null) {
1674
aSource = util.relative(this.sourceRoot, aSource);
1675
}
1676
1677
if (this._sources.has(aSource)) {
1678
return this.sourcesContent[this._sources.indexOf(aSource)];
1679
}
1680
1681
var url;
1682
if (this.sourceRoot != null
1683
&& (url = util.urlParse(this.sourceRoot))) {
1684
// XXX: file:// URIs and absolute paths lead to unexpected behavior for
1685
// many users. We can help them out when they expect file:// URIs to
1686
// behave like it would if they were running a local HTTP server. See
1687
// https://bugzilla.mozilla.org/show_bug.cgi?id=885597.
1688
var fileUriAbsPath = aSource.replace(/^file:\/\//, "");
1689
if (url.scheme == "file"
1690
&& this._sources.has(fileUriAbsPath)) {
1691
return this.sourcesContent[this._sources.indexOf(fileUriAbsPath)]
1692
}
1693
1694
if ((!url.path || url.path == "/")
1695
&& this._sources.has("/" + aSource)) {
1696
return this.sourcesContent[this._sources.indexOf("/" + aSource)];
1697
}
1698
}
1699
1700
// This function is used recursively from
1701
// IndexedSourceMapConsumer.prototype.sourceContentFor. In that case, we
1702
// don't want to throw if we can't find the source - we just want to
1703
// return null, so we provide a flag to exit gracefully.
1704
if (nullOnMissing) {
1705
return null;
1706
}
1707
else {
1708
throw new Error('"' + aSource + '" is not in the SourceMap.');
1709
}
1710
};
1711
1712
/**
1713
* Returns the generated line and column information for the original source,
1714
* line, and column positions provided. The only argument is an object with
1715
* the following properties:
1716
*
1717
* - source: The filename of the original source.
1718
* - line: The line number in the original source.
1719
* - column: The column number in the original source.
1720
*
1721
* and an object is returned with the following properties:
1722
*
1723
* - line: The line number in the generated source, or null.
1724
* - column: The column number in the generated source, or null.
1725
*/
1726
BasicSourceMapConsumer.prototype.generatedPositionFor =
1727
function SourceMapConsumer_generatedPositionFor(aArgs) {
1728
var needle = {
1729
source: util.getArg(aArgs, 'source'),
1730
originalLine: util.getArg(aArgs, 'line'),
1731
originalColumn: util.getArg(aArgs, 'column')
1732
};
1733
1734
if (this.sourceRoot != null) {
1735
needle.source = util.relative(this.sourceRoot, needle.source);
1736
}
1737
1738
var index = this._findMapping(needle,
1739
this._originalMappings,
1740
"originalLine",
1741
"originalColumn",
1742
util.compareByOriginalPositions);
1743
1744
if (index >= 0) {
1745
var mapping = this._originalMappings[index];
1746
1747
return {
1748
line: util.getArg(mapping, 'generatedLine', null),
1749
column: util.getArg(mapping, 'generatedColumn', null),
1750
lastColumn: util.getArg(mapping, 'lastGeneratedColumn', null)
1751
};
1752
}
1753
1754
return {
1755
line: null,
1756
column: null,
1757
lastColumn: null
1758
};
1759
};
1760
1761
exports.BasicSourceMapConsumer = BasicSourceMapConsumer;
1762
1763
/**
1764
* An IndexedSourceMapConsumer instance represents a parsed source map which
1765
* we can query for information. It differs from BasicSourceMapConsumer in
1766
* that it takes "indexed" source maps (i.e. ones with a "sections" field) as
1767
* input.
1768
*
1769
* The only parameter is a raw source map (either as a JSON string, or already
1770
* parsed to an object). According to the spec for indexed source maps, they
1771
* have the following attributes:
1772
*
1773
* - version: Which version of the source map spec this map is following.
1774
* - file: Optional. The generated file this source map is associated with.
1775
* - sections: A list of section definitions.
1776
*
1777
* Each value under the "sections" field has two fields:
1778
* - offset: The offset into the original specified at which this section
1779
* begins to apply, defined as an object with a "line" and "column"
1780
* field.
1781
* - map: A source map definition. This source map could also be indexed,
1782
* but doesn't have to be.
1783
*
1784
* Instead of the "map" field, it's also possible to have a "url" field
1785
* specifying a URL to retrieve a source map from, but that's currently
1786
* unsupported.
1787
*
1788
* Here's an example source map, taken from the source map spec[0], but
1789
* modified to omit a section which uses the "url" field.
1790
*
1791
* {
1792
* version : 3,
1793
* file: "app.js",
1794
* sections: [{
1795
* offset: {line:100, column:10},
1796
* map: {
1797
* version : 3,
1798
* file: "section.js",
1799
* sources: ["foo.js", "bar.js"],
1800
* names: ["src", "maps", "are", "fun"],
1801
* mappings: "AAAA,E;;ABCDE;"
1802
* }
1803
* }],
1804
* }
1805
*
1806
* [0]: https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit#heading=h.535es3xeprgt
1807
*/
1808
function IndexedSourceMapConsumer(aSourceMap) {
1809
var sourceMap = aSourceMap;
1810
if (typeof aSourceMap === 'string') {
1811
sourceMap = JSON.parse(aSourceMap.replace(/^\)\]\}'/, ''));
1812
}
1813
1814
var version = util.getArg(sourceMap, 'version');
1815
var sections = util.getArg(sourceMap, 'sections');
1816
1817
if (version != this._version) {
1818
throw new Error('Unsupported version: ' + version);
1819
}
1820
1821
var lastOffset = {
1822
line: -1,
1823
column: 0
1824
};
1825
this._sections = sections.map(function (s) {
1826
if (s.url) {
1827
// The url field will require support for asynchronicity.
1828
// See https://github.com/mozilla/source-map/issues/16
1829
throw new Error('Support for url field in sections not implemented.');
1830
}
1831
var offset = util.getArg(s, 'offset');
1832
var offsetLine = util.getArg(offset, 'line');
1833
var offsetColumn = util.getArg(offset, 'column');
1834
1835
if (offsetLine < lastOffset.line ||
1836
(offsetLine === lastOffset.line && offsetColumn < lastOffset.column)) {
1837
throw new Error('Section offsets must be ordered and non-overlapping.');
1838
}
1839
lastOffset = offset;
1840
1841
return {
1842
generatedOffset: {
1843
// The offset fields are 0-based, but we use 1-based indices when
1844
// encoding/decoding from VLQ.
1845
generatedLine: offsetLine + 1,
1846
generatedColumn: offsetColumn + 1
1847
},
1848
consumer: new SourceMapConsumer(util.getArg(s, 'map'))
1849
}
1850
});
1851
}
1852
1853
IndexedSourceMapConsumer.prototype = Object.create(SourceMapConsumer.prototype);
1854
IndexedSourceMapConsumer.prototype.constructor = SourceMapConsumer;
1855
1856
/**
1857
* The version of the source mapping spec that we are consuming.
1858
*/
1859
IndexedSourceMapConsumer.prototype._version = 3;
1860
1861
/**
1862
* The list of original sources.
1863
*/
1864
Object.defineProperty(IndexedSourceMapConsumer.prototype, 'sources', {
1865
get: function () {
1866
var sources = [];
1867
for (var i = 0; i < this._sections.length; i++) {
1868
for (var j = 0; j < this._sections[i].consumer.sources.length; j++) {
1869
sources.push(this._sections[i].consumer.sources[j]);
1870
}
1871
};
1872
return sources;
1873
}
1874
});
1875
1876
/**
1877
* Returns the original source, line, and column information for the generated
1878
* source's line and column positions provided. The only argument is an object
1879
* with the following properties:
1880
*
1881
* - line: The line number in the generated source.
1882
* - column: The column number in the generated source.
1883
*
1884
* and an object is returned with the following properties:
1885
*
1886
* - source: The original source file, or null.
1887
* - line: The line number in the original source, or null.
1888
* - column: The column number in the original source, or null.
1889
* - name: The original identifier, or null.
1890
*/
1891
IndexedSourceMapConsumer.prototype.originalPositionFor =
1892
function IndexedSourceMapConsumer_originalPositionFor(aArgs) {
1893
var needle = {
1894
generatedLine: util.getArg(aArgs, 'line'),
1895
generatedColumn: util.getArg(aArgs, 'column')
1896
};
1897
1898
// Find the section containing the generated position we're trying to map
1899
// to an original position.
1900
var sectionIndex = binarySearch.search(needle, this._sections,
1901
function(needle, section) {
1902
var cmp = needle.generatedLine - section.generatedOffset.generatedLine;
1903
if (cmp) {
1904
return cmp;
1905
}
1906
1907
return (needle.generatedColumn -
1908
section.generatedOffset.generatedColumn);
1909
}, binarySearch.GREATEST_LOWER_BOUND);
1910
var section = this._sections[sectionIndex];
1911
1912
if (!section) {
1913
return {
1914
source: null,
1915
line: null,
1916
column: null,
1917
name: null
1918
};
1919
}
1920
1921
return section.consumer.originalPositionFor({
1922
line: needle.generatedLine -
1923
(section.generatedOffset.generatedLine - 1),
1924
column: needle.generatedColumn -
1925
(section.generatedOffset.generatedLine === needle.generatedLine
1926
? section.generatedOffset.generatedColumn - 1
1927
: 0)
1928
});
1929
};
1930
1931
/**
1932
* Returns the original source content. The only argument is the url of the
1933
* original source file. Returns null if no original source content is
1934
* available.
1935
*/
1936
IndexedSourceMapConsumer.prototype.sourceContentFor =
1937
function IndexedSourceMapConsumer_sourceContentFor(aSource, nullOnMissing) {
1938
for (var i = 0; i < this._sections.length; i++) {
1939
var section = this._sections[i];
1940
1941
var content = section.consumer.sourceContentFor(aSource, true);
1942
if (content) {
1943
return content;
1944
}
1945
}
1946
if (nullOnMissing) {
1947
return null;
1948
}
1949
else {
1950
throw new Error('"' + aSource + '" is not in the SourceMap.');
1951
}
1952
};
1953
1954
/**
1955
* Returns the generated line and column information for the original source,
1956
* line, and column positions provided. The only argument is an object with
1957
* the following properties:
1958
*
1959
* - source: The filename of the original source.
1960
* - line: The line number in the original source.
1961
* - column: The column number in the original source.
1962
*
1963
* and an object is returned with the following properties:
1964
*
1965
* - line: The line number in the generated source, or null.
1966
* - column: The column number in the generated source, or null.
1967
*/
1968
IndexedSourceMapConsumer.prototype.generatedPositionFor =
1969
function IndexedSourceMapConsumer_generatedPositionFor(aArgs) {
1970
for (var i = 0; i < this._sections.length; i++) {
1971
var section = this._sections[i];
1972
1973
// Only consider this section if the requested source is in the list of
1974
// sources of the consumer.
1975
if (section.consumer.sources.indexOf(util.getArg(aArgs, 'source')) === -1) {
1976
continue;
1977
}
1978
var generatedPosition = section.consumer.generatedPositionFor(aArgs);
1979
if (generatedPosition) {
1980
var ret = {
1981
line: generatedPosition.line +
1982
(section.generatedOffset.generatedLine - 1),
1983
column: generatedPosition.column +
1984
(section.generatedOffset.generatedLine === generatedPosition.line
1985
? section.generatedOffset.generatedColumn - 1
1986
: 0)
1987
};
1988
return ret;
1989
}
1990
}
1991
1992
return {
1993
line: null,
1994
column: null
1995
};
1996
};
1997
1998
/**
1999
* Parse the mappings in a string in to a data structure which we can easily
2000
* query (the ordered arrays in the `this.__generatedMappings` and
2001
* `this.__originalMappings` properties).
2002
*/
2003
IndexedSourceMapConsumer.prototype._parseMappings =
2004
function IndexedSourceMapConsumer_parseMappings(aStr, aSourceRoot) {
2005
this.__generatedMappings = [];
2006
this.__originalMappings = [];
2007
for (var i = 0; i < this._sections.length; i++) {
2008
var section = this._sections[i];
2009
var sectionMappings = section.consumer._generatedMappings;
2010
for (var j = 0; j < sectionMappings.length; j++) {
2011
var mapping = sectionMappings[i];
2012
2013
var source = mapping.source;
2014
var sourceRoot = section.consumer.sourceRoot;
2015
2016
if (source != null && sourceRoot != null) {
2017
source = util.join(sourceRoot, source);
2018
}
2019
2020
// The mappings coming from the consumer for the section have
2021
// generated positions relative to the start of the section, so we
2022
// need to offset them to be relative to the start of the concatenated
2023
// generated file.
2024
var adjustedMapping = {
2025
source: source,
2026
generatedLine: mapping.generatedLine +
2027
(section.generatedOffset.generatedLine - 1),
2028
generatedColumn: mapping.column +
2029
(section.generatedOffset.generatedLine === mapping.generatedLine)
2030
? section.generatedOffset.generatedColumn - 1
2031
: 0,
2032
originalLine: mapping.originalLine,
2033
originalColumn: mapping.originalColumn,
2034
name: mapping.name
2035
};
2036
2037
this.__generatedMappings.push(adjustedMapping);
2038
if (typeof adjustedMapping.originalLine === 'number') {
2039
this.__originalMappings.push(adjustedMapping);
2040
}
2041
};
2042
};
2043
2044
this.__generatedMappings.sort(util.compareByGeneratedPositions);
2045
this.__originalMappings.sort(util.compareByOriginalPositions);
2046
};
2047
2048
exports.IndexedSourceMapConsumer = IndexedSourceMapConsumer;
2049
2050
});
2051
/* -*- Mode: js; js-indent-level: 2; -*- */
2052
/*
2053
* Copyright 2011 Mozilla Foundation and contributors
2054
* Licensed under the New BSD license. See LICENSE or:
2055
* http://opensource.org/licenses/BSD-3-Clause
2056
*/
2057
define('source-map/asm-parser', ['require', 'exports', 'module' , 'source-map/util'], function(require, exports, module) {
2058
2059
var util = require('./util');
2060
2061
// Convert the given ASCII string into a Uint8Array with room for `reserved`
2062
// number of int32 slots at the start.
2063
function stringToArray(string, reserved) {
2064
var reservedSize = reserved * 4;
2065
var length = string.length + reservedSize;
2066
// Find the smallest power of 2 that is greater than the required length.
2067
var arraySize = Math.pow(2, Math.ceil(Math.log2(length)));
2068
var array = new Uint8Array(arraySize);
2069
2070
for (var idx = reservedSize; idx < length; idx++) {
2071
var ch = string.charCodeAt(idx - reservedSize);
2072
if (ch > 0x80) {
2073
throw new Error("Unexpected non-ASCII character: '" + string.charAt(idx)
2074
+ "' at index " + idx + ". A source map's 'mappings' string "
2075
+ "should only contain ASCII characters.");
2076
}
2077
array[idx] = ch;
2078
}
2079
2080
return array;
2081
}
2082
2083
function AsmParse(stdlib, foreign, buffer) {
2084
"use asm";
2085
2086
// Foreign functions.
2087
2088
var eachMapping = foreign.eachMapping;
2089
var getEndOfString = foreign.getEndOfString;
2090
var log = foreign.log;
2091
2092
// Heap views.
2093
2094
var HEAPU8 = new stdlib.Uint8Array(buffer);
2095
var HEAP32 = new stdlib.Int32Array(buffer);
2096
var HEAPU32 = new stdlib.Uint32Array(buffer);
2097
2098
// Constants.
2099
2100
var semicolon = 59; // ';'
2101
var comma = 44; // ','
2102
2103
var VLQ_BASE_SHIFT = 5;
2104
var VLQ_BASE = 32; // 1 << VLQ_BASE_SHIFT
2105
var VLQ_BASE_MASK = 31; // VLQ_BASE - 1
2106
var VLQ_CONTINUATION_BIT = 32; // VLQ_BASE
2107
2108
// Reserved slots in the buffer. Access via `buffer[slotName]`.
2109
2110
// The current index into the string. Uint32.
2111
var slotIdx = 0;
2112
// The value of the last parsed VLQ. Int32.
2113
var slotVlq = 1;
2114
// The end index of the string. Uint32.
2115
var slotEndIdx = 2;
2116
2117
var NUMBER_OF_RESERVED_SLOTS = 3;
2118
var SIZE_OF_RESERVED_SLOTS = 12; // NUMBER_OF_RESERVED_SLOTS
2119
2120
// Reserved slot methods and accessors.
2121
2122
function getIdx() {
2123
return HEAPU32[(slotIdx << 2) >> 2]|0;
2124
}
2125
2126
function setIdx(val) {
2127
val = val|0;
2128
HEAPU32[(slotIdx << 2) >> 2] = val;
2129
return;
2130
}
2131
2132
function incIdx() {
2133
var idx = 0;
2134
var newIdx = 0;
2135
idx = getIdx()|0;
2136
newIdx = (idx + 1)|0;
2137
setIdx(newIdx);
2138
return;
2139
}
2140
2141
function getEndIdx() {
2142
return HEAPU32[(slotEndIdx << 2) >> 2]|0;
2143
}
2144
2145
function setEndIdx(val) {
2146
val = val|0;
2147
HEAPU32[(slotEndIdx << 2) >> 2] = val;
2148
return;
2149
}
2150
2151
function getVlq() {
2152
return HEAP32[(slotVlq << 2) >> 2]|0;
2153
}
2154
2155
function setVlq(val) {
2156
val = val|0;
2157
HEAP32[(slotVlq << 2) >> 2] = val;
2158
return;
2159
}
2160
2161
// Decode a base 64 value. char -> int. Returns -1 on failure.
2162
function decodeBase64(ch) {
2163
ch = ch|0;
2164
2165
var bigA = 65; // 'A'
2166
var bigZ = 90; // 'Z'
2167
2168
var littleA = 97; // 'a'
2169
var littleZ = 122; // 'z'
2170
2171
var zero = 48; // '0'
2172
var nine = 57; // '9'
2173
2174
var plus = 43; // '+'
2175
var slash = 47; // '/'
2176
2177
// 0 - 25: ABCDEFGHIJKLMNOPQRSTUVWXYZ
2178
if ((bigA|0) <= (ch|0)) if ((ch|0) <= (bigZ|0)) {
2179
return (ch - bigA)|0;
2180
}
2181
2182
// 26 - 51: abcdefghijklmnopqrstuvwxyz
2183
if ((littleA|0) <= (ch|0)) if ((ch|0) <= (littleZ|0)) {
2184
return (ch - littleA + 26)|0;
2185
}
2186
2187
// 52 - 61: 0123456789
2188
if ((zero|0) <= (ch|0)) if ((ch|0) <= (nine|0)) {
2189
return (ch - zero + 52)|0;
2190
}
2191
2192
// 62: +
2193
if ((ch|0) == (plus|0)) {
2194
return 62;
2195
}
2196
2197
// 63: /
2198
if ((ch|0) == (slash|0)) {
2199
return 63;
2200
}
2201
2202
// Invalid base64 string.
2203
return -1;
2204
}
2205
2206
function fromVLQSigned(value) {
2207
value = value|0;
2208
var isNegative = 0;
2209
var shifted = 0;
2210
2211
isNegative = (value & 1) == 1;
2212
shifted = value >> 1;
2213
if ((isNegative|0) == 1) {
2214
return (-shifted)|0;
2215
}
2216
2217
return shifted|0;
2218
}
2219
2220
// Returns 1 on success, 0 on failure. On success, result is stored in the
2221
// vlq reserved slot.
2222
function decodeVLQ() {
2223
var result = 0;
2224
var shift = 0;
2225
var shifted = 0;
2226
var digit = 0;
2227
var continuation = 0;
2228
2229
var idx = 0;
2230
var endIdx = 0;
2231
2232
endIdx = getEndIdx()|0;
2233
2234
do {
2235
idx = getIdx()|0;
2236
if ((idx|0) >= (endIdx|0)) {
2237
return 0;
2238
}
2239
2240
digit = decodeBase64(getCharacterAtIdx()|0)|0;
2241
if ((digit|0) < 0) {
2242
return 0;
2243
}
2244
incIdx();
2245
2246
continuation = digit & VLQ_CONTINUATION_BIT;
2247
digit = digit & VLQ_BASE_MASK;
2248
shifted = digit << shift;
2249
result = (result + shifted)|0;
2250
} while ((continuation|0) != 0);
2251
2252
result = fromVLQSigned(result)|0;
2253
setVlq(result);
2254
return 1;
2255
}
2256
2257
// Get the character at the current index.
2258
function getCharacterAtIdx() {
2259
var idx = 0;
2260
idx = getIdx()|0;
2261
return HEAPU8[idx >> 0]|0;
2262
}
2263
2264
// Return 1 if there is a mapping separator character at the current index,
2265
// otherwise 0.
2266
function isSeperatorAtIdx() {
2267
var ch = 0;
2268
var idx = 0;
2269
var endIdx = 0;
2270
2271
idx = getIdx()|0;
2272
endIdx = getEndIdx()|0;
2273
2274
if ((idx|0) >= (endIdx|0)) {
2275
return 1;
2276
}
2277
2278
ch = getCharacterAtIdx()|0;
2279
2280
if ((ch|0) == (comma|0)) {
2281
return 1;
2282
}
2283
2284
if ((ch|0) == (semicolon|0)) {
2285
return 1;
2286
}
2287
2288
return 0;
2289
}
2290
2291
// Returns 1 on success, 0 on failure.
2292
function parse() {
2293
var generatedLine = 1;
2294
var generatedColumn = 0;
2295
var originalLine = 0;
2296
var originalColumn = 0;
2297
var source = 0;
2298
var name = 0;
2299
2300
var ch = 0;
2301
var idx = 0;
2302
var endIdx = 0;
2303
var result = 0;
2304
var vlq = 0;
2305
2306
log(1,1,1,1,1,1,1,1,1);
2307
2308
// Skip past the reserved slots to the data.
2309
setIdx(SIZE_OF_RESERVED_SLOTS|0);
2310
2311
// Initialize the end index.
2312
endIdx = getEndOfString()|0;
2313
setEndIdx(endIdx|0);
2314
2315
log(getIdx()|0, endIdx|0);
2316
2317
while ((getIdx()|0) < (endIdx|0)) {
2318
ch = getCharacterAtIdx()|0;
2319
2320
log(ch|0, getIdx()|0, endIdx|0);
2321
2322
if ((ch|0) == (semicolon|0)) {
2323
generatedLine = (generatedLine + 1)|0;
2324
incIdx();
2325
generatedColumn = 0;
2326
continue;
2327
}
2328
2329
if ((ch|0) == (comma|0)) {
2330
incIdx();
2331
continue;
2332
}
2333
2334
// Generated column.
2335
result = decodeVLQ()|0;
2336
if ((result|0) == 0) {
2337
return 0;
2338
}
2339
vlq = getVlq()|0;
2340
generatedColumn = (generatedColumn + vlq)|0;
2341
result = isSeperatorAtIdx()|0;
2342
if ((result|0) == 1) {
2343
eachMapping(2, generatedLine|0, generatedColumn|0, -1, -1, -1, -1);
2344
continue;
2345
}
2346
2347
// Original source.
2348
result = decodeVLQ()|0;
2349
if ((result|0) == 0) {
2350
return 0;
2351
}
2352
vlq = getVlq()|0;
2353
source = (source + vlq)|0;
2354
result = isSeperatorAtIdx()|0;
2355
if ((result|0) == 1) {
2356
return 0;
2357
}
2358
2359
// Original line.
2360
result = decodeVLQ()|0;
2361
if ((result|0) == 0) {
2362
return 0;
2363
}
2364
vlq = getVlq()|0;
2365
originalLine = (originalLine + vlq)|0;
2366
result = isSeperatorAtIdx()|0;
2367
if ((result|0) == 1) {
2368
return 0;
2369
}
2370
2371
// Original column.
2372
result = decodeVLQ()|0;
2373
if ((result|0) == 0) {
2374
return 0;
2375
}
2376
vlq = getVlq()|0;
2377
originalColumn = (originalColumn + vlq)|0;
2378
result = isSeperatorAtIdx()|0;
2379
if ((result|0) == 1) {
2380
eachMapping(5, generatedLine|0, generatedColumn|0, source|0, originalLine|0,
2381
originalColumn|0, -1);
2382
continue;
2383
}
2384
2385
// Name.
2386
result = decodeVLQ()|0;
2387
if ((result|0) == 0) {
2388
return 0;
2389
}
2390
vlq = getVlq()|0;
2391
name = (name + vlq)|0;
2392
eachMapping(6, generatedLine|0, generatedColumn|0, source|0, originalLine|0,
2393
originalColumn|0, name|0);
2394
2395
// Eat away any garbage at the end of this mapping.
2396
result = isSeperatorAtIdx()|0;
2397
while ((result|0) == 0) {
2398
incIdx();
2399
result = isSeperatorAtIdx()|0;
2400
}
2401
}
2402
2403
return 1;
2404
}
2405
2406
return { parse: parse };
2407
};
2408
2409
var NUMBER_OF_RESERVED_SLOTS = 3;
2410
2411
exports.parseMappings = function (mappings, sourceRoot) {
2412
function eachMapping(segmentsParsed, generatedLine, generatedColumn,
2413
sourceIdx, originalLine, originalColumn, nameIdx) {
2414
console.log("eachMapping", ([].slice.call(arguments)));
2415
2416
var mapping = {};
2417
this.__generatedMappings.push(mapping);
2418
2419
mapping.generatedColumn = generatedColumn;
2420
mapping.generatedLine
2421
2422
if (segmentsParsed >= 5) {
2423
this.__originalMappings.push(mapping);
2424
2425
// try {
2426
// mapping.source = this._sources.at(sourceIdx);
2427
// } catch (e) {
2428
// // TODO
2429
// }
2430
mapping.originalLine = originalLine;
2431
mapping.originalColumn = originalColumn;
2432
2433
if (segmentsParsed >= 6 && this._names.has()) {
2434
// try {
2435
// mapping.name = this._names.at(nameIdx);
2436
// } catch (e) {
2437
// // TODO
2438
// }
2439
}
2440
}
2441
}
2442
2443
var array = stringToArray(mappings, NUMBER_OF_RESERVED_SLOTS);
2444
var result = AsmParse(typeof window !== "undefined" ? window : global,
2445
{
2446
eachMapping: eachMapping.bind(this),
2447
log: console.log.bind(console),
2448
getEndOfString: function () {
2449
return mappings.length + NUMBER_OF_RESERVED_SLOTS * 4;
2450
}
2451
},
2452
array.buffer)
2453
.parse();
2454
if (!result) {
2455
throw new Error("Error parsing source map's mappings");
2456
}
2457
2458
this.__generatedMappings.sort(util.compareByGeneratedPositions);
2459
this.__originalMappings.sort(util.compareByOriginalPositions);
2460
};
2461
2462
});
2463
/* -*- Mode: js; js-indent-level: 2; -*- */
2464
/*
2465
* Copyright 2011 Mozilla Foundation and contributors
2466
* Licensed under the New BSD license. See LICENSE or:
2467
* http://opensource.org/licenses/BSD-3-Clause
2468
*/
2469
define('source-map/binary-search', ['require', 'exports', 'module' , ], function(require, exports, module) {
2470
/**
2471
* Recursive implementation of binary search.
2472
*
2473
* @param aLow Indices here and lower do not contain the needle.
2474
* @param aHigh Indices here and higher do not contain the needle.
2475
* @param aNeedle The element being searched for.
2476
* @param aHaystack The non-empty array being searched.
2477
* @param aCompare Function which takes two elements and returns -1, 0, or 1.
2478
* @param aBias Either 'binarySearch.LEAST_UPPER_BOUND' or
2479
* 'binarySearch.GREATEST_LOWER_BOUND'. Specifies whether to return the
2480
* closest element that is smaller than or greater than the element we are
2481
* searching for if the exact element cannot be found.
2482
*/
2483
function recursiveSearch(aLow, aHigh, aNeedle, aHaystack, aCompare, aBias) {
2484
// This function terminates when one of the following is true:
2485
//
2486
// 1. We find the exact element we are looking for.
2487
//
2488
// 2. We did not find the exact element, but we can return the index of
2489
// the next closest element.
2490
//
2491
// 3. We did not find the exact element, and there is no next-closest
2492
// element than the one we are searching for, so we return -1.
2493
var mid = Math.floor((aHigh - aLow) / 2) + aLow;
2494
var cmp = aCompare(aNeedle, aHaystack[mid], true);
2495
if (cmp === 0) {
2496
// Found the element we are looking for.
2497
return mid;
2498
}
2499
else if (cmp > 0) {
2500
// Our needle is greater than aHaystack[mid].
2501
if (aHigh - mid > 1) {
2502
// The element is in the upper half.
2503
return recursiveSearch(mid, aHigh, aNeedle, aHaystack, aCompare, aBias);
2504
}
2505
// The exact needle element was not found in this haystack. Determine if
2506
// we are in termination case (3) or (2) and return the appropriate thing.
2507
if (aBias == exports.LEAST_UPPER_BOUND) {
2508
return aHigh < aHaystack.length ? aHigh : -1;
2509
} else {
2510
return mid;
2511
}
2512
}
2513
else {
2514
// Our needle is less than aHaystack[mid].
2515
if (mid - aLow > 1) {
2516
// The element is in the lower half.
2517
return recursiveSearch(aLow, mid, aNeedle, aHaystack, aCompare, aBias);
2518
}
2519
// The exact needle element was not found in this haystack. Determine if
2520
// we are in termination case (3) or (2) and return the appropriate thing.
2521
if (aBias == exports.LEAST_UPPER_BOUND) {
2522
return mid;
2523
} else {
2524
return aLow < 0 ? -1 : aLow;
2525
}
2526
}
2527
}
2528
2529
exports.LEAST_UPPER_BOUND = 1;
2530
exports.GREATEST_LOWER_BOUND = 2;
2531
2532
/**
2533
* This is an implementation of binary search which will always try and return
2534
* the index of next highest value checked if there is no exact hit. This is
2535
* because mappings between original and generated line/col pairs are single
2536
* points, and there is an implicit region between each of them, so a miss
2537
* just means that you aren't on the very start of a region.
2538
*
2539
* @param aNeedle The element you are looking for.
2540
* @param aHaystack The array that is being searched.
2541
* @param aCompare A function which takes the needle and an element in the
2542
* array and returns -1, 0, or 1 depending on whether the needle is less
2543
* than, equal to, or greater than the element, respectively.
2544
* @param aBias Either 'exports.LEAST_UPPER_BOUND' or
2545
* 'exports.GREATEST_LOWER_BOUND'. Specifies whether to return the
2546
* closest element that is smaller than or greater than the element we are
2547
* searching for if the exact element cannot be found. Defaults to
2548
* 'exports.LEAST_UPPER_BOUND'.
2549
*/
2550
exports.search = function search(aNeedle, aHaystack, aCompare, aBias) {
2551
var aBias = aBias || exports.LEAST_UPPER_BOUND;
2552
2553
if (aHaystack.length === 0) {
2554
return -1;
2555
}
2556
return recursiveSearch(-1, aHaystack.length, aNeedle, aHaystack, aCompare, aBias)
2557
};
2558
2559
});
2560
/* -*- Mode: js; js-indent-level: 2; -*- */
2561
/*
2562
* Copyright 2011 Mozilla Foundation and contributors
2563
* Licensed under the New BSD license. See LICENSE or:
2564
* http://opensource.org/licenses/BSD-3-Clause
2565
*/
2566
define('source-map/source-node', ['require', 'exports', 'module' , 'source-map/source-map-generator', 'source-map/util'], function(require, exports, module) {
2567
2568
var SourceMapGenerator = require('./source-map-generator').SourceMapGenerator;
2569
var util = require('./util');
2570
2571
// Matches a Windows-style `\r\n` newline or a `\n` newline used by all other
2572
// operating systems these days (capturing the result).
2573
var REGEX_NEWLINE = /(\r?\n)/;
2574
2575
// Newline character code for charCodeAt() comparisons
2576
var NEWLINE_CODE = 10;
2577
2578
// Private symbol for identifying `SourceNode`s when multiple versions of
2579
// the source-map library are loaded. This MUST NOT CHANGE across
2580
// versions!
2581
var isSourceNode = "$$$isSourceNode$$$";
2582
2583
/**
2584
* SourceNodes provide a way to abstract over interpolating/concatenating
2585
* snippets of generated JavaScript source code while maintaining the line and
2586
* column information associated with the original source code.
2587
*
2588
* @param aLine The original line number.
2589
* @param aColumn The original column number.
2590
* @param aSource The original source's filename.
2591
* @param aChunks Optional. An array of strings which are snippets of
2592
* generated JS, or other SourceNodes.
2593
* @param aName The original identifier.
2594
*/
2595
function SourceNode(aLine, aColumn, aSource, aChunks, aName) {
2596
this.children = [];
2597
this.sourceContents = {};
2598
this.line = aLine == null ? null : aLine;
2599
this.column = aColumn == null ? null : aColumn;
2600
this.source = aSource == null ? null : aSource;
2601
this.name = aName == null ? null : aName;
2602
this[isSourceNode] = true;
2603
if (aChunks != null) this.add(aChunks);
2604
}
2605
2606
/**
2607
* Creates a SourceNode from generated code and a SourceMapConsumer.
2608
*
2609
* @param aGeneratedCode The generated code
2610
* @param aSourceMapConsumer The SourceMap for the generated code
2611
* @param aRelativePath Optional. The path that relative sources in the
2612
* SourceMapConsumer should be relative to.
2613
*/
2614
SourceNode.fromStringWithSourceMap =
2615
function SourceNode_fromStringWithSourceMap(aGeneratedCode, aSourceMapConsumer, aRelativePath) {
2616
// The SourceNode we want to fill with the generated code
2617
// and the SourceMap
2618
var node = new SourceNode();
2619
2620
// All even indices of this array are one line of the generated code,
2621
// while all odd indices are the newlines between two adjacent lines
2622
// (since `REGEX_NEWLINE` captures its match).
2623
// Processed fragments are removed from this array, by calling `shiftNextLine`.
2624
var remainingLines = aGeneratedCode.split(REGEX_NEWLINE);
2625
var shiftNextLine = function() {
2626
var lineContents = remainingLines.shift();
2627
// The last line of a file might not have a newline.
2628
var newLine = remainingLines.shift() || "";
2629
return lineContents + newLine;
2630
};
2631
2632
// We need to remember the position of "remainingLines"
2633
var lastGeneratedLine = 1, lastGeneratedColumn = 0;
2634
2635
// The generate SourceNodes we need a code range.
2636
// To extract it current and last mapping is used.
2637
// Here we store the last mapping.
2638
var lastMapping = null;
2639
2640
aSourceMapConsumer.eachMapping(function (mapping) {
2641
if (lastMapping !== null) {
2642
// We add the code from "lastMapping" to "mapping":
2643
// First check if there is a new line in between.
2644
if (lastGeneratedLine < mapping.generatedLine) {
2645
var code = "";
2646
// Associate first line with "lastMapping"
2647
addMappingWithCode(lastMapping, shiftNextLine());
2648
lastGeneratedLine++;
2649
lastGeneratedColumn = 0;
2650
// The remaining code is added without mapping
2651
} else {
2652
// There is no new line in between.
2653
// Associate the code between "lastGeneratedColumn" and
2654
// "mapping.generatedColumn" with "lastMapping"
2655
var nextLine = remainingLines[0];
2656
var code = nextLine.substr(0, mapping.generatedColumn -
2657
lastGeneratedColumn);
2658
remainingLines[0] = nextLine.substr(mapping.generatedColumn -
2659
lastGeneratedColumn);
2660
lastGeneratedColumn = mapping.generatedColumn;
2661
addMappingWithCode(lastMapping, code);
2662
// No more remaining code, continue
2663
lastMapping = mapping;
2664
return;
2665
}
2666
}
2667
// We add the generated code until the first mapping
2668
// to the SourceNode without any mapping.
2669
// Each line is added as separate string.
2670
while (lastGeneratedLine < mapping.generatedLine) {
2671
node.add(shiftNextLine());
2672
lastGeneratedLine++;
2673
}
2674
if (lastGeneratedColumn < mapping.generatedColumn) {
2675
var nextLine = remainingLines[0];
2676
node.add(nextLine.substr(0, mapping.generatedColumn));
2677
remainingLines[0] = nextLine.substr(mapping.generatedColumn);
2678
lastGeneratedColumn = mapping.generatedColumn;
2679
}
2680
lastMapping = mapping;
2681
}, this);
2682
// We have processed all mappings.
2683
if (remainingLines.length > 0) {
2684
if (lastMapping) {
2685
// Associate the remaining code in the current line with "lastMapping"
2686
addMappingWithCode(lastMapping, shiftNextLine());
2687
}
2688
// and add the remaining lines without any mapping
2689
node.add(remainingLines.join(""));
2690
}
2691
2692
// Copy sourcesContent into SourceNode
2693
aSourceMapConsumer.sources.forEach(function (sourceFile) {
2694
var content = aSourceMapConsumer.sourceContentFor(sourceFile);
2695
if (content != null) {
2696
if (aRelativePath != null) {
2697
sourceFile = util.join(aRelativePath, sourceFile);
2698
}
2699
node.setSourceContent(sourceFile, content);
2700
}
2701
});
2702
2703
return node;
2704
2705
function addMappingWithCode(mapping, code) {
2706
if (mapping === null || mapping.source === undefined) {
2707
node.add(code);
2708
} else {
2709
var source = aRelativePath
2710
? util.join(aRelativePath, mapping.source)
2711
: mapping.source;
2712
node.add(new SourceNode(mapping.originalLine,
2713
mapping.originalColumn,
2714
source,
2715
code,
2716
mapping.name));
2717
}
2718
}
2719
};
2720
2721
/**
2722
* Add a chunk of generated JS to this source node.
2723
*
2724
* @param aChunk A string snippet of generated JS code, another instance of
2725
* SourceNode, or an array where each member is one of those things.
2726
*/
2727
SourceNode.prototype.add = function SourceNode_add(aChunk) {
2728
if (Array.isArray(aChunk)) {
2729
aChunk.forEach(function (chunk) {
2730
this.add(chunk);
2731
}, this);
2732
}
2733
else if (aChunk[isSourceNode] || typeof aChunk === "string") {
2734
if (aChunk) {
2735
this.children.push(aChunk);
2736
}
2737
}
2738
else {
2739
throw new TypeError(
2740
"Expected a SourceNode, string, or an array of SourceNodes and strings. Got " + aChunk
2741
);
2742
}
2743
return this;
2744
};
2745
2746
/**
2747
* Add a chunk of generated JS to the beginning of this source node.
2748
*
2749
* @param aChunk A string snippet of generated JS code, another instance of
2750
* SourceNode, or an array where each member is one of those things.
2751
*/
2752
SourceNode.prototype.prepend = function SourceNode_prepend(aChunk) {
2753
if (Array.isArray(aChunk)) {
2754
for (var i = aChunk.length-1; i >= 0; i--) {
2755
this.prepend(aChunk[i]);
2756
}
2757
}
2758
else if (aChunk[isSourceNode] || typeof aChunk === "string") {
2759
this.children.unshift(aChunk);
2760
}
2761
else {
2762
throw new TypeError(
2763
"Expected a SourceNode, string, or an array of SourceNodes and strings. Got " + aChunk
2764
);
2765
}
2766
return this;
2767
};
2768
2769
/**
2770
* Walk over the tree of JS snippets in this node and its children. The
2771
* walking function is called once for each snippet of JS and is passed that
2772
* snippet and the its original associated source's line/column location.
2773
*
2774
* @param aFn The traversal function.
2775
*/
2776
SourceNode.prototype.walk = function SourceNode_walk(aFn) {
2777
var chunk;
2778
for (var i = 0, len = this.children.length; i < len; i++) {
2779
chunk = this.children[i];
2780
if (chunk[isSourceNode]) {
2781
chunk.walk(aFn);
2782
}
2783
else {
2784
if (chunk !== '') {
2785
aFn(chunk, { source: this.source,
2786
line: this.line,
2787
column: this.column,
2788
name: this.name });
2789
}
2790
}
2791
}
2792
};
2793
2794
/**
2795
* Like `String.prototype.join` except for SourceNodes. Inserts `aStr` between
2796
* each of `this.children`.
2797
*
2798
* @param aSep The separator.
2799
*/
2800
SourceNode.prototype.join = function SourceNode_join(aSep) {
2801
var newChildren;
2802
var i;
2803
var len = this.children.length;
2804
if (len > 0) {
2805
newChildren = [];
2806
for (i = 0; i < len-1; i++) {
2807
newChildren.push(this.children[i]);
2808
newChildren.push(aSep);
2809
}
2810
newChildren.push(this.children[i]);
2811
this.children = newChildren;
2812
}
2813
return this;
2814
};
2815
2816
/**
2817
* Call String.prototype.replace on the very right-most source snippet. Useful
2818
* for trimming whitespace from the end of a source node, etc.
2819
*
2820
* @param aPattern The pattern to replace.
2821
* @param aReplacement The thing to replace the pattern with.
2822
*/
2823
SourceNode.prototype.replaceRight = function SourceNode_replaceRight(aPattern, aReplacement) {
2824
var lastChild = this.children[this.children.length - 1];
2825
if (lastChild[isSourceNode]) {
2826
lastChild.replaceRight(aPattern, aReplacement);
2827
}
2828
else if (typeof lastChild === 'string') {
2829
this.children[this.children.length - 1] = lastChild.replace(aPattern, aReplacement);
2830
}
2831
else {
2832
this.children.push(''.replace(aPattern, aReplacement));
2833
}
2834
return this;
2835
};
2836
2837
/**
2838
* Set the source content for a source file. This will be added to the SourceMapGenerator
2839
* in the sourcesContent field.
2840
*
2841
* @param aSourceFile The filename of the source file
2842
* @param aSourceContent The content of the source file
2843
*/
2844
SourceNode.prototype.setSourceContent =
2845
function SourceNode_setSourceContent(aSourceFile, aSourceContent) {
2846
this.sourceContents[util.toSetString(aSourceFile)] = aSourceContent;
2847
};
2848
2849
/**
2850
* Walk over the tree of SourceNodes. The walking function is called for each
2851
* source file content and is passed the filename and source content.
2852
*
2853
* @param aFn The traversal function.
2854
*/
2855
SourceNode.prototype.walkSourceContents =
2856
function SourceNode_walkSourceContents(aFn) {
2857
for (var i = 0, len = this.children.length; i < len; i++) {
2858
if (this.children[i][isSourceNode]) {
2859
this.children[i].walkSourceContents(aFn);
2860
}
2861
}
2862
2863
var sources = Object.keys(this.sourceContents);
2864
for (var i = 0, len = sources.length; i < len; i++) {
2865
aFn(util.fromSetString(sources[i]), this.sourceContents[sources[i]]);
2866
}
2867
};
2868
2869
/**
2870
* Return the string representation of this source node. Walks over the tree
2871
* and concatenates all the various snippets together to one string.
2872
*/
2873
SourceNode.prototype.toString = function SourceNode_toString() {
2874
var str = "";
2875
this.walk(function (chunk) {
2876
str += chunk;
2877
});
2878
return str;
2879
};
2880
2881
/**
2882
* Returns the string representation of this source node along with a source
2883
* map.
2884
*/
2885
SourceNode.prototype.toStringWithSourceMap = function SourceNode_toStringWithSourceMap(aArgs) {
2886
var generated = {
2887
code: "",
2888
line: 1,
2889
column: 0
2890
};
2891
var map = new SourceMapGenerator(aArgs);
2892
var sourceMappingActive = false;
2893
var lastOriginalSource = null;
2894
var lastOriginalLine = null;
2895
var lastOriginalColumn = null;
2896
var lastOriginalName = null;
2897
this.walk(function (chunk, original) {
2898
generated.code += chunk;
2899
if (original.source !== null
2900
&& original.line !== null
2901
&& original.column !== null) {
2902
if(lastOriginalSource !== original.source
2903
|| lastOriginalLine !== original.line
2904
|| lastOriginalColumn !== original.column
2905
|| lastOriginalName !== original.name) {
2906
map.addMapping({
2907
source: original.source,
2908
original: {
2909
line: original.line,
2910
column: original.column
2911
},
2912
generated: {
2913
line: generated.line,
2914
column: generated.column
2915
},
2916
name: original.name
2917
});
2918
}
2919
lastOriginalSource = original.source;
2920
lastOriginalLine = original.line;
2921
lastOriginalColumn = original.column;
2922
lastOriginalName = original.name;
2923
sourceMappingActive = true;
2924
} else if (sourceMappingActive) {
2925
map.addMapping({
2926
generated: {
2927
line: generated.line,
2928
column: generated.column
2929
}
2930
});
2931
lastOriginalSource = null;
2932
sourceMappingActive = false;
2933
}
2934
for (var idx = 0, length = chunk.length; idx < length; idx++) {
2935
if (chunk.charCodeAt(idx) === NEWLINE_CODE) {
2936
generated.line++;
2937
generated.column = 0;
2938
// Mappings end at eol
2939
if (idx + 1 === length) {
2940
lastOriginalSource = null;
2941
sourceMappingActive = false;
2942
} else if (sourceMappingActive) {
2943
map.addMapping({
2944
source: original.source,
2945
original: {
2946
line: original.line,
2947
column: original.column
2948
},
2949
generated: {
2950
line: generated.line,
2951
column: generated.column
2952
},
2953
name: original.name
2954
});
2955
}
2956
} else {
2957
generated.column++;
2958
}
2959
}
2960
});
2961
this.walkSourceContents(function (sourceFile, sourceContent) {
2962
map.setSourceContent(sourceFile, sourceContent);
2963
});
2964
2965
return { code: generated.code, map: map };
2966
};
2967
2968
exports.SourceNode = SourceNode;
2969
2970
});
2971
/* -*- Mode: js; js-indent-level: 2; -*- */
2972
///////////////////////////////////////////////////////////////////////////////
2973
2974
this.sourceMap = {
2975
SourceMapConsumer: require('source-map/source-map-consumer').SourceMapConsumer,
2976
SourceMapGenerator: require('source-map/source-map-generator').SourceMapGenerator,
2977
SourceNode: require('source-map/source-node').SourceNode
2978
};
2979
2980