Skip to content

Commit 1aef8d0

Browse files
committed
fix(wasm): fix WASM file handling and configuration generation
- make #generateWasmConfig private method and clean up wasm URL correctly.
1 parent 499a060 commit 1aef8d0

File tree

1 file changed

+55
-45
lines changed

1 file changed

+55
-45
lines changed

src/wasmLoader.js

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import untar from "js-untar";
22

33
const LOADED_URLS = [];
44
const PROMISES = {};
5-
let WASM_FILE_OBJECT = null;
65

76
/**
87
* Create a future that returns
@@ -35,34 +34,6 @@ function isSameConfig(a, b) {
3534
return a.rendering === b.rendering && a.exec === b.exec;
3635
}
3736

38-
/**
39-
* Generate a WebAssembly configuration object from a wasmLoader config.
40-
* @param {*} config - wasmLoader config with 'rendering' and 'exec' keys.
41-
* @returns wasmConfig object
42-
*/
43-
export function generateWasmConfig(config) {
44-
let wasmConfig = {}
45-
if (WASM_FILE_OBJECT) {
46-
wasmConfig.locateFile = (fileName) => {
47-
if (WASM_FILE_OBJECT && fileName == WASM_FILE_OBJECT.name) {
48-
return URL.createObjectURL(WASM_FILE_OBJECT);
49-
}
50-
return new URL(fileName, import.meta.url).href;
51-
};
52-
wasmConfig.onRuntimeInitialized = () => {
53-
// Free the object URL after runtime is initialized
54-
URL.revokeObjectURL(WASM_FILE_OBJECT);
55-
WASM_FILE_OBJECT = null;
56-
};
57-
}
58-
if (config?.rendering === "webgpu") {
59-
wasmConfig.preRun = [(module) => {
60-
module.ENV.VTK_GRAPHICS_BACKEND = "WEBGPU";
61-
}];
62-
}
63-
return wasmConfig;
64-
}
65-
6637
/**
6738
* Add script tag with provided URL with type="module"
6839
*
@@ -98,12 +69,14 @@ export function loadScriptAsModule(url) {
9869
* @property {Boolean} loaded
9970
*/
10071
export class VtkWASMLoader {
72+
#wasm;
73+
#wasmFile;
10174
constructor() {
10275
this.loaded = false;
10376
this.loadingPending = null;
104-
this.wasm = null;
10577
this.config = {};
106-
this.runtimes = [];
78+
this.#wasm = { url: null, instance: null };
79+
this.#wasmFile = null;
10780
}
10881

10982
/**
@@ -178,8 +151,7 @@ export class VtkWASMLoader {
178151
if (file.name === `${wasmBaseName}WebAssembly${this.config?.exec === "async" ? "Async" : ""}.mjs`) {
179152
jsModuleURL = URL.createObjectURL(new File([file.buffer], file.name, { type: "text/javascript" }));
180153
} else if (file.name === `${wasmBaseName}WebAssembly${this.config?.exec === "async" ? "Async" : ""}.wasm`) {
181-
// Create a file URL for the wasm file so it can be loaded
182-
WASM_FILE_OBJECT = new File([file.buffer], file.name, { type: "application/wasm" });
154+
this.#wasmFile = file;
183155
}
184156
});
185157
} else {
@@ -225,7 +197,7 @@ export class VtkWASMLoader {
225197

226198
// Load WASM
227199
if (window.createVTKWASM) {
228-
this.wasm = await window.createVTKWASM(generateWasmConfig(this.config));
200+
this.#wasm.instance = await window.createVTKWASM(this.#generateWasmConfig(this.config));
229201
}
230202

231203
// Capture objects
@@ -242,26 +214,32 @@ export class VtkWASMLoader {
242214
* @returns
243215
*/
244216
async createRemoteSession(config) {
245-
if (this.wasm) {
217+
if (this.#wasm.instance) {
246218
// New API
247-
if (this.wasm?.isAsync && this.wasm.isAsync()) {
219+
if (this.#wasm.instance?.isAsync && this.#wasm.instance.isAsync()) {
248220
if (!config || isSameConfig(this.config, config)) {
249221
// Reuse the same runtime
250222
console.log("(Main runtime in async)");
251-
return new this.wasm.vtkRemoteSession();
223+
return new this.#wasm.instance.vtkRemoteSession();
252224
} else {
253225
console.log("(New in async)");
254226
const newWASMRuntime = await window.createVTKWASM(
255-
generateWasmConfig(config || this.config),
227+
this.#generateWasmConfig(config || this.config),
256228
);
257229
return new newWASMRuntime.vtkRemoteSession();
258230
}
259231
} else {
260232
console.log("(New in sync)");
261-
const newWASMRuntime = await window.createVTKWASM(
262-
generateWasmConfig(config || this.config),
263-
);
264-
return new newWASMRuntime.vtkRemoteSession();
233+
if (!config || isSameConfig(this.config, config)) {
234+
// Reuse the same runtime
235+
return new this.#wasm.instance.vtkRemoteSession();
236+
}
237+
else {
238+
const newWASMRuntime = await window.createVTKWASM(
239+
this.#generateWasmConfig(config || this.config),
240+
);
241+
return new newWASMRuntime.vtkRemoteSession();
242+
}
265243
}
266244
}
267245

@@ -277,17 +255,49 @@ export class VtkWASMLoader {
277255
* @returns
278256
*/
279257
createStandaloneSession() {
280-
if (!this.wasm) {
258+
if (!this.#wasm.instance) {
281259
throw new Error("Current WASM version does not support standalone mode");
282260
}
283-
return new this.wasm.vtkStandaloneSession();
261+
return new this.#wasm.instance.vtkStandaloneSession();
284262
}
285263

286264
/** Helper for handling API change */
287265
createStateDecorator() {
288-
if (this.wasm) {
266+
if (this.#wasm.instance) {
289267
return convertToObj;
290268
}
291269
return convertToStr;
292270
}
271+
272+
/**
273+
* Generate a WebAssembly configuration object from a wasmLoader config.
274+
* @param {*} config - wasmLoader config with 'rendering' and 'exec' keys.
275+
* @returns wasmConfig object
276+
*/
277+
#generateWasmConfig(config) {
278+
let wasmConfig = {}
279+
if (this.#wasmFile !== null) {
280+
wasmConfig.locateFile = (fileName) => {
281+
if (this.#wasmFile === null) {
282+
throw new Error("WASM file unexpectedly transitioned to null");
283+
}
284+
if (fileName == this.#wasmFile.name) {
285+
this.#wasm.url = URL.createObjectURL(new File([this.#wasmFile.buffer], this.#wasmFile.name, { type: "application/wasm" }));
286+
return this.#wasm.url;
287+
}
288+
return new URL(fileName, import.meta.url).href;
289+
};
290+
wasmConfig.onRuntimeInitialized = () => {
291+
// Free the object URL after runtime is initialized
292+
URL.revokeObjectURL(this.#wasm.url);
293+
this.#wasm.url = null;
294+
};
295+
}
296+
if (config?.rendering === "webgpu") {
297+
wasmConfig.preRun = [(module) => {
298+
module.ENV.VTK_GRAPHICS_BACKEND = "WEBGPU";
299+
}];
300+
}
301+
return wasmConfig;
302+
}
293303
}

0 commit comments

Comments
 (0)