@@ -12,29 +12,70 @@ try {
12
12
/* nop */
13
13
}
14
14
15
- // Only install once if called multiple times
16
- var errorFormatterInstalled = false ;
17
- var uncaughtShimInstalled = false ;
15
+ // Increment this if the format of sharedData changes in a breaking way.
16
+ var sharedDataVersion = 1 ;
18
17
19
- // If true, the caches are reset before a stack trace formatting operation
20
- var emptyCacheBetweenOperations = false ;
18
+ function initializeSharedData ( defaults ) {
19
+ var sharedDataKey = 'source-map-support/sharedData' ;
20
+ if ( typeof Symbol !== 'undefined' ) {
21
+ sharedDataKey = Symbol . for ( sharedDataKey ) ;
22
+ }
23
+ var sharedData = this [ sharedDataKey ] ;
24
+ if ( ! sharedData ) {
25
+ sharedData = { version : sharedDataVersion } ;
26
+ if ( Object . defineProperty ) {
27
+ Object . defineProperty ( this , sharedDataKey , { value : sharedData } ) ;
28
+ } else {
29
+ this [ sharedDataKey ] = sharedData ;
30
+ }
31
+ }
32
+ if ( sharedDataVersion !== sharedData . version ) {
33
+ throw new Error ( "Multiple incompatible instances of source-map-support were loaded" ) ;
34
+ }
35
+ for ( var key in defaults ) {
36
+ if ( ! ( key in sharedData ) ) {
37
+ sharedData [ key ] = defaults [ key ] ;
38
+ }
39
+ }
40
+ return sharedData ;
41
+ }
21
42
22
- // Supports {browser, node, auto}
23
- var environment = "auto" ;
43
+ // If multiple instances of source-map-support are loaded into the same
44
+ // context, they shouldn't overwrite each other. By storing handlers, caches,
45
+ // and other state on a shared object, different instances of
46
+ // source-map-support can work together in a limited way. This does require
47
+ // that future versions of source-map-support continue to support the fields on
48
+ // this object. If this internal contract ever needs to be broken, increment
49
+ // sharedDataVersion. (This version number is not the same as any of the
50
+ // package's version numbers, which should reflect the *external* API of
51
+ // source-map-support.)
52
+ var sharedData = initializeSharedData ( {
24
53
25
- // Maps a file path to a string containing the file contents
26
- var fileContentsCache = { } ;
54
+ // Only install once if called multiple times
55
+ errorFormatterInstalled : false ,
56
+ uncaughtShimInstalled : false ,
27
57
28
- // Maps a file path to a source map for that file
29
- var sourceMapCache = { } ;
58
+ // If true, the caches are reset before a stack trace formatting operation
59
+ emptyCacheBetweenOperations : false ,
60
+
61
+ // Maps a file path to a string containing the file contents
62
+ fileContentsCache : { } ,
63
+
64
+ // Maps a file path to a source map for that file
65
+ sourceMapCache : { } ,
66
+
67
+ // Priority list of retrieve handlers
68
+ retrieveFileHandlers : [ ] ,
69
+ retrieveMapHandlers : [ ] ,
70
+
71
+ } ) ;
72
+
73
+ // Supports {browser, node, auto}
74
+ var environment = "auto" ;
30
75
31
76
// Regex for detecting source maps
32
77
var reSourceMap = / ^ d a t a : a p p l i c a t i o n \/ j s o n [ ^ , ] + b a s e 6 4 , / ;
33
78
34
- // Priority list of retrieve handlers
35
- var retrieveFileHandlers = [ ] ;
36
- var retrieveMapHandlers = [ ] ;
37
-
38
79
function isInBrowser ( ) {
39
80
if ( environment === "browser" )
40
81
return true ;
@@ -59,9 +100,9 @@ function handlerExec(list) {
59
100
} ;
60
101
}
61
102
62
- var retrieveFile = handlerExec ( retrieveFileHandlers ) ;
103
+ var retrieveFile = handlerExec ( sharedData . retrieveFileHandlers ) ;
63
104
64
- retrieveFileHandlers . push ( function ( path ) {
105
+ sharedData . retrieveFileHandlers . push ( function ( path ) {
65
106
// Trim the path to make sure there is no extra whitespace.
66
107
path = path . trim ( ) ;
67
108
if ( / ^ f i l e : / . test ( path ) ) {
@@ -72,8 +113,8 @@ retrieveFileHandlers.push(function(path) {
72
113
'/' ; // file:///root-dir/file -> /root-dir/file
73
114
} ) ;
74
115
}
75
- if ( path in fileContentsCache ) {
76
- return fileContentsCache [ path ] ;
116
+ if ( path in sharedData . fileContentsCache ) {
117
+ return sharedData . fileContentsCache [ path ] ;
77
118
}
78
119
79
120
var contents = null ;
@@ -95,7 +136,7 @@ retrieveFileHandlers.push(function(path) {
95
136
}
96
137
}
97
138
98
- return fileContentsCache [ path ] = contents ;
139
+ return sharedData . fileContentsCache [ path ] = contents ;
99
140
} ) ;
100
141
101
142
// Support URLs relative to a directory, but be careful about a protocol prefix
@@ -150,8 +191,8 @@ function retrieveSourceMapURL(source) {
150
191
// there is no source map. The map field may be either a string or the parsed
151
192
// JSON object (ie, it must be a valid argument to the SourceMapConsumer
152
193
// constructor).
153
- var retrieveSourceMap = handlerExec ( retrieveMapHandlers ) ;
154
- retrieveMapHandlers . push ( function ( source ) {
194
+ var retrieveSourceMap = handlerExec ( sharedData . retrieveMapHandlers ) ;
195
+ sharedData . retrieveMapHandlers . push ( function ( source ) {
155
196
var sourceMappingURL = retrieveSourceMapURL ( source ) ;
156
197
if ( ! sourceMappingURL ) return null ;
157
198
@@ -179,12 +220,12 @@ retrieveMapHandlers.push(function(source) {
179
220
} ) ;
180
221
181
222
function mapSourcePosition ( position ) {
182
- var sourceMap = sourceMapCache [ position . source ] ;
223
+ var sourceMap = sharedData . sourceMapCache [ position . source ] ;
183
224
if ( ! sourceMap ) {
184
225
// Call the (overrideable) retrieveSourceMap function to get the source map.
185
226
var urlAndMap = retrieveSourceMap ( position . source ) ;
186
227
if ( urlAndMap ) {
187
- sourceMap = sourceMapCache [ position . source ] = {
228
+ sourceMap = sharedData . sourceMapCache [ position . source ] = {
188
229
url : urlAndMap . url ,
189
230
map : new SourceMapConsumer ( urlAndMap . map )
190
231
} ;
@@ -196,12 +237,12 @@ function mapSourcePosition(position) {
196
237
var contents = sourceMap . map . sourcesContent [ i ] ;
197
238
if ( contents ) {
198
239
var url = supportRelativeURL ( sourceMap . url , source ) ;
199
- fileContentsCache [ url ] = contents ;
240
+ sharedData . fileContentsCache [ url ] = contents ;
200
241
}
201
242
} ) ;
202
243
}
203
244
} else {
204
- sourceMap = sourceMapCache [ position . source ] = {
245
+ sourceMap = sharedData . sourceMapCache [ position . source ] = {
205
246
url : null ,
206
247
map : null
207
248
} ;
@@ -383,9 +424,9 @@ function wrapCallSite(frame) {
383
424
// This function is part of the V8 stack trace API, for more info see:
384
425
// http://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
385
426
function prepareStackTrace ( error , stack ) {
386
- if ( emptyCacheBetweenOperations ) {
387
- fileContentsCache = { } ;
388
- sourceMapCache = { } ;
427
+ if ( sharedData . emptyCacheBetweenOperations ) {
428
+ sharedData . fileContentsCache = { } ;
429
+ sharedData . sourceMapCache = { } ;
389
430
}
390
431
391
432
return error + stack . map ( function ( frame ) {
@@ -402,7 +443,7 @@ function getErrorSource(error) {
402
443
var column = + match [ 3 ] ;
403
444
404
445
// Support the inline sourceContents inside the source map
405
- var contents = fileContentsCache [ source ] ;
446
+ var contents = sharedData . fileContentsCache [ source ] ;
406
447
407
448
// Support files on disk
408
449
if ( ! contents && fs && fs . existsSync ( source ) ) {
@@ -472,20 +513,20 @@ exports.install = function(options) {
472
513
// directly from disk.
473
514
if ( options . retrieveFile ) {
474
515
if ( options . overrideRetrieveFile ) {
475
- retrieveFileHandlers . length = 0 ;
516
+ sharedData . retrieveFileHandlers . length = 0 ;
476
517
}
477
518
478
- retrieveFileHandlers . unshift ( options . retrieveFile ) ;
519
+ sharedData . retrieveFileHandlers . unshift ( options . retrieveFile ) ;
479
520
}
480
521
481
522
// Allow source maps to be found by methods other than reading the files
482
523
// directly from disk.
483
524
if ( options . retrieveSourceMap ) {
484
525
if ( options . overrideRetrieveSourceMap ) {
485
- retrieveMapHandlers . length = 0 ;
526
+ sharedData . retrieveMapHandlers . length = 0 ;
486
527
}
487
528
488
- retrieveMapHandlers . unshift ( options . retrieveSourceMap ) ;
529
+ sharedData . retrieveMapHandlers . unshift ( options . retrieveSourceMap ) ;
489
530
}
490
531
491
532
// Support runtime transpilers that include inline source maps
@@ -500,8 +541,8 @@ exports.install = function(options) {
500
541
501
542
if ( ! $compile . __sourceMapSupport ) {
502
543
Module . prototype . _compile = function ( content , filename ) {
503
- fileContentsCache [ filename ] = content ;
504
- sourceMapCache [ filename ] = undefined ;
544
+ sharedData . fileContentsCache [ filename ] = content ;
545
+ sharedData . sourceMapCache [ filename ] = undefined ;
505
546
return $compile . call ( this , content , filename ) ;
506
547
} ;
507
548
@@ -510,18 +551,18 @@ exports.install = function(options) {
510
551
}
511
552
512
553
// Configure options
513
- if ( ! emptyCacheBetweenOperations ) {
514
- emptyCacheBetweenOperations = 'emptyCacheBetweenOperations' in options ?
554
+ if ( ! sharedData . emptyCacheBetweenOperations ) {
555
+ sharedData . emptyCacheBetweenOperations = 'emptyCacheBetweenOperations' in options ?
515
556
options . emptyCacheBetweenOperations : false ;
516
557
}
517
558
518
559
// Install the error reformatter
519
- if ( ! errorFormatterInstalled ) {
520
- errorFormatterInstalled = true ;
560
+ if ( ! sharedData . errorFormatterInstalled ) {
561
+ sharedData . errorFormatterInstalled = true ;
521
562
Error . prepareStackTrace = prepareStackTrace ;
522
563
}
523
564
524
- if ( ! uncaughtShimInstalled ) {
565
+ if ( ! sharedData . uncaughtShimInstalled ) {
525
566
var installHandler = 'handleUncaughtExceptions' in options ?
526
567
options . handleUncaughtExceptions : true ;
527
568
@@ -533,7 +574,7 @@ exports.install = function(options) {
533
574
// generated JavaScript code will be shown above the stack trace instead of
534
575
// the original source code.
535
576
if ( installHandler && hasGlobalProcessEventEmitter ( ) ) {
536
- uncaughtShimInstalled = true ;
577
+ sharedData . uncaughtShimInstalled = true ;
537
578
shimEmitUncaughtException ( ) ;
538
579
}
539
580
}
0 commit comments