forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwasmXScopes.js
More file actions
186 lines (168 loc) · 4.4 KB
/
Copy pathwasmXScopes.js
File metadata and controls
186 lines (168 loc) · 4.4 KB
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
// @flow
/* eslint camelcase: 0*/
import type { Location, SourceId } from "debugger-html";
const { getSourceMap } = require("./sourceMapRequests");
const { generatedToOriginalId } = require("./index");
const xScopes = new Map();
type XScopeItem = any;
type XScopeItemsIndex = Map<string, XScopeItem>;
function indexLinkingNames(items: XScopeItem[]): XScopeItemsIndex {
const result = new Map();
let queue = [...items];
while (queue.length > 0) {
const item = queue.shift();
if ("linkage_name" in item) {
result.set(item.linkage_name, item);
}
if ("children" in item) {
queue = [...queue, ...item.children];
}
}
return result;
}
type XScopeData = {
code_section_offset: number,
debug_info: Array<XScopeItem>,
idIndex: XScopeItemsIndex,
sources: Array<string>
};
async function getXScopes(sourceId: SourceId): Promise<?XScopeData> {
if (xScopes.has(sourceId)) {
return xScopes.get(sourceId);
}
const map = await getSourceMap(sourceId);
if (!map || !map.xScopes) {
xScopes.set(sourceId, null);
return null;
}
const { code_section_offset, debug_info } = map.xScopes;
const xScope = {
code_section_offset,
debug_info,
idIndex: indexLinkingNames(debug_info),
sources: map.sources
};
xScopes.set(sourceId, xScope);
return xScope;
}
function isInRange(item: XScopeItem, pc: number): boolean {
if ("ranges" in item) {
return item.ranges.some(r => r[0] <= pc && pc < r[1]);
}
if ("high_pc" in item) {
return item.low_pc <= pc && pc < item.high_pc;
}
return false;
}
type FoundScope = {
id: string,
name?: string,
file?: number,
line?: number
};
function filterScopes(
items: XScopeItem[],
pc: number,
lastItem: ?FoundScope,
index: XScopeItemsIndex
): FoundScope[] {
if (!items) {
return [];
}
return items.reduce((result, item) => {
switch (item.tag) {
case "compile_unit":
if (isInRange(item, pc)) {
result = [
...result,
...filterScopes(item.children, pc, lastItem, index)
];
}
break;
case "namespace":
case "structure_type":
case "union_type":
result = [
...result,
...filterScopes(item.children, pc, lastItem, index)
];
break;
case "subprogram":
if (isInRange(item, pc)) {
const s: FoundScope = {
id: item.linkage_name,
name: item.name
};
result = [...result, s, ...filterScopes(item.children, pc, s, index)];
}
break;
case "inlined_subroutine":
if (isInRange(item, pc)) {
const linkedItem = index.get(item.abstract_origin);
const s: FoundScope = {
id: item.abstract_origin,
name: linkedItem ? linkedItem.name : void 0
};
if (lastItem) {
lastItem.file = item.call_file;
lastItem.line = item.call_line;
}
result = [...result, s, ...filterScopes(item.children, pc, s, index)];
}
break;
}
return result;
}, []);
}
class XScope {
xScope: XScopeData;
constructor(xScopeData: XScopeData) {
this.xScope = xScopeData;
}
search(
generatedLocation: Location
): Array<{
displayName: string,
location?: Location
}> {
const { code_section_offset, debug_info, sources, idIndex } = this.xScope;
const pc = generatedLocation.line - (code_section_offset || 0);
const scopes = filterScopes(debug_info, pc, null, idIndex);
scopes.reverse();
return scopes.map(i => {
if (!("file" in i)) {
return {
displayName: i.name || ""
};
}
const sourceId = generatedToOriginalId(
generatedLocation.sourceId,
sources[i.file || 0]
);
return {
displayName: i.name || "",
location: {
line: i.line || 0,
sourceId
}
};
});
}
}
async function getWasmXScopes(sourceId: SourceId): Promise<?XScope> {
const xScopeData = await getXScopes(sourceId);
if (!xScopeData) {
return null;
}
return new XScope(xScopeData);
}
function clearWasmXScopes() {
xScopes.clear();
}
module.exports = {
getWasmXScopes,
clearWasmXScopes
};