/
memory-webview-view.tsx
302 lines (272 loc) · 13.3 KB
/
memory-webview-view.tsx
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
/********************************************************************************
* Copyright (C) 2022 Ericsson, Arm and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import React from 'react';
import { createRoot } from 'react-dom/client';
import { HOST_EXTENSION } from 'vscode-messenger-common';
import {
readyType,
logMessageType,
setOptionsType,
readMemoryType,
setTitleType,
setMemoryViewSettingsType,
resetMemoryViewSettingsType,
storeMemoryType,
applyMemoryType,
MemoryOptions,
memoryWrittenType,
ReadMemoryArguments,
sessionContextChangedType,
SessionContext,
} from '../common/messaging';
import { Decoration, MemoryDisplayConfiguration, MemoryState } from './utils/view-types';
import { MemoryWidget } from './components/memory-widget';
import { messenger } from './view-messenger';
import { ColumnStatus, columnContributionService } from './columns/column-contribution-service';
import { decorationService } from './decorations/decoration-service';
import { variableDecorator } from './variables/variable-decorations';
import { AsciiColumn } from './columns/ascii-column';
import { AddressColumn } from './columns/address-column';
import { DataColumn } from './columns/data-column';
import { PrimeReactProvider } from 'primereact/api';
import 'primeflex/primeflex.css';
import { getAddressLength, getAddressString, BigIntMemoryRange, doOverlap, WrittenMemory } from '../common/memory-range';
import { Memory, createMemoryFromRead } from '../common/memory';
export interface MemoryAppState extends MemoryState, MemoryDisplayConfiguration {
title: string;
sessionContext: SessionContext;
effectiveAddressLength: number;
decorations: Decoration[];
columns: ColumnStatus[];
isFrozen: boolean;
}
const DEFAULT_SESSION_CONTEXT: SessionContext = {
canRead: false,
canWrite: false
};
const MEMORY_DISPLAY_CONFIGURATION_DEFAULTS: MemoryDisplayConfiguration = {
bytesPerWord: 1,
wordsPerGroup: 1,
groupsPerRow: 4,
scrollingBehavior: 'Paginate',
addressPadding: 'Min',
addressRadix: 16,
showRadixPrefix: true,
};
const DEFAULT_READ_ARGUMENTS: Required<ReadMemoryArguments> = {
memoryReference: '',
offset: 0,
count: 256,
};
class App extends React.Component<{}, MemoryAppState> {
public constructor(props: {}) {
super(props);
columnContributionService.register(new AddressColumn(), false);
columnContributionService.register(new DataColumn(), false);
columnContributionService.register(variableDecorator);
columnContributionService.register(new AsciiColumn());
decorationService.register(variableDecorator);
this.state = {
title: 'Memory',
sessionContext: DEFAULT_SESSION_CONTEXT,
memory: undefined,
effectiveAddressLength: 0,
configuredReadArguments: DEFAULT_READ_ARGUMENTS,
activeReadArguments: DEFAULT_READ_ARGUMENTS,
decorations: [],
columns: columnContributionService.getColumns(),
isMemoryFetching: false,
isFrozen: false,
...MEMORY_DISPLAY_CONFIGURATION_DEFAULTS
};
}
public componentDidMount(): void {
messenger.onRequest(setOptionsType, options => this.setOptions(options));
messenger.onNotification(memoryWrittenType, writtenMemory => this.memoryWritten(writtenMemory));
messenger.onNotification(sessionContextChangedType, sessionContext => this.sessionContextChanged(sessionContext));
messenger.onNotification(setMemoryViewSettingsType, config => {
for (const column of columnContributionService.getColumns()) {
const id = column.contribution.id;
const configurable = column.configurable;
this.toggleColumn(id, !configurable || !!config.visibleColumns?.includes(id));
}
this.setState(prevState => ({ ...prevState, ...config, title: config.title ?? prevState.title, }));
});
messenger.sendNotification(readyType, HOST_EXTENSION, undefined);
}
public componentDidUpdate(_: {}, prevState: MemoryAppState): void {
const addressPaddingNeedsUpdate =
(this.state.addressPadding === 'Min' && this.state.memory !== prevState.memory)
|| this.state.addressPadding !== prevState.addressPadding;
if (addressPaddingNeedsUpdate) {
const effectiveAddressLength = this.getEffectiveAddressLength(this.state.memory);
if (this.state.effectiveAddressLength !== effectiveAddressLength) {
this.setState({ effectiveAddressLength });
}
}
}
protected memoryWritten(writtenMemory: WrittenMemory): void {
if (!this.state.memory) {
return;
}
if (this.state.activeReadArguments.memoryReference === writtenMemory.memoryReference) {
// catch simple case
this.updateMemoryState();
return;
}
try {
// If we are dealing with numeric addresses (and not expressions) then we can determine the overlap.
// Note that we use big int arithmetic here to determine the overlap for (start address + length) vs (memory state address + length), i.e.,
// we do not actually determine the end address may need to consider the size of a word in bytes
const written: BigIntMemoryRange = {
startAddress: BigInt(writtenMemory.memoryReference),
endAddress: BigInt(writtenMemory.memoryReference) + BigInt(writtenMemory.count ?? 0)
};
const shown: BigIntMemoryRange = {
startAddress: this.state.memory.address,
endAddress: this.state.memory.address + BigInt(this.state.memory.bytes.length)
};
if (doOverlap(written, shown)) {
this.updateMemoryState();
}
} catch (error) {
// ignore and fall through
}
// we could try to convert any expression we may have to an address by sending an evaluation request to the DA
// but for now we just go with a pessimistic approach: if we are unsure, we refresh the memory
this.updateMemoryState();
}
protected sessionContextChanged(sessionContext: SessionContext): void {
this.setState({ sessionContext });
}
public render(): React.ReactNode {
return <PrimeReactProvider>
<MemoryWidget
sessionContext={this.state.sessionContext}
configuredReadArguments={this.state.configuredReadArguments}
activeReadArguments={this.state.activeReadArguments}
memory={this.state.memory}
decorations={this.state.decorations}
columns={this.state.columns}
title={this.state.title}
effectiveAddressLength={this.state.effectiveAddressLength}
updateMemoryState={this.updateMemoryState}
updateMemoryDisplayConfiguration={this.updateMemoryDisplayConfiguration}
resetMemoryDisplayConfiguration={this.resetMemoryDisplayConfiguration}
updateTitle={this.updateTitle}
toggleColumn={this.toggleColumn}
toggleFrozen={this.toggleFrozen}
isFrozen={this.state.isFrozen}
fetchMemory={this.fetchMemory}
isMemoryFetching={this.state.isMemoryFetching}
bytesPerWord={this.state.bytesPerWord}
groupsPerRow={this.state.groupsPerRow}
wordsPerGroup={this.state.wordsPerGroup}
scrollingBehavior={this.state.scrollingBehavior}
addressPadding={this.state.addressPadding}
addressRadix={this.state.addressRadix}
showRadixPrefix={this.state.showRadixPrefix}
storeMemory={this.storeMemory}
applyMemory={this.applyMemory}
/>
</PrimeReactProvider>;
}
protected updateMemoryState = (newState?: Partial<MemoryState>) => this.setState(prevState => ({ ...prevState, ...newState }));
protected updateMemoryDisplayConfiguration = (newState: Partial<MemoryDisplayConfiguration>) => this.setState(prevState => ({ ...prevState, ...newState }));
protected resetMemoryDisplayConfiguration = () => messenger.sendNotification(resetMemoryViewSettingsType, HOST_EXTENSION, undefined);
protected updateTitle = (title: string) => {
this.setState({ title });
messenger.sendNotification(setTitleType, HOST_EXTENSION, title);
};
protected async setOptions(options?: MemoryOptions): Promise<void> {
messenger.sendRequest(logMessageType, HOST_EXTENSION, `Setting options: ${JSON.stringify(options)}`);
if (this.state.configuredReadArguments.memoryReference === '') {
// Only update if we have no user configured read arguments
this.setState(prevState => ({ ...prevState, configuredReadArguments: { ...this.state.configuredReadArguments, ...options } }));
}
return this.fetchMemory(options);
}
protected fetchMemory = async (partialOptions?: MemoryOptions): Promise<void> => this.doFetchMemory(partialOptions);
protected async doFetchMemory(partialOptions?: MemoryOptions): Promise<void> {
if (this.state.isFrozen) {
return;
}
this.setState(prev => ({ ...prev, isMemoryFetching: true }));
const completeOptions = {
memoryReference: partialOptions?.memoryReference || this.state.activeReadArguments.memoryReference,
offset: partialOptions?.offset ?? this.state.activeReadArguments.offset,
count: partialOptions?.count ?? this.state.activeReadArguments.count
};
try {
const response = await messenger.sendRequest(readMemoryType, HOST_EXTENSION, completeOptions);
await Promise.all(Array.from(
new Set(columnContributionService.getUpdateExecutors().concat(decorationService.getUpdateExecutors())),
executor => executor.fetchData(completeOptions)
));
const memory = createMemoryFromRead(response, completeOptions);
this.setState(prev => ({
...prev,
decorations: decorationService.decorations,
memory,
activeReadArguments: completeOptions,
isMemoryFetching: false
}));
messenger.sendRequest(setOptionsType, HOST_EXTENSION, completeOptions);
} catch (ex) {
// Do not show old results if the current search provided no memory
this.setState(prev => ({
...prev,
memory: undefined,
activeReadArguments: completeOptions,
}));
if (ex instanceof Error) {
console.error(ex);
}
} finally {
this.setState(prev => ({ ...prev, isMemoryFetching: false }));
}
}
protected getEffectiveAddressLength(memory?: Memory): number {
const { addressRadix, addressPadding } = this.state;
return addressPadding === 'Min' ? this.getLastAddressLength(memory) : getAddressLength(addressPadding, addressRadix);
}
protected getLastAddressLength(memory?: Memory): number {
if (memory === undefined || this.state.groupsPerRow === 'Autofit') {
return 0;
}
const rowLength = this.state.bytesPerWord * this.state.wordsPerGroup * this.state.groupsPerRow;
const rows = Math.ceil(memory.bytes.length / rowLength);
const finalAddress = memory.address + BigInt(((rows - 1) * rowLength));
return getAddressString(finalAddress, this.state.addressRadix).length;
}
protected toggleColumn = (id: string, active: boolean): void => { this.doToggleColumn(id, active); };
protected async doToggleColumn(id: string, isVisible: boolean): Promise<void> {
const columns = isVisible ? await columnContributionService.show(id, this.state) : columnContributionService.hide(id);
this.setState(prevState => ({ ...prevState, columns }));
}
protected toggleFrozen = (): void => { this.doToggleFrozen(); };
protected doToggleFrozen(): void {
this.setState(prevState => ({ ...prevState, isFrozen: !prevState.isFrozen }));
}
protected storeMemory = async (): Promise<void> => {
await messenger.sendRequest(storeMemoryType, HOST_EXTENSION, { ...this.state.activeReadArguments });
};
protected applyMemory = async (): Promise<void> => {
await messenger.sendRequest(applyMemoryType, HOST_EXTENSION, undefined);
};
}
const container = document.getElementById('root') as Element;
createRoot(container).render(<App />);