@theia/memory-inspector 1.34.3 → 1.34.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/LICENSE +641 -641
  2. package/README.md +71 -71
  3. package/lib/browser/diff-widget/memory-diff-options-widget.d.ts +36 -36
  4. package/lib/browser/diff-widget/memory-diff-options-widget.js +129 -129
  5. package/lib/browser/diff-widget/memory-diff-select-widget.d.ts +50 -50
  6. package/lib/browser/diff-widget/memory-diff-select-widget.js +137 -137
  7. package/lib/browser/diff-widget/memory-diff-table-widget.d.ts +77 -77
  8. package/lib/browser/diff-widget/memory-diff-table-widget.js +314 -314
  9. package/lib/browser/diff-widget/memory-diff-widget-types.d.ts +41 -41
  10. package/lib/browser/diff-widget/memory-diff-widget-types.js +23 -23
  11. package/lib/browser/editable-widget/memory-editable-table-widget.d.ts +64 -64
  12. package/lib/browser/editable-widget/memory-editable-table-widget.js +329 -329
  13. package/lib/browser/memory-inspector-frontend-contribution.d.ts +46 -46
  14. package/lib/browser/memory-inspector-frontend-contribution.js +291 -291
  15. package/lib/browser/memory-inspector-frontend-module.d.ts +21 -21
  16. package/lib/browser/memory-inspector-frontend-module.js +87 -87
  17. package/lib/browser/memory-provider/cdt-gdb-memory-provider.d.ts +35 -35
  18. package/lib/browser/memory-provider/cdt-gdb-memory-provider.js +136 -136
  19. package/lib/browser/memory-provider/memory-provider-service.d.ts +36 -36
  20. package/lib/browser/memory-provider/memory-provider-service.js +101 -101
  21. package/lib/browser/memory-provider/memory-provider.d.ts +65 -65
  22. package/lib/browser/memory-provider/memory-provider.js +96 -96
  23. package/lib/browser/memory-provider/memory-provider.spec.d.ts +16 -16
  24. package/lib/browser/memory-provider/memory-provider.spec.js +23 -23
  25. package/lib/browser/memory-widget/memory-options-widget.d.ts +136 -136
  26. package/lib/browser/memory-widget/memory-options-widget.js +561 -561
  27. package/lib/browser/memory-widget/memory-table-widget.d.ts +145 -145
  28. package/lib/browser/memory-widget/memory-table-widget.js +484 -484
  29. package/lib/browser/memory-widget/memory-widget.d.ts +32 -32
  30. package/lib/browser/memory-widget/memory-widget.js +109 -109
  31. package/lib/browser/register-widget/register-filter-service.d.ts +42 -42
  32. package/lib/browser/register-widget/register-filter-service.js +82 -82
  33. package/lib/browser/register-widget/register-options-widget.d.ts +81 -81
  34. package/lib/browser/register-widget/register-options-widget.js +343 -343
  35. package/lib/browser/register-widget/register-table-widget.d.ts +77 -77
  36. package/lib/browser/register-widget/register-table-widget.js +236 -236
  37. package/lib/browser/register-widget/register-widget-types.d.ts +29 -29
  38. package/lib/browser/register-widget/register-widget-types.js +35 -35
  39. package/lib/browser/utils/memory-commands.d.ts +27 -27
  40. package/lib/browser/utils/memory-commands.js +67 -67
  41. package/lib/browser/utils/memory-hover-renderer.d.ts +33 -33
  42. package/lib/browser/utils/memory-hover-renderer.js +111 -111
  43. package/lib/browser/utils/memory-recents.d.ts +27 -27
  44. package/lib/browser/utils/memory-recents.js +52 -52
  45. package/lib/browser/utils/memory-widget-components.d.ts +58 -58
  46. package/lib/browser/utils/memory-widget-components.js +69 -69
  47. package/lib/browser/utils/memory-widget-manager.d.ts +50 -50
  48. package/lib/browser/utils/memory-widget-manager.js +182 -182
  49. package/lib/browser/utils/memory-widget-utils.d.ts +113 -113
  50. package/lib/browser/utils/memory-widget-utils.js +42 -42
  51. package/lib/browser/utils/memory-widget-variable-utils.d.ts +57 -57
  52. package/lib/browser/utils/memory-widget-variable-utils.js +143 -143
  53. package/lib/browser/utils/multi-select-bar.d.ts +31 -31
  54. package/lib/browser/utils/multi-select-bar.js +34 -34
  55. package/lib/browser/wrapper-widgets/memory-dock-panel.d.ts +24 -24
  56. package/lib/browser/wrapper-widgets/memory-dock-panel.js +48 -48
  57. package/lib/browser/wrapper-widgets/memory-dockpanel-placeholder-widget.d.ts +23 -23
  58. package/lib/browser/wrapper-widgets/memory-dockpanel-placeholder-widget.js +57 -57
  59. package/lib/browser/wrapper-widgets/memory-layout-widget.d.ts +50 -50
  60. package/lib/browser/wrapper-widgets/memory-layout-widget.js +188 -188
  61. package/lib/common/util.d.ts +21 -21
  62. package/lib/common/util.js +30 -30
  63. package/lib/common/utils.spec.d.ts +16 -16
  64. package/lib/common/utils.spec.js +45 -45
  65. package/package.json +4 -4
  66. package/src/browser/diff-widget/memory-diff-options-widget.tsx +152 -152
  67. package/src/browser/diff-widget/memory-diff-select-widget.tsx +163 -163
  68. package/src/browser/diff-widget/memory-diff-table-widget.tsx +366 -366
  69. package/src/browser/diff-widget/memory-diff-widget-types.ts +45 -45
  70. package/src/browser/editable-widget/memory-editable-table-widget.tsx +360 -360
  71. package/src/browser/memory-inspector-frontend-contribution.ts +301 -301
  72. package/src/browser/memory-inspector-frontend-module.ts +118 -118
  73. package/src/browser/memory-provider/cdt-gdb-memory-provider.ts +132 -132
  74. package/src/browser/memory-provider/memory-provider-service.ts +86 -86
  75. package/src/browser/memory-provider/memory-provider.spec.ts +23 -23
  76. package/src/browser/memory-provider/memory-provider.ts +119 -119
  77. package/src/browser/memory-widget/memory-options-widget.tsx +738 -738
  78. package/src/browser/memory-widget/memory-table-widget.tsx +621 -621
  79. package/src/browser/memory-widget/memory-widget.ts +110 -110
  80. package/src/browser/register-widget/register-filter-service.ts +77 -77
  81. package/src/browser/register-widget/register-options-widget.tsx +393 -393
  82. package/src/browser/register-widget/register-table-widget.tsx +277 -277
  83. package/src/browser/register-widget/register-widget-types.ts +45 -45
  84. package/src/browser/register-widget/register-widget.css +34 -34
  85. package/src/browser/style/index.css +750 -750
  86. package/src/browser/style/memory-lock.svg +21 -21
  87. package/src/browser/style/memory-view.svg +20 -20
  88. package/src/browser/style/register-lock.svg +29 -29
  89. package/src/browser/style/register-view.svg +28 -28
  90. package/src/browser/utils/memory-commands.ts +76 -76
  91. package/src/browser/utils/memory-hover-renderer.ts +113 -113
  92. package/src/browser/utils/memory-recents.ts +58 -58
  93. package/src/browser/utils/memory-widget-components.tsx +193 -193
  94. package/src/browser/utils/memory-widget-manager.ts +179 -179
  95. package/src/browser/utils/memory-widget-utils.tsx +132 -132
  96. package/src/browser/utils/memory-widget-variable-utils.ts +170 -170
  97. package/src/browser/utils/multi-select-bar.css +62 -62
  98. package/src/browser/utils/multi-select-bar.tsx +75 -75
  99. package/src/browser/wrapper-widgets/memory-dock-panel.ts +51 -51
  100. package/src/browser/wrapper-widgets/memory-dockpanel-placeholder-widget.tsx +38 -38
  101. package/src/browser/wrapper-widgets/memory-layout-widget.tsx +163 -163
  102. package/src/common/util.ts +28 -28
  103. package/src/common/utils.spec.ts +52 -52
@@ -1,621 +1,621 @@
1
- /********************************************************************************
2
- * Copyright (C) 2021 Ericsson and others.
3
- *
4
- * This program and the accompanying materials are made available under the
5
- * terms of the Eclipse Public License v. 2.0 which is available at
6
- * http://www.eclipse.org/legal/epl-2.0.
7
- *
8
- * This Source Code may also be made available under the following Secondary
9
- * Licenses when the conditions for such availability set forth in the Eclipse
10
- * Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- * with the GNU Classpath Exception which is available at
12
- * https://www.gnu.org/software/classpath/license.html.
13
- *
14
- * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15
- ********************************************************************************/
16
-
17
- import { ContextMenuRenderer, ReactWidget, Widget } from '@theia/core/lib/browser';
18
- import { ThemeService } from '@theia/core/lib/browser/theming';
19
- import { Deferred } from '@theia/core/lib/common/promise-util';
20
- import { ThemeChangeEvent } from '@theia/core/lib/common/theme';
21
- import { inject, injectable, postConstruct } from '@theia/core/shared/inversify';
22
- import * as React from '@theia/core/shared/react';
23
- import { hexStrToUnsignedLong } from '../../common/util';
24
- import { MemoryProviderService } from '../memory-provider/memory-provider-service';
25
- import { EasilyMappedObject, MemoryHoverRendererService } from '../utils/memory-hover-renderer';
26
- import { MWMoreMemorySelect } from '../utils/memory-widget-components';
27
- import {
28
- Constants, Interfaces, Utils
29
- } from '../utils/memory-widget-utils';
30
- import { VariableDecoration, VariableFinder } from '../utils/memory-widget-variable-utils';
31
- import { MemoryOptionsWidget } from './memory-options-widget';
32
- import debounce = require('@theia/core/shared/lodash.debounce');
33
-
34
- /* eslint-disable @typescript-eslint/no-explicit-any */
35
- export namespace MemoryTable {
36
- export interface WrapperHandlers {
37
- onKeyDown?: React.KeyboardEventHandler;
38
- onClick?: React.MouseEventHandler;
39
- onContextMenu?: React.MouseEventHandler;
40
- onMouseMove?: React.MouseEventHandler;
41
- onFocus?(e: React.FocusEvent<HTMLDivElement>): any;
42
- onBlur?(e: React.FocusEvent<HTMLDivElement>): any;
43
- }
44
- /* eslint-enable @typescript-eslint/no-explicit-any */
45
-
46
- export interface StylableNodeAttributes {
47
- className?: string;
48
- style?: React.CSSProperties;
49
- variable?: VariableDecoration;
50
- title?: string;
51
- isHighlighted?: boolean;
52
- }
53
-
54
- export interface GroupData {
55
- node: React.ReactNode;
56
- ascii: string; index: number;
57
- variables: VariableDecoration[];
58
- isHighlighted?: boolean;
59
- }
60
- export interface ByteData {
61
- node: React.ReactNode;
62
- ascii: string; index: number;
63
- variables: VariableDecoration[];
64
- isHighlighted?: boolean;
65
- }
66
- export interface ItemData {
67
- node: React.ReactNode;
68
- content: string;
69
- variable?: VariableDecoration;
70
- index: number;
71
- isHighlighted?: boolean;
72
- }
73
-
74
- export interface RowOptions {
75
- address: string;
76
- groups: React.ReactNode;
77
- ascii?: string;
78
- variables?: VariableDecoration[];
79
- doShowDivider?: boolean;
80
- index: number;
81
- isHighlighted?: boolean;
82
- }
83
-
84
- export const ROW_CLASS = 't-mv-view-row';
85
- export const ROW_DIVIDER_CLASS = 't-mv-view-row-highlight';
86
- export const ADDRESS_DATA_CLASS = 't-mv-view-address';
87
- export const MEMORY_DATA_CLASS = 't-mv-view-data';
88
- export const EXTRA_COLUMN_DATA_CLASS = 't-mv-view-code';
89
- export const GROUP_SPAN_CLASS = 'byte-group';
90
- export const BYTE_SPAN_CLASS = 'single-byte';
91
- export const EIGHT_BIT_SPAN_CLASS = 'eight-bits';
92
- export const HEADER_LABEL_CONTAINER_CLASS = 't-mv-header-label-container';
93
- export const HEADER_LABEL_CLASS = 't-mv-header-label';
94
- export const VARIABLE_LABEL_CLASS = 't-mv-variable-label';
95
- export const HEADER_ROW_CLASS = 't-mv-header';
96
- }
97
-
98
- @injectable()
99
- export class MemoryTableWidget extends ReactWidget {
100
- static CONTEXT_MENU = ['memory.view.context.menu'];
101
- static ID = 'memory-table-widget';
102
-
103
- @inject(ThemeService) protected readonly themeService: ThemeService;
104
- @inject(MemoryOptionsWidget) readonly optionsWidget: MemoryOptionsWidget;
105
- @inject(MemoryProviderService) protected readonly memoryProvider: MemoryProviderService;
106
- @inject(MemoryHoverRendererService) protected readonly hoverRenderer: MemoryHoverRendererService;
107
- @inject(ContextMenuRenderer) protected readonly contextMenuRenderer: ContextMenuRenderer;
108
-
109
- protected previousBytes: Interfaces.LabeledUint8Array | undefined;
110
- protected memory: Interfaces.WidgetMemoryState;
111
- protected options: Interfaces.MemoryOptions;
112
- protected variableFinder: VariableFinder | undefined;
113
- protected deferredScrollContainer = new Deferred<HTMLDivElement>();
114
-
115
- @postConstruct()
116
- protected async init(): Promise<void> {
117
- this.id = MemoryTableWidget.ID;
118
- this.addClass(MemoryTableWidget.ID);
119
- this.scrollOptions = { ...this.scrollOptions, suppressScrollX: false };
120
- this.toDispose.push(this.optionsWidget.onOptionsChanged(optionId => this.handleOptionChange(optionId)));
121
- this.toDispose.push(this.optionsWidget.onMemoryChanged(e => this.handleMemoryChange(e)));
122
- this.toDispose.push(this.themeService.onDidColorThemeChange(e => this.handleThemeChange(e)));
123
-
124
- this.getStateAndUpdate();
125
- }
126
-
127
- protected handleOptionChange(_id?: string): Promise<void> {
128
- this.getStateAndUpdate();
129
- return Promise.resolve();
130
- }
131
-
132
- override update(): void {
133
- super.update();
134
- this.updateColumnWidths();
135
- }
136
-
137
- protected override onResize(msg: Widget.ResizeMessage): void {
138
- this.updateColumnWidths();
139
- super.onResize(msg);
140
- }
141
-
142
- protected updateColumnWidths = debounce(this.doUpdateColumnWidths.bind(this), Constants.DEBOUNCE_TIME);
143
-
144
- protected doUpdateColumnWidths(): void {
145
- setTimeout(() => {
146
- const firstTR = this.node.querySelector('tr');
147
- const header = this.node.querySelector(`.${MemoryTable.HEADER_ROW_CLASS}`) as HTMLDivElement;
148
- if (firstTR && header) {
149
- const allTDs = Array.from(firstTR.querySelectorAll('td'));
150
- const allSizes = allTDs.map(td => `minmax(max-content, ${td.clientWidth}px)`);
151
- header.style.gridTemplateColumns = allSizes.join(' ');
152
- }
153
- });
154
- }
155
-
156
- protected areSameRegion(a: Interfaces.MemoryReadResult, b: Interfaces.MemoryReadResult): boolean {
157
- return a.address.equals(b?.address) && a.bytes.length === b?.bytes.length;
158
- }
159
-
160
- protected handleMemoryChange(newMemory: Interfaces.MemoryReadResult): void {
161
- if (this.areSameRegion(this.memory, newMemory)) {
162
- this.previousBytes = this.memory.bytes;
163
- } else {
164
- this.previousBytes = undefined;
165
- }
166
- this.getStateAndUpdate();
167
- }
168
-
169
- protected handleThemeChange(_themeChange: ThemeChangeEvent): void {
170
- this.getStateAndUpdate();
171
- }
172
-
173
- protected getState(): void {
174
- this.options = this.optionsWidget.options;
175
- this.memory = this.optionsWidget.memory;
176
- const isHighContrast = this.themeService.getCurrentTheme().type === 'hc';
177
- this.variableFinder = this.optionsWidget.options.columnsDisplayed.variables.doRender
178
- ? new VariableFinder(this.memory.variables, isHighContrast)
179
- : undefined;
180
- }
181
-
182
- protected getStateAndUpdate(): void {
183
- this.getState();
184
- this.update();
185
- this.scrollIntoViewIfNecessary();
186
- }
187
-
188
- protected scrollIntoViewIfNecessary(): Promise<void> {
189
- return new Promise(resolve => setTimeout(() => {
190
- this.deferredScrollContainer.promise.then(scrollContainer => {
191
- const table = scrollContainer.querySelector('table');
192
- if (table && scrollContainer.scrollTop > table.clientHeight) {
193
- const valueToGetInWindow = table.clientHeight - this.node.clientHeight;
194
- const scrollHere = Math.max(valueToGetInWindow, 0);
195
- scrollContainer.scrollTo(scrollContainer.scrollLeft, scrollHere);
196
- }
197
- this.scrollBar?.update();
198
- resolve();
199
- });
200
- }));
201
- }
202
-
203
- protected getWrapperHandlers(): MemoryTable.WrapperHandlers {
204
- return { onMouseMove: this.handleTableMouseMove };
205
- }
206
-
207
- protected assignScrollContainerRef = (element: HTMLDivElement): void => {
208
- this.deferredScrollContainer.resolve(element);
209
- };
210
-
211
- override async getScrollContainer(): Promise<HTMLDivElement> {
212
- return this.deferredScrollContainer.promise;
213
- }
214
-
215
- render(): React.ReactNode {
216
- const rows = this.getTableRows();
217
- const { onClick, onContextMenu, onFocus, onBlur, onKeyDown, onMouseMove } = this.getWrapperHandlers();
218
- const headers: Interfaces.ColumnIDs[] = Object.entries(this.options.columnsDisplayed)
219
- .filter(([, { doRender }]) => doRender)
220
- .map(([id, { label }]) => ({ label, id }));
221
-
222
- return (
223
- <div
224
- className={this.getWrapperClass()}
225
- onClick={onClick}
226
- onContextMenu={onContextMenu}
227
- onFocus={onFocus}
228
- onBlur={onBlur}
229
- onKeyDown={onKeyDown}
230
- onMouseMove={onMouseMove}
231
- role='textbox'
232
- tabIndex={0}
233
- >
234
- <div
235
- className={this.getTableHeaderClass()}
236
- style={this.getTableHeaderStyle(headers.length)}
237
- >
238
- {this.getTableHeaders(headers)}
239
- </div>
240
- <div
241
- className='t-mv-view-container'
242
- style={{ position: 'relative' }}
243
- ref={this.assignScrollContainerRef}
244
- >
245
- {this.getBeforeTableContent()}
246
- <table className='t-mv-view'>
247
- <tbody>
248
- {rows}
249
- </tbody>
250
- </table>
251
- {this.getAfterTableContent()}
252
- </div>
253
- {this.getTableFooter()}
254
- </div>
255
- );
256
- }
257
-
258
- protected getWrapperClass(): string {
259
- return `t-mv-memory-container${this.options.isFrozen ? ' frozen' : ''}`;
260
- }
261
-
262
- protected getTableHeaderClass(): string {
263
- return MemoryTable.HEADER_ROW_CLASS + ' no-select';
264
- }
265
-
266
- protected getTableHeaderStyle(numLabels: number): React.CSSProperties {
267
- const safePercentage = Math.floor(100 / numLabels);
268
- const gridTemplateColumns = ` ${safePercentage}% `.repeat(numLabels);
269
- return { gridTemplateColumns };
270
- }
271
-
272
- protected getTableHeaders(labels: Interfaces.ColumnIDs[]): React.ReactNode {
273
- return labels.map(label => this.getTableHeader(label));
274
- }
275
-
276
- protected getTableHeader({ label, id }: Interfaces.ColumnIDs): React.ReactNode {
277
- return (
278
- <div key={id} className={MemoryTable.HEADER_LABEL_CONTAINER_CLASS}>
279
- <span className='t-mv-header-label'>{label}</span>
280
- </div>
281
- );
282
- }
283
-
284
- protected getBeforeTableContent(): React.ReactNode {
285
- return (
286
- !!this.memory.bytes.length && (<MWMoreMemorySelect
287
- options={[128, 256, 512]}
288
- direction='above'
289
- handler={this.loadMoreMemory}
290
- />)
291
- );
292
- }
293
-
294
- protected getAfterTableContent(): React.ReactNode {
295
- return (
296
- !!this.memory.bytes.length && (<MWMoreMemorySelect
297
- options={[128, 256, 512]}
298
- direction='below'
299
- handler={this.loadMoreMemory}
300
- />)
301
- );
302
- }
303
-
304
- protected loadMoreMemory = async (options: Interfaces.MoreMemoryOptions): Promise<void> => {
305
- const { direction, numBytes } = options;
306
- const { address, offset, length } = this.optionsWidget.options;
307
- let newOffset = 0;
308
- const newLength = length + numBytes;
309
- if (direction === 'above') {
310
- newOffset = offset - numBytes;
311
- }
312
- await this.optionsWidget.setAddressAndGo(`${address}`, newOffset, newLength, direction);
313
- };
314
-
315
- protected getTableFooter(): React.ReactNode {
316
- return undefined;
317
- }
318
-
319
- protected getTableRows(): React.ReactNode {
320
- return [...this.renderRows()];
321
- }
322
-
323
- protected *renderRows(iteratee: Interfaces.LabeledUint8Array = this.memory.bytes): IterableIterator<React.ReactNode> {
324
- const bytesPerRow = this.options.bytesPerGroup * this.options.groupsPerRow;
325
- let rowsYielded = 0;
326
- let groups: React.ReactNode[] = [];
327
- let ascii = '';
328
- let variables: VariableDecoration[] = [];
329
- let isRowHighlighted = false;
330
- for (const { node, index, ascii: groupAscii, variables: groupVariables, isHighlighted = false } of this.renderGroups(iteratee)) {
331
- groups.push(node);
332
- ascii += groupAscii;
333
- variables.push(...groupVariables);
334
- isRowHighlighted = isRowHighlighted || isHighlighted;
335
- if (groups.length === this.options.groupsPerRow || index === iteratee.length - 1) {
336
- const rowAddress = this.memory.address.add(bytesPerRow * rowsYielded);
337
- const options: MemoryTable.RowOptions = {
338
- address: `0x${rowAddress.toString(16)}`,
339
- doShowDivider: (rowsYielded % 4) === 3,
340
- isHighlighted: isRowHighlighted,
341
- ascii,
342
- groups,
343
- variables,
344
- index,
345
- };
346
- yield this.renderRow(options);
347
- ascii = '';
348
- variables = [];
349
- groups = [];
350
- rowsYielded += 1;
351
- isRowHighlighted = false;
352
- }
353
- }
354
- }
355
-
356
- protected renderRow(
357
- options: MemoryTable.RowOptions,
358
- getRowAttributes: Interfaces.RowDecorator = this.getRowAttributes.bind(this),
359
- ): React.ReactNode {
360
- const { address, groups } = options;
361
- const { className, style, title } = getRowAttributes(options);
362
- return (
363
- <tr
364
- // Add a marker to help visual navigation when scrolling
365
- className={className}
366
- style={style}
367
- title={title}
368
- key={address}
369
- >
370
- <td className={MemoryTable.ADDRESS_DATA_CLASS}>{address}</td>
371
- <td className={MemoryTable.MEMORY_DATA_CLASS}>{groups}</td>
372
- {this.getExtraColumn(options)}
373
- </tr>
374
- );
375
- }
376
-
377
- protected getRowAttributes(options: Partial<MemoryTable.RowOptions>): Partial<Interfaces.StylableNodeAttributes> {
378
- let className = MemoryTable.ROW_CLASS;
379
- if (options.doShowDivider) {
380
- className += ` ${MemoryTable.ROW_DIVIDER_CLASS}`;
381
- }
382
- return { className };
383
- }
384
-
385
- protected getExtraColumn(options: Pick<MemoryTable.RowOptions, 'ascii' | 'variables'>): React.ReactNode {
386
- const { variables } = options;
387
- const additionalColumns = [];
388
- if (this.options.columnsDisplayed.variables.doRender) {
389
- additionalColumns.push(
390
- <td className={MemoryTable.EXTRA_COLUMN_DATA_CLASS} key='variables'>
391
- {!!variables?.length && (
392
- <span className='variable-container'>
393
- {variables.map(({ name, color }) => (
394
- <span
395
- key={name}
396
- className={MemoryTable.VARIABLE_LABEL_CLASS}
397
- style={{ color }}
398
- >
399
- {name}
400
- </span>
401
- ))}
402
- </span>
403
- )}
404
- </td>,
405
- );
406
- }
407
- if (this.options.columnsDisplayed.ascii.doRender) {
408
- const asciiColumn = this.options.columnsDisplayed.ascii.doRender && <td className={MemoryTable.EXTRA_COLUMN_DATA_CLASS} key='ascii'>{options.ascii}</td>;
409
- additionalColumns.push(asciiColumn);
410
- }
411
- return additionalColumns;
412
- }
413
-
414
- protected *renderGroups(iteratee: Interfaces.LabeledUint8Array = this.memory.bytes): IterableIterator<MemoryTable.GroupData> {
415
- let bytesInGroup: React.ReactNode[] = [];
416
- let ascii = '';
417
- let variables: VariableDecoration[] = [];
418
- let isGroupHighlighted = false;
419
- for (const { node, index, ascii: byteAscii, variables: byteVariables, isHighlighted = false } of this.renderBytes(iteratee)) {
420
- this.buildGroupByEndianness(bytesInGroup, node);
421
- ascii += byteAscii;
422
- variables.push(...byteVariables);
423
- isGroupHighlighted = isGroupHighlighted || isHighlighted;
424
- if (bytesInGroup.length === this.options.bytesPerGroup || index === iteratee.length - 1) {
425
- const itemID = this.memory.address.add(index);
426
- yield {
427
- node: <span className='byte-group' key={itemID.toString(16)}>{bytesInGroup}</span>,
428
- ascii,
429
- index,
430
- variables,
431
- isHighlighted: isGroupHighlighted,
432
- };
433
- bytesInGroup = [];
434
- ascii = '';
435
- variables = [];
436
- isGroupHighlighted = false;
437
- }
438
- }
439
- }
440
-
441
- protected buildGroupByEndianness(oldBytes: React.ReactNode[], newByte: React.ReactNode): void {
442
- if (this.options.endianness === Interfaces.Endianness.Big) {
443
- oldBytes.push(newByte);
444
- } else {
445
- oldBytes.unshift(newByte);
446
- }
447
- }
448
-
449
- protected *renderBytes(iteratee: Interfaces.LabeledUint8Array = this.memory.bytes): IterableIterator<MemoryTable.ByteData> {
450
- const itemsPerByte = this.options.byteSize / 8;
451
- let currentByte = 0;
452
- let chunksInByte: React.ReactNode[] = [];
453
- let variables: VariableDecoration[] = [];
454
- let isByteHighlighted = false;
455
- for (const { node, content, index, variable, isHighlighted = false } of this.renderArrayItems(iteratee)) {
456
- chunksInByte.push(node);
457
- const numericalValue = parseInt(content, 16);
458
- currentByte = (currentByte << 8) + numericalValue;
459
- isByteHighlighted = isByteHighlighted || isHighlighted;
460
- if (variable?.firstAppearance) {
461
- variables.push(variable);
462
- }
463
- if (chunksInByte.length === itemsPerByte || index === iteratee.length - 1) {
464
- const itemID = this.memory.address.add(index);
465
- const ascii = this.getASCIIForSingleByte(currentByte);
466
- yield {
467
- node: <span className='single-byte' key={itemID.toString(16)}>{chunksInByte}</span>,
468
- ascii,
469
- index,
470
- variables,
471
- isHighlighted: isByteHighlighted,
472
- };
473
- currentByte = 0;
474
- chunksInByte = [];
475
- variables = [];
476
- isByteHighlighted = false;
477
- }
478
- }
479
- }
480
-
481
- protected getASCIIForSingleByte(byte: number | undefined): string {
482
- return typeof byte === 'undefined'
483
- ? ' ' : Utils.isPrintableAsAscii(byte) ? String.fromCharCode(byte) : '.';
484
- }
485
-
486
- protected *renderArrayItems(
487
- iteratee: Interfaces.LabeledUint8Array = this.memory.bytes,
488
- getBitAttributes: Interfaces.BitDecorator = this.getBitAttributes.bind(this),
489
- ): IterableIterator<MemoryTable.ItemData> {
490
- const { address } = this.memory;
491
-
492
- for (let i = 0; i < iteratee.length; i += 1) {
493
- const itemID = address.add(i).toString(16);
494
- const { content = '', className, style, variable, title, isHighlighted } = getBitAttributes(i, iteratee);
495
- const node = (
496
- <span
497
- style={style}
498
- key={itemID}
499
- className={className}
500
- data-id={itemID}
501
- title={title}
502
- >
503
- {content}
504
- </span>
505
- );
506
- yield {
507
- node,
508
- content,
509
- index: i,
510
- variable,
511
- isHighlighted,
512
- };
513
- }
514
- }
515
-
516
- protected getBitAttributes(arrayOffset: number, iteratee: Interfaces.LabeledUint8Array): Partial<Interfaces.FullNodeAttributes> {
517
- const itemAddress = this.memory.address.add(arrayOffset * 8 / this.options.byteSize);
518
- const classNames = [MemoryTable.EIGHT_BIT_SPAN_CLASS];
519
- const isChanged = this.previousBytes && iteratee[arrayOffset] !== this.previousBytes[arrayOffset];
520
- const variable = this.variableFinder?.getVariableForAddress(itemAddress);
521
- if (!this.options.isFrozen) {
522
- if (isChanged) {
523
- classNames.push('changed');
524
- }
525
- }
526
- return {
527
- className: classNames.join(' '),
528
- variable,
529
- style: { color: variable?.color },
530
- content: iteratee[arrayOffset].toString(16).padStart(2, '0')
531
- };
532
- }
533
-
534
- protected handleTableMouseMove = (e: React.MouseEvent): void => {
535
- const { target } = e; // react events can't be put into the debouncer
536
- this.debounceHandleMouseTableMove(target);
537
- };
538
-
539
- protected debounceHandleMouseTableMove = debounce(this.doHandleTableMouseMove.bind(this), Constants.DEBOUNCE_TIME, { trailing: true });
540
-
541
- protected doHandleTableMouseMove(targetSpan: React.MouseEvent['target']): void {
542
- const target = targetSpan instanceof HTMLElement && targetSpan;
543
- if (target) {
544
- const { x, y } = target.getBoundingClientRect();
545
- const anchor = { x: Math.round(x), y: Math.round(y + target.clientHeight) };
546
- if (target.classList.contains(MemoryTable.EIGHT_BIT_SPAN_CLASS)) {
547
- const properties = this.getHoverForChunk(target);
548
- this.hoverRenderer.render(this.node, anchor, properties);
549
- } else if (target.classList.contains(MemoryTable.VARIABLE_LABEL_CLASS)) {
550
- const properties = this.getHoverForVariable(target);
551
- this.hoverRenderer.render(this.node, anchor, properties);
552
- } else {
553
- this.hoverRenderer.hide();
554
- }
555
- } else {
556
- this.hoverRenderer.hide();
557
- }
558
- }
559
-
560
- protected getHoverForChunk(span: HTMLElement): EasilyMappedObject | undefined {
561
- if (span.classList.contains(MemoryTable.EIGHT_BIT_SPAN_CLASS)) {
562
- const parentByteContainer = span.parentElement;
563
- if (parentByteContainer?.textContent) {
564
- const hex = parentByteContainer.textContent ?? '';
565
- const decimal = parseInt(hex, 16);
566
- const binary = this.getPaddedBinary(decimal);
567
- const UTF8 = String.fromCodePoint(decimal);
568
- return { hex, binary, decimal, UTF8 };
569
- }
570
- }
571
- return undefined;
572
- }
573
-
574
- protected getPaddedBinary(decimal: number): string {
575
- const paddedBinary = decimal.toString(2).padStart(this.options.byteSize, '0');
576
- let paddedAndSpacedBinary = '';
577
- for (let i = 8; i <= paddedBinary.length; i += 8) {
578
- paddedAndSpacedBinary += ` ${paddedBinary.slice(i - 8, i)}`;
579
- }
580
- return paddedAndSpacedBinary.trim();
581
- }
582
-
583
- protected getHoverForVariable(span: HTMLElement): EasilyMappedObject | undefined {
584
- const variable = this.variableFinder?.searchForVariable(span.textContent ?? '');
585
- if (variable?.type) {
586
- return { type: variable.type };
587
- }
588
- return undefined;
589
- }
590
-
591
- protected handleTableRightClick = (e: React.MouseEvent): void => this.doHandleTableRightClick(e);
592
-
593
- protected doHandleTableRightClick(event: React.MouseEvent): void {
594
- event.preventDefault();
595
- const target = event.target as HTMLElement;
596
- if (target.classList?.contains('eight-bits')) {
597
- const { right, top } = target.getBoundingClientRect();
598
- this.update();
599
- event.stopPropagation();
600
- this.contextMenuRenderer.render({
601
- menuPath: MemoryTableWidget.CONTEXT_MENU,
602
- anchor: { x: right, y: top },
603
- args: this.getContextMenuArgs(event),
604
- });
605
- }
606
- }
607
-
608
- protected getContextMenuArgs(event: React.MouseEvent): unknown[] {
609
- const args: unknown[] = [this];
610
- const id = (event.target as HTMLElement).getAttribute('data-id');
611
- if (id) {
612
- const location = hexStrToUnsignedLong(id);
613
- args.push(location);
614
- const offset = this.memory.address.multiply(-1).add(location);
615
- const cellAddress = this.memory.address.add(offset.multiply(8 / this.options.byteSize));
616
- const variableAtLocation = this.variableFinder?.searchForVariable(cellAddress);
617
- args.push(variableAtLocation);
618
- }
619
- return args;
620
- }
621
- }
1
+ /********************************************************************************
2
+ * Copyright (C) 2021 Ericsson and others.
3
+ *
4
+ * This program and the accompanying materials are made available under the
5
+ * terms of the Eclipse Public License v. 2.0 which is available at
6
+ * http://www.eclipse.org/legal/epl-2.0.
7
+ *
8
+ * This Source Code may also be made available under the following Secondary
9
+ * Licenses when the conditions for such availability set forth in the Eclipse
10
+ * Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ * with the GNU Classpath Exception which is available at
12
+ * https://www.gnu.org/software/classpath/license.html.
13
+ *
14
+ * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15
+ ********************************************************************************/
16
+
17
+ import { ContextMenuRenderer, ReactWidget, Widget } from '@theia/core/lib/browser';
18
+ import { ThemeService } from '@theia/core/lib/browser/theming';
19
+ import { Deferred } from '@theia/core/lib/common/promise-util';
20
+ import { ThemeChangeEvent } from '@theia/core/lib/common/theme';
21
+ import { inject, injectable, postConstruct } from '@theia/core/shared/inversify';
22
+ import * as React from '@theia/core/shared/react';
23
+ import { hexStrToUnsignedLong } from '../../common/util';
24
+ import { MemoryProviderService } from '../memory-provider/memory-provider-service';
25
+ import { EasilyMappedObject, MemoryHoverRendererService } from '../utils/memory-hover-renderer';
26
+ import { MWMoreMemorySelect } from '../utils/memory-widget-components';
27
+ import {
28
+ Constants, Interfaces, Utils
29
+ } from '../utils/memory-widget-utils';
30
+ import { VariableDecoration, VariableFinder } from '../utils/memory-widget-variable-utils';
31
+ import { MemoryOptionsWidget } from './memory-options-widget';
32
+ import debounce = require('@theia/core/shared/lodash.debounce');
33
+
34
+ /* eslint-disable @typescript-eslint/no-explicit-any */
35
+ export namespace MemoryTable {
36
+ export interface WrapperHandlers {
37
+ onKeyDown?: React.KeyboardEventHandler;
38
+ onClick?: React.MouseEventHandler;
39
+ onContextMenu?: React.MouseEventHandler;
40
+ onMouseMove?: React.MouseEventHandler;
41
+ onFocus?(e: React.FocusEvent<HTMLDivElement>): any;
42
+ onBlur?(e: React.FocusEvent<HTMLDivElement>): any;
43
+ }
44
+ /* eslint-enable @typescript-eslint/no-explicit-any */
45
+
46
+ export interface StylableNodeAttributes {
47
+ className?: string;
48
+ style?: React.CSSProperties;
49
+ variable?: VariableDecoration;
50
+ title?: string;
51
+ isHighlighted?: boolean;
52
+ }
53
+
54
+ export interface GroupData {
55
+ node: React.ReactNode;
56
+ ascii: string; index: number;
57
+ variables: VariableDecoration[];
58
+ isHighlighted?: boolean;
59
+ }
60
+ export interface ByteData {
61
+ node: React.ReactNode;
62
+ ascii: string; index: number;
63
+ variables: VariableDecoration[];
64
+ isHighlighted?: boolean;
65
+ }
66
+ export interface ItemData {
67
+ node: React.ReactNode;
68
+ content: string;
69
+ variable?: VariableDecoration;
70
+ index: number;
71
+ isHighlighted?: boolean;
72
+ }
73
+
74
+ export interface RowOptions {
75
+ address: string;
76
+ groups: React.ReactNode;
77
+ ascii?: string;
78
+ variables?: VariableDecoration[];
79
+ doShowDivider?: boolean;
80
+ index: number;
81
+ isHighlighted?: boolean;
82
+ }
83
+
84
+ export const ROW_CLASS = 't-mv-view-row';
85
+ export const ROW_DIVIDER_CLASS = 't-mv-view-row-highlight';
86
+ export const ADDRESS_DATA_CLASS = 't-mv-view-address';
87
+ export const MEMORY_DATA_CLASS = 't-mv-view-data';
88
+ export const EXTRA_COLUMN_DATA_CLASS = 't-mv-view-code';
89
+ export const GROUP_SPAN_CLASS = 'byte-group';
90
+ export const BYTE_SPAN_CLASS = 'single-byte';
91
+ export const EIGHT_BIT_SPAN_CLASS = 'eight-bits';
92
+ export const HEADER_LABEL_CONTAINER_CLASS = 't-mv-header-label-container';
93
+ export const HEADER_LABEL_CLASS = 't-mv-header-label';
94
+ export const VARIABLE_LABEL_CLASS = 't-mv-variable-label';
95
+ export const HEADER_ROW_CLASS = 't-mv-header';
96
+ }
97
+
98
+ @injectable()
99
+ export class MemoryTableWidget extends ReactWidget {
100
+ static CONTEXT_MENU = ['memory.view.context.menu'];
101
+ static ID = 'memory-table-widget';
102
+
103
+ @inject(ThemeService) protected readonly themeService: ThemeService;
104
+ @inject(MemoryOptionsWidget) readonly optionsWidget: MemoryOptionsWidget;
105
+ @inject(MemoryProviderService) protected readonly memoryProvider: MemoryProviderService;
106
+ @inject(MemoryHoverRendererService) protected readonly hoverRenderer: MemoryHoverRendererService;
107
+ @inject(ContextMenuRenderer) protected readonly contextMenuRenderer: ContextMenuRenderer;
108
+
109
+ protected previousBytes: Interfaces.LabeledUint8Array | undefined;
110
+ protected memory: Interfaces.WidgetMemoryState;
111
+ protected options: Interfaces.MemoryOptions;
112
+ protected variableFinder: VariableFinder | undefined;
113
+ protected deferredScrollContainer = new Deferred<HTMLDivElement>();
114
+
115
+ @postConstruct()
116
+ protected async init(): Promise<void> {
117
+ this.id = MemoryTableWidget.ID;
118
+ this.addClass(MemoryTableWidget.ID);
119
+ this.scrollOptions = { ...this.scrollOptions, suppressScrollX: false };
120
+ this.toDispose.push(this.optionsWidget.onOptionsChanged(optionId => this.handleOptionChange(optionId)));
121
+ this.toDispose.push(this.optionsWidget.onMemoryChanged(e => this.handleMemoryChange(e)));
122
+ this.toDispose.push(this.themeService.onDidColorThemeChange(e => this.handleThemeChange(e)));
123
+
124
+ this.getStateAndUpdate();
125
+ }
126
+
127
+ protected handleOptionChange(_id?: string): Promise<void> {
128
+ this.getStateAndUpdate();
129
+ return Promise.resolve();
130
+ }
131
+
132
+ override update(): void {
133
+ super.update();
134
+ this.updateColumnWidths();
135
+ }
136
+
137
+ protected override onResize(msg: Widget.ResizeMessage): void {
138
+ this.updateColumnWidths();
139
+ super.onResize(msg);
140
+ }
141
+
142
+ protected updateColumnWidths = debounce(this.doUpdateColumnWidths.bind(this), Constants.DEBOUNCE_TIME);
143
+
144
+ protected doUpdateColumnWidths(): void {
145
+ setTimeout(() => {
146
+ const firstTR = this.node.querySelector('tr');
147
+ const header = this.node.querySelector(`.${MemoryTable.HEADER_ROW_CLASS}`) as HTMLDivElement;
148
+ if (firstTR && header) {
149
+ const allTDs = Array.from(firstTR.querySelectorAll('td'));
150
+ const allSizes = allTDs.map(td => `minmax(max-content, ${td.clientWidth}px)`);
151
+ header.style.gridTemplateColumns = allSizes.join(' ');
152
+ }
153
+ });
154
+ }
155
+
156
+ protected areSameRegion(a: Interfaces.MemoryReadResult, b: Interfaces.MemoryReadResult): boolean {
157
+ return a.address.equals(b?.address) && a.bytes.length === b?.bytes.length;
158
+ }
159
+
160
+ protected handleMemoryChange(newMemory: Interfaces.MemoryReadResult): void {
161
+ if (this.areSameRegion(this.memory, newMemory)) {
162
+ this.previousBytes = this.memory.bytes;
163
+ } else {
164
+ this.previousBytes = undefined;
165
+ }
166
+ this.getStateAndUpdate();
167
+ }
168
+
169
+ protected handleThemeChange(_themeChange: ThemeChangeEvent): void {
170
+ this.getStateAndUpdate();
171
+ }
172
+
173
+ protected getState(): void {
174
+ this.options = this.optionsWidget.options;
175
+ this.memory = this.optionsWidget.memory;
176
+ const isHighContrast = this.themeService.getCurrentTheme().type === 'hc';
177
+ this.variableFinder = this.optionsWidget.options.columnsDisplayed.variables.doRender
178
+ ? new VariableFinder(this.memory.variables, isHighContrast)
179
+ : undefined;
180
+ }
181
+
182
+ protected getStateAndUpdate(): void {
183
+ this.getState();
184
+ this.update();
185
+ this.scrollIntoViewIfNecessary();
186
+ }
187
+
188
+ protected scrollIntoViewIfNecessary(): Promise<void> {
189
+ return new Promise(resolve => setTimeout(() => {
190
+ this.deferredScrollContainer.promise.then(scrollContainer => {
191
+ const table = scrollContainer.querySelector('table');
192
+ if (table && scrollContainer.scrollTop > table.clientHeight) {
193
+ const valueToGetInWindow = table.clientHeight - this.node.clientHeight;
194
+ const scrollHere = Math.max(valueToGetInWindow, 0);
195
+ scrollContainer.scrollTo(scrollContainer.scrollLeft, scrollHere);
196
+ }
197
+ this.scrollBar?.update();
198
+ resolve();
199
+ });
200
+ }));
201
+ }
202
+
203
+ protected getWrapperHandlers(): MemoryTable.WrapperHandlers {
204
+ return { onMouseMove: this.handleTableMouseMove };
205
+ }
206
+
207
+ protected assignScrollContainerRef = (element: HTMLDivElement): void => {
208
+ this.deferredScrollContainer.resolve(element);
209
+ };
210
+
211
+ override async getScrollContainer(): Promise<HTMLDivElement> {
212
+ return this.deferredScrollContainer.promise;
213
+ }
214
+
215
+ render(): React.ReactNode {
216
+ const rows = this.getTableRows();
217
+ const { onClick, onContextMenu, onFocus, onBlur, onKeyDown, onMouseMove } = this.getWrapperHandlers();
218
+ const headers: Interfaces.ColumnIDs[] = Object.entries(this.options.columnsDisplayed)
219
+ .filter(([, { doRender }]) => doRender)
220
+ .map(([id, { label }]) => ({ label, id }));
221
+
222
+ return (
223
+ <div
224
+ className={this.getWrapperClass()}
225
+ onClick={onClick}
226
+ onContextMenu={onContextMenu}
227
+ onFocus={onFocus}
228
+ onBlur={onBlur}
229
+ onKeyDown={onKeyDown}
230
+ onMouseMove={onMouseMove}
231
+ role='textbox'
232
+ tabIndex={0}
233
+ >
234
+ <div
235
+ className={this.getTableHeaderClass()}
236
+ style={this.getTableHeaderStyle(headers.length)}
237
+ >
238
+ {this.getTableHeaders(headers)}
239
+ </div>
240
+ <div
241
+ className='t-mv-view-container'
242
+ style={{ position: 'relative' }}
243
+ ref={this.assignScrollContainerRef}
244
+ >
245
+ {this.getBeforeTableContent()}
246
+ <table className='t-mv-view'>
247
+ <tbody>
248
+ {rows}
249
+ </tbody>
250
+ </table>
251
+ {this.getAfterTableContent()}
252
+ </div>
253
+ {this.getTableFooter()}
254
+ </div>
255
+ );
256
+ }
257
+
258
+ protected getWrapperClass(): string {
259
+ return `t-mv-memory-container${this.options.isFrozen ? ' frozen' : ''}`;
260
+ }
261
+
262
+ protected getTableHeaderClass(): string {
263
+ return MemoryTable.HEADER_ROW_CLASS + ' no-select';
264
+ }
265
+
266
+ protected getTableHeaderStyle(numLabels: number): React.CSSProperties {
267
+ const safePercentage = Math.floor(100 / numLabels);
268
+ const gridTemplateColumns = ` ${safePercentage}% `.repeat(numLabels);
269
+ return { gridTemplateColumns };
270
+ }
271
+
272
+ protected getTableHeaders(labels: Interfaces.ColumnIDs[]): React.ReactNode {
273
+ return labels.map(label => this.getTableHeader(label));
274
+ }
275
+
276
+ protected getTableHeader({ label, id }: Interfaces.ColumnIDs): React.ReactNode {
277
+ return (
278
+ <div key={id} className={MemoryTable.HEADER_LABEL_CONTAINER_CLASS}>
279
+ <span className='t-mv-header-label'>{label}</span>
280
+ </div>
281
+ );
282
+ }
283
+
284
+ protected getBeforeTableContent(): React.ReactNode {
285
+ return (
286
+ !!this.memory.bytes.length && (<MWMoreMemorySelect
287
+ options={[128, 256, 512]}
288
+ direction='above'
289
+ handler={this.loadMoreMemory}
290
+ />)
291
+ );
292
+ }
293
+
294
+ protected getAfterTableContent(): React.ReactNode {
295
+ return (
296
+ !!this.memory.bytes.length && (<MWMoreMemorySelect
297
+ options={[128, 256, 512]}
298
+ direction='below'
299
+ handler={this.loadMoreMemory}
300
+ />)
301
+ );
302
+ }
303
+
304
+ protected loadMoreMemory = async (options: Interfaces.MoreMemoryOptions): Promise<void> => {
305
+ const { direction, numBytes } = options;
306
+ const { address, offset, length } = this.optionsWidget.options;
307
+ let newOffset = 0;
308
+ const newLength = length + numBytes;
309
+ if (direction === 'above') {
310
+ newOffset = offset - numBytes;
311
+ }
312
+ await this.optionsWidget.setAddressAndGo(`${address}`, newOffset, newLength, direction);
313
+ };
314
+
315
+ protected getTableFooter(): React.ReactNode {
316
+ return undefined;
317
+ }
318
+
319
+ protected getTableRows(): React.ReactNode {
320
+ return [...this.renderRows()];
321
+ }
322
+
323
+ protected *renderRows(iteratee: Interfaces.LabeledUint8Array = this.memory.bytes): IterableIterator<React.ReactNode> {
324
+ const bytesPerRow = this.options.bytesPerGroup * this.options.groupsPerRow;
325
+ let rowsYielded = 0;
326
+ let groups: React.ReactNode[] = [];
327
+ let ascii = '';
328
+ let variables: VariableDecoration[] = [];
329
+ let isRowHighlighted = false;
330
+ for (const { node, index, ascii: groupAscii, variables: groupVariables, isHighlighted = false } of this.renderGroups(iteratee)) {
331
+ groups.push(node);
332
+ ascii += groupAscii;
333
+ variables.push(...groupVariables);
334
+ isRowHighlighted = isRowHighlighted || isHighlighted;
335
+ if (groups.length === this.options.groupsPerRow || index === iteratee.length - 1) {
336
+ const rowAddress = this.memory.address.add(bytesPerRow * rowsYielded);
337
+ const options: MemoryTable.RowOptions = {
338
+ address: `0x${rowAddress.toString(16)}`,
339
+ doShowDivider: (rowsYielded % 4) === 3,
340
+ isHighlighted: isRowHighlighted,
341
+ ascii,
342
+ groups,
343
+ variables,
344
+ index,
345
+ };
346
+ yield this.renderRow(options);
347
+ ascii = '';
348
+ variables = [];
349
+ groups = [];
350
+ rowsYielded += 1;
351
+ isRowHighlighted = false;
352
+ }
353
+ }
354
+ }
355
+
356
+ protected renderRow(
357
+ options: MemoryTable.RowOptions,
358
+ getRowAttributes: Interfaces.RowDecorator = this.getRowAttributes.bind(this),
359
+ ): React.ReactNode {
360
+ const { address, groups } = options;
361
+ const { className, style, title } = getRowAttributes(options);
362
+ return (
363
+ <tr
364
+ // Add a marker to help visual navigation when scrolling
365
+ className={className}
366
+ style={style}
367
+ title={title}
368
+ key={address}
369
+ >
370
+ <td className={MemoryTable.ADDRESS_DATA_CLASS}>{address}</td>
371
+ <td className={MemoryTable.MEMORY_DATA_CLASS}>{groups}</td>
372
+ {this.getExtraColumn(options)}
373
+ </tr>
374
+ );
375
+ }
376
+
377
+ protected getRowAttributes(options: Partial<MemoryTable.RowOptions>): Partial<Interfaces.StylableNodeAttributes> {
378
+ let className = MemoryTable.ROW_CLASS;
379
+ if (options.doShowDivider) {
380
+ className += ` ${MemoryTable.ROW_DIVIDER_CLASS}`;
381
+ }
382
+ return { className };
383
+ }
384
+
385
+ protected getExtraColumn(options: Pick<MemoryTable.RowOptions, 'ascii' | 'variables'>): React.ReactNode {
386
+ const { variables } = options;
387
+ const additionalColumns = [];
388
+ if (this.options.columnsDisplayed.variables.doRender) {
389
+ additionalColumns.push(
390
+ <td className={MemoryTable.EXTRA_COLUMN_DATA_CLASS} key='variables'>
391
+ {!!variables?.length && (
392
+ <span className='variable-container'>
393
+ {variables.map(({ name, color }) => (
394
+ <span
395
+ key={name}
396
+ className={MemoryTable.VARIABLE_LABEL_CLASS}
397
+ style={{ color }}
398
+ >
399
+ {name}
400
+ </span>
401
+ ))}
402
+ </span>
403
+ )}
404
+ </td>,
405
+ );
406
+ }
407
+ if (this.options.columnsDisplayed.ascii.doRender) {
408
+ const asciiColumn = this.options.columnsDisplayed.ascii.doRender && <td className={MemoryTable.EXTRA_COLUMN_DATA_CLASS} key='ascii'>{options.ascii}</td>;
409
+ additionalColumns.push(asciiColumn);
410
+ }
411
+ return additionalColumns;
412
+ }
413
+
414
+ protected *renderGroups(iteratee: Interfaces.LabeledUint8Array = this.memory.bytes): IterableIterator<MemoryTable.GroupData> {
415
+ let bytesInGroup: React.ReactNode[] = [];
416
+ let ascii = '';
417
+ let variables: VariableDecoration[] = [];
418
+ let isGroupHighlighted = false;
419
+ for (const { node, index, ascii: byteAscii, variables: byteVariables, isHighlighted = false } of this.renderBytes(iteratee)) {
420
+ this.buildGroupByEndianness(bytesInGroup, node);
421
+ ascii += byteAscii;
422
+ variables.push(...byteVariables);
423
+ isGroupHighlighted = isGroupHighlighted || isHighlighted;
424
+ if (bytesInGroup.length === this.options.bytesPerGroup || index === iteratee.length - 1) {
425
+ const itemID = this.memory.address.add(index);
426
+ yield {
427
+ node: <span className='byte-group' key={itemID.toString(16)}>{bytesInGroup}</span>,
428
+ ascii,
429
+ index,
430
+ variables,
431
+ isHighlighted: isGroupHighlighted,
432
+ };
433
+ bytesInGroup = [];
434
+ ascii = '';
435
+ variables = [];
436
+ isGroupHighlighted = false;
437
+ }
438
+ }
439
+ }
440
+
441
+ protected buildGroupByEndianness(oldBytes: React.ReactNode[], newByte: React.ReactNode): void {
442
+ if (this.options.endianness === Interfaces.Endianness.Big) {
443
+ oldBytes.push(newByte);
444
+ } else {
445
+ oldBytes.unshift(newByte);
446
+ }
447
+ }
448
+
449
+ protected *renderBytes(iteratee: Interfaces.LabeledUint8Array = this.memory.bytes): IterableIterator<MemoryTable.ByteData> {
450
+ const itemsPerByte = this.options.byteSize / 8;
451
+ let currentByte = 0;
452
+ let chunksInByte: React.ReactNode[] = [];
453
+ let variables: VariableDecoration[] = [];
454
+ let isByteHighlighted = false;
455
+ for (const { node, content, index, variable, isHighlighted = false } of this.renderArrayItems(iteratee)) {
456
+ chunksInByte.push(node);
457
+ const numericalValue = parseInt(content, 16);
458
+ currentByte = (currentByte << 8) + numericalValue;
459
+ isByteHighlighted = isByteHighlighted || isHighlighted;
460
+ if (variable?.firstAppearance) {
461
+ variables.push(variable);
462
+ }
463
+ if (chunksInByte.length === itemsPerByte || index === iteratee.length - 1) {
464
+ const itemID = this.memory.address.add(index);
465
+ const ascii = this.getASCIIForSingleByte(currentByte);
466
+ yield {
467
+ node: <span className='single-byte' key={itemID.toString(16)}>{chunksInByte}</span>,
468
+ ascii,
469
+ index,
470
+ variables,
471
+ isHighlighted: isByteHighlighted,
472
+ };
473
+ currentByte = 0;
474
+ chunksInByte = [];
475
+ variables = [];
476
+ isByteHighlighted = false;
477
+ }
478
+ }
479
+ }
480
+
481
+ protected getASCIIForSingleByte(byte: number | undefined): string {
482
+ return typeof byte === 'undefined'
483
+ ? ' ' : Utils.isPrintableAsAscii(byte) ? String.fromCharCode(byte) : '.';
484
+ }
485
+
486
+ protected *renderArrayItems(
487
+ iteratee: Interfaces.LabeledUint8Array = this.memory.bytes,
488
+ getBitAttributes: Interfaces.BitDecorator = this.getBitAttributes.bind(this),
489
+ ): IterableIterator<MemoryTable.ItemData> {
490
+ const { address } = this.memory;
491
+
492
+ for (let i = 0; i < iteratee.length; i += 1) {
493
+ const itemID = address.add(i).toString(16);
494
+ const { content = '', className, style, variable, title, isHighlighted } = getBitAttributes(i, iteratee);
495
+ const node = (
496
+ <span
497
+ style={style}
498
+ key={itemID}
499
+ className={className}
500
+ data-id={itemID}
501
+ title={title}
502
+ >
503
+ {content}
504
+ </span>
505
+ );
506
+ yield {
507
+ node,
508
+ content,
509
+ index: i,
510
+ variable,
511
+ isHighlighted,
512
+ };
513
+ }
514
+ }
515
+
516
+ protected getBitAttributes(arrayOffset: number, iteratee: Interfaces.LabeledUint8Array): Partial<Interfaces.FullNodeAttributes> {
517
+ const itemAddress = this.memory.address.add(arrayOffset * 8 / this.options.byteSize);
518
+ const classNames = [MemoryTable.EIGHT_BIT_SPAN_CLASS];
519
+ const isChanged = this.previousBytes && iteratee[arrayOffset] !== this.previousBytes[arrayOffset];
520
+ const variable = this.variableFinder?.getVariableForAddress(itemAddress);
521
+ if (!this.options.isFrozen) {
522
+ if (isChanged) {
523
+ classNames.push('changed');
524
+ }
525
+ }
526
+ return {
527
+ className: classNames.join(' '),
528
+ variable,
529
+ style: { color: variable?.color },
530
+ content: iteratee[arrayOffset].toString(16).padStart(2, '0')
531
+ };
532
+ }
533
+
534
+ protected handleTableMouseMove = (e: React.MouseEvent): void => {
535
+ const { target } = e; // react events can't be put into the debouncer
536
+ this.debounceHandleMouseTableMove(target);
537
+ };
538
+
539
+ protected debounceHandleMouseTableMove = debounce(this.doHandleTableMouseMove.bind(this), Constants.DEBOUNCE_TIME, { trailing: true });
540
+
541
+ protected doHandleTableMouseMove(targetSpan: React.MouseEvent['target']): void {
542
+ const target = targetSpan instanceof HTMLElement && targetSpan;
543
+ if (target) {
544
+ const { x, y } = target.getBoundingClientRect();
545
+ const anchor = { x: Math.round(x), y: Math.round(y + target.clientHeight) };
546
+ if (target.classList.contains(MemoryTable.EIGHT_BIT_SPAN_CLASS)) {
547
+ const properties = this.getHoverForChunk(target);
548
+ this.hoverRenderer.render(this.node, anchor, properties);
549
+ } else if (target.classList.contains(MemoryTable.VARIABLE_LABEL_CLASS)) {
550
+ const properties = this.getHoverForVariable(target);
551
+ this.hoverRenderer.render(this.node, anchor, properties);
552
+ } else {
553
+ this.hoverRenderer.hide();
554
+ }
555
+ } else {
556
+ this.hoverRenderer.hide();
557
+ }
558
+ }
559
+
560
+ protected getHoverForChunk(span: HTMLElement): EasilyMappedObject | undefined {
561
+ if (span.classList.contains(MemoryTable.EIGHT_BIT_SPAN_CLASS)) {
562
+ const parentByteContainer = span.parentElement;
563
+ if (parentByteContainer?.textContent) {
564
+ const hex = parentByteContainer.textContent ?? '';
565
+ const decimal = parseInt(hex, 16);
566
+ const binary = this.getPaddedBinary(decimal);
567
+ const UTF8 = String.fromCodePoint(decimal);
568
+ return { hex, binary, decimal, UTF8 };
569
+ }
570
+ }
571
+ return undefined;
572
+ }
573
+
574
+ protected getPaddedBinary(decimal: number): string {
575
+ const paddedBinary = decimal.toString(2).padStart(this.options.byteSize, '0');
576
+ let paddedAndSpacedBinary = '';
577
+ for (let i = 8; i <= paddedBinary.length; i += 8) {
578
+ paddedAndSpacedBinary += ` ${paddedBinary.slice(i - 8, i)}`;
579
+ }
580
+ return paddedAndSpacedBinary.trim();
581
+ }
582
+
583
+ protected getHoverForVariable(span: HTMLElement): EasilyMappedObject | undefined {
584
+ const variable = this.variableFinder?.searchForVariable(span.textContent ?? '');
585
+ if (variable?.type) {
586
+ return { type: variable.type };
587
+ }
588
+ return undefined;
589
+ }
590
+
591
+ protected handleTableRightClick = (e: React.MouseEvent): void => this.doHandleTableRightClick(e);
592
+
593
+ protected doHandleTableRightClick(event: React.MouseEvent): void {
594
+ event.preventDefault();
595
+ const target = event.target as HTMLElement;
596
+ if (target.classList?.contains('eight-bits')) {
597
+ const { right, top } = target.getBoundingClientRect();
598
+ this.update();
599
+ event.stopPropagation();
600
+ this.contextMenuRenderer.render({
601
+ menuPath: MemoryTableWidget.CONTEXT_MENU,
602
+ anchor: { x: right, y: top },
603
+ args: this.getContextMenuArgs(event),
604
+ });
605
+ }
606
+ }
607
+
608
+ protected getContextMenuArgs(event: React.MouseEvent): unknown[] {
609
+ const args: unknown[] = [this];
610
+ const id = (event.target as HTMLElement).getAttribute('data-id');
611
+ if (id) {
612
+ const location = hexStrToUnsignedLong(id);
613
+ args.push(location);
614
+ const offset = this.memory.address.multiply(-1).add(location);
615
+ const cellAddress = this.memory.address.add(offset.multiply(8 / this.options.byteSize));
616
+ const variableAtLocation = this.variableFinder?.searchForVariable(cellAddress);
617
+ args.push(variableAtLocation);
618
+ }
619
+ return args;
620
+ }
621
+ }