-
Notifications
You must be signed in to change notification settings - Fork 10
/
memory-webview-view.tsx
210 lines (189 loc) · 9.24 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
/********************************************************************************
* 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,
} from '../common/messaging';
import type { DebugProtocol } from '@vscode/debugprotocol';
import { Decoration, Memory, 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 { hoverService, HoverService } from './hovers/hover-service';
import { AddressHover } from './hovers/address-hover';
import { DataHover } from './hovers/data-hover';
import { VariableHover } from './hovers/variable-hover';
export interface MemoryAppState extends MemoryState, MemoryDisplayConfiguration {
title: string;
decorations: Decoration[];
hoverService: HoverService;
columns: ColumnStatus[];
isFrozen: boolean;
}
const MEMORY_DISPLAY_CONFIGURATION_DEFAULTS: MemoryDisplayConfiguration = {
bytesPerWord: 1,
wordsPerGroup: 1,
groupsPerRow: 4,
scrollingBehavior: 'Paginate',
addressRadix: 16,
showRadixPrefix: true,
};
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);
hoverService.register(new AddressHover());
hoverService.register(new DataHover());
hoverService.register(new VariableHover());
this.state = {
title: 'Memory',
memory: undefined,
memoryReference: '',
offset: 0,
count: 256,
decorations: [],
hoverService: hoverService,
columns: columnContributionService.getColumns(),
isMemoryFetching: false,
isFrozen: false,
...MEMORY_DISPLAY_CONFIGURATION_DEFAULTS
};
}
public componentDidMount(): void {
messenger.onRequest(setOptionsType, options => this.setOptions(options));
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(): void {
hoverService.setMemoryState(this.state);
}
public render(): React.ReactNode {
return <PrimeReactProvider>
<MemoryWidget
memory={this.state.memory}
decorations={this.state.decorations}
hoverService={this.state.hoverService}
columns={this.state.columns}
memoryReference={this.state.memoryReference}
offset={this.state.offset ?? 0}
count={this.state.count}
title={this.state.title}
updateMemoryArguments={this.updateMemoryState}
updateMemoryDisplayConfiguration={this.updateMemoryDisplayConfiguration}
resetMemoryDisplayConfiguration={this.resetMemoryDisplayConfiguration}
updateTitle={this.updateTitle}
refreshMemory={this.refreshMemory}
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}
addressRadix={this.state.addressRadix}
showRadixPrefix={this.state.showRadixPrefix}
/>
</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?: Partial<DebugProtocol.ReadMemoryArguments>): Promise<void> {
messenger.sendRequest(logMessageType, HOST_EXTENSION, `Setting options: ${JSON.stringify(options)}`);
this.setState(prevState => ({ ...prevState, ...options }));
return this.fetchMemory(options);
}
protected refreshMemory = () => { this.fetchMemory(); };
protected fetchMemory = async (partialOptions?: Partial<DebugProtocol.ReadMemoryArguments>): Promise<void> => this.doFetchMemory(partialOptions);
protected async doFetchMemory(partialOptions?: Partial<DebugProtocol.ReadMemoryArguments>): Promise<void> {
if (this.state.isFrozen) {
return;
}
this.setState(prev => ({ ...prev, isMemoryFetching: true }));
const completeOptions = {
memoryReference: partialOptions?.memoryReference || this.state.memoryReference,
offset: partialOptions?.offset ?? this.state.offset,
count: partialOptions?.count ?? this.state.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)
));
this.setState({
decorations: decorationService.decorations,
memory: this.convertMemory(response),
memoryReference: completeOptions.memoryReference,
offset: completeOptions.offset,
count: completeOptions.count,
isMemoryFetching: false
});
messenger.sendRequest(setOptionsType, HOST_EXTENSION, completeOptions);
} finally {
this.setState(prev => ({ ...prev, isMemoryFetching: false }));
}
}
protected convertMemory(result: DebugProtocol.ReadMemoryResponse['body']): Memory {
if (!result?.data) { throw new Error('No memory provided!'); }
const address = BigInt(result.address);
const bytes = Uint8Array.from(Buffer.from(result.data, 'base64'));
return { bytes, address };
}
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 }));
}
}
const container = document.getElementById('root') as Element;
createRoot(container).render(<App />);