summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/test-tools/wamr-ide/VSCode-Extension/src/view/TargetConfigPanel.ts
blob: f2e1343a590c65059575381745d2b8e0f7588570 (plain)
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
/*
 * Copyright (C) 2019 Intel Corporation.  All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs';
import { readFromConfigFile, writeIntoConfigFile } from '../extension';
import { getUri } from '../utilities/getUri';

export class TargetConfigPanel {
    public static currentPanel: TargetConfigPanel | undefined;
    private readonly viewPanel: vscode.WebviewPanel;

    private _disposables: vscode.Disposable[] = [];
    public static buildArgs = {
        outputFileName: 'main.wasm',
        initMemorySize: '131072',
        maxMemorySize: '131072',
        stackSize: '4096',
        exportedSymbols: 'main',
    };

    private static readonly userInputError: number = -2;
    private static readonly executionSuccess: number = 0;

    /**
     *
     * @param context extension context from extension.ts active func
     * @param panelName
     */
    constructor(panel: vscode.WebviewPanel, extensionUri: vscode.Uri) {
        this.viewPanel = panel;
        this.viewPanel.webview.html = this._getHtmlForWebview(
            this.viewPanel.webview,
            extensionUri,
            'resource/webview/page/configBuildTarget.html'
        );
        this.viewPanel.onDidDispose(this.dispose, null, this._disposables);
        this._setWebviewMessageListener(this.viewPanel.webview);
    }

    /**
     *
     * @param context
     */
    public static render(context: vscode.ExtensionContext): void {
        /* check if current panel is initialized */
        if (TargetConfigPanel.currentPanel) {
            TargetConfigPanel.currentPanel.viewPanel.reveal(
                vscode.ViewColumn.One
            );
        } else {
            const panel = vscode.window.createWebviewPanel(
                'targetConfig',
                'Config building target',
                vscode.ViewColumn.One,
                {
                    enableScripts: true,
                    retainContextWhenHidden: true,
                }
            );

            TargetConfigPanel.currentPanel = new TargetConfigPanel(
                panel,
                context.extensionUri
            );
        }
    }

    private configBuildArgs(
        outputFileName: string,
        initMemSize: string,
        maxMemSize: string,
        stackSize: string,
        exportedSymbols: string
    ): number {
        if (
            outputFileName === '' ||
            initMemSize === '' ||
            maxMemSize === '' ||
            stackSize === '' ||
            exportedSymbols === ''
        ) {
            return TargetConfigPanel.userInputError;
        }

        let includePathArr = [];
        let excludeFileArr = [];

        const configObj = {
            outputFileName: outputFileName,
            initMemorySize: initMemSize,
            maxMemorySize: maxMemSize,
            stackSize: stackSize,
            exportedSymbols: exportedSymbols,
        };
        const configStr = readFromConfigFile();

        TargetConfigPanel.buildArgs = configObj;

        if (configStr !== '' && configStr !== undefined) {
            const configJson = JSON.parse(configStr);
            includePathArr =
                configJson['includePaths'] === undefined
                    ? []
                    : configJson['includePaths'];
            excludeFileArr =
                configJson['excludeFiles'] === undefined
                    ? []
                    : configJson['excludeFiles'];
        }

        writeIntoConfigFile(
            includePathArr,
            excludeFileArr,
            TargetConfigPanel.buildArgs
        );

        return TargetConfigPanel.executionSuccess;
    }

    private _getHtmlForWebview(
        webview: vscode.Webview,
        extensionUri: vscode.Uri,
        templatePath: string
    ) {
        /* get toolkit uri */
        const toolkitUri = getUri(webview, extensionUri, [
            'node_modules',
            '@vscode',
            'webview-ui-toolkit',
            'dist',
            'toolkit.js',
        ]);

        const styleUri = getUri(webview, extensionUri, [
            'resource',
            'webview',
            'css',
            'style.css',
        ]);

        const mainUri = getUri(webview, extensionUri, [
            'resource',
            'webview',
            'js',
            'configbuildtarget.js',
        ]);

        const resourcePath = path.join(extensionUri.fsPath, templatePath);
        let html = fs.readFileSync(resourcePath, 'utf-8');
        html = html
            .replace(/(\${toolkitUri})/, toolkitUri.toString())
            .replace(/(\${mainUri})/, mainUri.toString())
            .replace(/(\${styleUri})/, styleUri.toString())
            .replace(
                /(\${output_file_val})/,
                TargetConfigPanel.buildArgs.outputFileName
            )
            .replace(
                /(\${initial_mem_size_val})/,
                TargetConfigPanel.buildArgs.initMemorySize
            )
            .replace(
                /(\${max_mem_size_val})/,
                TargetConfigPanel.buildArgs.maxMemorySize
            )
            .replace(
                /(\${stack_size_val})/,
                TargetConfigPanel.buildArgs.stackSize
            )
            .replace(
                /(\${exported_symbols_val})/,
                TargetConfigPanel.buildArgs.exportedSymbols
            );

        return html;
    }

    private _setWebviewMessageListener(webview: vscode.Webview) {
        webview.onDidReceiveMessage(
            message => {
                switch (message.command) {
                    case 'config_build_target':
                        if (
                            message.outputFileName === '' ||
                            message.initMemSize === '' ||
                            message.maxMemSize === '' ||
                            message.stackSize === '' ||
                            message.exportedSymbols === ''
                        ) {
                            vscode.window.showErrorMessage(
                                'Please fill chart before your submit!'
                            );
                            return;
                        } else if (
                            this.configBuildArgs(
                                message.outputFileName,
                                message.initMemSize,
                                message.maxMemSize,
                                message.stackSize,
                                message.exportedSymbols
                            ) === TargetConfigPanel.executionSuccess
                        ) {
                            vscode.window
                                .showInformationMessage(
                                    'Configurations have been saved!',
                                    'OK'
                                )
                                .then(() => {
                                    this.viewPanel.dispose();
                                    return;
                                });
                        }

                    default:
                        break;
                }
            },
            undefined,
            this._disposables
        );
    }

    private dispose() {
        TargetConfigPanel.currentPanel = undefined;
        this.viewPanel.dispose();

        while (this._disposables.length) {
            const disposable = this._disposables.pop();
            if (disposable) {
                disposable.dispose();
            }
        }
    }
}