-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathlocation.dart
325 lines (281 loc) · 10.8 KB
/
location.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:async/async.dart';
import 'package:dwds/src/loaders/require.dart';
import 'package:logging/logging.dart';
import 'package:path/path.dart' as p;
import 'package:source_maps/parser.dart';
import 'package:source_maps/source_maps.dart';
import '../loaders/strategy.dart';
import '../readers/asset_reader.dart';
import '../utilities/dart_uri.dart';
import 'modules.dart';
var _startTokenId = 1337;
/// A source location, with both Dart and JS information.
class Location {
final JsLocation jsLocation;
final DartLocation dartLocation;
/// An arbitrary integer value used to represent this location.
final int tokenPos;
Location._(
this.jsLocation,
this.dartLocation,
) : tokenPos = _startTokenId++;
static Location from(
String module,
TargetLineEntry lineEntry,
TargetEntry entry,
DartUri dartUri,
) {
final dartLine = entry.sourceLine;
final dartColumn = entry.sourceColumn;
final jsLine = lineEntry.line;
final jsColumn = entry.column;
// lineEntry data is 0 based according to:
// https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k
return Location._(
JsLocation.fromZeroBased(module, jsLine, jsColumn),
DartLocation.fromZeroBased(dartUri, dartLine ?? 0, dartColumn ?? 0),
);
}
@override
String toString() => '$dartLocation -> $jsLocation';
}
/// Location information for a Dart source.
class DartLocation {
final DartUri uri;
/// 1 based row offset within the Dart source code.
final int line;
/// 1 based column offset within the Dart source code.
final int column;
DartLocation._(
this.uri,
this.line,
this.column,
);
int compareTo(DartLocation other) => compareToLine(other.line, other.column);
int compareToLine(int otherLine, int otherColumn) {
final result = line.compareTo(otherLine);
return result == 0 ? column.compareTo(otherColumn) : result;
}
@override
String toString() => '[${uri.serverPath}:$line:$column]';
factory DartLocation.fromZeroBased(DartUri uri, int line, int column) =>
DartLocation._(uri, line + 1, column + 1);
}
/// Location information for a JS source.
class JsLocation {
final String module;
/// 0 based row offset within the JS source code.
final int line;
/// 0 based column offset within the JS source code.
final int column;
JsLocation._(
this.module,
this.line,
this.column,
);
int compareTo(JsLocation other) => compareToLine(other.line, other.column);
int compareToLine(int otherLine, int otherColumn) {
final result = line.compareTo(otherLine);
return result == 0 ? column.compareTo(otherColumn) : result;
}
@override
String toString() => '[$module:$line:$column]';
// JS Location is 0 based according to:
// https://chromedevtools.github.io/devtools-protocol/tot/Debugger#type-Location
factory JsLocation.fromZeroBased(String module, int line, int column) =>
JsLocation._(module, line, column);
}
/// Contains meta data for known [Location]s.
class Locations {
final _logger = Logger('Locations');
/// [Location] data for Dart server path.
final Map<String, Set<Location>> _sourceToLocation = {};
final Map<String, AsyncMemoizer<Set<Location>>> _locationMemoizer = {};
/// `tokenPosTable` for Dart server path, as defined in the
/// Dart VM Service Protocol:
/// https://github.com/dart-lang/sdk/blob/main/runtime/vm/service/service.md#script
final Map<String, List<List<int>>> _sourceToTokenPosTable = {};
/// The set of all known [Location]s for a module.
final Map<String, Set<Location>> _moduleToLocations = {};
final AssetReader _assetReader;
final Modules _modules;
final String _root;
late String _entrypoint;
Locations(this._assetReader, this._modules, this._root);
Modules get modules => _modules;
void initialize(String entrypoint) {
_sourceToTokenPosTable.clear();
_sourceToLocation.clear();
_locationMemoizer.clear();
_moduleToLocations.clear();
_entrypoint = entrypoint;
}
/// Returns all [Location] data for a provided Dart source.
Future<Set<Location>> locationsForDart(String serverPath) async {
final module = await _modules.moduleForSource(serverPath);
if (module == null) {
_logger.warning('No module for server path $serverPath');
} else {
await _locationsForModule(module);
}
return _sourceToLocation[serverPath] ?? {};
}
/// Returns all [Location] data for a provided JS server path.
Future<Set<Location>> locationsForUrl(String url) async {
final module = await globalLoadStrategy.moduleForServerPath(
_entrypoint, Uri.parse(url).path);
final cache = _moduleToLocations[module];
if (cache != null) return cache;
if (module != null) {
await _locationsForModule(module);
}
return _moduleToLocations[module] ?? {};
}
/// Find the [Location] for the given Dart source position.
///
/// The [line] number is 1-based.
Future<Location?> locationForDart(DartUri uri, int line, int column) async {
final locations = await locationsForDart(uri.serverPath);
return _bestDartLocation(locations, line, column);
}
/// Find the [Location] for the given JS source position.
///
/// The [line] number is 0-based.
Future<Location?> locationForJs(String url, int line, int? column) async {
final locations = await locationsForUrl(url);
return _bestJsLocation(locations, line, column);
}
/// Find closest existing Dart location for the line and column.
///
/// Dart columns for breakpoints are either exact or start at the
/// beginning of the line - return the first existing location
/// that comes after the given column.
Location? _bestDartLocation(
Iterable<Location> locations, int line, int column) {
Location? bestLocation;
for (var location in locations) {
if (location.dartLocation.line == line &&
location.dartLocation.column >= column) {
bestLocation ??= location;
if (location.dartLocation.compareTo(bestLocation.dartLocation) < 0) {
bestLocation = location;
}
}
}
return bestLocation;
}
/// Find closest existing JavaScript location for the line and column.
///
/// Some JS locations are not stored in the source maps, so we find the
/// closest existing location coming before the given column.
///
/// This is a known problem that other code bases solve using by finding
/// the closest location to the current one:
///
/// https://github.com/microsoft/vscode-js-debug/blob/536f96bae61a3d87546b61bc7916097904c81429/src/common/sourceUtils.ts#L286
Location? _bestJsLocation(
Iterable<Location> locations, int line, int? column) {
column ??= 0;
Location? bestLocation;
for (var location in locations) {
if (location.jsLocation.compareToLine(line, column) <= 0) {
bestLocation ??= location;
if (location.jsLocation.compareTo(bestLocation.jsLocation) > 0) {
bestLocation = location;
}
}
}
return bestLocation;
}
/// Returns the tokenPosTable for the provided Dart script path as defined
/// in:
/// https://github.com/dart-lang/sdk/blob/main/runtime/vm/service/service.md#script
Future<List<List<int>>> tokenPosTableFor(String serverPath) async {
var tokenPosTable = _sourceToTokenPosTable[serverPath];
if (tokenPosTable != null) return tokenPosTable;
// Construct the tokenPosTable which is of the form:
// [lineNumber, (tokenId, columnNumber)*]
tokenPosTable = <List<int>>[];
final locations = await locationsForDart(serverPath);
final lineNumberToLocation = <int, Set<Location>>{};
for (var location in locations) {
lineNumberToLocation
.putIfAbsent(location.dartLocation.line, () => <Location>{})
.add(location);
}
for (var lineNumber in lineNumberToLocation.keys) {
final locations = lineNumberToLocation[lineNumber]!;
tokenPosTable.add([
lineNumber,
for (var location in locations) ...[
location.tokenPos,
location.dartLocation.column
]
]);
}
_sourceToTokenPosTable[serverPath] = tokenPosTable;
return tokenPosTable;
}
/// Returns all known [Location]s for the provided [module].
///
/// [module] refers to the JS path of a DDC module without the extension.
///
/// This will populate the [_sourceToLocation] and [_moduleToLocations] maps.
Future<Set<Location>> _locationsForModule(String module) async {
final memoizer =
_locationMemoizer.putIfAbsent(module, () => AsyncMemoizer());
return await memoizer.runOnce(() async {
if (_moduleToLocations.containsKey(module)) {
return _moduleToLocations[module]!;
}
final result = <Location>{};
if (module.isEmpty) return _moduleToLocations[module] = result;
if (module.endsWith('dart_sdk') || module.endsWith('dart_library')) {
return result;
}
final modulePath =
await globalLoadStrategy.serverPathForModule(_entrypoint, module);
final sourceMapPath =
await globalLoadStrategy.sourceMapPathForModule(_entrypoint, module);
final sourceMapContents =
await _assetReader.sourceMapContents(sourceMapPath);
final scriptLocation = p.url.dirname('/${relativizePath(modulePath)}');
if (sourceMapContents == null) return result;
// This happens to be a [SingleMapping] today in DDC.
final mapping = parse(sourceMapContents);
if (mapping is SingleMapping) {
// Create TokenPos for each entry in the source map.
for (var lineEntry in mapping.lines) {
for (var entry in lineEntry.entries) {
final index = entry.sourceUrlId;
if (index == null) continue;
// Source map URLS are relative to the script. They may have platform separators
// or they may use URL semantics. To be sure, we split and re-join them.
// This works on Windows because path treats both / and \ as separators.
// It will fail if the path has both separators in it.
final relativeSegments = p.split(mapping.urls[index]);
final path = p.url.normalize(
p.url.joinAll([scriptLocation, ...relativeSegments]));
final dartUri = DartUri(path, _root);
result.add(Location.from(
modulePath,
lineEntry,
entry,
dartUri,
));
}
}
}
for (var location in result) {
_sourceToLocation
.putIfAbsent(
location.dartLocation.uri.serverPath, () => <Location>{})
.add(location);
}
return _moduleToLocations[module] = result;
});
}
}