summaryrefslogtreecommitdiffstats
path: root/util/wasm/batchedtestrunner/qwasmjsruntime.js
blob: 3f2d421181152be9564b6a90ceb856c54ea44173 (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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

// Exposes platform capabilities as static properties

export class AbortedError extends Error {
    constructor(stdout) {
        super(`The program has been aborted`)

        this.stdout = stdout;
    }
}
export class Platform {
    static #webAssemblySupported = typeof WebAssembly !== 'undefined';

    static #canCompileStreaming = WebAssembly.compileStreaming !== 'undefined';

    static #webGLSupported = (() => {
        // We expect that WebGL is supported if WebAssembly is; however
        // the GPU may be blacklisted.
        try {
            const canvas = document.createElement('canvas');
            return !!(
                window.WebGLRenderingContext &&
                (canvas.getContext('webgl') || canvas.getContext('experimental-webgl'))
            );
        } catch (e) {
            return false;
        }
    })();

    static #canLoadQt = Platform.#webAssemblySupported && Platform.#webGLSupported;

    static get webAssemblySupported() {
        return this.#webAssemblySupported;
    }
    static get canCompileStreaming() {
        return this.#canCompileStreaming;
    }
    static get webGLSupported() {
        return this.#webGLSupported;
    }
    static get canLoadQt() {
        return this.#canLoadQt;
    }
}

// Locates a resource, based on its relative path
export class ResourceLocator {
    #rootPath;

    constructor(rootPath) {
        this.#rootPath = rootPath;
        if (rootPath.length > 0 && !rootPath.endsWith('/')) rootPath += '/';
    }

    locate(relativePath) {
        return this.#rootPath + relativePath;
    }
}

// Allows fetching of resources, such as text resources or wasm modules.
export class ResourceFetcher {
    #locator;

    constructor(locator) {
        this.#locator = locator;
    }

    async fetchText(filePath) {
        return (await this.#fetchRawResource(filePath)).text();
    }

    async fetchCompileWasm(filePath, onFetched) {
        const fetchResponse = await this.#fetchRawResource(filePath);
        onFetched?.();

        if (Platform.canCompileStreaming) {
            try {
                return await WebAssembly.compileStreaming(fetchResponse);
            } catch {
                // NOOP - fallback to sequential fetching below
            }
        }
        return WebAssembly.compile(await fetchResponse.arrayBuffer());
    }

    async #fetchRawResource(filePath) {
        const response = await fetch(this.#locator.locate(filePath));
        if (!response.ok)
            throw new Error(
                `${response.status} ${response.statusText} ${response.url}`
            );
        return response;
    }
}

// Represents a WASM module, wrapping the instantiation and execution thereof.
export class CompiledModule {
    #createQtAppInstanceFn;
    #js;
    #wasm;
    #resourceLocator;

    constructor(createQtAppInstanceFn, js, wasm, resourceLocator) {
        this.#createQtAppInstanceFn = createQtAppInstanceFn;
        this.#js = js;
        this.#wasm = wasm;
        this.#resourceLocator = resourceLocator;
    }

    static make(js, wasm, entryFunctionName, resourceLocator)
    {
        const exports = {};
        const module = {};
        eval(js);
        if (!module.exports) {
            throw new Error(
                '${entryFunctionName} has not been exported by the main script'
            );
        }

        return new CompiledModule(
            module.exports, js, wasm, resourceLocator
        );
    }

    async exec(parameters) {
        return await new Promise(async (resolve, reject) => {
            let instance = undefined;
            let result = undefined;

            let testFinished = false;
            const testFinishedEvent = new CustomEvent('testFinished');
            instance = await this.#createQtAppInstanceFn((() => {
                const params = this.#makeDefaultExecParams({
                    onInstantiationError: (error) => { reject(error); },
                });
                params.arguments = parameters?.args;
                let data = '';
                params.print = (out) => {
                    parameters?.onStdout?.(out);
                    data += `${out}\n`;
                };
                params.printErr = () => { };
                params.onAbort = () => reject(new AbortedError(data));
                params.quit = (code, exception) => {
                    if (exception && exception.name !== 'ExitStatus')
                        reject(exception);
                };
                params.notifyTestFinished = (code) => {
                    result = { stdout: data, exitCode: code };
                    testFinished = true;
                    window.dispatchEvent(testFinishedEvent);
                };
                return params;
            })());
            if (!testFinished) {
                await new Promise((resolve) => {
                    window.addEventListener('testFinished', () => {
                        resolve();
                    });
                });
            }
            resolve({
                stdout: result.stdout,
                exitCode: result.exitCode,
                instance,
            });
        });
    }

    #makeDefaultExecParams(params) {
        const instanceParams = {};
        instanceParams.instantiateWasm = async (imports, onDone) => {
            try {
                onDone(await WebAssembly.instantiate(this.#wasm, imports), this.#wasm);
            } catch (e) {
                params?.onInstantiationError?.(e);
            }
        };
        instanceParams.locateFile = (filename) =>
            this.#resourceLocator.locate(filename);
        instanceParams.monitorRunDependencies = (name) => { };
        instanceParams.print = (text) => true && console.log(text);
        instanceParams.printErr = (text) => true && console.warn(text);

        instanceParams.mainScriptUrlOrBlob = new Blob([this.#js], {
            type: 'text/javascript',
        });
        return instanceParams;
    }
}

// Streamlines loading of WASM modules.
export class ModuleLoader {
    #fetcher;
    #resourceLocator;

    constructor(
        fetcher,
        resourceLocator
    ) {
        this.#fetcher = fetcher;
        this.#resourceLocator = resourceLocator;
    }

    // Loads an emscripten module named |moduleName| from the main resource path. Provides
    // progress of 'downloading' and 'compiling' to the caller using the |onProgress| callback.
    async loadEmscriptenModule(
        moduleName, onProgress
    ) {
        if (!Platform.webAssemblySupported)
            throw new Error('Web assembly not supported');
        if (!Platform.webGLSupported)
            throw new Error('WebGL is not supported');

        onProgress('downloading');

        const jsLoadPromise = this.#fetcher.fetchText(`${moduleName}.js`);
        const wasmLoadPromise = this.#fetcher.fetchCompileWasm(
            `${moduleName}.wasm`,
            () => {
                onProgress('compiling');
            }
        );

        const [js, wasm] = await Promise.all([jsLoadPromise, wasmLoadPromise]);
        return CompiledModule.make(js, wasm, `${moduleName}_entry`, this.#resourceLocator);
    }
}