Skip to content

Commit d473ec5

Browse files
szuendDevtools-frontend LUCI CQ
authored andcommitted
[sdk] Use source-map-scopes-codec instead of prototype DevTools impl
This CL switches over decoding from the initial DevTools prototype to the codec library. The library represents the latest version of the "scopes" proposal. Follow-up changes will also update the types and remove the legacy encoding/decoding implementation. R=pfaffe@chromium.org Bug: 368222773 Change-Id: I73f9bb335419b9f4b173010b05fea9112bce9bcd Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6765502 Reviewed-by: Philip Pfaffe <pfaffe@chromium.org> Commit-Queue: Simon Zünd <szuend@chromium.org>
1 parent 0a42aeb commit d473ec5

3 files changed

Lines changed: 69 additions & 83 deletions

File tree

front_end/core/sdk/SourceMap.test.ts

Lines changed: 55 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
import * as TextUtils from '../../models/text_utils/text_utils.js';
66
import {describeWithEnvironment} from '../../testing/EnvironmentHelpers.js';
7-
import {encodeSourceMap, GeneratedRangeBuilder, OriginalScopeBuilder} from '../../testing/SourceMapEncoder.js';
7+
import {encodeSourceMap} from '../../testing/SourceMapEncoder.js';
8+
import * as ScopesCodec from '../../third_party/source-map-scopes-codec/source-map-scopes-codec.js';
89
import * as Platform from '../platform/platform.js';
910
import * as Root from '../root/root.js';
1011

@@ -1239,33 +1240,29 @@ describeWithEnvironment('SourceMap', () => {
12391240
it('can resolve generated positions with inlineFrameIndex', () => {
12401241
Root.Runtime.experiments.enableForTest(Root.Runtime.ExperimentName.USE_SOURCE_MAP_SCOPES);
12411242
// 'foo' calls 'bar', 'bar' calls 'baz'. 'bar' and 'baz' are inlined into 'foo'.
1242-
const names: string[] = [];
1243-
const originalScopes = [new OriginalScopeBuilder(names)
1244-
.start(0, 0, {kind: 'global'})
1245-
.start(10, 0, {kind: 'function', name: 'foo'})
1246-
.end(20, 0)
1247-
.start(30, 0, {kind: 'function', name: 'bar'})
1248-
.end(40, 0)
1249-
.start(50, 0, {kind: 'function', name: 'baz'})
1250-
.end(60, 0)
1251-
.end(70, 0)
1252-
.build()];
1253-
1254-
const generatedRanges =
1255-
new GeneratedRangeBuilder(names)
1256-
.start(0, 0, {definition: {sourceIdx: 0, scopeIdx: 0}})
1257-
.start(0, 0, {definition: {sourceIdx: 0, scopeIdx: 1}, isStackFrame: true})
1258-
.start(0, 5, {definition: {sourceIdx: 0, scopeIdx: 3}, callsite: {sourceIdx: 0, line: 15, column: 0}})
1259-
.start(0, 5, {definition: {sourceIdx: 0, scopeIdx: 5}, callsite: {sourceIdx: 0, line: 35, column: 0}})
1260-
.end(0, 10)
1261-
.end(0, 10)
1262-
.end(0, 10)
1263-
.end(0, 10)
1264-
.build();
1243+
const builder = new ScopesCodec.ScopeInfoBuilder();
1244+
builder.startScope(0, 0, {kind: 'global', key: 'global'})
1245+
.startScope(10, 0, {kind: 'function', key: 'foo', name: 'foo'})
1246+
.endScope(20, 0)
1247+
.startScope(30, 0, {kind: 'function', key: 'bar', name: 'bar'})
1248+
.endScope(40, 0)
1249+
.startScope(50, 0, {kind: 'function', key: 'baz', name: 'baz'})
1250+
.endScope(60, 0)
1251+
.endScope(70, 0);
1252+
1253+
builder.startRange(0, 0, {scopeKey: 'global'})
1254+
.startRange(0, 0, {scopeKey: 'foo', isStackFrame: true})
1255+
.startRange(0, 5, {scopeKey: 'bar', callSite: {sourceIndex: 0, line: 15, column: 0}})
1256+
.startRange(0, 5, {scopeKey: 'baz', callSite: {sourceIndex: 0, line: 35, column: 0}})
1257+
.endRange(0, 10)
1258+
.endRange(0, 10)
1259+
.endRange(0, 10)
1260+
.endRange(0, 10);
12651261

12661262
const sourceMap = new SDK.SourceMap.SourceMap(
12671263
compiledUrl, sourceMapJsonUrl,
1268-
{version: 3, sources: ['foo.ts'], mappings: '', names, originalScopes, generatedRanges});
1264+
ScopesCodec.encode(builder.build(), {version: 3, sources: ['foo.ts'], mappings: ''}) as
1265+
SDK.SourceMap.SourceMapV3Object);
12691266

12701267
assert.isNull(
12711268
sourceMap.findEntry(0, 7, 0)); // We don't have mappings, so inlineFrameIndex = 0 ('baz') has no entry.
@@ -1284,57 +1281,42 @@ describeWithEnvironment('SourceMap', () => {
12841281

12851282
it('combines "scopes" proposal scopes appropriately for index maps', () => {
12861283
Root.Runtime.experiments.enableForTest(Root.Runtime.ExperimentName.USE_SOURCE_MAP_SCOPES);
1287-
const names1: string[] = [];
1288-
const originalScopes1 = [new OriginalScopeBuilder(names1)
1289-
.start(0, 0, {kind: 'global'})
1290-
.start(10, 0, {name: 'foo', kind: 'function', isStackFrame: true})
1291-
.end(20, 0)
1292-
.end(30, 0)
1293-
.build()];
1294-
const generatedRanges1 = new GeneratedRangeBuilder(names1)
1295-
.start(0, 0, {definition: {sourceIdx: 0, scopeIdx: 0}})
1296-
.start(0, 7, {definition: {sourceIdx: 0, scopeIdx: 1}, isStackFrame: true})
1297-
.end(0, 14)
1298-
.end(0, 21)
1299-
.build();
1300-
const names2: string[] = [];
1301-
const originalScopes2 = [new OriginalScopeBuilder(names2)
1302-
.start(0, 0, {kind: 'global'})
1303-
.start(10, 0, {name: 'bar', kind: 'function', isStackFrame: true})
1304-
.end(20, 0)
1305-
.end(30, 0)
1306-
.build()];
1307-
const generatedRanges2 = new GeneratedRangeBuilder(names2)
1308-
.start(0, 0, {definition: {sourceIdx: 0, scopeIdx: 0}})
1309-
.start(0, 7, {definition: {sourceIdx: 0, scopeIdx: 1}, isStackFrame: true})
1310-
.end(0, 14)
1311-
.end(0, 21)
1312-
.build();
1284+
const info1 = new ScopesCodec.ScopeInfoBuilder()
1285+
.startScope(0, 0, {kind: 'global', key: 'global'})
1286+
.startScope(10, 0, {name: 'foo', key: 'foo', kind: 'function', isStackFrame: true})
1287+
.endScope(20, 0)
1288+
.endScope(30, 0)
1289+
.startRange(0, 0, {scopeKey: 'global'})
1290+
.startRange(0, 7, {scopeKey: 'foo', isStackFrame: true})
1291+
.endRange(0, 14)
1292+
.endRange(0, 21)
1293+
.build();
1294+
const map1 = ScopesCodec.encode(info1, {
1295+
version: 3,
1296+
sources: ['foo.ts'],
1297+
mappings: '',
1298+
});
1299+
1300+
const info2 = new ScopesCodec.ScopeInfoBuilder()
1301+
.startScope(0, 0, {kind: 'global', key: 'global'})
1302+
.startScope(10, 0, {name: 'bar', key: 'bar', kind: 'function', isStackFrame: true})
1303+
.endScope(20, 0)
1304+
.endScope(30, 0)
1305+
.startRange(0, 0, {scopeKey: 'global'})
1306+
.startRange(0, 7, {scopeKey: 'bar', isStackFrame: true})
1307+
.endRange(0, 14)
1308+
.endRange(0, 21)
1309+
.build();
1310+
const map2 = ScopesCodec.encode(info2, {
1311+
version: 3,
1312+
sources: ['bar.ts'],
1313+
mappings: '',
1314+
});
13131315
const indexMap: SDK.SourceMap.SourceMapV3 = {
13141316
version: 3,
13151317
sections: [
1316-
{
1317-
offset: {line: 0, column: 0},
1318-
map: {
1319-
version: 3,
1320-
sources: ['foo.ts'],
1321-
names: names1,
1322-
originalScopes: originalScopes1,
1323-
generatedRanges: generatedRanges1,
1324-
mappings: '',
1325-
},
1326-
},
1327-
{
1328-
offset: {line: 1, column: 100},
1329-
map: {
1330-
version: 3,
1331-
sources: ['bar.ts'],
1332-
names: names2,
1333-
originalScopes: originalScopes2,
1334-
generatedRanges: generatedRanges2,
1335-
mappings: '',
1336-
},
1337-
}
1318+
{offset: {line: 0, column: 0}, map: map1 as SDK.SourceMap.SourceMapV3Object},
1319+
{offset: {line: 1, column: 100}, map: map2 as SDK.SourceMap.SourceMapV3Object},
13381320
],
13391321
};
13401322

front_end/core/sdk/SourceMap.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@
3333
*/
3434

3535
import * as TextUtils from '../../models/text_utils/text_utils.js';
36+
import * as ScopesCodec from '../../third_party/source-map-scopes-codec/source-map-scopes-codec.js';
3637
import * as Common from '../common/common.js';
3738
import * as Platform from '../platform/platform.js';
3839
import * as Root from '../root/root.js';
3940

4041
import type {CallFrame, ScopeChainEntry} from './DebuggerModel.js';
4142
import {buildOriginalScopes, decodePastaRanges, type NamedFunctionRange} from './SourceMapFunctionRanges.js';
42-
import {decodeScopes, type OriginalScope, type Position as GeneratedPosition} from './SourceMapScopes.js';
43+
import type {OriginalScope, Position as GeneratedPosition} from './SourceMapScopes.js';
4344
import {SourceMapScopesInfo} from './SourceMapScopesInfo.js';
4445

4546
/**
@@ -61,6 +62,7 @@ export interface SourceMapV3Object {
6162

6263
names?: string[];
6364
ignoreList?: number[];
65+
scopes?: string;
6466
originalScopes?: string[];
6567
generatedRanges?: string;
6668
x_google_linecount?: number;
@@ -560,21 +562,23 @@ export class SourceMap {
560562
if (!this.#scopesInfo) {
561563
this.#scopesInfo = new SourceMapScopesInfo(this, {scopes: [], ranges: []});
562564
}
563-
if (map.originalScopes && map.generatedRanges) {
564-
const {originalScopes, generatedRanges} = decodeScopes(map, {line: baseLineNumber, column: baseColumnNumber});
565-
this.#scopesInfo.addOriginalScopes(originalScopes);
566-
this.#scopesInfo.addGeneratedRanges(generatedRanges);
565+
if (map.scopes) {
566+
const {scopes, ranges} = ScopesCodec.decode(
567+
map as ScopesCodec.SourceMapJson,
568+
{mode: ScopesCodec.DecodeMode.LAX, generatedOffset: {line: baseLineNumber, column: baseColumnNumber}});
569+
this.#scopesInfo.addOriginalScopes(scopes);
570+
this.#scopesInfo.addGeneratedRanges(ranges);
567571
} else if (map.x_com_bloomberg_sourcesFunctionMappings) {
568572
const originalScopes = this.parseBloombergScopes(map);
569573
this.#scopesInfo.addOriginalScopes(originalScopes);
570574
} else {
571575
// Keep the OriginalScope[] tree array consistent with sources.
572-
this.#scopesInfo.addOriginalScopes(new Array(map.sources.length));
576+
this.#scopesInfo.addOriginalScopes(new Array(map.sources.length).fill(null));
573577
}
574578
}
575579
}
576580

577-
private parseBloombergScopes(map: SourceMapV3Object): Array<OriginalScope|undefined> {
581+
private parseBloombergScopes(map: SourceMapV3Object): Array<OriginalScope|null> {
578582
const scopeList = map.x_com_bloomberg_sourcesFunctionMappings;
579583
if (!scopeList) {
580584
throw new Error('Cant decode pasta scopes without x_com_bloomberg_sourcesFunctionMappings field');
@@ -585,7 +589,7 @@ export class SourceMap {
585589

586590
return scopeList.map(rawScopes => {
587591
if (!rawScopes) {
588-
return undefined;
592+
return null;
589593
}
590594
const ranges = decodePastaRanges(rawScopes, names);
591595
return buildOriginalScopes(ranges);

front_end/core/sdk/SourceMapScopesInfo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ export class SourceMapScopesInfo {
2222
this.#generatedRanges = scopeInfo.ranges;
2323
}
2424

25-
addOriginalScopes(scopes: Array<OriginalScope|undefined>): void {
25+
addOriginalScopes(scopes: Array<OriginalScope|null>): void {
2626
for (const scope of scopes) {
27-
this.#originalScopes.push(scope ?? null);
27+
this.#originalScopes.push(scope);
2828
}
2929
}
3030

0 commit comments

Comments
 (0)