| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /// Standalone utility that manages loading source maps for all Dart scripts | 5 /// Standalone utility that manages loading source maps for all Dart scripts |
| 6 /// on the page compiled with DDC. | 6 /// on the page compiled with DDC. |
| 7 /// | 7 /// |
| 8 /// Example JavaScript usage: | 8 /// Example JavaScript usage: |
| 9 /// $dartStackTraceUtility.addLoadedListener(function() { | 9 /// $dartStackTraceUtility.addLoadedListener(function() { |
| 10 /// // All Dart source maps are now loaded. It is now safe to start your | 10 /// // All Dart source maps are now loaded. It is now safe to start your |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 import 'package:source_span/source_span.dart'; | 32 import 'package:source_span/source_span.dart'; |
| 33 import 'package:stack_trace/stack_trace.dart'; | 33 import 'package:stack_trace/stack_trace.dart'; |
| 34 | 34 |
| 35 typedef void ReadyCallback(); | 35 typedef void ReadyCallback(); |
| 36 | 36 |
| 37 /// Global object DDC uses to see if a stack trace utility has been registered. | 37 /// Global object DDC uses to see if a stack trace utility has been registered. |
| 38 @JS(r'$dartStackTraceUtility') | 38 @JS(r'$dartStackTraceUtility') |
| 39 external set dartStackTraceUtility(DartStackTraceUtility value); | 39 external set dartStackTraceUtility(DartStackTraceUtility value); |
| 40 | 40 |
| 41 typedef String StackTraceMapper(String stackTrace); | 41 typedef String StackTraceMapper(String stackTrace); |
| 42 typedef String SourceMapProvider(String modulePath); | 42 typedef dynamic SourceMapProvider(String modulePath); |
| 43 typedef String SetSourceMapProvider(SourceMapProvider provider); | 43 typedef String SetSourceMapProvider(SourceMapProvider provider); |
| 44 | 44 |
| 45 @JS() | 45 @JS() |
| 46 @anonymous | 46 @anonymous |
| 47 class DartStackTraceUtility { | 47 class DartStackTraceUtility { |
| 48 external factory DartStackTraceUtility( | 48 external factory DartStackTraceUtility( |
| 49 {StackTraceMapper mapper, SetSourceMapProvider setSourceMapProvider}); | 49 {StackTraceMapper mapper, SetSourceMapProvider setSourceMapProvider}); |
| 50 } | 50 } |
| 51 | 51 |
| 52 @JS('JSON.stringify') |
| 53 external String _stringify(dynamic json); |
| 54 |
| 52 /// Source mapping that is waits to parse source maps until they match the uri | 55 /// Source mapping that is waits to parse source maps until they match the uri |
| 53 /// of a requested source map. | 56 /// of a requested source map. |
| 54 /// | 57 /// |
| 55 /// This improves startup performance compared to using MappingBundle directly. | 58 /// This improves startup performance compared to using MappingBundle directly. |
| 56 /// The unparsed data for the source maps must still be loaded before | 59 /// The unparsed data for the source maps must still be loaded before |
| 57 /// LazyMapping is used. | 60 /// LazyMapping is used. |
| 58 class LazyMapping extends Mapping { | 61 class LazyMapping extends Mapping { |
| 59 MappingBundle _bundle = new MappingBundle(); | 62 MappingBundle _bundle = new MappingBundle(); |
| 60 SourceMapProvider _provider; | 63 SourceMapProvider _provider; |
| 61 | 64 |
| 62 LazyMapping(this._provider); | 65 LazyMapping(this._provider); |
| 63 | 66 |
| 64 List toJson() => _bundle.toJson(); | 67 List toJson() => _bundle.toJson(); |
| 65 | 68 |
| 66 SourceMapSpan spanFor(int line, int column, | 69 SourceMapSpan spanFor(int line, int column, |
| 67 {Map<String, SourceFile> files, String uri}) { | 70 {Map<String, SourceFile> files, String uri}) { |
| 68 if (uri == null) { | 71 if (uri == null) { |
| 69 throw new ArgumentError.notNull('uri'); | 72 throw new ArgumentError.notNull('uri'); |
| 70 } | 73 } |
| 71 | 74 |
| 72 if (!_bundle.containsMapping(uri)) { | 75 if (!_bundle.containsMapping(uri)) { |
| 73 var rawMap = _provider(uri); | 76 var rawMap = _provider(uri); |
| 74 if (rawMap != null) { | 77 if (rawMap != null) { |
| 78 if (rawMap is! String) { |
| 79 // The sourcemap was passed as regular JavaScript JSON. |
| 80 rawMap = _stringify(rawMap); |
| 81 } |
| 75 SingleMapping mapping = parse(rawMap); | 82 SingleMapping mapping = parse(rawMap); |
| 76 mapping | 83 mapping |
| 77 ..targetUrl = uri | 84 ..targetUrl = uri |
| 78 ..sourceRoot = '${path.dirname(uri)}/'; | 85 ..sourceRoot = '${path.dirname(uri)}/'; |
| 79 _bundle.addMapping(mapping); | 86 _bundle.addMapping(mapping); |
| 80 } | 87 } |
| 81 } | 88 } |
| 82 var span = _bundle.spanFor(line, column, files: files, uri: uri); | 89 var span = _bundle.spanFor(line, column, files: files, uri: uri); |
| 83 // TODO(jacobr): we shouldn't have to filter out invalid sourceUrl entries | 90 // TODO(jacobr): we shouldn't have to filter out invalid sourceUrl entries |
| 84 // here. | 91 // here. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 114 void setSourceMapProvider(SourceMapProvider provider) async { | 121 void setSourceMapProvider(SourceMapProvider provider) async { |
| 115 _mapping = new LazyMapping(provider); | 122 _mapping = new LazyMapping(provider); |
| 116 } | 123 } |
| 117 | 124 |
| 118 main() { | 125 main() { |
| 119 // Register with DDC. | 126 // Register with DDC. |
| 120 dartStackTraceUtility = new DartStackTraceUtility( | 127 dartStackTraceUtility = new DartStackTraceUtility( |
| 121 mapper: allowInterop(mapper), | 128 mapper: allowInterop(mapper), |
| 122 setSourceMapProvider: allowInterop(setSourceMapProvider)); | 129 setSourceMapProvider: allowInterop(setSourceMapProvider)); |
| 123 } | 130 } |
| OLD | NEW |