-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcomponent-viewer-instance.ts
More file actions
executable file
·199 lines (167 loc) · 6.91 KB
/
component-viewer-instance.ts
File metadata and controls
executable file
·199 lines (167 loc) · 6.91 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
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
* Copyright 2026 Arm Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as vscode from 'vscode';
import { URI } from 'vscode-uri';
import { parseStringPromise, ParserOptions } from 'xml2js';
import { Json } from './model/scvd-base';
import { Resolver } from './resolver';
import { ScvdComponentViewer } from './model/scvd-component-viewer';
import { ScvdBase } from './model/scvd-base';
import { StatementEngine } from './statement-engine/statement-engine';
import { ScvdEvalContext } from './scvd-eval-context';
import { GDBTargetDebugSession, GDBTargetDebugTracker } from '../../debug-session';
import { ScvdGuiTree } from './scvd-gui-tree';
const xmlOpts: ParserOptions = {
explicitArray: false,
mergeAttrs: true,
explicitRoot: true,
trim: true,
// Add child objects that carry their original tag name via '#name'
explicitChildren: true,
preserveChildrenOrder: true
};
export class ComponentViewerInstance {
private _model: ScvdComponentViewer | undefined;
private _memUsageStart: number = 0;
private _memUsageLast: number = 0;
private _timeUsageLast: number = 0;
private _statementEngine: StatementEngine | undefined;
private _guiTree: ScvdGuiTree | undefined;
public constructor(
) {
}
private injectLineNumbers(xml: string): string {
const lines = xml.split(/\r?\n/);
const result: string[] = [];
for (const [idx, line] of lines.entries()) {
result.push(line.replace(
/<((?!\/|!|\?)([A-Za-z_][A-Za-z0-9._:-]*))/g,
`<$1 __line="${idx + 1}"`
));
}
return result.join('\n');
}
public getGuiTree(): ScvdGuiTree[] | undefined {
return this._guiTree?.children;
}
public getStats(text: string): string {
const mem = process.memoryUsage();
const memCurrent = Math.round(mem.heapUsed / 1024 / 1024);
const timeCurrent = Date.now();
if (this._timeUsageLast === 0) {
this._timeUsageLast = timeCurrent;
}
if (this._memUsageStart === 0) {
this._memUsageStart = memCurrent;
this._memUsageLast = memCurrent;
}
const memUsage = memCurrent - this._memUsageLast;
const timeUsage = timeCurrent - this._timeUsageLast;
const memIncrease = memCurrent - this._memUsageStart;
this._memUsageLast = memCurrent;
this._timeUsageLast = timeCurrent;
return `${text}, Time: ${timeUsage} ms, Mem: ${memUsage}, Mem Increase: ${memIncrease} MB, (Total: ${memCurrent} MB)`;
}
public async readModel(filename: URI, debugSession: GDBTargetDebugSession, debugTracker: GDBTargetDebugTracker): Promise<void> {
const stats: string[] = [];
stats.push(this.getStats(` Start reading SCVD file ${filename}`));
const buf = (await this.readFileToBuffer(filename)).toString('utf-8');
stats.push(this.getStats(' read'));
const bufLineNo = this.injectLineNumbers(buf);
stats.push(this.getStats(' inject'));
const xml: Json = await this.parseXml(bufLineNo);
stats.push(this.getStats(' parse'));
if (xml === undefined) {
console.error('Failed to parse SCVD XML');
return;
}
ScvdBase.resetIds();
this.model = new ScvdComponentViewer(undefined);
if (!this.model) {
console.error('Failed to create SCVD model');
return;
}
this.model.readXml(xml);
stats.push(this.getStats(' model.readXml'));
const scvdEvalContext = new ScvdEvalContext(this.model);
scvdEvalContext.init(debugSession, debugTracker);
stats.push(this.getStats(' evalContext.init'));
const executionContext = scvdEvalContext.getExecutionContext();
this.model.setExecutionContextAll(executionContext);
stats.push(this.getStats(' model.setExecutionContextAll'));
this.model.configureAll();
stats.push(this.getStats(' model.configureAll'));
this.model.validateAll(true);
stats.push(this.getStats(' model.validateAll'));
const resolver = new Resolver(this.model);
resolver.resolve();
stats.push(this.getStats(' resolver.resolve'));
await this.model.calculateTypedefs();
stats.push(this.getStats(' model.calculateTypedefs'));
this.statementEngine = new StatementEngine(this.model, executionContext);
this.statementEngine.initialize();
stats.push(this.getStats(' statementEngine.initialize'));
this._guiTree = new ScvdGuiTree(undefined, 'component-viewer-root');
console.log('ComponentViewerInstance readModel stats:\n' + stats.join('\n '));
}
public async update(): Promise<void> {
const stats: string[] = [];
stats.push(this.getStats(' start'));
if (this._statementEngine === undefined || this._guiTree === undefined) {
return;
}
const epoch = this._guiTree.beginUpdate();
await this._statementEngine.executeAll(this._guiTree);
this._guiTree.finalizeUpdate(epoch);
stats.push(this.getStats('end'));
console.log('ComponentViewerInstance update stats:\n' + stats.join('\n '));
}
private async readFileToBuffer(filePath: URI): Promise<Buffer> {
try {
const buffer = await vscode.workspace.fs.readFile(filePath);
return Buffer.from(buffer);
} catch (error) {
console.error('Error reading file:', error);
throw error;
}
}
private async parseXml(text: string) {
try {
const json = await parseStringPromise(text, xmlOpts);
//console.log(JSON.stringify(json, null, 2));
return json;
} catch (err) {
console.error('Error parsing XML:', err);
}
}
public get model(): ScvdComponentViewer | undefined {
return this._model;
}
private set model(value: ScvdComponentViewer | undefined) {
this._model = value;
}
public get statementEngine(): StatementEngine | undefined {
return this._statementEngine;
}
private set statementEngine(value: StatementEngine | undefined) {
this._statementEngine = value;
}
public async executeStatements(guiTree: ScvdGuiTree): Promise<void> {
if (this._statementEngine !== undefined) {
await this._statementEngine.executeAll(guiTree);
}
}
}